Hash :
84e54d88
Author :
Date :
2024-07-22T14:39:22
WebGPU: Add command buffers and renderpass management Add a CommandBuffer class to serialize and replay WebGPU commands. Only Draw and SetPipeline are implemented in this patch. Manage render pass begin and end events due to framebuffer changes and swapping. Handle the color mask dirty bits so that a non-zero color mask will be used. All togther, this is enough to draw a triangle using a hard-coded shader without inputs. Bug: angleproject:0 Change-Id: I0fbf0296563c02c7f0774ad4197b83f4c93c22bb Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5731594 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Liza Burakova <liza@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
//
// 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
{
uint64_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
{
uint64_t pad;
};
struct SetBlendConstantCommand
{
uint64_t pad;
};
struct SetIndexBufferCommand
{
uint64_t pad;
};
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
{
uint64_t pad;
};
struct SetStencilReferenceCommand
{
uint64_t pad;
};
struct SetVertexBufferCommand
{
uint64_t pad;
};
struct SetViewportCommand
{
uint64_t pad;
};
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 setPipeline(wgpu::RenderPipeline pipeline);
void clear();
bool hasCommands() const { return mCommandCount > 0; }
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;
// std::unordered_set required because it does not move elements and stored command reference
// addresses in the set
std::unordered_set<wgpu::RenderPipeline> mReferencedRenderPipelines;
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_