refactor: optimize terrain recomputation in TerritoryRenderer

Updated the terrain recomputation logic to trigger asynchronously, improving performance by allowing rendering to continue without blocking. This change ensures that the terrain will be ready for the next frame, which may result in displaying stale terrain for one frame but enhances overall rendering efficiency.
This commit is contained in:
scamiv
2026-01-16 22:00:21 +01:00
parent 256dac36fc
commit 43aa6b8a02
@@ -425,31 +425,14 @@ export class TerritoryRenderer {
return; return;
} }
// Check if terrain needs recomputation (e.g., theme changed) // If terrain needs recomputation, trigger it asynchronously (no blocking)
// If so, compute it in the same command buffer before rendering // It will be ready for the next frame, acceptable trade-off for performance
if (this.terrainComputePass?.needsUpdate()) { if (this.terrainComputePass?.needsUpdate()) {
this.resources.uploadTerrainParams(); this.resources.uploadTerrainParams();
const computeEncoder = this.device.device.createCommandEncoder();
// Use a single encoder to ensure compute completes before render this.terrainComputePass.execute(computeEncoder, this.resources);
const encoder = this.device.device.createCommandEncoder(); this.device.device.queue.submit([computeEncoder.finish()]);
// Continue with render - may show stale terrain for one frame, but better performance
// Execute terrain compute first
this.terrainComputePass.execute(encoder, this.resources);
// Then execute render passes in the same command buffer
// The render pass will rebuild its bind group, which will now use the updated terrain texture
const textureView = this.device.context.getCurrentTexture().createView();
for (const pass of this.renderPassOrder) {
if (!pass.needsUpdate()) {
continue;
}
pass.execute(encoder, this.resources, textureView);
}
// Submit single command buffer with both compute and render
// This ensures compute completes before render reads the terrain texture
this.device.device.queue.submit([encoder.finish()]);
return;
} }
const encoder = this.device.device.createCommandEncoder(); const encoder = this.device.device.createCommandEncoder();