Hash :
84e54d88
Author :
Date :
2024-07-22T14:39:22
WebGPU: Add command buffers and renderpass management Add a CommandBuffer class to serialize and replay WebGPU commands. Only Draw and SetPipeline are implemented in this patch. Manage render pass begin and end events due to framebuffer changes and swapping. Handle the color mask dirty bits so that a non-zero color mask will be used. All togther, this is enough to draw a triangle using a hard-coded shader without inputs. Bug: angleproject:0 Change-Id: I0fbf0296563c02c7f0774ad4197b83f4c93c22bb Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5731594 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Liza Burakova <liza@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
//
// 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_utils.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "libANGLE/renderer/wgpu/ContextWgpu.h"
#include "libANGLE/renderer/wgpu/DisplayWgpu.h"
namespace rx
{
namespace webgpu
{
DisplayWgpu *GetDisplay(const gl::Context *context)
{
ContextWgpu *contextWgpu = GetImpl(context);
return contextWgpu->getDisplay();
}
wgpu::Device GetDevice(const gl::Context *context)
{
DisplayWgpu *display = GetDisplay(context);
return display->getDevice();
}
wgpu::Instance GetInstance(const gl::Context *context)
{
DisplayWgpu *display = GetDisplay(context);
return display->getInstance();
}
wgpu::RenderPassColorAttachment CreateNewClearColorAttachment(wgpu::Color clearValue,
uint32_t depthSlice,
wgpu::TextureView textureView)
{
wgpu::RenderPassColorAttachment colorAttachment;
colorAttachment.view = textureView;
colorAttachment.depthSlice = depthSlice;
colorAttachment.loadOp = wgpu::LoadOp::Clear;
colorAttachment.storeOp = wgpu::StoreOp::Store;
colorAttachment.clearValue = clearValue;
return colorAttachment;
}
bool IsWgpuError(wgpu::WaitStatus waitStatus)
{
return waitStatus != wgpu::WaitStatus::Success;
}
bool IsWgpuError(WGPUBufferMapAsyncStatus mapBufferStatus)
{
return mapBufferStatus != WGPUBufferMapAsyncStatus_Success;
}
ClearValuesArray::ClearValuesArray() : mValues{}, mEnabled{} {}
ClearValuesArray::~ClearValuesArray() = default;
ClearValuesArray::ClearValuesArray(const ClearValuesArray &other) = default;
ClearValuesArray &ClearValuesArray::operator=(const ClearValuesArray &rhs) = default;
void ClearValuesArray::store(uint32_t index, ClearValues clearValues)
{
mValues[index] = clearValues;
mEnabled.set(index);
}
gl::DrawBufferMask ClearValuesArray::getColorMask() const
{
return gl::DrawBufferMask(mEnabled.bits() & kUnpackedColorBuffersMask);
}
void EnsureCapsInitialized(const wgpu::Device &device, gl::Caps *nativeCaps)
{
wgpu::SupportedLimits limitsWgpu = {};
device.GetLimits(&limitsWgpu);
nativeCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
nativeCaps->max3DTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension3D);
nativeCaps->max2DTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
nativeCaps->maxArrayTextureLayers = rx::LimitToInt(limitsWgpu.limits.maxTextureArrayLayers);
nativeCaps->maxCubeMapTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
nativeCaps->maxRenderbufferSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
nativeCaps->maxDrawBuffers = rx::LimitToInt(limitsWgpu.limits.maxColorAttachments);
nativeCaps->maxFramebufferWidth = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
nativeCaps->maxFramebufferHeight = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
nativeCaps->maxColorAttachments = rx::LimitToInt(limitsWgpu.limits.maxColorAttachments);
nativeCaps->maxVertexAttribStride =
rx::LimitToInt(limitsWgpu.limits.maxVertexBufferArrayStride);
nativeCaps->maxVertexAttributes = rx::LimitToInt(limitsWgpu.limits.maxVertexAttributes);
nativeCaps->maxTextureBufferSize = rx::LimitToInt(limitsWgpu.limits.maxBufferSize);
}
bool IsStripPrimitiveTopology(wgpu::PrimitiveTopology topology)
{
switch (topology)
{
case wgpu::PrimitiveTopology::LineStrip:
case wgpu::PrimitiveTopology::TriangleStrip:
return true;
default:
return false;
}
}
} // namespace webgpu
namespace wgpu_gl
{
gl::LevelIndex getLevelIndex(webgpu::LevelIndex levelWgpu, gl::LevelIndex baseLevel)
{
return gl::LevelIndex(levelWgpu.get() + baseLevel.get());
}
gl::Extents getExtents(wgpu::Extent3D wgpuExtent)
{
gl::Extents glExtent;
glExtent.width = wgpuExtent.width;
glExtent.height = wgpuExtent.height;
glExtent.depth = wgpuExtent.depthOrArrayLayers;
return glExtent;
}
} // namespace wgpu_gl
namespace gl_wgpu
{
webgpu::LevelIndex getLevelIndex(gl::LevelIndex levelGl, gl::LevelIndex baseLevel)
{
ASSERT(baseLevel <= levelGl);
return webgpu::LevelIndex(levelGl.get() - baseLevel.get());
}
wgpu::Extent3D getExtent3D(const gl::Extents &glExtent)
{
wgpu::Extent3D wgpuExtent;
wgpuExtent.width = glExtent.width;
wgpuExtent.height = glExtent.height;
wgpuExtent.depthOrArrayLayers = glExtent.depth;
return wgpuExtent;
}
wgpu::PrimitiveTopology GetPrimitiveTopology(gl::PrimitiveMode mode)
{
switch (mode)
{
case gl::PrimitiveMode::Points:
return wgpu::PrimitiveTopology::PointList;
case gl::PrimitiveMode::Lines:
return wgpu::PrimitiveTopology::LineList;
case gl::PrimitiveMode::LineLoop:
UNIMPLEMENTED();
return wgpu::PrimitiveTopology::LineList; // Emulated
case gl::PrimitiveMode::LineStrip:
return wgpu::PrimitiveTopology::LineStrip;
case gl::PrimitiveMode::Triangles:
return wgpu::PrimitiveTopology::TriangleList;
case gl::PrimitiveMode::TriangleStrip:
return wgpu::PrimitiveTopology::TriangleStrip;
case gl::PrimitiveMode::TriangleFan:
UNIMPLEMENTED();
return wgpu::PrimitiveTopology::TriangleList; // Emulated
default:
UNREACHABLE();
return wgpu::PrimitiveTopology::Undefined;
}
}
wgpu::IndexFormat GetIndexFormat(gl::DrawElementsType drawElementsType)
{
switch (drawElementsType)
{
case gl::DrawElementsType::UnsignedByte:
UNIMPLEMENTED();
return wgpu::IndexFormat::Uint16; // Emulated
case gl::DrawElementsType::UnsignedShort:
return wgpu::IndexFormat::Uint16;
case gl::DrawElementsType::UnsignedInt:
return wgpu::IndexFormat::Uint32;
default:
UNREACHABLE();
return wgpu::IndexFormat::Undefined;
}
}
wgpu::FrontFace GetFrontFace(GLenum frontFace)
{
switch (frontFace)
{
case GL_CW:
return wgpu::FrontFace::CW;
case GL_CCW:
return wgpu::FrontFace::CCW;
default:
UNREACHABLE();
return wgpu::FrontFace::Undefined;
}
}
wgpu::CullMode GetCullMode(gl::CullFaceMode mode, bool cullFaceEnabled)
{
if (!cullFaceEnabled)
{
return wgpu::CullMode::None;
}
switch (mode)
{
case gl::CullFaceMode::Front:
return wgpu::CullMode::Front;
case gl::CullFaceMode::Back:
return wgpu::CullMode::Back;
case gl::CullFaceMode::FrontAndBack:
UNIMPLEMENTED();
return wgpu::CullMode::None; // Emulated
default:
UNREACHABLE();
return wgpu::CullMode::None;
}
}
wgpu::ColorWriteMask GetColorWriteMask(bool r, bool g, bool b, bool a)
{
return (r ? wgpu::ColorWriteMask::Red : wgpu::ColorWriteMask::None) |
(g ? wgpu::ColorWriteMask::Green : wgpu::ColorWriteMask::None) |
(b ? wgpu::ColorWriteMask::Blue : wgpu::ColorWriteMask::None) |
(a ? wgpu::ColorWriteMask::Alpha : wgpu::ColorWriteMask::None);
}
wgpu::TextureDimension getWgpuTextureDimension(gl::TextureType glTextureType)
{
wgpu::TextureDimension dimension = {};
switch (glTextureType)
{
case gl::TextureType::_2D:
case gl::TextureType::_2DMultisample:
case gl::TextureType::Rectangle:
case gl::TextureType::External:
case gl::TextureType::Buffer:
dimension = wgpu::TextureDimension::e2D;
break;
case gl::TextureType::_2DArray:
case gl::TextureType::_2DMultisampleArray:
case gl::TextureType::_3D:
case gl::TextureType::CubeMap:
case gl::TextureType::CubeMapArray:
case gl::TextureType::VideoImage:
dimension = wgpu::TextureDimension::e3D;
break;
default:
break;
}
return dimension;
}
} // namespace gl_wgpu
} // namespace rx