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
//
// 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.
//
#ifndef COMPILER_REWRITE_BUILTIN_VARIABLES_H_
#define COMPILER_REWRITE_BUILTIN_VARIABLES_H_
#include <variant>
#include "compiler/translator/Common.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/InfoSink.h"
#include "compiler/translator/IntermNode.h"
#include "compiler/translator/SymbolUniqueId.h"
#include "compiler/translator/wgsl/Utils.h"
namespace sh
{
// In WGSL, all input values are parameters to the shader's main function and all output values are
// return values of the shader's main functions (the input/output values can be embedded within
// struct types). So this rewrites all accesses of GLSL's input/output variables (including
// builtins) to be accesses of a global struct, and writes a new main function (called wgslMain())
// that populates the global input struct with parameters of the wgslMain function, and populates
// wgslMain's return value with the fields of the global output struct.
//
// TODO(anglebug.com/42267100): some of these WGSL builtins do not correspond exactly to GLSL
// builtin values and will need modification at the beginning (resp. end) of the main function for
// input values (resp. output values). E.g. normalized device coordinates in GLSL have -1.0 <= z
// <= 1.0, whereas NDC in WGSL have 0.0 <= z <= 1.0.
// See e.g. bool TranslatorMSL::appendVertexShaderDepthCorrectionToMain().
//
// Example GLSL:
//
// attribute vec2 xy_position;
// void setPosition() {
// gl_Position = vec4(xy_position.x, xy_position.y, 0.0, 1.0);
// }
// void main()
// {
// setPosition();
// }
//
// The resulting WGSL:
// struct ANGLE_Input_Global {
// xy_position : vec2<f32>,
// };
//
// var<private> ANGLE_input_global : ANGLE_Input_Global;
//
// struct ANGLE_Input_Annotated {
// @location(@@@@@@) xy_position : vec2<f32>,
// };
//
// struct ANGLE_Output_Global {
// gl_Position_ : vec4<f32>,
// };
//
// var<private> ANGLE_output_global : ANGLE_Output_Global;
//
// struct ANGLE_Output_Annotated {
// @builtin(position) gl_Position_ : vec4<f32>,
// };
//
// // Generated versions of _umain() and _usetPosition() go here.
//
// @vertex
// fn wgslMain(ANGLE_input_annotated : ANGLE_Input_Annotated) -> ANGLE_Output_Annotated
// {
// ANGLE_input_global.xy_position = ANGLE_input_annotated.xy_position;
// _umain();
// var ANGLE_output_annotated : ANGLE_Output_Annotated;
// ANGLE_output_annotated.gl_Position_ = ANGLE_output_global.gl_Position_;
// return ANGLE_output_annotated;
// }
//
// Note the WGSL outputter should not output any declarations of global in/out variables, nor any
// redeclarations of builtin variables. And all accesses to global in/out variables should be
// rewritten as struct accesses of the global structs.
const char kBuiltinInputStructType[] = "ANGLE_Input_Global";
const char kBuiltinOutputStructType[] = "ANGLE_Output_Global";
const char kBuiltinInputAnnotatedStructType[] = "ANGLE_Input_Annotated";
const char kBuiltinOutputAnnotatedStructType[] = "ANGLE_Output_Annotated";
const char kBuiltinInputStructName[] = "ANGLE_input_global";
const char kBuiltinOutputStructName[] = "ANGLE_output_global";
const char kBuiltinInputAnnotatedStructName[] = "ANGLE_input_annotated";
const char kBuiltinOutputAnnotatedStructName[] = "ANGLE_output_annotated";
class RewritePipelineVarOutputBuilder;
struct RewritePipelineVarOutput
{
public:
RewritePipelineVarOutput(sh::GLenum shaderType);
// Every time the translator goes to output a TVariable/TSymbol it checks these functions to see
// if it should generate a struct access instead.
bool IsInputVar(TSymbolUniqueId angleInputVar) const;
bool IsOutputVar(TSymbolUniqueId angleOutputVar) const;
bool OutputStructs(TInfoSinkBase &output);
bool OutputMainFunction(TInfoSinkBase &output);
private:
friend RewritePipelineVarOutputBuilder;
// The key is TSymbolUniqueId::get().
using RewrittenVarSet = TUnorderedSet<int>;
struct WgslIOBlock
{
TVector<ImmutableString> angleGlobalMembers;
TVector<ImmutableString> angleAnnotatedMembers;
TVector<ImmutableString> angleConversionFuncs;
};
static bool OutputIOStruct(TInfoSinkBase &output,
WgslIOBlock &block,
ImmutableString builtinStructType,
ImmutableString builtinStructName,
ImmutableString builtinAnnotatedStructType);
// Represents the input and output structs for the WGSL main function.
WgslIOBlock mInputBlock;
WgslIOBlock mOutputBlock;
// Tracks all symbols (attributes, output vars, builtins) that need to be rewritten into struct
// accesses by the WGSL outputter. Used in `IsInputVar()` and `IsOutputVar()`.
RewrittenVarSet mAngleInputVars;
RewrittenVarSet mAngleOutputVars;
sh::GLenum mShaderType;
};
// `outVarReplacements` is a RewritePipelineVarOutput that can output the WGSL main function and the
// input/output structs that represent the input and output variables of the GLSL shader,
[[nodiscard]] bool GenerateMainFunctionAndIOStructs(TCompiler &compiler,
TIntermBlock &root,
RewritePipelineVarOutput &outVarReplacements);
} // namespace sh
#endif // COMPILER_REWRITE_BUILTIN_VARIABLES_H_