Commit dc99fc40 by Shahbaz Youssefi Committed by Commit Bot

Vulkan: Translator pass to monomorphize problematic functions

With array of array of samplers and images, we currently require the shader*ArrayDynamicIndexing Vulkan features. With atomic counters, we require the shaderStorageBufferArrayDynamicIndexing feature. The above features are required to enable passing opaque uniforms to functions. This change introduces a translator pass that monomorphizes functions that receive atomic counters, or partially subscripted array of array of samplers or images, etc by removing those arguments and using the opaque uniform directly. Follow up changes will include: - Great simplification to RewriteStructSamplers, and removal of RewriteStructSamplersOld. This will drop dependency to shaderSampledImageArrayDynamicIndexing and shaderStorageImageArrayDynamicIndexing. - Great simplification to RewriteAtomicCounters. This will drop dependency to shaderStorageBufferArrayDynamicIndexing. - Emulation of imageAtomicExchange for r32f formats, but changing the qualifier to r32ui. Note that parts of RewriteStructSampler are obsolete with this change, but will be refactored as a follow up. Bug: angleproject:3881 Bug: angleproject:4071 Bug: angleproject:5535 Change-Id: Ifd1435b2a31ebf364815046886aeded60297da79 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2628127 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarTim Van Patten <timvp@google.com>
parent d06feeac
......@@ -311,6 +311,8 @@ angle_translator_lib_vulkan_sources = [
"src/compiler/translator/tree_ops/vulkan/EarlyFragmentTestsOptimization.cpp",
"src/compiler/translator/tree_ops/vulkan/FlagSamplersWithTexelFetch.cpp",
"src/compiler/translator/tree_ops/vulkan/FlagSamplersWithTexelFetch.h",
"src/compiler/translator/tree_ops/vulkan/MonomorphizeUnsupportedFunctionsInVulkanGLSL.cpp",
"src/compiler/translator/tree_ops/vulkan/MonomorphizeUnsupportedFunctionsInVulkanGLSL.h",
"src/compiler/translator/tree_ops/vulkan/NameEmbeddedUniformStructs.cpp",
"src/compiler/translator/tree_ops/vulkan/NameEmbeddedUniformStructs.h",
"src/compiler/translator/tree_ops/vulkan/RemoveAtomicCounterBuiltins.cpp",
......
......@@ -430,6 +430,12 @@ bool TIntermBlock::replaceChildNode(TIntermNode *original, TIntermNode *replacem
return replaceChildNodeInternal(original, replacement);
}
void TIntermBlock::replaceAllChildren(const TIntermSequence &newStatements)
{
mStatements.clear();
mStatements.insert(mStatements.begin(), newStatements.begin(), newStatements.end());
}
size_t TIntermFunctionPrototype::getChildCount() const
{
return 0;
......@@ -461,6 +467,14 @@ bool TIntermDeclaration::replaceChildNode(TIntermNode *original, TIntermNode *re
return replaceChildNodeInternal(original, replacement);
}
TIntermDeclaration::TIntermDeclaration(const TIntermDeclaration &node)
{
for (TIntermNode *node : node.mDeclarators)
{
mDeclarators.push_back(node->deepCopy());
}
}
bool TIntermAggregateBase::replaceChildNodeInternal(TIntermNode *original, TIntermNode *replacement)
{
for (size_t ii = 0; ii < getSequence()->size(); ++ii)
......
......@@ -683,6 +683,7 @@ class TIntermBlock : public TIntermNode, public TIntermAggregateBase
size_t getChildCount() const final;
TIntermNode *getChildNode(size_t index) const final;
bool replaceChildNode(TIntermNode *original, TIntermNode *replacement) override;
void replaceAllChildren(const TIntermSequence &newStatements);
// Only intended for initially building the block.
void appendStatement(TIntermNode *statement);
......@@ -793,13 +794,23 @@ class TIntermDeclaration : public TIntermNode, public TIntermAggregateBase
TIntermSequence *getSequence() override { return &mDeclarators; }
const TIntermSequence *getSequence() const override { return &mDeclarators; }
TIntermNode *deepCopy() const override
TIntermDeclaration *deepCopy() const override
{
UNREACHABLE();
return nullptr;
// Note: This is only useful as support for deepCopy of TIntermBlock and TIntermLoop, but is
// not sufficient as it will be redeclaring the same TVariable. If a function body is
// duplicated for example, it means that both functions reference the same TVariable pointer
// which works, but is technically not correct. In particular, maps with TVariable * as key
// can get confused.
//
// After deepCopy() is issued, ReplaceVariables must be used to replace every declared
// variable with a duplicate. This is NOT automatically done when deepCopy-ing TIntermBlock
// and TIntermLoop nodes.
return new TIntermDeclaration(*this);
}
protected:
TIntermDeclaration(const TIntermDeclaration &node);
TIntermSequence mDeclarators;
};
......
......@@ -20,16 +20,14 @@
#include "compiler/translator/OutputVulkanGLSL.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/tree_ops/vulkan/FlagSamplersWithTexelFetch.h"
#include "compiler/translator/tree_ops/vulkan/MonomorphizeUnsupportedFunctionsInVulkanGLSL.h"
#include "compiler/translator/tree_ops/vulkan/NameEmbeddedUniformStructs.h"
#include "compiler/translator/tree_ops/vulkan/RemoveAtomicCounterBuiltins.h"
#include "compiler/translator/tree_ops/vulkan/RemoveInactiveInterfaceVariables.h"
#include "compiler/translator/tree_ops/vulkan/RewriteAtomicCounters.h"
#include "compiler/translator/tree_ops/vulkan/RewriteCubeMapSamplersAs2DArray.h"
#include "compiler/translator/tree_ops/vulkan/RewriteDfdy.h"
#include "compiler/translator/tree_ops/vulkan/RewriteInterpolateAtOffset.h"
#include "compiler/translator/tree_ops/vulkan/RewriteStructSamplers.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/DriverUniform.h"
......@@ -761,8 +759,21 @@ bool TranslatorVulkan::translateImpl(TIntermBlock *root,
return false;
}
// TODO(lucferron): Refactor this function to do fewer tree traversals.
// http://anglebug.com/2461
// If there are any function calls that take array-of-array of opaque uniform parameters, or
// other opaque uniforms that need special handling in Vulkan, such as atomic counters,
// monomorphize the functions by removing said parameters and replacing them in the function
// body with the call arguments.
//
// This has a few benefits:
//
// - It dramatically simplifies future transformations w.r.t to samplers in structs, array of
// arrays of opaque types, atomic counters etc.
// - Avoids the need for shader*ArrayDynamicIndexing Vulkan features.
if (!MonomorphizeUnsupportedFunctionsInVulkanGLSL(this, root, &getSymbolTable()))
{
return false;
}
if (aggregateTypesUsedForUniforms > 0)
{
if (!NameEmbeddedStructUniforms(this, root, &getSymbolTable()))
......
//
// Copyright 2021 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// MonomorphizeUnsupportedFunctionsInVulkanGLSL: Monomorphize functions that are called with
// parameters that are not compatible with Vulkan GLSL:
//
// - Samplers in structs
// - Structs that have samplers
// - Partially subscripted array of array of samplers
// - Partially subscripted array of array of images
// - Atomic counters
//
// This transformation basically duplicates such functions, removes the
// sampler/image/atomic_counter parameters and uses the opaque uniforms used by the caller.
#ifndef COMPILER_TRANSLATOR_TREEOPS_VULKAN_MONOMORPHIZEUNSUPPORTEDFUNCTIONSINVULKANGLSL_H_
#define COMPILER_TRANSLATOR_TREEOPS_VULKAN_MONOMORPHIZEUNSUPPORTEDFUNCTIONSINVULKANGLSL_H_
#include "common/angleutils.h"
namespace sh
{
class TCompiler;
class TIntermBlock;
class TSymbolTable;
ANGLE_NO_DISCARD bool MonomorphizeUnsupportedFunctionsInVulkanGLSL(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable);
} // namespace sh
#endif // COMPILER_TRANSLATOR_TREEOPS_VULKAN_MONOMORPHIZEUNSUPPORTEDFUNCTIONSINVULKANGLSL_H_
......@@ -40,6 +40,67 @@ class ReplaceVariableTraverser : public TIntermTraverser
const TIntermTyped *const mReplacement;
};
class ReplaceVariablesTraverser : public TIntermTraverser
{
public:
ReplaceVariablesTraverser(const VariableReplacementMap &variableMap)
: TIntermTraverser(true, false, false), mVariableMap(variableMap)
{}
void visitSymbol(TIntermSymbol *node) override
{
auto iter = mVariableMap.find(&node->variable());
if (iter != mVariableMap.end())
{
queueReplacement(iter->second->deepCopy(), OriginalNode::IS_DROPPED);
}
}
private:
const VariableReplacementMap &mVariableMap;
};
class GetDeclaratorReplacementsTraverser : public TIntermTraverser
{
public:
GetDeclaratorReplacementsTraverser(TSymbolTable *symbolTable,
VariableReplacementMap *variableMap)
: TIntermTraverser(true, false, false, symbolTable), mVariableMap(variableMap)
{}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
const TIntermSequence &sequence = *(node->getSequence());
for (TIntermNode *decl : sequence)
{
TIntermSymbol *asSymbol = decl->getAsSymbolNode();
TIntermBinary *asBinary = decl->getAsBinaryNode();
if (asBinary != nullptr)
{
ASSERT(asBinary->getOp() == EOpInitialize);
asSymbol = asBinary->getLeft()->getAsSymbolNode();
}
ASSERT(asSymbol);
const TVariable &variable = asSymbol->variable();
ASSERT(mVariableMap->find(&variable) == mVariableMap->end());
const TVariable *replacementVariable = new TVariable(
mSymbolTable, variable.name(), &variable.getType(), variable.symbolType());
(*mVariableMap)[&variable] = new TIntermSymbol(replacementVariable);
}
return false;
}
private:
VariableReplacementMap *mVariableMap;
};
} // anonymous namespace
// Replaces every occurrence of a variable with another variable.
......@@ -53,6 +114,23 @@ ANGLE_NO_DISCARD bool ReplaceVariable(TCompiler *compiler,
return traverser.updateTree(compiler, root);
}
ANGLE_NO_DISCARD bool ReplaceVariables(TCompiler *compiler,
TIntermBlock *root,
const VariableReplacementMap &variableMap)
{
ReplaceVariablesTraverser traverser(variableMap);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
void GetDeclaratorReplacements(TSymbolTable *symbolTable,
TIntermBlock *root,
VariableReplacementMap *variableMap)
{
GetDeclaratorReplacementsTraverser traverser(symbolTable, variableMap);
root->traverse(&traverser);
}
// Replaces every occurrence of a variable with a TIntermNode.
ANGLE_NO_DISCARD bool ReplaceVariableWithTyped(TCompiler *compiler,
TIntermBlock *root,
......
......@@ -36,6 +36,20 @@ ANGLE_NO_DISCARD bool ReplaceVariableWithTyped(TCompiler *compiler,
const TVariable *toBeReplaced,
const TIntermTyped *replacement);
using VariableReplacementMap = angle::HashMap<const TVariable *, const TIntermTyped *>;
// Replace a set of variables with their corresponding expression.
ANGLE_NO_DISCARD bool ReplaceVariables(TCompiler *compiler,
TIntermBlock *root,
const VariableReplacementMap &variableMap);
// Find all declarators, and replace the TVariable they are declaring with a duplicate. This is
// used to support deepCopy of TIntermBlock and TIntermLoop nodes that include declarations.
// Replacements already present in variableMap are preserved.
void GetDeclaratorReplacements(TSymbolTable *symbolTable,
TIntermBlock *root,
VariableReplacementMap *variableMap);
// A helper class to keep track of opaque variable re-typing during a pass. Unlike the above
// functions, this can be used to replace all opaque variables of a certain type with another in a
// pass that possibly does other related transformations. Only opaque variables are supported as
......
......@@ -4209,10 +4209,13 @@ TEST_P(GLSLTest_ES31, ParameterArrayArrayArraySampler)
GLint numTextures;
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &numTextures);
ANGLE_SKIP_TEST_IF(numTextures < 2 * 3 * 4 + 4);
// anglebug.com/3832 - no sampler array params on Android
ANGLE_SKIP_TEST_IF(IsAndroid() && IsOpenGLES());
// Seems like this is failing on Windows Intel?
// http://anglebug.com/5546
ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsOpenGL());
constexpr char kFS[] =
"#version 310 es\n"
"precision mediump float;\n"
......@@ -8521,6 +8524,229 @@ void main(void)
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
// Test that sending mixture of resources to functions works.
TEST_P(GLSLTest_ES31, MixOfResourcesAsFunctionArgs)
{
// http://anglebug.com/5546
ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsOpenGL());
// anglebug.com/3832 - no sampler array params on Android
ANGLE_SKIP_TEST_IF(IsAndroid() && IsOpenGLES());
// anglebug.com/2703 - QC doesn't support arrays of samplers as parameters,
// so sampler array of array handling is disabled
ANGLE_SKIP_TEST_IF(IsAndroid() && IsVulkan());
constexpr char kComputeShader[] = R"(#version 310 es
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 1, std430) buffer Output {
uint success;
} outbuf;
uniform uint initialAcValue;
uniform sampler2D smplr[2][3];
layout(binding=0) uniform atomic_uint ac;
bool sampler1DAndAtomicCounter(uvec3 sExpect, in sampler2D s[3], in atomic_uint a, uint aExpect)
{
uvec3 sResult = uvec3(uint(texture(s[0], vec2(0.5, 0.5)).x * 255.0),
uint(texture(s[1], vec2(0.5, 0.5)).x * 255.0),
uint(texture(s[2], vec2(0.5, 0.5)).x * 255.0));
uint aResult = atomicCounterIncrement(a);
return sExpect == sResult && aExpect == aResult;
}
bool sampler2DAndAtomicCounter(in sampler2D s[2][3], uint aInitial, in atomic_uint a)
{
bool success = true;
success = sampler1DAndAtomicCounter(uvec3(0, 127, 255), s[0], a, aInitial) && success;
success = sampler1DAndAtomicCounter(uvec3(31, 63, 191), s[1], a, aInitial + 1u) && success;
return success;
}
void main(void)
{
outbuf.success = uint(sampler2DAndAtomicCounter(smplr, initialAcValue, ac));
}
)";
ANGLE_GL_COMPUTE_PROGRAM(program, kComputeShader);
EXPECT_GL_NO_ERROR();
glUseProgram(program);
unsigned int outputInitData = 0x12345678u;
GLBuffer outputBuffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, outputBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(outputInitData), &outputInitData, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, outputBuffer);
EXPECT_GL_NO_ERROR();
unsigned int acData = 2u;
GLint uniformLocation = glGetUniformLocation(program, "initialAcValue");
ASSERT_NE(uniformLocation, -1);
glUniform1ui(uniformLocation, acData);
GLBuffer atomicCounterBuffer;
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicCounterBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(acData), &acData, GL_STATIC_DRAW);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicCounterBuffer);
EXPECT_GL_NO_ERROR();
const std::array<GLColor, 6> kTextureData = {
GLColor(0, 0, 0, 0), GLColor(127, 0, 0, 0), GLColor(255, 0, 0, 0),
GLColor(31, 0, 0, 0), GLColor(63, 0, 0, 0), GLColor(191, 0, 0, 0),
};
GLTexture textures[2][3];
for (int dim1 = 0; dim1 < 2; ++dim1)
{
for (int dim2 = 0; dim2 < 3; ++dim2)
{
int textureUnit = dim1 * 3 + dim2;
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textures[dim1][dim2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&kTextureData[textureUnit]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
std::stringstream uniformName;
uniformName << "smplr[" << dim1 << "][" << dim2 << "]";
GLint samplerLocation = glGetUniformLocation(program, uniformName.str().c_str());
EXPECT_NE(samplerLocation, -1);
glUniform1i(samplerLocation, textureUnit);
}
}
ASSERT_GL_NO_ERROR();
glDispatchCompute(1, 1, 1);
EXPECT_GL_NO_ERROR();
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
// read back
const GLuint *ptr = reinterpret_cast<const GLuint *>(
glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(outputInitData), GL_MAP_READ_BIT));
EXPECT_EQ(ptr[0], 1u);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
// Test that array of array of samplers used as function parameter with an index that has a
// side-effect works.
TEST_P(GLSLTest_ES31, ArrayOfArrayOfSamplerAsFunctionParameterIndexedWithSideEffect)
{
// http://anglebug.com/5546
ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsOpenGL());
// anglebug.com/3832 - no sampler array params on Android
ANGLE_SKIP_TEST_IF(IsAndroid() && IsOpenGLES());
// anglebug.com/2703 - QC doesn't support arrays of samplers as parameters,
// so sampler array of array handling is disabled
ANGLE_SKIP_TEST_IF(IsAndroid() && IsVulkan());
// Skip if EXT_gpu_shader5 is not enabled.
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_gpu_shader5"));
constexpr char kComputeShader[] = R"(#version 310 es
#extension GL_EXT_gpu_shader5 : require
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(binding = 1, std430) buffer Output {
uint success;
} outbuf;
uniform sampler2D smplr[2][3];
layout(binding=0) uniform atomic_uint ac;
bool sampler1DAndAtomicCounter(uvec3 sExpect, in sampler2D s[3], in atomic_uint a, uint aExpect)
{
uvec3 sResult = uvec3(uint(texture(s[0], vec2(0.5, 0.5)).x * 255.0),
uint(texture(s[1], vec2(0.5, 0.5)).x * 255.0),
uint(texture(s[2], vec2(0.5, 0.5)).x * 255.0));
uint aResult = atomicCounter(a);
return sExpect == sResult && aExpect == aResult;
}
bool sampler2DAndAtomicCounter(in sampler2D s[2][3], uint aInitial, in atomic_uint a)
{
bool success = true;
success = sampler1DAndAtomicCounter(uvec3(0, 127, 255),
s[atomicCounterIncrement(ac)], a, aInitial + 1u) && success;
success = sampler1DAndAtomicCounter(uvec3(31, 63, 191),
s[atomicCounterIncrement(ac)], a, aInitial + 2u) && success;
return success;
}
void main(void)
{
outbuf.success = uint(sampler2DAndAtomicCounter(smplr, 0u, ac));
}
)";
ANGLE_GL_COMPUTE_PROGRAM(program, kComputeShader);
EXPECT_GL_NO_ERROR();
glUseProgram(program);
unsigned int outputInitData = 0x12345678u;
GLBuffer outputBuffer;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, outputBuffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(outputInitData), &outputInitData, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, outputBuffer);
EXPECT_GL_NO_ERROR();
unsigned int acData = 0u;
GLBuffer atomicCounterBuffer;
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, atomicCounterBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(acData), &acData, GL_STATIC_DRAW);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, atomicCounterBuffer);
EXPECT_GL_NO_ERROR();
const std::array<GLColor, 6> kTextureData = {
GLColor(0, 0, 0, 0), GLColor(127, 0, 0, 0), GLColor(255, 0, 0, 0),
GLColor(31, 0, 0, 0), GLColor(63, 0, 0, 0), GLColor(191, 0, 0, 0),
};
GLTexture textures[2][3];
for (int dim1 = 0; dim1 < 2; ++dim1)
{
for (int dim2 = 0; dim2 < 3; ++dim2)
{
int textureUnit = dim1 * 3 + dim2;
glActiveTexture(GL_TEXTURE0 + textureUnit);
glBindTexture(GL_TEXTURE_2D, textures[dim1][dim2]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&kTextureData[textureUnit]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
std::stringstream uniformName;
uniformName << "smplr[" << dim1 << "][" << dim2 << "]";
GLint samplerLocation = glGetUniformLocation(program, uniformName.str().c_str());
EXPECT_NE(samplerLocation, -1);
glUniform1i(samplerLocation, textureUnit);
}
}
ASSERT_GL_NO_ERROR();
glDispatchCompute(1, 1, 1);
EXPECT_GL_NO_ERROR();
glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
// read back
const GLuint *ptr = reinterpret_cast<const GLuint *>(
glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(outputInitData), GL_MAP_READ_BIT));
EXPECT_EQ(ptr[0], 1u);
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
}
// Test that multiple nested assignments are handled correctly.
TEST_P(GLSLTest_ES31, MixedRowAndColumnMajorMatrices_WriteSideEffect)
{
......
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