feat: add option to map unused uniforms

This commit is contained in:
Kai Angulo 2024-10-28 00:05:51 -07:00
parent 5aa896ae05
commit ee04ba0786
7 changed files with 21 additions and 5 deletions

View file

@ -397,6 +397,10 @@ GLSLANG_EXPORT void glslang_shader_set_options(glslang_shader_t* shader, int opt
shader->shader->setInvertY(true); shader->shader->setInvertY(true);
} }
if (options & GLSLANG_SHADER_MAP_UNUSED_UNIFORMS) {
shader->shader->setMapUnusedUniforms();
}
#ifdef ENABLE_HLSL #ifdef ENABLE_HLSL
if (options & GLSLANG_SHADER_HLSL_IO_MAPPING) { if (options & GLSLANG_SHADER_HLSL_IO_MAPPING) {
shader->shader->setHlslIoMapping(true); shader->shader->setHlslIoMapping(true);

View file

@ -212,9 +212,10 @@ typedef enum {
GLSLANG_SHADER_AUTO_MAP_LOCATIONS = (1 << 1), GLSLANG_SHADER_AUTO_MAP_LOCATIONS = (1 << 1),
GLSLANG_SHADER_VULKAN_RULES_RELAXED = (1 << 2), GLSLANG_SHADER_VULKAN_RULES_RELAXED = (1 << 2),
GLSLANG_SHADER_INVERT_Y = (1 << 3), GLSLANG_SHADER_INVERT_Y = (1 << 3),
GLSLANG_SHADER_MAP_UNUSED_UNIFORMS = (1 << 4),
#ifdef ENABLE_HLSL #ifdef ENABLE_HLSL
GLSLANG_SHADER_HLSL_IO_MAPPING = (1 << 4), GLSLANG_SHADER_HLSL_IO_MAPPING = (1 << 5),
GLSLANG_SHADER_HLSL_FLATTEN_UNIFORM_ARRAYS = (1 << 5), GLSLANG_SHADER_HLSL_FLATTEN_UNIFORM_ARRAYS = (1 << 6),
#endif #endif
LAST_ELEMENT_MARKER(GLSLANG_SHADER_COUNT), LAST_ELEMENT_MARKER(GLSLANG_SHADER_COUNT),
} glslang_shader_options_t; } glslang_shader_options_t;

View file

@ -1843,6 +1843,8 @@ void TShader::setShiftUavBinding(unsigned int base) { setShiftBinding(EResUa
void TShader::setShiftSsboBinding(unsigned int base) { setShiftBinding(EResSsbo, base); } void TShader::setShiftSsboBinding(unsigned int base) { setShiftBinding(EResSsbo, base); }
// Enables binding automapping using TIoMapper // Enables binding automapping using TIoMapper
void TShader::setAutoMapBindings(bool map) { intermediate->setAutoMapBindings(map); } void TShader::setAutoMapBindings(bool map) { intermediate->setAutoMapBindings(map); }
void TShader::setMapUnusedUniforms() { intermediate->setMapUnusedUniforms(); }
// Enables position.Y output negation in vertex shader // Enables position.Y output negation in vertex shader
// Fragile: currently within one stage: simple auto-assignment of location // Fragile: currently within one stage: simple auto-assignment of location

View file

@ -885,6 +885,8 @@ bool TDefaultIoResolverBase::doAutoBindingMapping() const { return referenceInte
bool TDefaultIoResolverBase::doAutoLocationMapping() const { return referenceIntermediate.getAutoMapLocations(); } bool TDefaultIoResolverBase::doAutoLocationMapping() const { return referenceIntermediate.getAutoMapLocations(); }
bool TDefaultIoResolverBase::doMapUnusedUniforms() const { return referenceIntermediate.getMapUnusedUniforms(); }
TDefaultIoResolverBase::TSlotSet::iterator TDefaultIoResolverBase::findSlot(int set, int slot) { TDefaultIoResolverBase::TSlotSet::iterator TDefaultIoResolverBase::findSlot(int set, int slot) {
return std::lower_bound(slots[set].begin(), slots[set].end(), slot); return std::lower_bound(slots[set].begin(), slots[set].end(), slot);
} }
@ -1230,7 +1232,7 @@ int TDefaultGlslIoResolver::resolveBinding(EShLanguage stage, TVarEntryInfo& ent
ent.newBinding = iter->second; ent.newBinding = iter->second;
} }
} }
if (!hasBinding && (ent.live && doAutoBindingMapping())) { if (!hasBinding && ((ent.live || doMapUnusedUniforms()) && doAutoBindingMapping())) {
// find free slot, the caller did make sure it passes all vars with binding // find free slot, the caller did make sure it passes all vars with binding
// first and now all are passed that do not have a binding and needs one // first and now all are passed that do not have a binding and needs one
int binding = getFreeSlot(resourceKey, getBaseBinding(stage, resource, set), numBindings); int binding = getFreeSlot(resourceKey, getBaseBinding(stage, resource, set), numBindings);
@ -1408,7 +1410,7 @@ struct TDefaultIoResolver : public TDefaultIoResolverBase {
if (type.getQualifier().hasBinding()) { if (type.getQualifier().hasBinding()) {
return ent.newBinding = reserveSlot( return ent.newBinding = reserveSlot(
set, getBaseBinding(stage, resource, set) + type.getQualifier().layoutBinding, numBindings); set, getBaseBinding(stage, resource, set) + type.getQualifier().layoutBinding, numBindings);
} else if (ent.live && doAutoBindingMapping()) { } else if ((ent.live || doMapUnusedUniforms()) && doAutoBindingMapping()) {
// find free slot, the caller did make sure it passes all vars with binding // find free slot, the caller did make sure it passes all vars with binding
// first and now all are passed that do not have a binding and needs one // first and now all are passed that do not have a binding and needs one
return ent.newBinding = getFreeSlot(set, getBaseBinding(stage, resource, set), numBindings); return ent.newBinding = getFreeSlot(set, getBaseBinding(stage, resource, set), numBindings);
@ -1490,7 +1492,7 @@ struct TDefaultHlslIoResolver : public TDefaultIoResolverBase {
if (resource < EResCount) { if (resource < EResCount) {
if (type.getQualifier().hasBinding()) { if (type.getQualifier().hasBinding()) {
return ent.newBinding = reserveSlot(set, getBaseBinding(stage, resource, set) + type.getQualifier().layoutBinding); return ent.newBinding = reserveSlot(set, getBaseBinding(stage, resource, set) + type.getQualifier().layoutBinding);
} else if (ent.live && doAutoBindingMapping()) { } else if ((ent.live || doMapUnusedUniforms()) && doAutoBindingMapping()) {
// find free slot, the caller did make sure it passes all vars with binding // find free slot, the caller did make sure it passes all vars with binding
// first and now all are passed that do not have a binding and needs one // first and now all are passed that do not have a binding and needs one
return ent.newBinding = getFreeSlot(set, getBaseBinding(stage, resource, set)); return ent.newBinding = getFreeSlot(set, getBaseBinding(stage, resource, set));

View file

@ -72,6 +72,7 @@ public:
virtual TResourceType getResourceType(const glslang::TType& type) = 0; virtual TResourceType getResourceType(const glslang::TType& type) = 0;
bool doAutoBindingMapping() const; bool doAutoBindingMapping() const;
bool doAutoLocationMapping() const; bool doAutoLocationMapping() const;
bool doMapUnusedUniforms() const;
TSlotSet::iterator findSlot(int set, int slot); TSlotSet::iterator findSlot(int set, int slot);
bool checkEmpty(int set, int slot); bool checkEmpty(int set, int slot);
bool validateInOut(EShLanguage /*stage*/, TVarEntryInfo& /*ent*/) override { return true; } bool validateInOut(EShLanguage /*stage*/, TVarEntryInfo& /*ent*/) override { return true; }

View file

@ -344,6 +344,7 @@ public:
numTaskEXTPayloads(0), numTaskEXTPayloads(0),
autoMapBindings(false), autoMapBindings(false),
autoMapLocations(false), autoMapLocations(false),
mapUnusedUniforms(false),
flattenUniformArrays(false), flattenUniformArrays(false),
useUnknownFormat(false), useUnknownFormat(false),
hlslOffsets(false), hlslOffsets(false),
@ -1008,6 +1009,9 @@ public:
else else
return pos->second; return pos->second;
} }
void setMapUnusedUniforms() { mapUnusedUniforms = true; }
bool getMapUnusedUniforms() const { return mapUnusedUniforms; }
#ifdef ENABLE_HLSL #ifdef ENABLE_HLSL
void setHlslFunctionality1() { hlslFunctionality1 = true; } void setHlslFunctionality1() { hlslFunctionality1 = true; }
bool getHlslFunctionality1() const { return hlslFunctionality1; } bool getHlslFunctionality1() const { return hlslFunctionality1; }
@ -1237,6 +1241,7 @@ protected:
std::vector<std::string> resourceSetBinding; std::vector<std::string> resourceSetBinding;
bool autoMapBindings; bool autoMapBindings;
bool autoMapLocations; bool autoMapLocations;
bool mapUnusedUniforms;
bool flattenUniformArrays; bool flattenUniformArrays;
bool useUnknownFormat; bool useUnknownFormat;
bool hlslOffsets; bool hlslOffsets;

View file

@ -491,6 +491,7 @@ public:
GLSLANG_EXPORT void setResourceSetBinding(const std::vector<std::string>& base); GLSLANG_EXPORT void setResourceSetBinding(const std::vector<std::string>& base);
GLSLANG_EXPORT void setAutoMapBindings(bool map); GLSLANG_EXPORT void setAutoMapBindings(bool map);
GLSLANG_EXPORT void setAutoMapLocations(bool map); GLSLANG_EXPORT void setAutoMapLocations(bool map);
GLSLANG_EXPORT void setMapUnusedUniforms();
GLSLANG_EXPORT void addUniformLocationOverride(const char* name, int loc); GLSLANG_EXPORT void addUniformLocationOverride(const char* name, int loc);
GLSLANG_EXPORT void setUniformLocationBase(int base); GLSLANG_EXPORT void setUniformLocationBase(int base);
GLSLANG_EXPORT void setInvertY(bool invert); GLSLANG_EXPORT void setInvertY(bool invert);