@@ -64,13 +64,13 @@ void renderPipelineSafeRelease(const WGPURenderPipeline pipeline)
64
64
Renderer::Renderer (const RendererDescriptor& rendererDesc, const GpuContext& gpuContext)
65
65
: frameDataBuffer(nullptr ),
66
66
pixelBuffer (nullptr ),
67
- computeImagesBindGroup(nullptr ),
67
+ computePixelsBindGroup(nullptr ),
68
+ computePipeline(nullptr ),
68
69
vertexBuffer(nullptr ),
69
70
vertexBufferByteSize(0 ),
70
71
uniformsBuffer(nullptr ),
71
72
uniformsBindGroup(nullptr ),
72
- renderImagesBindGroup(nullptr ),
73
- computePipeline(nullptr ),
73
+ renderPixelsBindGroup(nullptr ),
74
74
renderPipeline(nullptr ),
75
75
currentFramebufferSize(rendererDesc.currentFramebufferSize),
76
76
frameCount(0 )
@@ -102,6 +102,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
102
102
103
103
{
104
104
// Shader module
105
+
105
106
const char * const computeSource = R"(
106
107
struct FrameData {
107
108
dimensions: vec2<u32>,
@@ -177,14 +178,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
177
178
const WGPUShaderModule computeModule =
178
179
wgpuDeviceCreateShaderModule (gpuContext.device , &shaderDesc);
179
180
180
- // Image bind group layout
181
+ // pixels bind group layout
181
182
182
183
assert (pixelBufferNumBytes < std::numeric_limits<std::uint64_t >::max ());
183
184
184
- std::array<WGPUBindGroupLayoutEntry, 2 > imagesGroupLayoutEntries {
185
+ std::array<WGPUBindGroupLayoutEntry, 2 > pixelsBindGroupLayoutEntries {
185
186
WGPUBindGroupLayoutEntry{
186
187
.nextInChain = nullptr ,
187
- .binding = 0 , // binding index used in the @binding attribute
188
+ .binding = 0 ,
188
189
.visibility = WGPUShaderStage_Compute,
189
190
.buffer =
190
191
WGPUBufferBindingLayout{
@@ -246,18 +247,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
246
247
},
247
248
};
248
249
249
- const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc {
250
+ const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc {
250
251
.nextInChain = nullptr ,
251
252
.label = " images group layout (compute pipeline)" ,
252
- .entryCount = imagesGroupLayoutEntries .size (),
253
- .entries = imagesGroupLayoutEntries .data (),
253
+ .entryCount = pixelsBindGroupLayoutEntries .size (),
254
+ .entries = pixelsBindGroupLayoutEntries .data (),
254
255
};
255
- const WGPUBindGroupLayout imagesGroupLayout =
256
- wgpuDeviceCreateBindGroupLayout (gpuContext.device , &imagesGroupLayoutDesc );
256
+ const WGPUBindGroupLayout pixelsBindGroupLayout =
257
+ wgpuDeviceCreateBindGroupLayout (gpuContext.device , &pixelsBindGroupLayoutDesc );
257
258
258
259
// Bind group
259
260
260
- std::array<WGPUBindGroupEntry, 2 > imagesGroupEntries {
261
+ std::array<WGPUBindGroupEntry, 2 > pixelsBindGroupEntries {
261
262
WGPUBindGroupEntry{
262
263
.nextInChain = nullptr ,
263
264
.binding = 0 ,
@@ -278,23 +279,22 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
278
279
},
279
280
};
280
281
281
- const WGPUBindGroupDescriptor imagesGroupDesc {
282
+ const WGPUBindGroupDescriptor pixelsBindGroupDesc {
282
283
.nextInChain = nullptr ,
283
284
.label = " image bind group" ,
284
- .layout = imagesGroupLayout ,
285
- .entryCount = imagesGroupEntries .size (),
286
- .entries = imagesGroupEntries .data (),
285
+ .layout = pixelsBindGroupLayout ,
286
+ .entryCount = pixelsBindGroupEntries .size (),
287
+ .entries = pixelsBindGroupEntries .data (),
287
288
};
288
- // TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
289
- computeImagesBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &imagesGroupDesc);
289
+ computePixelsBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &pixelsBindGroupDesc);
290
290
291
291
// Pipeline layout
292
292
293
293
const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
294
294
.nextInChain = nullptr ,
295
295
.label = " Compute pipeline layout" ,
296
296
.bindGroupLayoutCount = 1 ,
297
- .bindGroupLayouts = &imagesGroupLayout ,
297
+ .bindGroupLayouts = &pixelsBindGroupLayout ,
298
298
};
299
299
const WGPUPipelineLayout pipelineLayout =
300
300
wgpuDeviceCreatePipelineLayout (gpuContext.device , &pipelineLayoutDesc);
@@ -315,7 +315,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
315
315
computePipeline = wgpuDeviceCreateComputePipeline (gpuContext.device , &computeDesc);
316
316
}
317
317
318
- auto [buffer, byteSize] = [&gpuContext]() -> std::tuple<WGPUBuffer, std:: size_t > {
318
+ {
319
319
const std::array<Vertex, 6 > vertexData{
320
320
Vertex{{-0 .5f , -0 .5f }, {0 .0f , 0 .0f }},
321
321
Vertex{{0 .5f , -0 .5f }, {1 .0f , 0 .0f }},
@@ -324,35 +324,30 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
324
324
Vertex{{-0 .5f , 0 .5f }, {0 .0f , 1 .0f }},
325
325
Vertex{{-0 .5f , -0 .5f }, {0 .0f , 0 .0f }},
326
326
};
327
- const std:: size_t vertexDataByteSize = sizeof (Vertex) * vertexData.size ();
328
- assert (vertexDataByteSize <= std::numeric_limits<std::uint64_t >::max ());
327
+ vertexBufferByteSize = sizeof (Vertex) * vertexData.size ();
328
+ assert (vertexBufferByteSize <= std::numeric_limits<std::uint64_t >::max ());
329
329
330
330
const WGPUBufferDescriptor vertexBufferDesc{
331
331
.nextInChain = nullptr ,
332
332
.label = " Vertex buffer" ,
333
333
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
334
- .size = static_cast <std::uint64_t >(vertexDataByteSize ),
334
+ .size = static_cast <std::uint64_t >(vertexBufferByteSize ),
335
335
.mappedAtCreation = false ,
336
336
};
337
- const WGPUBuffer vertexBuffer =
338
- wgpuDeviceCreateBuffer (gpuContext.device , &vertexBufferDesc);
337
+ vertexBuffer = wgpuDeviceCreateBuffer (gpuContext.device , &vertexBufferDesc);
339
338
wgpuQueueWriteBuffer (
340
- gpuContext.queue , vertexBuffer, 0 , vertexData.data (), vertexDataByteSize);
341
-
342
- return std::make_tuple (vertexBuffer, vertexDataByteSize);
343
- }();
344
- vertexBuffer = buffer;
345
- vertexBufferByteSize = byteSize;
339
+ gpuContext.queue , vertexBuffer, 0 , vertexData.data (), vertexBufferByteSize);
340
+ }
346
341
347
- uniformsBuffer = [&gpuContext]() -> WGPUBuffer {
342
+ {
348
343
const WGPUBufferDescriptor uniformDesc{
349
344
.nextInChain = nullptr ,
350
345
.label = " Uniform buffer" ,
351
346
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
352
347
.size = sizeof (glm::mat4),
353
348
.mappedAtCreation = false ,
354
349
};
355
- const WGPUBuffer buffer = wgpuDeviceCreateBuffer (gpuContext.device , &uniformDesc);
350
+ uniformsBuffer = wgpuDeviceCreateBuffer (gpuContext.device , &uniformDesc);
356
351
357
352
{
358
353
// DirectX, Metal, wgpu share the same left-handed coordinate system
@@ -361,14 +356,12 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
361
356
glm::mat4 viewProjectionMatrix = glm::orthoLH (-0 .5f , 0 .5f , -0 .5f , 0 .5f , -1 .f , 1 .f );
362
357
wgpuQueueWriteBuffer (
363
358
gpuContext.queue ,
364
- buffer ,
359
+ uniformsBuffer ,
365
360
0 ,
366
361
&viewProjectionMatrix[0 ],
367
362
sizeof (viewProjectionMatrix));
368
363
}
369
-
370
- return buffer;
371
- }();
364
+ }
372
365
373
366
{
374
367
// Blend state for color target
@@ -501,7 +494,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
501
494
502
495
// uniforms bind group layout
503
496
504
- const WGPUBindGroupLayoutEntry uniformsGroupLayoutEntry {
497
+ const WGPUBindGroupLayoutEntry uniformsBindGroupLayoutEntry {
505
498
.nextInChain = nullptr ,
506
499
.binding = 0 , // binding index used in the @binding attribute
507
500
.visibility = WGPUShaderStage_Vertex,
@@ -532,21 +525,21 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
532
525
},
533
526
};
534
527
535
- const WGPUBindGroupLayoutDescriptor uniformsGroupLayoutDesc {
528
+ const WGPUBindGroupLayoutDescriptor uniformsBindGroupLayoutDesc {
536
529
.nextInChain = nullptr ,
537
530
.label = " uniforms group layout" ,
538
531
.entryCount = 1 ,
539
- .entries = &uniformsGroupLayoutEntry ,
532
+ .entries = &uniformsBindGroupLayoutEntry ,
540
533
};
541
- const WGPUBindGroupLayout uniformsGroupLayout =
542
- wgpuDeviceCreateBindGroupLayout (gpuContext.device , &uniformsGroupLayoutDesc );
534
+ const WGPUBindGroupLayout uniformsBindGroupLayout =
535
+ wgpuDeviceCreateBindGroupLayout (gpuContext.device , &uniformsBindGroupLayoutDesc );
543
536
544
537
// images bind group layout
545
538
546
- std::array<WGPUBindGroupLayoutEntry, 2 > imagesGroupLayoutEntries {
539
+ std::array<WGPUBindGroupLayoutEntry, 2 > pixelsBindGroupLayoutEntries {
547
540
WGPUBindGroupLayoutEntry{
548
541
.nextInChain = nullptr ,
549
- .binding = 0 , // binding index used in the @binding attribute
542
+ .binding = 0 ,
550
543
.visibility = WGPUShaderStage_Fragment,
551
544
.buffer =
552
545
WGPUBufferBindingLayout{
@@ -608,18 +601,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
608
601
},
609
602
};
610
603
611
- const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc {
604
+ const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc {
612
605
.nextInChain = nullptr ,
613
606
.label = " images group layout (render pipeline)" ,
614
- .entryCount = imagesGroupLayoutEntries .size (),
615
- .entries = imagesGroupLayoutEntries .data (),
607
+ .entryCount = pixelsBindGroupLayoutEntries .size (),
608
+ .entries = pixelsBindGroupLayoutEntries .data (),
616
609
};
617
- const WGPUBindGroupLayout imagesGroupLayout =
618
- wgpuDeviceCreateBindGroupLayout (gpuContext.device , &imagesGroupLayoutDesc );
610
+ const WGPUBindGroupLayout pixelsBindGroupLayout =
611
+ wgpuDeviceCreateBindGroupLayout (gpuContext.device , &pixelsBindGroupLayoutDesc );
619
612
620
613
std::array<WGPUBindGroupLayout, 2 > bindGroupLayouts{
621
- uniformsGroupLayout ,
622
- imagesGroupLayout ,
614
+ uniformsBindGroupLayout ,
615
+ pixelsBindGroupLayout ,
623
616
};
624
617
625
618
const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
@@ -633,7 +626,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
633
626
634
627
// uniforms bind group
635
628
636
- const WGPUBindGroupEntry uniformsGroupEntry {
629
+ const WGPUBindGroupEntry uniformsBindGroupEntry {
637
630
.nextInChain = nullptr ,
638
631
.binding = 0 ,
639
632
.buffer = uniformsBuffer,
@@ -643,16 +636,16 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
643
636
.textureView = nullptr ,
644
637
};
645
638
646
- const WGPUBindGroupDescriptor uniformsGroupDesc {
639
+ const WGPUBindGroupDescriptor uniformsBindGroupDesc {
647
640
.nextInChain = nullptr ,
648
641
.label = " Bind group" ,
649
- .layout = uniformsGroupLayout ,
642
+ .layout = uniformsBindGroupLayout ,
650
643
.entryCount = 1 ,
651
- .entries = &uniformsGroupEntry ,
644
+ .entries = &uniformsBindGroupEntry ,
652
645
};
653
- uniformsBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &uniformsGroupDesc );
646
+ uniformsBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &uniformsBindGroupDesc );
654
647
655
- std::array<WGPUBindGroupEntry, 2 > imagesGroupEntries {
648
+ std::array<WGPUBindGroupEntry, 2 > pixelsBindGroupEntries {
656
649
WGPUBindGroupEntry{
657
650
.nextInChain = nullptr ,
658
651
.binding = 0 ,
@@ -673,15 +666,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
673
666
},
674
667
};
675
668
676
- const WGPUBindGroupDescriptor imagesGroupDesc {
669
+ const WGPUBindGroupDescriptor pixelsBindGroupDesc {
677
670
.nextInChain = nullptr ,
678
671
.label = " image bind group" ,
679
- .layout = imagesGroupLayout ,
680
- .entryCount = imagesGroupEntries .size (),
681
- .entries = imagesGroupEntries .data (),
672
+ .layout = pixelsBindGroupLayout ,
673
+ .entryCount = pixelsBindGroupEntries .size (),
674
+ .entries = pixelsBindGroupEntries .data (),
682
675
};
683
- // TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
684
- renderImagesBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &imagesGroupDesc);
676
+ renderPixelsBindGroup = wgpuDeviceCreateBindGroup (gpuContext.device , &pixelsBindGroupDesc);
685
677
686
678
const WGPURenderPipelineDescriptor pipelineDesc{
687
679
.nextInChain = nullptr ,
@@ -728,18 +720,18 @@ Renderer::~Renderer()
728
720
{
729
721
renderPipelineSafeRelease (renderPipeline);
730
722
renderPipeline = nullptr ;
731
- computePipelineSafeRelease (computePipeline);
732
- computePipeline = nullptr ;
733
- bindGroupSafeRelease (renderImagesBindGroup);
734
- renderImagesBindGroup = nullptr ;
723
+ bindGroupSafeRelease (renderPixelsBindGroup);
724
+ renderPixelsBindGroup = nullptr ;
735
725
bindGroupSafeRelease (uniformsBindGroup);
736
726
uniformsBindGroup = nullptr ;
737
727
bufferSafeRelease (uniformsBuffer);
738
728
uniformsBuffer = nullptr ;
739
729
bufferSafeRelease (vertexBuffer);
740
730
vertexBuffer = nullptr ;
741
- bindGroupSafeRelease (computeImagesBindGroup);
742
- computeImagesBindGroup = nullptr ;
731
+ computePipelineSafeRelease (computePipeline);
732
+ computePipeline = nullptr ;
733
+ bindGroupSafeRelease (computePixelsBindGroup);
734
+ computePixelsBindGroup = nullptr ;
743
735
bufferSafeRelease (pixelBuffer);
744
736
pixelBuffer = nullptr ;
745
737
bufferSafeRelease (frameDataBuffer);
@@ -791,7 +783,7 @@ void Renderer::render(const GpuContext& gpuContext)
791
783
792
784
wgpuComputePassEncoderSetPipeline (computePassEncoder, computePipeline);
793
785
wgpuComputePassEncoderSetBindGroup (
794
- computePassEncoder, 0 , computeImagesBindGroup , 0 , nullptr );
786
+ computePassEncoder, 0 , computePixelsBindGroup , 0 , nullptr );
795
787
796
788
const Extent2u workgroupSize{.x = 8 , .y = 8 };
797
789
const Extent2u numWorkgroups{
@@ -806,8 +798,8 @@ void Renderer::render(const GpuContext& gpuContext)
806
798
}
807
799
808
800
{
809
- // TODO: rename renderPass -> renderPassEncoder
810
- const WGPURenderPassEncoder renderPass = [encoder, nextTexture]() -> WGPURenderPassEncoder {
801
+ const WGPURenderPassEncoder renderPassEncoder = [encoder,
802
+ nextTexture]() -> WGPURenderPassEncoder {
811
803
const WGPURenderPassColorAttachment renderPassColorAttachment{
812
804
.nextInChain = nullptr ,
813
805
.view = nextTexture,
@@ -832,15 +824,16 @@ void Renderer::render(const GpuContext& gpuContext)
832
824
}();
833
825
834
826
{
835
- wgpuRenderPassEncoderSetPipeline (renderPass, renderPipeline);
836
- wgpuRenderPassEncoderSetBindGroup (renderPass, 0 , uniformsBindGroup, 0 , nullptr );
837
- wgpuRenderPassEncoderSetBindGroup (renderPass, 1 , renderImagesBindGroup, 0 , nullptr );
827
+ wgpuRenderPassEncoderSetPipeline (renderPassEncoder, renderPipeline);
828
+ wgpuRenderPassEncoderSetBindGroup (renderPassEncoder, 0 , uniformsBindGroup, 0 , nullptr );
829
+ wgpuRenderPassEncoderSetBindGroup (
830
+ renderPassEncoder, 1 , renderPixelsBindGroup, 0 , nullptr );
838
831
wgpuRenderPassEncoderSetVertexBuffer (
839
- renderPass , 0 , vertexBuffer, 0 , vertexBufferByteSize);
840
- wgpuRenderPassEncoderDraw (renderPass , 6 , 1 , 0 , 0 );
832
+ renderPassEncoder , 0 , vertexBuffer, 0 , vertexBufferByteSize);
833
+ wgpuRenderPassEncoderDraw (renderPassEncoder , 6 , 1 , 0 , 0 );
841
834
}
842
835
843
- wgpuRenderPassEncoderEnd (renderPass );
836
+ wgpuRenderPassEncoderEnd (renderPassEncoder );
844
837
}
845
838
846
839
const WGPUCommandBuffer cmdBuffer = [encoder]() {
@@ -854,4 +847,14 @@ void Renderer::render(const GpuContext& gpuContext)
854
847
855
848
wgpuTextureViewRelease (nextTexture);
856
849
}
850
+
851
+ void Renderer::resizeFramebuffer (const Extent2i& newSize)
852
+ {
853
+ if (newSize.x <= 0 || newSize.y <= 0 )
854
+ {
855
+ return ;
856
+ }
857
+
858
+ currentFramebufferSize = newSize;
859
+ }
857
860
} // namespace pt
0 commit comments