Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ShaderProgram.h
Go to the documentation of this file.
1/* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM
19#define INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM
20
21#include "lib/ogl.h"
23#include "ps/containers/Span.h"
24#include "ps/CStrForward.h"
28
29#include <map>
30#include <vector>
31
32struct CColor;
33class CMatrix3D;
34class CVector3D;
35class CShaderDefines;
36class CStrIntern;
37
38namespace Renderer
39{
40
41namespace Backend
42{
43
44namespace GL
45{
46
47class CDevice;
48
50{
51public:
53 : m_Device(device), m_Attributes(attributes.begin(), attributes.end())
54 {
55 for (const SVertexAttributeFormat& attribute : m_Attributes)
56 {
57 ENSURE(attribute.format != Format::UNDEFINED);
58 ENSURE(attribute.stride > 0);
59 }
60 }
61
62 ~CVertexInputLayout() override = default;
63
64 IDevice* GetDevice() override;
65
66 const std::vector<SVertexAttributeFormat>& GetAttributes() const noexcept { return m_Attributes; }
67
68private:
69 CDevice* m_Device = nullptr;
70
71 std::vector<SVertexAttributeFormat> m_Attributes;
72};
73
74/**
75 * A compiled vertex+fragment shader program.
76 * The implementation may use GL_ARB_{vertex,fragment}_program (ARB assembly syntax)
77 * or GL_ARB_{vertex,fragment}_shader (GLSL), or may use hard-coded fixed-function
78 * multitexturing setup code; the difference is hidden from the caller.
79 *
80 * Texture/uniform IDs are typically strings, corresponding to the names defined in
81 * the shader .xml file. Alternatively (and more efficiently, if used very frequently),
82 * call GetBindingSlot and pass its return value as the ID.
83 * Setting uniforms that the shader .xml doesn't support is harmless.
84 *
85 * For a high-level overview of shaders and materials, see
86 * http://trac.wildfiregames.com/wiki/MaterialSystem
87 */
89{
91
92public:
94
95 static std::unique_ptr<CShaderProgram> Create(
96 CDevice* device, const CStr& name, const CShaderDefines& baseDefines);
97
98 ~CShaderProgram() override;
99
100 /**
101 * Binds the shader into the GL context. Call this before calling Uniform()
102 * or trying to render with it.
103 */
104 virtual void Bind(CShaderProgram* previousShaderProgram) = 0;
105
106 /**
107 * Unbinds the shader from the GL context. Call this after rendering with it.
108 */
109 virtual void Unbind() = 0;
110
112 {
113 GLenum type;
114 GLenum target;
115 GLint unit;
116 };
117 virtual TextureUnit GetTextureUnit(const int32_t bindingSlot) = 0;
118
119 virtual void SetUniform(
120 const int32_t bindingSlot,
121 const float value) = 0;
122 virtual void SetUniform(
123 const int32_t bindingSlot,
124 const float valueX, const float valueY) = 0;
125 virtual void SetUniform(
126 const int32_t bindingSlot,
127 const float valueX, const float valueY,
128 const float valueZ) = 0;
129 virtual void SetUniform(
130 const int32_t bindingSlot,
131 const float valueX, const float valueY,
132 const float valueZ, const float valueW) = 0;
133 virtual void SetUniform(
134 const int32_t bindingSlot, PS::span<const float> values) = 0;
135
136 // Vertex attribute pointers (equivalent to glVertexPointer etc).
137 virtual void VertexAttribPointer(
138 const VertexAttributeStream stream, const Format format,
139 const uint32_t offset, const uint32_t stride,
140 const VertexAttributeRate rate, const void* data);
141
142 bool IsStreamActive(const VertexAttributeStream stream) const;
143
144 /**
145 * Checks that all the required vertex attributes have been set.
146 * Call this before calling Draw/DrawIndexed etc to avoid potential crashes.
147 */
148 void AssertPointersBound();
149
150protected:
151 CShaderProgram(int streamflags);
152
153 void VertexPointer(const Renderer::Backend::Format format, GLsizei stride, const void* pointer);
154 void NormalPointer(const Renderer::Backend::Format format, GLsizei stride, const void* pointer);
155 void ColorPointer(const Renderer::Backend::Format format, GLsizei stride, const void* pointer);
156 void TexCoordPointer(GLenum texture, const Renderer::Backend::Format format, GLsizei stride, const void* pointer);
157
159
160 // Non-GLSL client state handling:
161 void BindClientStates();
162 void UnbindClientStates();
163 int m_ValidStreams; // which streams have been specified via VertexPointer etc since the last Bind
164};
165
166} // namespace GL
167
168} // namespace Backend
169
170} // namespace Renderer
171
172#endif // INCLUDED_RENDERER_BACKEND_GL_SHADERPROGRAM
Definition: Matrix3D.h:34
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:147
Interned 8-bit strings.
Definition: CStrIntern.h:38
Definition: Vector3D.h:31
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
Definition: Device.h:51
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:89
bool IsStreamActive(const VertexAttributeStream stream) const
Definition: ShaderProgram.cpp:1506
CStrIntern attrib_id_t
Definition: ShaderProgram.h:93
void BindClientStates()
Definition: ShaderProgram.cpp:1444
virtual void SetUniform(const int32_t bindingSlot, const float value)=0
virtual void SetUniform(const int32_t bindingSlot, const float valueX, const float valueY, const float valueZ, const float valueW)=0
virtual void SetUniform(const int32_t bindingSlot, PS::span< const float > values)=0
virtual void Unbind()=0
Unbinds the shader from the GL context.
void AssertPointersBound()
Checks that all the required vertex attributes have been set.
Definition: ShaderProgram.cpp:1546
virtual void VertexAttribPointer(const VertexAttributeStream stream, const Format format, const uint32_t offset, const uint32_t stride, const VertexAttributeRate rate, const void *data)
Definition: ShaderProgram.cpp:1511
static std::unique_ptr< CShaderProgram > Create(CDevice *device, const CStr &name, const CShaderDefines &baseDefines)
Definition: ShaderProgram.cpp:1193
int m_ValidStreams
Definition: ShaderProgram.h:163
void ColorPointer(const Renderer::Backend::Format format, GLsizei stride, const void *pointer)
Definition: ShaderProgram.cpp:1424
int m_StreamFlags
Definition: ShaderProgram.h:158
virtual void SetUniform(const int32_t bindingSlot, const float valueX, const float valueY)=0
void VertexPointer(const Renderer::Backend::Format format, GLsizei stride, const void *pointer)
Definition: ShaderProgram.cpp:1408
virtual TextureUnit GetTextureUnit(const int32_t bindingSlot)=0
void TexCoordPointer(GLenum texture, const Renderer::Backend::Format format, GLsizei stride, const void *pointer)
Definition: ShaderProgram.cpp:1433
void UnbindClientStates()
Definition: ShaderProgram.cpp:1481
virtual void Bind(CShaderProgram *previousShaderProgram)=0
Binds the shader into the GL context.
CShaderProgram(int streamflags)
Definition: ShaderProgram.cpp:1185
virtual void SetUniform(const int32_t bindingSlot, const float valueX, const float valueY, const float valueZ)=0
void NormalPointer(const Renderer::Backend::Format format, GLsizei stride, const void *pointer)
Definition: ShaderProgram.cpp:1417
Definition: ShaderProgram.h:50
CVertexInputLayout(CDevice *device, const PS::span< const SVertexAttributeFormat > attributes)
Definition: ShaderProgram.h:52
IDevice * GetDevice() override
Definition: ShaderProgram.cpp:308
std::vector< SVertexAttributeFormat > m_Attributes
Definition: ShaderProgram.h:71
const std::vector< SVertexAttributeFormat > & GetAttributes() const noexcept
Definition: ShaderProgram.h:66
CDevice * m_Device
Definition: ShaderProgram.h:69
Definition: IDevice.h:48
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:81
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:74
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:277
Format
Definition: Format.h:28
VertexAttributeStream
Definition: IShaderProgram.h:33
Backend
Definition: Backend.h:28
VertexAttributeRate
Definition: IShaderProgram.h:48
Definition: VideoMode.h:29
Definition: Color.h:43
GLenum target
Definition: ShaderProgram.h:114
GLenum type
Definition: ShaderProgram.h:113
GLint unit
Definition: ShaderProgram.h:115
Definition: IShaderProgram.h:54
unsigned int uint32_t
Definition: wposix_types.h:53