diff --git a/src/eter_render/HardwareScene.cpp b/src/eter_render/HardwareScene.cpp index aa14adeb..c0394966 100644 --- a/src/eter_render/HardwareScene.cpp +++ b/src/eter_render/HardwareScene.cpp @@ -239,13 +239,13 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std std::unordered_set activeTextures{white_}; for(const auto& b:batches)if(b.texture)activeTextures.insert(b.texture); for(auto& [key,entry]:textures_)if(activeTextures.count(entry.texture))entry.lastUsed=frame_; - while(textureBytes_>textureBudget_) { - auto oldest=textures_.end(); - for(auto it=textures_.begin();it!=textures_.end();++it) { - if(it->second.lastUsed>=frame_ || it->second.texture==white_)continue; - if(oldest==textures_.end() || it->second.lastUsedsecond.lastUsed)oldest=it; - } - if(oldest==textures_.end())break; // active working set is protected, even above the soft budget + // Oldest inactive images first; the active working set is protected, even above the soft budget. + std::vector candidates; + for(auto it=textures_.begin();it!=textures_.end();++it) + if(it->second.lastUsedsecond.texture!=white_)candidates.push_back(it); + std::sort(candidates.begin(),candidates.end(),[](const auto& a,const auto& b){return a->second.lastUsedsecond.lastUsed;}); + for(auto oldest:candidates) { + if(textureBytes_<=textureBudget_)break; textureBytes_-=oldest->second.bytes;DropPending(oldest->second.texture);SDL_ReleaseGPUTexture(device_,oldest->second.texture);textures_.erase(oldest); } } @@ -294,6 +294,10 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std auto* pass=SDL_BeginGPURenderPass(cmd,&color,1,&depth);if(!pass){SDL_SubmitGPUCommandBuffer(cmd);return false;} SDL_BindGPUVertexStorageBuffers(pass,0,&instances_,1); float palette[128*16]{};SDL_PushGPUVertexUniformData(cmd,1,palette,sizeof(palette)); + // Every draw reads its transform from the instance storage buffer (firstInstance = batch index), + // so the uniform header is pushed once per frame instead of 288 bytes per draw. + 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 for(size_t i=0;iindices : 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); - float params[4]={batch.alphaRef,(!batch.blend&&!batch.additive)?1.f:0.f,0,0};SDL_PushGPUFragmentUniformData(cmd,0,params,sizeof(params)); - auto transform=batch.transform;if(count>1)transform.modes[0]=2; - SDL_PushGPUVertexUniformData(cmd,0,&transform,sizeof(transform)); + 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; if(transform.skin[0]>0 && batch.palette && batch.palette.get()!=pushedPalette){pushedPalette=batch.palette.get();std::fill(std::begin(palette),std::end(palette),0);std::copy_n(batch.palette->data(),std::min(batch.palette->size(),size_t(128*16)),palette);SDL_PushGPUVertexUniformData(cmd,1,palette,sizeof(palette));} - SDL_DrawGPUIndexedPrimitives(pass,batch.indexCount,count,batch.firstIndex,0,count>1 ? i : 0);++lastDrawCalls_;i+=count; + SDL_DrawGPUIndexedPrimitives(pass,batch.indexCount,count,batch.firstIndex,0,i);++lastDrawCalls_;i+=count; } SDL_EndGPURenderPass(pass);const auto beforeSubmit=SDL_GetPerformanceCounter(); if(!SDL_SubmitGPUCommandBuffer(cmd))return false;