GteShader.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.2 (2016/10/31)
7 
8 #pragma once
9 
10 #include <LowLevel/GteLogger.h>
12 #include <Graphics/GteRawBuffer.h>
15 #include <Graphics/GteTexture1.h>
16 #include <Graphics/GteTexture2.h>
17 #include <Graphics/GteTexture3.h>
23 #if defined(GTE_DEV_OPENGL)
25 #else
27 #endif
28 #include <memory>
29 
30 namespace gte
31 {
32 
34 {
35 public:
36 #if defined(GTE_DEV_OPENGL)
37  // Constructon for shaders corresponding to GLSL. The 'type' parameter
38  // in the constructor is one of the enumerates in GLSLReflection:
39  // ST_VERTEX, ST_GEOMETRY, ST_PIXEL, ST_COMPUTE.
40  Shader(GLSLReflection const& reflector, int type);
41 #else
42  // Construction for shaders corresponding to HLSL.
43  Shader(HLSLShader const& program);
44 #endif
45 
46  // To avoid frequent string comparisons during run time, obtain a handle
47  // for an object and use it instead for setting and getting the values.
48  // If the named object exists, the returned handle is nonnegative;
49  // otherwise, it is -1.
50  int Get(std::string const& name) const;
51 
52  // Set or get the buffers. If the set is successful, the return value is
53  // nonnegative and is the index into the appropriate array. This handle
54  // may be passed to the Set(handle,*) and Get(handle,*) functions. The
55  // mechanism allows you to set directly by index and avoid the name
56  // comparisons that occur with the Set(name,*) and Get(name,*) functions.
57  template <typename T>
58  int Set(std::string const& name, std::shared_ptr<T> const& object);
59 
60  template <typename T>
61  std::shared_ptr<T> const Get(std::string const& name) const;
62 
63  template <typename T>
64  void Set(int handle, std::shared_ptr<T> const& object);
65 
66  template <typename T>
67  std::shared_ptr<T> const Get(int handle) const;
68 
69  // Access size of one of these buffers.
70  // Returns 0 if the requested buffer does not exist.
71  // For StructuredBuffers, it's the size of one structure element.
72  unsigned int GetConstantBufferSize(int handle) const;
73  unsigned int GetConstantBufferSize(std::string const& name) const;
74  unsigned int GetTextureBufferSize(int handle) const;
75  unsigned int GetTextureBufferSize(std::string const& name) const;
76  unsigned int GetStructuredBufferSize(int handle) const;
77  unsigned int GetStructuredBufferSize(std::string const& name) const;
78 
79  // Access member layouts for these types of buffers.
80  bool GetConstantBufferLayout(int handle, BufferLayout& layout) const;
81  bool GetConstantBufferLayout(std::string const& name, BufferLayout& layout) const;
82  bool GetTextureBufferLayout(int handle, BufferLayout& layout) const;
83  bool GetTextureBufferLayout(std::string const& name, BufferLayout& layout) const;
84 #if defined(GTE_DEV_OPENGL)
85  bool GetStructuredBufferLayout(int handle, BufferLayout& layout) const;
86  bool GetStructuredBufferLayout(std::string const& name, BufferLayout& layout) const;
87 #endif
88 
89  inline unsigned int GetNumXThreads() const;
90  inline unsigned int GetNumYThreads() const;
91  inline unsigned int GetNumZThreads() const;
92 
93 #if defined(GTE_DEV_OPENGL)
94  enum
95  {
96  ConstantBufferShaderDataLookup = 0, // CB
97  TextureBufferShaderDataLookup = 1, // TB
98  StructuredBufferShaderDataLookup = 2, // SB
99  RawBufferShaderDataLookup = 3, // RB
100  TextureSingleShaderDataLookup = 4, // TX
101  TextureArrayShaderDataLookup = 5, // TS
102  SamplerStateShaderDataLookup = 6, // SS
103  AtomicCounterBufferShaderDataLookup = 7,// AB
104  AtomicCounterShaderDataLookup = 8, // AC
105  NUM_LOOKUP_INDICES = 9
106  };
107 #else
108  enum
109  {
110  ConstantBufferShaderDataLookup = 0, // CB
111  TextureBufferShaderDataLookup = 1, // TB
112  StructuredBufferShaderDataLookup = 2, // SB
113  RawBufferShaderDataLookup = 3, // RB
114  TextureSingleShaderDataLookup = 4, // TX
115  TextureArrayShaderDataLookup = 5, // TS
116  SamplerStateShaderDataLookup = 6, // SS
117  NUM_LOOKUP_INDICES = 7
118  };
119 #endif
120 
121 protected:
122  // This structure provides the data necessary for the engine to attach
123  // the associated resources to the shader, including name lookups and
124  // resource validation. Not all members are used for each graphics
125  // object type:
126  // CB - constant buffer, lookup 0
127  // TB - texture buffer, lookup 1
128  // SB - structured buffer (and derived classes), lookup 2
129  // TODO: typed buffer
130  // RB - raw buffer, lookup 3
131  // TX - texture (and derived classes), lookup 4
132  // TA - texture array (and derived classes), lookup 5
133  // SS - sampler state, lookup 6
134 #if defined(GTE_DEV_OPENGL)
135  // AB - atomic (counter) buffer, lookup 7
136  // AC - atomic counter, lookup 8
137  //
138  // How to use atomic counter information. Given structured buffer data
139  // at index some-index:
140  //
141  // sb = mData[StructuredBufferShaderDataLookup][some-index];
142  //
143  // Does structured buffer have a counter? Check
144  //
145  // sb.isGpuWritable
146  //
147  // Access the atomic counter:
148  //
149  // ac = mData[AtomicCounterShaderDataLookup][sb.extra];
150  //
151  // Access where this atomic counter exists in one of the atomic counter buffers:
152  //
153  // acb = mData[AtomicCounterBufferShaderDataLookup][ac.bindPoint];
154  // acbIndex = acb.bindPoint;
155  // acbOffset = acb.extra;
156  //
157  // TODO: factor out differences between DX11 and GL4.
158  // TODO: find more meaningful names for fields like extra, isGpuWritable based on usage
159  struct Data
160  {
161  Data(GraphicsObjectType inType, std::string const& inName,
162  int inBindPoint, int inNumBytes, unsigned int inExtra,
163  bool inIsGpuWritable);
164 
165  std::shared_ptr<GraphicsObject> object; // CB, TB, SB, RB, TX, TA, SS
166  GraphicsObjectType type; // CB, TB, SB, RB, TX, TA, SS
167  std::string name; // CB, TB, SB, RB, TX, TA, SS, AC
168  int bindPoint; // CB, TB, SB, RB, TX, TA, SS, AB, AC (atomic counter buffer index)
169  int numBytes; // CB, TB, SB, RB, AB, AC (always 4)
170  unsigned int extra; // TX, TA (dims), SS (type for TX or TA), SB (if has atomic counter, AC index), AC (offset)
171  bool isGpuWritable; // SB (true if has atomic counter), RB, TX/TA (false for gsampler*, true for gimage*)
172  };
173 #else
174  struct Data
175  {
176  Data(GraphicsObjectType inType, std::string const& inName,
177  int inBindPoint, int inNumBytes, unsigned int inExtra,
178  bool inIsGpuWritable);
179 
180  std::shared_ptr<GraphicsObject> object; // CB, TB, SB, RB, TX, TA, SS
181  GraphicsObjectType type; // CB, TB, SB, RB, TX, TA, SS
182  std::string name; // CB, TB, SB, RB, TX, TA, SS
183  int bindPoint; // CB, TB, SB, RB, TX, TA, SS
184  int numBytes; // CB, TB, SB, RB
185  unsigned int extra; // TX, TA (dims), SB (ctrtype)
186  bool isGpuWritable; // SB, RB, TX, TA
187  };
188 #endif
189 
190  bool IsValid(Data const& goal, ConstantBuffer* resource) const;
191  bool IsValid(Data const& goal, TextureBuffer* resource) const;
192  bool IsValid(Data const& goal, StructuredBuffer* resource) const;
193  bool IsValid(Data const& goal, RawBuffer* resource) const;
194  bool IsValid(Data const& goal, TextureSingle* resource) const;
195  bool IsValid(Data const& goal, TextureArray* resource) const;
196  bool IsValid(Data const& goal, SamplerState* state) const;
197 
198  std::vector<Data> mData[NUM_LOOKUP_INDICES];
199  std::vector<unsigned char> mCompiledCode;
200  unsigned int mNumXThreads;
201  unsigned int mNumYThreads;
202  unsigned int mNumZThreads;
203 
204 private:
205  std::vector<BufferLayout> mCBufferLayouts;
206  std::vector<BufferLayout> mTBufferLayouts;
207 #if defined(GTE_DEV_OPENGL)
208  std::vector<BufferLayout> mSBufferLayouts;
209 #endif
210 
211 public:
212  // For use by the graphics engine.
213  inline std::vector<unsigned char> const& GetCompiledCode() const;
214  inline std::vector<Data> const& GetData(int lookup) const;
215 };
216 
217 
218 template <typename T>
219 int Shader::Set(std::string const& name, std::shared_ptr<T> const& object)
220 {
221  int handle = 0;
222  for (auto& data : mData[T::shaderDataLookup])
223  {
224  if (name == data.name)
225  {
226  if (IsValid(data, object.get()))
227  {
228  data.object = object;
229  return handle;
230  }
231  return -1;
232  }
233  ++handle;
234  }
235 
236  LogError("Cannot find object " + name + ".");
237  return -1;
238 }
239 
240 template <typename T>
241 std::shared_ptr<T> const Shader::Get(std::string const& name) const
242 {
243  for (auto const& data : mData[T::shaderDataLookup])
244  {
245  if (name == data.name)
246  {
247  return std::static_pointer_cast<T>(data.object);
248  }
249  }
250  return nullptr;
251 }
252 
253 template <typename T>
254 void Shader::Set(int handle, std::shared_ptr<T> const& object)
255 {
256  std::vector<Data>& data = mData[T::shaderDataLookup];
257  if (0 <= handle && handle < static_cast<int>(data.size()))
258  {
259  auto& d = data[handle];
260  if (IsValid(d, object.get()))
261  {
262  d.object = object;
263  }
264  return;
265  }
266 
267  LogError("Invalid handle for object.");
268 }
269 
270 template <typename T>
271 std::shared_ptr<T> const Shader::Get(int handle) const
272 {
273  std::vector<Data> const& data = mData[T::shaderDataLookup];
274  if (0 <= handle && handle < static_cast<int>(data.size()))
275  {
276  return std::static_pointer_cast<T>(data[handle].object);
277  }
278  return nullptr;
279 }
280 
281 // Specialization to copy the member layouts of the shader program to the
282 // buffer objects.
283 template <> inline
285  std::shared_ptr<ConstantBuffer> const& object)
286 {
287  int handle = 0;
288  for (auto& data : mData[ConstantBuffer::shaderDataLookup])
289  {
290  if (name == data.name)
291  {
292  if (IsValid(data, object.get()))
293  {
294  data.object = object;
295  object->SetLayout(mCBufferLayouts[handle]);
296  return handle;
297  }
298  return -1;
299  }
300  ++handle;
301  }
302 
303  LogError("Cannot find object " + name + ".");
304  return -1;
305 }
306 
307 // Specialization to copy the member layouts of the shader program to the
308 // buffer objects.
309 template <> inline
311  std::shared_ptr<TextureBuffer> const& object)
312 {
313  int handle = 0;
314  for (auto& data : mData[TextureBuffer::shaderDataLookup])
315  {
316  if (name == data.name)
317  {
318  if (IsValid(data, object.get()))
319  {
320  data.object = object;
321  object->SetLayout(mTBufferLayouts[handle]);
322  return handle;
323  }
324  return -1;
325  }
326  ++handle;
327  }
328 
329  LogError("Cannot find object " + name + ".");
330  return -1;
331 }
332 
333 // Specialization to copy the member layouts of the shader program to the
334 // buffer objects.
335 template<> inline
336 void Shader::Set(int handle, std::shared_ptr<ConstantBuffer> const& object)
337 {
338  std::vector<Data>& data = mData[ConstantBuffer::shaderDataLookup];
339  if (0 <= handle && handle < static_cast<int>(data.size()))
340  {
341  auto& d = data[handle];
342  if (IsValid(d, object.get()))
343  {
344  d.object = object;
345  object->SetLayout(mCBufferLayouts[handle]);
346  }
347  return;
348  }
349 
350  LogError("Invalid handle for object.");
351 }
352 
353 // Specialization to copy the member layouts of the shader program to the
354 // buffer objects.
355 template<> inline
356 void Shader::Set(int handle, std::shared_ptr<TextureBuffer> const& object)
357 {
358  std::vector<Data>& data = mData[TextureBuffer::shaderDataLookup];
359  if (0 <= handle && handle < static_cast<int>(data.size()))
360  {
361  auto& d = data[handle];
362  if (IsValid(d, object.get()))
363  {
364  d.object = object;
365  object->SetLayout(mTBufferLayouts[handle]);
366  }
367  return;
368  }
369 
370  LogError("Invalid handle for object.");
371 }
372 
373 inline unsigned int Shader::GetNumXThreads() const
374 {
375  return mNumXThreads;
376 }
377 
378 inline unsigned int Shader::GetNumYThreads() const
379 {
380  return mNumYThreads;
381 }
382 
383 inline unsigned int Shader::GetNumZThreads() const
384 {
385  return mNumZThreads;
386 }
387 
388 inline std::vector<unsigned char> const& Shader::GetCompiledCode() const
389 {
390  return mCompiledCode;
391 }
392 
393 inline std::vector<Shader::Data> const& Shader::GetData(int lookup) const
394 {
395  return mData[lookup];
396 }
397 
398 
399 }
std::vector< BufferLayout > mTBufferLayouts
Definition: GteShader.h:206
std::vector< MemberLayout > BufferLayout
GLbitfield GLuint program
Definition: glcorearb.h:1926
GraphicsObjectType type
Definition: GteShader.h:181
GLuint const GLchar * name
Definition: glcorearb.h:781
static int const shaderDataLookup
int Get(std::string const &name) const
Definition: GteShader.cpp:456
unsigned int extra
Definition: GteShader.h:185
std::string name
Definition: GteShader.h:182
int Set(std::string const &name, std::shared_ptr< T > const &object)
Definition: GteShader.h:219
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
#define LogError(message)
Definition: GteLogger.h:92
GLboolean * data
Definition: glcorearb.h:126
static int const shaderDataLookup
unsigned int GetNumYThreads() const
Definition: GteShader.h:378
std::shared_ptr< GraphicsObject > object
Definition: GteShader.h:180
unsigned int mNumXThreads
Definition: GteShader.h:200
unsigned int mNumYThreads
Definition: GteShader.h:201
std::vector< BufferLayout > mCBufferLayouts
Definition: GteShader.h:205
std::vector< unsigned char > mCompiledCode
Definition: GteShader.h:199
unsigned int mNumZThreads
Definition: GteShader.h:202
GLuint object
Definition: glext.h:6426
unsigned int GetNumZThreads() const
Definition: GteShader.h:383
unsigned int GetNumXThreads() const
Definition: GteShader.h:373
std::vector< unsigned char > const & GetCompiledCode() const
Definition: GteShader.h:388
#define GTE_IMPEXP
Definition: GTEngineDEF.h:63
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:103
std::vector< Data > const & GetData(int lookup) const
Definition: GteShader.h:393


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01