Group depth-writing draws by pipeline state and skip redundant GPU binds

HardwareScene now tracks the bound vertex/index buffer, pipeline and
sampler within the pass and only rebinds on change (each sampler rebind
allocates a new descriptor set on SDL's Vulkan backend). The opaque sort
orders depth-writing opaque and alpha-tested draws by cull mode, texture
and geometry so consecutive batches share state; blended and
non-depth-writing draws keep back-to-front order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 17:29:10 +09:00
co-authored by Claude Opus 5.5
parent dcd301a080
commit 696f4f62e6
3 changed files with 22 additions and 9 deletions
+8 -3
View File
@@ -880,9 +880,14 @@ void Gr2SceneRenderer::RenderSceneGPU(SDL_Renderer* renderer, const std::vector<
std::stable_sort(m_scratchSortedDraws.begin(),m_scratchSortedDraws.end(),[this](const DrawOrder& a,const DrawOrder& b){
if(a.priority!=b.priority)return a.priority<b.priority;
if(m_hardware && a.priority==1) {
if(a.draw->alpha_blend!=b.draw->alpha_blend)return !a.draw->alpha_blend;
if(!a.draw->alpha_blend) {
if(m_hardware && (a.priority==1 || a.priority==3)) {
// Opaque and alpha-tested draws that write depth do not depend on submission order:
// group them by pipeline state (cull/depth write), then texture and geometry, so
// HardwareScene can skip redundant binds and instance runs. Everything else stays back to front.
const bool aState=!a.draw->alpha_blend&&a.draw->z_write,bState=!b.draw->alpha_blend&&b.draw->z_write;
if(aState!=bState)return aState;
if(aState) {
if(a.draw->cull_mode!=b.draw->cull_mode)return a.draw->cull_mode<b.draw->cull_mode;
if(a.texture!=b.texture)return std::less<SDL_GPUTexture*>{}(a.texture,b.texture);
if(a.draw->geometry.get()!=b.draw->geometry.get())return std::less<const Render3DDraw*>{}(a.draw->geometry.get(),b.draw->geometry.get());
return a.depth<b.depth;
+11 -5
View File
@@ -133,7 +133,7 @@ bool HardwareScene::Initialize(SDL_Renderer* renderer,int width,int height) {
return true;
}
SDL_GPUGraphicsPipeline* HardwareScene::Pipeline(const HardwareBatch& b) {
unsigned key=b.cull|(b.depth<<3)|(b.depthWrite<<4)|(b.blend<<5)|(b.additive<<6);
const unsigned key=PipelineKey(b);
auto it=pipelines_.find(key);if(it!=pipelines_.end())return it->second;
SDL_GPUGraphicsPipelineCreateInfo info{};
info.vertex_shader=vertex_;info.fragment_shader=fragment_;info.primitive_type=SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
@@ -300,6 +300,10 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
HardwareTransform header{};header.modes[0]=2;SDL_PushGPUVertexUniformData(cmd,0,&header,sizeof(header));
float pushedParams[4]={-2,-1,0,0}; // never a real alphaRef/opaque pair
const std::vector<float>* pushedPalette=nullptr; // draws of one instance share a palette; push once
// Bindings persist within the pass (SDL re-applies them after a pipeline change); skip redundant ones,
// each sampler rebind costs a fresh descriptor set on Vulkan.
SDL_GPUBuffer* boundVertices=nullptr;SDL_GPUBuffer* boundIndices=nullptr;SDL_GPUTexture* boundTexture=nullptr;
unsigned boundPipeline=~0u;
for(size_t i=0;i<batches.size();) {
const auto& batch=batches[i];auto* mesh=meshes[i];
size_t count=1;
@@ -310,10 +314,12 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
if(meshes[i+count]!=mesh || next.firstIndex!=batch.firstIndex || next.indexCount!=batch.indexCount || next.texture!=batch.texture || next.cull!=batch.cull || next.depth!=batch.depth || next.depthWrite!=batch.depthWrite || next.blend || next.additive || next.alphaRef!=batch.alphaRef || next.transform.skin[0]!=0 || next.transform.modes[0]<.5f)break;
++count;
}
SDL_GPUBufferBinding binding{mesh ? mesh->vertices : vertices_,0};SDL_BindGPUVertexBuffers(pass,0,&binding,1);
binding.buffer=mesh ? mesh->indices : indices_;SDL_BindGPUIndexBuffer(pass,&binding,SDL_GPU_INDEXELEMENTSIZE_32BIT);
SDL_BindGPUGraphicsPipeline(pass,Pipeline(batch));
SDL_GPUTextureSamplerBinding texture{batch.texture ? batch.texture : white_,sampler_};SDL_BindGPUFragmentSamplers(pass,0,&texture,1);
SDL_GPUBuffer* vertexBuffer=mesh ? mesh->vertices : vertices_;SDL_GPUBuffer* indexBuffer=mesh ? mesh->indices : indices_;
if(vertexBuffer!=boundVertices){boundVertices=vertexBuffer;SDL_GPUBufferBinding binding{vertexBuffer,0};SDL_BindGPUVertexBuffers(pass,0,&binding,1);}
if(indexBuffer!=boundIndices){boundIndices=indexBuffer;SDL_GPUBufferBinding binding{indexBuffer,0};SDL_BindGPUIndexBuffer(pass,&binding,SDL_GPU_INDEXELEMENTSIZE_32BIT);}
if(PipelineKey(batch)!=boundPipeline){boundPipeline=PipelineKey(batch);SDL_BindGPUGraphicsPipeline(pass,Pipeline(batch));}
SDL_GPUTexture* batchTexture=batch.texture ? batch.texture : white_;
if(batchTexture!=boundTexture){boundTexture=batchTexture;SDL_GPUTextureSamplerBinding texture{batchTexture,sampler_};SDL_BindGPUFragmentSamplers(pass,0,&texture,1);}
float params[4]={batch.alphaRef,(!batch.blend&&!batch.additive)?1.f:0.f,0,0};
if(std::memcmp(params,pushedParams,sizeof(params))!=0){std::memcpy(pushedParams,params,sizeof(params));SDL_PushGPUFragmentUniformData(cmd,0,params,sizeof(params));}
const auto& transform=batch.transform;
+3 -1
View File
@@ -36,8 +36,10 @@ struct HardwareBatch {
HardwareTransform transform{};
std::shared_ptr<const Render3DDraw> geometry;
std::shared_ptr<const std::vector<float>> palette;
};
inline unsigned PipelineKey(const HardwareBatch& b) {
return b.cull|(unsigned(b.depth)<<3)|(unsigned(b.depthWrite)<<4)|(unsigned(b.blend)<<5)|(unsigned(b.additive)<<6);
}
class HardwareScene {
public:
explicit HardwareScene(SDL_GPUDevice* device): device_(device) {}