Skip to content

Commit

Permalink
wip commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Apr 2, 2024
1 parent be50be6 commit 7faa086
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 25 deletions.
72 changes: 70 additions & 2 deletions src/pt/deferred_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <algorithm>
#include <array>
#include <numeric>

namespace nlrs
{
Expand Down Expand Up @@ -94,7 +95,9 @@ DeferredRenderer::DeferredRenderer(
sizeof(TimestampsLayout)),
mGbufferPass(gpuContext, rendererDesc),
mDebugPass(),
mLightingPass()
mLightingPass(),
mGbufferPassDurationsNs(),
mLightingPassDurationsNs()
{
{
const std::array<WGPUTextureFormat, 1> depthFormats{
Expand Down Expand Up @@ -208,7 +211,7 @@ void DeferredRenderer::render(const GpuContext& gpuContext, const RenderDescript
do
{
wgpuDeviceTick(gpuContext.device);
} while (wgpuBufferGetMapState(mTimestampBuffer.ptr()) != WGPUBufferMapState_Unmapped);
} while (wgpuBufferGetMapState(mTimestampsBuffer.ptr()) != WGPUBufferMapState_Unmapped);

const WGPUCommandEncoder encoder = [&gpuContext]() {
const WGPUCommandEncoderDescriptor cmdEncoderDesc{
Expand Down Expand Up @@ -265,6 +268,51 @@ void DeferredRenderer::render(const GpuContext& gpuContext, const RenderDescript
return wgpuCommandEncoderFinish(encoder, &cmdBufferDesc);
}();
wgpuQueueSubmit(gpuContext.queue, 1, &cmdBuffer);

wgpuBufferMapAsync(
mTimestampsBuffer.ptr(),
WGPUMapMode_Read,
0,
sizeof(TimestampsLayout),
[](const WGPUBufferMapAsyncStatus status, void* const userdata) -> void {
if (status == WGPUBufferMapAsyncStatus_Success)
{
NLRS_ASSERT(userdata != nullptr);
DeferredRenderer& renderer = *static_cast<DeferredRenderer*>(userdata);
GpuBuffer& timestampBuffer = renderer.mTimestampsBuffer;
const void* const bufferData = wgpuBufferGetConstMappedRange(
timestampBuffer.ptr(), 0, sizeof(TimestampsLayout));
NLRS_ASSERT(bufferData != nullptr);

const TimestampsLayout& timestamps =
*static_cast<const TimestampsLayout*>(bufferData);

auto& gbufferDurations = renderer.mGbufferPassDurationsNs;
const auto gbufferDuration =
timestamps.gbufferPassEnd - timestamps.gbufferPassStart;
gbufferDurations.push_back(gbufferDuration);
if (gbufferDurations.size() > 30)
{
gbufferDurations.pop_front();
}

auto& lightingDurations = renderer.mLightingPassDurationsNs;
const auto lightingDuration =
timestamps.lightingPassEnd - timestamps.lightingPassStart;
lightingDurations.push_back(lightingDuration);
if (lightingDurations.size() > 30)
{
lightingDurations.pop_front();
}

wgpuBufferUnmap(timestampBuffer.ptr());
}
else
{
std::fprintf(stderr, "Failed to map timestamps buffer\n");
}
},
this);
}

void DeferredRenderer::renderDebug(
Expand Down Expand Up @@ -1405,4 +1453,24 @@ void DeferredRenderer::LightingPass::render(
wgpuRenderPassEncoderDraw(renderPass, 6, 1, 0, 0);
wgpuRenderPassEncoderEnd(renderPass);
}

DeferredRenderer::PerfStats DeferredRenderer::getPerfStats() const
{
NLRS_ASSERT(mGbufferPassDurationsNs.size() == mLightingPassDurationsNs.size());

if (mGbufferPassDurationsNs.empty())
{
return {};
}

return {
0.000001f *
static_cast<float>(std::accumulate(
mGbufferPassDurationsNs.begin(), mGbufferPassDurationsNs.end(), 0ll)) /
mGbufferPassDurationsNs.size(),
0.000001f *
static_cast<float>(std::accumulate(
mLightingPassDurationsNs.begin(), mLightingPassDurationsNs.end(), 0ll)) /
mLightingPassDurationsNs.size()};
}
} // namespace nlrs
39 changes: 25 additions & 14 deletions src/pt/deferred_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <cstddef>
#include <cstdint>
#include <deque>
#include <span>
#include <vector>

Expand Down Expand Up @@ -45,6 +46,12 @@ struct RenderDescriptor
class DeferredRenderer
{
public:
struct PerfStats
{
float averageGbufferPassDurationsMs = 0.0f;
float averageLightingPassDurationsMs = 0.0f;
};

DeferredRenderer(const GpuContext&, const DeferredRendererDescriptor&);
~DeferredRenderer();

Expand All @@ -58,6 +65,8 @@ class DeferredRenderer
void renderDebug(const GpuContext&, const glm::mat4&, const Extent2f&, WGPUTextureView);
void resize(const GpuContext&, const Extent2u&);

PerfStats getPerfStats() const;

private:
struct IndexBuffer
{
Expand Down Expand Up @@ -182,19 +191,21 @@ class DeferredRenderer
float exposure);
};

WGPUTexture mDepthTexture;
WGPUTextureView mDepthTextureView;
WGPUTexture mAlbedoTexture;
WGPUTextureView mAlbedoTextureView;
WGPUTexture mNormalTexture;
WGPUTextureView mNormalTextureView;
GpuBindGroupLayout mGbufferBindGroupLayout;
GpuBindGroup mGbufferBindGroup;
WGPUQuerySet mQuerySet;
GpuBuffer mQueryBuffer;
GpuBuffer mTimestampsBuffer;
GbufferPass mGbufferPass;
DebugPass mDebugPass;
LightingPass mLightingPass;
WGPUTexture mDepthTexture;
WGPUTextureView mDepthTextureView;
WGPUTexture mAlbedoTexture;
WGPUTextureView mAlbedoTextureView;
WGPUTexture mNormalTexture;
WGPUTextureView mNormalTextureView;
GpuBindGroupLayout mGbufferBindGroupLayout;
GpuBindGroup mGbufferBindGroup;
WGPUQuerySet mQuerySet;
GpuBuffer mQueryBuffer;
GpuBuffer mTimestampsBuffer;
GbufferPass mGbufferPass;
DebugPass mDebugPass;
LightingPass mLightingPass;
std::deque<std::uint64_t> mGbufferPassDurationsNs;
std::deque<std::uint64_t> mLightingPassDurationsNs;
};
} // namespace nlrs
38 changes: 29 additions & 9 deletions src/pt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ try

auto onNewFrame = [&gui]() -> void { gui.beginFrame(); };

auto onUpdate = [&appState, &renderer](GLFWwindow* windowPtr, float deltaTime) -> void {
auto onUpdate =
[&appState, &renderer, &deferredRenderer](GLFWwindow* windowPtr, float deltaTime) -> void {
{
// Skip input if ImGui captured input
if (!ImGui::GetIO().WantCaptureMouse)
Expand Down Expand Up @@ -320,14 +321,33 @@ try
ImGui::RadioButton("debug", &appState.ui.rendererType, RendererType_Debug);
ImGui::Separator();

ImGui::Text("Renderer stats");
ImGui::Text("Perf stats");
{
const float renderAverageMs = renderer.averageRenderpassDurationMs();
const float progressPercentage = renderer.renderProgressPercentage();
ImGui::Text(
"render pass: %.2f ms (%.1f FPS)", renderAverageMs, 1000.0f / renderAverageMs);
ImGui::Text("render progress: %.2f %%", progressPercentage);
switch (appState.ui.rendererType)
{
case RendererType_PathTracer:
{
const float renderAverageMs = renderer.averageRenderpassDurationMs();
const float progressPercentage = renderer.renderProgressPercentage();
ImGui::Text(
"render pass: %.2f ms (%.1f FPS)",
renderAverageMs,
1000.0f / renderAverageMs);
ImGui::Text("render progress: %.2f %%", progressPercentage);
break;
}
case RendererType_Deferred:
{
const auto perfStats = deferredRenderer.getPerfStats();
ImGui::Text("gbuffer pass: %.2f ms", perfStats.averageGbufferPassDurationsMs);
ImGui::Text("lighting pass: %.2f ms", perfStats.averageLightingPassDurationsMs);
break;
}
default:
ImGui::Text("no perf stats available");
}
}

ImGui::Separator();

ImGui::Text("Parameters");
Expand Down Expand Up @@ -455,8 +475,8 @@ try

auto onResize = [&gpuContext, &deferredRenderer, &textureBlitter](
const nlrs::FramebufferSize newSize) -> void {
// TODO: this function is not really needed since I get the current framebuffer size on each
// render anyway.
// TODO: this function is not really needed since I get the current framebuffer size on
// each render anyway.
const auto sz = nlrs::Extent2u(newSize);
deferredRenderer.resize(gpuContext, sz);
textureBlitter.resize(gpuContext, sz);
Expand Down

0 comments on commit 7faa086

Please sign in to comment.