Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 135f8fc

Browse files
ShabbyXCommit Bot
authored andcommitted
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: Jamie Madill <jmadill@chromium.org>
1 parent abaeb41 commit 135f8fc

File tree

7 files changed

+49
-27
lines changed

7 files changed

+49
-27
lines changed

src/compiler/translator/TranslatorVulkan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
712712
int atomicCounterCount = 0;
713713
for (const auto &uniform : getUniforms())
714714
{
715-
if (!uniform.isBuiltIn() && uniform.staticUse && !gl::IsOpaqueType(uniform.type))
715+
if (!uniform.isBuiltIn() && uniform.active && !gl::IsOpaqueType(uniform.type))
716716
{
717717
++defaultUniformCount;
718718
}

src/compiler/translator/blocklayout.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ void GetInterfaceBlockInfo(const std::vector<VarT> &fields,
4848
const std::string &prefix,
4949
BlockLayoutEncoder *encoder,
5050
bool inRowMajorLayout,
51+
bool onlyActiveVariables,
5152
BlockLayoutMap *blockInfoOut)
5253
{
5354
BlockLayoutMapVisitor visitor(blockInfoOut, prefix, encoder);
54-
TraverseShaderVariables(fields, inRowMajorLayout, &visitor);
55+
if (onlyActiveVariables)
56+
{
57+
TraverseActiveShaderVariables(fields, inRowMajorLayout, &visitor);
58+
}
59+
else
60+
{
61+
TraverseShaderVariables(fields, inRowMajorLayout, &visitor);
62+
}
5563
}
5664

5765
void TraverseStructVariable(const ShaderVariable &variable,
@@ -345,17 +353,19 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields,
345353
{
346354
// Matrix packing is always recorded in individual fields, so they'll set the row major layout
347355
// flag to true if needed.
348-
GetInterfaceBlockInfo(fields, prefix, encoder, false, blockInfoOut);
356+
// Iterates over all variables.
357+
GetInterfaceBlockInfo(fields, prefix, encoder, false, false, blockInfoOut);
349358
}
350359

351-
void GetUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
352-
const std::string &prefix,
353-
BlockLayoutEncoder *encoder,
354-
BlockLayoutMap *blockInfoOut)
360+
void GetActiveUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
361+
const std::string &prefix,
362+
BlockLayoutEncoder *encoder,
363+
BlockLayoutMap *blockInfoOut)
355364
{
356365
// Matrix packing is always recorded in individual fields, so they'll set the row major layout
357366
// flag to true if needed.
358-
GetInterfaceBlockInfo(uniforms, prefix, encoder, false, blockInfoOut);
367+
// Iterates only over the active variables.
368+
GetInterfaceBlockInfo(uniforms, prefix, encoder, false, true, blockInfoOut);
359369
}
360370

361371
// VariableNameVisitor implementation.

src/compiler/translator/blocklayout.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ void GetInterfaceBlockInfo(const std::vector<ShaderVariable> &fields,
179179
BlockLayoutMap *blockInfoOut);
180180

181181
// Used for laying out the default uniform block on the Vulkan backend.
182-
void GetUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
183-
const std::string &prefix,
184-
BlockLayoutEncoder *encoder,
185-
BlockLayoutMap *blockInfoOut);
182+
void GetActiveUniformBlockInfo(const std::vector<ShaderVariable> &uniforms,
183+
const std::string &prefix,
184+
BlockLayoutEncoder *encoder,
185+
BlockLayoutMap *blockInfoOut);
186186

187187
class ShaderVariableVisitor
188188
{
@@ -298,6 +298,20 @@ void TraverseShaderVariables(const std::vector<T> &vars,
298298
TraverseShaderVariable(var, isRowMajorLayout, visitor);
299299
}
300300
}
301+
302+
template <typename T>
303+
void TraverseActiveShaderVariables(const std::vector<T> &vars,
304+
bool isRowMajorLayout,
305+
ShaderVariableVisitor *visitor)
306+
{
307+
for (const T &var : vars)
308+
{
309+
if (var.active)
310+
{
311+
TraverseShaderVariable(var, isRowMajorLayout, visitor);
312+
}
313+
}
314+
}
301315
} // namespace sh
302316

