Pyrogenesis  trunk
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"
22 #include "lib/file/vfs/vfs_path.h"
23 #include "ps/containers/Span.h"
24 #include "ps/CStrForward.h"
28 
29 #include <map>
30 #include <vector>
31 
32 struct CColor;
33 class CMatrix3D;
34 class CVector3D;
35 class CShaderDefines;
36 class CStrIntern;
37 
38 namespace Renderer
39 {
40 
41 namespace Backend
42 {
43 
44 namespace GL
45 {
46 
47 class CDevice;
48 
50 {
51 public:
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 
68 private:
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 
92 public:
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 
111  struct TextureUnit
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 
150 protected:
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
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
VertexAttributeRate
Definition: IShaderProgram.h:47
CDevice * m_Device
Definition: ShaderProgram.h:69
CVertexInputLayout(CDevice *device, const PS::span< const SVertexAttributeFormat > attributes)
Definition: ShaderProgram.h:52
Definition: Color.h:42
CStrIntern attrib_id_t
Definition: ShaderProgram.h:93
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
Definition: Vector3D.h:30
Definition: Matrix3D.h:33
Format
Definition: Format.h:27
GLint unit
Definition: ShaderProgram.h:115
VertexAttributeStream
Definition: IShaderProgram.h:32
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:290
Interned 8-bit strings.
Definition: CStrIntern.h:37
A compiled vertex+fragment shader program.
Definition: ShaderProgram.h:88
Definition: IDevice.h:47
int m_ValidStreams
Definition: ShaderProgram.h:163
IShaderProgram is a container for multiple shaders of different types.
Definition: IShaderProgram.h:80
Backend
Definition: Backend.h:27
Definition: ShaderProgram.h:49
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:146
std::vector< SVertexAttributeFormat > m_Attributes
Definition: ShaderProgram.h:71
const std::vector< SVertexAttributeFormat > & GetAttributes() const noexcept
Definition: ShaderProgram.h:66
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
Definition: IShaderProgram.h:53
GLenum target
Definition: ShaderProgram.h:114
int m_StreamFlags
Definition: ShaderProgram.h:158
GLenum type
Definition: ShaderProgram.h:113
Definition: Device.h:50
Simplifed version of std::span (C++20) as we don&#39;t support the original one yet.
Definition: Span.h:36
IDevice * GetDevice() override
Definition: ShaderProgram.cpp:269