Hash :
ca2e588b
Author :
Date :
2024-09-12T14:20:33
WebGPU: Add support for depth/stencil clears This CL also adds helper methods to create webgpu depth/stencil attachments for a render pass descriptor, as well as depth/stencil attachment images. Bug: angleproject:42267012 Change-Id: Iebef99ba34db2e50f56449d0737b3dbb03b90f2e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5750001 Commit-Queue: Liza Burakova <liza@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Matthew Denton <mpdenton@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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
//
// 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.
//
// SurfaceWgpu.cpp:
// Implements the class methods for SurfaceWgpu.
//
#include "libANGLE/renderer/wgpu/SurfaceWgpu.h"
#include "common/debug.h"
#include "libANGLE/Display.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/wgpu/DisplayWgpu.h"
#include "libANGLE/renderer/wgpu/FramebufferWgpu.h"
namespace rx
{
constexpr wgpu::TextureUsage kSurfaceTextureUsage =
wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment |
wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::CopyDst;
SurfaceWgpu::SurfaceWgpu(const egl::SurfaceState &surfaceState) : SurfaceImpl(surfaceState) {}
SurfaceWgpu::~SurfaceWgpu() {}
angle::Result SurfaceWgpu::createDepthStencilAttachment(uint32_t width,
uint32_t height,
const webgpu::Format &webgpuFormat,
wgpu::Device &device,
AttachmentImage *outDepthStencilAttachment)
{
wgpu::TextureDescriptor desc = outDepthStencilAttachment->texture.createTextureDescriptor(
kSurfaceTextureUsage, wgpu::TextureDimension::e2D, {width, height, 1},
webgpuFormat.getActualWgpuTextureFormat(), 1, 1);
constexpr uint32_t level = 0;
constexpr uint32_t layer = 0;
ANGLE_TRY(outDepthStencilAttachment->texture.initImage(webgpuFormat.getIntendedFormatID(),
webgpuFormat.getActualImageFormatID(),
device, gl::LevelIndex(level), desc));
wgpu::TextureView view;
ANGLE_TRY(
outDepthStencilAttachment->texture.createTextureView(gl::LevelIndex(level), layer, view));
outDepthStencilAttachment->renderTarget.set(
&outDepthStencilAttachment->texture, view, webgpu::LevelIndex(level), layer,
outDepthStencilAttachment->texture.toWgpuTextureFormat());
return angle::Result::Continue;
}
OffscreenSurfaceWgpu::OffscreenSurfaceWgpu(const egl::SurfaceState &surfaceState)
: SurfaceWgpu(surfaceState),
mWidth(surfaceState.attributes.getAsInt(EGL_WIDTH, 0)),
mHeight(surfaceState.attributes.getAsInt(EGL_HEIGHT, 0))
{}
OffscreenSurfaceWgpu::~OffscreenSurfaceWgpu() {}
egl::Error OffscreenSurfaceWgpu::initialize(const egl::Display *display)
{
return angle::ResultToEGL(initializeImpl(display));
}
egl::Error OffscreenSurfaceWgpu::swap(const gl::Context *context)
{
UNREACHABLE();
return egl::NoError();
}
egl::Error OffscreenSurfaceWgpu::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error OffscreenSurfaceWgpu::releaseTexImage(const gl::Context *context, EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
void OffscreenSurfaceWgpu::setSwapInterval(const egl::Display *display, EGLint interval) {}
EGLint OffscreenSurfaceWgpu::getWidth() const
{
return mWidth;
}
EGLint OffscreenSurfaceWgpu::getHeight() const
{
return mHeight;
}
EGLint OffscreenSurfaceWgpu::getSwapBehavior() const
{
return EGL_BUFFER_DESTROYED;
}
angle::Result OffscreenSurfaceWgpu::initializeContents(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex)
{
UNIMPLEMENTED();
return angle::Result::Continue;
}
egl::Error OffscreenSurfaceWgpu::attachToFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error OffscreenSurfaceWgpu::detachFromFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
angle::Result OffscreenSurfaceWgpu::getAttachmentRenderTarget(
const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
if (binding == GL_BACK)
{
*rtOut = &mColorAttachment.renderTarget;
}
else
{
ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL);
*rtOut = &mDepthStencilAttachment.renderTarget;
}
return angle::Result::Continue;
}
angle::Result OffscreenSurfaceWgpu::initializeImpl(const egl::Display *display)
{
DisplayWgpu *displayWgpu = webgpu::GetImpl(display);
wgpu::Device &device = displayWgpu->getDevice();
const egl::Config *config = mState.config;
if (config->renderTargetFormat != GL_NONE)
{
const webgpu::Format &webgpuFormat = displayWgpu->getFormat(config->renderTargetFormat);
wgpu::TextureDescriptor desc = mColorAttachment.texture.createTextureDescriptor(
kSurfaceTextureUsage, wgpu::TextureDimension::e2D,
{static_cast<uint32_t>(mWidth), static_cast<uint32_t>(mHeight), 1},
webgpuFormat.getActualWgpuTextureFormat(), 1, 1);
constexpr uint32_t level = 0;
constexpr uint32_t layer = 0;
ANGLE_TRY(mColorAttachment.texture.initImage(webgpuFormat.getIntendedFormatID(),
webgpuFormat.getActualImageFormatID(), device,
gl::LevelIndex(level), desc));
wgpu::TextureView view;
ANGLE_TRY(mColorAttachment.texture.createTextureView(gl::LevelIndex(level), layer, view));
mColorAttachment.renderTarget.set(&mColorAttachment.texture, view,
webgpu::LevelIndex(level), layer,
mColorAttachment.texture.toWgpuTextureFormat());
}
if (config->depthStencilFormat != GL_NONE)
{
const webgpu::Format &webgpuFormat = displayWgpu->getFormat(config->depthStencilFormat);
ANGLE_TRY(createDepthStencilAttachment(static_cast<uint32_t>(mWidth),
static_cast<uint32_t>(mHeight), webgpuFormat, device,
&mDepthStencilAttachment));
}
return angle::Result::Continue;
}
WindowSurfaceWgpu::WindowSurfaceWgpu(const egl::SurfaceState &surfaceState,
EGLNativeWindowType window)
: SurfaceWgpu(surfaceState), mNativeWindow(window)
{}
WindowSurfaceWgpu::~WindowSurfaceWgpu() {}
egl::Error WindowSurfaceWgpu::initialize(const egl::Display *display)
{
return angle::ResultToEGL(initializeImpl(display));
}
void WindowSurfaceWgpu::destroy(const egl::Display *display)
{
mSurface = nullptr;
mSwapChain = nullptr;
mColorAttachment.renderTarget.reset();
mColorAttachment.texture.resetImage();
mDepthStencilAttachment.renderTarget.reset();
mDepthStencilAttachment.texture.resetImage();
}
egl::Error WindowSurfaceWgpu::swap(const gl::Context *context)
{
return angle::ResultToEGL(swapImpl(context));
}
egl::Error WindowSurfaceWgpu::bindTexImage(const gl::Context *context,
gl::Texture *texture,
EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
egl::Error WindowSurfaceWgpu::releaseTexImage(const gl::Context *context, EGLint buffer)
{
UNIMPLEMENTED();
return egl::NoError();
}
void WindowSurfaceWgpu::setSwapInterval(const egl::Display *display, EGLint interval)
{
UNIMPLEMENTED();
}
EGLint WindowSurfaceWgpu::getWidth() const
{
return mCurrentSwapChainSize.width;
}
EGLint WindowSurfaceWgpu::getHeight() const
{
return mCurrentSwapChainSize.height;
}
EGLint WindowSurfaceWgpu::getSwapBehavior() const
{
UNIMPLEMENTED();
return 0;
}
angle::Result WindowSurfaceWgpu::initializeContents(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex)
{
UNIMPLEMENTED();
return angle::Result::Continue;
}
egl::Error WindowSurfaceWgpu::attachToFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
return egl::NoError();
}
egl::Error WindowSurfaceWgpu::detachFromFramebuffer(const gl::Context *context,
gl::Framebuffer *framebuffer)
{
return egl::NoError();
}
angle::Result WindowSurfaceWgpu::getAttachmentRenderTarget(
const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
if (binding == GL_BACK)
{
*rtOut = &mColorAttachment.renderTarget;
}
else
{
ASSERT(binding == GL_DEPTH || binding == GL_STENCIL || binding == GL_DEPTH_STENCIL);
*rtOut = &mDepthStencilAttachment.renderTarget;
}
return angle::Result::Continue;
}
angle::Result WindowSurfaceWgpu::initializeImpl(const egl::Display *display)
{
DisplayWgpu *displayWgpu = webgpu::GetImpl(display);
wgpu::Device &device = displayWgpu->getDevice();
ANGLE_TRY(createWgpuSurface(display, &mSurface));
const egl::Config *config = mState.config;
ASSERT(config->renderTargetFormat != GL_NONE);
gl::Extents size;
ANGLE_TRY(getCurrentWindowSize(display, &size));
if (config->depthStencilFormat != GL_NONE)
{
const webgpu::Format &dsWebgpuFormat = displayWgpu->getFormat(config->depthStencilFormat);
ANGLE_TRY(createDepthStencilAttachment(static_cast<uint32_t>(size.width),
static_cast<uint32_t>(size.height), dsWebgpuFormat,
device, &mDepthStencilAttachment));
}
const webgpu::Format &webgpuFormat = displayWgpu->getFormat(config->renderTargetFormat);
wgpu::SwapChainDescriptor swapChainDesc = {};
swapChainDesc.usage = wgpu::TextureUsage::RenderAttachment | wgpu::TextureUsage::CopySrc |
wgpu::TextureUsage::CopyDst;
swapChainDesc.format = webgpuFormat.getActualWgpuTextureFormat();
swapChainDesc.width = size.width;
swapChainDesc.height = size.height;
swapChainDesc.presentMode = wgpu::PresentMode::Mailbox;
mSwapChain = device.CreateSwapChain(mSurface, &swapChainDesc);
mCurrentSwapChainSize = size;
ANGLE_TRY(updateCurrentTexture(display));
return angle::Result::Continue;
}
angle::Result WindowSurfaceWgpu::swapImpl(const gl::Context *context)
{
const egl::Display *display = context->getDisplay();
ContextWgpu *contextWgpu = webgpu::GetImpl(context);
ANGLE_TRY(contextWgpu->flush(webgpu::RenderPassClosureReason::EGLSwapBuffers));
mSwapChain.Present();
ANGLE_TRY(getCurrentWindowSize(display, &mCurrentSwapChainSize));
ANGLE_TRY(updateCurrentTexture(display));
return angle::Result::Continue;
}
angle::Result WindowSurfaceWgpu::updateCurrentTexture(const egl::Display *display)
{
wgpu::Texture texture = mSwapChain.GetCurrentTexture();
wgpu::TextureView view = mSwapChain.GetCurrentTextureView();
wgpu::TextureFormat wgpuFormat = texture.GetFormat();
angle::FormatID angleFormat = webgpu::GetFormatIDFromWgpuTextureFormat(wgpuFormat);
ANGLE_TRY(mColorAttachment.texture.initExternal(angleFormat, angleFormat, texture));
mColorAttachment.renderTarget.set(&mColorAttachment.texture, view, webgpu::LevelIndex(0), 0,
wgpuFormat);
return angle::Result::Continue;
}
} // namespace rx