Hash :
f4485224
Author :
Date :
2024-04-05T10:29:23
WebGPU: Implement glBufferData and glBufferSubData Define several usages for WebGPU buffers that map to wgpu buffer usage flags. Maintain a set of buffers by usage and a serial of the one with the most recent data. Defer creation of the buffer as long as possible to take advantage of the WebGPU mapAtCreation flag for data upload. If we ever have to unmap these buffers, staging buffers must be used to upload afterwards. Add some helpers for getting Device and Instance from a gl::Context. Bug: angleproject:8654 Change-Id: Ibb8147119af8a98738fc4d579830a02ccaa1d7c5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5426813 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Liza Burakova <liza@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
//
// 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.
//
#include "libANGLE/renderer/wgpu/wgpu_helpers.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/DisplayWgpu.h"
namespace rx
{
namespace webgpu
{
ImageHelper::ImageHelper() {}
ImageHelper::~ImageHelper() {}
angle::Result ImageHelper::initImage(wgpu::Device &device,
wgpu::TextureUsage usage,
wgpu::TextureDimension dimension,
wgpu::Extent3D size,
wgpu::TextureFormat format,
std::uint32_t mipLevelCount,
std::uint32_t sampleCount,
std::size_t viewFormatCount)
{
mTextureDescriptor.usage = usage;
mTextureDescriptor.dimension = dimension;
mTextureDescriptor.size = size;
mTextureDescriptor.format = format;
mTextureDescriptor.mipLevelCount = mipLevelCount;
mTextureDescriptor.sampleCount = sampleCount;
mTextureDescriptor.viewFormatCount = viewFormatCount;
mTexture = device.CreateTexture(&mTextureDescriptor);
return angle::Result::Continue;
}
void ImageHelper::flushStagedUpdates(wgpu::Device &device)
{
wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
wgpu::ImageCopyTexture dst;
for (const QueuedDataUpload &src : mBufferQueue)
{
if (src.targetLevel < mFirstAllocatedLevel ||
src.targetLevel >= (mFirstAllocatedLevel + mTextureDescriptor.mipLevelCount))
{
continue;
}
LevelIndex targetLevelWgpu = toWgpuLevel(src.targetLevel);
dst.texture = mTexture;
dst.mipLevel = targetLevelWgpu.get();
encoder.CopyBufferToTexture(&src.copyBuffer, &dst, &mTextureDescriptor.size);
}
}
LevelIndex ImageHelper::toWgpuLevel(gl::LevelIndex levelIndexGl) const
{
return gl_wgpu::getLevelIndex(levelIndexGl, mFirstAllocatedLevel);
}
gl::LevelIndex ImageHelper::toGlLevel(LevelIndex levelIndexWgpu) const
{
return wgpu_gl::getLevelIndex(levelIndexWgpu, mFirstAllocatedLevel);
}
TextureInfo ImageHelper::getWgpuTextureInfo(const gl::ImageIndex &index)
{
TextureInfo textureInfo;
textureInfo.dimension = gl_wgpu::getWgpuTextureDimension(index.getType());
textureInfo.mipLevelCount = index.getLayerCount();
return textureInfo;
}
BufferHelper::BufferHelper() {}
BufferHelper::~BufferHelper() {}
void BufferHelper::reset()
{
mBuffer = nullptr;
mMappedState.reset();
}
angle::Result BufferHelper::initBuffer(wgpu::Device device,
size_t size,
wgpu::BufferUsage usage,
MapAtCreation mappedAtCreation)
{
wgpu::BufferDescriptor descriptor;
descriptor.size = size;
descriptor.usage = usage;
descriptor.mappedAtCreation = mappedAtCreation == MapAtCreation::Yes;
mBuffer = device.CreateBuffer(&descriptor);
if (mappedAtCreation == MapAtCreation::Yes)
{
mMappedState = {wgpu::MapMode::Read | wgpu::MapMode::Write, 0, size};
}
else
{
mMappedState.reset();
}
return angle::Result::Continue;
}
angle::Result BufferHelper::mapImmediate(ContextWgpu *context,
wgpu::MapMode mode,
size_t offset,
size_t size)
{
ASSERT(!mMappedState.has_value());
WGPUBufferMapAsyncStatus mapResult = WGPUBufferMapAsyncStatus_Unknown;
wgpu::BufferMapCallbackInfo callbackInfo;
callbackInfo.mode = wgpu::CallbackMode::WaitAnyOnly;
callbackInfo.callback = [](WGPUBufferMapAsyncStatus status, void *userdata) {
*static_cast<WGPUBufferMapAsyncStatus *>(userdata) = status;
};
callbackInfo.userdata = &mapResult;
wgpu::FutureWaitInfo waitInfo;
waitInfo.future = mBuffer.MapAsync(mode, offset, size, callbackInfo);
wgpu::Instance instance = context->getDisplay()->getInstance();
ANGLE_WGPU_TRY(context, instance.WaitAny(1, &waitInfo, -1));
ANGLE_WGPU_TRY(context, mapResult);
ASSERT(waitInfo.completed);
mMappedState = {mode, offset, size};
return angle::Result::Continue;
}
angle::Result BufferHelper::unmap()
{
ASSERT(mMappedState.has_value());
mBuffer.Unmap();
mMappedState.reset();
return angle::Result::Continue;
}
uint8_t *BufferHelper::getMapWritePointer(size_t offset, size_t size) const
{
ASSERT(mBuffer.GetMapState() == wgpu::BufferMapState::Mapped);
ASSERT(mMappedState.has_value());
ASSERT(mMappedState->offset <= offset);
ASSERT(mMappedState->offset + mMappedState->size >= offset + size);
void *mapPtr = mBuffer.GetMappedRange(offset, size);
ASSERT(mapPtr);
return static_cast<uint8_t *>(mapPtr);
}
const std::optional<BufferMapState> &BufferHelper::getMappedState() const
{
return mMappedState;
}
bool BufferHelper::canMapForRead() const
{
return (mMappedState.has_value() && (mMappedState->mode & wgpu::MapMode::Read)) ||
(mBuffer && (mBuffer.GetUsage() & wgpu::BufferUsage::MapRead));
}
bool BufferHelper::canMapForWrite() const
{
return (mMappedState.has_value() && (mMappedState->mode & wgpu::MapMode::Write)) ||
(mBuffer && (mBuffer.GetUsage() & wgpu::BufferUsage::MapWrite));
}
wgpu::Buffer &BufferHelper::getBuffer()
{
return mBuffer;
}
uint64_t BufferHelper::size() const
{
return mBuffer ? mBuffer.GetSize() : 0;
}
} // namespace webgpu
} // namespace rx