diff --git a/src/app/main.cpp b/src/app/main.cpp index b61afe6e..34fa185f 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -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 draw_refs; + static std::vector 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(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(screen_width), static_cast(screen_height) }; diff --git a/src/eter_render/Gr2ModelRenderer.cpp b/src/eter_render/Gr2ModelRenderer.cpp index 86abfddd..1b42f35f 100644 --- a/src/eter_render/Gr2ModelRenderer.cpp +++ b/src/eter_render/Gr2ModelRenderer.cpp @@ -733,6 +733,14 @@ void Gr2SceneRenderer::RenderScene(SDL_Surface* targetSurface, const std::vector TrimTextureCaches(true); } +void Gr2SceneRenderer::RenderScene(SDL_Surface* targetSurface, const std::vector& draws) { + ++m_textureFrame; + for (const auto* draw : draws) { + RenderDraw(targetSurface, *draw); + } + TrimTextureCaches(true); +} + void Gr2SceneRenderer::DestroyGPUTextures() { for (auto& pair : m_gpuTextureCache) { if (pair.second.texture) { @@ -805,6 +813,13 @@ void Gr2SceneRenderer::SetGPUDevice(SDL_GPUDevice* device) { } void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws) { + m_scratchDrawPtrs.clear(); + m_scratchDrawPtrs.reserve(draws.size()); + for (const auto& d : draws) m_scratchDrawPtrs.push_back(&d); + RenderSceneGPU(renderer, m_scratchDrawPtrs); +} + +void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws) { if (!renderer || draws.empty()) return; ++m_textureFrame; @@ -825,7 +840,8 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector< m_scratchSortedDraws.clear(); m_scratchSortedDraws.reserve(draws.size()); - for (const auto& d : draws) { + for (const auto* drawPtr : draws) { + const auto& d = *drawPtr; DrawOrder ord; ord.draw = &d; const auto& sortedMesh = d.Mesh(); diff --git a/src/eter_render/Gr2ModelRenderer.h b/src/eter_render/Gr2ModelRenderer.h index 1b4438ad..e7137287 100644 --- a/src/eter_render/Gr2ModelRenderer.h +++ b/src/eter_render/Gr2ModelRenderer.h @@ -90,9 +90,11 @@ public: // Rasterize a list of draw commands (CPU software rasterizer) void RenderScene(SDL_Surface* targetSurface, const std::vector& draws); + void RenderScene(SDL_Surface* targetSurface, const std::vector& draws); // Native SDL_GPU depth/perspective pipeline; SDL geometry fallback for software renderers. void RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws); + void RenderSceneGPU(SDL_Renderer* renderer, const std::vector& draws); void SetGPUDevice(SDL_GPUDevice* device); void DestroyGPUTextures(); SDL_Texture* GetGPUTexture(SDL_Renderer* renderer, const std::string& texturePath); @@ -166,6 +168,7 @@ private: // Persistent scratch buffers to eliminate per-draw heap allocation churn in RenderSceneGPU std::vector m_scratchSortedDraws; + std::vector m_scratchDrawPtrs; std::vector m_scratchGpuXverts; std::vector m_scratchSdlVertices; std::vector m_scratchSdlIndices; diff --git a/src/eter_render/MapObjectManager.cpp b/src/eter_render/MapObjectManager.cpp index f82c7b29..fd2a7243 100644 --- a/src/eter_render/MapObjectManager.cpp +++ b/src/eter_render/MapObjectManager.cpp @@ -374,7 +374,16 @@ bool MapObjectManager::CheckCollisionNormal(float worldX, float worldZ, float ac } std::vector MapObjectManager::BuildDrawCommands(const float* view, const float* proj) const { + std::vector refs; + AppendDrawRefs(view, proj, 0, refs); std::vector all_draws; + all_draws.reserve(refs.size()); + for (const auto& ref : refs) all_draws.push_back(*ref.draw); + return all_draws; +} + +void MapObjectManager::AppendDrawRefs(const float* view, const float* proj, std::size_t before, std::vector& out, + const float* light_direction, const float* light_ambient) const { for (const auto& obj : m_objects) { if (!obj.model) continue; @@ -404,9 +413,12 @@ std::vector MapObjectManager::BuildDrawCommands(const float* view, obj.draws_cached = true; } if (!IsBoundsVisible(obj.bounds_min, obj.bounds_max, view, proj)) continue; - all_draws.insert(all_draws.end(), obj.cached_draws.begin(), obj.cached_draws.end()); + for (auto& draw : obj.cached_draws) { + 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 all_draws; } void MapObjectManager::ClearObjects() { diff --git a/src/eter_render/MapObjectManager.h b/src/eter_render/MapObjectManager.h index ddfca8d8..e888bf06 100644 --- a/src/eter_render/MapObjectManager.h +++ b/src/eter_render/MapObjectManager.h @@ -134,6 +134,10 @@ public: // Build 3D draw commands for all instantiated objects std::vector BuildDrawCommands(const float* view = nullptr, const float* proj = nullptr) const; + // Same visible set without copying: the cached commands are referenced, spliced before owned[before]. + // Valid until the objects are cleared or added to. light_* (optional) are written into the cache. + void AppendDrawRefs(const float* view, const float* proj, std::size_t before, std::vector& out, + const float* light_direction = nullptr, const float* light_ambient = nullptr) const; // Clear all instantiated objects (retaining loaded model cache) void ClearObjects(); diff --git a/src/eter_render/RenderCommands3D.h b/src/eter_render/RenderCommands3D.h index b8c779de..7d23b5c5 100644 --- a/src/eter_render/RenderCommands3D.h +++ b/src/eter_render/RenderCommands3D.h @@ -115,4 +115,22 @@ struct Render3DDraw { float light_ambient[4] = {0.55f, 0.55f, 0.55f, 1.0f}; }; +// A cached command submitted by pointer instead of copied into the frame's list; it is +// drawn just before owned[before], so the flattened order equals a single flat list. +struct Render3DDrawRef { + std::size_t before = 0; + const Render3DDraw* draw = nullptr; +}; + +inline void FlattenDraws(const std::vector& owned, const std::vector& refs, + std::vector& out) { + out.clear(); + out.reserve(owned.size() + refs.size()); + std::size_t r = 0; + for (std::size_t i = 0; i <= owned.size(); ++i) { + for (; r < refs.size() && refs[r].before <= i; ++r) out.push_back(refs[r].draw); + if (i < owned.size()) out.push_back(&owned[i]); + } +} + } // namespace ava2 diff --git a/src/scripting/python_background.cpp b/src/scripting/python_background.cpp index e9ec048e..64a66e23 100644 --- a/src/scripting/python_background.cpp +++ b/src/scripting/python_background.cpp @@ -632,7 +632,8 @@ void CPythonBackground::RenderCharacterShadowToTexture() { void CPythonBackground::RenderSnow() { } -void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector& outDraws) { +void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector& outDraws, + std::vector* outRefs) { if (!IsMapReady()) return; if (m_bVisiblePart[PART_SKY] && m_skybox && viewMatrix && projMatrix && camPos) { @@ -675,7 +676,17 @@ void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* outDraws.push_back(std::move(wd)); } - if (m_bVisiblePart[PART_OBJECT] && m_mapObjects) { + 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); + } else if (m_bVisiblePart[PART_OBJECT] && m_mapObjects) { auto bdraws = m_mapObjects->BuildDrawCommands(viewMatrix, projMatrix); for (auto& bd : bdraws) { if (m_pCurrentEnvironment) { diff --git a/src/scripting/python_background.h b/src/scripting/python_background.h index 1809c3c5..e47cb095 100644 --- a/src/scripting/python_background.h +++ b/src/scripting/python_background.h @@ -205,7 +205,9 @@ public: SpeedTreeForest* GetForest() { return m_forest.get(); } const OutdoorMapInfo* GetOutdoorMap() const { return m_strMapName.empty() ? nullptr : &m_outdoorMap; } - void BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector& outDraws); + // outRefs (optional) receives the cached map-object commands by pointer instead of copies in outDraws. + void BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector& outDraws, + std::vector* outRefs = nullptr); private: void LoadAtlas(); diff --git a/tests/hardware_scene_test.cpp b/tests/hardware_scene_test.cpp index 00b9bd04..093e0bf0 100644 --- a/tests/hardware_scene_test.cpp +++ b/tests/hardware_scene_test.cpp @@ -16,6 +16,13 @@ #include #include int main() { + { // Referenced (cached) commands splice in before owned[before], in submission order. + std::vector owned(3);ava2::Render3DDraw cachedA,cachedB,cachedC; + std::vector refs{{1,&cachedA},{1,&cachedB},{3,&cachedC}}; + std::vector list;ava2::FlattenDraws(owned,refs,list); + const std::vector expect{&owned[0],&cachedA,&cachedB,&owned[1],&owned[2],&cachedC}; + assert(list==expect); + } if(!SDL_Init(SDL_INIT_VIDEO))return 77; auto* device=SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_MSL|SDL_GPU_SHADERFORMAT_SPIRV,true,nullptr); if(!device){SDL_Quit();return 77;}