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 = {};
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 = {};
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 // colorBufferTexture = wgpuDeviceCreateErrorTexture(device, &desc);
63
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);
74 }
75
76#ifndef EMSCRIPTEN
77 {
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;
87 desc.sampleCount = 1;
88 desc.viewFormatCount = sizeof(formats)/sizeof(formats[0]);
89 desc.viewFormats = formats;
90
91 resolveTexture = wgpuDeviceCreateTexture(device, &desc);
92 // resolveTexture = wgpuDeviceCreateErrorTexture(device, &desc);
93
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);
104 }
105#endif
106
107 {
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;
120
121 depthBufferTexture = wgpuDeviceCreateTexture(device, &desc);
122 // depthBufferTexture = wgpuDeviceCreateErrorTexture(device, &desc);
123
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);
134 }
135 }
136
137 void SwapChainWebGPU::destroy()
138 {
139 if(samples > 1){
140 wgpuTextureViewRelease(colorBufferView);
141 wgpuTextureDestroy(colorBufferTexture);
142 wgpuTextureRelease(colorBufferTexture);
143 }
144#ifndef EMSCRIPTEN
145 wgpuTextureViewRelease(resolveView);
146 wgpuTextureDestroy(resolveTexture);
147 wgpuTextureRelease(resolveTexture);
148#endif
149 wgpuTextureViewRelease(depthBufferView);
150 wgpuTextureDestroy(depthBufferTexture);
151 wgpuTextureRelease(depthBufferTexture);
152
153 wgpuSurfaceUnconfigure(surface);
154 }
155
157 {
158 if(resize){
159 destroy();
160 create();
161 resize = false;
162 }
163 #ifdef EMSCRIPTEN
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);
175 #endif
176 }
177
178 void SwapChainWebGPU::endFrame(uint32_t syncInterval, PresentFlags presentFlags)
179 {
180 (void)syncInterval; // TODO
181 (void)presentFlags; // TODO
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