1 //
2 // Copyright 2024 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #ifndef COMPILER_TRANSLATOR_WGSL_UTILS_H_
8 #define COMPILER_TRANSLATOR_WGSL_UTILS_H_
9
10 #include "compiler/translator/Common.h"
11 #include "compiler/translator/InfoSink.h"
12 #include "compiler/translator/IntermNode.h"
13 #include "compiler/translator/Types.h"
14
15 namespace sh
16 {
17
18 // Can be used with TSymbol or TField or TFunc.
19 template <typename StringStreamType, typename Object>
WriteNameOf(StringStreamType & output,const Object & namedObject)20 void WriteNameOf(StringStreamType &output, const Object &namedObject)
21 {
22 WriteNameOf(output, namedObject.symbolType(), namedObject.name());
23 }
24
25 template <typename StringStreamType>
26 void WriteNameOf(StringStreamType &output, SymbolType symbolType, const ImmutableString &name);
27
28 enum class WgslAddressSpace
29 {
30 Uniform,
31 NonUniform
32 };
33
34 struct EmitTypeConfig
35 {
36 // If `addressSpace` is WgslAddressSpace::Uniform, all arrays with stride not a multiple of 16
37 // will need a wrapper struct for the array element type that is of size a multiple of 16, if
38 // the array element type that is not already a struct. This is to satisfy WGSL's uniform
39 // address space layout constraints.
40 WgslAddressSpace addressSpace = WgslAddressSpace::NonUniform;
41 };
42
43 template <typename StringStreamType>
44 void WriteWgslBareTypeName(StringStreamType &output,
45 const TType &type,
46 const EmitTypeConfig &config);
47 template <typename StringStreamType>
48 void WriteWgslType(StringStreamType &output, const TType &type, const EmitTypeConfig &config);
49
50 // GLSL's samplers are split into a separate sampler and texture in WGSL, so two different types
51 // will be emitted for a single sampler type.
52 enum class WgslSamplerTypeConfig
53 {
54 Sampler,
55 Texture
56 };
57
58 template <typename StringStreamType>
59 void WriteWgslSamplerType(StringStreamType &output,
60 const TType &type,
61 WgslSamplerTypeConfig samplerType);
62
63 // From the type, creates a legal WGSL name for a struct that wraps it.
64 ImmutableString MakeUniformWrapperStructName(const TType *type);
65
66 // Returns true if a `type` in the uniform address space is an array that needs its element type
67 // wrapped in a struct.
68 bool ElementTypeNeedsUniformWrapperStruct(bool inUniformAddressSpace, const TType *type);
69
70 using GlobalVars = TMap<ImmutableString, TIntermDeclaration *>;
71 GlobalVars FindGlobalVars(TIntermBlock *root);
72
73 } // namespace sh
74
75 #endif // COMPILER_TRANSLATOR_WGSL_UTILS_H_
76