1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES Utilities
3 * ------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Wrapper for GL program object.
22 *//*--------------------------------------------------------------------*/
24 #include "gluShaderProgram.hpp"
25 #include "gluRenderContext.hpp"
26 #include "glwFunctions.hpp"
27 #include "glwEnums.hpp"
28 #include "tcuTestLog.hpp"
40 Shader::Shader (const RenderContext& renderCtx, ShaderType shaderType)
41 : m_gl (renderCtx.getFunctions())
44 m_info.type = shaderType;
45 m_shader = m_gl.createShader(getGLShaderType(shaderType));
46 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glCreateShader()");
50 Shader::Shader (const glw::Functions& gl, ShaderType shaderType)
54 m_info.type = shaderType;
55 m_shader = m_gl.createShader(getGLShaderType(shaderType));
56 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glCreateShader()");
60 Shader::~Shader (void)
62 m_gl.deleteShader(m_shader);
65 void Shader::setSources (int numSourceStrings, const char* const* sourceStrings, const int* lengths)
67 m_gl.shaderSource(m_shader, numSourceStrings, sourceStrings, lengths);
68 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glShaderSource()");
70 m_info.source.clear();
71 for (int ndx = 0; ndx < numSourceStrings; ndx++)
73 const size_t length = lengths && lengths[ndx] >= 0 ? lengths[ndx] : strlen(sourceStrings[ndx]);
74 m_info.source += std::string(sourceStrings[ndx], length);
78 void Shader::compile (void)
80 m_info.compileOk = false;
81 m_info.compileTimeUs = 0;
82 m_info.infoLog.clear();
85 deUint64 compileStart = deGetMicroseconds();
86 m_gl.compileShader(m_shader);
87 m_info.compileTimeUs = deGetMicroseconds() - compileStart;
90 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glCompileShader()");
94 int compileStatus = 0;
96 m_gl.getShaderiv(m_shader, GL_COMPILE_STATUS, &compileStatus);
97 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetShaderiv()");
99 m_info.compileOk = compileStatus != GL_FALSE;
107 m_gl.getShaderiv(m_shader, GL_INFO_LOG_LENGTH, &infoLogLen);
108 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetShaderiv()");
112 // The INFO_LOG_LENGTH query and the buffer query implementations have
113 // very commonly off-by-one errors. Try to work around these issues.
115 // add tolerance for off-by-one in log length, buffer write, and for terminator
116 std::vector<char> infoLog(infoLogLen + 3, '\0');
118 // claim buf size is one smaller to protect from off-by-one writing over buffer bounds
119 m_gl.getShaderInfoLog(m_shader, (int)infoLog.size() - 1, &unusedLen, &infoLog[0]);
121 if (infoLog[(int)(infoLog.size()) - 1] != '\0')
123 // return whole buffer if null terminator was overwritten
124 m_info.infoLog = std::string(&infoLog[0], infoLog.size());
128 // read as C string. infoLog is guaranteed to be 0-terminated
129 m_info.infoLog = std::string(&infoLog[0]);
135 void Shader::specialize (const char* entryPoint, glw::GLuint numSpecializationConstants,
136 const glw::GLuint* constantIndex, const glw::GLuint* constantValue)
138 m_info.compileOk = false;
139 m_info.compileTimeUs = 0;
140 m_info.infoLog.clear();
143 deUint64 compileStart = deGetMicroseconds();
144 m_gl.specializeShader(m_shader, entryPoint, numSpecializationConstants, constantIndex, constantValue);
145 m_info.compileTimeUs = deGetMicroseconds() - compileStart;
148 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glSpecializeShader()");
152 int compileStatus = 0;
154 m_gl.getShaderiv(m_shader, GL_COMPILE_STATUS, &compileStatus);
155 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetShaderiv()");
157 m_info.compileOk = compileStatus != GL_FALSE;
165 m_gl.getShaderiv(m_shader, GL_INFO_LOG_LENGTH, &infoLogLen);
166 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetShaderiv()");
170 // The INFO_LOG_LENGTH query and the buffer query implementations have
171 // very commonly off-by-one errors. Try to work around these issues.
173 // add tolerance for off-by-one in log length, buffer write, and for terminator
174 std::vector<char> infoLog(infoLogLen + 3, '\0');
176 // claim buf size is one smaller to protect from off-by-one writing over buffer bounds
177 m_gl.getShaderInfoLog(m_shader, (int)infoLog.size() - 1, &unusedLen, &infoLog[0]);
178 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetShaderInfoLog()");
180 if (infoLog[(int)(infoLog.size()) - 1] != '\0')
182 // return whole buffer if null terminator was overwritten
183 m_info.infoLog = std::string(&infoLog[0], infoLog.size());
187 // read as C string. infoLog is guaranteed to be 0-terminated
188 m_info.infoLog = std::string(&infoLog[0]);
196 static bool getProgramLinkStatus (const glw::Functions& gl, deUint32 program)
200 gl.getProgramiv(program, GL_LINK_STATUS, &linkStatus);
201 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramiv()");
202 return (linkStatus != GL_FALSE);
205 static std::string getProgramInfoLog (const glw::Functions& gl, deUint32 program)
210 gl.getProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLen);
211 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetProgramiv()");
215 // The INFO_LOG_LENGTH query and the buffer query implementations have
216 // very commonly off-by-one errors. Try to work around these issues.
218 // add tolerance for off-by-one in log length, buffer write, and for terminator
219 std::vector<char> infoLog(infoLogLen + 3, '\0');
221 // claim buf size is one smaller to protect from off-by-one writing over buffer bounds
222 gl.getProgramInfoLog(program, (int)infoLog.size() - 1, &unusedLen, &infoLog[0]);
224 // return whole buffer if null terminator was overwritten
225 if (infoLog[(int)(infoLog.size()) - 1] != '\0')
226 return std::string(&infoLog[0], infoLog.size());
228 // read as C string. infoLog is guaranteed to be 0-terminated
229 return std::string(&infoLog[0]);
231 return std::string();
234 Program::Program (const RenderContext& renderCtx)
235 : m_gl (renderCtx.getFunctions())
238 m_program = m_gl.createProgram();
239 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glCreateProgram()");
242 Program::Program (const glw::Functions& gl)
246 m_program = m_gl.createProgram();
247 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glCreateProgram()");
250 Program::Program (const RenderContext& renderCtx, deUint32 program)
251 : m_gl (renderCtx.getFunctions())
252 , m_program (program)
254 m_info.linkOk = getProgramLinkStatus(m_gl, program);
255 m_info.infoLog = getProgramInfoLog(m_gl, program);
258 Program::~Program (void)
260 m_gl.deleteProgram(m_program);
263 void Program::attachShader (deUint32 shader)
265 m_gl.attachShader(m_program, shader);
266 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glAttachShader()");
269 void Program::detachShader (deUint32 shader)
271 m_gl.detachShader(m_program, shader);
272 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glDetachShader()");
275 void Program::bindAttribLocation (deUint32 location, const char* name)
277 m_gl.bindAttribLocation(m_program, location, name);
278 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glBindAttribLocation()");
281 void Program::transformFeedbackVaryings (int count, const char* const* varyings, deUint32 bufferMode)
283 m_gl.transformFeedbackVaryings(m_program, count, varyings, bufferMode);
284 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glTransformFeedbackVaryings()");
287 void Program::link (void)
289 m_info.linkOk = false;
290 m_info.linkTimeUs = 0;
291 m_info.infoLog.clear();
294 deUint64 linkStart = deGetMicroseconds();
295 m_gl.linkProgram(m_program);
296 m_info.linkTimeUs = deGetMicroseconds() - linkStart;
298 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glLinkProgram()");
300 m_info.linkOk = getProgramLinkStatus(m_gl, m_program);
301 m_info.infoLog = getProgramInfoLog(m_gl, m_program);
304 bool Program::isSeparable (void) const
306 int separable = GL_FALSE;
308 m_gl.getProgramiv(m_program, GL_PROGRAM_SEPARABLE, &separable);
309 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGetProgramiv()");
311 return (separable != GL_FALSE);
314 void Program::setSeparable (bool separable)
316 m_gl.programParameteri(m_program, GL_PROGRAM_SEPARABLE, separable);
317 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glProgramParameteri()");
322 ProgramPipeline::ProgramPipeline (const RenderContext& renderCtx)
323 : m_gl (renderCtx.getFunctions())
326 m_gl.genProgramPipelines(1, &m_pipeline);
327 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGenProgramPipelines()");
330 ProgramPipeline::ProgramPipeline (const glw::Functions& gl)
334 m_gl.genProgramPipelines(1, &m_pipeline);
335 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glGenProgramPipelines()");
338 ProgramPipeline::~ProgramPipeline (void)
340 m_gl.deleteProgramPipelines(1, &m_pipeline);
343 void ProgramPipeline::useProgramStages (deUint32 stages, deUint32 program)
345 m_gl.useProgramStages(m_pipeline, stages, program);
346 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glUseProgramStages()");
349 void ProgramPipeline::activeShaderProgram (deUint32 program)
351 m_gl.activeShaderProgram(m_pipeline, program);
352 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glActiveShaderProgram()");
355 bool ProgramPipeline::isValid (void)
357 glw::GLint status = GL_FALSE;
358 m_gl.validateProgramPipeline(m_pipeline);
359 GLU_EXPECT_NO_ERROR(m_gl.getError(), "glValidateProgramPipeline()");
361 m_gl.getProgramPipelineiv(m_pipeline, GL_VALIDATE_STATUS, &status);
363 return (status != GL_FALSE);
368 ShaderProgram::ShaderProgram (const RenderContext& renderCtx, const ProgramSources& sources)
369 : m_program(renderCtx.getFunctions())
371 init(renderCtx.getFunctions(), sources);
374 ShaderProgram::ShaderProgram (const RenderContext& renderCtx, const ProgramBinaries& binaries)
375 : m_program(renderCtx.getFunctions())
377 init(renderCtx.getFunctions(), binaries);
380 ShaderProgram::ShaderProgram (const glw::Functions& gl, const ProgramSources& sources)
386 ShaderProgram::ShaderProgram (const glw::Functions& gl, const ProgramBinaries& binaries)
392 void ShaderProgram::init (const glw::Functions& gl, const ProgramSources& sources)
396 bool shadersOk = true;
398 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
400 for (int shaderNdx = 0; shaderNdx < (int)sources.sources[shaderType].size(); ++shaderNdx)
402 const char* source = sources.sources[shaderType][shaderNdx].c_str();
403 const int length = (int)sources.sources[shaderType][shaderNdx].size();
405 m_shaders[shaderType].reserve(m_shaders[shaderType].size() + 1);
407 m_shaders[shaderType].push_back(new Shader(gl, ShaderType(shaderType)));
408 m_shaders[shaderType].back()->setSources(1, &source, &length);
409 m_shaders[shaderType].back()->compile();
411 shadersOk = shadersOk && m_shaders[shaderType].back()->getCompileStatus();
417 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
418 for (int shaderNdx = 0; shaderNdx < (int)m_shaders[shaderType].size(); ++shaderNdx)
419 m_program.attachShader(m_shaders[shaderType][shaderNdx]->getShader());
421 for (std::vector<AttribLocationBinding>::const_iterator binding = sources.attribLocationBindings.begin(); binding != sources.attribLocationBindings.end(); ++binding)
422 m_program.bindAttribLocation(binding->location, binding->name.c_str());
424 DE_ASSERT((sources.transformFeedbackBufferMode == GL_NONE) == sources.transformFeedbackVaryings.empty());
425 if (sources.transformFeedbackBufferMode != GL_NONE)
427 std::vector<const char*> tfVaryings(sources.transformFeedbackVaryings.size());
428 for (int ndx = 0; ndx < (int)tfVaryings.size(); ndx++)
429 tfVaryings[ndx] = sources.transformFeedbackVaryings[ndx].c_str();
431 m_program.transformFeedbackVaryings((int)tfVaryings.size(), &tfVaryings[0], sources.transformFeedbackBufferMode);
434 if (sources.separable)
435 m_program.setSeparable(true);
442 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
443 for (int shaderNdx = 0; shaderNdx < (int)m_shaders[shaderType].size(); ++shaderNdx)
444 delete m_shaders[shaderType][shaderNdx];
449 void ShaderProgram::init (const glw::Functions& gl, const ProgramBinaries& binaries)
453 bool shadersOk = true;
455 for (deUint32 binaryNdx = 0; binaryNdx < binaries.binaries.size(); ++binaryNdx)
457 ShaderBinary shaderBinary = binaries.binaries[binaryNdx];
458 if (!shaderBinary.binary.empty())
460 const char* binary = (const char*)shaderBinary.binary.data();
461 const int length = (int)(shaderBinary.binary.size() * sizeof(deUint32));
463 DE_ASSERT(shaderBinary.shaderEntryPoints.size() == shaderBinary.shaderTypes.size());
465 std::vector<Shader*> shaders;
466 for (deUint32 shaderTypeNdx = 0; shaderTypeNdx < shaderBinary.shaderTypes.size(); ++shaderTypeNdx)
468 ShaderType shaderType = shaderBinary.shaderTypes[shaderTypeNdx];
470 Shader* shader = new Shader(gl, ShaderType(shaderType));
472 m_shaders[shaderType].reserve(m_shaders[shaderType].size() + 1);
473 m_shaders[shaderType].push_back(shader);
474 shaders.push_back(shader);
477 setBinary(gl, shaders, binaries.binaryFormat, binary, length);
479 for (deUint32 shaderNdx = 0; shaderNdx < shaders.size(); ++shaderNdx)
481 shaders[shaderNdx]->specialize(shaderBinary.shaderEntryPoints[shaderNdx].c_str(),
482 (deUint32)shaderBinary.specializationIndices.size(),
483 shaderBinary.specializationIndices.data(),
484 shaderBinary.specializationValues.data());
486 shadersOk = shadersOk && shaders[shaderNdx]->getCompileStatus();
493 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
494 for (int shaderNdx = 0; shaderNdx < (int)m_shaders[shaderType].size(); ++shaderNdx)
495 m_program.attachShader(m_shaders[shaderType][shaderNdx]->getShader());
502 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
503 for (int shaderNdx = 0; shaderNdx < (int)m_shaders[shaderType].size(); ++shaderNdx)
504 delete m_shaders[shaderType][shaderNdx];
509 void ShaderProgram::setBinary (const glw::Functions& gl, std::vector<Shader*>& shaders, glw::GLenum binaryFormat, const void* binaryData, const int length)
511 std::vector<glw::GLuint> shaderVec;
512 for (deUint32 shaderNdx = 0; shaderNdx < shaders.size(); ++shaderNdx)
513 shaderVec.push_back(shaders[shaderNdx]->getShader());
515 gl.shaderBinary((glw::GLsizei)shaderVec.size(), shaderVec.data(), binaryFormat, binaryData, length);
516 GLU_EXPECT_NO_ERROR(gl.getError(), "glShaderBinary");
518 for (deUint32 shaderNdx = 0; shaderNdx < shaders.size(); ++shaderNdx)
520 glw::GLint shaderState;
521 gl.getShaderiv(shaders[shaderNdx]->getShader(), GL_SPIR_V_BINARY_ARB, &shaderState);
522 GLU_EXPECT_NO_ERROR(gl.getError(), "getShaderiv");
524 DE_ASSERT(shaderState == GL_TRUE);
528 ShaderProgram::~ShaderProgram (void)
530 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
531 for (int shaderNdx = 0; shaderNdx < (int)m_shaders[shaderType].size(); ++shaderNdx)
532 delete m_shaders[shaderType][shaderNdx];
537 deUint32 getGLShaderType (ShaderType shaderType)
539 static const deUint32 s_typeMap[] =
544 GL_TESS_CONTROL_SHADER,
545 GL_TESS_EVALUATION_SHADER,
548 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_typeMap) == SHADERTYPE_LAST);
549 DE_ASSERT(de::inBounds<int>(shaderType, 0, DE_LENGTH_OF_ARRAY(s_typeMap)));
550 return s_typeMap[shaderType];
553 deUint32 getGLShaderTypeBit (ShaderType shaderType)
555 static const deUint32 s_typebitMap[] =
557 GL_VERTEX_SHADER_BIT,
558 GL_FRAGMENT_SHADER_BIT,
559 GL_GEOMETRY_SHADER_BIT,
560 GL_TESS_CONTROL_SHADER_BIT,
561 GL_TESS_EVALUATION_SHADER_BIT,
562 GL_COMPUTE_SHADER_BIT
564 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_typebitMap) == SHADERTYPE_LAST);
565 DE_ASSERT(de::inBounds<int>(shaderType, 0, DE_LENGTH_OF_ARRAY(s_typebitMap)));
566 return s_typebitMap[shaderType];
569 qpShaderType getLogShaderType (ShaderType shaderType)
571 static const qpShaderType s_typeMap[] =
573 QP_SHADER_TYPE_VERTEX,
574 QP_SHADER_TYPE_FRAGMENT,
575 QP_SHADER_TYPE_GEOMETRY,
576 QP_SHADER_TYPE_TESS_CONTROL,
577 QP_SHADER_TYPE_TESS_EVALUATION,
578 QP_SHADER_TYPE_COMPUTE
580 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_typeMap) == SHADERTYPE_LAST);
581 DE_ASSERT(de::inBounds<int>(shaderType, 0, DE_LENGTH_OF_ARRAY(s_typeMap)));
582 return s_typeMap[shaderType];
585 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderInfo& shaderInfo)
587 return log << tcu::TestLog::Shader(getLogShaderType(shaderInfo.type), shaderInfo.source, shaderInfo.compileOk, shaderInfo.infoLog);
590 tcu::TestLog& operator<< (tcu::TestLog& log, const Shader& shader)
592 return log << tcu::TestLog::ShaderProgram(false, "Plain shader") << shader.getInfo() << tcu::TestLog::EndShaderProgram;
595 static void logShaderProgram (tcu::TestLog& log, const ProgramInfo& programInfo, size_t numShaders, const ShaderInfo* const* shaderInfos)
597 log << tcu::TestLog::ShaderProgram(programInfo.linkOk, programInfo.infoLog);
600 for (size_t shaderNdx = 0; shaderNdx < numShaders; ++shaderNdx)
601 log << *shaderInfos[shaderNdx];
605 log << tcu::TestLog::EndShaderProgram;
608 log << tcu::TestLog::EndShaderProgram;
615 const char* description;
616 } s_compileTimeDesc[] =
618 { "VertexCompileTime", "Vertex shader compile time" },
619 { "FragmentCompileTime", "Fragment shader compile time" },
620 { "GeometryCompileTime", "Geometry shader compile time" },
621 { "TessControlCompileTime", "Tesselation control shader compile time" },
622 { "TessEvaluationCompileTime", "Tesselation evaluation shader compile time" },
623 { "ComputeCompileTime", "Compute shader compile time" },
625 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(s_compileTimeDesc) == SHADERTYPE_LAST);
627 bool allShadersOk = true;
629 for (size_t shaderNdx = 0; shaderNdx < numShaders; ++shaderNdx)
631 const ShaderInfo& shaderInfo = *shaderInfos[shaderNdx];
633 log << tcu::TestLog::Float(s_compileTimeDesc[shaderInfo.type].name,
634 s_compileTimeDesc[shaderInfo.type].description,
635 "ms", QP_KEY_TAG_TIME, (float)shaderInfo.compileTimeUs / 1000.0f);
637 allShadersOk = allShadersOk && shaderInfo.compileOk;
641 log << tcu::TestLog::Float("LinkTime", "Link time", "ms", QP_KEY_TAG_TIME, (float)programInfo.linkTimeUs / 1000.0f);
645 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderProgramInfo& shaderProgramInfo)
647 std::vector<const ShaderInfo*> shaderPtrs (shaderProgramInfo.shaders.size());
649 for (size_t ndx = 0; ndx < shaderPtrs.size(); ndx++)
650 shaderPtrs[ndx] = &shaderProgramInfo.shaders[ndx];
652 logShaderProgram(log, shaderProgramInfo.program, shaderPtrs.size(), shaderPtrs.empty() ? DE_NULL : &shaderPtrs[0]);
657 tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderProgram& shaderProgram)
659 std::vector<const ShaderInfo*> shaderPtrs;
661 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
663 for (int shaderNdx = 0; shaderNdx < shaderProgram.getNumShaders((ShaderType)shaderType); shaderNdx++)
664 shaderPtrs.push_back(&shaderProgram.getShaderInfo((ShaderType)shaderType, shaderNdx));
667 logShaderProgram(log, shaderProgram.getProgramInfo(), shaderPtrs.size(), shaderPtrs.empty() ? DE_NULL : &shaderPtrs[0]);
672 tcu::TestLog& operator<< (tcu::TestLog& log, const ProgramSources& sources)
674 log << tcu::TestLog::ShaderProgram(false, "(Source only)");
678 for (int shaderType = 0; shaderType < SHADERTYPE_LAST; shaderType++)
680 for (size_t shaderNdx = 0; shaderNdx < sources.sources[shaderType].size(); shaderNdx++)
682 log << tcu::TestLog::Shader(getLogShaderType((ShaderType)shaderType),
683 sources.sources[shaderType][shaderNdx],
690 log << tcu::TestLog::EndShaderProgram;
694 log << tcu::TestLog::EndShaderProgram;