Skip to content

Commit

Permalink
Fix inconsistent naming, TODOs, unneeded immediate lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Oct 31, 2023
1 parent 0c56da7 commit 645c09c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 86 deletions.
1 change: 1 addition & 0 deletions src/main.cpp
Expand Up @@ -46,6 +46,7 @@ int main()
{
curFramebufferSize = newFramebufferSize;
gpuContext.resizeFramebuffer(curFramebufferSize);
renderer.resizeFramebuffer(curFramebufferSize);
}
}

Expand Down
159 changes: 81 additions & 78 deletions src/renderer.cpp
Expand Up @@ -64,13 +64,13 @@ void renderPipelineSafeRelease(const WGPURenderPipeline pipeline)
Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpuContext)
: frameDataBuffer(nullptr),
pixelBuffer(nullptr),
computeImagesBindGroup(nullptr),
computePixelsBindGroup(nullptr),
computePipeline(nullptr),
vertexBuffer(nullptr),
vertexBufferByteSize(0),
uniformsBuffer(nullptr),
uniformsBindGroup(nullptr),
renderImagesBindGroup(nullptr),
computePipeline(nullptr),
renderPixelsBindGroup(nullptr),
renderPipeline(nullptr),
currentFramebufferSize(rendererDesc.currentFramebufferSize),
frameCount(0)
Expand Down Expand Up @@ -102,6 +102,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu

