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 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 17:21:41 +09:00
co-authored by Claude Opus 5.5
parent df87bc64eb
commit dcd301a080
4 changed files with 47 additions and 27 deletions
+7 -12
View File
@@ -2038,8 +2038,13 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
} }
// 2. Game phase: render outdoor terrain with quadtree culling & multi-texture patches // 2. Game phase: render outdoor terrain with quadtree culling & multi-texture patches
auto terrain_draws = outdoor_terrain.BuildCulledDraws(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), 8000.0f); const float terrain_light_dir[3] = {current_env.dir_light.direction[0], current_env.dir_light.direction[1], current_env.dir_light.direction[2]};
if (terrain_draws.empty()) { 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); auto td = outdoor_terrain.BuildDrawCommand(nullptr, 2);
td.light_direction[0] = current_env.dir_light.direction[0]; td.light_direction[0] = current_env.dir_light.direction[0];
td.light_direction[1] = current_env.dir_light.direction[1]; 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[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]); td.light_ambient[2] = std::max(0.38f, current_env.dir_light.bg_ambient[2]);
draws.push_back(std::move(td)); 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) // Add water surface (translucent, animated)
+16 -4
View File
@@ -600,8 +600,18 @@ void TerrainHeightmap::QueryQuadtree(const TerrainQuadNode* node, const float pl
} }
std::vector<Render3DDraw> TerrainHeightmap::BuildCulledDraws(const float view[16], const float proj[16], float max_distance) const { std::vector<Render3DDraw> TerrainHeightmap::BuildCulledDraws(const float view[16], const float proj[16], float max_distance) const {
std::vector<Render3DDrawRef> refs;
AppendCulledDrawRefs(view, proj, max_distance, 0, refs);
std::vector<Render3DDraw> draws; std::vector<Render3DDraw> 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<Render3DDrawRef>& out,
const float* light_direction, const float* light_ambient) const {
if (m_heights.empty() || m_patches.empty() || !m_quadtreeRoot) return;
float planes[6][4]; float planes[6][4];
ExtractFrustumPlanes(view, proj, planes); ExtractFrustumPlanes(view, proj, planes);
@@ -634,10 +644,12 @@ std::vector<Render3DDraw> TerrainHeightmap::BuildCulledDraws(const float view[16
for (auto& draw : patchDraws) draw = Render3DDraw::ShareGeometry(std::move(draw)); for (auto& draw : patchDraws) draw = Render3DDraw::ShareGeometry(std::move(draw));
it = m_patchDrawCache.emplace(key, std::move(patchDraws)).first; 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 { Render3DDraw TerrainHeightmap::BuildPatchDraw(int patch_x, int patch_y, int step) const {
+5
View File
@@ -122,6 +122,11 @@ public:
// Build multi-texture, patch-subdivided 3D draw commands with QuadTree frustum culling // Build multi-texture, patch-subdivided 3D draw commands with QuadTree frustum culling
std::vector<Render3DDraw> BuildCulledDraws(const float view[16], const float proj[16], float max_distance = 4000.0f) const; std::vector<Render3DDraw> 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<Render3DDrawRef>& out,
const float* light_direction = nullptr, const float* light_ambient = nullptr) const;
// Build patch draw command for a single patch // Build patch draw command for a single patch
Render3DDraw BuildPatchDraw(int patch_x, int patch_y, int step = 1) const; Render3DDraw BuildPatchDraw(int patch_x, int patch_y, int step = 1) const;
+19 -11
View File
@@ -640,6 +640,17 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float*
std::vector<Render3DDrawRef>* outRefs) { std::vector<Render3DDrawRef>* outRefs) {
if (!IsMapReady()) return; 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) { if (m_bVisiblePart[PART_SKY] && m_skybox && viewMatrix && projMatrix && camPos) {
auto sdraws = m_skybox->BuildDrawCommands(viewMatrix, projMatrix, camPos[0], camPos[1], camPos[2]); auto sdraws = m_skybox->BuildDrawCommands(viewMatrix, projMatrix, camPos[0], camPos[1], camPos[2]);
for (auto& sd : sdraws) outDraws.push_back(std::move(sd)); 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) { if (m_bVisiblePart[PART_TERRAIN] && m_terrain) {
std::vector<Render3DDraw> tdraws; std::vector<Render3DDraw> 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()); tdraws = m_terrain->BuildCulledDraws(viewMatrix, projMatrix, GetFarClip());
} }
if (tdraws.empty()) { if (!referenced && tdraws.empty()) {
auto td = m_terrain->BuildDrawCommand(nullptr, 2); auto td = m_terrain->BuildDrawCommand(nullptr, 2);
if (!td.positions.empty()) tdraws.push_back(std::move(td)); 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) { if (m_bVisiblePart[PART_OBJECT] && m_mapObjects && outRefs) {
float lightDir[3], lightAmbient[3]; m_mapObjects->AppendDrawRefs(viewMatrix, projMatrix, outDraws.size(), *outRefs, refLightDir, refLightAmbient);
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);
} else if (m_bVisiblePart[PART_OBJECT] && m_mapObjects) { } else if (m_bVisiblePart[PART_OBJECT] && m_mapObjects) {
auto bdraws = m_mapObjects->BuildDrawCommands(viewMatrix, projMatrix); auto bdraws = m_mapObjects->BuildDrawCommands(viewMatrix, projMatrix);
for (auto& bd : bdraws) { for (auto& bd : bdraws) {