Hash :
bb14aa4d
Author :
Date :
2024-09-19T11:17:05
WebGPU: set vertex buffers. This change actually sets the vertex attributes that are added to the render pipeline description owned by the context, as well as sets the vertex buffer in the pipeline. Bug: angleproject:359823692 Change-Id: I5e94b357a4e6eadc1bbba54965cef94f90113b4e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5789155 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Geoff Lang <geofflang@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 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
//
// 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 <algorithm>
#include "libANGLE/Error.h"
#include "libANGLE/ImageIndex.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/wgpu_utils.h"
namespace rx
{
class ContextWgpu;
namespace webgpu
{
// WebGPU requires copy buffers bytesPerRow to be aligned to 256.
// https://www.w3.org/TR/webgpu/#abstract-opdef-validating-gpuimagecopybuffer
static const GLuint kCopyBufferAlignment = 256;
enum class UpdateSource
{
Clear,
Texture,
};
struct ClearUpdate
{
ClearValues clearValues;
bool hasDepth;
bool hasStencil;
};
struct SubresourceUpdate
{
SubresourceUpdate() {}
~SubresourceUpdate() {}
SubresourceUpdate(UpdateSource targetUpdateSource,
gl::LevelIndex newTargetLevel,
wgpu::ImageCopyBuffer targetBuffer)
{
updateSource = targetUpdateSource;
textureData = targetBuffer;
targetLevel = newTargetLevel;
}
SubresourceUpdate(UpdateSource targetUpdateSource,
gl::LevelIndex newTargetLevel,
ClearValues clearValues,
bool hasDepth,
bool hasStencil)
{
updateSource = targetUpdateSource;
targetLevel = newTargetLevel;
clearData.clearValues = clearValues;
clearData.hasDepth = hasDepth;
clearData.hasStencil = hasStencil;
}
UpdateSource updateSource;
ClearUpdate clearData;
wgpu::ImageCopyBuffer textureData;
gl::LevelIndex targetLevel;
};
wgpu::TextureDimension toWgpuTextureDimension(gl::TextureType glTextureType);
class ImageHelper
{
public:
ImageHelper();
~ImageHelper();
angle::Result initImage(angle::FormatID intendedFormatID,
angle::FormatID actualFormatID,
wgpu::Device &device,
gl::LevelIndex firstAllocatedLevel,
wgpu::TextureDescriptor textureDescriptor);
angle::Result initExternal(angle::FormatID intendedFormatID,
angle::FormatID actualFormatID,
wgpu::Texture externalTexture);
angle::Result flushStagedUpdates(ContextWgpu *contextWgpu);
angle::Result flushSingleLevelUpdates(ContextWgpu *contextWgpu,
gl::LevelIndex levelGL,
ClearValuesArray *deferredClears,
uint32_t deferredClearIndex);
wgpu::TextureDescriptor createTextureDescriptor(wgpu::TextureUsage usage,
wgpu::TextureDimension dimension,
wgpu::Extent3D size,
wgpu::TextureFormat format,
std::uint32_t mipLevelCount,
std::uint32_t sampleCount);
angle::Result stageTextureUpload(ContextWgpu *contextWgpu,
const webgpu::Format &webgpuFormat,
GLenum type,
const gl::Extents &glExtents,
GLuint inputRowPitch,
GLuint inputDepthPitch,
uint32_t outputRowPitch,
uint32_t outputDepthPitch,
uint32_t allocationSize,
const gl::ImageIndex &index,
const uint8_t *pixels);
void stageClear(gl::LevelIndex targetLevel,
ClearValues clearValues,
bool hasDepth,
bool hasStencil);
void removeStagedUpdates(gl::LevelIndex levelToRemove);
void resetImage();
static angle::Result getReadPixelsParams(rx::ContextWgpu *contextWgpu,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
GLenum format,
GLenum type,
const gl::Rectangle &area,
const gl::Rectangle &clippedArea,
rx::PackPixelsParams *paramsOut,
GLuint *skipBytesOut);
angle::Result readPixels(rx::ContextWgpu *contextWgpu,
const gl::Rectangle &area,
const rx::PackPixelsParams &packPixelsParams,
void *pixels);
angle::Result createTextureView(gl::LevelIndex targetLevel,
uint32_t layerIndex,
wgpu::TextureView &textureViewOut);
LevelIndex toWgpuLevel(gl::LevelIndex levelIndexGl) const;
gl::LevelIndex toGlLevel(LevelIndex levelIndexWgpu) const;
bool isTextureLevelInAllocatedImage(gl::LevelIndex textureLevel);
wgpu::Texture &getTexture() { return mTexture; }
wgpu::TextureFormat toWgpuTextureFormat() const { return mTextureDescriptor.format; }
angle::FormatID getIntendedFormatID() { return mIntendedFormatID; }
angle::FormatID getActualFormatID() { return mActualFormatID; }
const wgpu::TextureDescriptor &getTextureDescriptor() const { return mTextureDescriptor; }
gl::LevelIndex getFirstAllocatedLevel() { return mFirstAllocatedLevel; }
gl::LevelIndex getLastAllocatedLevel();
uint32_t getLevelCount() { return mTextureDescriptor.mipLevelCount; }
wgpu::Extent3D getSize() { return mTextureDescriptor.size; }
bool isInitialized() { return mInitialized; }
private:
void appendSubresourceUpdate(gl::LevelIndex level, SubresourceUpdate &&update);
std::vector<SubresourceUpdate> *getLevelUpdates(gl::LevelIndex level);
wgpu::Texture mTexture;
wgpu::TextureDescriptor mTextureDescriptor = {};
bool mInitialized = false;
gl::LevelIndex mFirstAllocatedLevel = gl::LevelIndex(0);
angle::FormatID mIntendedFormatID;
angle::FormatID mActualFormatID;
std::vector<std::vector<SubresourceUpdate>> mSubresourceQueue;
};
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.operator bool(); }
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 uint8_t *getMapReadPointer(size_t offset, size_t size) const;
const std::optional<BufferMapState> &getMappedState() const;
bool canMapForRead() const;
bool canMapForWrite() const;
wgpu::Buffer &getBuffer();
uint64_t requestedSize() const;
uint64_t actualSize() const;
private:
wgpu::Buffer mBuffer;
size_t mRequestedSize = 0;
std::optional<BufferMapState> mMappedState;
};
} // namespace webgpu
} // namespace rx
#endif // LIBANGLE_RENDERER_WGPU_WGPU_HELPERS_H_