Submit cached map-object draws by pointer instead of copying

MapObjectManager::AppendDrawRefs references each visible object's cached
Render3DDraw (writing the per-frame light into the cache) rather than
copying the whole command (two texture strings, shared_ptrs, matrices)
every frame. main.cpp flattens owned draws and refs in submission order
(FlattenDraws) and passes a pointer list to RenderSceneGPU/RenderScene.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 17:09:45 +09:00
co-authored by Claude Opus 5.5
parent b5a15871f4
commit 46880be537
9 changed files with 89 additions and 12 deletions
+10 -6
View File
@@ -1965,6 +1965,10 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
// Draw count is stable frame to frame; reserve last frame's peak up front.
static size_t draw_reserve = 0;
draws.reserve(draw_reserve);
// Cached map-object commands go by pointer; flattened in order right before rendering.
static std::vector<ava2::Render3DDrawRef> draw_refs;
static std::vector<const ava2::Render3DDraw*> draw_list;
draw_refs.clear();
const auto profileBuildStart = std::chrono::steady_clock::now();
if (last_phase == "game") {
// Update outdoor SkyBox, Clouds UV scrolling & dynamic environment
@@ -2021,7 +2025,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
if (ava2::CPythonBackground::Instance().IsMapReady()) {
ava2::CPythonBackground::Instance().Update(eyeX, eyeY, eyeZ);
float camPos[3] = {eyeX, eyeY, eyeZ};
ava2::CPythonBackground::Instance().BuildDrawCommands(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), camPos, draws);
ava2::CPythonBackground::Instance().BuildDrawCommands(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), camPos, draws, &draw_refs);
bg_rendered = true;
}
if (!bg_rendered)
@@ -2063,8 +2067,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
draws.push_back(std::move(water_draw));
}
auto bldg_draws = map_objects.BuildDrawCommands(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix());
for (auto& bd : bldg_draws) draws.push_back(std::move(bd));
map_objects.AppendDrawRefs(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), draws.size(), draw_refs);
// Add outdoor forest trees, pines, and vegetation (SpeedTree Engine)
auto tree_draws = outdoor_forest.BuildDrawCommands(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix(), eyeX, eyeY, eyeZ, 6000.0f);
@@ -2184,7 +2187,8 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
profileBuildMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now()-profileBuildStart).count();
draw_reserve = std::max(draw_reserve, draws.size());
if (!draws.empty()) {
ava2::FlattenDraws(draws, draw_refs, draw_list);
if (!draw_list.empty()) {
const auto profileRasterStart = std::chrono::steady_clock::now();
uint32_t clear_col = (last_phase == "game") ? current_env.fog.GetColorUint() : 0x00000000;
if (use_gpu_raster) {
@@ -2195,7 +2199,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
SDL_RenderClear(renderer.get());
}
renderer3d->RenderSceneGPU(renderer.get(), draws);
renderer3d->RenderSceneGPU(renderer.get(), draw_list);
if (gpu_scene_texture) {
SDL_SetRenderTarget(renderer.get(), nullptr);
@@ -2206,7 +2210,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
// CPU Software Rasterizer (Fallback & Headless verification)
SDL_FillSurfaceRect(surface3d.get(), nullptr, clear_col);
renderer3d->Clear(clear_col, 1.0f);
renderer3d->RenderScene(surface3d.get(), draws);
renderer3d->RenderScene(surface3d.get(), draw_list);
SDL_UpdateTexture(texture3d.get(), nullptr, surface3d->pixels, surface3d->pitch);
SDL_FRect dst = { 0.0f, 0.0f, static_cast<float>(screen_width), static_cast<float>(screen_height) };