Cogs.Core
SwapChainWebGPU.cpp
1#include "SwapChainWebGPU.h"
2
3#include "Foundation/Logging/Logger.h"
4
5namespace{
6 Cogs::Logging::Log logger = Cogs::Logging::getLogger("SwapChainWebGPU");
7}
8
9namespace Cogs{
10
11 SwapChainWebGPU::SwapChainWebGPU()
12 {
13 }
14
15 SwapChainWebGPU::~SwapChainWebGPU()
16 {
17 // destroy(); // Done in GraphicsDevice::destroy();
18 }
19
20 void SwapChainWebGPU::initialize(WGPUDevice device_in, WGPUSurface surface_in)
21 {
22 device = device_in;
23 surface = surface_in;
24 create();
25 }
26
27 void SwapChainWebGPU::create()
28 {
29 WGPUTextureFormat formats[] = {
30 color_format,
31 };
32
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;
44
45 wgpuSurfaceConfigure(surface, &surfaceConf);
46
47 if(samples > 1){
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;
60
61 colorBufferTexture = wgpuDeviceCreateTexture(device, &desc);
62
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);
73 }
74
75#ifndef __EMSCRIPTEN__
76 {
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;
86 desc.sampleCount = 1;
87 desc.viewFormatCount = sizeof(formats)/sizeof(formats[0]);
88 desc.viewFormats = formats;
89
90 resolveTexture = wgpuDeviceCreateTexture(device, &desc);
91
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);
102 }
103#endif
104
105 {
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;
118
119 depthBufferTexture = wgpuDeviceCreateTexture(device, &desc);
120
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);
131 }
132 }
133
134 void SwapChainWebGPU::destroy()
135 {
136 if(samples > 1){
137 wgpuTextureViewRelease(colorBufferView);
138 wgpuTextureDestroy(colorBufferTexture);
139 wgpuTextureRelease(colorBufferTexture);
140 }
141#ifndef __EMSCRIPTEN__
142 wgpuTextureViewRelease(resolveView);
143 wgpuTextureDestroy(resolveTexture);
144 wgpuTextureRelease(resolveTexture);
145#endif
146 wgpuTextureViewRelease(depthBufferView);
147 wgpuTextureDestroy(depthBufferTexture);
148 wgpuTextureRelease(depthBufferTexture);
149
150 wgpuSurfaceUnconfigure(surface);
151 }
152
154 {
155 if(resize){
156 destroy();
157 create();
158 resize = false;
159 }
160#ifdef __EMSCRIPTEN__
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);
172#endif
173 }
174
175 void SwapChainWebGPU::endFrame(uint32_t syncInterval, PresentFlags presentFlags)
176 {
177 (void)syncInterval; // TODO
178 (void)presentFlags; // TODO
179#ifdef __EMSCRIPTEN__
180 wgpuTextureViewRelease(resolveView);
181#endif
182#ifndef __EMSCRIPTEN__
183 wgpuSurfacePresent(surface);
184#endif
185 }
186
187 void SwapChainWebGPU::setSize(int width, int height, bool /*forceIt*/)
188 {
189 LOG_DEBUG(logger, "setSize %d %d", width, height);
190 set_width = width;
191 set_height = height;
192 resize = true;
193 }
194
195 void SwapChainWebGPU::setFullscreen(bool /*enabled*/)
196 {
197 }
198
200 {
201 return false;
202 }
203
205 {
207 }
208
210 {
212 }
213
214 WGPUSurfaceTexture SwapChainWebGPU::getTextureData()
215 {
216 WGPUSurfaceTexture surfaceTexture;
217 wgpuSurfaceGetCurrentTexture(surface, &surfaceTexture);
218
219 if(surfaceTexture.status == WGPUSurfaceGetCurrentTextureStatus_Error) {
220 LOG_ERROR(logger, "failed to retrieve image from swapchain");
221 }
222
223
224 return surfaceTexture;
225 }
226}
Log implementation class.
Definition: LogManager.h:140
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
Definition: LogManager.h:181
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
PresentFlags
Flags controlling presentation.
Definition: Common.h:166
static const Handle_t InvalidHandle
Represents an invalid handle.
Definition: Common.h:81