Overte C++ Documentation
Shader.h
1 //
2 // Shader.h
3 // libraries/gpu/src/gpu
4 //
5 // Created by Sam Gateau on 2/27/2015.
6 // Copyright 2014 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_Shader_h
12 #define hifi_gpu_Shader_h
13 
14 #include "Resource.h"
15 #include <string>
16 #include <memory>
17 #include <set>
18 #include <unordered_set>
19 #include <unordered_map>
20 #include <map>
21 #include <functional>
22 #include <shaders/Shaders.h>
23 #include <QUrl>
24 
25 namespace gpu {
26 
27 class Shader {
28 public:
29  // unique identifier of a shader
30  using ID = uint32_t;
31 
32  enum Type
33  {
34  VERTEX = 0,
35  PIXEL,
36  FRAGMENT = PIXEL,
37  GEOMETRY,
38  NUM_DOMAINS,
39 
40  PROGRAM,
41  };
42 
43  typedef std::shared_ptr<Shader> Pointer;
44  typedef std::vector<Pointer> Shaders;
45 
46  using Source = shader::Source;
47  using Reflection = shader::Reflection;
48  using Dialect = shader::Dialect;
49  using Variant = shader::Variant;
50 
52  struct CompilationLog {
54  std::string message;
55 
57  bool compiled{ false };
58 
59  CompilationLog() {}
60  CompilationLog(const CompilationLog& src) : message(src.message), compiled(src.compiled) {}
61  };
62  using CompilationLogs = std::vector<CompilationLog>;
63 
70  static const Source& getShaderSource(uint32_t id);
71 
80  static const Source& getVertexShaderSource(uint32_t id) { return getShaderSource(id); }
81 
90  static const Source& getFragmentShaderSource(uint32_t id) { return getShaderSource(id); }
91 
99  static Pointer createVertex(const Source& source);
100 
108  static Pointer createPixel(const Source& source);
109 
117  static Pointer createVertex(uint32_t shaderId);
118 
126  static Pointer createPixel(uint32_t shaderId);
127 
136  static Pointer createProgram(uint32_t programId);
137 
145  static Pointer createProgram(const Pointer& vertexShader, const Pointer& pixelShader);
146 
147  ~Shader();
148 
156  ID getID() const;
157 
164  Type getType() const { return _type; }
165 
169  bool isProgram() const { return getType() > NUM_DOMAINS; }
170 
174  bool isDomain() const { return getType() < NUM_DOMAINS; }
175 
179  const Source& getSource() const { return _source; }
180 
186  const Shaders& getShaders() const { return _shaders; }
187 
195  Reflection getReflection(shader::Dialect dialect, shader::Variant variant) const;
196 
204  Reflection getReflection() const;
205 
206 
216  using CompilationHandler = std::function<bool(const Shader&, const std::string&, CompilationLog&, std::string&)>;
217 
221  bool compilationHasFailed() const { return _compilationHasFailed; }
222 
226  const CompilationLogs& getCompilationLogs() const { return _compilationLogs; }
227 
234  void setCompilationHasFailed(bool compilationHasFailed) { _compilationHasFailed = compilationHasFailed; }
235 
242  void setCompilationLogs(const CompilationLogs& logs) const;
243 
244  // Object representing shader on the API-specific backend side.
245  const GPUObjectPointer gpuObject{};
246 
247 protected:
255  Shader(Type type, const Source& source, bool dynamic);
256 
265  Shader(Type type, const Pointer& vertex, const Pointer& geometry, const Pointer& pixel);
266 
268  const Source _source;
269 
271  const Shaders _shaders;
272 
273 
275  const Type _type;
276 
278  mutable CompilationLogs _compilationLogs;
279 
281  bool _compilationHasFailed{ false };
282 
291  static ShaderPointer createOrReuseDomainShader(Type type, uint32_t sourceId);
292 
293  //. The IDs of the shaders in a program make its key.
294  using ProgramMapKey = glm::uvec3;
295 
298  public:
299  bool operator()(const ProgramMapKey& l, const ProgramMapKey& r) const {
300  if (l.x == r.x) {
301  if (l.y == r.y) {
302  return (l.z < r.z);
303  } else {
304  return (l.y < r.y);
305  }
306  } else {
307  return (l.x < r.x);
308  }
309  }
310  };
311  using ProgramMap = std::map<ProgramMapKey, std::weak_ptr<Shader>, ProgramKeyLess>;
312 
314  static ProgramMap _programMap;
315 
326  static ShaderPointer createOrReuseProgramShader(Type type,
327  const Pointer& vertexShader,
328  const Pointer& geometryShader,
329  const Pointer& pixelShader);
330  friend class Serializer;
331  friend class Deserializer;
332 };
333 
334 typedef Shader::Pointer ShaderPointer;
335 typedef std::vector<ShaderPointer> Shaders;
336 
337 }; // namespace gpu
338 
339 #endif
Used for sorting program keys in the map.
Definition: Shader.h:297
Helper class containing compilation log and variable stating if compilation was successful.
Definition: Shader.h:52
bool compiled
true if compilation was successful.
Definition: Shader.h:57
std::string message
Log from shader compilation.
Definition: Shader.h:54