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
//
// 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_HELPERS_H_
#define LIBANGLE_RENDERER_WGPU_WGPU_HELPERS_H_
#include <dawn/webgpu_cpp.h>
#include <stdint.h>
#include "libANGLE/Error.h"
#include "libANGLE/ImageIndex.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
namespace rx
{
class ContextWgpu;
namespace webgpu
{
struct QueuedDataUpload
{
wgpu::ImageCopyBuffer copyBuffer;
gl::LevelIndex targetLevel;
};
// Stores subset of information required to create a wgpu::Texture
struct TextureInfo
{
wgpu::TextureUsage usage = wgpu::TextureUsage::None;
wgpu::TextureDimension dimension;
uint32_t mipLevelCount;
};
wgpu::TextureDimension toWgpuTextureDimension(gl::TextureType glTextureType);
class ImageHelper
{
public:
ImageHelper();
~ImageHelper();
angle::Result 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);
void flushStagedUpdates(wgpu::Device &device);
LevelIndex toWgpuLevel(gl::LevelIndex levelIndexGl) const;
gl::LevelIndex toGlLevel(LevelIndex levelIndexWgpu) const;
wgpu::Texture &getTexture() { return mTexture; }
TextureInfo getWgpuTextureInfo(const gl::ImageIndex &index);
wgpu::TextureFormat toWgpuTextureFormat() const { return mTextureDescriptor.format; }
const wgpu::TextureDescriptor &getTextureDescriptor() const { return mTextureDescriptor; }
gl::LevelIndex getFirstAllocatedLevel() { return mFirstAllocatedLevel; }
private:
wgpu::Texture mTexture;
wgpu::TextureDescriptor mTextureDescriptor = {};
gl::LevelIndex mFirstAllocatedLevel;
std::vector<QueuedDataUpload> mBufferQueue;
};
struct BufferMapState
{
wgpu::MapMode mode;
size_t offset;
size_t size;
};
enum class MapAtCreation
{
No,
Yes,
};
class BufferHelper : public angle::NonCopyable
{
public:
BufferHelper();
~BufferHelper();
bool valid() const { return mBuffer != nullptr; }
void reset();
angle::Result initBuffer(wgpu::Device device,
size_t size,
wgpu::BufferUsage usage,
MapAtCreation mappedAtCreation);
angle::Result mapImmediate(ContextWgpu *context,
wgpu::MapMode mode,
size_t offset,
size_t size);
angle::Result unmap();
uint8_t *getMapWritePointer(size_t offset, size_t size) const;
const std::optional<BufferMapState> &getMappedState() const;
bool canMapForRead() const;
bool canMapForWrite() const;
wgpu::Buffer &getBuffer();
uint64_t size() const;
private:
wgpu::Buffer mBuffer;
std::optional<BufferMapState> mMappedState;
};
} // namespace webgpu
} // namespace rx
#endif // LIBANGLE_RENDERER_WGPU_WGPU_HELPERS_H_