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
//
// 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.
//
// BufferWgpu.cpp:
// Implements the class methods for BufferWgpu.
//
#include "libANGLE/renderer/wgpu/BufferWgpu.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
namespace rx
{
namespace
{
// Based on a buffer binding target, compute the default wgpu usage flags. More can be added if the
// buffer is used in new ways.
wgpu::BufferUsage GetDefaultWGPUBufferUsageForBinding(gl::BufferBinding binding)
{
switch (binding)
{
case gl::BufferBinding::Array:
case gl::BufferBinding::ElementArray:
return wgpu::BufferUsage::Vertex | wgpu::BufferUsage::Index |
wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst;
case gl::BufferBinding::Uniform:
return wgpu::BufferUsage::Uniform | wgpu::BufferUsage::CopySrc |
wgpu::BufferUsage::CopyDst;
case gl::BufferBinding::PixelPack:
return wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst;
case gl::BufferBinding::PixelUnpack:
return wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc;
case gl::BufferBinding::CopyRead:
case gl::BufferBinding::CopyWrite:
case gl::BufferBinding::ShaderStorage:
case gl::BufferBinding::Texture:
case gl::BufferBinding::TransformFeedback:
case gl::BufferBinding::DispatchIndirect:
case gl::BufferBinding::DrawIndirect:
case gl::BufferBinding::AtomicCounter:
UNIMPLEMENTED();
return wgpu::BufferUsage::None;
default:
UNREACHABLE();
return wgpu::BufferUsage::None;
}
}
} // namespace
BufferWgpu::BufferWgpu(const gl::BufferState &state) : BufferImpl(state) {}
BufferWgpu::~BufferWgpu() {}
angle::Result BufferWgpu::setData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
gl::BufferUsage usage)
{
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
wgpu::Device device = webgpu::GetDevice(context);
bool hasData = data && size > 0;
// Allocate a new buffer if the current one is invalid, the size is different, or the current
// buffer cannot be mapped for writing when data needs to be uploaded.
if (!mBuffer.valid() || mBuffer.requestedSize() != size ||
(hasData && !mBuffer.canMapForWrite()))
{
// Allocate a new buffer
ANGLE_TRY(mBuffer.initBuffer(device, size, GetDefaultWGPUBufferUsageForBinding(target),
webgpu::MapAtCreation::Yes));
}
if (hasData)
{
ASSERT(mBuffer.canMapForWrite());
if (!mBuffer.getMappedState().has_value())
{
ANGLE_TRY(mBuffer.mapImmediate(contextWgpu, wgpu::MapMode::Write, 0, size));
}
uint8_t *mappedData = mBuffer.getMapWritePointer(0, size);
memcpy(mappedData, data, size);
}
return angle::Result::Continue;
}
angle::Result BufferWgpu::setSubData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
size_t offset)
{
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
wgpu::Device device = webgpu::GetDevice(context);
ASSERT(mBuffer.valid());
if (mBuffer.canMapForWrite())
{
if (!mBuffer.getMappedState().has_value())
{
ANGLE_TRY(mBuffer.mapImmediate(contextWgpu, wgpu::MapMode::Write, offset, size));
}
uint8_t *mappedData = mBuffer.getMapWritePointer(offset, size);
memcpy(mappedData, data, size);
}
else
{
// TODO: Upload into a staging buffer and copy to the destination buffer so that the copy
// happens at the right point in time for command buffer recording.
wgpu::Queue &queue = contextWgpu->getQueue();
queue.WriteBuffer(mBuffer.getBuffer(), offset, data, size);
}
return angle::Result::Continue;
}
angle::Result BufferWgpu::copySubData(const gl::Context *context,
BufferImpl *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
return angle::Result::Continue;
}
angle::Result BufferWgpu::map(const gl::Context *context, GLenum access, void **mapPtr)
{
return angle::Result::Continue;
}
angle::Result BufferWgpu::mapRange(const gl::Context *context,
size_t offset,
size_t length,
GLbitfield access,
void **mapPtr)
{
return angle::Result::Continue;
}
angle::Result BufferWgpu::unmap(const gl::Context *context, GLboolean *result)
{
*result = GL_TRUE;
return angle::Result::Continue;
}
angle::Result BufferWgpu::getIndexRange(const gl::Context *context,
gl::DrawElementsType type,
size_t offset,
size_t count,
bool primitiveRestartEnabled,
gl::IndexRange *outRange)
{
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
wgpu::Device device = webgpu::GetDevice(context);
if (mBuffer.getMappedState())
{
ANGLE_TRY(mBuffer.unmap());
}
// Create a staging buffer just big enough for this index range
const GLuint typeBytes = gl::GetDrawElementsTypeSize(type);
const size_t stagingBufferSize =
roundUpPow2(count * typeBytes, webgpu::kBufferCopyToBufferAlignment);
webgpu::BufferHelper stagingBuffer;
ANGLE_TRY(stagingBuffer.initBuffer(device, stagingBufferSize,
wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead,
webgpu::MapAtCreation::No));
// Copy the source buffer to staging and flush the commands
contextWgpu->ensureCommandEncoderCreated();
wgpu::CommandEncoder &commandEncoder = contextWgpu->getCurrentCommandEncoder();
commandEncoder.CopyBufferToBuffer(mBuffer.getBuffer(), offset, stagingBuffer.getBuffer(), 0,
stagingBufferSize);
ANGLE_TRY(contextWgpu->flush(webgpu::RenderPassClosureReason::IndexRangeReadback));
// Read back from the staging buffer and compute the index range
ANGLE_TRY(stagingBuffer.mapImmediate(contextWgpu, wgpu::MapMode::Read, 0, stagingBufferSize));
const uint8_t *data = stagingBuffer.getMapReadPointer(0, stagingBufferSize);
*outRange = gl::ComputeIndexRange(type, data, count, primitiveRestartEnabled);
ANGLE_TRY(stagingBuffer.unmap());
return angle::Result::Continue;
}
} // namespace rx