11 #ifndef hifi_gpu_Shader_h
12 #define hifi_gpu_Shader_h
18 #include <unordered_set>
19 #include <unordered_map>
22 #include <shaders/Shaders.h>
43 typedef std::shared_ptr<Shader> Pointer;
44 typedef std::vector<Pointer> Shaders;
46 using Source = shader::Source;
47 using Reflection = shader::Reflection;
48 using Dialect = shader::Dialect;
49 using Variant = shader::Variant;
51 struct CompilationLog {
53 bool compiled{
false };
56 CompilationLog(
const CompilationLog& src) : message(src.message), compiled(src.compiled) {}
58 using CompilationLogs = std::vector<CompilationLog>;
60 static const int32 INVALID_LOCATION = -1;
65 bool operator()(
const T& x,
const T& y)
const {
return x._name < y._name; }
68 static const Source& getShaderSource(uint32_t
id);
69 static const Source& getVertexShaderSource(uint32_t
id) {
return getShaderSource(
id); }
70 static const Source& getFragmentShaderSource(uint32_t
id) {
return getShaderSource(
id); }
71 static Pointer createVertex(
const Source& source);
72 static Pointer createPixel(
const Source& source);
73 static Pointer createGeometry(
const Source& source);
75 static Pointer createVertex(uint32_t shaderId);
76 static Pointer createPixel(uint32_t shaderId);
77 static Pointer createGeometry(uint32_t shaderId);
79 static Pointer createProgram(uint32_t programId);
80 static Pointer createProgram(
const Pointer& vertexShader,
const Pointer& pixelShader);
81 static Pointer createProgram(
const Pointer& vertexShader,
const Pointer& geometryShader,
const Pointer& pixelShader);
86 Type getType()
const {
return _type; }
87 bool isProgram()
const {
return getType() > NUM_DOMAINS; }
88 bool isDomain()
const {
return getType() < NUM_DOMAINS; }
90 const Source& getSource()
const {
return _source; }
92 const Shaders& getShaders()
const {
return _shaders; }
94 Reflection getReflection(shader::Dialect dialect, shader::Variant variant)
const;
95 Reflection getReflection()
const;
104 using CompilationHandler = std::function<bool(
const Shader&,
const std::string&, CompilationLog&, std::string&)>;
107 bool compilationHasFailed()
const {
return _compilationHasFailed; }
108 const CompilationLogs& getCompilationLogs()
const {
return _compilationLogs; }
109 uint32_t getNumCompilationAttempts()
const {
return _numCompilationAttempts; }
112 void setCompilationHasFailed(
bool compilationHasFailed) { _compilationHasFailed = compilationHasFailed; }
113 void setCompilationLogs(
const CompilationLogs& logs)
const;
114 void incrementCompilationAttempt()
const;
116 const GPUObjectPointer gpuObject{};
119 Shader(Type type,
const Source& source,
bool dynamic);
120 Shader(Type type,
const Pointer& vertex,
const Pointer& geometry,
const Pointer& pixel);
123 const Source _source;
126 const Shaders _shaders;
133 mutable uint32_t _numCompilationAttempts{ 0 };
135 mutable CompilationLogs _compilationLogs;
138 bool _compilationHasFailed{
false };
144 static ShaderPointer createOrReuseDomainShader(Type type, uint32_t sourceId);
146 using ProgramMapKey = glm::uvec3;
147 class ProgramKeyLess {
149 bool operator()(
const ProgramMapKey& l,
const ProgramMapKey& r)
const {
161 using ProgramMap = std::map<ProgramMapKey, std::weak_ptr<Shader>, ProgramKeyLess>;
162 static ProgramMap _programMap;
164 static ShaderPointer createOrReuseProgramShader(Type type,
165 const Pointer& vertexShader,
166 const Pointer& geometryShader,
167 const Pointer& pixelShader);
168 friend class Serializer;
169 friend class Deserializer;
172 typedef Shader::Pointer ShaderPointer;
173 typedef std::vector<ShaderPointer> Shaders;