Hash :
53ec86ab
Author :
Date :
2024-12-17T14:40:31
WGSL: support small stride arrays in uniforms WGSL requires arrays in the uniform address space to have a stride a multiple of 16. This CL makes WGSL translator emit wrapper structs for array element types used in the uniform address space, when the array stride is not a multiple of 16. The exception is for structs that aren't an aligned size of 16n, and for any types matCx2, since they are (or will be) handled in different ways that ensure alignment to 16. This should leave only f32, i32, u32, and vec2. See https://www.w3.org/TR/WGSL/#example-67da5de6 for an example of using a wrapper struct. This requires converting arrays with a wrapper struct element type to arrays with an unwrapped element type when they are first used; this can be "optimized" later for the common case of accessing a single array element, which can then be unwrapped immediately. This CL generates WGSL conversion functions when necessary. After this, the only types that can't yet be used in a uniform are matCx2 and bools. This is #2 in https://docs.google.com/document/d/17Qku1QEbLDhvJS-JJ9lPQAbnuZtLxWhG-ha5eCUhtEY/edit?tab=t.0#bookmark=id.rt3slgehd4te Bug: angleproject:376553328 Change-Id: I1edfa7f481a6cbf5b595643aae8728e67bc4b770 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6092038 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Liza Burakova <liza@chromium.org> Reviewed-by: Matt Denton <mpdenton@google.com> Commit-Queue: Matt Denton <mpdenton@google.com>
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
//
// Copyright 2024 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/Utils.h"
#include "compiler/translator/Common.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/Types.h"
#include "compiler/translator/util.h"
namespace sh
{
template <typename StringStreamType>
void WriteWgslBareTypeName(StringStreamType &output,
const TType &type,
const EmitTypeConfig &config)
{
const TBasicType basicType = type.getBasicType();
switch (basicType)
{
case TBasicType::EbtVoid:
case TBasicType::EbtBool:
output << type.getBasicString();
break;
// TODO(anglebug.com/42267100): is there double precision (f64) in GLSL? It doesn't really
// exist in WGSL (i.e. f64 does not exist but AbstractFloat can handle 64 bits???) Metal
// does not have 64 bit double precision types. It's being implemented in WGPU:
// https://github.com/gpuweb/gpuweb/issues/2805
case TBasicType::EbtFloat:
output << "f32";
break;
case TBasicType::EbtInt:
output << "i32";
break;
case TBasicType::EbtUInt:
output << "u32";
break;
case TBasicType::EbtStruct:
WriteNameOf(output, *type.getStruct());
break;
case TBasicType::EbtInterfaceBlock:
WriteNameOf(output, *type.getInterfaceBlock());
break;
default:
if (IsSampler(basicType))
{
// TODO(anglebug.com/42267100): possibly emit both a sampler and a texture2d. WGSL
// has sampler variables for the sampler configuration, whereas GLSL has sampler2d
// and other sampler* variables for an actual texture.
output << "texture2d<";
switch (type.getBasicType())
{
case EbtSampler2D:
output << "f32";
break;
case EbtISampler2D:
output << "i32";
break;
case EbtUSampler2D:
output << "u32";
break;
default:
// TODO(anglebug.com/42267100): are any of the other sampler types necessary
// to translate?
UNIMPLEMENTED();
break;
}
if (type.getMemoryQualifier().readonly || type.getMemoryQualifier().writeonly)
{
// TODO(anglebug.com/42267100): implement memory qualifiers.
UNIMPLEMENTED();
}
output << ">";
}
else if (IsImage(basicType))
{
// TODO(anglebug.com/42267100): does texture2d also correspond to GLSL's image type?
output << "texture2d<";
switch (type.getBasicType())
{
case EbtImage2D:
output << "f32";
break;
case EbtIImage2D:
output << "i32";
break;
case EbtUImage2D:
output << "u32";
break;
default:
// TODO(anglebug.com/42267100): are any of the other image types necessary
// to translate?
UNIMPLEMENTED();
break;
}
if (type.getMemoryQualifier().readonly || type.getMemoryQualifier().writeonly)
{
// TODO(anglebug.com/42267100): implement memory qualifiers.
UNREACHABLE();
}
output << ">";
}
else
{
UNREACHABLE();
}
break;
}
}
template <typename StringStreamType>
void WriteNameOf(StringStreamType &output, SymbolType symbolType, const ImmutableString &name)
{
switch (symbolType)
{
case SymbolType::BuiltIn:
output << name;
break;
case SymbolType::UserDefined:
output << kUserDefinedNamePrefix << name;
break;
case SymbolType::AngleInternal:
output << name;
break;
case SymbolType::Empty:
// TODO(anglebug.com/42267100): support this if necessary
UNREACHABLE();
}
}
template <typename StringStreamType>
void WriteWgslType(StringStreamType &output, const TType &type, const EmitTypeConfig &config)
{
if (type.isArray())
{
// Examples:
// array<f32, 5>
// array<array<u32, 5>, 10>
output << "array<";
TType innerType = type;
innerType.toArrayElementType();
if (ElementTypeNeedsUniformWrapperStruct(config.addressSpace == WgslAddressSpace::Uniform,
&type))
{
// Multidimensional arrays not currently supported in uniforms in the WebGPU backend
ASSERT(!innerType.isArray());
// Due to uniform address space layout constraints, certain array element types must
// be wrapped in a wrapper struct.
// Example: array<ANGLE_wrapped_f32, 5>
output << MakeUniformWrapperStructName(&innerType);
}
else
{
WriteWgslType(output, innerType, config);
}
output << ", " << type.getOutermostArraySize() << ">";
}
else if (type.isVector())
{
output << "vec" << static_cast<uint32_t>(type.getNominalSize()) << "<";
WriteWgslBareTypeName(output, type, config);
output << ">";
}
else if (type.isMatrix())
{
output << "mat" << static_cast<uint32_t>(type.getCols()) << "x"
<< static_cast<uint32_t>(type.getRows()) << "<";
WriteWgslBareTypeName(output, type, config);
output << ">";
}
else
{
// This type has no dimensions and is equivalent to its bare type.
WriteWgslBareTypeName(output, type, config);
}
}
template void WriteWgslBareTypeName<TInfoSinkBase>(TInfoSinkBase &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteNameOf<TInfoSinkBase>(TInfoSinkBase &output,
SymbolType symbolType,
const ImmutableString &name);
template void WriteWgslType<TInfoSinkBase>(TInfoSinkBase &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteWgslBareTypeName<TStringStream>(TStringStream &output,
const TType &type,
const EmitTypeConfig &config);
template void WriteNameOf<TStringStream>(TStringStream &output,
SymbolType symbolType,
const ImmutableString &name);
template void WriteWgslType<TStringStream>(TStringStream &output,
const TType &type,
const EmitTypeConfig &config);
ImmutableString MakeUniformWrapperStructName(const TType *type)
{
return BuildConcatenatedImmutableString("ANGLE_wrapped_", type->getBuiltInTypeNameString());
}
bool ElementTypeNeedsUniformWrapperStruct(bool inUniformAddressSpace, const TType *type)
{
// Only types that are used as array element types in the uniform address space need wrapper
// structs. If the array element type is a struct it does not need to be wrapped in another
// layer of struct.
if (!inUniformAddressSpace || !type->isArray() || type->getStruct())
{
return false;
}
TType elementType = *type;
elementType.toArrayElementType();
// If the array element type's stride is already a multiple of 16, it does not need a wrapper
// struct.
//
// The remaining possible element types are scalars, vectors, matrices, and other arrays.
// - Scalars need to be aligned to 16.
// - vec3 and vec4 are already aligned to 16, but vec2 needs to be aligned.
// - Matrices are aligned to 16 automatically, except matCx2 which already needs to be handled
// by specialized code anyway.
// TODO(anglebug.com/376553328): re-enable this ASSERT once matCx2 are handled.
// ASSERT(!type->isMatrix() || type->getRows() != 2);
// - WebGL2 doesn't support nested arrays so this won't either, though support wouldn't be hard.
ASSERT(!elementType.isArray());
return elementType.isScalar() || (elementType.isVector() && elementType.getNominalSize() == 2);
}
GlobalVars FindGlobalVars(TIntermBlock *root)
{
GlobalVars globals;
for (TIntermNode *node : *root->getSequence())
{
if (TIntermDeclaration *declNode = node->getAsDeclarationNode())
{
Declaration decl = ViewDeclaration(*declNode);
globals.insert({decl.symbol.variable().name(), declNode});
}
}
return globals;
}
} // namespace sh