Optimize rendering: frame pacing, UI culling, render scale and GPU scratch buffers

This commit is contained in:
shen
2026-10-08 17:06:17 -07:00
parent d7adbb3014
commit 08cc8865be
5 changed files with 180 additions and 72 deletions
+24 -27
View File
@@ -570,6 +570,8 @@ Gr2SceneRenderer::Gr2SceneRenderer(int width, int height) {
m_view[i] = (i % 5 == 0) ? 1.0f : 0.0f;
m_proj[i] = (i % 5 == 0) ? 1.0f : 0.0f;
}
m_logicalWidth = width;
m_logicalHeight = height;
SetViewport(0, 0, width, height);
}
@@ -1147,13 +1149,8 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
// 3: Alpha-tested geometry (speedtree foliage, hair)
// 4: Translucent surfaces (water, glass) - sorted back-to-front
// 5: Additive particles, weapon shining, skill ribbons - sorted back-to-front
struct DrawOrder {
int priority = 1;
float depth = 0.0f;
const Render3DDraw* draw = nullptr;
};
std::vector<DrawOrder> sortedDraws;
sortedDraws.reserve(draws.size());
m_scratchSortedDraws.clear();
m_scratchSortedDraws.reserve(draws.size());
for (const auto& d : draws) {
DrawOrder ord;
@@ -1178,10 +1175,10 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
} else {
ord.priority = 1; // Opaque
}
sortedDraws.push_back(ord);
m_scratchSortedDraws.push_back(ord);
}
std::stable_sort(sortedDraws.begin(), sortedDraws.end(), [](const DrawOrder& a, const DrawOrder& b) {
std::stable_sort(m_scratchSortedDraws.begin(), m_scratchSortedDraws.end(), [](const DrawOrder& a, const DrawOrder& b) {
if (a.priority != b.priority) return a.priority < b.priority;
if (a.priority >= 4) return a.depth > b.depth; // Back-to-front for transparent / additive
return a.depth < b.depth; // Front-to-back for opaque
@@ -1191,16 +1188,9 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
float fog_g = static_cast<float>((m_fog_color >> 8) & 0xFF) / 255.0f;
float fog_b = static_cast<float>(m_fog_color & 0xFF) / 255.0f;
struct GpuTransformedVertex {
float x_clip, y_clip, z_clip, w_clip;
float x_screen, y_screen;
float u, v;
float r, g, b, a;
};
const float NEAR_W = 0.05f;
for (const auto& ord : sortedDraws) {
for (const auto& ord : m_scratchSortedDraws) {
const auto& draw = *ord.draw;
if (draw.indices.size() < 3 || draw.positions.size() < 9) continue;
if (draw.texture0.empty() && draw.diffuse.empty()) continue;
@@ -1222,7 +1212,8 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
float mat_a = std::clamp(draw.material_diffuse[3], 0.0f, 1.0f);
size_t num_verts = draw.positions.size() / 3;
std::vector<GpuTransformedVertex> xverts(num_verts);
m_scratchGpuXverts.resize(num_verts);
auto& xverts = m_scratchGpuXverts;
for (size_t i = 0; i < num_verts; ++i) {
float px = draw.positions[i * 3 + 0];
@@ -1299,10 +1290,12 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
xverts[i].a = va;
}
std::vector<SDL_Vertex> vertices;
std::vector<int> indices;
vertices.reserve(draw.indices.size());
indices.reserve(draw.indices.size());
m_scratchSdlVertices.clear();
m_scratchSdlIndices.clear();
m_scratchSdlVertices.reserve(draw.indices.size());
m_scratchSdlIndices.reserve(draw.indices.size());
auto& vertices = m_scratchSdlVertices;
auto& indices = m_scratchSdlIndices;
auto add_sdl_vertex = [&](const GpuTransformedVertex& gv) -> int {
SDL_Vertex sv;
@@ -1442,18 +1435,22 @@ bool Gr2SceneRenderer::ProjectToScreen(float worldX, float worldY, float worldZ,
float ndc_x = clip[0] * inv_w;
float ndc_y = clip[1] * inv_w;
if (ndc_x < -2.0f || ndc_x > 2.0f || ndc_y < -2.0f || ndc_y > 2.0f) return false;
screenX = static_cast<float>(m_viewport[0]) + (ndc_x + 1.0f) * 0.5f * static_cast<float>(m_viewport[2]);
screenY = static_cast<float>(m_viewport[1]) + (1.0f - ndc_y) * 0.5f * static_cast<float>(m_viewport[3]);
int targetW = m_logicalWidth > 0 ? m_logicalWidth : m_viewport[2];
int targetH = m_logicalHeight > 0 ? m_logicalHeight : m_viewport[3];
screenX = static_cast<float>(m_viewport[0]) + (ndc_x + 1.0f) * 0.5f * static_cast<float>(targetW);
screenY = static_cast<float>(m_viewport[1]) + (1.0f - ndc_y) * 0.5f * static_cast<float>(targetH);
return true;
}
bool Gr2SceneRenderer::ScreenToWorldRay(float screenX, float screenY, float rayOrig[3], float rayDir[3]) const {
if (m_viewport[2] <= 0 || m_viewport[3] <= 0) return false;
int targetW = m_logicalWidth > 0 ? m_logicalWidth : m_viewport[2];
int targetH = m_logicalHeight > 0 ? m_logicalHeight : m_viewport[3];
if (targetW <= 0 || targetH <= 0) return false;
if (std::abs(m_proj[0]) < 1e-6f || std::abs(m_proj[5]) < 1e-6f) return false;
// 1. Calculate NDC coordinates [-1, 1]
float ndc_x = (2.0f * (screenX - static_cast<float>(m_viewport[0]))) / static_cast<float>(m_viewport[2]) - 1.0f;
float ndc_y = 1.0f - (2.0f * (screenY - static_cast<float>(m_viewport[1]))) / static_cast<float>(m_viewport[3]);
float ndc_x = (2.0f * (screenX - static_cast<float>(m_viewport[0]))) / static_cast<float>(targetW) - 1.0f;
float ndc_y = 1.0f - (2.0f * (screenY - static_cast<float>(m_viewport[1]))) / static_cast<float>(targetH);
// 2. View space direction from D3D perspective projection
float w = m_proj[0];