Hash :
cac1e824
Author :
Date :
2025-04-29T18:27:36
WGSL: Output driver uniform and UBO structs This is the WGSL half of the change to implement driver uniforms. Driver uniforms are implemented as a UBO and reuse the default set of driver uniforms. User-provided UBOs don't yet have variables outputted for them. This requires moving MSL's ReduceInterfaceBlocks to the tree_ops dir in order to change interface block definitions into struct definitions. Bug: angleproject:389145696 Change-Id: I27f3837b3d115f2ffac66cc545f3b60ca9f01cb6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6477564 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> 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 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
//
// 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/OutputUniformBlocks.h"
#include <iostream>
#include "GLSLANG/ShaderVars.h"
#include "angle_gl.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "compiler/translator/BaseTypes.h"
#include "compiler/translator/Common.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/InfoSink.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/SymbolUniqueId.h"
#include "compiler/translator/tree_util/DriverUniform.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/util.h"
#include "compiler/translator/wgsl/Utils.h"
namespace sh
{
namespace
{
// Traverses the AST and finds all structs that are used in the uniform address space (see the
// UniformBlockMetadata struct).
class FindUniformAddressSpaceStructs : public TIntermTraverser
{
public:
FindUniformAddressSpaceStructs(UniformBlockMetadata *uniformBlockMetadata)
: TIntermTraverser(true, false, false), mUniformBlockMetadata(uniformBlockMetadata)
{}
~FindUniformAddressSpaceStructs() override = default;
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
const TIntermSequence &sequence = *(node->getSequence());
TIntermTyped *variable = sequence.front()->getAsTyped();
const TType &type = variable->getType();
// TODO(anglebug.com/376553328): should eventually ASSERT that there are no default uniforms
// here.
if (type.getQualifier() == EvqUniform)
{
recordTypesUsedInUniformAddressSpace(&type);
}
return true;
}
private:
// Recurses through the tree of types referred to be `type` (which is used in the uniform
// address space) and fills in the `mUniformBlockMetadata` struct appropriately.
void recordTypesUsedInUniformAddressSpace(const TType *type)
{
if (type->isArray())
{
TType innerType = *type;
innerType.toArrayBaseType();
recordTypesUsedInUniformAddressSpace(&innerType);
}
else if (type->getStruct() != nullptr)
{
mUniformBlockMetadata->structsInUniformAddressSpace.insert(
type->getStruct()->uniqueId().get());
// Recurse into the types of the fields of this struct type.
for (TField *const field : type->getStruct()->fields())
{
recordTypesUsedInUniformAddressSpace(field->type());
}
}
}
UniformBlockMetadata *const mUniformBlockMetadata;
};
} // namespace
bool RecordUniformBlockMetadata(TIntermBlock *root, UniformBlockMetadata &outMetadata)
{
FindUniformAddressSpaceStructs traverser(&outMetadata);
root->traverse(&traverser);
return true;
}
bool OutputUniformWrapperStructsAndConversions(
TInfoSinkBase &output,
const WGSLGenerationMetadataForUniforms &wgslGenerationMetadataForUniforms)
{
auto generate16AlignedWrapperStruct = [&output](const TType &type) {
output << "struct " << MakeUniformWrapperStructName(&type) << "\n{\n";
output << " @align(16) " << kWrappedStructFieldName << " : ";
WriteWgslType(output, type, {});
output << "\n};\n";
};
bool generatedVec2WrapperStruct = false;
for (const TType &type : wgslGenerationMetadataForUniforms.arrayElementTypesInUniforms)
{
// Structs don't need wrapper structs.
ASSERT(type.getStruct() == nullptr);
// Multidimensional arrays not currently supported in uniforms
ASSERT(!type.isArray());
if (type.isVector() && type.getNominalSize() == 2)
{
generatedVec2WrapperStruct = true;
}
generate16AlignedWrapperStruct(type);
}
// matCx2 is represented as array<ANGLE_wrapped_vec2, C> so if there are matCx2s we need to
// generate an ANGLE_wrapped_vec2 struct.
if (!wgslGenerationMetadataForUniforms.outputMatCx2Conversion.empty() &&
!generatedVec2WrapperStruct)
{
generate16AlignedWrapperStruct(*new TType(TBasicType::EbtFloat, 2));
}
for (const TType &type :
wgslGenerationMetadataForUniforms.arrayElementTypesThatNeedUnwrappingConversions)
{
// Should be a subset of the types that have had wrapper structs generated above, otherwise
// it's impossible to unwrap them!
TType innerType = type;
innerType.toArrayElementType();
ASSERT(wgslGenerationMetadataForUniforms.arrayElementTypesInUniforms.count(innerType) != 0);
// This could take ptr<uniform, typeName>, with the unrestricted_pointer_parameters
// extension. This is probably fine.
output << "fn " << MakeUnwrappingArrayConversionFunctionName(&type) << "(wrappedArr : ";
WriteWgslType(output, type, {WgslAddressSpace::Uniform});
output << ") -> ";
WriteWgslType(output, type, {WgslAddressSpace::NonUniform});
output << "\n{\n";
output << " var retVal : ";
WriteWgslType(output, type, {WgslAddressSpace::NonUniform});
output << ";\n";
output << " for (var i : u32 = 0; i < " << type.getOutermostArraySize() << "; i++) {;\n";
output << " retVal[i] = wrappedArr[i]." << kWrappedStructFieldName << ";\n";
output << " }\n";
output << " return retVal;\n";
output << "}\n";
}
for (const TType &type : wgslGenerationMetadataForUniforms.outputMatCx2Conversion)
{
ASSERT(type.isMatrix() && type.getRows() == 2);
output << "fn " << MakeMatCx2ConversionFunctionName(&type) << "(mangledMatrix : ";
WriteWgslType(output, type, {WgslAddressSpace::Uniform});
output << ") -> ";
WriteWgslType(output, type, {WgslAddressSpace::NonUniform});
output << "\n{\n";
output << " var retVal : ";
WriteWgslType(output, type, {WgslAddressSpace::NonUniform});
output << ";\n";
if (type.isArray())
{
output << " for (var i : u32 = 0; i < " << type.getOutermostArraySize()
<< "; i++) {;\n";
output << " retVal[i] = ";
}
else
{
output << " retVal = ";
}
TType baseType = type;
baseType.toArrayBaseType();
WriteWgslType(output, baseType, {WgslAddressSpace::NonUniform});
output << "(";
for (uint8_t i = 0; i < type.getCols(); i++)
{
if (i != 0)
{
output << ", ";
}
// The mangled matrix is an array and the elements are wrapped vec2s, which can be
// passed directly to the matCx2 constructor.
output << "mangledMatrix" << (type.isArray() ? "[i]" : "") << "[" << static_cast<int>(i)
<< "]." << kWrappedStructFieldName;
}
output << ");\n";
if (type.isArray())
{
// Close the for loop.
output << " }\n";
}
output << " return retVal;\n";
output << "}\n";
}
return true;
}
ImmutableString MakeUnwrappingArrayConversionFunctionName(const TType *type)
{
ASSERT(type->getNumArraySizes() <= 1);
ImmutableString arrStr = type->isArray() ? BuildConcatenatedImmutableString(
"Array", type->getOutermostArraySize(), "_")
: kEmptyImmutableString;
return BuildConcatenatedImmutableString("ANGLE_Convert_", arrStr,
MakeUniformWrapperStructName(type), "_ElementsTo_",
type->getBuiltInTypeNameString(), "_Elements");
}
bool IsMatCx2(const TType *type)
{
return type->isMatrix() && type->getRows() == 2;
}
ImmutableString MakeMatCx2ConversionFunctionName(const TType *type)
{
ASSERT(type->getNumArraySizes() <= 1);
ImmutableString arrStr = type->isArray() ? BuildConcatenatedImmutableString(
"Array", type->getOutermostArraySize(), "_")
: kEmptyImmutableString;
return BuildConcatenatedImmutableString("ANGLE_Convert_", arrStr, "Mat", type->getCols(), "x2");
}
bool OutputUniformBlocksAndSamplers(TCompiler *compiler, TIntermBlock *root)
{
// TODO(anglebug.com/42267100): This should eventually just be handled the same way as a regular
// UBO, like in Vulkan which create a block out of the default uniforms with a traverser:
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/angle/src/compiler/translator/spirv/TranslatorSPIRV.cpp;l=70;drc=451093bbaf7fe812bf67d27d760f3bb64c92830b
const std::vector<ShaderVariable> &basicUniforms = compiler->getUniforms();
TInfoSinkBase &output = compiler->getInfoSink().obj;
GlobalVars globalVars = FindGlobalVars(root);
// Only output a struct at all if there are going to be members.
bool outputStructHeader = false;
for (const ShaderVariable &shaderVar : basicUniforms)
{
if (gl::IsOpaqueType(shaderVar.type) || !shaderVar.active)
{
continue;
}
if (shaderVar.isBuiltIn())
{
// gl_DepthRange and also the GLSL 4.2 gl_NumSamples are uniforms.
// TODO(anglebug.com/42267100): put gl_DepthRange into default uniform block.
continue;
}
// TODO(anglebug.com/42267100): some types will NOT match std140 layout here, namely matCx2,
// bool, and arrays with stride less than 16.
// (this check does not cover the unsupported case where there is an array of structs of
// size < 16).
if (shaderVar.type == GL_BOOL)
{
return false;
}
// Some uniform variables might have been deleted, for example if they were structs that
// only contained samplers (which are pulled into separate default uniforms).
auto globalVarIter = globalVars.find(shaderVar.name);
if (globalVarIter == globalVars.end())
{
continue;
}
if (!outputStructHeader)
{
output << "struct ANGLE_DefaultUniformBlock {\n";
outputStructHeader = true;
}
output << " ";
output << shaderVar.name << " : ";
TIntermDeclaration *declNode = globalVarIter->second;
const TVariable *astVar = &ViewDeclaration(*declNode).symbol.variable();
WriteWgslType(output, astVar->getType(), {WgslAddressSpace::Uniform});
output << ",\n";
}
if (outputStructHeader)
{
ASSERT(compiler->getShaderType() == GL_VERTEX_SHADER ||
compiler->getShaderType() == GL_FRAGMENT_SHADER);
const uint32_t bindingIndex = compiler->getShaderType() == GL_VERTEX_SHADER
? kDefaultVertexUniformBlockBinding
: kDefaultFragmentUniformBlockBinding;
output << "};\n\n"
<< "@group(" << kDefaultUniformBlockBindGroup << ") @binding(" << bindingIndex
<< ") var<uniform> " << kDefaultUniformBlockVarName << " : "
<< kDefaultUniformBlockVarType << ";\n";
}
// Output interface blocks. Start with driver uniforms in their own bind group.
output << "@group(" << kDriverUniformBindGroup << ") @binding(" << kDriverUniformBlockBinding
<< ") var<uniform> " << kDriverUniformsVarName << " : " << kDriverUniformsBlockName
<< ";\n";
// TODO(anglebug.com/376553328): now output the UBOs in `compiler->getUniformBlocks()` in its
// own bind group.
// Output split texture/sampler variables.
for (const auto &globalVarIter : globalVars)
{
TIntermDeclaration *declNode = globalVarIter.second;
ASSERT(declNode);
const TIntermSymbol *declSymbol = &ViewDeclaration(*declNode).symbol;
const TType &declType = declSymbol->getType();
if (!declType.isSampler())
{
continue;
}
// Note that this may output ignored symbols.
output << kTextureSamplerBindingMarker << kAngleSamplerPrefix << declSymbol->getName()
<< " : ";
WriteWgslSamplerType(output, declType, WgslSamplerTypeConfig::Sampler);
output << ";\n";
output << kTextureSamplerBindingMarker << kAngleTexturePrefix << declSymbol->getName()
<< " : ";
WriteWgslSamplerType(output, declType, WgslSamplerTypeConfig::Texture);
output << ";\n";
}
return true;
}
std::string WGSLGetMappedSamplerName(const std::string &originalName)
{
std::string samplerName = originalName;
// Samplers in structs are extracted.
std::replace(samplerName.begin(), samplerName.end(), '.', '_');
// Remove array elements
auto out = samplerName.begin();
for (auto in = samplerName.begin(); in != samplerName.end(); in++)
{
if (*in == '[')
{
while (*in != ']')
{
in++;
ASSERT(in != samplerName.end());
}
}
else
{
*out++ = *in;
}
}
samplerName.erase(out, samplerName.end());
return samplerName;
}
} // namespace sh