Skip to content

Commit 645c09c

Browse files
committed
Fix inconsistent naming, TODOs, unneeded immediate lambdas
1 parent 0c56da7 commit 645c09c

File tree

3 files changed

+92
-86
lines changed

3 files changed

+92
-86
lines changed

src/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ int main()
4646
{
4747
curFramebufferSize = newFramebufferSize;
4848
gpuContext.resizeFramebuffer(curFramebufferSize);
49+
renderer.resizeFramebuffer(curFramebufferSize);
4950
}
5051
}
5152

src/renderer.cpp

Lines changed: 81 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ void renderPipelineSafeRelease(const WGPURenderPipeline pipeline)
6464
Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpuContext)
6565
: frameDataBuffer(nullptr),
6666
pixelBuffer(nullptr),
67-
computeImagesBindGroup(nullptr),
67+
computePixelsBindGroup(nullptr),
68+
computePipeline(nullptr),
6869
vertexBuffer(nullptr),
6970
vertexBufferByteSize(0),
7071
uniformsBuffer(nullptr),
7172
uniformsBindGroup(nullptr),
72-
renderImagesBindGroup(nullptr),
73-
computePipeline(nullptr),
73+
renderPixelsBindGroup(nullptr),
7474
renderPipeline(nullptr),
7575
currentFramebufferSize(rendererDesc.currentFramebufferSize),
7676
frameCount(0)
@@ -102,6 +102,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
102102

