1 #ifndef _GLUSHADERPROGRAM_HPP
2 #define _GLUSHADERPROGRAM_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program OpenGL ES Utilities
5 * ------------------------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Shader and Program helpers.
24 *//*--------------------------------------------------------------------*/
26 #include "gluDefs.hpp"
27 #include "gluShaderUtil.hpp"
28 #include "glwEnums.hpp"
29 #include "qpTestLog.h"
44 typedef std::vector<deUint32> ShaderBinaryDataType;
46 /*--------------------------------------------------------------------*//*!
47 * \brief Shader information (compile status, log, etc.).
48 *//*--------------------------------------------------------------------*/
51 ShaderType type; //!< Shader type.
52 std::string source; //!< Shader source.
53 std::string infoLog; //!< Compile info log.
54 bool compileOk; //!< Did compilation succeed?
55 deUint64 compileTimeUs; //!< Compile time in microseconds (us).
57 ShaderInfo (void) : compileOk(false), compileTimeUs(0) {}
60 /*--------------------------------------------------------------------*//*!
61 * \brief Program information (link status, log).
62 *//*--------------------------------------------------------------------*/
65 std::string infoLog; //!< Link info log.
66 bool linkOk; //!< Did link succeed?
67 deUint64 linkTimeUs; //!< Link time in microseconds (us).
69 ProgramInfo (void) : linkOk(false), linkTimeUs(0) {}
72 /*--------------------------------------------------------------------*//*!
73 * \brief Combined shader compilation and program linking info.
74 *//*--------------------------------------------------------------------*/
75 struct ShaderProgramInfo
77 glu::ProgramInfo program;
78 std::vector<glu::ShaderInfo> shaders;
81 /*--------------------------------------------------------------------*//*!
82 * \brief Shader object.
83 *//*--------------------------------------------------------------------*/
87 Shader (const glw::Functions& gl, ShaderType shaderType);
88 Shader (const RenderContext& renderCtx, ShaderType shaderType);
91 void setSources (int numSourceStrings, const char* const* sourceStrings, const int* lengths);
93 void specialize (const char* entryPoint, glw::GLuint numSpecializationConstants,
94 const glw::GLuint* constantIndex, const glw::GLuint* constantValue);
96 deUint32 getShader (void) const { return m_shader; }
97 const ShaderInfo& getInfo (void) const { return m_info; }
99 glu::ShaderType getType (void) const { return getInfo().type; }
100 bool getCompileStatus (void) const { return getInfo().compileOk; }
101 const std::string& getSource (void) const { return getInfo().source; }
102 const std::string& getInfoLog (void) const { return getInfo().infoLog; }
104 deUint32 operator* (void) const { return getShader(); }
107 Shader (const Shader& other);
108 Shader& operator= (const Shader& other);
110 const glw::Functions& m_gl;
111 deUint32 m_shader; //!< Shader handle.
112 ShaderInfo m_info; //!< Client-side clone of state for debug / perf reasons.
115 /*--------------------------------------------------------------------*//*!
116 * \brief Program object.
117 *//*--------------------------------------------------------------------*/
121 Program (const glw::Functions& gl);
122 Program (const RenderContext& renderCtx);
123 Program (const RenderContext& renderCtx, deUint32 program);
126 void attachShader (deUint32 shader);
127 void detachShader (deUint32 shader);
129 void bindAttribLocation (deUint32 location, const char* name);
130 void transformFeedbackVaryings (int count, const char* const* varyings, deUint32 bufferMode);
134 deUint32 getProgram (void) const { return m_program; }
135 const ProgramInfo& getInfo (void) const { return m_info; }
137 bool getLinkStatus (void) const { return getInfo().linkOk; }
138 const std::string& getInfoLog (void) const { return getInfo().infoLog; }
140 bool isSeparable (void) const;
141 void setSeparable (bool separable);
143 int getUniformLocation (const std::string& name);
145 deUint32 operator* (void) const { return getProgram(); }
148 Program (const Program& other);
149 Program& operator= (const Program& other);
151 const glw::Functions& m_gl;
157 /*--------------------------------------------------------------------*//*!
158 * \brief Program pipeline object.
159 *//*--------------------------------------------------------------------*/
160 class ProgramPipeline
163 ProgramPipeline (const RenderContext& renderCtx);
164 ProgramPipeline (const glw::Functions& gl);
165 ~ProgramPipeline (void);
167 deUint32 getPipeline (void) const { return m_pipeline; }
168 void useProgramStages (deUint32 stages, deUint32 program);
169 void activeShaderProgram (deUint32 program);
173 ProgramPipeline (const ProgramPipeline& other);
174 ProgramPipeline& operator= (const ProgramPipeline& other);
176 const glw::Functions& m_gl;
180 struct ProgramSources;
181 struct ProgramBinaries;
183 /*--------------------------------------------------------------------*//*!
184 * \brief Shader program manager.
186 * ShaderProgram manages both Shader and Program objects, and provides
187 * convenient API for constructing such programs.
188 *//*--------------------------------------------------------------------*/
192 ShaderProgram (const glw::Functions& gl, const ProgramSources& sources);
193 ShaderProgram (const glw::Functions& gl, const ProgramBinaries& binaries);
194 ShaderProgram (const RenderContext& renderCtx, const ProgramSources& sources);
195 ShaderProgram (const RenderContext& renderCtx, const ProgramBinaries& binaries);
196 ~ShaderProgram (void);
198 bool isOk (void) const { return m_program.getLinkStatus(); }
199 deUint32 getProgram (void) const { return m_program.getProgram(); }
201 bool hasShader (glu::ShaderType shaderType) const { return !m_shaders[shaderType].empty(); }
202 Shader* getShader (glu::ShaderType shaderType, int shaderNdx = 0) const { return m_shaders[shaderType][shaderNdx]; }
203 int getNumShaders (glu::ShaderType shaderType) const { return (int)m_shaders[shaderType].size(); }
204 const ShaderInfo& getShaderInfo (glu::ShaderType shaderType, int shaderNdx = 0) const { return m_shaders[shaderType][shaderNdx]->getInfo(); }
205 const ProgramInfo& getProgramInfo (void) const { return m_program.getInfo(); }
208 ShaderProgram (const ShaderProgram& other);
209 ShaderProgram& operator= (const ShaderProgram& other);
210 void init (const glw::Functions& gl, const ProgramSources& sources);
211 void init (const glw::Functions& gl, const ProgramBinaries& binaries);
212 void setBinary (const glw::Functions& gl, std::vector<Shader*>& shaders, glw::GLenum binaryFormat, const void* binaryData, const int length);
214 std::vector<Shader*> m_shaders[SHADERTYPE_LAST];
220 deUint32 getGLShaderType (ShaderType shaderType);
221 deUint32 getGLShaderTypeBit (ShaderType shaderType);
222 qpShaderType getLogShaderType (ShaderType shaderType);
224 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderInfo& shaderInfo);
225 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderProgramInfo& shaderProgramInfo);
226 tcu::TestLog& operator<< (tcu::TestLog& log, const ProgramSources& sources);
227 tcu::TestLog& operator<< (tcu::TestLog& log, const Shader& shader);
228 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderProgram& program);
230 // ProgramSources utilities and implementation.
232 struct AttribLocationBinding
237 AttribLocationBinding (void) : location(0) {}
238 AttribLocationBinding (const std::string& name_, deUint32 location_) : name(name_), location(location_) {}
241 struct TransformFeedbackMode
245 TransformFeedbackMode (void) : mode(0) {}
246 TransformFeedbackMode (deUint32 mode_) : mode(mode_) {}
249 struct TransformFeedbackVarying
253 explicit TransformFeedbackVarying (const std::string& name_) : name(name_) {}
256 struct ProgramSeparable
259 explicit ProgramSeparable (bool separable_) : separable(separable_) {}
262 template<typename Iterator>
263 struct TransformFeedbackVaryings
268 TransformFeedbackVaryings (Iterator begin_, Iterator end_) : begin(begin_), end(end_) {}
273 ShaderType shaderType;
276 ShaderSource (void) : shaderType(SHADERTYPE_LAST) {}
277 ShaderSource (glu::ShaderType shaderType_, const std::string& source_) : shaderType(shaderType_), source(source_) { DE_ASSERT(!source_.empty()); }
280 struct VertexSource : public ShaderSource
282 VertexSource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_VERTEX, source_) {}
285 struct FragmentSource : public ShaderSource
287 FragmentSource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_FRAGMENT, source_) {}
290 struct GeometrySource : public ShaderSource
292 GeometrySource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_GEOMETRY, source_) {}
295 struct ComputeSource : public ShaderSource
297 ComputeSource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_COMPUTE, source_) {}
300 struct TessellationControlSource : public ShaderSource
302 TessellationControlSource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_TESSELLATION_CONTROL, source_) {}
305 struct TessellationEvaluationSource : public ShaderSource
307 TessellationEvaluationSource (const std::string& source_) : ShaderSource(glu::SHADERTYPE_TESSELLATION_EVALUATION, source_) {}
310 struct ProgramSources
312 std::vector<std::string> sources[SHADERTYPE_LAST];
313 std::vector<AttribLocationBinding> attribLocationBindings;
315 deUint32 transformFeedbackBufferMode; //!< TF buffer mode, or GL_NONE.
316 std::vector<std::string> transformFeedbackVaryings;
319 ProgramSources (void) : transformFeedbackBufferMode(0), separable(false) {}
321 ProgramSources& operator<< (const AttribLocationBinding& binding) { attribLocationBindings.push_back(binding); return *this; }
322 ProgramSources& operator<< (const TransformFeedbackMode& mode) { transformFeedbackBufferMode = mode.mode; return *this; }
323 ProgramSources& operator<< (const TransformFeedbackVarying& varying) { transformFeedbackVaryings.push_back(varying.name); return *this; }
324 ProgramSources& operator<< (const ShaderSource& shaderSource) { sources[shaderSource.shaderType].push_back(shaderSource.source); return *this; }
325 ProgramSources& operator<< (const ProgramSeparable& progSeparable) { separable = progSeparable.separable; return *this; }
327 template<typename Iterator>
328 ProgramSources& operator<< (const TransformFeedbackVaryings<Iterator>& varyings);
331 struct SpecializationData
336 SpecializationData (void) : index(0), value(0) {}
337 SpecializationData (const deUint32 index_, const deUint32 value_) : index(index_), value(value_) {}
342 ShaderBinaryDataType binary;
343 std::vector<ShaderType> shaderTypes;
344 std::vector<std::string> shaderEntryPoints;
345 std::vector<deUint32> specializationIndices;
346 std::vector<deUint32> specializationValues;
348 ShaderBinary (void) {}
349 ShaderBinary (const ShaderBinaryDataType binary_) : binary(binary_)
351 DE_ASSERT(!binary_.empty());
353 ShaderBinary (const ShaderBinaryDataType binary_, glu::ShaderType shaderType_) : binary(binary_)
355 DE_ASSERT(!binary_.empty());
356 shaderTypes.push_back(shaderType_);
357 shaderEntryPoints.push_back("main");
360 ShaderBinary& operator<< (const ShaderType& shaderType)
362 shaderTypes.push_back(shaderType);
366 ShaderBinary& operator<< (const std::string& entryPoint)
368 shaderEntryPoints.push_back(entryPoint);
372 ShaderBinary& operator<< (const SpecializationData& specData)
374 specializationIndices.push_back(specData.index);
375 specializationValues.push_back(specData.value);
380 struct VertexBinary : public ShaderBinary
382 VertexBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_VERTEX) {}
385 struct FragmentBinary : public ShaderBinary
387 FragmentBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_FRAGMENT) {}
390 struct GeometryBinary : public ShaderBinary
392 GeometryBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_GEOMETRY) {}
395 struct ComputeBinary : public ShaderBinary
397 ComputeBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_COMPUTE) {}
400 struct TessellationControlBinary : public ShaderBinary
402 TessellationControlBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_TESSELLATION_CONTROL) {}
405 struct TessellationEvaluationBinary : public ShaderBinary
407 TessellationEvaluationBinary (const ShaderBinaryDataType binary_) : ShaderBinary(binary_, glu::SHADERTYPE_TESSELLATION_EVALUATION) {}
410 struct ProgramBinaries
412 std::vector<ShaderBinary> binaries;
414 glw::GLenum binaryFormat;
416 ProgramBinaries (void) : binaryFormat(GL_SHADER_BINARY_FORMAT_SPIR_V_ARB) {}
417 ProgramBinaries (glw::GLenum binaryFormat_) : binaryFormat(binaryFormat_) {}
419 ProgramBinaries& operator<< (const ShaderBinary& shaderBinary) { binaries.push_back(shaderBinary); return *this; }
422 template<typename Iterator>
423 inline ProgramSources& ProgramSources::operator<< (const TransformFeedbackVaryings<Iterator>& varyings)
425 for (Iterator cur = varyings.begin; cur != varyings.end; ++cur)
426 transformFeedbackVaryings.push_back(*cur);
430 //! Helper for constructing vertex-fragment source pair.
431 inline ProgramSources makeVtxFragSources (const std::string& vertexSrc, const std::string& fragmentSrc)
433 ProgramSources sources;
434 sources.sources[SHADERTYPE_VERTEX].push_back(vertexSrc);
435 sources.sources[SHADERTYPE_FRAGMENT].push_back(fragmentSrc);
441 #endif // _GLUSHADERPROGRAM_HPP