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
+22
View File
@@ -133,6 +133,28 @@ int main() {
assert(raster.GetDecodedTextureBytes()==16);
raster.Clear();raster.RenderScene(cpu,{shared});assert(raster.GetTextureDecodeCount()==decodes+2);
SDL_DestroySurface(cpu);std::filesystem::remove(path);
// Blended draws sort back to front by the bounds centre of their indexed vertices: the far
// blue triangle's unindexed vertex 0 sits right before the eye and must not pull it in front.
ava2::Render3DDraw nearRed;
nearRed.positions={-.9f,-.9f,-.5f,.9f,-.9f,-.5f,0,.9f,-.5f};
nearRed.indices={0,1,2};nearRed.diffuse={0xffff0000,0xffff0000,0xffff0000};
nearRed.lighting=false;nearRed.fog=false;nearRed.cull_mode=1;nearRed.alpha_blend=true;nearRed.z_write=false;
ava2::Render3DDraw farBlue=nearRed;
farBlue.positions={0,0,-1.95f,-2,-2,1,2,-2,1,0,2,1};
farBlue.indices={1,2,3};farBlue.diffuse=std::vector<uint32_t>(4,0xff0000ff);
auto readOrder=[&](const std::vector<ava2::Render3DDraw>& commands) {
SDL_SetRenderDrawColor(renderer,0,0,0,255);SDL_RenderClear(renderer);
raster.RenderSceneGPU(renderer,commands);
auto* surface=SDL_RenderReadPixels(renderer,nullptr);assert(surface);
Uint32 pixel;std::memcpy(&pixel,static_cast<Uint8*>(surface->pixels)+32*surface->pitch+32*SDL_BYTESPERPIXEL(surface->format),SDL_BYTESPERPIXEL(surface->format));
Uint8 r,g,b,a;SDL_GetRGBA(pixel,SDL_GetPixelFormatDetails(surface->format),nullptr,&r,&g,&b,&a);
SDL_DestroySurface(surface);SDL_RenderPresent(renderer);
return std::array<int,3>{r,g,b};
};
auto order=readOrder({nearRed,farBlue});assert(order[0]>245&&order[2]<5);
order=readOrder({farBlue,nearRed});assert(order[0]>245&&order[2]<5);
auto sharedBlue=ava2::Render3DDraw::ShareGeometry(farBlue);
for(int frame=0;frame<2;++frame){order=readOrder({nearRed,sharedBlue});assert(order[0]>245&&order[2]<5);}
}
{
ava2::HardwareScene resident(device);