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.
//
#include "compiler/translator/tree_ops/vulkan/MonomorphizeUnsupportedFunctionsInVulkanGLSL.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
namespace sh
{
namespace
{
struct Argument
{
size_t argumentIndex;
TIntermTyped *argument;
};
struct FunctionData
{
// Whether the original function is used. If this is false, the function can be removed because
// all callers have been modified.
bool isOriginalUsed;
// The original definition of the function, used to create the monomorphized version.
TIntermFunctionDefinition *originalDefinition;
// List of monomorphized versions of this function. They will be added next to the original
// version (or replace it).
TVector<TIntermFunctionDefinition *> monomorphizedDefinitions;
};
using FunctionMap = angle::HashMap<const TFunction *, FunctionData>;
// Traverse the function definitions and initialize the map. Allows visitAggregate to have access
// to TIntermFunctionDefinition even when the function is only forward declared at that point.
void InitializeFunctionMap(TIntermBlock *root, FunctionMap *functionMapOut)
{
TIntermSequence &sequence = *root->getSequence();
for (TIntermNode *node : sequence)
{
TIntermFunctionDefinition *asFuncDef = node->getAsFunctionDefinition();
if (asFuncDef != nullptr)
{
const TFunction *function = asFuncDef->getFunction();
ASSERT(function && functionMapOut->find(function) == functionMapOut->end());
(*functionMapOut)[function] = FunctionData{false, asFuncDef, {}};
}
}
}
const TVariable *GetBaseUniform(TIntermTyped *node, bool *isSamplerInStructOut)
{
*isSamplerInStructOut = false;
while (node->getAsBinaryNode())
{
TIntermBinary *asBinary = node->getAsBinaryNode();
TOperator op = asBinary->getOp();
// No opaque uniform can be inside an interface block.
if (op == EOpIndexDirectInterfaceBlock)
{
return nullptr;
}
if (op == EOpIndexDirectStruct)
{
*isSamplerInStructOut = true;
}
node = asBinary->getLeft();
}
// Only interested in uniform opaque types. If a function call within another function uses
// opaque uniforms in an unsupported way, it will be replaced in a follow up pass after the
// calling function is monomorphized.
if (node->getType().getQualifier() != EvqUniform)
{
return nullptr;
}
ASSERT(IsOpaqueType(node->getType().getBasicType()) ||
node->getType().isStructureContainingSamplers());
TIntermSymbol *asSymbol = node->getAsSymbolNode();
ASSERT(asSymbol);
return &asSymbol->variable();
}
TIntermTyped *ExtractSideEffects(TSymbolTable *symbolTable,
TIntermTyped *node,
TIntermSequence *replacementIndices)
{
TIntermTyped *withoutSideEffects = node->deepCopy();
for (TIntermBinary *asBinary = withoutSideEffects->getAsBinaryNode(); asBinary;
asBinary = asBinary->getLeft()->getAsBinaryNode())
{
TOperator op = asBinary->getOp();
TIntermTyped *index = asBinary->getRight();
if (op == EOpIndexDirectStruct)
{
break;
}
// No side effects with constant expressions.
if (op == EOpIndexDirect)
{
ASSERT(index->getAsConstantUnion());
continue;
}
ASSERT(op == EOpIndexIndirect);
// If the index is a symbol, there's no side effect, so leave it as-is.
if (index->getAsSymbolNode())
{
continue;
}
// Otherwise create a temp variable initialized with the index and use that temp variable as
// the index.
TIntermDeclaration *tempDecl = nullptr;
TVariable *tempVar = DeclareTempVariable(symbolTable, index, EvqTemporary, &tempDecl);
replacementIndices->push_back(tempDecl);
asBinary->replaceChildNode(index, new TIntermSymbol(tempVar));
}
return withoutSideEffects;
}
void CreateMonomorphizedFunctionCallArgs(const TIntermSequence &originalCallArguments,
const TVector<Argument> &replacedArguments,
TIntermSequence *substituteArgsOut)
{
size_t nextReplacedArg = 0;
for (size_t argIndex = 0; argIndex < originalCallArguments.size(); ++argIndex)
{
if (nextReplacedArg >= replacedArguments.size() ||
argIndex != replacedArguments[nextReplacedArg].argumentIndex)
{
// Not replaced, keep argument as is.
substituteArgsOut->push_back(originalCallArguments[argIndex]);
}
else
{
TIntermTyped *argument = replacedArguments[nextReplacedArg].argument;
// Iterate over indices of the argument and create a new arg for every non-const
// index. Note that the index itself may be an expression, and it may require further
// substitution in the next pass.
while (argument->getAsBinaryNode())
{
TIntermBinary *asBinary = argument->getAsBinaryNode();
if (asBinary->getOp() == EOpIndexIndirect)
{
TIntermTyped *index = asBinary->getRight();
substituteArgsOut->push_back(index->deepCopy());
}
argument = asBinary->getLeft();
}
++nextReplacedArg;
}
}
}
const TFunction *MonomorphizeFunction(TSymbolTable *symbolTable,
const TFunction *original,
TVector<Argument> *replacedArguments,
VariableReplacementMap *argumentMapOut)
{
TFunction *substituteFunction =
new TFunction(symbolTable, kEmptyImmutableString, SymbolType::AngleInternal,
&original->getReturnType(), original->isKnownToNotHaveSideEffects());
size_t nextReplacedArg = 0;
for (size_t paramIndex = 0; paramIndex < original->getParamCount(); ++paramIndex)
{
const TVariable *originalParam = original->getParam(paramIndex);
if (nextReplacedArg >= replacedArguments->size() ||
paramIndex != (*replacedArguments)[nextReplacedArg].argumentIndex)
{
// Not replaced, add an identical parameter.
substituteFunction->addParameter(new TVariable(symbolTable, originalParam->name(),
&originalParam->getType(),
originalParam->symbolType()));
}
else
{
TIntermTyped *substituteArgument = (*replacedArguments)[nextReplacedArg].argument;
(*argumentMapOut)[originalParam] = substituteArgument;
// Iterate over indices of the argument and create a new parameter for every non-const
// index (which may be an expression). Replace the symbol in the argument with a
// variable of the index type. This is later used to replace the parameter in the
// function body.
while (substituteArgument->getAsBinaryNode())
{
TIntermBinary *asBinary = substituteArgument->getAsBinaryNode();
if (asBinary->getOp() == EOpIndexIndirect)
{
TIntermTyped *index = asBinary->getRight();
TVariable *param = new TVariable(symbolTable, kEmptyImmutableString,
&index->getType(), SymbolType::AngleInternal);
substituteFunction->addParameter(param);
// The argument now uses the function parameters as indices.
asBinary->replaceChildNode(asBinary->getRight(), new TIntermSymbol(param));
}
substituteArgument = asBinary->getLeft();
}
++nextReplacedArg;
}
}
return substituteFunction;
}
class MonomorphizeTraverser final : public TIntermTraverser
{
public:
explicit MonomorphizeTraverser(TCompiler *compiler,
TSymbolTable *symbolTable,
FunctionMap *functionMap)
: TIntermTraverser(true, false, false, symbolTable),
mCompiler(compiler),
mFunctionMap(functionMap)
{}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
if (node->getOp() != EOpCallFunctionInAST)
{
return true;
}
const TFunction *function = node->getFunction();
ASSERT(function && mFunctionMap->find(function) != mFunctionMap->end());
FunctionData &data = (*mFunctionMap)[function];
TIntermFunctionDefinition *monomorphized =
processFunctionCall(node, data.originalDefinition, &data.isOriginalUsed);
if (monomorphized)
{
data.monomorphizedDefinitions.push_back(monomorphized);
}
return true;
}
bool getAnyMonomorphized() const { return mAnyMonomorphized; }
private:
TIntermFunctionDefinition *processFunctionCall(TIntermAggregate *functionCall,
TIntermFunctionDefinition *originalDefinition,
bool *isOriginalUsedOut)
{
const TFunction *function = functionCall->getFunction();
const TIntermSequence &callArguments = *functionCall->getSequence();
TVector<Argument> replacedArguments;
TIntermSequence replacementIndices;
// Go through function call arguments, and see if any is used in an unsupported way.
for (size_t argIndex = 0; argIndex < callArguments.size(); ++argIndex)
{
TIntermTyped *callArgument = callArguments[argIndex]->getAsTyped();
const TVariable *funcArgument = function->getParam(argIndex);
// Only interested in opaque uniforms and structs that contain samplers.
const bool isOpaqueType = IsOpaqueType(funcArgument->getType().getBasicType());
const bool isStructContainingSamplers =
funcArgument->getType().isStructureContainingSamplers();
if (!isOpaqueType && !isStructContainingSamplers)
{
continue;
}
// If not uniform (the variable was itself a function parameter), don't process it in
// this pass, as we don't know which actual uniform it corresponds to.
bool isSamplerInStruct = false;
const TVariable *uniform = GetBaseUniform(callArgument, &isSamplerInStruct);
if (uniform == nullptr)
{
continue;
}
// Conditions for monomorphization:
//
// - If the parameter is a structure that contains samplers (so in RewriteStructSamplers
// we don't need to rewrite the functions to accept multiple parameters split from the
// struct), or
// - If the opaque uniform is a sampler in a struct (which can create an array-of-array
// situation), and the function expects an array of samplers, or
// - If the opaque uniform is an array of array of sampler or image, and it's partially
// subscripted (i.e. the function itself expects an array), or
// - The opaque uniform is an atomic counter
//
const TType &type = uniform->getType();
const bool isArrayOfArrayOfSamplerOrImage =
(type.isSampler() || type.isImage()) && type.isArrayOfArrays();
const bool isParameterArrayOfOpaqueType = funcArgument->getType().isArray();
const bool isAtomicCounter = type.isAtomicCounter();
if (!(isStructContainingSamplers ||
(isSamplerInStruct && isParameterArrayOfOpaqueType) ||
(isArrayOfArrayOfSamplerOrImage && isParameterArrayOfOpaqueType) ||
isAtomicCounter))
{
continue;
}
// Copy the argument and extract the side effects.
TIntermTyped *argument =
ExtractSideEffects(mSymbolTable, callArgument, &replacementIndices);
replacedArguments.push_back({argIndex, argument});
}
if (replacedArguments.empty())
{
*isOriginalUsedOut = true;
return nullptr;
}
mAnyMonomorphized = true;
insertStatementsInParentBlock(replacementIndices);
// Create the arguments for the substitute function call. Done before monomorphizing the
// function, which transforms the arguments to what needs to be replaced in the function
// body.
TIntermSequence newCallArgs;
CreateMonomorphizedFunctionCallArgs(callArguments, replacedArguments, &newCallArgs);
// Duplicate the function and substitute the replaced arguments with only the non-const
// indices. Additionally, substitute the non-const indices of arguments with the new
// function parameters.
VariableReplacementMap argumentMap;
const TFunction *monomorphized =
MonomorphizeFunction(mSymbolTable, function, &replacedArguments, &argumentMap);
// Replace this function call with a call to the new one.
queueReplacement(TIntermAggregate::CreateFunctionCall(*monomorphized, &newCallArgs),
OriginalNode::IS_DROPPED);
// Create a new function definition, with the body of the old function but with the replaced
// parameters substituted with the calling expressions.
TIntermFunctionPrototype *substitutePrototype = new TIntermFunctionPrototype(monomorphized);
TIntermBlock *substituteBlock = originalDefinition->getBody()->deepCopy();
GetDeclaratorReplacements(mSymbolTable, substituteBlock, &argumentMap);
bool valid = ReplaceVariables(mCompiler, substituteBlock, argumentMap);
ASSERT(valid);
return new TIntermFunctionDefinition(substitutePrototype, substituteBlock);
}
TCompiler *mCompiler;
bool mAnyMonomorphized = false;
// Map of original to monomorphized functions.
FunctionMap *mFunctionMap;
};
class UpdateFunctionsDefinitionsTraverser final : public TIntermTraverser
{
public:
explicit UpdateFunctionsDefinitionsTraverser(TSymbolTable *symbolTable,
const FunctionMap &functionMap)
: TIntermTraverser(true, false, false, symbolTable), mFunctionMap(functionMap)
{}
void visitFunctionPrototype(TIntermFunctionPrototype *node) override
{
const bool isInFunctionDefinition = getParentNode()->getAsFunctionDefinition() != nullptr;
if (isInFunctionDefinition)
{
return;
}
// Add to and possibly replace the function prototype with replacement prototypes.
const TFunction *function = node->getFunction();
ASSERT(function && mFunctionMap.find(function) != mFunctionMap.end());
const FunctionData &data = mFunctionMap.at(function);
// If nothing to do, leave it be.
if (data.monomorphizedDefinitions.empty())
{
ASSERT(data.isOriginalUsed);
return;
}
// Replace the prototype with itself (if function is still used) as well as any
// monomorphized versions.
TIntermSequence replacement;
if (data.isOriginalUsed)
{
replacement.push_back(node);
}
for (TIntermFunctionDefinition *monomorphizedDefinition : data.monomorphizedDefinitions)
{
replacement.push_back(new TIntermFunctionPrototype(
monomorphizedDefinition->getFunctionPrototype()->getFunction()));
}
mMultiReplacements.emplace_back(getParentNode()->getAsBlock(), node, replacement);
}
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
{
// Add to and possibly replace the function definition with replacement definitions.
const TFunction *function = node->getFunction();
ASSERT(function && mFunctionMap.find(function) != mFunctionMap.end());
const FunctionData &data = mFunctionMap.at(function);
// If nothing to do, leave it be.
if (data.monomorphizedDefinitions.empty())
{
ASSERT(data.isOriginalUsed || function->name() == "main");
return false;
}
// Replace the definition with itself (if function is still used) as well as any
// monomorphized versions.
TIntermSequence replacement;
if (data.isOriginalUsed)
{
replacement.push_back(node);
}
for (TIntermFunctionDefinition *monomorphizedDefinition : data.monomorphizedDefinitions)
{
replacement.push_back(monomorphizedDefinition);
}
mMultiReplacements.emplace_back(getParentNode()->getAsBlock(), node, replacement);
return false;
}
private:
const FunctionMap &mFunctionMap;
};
void SortDeclarations(TIntermBlock *root)
{
TIntermSequence *original = root->getSequence();
TIntermSequence replacement;
TIntermSequence functionDefs;
// Accumulate non-function-definition declarations in |replacement| and function definitions in
// |functionDefs|.
for (TIntermNode *node : *original)
{
if (node->getAsFunctionDefinition() || node->getAsFunctionPrototypeNode())
{
functionDefs.push_back(node);
}
else
{
replacement.push_back(node);
}
}
// Append function definitions to |replacement|.
replacement.insert(replacement.end(), functionDefs.begin(), functionDefs.end());
// Replace root's sequence with |replacement|.
root->replaceAllChildren(replacement);
}
} // anonymous namespace
bool MonomorphizeUnsupportedFunctionsInVulkanGLSL(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable)
{
// First, sort out the declarations such that all non-function declarations are placed before
// function definitions. This way when the function is replaced with one that references said
// declarations (i.e. uniforms), the uniform declaration is already present above it.
SortDeclarations(root);
while (true)
{
FunctionMap functionMap;
InitializeFunctionMap(root, &functionMap);
MonomorphizeTraverser monomorphizer(compiler, symbolTable, &functionMap);
root->traverse(&monomorphizer);
if (!monomorphizer.getAnyMonomorphized())
{
break;
}
if (!monomorphizer.updateTree(compiler, root))
{
return false;
}
UpdateFunctionsDefinitionsTraverser functionUpdater(symbolTable, functionMap);
root->traverse(&functionUpdater);
if (!functionUpdater.updateTree(compiler, root))
{
return false;
}
}
return true;
}
} // namespace sh
//
// 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