Generate mip chains for GPU scene textures

Textures were created with one level while the sampler requested linear
mip filtering (and max_lod defaulted to 0). Allocate the full chain,
generate it after upload, and let the sampler use it, so minified
terrain and props stop shimmering and read less bandwidth. Budget
accounting includes the extra third.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 16:20:29 +09:00
co-authored by Claude Opus 5.5
parent c85f9d815d
commit d5333745c5
+7 -2
View File
@@ -68,7 +68,10 @@ SDL_GPUTexture* HardwareScene::Texture(const std::string& key,int width,int heig
// 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.
SDL_GPUTextureCreateInfo ti{};ti.type=SDL_GPU_TEXTURETYPE_2D;ti.format=SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM;
ti.usage=SDL_GPU_TEXTUREUSAGE_SAMPLER;ti.width=width;ti.height=height;ti.layer_count_or_depth=1;ti.num_levels=1;
// 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);
@@ -85,9 +88,10 @@ SDL_GPUTexture* HardwareScene::Texture(const std::string& key,int width,int heig
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 size_t imageBytes=size_t(width)*height*4;
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;
}
bool HardwareScene::Initialize(SDL_Renderer* renderer,int width,int height) {
@@ -108,6 +112,7 @@ bool HardwareScene::Initialize(SDL_Renderer* renderer,int width,int height) {
SDL_GPUSamplerCreateInfo si{};
si.min_filter=si.mag_filter=SDL_GPU_FILTER_LINEAR;
si.mipmap_mode=SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
si.max_lod=1000.f; // zero-initialized max_lod would clamp sampling to level 0
si.address_mode_u=si.address_mode_v=si.address_mode_w=SDL_GPU_SAMPLERADDRESSMODE_REPEAT;
sampler_=SDL_CreateGPUSampler(device_,&si);
const Uint32 white=0xffffffff;