Hash :
e746fb06
Author :
Date :
2025-01-23T13:23:30
WebGPU: Implement blending Constant alpha is not handled yet. It needs to be emualted or added to WebGPU. Bug: angleproject:370690274 Change-Id: I5f8402f74ab08345b2f02c18e2d43fa39b2479a7 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6194554 Reviewed-by: Liza Burakova <liza@chromium.org> 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
//
// 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 LIBANGLE_RENDERER_WGPU_WGPU_COMMAND_BUFFER_H_
#define LIBANGLE_RENDERER_WGPU_WGPU_COMMAND_BUFFER_H_
#include "common/debug.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
#include <dawn/webgpu_cpp.h>
#include <unordered_set>
namespace rx
{
namespace webgpu
{
#define ANGLE_WGPU_COMMANDS_X(PROC) \
PROC(BeginOcclusionQuery) \
PROC(Draw) \
PROC(DrawIndexed) \
PROC(DrawIndexedIndirect) \
PROC(DrawIndirect) \
PROC(End) \
PROC(EndOcclusionQuery) \
PROC(ExecuteBundles) \
PROC(InsertDebugMarker) \
PROC(PixelLocalStorageBarrier) \
PROC(PopDebugGroup) \
PROC(PushDebugGroup) \
PROC(SetBindGroup) \
PROC(SetBlendConstant) \
PROC(SetIndexBuffer) \
PROC(SetLabel) \
PROC(SetPipeline) \
PROC(SetScissorRect) \
PROC(SetStencilReference) \
PROC(SetVertexBuffer) \
PROC(SetViewport) \
PROC(WriteTimestamp)
#define WGPU_DECLARE_COMMAND_ID(CMD) CMD,
enum class CommandID : uint8_t
{
Invalid = 0,
ANGLE_WGPU_COMMANDS_X(WGPU_DECLARE_COMMAND_ID)
};
ANGLE_ENABLE_STRUCT_PADDING_WARNINGS
struct BeginOcclusionQueryCommand
{
uint64_t pad;
};
struct DrawCommand
{
uint32_t vertexCount;
uint32_t instanceCount;
uint32_t firstVertex;
uint32_t firstInstance;
};
struct DrawIndexedCommand
{
uint32_t indexCount;
uint32_t instanceCount;
uint32_t firstIndex;
uint32_t baseVertex;
uint32_t firstInstance;
uint32_t pad;
};
struct DrawIndexedIndirectCommand
{
uint64_t pad;
};
struct DrawIndirectCommand
{
uint64_t pad;
};
struct EndCommand
{
uint64_t pad;
};
struct EndOcclusionQueryCommand
{
uint64_t pad;
};
struct ExecuteBundlesCommand
{
uint64_t pad;
};
struct InsertDebugMarkerCommand
{
uint64_t pad;
};
struct PixelLocalStorageBarrierCommand
{
uint64_t pad;
};
struct PopDebugGroupCommand
{
uint64_t pad;
};
struct PushDebugGroupCommand
{
uint64_t pad;
};
struct SetBindGroupCommand
{
uint32_t groupIndex;
uint32_t pad0;
union
{
const wgpu::BindGroup *bindGroup;
uint64_t pad1; // Pad to 64 bits on 32-bit systems
};
};
struct SetBlendConstantCommand
{
float r;
float g;
float b;
float a;
};
struct SetIndexBufferCommand
{
union
{
const wgpu::Buffer *buffer;
uint64_t pad0; // Pad to 64 bits on 32-bit systems
};
wgpu::IndexFormat format;
uint32_t pad1;
uint64_t offset;
uint64_t size;
};
struct SetLabelCommand
{
uint64_t pad;
};
struct SetPipelineCommand
{
union
{
const wgpu::RenderPipeline *pipeline;
uint64_t padding; // Pad to 64 bits on 32-bit systems
};
};
struct SetScissorRectCommand
{
uint32_t x;
uint32_t y;
uint32_t width;
uint32_t height;
};
struct SetStencilReferenceCommand
{
uint64_t pad;
};
struct SetVertexBufferCommand
{
uint32_t slot;
uint32_t pad0;
union
{
const wgpu::Buffer *buffer;
uint64_t pad1; // Pad to 64 bits on 32-bit systems
};
uint64_t offset;
uint64_t size;
};
struct SetViewportCommand
{
float x;
float y;
float width;
float height;
float minDepth;
float maxDepth;
};
struct WriteTimestampCommand
{
uint64_t pad;
};
ANGLE_DISABLE_STRUCT_PADDING_WARNINGS
#define VERIFY_COMMAND_8_BYTE_ALIGNMENT(CMD) \
static_assert((sizeof(CMD##Command) % 8) == 0, "Check " #CMD "Command alignment");
ANGLE_WGPU_COMMANDS_X(VERIFY_COMMAND_8_BYTE_ALIGNMENT)
#undef VERIFY_COMMAND_8_BYTE_ALIGNMENT
template <CommandID>
struct CommandTypeHelper;
#define ANGLE_WGPU_COMMAND_TYPE_HELPER(CMD) \
template <> \
struct CommandTypeHelper<CommandID::CMD> \
{ \
using CommandType = CMD##Command; \
};
ANGLE_WGPU_COMMANDS_X(ANGLE_WGPU_COMMAND_TYPE_HELPER)
#undef ANGLE_WGPU_COMMAND_TYPE_HELPER
static constexpr size_t kCommandBlockSize = 1 << 14; // 16kB
class CommandBuffer
{
public:
CommandBuffer();
void draw(uint32_t vertexCount,
uint32_t instanceCount,
uint32_t firstVertex,
uint32_t firstInstance);
void drawIndexed(uint32_t indexCount,
uint32_t instanceCount,
uint32_t firstIndex,
int32_t baseVertex,
uint32_t firstInstance);
void setBindGroup(uint32_t groupIndex, wgpu::BindGroup bindGroup);
void setBlendConstant(float r, float g, float b, float a);
void setPipeline(wgpu::RenderPipeline pipeline);
void setScissorRect(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
void setViewport(float x, float y, float width, float height, float minDepth, float maxDepth);
void setIndexBuffer(wgpu::Buffer buffer,
wgpu::IndexFormat format,
uint64_t offset,
uint64_t size);
void setVertexBuffer(uint32_t slot, wgpu::Buffer buffer, uint64_t offset, uint64_t size);
void clear();
bool hasCommands() const { return mCommandCount > 0; }
bool hasSetScissorCommand() const { return mHasSetScissorCommand; }
bool hasSetViewportCommand() const { return mHasSetViewportCommand; }
bool hasSetBlendConstantCommand() const { return mHasSetBlendConstantCommand; }
void recordCommands(wgpu::RenderPassEncoder encoder);
private:
struct CommandBlock
{
static constexpr size_t kCommandBlockDataSize = kCommandBlockSize - (sizeof(size_t) * 2);
uint8_t mData[kCommandBlockDataSize] = {0};
size_t mCurrentPosition = 0;
static constexpr size_t kCommandIDSize = sizeof(CommandID);
static constexpr size_t kCommandBlockInitialRemainingSize =
kCommandBlockDataSize - kCommandIDSize; // Leave room for one command ID at the end to
// signify the end of the list
size_t mRemainingSize = kCommandBlockInitialRemainingSize;
void clear();
void finalize();
template <typename T>
T *getDataAtCurrentPositionAndReserveSpace(size_t space)
{
T *data = reinterpret_cast<T *>(&mData[mCurrentPosition]);
ASSERT(mRemainingSize >= space);
mCurrentPosition += space;
mRemainingSize -= space;
return data;
}
};
static constexpr size_t kCommandBlockStructSize = sizeof(CommandBlock);
static_assert(kCommandBlockStructSize == kCommandBlockSize, "Size mismatch");
std::vector<std::unique_ptr<CommandBlock>> mCommandBlocks;
size_t mCurrentCommandBlock = 0;
size_t mCommandCount = 0;
bool mHasSetScissorCommand = false;
bool mHasSetViewportCommand = false;
bool mHasSetBlendConstantCommand = false;
// std::unordered_set required because it does not move elements and stored command reference
// addresses in the set
std::unordered_set<wgpu::RenderPipeline> mReferencedRenderPipelines;
std::unordered_set<wgpu::Buffer> mReferencedBuffers;
std::unordered_set<wgpu::BindGroup> mReferencedBindGroups;
void nextCommandBlock();
void ensureCommandSpace(size_t space)
{
if (mCommandBlocks.empty() || mCommandBlocks[mCurrentCommandBlock]->mRemainingSize < space)
{
nextCommandBlock();
}
}
template <CommandID Command, typename CommandType = CommandTypeHelper<Command>::CommandType>
CommandType *initCommand()
{
constexpr size_t allocationSize = sizeof(CommandID) + sizeof(CommandType);
ensureCommandSpace(allocationSize);
CommandBlock *commandBlock = mCommandBlocks[mCurrentCommandBlock].get();
uint8_t *idAndCommandStorage =
commandBlock->getDataAtCurrentPositionAndReserveSpace<uint8_t>(allocationSize);
CommandID *id = reinterpret_cast<CommandID *>(idAndCommandStorage);
*id = Command;
CommandType *commandStruct =
reinterpret_cast<CommandType *>(idAndCommandStorage + sizeof(CommandID));
mCommandCount++;
return commandStruct;
}
};
} // namespace webgpu
} // namespace rx
#endif // LIBANGLE_RENDERER_WGPU_WGPU_COMMAND_BUFFER_H_