-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
591 lines (518 loc) · 21.2 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include <memory>
#include <optional>
#include <string>
#include <emscripten.h>
#include <emscripten/bind.h>
#include <emscripten/html5.h>
#include <emscripten/html5_webgpu.h>
#include <webgpu/webgpu_cpp.h>
static constexpr auto DEFAULT_SWAPCHAIN_FORMAT = wgpu::TextureFormat::BGRA8Unorm;
static constexpr auto DEFAULT_DEPTH_FORMAT = wgpu::TextureFormat::Depth32Float;
#define LOG(msg, ...) \
{ \
std::string endlined_msg { std::string{"log: "} + msg }; \
endlined_msg += " (in %s:%i -> %s)\n"; \
printf(endlined_msg.data() __VA_OPT__(,) __VA_ARGS__, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
}
#define TODO(msg) \
{ \
std::string endlined_msg { "todo: " }; \
endlined_msg += msg; \
endlined_msg += " (in %s:%i -> %s)\n"; \
printf(endlined_msg.data(), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
}
#define TODO_ONCE(msg) \
{ \
std::string endlined_msg { "todo: " }; \
static bool has_run##__LINE__ {false}; \
if(!(has_run##__LINE__)) \
{ \
endlined_msg += msg; \
endlined_msg += " (in %s:%i -> %s)\n"; \
printf(endlined_msg.data(), __FILE__, __LINE__, __PRETTY_FUNCTION__); \
has_run##__LINE__ = true; \
} \
}
constexpr uint32_t vert_spv[]
{
0x07230203,0x00010000,0x000d0008,0x00000018,
0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,
0x00000000,0x0003000e,0x00000000,0x00000001,
0x0009000f,0x00000000,0x00000004,0x6e69616d,
0x00000000,0x00000009,0x0000000b,0x00000012,
0x00000015,0x00030003,0x00000002,0x000001c2,
0x000a0004,0x475f4c47,0x4c474f4f,0x70635f45,
0x74735f70,0x5f656c79,0x656e696c,0x7269645f,
0x69746365,0x00006576,0x00080004,0x475f4c47,
0x4c474f4f,0x6e695f45,0x64756c63,0x69645f65,
0x74636572,0x00657669,0x00040005,0x00000004,
0x6e69616d,0x00000000,0x00040005,0x00000009,
0x6c6f6366,0x0000726f,0x00040005,0x0000000b,
0x6c6f6376,0x0000726f,0x00060005,0x00000010,
0x505f6c67,0x65567265,0x78657472,0x00000000,
0x00060006,0x00000010,0x00000000,0x505f6c67,
0x7469736f,0x006e6f69,0x00070006,0x00000010,
0x00000001,0x505f6c67,0x746e696f,0x657a6953,
0x00000000,0x00070006,0x00000010,0x00000002,
0x435f6c67,0x4470696c,0x61747369,0x0065636e,
0x00070006,0x00000010,0x00000003,0x435f6c67,
0x446c6c75,0x61747369,0x0065636e,0x00030005,
0x00000012,0x00000000,0x00030005,0x00000015,
0x00736f70,0x00040047,0x00000009,0x0000001e,
0x00000000,0x00040047,0x0000000b,0x0000001e,
0x00000001,0x00050048,0x00000010,0x00000000,
0x0000000b,0x00000000,0x00050048,0x00000010,
0x00000001,0x0000000b,0x00000001,0x00050048,
0x00000010,0x00000002,0x0000000b,0x00000003,
0x00050048,0x00000010,0x00000003,0x0000000b,
0x00000004,0x00030047,0x00000010,0x00000002,
0x00040047,0x00000015,0x0000001e,0x00000000,
0x00020013,0x00000002,0x00030021,0x00000003,
0x00000002,0x00030016,0x00000006,0x00000020,
0x00040017,0x00000007,0x00000006,0x00000004,
0x00040020,0x00000008,0x00000003,0x00000007,
0x0004003b,0x00000008,0x00000009,0x00000003,
0x00040020,0x0000000a,0x00000001,0x00000007,
0x0004003b,0x0000000a,0x0000000b,0x00000001,
0x00040015,0x0000000d,0x00000020,0x00000000,
0x0004002b,0x0000000d,0x0000000e,0x00000001,
0x0004001c,0x0000000f,0x00000006,0x0000000e,
0x0006001e,0x00000010,0x00000007,0x00000006,
0x0000000f,0x0000000f,0x00040020,0x00000011,
0x00000003,0x00000010,0x0004003b,0x00000011,
0x00000012,0x00000003,0x00040015,0x00000013,
0x00000020,0x00000001,0x0004002b,0x00000013,
0x00000014,0x00000000,0x0004003b,0x0000000a,
0x00000015,0x00000001,0x00050036,0x00000002,
0x00000004,0x00000000,0x00000003,0x000200f8,
0x00000005,0x0004003d,0x00000007,0x0000000c,
0x0000000b,0x0003003e,0x00000009,0x0000000c,
0x0004003d,0x00000007,0x00000016,0x00000015,
0x00050041,0x00000008,0x00000017,0x00000012,
0x00000014,0x0003003e,0x00000017,0x00000016,
0x000100fd,0x00010038
};
constexpr uint32_t frag_spv[]
{
0x07230203,0x00010000,0x000d0008,0x0000000d,
0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,
0x00000000,0x0003000e,0x00000000,0x00000001,
0x0007000f,0x00000004,0x00000004,0x6e69616d,
0x00000000,0x00000009,0x0000000b,0x00030010,
0x00000004,0x00000007,0x00030003,0x00000002,
0x000001c2,0x000a0004,0x475f4c47,0x4c474f4f,
0x70635f45,0x74735f70,0x5f656c79,0x656e696c,
0x7269645f,0x69746365,0x00006576,0x00080004,
0x475f4c47,0x4c474f4f,0x6e695f45,0x64756c63,
0x69645f65,0x74636572,0x00657669,0x00040005,
0x00000004,0x6e69616d,0x00000000,0x00040005,
0x00000009,0x6f6c6f63,0x00000072,0x00040005,
0x0000000b,0x6c6f6366,0x0000726f,0x00040047,
0x00000009,0x0000001e,0x00000000,0x00040047,
0x0000000b,0x0000001e,0x00000000,0x00020013,
0x00000002,0x00030021,0x00000003,0x00000002,
0x00030016,0x00000006,0x00000020,0x00040017,
0x00000007,0x00000006,0x00000004,0x00040020,
0x00000008,0x00000003,0x00000007,0x0004003b,
0x00000008,0x00000009,0x00000003,0x00040020,
0x0000000a,0x00000001,0x00000007,0x0004003b,
0x0000000a,0x0000000b,0x00000001,0x00050036,
0x00000002,0x00000004,0x00000000,0x00000003,
0x000200f8,0x00000005,0x0004003d,0x00000007,
0x0000000c,0x0000000b,0x0003003e,0x00000009,
0x0000000c,0x000100fd,0x00010038
};
constexpr float vertex_data_pos[]
{
1.f, -1.f, 0.f, 1.f,
-1.f, -1.f, 0.f, 1.f,
0.f, 1.f, 0.f, 1.f,
};
constexpr float vertex_data_col[]
{
1.f , 0.f , 0.f, 1.f,
0.f , 1.f , 0.f, 1.f,
0.f , 0.f , 1.f, 1.f,
};
constexpr float vertex_data[]
{
vertex_data_pos[0], vertex_data_pos[1], vertex_data_pos[2] , vertex_data_pos[3] ,
vertex_data_col[0], vertex_data_col[1], vertex_data_col[2] , vertex_data_col[3] ,
vertex_data_pos[4], vertex_data_pos[5], vertex_data_pos[6] , vertex_data_pos[7] ,
vertex_data_col[4], vertex_data_col[5], vertex_data_col[6] , vertex_data_col[7] ,
vertex_data_pos[8], vertex_data_pos[9], vertex_data_pos[10], vertex_data_pos[11],
vertex_data_col[8], vertex_data_col[9], vertex_data_col[10], vertex_data_col[11],
};
namespace psl::webgpu
{
inline namespace
{
template<typename T>
concept IsDestructible = requires(T t) { t.Destroy(); };
template<typename T>
class wgpu_resource_t
{
public:
constexpr auto resource() const noexcept -> const T& { return m_Resource; }
~wgpu_resource_t()
{
if(m_ID == std::numeric_limits<size_t>::max())
return;
LOG("destroying ID[%lu]", m_ID);
if constexpr(IsDestructible<T>)
{
m_Resource.Destroy();
}
}
constexpr wgpu_resource_t(wgpu_resource_t&& other) noexcept : m_Resource(other.m_Resource), m_ID(other.m_ID)
{
other.m_ID = std::numeric_limits<size_t>::max();
}
constexpr wgpu_resource_t& operator=(wgpu_resource_t&& other) noexcept
{
if(this != &other)
{
m_ID = other.m_ID;
m_Resource = other.m_Resource;
other.m_ID = std::numeric_limits<size_t>::max();
}
return *this;
}
wgpu_resource_t(const wgpu_resource_t& other) = delete;
wgpu_resource_t& operator=(const wgpu_resource_t& other) = delete;
protected:
wgpu_resource_t(T resource) noexcept : m_Resource(resource)
{
LOG("creating ID[%lu]", m_ID);
}
wgpu_resource_t() noexcept
{
LOG("creating ID[%lu]", m_ID);
}
T m_Resource{};
size_t m_ID{m_IDGenerator++};
static inline size_t m_IDGenerator = 0;
};
}
class device : public wgpu_resource_t<wgpu::Device>
{
public:
device() : wgpu_resource_t(wgpu::Device::Acquire(emscripten_webgpu_get_device()))
{
m_Resource.SetUncapturedErrorCallback(
[](WGPUErrorType errorType, const char* message, void*) {
printf("%d: %s\n", errorType, message);
}, nullptr);
}
};
class surface : public wgpu_resource_t<wgpu::Surface>
{
public:
surface()
{
wgpu::SurfaceDescriptorFromCanvasHTMLSelector canvasDesc{};
canvasDesc.selector = "#canvas";
wgpu::SurfaceDescriptor surfDesc{};
surfDesc.nextInChain = &canvasDesc;
wgpu::Instance instance{}; // null instance
m_Resource = instance.CreateSurface(&surfDesc);
}
};
class queue : public wgpu_resource_t<wgpu::Queue>
{
public:
queue(device& device) : wgpu_resource_t(device.resource().GetDefaultQueue()) {}
};
class texture : public wgpu_resource_t<wgpu::Texture>
{
public:
texture(device& device, size_t width, size_t height, size_t layers, wgpu::TextureFormat format, wgpu::TextureUsage usage)
{
wgpu::Extent3D dimensions{};
dimensions.width = width;
dimensions.height = height;
dimensions.depth = layers;
wgpu::TextureDescriptor desc{};
desc.size = dimensions;
desc.usage = usage;
desc.format = format;
m_Resource = device.resource().CreateTexture(&desc);
}
};
struct with_depth_texture_t{};
inline constexpr auto with_depth_texture = with_depth_texture_t{};
class swapchain : public wgpu_resource_t<wgpu::SwapChain>
{
public:
swapchain(device& device, surface& surface, size_t width = 1280, size_t height = 720, wgpu::TextureFormat format = DEFAULT_SWAPCHAIN_FORMAT, wgpu::PresentMode presentMode = wgpu::PresentMode::Fifo, std::optional<wgpu::TextureFormat> depthFormat = std::nullopt) :
m_Device(device),
m_Surface(surface),
m_Width(width),
m_Height(height),
m_Format(format),
m_DepthFormat(depthFormat.value_or(DEFAULT_DEPTH_FORMAT)),
m_PresentMode(presentMode)
{
LOG("creating swapchain [width: %lu] [height: %lu]", width, height);
recreate_swapchain(depthFormat.has_value());
}
void resize(size_t width, size_t height)
{
m_Width = width;
m_Height = height;
LOG("recreating swapchain [width: %lu] [height: %lu]", width, height);
recreate_swapchain(m_DepthTexture.has_value());
}
constexpr auto width() const noexcept -> size_t { return m_Width; }
constexpr auto height() const noexcept -> size_t { return m_Height; }
auto depth_resource() const noexcept -> const auto& { return m_DepthTexture->resource(); }
private:
void recreate_swapchain(bool withDepth)
{
wgpu::SwapChainDescriptor scDesc{};
scDesc.usage = wgpu::TextureUsage::RenderAttachment;
scDesc.format = m_Format;
scDesc.width = m_Width;
scDesc.height = m_Height;
scDesc.presentMode = m_PresentMode;
m_Resource = m_Device.resource().CreateSwapChain(m_Surface.resource(), &scDesc);
if(withDepth)
{
m_DepthTexture = std::move(texture{m_Device, m_Width, m_Height, 1u, m_DepthFormat, wgpu::TextureUsage::RenderAttachment});
}
}
device& m_Device;
surface& m_Surface;
std::optional<texture> m_DepthTexture { std::nullopt };
wgpu::TextureFormat m_Format{DEFAULT_SWAPCHAIN_FORMAT};
wgpu::TextureFormat m_DepthFormat{DEFAULT_DEPTH_FORMAT};
wgpu::PresentMode m_PresentMode{wgpu::PresentMode::Fifo};
size_t m_Width{1280};
size_t m_Height{720};
};
enum class module_type
{
VERT,
FRAG,
COMPUTE,
};
class generic_module : public wgpu_resource_t<wgpu::ShaderModule>
{
public:
template<size_t N>
generic_module(device& device, const uint32_t (&bytecode)[N], module_type type) : m_Type(type)
{
wgpu::ShaderModuleSPIRVDescriptor spirvDesc{};
spirvDesc.codeSize = N;
spirvDesc.code = bytecode;
wgpu::ShaderModuleDescriptor descriptor{};
descriptor.nextInChain = &spirvDesc;
m_Resource = device.resource().CreateShaderModule(&descriptor);
}
protected:
module_type m_Type; // unneeded, but good for debug logging
};
template<module_type Type>
class module final : public generic_module
{
public:
module(device& device, const auto& bytecode) : generic_module(device, bytecode, Type) {}
};
using module_vert = module<module_type::VERT>;
class generic_buffer : public wgpu_resource_t<wgpu::Buffer>
{
public:
generic_buffer(device& device, size_t size, wgpu::BufferUsage usage)
{
wgpu::BufferDescriptor descriptor{};
descriptor.size = size;
descriptor.usage = wgpu::BufferUsage::Vertex;
descriptor.mappedAtCreation = true;
m_Resource = device.resource().CreateBuffer(&descriptor);
}
template<size_t N>
void map(const float (&data)[N])
{
map(data, N * sizeof(float));
}
void map(const float* data, size_t size)
{
auto mapped_range = m_Resource.GetMappedRange();
memcpy(mapped_range, data, size);
m_Resource.Unmap();
}
};
enum class buffer_usage
{
VBO,
IBO,
UBO,
SSBO,
};
inline namespace
{
constexpr auto to_wgpu(buffer_usage usage) -> wgpu::BufferUsage
{
switch(usage)
{
case buffer_usage::VBO: return wgpu::BufferUsage::Vertex;
case buffer_usage::IBO: return wgpu::BufferUsage::Index;
case buffer_usage::UBO: return wgpu::BufferUsage::Uniform;
case buffer_usage::SSBO: return wgpu::BufferUsage::Storage;
}
throw std::exception();
return wgpu::BufferUsage::None;
}
}
template<buffer_usage Usage>
class buffer : public generic_buffer
{
public:
buffer(device& device, size_t size) : generic_buffer(device, size, to_wgpu(Usage)){}
};
class pipeline : public wgpu_resource_t<wgpu::RenderPipeline>
{
public:
pipeline(device& device, module<module_type::VERT>& vertModule, module<module_type::FRAG>& fragModule)
{
TODO("pipeline is currently hardcoded, good for fast example, bad for abstraction.");
wgpu::ProgrammableStageDescriptor fragmentStage{};
fragmentStage.module = fragModule.resource();
fragmentStage.entryPoint = "main";
wgpu::ProgrammableStageDescriptor vertexStage{};
vertexStage.module = vertModule.resource();
vertexStage.entryPoint = "main";
wgpu::ColorStateDescriptor colorStateDescriptor{};
colorStateDescriptor.format = DEFAULT_SWAPCHAIN_FORMAT;
wgpu::DepthStencilStateDescriptor depthstencilStateDescriptor{};
depthstencilStateDescriptor.format = DEFAULT_DEPTH_FORMAT;
depthstencilStateDescriptor.depthCompare = wgpu::CompareFunction::Less;
depthstencilStateDescriptor.depthWriteEnabled = true;
wgpu::PipelineLayoutDescriptor pl{};
pl.bindGroupLayoutCount = 0;
pl.bindGroupLayouts = nullptr;
// describes attribute layout
wgpu::VertexStateDescriptor vertexDescr{};
wgpu::VertexAttributeDescriptor vertAttributes[2]{};
wgpu::VertexBufferLayoutDescriptor vertexAttrDescr{};
vertAttributes[0].format = wgpu::VertexFormat::Float4;
vertAttributes[0].offset = 0;
vertAttributes[0].shaderLocation = 0;
vertAttributes[1].format = wgpu::VertexFormat::Float4;
vertAttributes[1].offset = 4 * 4; // 4 floats of 32 bits.
vertAttributes[1].shaderLocation = 1;
vertexAttrDescr.attributeCount = 2;
vertexAttrDescr.attributes = vertAttributes;
vertexAttrDescr.arrayStride = 2 * (4 * 4); // 2 attributes of float4 (32 bits per float)
vertexDescr.vertexBufferCount = 1;
vertexDescr.vertexBuffers = &vertexAttrDescr;
wgpu::RenderPipelineDescriptor descriptor{};
descriptor.layout = device.resource().CreatePipelineLayout(&pl);
descriptor.vertexStage = vertexStage;
descriptor.vertexState = &vertexDescr;
descriptor.fragmentStage = &fragmentStage;
descriptor.colorStateCount = 1;
descriptor.colorStates = &colorStateDescriptor;
descriptor.primitiveTopology = wgpu::PrimitiveTopology::TriangleList;
descriptor.depthStencilState = &depthstencilStateDescriptor;
m_Resource = device.resource().CreateRenderPipeline(&descriptor);
}
};
class renderpass : public wgpu_resource_t<wgpu::RenderPassDescriptor>
{
public:
renderpass(swapchain& swapchain) : m_SwapChain(swapchain)
{
m_ColourAttachment.attachment = swapchain.resource().GetCurrentTextureView();
m_ColourAttachment.clearColor = { 0.6, 0.6, 0.6, 1.0 };
m_ColourAttachment.storeOp = wgpu::StoreOp::Store;
m_DepthStencilAttachment.attachment = swapchain.depth_resource().CreateView();
m_DepthStencilAttachment.clearDepth = 1.0;
m_DepthStencilAttachment.depthStoreOp = wgpu::StoreOp::Store;
m_DepthStencilAttachment.clearStencil = 0;
m_DepthStencilAttachment.stencilStoreOp = wgpu::StoreOp::Clear;
m_Resource.colorAttachments = &m_ColourAttachment;
m_Resource.colorAttachmentCount = 1;
m_Resource.depthStencilAttachment = &m_DepthStencilAttachment;
}
void draw(device& device, queue& queue)
{
m_ColourAttachment.attachment = m_SwapChain.resource().GetCurrentTextureView();
auto commandEncoder = device.resource().CreateCommandEncoder();
auto renderPassEncoder = commandEncoder.BeginRenderPass(&m_Resource);
renderPassEncoder.ExecuteBundles(1, &m_RenderBundle);
renderPassEncoder.EndPass();
auto commandBuffer = commandEncoder.Finish();
queue.resource().Submit(1u, &commandBuffer);
}
auto begin(device& device) -> auto&
{
wgpu::RenderBundleEncoderDescriptor descriptor{};
descriptor.colorFormatsCount = 1;
descriptor.colorFormats = &DEFAULT_SWAPCHAIN_FORMAT;
descriptor.depthStencilFormat = DEFAULT_DEPTH_FORMAT;
descriptor.sampleCount = 1;
m_RenderBundleEncoder = device.resource().CreateRenderBundleEncoder(&descriptor);
return m_RenderBundleEncoder;
}
void end()
{
m_RenderBundle = m_RenderBundleEncoder.Finish();
}
private:
swapchain& m_SwapChain;
wgpu::RenderBundle m_RenderBundle;
wgpu::RenderBundleEncoder m_RenderBundleEncoder;
wgpu::RenderPassColorAttachmentDescriptor m_ColourAttachment{};
wgpu::RenderPassDepthStencilAttachmentDescriptor m_DepthStencilAttachment{};
};
}
std::unique_ptr<psl::webgpu::device> m_Device = nullptr;
std::unique_ptr<psl::webgpu::surface> m_Surface = nullptr;
std::unique_ptr<psl::webgpu::swapchain> m_SwapChain = nullptr;
std::unique_ptr<psl::webgpu::pipeline> m_Pipeline = nullptr;
std::unique_ptr<psl::webgpu::buffer<psl::webgpu::buffer_usage::VBO>> m_VertexBuffer = nullptr;
std::unique_ptr<psl::webgpu::queue> m_Queue = nullptr;
std::unique_ptr<psl::webgpu::renderpass> m_RenderPass = nullptr;
void render_loop()
{
m_RenderPass->draw(*m_Device, *m_Queue);
}
void resize_swapchain()
{
double width{}, height{};
emscripten_get_element_css_size("#canvas", &width, &height);
m_SwapChain->resize(width, height);
}
EMSCRIPTEN_BINDINGS(Module)
{
emscripten::function("resize_swapchain", &resize_swapchain);
}
int main()
{
m_Device = std::make_unique<psl::webgpu::device>();
m_Surface = std::make_unique<psl::webgpu::surface>();
double width{}, height{};
emscripten_get_element_css_size("#canvas", &width, &height);
m_SwapChain = std::make_unique<psl::webgpu::swapchain>(*m_Device, *m_Surface, width, height, DEFAULT_SWAPCHAIN_FORMAT, wgpu::PresentMode::Fifo, DEFAULT_DEPTH_FORMAT);
auto vertModule = psl::webgpu::module<psl::webgpu::module_type::VERT>{*m_Device, vert_spv};
auto fragModule = psl::webgpu::module<psl::webgpu::module_type::FRAG>{*m_Device, frag_spv};
m_Pipeline = std::make_unique<psl::webgpu::pipeline>(*m_Device, vertModule, fragModule);
m_VertexBuffer = std::make_unique<psl::webgpu::buffer<psl::webgpu::buffer_usage::VBO>>(*m_Device, sizeof(vertex_data) * sizeof(vertex_data[0]));
m_VertexBuffer->map(vertex_data);
m_Queue = std::make_unique<psl::webgpu::queue>(*m_Device);
m_RenderPass = std::make_unique<psl::webgpu::renderpass>(*m_SwapChain);
// we record the draw instruction once into a GPURenderBundle contained inside the renderpass abstraction.
// this way we record once, and resubmit our non-dynamic data, rather than re-recording every frame.
{
auto renderPass = m_RenderPass->begin(*m_Device);
renderPass.SetPipeline(m_Pipeline->resource());
renderPass.SetVertexBuffer(0, m_VertexBuffer->resource());
renderPass.Draw(3, 1, 0, 0);
m_RenderPass->end();
}
emscripten_set_main_loop(render_loop, 0, false);
return 0;
}