Hash :
25390156
Author :
Date :
2025-08-21T00:13:19
Suppress unsafe buffers on a file-by-file basis in src/ [1 of N] In this CL, we suppress many files but stop short of actually enabling the warning by not removing the line from the unsafe_buffers_paths.txt file. That will happen in a follow-on CL, along with resolving any stragglers missed here. This is mostly a manual change so as to familiarize myself with the kinds of issues faced by the Angle codebase when applying buffer safety warnings. -- Re-generate affected hashes. -- Clang-format applied to all changed files. -- Add a few missing .reserve() calls to vectors as noticed. -- Fix some mismatches between file names and header comments. -- Be more consistent with header comment format (blank lines and trailing //-only lines when a filename comment adjoins license boilerplate). Bug: b/436880895 Change-Id: I3bde5cc2059acbe8345057289214f1a26f1c34aa Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6869022 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
//
// 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.
//
// ProgramWgpu.cpp:
// Implements the class methods for ProgramWgpu.
//
#ifdef UNSAFE_BUFFERS_BUILD
# pragma allow_unsafe_buffers
#endif
#include "libANGLE/renderer/wgpu/ProgramWgpu.h"
#include "GLES2/gl2.h"
#include "common/PackedEnums.h"
#include "common/PackedGLEnums_autogen.h"
#include "common/debug.h"
#include "common/log_utils.h"
#include "libANGLE/Error.h"
#include "libANGLE/ProgramExecutable.h"
#include "libANGLE/renderer/wgpu/ProgramExecutableWgpu.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
#include "libANGLE/renderer/wgpu/wgpu_wgsl_util.h"
#include "libANGLE/trace.h"
#include "platform/autogen/FeaturesWgpu_autogen.h"
namespace rx
{
namespace
{
const bool kOutputFinalSource = false;
// Identical to Std140 encoder in all aspects, except it ignores opaque uniform types.
class WgpuDefaultBlockEncoder : public sh::Std140BlockEncoder
{
public:
void advanceOffset(GLenum type,
const std::vector<unsigned int> &arraySizes,
bool isRowMajorMatrix,
int arrayStride,
int matrixStride) override
{
if (gl::IsOpaqueType(type))
{
return;
}
sh::Std140BlockEncoder::advanceOffset(type, arraySizes, isRowMajorMatrix, arrayStride,
matrixStride);
}
};
angle::Result InitDefaultUniformBlock(const std::vector<sh::ShaderVariable> &uniforms,
sh::BlockLayoutMap *blockLayoutMapOut,
size_t *blockSizeOut)
{
if (uniforms.empty())
{
*blockSizeOut = 0;
return angle::Result::Continue;
}
WgpuDefaultBlockEncoder blockEncoder;
sh::GetActiveUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
// The default uniforms are packed into a struct, and so the size of the struct must be aligned
// to kUniformStructAlignment;
angle::CheckedNumeric blockSize =
CheckedRoundUp(blockEncoder.getCurrentOffset(), webgpu::kUniformStructAlignment);
if (!blockSize.IsValid())
{
ERR() << "Packing the default uniforms into a struct results in a struct that is too "
"large. Unaligned size = "
<< blockEncoder.getCurrentOffset()
<< ", alignment = " << webgpu::kUniformStructAlignment;
return angle::Result::Stop;
}
*blockSizeOut = blockSize.ValueOrDie();
return angle::Result::Continue;
}
std::string FormatWGPUCompilationMessage(const WGPUCompilationMessage &message)
{
std::ostringstream oss;
oss << message.lineNum << ":" << message.linePos << ": "
<< std::string(message.message.data, message.message.length);
return oss.str();
}
class CreateWGPUShaderModuleTask : public LinkSubTask
{
public:
CreateWGPUShaderModuleTask(const DawnProcTable *wgpu,
webgpu::InstanceHandle instance,
webgpu::DeviceHandle device,
const angle::FeaturesWgpu &features,
const gl::SharedCompiledShaderState &compiledShaderState,
const gl::ProgramExecutable &executable,
gl::ProgramMergedVaryings mergedVaryings,
TranslatedWGPUShaderModule &resultShaderModule)
: mProcTable(wgpu),
mInstance(instance),
mDevice(device),
mFeatures(features),
mCompiledShaderState(compiledShaderState),
mExecutable(executable),
mMergedVaryings(std::move(mergedVaryings)),
mShaderModule(resultShaderModule)
{}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
infoLog << mLog.str();
return mResult;
}
void operator()() override
{
ANGLE_TRACE_EVENT0("gpu.angle", "CreateWGPUShaderModuleTask");
gl::ShaderType shaderType = mCompiledShaderState->shaderType;
ASSERT((mExecutable.getLinkedShaderStages() &
~gl::ShaderBitSet({gl::ShaderType::Vertex, gl::ShaderType::Fragment}))
.none());
std::string finalShaderSource;
if (shaderType == gl::ShaderType::Vertex)
{
finalShaderSource = webgpu::WgslAssignLocationsAndSamplerBindings(
mExecutable, mCompiledShaderState->translatedSource, mExecutable.getProgramInputs(),
mMergedVaryings, shaderType);
}
else if (shaderType == gl::ShaderType::Fragment)
{
finalShaderSource = webgpu::WgslAssignLocationsAndSamplerBindings(
mExecutable, mCompiledShaderState->translatedSource,
mExecutable.getOutputVariables(), mMergedVaryings, shaderType);
}
else
{
UNIMPLEMENTED();
}
if (kOutputFinalSource)
{
std::cout << finalShaderSource;
}
WGPUShaderSourceWGSL shaderModuleWGSLDescriptor = WGPU_SHADER_SOURCE_WGSL_INIT;
shaderModuleWGSLDescriptor.code = {finalShaderSource.c_str(), finalShaderSource.length()};
WGPUShaderModuleDescriptor shaderModuleDescriptor = WGPU_SHADER_MODULE_DESCRIPTOR_INIT;
shaderModuleDescriptor.nextInChain = &shaderModuleWGSLDescriptor.chain;
mShaderModule.module = webgpu::ShaderModuleHandle::Acquire(
mProcTable,
mProcTable->deviceCreateShaderModule(mDevice.get(), &shaderModuleDescriptor));
if (mFeatures.avoidWaitAny.enabled)
{
WGPUCompilationInfoCallbackInfo getCompilationInfoCallback =
WGPU_COMPILATION_INFO_CALLBACK_INFO_INIT;
getCompilationInfoCallback.mode = WGPUCallbackMode_AllowSpontaneous;
getCompilationInfoCallback.callback =
[](WGPUCompilationInfoRequestStatus status,
struct WGPUCompilationInfo const *compilationInfo, void *userdata1,
void *userdata2) {
ASSERT(userdata1 == nullptr);
ASSERT(userdata2 == nullptr);
for (size_t msgIdx = 0;
compilationInfo && msgIdx < compilationInfo->messageCount; ++msgIdx)
{
const WGPUCompilationMessage &message = compilationInfo->messages[msgIdx];
switch (message.type)
{
case WGPUCompilationMessageType_Error:
ERR() << FormatWGPUCompilationMessage(message);
break;
case WGPUCompilationMessageType_Warning:
WARN() << FormatWGPUCompilationMessage(message);
break;
case WGPUCompilationMessageType_Info:
INFO() << FormatWGPUCompilationMessage(message);
break;
default:
UNIMPLEMENTED();
break;
}
}
};
mProcTable->shaderModuleGetCompilationInfo(mShaderModule.module.get(),
getCompilationInfoCallback);
}
else
{
WGPUCompilationInfoCallbackInfo getCompilationInfoCallback =
WGPU_COMPILATION_INFO_CALLBACK_INFO_INIT;
getCompilationInfoCallback.mode = WGPUCallbackMode_WaitAnyOnly;
getCompilationInfoCallback.callback =
[](WGPUCompilationInfoRequestStatus status,
struct WGPUCompilationInfo const *compilationInfo, void *userdata1,
void *userdata2) {
CreateWGPUShaderModuleTask *task =
reinterpret_cast<CreateWGPUShaderModuleTask *>(userdata1);
ASSERT(userdata2 == nullptr);
if (status != WGPUCompilationInfoRequestStatus_Success)
{
task->mResult = angle::Result::Stop;
}
for (size_t msgIdx = 0;
compilationInfo && msgIdx < compilationInfo->messageCount; ++msgIdx)
{
const WGPUCompilationMessage &message = compilationInfo->messages[msgIdx];
switch (message.type)
{
case WGPUCompilationMessageType_Error:
task->mLog << "Error: ";
break;
case WGPUCompilationMessageType_Warning:
task->mLog << "Warning: ";
break;
case WGPUCompilationMessageType_Info:
task->mLog << "Info: ";
break;
default:
task->mLog << "Unknown: ";
break;
}
task->mLog << FormatWGPUCompilationMessage(message) << std::endl;
}
};
getCompilationInfoCallback.userdata1 = this;
WGPUFutureWaitInfo waitInfo = WGPU_FUTURE_WAIT_INFO_INIT;
waitInfo.future = mProcTable->shaderModuleGetCompilationInfo(
mShaderModule.module.get(), getCompilationInfoCallback);
WGPUWaitStatus waitStatus =
mProcTable->instanceWaitAny(mInstance.get(), 1, &waitInfo, -1);
if (waitStatus != WGPUWaitStatus_Success)
{
mResult = angle::Result::Stop;
}
}
}
private:
const DawnProcTable *mProcTable = nullptr;
webgpu::InstanceHandle mInstance;
webgpu::DeviceHandle mDevice;
const angle::FeaturesWgpu &mFeatures;
gl::SharedCompiledShaderState mCompiledShaderState;
const gl::ProgramExecutable &mExecutable;
gl::ProgramMergedVaryings mMergedVaryings;
TranslatedWGPUShaderModule &mShaderModule;
std::ostringstream mLog;
angle::Result mResult = angle::Result::Continue;
};
class LinkTaskWgpu : public LinkTask
{
public:
LinkTaskWgpu(const DawnProcTable *wgpu,
webgpu::InstanceHandle instance,
webgpu::DeviceHandle device,
const angle::FeaturesWgpu &features,
ProgramWgpu *program)
: mProcTable(wgpu),
mInstance(instance),
mDevice(device),
mFeatures(features),
mProgram(program),
mExecutable(&mProgram->getState().getExecutable())
{}
~LinkTaskWgpu() override = default;
void link(const gl::ProgramLinkedResources &resources,
const gl::ProgramMergedVaryings &mergedVaryings,
std::vector<std::shared_ptr<LinkSubTask>> *linkSubTasksOut,
std::vector<std::shared_ptr<LinkSubTask>> *postLinkSubTasksOut) override
{
ASSERT(linkSubTasksOut && linkSubTasksOut->empty());
ASSERT(postLinkSubTasksOut && postLinkSubTasksOut->empty());
ProgramExecutableWgpu *executable =
GetImplAs<ProgramExecutableWgpu>(&mProgram->getState().getExecutable());
const gl::ShaderMap<gl::SharedCompiledShaderState> &shaders =
mProgram->getState().getAttachedShaders();
for (gl::ShaderType shaderType : gl::AllShaderTypes())
{
if (shaders[shaderType])
{
auto task = std::make_shared<CreateWGPUShaderModuleTask>(
mProcTable, mInstance, mDevice, mFeatures, shaders[shaderType],
*executable->getExecutable(), mergedVaryings,
executable->getShaderModule(shaderType));
linkSubTasksOut->push_back(task);
}
}
// The default uniform block's CPU buffer needs to be allocated and the layout calculated,
// now that the list of uniforms is known.
angle::Result initUniformBlocksResult = initDefaultUniformBlocks();
if (IsError(initUniformBlocksResult))
{
mLinkResult = initUniformBlocksResult;
return;
}
mLinkResult = angle::Result::Continue;
}
angle::Result getResult(const gl::Context *context, gl::InfoLog &infoLog) override
{
return mLinkResult;
}
private:
angle::Result initDefaultUniformBlocks()
{
ProgramExecutableWgpu *executableWgpu = webgpu::GetImpl(mExecutable);
// Process vertex and fragment uniforms into std140 packing.
gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
gl::ShaderMap<size_t> requiredBufferSize;
requiredBufferSize.fill(0);
ANGLE_TRY(generateUniformLayoutMapping(&layoutMap, &requiredBufferSize));
initDefaultUniformLayoutMapping(&layoutMap);
// All uniform initializations are complete, now resize the buffers accordingly and return
ANGLE_TRY(executableWgpu->resizeUniformBlockMemory(requiredBufferSize));
executableWgpu->markDefaultUniformsDirty();
return angle::Result::Continue;
}
angle::Result generateUniformLayoutMapping(gl::ShaderMap<sh::BlockLayoutMap> *layoutMapOut,
gl::ShaderMap<size_t> *requiredBufferSizeOut)
{
for (const gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
const gl::SharedCompiledShaderState &shader =
mProgram->getState().getAttachedShader(shaderType);
if (shader)
{
const std::vector<sh::ShaderVariable> &uniforms = shader->uniforms;
ANGLE_TRY(InitDefaultUniformBlock(uniforms, &(*layoutMapOut)[shaderType],
&(*requiredBufferSizeOut)[shaderType]));
}
}
return angle::Result::Continue;
}
void initDefaultUniformLayoutMapping(gl::ShaderMap<sh::BlockLayoutMap> *layoutMapOut)
{
// Init the default block layout info.
ProgramExecutableWgpu *executableWgpu = webgpu::GetImpl(mExecutable);
const auto &uniforms = mExecutable->getUniforms();
for (const gl::VariableLocation &location : mExecutable->getUniformLocations())
{
gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
if (location.used() && !location.ignored)
{
const auto &uniform = uniforms[location.index];
if (uniform.isInDefaultBlock() && !uniform.isSampler() && !uniform.isImage() &&
!uniform.isFragmentInOut())
{
std::string uniformName = mExecutable->getUniformNameByIndex(location.index);
if (uniform.isArray())
{
// Gets the uniform name without the [0] at the end.
uniformName = gl::StripLastArrayIndex(uniformName);
ASSERT(uniformName.size() !=
mExecutable->getUniformNameByIndex(location.index).size());
}
bool found = false;
for (const gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
auto it = (*layoutMapOut)[shaderType].find(uniformName);
if (it != (*layoutMapOut)[shaderType].end())
{
found = true;
layoutInfo[shaderType] = it->second;
}
}
ASSERT(found);
}
}
for (const gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
executableWgpu->getSharedDefaultUniformBlock(shaderType)
->uniformLayout.push_back(layoutInfo[shaderType]);
}
}
}
const DawnProcTable *mProcTable = nullptr;
webgpu::InstanceHandle mInstance;
webgpu::DeviceHandle mDevice;
const angle::FeaturesWgpu &mFeatures;
ProgramWgpu *mProgram = nullptr;
const gl::ProgramExecutable *mExecutable;
angle::Result mLinkResult = angle::Result::Stop;
};
} // anonymous namespace
ProgramWgpu::ProgramWgpu(const gl::ProgramState &state) : ProgramImpl(state) {}
ProgramWgpu::~ProgramWgpu() {}
angle::Result ProgramWgpu::load(const gl::Context *context,
gl::BinaryInputStream *stream,
std::shared_ptr<LinkTask> *loadTaskOut,
egl::CacheGetResult *resultOut)
{
*loadTaskOut = {};
*resultOut = egl::CacheGetResult::Success;
return angle::Result::Continue;
}
void ProgramWgpu::save(const gl::Context *context, gl::BinaryOutputStream *stream) {}
void ProgramWgpu::setBinaryRetrievableHint(bool retrievable) {}
void ProgramWgpu::setSeparable(bool separable) {}
angle::Result ProgramWgpu::link(const gl::Context *context, std::shared_ptr<LinkTask> *linkTaskOut)
{
const DawnProcTable *wgpu = webgpu::GetProcs(context);
const angle::FeaturesWgpu &features = webgpu::GetFeatures(context);
webgpu::DeviceHandle device = webgpu::GetDevice(context);
webgpu::InstanceHandle instance = webgpu::GetInstance(context);
*linkTaskOut =
std::shared_ptr<LinkTask>(new LinkTaskWgpu(wgpu, instance, device, features, this));
return angle::Result::Continue;
}
GLboolean ProgramWgpu::validate(const gl::Caps &caps)
{
return GL_TRUE;
}
} // namespace rx