From f1e8cfc70fd6c2d4a1b67d8cf2ecdefb65206fe9 Mon Sep 17 00:00:00 2001 From: quou Date: Mon, 10 Mar 2025 14:08:57 +1100 Subject: sort the forward drawlist front-to-back --- c2.cpp | 2 +- model.cpp | 1 + model.hpp | 1 + renderer.cpp | 19 +++++++++++++++++++ renderer.hpp | 1 + 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/c2.cpp b/c2.cpp index 94e2e38..1262241 100644 --- a/c2.cpp +++ b/c2.cpp @@ -21,7 +21,7 @@ extern "C" { #define video_arena_size (1024 * 1024 * 16) #define asset_arena_size (1024 * 1024 * 4) #define ui_arena_size (1024 * 1024) -#define scene_arena_size (1024 * 4) +#define scene_arena_size (1024 * 8) #define per_frame_memory_size (1024 * 1024) #define MSAA_SAMPLES 4 diff --git a/model.cpp b/model.cpp index cb9c23e..6aba19e 100644 --- a/model.cpp +++ b/model.cpp @@ -308,6 +308,7 @@ void Model_Instance::update() { ); bound.encompass(bounds[i]); } + centre = bound.min + bound.max * 0.5f; } void Model_Instance::update_cbuffers(Device* dev) { diff --git a/model.hpp b/model.hpp index 7417e51..75e310e 100644 --- a/model.hpp +++ b/model.hpp @@ -110,6 +110,7 @@ struct Model_Instance { Model* m; AABB* bounds; AABB bound; + v3f centre; void init(Device* dev, Heap* h, Model* model); void destroy(Device* dev, Heap* h); diff --git a/renderer.cpp b/renderer.cpp index c7040ec..64c3795 100644 --- a/renderer.cpp +++ b/renderer.cpp @@ -5,6 +5,8 @@ extern "C" { #include "memory.h" } +#include + struct VP_Cbuffer { m4f view_projection; }; @@ -91,6 +93,21 @@ void Drawlist::render( } } +void Drawlist::sort(const Renderer& r) { + const Camera& cam = r.get_camera(camera); + v3f far = cam.position + cam.far * cam.forward; + /* ideally want to sort the meshes within each model as + * well but as long as the models are small it wont matter + * too much... */ + std::sort( + models, + models + count, + [&far](const Model_Instance* a, const Model_Instance* b) { + return v3f::dot(a->centre, far) < v3f::dot(b->centre, far); + } + ); +} + void Renderer::update_globals( const Lighting* l, Device* d, @@ -136,6 +153,8 @@ void Renderer::render( .build_rp(); } + drawlists[FORWARD].sort(*this); + ctx.debug_push("depth prepass"); drawlists[FORWARD].render(*this, dev, a, l, depth_prepass, 0); ctx.debug_pop(); diff --git a/renderer.hpp b/renderer.hpp index 6bfe551..a76afa9 100644 --- a/renderer.hpp +++ b/renderer.hpp @@ -30,6 +30,7 @@ struct Drawlist { Render_Pass& pass, void (*overrider)(Pipeline_Builder&) ); + void sort(const Renderer& r); }; struct Renderer { -- cgit v1.2.3-54-g00ecf