Hash :
a6ee4641
Author :
Date :
2024-09-25T11:41:47
WGSL: Output default uniform block and accesses to it Default uniforms are put into a WGSL struct, and all accesses of those uniforms now output struct accesses. Similarly to I/O vars and builtins, these are outputted in a pre-pass, but in the future it might make sense to do what Vulkan does and do an AST transformation to put the default uniforms into a UBO which should be outputted similarly. This does not handle bool, matCx2, or array of element size < 16. Bug: angleproject:42267100 Change-Id: If29e2895a8aba3212b581813316af87273c1515c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5878759 Reviewed-by: Liza Burakova <liza@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Matthew Denton <mpdenton@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
//
// 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/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 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)
{
if (type.isArray())
{
// Examples:
// array<f32, 5>
// array<array<u32, 5>, 10>
output << "array<";
TType innerType = type;
innerType.toArrayElementType();
WriteWgslType(output, innerType);
output << ", " << type.getOutermostArraySize() << ">";
}
else if (type.isVector())
{
output << "vec" << static_cast<uint32_t>(type.getNominalSize()) << "<";
WriteWgslBareTypeName(output, type);
output << ">";
}
else if (type.isMatrix())
{
output << "mat" << static_cast<uint32_t>(type.getCols()) << "x"
<< static_cast<uint32_t>(type.getRows()) << "<";
WriteWgslBareTypeName(output, type);
output << ">";
}
else
{
// This type has no dimensions and is equivalent to its bare type.
WriteWgslBareTypeName(output, type);
}
}
template void WriteWgslBareTypeName<TInfoSinkBase>(TInfoSinkBase &output, const TType &type);
template void WriteNameOf<TInfoSinkBase>(TInfoSinkBase &output,
SymbolType symbolType,
const ImmutableString &name);
template void WriteWgslType<TInfoSinkBase>(TInfoSinkBase &output, const TType &type);
template void WriteWgslBareTypeName<TStringStream>(TStringStream &output, const TType &type);
template void WriteNameOf<TStringStream>(TStringStream &output,
SymbolType symbolType,
const ImmutableString &name);
template void WriteWgslType<TStringStream>(TStringStream &output, const TType &type);
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