1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.1 Module
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 Program interface utilities
22 *//*--------------------------------------------------------------------*/
24 #include "es31fProgramInterfaceDefinitionUtil.hpp"
25 #include "es31fProgramInterfaceDefinition.hpp"
26 #include "gluVarType.hpp"
27 #include "gluVarTypeUtil.hpp"
28 #include "gluShaderUtil.hpp"
30 #include "deStringUtil.hpp"
31 #include "glwEnums.hpp"
45 namespace ProgramInterfaceDefinition
48 VariableSearchFilter::VariableSearchFilter (void)
49 : m_shaderTypeBits (0xFFFFFFFFul)
50 , m_storageBits (0xFFFFFFFFul)
54 VariableSearchFilter VariableSearchFilter::createShaderTypeFilter (glu::ShaderType type)
56 DE_ASSERT(type < glu::SHADERTYPE_LAST);
58 VariableSearchFilter filter;
59 filter.m_shaderTypeBits = (1u << type);
63 VariableSearchFilter VariableSearchFilter::createStorageFilter (glu::Storage storage)
65 DE_ASSERT(storage < glu::STORAGE_LAST);
67 VariableSearchFilter filter;
68 filter.m_storageBits = (1u << storage);
72 VariableSearchFilter VariableSearchFilter::createShaderTypeStorageFilter (glu::ShaderType type, glu::Storage storage)
74 return logicalAnd(createShaderTypeFilter(type), createStorageFilter(storage));
77 VariableSearchFilter VariableSearchFilter::logicalOr (const VariableSearchFilter& a, const VariableSearchFilter& b)
79 VariableSearchFilter filter;
80 filter.m_shaderTypeBits = a.m_shaderTypeBits | b.m_shaderTypeBits;
81 filter.m_storageBits = a.m_storageBits | b.m_storageBits;
85 VariableSearchFilter VariableSearchFilter::logicalAnd (const VariableSearchFilter& a, const VariableSearchFilter& b)
87 VariableSearchFilter filter;
88 filter.m_shaderTypeBits = a.m_shaderTypeBits & b.m_shaderTypeBits;
89 filter.m_storageBits = a.m_storageBits & b.m_storageBits;
93 bool VariableSearchFilter::matchesFilter (const ProgramInterfaceDefinition::Shader* shader) const
95 DE_ASSERT(shader->getType() < glu::SHADERTYPE_LAST);
96 return (m_shaderTypeBits & (1u << shader->getType())) != 0;
99 bool VariableSearchFilter::matchesFilter (const glu::VariableDeclaration& variable) const
101 DE_ASSERT(variable.storage < glu::STORAGE_LAST);
102 return (m_storageBits & (1u << variable.storage)) != 0;
105 bool VariableSearchFilter::matchesFilter (const glu::InterfaceBlock& block) const
107 DE_ASSERT(block.storage < glu::STORAGE_LAST);
108 return (m_storageBits & (1u << block.storage)) != 0;
111 } // ProgramInterfaceDefinition
113 static bool incrementMultiDimensionIndex (std::vector<int>& index, const std::vector<int>& dimensions)
115 int incrementDimensionNdx = (int)(index.size() - 1);
117 while (incrementDimensionNdx >= 0)
119 if (++index[incrementDimensionNdx] == dimensions[incrementDimensionNdx])
120 index[incrementDimensionNdx--] = 0;
125 return (incrementDimensionNdx != -1);
128 bool programContainsIOBlocks (const ProgramInterfaceDefinition::Program* program)
130 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
132 if (shaderContainsIOBlocks(program->getShaders()[shaderNdx]))
139 bool shaderContainsIOBlocks (const ProgramInterfaceDefinition::Shader* shader)
141 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
143 const glu::Storage storage = shader->getDefaultBlock().interfaceBlocks[ndx].storage;
144 if (storage == glu::STORAGE_IN ||
145 storage == glu::STORAGE_OUT ||
146 storage == glu::STORAGE_PATCH_IN ||
147 storage == glu::STORAGE_PATCH_OUT)
155 glu::ShaderType getProgramTransformFeedbackStage (const ProgramInterfaceDefinition::Program* program)
157 if (program->hasStage(glu::SHADERTYPE_GEOMETRY))
158 return glu::SHADERTYPE_GEOMETRY;
160 if (program->hasStage(glu::SHADERTYPE_TESSELLATION_EVALUATION))
161 return glu::SHADERTYPE_TESSELLATION_EVALUATION;
163 if (program->hasStage(glu::SHADERTYPE_VERTEX))
164 return glu::SHADERTYPE_VERTEX;
167 return glu::SHADERTYPE_LAST;
170 void generateVariableTypeResourceNames (std::vector<std::string>& resources, const std::string& name, const glu::VarType& type, deUint32 resourceNameGenerationFlags)
172 DE_ASSERT((resourceNameGenerationFlags & (~RESOURCE_NAME_GENERATION_FLAG_MASK)) == 0);
174 // remove top-level flag from children
175 const deUint32 childFlags = resourceNameGenerationFlags & ~((deUint32)RESOURCE_NAME_GENERATION_FLAG_TOP_LEVEL_BUFFER_VARIABLE);
177 if (type.isBasicType())
178 resources.push_back(name);
179 else if (type.isStructType())
181 const glu::StructType* structType = type.getStructPtr();
182 for (int ndx = 0; ndx < structType->getNumMembers(); ++ndx)
183 generateVariableTypeResourceNames(resources, name + "." + structType->getMember(ndx).getName(), structType->getMember(ndx).getType(), childFlags);
185 else if (type.isArrayType())
187 // Bottom-level arrays of basic types of a transform feedback variable will produce only the first
188 // element but without the trailing "[0]"
189 if (type.getElementType().isBasicType() &&
190 (resourceNameGenerationFlags & RESOURCE_NAME_GENERATION_FLAG_TRANSFORM_FEEDBACK_VARIABLE) != 0)
192 resources.push_back(name);
194 // Bottom-level arrays of basic types and SSBO top-level arrays of any type procude only first element
195 else if (type.getElementType().isBasicType() ||
196 (resourceNameGenerationFlags & RESOURCE_NAME_GENERATION_FLAG_TOP_LEVEL_BUFFER_VARIABLE) != 0)
198 generateVariableTypeResourceNames(resources, name + "[0]", type.getElementType(), childFlags);
200 // Other arrays of aggregate types are expanded
203 for (int ndx = 0; ndx < type.getArraySize(); ++ndx)
204 generateVariableTypeResourceNames(resources, name + "[" + de::toString(ndx) + "]", type.getElementType(), childFlags);
211 // Program source generation
216 using ProgramInterfaceDefinition::VariablePathComponent;
217 using ProgramInterfaceDefinition::VariableSearchFilter;
219 static std::string getShaderExtensionDeclarations (const ProgramInterfaceDefinition::Shader* shader)
221 std::vector<std::string> extensions;
222 std::ostringstream buf;
224 if (shader->getType() == glu::SHADERTYPE_GEOMETRY)
226 extensions.push_back("GL_EXT_geometry_shader");
228 else if (shader->getType() == glu::SHADERTYPE_TESSELLATION_CONTROL ||
229 shader->getType() == glu::SHADERTYPE_TESSELLATION_EVALUATION)
231 extensions.push_back("GL_EXT_tessellation_shader");
234 if (shaderContainsIOBlocks(shader))
235 extensions.push_back("GL_EXT_shader_io_blocks");
237 for (int ndx = 0; ndx < (int)extensions.size(); ++ndx)
238 buf << "#extension " << extensions[ndx] << " : require\n";
242 static std::string getShaderTypeDeclarations (const ProgramInterfaceDefinition::Program* program, glu::ShaderType type)
246 case glu::SHADERTYPE_VERTEX:
249 case glu::SHADERTYPE_FRAGMENT:
252 case glu::SHADERTYPE_GEOMETRY:
254 std::ostringstream buf;
255 buf << "layout(points) in;\n"
256 "layout(points, max_vertices=" << program->getGeometryNumOutputVertices() << ") out;\n";
260 case glu::SHADERTYPE_TESSELLATION_CONTROL:
262 std::ostringstream buf;
263 buf << "layout(vertices=" << program->getTessellationNumOutputPatchVertices() << ") out;\n";
267 case glu::SHADERTYPE_TESSELLATION_EVALUATION:
268 return "layout(triangles, point_mode) in;\n";
270 case glu::SHADERTYPE_COMPUTE:
271 return "layout(local_size_x=1) in;\n";
279 class StructNameEqualPredicate
282 StructNameEqualPredicate (const char* name) : m_name(name) { }
283 bool operator() (const glu::StructType* type) { return type->hasTypeName() && (deStringEqual(m_name, type->getTypeName()) == DE_TRUE); }
288 static void collectNamedStructureDefinitions (std::vector<const glu::StructType*>& dst, const glu::VarType& type)
290 if (type.isBasicType())
292 else if (type.isArrayType())
293 return collectNamedStructureDefinitions(dst, type.getElementType());
294 else if (type.isStructType())
296 if (type.getStructPtr()->hasTypeName())
298 // must be unique (may share the the same struct)
299 std::vector<const glu::StructType*>::iterator where = std::find_if(dst.begin(), dst.end(), StructNameEqualPredicate(type.getStructPtr()->getTypeName()));
300 if (where != dst.end())
302 DE_ASSERT(**where == *type.getStructPtr());
304 // identical type has been added already, types of members must be added too
309 // Add types of members first
310 for (int ndx = 0; ndx < type.getStructPtr()->getNumMembers(); ++ndx)
311 collectNamedStructureDefinitions(dst, type.getStructPtr()->getMember(ndx).getType());
313 dst.push_back(type.getStructPtr());
319 static void writeStructureDefinitions (std::ostringstream& buf, const ProgramInterfaceDefinition::DefaultBlock& defaultBlock)
321 std::vector<const glu::StructType*> namedStructs;
323 // Collect all structs in post order
325 for (int ndx = 0; ndx < (int)defaultBlock.variables.size(); ++ndx)
326 collectNamedStructureDefinitions(namedStructs, defaultBlock.variables[ndx].varType);
328 for (int blockNdx = 0; blockNdx < (int)defaultBlock.interfaceBlocks.size(); ++blockNdx)
329 for (int ndx = 0; ndx < (int)defaultBlock.interfaceBlocks[blockNdx].variables.size(); ++ndx)
330 collectNamedStructureDefinitions(namedStructs, defaultBlock.interfaceBlocks[blockNdx].variables[ndx].varType);
334 for (int structNdx = 0; structNdx < (int)namedStructs.size(); ++structNdx)
336 buf << "struct " << namedStructs[structNdx]->getTypeName() << "\n"
339 for (int memberNdx = 0; memberNdx < namedStructs[structNdx]->getNumMembers(); ++memberNdx)
340 buf << glu::indent(1) << glu::declare(namedStructs[structNdx]->getMember(memberNdx).getType(), namedStructs[structNdx]->getMember(memberNdx).getName(), 1) << ";\n";
345 if (!namedStructs.empty())
349 static void writeInterfaceBlock (std::ostringstream& buf, const glu::InterfaceBlock& interfaceBlock)
351 buf << interfaceBlock.layout;
353 if (interfaceBlock.layout != glu::Layout())
356 buf << glu::getStorageName(interfaceBlock.storage) << " " << interfaceBlock.interfaceName << "\n"
359 for (int ndx = 0; ndx < (int)interfaceBlock.variables.size(); ++ndx)
360 buf << glu::indent(1) << interfaceBlock.variables[ndx] << ";\n";
364 if (!interfaceBlock.instanceName.empty())
365 buf << " " << interfaceBlock.instanceName;
367 for (int dimensionNdx = 0; dimensionNdx < (int)interfaceBlock.dimensions.size(); ++dimensionNdx)
368 buf << "[" << interfaceBlock.dimensions[dimensionNdx] << "]";
373 static bool isReadableInterface (const glu::InterfaceBlock& interface)
375 return interface.storage == glu::STORAGE_UNIFORM ||
376 interface.storage == glu::STORAGE_IN ||
377 interface.storage == glu::STORAGE_PATCH_IN ||
378 (interface.storage == glu::STORAGE_BUFFER && (interface.memoryAccessQualifierFlags & glu::MEMORYACCESSQUALIFIER_WRITEONLY_BIT) == 0);
381 static bool isWritableInterface (const glu::InterfaceBlock& interface)
383 return interface.storage == glu::STORAGE_OUT ||
384 interface.storage == glu::STORAGE_PATCH_OUT ||
385 (interface.storage == glu::STORAGE_BUFFER && (interface.memoryAccessQualifierFlags & glu::MEMORYACCESSQUALIFIER_READONLY_BIT) == 0);
389 static void writeVariableReadAccumulateExpression (std::ostringstream& buf,
390 const std::string& accumulatorName,
391 const std::string& name,
392 glu::ShaderType shaderType,
393 glu::Storage storage,
394 const ProgramInterfaceDefinition::Program* program,
395 const glu::VarType& varType)
397 if (varType.isBasicType())
399 buf << "\t" << accumulatorName << " += ";
401 if (glu::isDataTypeScalar(varType.getBasicType()))
402 buf << "vec4(float(" << name << "))";
403 else if (glu::isDataTypeVector(varType.getBasicType()))
404 buf << "vec4(" << name << ".xyxy)";
405 else if (glu::isDataTypeMatrix(varType.getBasicType()))
406 buf << "vec4(float(" << name << "[0][0]))";
407 else if (glu::isDataTypeSamplerMultisample(varType.getBasicType()))
408 buf << "vec4(float(textureSize(" << name << ").x))";
409 else if (glu::isDataTypeSampler(varType.getBasicType()))
410 buf << "vec4(float(textureSize(" << name << ", 0).x))";
411 else if (glu::isDataTypeImage(varType.getBasicType()))
412 buf << "vec4(float(imageSize(" << name << ").x))";
413 else if (varType.getBasicType() == glu::TYPE_UINT_ATOMIC_COUNTER)
414 buf << "vec4(float(atomicCounterIncrement(" << name << ")))";
420 else if (varType.isStructType())
422 for (int ndx = 0; ndx < varType.getStructPtr()->getNumMembers(); ++ndx)
423 writeVariableReadAccumulateExpression(buf,
425 name + "." + varType.getStructPtr()->getMember(ndx).getName(),
429 varType.getStructPtr()->getMember(ndx).getType());
431 else if (varType.isArrayType())
433 if (varType.getArraySize() != glu::VarType::UNSIZED_ARRAY)
435 for (int ndx = 0; ndx < varType.getArraySize(); ++ndx)
436 writeVariableReadAccumulateExpression(buf,
438 name + "[" + de::toString(ndx) + "]",
442 varType.getElementType());
444 else if (storage == glu::STORAGE_BUFFER)
446 // run-time sized array, read arbitrary
447 writeVariableReadAccumulateExpression(buf,
453 varType.getElementType());
457 DE_ASSERT(storage == glu::STORAGE_IN);
459 if (shaderType == glu::SHADERTYPE_GEOMETRY)
461 // implicit sized geometry input array, size = primitive size. Just reading first is enough
462 writeVariableReadAccumulateExpression(buf,
468 varType.getElementType());
470 else if (shaderType == glu::SHADERTYPE_TESSELLATION_CONTROL)
472 // implicit sized tessellation input array, size = input patch max size. Just reading current is enough
473 writeVariableReadAccumulateExpression(buf,
475 name + "[gl_InvocationID]",
479 varType.getElementType());
481 else if (shaderType == glu::SHADERTYPE_TESSELLATION_EVALUATION)
483 // implicit sized tessellation input array, size = output patch max size. Read all to prevent optimizations
484 DE_ASSERT(program->getTessellationNumOutputPatchVertices() > 0);
485 for (int ndx = 0; ndx < (int)program->getTessellationNumOutputPatchVertices(); ++ndx)
487 writeVariableReadAccumulateExpression(buf,
489 name + "[" + de::toString(ndx) + "]",
493 varType.getElementType());
504 static void writeInterfaceReadAccumulateExpression (std::ostringstream& buf,
505 const std::string& accumulatorName,
506 const glu::InterfaceBlock& block,
507 glu::ShaderType shaderType,
508 const ProgramInterfaceDefinition::Program* program)
510 if (block.dimensions.empty())
512 const std::string prefix = (block.instanceName.empty()) ? ("") : (block.instanceName + ".");
514 for (int ndx = 0; ndx < (int)block.variables.size(); ++ndx)
516 writeVariableReadAccumulateExpression(buf,
518 prefix + block.variables[ndx].name,
522 block.variables[ndx].varType);
527 std::vector<int> index(block.dimensions.size(), 0);
533 std::ostringstream name;
534 name << block.instanceName;
536 for (int dimensionNdx = 0; dimensionNdx < (int)block.dimensions.size(); ++dimensionNdx)
537 name << "[" << index[dimensionNdx] << "]";
539 for (int ndx = 0; ndx < (int)block.variables.size(); ++ndx)
541 writeVariableReadAccumulateExpression(buf,
543 name.str() + "." + block.variables[ndx].name,
547 block.variables[ndx].varType);
552 if (!incrementMultiDimensionIndex(index, block.dimensions))
558 static void writeVariableWriteExpression (std::ostringstream& buf,
559 const std::string& sourceVec4Name,
560 const std::string& name,
561 glu::ShaderType shaderType,
562 glu::Storage storage,
563 const ProgramInterfaceDefinition::Program* program,
564 const glu::VarType& varType)
566 if (varType.isBasicType())
568 buf << "\t" << name << " = ";
570 if (glu::isDataTypeScalar(varType.getBasicType()))
571 buf << glu::getDataTypeName(varType.getBasicType()) << "(" << sourceVec4Name << ".y)";
572 else if (glu::isDataTypeVector(varType.getBasicType()) || glu::isDataTypeMatrix(varType.getBasicType()))
573 buf << glu::getDataTypeName(varType.getBasicType()) << "(" << glu::getDataTypeName(glu::getDataTypeScalarType(varType.getBasicType())) << "(" << sourceVec4Name << ".y))";
579 else if (varType.isStructType())
581 for (int ndx = 0; ndx < varType.getStructPtr()->getNumMembers(); ++ndx)
582 writeVariableWriteExpression(buf,
584 name + "." + varType.getStructPtr()->getMember(ndx).getName(),
588 varType.getStructPtr()->getMember(ndx).getType());
590 else if (varType.isArrayType())
592 if (varType.getArraySize() != glu::VarType::UNSIZED_ARRAY)
594 for (int ndx = 0; ndx < varType.getArraySize(); ++ndx)
595 writeVariableWriteExpression(buf,
597 name + "[" + de::toString(ndx) + "]",
601 varType.getElementType());
603 else if (storage == glu::STORAGE_BUFFER)
605 // run-time sized array, write arbitrary
606 writeVariableWriteExpression(buf,
612 varType.getElementType());
616 DE_ASSERT(storage == glu::STORAGE_OUT);
618 if (shaderType == glu::SHADERTYPE_TESSELLATION_CONTROL)
620 // implicit sized tessellation onput array, size = output patch max size. Can only write to gl_InvocationID
621 writeVariableWriteExpression(buf,
623 name + "[gl_InvocationID]",
627 varType.getElementType());
637 static void writeInterfaceWriteExpression (std::ostringstream& buf,
638 const std::string& sourceVec4Name,
639 const glu::InterfaceBlock& block,
640 glu::ShaderType shaderType,
641 const ProgramInterfaceDefinition::Program* program)
643 if (block.dimensions.empty())
645 const std::string prefix = (block.instanceName.empty()) ? ("") : (block.instanceName + ".");
647 for (int ndx = 0; ndx < (int)block.variables.size(); ++ndx)
649 writeVariableWriteExpression(buf,
651 prefix + block.variables[ndx].name,
655 block.variables[ndx].varType);
660 std::vector<int> index(block.dimensions.size(), 0);
666 std::ostringstream name;
667 name << block.instanceName;
669 for (int dimensionNdx = 0; dimensionNdx < (int)block.dimensions.size(); ++dimensionNdx)
670 name << "[" << index[dimensionNdx] << "]";
672 for (int ndx = 0; ndx < (int)block.variables.size(); ++ndx)
674 writeVariableWriteExpression(buf,
676 name.str() + "." + block.variables[ndx].name,
680 block.variables[ndx].varType);
685 if (!incrementMultiDimensionIndex(index, block.dimensions))
691 static bool traverseVariablePath (std::vector<VariablePathComponent>& typePath, const char* subPath, const glu::VarType& type)
693 glu::VarTokenizer tokenizer(subPath);
695 typePath.push_back(VariablePathComponent(&type));
697 if (tokenizer.getToken() == glu::VarTokenizer::TOKEN_END)
700 if (type.isStructType() && tokenizer.getToken() == glu::VarTokenizer::TOKEN_PERIOD)
705 if (tokenizer.getToken() != glu::VarTokenizer::TOKEN_IDENTIFIER)
708 for (int memberNdx = 0; memberNdx < type.getStructPtr()->getNumMembers(); ++memberNdx)
709 if (type.getStructPtr()->getMember(memberNdx).getName() == tokenizer.getIdentifier())
710 return traverseVariablePath(typePath, subPath + tokenizer.getCurrentTokenEndLocation(), type.getStructPtr()->getMember(memberNdx).getType());
712 // malformed path, no such member
715 else if (type.isArrayType() && tokenizer.getToken() == glu::VarTokenizer::TOKEN_LEFT_BRACKET)
720 if (tokenizer.getToken() != glu::VarTokenizer::TOKEN_NUMBER)
724 if (tokenizer.getToken() != glu::VarTokenizer::TOKEN_RIGHT_BRACKET)
727 return traverseVariablePath(typePath, subPath + tokenizer.getCurrentTokenEndLocation(), type.getElementType());
733 static bool traverseVariablePath (std::vector<VariablePathComponent>& typePath, const std::string& path, const glu::VariableDeclaration& var)
735 if (glu::parseVariableName(path.c_str()) != var.name)
738 typePath.push_back(VariablePathComponent(&var));
739 return traverseVariablePath(typePath, path.c_str() + var.name.length(), var.varType);
742 static bool traverseShaderVariablePath (std::vector<VariablePathComponent>& typePath, const ProgramInterfaceDefinition::Shader* shader, const std::string& path, const VariableSearchFilter& filter)
744 // Default block variable?
745 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().variables.size(); ++varNdx)
746 if (filter.matchesFilter(shader->getDefaultBlock().variables[varNdx]))
747 if (traverseVariablePath(typePath, path, shader->getDefaultBlock().variables[varNdx]))
750 // is variable an interface block variable?
752 const std::string blockName = glu::parseVariableName(path.c_str());
754 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
756 if (!filter.matchesFilter(shader->getDefaultBlock().interfaceBlocks[interfaceNdx]))
759 if (shader->getDefaultBlock().interfaceBlocks[interfaceNdx].interfaceName == blockName)
761 // resource is a member of a named interface block
762 // \note there is no array index specifier even if the interface is declared as an array of instances
763 const std::string blockMemberPath = path.substr(blockName.size() + 1);
764 const std::string blockMemeberName = glu::parseVariableName(blockMemberPath.c_str());
766 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables.size(); ++varNdx)
768 if (shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables[varNdx].name == blockMemeberName)
770 typePath.push_back(VariablePathComponent(&shader->getDefaultBlock().interfaceBlocks[interfaceNdx]));
771 return traverseVariablePath(typePath, blockMemberPath, shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables[varNdx]);
778 else if (shader->getDefaultBlock().interfaceBlocks[interfaceNdx].instanceName.empty())
780 const std::string blockMemeberName = glu::parseVariableName(path.c_str());
782 // unnamed block contains such variable?
783 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables.size(); ++varNdx)
785 if (shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables[varNdx].name == blockMemeberName)
787 typePath.push_back(VariablePathComponent(&shader->getDefaultBlock().interfaceBlocks[interfaceNdx]));
788 return traverseVariablePath(typePath, path, shader->getDefaultBlock().interfaceBlocks[interfaceNdx].variables[varNdx]);
800 static bool traverseProgramVariablePath (std::vector<VariablePathComponent>& typePath, const ProgramInterfaceDefinition::Program* program, const std::string& path, const VariableSearchFilter& filter)
802 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
804 const ProgramInterfaceDefinition::Shader* shader = program->getShaders()[shaderNdx];
806 if (filter.matchesFilter(shader))
808 // \note modifying output variable even when returning false
810 if (traverseShaderVariablePath(typePath, shader, path, filter))
818 static bool containsSubType (const glu::VarType& complexType, glu::DataType basicType)
820 if (complexType.isBasicType())
822 return complexType.getBasicType() == basicType;
824 else if (complexType.isArrayType())
826 return containsSubType(complexType.getElementType(), basicType);
828 else if (complexType.isStructType())
830 for (int ndx = 0; ndx < complexType.getStructPtr()->getNumMembers(); ++ndx)
831 if (containsSubType(complexType.getStructPtr()->getMember(ndx).getType(), basicType))
842 static int getNumShaderBlocks (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
846 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
848 if (shader->getDefaultBlock().interfaceBlocks[ndx].storage == storage)
850 int numInstances = 1;
852 for (int dimensionNdx = 0; dimensionNdx < (int)shader->getDefaultBlock().interfaceBlocks[ndx].dimensions.size(); ++dimensionNdx)
853 numInstances *= shader->getDefaultBlock().interfaceBlocks[ndx].dimensions[dimensionNdx];
855 retVal += numInstances;
862 static int getNumAtomicCounterBuffers (const ProgramInterfaceDefinition::Shader* shader)
864 std::set<int> buffers;
866 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
868 if (containsSubType(shader->getDefaultBlock().variables[ndx].varType, glu::TYPE_UINT_ATOMIC_COUNTER))
870 DE_ASSERT(shader->getDefaultBlock().variables[ndx].layout.binding != -1);
871 buffers.insert(shader->getDefaultBlock().variables[ndx].layout.binding);
875 return (int)buffers.size();
878 template <typename DataTypeMap>
879 static int accumulateComplexType (const glu::VarType& complexType, const DataTypeMap& dTypeMap)
881 if (complexType.isBasicType())
882 return dTypeMap(complexType.getBasicType());
883 else if (complexType.isArrayType())
885 const int arraySize = (complexType.getArraySize() == glu::VarType::UNSIZED_ARRAY) ? (1) : (complexType.getArraySize());
886 return arraySize * accumulateComplexType(complexType.getElementType(), dTypeMap);
888 else if (complexType.isStructType())
891 for (int ndx = 0; ndx < complexType.getStructPtr()->getNumMembers(); ++ndx)
892 sum += accumulateComplexType(complexType.getStructPtr()->getMember(ndx).getType(), dTypeMap);
902 template <typename InterfaceBlockFilter, typename VarDeclFilter, typename DataTypeMap>
903 static int accumulateShader (const ProgramInterfaceDefinition::Shader* shader,
904 const InterfaceBlockFilter& ibFilter,
905 const VarDeclFilter& vdFilter,
906 const DataTypeMap& dMap)
910 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
912 if (ibFilter(shader->getDefaultBlock().interfaceBlocks[ndx]))
914 int numInstances = 1;
916 for (int dimensionNdx = 0; dimensionNdx < (int)shader->getDefaultBlock().interfaceBlocks[ndx].dimensions.size(); ++dimensionNdx)
917 numInstances *= shader->getDefaultBlock().interfaceBlocks[ndx].dimensions[dimensionNdx];
919 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().interfaceBlocks[ndx].variables.size(); ++varNdx)
920 retVal += numInstances * accumulateComplexType(shader->getDefaultBlock().interfaceBlocks[ndx].variables[varNdx].varType, dMap);
924 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().variables.size(); ++varNdx)
925 if (vdFilter(shader->getDefaultBlock().variables[varNdx]))
926 retVal += accumulateComplexType(shader->getDefaultBlock().variables[varNdx].varType, dMap);
931 static bool dummyTrueConstantTypeFilter (glu::DataType d)
937 class InstanceCounter
940 InstanceCounter (bool (*predicate)(glu::DataType))
941 : m_predicate(predicate)
945 int operator() (glu::DataType t) const
947 return (m_predicate(t)) ? (1) : (0);
951 bool (*const m_predicate)(glu::DataType);
954 class InterfaceBlockStorageFilter
957 InterfaceBlockStorageFilter (glu::Storage storage)
962 bool operator() (const glu::InterfaceBlock& b) const
964 return m_storage == b.storage;
968 const glu::Storage m_storage;
971 class VariableDeclarationStorageFilter
974 VariableDeclarationStorageFilter (glu::Storage storage)
979 bool operator() (const glu::VariableDeclaration& d) const
981 return m_storage == d.storage;
985 const glu::Storage m_storage;
988 static int getNumTypeInstances (const glu::VarType& complexType, bool (*predicate)(glu::DataType))
990 return accumulateComplexType(complexType, InstanceCounter(predicate));
993 static int getNumTypeInstances (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage, bool (*predicate)(glu::DataType))
995 return accumulateShader(shader, InterfaceBlockStorageFilter(storage), VariableDeclarationStorageFilter(storage), InstanceCounter(predicate));
998 static int getNumTypeInstances (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1000 return getNumTypeInstances(shader, storage, dummyTrueConstantTypeFilter);
1003 static int accumulateShaderStorage (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage, int (*typeMap)(glu::DataType))
1005 return accumulateShader(shader, InterfaceBlockStorageFilter(storage), VariableDeclarationStorageFilter(storage), typeMap);
1008 static int getNumDataTypeComponents (glu::DataType type)
1010 if (glu::isDataTypeScalarOrVector(type) || glu::isDataTypeMatrix(type))
1011 return glu::getDataTypeScalarSize(type);
1016 static int getNumDataTypeVectors (glu::DataType type)
1018 if (glu::isDataTypeScalar(type))
1020 else if (glu::isDataTypeVector(type))
1022 else if (glu::isDataTypeMatrix(type))
1023 return glu::getDataTypeMatrixNumColumns(type);
1028 static int getNumComponents (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1030 return accumulateShaderStorage(shader, storage, getNumDataTypeComponents);
1033 static int getNumVectors (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1035 return accumulateShaderStorage(shader, storage, getNumDataTypeVectors);
1038 static int getNumDefaultBlockComponents (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1042 for (int varNdx = 0; varNdx < (int)shader->getDefaultBlock().variables.size(); ++varNdx)
1043 if (shader->getDefaultBlock().variables[varNdx].storage == storage)
1044 retVal += accumulateComplexType(shader->getDefaultBlock().variables[varNdx].varType, getNumDataTypeComponents);
1049 static int getMaxBufferBinding (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1051 int maxBinding = -1;
1053 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
1055 if (shader->getDefaultBlock().interfaceBlocks[ndx].storage == storage)
1057 const int binding = (shader->getDefaultBlock().interfaceBlocks[ndx].layout.binding == -1) ? (0) : (shader->getDefaultBlock().interfaceBlocks[ndx].layout.binding);
1058 int numInstances = 1;
1060 for (int dimensionNdx = 0; dimensionNdx < (int)shader->getDefaultBlock().interfaceBlocks[ndx].dimensions.size(); ++dimensionNdx)
1061 numInstances *= shader->getDefaultBlock().interfaceBlocks[ndx].dimensions[dimensionNdx];
1063 maxBinding = de::max(maxBinding, binding + numInstances - 1);
1067 return (int)maxBinding;
1070 static int getBufferTypeSize (glu::DataType type, glu::MatrixOrder order)
1072 // assume vec4 alignments, should produce values greater than or equal to the actual resource usage
1075 if (glu::isDataTypeScalarOrVector(type))
1077 else if (glu::isDataTypeMatrix(type) && order == glu::MATRIXORDER_ROW_MAJOR)
1078 numVectors = glu::getDataTypeMatrixNumRows(type);
1079 else if (glu::isDataTypeMatrix(type) && order != glu::MATRIXORDER_ROW_MAJOR)
1080 numVectors = glu::getDataTypeMatrixNumColumns(type);
1084 return 4 * numVectors;
1087 static int getBufferVariableSize (const glu::VarType& type, glu::MatrixOrder order)
1089 if (type.isBasicType())
1090 return getBufferTypeSize(type.getBasicType(), order);
1091 else if (type.isArrayType())
1093 const int arraySize = (type.getArraySize() == glu::VarType::UNSIZED_ARRAY) ? (1) : (type.getArraySize());
1094 return arraySize * getBufferVariableSize(type.getElementType(), order);
1096 else if (type.isStructType())
1099 for (int ndx = 0; ndx < type.getStructPtr()->getNumMembers(); ++ndx)
1100 sum += getBufferVariableSize(type.getStructPtr()->getMember(ndx).getType(), order);
1110 static int getBufferSize (const glu::InterfaceBlock& block, glu::MatrixOrder blockOrder)
1114 for (int ndx = 0; ndx < (int)block.variables.size(); ++ndx)
1115 size += getBufferVariableSize(block.variables[ndx].varType, (block.variables[ndx].layout.matrixOrder == glu::MATRIXORDER_LAST) ? (blockOrder) : (block.variables[ndx].layout.matrixOrder));
1120 static int getBufferMaxSize (const ProgramInterfaceDefinition::Shader* shader, glu::Storage storage)
1124 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
1125 if (shader->getDefaultBlock().interfaceBlocks[ndx].storage == storage)
1126 maxSize = de::max(maxSize, getBufferSize(shader->getDefaultBlock().interfaceBlocks[ndx], shader->getDefaultBlock().interfaceBlocks[ndx].layout.matrixOrder));
1128 return (int)maxSize;
1131 static int getAtomicCounterMaxBinding (const ProgramInterfaceDefinition::Shader* shader)
1133 int maxBinding = -1;
1135 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1137 if (containsSubType(shader->getDefaultBlock().variables[ndx].varType, glu::TYPE_UINT_ATOMIC_COUNTER))
1139 DE_ASSERT(shader->getDefaultBlock().variables[ndx].layout.binding != -1);
1140 maxBinding = de::max(maxBinding, shader->getDefaultBlock().variables[ndx].layout.binding);
1144 return (int)maxBinding;
1147 static int getUniformMaxBinding (const ProgramInterfaceDefinition::Shader* shader, bool (*predicate)(glu::DataType))
1149 int maxBinding = -1;
1151 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1153 const int binding = (shader->getDefaultBlock().variables[ndx].layout.binding == -1) ? (0) : (shader->getDefaultBlock().variables[ndx].layout.binding);
1154 const int numInstances = getNumTypeInstances(shader->getDefaultBlock().variables[ndx].varType, predicate);
1156 maxBinding = de::max(maxBinding, binding + numInstances - 1);
1162 static int getAtomicCounterMaxBufferSize (const ProgramInterfaceDefinition::Shader* shader)
1164 std::map<int, int> bufferSizes;
1167 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1169 if (containsSubType(shader->getDefaultBlock().variables[ndx].varType, glu::TYPE_UINT_ATOMIC_COUNTER))
1171 const int bufferBinding = shader->getDefaultBlock().variables[ndx].layout.binding;
1172 const int offset = (shader->getDefaultBlock().variables[ndx].layout.offset == -1) ? (0) : (shader->getDefaultBlock().variables[ndx].layout.offset);
1173 const int size = offset + 4 * getNumTypeInstances(shader->getDefaultBlock().variables[ndx].varType, glu::isDataTypeAtomicCounter);
1175 DE_ASSERT(shader->getDefaultBlock().variables[ndx].layout.binding != -1);
1177 if (bufferSizes.find(bufferBinding) == bufferSizes.end())
1178 bufferSizes[bufferBinding] = size;
1180 bufferSizes[bufferBinding] = de::max<int>(bufferSizes[bufferBinding], size);
1184 for (std::map<int, int>::iterator it = bufferSizes.begin(); it != bufferSizes.end(); ++it)
1185 maxSize = de::max<int>(maxSize, it->second);
1190 static int getNumFeedbackVaryingComponents (const ProgramInterfaceDefinition::Program* program, const std::string& name)
1192 std::vector<VariablePathComponent> path;
1194 if (name == "gl_Position")
1197 DE_ASSERT(deStringBeginsWith(name.c_str(), "gl_") == DE_FALSE);
1199 if (!traverseProgramVariablePath(path, program, name, VariableSearchFilter::createShaderTypeStorageFilter(getProgramTransformFeedbackStage(program), glu::STORAGE_OUT)))
1200 DE_ASSERT(false); // Program failed validate, invalid operation
1202 return accumulateComplexType(*path.back().getVariableType(), getNumDataTypeComponents);
1205 static int getNumXFBComponents (const ProgramInterfaceDefinition::Program* program)
1207 int numComponents = 0;
1209 for (int ndx = 0; ndx < (int)program->getTransformFeedbackVaryings().size(); ++ndx)
1210 numComponents += getNumFeedbackVaryingComponents(program, program->getTransformFeedbackVaryings()[ndx]);
1212 return numComponents;
1215 static int getNumMaxXFBOutputComponents (const ProgramInterfaceDefinition::Program* program)
1217 int numComponents = 0;
1219 for (int ndx = 0; ndx < (int)program->getTransformFeedbackVaryings().size(); ++ndx)
1220 numComponents = de::max(numComponents, getNumFeedbackVaryingComponents(program, program->getTransformFeedbackVaryings()[ndx]));
1222 return numComponents;
1225 static int getFragmentOutputMaxLocation (const ProgramInterfaceDefinition::Shader* shader)
1227 DE_ASSERT(shader->getType() == glu::SHADERTYPE_FRAGMENT);
1229 int maxOutputLocation = -1;
1231 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1233 if (shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_OUT)
1235 // missing location qualifier means location == 0
1236 const int outputLocation = (shader->getDefaultBlock().variables[ndx].layout.location == -1)
1238 : (shader->getDefaultBlock().variables[ndx].layout.location);
1240 // only basic types or arrays of basic types possible
1241 DE_ASSERT(!shader->getDefaultBlock().variables[ndx].varType.isStructType());
1243 const int locationSlotsTaken = (shader->getDefaultBlock().variables[ndx].varType.isArrayType())
1244 ? (shader->getDefaultBlock().variables[ndx].varType.getArraySize())
1247 maxOutputLocation = de::max(maxOutputLocation, outputLocation + locationSlotsTaken - 1);
1251 return maxOutputLocation;
1256 std::vector<std::string> getProgramInterfaceBlockMemberResourceList (const glu::InterfaceBlock& interfaceBlock)
1258 const std::string namePrefix = (!interfaceBlock.instanceName.empty()) ? (interfaceBlock.interfaceName + ".") : ("");
1259 const bool isTopLevelBufferVariable = (interfaceBlock.storage == glu::STORAGE_BUFFER);
1260 std::vector<std::string> resources;
1262 // \note this is defined in the GLSL spec, not in the GL spec
1263 for (int variableNdx = 0; variableNdx < (int)interfaceBlock.variables.size(); ++variableNdx)
1264 generateVariableTypeResourceNames(resources,
1265 namePrefix + interfaceBlock.variables[variableNdx].name,
1266 interfaceBlock.variables[variableNdx].varType,
1267 (isTopLevelBufferVariable) ?
1268 (RESOURCE_NAME_GENERATION_FLAG_TOP_LEVEL_BUFFER_VARIABLE) :
1269 (RESOURCE_NAME_GENERATION_FLAG_DEFAULT));
1274 std::vector<std::string> getProgramInterfaceResourceList (const ProgramInterfaceDefinition::Program* program, ProgramInterface interface)
1276 // The same {uniform (block), buffer (variable)} can exist in multiple shaders, remove duplicates but keep order
1277 const bool removeDuplicated = (interface == PROGRAMINTERFACE_UNIFORM) ||
1278 (interface == PROGRAMINTERFACE_UNIFORM_BLOCK) ||
1279 (interface == PROGRAMINTERFACE_BUFFER_VARIABLE) ||
1280 (interface == PROGRAMINTERFACE_SHADER_STORAGE_BLOCK);
1281 std::vector<std::string> resources;
1285 case PROGRAMINTERFACE_UNIFORM:
1286 case PROGRAMINTERFACE_BUFFER_VARIABLE:
1288 const glu::Storage storage = (interface == PROGRAMINTERFACE_UNIFORM) ? (glu::STORAGE_UNIFORM) : (glu::STORAGE_BUFFER);
1290 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
1292 const ProgramInterfaceDefinition::Shader* shader = program->getShaders()[shaderNdx];
1294 for (int variableNdx = 0; variableNdx < (int)shader->getDefaultBlock().variables.size(); ++variableNdx)
1295 if (shader->getDefaultBlock().variables[variableNdx].storage == storage)
1296 generateVariableTypeResourceNames(resources,
1297 shader->getDefaultBlock().variables[variableNdx].name,
1298 shader->getDefaultBlock().variables[variableNdx].varType,
1299 RESOURCE_NAME_GENERATION_FLAG_DEFAULT);
1301 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
1303 const glu::InterfaceBlock& interfaceBlock = shader->getDefaultBlock().interfaceBlocks[interfaceNdx];
1304 if (interfaceBlock.storage == storage)
1306 const std::vector<std::string> blockResources = getProgramInterfaceBlockMemberResourceList(interfaceBlock);
1307 resources.insert(resources.end(), blockResources.begin(), blockResources.end());
1314 case PROGRAMINTERFACE_UNIFORM_BLOCK:
1315 case PROGRAMINTERFACE_SHADER_STORAGE_BLOCK:
1317 const glu::Storage storage = (interface == PROGRAMINTERFACE_UNIFORM_BLOCK) ? (glu::STORAGE_UNIFORM) : (glu::STORAGE_BUFFER);
1319 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
1321 const ProgramInterfaceDefinition::Shader* shader = program->getShaders()[shaderNdx];
1322 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
1324 const glu::InterfaceBlock& interfaceBlock = shader->getDefaultBlock().interfaceBlocks[interfaceNdx];
1325 if (interfaceBlock.storage == storage)
1327 std::vector<int> index(interfaceBlock.dimensions.size(), 0);
1331 // add resource string for each element
1333 std::ostringstream name;
1334 name << interfaceBlock.interfaceName;
1336 for (int dimensionNdx = 0; dimensionNdx < (int)interfaceBlock.dimensions.size(); ++dimensionNdx)
1337 name << "[" << index[dimensionNdx] << "]";
1339 resources.push_back(name.str());
1343 if (!incrementMultiDimensionIndex(index, interfaceBlock.dimensions))
1352 case PROGRAMINTERFACE_PROGRAM_INPUT:
1353 case PROGRAMINTERFACE_PROGRAM_OUTPUT:
1355 const glu::Storage queryStorage = (interface == PROGRAMINTERFACE_PROGRAM_INPUT) ? (glu::STORAGE_IN) : (glu::STORAGE_OUT);
1356 const glu::Storage queryPatchStorage = (interface == PROGRAMINTERFACE_PROGRAM_INPUT) ? (glu::STORAGE_PATCH_IN) : (glu::STORAGE_PATCH_OUT);
1357 const glu::ShaderType shaderType = (interface == PROGRAMINTERFACE_PROGRAM_INPUT) ? (program->getFirstStage()) : (program->getLastStage());
1359 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
1361 const ProgramInterfaceDefinition::Shader* shader = program->getShaders()[shaderNdx];
1363 if (shader->getType() != shaderType)
1366 for (int variableNdx = 0; variableNdx < (int)shader->getDefaultBlock().variables.size(); ++variableNdx)
1368 const glu::Storage variableStorage = shader->getDefaultBlock().variables[variableNdx].storage;
1369 if (variableStorage == queryStorage || variableStorage == queryPatchStorage)
1370 generateVariableTypeResourceNames(resources,
1371 shader->getDefaultBlock().variables[variableNdx].name,
1372 shader->getDefaultBlock().variables[variableNdx].varType,
1373 RESOURCE_NAME_GENERATION_FLAG_DEFAULT);
1376 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
1378 const glu::InterfaceBlock& interfaceBlock = shader->getDefaultBlock().interfaceBlocks[interfaceNdx];
1379 if (interfaceBlock.storage == queryStorage || interfaceBlock.storage == queryPatchStorage)
1381 const std::vector<std::string> blockResources = getProgramInterfaceBlockMemberResourceList(interfaceBlock);
1382 resources.insert(resources.end(), blockResources.begin(), blockResources.end());
1388 if (interface == PROGRAMINTERFACE_PROGRAM_INPUT)
1390 if (shaderType == glu::SHADERTYPE_VERTEX && resources.empty())
1391 resources.push_back("gl_VertexID"); // only read from when there are no other inputs
1392 else if (shaderType == glu::SHADERTYPE_FRAGMENT && resources.empty())
1393 resources.push_back("gl_FragCoord"); // only read from when there are no other inputs
1394 else if (shaderType == glu::SHADERTYPE_GEOMETRY)
1395 resources.push_back("gl_PerVertex.gl_Position");
1396 else if (shaderType == glu::SHADERTYPE_TESSELLATION_CONTROL)
1398 resources.push_back("gl_InvocationID");
1399 resources.push_back("gl_PerVertex.gl_Position");
1401 else if (shaderType == glu::SHADERTYPE_TESSELLATION_EVALUATION)
1402 resources.push_back("gl_PerVertex.gl_Position");
1403 else if (shaderType == glu::SHADERTYPE_COMPUTE && resources.empty())
1404 resources.push_back("gl_NumWorkGroups"); // only read from when there are no other inputs
1406 else if (interface == PROGRAMINTERFACE_PROGRAM_OUTPUT)
1408 if (shaderType == glu::SHADERTYPE_VERTEX)
1409 resources.push_back("gl_Position");
1410 else if (shaderType == glu::SHADERTYPE_FRAGMENT && resources.empty())
1411 resources.push_back("gl_FragDepth"); // only written to when there are no other outputs
1412 else if (shaderType == glu::SHADERTYPE_GEOMETRY)
1413 resources.push_back("gl_Position");
1414 else if (shaderType == glu::SHADERTYPE_TESSELLATION_CONTROL)
1416 resources.push_back("gl_PerVertex.gl_Position");
1417 resources.push_back("gl_TessLevelOuter[0]");
1418 resources.push_back("gl_TessLevelInner[0]");
1420 else if (shaderType == glu::SHADERTYPE_TESSELLATION_EVALUATION)
1421 resources.push_back("gl_Position");
1427 case PROGRAMINTERFACE_TRANSFORM_FEEDBACK_VARYING:
1429 const glu::ShaderType xfbStage = getProgramTransformFeedbackStage(program);
1431 for (int varyingNdx = 0; varyingNdx < (int)program->getTransformFeedbackVaryings().size(); ++varyingNdx)
1433 const std::string& varyingName = program->getTransformFeedbackVaryings()[varyingNdx];
1435 if (deStringBeginsWith(varyingName.c_str(), "gl_"))
1436 resources.push_back(varyingName); // builtin
1439 std::vector<VariablePathComponent> path;
1441 if (!traverseProgramVariablePath(path, program, varyingName, VariableSearchFilter::createShaderTypeStorageFilter(xfbStage, glu::STORAGE_OUT)))
1442 DE_ASSERT(false); // Program failed validate, invalid operation
1444 generateVariableTypeResourceNames(resources,
1446 *path.back().getVariableType(),
1447 RESOURCE_NAME_GENERATION_FLAG_TRANSFORM_FEEDBACK_VARIABLE);
1458 if (removeDuplicated)
1460 std::set<std::string> addedVariables;
1461 std::vector<std::string> uniqueResouces;
1463 for (int ndx = 0; ndx < (int)resources.size(); ++ndx)
1465 if (addedVariables.find(resources[ndx]) == addedVariables.end())
1467 addedVariables.insert(resources[ndx]);
1468 uniqueResouces.push_back(resources[ndx]);
1472 uniqueResouces.swap(resources);
1479 * Name of the dummy uniform added by generateProgramInterfaceProgramSources
1481 * A uniform named "dummyZero" is added by
1482 * generateProgramInterfaceProgramSources. It is used in expressions to
1483 * prevent various program resources from being eliminated by the GLSL
1484 * compiler's optimizer.
1486 * \sa deqp::gles31::Functional::ProgramInterfaceDefinition::generateProgramInterfaceProgramSources
1488 const char* getDummyZeroUniformName()
1493 glu::ProgramSources generateProgramInterfaceProgramSources (const ProgramInterfaceDefinition::Program* program)
1495 glu::ProgramSources sources;
1497 DE_ASSERT(program->isValid());
1499 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
1501 const ProgramInterfaceDefinition::Shader* shader = program->getShaders()[shaderNdx];
1502 bool containsUserDefinedOutputs = false;
1503 bool containsUserDefinedInputs = false;
1504 std::ostringstream sourceBuf;
1505 std::ostringstream usageBuf;
1507 sourceBuf << glu::getGLSLVersionDeclaration(shader->getVersion()) << "\n"
1508 << getShaderExtensionDeclarations(shader)
1509 << getShaderTypeDeclarations(program, shader->getType())
1512 // Struct definitions
1514 writeStructureDefinitions(sourceBuf, shader->getDefaultBlock());
1516 // variables in the default scope
1518 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1519 sourceBuf << shader->getDefaultBlock().variables[ndx] << ";\n";
1521 if (!shader->getDefaultBlock().variables.empty())
1526 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++ndx)
1527 writeInterfaceBlock(sourceBuf, shader->getDefaultBlock().interfaceBlocks[ndx]);
1529 // Use inputs and outputs so that they won't be removed by the optimizer
1531 usageBuf << "highp uniform vec4 " << getDummyZeroUniformName() << "; // Default value is vec4(0.0).\n"
1532 "highp vec4 readInputs()\n"
1534 " highp vec4 retValue = " << getDummyZeroUniformName() << ";\n";
1536 // User-defined inputs
1538 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1540 if (shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_IN ||
1541 shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_PATCH_IN ||
1542 shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_UNIFORM)
1544 writeVariableReadAccumulateExpression(usageBuf,
1546 shader->getDefaultBlock().variables[ndx].name,
1548 shader->getDefaultBlock().variables[ndx].storage,
1550 shader->getDefaultBlock().variables[ndx].varType);
1551 containsUserDefinedInputs = true;
1555 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
1557 const glu::InterfaceBlock& interface = shader->getDefaultBlock().interfaceBlocks[interfaceNdx];
1558 if (isReadableInterface(interface))
1560 writeInterfaceReadAccumulateExpression(usageBuf,
1565 containsUserDefinedInputs = true;
1571 switch (shader->getType())
1573 case glu::SHADERTYPE_VERTEX:
1574 // make readInputs to never be compile time constant
1575 if (!containsUserDefinedInputs)
1576 usageBuf << " retValue += vec4(float(gl_VertexID));\n";
1579 case glu::SHADERTYPE_FRAGMENT:
1580 // make readInputs to never be compile time constant
1581 if (!containsUserDefinedInputs)
1582 usageBuf << " retValue += gl_FragCoord;\n";
1584 case glu::SHADERTYPE_GEOMETRY:
1585 // always use previous stage's output values so that previous stage won't be optimized out
1586 usageBuf << " retValue += gl_in[0].gl_Position;\n";
1588 case glu::SHADERTYPE_TESSELLATION_CONTROL:
1589 // always use previous stage's output values so that previous stage won't be optimized out
1590 usageBuf << " retValue += gl_in[0].gl_Position;\n";
1592 case glu::SHADERTYPE_TESSELLATION_EVALUATION:
1593 // always use previous stage's output values so that previous stage won't be optimized out
1594 usageBuf << " retValue += gl_in[0].gl_Position;\n";
1597 case glu::SHADERTYPE_COMPUTE:
1598 // make readInputs to never be compile time constant
1599 if (!containsUserDefinedInputs)
1600 usageBuf << " retValue += vec4(float(gl_NumWorkGroups.x));\n";
1606 usageBuf << " return retValue;\n"
1609 usageBuf << "void writeOutputs(in highp vec4 dummyValue)\n"
1612 // User-defined outputs
1614 for (int ndx = 0; ndx < (int)shader->getDefaultBlock().variables.size(); ++ndx)
1616 if (shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_OUT ||
1617 shader->getDefaultBlock().variables[ndx].storage == glu::STORAGE_PATCH_OUT)
1619 writeVariableWriteExpression(usageBuf,
1621 shader->getDefaultBlock().variables[ndx].name,
1623 shader->getDefaultBlock().variables[ndx].storage,
1625 shader->getDefaultBlock().variables[ndx].varType);
1626 containsUserDefinedOutputs = true;
1630 for (int interfaceNdx = 0; interfaceNdx < (int)shader->getDefaultBlock().interfaceBlocks.size(); ++interfaceNdx)
1632 const glu::InterfaceBlock& interface = shader->getDefaultBlock().interfaceBlocks[interfaceNdx];
1633 if (isWritableInterface(interface))
1635 writeInterfaceWriteExpression(usageBuf, "dummyValue", interface, shader->getType(), program);
1636 containsUserDefinedOutputs = true;
1640 // Builtin-outputs that must be written to
1642 if (shader->getType() == glu::SHADERTYPE_VERTEX)
1643 usageBuf << " gl_Position = dummyValue;\n";
1644 else if (shader->getType() == glu::SHADERTYPE_GEOMETRY)
1645 usageBuf << " gl_Position = dummyValue;\n"
1647 else if (shader->getType() == glu::SHADERTYPE_TESSELLATION_CONTROL)
1648 usageBuf << " gl_out[gl_InvocationID].gl_Position = dummyValue;\n"
1649 " gl_TessLevelOuter[0] = 2.8;\n"
1650 " gl_TessLevelOuter[1] = 2.8;\n"
1651 " gl_TessLevelOuter[2] = 2.8;\n"
1652 " gl_TessLevelOuter[3] = 2.8;\n"
1653 " gl_TessLevelInner[0] = 2.8;\n"
1654 " gl_TessLevelInner[1] = 2.8;\n";
1655 else if (shader->getType() == glu::SHADERTYPE_TESSELLATION_EVALUATION)
1656 usageBuf << " gl_Position = dummyValue;\n";
1658 // Output to sink input data to
1660 if (!containsUserDefinedOutputs)
1662 if (shader->getType() == glu::SHADERTYPE_FRAGMENT)
1663 usageBuf << " gl_FragDepth = dot(dummyValue.xy, dummyValue.xw);\n";
1664 else if (shader->getType() == glu::SHADERTYPE_COMPUTE)
1665 usageBuf << " dummyOutputBlock.dummyValue = dummyValue;\n";
1671 " writeOutputs(readInputs());\n"
1674 // Interface for dummy output
1676 if (shader->getType() == glu::SHADERTYPE_COMPUTE && !containsUserDefinedOutputs)
1678 sourceBuf << "writeonly buffer DummyOutputInterface\n"
1680 << " highp vec4 dummyValue;\n"
1681 << "} dummyOutputBlock;\n\n";
1684 sources << glu::ShaderSource(shader->getType(), sourceBuf.str() + usageBuf.str());
1687 if (program->isSeparable())
1688 sources << glu::ProgramSeparable(true);
1690 for (int ndx = 0; ndx < (int)program->getTransformFeedbackVaryings().size(); ++ndx)
1691 sources << glu::TransformFeedbackVarying(program->getTransformFeedbackVaryings()[ndx]);
1693 if (program->getTransformFeedbackMode())
1694 sources << glu::TransformFeedbackMode(program->getTransformFeedbackMode());
1699 bool findProgramVariablePathByPathName (std::vector<VariablePathComponent>& typePath, const ProgramInterfaceDefinition::Program* program, const std::string& pathName, const VariableSearchFilter& filter)
1701 std::vector<VariablePathComponent> modifiedPath;
1703 if (!traverseProgramVariablePath(modifiedPath, program, pathName, filter))
1706 // modify param only on success
1707 typePath.swap(modifiedPath);
1711 ProgramInterfaceDefinition::ShaderResourceUsage getShaderResourceUsage (const ProgramInterfaceDefinition::Program* program, const ProgramInterfaceDefinition::Shader* shader)
1713 ProgramInterfaceDefinition::ShaderResourceUsage retVal;
1715 retVal.numInputs = getNumTypeInstances(shader, glu::STORAGE_IN);
1716 retVal.numInputVectors = getNumVectors(shader, glu::STORAGE_IN);
1717 retVal.numInputComponents = getNumComponents(shader, glu::STORAGE_IN);
1719 retVal.numOutputs = getNumTypeInstances(shader, glu::STORAGE_OUT);
1720 retVal.numOutputVectors = getNumVectors(shader, glu::STORAGE_OUT);
1721 retVal.numOutputComponents = getNumComponents(shader, glu::STORAGE_OUT);
1723 retVal.numPatchInputComponents = getNumComponents(shader, glu::STORAGE_PATCH_IN);
1724 retVal.numPatchOutputComponents = getNumComponents(shader, glu::STORAGE_PATCH_OUT);
1726 retVal.numDefaultBlockUniformComponents = getNumDefaultBlockComponents(shader, glu::STORAGE_UNIFORM);
1727 retVal.numCombinedUniformComponents = getNumComponents(shader, glu::STORAGE_UNIFORM);
1728 retVal.numUniformVectors = getNumVectors(shader, glu::STORAGE_UNIFORM);
1730 retVal.numSamplers = getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeSampler);
1731 retVal.numImages = getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeImage);
1733 retVal.numAtomicCounterBuffers = getNumAtomicCounterBuffers(shader);
1734 retVal.numAtomicCounters = getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeAtomicCounter);
1736 retVal.numUniformBlocks = getNumShaderBlocks(shader, glu::STORAGE_UNIFORM);
1737 retVal.numShaderStorageBlocks = getNumShaderBlocks(shader, glu::STORAGE_BUFFER);
1740 switch (shader->getType())
1742 case glu::SHADERTYPE_VERTEX:
1743 // gl_Position is not counted
1746 case glu::SHADERTYPE_FRAGMENT:
1750 case glu::SHADERTYPE_GEOMETRY:
1751 // gl_Position in (point mode => size 1)
1752 retVal.numInputs += 1;
1753 retVal.numInputVectors += 1;
1754 retVal.numInputComponents += 4;
1757 retVal.numOutputs += 1;
1758 retVal.numOutputVectors += 1;
1759 retVal.numOutputComponents += 4;
1762 case glu::SHADERTYPE_TESSELLATION_CONTROL:
1763 // gl_Position in is read up to gl_InstanceID
1764 retVal.numInputs += 1 * program->getTessellationNumOutputPatchVertices();
1765 retVal.numInputVectors += 1 * program->getTessellationNumOutputPatchVertices();
1766 retVal.numInputComponents += 4 * program->getTessellationNumOutputPatchVertices();
1768 // gl_Position out, size = num patch out vertices
1769 retVal.numOutputs += 1 * program->getTessellationNumOutputPatchVertices();
1770 retVal.numOutputVectors += 1 * program->getTessellationNumOutputPatchVertices();
1771 retVal.numOutputComponents += 4 * program->getTessellationNumOutputPatchVertices();
1774 case glu::SHADERTYPE_TESSELLATION_EVALUATION:
1775 // gl_Position in is read up to gl_InstanceID
1776 retVal.numInputs += 1 * program->getTessellationNumOutputPatchVertices();
1777 retVal.numInputVectors += 1 * program->getTessellationNumOutputPatchVertices();
1778 retVal.numInputComponents += 4 * program->getTessellationNumOutputPatchVertices();
1781 retVal.numOutputs += 1;
1782 retVal.numOutputVectors += 1;
1783 retVal.numOutputComponents += 4;
1786 case glu::SHADERTYPE_COMPUTE:
1797 ProgramInterfaceDefinition::ProgramResourceUsage getCombinedProgramResourceUsage (const ProgramInterfaceDefinition::Program* program)
1799 ProgramInterfaceDefinition::ProgramResourceUsage retVal;
1800 int numVertexOutputComponents = 0;
1801 int numFragmentInputComponents = 0;
1802 int numVertexOutputVectors = 0;
1803 int numFragmentInputVectors = 0;
1805 retVal.uniformBufferMaxBinding = -1; // max binding is inclusive upper bound. Allow 0 bindings by using negative value
1806 retVal.uniformBufferMaxSize = 0;
1807 retVal.numUniformBlocks = 0;
1808 retVal.numCombinedVertexUniformComponents = 0;
1809 retVal.numCombinedFragmentUniformComponents = 0;
1810 retVal.numCombinedGeometryUniformComponents = 0;
1811 retVal.numCombinedTessControlUniformComponents = 0;
1812 retVal.numCombinedTessEvalUniformComponents = 0;
1813 retVal.shaderStorageBufferMaxBinding = -1; // see above
1814 retVal.shaderStorageBufferMaxSize = 0;
1815 retVal.numShaderStorageBlocks = 0;
1816 retVal.numVaryingComponents = 0;
1817 retVal.numVaryingVectors = 0;
1818 retVal.numCombinedSamplers = 0;
1819 retVal.atomicCounterBufferMaxBinding = -1; // see above
1820 retVal.atomicCounterBufferMaxSize = 0;
1821 retVal.numAtomicCounterBuffers = 0;
1822 retVal.numAtomicCounters = 0;
1823 retVal.maxImageBinding = -1; // see above
1824 retVal.numCombinedImages = 0;
1825 retVal.numCombinedOutputResources = 0;
1826 retVal.numXFBInterleavedComponents = 0;
1827 retVal.numXFBSeparateAttribs = 0;
1828 retVal.numXFBSeparateComponents = 0;
1829 retVal.fragmentOutputMaxBinding = -1; // see above
1831 for (int shaderNdx = 0; shaderNdx < (int)program->getShaders().size(); ++shaderNdx)
1833 const ProgramInterfaceDefinition::Shader* const shader = program->getShaders()[shaderNdx];
1835 retVal.uniformBufferMaxBinding = de::max(retVal.uniformBufferMaxBinding, getMaxBufferBinding(shader, glu::STORAGE_UNIFORM));
1836 retVal.uniformBufferMaxSize = de::max(retVal.uniformBufferMaxSize, getBufferMaxSize(shader, glu::STORAGE_UNIFORM));
1837 retVal.numUniformBlocks += getNumShaderBlocks(shader, glu::STORAGE_UNIFORM);
1839 switch (shader->getType())
1841 case glu::SHADERTYPE_VERTEX: retVal.numCombinedVertexUniformComponents += getNumComponents(shader, glu::STORAGE_UNIFORM); break;
1842 case glu::SHADERTYPE_FRAGMENT: retVal.numCombinedFragmentUniformComponents += getNumComponents(shader, glu::STORAGE_UNIFORM); break;
1843 case glu::SHADERTYPE_GEOMETRY: retVal.numCombinedGeometryUniformComponents += getNumComponents(shader, glu::STORAGE_UNIFORM); break;
1844 case glu::SHADERTYPE_TESSELLATION_CONTROL: retVal.numCombinedTessControlUniformComponents += getNumComponents(shader, glu::STORAGE_UNIFORM); break;
1845 case glu::SHADERTYPE_TESSELLATION_EVALUATION: retVal.numCombinedTessEvalUniformComponents += getNumComponents(shader, glu::STORAGE_UNIFORM); break;
1849 retVal.shaderStorageBufferMaxBinding = de::max(retVal.shaderStorageBufferMaxBinding, getMaxBufferBinding(shader, glu::STORAGE_BUFFER));
1850 retVal.shaderStorageBufferMaxSize = de::max(retVal.shaderStorageBufferMaxSize, getBufferMaxSize(shader, glu::STORAGE_BUFFER));
1851 retVal.numShaderStorageBlocks += getNumShaderBlocks(shader, glu::STORAGE_BUFFER);
1853 if (shader->getType() == glu::SHADERTYPE_VERTEX)
1855 numVertexOutputComponents += getNumComponents(shader, glu::STORAGE_OUT);
1856 numVertexOutputVectors += getNumVectors(shader, glu::STORAGE_OUT);
1858 else if (shader->getType() == glu::SHADERTYPE_FRAGMENT)
1860 numFragmentInputComponents += getNumComponents(shader, glu::STORAGE_IN);
1861 numFragmentInputVectors += getNumVectors(shader, glu::STORAGE_IN);
1864 retVal.numCombinedSamplers += getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeSampler);
1866 retVal.atomicCounterBufferMaxBinding = de::max(retVal.atomicCounterBufferMaxBinding, getAtomicCounterMaxBinding(shader));
1867 retVal.atomicCounterBufferMaxSize = de::max(retVal.atomicCounterBufferMaxSize, getAtomicCounterMaxBufferSize(shader));
1868 retVal.numAtomicCounterBuffers += getNumAtomicCounterBuffers(shader);
1869 retVal.numAtomicCounters += getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeAtomicCounter);
1870 retVal.maxImageBinding = de::max(retVal.maxImageBinding, getUniformMaxBinding(shader, glu::isDataTypeImage));
1871 retVal.numCombinedImages += getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeImage);
1873 retVal.numCombinedOutputResources += getNumTypeInstances(shader, glu::STORAGE_UNIFORM, glu::isDataTypeImage);
1874 retVal.numCombinedOutputResources += getNumShaderBlocks(shader, glu::STORAGE_BUFFER);
1876 if (shader->getType() == glu::SHADERTYPE_FRAGMENT)
1878 retVal.numCombinedOutputResources += getNumVectors(shader, glu::STORAGE_OUT);
1879 retVal.fragmentOutputMaxBinding = de::max(retVal.fragmentOutputMaxBinding, getFragmentOutputMaxLocation(shader));
1883 if (program->getTransformFeedbackMode() == GL_INTERLEAVED_ATTRIBS)
1884 retVal.numXFBInterleavedComponents = getNumXFBComponents(program);
1885 else if (program->getTransformFeedbackMode() == GL_SEPARATE_ATTRIBS)
1887 retVal.numXFBSeparateAttribs = (int)program->getTransformFeedbackVaryings().size();
1888 retVal.numXFBSeparateComponents = getNumMaxXFBOutputComponents(program);
1892 retVal.numVaryingComponents = de::max(numVertexOutputComponents, numFragmentInputComponents);
1893 retVal.numVaryingVectors = de::max(numVertexOutputVectors, numFragmentInputVectors);