{
// Shader module

const char* const computeSource = R"(
struct FrameData {
dimensions: vec2<u32>,
Expand Down Expand Up @@ -177,14 +178,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
const WGPUShaderModule computeModule =
wgpuDeviceCreateShaderModule(gpuContext.device, &shaderDesc);

// Image bind group layout
// pixels bind group layout

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

std::array<WGPUBindGroupLayoutEntry, 2> imagesGroupLayoutEntries{
std::array<WGPUBindGroupLayoutEntry, 2> pixelsBindGroupLayoutEntries{
WGPUBindGroupLayoutEntry{
.nextInChain = nullptr,
.binding = 0, // binding index used in the @binding attribute
.binding = 0,
.visibility = WGPUShaderStage_Compute,
.buffer =
WGPUBufferBindingLayout{
Expand Down Expand Up @@ -246,18 +247,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
},
};

const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc{
const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc{
.nextInChain = nullptr,
.label = "images group layout (compute pipeline)",
.entryCount = imagesGroupLayoutEntries.size(),
.entries = imagesGroupLayoutEntries.data(),
.entryCount = pixelsBindGroupLayoutEntries.size(),
.entries = pixelsBindGroupLayoutEntries.data(),
};
const WGPUBindGroupLayout imagesGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &imagesGroupLayoutDesc);
const WGPUBindGroupLayout pixelsBindGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &pixelsBindGroupLayoutDesc);

// Bind group

std::array<WGPUBindGroupEntry, 2> imagesGroupEntries{
std::array<WGPUBindGroupEntry, 2> pixelsBindGroupEntries{
WGPUBindGroupEntry{
.nextInChain = nullptr,
.binding = 0,
Expand All @@ -278,23 +279,22 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
},
};

const WGPUBindGroupDescriptor imagesGroupDesc{
const WGPUBindGroupDescriptor pixelsBindGroupDesc{
.nextInChain = nullptr,
.label = "image bind group",
.layout = imagesGroupLayout,
.entryCount = imagesGroupEntries.size(),
.entries = imagesGroupEntries.data(),
.layout = pixelsBindGroupLayout,
.entryCount = pixelsBindGroupEntries.size(),
.entries = pixelsBindGroupEntries.data(),
};
// TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
computeImagesBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &imagesGroupDesc);
computePixelsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &pixelsBindGroupDesc);

// Pipeline layout

const WGPUPipelineLayoutDescriptor pipelineLayoutDesc{
.nextInChain = nullptr,
.label = "Compute pipeline layout",
.bindGroupLayoutCount = 1,
.bindGroupLayouts = &imagesGroupLayout,
.bindGroupLayouts = &pixelsBindGroupLayout,
};
const WGPUPipelineLayout pipelineLayout =
wgpuDeviceCreatePipelineLayout(gpuContext.device, &pipelineLayoutDesc);
Expand All @@ -315,7 +315,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
computePipeline = wgpuDeviceCreateComputePipeline(gpuContext.device, &computeDesc);
}

auto [buffer, byteSize] = [&gpuContext]() -> std::tuple<WGPUBuffer, std::size_t> {
{
const std::array<Vertex, 6> vertexData{
Vertex{{-0.5f, -0.5f}, {0.0f, 0.0f}},
Vertex{{0.5f, -0.5f}, {1.0f, 0.0f}},
Expand All @@ -324,35 +324,30 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
Vertex{{-0.5f, 0.5f}, {0.0f, 1.0f}},
Vertex{{-0.5f, -0.5f}, {0.0f, 0.0f}},
};
const std::size_t vertexDataByteSize = sizeof(Vertex) * vertexData.size();
assert(vertexDataByteSize <= std::numeric_limits<std::uint64_t>::max());
vertexBufferByteSize = sizeof(Vertex) * vertexData.size();
assert(vertexBufferByteSize <= std::numeric_limits<std::uint64_t>::max());

const WGPUBufferDescriptor vertexBufferDesc{
.nextInChain = nullptr,
.label = "Vertex buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Vertex,
.size = static_cast<std::uint64_t>(vertexDataByteSize),
.size = static_cast<std::uint64_t>(vertexBufferByteSize),
.mappedAtCreation = false,
};
const WGPUBuffer vertexBuffer =
wgpuDeviceCreateBuffer(gpuContext.device, &vertexBufferDesc);
vertexBuffer = wgpuDeviceCreateBuffer(gpuContext.device, &vertexBufferDesc);
wgpuQueueWriteBuffer(
gpuContext.queue, vertexBuffer, 0, vertexData.data(), vertexDataByteSize);

return std::make_tuple(vertexBuffer, vertexDataByteSize);
}();
vertexBuffer = buffer;
vertexBufferByteSize = byteSize;
gpuContext.queue, vertexBuffer, 0, vertexData.data(), vertexBufferByteSize);
}

uniformsBuffer = [&gpuContext]() -> WGPUBuffer {
{
const WGPUBufferDescriptor uniformDesc{
.nextInChain = nullptr,
.label = "Uniform buffer",
.usage = WGPUBufferUsage_CopyDst | WGPUBufferUsage_Uniform,
.size = sizeof(glm::mat4),
.mappedAtCreation = false,
};
const WGPUBuffer buffer = wgpuDeviceCreateBuffer(gpuContext.device, &uniformDesc);
uniformsBuffer = wgpuDeviceCreateBuffer(gpuContext.device, &uniformDesc);

{
// DirectX, Metal, wgpu share the same left-handed coordinate system
Expand All @@ -361,14 +356,12 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
glm::mat4 viewProjectionMatrix = glm::orthoLH(-0.5f, 0.5f, -0.5f, 0.5f, -1.f, 1.f);
wgpuQueueWriteBuffer(
gpuContext.queue,
buffer,
uniformsBuffer,
0,
&viewProjectionMatrix[0],
sizeof(viewProjectionMatrix));
}

return buffer;
}();
}

{
// Blend state for color target
Expand Down Expand Up @@ -501,7 +494,7 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu

// uniforms bind group layout

const WGPUBindGroupLayoutEntry uniformsGroupLayoutEntry{
const WGPUBindGroupLayoutEntry uniformsBindGroupLayoutEntry{
.nextInChain = nullptr,
.binding = 0, // binding index used in the @binding attribute
.visibility = WGPUShaderStage_Vertex,
Expand Down Expand Up @@ -532,21 +525,21 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
},
};

const WGPUBindGroupLayoutDescriptor uniformsGroupLayoutDesc{
const WGPUBindGroupLayoutDescriptor uniformsBindGroupLayoutDesc{
.nextInChain = nullptr,
.label = "uniforms group layout",
.entryCount = 1,
.entries = &uniformsGroupLayoutEntry,
.entries = &uniformsBindGroupLayoutEntry,
};
const WGPUBindGroupLayout uniformsGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &uniformsGroupLayoutDesc);
const WGPUBindGroupLayout uniformsBindGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &uniformsBindGroupLayoutDesc);

// images bind group layout

std::array<WGPUBindGroupLayoutEntry, 2> imagesGroupLayoutEntries{
std::array<WGPUBindGroupLayoutEntry, 2> pixelsBindGroupLayoutEntries{
WGPUBindGroupLayoutEntry{
.nextInChain = nullptr,
.binding = 0, // binding index used in the @binding attribute
.binding = 0,
.visibility = WGPUShaderStage_Fragment,
.buffer =
WGPUBufferBindingLayout{
Expand Down Expand Up @@ -608,18 +601,18 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
},
};

const WGPUBindGroupLayoutDescriptor imagesGroupLayoutDesc{
const WGPUBindGroupLayoutDescriptor pixelsBindGroupLayoutDesc{
.nextInChain = nullptr,
.label = "images group layout (render pipeline)",
.entryCount = imagesGroupLayoutEntries.size(),
.entries = imagesGroupLayoutEntries.data(),
.entryCount = pixelsBindGroupLayoutEntries.size(),
.entries = pixelsBindGroupLayoutEntries.data(),
};
const WGPUBindGroupLayout imagesGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &imagesGroupLayoutDesc);
const WGPUBindGroupLayout pixelsBindGroupLayout =
wgpuDeviceCreateBindGroupLayout(gpuContext.device, &pixelsBindGroupLayoutDesc);

std::array<WGPUBindGroupLayout, 2> bindGroupLayouts{
uniformsGroupLayout,
imagesGroupLayout,
uniformsBindGroupLayout,
pixelsBindGroupLayout,
};

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

// uniforms bind group

const WGPUBindGroupEntry uniformsGroupEntry{
const WGPUBindGroupEntry uniformsBindGroupEntry{
.nextInChain = nullptr,
.binding = 0,
.buffer = uniformsBuffer,
Expand All @@ -643,16 +636,16 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
.textureView = nullptr,
};

const WGPUBindGroupDescriptor uniformsGroupDesc{
const WGPUBindGroupDescriptor uniformsBindGroupDesc{
.nextInChain = nullptr,
.label = "Bind group",
.layout = uniformsGroupLayout,
.layout = uniformsBindGroupLayout,
.entryCount = 1,
.entries = &uniformsGroupEntry,
.entries = &uniformsBindGroupEntry,
};
uniformsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &uniformsGroupDesc);
uniformsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &uniformsBindGroupDesc);

std::array<WGPUBindGroupEntry, 2> imagesGroupEntries{
std::array<WGPUBindGroupEntry, 2> pixelsBindGroupEntries{
WGPUBindGroupEntry{
.nextInChain = nullptr,
.binding = 0,
Expand All @@ -673,15 +666,14 @@ Renderer::Renderer(const RendererDescriptor& rendererDesc, const GpuContext& gpu
},
};

const WGPUBindGroupDescriptor imagesGroupDesc{
const WGPUBindGroupDescriptor pixelsBindGroupDesc{
.nextInChain = nullptr,
.label = "image bind group",
.layout = imagesGroupLayout,
.entryCount = imagesGroupEntries.size(),
.entries = imagesGroupEntries.data(),
.layout = pixelsBindGroupLayout,
.entryCount = pixelsBindGroupEntries.size(),
.entries = pixelsBindGroupEntries.data(),
};
// TODO: naming is inconsistent between `imagesBindGroup` and `imagesGroupDesc`
renderImagesBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &imagesGroupDesc);
renderPixelsBindGroup = wgpuDeviceCreateBindGroup(gpuContext.device, &pixelsBindGroupDesc);

const WGPURenderPipelineDescriptor pipelineDesc{
.nextInChain = nullptr,
Expand Down Expand Up @@ -728,18 +720,18 @@ Renderer::~Renderer()
{
renderPipelineSafeRelease(renderPipeline);
renderPipeline = nullptr;
computePipelineSafeRelease(computePipeline);
computePipeline = nullptr;
bindGroupSafeRelease(renderImagesBindGroup);
renderImagesBindGroup = nullptr;
bindGroupSafeRelease(renderPixelsBindGroup);
renderPixelsBindGroup = nullptr;
bindGroupSafeRelease(uniformsBindGroup);
uniformsBindGroup = nullptr;
bufferSafeRelease(uniformsBuffer);
uniformsBuffer = nullptr;
bufferSafeRelease(vertexBuffer);
vertexBuffer = nullptr;
bindGroupSafeRelease(computeImagesBindGroup);
computeImagesBindGroup = nullptr;
computePipelineSafeRelease(computePipeline);
computePipeline = nullptr;
bindGroupSafeRelease(computePixelsBindGroup);
computePixelsBindGroup = nullptr;
bufferSafeRelease(pixelBuffer);
pixelBuffer = nullptr;
bufferSafeRelease(frameDataBuffer);
Expand Down Expand Up @@ -791,7 +783,7 @@ void Renderer::render(const GpuContext& gpuContext)

wgpuComputePassEncoderSetPipeline(computePassEncoder, computePipeline);
wgpuComputePassEncoderSetBindGroup(
computePassEncoder, 0, computeImagesBindGroup, 0, nullptr);
computePassEncoder, 0, computePixelsBindGroup, 0, nullptr);

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

{
// TODO: rename renderPass -> renderPassEncoder
const WGPURenderPassEncoder renderPass = [encoder, nextTexture]() -> WGPURenderPassEncoder {
const WGPURenderPassEncoder renderPassEncoder = [encoder,
nextTexture]() -> WGPURenderPassEncoder {
const WGPURenderPassColorAttachment renderPassColorAttachment{
.nextInChain = nullptr,
.view = nextTexture,
Expand All @@ -832,15 +824,16 @@ void Renderer::render(const GpuContext& gpuContext)
}();

{
wgpuRenderPassEncoderSetPipeline(renderPass, renderPipeline);
wgpuRenderPassEncoderSetBindGroup(renderPass, 0, uniformsBindGroup, 0, nullptr);
wgpuRenderPassEncoderSetBindGroup(renderPass, 1, renderImagesBindGroup, 0, nullptr);
wgpuRenderPassEncoderSetPipeline(renderPassEncoder, renderPipeline);
wgpuRenderPassEncoderSetBindGroup(renderPassEncoder, 0, uniformsBindGroup, 0, nullptr);
wgpuRenderPassEncoderSetBindGroup(
renderPassEncoder, 1, renderPixelsBindGroup, 0, nullptr);
wgpuRenderPassEncoderSetVertexBuffer(
renderPass, 0, vertexBuffer, 0, vertexBufferByteSize);
wgpuRenderPassEncoderDraw(renderPass, 6, 1, 0, 0);
renderPassEncoder, 0, vertexBuffer, 0, vertexBufferByteSize);
wgpuRenderPassEncoderDraw(renderPassEncoder, 6, 1, 0, 0);
}

wgpuRenderPassEncoderEnd(renderPass);
wgpuRenderPassEncoderEnd(renderPassEncoder);
}

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

wgpuTextureViewRelease(nextTexture);
}

void Renderer::resizeFramebuffer(const Extent2i& newSize)
{
if (newSize.x <= 0 || newSize.y <= 0)
{
return;
}

currentFramebufferSize = newSize;
}
} // namespace pt

0 comments on commit 645c09c

Please sign in to comment.