Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ add_executable(vkdriven
src/platform/file_watcher.h
src/platform/window.cpp
src/platform/window.h
src/profiling/benchmark.cpp
src/profiling/benchmark.h
src/render/scene_draws.cpp
src/render/scene_draws.h
src/render/scene_pass.cpp
Expand Down Expand Up @@ -94,6 +96,8 @@ add_executable(vkdriven
src/vk/device.h
src/vk/frame_resources.cpp
src/vk/frame_resources.h
src/vk/gpu_timer.cpp
src/vk/gpu_timer.h
src/vk/graphics_pipeline.cpp
src/vk/graphics_pipeline.h
src/vk/handle.h
Expand Down
19 changes: 19 additions & 0 deletions benchmarks/2026-10-11_210546_2bce5e4.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
commit,build,viewport,view,metric,samples,min_ms,median_ms,mean_ms,p95_ms
2bce5e4,release,1920x1080,Atrium,CPU record,500,0.0343,0.0456,0.0469,0.0523
2bce5e4,release,1920x1080,Atrium,GPU Frame,500,0.9875,0.9952,1.0568,1.3229
2bce5e4,release,1920x1080,Atrium,GPU Shadows,500,0.4417,0.4453,0.4814,0.7568
2bce5e4,release,1920x1080,Atrium,GPU Scene,500,0.4823,0.4956,0.5157,0.8069
2bce5e4,release,1920x1080,Atrium,GPU Tonemap,500,0.0133,0.0154,0.0177,0.0164
2bce5e4,release,1920x1080,Atrium,GPU ImGui,500,0.0389,0.0389,0.0418,0.0420
2bce5e4,release,1920x1080,Wall,CPU record,500,0.0366,0.0449,0.0468,0.0515
2bce5e4,release,1920x1080,Wall,GPU Frame,500,0.8419,0.8573,0.9060,1.1898
2bce5e4,release,1920x1080,Wall,GPU Shadows,500,0.4107,0.4193,0.4447,0.7228
2bce5e4,release,1920x1080,Wall,GPU Scene,500,0.3738,0.3809,0.4029,0.7096
2bce5e4,release,1920x1080,Wall,GPU Tonemap,500,0.0154,0.0164,0.0172,0.0184
2bce5e4,release,1920x1080,Wall,GPU ImGui,500,0.0379,0.0389,0.0411,0.0399
2bce5e4,release,1920x1080,Gallery,CPU record,500,0.0365,0.0476,0.0488,0.0539
2bce5e4,release,1920x1080,Gallery,GPU Frame,500,0.9845,0.9932,1.0509,1.3185
2bce5e4,release,1920x1080,Gallery,GPU Shadows,500,0.4299,0.4328,0.4659,0.7440
2bce5e4,release,1920x1080,Gallery,GPU Scene,500,0.4997,0.5059,0.5253,0.8151
2bce5e4,release,1920x1080,Gallery,GPU Tonemap,500,0.0133,0.0143,0.0154,0.0164
2bce5e4,release,1920x1080,Gallery,GPU ImGui,500,0.0389,0.0399,0.0442,0.0399
1 change: 0 additions & 1 deletion docs/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ A **renderer**: it loads glTF models and scenes and draws them with modern Vulka
## 3. Development rules

- [ ] **Short milestones.** Each phase is 3–6 weeks. If a milestone is heading past 8 weeks, cut scope.
- [ ] **Each milestone ends with 3 things:** a git tag, a README screenshot or gif, and a short blog post.
- [ ] **Get it working, then generalize.** Hardcode the first version. Only refactor once something is needed a second time.
- [ ] **Keep the build green.** CI builds on every push. The validation layers should report zero errors, always.
- [ ] **When stuck, write a smaller test first.** Get the technique working in isolation before integrating it.
Expand Down
72 changes: 64 additions & 8 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
#include "vk/check.h"

#include <algorithm>
#include <chrono>
#include <cmath>
#include <limits>
#include <print>
#include <span>
#include <vector>

namespace
{
Expand All @@ -21,10 +24,10 @@ constexpr const char* environmentPath = "assets/environment/kloofendal_48d_partl

} // namespace

App::App()
: window_{"vkdriven", 1280, 720}, instance_{"vkdriven", window_.requiredInstanceExtensions()},
App::App(RunMode mode)
: mode_{mode}, window_{"vkdriven", 1920, 1080}, instance_{"vkdriven", window_.requiredInstanceExtensions()},
surface_{instance_, window_}, device_{instance_, surface_}, allocator_{instance_, device_},
swapchain_{device_, surface_, window_}, commandPool_{device_},
swapchain_{device_, surface_, window_, mode != RunMode::Benchmark}, commandPool_{device_},
frames_{device_, allocator_, commandPool_, sizeof(FrameData)},
scene_{orThrow(Scene::loadGltf(device_, allocator_, commandPool_, scenePath))},
environment_{orThrow(Texture::loadHdr(device_, allocator_, commandPool_, environmentPath))},
Expand Down Expand Up @@ -63,8 +66,21 @@ App::~App()

void App::run()
{
// Gives the viewport panel a few frames to settle on its size before the benchmark records it.
constexpr uint32_t benchmarkStartFrame = 10;
uint32_t frameCount = 0;

while (true)
{
if (mode_ == RunMode::Benchmark)
{
if (frameCount == benchmarkStartFrame)
benchmark_.start();
else if (frameCount > benchmarkStartFrame && !benchmark_.running())
return;
frameCount++;
}

if (!handleEvents())
return;

Expand All @@ -86,19 +102,29 @@ void App::run()
updateSwapchain_ = false;
}

if (viewport_.needsResize(requestedViewportExtent_))
// The benchmark always renders at the same size, so results don't depend on the window or panel layout.
const VkExtent2D viewportExtent = benchmark_.running() ? Benchmark::renderExtent : requestedViewportExtent_;
if (viewport_.needsResize(viewportExtent))
{
// Earlier frames may still be rendering into or sampling the old images.
device_.waitIdle();
viewport_.resize(requestedViewportExtent_);
viewport_.resize(viewportExtent);

const VkDescriptorImageInfo hdrInfo = viewport_.hdrDescriptorInfo();
bindlessTextures_.write(std::span{&hdrInfo, 1}, hdrTextureIndex());
}

imgui_.beginFrame();
drawUi();
camera_.update(window_, viewportHovered_, mouseDelta_);
if (benchmark_.running())
{
const BenchmarkView& view = benchmark_.currentView();
camera_.setPose(view.position, view.yawRadians, view.pitchRadians);
}
else
{
camera_.update(window_, viewportHovered_, mouseDelta_);
}
imgui_.endFrame();
drawFrame();
}
Expand All @@ -125,7 +151,7 @@ void App::drawUi()
if (viewportPanel.requestedExtent)
requestedViewportExtent_ = *viewportPanel.requestedExtent;
viewportHovered_ = viewportPanel.hovered;
drawStatsPanel(scene_, viewport_);
drawStatsPanel(scene_, viewport_, camera_, gpuTimings_, cpuRecordMilliseconds_, benchmark_);
drawLightPanel(light_);
drawShadowPanel(shadowSettings_);
drawTonemapPanel(tonemapSettings_);
Expand All @@ -138,6 +164,14 @@ void App::drawFrame()
// Wait on fence
chk(vkWaitForFences(device_.handle(), 1, frame.inFlight.ptr(), VK_TRUE, std::numeric_limits<uint64_t>::max()));

// The fence guarantees this frame's last timestamps are written; read them before recording resets the queries.
// A running average keeps the numbers readable in the stats panel.
const std::vector<GpuTiming> latestTimings = frame.gpuTimer.read();
if (latestTimings.size() != gpuTimings_.size())
gpuTimings_ = latestTimings;
for (size_t i = 0; i < latestTimings.size(); i++)
gpuTimings_[i].milliseconds = std::lerp(gpuTimings_[i].milliseconds, latestTimings[i].milliseconds, 0.05f);

// Acquire next image
const auto [acquireStatus, imageIndex] = swapchain_.acquireNextImage(frame.imageAcquired.get());
if (acquireStatus == SwapchainStatus::OutOfDate)
Expand All @@ -151,8 +185,13 @@ void App::drawFrame()
}
chk(vkResetFences(device_.handle(), 1, frame.inFlight.ptr()));

const auto recordStart = std::chrono::steady_clock::now();
updateFrameData(frame);
recordCommandBuffer(frame.commandBuffer, imageIndex, frame);
const float cpuRecordMilliseconds =
std::chrono::duration<float, std::milli>(std::chrono::steady_clock::now() - recordStart).count();
cpuRecordMilliseconds_ = std::lerp(cpuRecordMilliseconds_, cpuRecordMilliseconds, 0.05f);
benchmark_.addFrame(latestTimings, cpuRecordMilliseconds);

// Submit command buffer
VkSemaphoreSubmitInfo waitSemaphoreInfo{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO,
Expand Down Expand Up @@ -203,17 +242,34 @@ void App::updateFrameData(Frame& frame) const
frame.shaderData.write(std::span{&frameData, 1});
}

void App::recordCommandBuffer(VkCommandBuffer cb, uint32_t imageIndex, const Frame& frame) const
void App::recordCommandBuffer(VkCommandBuffer cb, uint32_t imageIndex, Frame& frame) const
{
chk(vkResetCommandBuffer(cb, 0));
VkCommandBufferBeginInfo cbBI{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT};
chk(vkBeginCommandBuffer(cb, &cbBI));

GpuTimer& timer = frame.gpuTimer;
timer.reset(cb);
const uint32_t frameScope = timer.begin(cb, "Frame");

const uint32_t shadowScope = timer.begin(cb, "Shadows");
shadowPass_.record(cb, scene_, frame.shaderData.deviceAddress(), shadowSettings_);
timer.end(cb, shadowScope);

const uint32_t sceneScope = timer.begin(cb, "Scene");
scenePass_.record(cb, viewport_, scene_, frame.shaderData.deviceAddress());
timer.end(cb, sceneScope);

const uint32_t tonemapScope = timer.begin(cb, "Tonemap");
tonemapPass_.record(cb, viewport_, hdrTextureIndex(), tonemapSettings_);
timer.end(cb, tonemapScope);

const uint32_t imguiScope = timer.begin(cb, "ImGui");
imgui_.record(cb, swapchain_, imageIndex);
timer.end(cb, imguiScope);

timer.end(cb, frameScope);

chk(vkEndCommandBuffer(cb));
}
18 changes: 16 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "platform/file_watcher.h"
#include "platform/window.h"
#include "profiling/benchmark.h"
#include "render/scene_pass.h"
#include "render/shadow_pass.h"
#include "render/tonemap_pass.h"
Expand All @@ -28,14 +29,23 @@

#include <cstdint>
#include <filesystem>
#include <vector>

enum class RunMode
{
Interactive,
// Runs the benchmark once the viewport has settled, then quits. Vsync is off, so the GPU never idles
// waiting on the display and its clocks stay up.
Benchmark,
};

// Owns the whole renderer. Members are declared in dependency order, so they're created top to
// bottom and destroyed bottom to top; the destructor only has to wait for the GPU first.
// Throws std::runtime_error from the constructor if an asset or shader fails to load.
class App
{
public:
App();
explicit App(RunMode mode);
~App();

App(const App&) = delete;
Expand All @@ -50,7 +60,7 @@ class App
void drawUi();
void drawFrame();
void updateFrameData(Frame& frame) const;
void recordCommandBuffer(VkCommandBuffer cb, uint32_t imageIndex, const Frame& frame) const;
void recordCommandBuffer(VkCommandBuffer cb, uint32_t imageIndex, Frame& frame) const;

[[nodiscard]] uint32_t hdrTextureIndex() const
{
Expand All @@ -67,6 +77,7 @@ class App
return shadowMapTextureIndex() + 1;
}

RunMode mode_;
Window window_;
Instance instance_;
Surface surface_;
Expand All @@ -92,6 +103,9 @@ class App
DirectionalLight light_;
ShadowSettings shadowSettings_;
TonemapSettings tonemapSettings_;
std::vector<GpuTiming> gpuTimings_;
float cpuRecordMilliseconds_{0.0f};
Benchmark benchmark_;
glm::vec2 mouseDelta_{};
bool viewportHovered_{false};

Expand Down
11 changes: 9 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

#include "app.h"

#include <algorithm>
#include <exception>
#include <print>
#include <span>
#include <string_view>

int main(int, char**)
int main(int argc, char** argv)
{
const std::span arguments{argv, static_cast<size_t>(argc)};
const bool benchmark = std::ranges::any_of(
arguments.subspan(1), [](const char* argument) { return std::string_view{argument} == "--benchmark"; });

try
{
App app;
App app{benchmark ? RunMode::Benchmark : RunMode::Interactive};
app.run();
}
catch (const std::exception& e)
Expand Down
Loading
Loading