Hash :
a41b798e
Author :
Date :
2024-11-21T11:07:34
WebGPU: Ensure mDefaultBindGroup is created mDefaultBindGroup would not be created if the program has no uniforms because they are not dirty. Mark all stages as dirty after linking. Bug: angleproject:376553328 Change-Id: I1663791fa1642be052948c5acb8e403fa8b844f9 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6039006 Reviewed-by: Matthew Denton <mpdenton@chromium.org> Commit-Queue: 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 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
//
// 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.
//
// ProgramExecutableWgpu.cpp: Implementation of ProgramExecutableWgpu.
#include "libANGLE/renderer/wgpu/ProgramExecutableWgpu.h"
#include <iterator>
#include "angle_gl.h"
#include "anglebase/numerics/safe_conversions.h"
#include "common/PackedGLEnums_autogen.h"
#include "compiler/translator/wgsl/OutputUniformBlocks.h"
#include "libANGLE/Error.h"
#include "libANGLE/Program.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/wgpu_helpers.h"
#include "libANGLE/renderer/wgpu/wgpu_pipeline_state.h"
namespace rx
{
ProgramExecutableWgpu::ProgramExecutableWgpu(const gl::ProgramExecutable *executable)
: ProgramExecutableImpl(executable)
{
for (std::shared_ptr<BufferAndLayout> &defaultBlock : mDefaultUniformBlocks)
{
defaultBlock = std::make_shared<BufferAndLayout>();
}
}
ProgramExecutableWgpu::~ProgramExecutableWgpu() = default;
void ProgramExecutableWgpu::destroy(const gl::Context *context) {}
angle::Result ProgramExecutableWgpu::updateUniformsAndGetBindGroup(ContextWgpu *contextWgpu,
wgpu::BindGroup *outBindGroup)
{
if (mDefaultUniformBlocksDirty.any())
{
// TODO(anglebug.com/376553328): this creates an entire new buffer every time a single
// uniform changes, and the old ones are just garbage collected. This should be optimized.
webgpu::BufferHelper defaultUniformBuffer;
gl::ShaderMap<uint64_t> offsets =
{}; // offset in the GPU-side buffer of each shader stage's uniform data.
size_t requiredSpace;
angle::CheckedNumeric<size_t> requiredSpaceChecked =
calcUniformUpdateRequiredSpace(contextWgpu, &offsets);
if (!requiredSpaceChecked.AssignIfValid(&requiredSpace))
{
return angle::Result::Stop;
}
ANGLE_TRY(defaultUniformBuffer.initBuffer(
contextWgpu->getDevice(), requiredSpace,
wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopyDst, webgpu::MapAtCreation::Yes));
ASSERT(defaultUniformBuffer.valid());
// Copy all of the CPU-side data into this buffer which will be visible to the GPU after it
// is unmapped here on the CPU.
uint8_t *bufferData = defaultUniformBuffer.getMapWritePointer(0, requiredSpace);
for (gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
const angle::MemoryBuffer &uniformData = mDefaultUniformBlocks[shaderType]->uniformData;
memcpy(&bufferData[offsets[shaderType]], uniformData.data(), uniformData.size());
mDefaultUniformBlocksDirty.reset(shaderType);
}
ANGLE_TRY(defaultUniformBuffer.unmap());
// Create the BindGroupEntries
std::vector<wgpu::BindGroupEntry> bindings;
auto addBindingToGroupIfNecessary = [&](uint32_t bindingIndex, gl::ShaderType shaderType) {
if (mDefaultUniformBlocks[shaderType]->uniformData.size() != 0)
{
wgpu::BindGroupEntry bindGroupEntry;
bindGroupEntry.binding = bindingIndex;
bindGroupEntry.buffer = defaultUniformBuffer.getBuffer();
bindGroupEntry.offset = offsets[shaderType];
bindGroupEntry.size = mDefaultUniformBlocks[shaderType]->uniformData.size();
bindings.push_back(bindGroupEntry);
}
};
// Add the BindGroupEntry for the default blocks of both the vertex and fragment shaders.
// They will use the same buffer with a different offset.
addBindingToGroupIfNecessary(sh::kDefaultVertexUniformBlockBinding, gl::ShaderType::Vertex);
addBindingToGroupIfNecessary(sh::kDefaultFragmentUniformBlockBinding,
gl::ShaderType::Fragment);
// A bind group contains one or multiple bindings
wgpu::BindGroupDescriptor bindGroupDesc{};
bindGroupDesc.layout = mDefaultBindGroupLayout;
// There must be as many bindings as declared in the layout!
bindGroupDesc.entryCount = bindings.size();
bindGroupDesc.entries = bindings.data();
mDefaultBindGroup = contextWgpu->getDevice().CreateBindGroup(&bindGroupDesc);
}
ASSERT(mDefaultBindGroup);
*outBindGroup = mDefaultBindGroup;
return angle::Result::Continue;
}
angle::CheckedNumeric<size_t> ProgramExecutableWgpu::getDefaultUniformAlignedSize(
ContextWgpu *context,
gl::ShaderType shaderType) const
{
size_t alignment = angle::base::checked_cast<size_t>(
context->getDisplay()->getLimitsWgpu().minUniformBufferOffsetAlignment);
return CheckedRoundUp(mDefaultUniformBlocks[shaderType]->uniformData.size(), alignment);
}
angle::CheckedNumeric<size_t> ProgramExecutableWgpu::calcUniformUpdateRequiredSpace(
ContextWgpu *context,
gl::ShaderMap<uint64_t> *uniformOffsets) const
{
angle::CheckedNumeric<size_t> requiredSpace = 0;
for (gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
(*uniformOffsets)[shaderType] = requiredSpace.ValueOrDie();
requiredSpace += getDefaultUniformAlignedSize(context, shaderType);
if (!requiredSpace.IsValid())
{
break;
}
}
return requiredSpace;
}
angle::Result ProgramExecutableWgpu::resizeUniformBlockMemory(
const gl::ShaderMap<size_t> &requiredBufferSize)
{
for (gl::ShaderType shaderType : mExecutable->getLinkedShaderStages())
{
if (requiredBufferSize[shaderType] > 0)
{
if (!mDefaultUniformBlocks[shaderType]->uniformData.resize(
requiredBufferSize[shaderType]))
{
return angle::Result::Stop;
}
// Initialize uniform buffer memory to zero by default.
mDefaultUniformBlocks[shaderType]->uniformData.fill(0);
mDefaultUniformBlocksDirty.set(shaderType);
}
}
return angle::Result::Continue;
}
void ProgramExecutableWgpu::markDefaultUniformsDirty()
{
// Mark all linked stages as having dirty default uniforms
mDefaultUniformBlocksDirty = getExecutable()->getLinkedShaderStages();
}
void ProgramExecutableWgpu::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
{
SetUniform(mExecutable, location, count, v, GL_FLOAT, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
{
SetUniform(mExecutable, location, count, v, GL_FLOAT_VEC2, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
{
SetUniform(mExecutable, location, count, v, GL_FLOAT_VEC3, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
{
SetUniform(mExecutable, location, count, v, GL_FLOAT_VEC4, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform1iv(GLint location, GLsizei count, const GLint *v)
{
const gl::VariableLocation &locationInfo = mExecutable->getUniformLocations()[location];
const gl::LinkedUniform &linkedUniform = mExecutable->getUniforms()[locationInfo.index];
if (linkedUniform.isSampler())
{
// TODO(anglebug.com/42267100): handle samplers.
return;
}
SetUniform(mExecutable, location, count, v, GL_INT, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform2iv(GLint location, GLsizei count, const GLint *v)
{
SetUniform(mExecutable, location, count, v, GL_INT_VEC2, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform3iv(GLint location, GLsizei count, const GLint *v)
{
SetUniform(mExecutable, location, count, v, GL_INT_VEC3, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform4iv(GLint location, GLsizei count, const GLint *v)
{
SetUniform(mExecutable, location, count, v, GL_INT_VEC4, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
{
SetUniform(mExecutable, location, count, v, GL_UNSIGNED_INT, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
{
SetUniform(mExecutable, location, count, v, GL_UNSIGNED_INT_VEC2, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
{
SetUniform(mExecutable, location, count, v, GL_UNSIGNED_INT_VEC3, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
{
SetUniform(mExecutable, location, count, v, GL_UNSIGNED_INT_VEC4, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<2, 2>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<3, 3>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<4, 4>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix2x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<2, 3>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix3x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<3, 2>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix2x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<2, 4>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix4x2fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<4, 2>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix3x4fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<3, 4>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::setUniformMatrix4x3fv(GLint location,
GLsizei count,
GLboolean transpose,
const GLfloat *value)
{
SetUniformMatrixfv<4, 3>(mExecutable, location, count, transpose, value, &mDefaultUniformBlocks,
&mDefaultUniformBlocksDirty);
}
void ProgramExecutableWgpu::getUniformfv(const gl::Context *context,
GLint location,
GLfloat *params) const
{
GetUniform(mExecutable, location, params, GL_FLOAT, &mDefaultUniformBlocks);
}
void ProgramExecutableWgpu::getUniformiv(const gl::Context *context,
GLint location,
GLint *params) const
{
GetUniform(mExecutable, location, params, GL_INT, &mDefaultUniformBlocks);
}
void ProgramExecutableWgpu::getUniformuiv(const gl::Context *context,
GLint location,
GLuint *params) const
{
GetUniform(mExecutable, location, params, GL_UNSIGNED_INT, &mDefaultUniformBlocks);
}
TranslatedWGPUShaderModule &ProgramExecutableWgpu::getShaderModule(gl::ShaderType type)
{
return mShaderModules[type];
}
angle::Result ProgramExecutableWgpu::getRenderPipeline(ContextWgpu *context,
const webgpu::RenderPipelineDesc &desc,
wgpu::RenderPipeline *pipelineOut)
{
gl::ShaderMap<wgpu::ShaderModule> shaders;
for (gl::ShaderType shaderType : gl::AllShaderTypes())
{
shaders[shaderType] = mShaderModules[shaderType].module;
}
genBindingLayoutIfNecessary(context);
return mPipelineCache.getRenderPipeline(context, desc, mPipelineLayout, shaders, pipelineOut);
}
void ProgramExecutableWgpu::genBindingLayoutIfNecessary(ContextWgpu *context)
{
if (mPipelineLayout)
{
return;
}
// TODO(anglebug.com/42267100): for now, only create a wgpu::PipelineLayout with the default
// uniform block. Will need to be extended for driver uniforms, UBOs, and textures/samplers.
// Also, possibly provide this layout as a compilation hint to createShaderModule().
std::vector<wgpu::BindGroupLayoutEntry> bindGroupLayoutEntries;
auto addBindGroupLayoutEntryIfNecessary = [&](uint32_t bindingIndex, gl::ShaderType shaderType,
wgpu::ShaderStage wgpuVisibility) {
if (mDefaultUniformBlocks[shaderType]->uniformData.size() != 0)
{
wgpu::BindGroupLayoutEntry bindGroupLayoutEntry;
bindGroupLayoutEntry.visibility = wgpuVisibility;
bindGroupLayoutEntry.binding = bindingIndex;
bindGroupLayoutEntry.buffer.type = wgpu::BufferBindingType::Uniform;
// By setting a `minBindingSize`, some validation is pushed from every draw call to
// pipeline creation time.
bindGroupLayoutEntry.buffer.minBindingSize =
mDefaultUniformBlocks[shaderType]->uniformData.size();
bindGroupLayoutEntries.push_back(bindGroupLayoutEntry);
}
};
// Default uniform blocks for each of the vertex shader and the fragment shader.
addBindGroupLayoutEntryIfNecessary(sh::kDefaultVertexUniformBlockBinding,
gl::ShaderType::Vertex, wgpu::ShaderStage::Vertex);
addBindGroupLayoutEntryIfNecessary(sh::kDefaultFragmentUniformBlockBinding,
gl::ShaderType::Fragment, wgpu::ShaderStage::Fragment);
// Create a bind group layout with these entries.
wgpu::BindGroupLayoutDescriptor bindGroupLayoutDesc{};
bindGroupLayoutDesc.entryCount = bindGroupLayoutEntries.size();
bindGroupLayoutDesc.entries = bindGroupLayoutEntries.data();
mDefaultBindGroupLayout = context->getDevice().CreateBindGroupLayout(&bindGroupLayoutDesc);
// Create the pipeline layout. This is a list where each element N corresponds to the @group(N)
// in the compiled shaders.
wgpu::PipelineLayoutDescriptor layoutDesc{};
layoutDesc.bindGroupLayoutCount = 1;
layoutDesc.bindGroupLayouts = &mDefaultBindGroupLayout;
mPipelineLayout = context->getDevice().CreatePipelineLayout(&layoutDesc);
}
} // namespace rx