Hash :
fecb8ead
Author :
Date :
2025-09-04T12:27:48
WGSL: implement inc/dec with generated functions Implements inc/dec with generated WGSL functions that take pointers and perform a post/pre inc/decrement. This works with scalars, vectors, and matrices, both float and int. WGSL supports inc/dec only on integer types, and only as statements (not as expressions). https://www.w3.org/TR/WGSL/#increment-decrement. The regular ++ and -- are used in this specific case. The WGSL outputter records usage of increment/decrement and produces a call to the correct function. A new class is introduced to keep the record of which types need generated inc/dec WGSL functions. Bug: angleproject:42267100 Change-Id: I0e70760ba5bd00f978e216f958216ae3137a146e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6935269 Commit-Queue: Matthew Denton <mpdenton@chromium.org> Reviewed-by: Geoff Lang <geofflang@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
//
// 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.
//
#include "compiler/translator/wgsl/WGSLProgramPrelude.h"
#include "common/log_utils.h"
#include "compiler/translator/BaseTypes.h"
#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/util.h"
#include "compiler/translator/wgsl/Utils.h"
namespace sh
{
namespace
{
constexpr ImmutableString kEndParanthesis = ImmutableString(")");
void EmitConstructorList(TInfoSinkBase &sink, const TType &type, ImmutableString scalar)
{
ASSERT(!type.isArray());
ASSERT(!type.getStruct());
sink << "(";
size_t numScalars = 1;
if (type.isMatrix())
{
numScalars = type.getCols() * type.getRows();
}
for (size_t i = 0; i < numScalars; i++)
{
if (i != 0)
{
sink << ", ";
}
sink << scalar;
}
sink << ")";
}
} // namespace
WGSLWrapperFunction WGSLProgramPrelude::preIncrement(const TType &incrementedType)
{
mPreIncrementedTypes.insert(incrementedType);
switch (incrementedType.getQualifier())
{
case EvqTemporary:
// NOTE: As of Sept 2025, parameters are immutable in WGSL (and are handled by an AST pass
// that copies parameters to temporaries). Include these here in case parameters become
// mutable in the future.
case EvqParamIn:
case EvqParamOut:
case EvqParamInOut:
return {ImmutableString("preIncFunc(&"), kEndParanthesis};
default:
// EvqGlobal and various other shader outputs/builtins are all globals.
return {ImmutableString("preIncPriv(&"), kEndParanthesis};
}
}
WGSLWrapperFunction WGSLProgramPrelude::preDecrement(const TType &decrementedType)
{
mPreDecrementedTypes.insert(decrementedType);
switch (decrementedType.getQualifier())
{
case EvqTemporary:
// NOTE: As of Sept 2025, parameters are immutable in WGSL (and are handled by an AST pass
// that copies parameters to temporaries). Include these here in case parameters become
// mutable in the future.
case EvqParamIn:
case EvqParamOut:
case EvqParamInOut:
return {ImmutableString("preDecFunc(&"), kEndParanthesis};
default:
// EvqGlobal and various other shader outputs/builtins are all globals.
return {ImmutableString("preDecPriv(&"), kEndParanthesis};
}
}
WGSLWrapperFunction WGSLProgramPrelude::postIncrement(const TType &incrementedType)
{
mPostIncrementedTypes.insert(incrementedType);
switch (incrementedType.getQualifier())
{
case EvqTemporary:
// NOTE: As of Sept 2025, parameters are immutable in WGSL (and are handled by an AST pass
// that copies parameters to temporaries). Include these here in case parameters become
// mutable in the future.
case EvqParamIn:
case EvqParamOut:
case EvqParamInOut:
return {ImmutableString("postIncFunc(&"), kEndParanthesis};
default:
// EvqGlobal and various other shader outputs/builtins are all globals.
return {ImmutableString("postIncPriv(&"), kEndParanthesis};
}
}
WGSLWrapperFunction WGSLProgramPrelude::postDecrement(const TType &decrementedType)
{
mPostDecrementedTypes.insert(decrementedType);
switch (decrementedType.getQualifier())
{
case EvqTemporary:
// NOTE: As of Sept 2025, parameters are immutable in WGSL (and are handled by an AST pass
// that copies parameters to temporaries). Include these here in case parameters become
// mutable in the future.
case EvqParamIn:
case EvqParamOut:
case EvqParamInOut:
return {ImmutableString("postDecFunc(&"), kEndParanthesis};
default:
// EvqGlobal and various other shader outputs/builtins are all globals.
return {ImmutableString("postDecPriv(&"), kEndParanthesis};
}
}
void WGSLProgramPrelude::outputPrelude(TInfoSinkBase &sink)
{
auto genPreIncOrDec = [&](ImmutableString addressSpace, const TType &type, ImmutableString op,
ImmutableString funcName) {
TStringStream typeStr;
WriteWgslType(typeStr, type, {});
sink << "fn " << funcName << "(x : ptr<" << addressSpace << ", " << typeStr.str()
<< ">) -> " << typeStr.str() << " {\n";
sink << " (*x) " << op << " " << typeStr.str();
EmitConstructorList(sink, type, ImmutableString("1"));
sink << ";\n";
sink << " return *x;\n";
sink << "}\n";
};
for (const TType &type : mPreIncrementedTypes)
{
// NOTE: it's easiest just to generate increments and decrements functions for variables
// that live in either the function-local scope or the module-local scope. TType holds a
// qualifier, but its operator== and operator< ignore the qualifier. We could keep track of
// the qualifiers used but that's overkill.
genPreIncOrDec(ImmutableString("private"), type, ImmutableString("+="),
ImmutableString("preIncPriv"));
genPreIncOrDec(ImmutableString("function"), type, ImmutableString("+="),
ImmutableString("preIncFunc"));
}
for (const TType &type : mPreDecrementedTypes)
{
// NOTE: it's easiest just to generate increments and decrements functions for variables
// that live in either the function-local scope or the module-local scope. TType holds a
// qualifier, but its operator== and operator< ignore the qualifier. We could keep track of
// the qualifiers used but that's overkill.
genPreIncOrDec(ImmutableString("private"), type, ImmutableString("-="),
ImmutableString("preDecPriv"));
genPreIncOrDec(ImmutableString("function"), type, ImmutableString("-="),
ImmutableString("preDecFunc"));
}
auto genPostIncOrDec = [&](ImmutableString addressSpace, const TType &type, ImmutableString op,
ImmutableString funcName) {
TStringStream typeStr;
WriteWgslType(typeStr, type, {});
sink << "fn " << funcName << "(x : ptr<" << addressSpace << ", " << typeStr.str()
<< ">) -> " << typeStr.str() << " {\n";
sink << " var old = *x;\n";
sink << " (*x) " << op << " " << typeStr.str();
EmitConstructorList(sink, type, ImmutableString("1"));
sink << ";\n";
sink << " return old;\n";
sink << "}\n";
};
for (const TType &type : mPostIncrementedTypes)
{
// NOTE: it's easiest just to generate increments and decrements functions for variables
// that live in either the function-local scope or the module-local scope. TType holds a
// qualifier, but its operator== and operator< ignore the qualifier. We could keep track of
// the qualifiers used but that's overkill.
genPostIncOrDec(ImmutableString("private"), type, ImmutableString("+="),
ImmutableString("postIncPriv"));
genPostIncOrDec(ImmutableString("function"), type, ImmutableString("+="),
ImmutableString("postIncFunc"));
}
for (const TType &type : mPostDecrementedTypes)
{
// NOTE: it's easiest just to generate increments and decrements functions for variables
// that live in either the function-local scope or the module-local scope. TType holds a
// qualifier, but its operator== and operator< ignore the qualifier. We could keep track of
// the qualifiers used but that's overkill.
genPostIncOrDec(ImmutableString("private"), type, ImmutableString("-="),
ImmutableString("postDecPriv"));
genPostIncOrDec(ImmutableString("function"), type, ImmutableString("-="),
ImmutableString("postDecFunc"));
}
}
} // namespace sh