Hash :
cfe2c8fe
Author :
Date :
2025-06-25T13:25:01
WGSL: RewriteMultielementSwizzle WGSL doesn't support assignments to multi-element swizzles. This is used in a lot of shader tests, so temporarily work around this with an AST traverser that splits these assignments into multiple assignments that only assign to single element swizzles. One special case is multiplication-by-a-matrix assignment: vec.xy *= mat; is converted to vec.x = (vec.xy * mat).x; vec.y = (vec.xy * mat).y; Bug: angleproject:392542001 Change-Id: I3f393039aae13eb3f2c5dc5e553f68eb03b6316d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6847280 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Matthew Denton <mpdenton@chromium.org> Reviewed-by: Liza Burakova <liza@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
//
// Copyright 2025 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.
//
// RewriteMultielemntSwizzleAssignment: WGSL does not support assignment to multielement swizzles.
// This splits them into mutliple assignments to single-element swizzles.
//
#include "compiler/translator/tree_ops/wgsl/RewriteMultielementSwizzleAssignment.h"
#include "common/log_utils.h"
#include "compiler/translator/BaseTypes.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/Operator_autogen.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/DriverUniform.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
bool IsMultielementSwizzleAssignment(TOperator op, TIntermTyped *assignedNode)
{
if (!IsAssignment(op))
{
return false;
}
TIntermSwizzle *leftSwizzleNode = assignedNode->getAsSwizzleNode();
if (!leftSwizzleNode || leftSwizzleNode->getSwizzleOffsets().size() <= 1)
{
return false;
}
return true;
}
// Splits multielement swizzles into single-element swizzles.
class MultielementSwizzleAssignmentTraverser : public TIntermTraverser
{
public:
MultielementSwizzleAssignmentTraverser(TCompiler *compiler)
: TIntermTraverser(true, false, false), mCompiler(compiler)
{}
bool visitBinary(Visit visit, TIntermBinary *node) override;
bool visitUnary(Visit visit, TIntermUnary *node) override;
private:
TCompiler *mCompiler;
};
bool MultielementSwizzleAssignmentTraverser::visitUnary(Visit vist, TIntermUnary *unaryNode)
{
if (!IsMultielementSwizzleAssignment(unaryNode->getOp(), unaryNode->getOperand()))
{
return true;
}
// TODO(anglebug.com/42267100): increments and decrements should be handled by a pre-pass that
// converts them into additions of component-wise vectors filled with 1s.
// Then this can be converted into UNREACHABLE().
switch (unaryNode->getOp())
{
case EOpPostIncrement:
case EOpPostDecrement:
case EOpPreIncrement:
case EOpPreDecrement:
UNIMPLEMENTED();
break;
default:
break;
}
return true;
}
bool MultielementSwizzleAssignmentTraverser::visitBinary(Visit visit, TIntermBinary *parentBinNode)
{
ASSERT(visit == Visit::PreVisit);
if (!IsMultielementSwizzleAssignment(parentBinNode->getOp(), parentBinNode->getLeft()))
{
return true;
}
if (!getParentNode()->getAsBlock())
{
// TODO(anglebug.com/42267100): Cannot yet handle assignments to swizzles that are nested
// inside other expressions.
UNIMPLEMENTED();
return false;
}
TIntermSequence insertionsBefore;
TIntermSwizzle *leftSwizzleNode = parentBinNode->getLeft()->getAsSwizzleNode();
// TODO(anglebug.com/42267100): The deepCopied node below would need to be traversed, but
// this doesn't yet handle assignments to swizzles that are nested in other expressions anyway.
// Store the RHS in a temp in case of side effects, since it will be duplicated.
TIntermDeclaration *rhsTempDeclaration;
TVariable *rhsTempVariable =
DeclareTempVariable(&mCompiler->getSymbolTable(), parentBinNode->getRight()->deepCopy(),
EvqTemporary, &rhsTempDeclaration);
insertionsBefore.push_back(rhsTempDeclaration);
TIntermSequence singleElementSwizzleAssignments;
for (uint32_t i = 0; i < leftSwizzleNode->getSwizzleOffsets().size(); i++)
{
// TODO(anglebug.com/42267100): Doing a deepCopy of the swizzle operand can cause problems
// if it has side effects. E.g.
//
// x[i++].xy = vec(0.0, 0.0);
//
// will be transformed to something like
//
// x[i++].x = vec(0.0, 0.0).x;
// x[i++].y = vec(0.0, 0.0).y;
//
// Which obviously increments `i` an extra time.
if (leftSwizzleNode->getOperand()->hasSideEffects())
{
UNIMPLEMENTED();
}
TIntermTyped *swizzleOperandCopy = leftSwizzleNode->getOperand()->deepCopy();
TIntermSwizzle *leftSideNewSwizzle =
new TIntermSwizzle(swizzleOperandCopy, {leftSwizzleNode->getSwizzleOffsets()[i]});
// The resulting swizzle doesn't need folding here because it would've already been folded,
// this just turned it from a multielement into a single element swizzle.
// Most binary assignments are component-wise and so we just swizzle the right side to
// select one component for this particular single-element swizzle assignment. The only
// special case is multiplication by a matrix, which is not component-wise obviously.
TIntermTyped *newRightSide = CreateTempSymbolNode(rhsTempVariable);
TOperator newOp = parentBinNode->getOp();
if (newRightSide->getType().isMatrix() || newRightSide->getType().isVector())
{
if (newRightSide->getType().isMatrix())
{
ASSERT(parentBinNode->getOp() == EOpVectorTimesMatrixAssign);
// The `vec.xy *= mat` is being converted to something like `vec.x = (vec.xy *
// mat).x; vec.y = (vec.xy * mat).y;`
newOp = EOpAssign;
// TODO(anglebug.com/42267100): this matrix multiplication could be kept in a temp
// var.
// TODO(anglebug.com/42267100): as above this does not work if the deepCopied AST
// subtrees have side effects.
newRightSide = new TIntermBinary(EOpVectorTimesMatrix, leftSwizzleNode->deepCopy(),
newRightSide);
}
ASSERT(newRightSide->getType().isVector());
// Now swizzle to select the i-th element.
newRightSide = new TIntermSwizzle(newRightSide, {i});
// No need to fold newRightSide because we are swizzling either a temporary or the
// binary matrix multiplication, so it won't be a swizzle.
}
else if (newRightSide->getType().isScalar())
{
// Needs no special handling, and can just be unmodified as the right hand side.
}
else
{
UNREACHABLE();
}
// At this point the right hand side should match the type of the left hand side.
ASSERT(newRightSide->isScalar());
ASSERT(newRightSide->getBasicType() == leftSideNewSwizzle->getBasicType());
singleElementSwizzleAssignments.emplace_back(
new TIntermBinary(newOp, leftSideNewSwizzle, newRightSide));
}
mMultiReplacements.emplace_back(getParentNode()->getAsBlock(), parentBinNode,
std::move(singleElementSwizzleAssignments));
insertStatementsInParentBlock(insertionsBefore);
// Already traversed the left and right sides above.
return false;
}
} // anonymous namespace
bool RewriteMultielementSwizzleAssignment(TCompiler *compiler, TIntermBlock *root)
{
MultielementSwizzleAssignmentTraverser traverser(compiler);
root->traverse(&traverser);
return traverser.updateTree(compiler, root);
}
} // namespace sh