From dcd301a080791a193a31615462de8fed333ddc76 Mon Sep 17 00:00:00 2001 From: shenlei Date: Mon, 12 Oct 2026 17:21:41 +0900 Subject: [PATCH] Submit cached terrain patch draws by pointer TerrainHeightmap::AppendCulledDrawRefs references the cached, shared- geometry patch commands for the visible quadtree patches (writing the background light into the cache) instead of copying them into a new vector each frame. BuildCulledDraws keeps its copying behaviour on top of it for existing callers. Co-Authored-By: Claude Opus 5.5 --- src/app/main.cpp | 19 +++++++----------- src/eter_render/TerrainHeightmap.cpp | 20 +++++++++++++++---- src/eter_render/TerrainHeightmap.h | 5 +++++ src/scripting/python_background.cpp | 30 ++++++++++++++++++---------- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 2caf0f14..2c1abe7e 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -2038,8 +2038,13 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); } // 2. Game phase: render outdoor terrain with quadtree culling & multi-texture patches - auto terrain_draws = outdoor_terrain.BuildCulledDraws(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), 8000.0f); - if (terrain_draws.empty()) { + const float terrain_light_dir[3] = {current_env.dir_light.direction[0], current_env.dir_light.direction[1], current_env.dir_light.direction[2]}; + const float terrain_light_ambient[3] = {std::max(0.35f, current_env.dir_light.bg_ambient[0]), + std::max(0.35f, current_env.dir_light.bg_ambient[1]), std::max(0.38f, current_env.dir_light.bg_ambient[2])}; + const size_t terrain_ref_count = draw_refs.size(); + outdoor_terrain.AppendCulledDrawRefs(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), 8000.0f, draws.size(), draw_refs, + terrain_light_dir, terrain_light_ambient); + if (draw_refs.size() == terrain_ref_count) { auto td = outdoor_terrain.BuildDrawCommand(nullptr, 2); td.light_direction[0] = current_env.dir_light.direction[0]; td.light_direction[1] = current_env.dir_light.direction[1]; @@ -2048,16 +2053,6 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); td.light_ambient[1] = std::max(0.35f, current_env.dir_light.bg_ambient[1]); td.light_ambient[2] = std::max(0.38f, current_env.dir_light.bg_ambient[2]); draws.push_back(std::move(td)); - } else { - for (auto& td : terrain_draws) { - td.light_direction[0] = current_env.dir_light.direction[0]; - td.light_direction[1] = current_env.dir_light.direction[1]; - td.light_direction[2] = current_env.dir_light.direction[2]; - td.light_ambient[0] = std::max(0.35f, current_env.dir_light.bg_ambient[0]); - td.light_ambient[1] = std::max(0.35f, current_env.dir_light.bg_ambient[1]); - td.light_ambient[2] = std::max(0.38f, current_env.dir_light.bg_ambient[2]); - draws.push_back(std::move(td)); - } } // Add water surface (translucent, animated) diff --git a/src/eter_render/TerrainHeightmap.cpp b/src/eter_render/TerrainHeightmap.cpp index 7f5cac94..546a3793 100644 --- a/src/eter_render/TerrainHeightmap.cpp +++ b/src/eter_render/TerrainHeightmap.cpp @@ -600,8 +600,18 @@ void TerrainHeightmap::QueryQuadtree(const TerrainQuadNode* node, const float pl } std::vector TerrainHeightmap::BuildCulledDraws(const float view[16], const float proj[16], float max_distance) const { + std::vector refs; + AppendCulledDrawRefs(view, proj, max_distance, 0, refs); std::vector draws; - if (m_heights.empty() || m_patches.empty() || !m_quadtreeRoot) return draws; + draws.reserve(refs.size()); + for (const auto& ref : refs) draws.push_back(*ref.draw); + return draws; +} + +void TerrainHeightmap::AppendCulledDrawRefs(const float view[16], const float proj[16], float max_distance, std::size_t before, + std::vector& out, + const float* light_direction, const float* light_ambient) const { + if (m_heights.empty() || m_patches.empty() || !m_quadtreeRoot) return; float planes[6][4]; ExtractFrustumPlanes(view, proj, planes); @@ -634,10 +644,12 @@ std::vector TerrainHeightmap::BuildCulledDraws(const float view[16 for (auto& draw : patchDraws) draw = Render3DDraw::ShareGeometry(std::move(draw)); it = m_patchDrawCache.emplace(key, std::move(patchDraws)).first; } - draws.insert(draws.end(), it->second.begin(), it->second.end()); + for (auto& draw : it->second) { + if (light_direction) std::copy_n(light_direction, 3, draw.light_direction); + if (light_ambient) std::copy_n(light_ambient, 3, draw.light_ambient); + out.push_back({before, &draw}); + } } - - return draws; } Render3DDraw TerrainHeightmap::BuildPatchDraw(int patch_x, int patch_y, int step) const { diff --git a/src/eter_render/TerrainHeightmap.h b/src/eter_render/TerrainHeightmap.h index 7ecf3c1e..c9f3e498 100644 --- a/src/eter_render/TerrainHeightmap.h +++ b/src/eter_render/TerrainHeightmap.h @@ -122,6 +122,11 @@ public: // Build multi-texture, patch-subdivided 3D draw commands with QuadTree frustum culling std::vector BuildCulledDraws(const float view[16], const float proj[16], float max_distance = 4000.0f) const; + // Same visible patches without copying: cached patch commands are referenced, spliced before owned[before]. + // Valid until the heightmap is reloaded/edited. light_* (optional) are written into the cache. + void AppendCulledDrawRefs(const float view[16], const float proj[16], float max_distance, std::size_t before, + std::vector& out, + const float* light_direction = nullptr, const float* light_ambient = nullptr) const; // Build patch draw command for a single patch Render3DDraw BuildPatchDraw(int patch_x, int patch_y, int step = 1) const; diff --git a/src/scripting/python_background.cpp b/src/scripting/python_background.cpp index 79eee974..95a97398 100644 --- a/src/scripting/python_background.cpp +++ b/src/scripting/python_background.cpp @@ -640,6 +640,17 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* std::vector* outRefs) { if (!IsMapReady()) return; + // Background light written into the referenced (cached) terrain and map-object commands. + float lightDir[3], lightAmbient[3]; + if (m_pCurrentEnvironment) { + std::copy_n(m_pCurrentEnvironment->dir_light.direction, 3, lightDir); + lightAmbient[0] = std::max(0.35f, m_pCurrentEnvironment->dir_light.bg_ambient[0]); + lightAmbient[1] = std::max(0.35f, m_pCurrentEnvironment->dir_light.bg_ambient[1]); + lightAmbient[2] = std::max(0.38f, m_pCurrentEnvironment->dir_light.bg_ambient[2]); + } + const float* refLightDir = m_pCurrentEnvironment ? lightDir : nullptr; + const float* refLightAmbient = m_pCurrentEnvironment ? lightAmbient : nullptr; + if (m_bVisiblePart[PART_SKY] && m_skybox && viewMatrix && projMatrix && camPos) { auto sdraws = m_skybox->BuildDrawCommands(viewMatrix, projMatrix, camPos[0], camPos[1], camPos[2]); for (auto& sd : sdraws) outDraws.push_back(std::move(sd)); @@ -647,10 +658,15 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* if (m_bVisiblePart[PART_TERRAIN] && m_terrain) { std::vector tdraws; - if (viewMatrix && projMatrix) { + bool referenced = false; + if (viewMatrix && projMatrix && outRefs) { + const size_t refCount = outRefs->size(); + m_terrain->AppendCulledDrawRefs(viewMatrix, projMatrix, GetFarClip(), outDraws.size(), *outRefs, refLightDir, refLightAmbient); + referenced = outRefs->size() != refCount; + } else if (viewMatrix && projMatrix) { tdraws = m_terrain->BuildCulledDraws(viewMatrix, projMatrix, GetFarClip()); } - if (tdraws.empty()) { + if (!referenced && tdraws.empty()) { auto td = m_terrain->BuildDrawCommand(nullptr, 2); if (!td.positions.empty()) tdraws.push_back(std::move(td)); } @@ -681,15 +697,7 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* } if (m_bVisiblePart[PART_OBJECT] && m_mapObjects && outRefs) { - float lightDir[3], lightAmbient[3]; - if (m_pCurrentEnvironment) { - std::copy_n(m_pCurrentEnvironment->dir_light.direction, 3, lightDir); - lightAmbient[0] = std::max(0.35f, m_pCurrentEnvironment->dir_light.bg_ambient[0]); - lightAmbient[1] = std::max(0.35f, m_pCurrentEnvironment->dir_light.bg_ambient[1]); - lightAmbient[2] = std::max(0.38f, m_pCurrentEnvironment->dir_light.bg_ambient[2]); - } - m_mapObjects->AppendDrawRefs(viewMatrix, projMatrix, outDraws.size(), *outRefs, - m_pCurrentEnvironment ? lightDir : nullptr, m_pCurrentEnvironment ? lightAmbient : nullptr); + m_mapObjects->AppendDrawRefs(viewMatrix, projMatrix, outDraws.size(), *outRefs, refLightDir, refLightAmbient); } else if (m_bVisiblePart[PART_OBJECT] && m_mapObjects) { auto bdraws = m_mapObjects->BuildDrawCommands(viewMatrix, projMatrix); for (auto& bd : bdraws) {