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:
co-authored by
Claude Opus 5.5
parent
b5a15871f4
commit
46880be537
+10
-6
@@ -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) };
|
||||
|
||||
@@ -733,6 +733,14 @@ void Gr2SceneRenderer::RenderScene(SDL_Surface* targetSurface, const std::vector
|
||||
TrimTextureCaches(true);
|
||||
}
|
||||
|
||||
void Gr2SceneRenderer::RenderScene(SDL_Surface* targetSurface, const std::vector<const Render3DDraw*>& 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<Render3DDraw>& 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<const Render3DDraw*>& 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();
|
||||
|
||||
@@ -90,9 +90,11 @@ public:
|
||||
|
||||
// Rasterize a list of draw commands (CPU software rasterizer)
|
||||
void RenderScene(SDL_Surface* targetSurface, const std::vector<Render3DDraw>& draws);
|
||||
void RenderScene(SDL_Surface* targetSurface, const std::vector<const Render3DDraw*>& draws);
|
||||
|
||||
// Native SDL_GPU depth/perspective pipeline; SDL geometry fallback for software renderers.
|
||||
void RenderSceneGPU(SDL_Renderer* renderer, const std::vector<Render3DDraw>& draws);
|
||||
void RenderSceneGPU(SDL_Renderer* renderer, const std::vector<const Render3DDraw*>& 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<DrawOrder> m_scratchSortedDraws;
|
||||
std::vector<const Render3DDraw*> m_scratchDrawPtrs;
|
||||
std::vector<GpuTransformedVertex> m_scratchGpuXverts;
|
||||
std::vector<SDL_Vertex> m_scratchSdlVertices;
|
||||
std::vector<int> m_scratchSdlIndices;
|
||||
|
||||
@@ -374,7 +374,16 @@ bool MapObjectManager::CheckCollisionNormal(float worldX, float worldZ, float ac
|
||||
}
|
||||
|
||||
std::vector<Render3DDraw> MapObjectManager::BuildDrawCommands(const float* view, const float* proj) const {
|
||||
std::vector<Render3DDrawRef> refs;
|
||||
AppendDrawRefs(view, proj, 0, refs);
|
||||
std::vector<Render3DDraw> 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<Render3DDrawRef>& 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<Render3DDraw> 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() {
|
||||
|
||||
@@ -134,6 +134,10 @@ public:
|
||||
|
||||
// Build 3D draw commands for all instantiated objects
|
||||
std::vector<Render3DDraw> 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<Render3DDrawRef>& out,
|
||||
const float* light_direction = nullptr, const float* light_ambient = nullptr) const;
|
||||
|
||||
// Clear all instantiated objects (retaining loaded model cache)
|
||||
void ClearObjects();
|
||||
|
||||
@@ -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<Render3DDraw>& owned, const std::vector<Render3DDrawRef>& refs,
|
||||
std::vector<const Render3DDraw*>& 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
|
||||
|
||||
@@ -632,7 +632,8 @@ void CPythonBackground::RenderCharacterShadowToTexture() {
|
||||
void CPythonBackground::RenderSnow() {
|
||||
}
|
||||
|
||||
void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector<Render3DDraw>& outDraws) {
|
||||
void CPythonBackground::BuildDrawCommands(const float* viewMatrix, const float* projMatrix, const float* camPos, std::vector<Render3DDraw>& outDraws,
|
||||
std::vector<Render3DDrawRef>* 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) {
|
||||
|
||||
@@ -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<Render3DDraw>& 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<Render3DDraw>& outDraws,
|
||||
std::vector<Render3DDrawRef>* outRefs = nullptr);
|
||||
|
||||
private:
|
||||
void LoadAtlas();
|
||||
|
||||
@@ -16,6 +16,13 @@
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
int main() {
|
||||
{ // Referenced (cached) commands splice in before owned[before], in submission order.
|
||||
std::vector<ava2::Render3DDraw> owned(3);ava2::Render3DDraw cachedA,cachedB,cachedC;
|
||||
std::vector<ava2::Render3DDrawRef> refs{{1,&cachedA},{1,&cachedB},{3,&cachedC}};
|
||||
std::vector<const ava2::Render3DDraw*> list;ava2::FlattenDraws(owned,refs,list);
|
||||
const std::vector<const ava2::Render3DDraw*> 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;}
|
||||
|
||||
Reference in New Issue
Block a user