1#include "SwapChainWebGPU.h"
3#include "Foundation/Logging/Logger.h"
11 SwapChainWebGPU::SwapChainWebGPU()
15 SwapChainWebGPU::~SwapChainWebGPU()
20 void SwapChainWebGPU::initialize(WGPUDevice device_in, WGPUSurface surface_in)
27 void SwapChainWebGPU::create()
29 WGPUTextureFormat formats[] = {
33 WGPUSurfaceConfiguration surfaceConf = {};
34 surfaceConf.nextInChain =
nullptr;
35 surfaceConf.width = set_width;
36 surfaceConf.height = set_height;
37 surfaceConf.format = swap_chain_format;
38 surfaceConf.viewFormatCount =
sizeof(formats) /
sizeof(WGPUTextureFormat);
39 surfaceConf.viewFormats = formats;
40 surfaceConf.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopyDst;
41 surfaceConf.device = device;
42 surfaceConf.presentMode = WGPUPresentMode_Fifo;
43 surfaceConf.alphaMode = WGPUCompositeAlphaMode_Auto;
45 wgpuSurfaceConfigure(surface, &surfaceConf);
48 WGPUTextureDescriptor desc = {};
49 desc.label = {
"colorBufferTexture", WGPU_STRLEN};
50 desc.usage = WGPUTextureUsage_RenderAttachment;
51 desc.dimension = WGPUTextureDimension_2D;
52 desc.size.width = set_width;
53 desc.size.height = set_height;
54 desc.size.depthOrArrayLayers = 1;
55 desc.format = color_format;
56 desc.mipLevelCount = 1;
57 desc.sampleCount = samples;
58 desc.viewFormatCount =
sizeof(formats)/
sizeof(formats[0]);
59 desc.viewFormats = formats;
61 colorBufferTexture = wgpuDeviceCreateTexture(device, &desc);
64 WGPUTextureViewDescriptor view_desc = {};
65 view_desc.label = {
"colorBufferView", WGPU_STRLEN};
66 view_desc.format = color_format;
67 view_desc.dimension = WGPUTextureViewDimension_2D;
68 view_desc.baseMipLevel = 0;
69 view_desc.mipLevelCount = 1;
70 view_desc.baseArrayLayer = 0;
71 view_desc.arrayLayerCount = 1;
72 view_desc.aspect = WGPUTextureAspect_All;
73 colorBufferView = wgpuTextureCreateView(colorBufferTexture, &view_desc);
78 WGPUTextureDescriptor desc = {};
79 desc.label = {
"resolveTexture", WGPU_STRLEN};
80 desc.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
81 desc.dimension = WGPUTextureDimension_2D;
82 desc.size.width = set_width;
83 desc.size.height = set_height;
84 desc.size.depthOrArrayLayers = 1;
85 desc.format = color_format;
86 desc.mipLevelCount = 1;
88 desc.viewFormatCount =
sizeof(formats)/
sizeof(formats[0]);
89 desc.viewFormats = formats;
91 resolveTexture = wgpuDeviceCreateTexture(device, &desc);
94 WGPUTextureViewDescriptor view_desc = {};
95 view_desc.label = {
"resolveView", WGPU_STRLEN};
96 view_desc.format = color_format;
97 view_desc.dimension = WGPUTextureViewDimension_2D;
98 view_desc.baseMipLevel = 0;
99 view_desc.mipLevelCount = 1;
100 view_desc.baseArrayLayer = 0;
101 view_desc.arrayLayerCount = 1;
102 view_desc.aspect = WGPUTextureAspect_All;
103 resolveView = wgpuTextureCreateView(resolveTexture, &view_desc);
108 WGPUTextureDescriptor desc = {};
109 desc.label = {
"depthBufferTexture", WGPU_STRLEN};
110 desc.usage = WGPUTextureUsage_RenderAttachment;
111 desc.dimension = WGPUTextureDimension_2D;
112 desc.size.width = set_width;
113 desc.size.height = set_height;
114 desc.size.depthOrArrayLayers = 1;
115 desc.format = depth_format;
116 desc.mipLevelCount = 1;
117 desc.sampleCount = samples;
118 desc.viewFormatCount = 1;
119 desc.viewFormats = &depth_format;
121 depthBufferTexture = wgpuDeviceCreateTexture(device, &desc);
124 WGPUTextureViewDescriptor view_desc = {};
125 view_desc.label = {
"depthBufferView", WGPU_STRLEN};
126 view_desc.format = depth_format;
127 view_desc.dimension = WGPUTextureViewDimension_2D;
128 view_desc.baseMipLevel = 0;
129 view_desc.mipLevelCount = 1;
130 view_desc.baseArrayLayer = 0;
131 view_desc.arrayLayerCount = 1;
132 view_desc.aspect = WGPUTextureAspect_DepthOnly;
133 depthBufferView = wgpuTextureCreateView(depthBufferTexture, &view_desc);
137 void SwapChainWebGPU::destroy()
140 wgpuTextureViewRelease(colorBufferView);
141 wgpuTextureDestroy(colorBufferTexture);
142 wgpuTextureRelease(colorBufferTexture);
145 wgpuTextureViewRelease(resolveView);
146 wgpuTextureDestroy(resolveTexture);
147 wgpuTextureRelease(resolveTexture);
149 wgpuTextureViewRelease(depthBufferView);
150 wgpuTextureDestroy(depthBufferTexture);
151 wgpuTextureRelease(depthBufferTexture);
153 wgpuSurfaceUnconfigure(surface);
164 WGPUSurfaceTexture surfaceTexture = getTextureData();
165 WGPUTextureViewDescriptor view_desc = {};
166 view_desc.label = {
"resolveView", WGPU_STRLEN };
167 view_desc.format = color_format;
168 view_desc.dimension = WGPUTextureViewDimension_2D;
169 view_desc.baseMipLevel = 0;
170 view_desc.mipLevelCount = 1;
171 view_desc.baseArrayLayer = 0;
172 view_desc.arrayLayerCount = 1;
173 view_desc.aspect = WGPUTextureAspect_All;
174 resolveView = wgpuTextureCreateView(surfaceTexture.texture, &view_desc);
183 wgpuSurfacePresent(surface);
189 LOG_DEBUG(logger,
"setSize %d %d", width, height);
214 WGPUSurfaceTexture SwapChainWebGPU::getTextureData()
216 WGPUSurfaceTexture surfaceTexture;
217 wgpuSurfaceGetCurrentTexture(surface, &surfaceTexture);
219 if(surfaceTexture.status == WGPUSurfaceGetCurrentTextureStatus_Error) {
220 LOG_ERROR(logger,
"failed to retrieve image from swapchain");
224 return surfaceTexture;
Log implementation class.
virtual bool isFullscreen() override
Test whether the current swap chain is fullscreen.
virtual const DepthStencilHandle & getDepthStencil() const
Returns the depth stencil managed by this swap chain.
virtual void beginFrame() override
Signal the beginning of a new frame to the graphics device.
virtual void setFullscreen(bool enabled) override
Switch this swap chain to fullscreen or windowed depending on the parameter.
virtual void endFrame(uint32_t syncInterval=0, PresentFlags presentFlags=PresentFlags::None) override
Signal the end of a frame to the graphics device.
virtual const RenderTargetHandle & getRenderTarget() const
Returns the render target managed by this swap chain.
virtual void setSize(int newWidth, int newHeight, bool forceIt=false) override
Set the size of the main drawing buffer used by the graphics device in pixels.
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Contains all Cogs related functionality.
PresentFlags
Flags controlling presentation.
static const Handle_t InvalidHandle
Represents an invalid handle.