Hash :
770242db
Author :
Date :
2024-11-20T17:03:07
Translator: Remove the `gimage1D` base type This is a desktop GL type, whose removal was left out of b16d105fc6. Bug: angleproject:370937467 Change-Id: I0bda5453b95ddf924ba0583de346902b333603a6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6037776 Auto-Submit: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
//
// 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.
//
// RewriteR32fImages: Change images qualified with r32f to use r32ui instead.
//
#include "compiler/translator/tree_ops/spirv/RewriteR32fImages.h"
#include "compiler/translator/Compiler.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
{
bool IsR32fImage(const TType &type)
{
return type.getQualifier() == EvqUniform && type.isImage() &&
type.getLayoutQualifier().imageInternalFormat == EiifR32F;
}
using ImageMap = angle::HashMap<const TVariable *, const TVariable *>;
TIntermTyped *RewriteBuiltinFunctionCall(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermAggregate *node,
const ImageMap &imageMap);
// Given an expression, this traverser calculates a new expression where builtin function calls to
// r32f images are replaced with ones to the mapped r32ui image. In particular, this is run on the
// right node of EOpIndexIndirect binary nodes, so that the expression in the index gets a chance to
// go through this transformation.
class RewriteExpressionTraverser final : public TIntermTraverser
{
public:
explicit RewriteExpressionTraverser(TCompiler *compiler,
TSymbolTable *symbolTable,
const ImageMap &imageMap)
: TIntermTraverser(true, false, false, symbolTable),
mCompiler(compiler),
mImageMap(imageMap)
{}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
TIntermTyped *rewritten =
RewriteBuiltinFunctionCall(mCompiler, mSymbolTable, node, mImageMap);
if (rewritten == nullptr)
{
return true;
}
queueReplacement(rewritten, OriginalNode::IS_DROPPED);
// Don't iterate as the expression is rewritten.
return false;
}
private:
TCompiler *mCompiler;
const ImageMap &mImageMap;
};
// Rewrite the index of an EOpIndexIndirect expression as well as any arguments to the builtin
// function call.
TIntermTyped *RewriteExpression(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermTyped *expression,
const ImageMap &imageMap)
{
// Create a fake block to insert the node in. The root itself may need changing.
TIntermBlock block;
block.appendStatement(expression);
RewriteExpressionTraverser traverser(compiler, symbolTable, imageMap);
block.traverse(&traverser);
bool valid = traverser.updateTree(compiler, &block);
ASSERT(valid);
TIntermTyped *rewritten = block.getChildNode(0)->getAsTyped();
return rewritten;
}
// Given a builtin function call such as the following:
//
// imageLoad(expression, ...);
//
// expression is in the form of:
//
// - image uniform
// - image uniform array indexed with EOpIndexDirect or EOpIndexIndirect. Note that
// RewriteArrayOfArrayOfOpaqueUniforms has already ensured that the image array is
// single-dimension.
//
// The latter case (with EOpIndexIndirect) is not valid GLSL (up to GL_EXT_gpu_shader5), but if it
// were, the index itself could have contained an image builtin function call, so is recursively
// processed (in case supported in future). Additionally, the other builtin function arguments may
// need processing too.
//
// This function creates a similar expression where the image uniforms (of type r32f) are replaced
// with those of r32ui type.
//
TIntermTyped *RewriteBuiltinFunctionCall(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermAggregate *node,
const ImageMap &imageMap)
{
if (!BuiltInGroup::IsBuiltIn(node->getOp()))
{
// AST functions don't require modification as r32f image function parameters are removed by
// MonomorphizeUnsupportedFunctions.
return nullptr;
}
// If it's an |image*| function, replace the function with an equivalent that uses an r32ui
// image.
if (!node->getFunction()->isImageFunction())
{
return nullptr;
}
TIntermSequence *arguments = node->getSequence();
TIntermTyped *imageExpression = (*arguments)[0]->getAsTyped();
ASSERT(imageExpression);
// Find the image uniform that's being indexed, if indexed.
TIntermBinary *asBinary = imageExpression->getAsBinaryNode();
TIntermSymbol *imageUniform = imageExpression->getAsSymbolNode();
if (asBinary)
{
ASSERT(asBinary->getOp() == EOpIndexDirect || asBinary->getOp() == EOpIndexIndirect);
imageUniform = asBinary->getLeft()->getAsSymbolNode();
}
ASSERT(imageUniform);
if (!IsR32fImage(imageUniform->getType()))
{
return nullptr;
}
ASSERT(imageMap.find(&imageUniform->variable()) != imageMap.end());
const TVariable *replacementImage = imageMap.at(&imageUniform->variable());
// Build the expression again, with the image uniform replaced. If index is dynamic,
// recursively process it.
TIntermTyped *replacementExpression = new TIntermSymbol(replacementImage);
// Index it, if indexed.
if (asBinary != nullptr)
{
TIntermTyped *index = asBinary->getRight();
switch (asBinary->getOp())
{
case EOpIndexDirect:
break;
case EOpIndexIndirect:
{
// Run RewriteExpressionTraverser on the index node. This case is currently
// impossible with known extensions.
UNREACHABLE();
index = RewriteExpression(compiler, symbolTable, index, imageMap);
break;
}
default:
UNREACHABLE();
break;
}
replacementExpression = new TIntermBinary(asBinary->getOp(), replacementExpression, index);
}
TIntermSequence substituteArguments;
substituteArguments.push_back(replacementExpression);
for (size_t argIndex = 1; argIndex < arguments->size(); ++argIndex)
{
TIntermTyped *arg = (*arguments)[argIndex]->getAsTyped();
// Run RewriteExpressionTraverser on the argument. It may itself be an expression with an
// r32f image that needs to be rewritten.
arg = RewriteExpression(compiler, symbolTable, arg, imageMap);
substituteArguments.push_back(arg);
}
const ImmutableString &functionName = node->getFunction()->name();
bool isImageAtomicExchange = functionName == "imageAtomicExchange";
bool isImageLoad = false;
if (functionName == "imageStore" || isImageAtomicExchange)
{
// The last parameter is float data, which should be changed to floatBitsToUint(data).
TIntermTyped *data = substituteArguments.back()->getAsTyped();
substituteArguments.back() =
CreateBuiltInUnaryFunctionCallNode("floatBitsToUint", data, *symbolTable, 300);
}
else if (functionName == "imageLoad")
{
isImageLoad = true;
}
else
{
// imageSize does not have any other arguments.
ASSERT(functionName == "imageSize");
ASSERT(arguments->size() == 1);
}
TIntermTyped *replacementCall =
CreateBuiltInFunctionCallNode(functionName.data(), &substituteArguments, *symbolTable, 310);
// If imageLoad or imageAtomicExchange, the result is now uint, which should be converted with
// uintBitsToFloat. With imageLoad, the alpha channel should always read 1.0 regardless.
if (isImageLoad || isImageAtomicExchange)
{
if (isImageLoad)
{
// imageLoad().rgb
replacementCall = new TIntermSwizzle(replacementCall, {0, 1, 2});
}
// uintBitsToFloat(imageLoad().rgb), or uintBitsToFloat(imageAtomicExchange())
replacementCall = CreateBuiltInUnaryFunctionCallNode("uintBitsToFloat", replacementCall,
*symbolTable, 300);
if (isImageLoad)
{
// vec4(uintBitsToFloat(imageLoad().rgb), 1.0)
const TType &vec4Type = *StaticType::GetBasic<EbtFloat, EbpHigh, 4>();
TIntermSequence constructorArgs = {replacementCall, CreateFloatNode(1.0f, EbpMedium)};
replacementCall = TIntermAggregate::CreateConstructor(vec4Type, &constructorArgs);
}
}
return replacementCall;
}
// Traverser that:
//
// 1. Converts the layout(r32f, ...) ... image* name; declarations to use the r32ui format
// 2. Converts |imageLoad| and |imageStore| functions to use |uintBitsToFloat| and |floatBitsToUint|
// respectively.
// 3. Converts |imageAtomicExchange| to use |floatBitsToUint| and |uintBitsToFloat|.
class RewriteR32fImagesTraverser : public TIntermTraverser
{
public:
RewriteR32fImagesTraverser(TCompiler *compiler, TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable), mCompiler(compiler)
{}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
if (visit != PreVisit)
{
return true;
}
const TIntermSequence &sequence = *(node->getSequence());
TIntermTyped *declVariable = sequence.front()->getAsTyped();
const TType &type = declVariable->getType();
if (!IsR32fImage(type))
{
return true;
}
TIntermSymbol *oldSymbol = declVariable->getAsSymbolNode();
ASSERT(oldSymbol != nullptr);
const TVariable &oldVariable = oldSymbol->variable();
TType *newType = new TType(type);
TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
layoutQualifier.imageInternalFormat = EiifR32UI;
newType->setLayoutQualifier(layoutQualifier);
switch (type.getBasicType())
{
case EbtImage2D:
newType->setBasicType(EbtUImage2D);
break;
case EbtImage3D:
newType->setBasicType(EbtUImage3D);
break;
case EbtImage2DArray:
newType->setBasicType(EbtUImage2DArray);
break;
case EbtImageCube:
newType->setBasicType(EbtUImageCube);
break;
case EbtImage2DMS:
newType->setBasicType(EbtUImage2DMS);
break;
case EbtImage2DMSArray:
newType->setBasicType(EbtUImage2DMSArray);
break;
case EbtImageCubeArray:
newType->setBasicType(EbtUImageCubeArray);
break;
case EbtImageRect:
newType->setBasicType(EbtUImageRect);
break;
case EbtImageBuffer:
newType->setBasicType(EbtUImageBuffer);
break;
default:
UNREACHABLE();
}
TVariable *newVariable =
new TVariable(oldVariable.uniqueId(), oldVariable.name(), oldVariable.symbolType(),
oldVariable.extensions(), newType);
mImageMap[&oldVariable] = newVariable;
TIntermDeclaration *newDecl = new TIntermDeclaration();
newDecl->appendDeclarator(new TIntermSymbol(newVariable));
queueReplacement(newDecl, OriginalNode::IS_DROPPED);
return false;
}
// Same implementation as in RewriteExpressionTraverser. That traverser cannot replace root.
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
TIntermTyped *rewritten =
RewriteBuiltinFunctionCall(mCompiler, mSymbolTable, node, mImageMap);
if (rewritten == nullptr)
{
return true;
}
queueReplacement(rewritten, OriginalNode::IS_DROPPED);
return false;
}
void visitSymbol(TIntermSymbol *symbol) override
{
// Cannot encounter the image symbol directly. It can only be used with built-in functions,
// and therefore it's handled by visitAggregate.
ASSERT(!IsR32fImage(symbol->getType()));
}
private:
TCompiler *mCompiler;
// Map from r32f image to r32ui image
ImageMap mImageMap;
};
} // anonymous namespace
bool RewriteR32fImages(TCompiler *compiler, TIntermBlock *root, TSymbolTable *symbolTable)
{
RewriteR32fImagesTraverser traverser(compiler, symbolTable);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
} // namespace sh