Commit 135f8fcb by Shahbaz Youssefi Committed by Commit Bot

Vulkan: Remove inactive uniforms in the translator

By removing inactive uniforms in the translator, glslang wrapper doesn't need to comment them out. Additionally, inactive uniforms don't find their way in the default uniform block, reducing its size if there's a mix of active and inactive uniforms. As collateral, it also fixes a bug where inactive uniforms of struct type were not correctly removed by glslang wrapper. Bug: angleproject:3394 Bug: angleproject:4211 Bug: angleproject:4248 Change-Id: I874747070e875fe24bf59d39d1322e319e280a16 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1999278 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org>
parent abaeb415
...@@ -712,7 +712,7 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root, ...@@ -712,7 +712,7 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
int atomicCounterCount = 0; int atomicCounterCount = 0;
for (const auto &uniform : getUniforms()) for (const auto &uniform : getUniforms())
{ {
if (!uniform.isBuiltIn() && uniform.staticUse && !gl::IsOpaqueType(uniform.type)) if (!uniform.isBuiltIn() && uniform.active && !gl::IsOpaqueType(uniform.type))
{ {
++defaultUniformCount; ++defaultUniformCount;
} }
......
...@@ -48,10 +48,18 @@ void GetInterfaceBlockInfo(const std::vector<VarT> &fields, ...@@ -48,10 +48,18 @@ void GetInterfaceBlockInfo(const std::vector<VarT> &fields,
const std::string &prefix, const std::string &prefix,
BlockLayoutEncoder *encoder, BlockLayoutEncoder *encoder,
bool inRowMajorLayout, bool inRowMajorLayout,
bool onlyActiveVariables,
BlockLayoutMap *blockInfoOut) BlockLayoutMap *blockInfoOut)
{ {
BlockLayoutMapVisitor visitor(blockInfoOut, prefix, encoder); BlockLayoutMapVisitor visitor(blockInfoOut, prefix, encoder);
TraverseShaderVariables(fields, inRowMajorLayout, &visitor); if (onlyActiveVariables)
{
TraverseActiveShaderVariables(fields, inRowMajorLayout, &visitor);
}
else
{
TraverseShaderVariables(fields, inRowMajorLayout, &visitor);
}
} }
void TraverseStructVariable(const ShaderVariable &variable, void TraverseStructVariable(const ShaderVariable &variable,
...@@ -345,17 +353,19 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields, ...@@ -345,17 +353,19 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields,
{ {
// Matrix packing is always recorded in individual fields, so they'll set the row major layout // Matrix packing is always recorded in individual fields, so they'll set the row major layout
// flag to true if needed. // flag to true if needed.
GetInterfaceBlockInfo(fields, prefix, encoder, false, blockInfoOut); // Iterates over all variables.
GetInterfaceBlockInfo(fields, prefix, encoder, false, false, blockInfoOut);
} }
void GetUniformBlockInfo(const std::vector<ShaderVariable> &uniforms, void GetActiveUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
const std::string &prefix, const std::string &prefix,
BlockLayoutEncoder *encoder, BlockLayoutEncoder *encoder,
BlockLayoutMap *blockInfoOut) BlockLayoutMap *blockInfoOut)
{ {
// Matrix packing is always recorded in individual fields, so they'll set the row major layout // Matrix packing is always recorded in individual fields, so they'll set the row major layout
// flag to true if needed. // flag to true if needed.
GetInterfaceBlockInfo(uniforms, prefix, encoder, false, blockInfoOut); // Iterates only over the active variables.
GetInterfaceBlockInfo(uniforms, prefix, encoder, false, true, blockInfoOut);
} }
// VariableNameVisitor implementation. // VariableNameVisitor implementation.
......
...@@ -179,10 +179,10 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields, ...@@ -179,10 +179,10 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields,
BlockLayoutMap *blockInfoOut); BlockLayoutMap *blockInfoOut);
// Used for laying out the default uniform block on the Vulkan backend. // Used for laying out the default uniform block on the Vulkan backend.
void GetUniformBlockInfo(const std::vector<ShaderVariable> &uniforms, void GetActiveUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
const std::string &prefix, const std::string &prefix,
BlockLayoutEncoder *encoder, BlockLayoutEncoder *encoder,
BlockLayoutMap *blockInfoOut); BlockLayoutMap *blockInfoOut);
class ShaderVariableVisitor class ShaderVariableVisitor
{ {
...@@ -298,6 +298,20 @@ void TraverseShaderVariables(const std::vector<T> &vars, ...@@ -298,6 +298,20 @@ void TraverseShaderVariables(const std::vector<T> &vars,
TraverseShaderVariable(var, isRowMajorLayout, visitor); TraverseShaderVariable(var, isRowMajorLayout, visitor);
} }
} }
template <typename T>
void TraverseActiveShaderVariables(const std::vector<T> &vars,
bool isRowMajorLayout,
ShaderVariableVisitor *visitor)
{
for (const T &var : vars)
{
if (var.active)
{
TraverseShaderVariable(var, isRowMajorLayout, visitor);
}
}
}
} // namespace sh } // namespace sh
#endif // COMMON_BLOCKLAYOUT_H_ #endif // COMMON_BLOCKLAYOUT_H_
...@@ -41,7 +41,7 @@ RemoveInactiveInterfaceVariablesTraverser::RemoveInactiveInterfaceVariablesTrave ...@@ -41,7 +41,7 @@ RemoveInactiveInterfaceVariablesTraverser::RemoveInactiveInterfaceVariablesTrave
{} {}
template <typename Variable> template <typename Variable>
bool isVariableActive(const std::vector<Variable> &mVars, const ImmutableString &name) bool IsVariableActive(const std::vector<Variable> &mVars, const ImmutableString &name)
{ {
for (const Variable &var : mVars) for (const Variable &var : mVars)
{ {
...@@ -71,7 +71,7 @@ bool RemoveInactiveInterfaceVariablesTraverser::visitDeclaration(Visit visit, ...@@ -71,7 +71,7 @@ bool RemoveInactiveInterfaceVariablesTraverser::visitDeclaration(Visit visit,
const TType &type = declarator->getType(); const TType &type = declarator->getType();
// Only remove opaque uniform and interface block declarations. // Only remove uniform and interface block declarations.
// //
// Note: Don't remove varyings. Imagine a situation where the VS doesn't write to a varying // Note: Don't remove varyings. Imagine a situation where the VS doesn't write to a varying
// but the FS reads from it. This is allowed, though the value of the varying is undefined. // but the FS reads from it. This is allowed, though the value of the varying is undefined.
...@@ -81,11 +81,11 @@ bool RemoveInactiveInterfaceVariablesTraverser::visitDeclaration(Visit visit, ...@@ -81,11 +81,11 @@ bool RemoveInactiveInterfaceVariablesTraverser::visitDeclaration(Visit visit,
if (type.isInterfaceBlock()) if (type.isInterfaceBlock())
{ {
removeDeclaration = !isVariableActive(mInterfaceBlocks, type.getInterfaceBlock()->name()); removeDeclaration = !IsVariableActive(mInterfaceBlocks, type.getInterfaceBlock()->name());
} }
else if (type.getQualifier() == EvqUniform && IsOpaqueType(type.getBasicType())) else if (type.getQualifier() == EvqUniform)
{ {
removeDeclaration = !isVariableActive(mUniforms, asSymbol->getName()); removeDeclaration = !IsVariableActive(mUniforms, asSymbol->getName());
} }
if (removeDeclaration) if (removeDeclaration)
......
...@@ -1045,20 +1045,18 @@ void CleanupUnusedEntities(bool useOldRewriteStructSamplers, ...@@ -1045,20 +1045,18 @@ void CleanupUnusedEntities(bool useOldRewriteStructSamplers,
} }
} }
// Comment out unused default uniforms. This relies on the fact that the shader compiler // Comment out inactive samplers. This relies on the fact that the shader compiler outputs
// outputs uniforms to a single line. // uniforms to a single line.
for (const gl::UnusedUniform &unusedUniform : resources.unusedUniforms) for (const gl::UnusedUniform &unusedUniform : resources.unusedUniforms)
{ {
if (unusedUniform.isImage || unusedUniform.isAtomicCounter) if (!unusedUniform.isSampler)
{ {
continue; continue;
} }
std::string uniformName = unusedUniform.isSampler std::string uniformName = useOldRewriteStructSamplers
? useOldRewriteStructSamplers ? GetMappedSamplerNameOld(unusedUniform.name)
? GetMappedSamplerNameOld(unusedUniform.name) : GlslangGetMappedSamplerName(unusedUniform.name);
: GlslangGetMappedSamplerName(unusedUniform.name)
: unusedUniform.name;
for (IntermediateShaderSource &shaderSource : *shaderSources) for (IntermediateShaderSource &shaderSource : *shaderSources)
{ {
......
...@@ -124,7 +124,7 @@ void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms, ...@@ -124,7 +124,7 @@ void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
} }
sh::Std140BlockEncoder blockEncoder; sh::Std140BlockEncoder blockEncoder;
sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut); sh::GetActiveUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
size_t blockSize = blockEncoder.getCurrentOffset(); size_t blockSize = blockEncoder.getCurrentOffset();
......
...@@ -58,7 +58,7 @@ void InitDefaultUniformBlock(const std::vector<sh::ShaderVariable> &uniforms, ...@@ -58,7 +58,7 @@ void InitDefaultUniformBlock(const std::vector<sh::ShaderVariable> &uniforms,
} }
VulkanDefaultBlockEncoder blockEncoder; VulkanDefaultBlockEncoder blockEncoder;
sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut); sh::GetActiveUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
size_t blockSize = blockEncoder.getCurrentOffset(); size_t blockSize = blockEncoder.getCurrentOffset();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment