render: read every draw's transform from the instance buffer
The per-batch transforms are already uploaded to the storage buffer, so draws use firstInstance = batch index and the 288-byte uniform header is pushed once per frame; fragment params are pushed only on change. Texture eviction sorts inactive images once instead of rescanning the map per evicted image. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
5079c71f2e
commit
45644183b0
@@ -239,13 +239,13 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
std::unordered_set<SDL_GPUTexture*> 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.lastUsed<oldest->second.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<decltype(textures_)::iterator> candidates;
|
||||
for(auto it=textures_.begin();it!=textures_.end();++it)
|
||||
if(it->second.lastUsed<frame_ && it->second.texture!=white_)candidates.push_back(it);
|
||||
std::sort(candidates.begin(),candidates.end(),[](const auto& a,const auto& b){return a->second.lastUsed<b->second.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<float>* pushedPalette=nullptr; // draws of one instance share a palette; push once
|
||||
for(size_t i=0;i<batches.size();) {
|
||||
const auto& batch=batches[i];auto* mesh=meshes[i];
|
||||
@@ -309,11 +313,11 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
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);
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user