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 = WGPU_SURFACE_CONFIGURATION_INIT;
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 = WGPU_TEXTURE_DESCRIPTOR_INIT;
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);
63 WGPUTextureViewDescriptor view_desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
64 view_desc.label = {
"colorBufferView", WGPU_STRLEN};
65 view_desc.format = color_format;
66 view_desc.dimension = WGPUTextureViewDimension_2D;
67 view_desc.baseMipLevel = 0;
68 view_desc.mipLevelCount = 1;
69 view_desc.baseArrayLayer = 0;
70 view_desc.arrayLayerCount = 1;
71 view_desc.aspect = WGPUTextureAspect_All;
72 colorBufferView = wgpuTextureCreateView(colorBufferTexture, &view_desc);
77 WGPUTextureDescriptor desc = WGPU_TEXTURE_DESCRIPTOR_INIT;
78 desc.label = {
"resolveTexture", WGPU_STRLEN};
79 desc.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc;
80 desc.dimension = WGPUTextureDimension_2D;
81 desc.size.width = set_width;
82 desc.size.height = set_height;
83 desc.size.depthOrArrayLayers = 1;
84 desc.format = color_format;
85 desc.mipLevelCount = 1;
87 desc.viewFormatCount =
sizeof(formats)/
sizeof(formats[0]);
88 desc.viewFormats = formats;
90 resolveTexture = wgpuDeviceCreateTexture(device, &desc);
92 WGPUTextureViewDescriptor view_desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
93 view_desc.label = {
"resolveView", WGPU_STRLEN};
94 view_desc.format = color_format;
95 view_desc.dimension = WGPUTextureViewDimension_2D;
96 view_desc.baseMipLevel = 0;
97 view_desc.mipLevelCount = 1;
98 view_desc.baseArrayLayer = 0;
99 view_desc.arrayLayerCount = 1;
100 view_desc.aspect = WGPUTextureAspect_All;
101 resolveView = wgpuTextureCreateView(resolveTexture, &view_desc);
106 WGPUTextureDescriptor desc = WGPU_TEXTURE_DESCRIPTOR_INIT;
107 desc.label = {
"depthBufferTexture", WGPU_STRLEN};
108 desc.usage = WGPUTextureUsage_RenderAttachment;
109 desc.dimension = WGPUTextureDimension_2D;
110 desc.size.width = set_width;
111 desc.size.height = set_height;
112 desc.size.depthOrArrayLayers = 1;
113 desc.format = depth_format;
114 desc.mipLevelCount = 1;
115 desc.sampleCount = samples;
116 desc.viewFormatCount = 1;
117 desc.viewFormats = &depth_format;
119 depthBufferTexture = wgpuDeviceCreateTexture(device, &desc);
121 WGPUTextureViewDescriptor view_desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
122 view_desc.label = {
"depthBufferView", WGPU_STRLEN};
123 view_desc.format = depth_format;
124 view_desc.dimension = WGPUTextureViewDimension_2D;
125 view_desc.baseMipLevel = 0;
126 view_desc.mipLevelCount = 1;
127 view_desc.baseArrayLayer = 0;
128 view_desc.arrayLayerCount = 1;
129 view_desc.aspect = WGPUTextureAspect_DepthOnly;
130 depthBufferView = wgpuTextureCreateView(depthBufferTexture, &view_desc);
134 void SwapChainWebGPU::destroy()
137 wgpuTextureViewRelease(colorBufferView);
138 wgpuTextureDestroy(colorBufferTexture);
139 wgpuTextureRelease(colorBufferTexture);
141#ifndef __EMSCRIPTEN__
142 wgpuTextureViewRelease(resolveView);
143 wgpuTextureDestroy(resolveTexture);
144 wgpuTextureRelease(resolveTexture);
146 wgpuTextureViewRelease(depthBufferView);
147 wgpuTextureDestroy(depthBufferTexture);
148 wgpuTextureRelease(depthBufferTexture);
150 wgpuSurfaceUnconfigure(surface);
161 WGPUSurfaceTexture surfaceTexture = getTextureData();
162 WGPUTextureViewDescriptor view_desc = WGPU_TEXTURE_VIEW_DESCRIPTOR_INIT;
163 view_desc.label = {
"resolveView", WGPU_STRLEN };
164 view_desc.format = color_format;
165 view_desc.dimension = WGPUTextureViewDimension_2D;
166 view_desc.baseMipLevel = 0;
167 view_desc.mipLevelCount = 1;
168 view_desc.baseArrayLayer = 0;
169 view_desc.arrayLayerCount = 1;
170 view_desc.aspect = WGPUTextureAspect_All;
171 resolveView = wgpuTextureCreateView(surfaceTexture.texture, &view_desc);
180 wgpuTextureViewRelease(resolveView);
182#ifndef __EMSCRIPTEN__
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.