render: batch texture and mesh uploads into the frame command buffer
Texture() and Resident() stage their bytes on the CPU; Render copies everything in one transfer buffer and copy pass (plus mip generation) at the start of its own command buffer instead of acquiring and submitting a command buffer per resource. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
aaee4c8f70
commit
5079c71f2e
@@ -67,32 +67,17 @@ SDL_GPUTexture* HardwareScene::FindTexture(const std::string& key) {
|
||||
}
|
||||
SDL_GPUTexture* HardwareScene::Texture(const std::string& key,int width,int height,const void* rgba) {
|
||||
if(auto* found=FindTexture(key))return found;
|
||||
// Own uploads on the same queue as the 3D pass. SDL_Renderer's pending
|
||||
// texture uploads are submitted at Present, after our custom command buffer.
|
||||
// Own uploads on the same queue as the 3D pass (SDL_Renderer's pending texture uploads are
|
||||
// submitted at Present, after our custom command buffer); copied at the start of Render.
|
||||
SDL_GPUTextureCreateInfo ti{};ti.type=SDL_GPU_TEXTURETYPE_2D;ti.format=SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
|
||||
// Full mip chain: minified terrain/props otherwise shimmer and read the whole level-0 texel footprint.
|
||||
// SDL_GenerateMipmapsForGPUTexture needs COLOR_TARGET usage.
|
||||
Uint32 levels=1;for(int extent=std::max(width,height);extent>1;extent>>=1)++levels;
|
||||
ti.usage=SDL_GPU_TEXTUREUSAGE_SAMPLER|(levels>1 ? SDL_GPU_TEXTUREUSAGE_COLOR_TARGET : 0);ti.width=width;ti.height=height;ti.layer_count_or_depth=1;ti.num_levels=levels;
|
||||
auto* texture=SDL_CreateGPUTexture(device_,&ti);if(!texture)return nullptr;
|
||||
SDL_GPUTransferBufferCreateInfo transferInfo{};transferInfo.usage=SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;transferInfo.size=width*height*4;
|
||||
auto* transfer=SDL_CreateGPUTransferBuffer(device_,&transferInfo);
|
||||
auto* bytes=transfer ? SDL_MapGPUTransferBuffer(device_,transfer,false) : nullptr;
|
||||
auto* command=bytes ? SDL_AcquireGPUCommandBuffer(device_) : nullptr;
|
||||
if(!bytes || !command) {
|
||||
if(bytes)SDL_UnmapGPUTransferBuffer(device_,transfer);
|
||||
if(transfer)SDL_ReleaseGPUTransferBuffer(device_,transfer);
|
||||
SDL_ReleaseGPUTexture(device_,texture);return nullptr;
|
||||
}
|
||||
std::memcpy(bytes,rgba,transferInfo.size);SDL_UnmapGPUTransferBuffer(device_,transfer);
|
||||
auto* copy=SDL_BeginGPUCopyPass(command);
|
||||
if(!copy){SDL_CancelGPUCommandBuffer(command);SDL_ReleaseGPUTransferBuffer(device_,transfer);SDL_ReleaseGPUTexture(device_,texture);return nullptr;}
|
||||
SDL_GPUTextureTransferInfo source{};source.transfer_buffer=transfer;source.pixels_per_row=width;source.rows_per_layer=height;
|
||||
SDL_GPUTextureRegion dest{};dest.texture=texture;dest.w=width;dest.h=height;dest.d=1;
|
||||
SDL_UploadToGPUTexture(copy,&source,&dest,false);SDL_EndGPUCopyPass(copy);
|
||||
if(levels>1)SDL_GenerateMipmapsForGPUTexture(command,texture);
|
||||
const bool submitted=SDL_SubmitGPUCommandBuffer(command);SDL_ReleaseGPUTransferBuffer(device_,transfer);
|
||||
if(!submitted){SDL_ReleaseGPUTexture(device_,texture);return nullptr;}
|
||||
const uint32_t size=uint32_t(width)*height*4;uint32_t offset=0;
|
||||
std::memcpy(Stage(size,offset),rgba,size);
|
||||
pending_.push_back({texture,nullptr,offset,size,uint32_t(width),uint32_t(height),levels>1});
|
||||
const size_t imageBytes=size_t(width)*height*4*(levels>1 ? 4 : 3)/3; // mip chain adds ~1/3
|
||||
textures_.emplace(key,TextureEntry{texture,imageBytes,frame_+1});textureBytes_+=imageBytes;++textureUploads_;return texture;
|
||||
}
|
||||
@@ -193,6 +178,39 @@ HardwareVertex MakeHardwareVertex(const Render3DDraw& mesh,size_t i) {
|
||||
v.bones[k]=mesh.bone_indices[i*4+k];v.weights[k]=uint16_t(std::lround(std::clamp(mesh.bone_weights[i*4+k],0.f,1.f)*65535.f));}
|
||||
return v;
|
||||
}
|
||||
uint8_t* HardwareScene::Stage(uint32_t size,uint32_t& offset) {
|
||||
offset=uint32_t((pendingBytes_.size()+15)&~size_t(15)); // texel/placement-friendly offsets
|
||||
pendingBytes_.resize(offset+size);return pendingBytes_.data()+offset;
|
||||
}
|
||||
void HardwareScene::DropPending(const void* resource) {
|
||||
pending_.erase(std::remove_if(pending_.begin(),pending_.end(),[&](const PendingUpload& upload){
|
||||
return upload.texture==resource || upload.buffer==resource;}),pending_.end());
|
||||
}
|
||||
bool HardwareScene::FlushUploads(SDL_GPUCommandBuffer* command) {
|
||||
if(pending_.empty())return true;
|
||||
SDL_GPUTransferBufferCreateInfo ti{};ti.usage=SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;ti.size=uint32_t(pendingBytes_.size());
|
||||
auto* transfer=SDL_CreateGPUTransferBuffer(device_,&ti);
|
||||
auto* bytes=transfer ? SDL_MapGPUTransferBuffer(device_,transfer,false) : nullptr;
|
||||
if(!bytes){if(transfer)SDL_ReleaseGPUTransferBuffer(device_,transfer);return false;}
|
||||
std::memcpy(bytes,pendingBytes_.data(),pendingBytes_.size());SDL_UnmapGPUTransferBuffer(device_,transfer);
|
||||
auto* copy=SDL_BeginGPUCopyPass(command);if(!copy){SDL_ReleaseGPUTransferBuffer(device_,transfer);return false;}
|
||||
for(const auto& upload:pending_) {
|
||||
if(upload.texture) {
|
||||
SDL_GPUTextureTransferInfo source{};source.transfer_buffer=transfer;source.offset=upload.offset;source.pixels_per_row=upload.width;source.rows_per_layer=upload.height;
|
||||
SDL_GPUTextureRegion dest{};dest.texture=upload.texture;dest.w=upload.width;dest.h=upload.height;dest.d=1;
|
||||
SDL_UploadToGPUTexture(copy,&source,&dest,false);
|
||||
} else {
|
||||
SDL_GPUTransferBufferLocation from{transfer,upload.offset};SDL_GPUBufferRegion to{upload.buffer,0,upload.size};
|
||||
SDL_UploadToGPUBuffer(copy,&from,&to,false);
|
||||
}
|
||||
}
|
||||
SDL_EndGPUCopyPass(copy);
|
||||
for(const auto& upload:pending_)if(upload.texture && upload.mipmaps)SDL_GenerateMipmapsForGPUTexture(command,upload.texture);
|
||||
SDL_ReleaseGPUTransferBuffer(device_,transfer); // released once the command buffer completes
|
||||
pending_.clear();pendingBytes_.clear();
|
||||
if(pendingBytes_.capacity()>64u*1024u*1024u)pendingBytes_.shrink_to_fit(); // drop a map-load burst
|
||||
return true;
|
||||
}
|
||||
HardwareScene::ResidentMesh* HardwareScene::Resident(const std::shared_ptr<const Render3DDraw>& geometry) {
|
||||
auto it=resident_.find(geometry.get());
|
||||
if(it!=resident_.end()){it->second.lastUsed=frame_;return &it->second;}
|
||||
@@ -200,24 +218,16 @@ HardwareScene::ResidentMesh* HardwareScene::Resident(const std::shared_ptr<const
|
||||
const size_t count=mesh.positions.size()/3;
|
||||
if(!count || mesh.indices.empty())return nullptr;
|
||||
for(auto index:mesh.indices)if(index>=count)return nullptr;
|
||||
std::vector<HardwareVertex> packed;packed.reserve(count);
|
||||
for(size_t i=0;i<count;++i)packed.push_back(MakeHardwareVertex(mesh,i));
|
||||
const uint32_t vb=packed.size()*sizeof(HardwareVertex),ib=mesh.indices.size()*sizeof(uint32_t);
|
||||
const uint32_t vb=count*sizeof(HardwareVertex),ib=mesh.indices.size()*sizeof(uint32_t);
|
||||
SDL_GPUBufferCreateInfo bi{};bi.usage=SDL_GPU_BUFFERUSAGE_VERTEX;bi.size=vb;
|
||||
ResidentMesh r;r.source=geometry;r.vertices=SDL_CreateGPUBuffer(device_,&bi);
|
||||
bi.usage=SDL_GPU_BUFFERUSAGE_INDEX;bi.size=ib;r.indices=SDL_CreateGPUBuffer(device_,&bi);
|
||||
SDL_GPUTransferBufferCreateInfo ti{};ti.usage=SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;ti.size=vb+ib;
|
||||
auto* transfer=SDL_CreateGPUTransferBuffer(device_,&ti);
|
||||
auto* bytes=transfer ? static_cast<Uint8*>(SDL_MapGPUTransferBuffer(device_,transfer,false)) : nullptr;
|
||||
auto* command=bytes && r.vertices && r.indices ? SDL_AcquireGPUCommandBuffer(device_) : nullptr;
|
||||
if(!command){if(bytes)SDL_UnmapGPUTransferBuffer(device_,transfer);if(transfer)SDL_ReleaseGPUTransferBuffer(device_,transfer);if(r.vertices)SDL_ReleaseGPUBuffer(device_,r.vertices);if(r.indices)SDL_ReleaseGPUBuffer(device_,r.indices);return nullptr;}
|
||||
std::memcpy(bytes,packed.data(),vb);std::memcpy(bytes+vb,mesh.indices.data(),ib);SDL_UnmapGPUTransferBuffer(device_,transfer);
|
||||
auto* copy=SDL_BeginGPUCopyPass(command);
|
||||
if(!copy){SDL_CancelGPUCommandBuffer(command);SDL_ReleaseGPUTransferBuffer(device_,transfer);SDL_ReleaseGPUBuffer(device_,r.vertices);SDL_ReleaseGPUBuffer(device_,r.indices);return nullptr;}
|
||||
SDL_GPUTransferBufferLocation from{transfer,0};SDL_GPUBufferRegion to{r.vertices,0,vb};SDL_UploadToGPUBuffer(copy,&from,&to,false);
|
||||
from.offset=vb;to={r.indices,0,ib};SDL_UploadToGPUBuffer(copy,&from,&to,false);SDL_EndGPUCopyPass(copy);
|
||||
bool ok=SDL_SubmitGPUCommandBuffer(command);SDL_ReleaseGPUTransferBuffer(device_,transfer);
|
||||
if(!ok){SDL_ReleaseGPUBuffer(device_,r.vertices);SDL_ReleaseGPUBuffer(device_,r.indices);return nullptr;}
|
||||
if(!r.vertices || !r.indices){if(r.vertices)SDL_ReleaseGPUBuffer(device_,r.vertices);if(r.indices)SDL_ReleaseGPUBuffer(device_,r.indices);return nullptr;}
|
||||
uint32_t offset=0;auto* packed=Stage(vb,offset);
|
||||
for(size_t i=0;i<count;++i){const auto vertex=MakeHardwareVertex(mesh,i);std::memcpy(packed+i*sizeof(vertex),&vertex,sizeof(vertex));}
|
||||
pending_.push_back({nullptr,r.vertices,offset,vb});
|
||||
std::memcpy(Stage(ib,offset),mesh.indices.data(),ib);
|
||||
pending_.push_back({nullptr,r.indices,offset,ib});
|
||||
r.count=mesh.indices.size();r.bytes=vb+ib;r.lastUsed=frame_;residentBytes_+=r.bytes;++residentUploads_;
|
||||
return &resident_.emplace(geometry.get(),std::move(r)).first->second;
|
||||
}
|
||||
@@ -236,13 +246,13 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
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
|
||||
textureBytes_-=oldest->second.bytes;SDL_ReleaseGPUTexture(device_,oldest->second.texture);textures_.erase(oldest);
|
||||
textureBytes_-=oldest->second.bytes;DropPending(oldest->second.texture);SDL_ReleaseGPUTexture(device_,oldest->second.texture);textures_.erase(oldest);
|
||||
}
|
||||
}
|
||||
// Cached buffers can be released without waiting; SDL defers GPU destruction.
|
||||
for(auto it=resident_.begin();it!=resident_.end();) {
|
||||
if(residentBytes_>256u*1024u*1024u && it->second.lastUsed+2<frame_){
|
||||
residentBytes_-=it->second.bytes;SDL_ReleaseGPUBuffer(device_,it->second.vertices);SDL_ReleaseGPUBuffer(device_,it->second.indices);it=resident_.erase(it);
|
||||
residentBytes_-=it->second.bytes;DropPending(it->second.vertices);DropPending(it->second.indices);SDL_ReleaseGPUBuffer(device_,it->second.vertices);SDL_ReleaseGPUBuffer(device_,it->second.indices);it=resident_.erase(it);
|
||||
}else ++it;
|
||||
}
|
||||
std::vector<ResidentMesh*> meshes;meshes.reserve(batches.size());
|
||||
@@ -273,13 +283,15 @@ bool HardwareScene::Render(SDL_Renderer* renderer,int width,int height,const std
|
||||
SDL_UnmapGPUTransferBuffer(device_,instanceTransfer_);
|
||||
if(!SDL_FlushRenderer(renderer))return false;
|
||||
auto* cmd=SDL_AcquireGPUCommandBuffer(device_);if(!cmd)return false;
|
||||
auto* copy=SDL_BeginGPUCopyPass(cmd);if(!copy){SDL_CancelGPUCommandBuffer(cmd);return false;}
|
||||
if(!FlushUploads(cmd)){SDL_CancelGPUCommandBuffer(cmd);return false;}
|
||||
// Resource uploads are recorded now; on failure submit rather than cancel so they still land.
|
||||
auto* copy=SDL_BeginGPUCopyPass(cmd);if(!copy){SDL_SubmitGPUCommandBuffer(cmd);return false;}
|
||||
SDL_GPUTransferBufferLocation source{};SDL_GPUBufferRegion dest{};
|
||||
if(vb||ib){source={transfer_,0};dest={vertices_,0,vb};SDL_UploadToGPUBuffer(copy,&source,&dest,true);source.offset=vb;dest={indices_,0,ib};SDL_UploadToGPUBuffer(copy,&source,&dest,true);}
|
||||
source={instanceTransfer_,0};dest={instances_,0,instanceSize};SDL_UploadToGPUBuffer(copy,&source,&dest,true);SDL_EndGPUCopyPass(copy);
|
||||
SDL_GPUColorTargetInfo color{};color.texture=color_;color.clear_color={0,0,0,0};color.load_op=SDL_GPU_LOADOP_CLEAR;color.store_op=SDL_GPU_STOREOP_STORE;
|
||||
SDL_GPUDepthStencilTargetInfo depth{};depth.texture=depth_;depth.clear_depth=1;depth.load_op=SDL_GPU_LOADOP_CLEAR;depth.store_op=SDL_GPU_STOREOP_DONT_CARE;depth.stencil_load_op=SDL_GPU_LOADOP_DONT_CARE;depth.stencil_store_op=SDL_GPU_STOREOP_DONT_CARE;
|
||||
auto* pass=SDL_BeginGPURenderPass(cmd,&color,1,&depth);if(!pass){SDL_CancelGPUCommandBuffer(cmd);return false;}
|
||||
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));
|
||||
const std::vector<float>* pushedPalette=nullptr; // draws of one instance share a palette; push once
|
||||
|
||||
Reference in New Issue
Block a user