Hash :
6f0337a0
Author :
Date :
2025-08-28T23:13:22
Avoid some needless c_str() calls. Remove conversions from string -> char* -> string, as detected some time ago by a clang compiler plugin. Typically, this occurs when passing a c_str() result to a function that expects a string argument. Bug: b/412730353 Change-Id: I1d9c83e9ed5c4900eec266e71f534661f0f3d4d4 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6896657 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Auto-Submit: Tom Sepez <tsepez@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@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
//
// 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.
//
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "libANGLE/renderer/wgpu/wgpu_wgsl_util.h"
#include <sstream>
#include <string>
#include "common/PackedEnums.h"
#include "common/PackedGLEnums_autogen.h"
#include "common/log_utils.h"
#include "common/utilities.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.substr(startOfNamePos, endOfNamePos - startOfNamePos);
// Use the shader variable's name to get the assigned location
auto locationIter = varNameToLocation.find(name);
if (locationIter == varNameToLocation.end())
{
if (kOutputReplacements)
{
std::cout << "Didn't find " << name << ", so skipping to next semicolon" << std::endl;
}
// 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;
}
void AddShaderVarLocation(std::map<std::string, int> &varNameToLocation,
std::string varName,
int &startLoc,
GLenum varType,
unsigned int arraySize)
{
ASSERT(!gl::IsSamplerType(varType));
if (!arraySize && !gl::IsMatrixType(varType))
{
ASSERT(varNameToLocation.find(varName) == varNameToLocation.end());
varNameToLocation[varName] = startLoc++;
return;
}
if (arraySize)
{
// TODO(anglebug.com/42267100): need to support arrays (of scalars, vectors, and matrices).
UNIMPLEMENTED();
return;
}
if (gl::IsMatrixType(varType))
{
// Split into column vectors.
for (int i = 0; i < gl::VariableColumnCount(varType); i++)
{
std::string fullVarName = varName + "_col" + std::to_string(i);
ASSERT(varNameToLocation.find(fullVarName) == varNameToLocation.end());
varNameToLocation[fullVarName] = startLoc++;
}
}
}
} // 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;
}
int loc = shaderVar.getLocation();
AddShaderVarLocation(varNameToLocation, shaderVar.name, loc, shaderVar.getType(),
shaderVar.isArray() ? shaderVar.getBasicTypeElementCount() : 0u);
}
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;
}
AddShaderVarLocation(varNameToLocation, shaderVar->name, currLocMarker, shaderVar->type,
shaderVar->isArray() ? shaderVar->getBasicTypeElementCount() : 0u);
}
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