Hash :
d57ce815
Author :
Date :
2022-07-16T16:33:08
Use "readwrite" PLS images when possible We actually only need readonly/writeonly aliases on ESSL, non r32f/r32ui. For all other cases, this change updates the compiler to emit a single readwrite image. We also optimize this image with the "restrict" qualifier since PLS specifically disallows aliasing. Removing the aliased load and store also eliminates our issue with an Intel driver bug, and all the PLS tests now pass without any workarounds. Bug: angleproject:7279 Bug: angleproject:7398 Change-Id: I350b239793647da33add96509b8f4b1bbef02245 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3767537 Commit-Queue: Chris Dalton <chris@rive.app> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
//
// Copyright 2022 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.
//
#include "compiler/translator/tree_ops/RewritePixelLocalStorage.h"
#include "common/angleutils.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_ops/MonomorphizeUnsupportedFunctions.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/RunAtTheBeginningOfShader.h"
namespace sh
{
namespace
{
constexpr static TBasicType DataTypeOfPLSType(TBasicType plsType)
{
switch (plsType)
{
case EbtPixelLocalANGLE:
return EbtFloat;
case EbtIPixelLocalANGLE:
return EbtInt;
case EbtUPixelLocalANGLE:
return EbtUInt;
default:
UNREACHABLE();
return EbtVoid;
}
}
constexpr static TBasicType Image2DTypeOfPLSType(TBasicType plsType)
{
switch (plsType)
{
case EbtPixelLocalANGLE:
return EbtImage2D;
case EbtIPixelLocalANGLE:
return EbtIImage2D;
case EbtUPixelLocalANGLE:
return EbtUImage2D;
default:
UNREACHABLE();
return EbtVoid;
}
}
// Rewrites high level PLS operations to shader image operations.
class RewriteToImagesTraverser : public TIntermTraverser
{
public:
RewriteToImagesTraverser(TCompiler *compiler, TSymbolTable &symbolTable, int shaderVersion)
: TIntermTraverser(true, false, false, &symbolTable),
mCompiler(compiler),
mShaderVersion(shaderVersion)
{}
bool visitDeclaration(Visit, TIntermDeclaration *decl) override
{
TIntermTyped *declVariable = (decl->getSequence())->front()->getAsTyped();
ASSERT(declVariable);
if (!IsPixelLocal(declVariable->getBasicType()))
{
return true;
}
// PLS is not allowed in arrays.
ASSERT(!declVariable->isArray());
// This visitDeclaration doesn't get called for function arguments, and opaque types can
// otherwise only be uniforms.
ASSERT(declVariable->getQualifier() == EvqUniform);
TIntermSymbol *plsSymbol = declVariable->getAsSymbolNode();
ASSERT(plsSymbol);
// Insert a global to hold the pixel coordinate as soon as we see PLS declared. This will be
// initialized at the beginning of main().
if (!mGlobalPixelCoord)
{
TType *coordType = new TType(EbtInt, EbpHigh, EvqGlobal, 2);
mGlobalPixelCoord = CreateTempVariable(mSymbolTable, coordType);
insertStatementInParentBlock(CreateTempDeclarationNode(mGlobalPixelCoord));
}
PLSImages &pls = insertNullPLSImages(plsSymbol);
TLayoutImageInternalFormat format =
plsSymbol->getType().getLayoutQualifier().imageInternalFormat;
if (mCompiler->getOutputType() == ShShaderOutput::SH_ESSL_OUTPUT &&
format != TLayoutImageInternalFormat::EiifR32F &&
format != TLayoutImageInternalFormat::EiifR32I &&
format != TLayoutImageInternalFormat::EiifR32UI)
{
// ES 3.1 requires image formats other than r32f/r32i/r32ui to be either readonly or
// writeonly. To work around this, we create two aliases of the same image: one readonly
// and one writeonly.
//
// TODO(anglebug.com/7279): Maybe we could manually pack 4-byte formats into GL_R32UI
// instead of aliasing them. We could also walk the tree first and see which image is
// used how. If the image is never loaded, no need to generate the readonly binding for
// example.
//
// First insert a readonly image2D directly before the PLS declaration.
pls.image2DForLoading = createPLSImage(plsSymbol, ImageAccess::ReadOnly);
insertStatementInParentBlock(
new TIntermDeclaration({new TIntermSymbol(pls.image2DForLoading)}));
// Replace the PLS declaration with a writeonly image2D.
pls.image2DForStoring = createPLSImage(plsSymbol, ImageAccess::WriteOnly);
queueReplacement(new TIntermDeclaration({new TIntermSymbol(pls.image2DForStoring)}),
OriginalNode::IS_DROPPED);
}
else
{
// Replace the PLS declaration with a single readwrite image2D.
TVariable *readWriteImage = createPLSImage(plsSymbol, ImageAccess::ReadWrite);
queueReplacement(new TIntermDeclaration({new TIntermSymbol(readWriteImage)}),
OriginalNode::IS_DROPPED);
pls.image2DForLoading = pls.image2DForStoring = readWriteImage;
}
return false;
}
bool visitAggregate(Visit, TIntermAggregate *aggregate) override
{
if (!BuiltInGroup::IsPixelLocal(aggregate->getOp()))
{
return true;
}
const TIntermSequence &args = *aggregate->getSequence();
ASSERT(args.size() >= 1);
TIntermSymbol *plsSymbol = args[0]->getAsSymbolNode();
const PLSImages &pls = findPLSImages(plsSymbol);
ASSERT(mGlobalPixelCoord);
// Rewrite pixelLocalLoadANGLE -> imageLoad.
if (aggregate->getOp() == EOpPixelLocalLoadANGLE)
{
// Replace the pixelLocalLoadANGLE with imageLoad.
TIntermSequence imageLoadArgs = {new TIntermSymbol(pls.image2DForLoading),
new TIntermSymbol(mGlobalPixelCoord)};
queueReplacement(CreateBuiltInFunctionCallNode("imageLoad", &imageLoadArgs,
*mSymbolTable, mShaderVersion),
OriginalNode::IS_DROPPED);
return false; // No need to recurse since this node is being dropped.
}
// Rewrite pixelLocalStoreANGLE -> imageStore.
if (aggregate->getOp() == EOpPixelLocalStoreANGLE)
{
// Surround the store with memoryBarrierImage calls in order to ensure dependent stores
// and loads in a single shader invocation are coherent. From the ES 3.1 spec:
//
// Using variables declared as "coherent" guarantees only that the results of stores
// will be immediately visible to shader invocations using similarly-declared
// variables; calling MemoryBarrier is required to ensure that the stores are visible
// to other operations.
//
// Also hoist the 'value' expression into a temp. In the event of
// "pixelLocalStoreANGLE(..., pixelLocalLoadANGLE(...))", this ensures the load occurs
// _before_ the memoryBarrierImage.
//
// NOTE: It is generally unsafe to hoist function arguments due to short circuiting,
// e.g., "if (false && function(...))", but pixelLocalStoreANGLE returns type void, so
// it is safe in this particular case.
TType *valueType = new TType(DataTypeOfPLSType(plsSymbol->getBasicType()),
plsSymbol->getPrecision(), EvqTemporary, 4);
TVariable *valueVar = CreateTempVariable(mSymbolTable, valueType);
TIntermDeclaration *valueDecl =
CreateTempInitDeclarationNode(valueVar, args[1]->getAsTyped());
valueDecl->traverse(this); // Rewrite any potential pixelLocalLoadANGLEs in valueDecl.
insertStatementsInParentBlock({valueDecl, createMemoryBarrierImageNode()}, // Before.
{createMemoryBarrierImageNode()}); // After.
// Rewrite the pixelLocalStoreANGLE with imageStore.
TIntermSequence imageStoreArgs = {new TIntermSymbol(pls.image2DForStoring),
new TIntermSymbol(mGlobalPixelCoord),
new TIntermSymbol(valueVar)};
queueReplacement(CreateBuiltInFunctionCallNode("imageStore", &imageStoreArgs,
*mSymbolTable, mShaderVersion),
OriginalNode::IS_DROPPED);
return false; // No need to recurse since this node is being dropped.
}
return true;
}
TVariable *globalPixelCoord() const { return mGlobalPixelCoord; }
private:
// Internal implementation of an opaque 'gpixelLocalANGLE' handle. Since ES 3.1 requires most
// image formats to be either readonly or writeonly, we have to make two separate images that
// alias the same binding.
struct PLSImages
{
TVariable *image2DForLoading = nullptr;
TVariable *image2DForStoring = nullptr;
};
// Adds a null 'PLSImages' entry to the map for the given symbol. An entry must not already
// exist in the map for this symbol.
PLSImages &insertNullPLSImages(TIntermSymbol *plsSymbol)
{
ASSERT(plsSymbol);
ASSERT(IsPixelLocal(plsSymbol->getBasicType()));
int binding = plsSymbol->getType().getLayoutQualifier().binding;
ASSERT(binding >= 0);
auto result = mPLSImages.insert({binding, PLSImages()});
ASSERT(result.second); // Ensure PLSImages didn't already exist for this symbol.
return result.first->second;
}
// Looks up the PLSImages for the given symbol. An entry must already exist in the map for this
// symbol.
PLSImages &findPLSImages(TIntermSymbol *plsSymbol)
{
ASSERT(plsSymbol);
ASSERT(IsPixelLocal(plsSymbol->getBasicType()));
int binding = plsSymbol->getType().getLayoutQualifier().binding;
ASSERT(binding >= 0);
auto iter = mPLSImages.find(binding);
ASSERT(iter != mPLSImages.end()); // Ensure PLSImages already exist for this symbol.
return iter->second;
}
enum class ImageAccess
{
ReadOnly,
WriteOnly,
ReadWrite
};
// Creates a 'gimage2D' that implements a pixel local storage handle.
TVariable *createPLSImage(const TIntermSymbol *plsSymbol, ImageAccess access)
{
ASSERT(plsSymbol);
ASSERT(IsPixelLocal(plsSymbol->getBasicType()));
TMemoryQualifier memoryQualifier;
memoryQualifier.coherent = true;
memoryQualifier.restrictQualifier = access == ImageAccess::ReadWrite;
memoryQualifier.volatileQualifier = access != ImageAccess::ReadWrite;
memoryQualifier.readonly = access == ImageAccess::ReadOnly;
memoryQualifier.writeonly = access == ImageAccess::WriteOnly;
TType *imageType = new TType(plsSymbol->getType());
imageType->setBasicType(Image2DTypeOfPLSType(plsSymbol->getBasicType()));
imageType->setMemoryQualifier(memoryQualifier);
std::string name = "_pls";
name.append(plsSymbol->getName().data());
if (access == ImageAccess::ReadOnly)
{
name.append("_R");
}
else if (access == ImageAccess::WriteOnly)
{
name.append("_W");
}
return new TVariable(mSymbolTable, ImmutableString(name), imageType, SymbolType::BuiltIn);
}
// Creates a function call to memoryBarrier().
TIntermNode *createMemoryBarrierImageNode()
{
TIntermSequence emptyArgs;
return CreateBuiltInFunctionCallNode("memoryBarrierImage", &emptyArgs, *mSymbolTable,
mShaderVersion);
}
const TCompiler *const mCompiler;
const int mShaderVersion;
// Stores the shader invocation's pixel coordinate as "ivec2(floor(gl_FragCoord.xy))".
TVariable *mGlobalPixelCoord = nullptr;
// Maps PLS variables to their gimage2D aliases.
angle::HashMap<int, PLSImages> mPLSImages;
};
} // anonymous namespace
bool RewritePixelLocalStorageToImages(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable &symbolTable,
ShCompileOptions compileOptions,
int shaderVersion)
{
// If any functions take PLS arguments, monomorphize the functions by removing said parameters
// and making the PLS calls from main() instead, using the global uniform from the call site
// instead of the function argument. This is necessary because function arguments don't carry
// the necessary "binding" or "format" layout qualifiers.
if (!MonomorphizeUnsupportedFunctions(
compiler, root, &symbolTable, compileOptions,
UnsupportedFunctionArgsBitSet{UnsupportedFunctionArgs::PixelLocalStorage}))
{
return false;
}
RewriteToImagesTraverser traverser(compiler, symbolTable, shaderVersion);
root->traverse(&traverser);
if (!traverser.updateTree(compiler, root))
{
return false;
}
if (traverser.globalPixelCoord())
{
// Initialize the global pixel coord at the beginning of main():
//
// pixelCoord = ivec2(floor(gl_FragCoord.xy));
//
TIntermTyped *expr;
expr =
ReferenceBuiltInVariable(ImmutableString("gl_FragCoord"), symbolTable, shaderVersion);
expr = CreateSwizzle(expr, 0, 1);
expr = CreateBuiltInUnaryFunctionCallNode("floor", expr, symbolTable, shaderVersion);
TIntermSequence typeConversionArgs = {expr};
expr = TIntermAggregate::CreateConstructor(TType(EbtInt, 2), &typeConversionArgs);
if (!RunAtTheBeginningOfShader(
compiler, root, CreateTempAssignmentNode(traverser.globalPixelCoord(), expr)))
{
return false;
}
}
return true;
}
} // namespace sh