103103
{
104104
// Shader module
105+
105106
const char* const computeSource = R"(
106107
struct FrameData {
107108
dimensions: vec2<u32>,
@@ -177,14 +178,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
177178
const WGPUShaderModule computeModule =
178179
wgpuDeviceCreateShaderModule(gpuContext.device, &shaderDesc);
179180

180-
// Image bind group layout
181+
// pixels bind group layout
181182

182183
assert(pixelBufferNumBytes < std::numeric_limits<std::uint64_t>::max());
183184

184-
std::array<WGPUBindGroupLayoutEntry, 2> imagesGroupLayoutEntries{
185+
std::array<WGPUBindGroupLayoutEntry, 2> pixelsBindGroupLayoutEntries{
185186
WGPUBindGroupLayoutEntry{
186187
.nextInChain = nullptr,
187-
.binding = 0, // binding index used in the @binding attribute
188+
.binding = 0,
188189
.visibility = WGPUShaderStage_Compute,
189190
.buffer =
190191
WGPUBufferBindingLayout{
@@ -246,18 +247,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
246247
},
247248
};
248249

249-
const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc{
250+
const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc{
250251
.nextInChain = nullptr,
251252
.label = "images group layout (compute pipeline)",
252-
.entryCount = imagesGroupLayoutEntries.size(),
253-
.entries = imagesGroupLayoutEntries.data(),
253+
.entryCount = pixelsBindGroupLayoutEntries.size(),
254+
.entries = pixelsBindGroupLayoutEntries.data(),
254255
};
255-
const WGPUBindGroupLayout imagesGroupLayout =
256-
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &imagesGroupLayoutDesc);
256+
const WGPUBindGroupLayout pixelsBindGroupLayout =
257+
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &pixelsBindGroupLayoutDesc);
257258

258259
// Bind group
259260

260-
std::array<WGPUBindGroupEntry, 2> imagesGroupEntries{
261+
std::array<WGPUBindGroupEntry, 2> pixelsBindGroupEntries{
261262
WGPUBindGroupEntry{
262263
.nextInChain = nullptr,
263264
.binding = 0,
@@ -278,23 +279,22 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
278279
},
279280
};
280281

281-
const WGPUBindGroupDescriptor imagesGroupDesc{
282+
const WGPUBindGroupDescriptor pixelsBindGroupDesc{
282283
.nextInChain = nullptr,
283284
.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(),
287288
};
288-
// TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
289-
computeImagesBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &imagesGroupDesc);
289+
computePixelsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &pixelsBindGroupDesc);
290290

291291
// Pipeline layout
292292

293293
const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
294294
.nextInChain = nullptr,
295295
.label = "Compute pipeline layout",
296296
.bindGroupLayoutCount = 1,
297-
.bindGroupLayouts = &imagesGroupLayout,
297+
.bindGroupLayouts = &pixelsBindGroupLayout,
298298
};
299299
const WGPUPipelineLayout pipelineLayout =
300300
wgpuDeviceCreatePipelineLayout(gpuContext.device, &pipelineLayoutDesc);
@@ -315,7 +315,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
315315
computePipeline = wgpuDeviceCreateComputePipeline(gpuContext.device, &computeDesc);
316316
}
317317

318-
auto [buffer, byteSize] = [&gpuContext]() -> std::tuple<WGPUBuffer, std::size_t> {
318+
{
319319
const std::array<Vertex, 6> vertexData{
320320
Vertex{{-0.5f, -0.5f}, {0.0f, 0.0f}},
321321
Vertex{{0.5f, -0.5f}, {1.0f, 0.0f}},
@@ -324,35 +324,30 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
324324
Vertex{{-0.5f, 0.5f}, {0.0f, 1.0f}},
325325
Vertex{{-0.5f, -0.5f}, {0.0f, 0.0f}},
326326
};
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());
329329

330330
const WGPUBufferDescriptor vertexBufferDesc{
331331
.nextInChain = nullptr,
332332
.label = "Vertex buffer",
333333
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
334-
.size = static_cast<std::uint64_t>(vertexDataByteSize),
334+
.size = static_cast<std::uint64_t>(vertexBufferByteSize),
335335
.mappedAtCreation = false,
336336
};
337-
const WGPUBuffer vertexBuffer =
338-
wgpuDeviceCreateBuffer(gpuContext.device, &vertexBufferDesc);
337+
vertexBuffer = wgpuDeviceCreateBuffer(gpuContext.device, &vertexBufferDesc);
339338
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+
}
346341

347-
uniformsBuffer = [&gpuContext]() -> WGPUBuffer {
342+
{
348343
const WGPUBufferDescriptor uniformDesc{
349344
.nextInChain = nullptr,
350345
.label = "Uniform buffer",
351346
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
352347
.size = sizeof(glm::mat4),
353348
.mappedAtCreation = false,
354349
};
355-
const WGPUBuffer buffer = wgpuDeviceCreateBuffer(gpuContext.device, &uniformDesc);
350+
uniformsBuffer = wgpuDeviceCreateBuffer(gpuContext.device, &uniformDesc);
356351

357352
{
358353
// DirectX, Metal, wgpu share the same left-handed coordinate system
@@ -361,14 +356,12 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
361356
glm::mat4 viewProjectionMatrix = glm::orthoLH(-0.5f, 0.5f, -0.5f, 0.5f, -1.f, 1.f);
362357
wgpuQueueWriteBuffer(
363358
gpuContext.queue,
364-
buffer,
359+
uniformsBuffer,
365360
0,
366361
&viewProjectionMatrix[0],
367362
sizeof(viewProjectionMatrix));
368363
}
369-
370-
return buffer;
371-
}();
364+
}
372365

373366
{
374367
// Blend state for color target
@@ -501,7 +494,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
501494

502495
// uniforms bind group layout
503496

504-
const WGPUBindGroupLayoutEntry uniformsGroupLayoutEntry{
497+
const WGPUBindGroupLayoutEntry uniformsBindGroupLayoutEntry{
505498
.nextInChain = nullptr,
506499
.binding = 0, // binding index used in the @binding attribute
507500
.visibility = WGPUShaderStage_Vertex,
@@ -532,21 +525,21 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
532525
},
533526
};
534527

535-
const WGPUBindGroupLayoutDescriptor uniformsGroupLayoutDesc{
528+
const WGPUBindGroupLayoutDescriptor uniformsBindGroupLayoutDesc{
536529
.nextInChain = nullptr,
537530
.label = "uniforms group layout",
538531
.entryCount = 1,
539-
.entries = &uniformsGroupLayoutEntry,
532+
.entries = &uniformsBindGroupLayoutEntry,
540533
};
541-
const WGPUBindGroupLayout uniformsGroupLayout =
542-
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &uniformsGroupLayoutDesc);
534+
const WGPUBindGroupLayout uniformsBindGroupLayout =
535+
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &uniformsBindGroupLayoutDesc);
543536

544537
// images bind group layout
545538

546-
std::array<WGPUBindGroupLayoutEntry, 2> imagesGroupLayoutEntries{
539+
std::array<WGPUBindGroupLayoutEntry, 2> pixelsBindGroupLayoutEntries{
547540
WGPUBindGroupLayoutEntry{
548541
.nextInChain = nullptr,
549-
.binding = 0, // binding index used in the @binding attribute
542+
.binding = 0,
550543
.visibility = WGPUShaderStage_Fragment,
551544
.buffer =
552545
WGPUBufferBindingLayout{
@@ -608,18 +601,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
608601
},
609602
};
610603

611-
const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc{
604+
const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc{
612605
.nextInChain = nullptr,
613606
.label = "images group layout (render pipeline)",
614-
.entryCount = imagesGroupLayoutEntries.size(),
615-
.entries = imagesGroupLayoutEntries.data(),
607+
.entryCount = pixelsBindGroupLayoutEntries.size(),
608+
.entries = pixelsBindGroupLayoutEntries.data(),
616609
};
617-
const WGPUBindGroupLayout imagesGroupLayout =
618-
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &imagesGroupLayoutDesc);
610+
const WGPUBindGroupLayout pixelsBindGroupLayout =
611+
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &pixelsBindGroupLayoutDesc);
619612

620613
std::array<WGPUBindGroupLayout, 2> bindGroupLayouts{
621-
uniformsGroupLayout,
622-
imagesGroupLayout,
614+
uniformsBindGroupLayout,
615+
pixelsBindGroupLayout,
623616
};
624617

625618
const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
@@ -633,7 +626,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
633626

634627
// uniforms bind group
635628

636-
const WGPUBindGroupEntry uniformsGroupEntry{
629+
const WGPUBindGroupEntry uniformsBindGroupEntry{
637630
.nextInChain = nullptr,
638631
.binding = 0,
639632
.buffer = uniformsBuffer,
@@ -643,16 +636,16 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
643636
.textureView = nullptr,
644637
};
645638

646-
const WGPUBindGroupDescriptor uniformsGroupDesc{
639+
const WGPUBindGroupDescriptor uniformsBindGroupDesc{
647640
.nextInChain = nullptr,
648641
.label = "Bind group",
649-
.layout = uniformsGroupLayout,
642+
.layout = uniformsBindGroupLayout,
650643
.entryCount = 1,
651-
.entries = &uniformsGroupEntry,
644+
.entries = &uniformsBindGroupEntry,
652645
};
653-
uniformsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &uniformsGroupDesc);
646+
uniformsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &uniformsBindGroupDesc);
654647

655-
std::array<WGPUBindGroupEntry, 2> imagesGroupEntries{
648+
std::array<WGPUBindGroupEntry, 2> pixelsBindGroupEntries{
656649
WGPUBindGroupEntry{
657650
.nextInChain = nullptr,
658651
.binding = 0,
@@ -673,15 +666,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
673666
},
674667
};
675668

676-
const WGPUBindGroupDescriptor imagesGroupDesc{
669+
const WGPUBindGroupDescriptor pixelsBindGroupDesc{
677670
.nextInChain = nullptr,
678671
.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(),
682675
};
683-
// TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
684-
renderImagesBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &imagesGroupDesc);
676+
renderPixelsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &pixelsBindGroupDesc);
685677

686678
const WGPURenderPipelineDescriptor pipelineDesc{
687679
.nextInChain = nullptr,
@@ -728,18 +720,18 @@ Renderer::~Renderer()
728720
{
729721
renderPipelineSafeRelease(renderPipeline);
730722
renderPipeline = nullptr;
731-
computePipelineSafeRelease(computePipeline);
732-
computePipeline = nullptr;
733-
bindGroupSafeRelease(renderImagesBindGroup);
734-
renderImagesBindGroup = nullptr;
723+
bindGroupSafeRelease(renderPixelsBindGroup);
724+
renderPixelsBindGroup = nullptr;
735725
bindGroupSafeRelease(uniformsBindGroup);
736726
uniformsBindGroup = nullptr;
737727
bufferSafeRelease(uniformsBuffer);
738728
uniformsBuffer = nullptr;
739729
bufferSafeRelease(vertexBuffer);
740730
vertexBuffer = nullptr;
741-
bindGroupSafeRelease(computeImagesBindGroup);
742-
computeImagesBindGroup = nullptr;
731+
computePipelineSafeRelease(computePipeline);
732+
computePipeline = nullptr;
733+
bindGroupSafeRelease(computePixelsBindGroup);
734+
computePixelsBindGroup = nullptr;
743735
bufferSafeRelease(pixelBuffer);
744736
pixelBuffer = nullptr;
745737
bufferSafeRelease(frameDataBuffer);
@@ -791,7 +783,7 @@ void Renderer::render(const GpuContext& gpuContext)
791783

792784
wgpuComputePassEncoderSetPipeline(computePassEncoder, computePipeline);
793785
wgpuComputePassEncoderSetBindGroup(
794-
computePassEncoder, 0, computeImagesBindGroup, 0, nullptr);
786+
computePassEncoder, 0, computePixelsBindGroup, 0, nullptr);
795787

796788
const Extent2u workgroupSize{.x = 8, .y = 8};
797789
const Extent2u numWorkgroups{
@@ -806,8 +798,8 @@ void Renderer::render(const GpuContext& gpuContext)
806798
}
807799

808800
{
809-
// TODO: rename renderPass -> renderPassEncoder
810-
const WGPURenderPassEncoder renderPass = [encoder, nextTexture]() -> WGPURenderPassEncoder {
801+
const WGPURenderPassEncoder renderPassEncoder = [encoder,
802+
nextTexture]() -> WGPURenderPassEncoder {
811803
const WGPURenderPassColorAttachment renderPassColorAttachment{
812804
.nextInChain = nullptr,
813805
.view = nextTexture,
@@ -832,15 +824,16 @@ void Renderer::render(const GpuContext& gpuContext)
832824
}();
833825

834826
{
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);
838831
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);
841834
}
842835

843-
wgpuRenderPassEncoderEnd(renderPass);
836+
wgpuRenderPassEncoderEnd(renderPassEncoder);
844837
}
845838

846839
const WGPUCommandBuffer cmdBuffer = [encoder]() {
@@ -854,4 +847,14 @@ void Renderer::render(const GpuContext& gpuContext)
854847

855848
wgpuTextureViewRelease(nextTexture);
856849
}
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+
}
857860
} // namespace pt

0 commit comments

Comments
 (0)