Hash :
8ee72cc7
Author :
Date :
2025-01-13T13:44:31
WGSL: support matCx2 in uniforms matCx2 in WGSL does not match std140 layout and so it needs to be translated as array<ANGLE_wrapped_vec2, C> when in a uniform. On use it needs to be converted to a WGSL-native matCx2. This also includes the optimization of an array<matCx2> in a uniform--when indexing into it, only the indexed element will be converted to a native matCx2, instead of converting the entire array and then indexing. Bug: angleproject:376553328 Change-Id: I1d84471234b3d3cf4cf361ae89cb61675d5bf9a8 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6157788 Reviewed-by: Liza Burakova <liza@chromium.org> Commit-Queue: Matthew Denton <mpdenton@chromium.org> Reviewed-by: 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
//
// 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
{
namespace
{
const char kWrappedPrefix[] = "ANGLE_wrapped_";
}
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())
{
if (config.addressSpace == WgslAddressSpace::Uniform && type.getRows() == 2)
{
// matCx2 in the uniform address space is too packed for std140, and so they will be
// represented by an array<ANGLE_wrapped_vec2, C>.
output << "array<" << kWrappedPrefix << "vec2, "
<< static_cast<uint32_t>(type.getCols()) << ">";
}
else
{
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(kWrappedPrefix, 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