diff --git a/src/eter_render/Gr2ModelRenderer.cpp b/src/eter_render/Gr2ModelRenderer.cpp index 1b42f35f..93726db1 100644 --- a/src/eter_render/Gr2ModelRenderer.cpp +++ b/src/eter_render/Gr2ModelRenderer.cpp @@ -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.priorityalpha_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_modecull_mode; if(a.texture!=b.texture)return std::less{}(a.texture,b.texture); if(a.draw->geometry.get()!=b.draw->geometry.get())return std::less{}(a.draw->geometry.get(),b.draw->geometry.get()); return a.depthsecond; 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* 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;ivertices : 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; diff --git a/src/eter_render/HardwareScene.h b/src/eter_render/HardwareScene.h index 658c66c2..c48bcfb9 100644 --- a/src/eter_render/HardwareScene.h +++ b/src/eter_render/HardwareScene.h @@ -36,8 +36,10 @@ struct HardwareBatch { HardwareTransform transform{}; std::shared_ptr geometry; std::shared_ptr> 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) {}