Hash :
37dd8e92
Author :
Date :
2024-09-20T17:49:11
WebGPU: Stream client arrays Add support for vertex attributes and index data without buffer backings. Stream the data to buffers at draw time. Bug: angleproject:368602384 Change-Id: I697b7882cdebf0dfab08fcffabd2d36048920547 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5878137 Reviewed-by: Shahbaz Youssefi <syoussefi@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.
//
// VertexArrayWgpu.cpp:
// Implements the class methods for VertexArrayWgpu.
//
#include "libANGLE/renderer/wgpu/VertexArrayWgpu.h"
#include "common/debug.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
namespace rx
{
VertexArrayWgpu::VertexArrayWgpu(const gl::VertexArrayState &data) : VertexArrayImpl(data)
{
// Pre-initialize mCurrentIndexBuffer to a streaming buffer because no index buffer dirty bit is
// triggered if our first draw call has no buffer bound.
mCurrentIndexBuffer = &mStreamingIndexBuffer;
}
angle::Result VertexArrayWgpu::syncState(const gl::Context *context,
const gl::VertexArray::DirtyBits &dirtyBits,
gl::VertexArray::DirtyAttribBitsArray *attribBits,
gl::VertexArray::DirtyBindingBitsArray *bindingBits)
{
ASSERT(dirtyBits.any());
ContextWgpu *contextWgpu = GetImplAs<ContextWgpu>(context);
const std::vector<gl::VertexAttribute> &attribs = mState.getVertexAttributes();
const std::vector<gl::VertexBinding> &bindings = mState.getVertexBindings();
gl::AttributesMask syncedAttributes;
for (auto iter = dirtyBits.begin(), endIter = dirtyBits.end(); iter != endIter; ++iter)
{
size_t dirtyBit = *iter;
switch (dirtyBit)
{
case gl::VertexArray::DIRTY_BIT_LOST_OBSERVATION:
break;
case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER:
ANGLE_TRY(syncDirtyElementArrayBuffer(contextWgpu));
contextWgpu->invalidateIndexBuffer();
break;
case gl::VertexArray::DIRTY_BIT_ELEMENT_ARRAY_BUFFER_DATA:
break;
#define ANGLE_VERTEX_DIRTY_ATTRIB_FUNC(INDEX) \
case gl::VertexArray::DIRTY_BIT_ATTRIB_0 + INDEX: \
ANGLE_TRY(syncDirtyAttrib(contextWgpu, attribs[INDEX], \
bindings[attribs[INDEX].bindingIndex], INDEX)); \
(*attribBits)[INDEX].reset(); \
syncedAttributes.set(INDEX); \
break;
ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_ATTRIB_FUNC)
#define ANGLE_VERTEX_DIRTY_BINDING_FUNC(INDEX) \
case gl::VertexArray::DIRTY_BIT_BINDING_0 + INDEX: \
ANGLE_TRY(syncDirtyAttrib(contextWgpu, attribs[INDEX], \
bindings[attribs[INDEX].bindingIndex], INDEX)); \
(*bindingBits)[INDEX].reset(); \
syncedAttributes.set(INDEX); \
break;
ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BINDING_FUNC)
#define ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC(INDEX) \
case gl::VertexArray::DIRTY_BIT_BUFFER_DATA_0 + INDEX: \
ANGLE_TRY(syncDirtyAttrib(contextWgpu, attribs[INDEX], \
bindings[attribs[INDEX].bindingIndex], INDEX)); \
syncedAttributes.set(INDEX); \
break;
ANGLE_VERTEX_INDEX_CASES(ANGLE_VERTEX_DIRTY_BUFFER_DATA_FUNC)
default:
break;
}
}
for (size_t syncedAttribIndex : syncedAttributes)
{
contextWgpu->setVertexAttribute(syncedAttribIndex, mCurrentAttribs[syncedAttribIndex]);
contextWgpu->invalidateVertexBuffer(syncedAttribIndex);
}
return angle::Result::Continue;
}
angle::Result VertexArrayWgpu::syncClientArrays(const gl::Context *context,
const gl::AttributesMask &activeAttributesMask,
GLint first,
GLsizei count,
GLsizei instanceCount,
gl::DrawElementsType drawElementsTypeOrInvalid,
const void *indices,
GLint baseVertex,
bool primitiveRestartEnabled,
const void **adjustedIndicesPtr)
{
*adjustedIndicesPtr = indices;
gl::AttributesMask clientAttributesToSync = mState.getClientMemoryAttribsMask() &
mState.getEnabledAttributesMask() &
activeAttributesMask;
bool indexedDrawCallWithNoIndexBuffer =
drawElementsTypeOrInvalid != gl::DrawElementsType::InvalidEnum &&
!mState.getElementArrayBuffer();
if (!clientAttributesToSync.any() && !indexedDrawCallWithNoIndexBuffer)
{
return angle::Result::Continue;
}
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
wgpu::Device device = webgpu::GetDevice(context);
// If any attributes need to be streamed, we need to know the index range.
std::optional<gl::IndexRange> indexRange;
if (clientAttributesToSync.any())
{
GLint startVertex = 0;
size_t vertexCount = 0;
ANGLE_TRY(GetVertexRangeInfo(context, first, count, drawElementsTypeOrInvalid, indices,
baseVertex, &startVertex, &vertexCount));
indexRange = gl::IndexRange(startVertex, startVertex + vertexCount - 1, 0);
}
// Pre-compute the total size of all streamed vertex and index data so a single staging buffer
// can be used
size_t stagingBufferSize = 0;
std::optional<size_t> indexDataSize;
if (indexedDrawCallWithNoIndexBuffer)
{
indexDataSize = gl::GetDrawElementsTypeSize(drawElementsTypeOrInvalid) * count;
stagingBufferSize +=
rx::roundUpPow2(indexDataSize.value(), webgpu::kBufferCopyToBufferAlignment);
}
const std::vector<gl::VertexAttribute> &attribs = mState.getVertexAttributes();
const std::vector<gl::VertexBinding> &bindings = mState.getVertexBindings();
if (clientAttributesToSync.any())
{
for (size_t attribIndex : clientAttributesToSync)
{
const gl::VertexAttribute &attrib = attribs[attribIndex];
const gl::VertexBinding &binding = bindings[attrib.bindingIndex];
size_t typeSize = gl::ComputeVertexAttributeTypeSize(attrib);
size_t attribSize =
typeSize * gl::ComputeVertexBindingElementCount(
binding.getDivisor(), indexRange->vertexCount(), instanceCount);
stagingBufferSize += rx::roundUpPow2(attribSize, webgpu::kBufferCopyToBufferAlignment);
}
}
ASSERT(stagingBufferSize > 0);
ASSERT(stagingBufferSize % webgpu::kBufferSizeAlignment == 0);
webgpu::BufferHelper stagingBuffer;
ANGLE_TRY(stagingBuffer.initBuffer(device, stagingBufferSize,
wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::MapWrite,
webgpu::MapAtCreation::Yes));
struct BufferCopy
{
uint64_t sourceOffset;
webgpu::BufferHelper *dest;
uint64_t size;
};
std::vector<BufferCopy> stagingUploads;
uint8_t *stagingData = stagingBuffer.getMapWritePointer(0, stagingBufferSize);
size_t currentStagingDataPosition = 0;
auto ensureStreamingBufferCreated = [device](webgpu::BufferHelper &buffer, size_t size,
wgpu::BufferUsage usage, bool *outNewBuffer) {
if (buffer.valid() && buffer.requestedSize() >= size)
{
ASSERT(buffer.getBuffer().GetUsage() == usage);
*outNewBuffer = false;
return angle::Result::Continue;
}
ANGLE_TRY(buffer.initBuffer(device, size, usage, webgpu::MapAtCreation::No));
*outNewBuffer = true;
return angle::Result::Continue;
};
if (indexedDrawCallWithNoIndexBuffer)
{
ASSERT(indexDataSize.has_value());
memcpy(stagingData + currentStagingDataPosition, indices, indexDataSize.value());
size_t copySize =
rx::roundUpPow2(indexDataSize.value(), webgpu::kBufferCopyToBufferAlignment);
bool newBuffer = false;
ANGLE_TRY(ensureStreamingBufferCreated(
mStreamingIndexBuffer, copySize, wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Index,
&newBuffer));
if (newBuffer)
{
contextWgpu->invalidateIndexBuffer();
}
stagingUploads.push_back({currentStagingDataPosition, &mStreamingIndexBuffer, copySize});
currentStagingDataPosition += copySize;
// Indices are streamed to the start of the buffer. Tell the draw call command to use 0 for
// firstIndex.
*adjustedIndicesPtr = 0;
}
for (size_t attribIndex : clientAttributesToSync)
{
const gl::VertexAttribute &attrib = attribs[attribIndex];
const gl::VertexBinding &binding = bindings[attrib.bindingIndex];
size_t streamedVertexCount = gl::ComputeVertexBindingElementCount(
binding.getDivisor(), indexRange->vertexCount(), instanceCount);
const size_t sourceStride = ComputeVertexAttributeStride(attrib, binding);
const size_t typeSize = gl::ComputeVertexAttributeTypeSize(attrib);
// Vertices do not apply the 'start' offset when the divisor is non-zero even when doing
// a non-instanced draw call
const size_t firstIndex = (binding.getDivisor() == 0) ? indexRange->start : 0;
// Attributes using client memory ignore the VERTEX_ATTRIB_BINDING state.
// https://www.opengl.org/registry/specs/ARB/vertex_attrib_binding.txt
const uint8_t *inputPointer = static_cast<const uint8_t *>(attrib.pointer);
// Pack the data when copying it, user could have supplied a very large stride that
// would cause the buffer to be much larger than needed.
if (typeSize == sourceStride)
{
// Can copy in one go, the data is packed
memcpy(stagingData + currentStagingDataPosition,
inputPointer + (sourceStride * firstIndex), streamedVertexCount * typeSize);
}
else
{
for (size_t vertexIdx = 0; vertexIdx < streamedVertexCount; vertexIdx++)
{
uint8_t *out = stagingData + currentStagingDataPosition + (typeSize * vertexIdx);
const uint8_t *in = inputPointer + sourceStride * (vertexIdx + firstIndex);
memcpy(out, in, typeSize);
}
}
size_t copySize =
rx::roundUpPow2(streamedVertexCount * typeSize, webgpu::kBufferCopyToBufferAlignment);
bool newBuffer = false;
ANGLE_TRY(ensureStreamingBufferCreated(
mStreamingArrayBuffers[attribIndex], copySize,
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::Vertex, &newBuffer));
if (newBuffer)
{
contextWgpu->invalidateVertexBuffer(attribIndex);
}
stagingUploads.push_back(
{currentStagingDataPosition, &mStreamingArrayBuffers[attribIndex], copySize});
currentStagingDataPosition += copySize;
}
ANGLE_TRY(stagingBuffer.unmap());
ANGLE_TRY(contextWgpu->flush(webgpu::RenderPassClosureReason::VertexArrayStreaming));
contextWgpu->ensureCommandEncoderCreated();
wgpu::CommandEncoder &commandEncoder = contextWgpu->getCurrentCommandEncoder();
for (const BufferCopy © : stagingUploads)
{
commandEncoder.CopyBufferToBuffer(stagingBuffer.getBuffer(), copy.sourceOffset,
copy.dest->getBuffer(), 0, copy.size);
}
return angle::Result::Continue;
}
angle::Result VertexArrayWgpu::syncDirtyAttrib(ContextWgpu *contextWgpu,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
size_t attribIndex)
{
if (attrib.enabled)
{
SetBitField(mCurrentAttribs[attribIndex].enabled, true);
const webgpu::Format &webgpuFormat =
contextWgpu->getFormat(attrib.format->glInternalFormat);
SetBitField(mCurrentAttribs[attribIndex].format, webgpuFormat.getActualWgpuVertexFormat());
SetBitField(mCurrentAttribs[attribIndex].shaderLocation, attribIndex);
SetBitField(mCurrentAttribs[attribIndex].stride, binding.getStride());
gl::Buffer *bufferGl = binding.getBuffer().get();
if (bufferGl && bufferGl->getSize() > 0)
{
SetBitField(mCurrentAttribs[attribIndex].offset,
reinterpret_cast<uintptr_t>(attrib.pointer));
BufferWgpu *bufferWgpu = webgpu::GetImpl(bufferGl);
mCurrentArrayBuffers[attribIndex] = &(bufferWgpu->getBuffer());
}
else
{
SetBitField(mCurrentAttribs[attribIndex].offset, 0);
mCurrentArrayBuffers[attribIndex] = &mStreamingArrayBuffers[attribIndex];
}
}
else
{
memset(&mCurrentAttribs[attribIndex], 0, sizeof(webgpu::PackedVertexAttribute));
mCurrentArrayBuffers[attribIndex] = nullptr;
}
return angle::Result::Continue;
}
angle::Result VertexArrayWgpu::syncDirtyElementArrayBuffer(ContextWgpu *contextWgpu)
{
gl::Buffer *bufferGl = mState.getElementArrayBuffer();
if (bufferGl)
{
BufferWgpu *buffer = webgpu::GetImpl(bufferGl);
mCurrentIndexBuffer = &buffer->getBuffer();
}
else
{
mCurrentIndexBuffer = &mStreamingIndexBuffer;
}
return angle::Result::Continue;
}
} // namespace rx