Hash :
4b69ba93
Author :
Date :
2025-04-28T16:45:56
WGPU: upload texture bind groups and configured samplers
GLSL samplers are split into separate sampler/texture variables
in WGSL. Before this CL, the WGSL translator generated shaders
that look like:
@group(1) @binding(@@@@@@) var ANGLE_sampler_samp2D :
sampler;
@group(1) @binding(@@@@@@) var ANGLE_texture_samp2D :
texture_2d<f32>;
@group(1) @binding(@@@@@@) var ANGLE_sampler_sampCube :
sampler;
@group(1) @binding(@@@@@@) var ANGLE_texture_sampCube :
texture_cube<f32>;
This CL replaces those with actual binding numbers
@group(1) @binding(0) var ANGLE_sampler_samp2D :
sampler;
@group(1) @binding(1) var ANGLE_texture_samp2D :
texture_2d<f32>;
...
Such that @binding(n*2) is the WGSL sampler variable corresponding
to the n-th GLSL sampler and @binding(n*2+1) is the WGSL texture
variable corresponding to the n-th GLSL sampler.
This CL then generates binding group layouts matching the above,
and uploads textures and configured samplers in bind groups.
This makes some of the deqp_gles2 tests 2d texture tests pass,
though some fail because they need a flipped y coordinate.
Not yet supported:
1. arrays of samplers
2. shadow samplers
3. cube textures
Bug: angleproject:389145696
Change-Id: I2ab18ae5ebb4d1289101266bd9451576aa04ce2a
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6382272
Reviewed-by: Liza Burakova <liza@chromium.org>
Auto-Submit: Matthew Denton <mpdenton@chromium.org>
Commit-Queue: Geoff Lang <geofflang@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
//
// 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 "libANGLE/renderer/wgpu/wgpu_wgsl_util.h"
#include <sstream>
#include "common/PackedEnums.h"
#include "common/PackedGLEnums_autogen.h"
#include "common/log_utils.h"
#include "compiler/translator/wgsl/OutputUniformBlocks.h"
#include "libANGLE/Program.h"
#include "libANGLE/ProgramExecutable.h"
namespace rx
{
namespace webgpu
{
namespace
{
const bool kOutputReplacements = false;
void ReplaceFoundMarker(const std::string &shaderSource,
std::string &newSource,
const std::map<std::string, int> &varNameToLocation,
const char *marker,
const char *markerReplacement,
const char *replacementEnd,
size_t nextMarker,
size_t &currPos)
{
const char *endOfName = " : ";
// Copy up to the next marker
newSource.append(shaderSource, currPos, nextMarker - currPos);
// Extract name from something like `@location(@@@@@@) NAME : TYPE`.
size_t startOfNamePos = nextMarker + strlen(marker);
size_t endOfNamePos = shaderSource.find(endOfName, startOfNamePos, strlen(endOfName));
std::string name(shaderSource.c_str() + startOfNamePos, endOfNamePos - startOfNamePos);
// Use the shader variable's name to get the assigned location
auto locationIter = varNameToLocation.find(name);
if (locationIter == varNameToLocation.end())
{
// This should be an ignored sampler, so just delete it.
size_t endOfLine = shaderSource.find(";\n", nextMarker);
currPos = endOfLine + 2;
return;
}
// TODO(anglebug.com/42267100): if the GLSL input is a matrix there should be multiple
// WGSL input variables (multiple vectors representing the columns of the matrix).
int location = locationIter->second;
std::ostringstream locationReplacementStream;
locationReplacementStream << markerReplacement << location << replacementEnd << " " << name;
if (kOutputReplacements)
{
std::cout << "Replace \"" << marker << name << "\" with \""
<< locationReplacementStream.str() << "\"" << std::endl;
}
// Append the new `@location(N) name` and then continue from the ` : type`.
newSource.append(locationReplacementStream.str());
currPos = endOfNamePos;
}
std::string WgslReplaceMarkers(const std::string &shaderSource,
const std::map<std::string, int> &varNameToLocation)
{
const char *locationMarker = "@location(@@@@@@) ";
const char *bindingMarker = "@group(1) @binding(@@@@@@) var ";
std::string newSource;
newSource.reserve(shaderSource.size());
size_t currPos = 0;
while (true)
{
size_t nextMarker = shaderSource.find(locationMarker, currPos, strlen(locationMarker));
if (nextMarker != std::string::npos)
{
ReplaceFoundMarker(shaderSource, newSource, varNameToLocation, locationMarker,
"@location(", ")", nextMarker, currPos);
}
else if ((nextMarker = shaderSource.find(bindingMarker, currPos, strlen(bindingMarker))) !=
std::string::npos)
{
ReplaceFoundMarker(shaderSource, newSource, varNameToLocation, bindingMarker,
"@group(1) @binding(", ") var", nextMarker, currPos);
}
else
{
// Copy the rest of the shader and end the loop.
newSource.append(shaderSource, currPos);
break;
}
}
return newSource;
}
} // namespace
template <typename T>
std::string WgslAssignLocationsAndSamplerBindings(const gl::ProgramExecutable &executable,
const std::string &shaderSource,
const std::vector<T> shaderVars,
const gl::ProgramMergedVaryings &mergedVaryings,
gl::ShaderType shaderType)
{
std::map<std::string, int> varNameToLocation;
for (const T &shaderVar : shaderVars)
{
if (shaderVar.isBuiltIn())
{
continue;
}
varNameToLocation[shaderVar.name] = shaderVar.getLocation();
}
int currLocMarker = 0;
for (const gl::ProgramVaryingRef &linkedVarying : mergedVaryings)
{
gl::ShaderBitSet supportedShaderStages =
gl::ShaderBitSet({gl::ShaderType::Vertex, gl::ShaderType::Fragment});
ASSERT(linkedVarying.frontShaderStage == gl::ShaderType::InvalidEnum ||
supportedShaderStages.test(linkedVarying.frontShaderStage));
ASSERT(linkedVarying.backShaderStage == gl::ShaderType::InvalidEnum ||
supportedShaderStages.test(linkedVarying.backShaderStage));
if (!linkedVarying.frontShader && !linkedVarying.backShader)
{
continue;
}
const sh::ShaderVariable *shaderVar = shaderType == gl::ShaderType::Vertex
? linkedVarying.frontShader
: linkedVarying.backShader;
if (shaderVar)
{
if (shaderVar->isBuiltIn())
{
continue;
}
ASSERT(varNameToLocation.find(shaderVar->name) == varNameToLocation.end());
varNameToLocation[shaderVar->name] = currLocMarker++;
}
else
{
const sh::ShaderVariable *otherShaderVar = shaderType == gl::ShaderType::Vertex
? linkedVarying.backShader
: linkedVarying.frontShader;
if (!otherShaderVar->isBuiltIn())
{
// Increment `currLockMarker` to keep locations in sync with the WGSL source
// generated for the other shader stage, which will also have incremented
// `currLocMarker` when seeing this variable.
currLocMarker++;
}
}
}
const std::vector<gl::SamplerBinding> &samplerBindings = executable.getSamplerBindings();
// GLSL samplers are split into WGSL samplers/textures and need to be assigned consecutive
// locations, alternating between a sampler and its corresponding texture.
// The WGPU backend will read the same metadata and lay out its bind groups in the same
// alternating fashion.
for (uint32_t textureIndex = 0; textureIndex < samplerBindings.size(); ++textureIndex)
{
if (samplerBindings[textureIndex].textureUnitsCount != 1)
{
// TODO(anglebug.com/389145696): implement sampler arrays.
UNIMPLEMENTED();
continue;
}
// Get the name of the sampler variable from the uniform metadata.
uint32_t uniformIndex = executable.getUniformIndexFromSamplerIndex(textureIndex);
const std::string &uniformName = executable.getUniformNames()[uniformIndex];
std::string mappedSamplerName = sh::WGSLGetMappedSamplerName(uniformName);
varNameToLocation[std::string(sh::kAngleSamplerPrefix) + mappedSamplerName] =
textureIndex * 2;
varNameToLocation[std::string(sh::kAngleTexturePrefix) + mappedSamplerName] =
textureIndex * 2 + 1;
}
return WgslReplaceMarkers(shaderSource, varNameToLocation);
}
template std::string WgslAssignLocationsAndSamplerBindings<gl::ProgramInput>(
const gl::ProgramExecutable &executable,
const std::string &shaderSource,
const std::vector<gl::ProgramInput> shaderVars,
const gl::ProgramMergedVaryings &mergedVaryings,
gl::ShaderType shaderType);
template std::string WgslAssignLocationsAndSamplerBindings<gl::ProgramOutput>(
const gl::ProgramExecutable &executable,
const std::string &shaderSource,
const std::vector<gl::ProgramOutput> shaderVars,
const gl::ProgramMergedVaryings &mergedVaryings,
gl::ShaderType shaderType);
} // namespace webgpu
} // namespace rx