303317
#endif // COMMON_BLOCKLAYOUT_H_

src/compiler/translator/tree_ops/RemoveInactiveInterfaceVariables.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ RemoveInactiveInterfaceVariablesTraverser::RemoveInactiveInterfaceVariablesTrave
4141
{}
4242

4343
template <typename Variable>
44-
bool isVariableActive(const std::vector<Variable> &mVars, const ImmutableString &name)
44+
bool IsVariableActive(const std::vector<Variable> &mVars, const ImmutableString &name)
4545
{
4646
for (const Variable &var : mVars)
4747
{
@@ -71,7 +71,7 @@ bool RemoveInactiveInterfaceVariablesTraverser::visitDeclaration(Visit visit,
7171

7272
const TType &type = declarator->getType();
7373

74-
// Only remove opaque uniform and interface block declarations.
74+
// Only remove uniform and interface block declarations.
7575
//
7676
// Note: Don't remove varyings. Imagine a situation where the VS doesn't write to a varying
7777
// 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,
8181

8282
if (type.isInterfaceBlock())
8383
{
84-
removeDeclaration = !isVariableActive(mInterfaceBlocks, type.getInterfaceBlock()->name());
84+
removeDeclaration = !IsVariableActive(mInterfaceBlocks, type.getInterfaceBlock()->name());
8585
}
86-
else if (type.getQualifier() == EvqUniform && IsOpaqueType(type.getBasicType()))
86+
else if (type.getQualifier() == EvqUniform)
8787
{
88-
removeDeclaration = !isVariableActive(mUniforms, asSymbol->getName());
88+
removeDeclaration = !IsVariableActive(mUniforms, asSymbol->getName());
8989
}
9090

9191
if (removeDeclaration)

src/libANGLE/renderer/glslang_wrapper_utils.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,20 +1045,18 @@ void CleanupUnusedEntities(bool useOldRewriteStructSamplers,
10451045
}
10461046
}
10471047

1048-
// Comment out unused default uniforms. This relies on the fact that the shader compiler
1049-
// outputs uniforms to a single line.
1048+
// Comment out inactive samplers. This relies on the fact that the shader compiler outputs
1049+
// uniforms to a single line.
10501050
for (const gl::UnusedUniform &unusedUniform : resources.unusedUniforms)
10511051
{
1052-
if (unusedUniform.isImage || unusedUniform.isAtomicCounter)
1052+
if (!unusedUniform.isSampler)
10531053
{
10541054
continue;
10551055
}
10561056

1057-
std::string uniformName = unusedUniform.isSampler
1058-
? useOldRewriteStructSamplers
1059-
? GetMappedSamplerNameOld(unusedUniform.name)
1060-
: GlslangGetMappedSamplerName(unusedUniform.name)
1061-
: unusedUniform.name;
1057+
std::string uniformName = useOldRewriteStructSamplers
1058+
? GetMappedSamplerNameOld(unusedUniform.name)
1059+
: GlslangGetMappedSamplerName(unusedUniform.name);
10621060

10631061
for (IntermediateShaderSource &shaderSource : *shaderSources)
10641062
{

src/libANGLE/renderer/metal/ProgramMtl.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
124124
}
125125

126126
sh::Std140BlockEncoder blockEncoder;
127-
sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
127+
sh::GetActiveUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
128128

129129
size_t blockSize = blockEncoder.getCurrentOffset();
130130

src/libANGLE/renderer/vulkan/ProgramVk.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void InitDefaultUniformBlock(const std::vector<sh::ShaderVariable> &uniforms,
5858
}
5959

6060
VulkanDefaultBlockEncoder blockEncoder;
61-
sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
61+
sh::GetActiveUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
6262

6363
size_t blockSize = blockEncoder.getCurrentOffset();
6464

0 commit comments

Comments
 (0)