1 // 2 // Copyright 2024 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // SurfaceWgpu.h: 7 // Defines the class interface for SurfaceWgpu, implementing SurfaceImpl. 8 // 9 10 #ifndef LIBANGLE_RENDERER_WGPU_SURFACEWGPU_H_ 11 #define LIBANGLE_RENDERER_WGPU_SURFACEWGPU_H_ 12 13 #include "libANGLE/renderer/SurfaceImpl.h" 14 15 #include "libANGLE/renderer/wgpu/RenderTargetWgpu.h" 16 #include "libANGLE/renderer/wgpu/wgpu_helpers.h" 17 18 #include <dawn/webgpu_cpp.h> 19 20 namespace rx 21 { 22 23 class SurfaceWgpu : public SurfaceImpl 24 { 25 public: 26 SurfaceWgpu(const egl::SurfaceState &surfaceState); 27 ~SurfaceWgpu() override; 28 29 protected: 30 struct AttachmentImage 31 { 32 webgpu::ImageHelper texture; 33 RenderTargetWgpu renderTarget; 34 }; 35 angle::Result createDepthStencilAttachment(uint32_t width, 36 uint32_t height, 37 const webgpu::Format &webgpuFormat, 38 wgpu::Device &device, 39 AttachmentImage *outDepthStencilAttachment); 40 }; 41 42 class OffscreenSurfaceWgpu : public SurfaceWgpu 43 { 44 public: 45 OffscreenSurfaceWgpu(const egl::SurfaceState &surfaceState); 46 ~OffscreenSurfaceWgpu() override; 47 48 egl::Error initialize(const egl::Display *display) override; 49 egl::Error swap(const gl::Context *context) override; 50 egl::Error bindTexImage(const gl::Context *context, 51 gl::Texture *texture, 52 EGLint buffer) override; 53 egl::Error releaseTexImage(const gl::Context *context, EGLint buffer) override; 54 void setSwapInterval(const egl::Display *display, EGLint interval) override; 55 56 // width and height can change with client window resizing 57 EGLint getWidth() const override; 58 EGLint getHeight() const override; 59 60 EGLint getSwapBehavior() const override; 61 62 angle::Result initializeContents(const gl::Context *context, 63 GLenum binding, 64 const gl::ImageIndex &imageIndex) override; 65 66 egl::Error attachToFramebuffer(const gl::Context *context, 67 gl::Framebuffer *framebuffer) override; 68 egl::Error detachFromFramebuffer(const gl::Context *context, 69 gl::Framebuffer *framebuffer) override; 70 71 angle::Result getAttachmentRenderTarget(const gl::Context *context, 72 GLenum binding, 73 const gl::ImageIndex &imageIndex, 74 GLsizei samples, 75 FramebufferAttachmentRenderTarget **rtOut) override; 76 77 private: 78 angle::Result initializeImpl(const egl::Display *display); 79 80 EGLint mWidth; 81 EGLint mHeight; 82 83 AttachmentImage mColorAttachment; 84 AttachmentImage mDepthStencilAttachment; 85 }; 86 87 class WindowSurfaceWgpu : public SurfaceWgpu 88 { 89 public: 90 WindowSurfaceWgpu(const egl::SurfaceState &surfaceState, EGLNativeWindowType window); 91 ~WindowSurfaceWgpu() override; 92 93 egl::Error initialize(const egl::Display *display) override; 94 void destroy(const egl::Display *display) override; 95 egl::Error swap(const gl::Context *context) override; 96 egl::Error bindTexImage(const gl::Context *context, 97 gl::Texture *texture, 98 EGLint buffer) override; 99 egl::Error releaseTexImage(const gl::Context *context, EGLint buffer) override; 100 void setSwapInterval(const egl::Display *display, EGLint interval) override; 101 102 // width and height can change with client window resizing 103 EGLint getWidth() const override; 104 EGLint getHeight() const override; 105 106 EGLint getSwapBehavior() const override; 107 108 angle::Result initializeContents(const gl::Context *context, 109 GLenum binding, 110 const gl::ImageIndex &imageIndex) override; 111 112 egl::Error attachToFramebuffer(const gl::Context *context, 113 gl::Framebuffer *framebuffer) override; 114 egl::Error detachFromFramebuffer(const gl::Context *context, 115 gl::Framebuffer *framebuffer) override; 116 117 angle::Result getAttachmentRenderTarget(const gl::Context *context, 118 GLenum binding, 119 const gl::ImageIndex &imageIndex, 120 GLsizei samples, 121 FramebufferAttachmentRenderTarget **rtOut) override; 122 123 protected: getNativeWindow()124 EGLNativeWindowType getNativeWindow() const { return mNativeWindow; } 125 126 private: 127 angle::Result initializeImpl(const egl::Display *display); 128 129 angle::Result swapImpl(const gl::Context *context); 130 131 angle::Result configureSurface(const egl::Display *display, const gl::Extents &size); 132 angle::Result updateCurrentTexture(const egl::Display *display); 133 134 virtual angle::Result createWgpuSurface(const egl::Display *display, 135 wgpu::Surface *outSurface) = 0; 136 virtual angle::Result getCurrentWindowSize(const egl::Display *display, 137 gl::Extents *outSize) = 0; 138 139 EGLNativeWindowType mNativeWindow; 140 wgpu::Surface mSurface; 141 142 const webgpu::Format *mSurfaceTextureFormat = nullptr; 143 wgpu::TextureUsage mSurfaceTextureUsage; 144 wgpu::PresentMode mPresentMode; 145 146 const webgpu::Format *mDepthStencilFormat = nullptr; 147 148 gl::Extents mCurrentSurfaceSize; 149 150 AttachmentImage mColorAttachment; 151 AttachmentImage mDepthStencilAttachment; 152 }; 153 154 WindowSurfaceWgpu *CreateWgpuWindowSurface(const egl::SurfaceState &surfaceState, 155 EGLNativeWindowType window); 156 157 } // namespace rx 158 159 #endif // LIBANGLE_RENDERER_WGPU_SURFACEWGPU_H_ 160