add reflection queries to return a TType. Fix minor issue with interface names.
- Add new queries: TProgram::getUniformTType and getUniformBlockTType, which return a const TType*, or nullptr on a bad index. These are valid for any source language. - Interface name for HLSL cbuffers is taken from the (only) available declaration name, whereas before it was always an empty string, which caused some troubles with reflection mapping them all to the same index slot. This also makes it appear in the SPIR-V binary instead of an empty string. - Print the binding as part of the reflection textual dump. - TType::clone becomes const. Needed to call it from a const method, and anyway it doesn't change the object it's called on. - Because the TObjectReflection constructor is called with a TType *reference* (not pointer) so that it's guaranteed to pass in a type, and the "badReflection" value should use a nullptr there, that now has a dedicated static method to obtain the bad value. It uses a private constructor, so external users can't create one with a nullptr type.
This commit is contained in:
@@ -1202,7 +1202,7 @@ public:
|
||||
typeName = NewPoolTString(copyOf.typeName->c_str());
|
||||
}
|
||||
|
||||
TType* clone()
|
||||
TType* clone() const
|
||||
{
|
||||
TType *newType = new TType();
|
||||
newType->deepCopy(*this);
|
||||
|
||||
@@ -1694,19 +1694,21 @@ bool TProgram::buildReflection()
|
||||
return true;
|
||||
}
|
||||
|
||||
int TProgram::getNumLiveUniformVariables() { return reflection->getNumUniforms(); }
|
||||
int TProgram::getNumLiveUniformBlocks() { return reflection->getNumUniformBlocks(); }
|
||||
const char* TProgram::getUniformName(int index) { return reflection->getUniform(index).name.c_str(); }
|
||||
const char* TProgram::getUniformBlockName(int index) { return reflection->getUniformBlock(index).name.c_str(); }
|
||||
int TProgram::getUniformBlockSize(int index) { return reflection->getUniformBlock(index).size; }
|
||||
int TProgram::getUniformIndex(const char* name) { return reflection->getIndex(name); }
|
||||
int TProgram::getUniformBlockIndex(int index) { return reflection->getUniform(index).index; }
|
||||
int TProgram::getUniformType(int index) { return reflection->getUniform(index).glDefineType; }
|
||||
int TProgram::getUniformBufferOffset(int index) { return reflection->getUniform(index).offset; }
|
||||
int TProgram::getUniformArraySize(int index) { return reflection->getUniform(index).size; }
|
||||
int TProgram::getNumLiveAttributes() { return reflection->getNumAttributes(); }
|
||||
const char* TProgram::getAttributeName(int index) { return reflection->getAttribute(index).name.c_str(); }
|
||||
int TProgram::getAttributeType(int index) { return reflection->getAttribute(index).glDefineType; }
|
||||
int TProgram::getNumLiveUniformVariables() const { return reflection->getNumUniforms(); }
|
||||
int TProgram::getNumLiveUniformBlocks() const { return reflection->getNumUniformBlocks(); }
|
||||
const char* TProgram::getUniformName(int index) const { return reflection->getUniform(index).name.c_str(); }
|
||||
const char* TProgram::getUniformBlockName(int index) const { return reflection->getUniformBlock(index).name.c_str(); }
|
||||
int TProgram::getUniformBlockSize(int index) const { return reflection->getUniformBlock(index).size; }
|
||||
int TProgram::getUniformIndex(const char* name) const { return reflection->getIndex(name); }
|
||||
int TProgram::getUniformBlockIndex(int index) const { return reflection->getUniform(index).index; }
|
||||
int TProgram::getUniformType(int index) const { return reflection->getUniform(index).glDefineType; }
|
||||
int TProgram::getUniformBufferOffset(int index) const { return reflection->getUniform(index).offset; }
|
||||
int TProgram::getUniformArraySize(int index) const { return reflection->getUniform(index).size; }
|
||||
int TProgram::getNumLiveAttributes() const { return reflection->getNumAttributes(); }
|
||||
const char* TProgram::getAttributeName(int index) const { return reflection->getAttribute(index).name.c_str(); }
|
||||
int TProgram::getAttributeType(int index) const { return reflection->getAttribute(index).glDefineType; }
|
||||
const TType* TProgram::getUniformTType(int index) const { return reflection->getUniform(index).getType(); }
|
||||
const TType* TProgram::getUniformBlockTType(int index) const { return reflection->getUniformBlock(index).getType(); }
|
||||
|
||||
void TProgram::dumpReflection() { reflection->dump(); }
|
||||
|
||||
|
||||
@@ -109,7 +109,7 @@ public:
|
||||
TReflection::TNameToIndex::const_iterator it = reflection.nameToIndex.find(name);
|
||||
if (it == reflection.nameToIndex.end()) {
|
||||
reflection.nameToIndex[name] = (int)reflection.indexToAttribute.size();
|
||||
reflection.indexToAttribute.push_back(TObjectReflection(name, 0, mapToGlType(type), 0, 0));
|
||||
reflection.indexToAttribute.push_back(TObjectReflection(name, type, 0, mapToGlType(type), 0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -245,7 +245,8 @@ public:
|
||||
TReflection::TNameToIndex::const_iterator it = reflection.nameToIndex.find(name);
|
||||
if (it == reflection.nameToIndex.end()) {
|
||||
reflection.nameToIndex[name] = (int)reflection.indexToUniform.size();
|
||||
reflection.indexToUniform.push_back(TObjectReflection(name, offset, mapToGlType(*terminalType), arraySize, blockIndex));
|
||||
reflection.indexToUniform.push_back(TObjectReflection(name, *terminalType, offset, mapToGlType(*terminalType),
|
||||
arraySize, blockIndex));
|
||||
} else if (arraySize > 1) {
|
||||
int& reflectedArraySize = reflection.indexToUniform[it->second].size;
|
||||
reflectedArraySize = std::max(arraySize, reflectedArraySize);
|
||||
@@ -296,12 +297,18 @@ public:
|
||||
if (block) {
|
||||
offset = 0;
|
||||
anonymous = IsAnonymous(base->getName());
|
||||
|
||||
const TString& blockName = base->getType().getTypeName();
|
||||
|
||||
if (base->getType().isArray()) {
|
||||
TType derefType(base->getType(), 0);
|
||||
|
||||
assert(! anonymous);
|
||||
for (int e = 0; e < base->getType().getCumulativeArraySize(); ++e)
|
||||
blockIndex = addBlockName(base->getType().getTypeName() + "[" + String(e) + "]", getBlockSize(base->getType()));
|
||||
blockIndex = addBlockName(blockName + "[" + String(e) + "]", derefType,
|
||||
getBlockSize(base->getType()));
|
||||
} else
|
||||
blockIndex = addBlockName(base->getType().getTypeName(), getBlockSize(base->getType()));
|
||||
blockIndex = addBlockName(blockName, base->getType(), getBlockSize(base->getType()));
|
||||
}
|
||||
|
||||
// Process the dereference chain, backward, accumulating the pieces for later forward traversal.
|
||||
@@ -334,14 +341,14 @@ public:
|
||||
blowUpActiveAggregate(base->getType(), baseName, derefs, derefs.begin(), offset, blockIndex, arraySize);
|
||||
}
|
||||
|
||||
int addBlockName(const TString& name, int size)
|
||||
int addBlockName(const TString& name, const TType& type, int size)
|
||||
{
|
||||
int blockIndex;
|
||||
TReflection::TNameToIndex::const_iterator it = reflection.nameToIndex.find(name);
|
||||
if (reflection.nameToIndex.find(name) == reflection.nameToIndex.end()) {
|
||||
blockIndex = (int)reflection.indexToUniformBlock.size();
|
||||
reflection.nameToIndex[name] = blockIndex;
|
||||
reflection.indexToUniformBlock.push_back(TObjectReflection(name, -1, -1, size, -1));
|
||||
reflection.indexToUniformBlock.push_back(TObjectReflection(name, type, -1, -1, size, -1));
|
||||
} else
|
||||
blockIndex = it->second;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
//Copyright (C) 2013 LunarG, Inc.
|
||||
//Copyright (C) 2013-2016 LunarG, Inc.
|
||||
//
|
||||
//All rights reserved.
|
||||
//
|
||||
@@ -37,6 +37,7 @@
|
||||
#define _REFLECTION_INCLUDED
|
||||
|
||||
#include "../Public/ShaderLang.h"
|
||||
#include "../Include/Types.h"
|
||||
|
||||
#include <list>
|
||||
#include <set>
|
||||
@@ -54,20 +55,41 @@ class TReflectionTraverser;
|
||||
// Data needed for just a single object at the granularity exchanged by the reflection API
|
||||
class TObjectReflection {
|
||||
public:
|
||||
TObjectReflection(const TString& pName, int pOffset, int pGLDefineType, int pSize, int pIndex) :
|
||||
name(pName), offset(pOffset), glDefineType(pGLDefineType), size(pSize), index(pIndex) { }
|
||||
void dump() const { printf("%s: offset %d, type %x, size %d, index %d\n", name.c_str(), offset, glDefineType, size, index); }
|
||||
TObjectReflection(const TString& pName, const TType& pType, int pOffset, int pGLDefineType, int pSize, int pIndex) :
|
||||
name(pName), type(pType.clone()),
|
||||
offset(pOffset), glDefineType(pGLDefineType), size(pSize), index(pIndex) { }
|
||||
|
||||
void dump() const {
|
||||
printf("%s: offset %d, type %x, size %d, index %d, binding %d\n",
|
||||
name.c_str(), offset, glDefineType, size, index, getBinding() );
|
||||
}
|
||||
|
||||
const TType* const getType() const { return type; }
|
||||
|
||||
TString name;
|
||||
int offset;
|
||||
int glDefineType;
|
||||
int size; // data size in bytes for a block, array size for a (non-block) object that's an array
|
||||
int index;
|
||||
|
||||
static TObjectReflection badReflection() { return TObjectReflection(); }
|
||||
|
||||
protected:
|
||||
int getBinding() const {
|
||||
if (type == nullptr || type->getQualifier().layoutBinding == TQualifier::layoutBindingEnd)
|
||||
return -1;
|
||||
return type->getQualifier().layoutBinding;
|
||||
}
|
||||
|
||||
TObjectReflection() : type(nullptr), offset(-1), glDefineType(-1), size(-1), index(-1) { }
|
||||
|
||||
const TType* type;
|
||||
};
|
||||
|
||||
// The full reflection database
|
||||
class TReflection {
|
||||
public:
|
||||
TReflection() : badReflection("__bad__", -1, -1, -1, -1) {}
|
||||
TReflection() : badReflection(TObjectReflection::badReflection()) { }
|
||||
virtual ~TReflection() {}
|
||||
|
||||
// grow the reflection stage by stage
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
//
|
||||
//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
|
||||
//Copyright (C) 2013-2016 LunarG, Inc.
|
||||
//
|
||||
//All rights reserved.
|
||||
//
|
||||
//Redistribution and use in source and binary forms, with or without
|
||||
@@ -101,6 +103,8 @@ typedef enum {
|
||||
|
||||
namespace glslang {
|
||||
|
||||
class TType;
|
||||
|
||||
typedef enum {
|
||||
EShSourceNone,
|
||||
EShSourceGlsl,
|
||||
@@ -461,19 +465,22 @@ public:
|
||||
|
||||
// Reflection Interface
|
||||
bool buildReflection(); // call first, to do liveness analysis, index mapping, etc.; returns false on failure
|
||||
int getNumLiveUniformVariables(); // can be used for glGetProgramiv(GL_ACTIVE_UNIFORMS)
|
||||
int getNumLiveUniformBlocks(); // can be used for glGetProgramiv(GL_ACTIVE_UNIFORM_BLOCKS)
|
||||
const char* getUniformName(int index); // can be used for "name" part of glGetActiveUniform()
|
||||
const char* getUniformBlockName(int blockIndex); // can be used for glGetActiveUniformBlockName()
|
||||
int getUniformBlockSize(int blockIndex); // can be used for glGetActiveUniformBlockiv(UNIFORM_BLOCK_DATA_SIZE)
|
||||
int getUniformIndex(const char* name); // can be used for glGetUniformIndices()
|
||||
int getUniformBlockIndex(int index); // can be used for glGetActiveUniformsiv(GL_UNIFORM_BLOCK_INDEX)
|
||||
int getUniformType(int index); // can be used for glGetActiveUniformsiv(GL_UNIFORM_TYPE)
|
||||
int getUniformBufferOffset(int index); // can be used for glGetActiveUniformsiv(GL_UNIFORM_OFFSET)
|
||||
int getUniformArraySize(int index); // can be used for glGetActiveUniformsiv(GL_UNIFORM_SIZE)
|
||||
int getNumLiveAttributes(); // can be used for glGetProgramiv(GL_ACTIVE_ATTRIBUTES)
|
||||
const char *getAttributeName(int index); // can be used for glGetActiveAttrib()
|
||||
int getAttributeType(int index); // can be used for glGetActiveAttrib()
|
||||
int getNumLiveUniformVariables() const; // can be used for glGetProgramiv(GL_ACTIVE_UNIFORMS)
|
||||
int getNumLiveUniformBlocks() const; // can be used for glGetProgramiv(GL_ACTIVE_UNIFORM_BLOCKS)
|
||||
const char* getUniformName(int index) const; // can be used for "name" part of glGetActiveUniform()
|
||||
const char* getUniformBlockName(int blockIndex) const; // can be used for glGetActiveUniformBlockName()
|
||||
int getUniformBlockSize(int blockIndex) const; // can be used for glGetActiveUniformBlockiv(UNIFORM_BLOCK_DATA_SIZE)
|
||||
int getUniformIndex(const char* name) const; // can be used for glGetUniformIndices()
|
||||
int getUniformBlockIndex(int index) const; // can be used for glGetActiveUniformsiv(GL_UNIFORM_BLOCK_INDEX)
|
||||
int getUniformType(int index) const; // can be used for glGetActiveUniformsiv(GL_UNIFORM_TYPE)
|
||||
int getUniformBufferOffset(int index) const; // can be used for glGetActiveUniformsiv(GL_UNIFORM_OFFSET)
|
||||
int getUniformArraySize(int index) const; // can be used for glGetActiveUniformsiv(GL_UNIFORM_SIZE)
|
||||
int getNumLiveAttributes() const; // can be used for glGetProgramiv(GL_ACTIVE_ATTRIBUTES)
|
||||
const char *getAttributeName(int index) const; // can be used for glGetActiveAttrib()
|
||||
int getAttributeType(int index) const; // can be used for glGetActiveAttrib()
|
||||
const TType* getUniformTType(int index) const; // returns a TType*
|
||||
const TType* getUniformBlockTType(int index) const; // returns a TType*
|
||||
|
||||
void dumpReflection();
|
||||
|
||||
// I/O mapping: apply base offsets and map live unbound variables
|
||||
|
||||
Reference in New Issue
Block a user