Sort blended draws by bounds centre instead of first vertex

Back-to-front order for blended, non-depth-writing, water and additive
draws now uses the bounds centre of the vertices the draw indexes. The
centre of shared (immutable) geometry is cached per index range and
validated against the owning pointer; per-frame geometry is recomputed.
Depth-writing opaque draws keep the first-vertex tie-break.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 18:09:31 +09:00
co-authored by Claude Opus 5.5
parent efb7c120c0
commit 5de09d07e9
3 changed files with 80 additions and 9 deletions
+45 -9
View File
@@ -819,6 +819,38 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
RenderSceneGPU(renderer, m_scratchDrawPtrs);
}
void Gr2SceneRenderer::DrawSortCenter(const Render3DDraw& draw, float out[3]) {
const auto& mesh = draw.Mesh();
const auto indices = draw.Indices();
SortCenterKey key{draw.geometry.get(), draw.first_index, draw.index_count};
if (draw.geometry) {
auto it = m_sortCenterCache.find(key);
if (it != m_sortCenterCache.end()) {
if (it->second.owner.lock() == draw.geometry) {
std::memcpy(out, it->second.c, sizeof(it->second.c));
return;
}
m_sortCenterCache.erase(it); // freed geometry whose address was reused
}
}
const size_t vertexCount = mesh.positions.size() / 3;
float lo[3] = {INFINITY, INFINITY, INFINITY}, hi[3] = {-INFINITY, -INFINITY, -INFINITY};
for (size_t i = 0; i < indices.size; ++i) {
const uint32_t v = indices.data[i];
if (v >= vertexCount) continue;
const float* p = &mesh.positions[size_t(v) * 3];
for (int a = 0; a < 3; ++a) { lo[a] = std::min(lo[a], p[a]); hi[a] = std::max(hi[a], p[a]); }
}
if (lo[0] > hi[0]) return; // no valid vertex: keep the caller's fallback
for (int a = 0; a < 3; ++a) out[a] = 0.5f * (lo[a] + hi[a]);
if (draw.geometry) {
if (m_sortCenterCache.size() >= 8192) m_sortCenterCache.clear();
auto& entry = m_sortCenterCache[key];
entry.owner = draw.geometry;
std::memcpy(entry.c, out, sizeof(entry.c));
}
}
void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<const Render3DDraw*>& draws) {
if (!renderer || draws.empty()) return;
++m_textureFrame;
@@ -853,15 +885,6 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
ord.texture=m_hardware->Texture(d.texture0,data->width,data->height,data->rgba.data());}
if(!ord.texture)continue;
}
{
float cx = sortedMesh.positions[0], cy = sortedMesh.positions[1], cz = sortedMesh.positions[2];
float wx = cx * d.world[0] + cy * d.world[4] + cz * d.world[8] + d.world[12];
float wy = cx * d.world[1] + cy * d.world[5] + cz * d.world[9] + d.world[13];
float wz = cx * d.world[2] + cy * d.world[6] + cz * d.world[10] + d.world[14];
float dx = wx - m_eye[0], dy = wy - m_eye[1], dz = wz - m_eye[2];
ord.depth = dx * dx + dy * dy + dz * dz;
}
if (!d.z_enable) {
ord.priority = 0; // Skybox
} else if (d.terrain) {
@@ -875,6 +898,19 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
} else {
ord.priority = 1; // Opaque
}
{
// Order-dependent draws (blended, no depth write) sort by the bounds centre of the
// vertices they reference; for depth-writing opaque draws depth is only a tie-break.
float c[3] = {sortedMesh.positions[0], sortedMesh.positions[1], sortedMesh.positions[2]};
if (ord.priority >= 4 || d.alpha_blend || !d.z_write) DrawSortCenter(d, c);
float cx = c[0], cy = c[1], cz = c[2];
float wx = cx * d.world[0] + cy * d.world[4] + cz * d.world[8] + d.world[12];
float wy = cx * d.world[1] + cy * d.world[5] + cz * d.world[9] + d.world[13];
float wz = cx * d.world[2] + cy * d.world[6] + cz * d.world[10] + d.world[14];
float dx = wx - m_eye[0], dy = wy - m_eye[1], dz = wz - m_eye[2];
ord.depth = dx * dx + dy * dy + dz * dz;
}
m_scratchSortedDraws.push_back(ord);
}
+13
View File
@@ -170,6 +170,19 @@ private:
// Persistent scratch buffers to eliminate per-draw heap allocation churn in RenderSceneGPU
std::vector<DrawOrder> m_scratchSortedDraws;
// Local bounds centre of a shared geometry's index range (immutable, so cached across frames).
struct SortCenterKey {
const Render3DDraw* geometry; std::uint32_t first, count;
bool operator==(const SortCenterKey& o) const { return geometry==o.geometry && first==o.first && count==o.count; }
};
struct SortCenterHash {
size_t operator()(const SortCenterKey& k) const {
return std::hash<const void*>{}(k.geometry) ^ (size_t(k.first) * 0x9E3779B97F4A7C15ull) ^ (size_t(k.count) << 1);
}
};
struct SortCenter { std::weak_ptr<const Render3DDraw> owner; float c[3]; };
std::unordered_map<SortCenterKey, SortCenter, SortCenterHash> m_sortCenterCache;
void DrawSortCenter(const Render3DDraw& draw, float out[3]);
std::vector<const Render3DDraw*> m_scratchDrawPtrs;
std::vector<GpuTransformedVertex> m_scratchGpuXverts;
std::vector<SDL_Vertex> m_scratchSdlVertices;