| 115 | } |
| 116 | |
| 117 | GPUTexture* RenderBuffers::RequestHiZ(GPUContext* context, bool fullRes, int32 mipLevels) |
| 118 | { |
| 119 | // Skip if already done in the current frame |
| 120 | const auto currentFrame = Engine::FrameCount; |
| 121 | if (LastFrameHiZ == currentFrame) |
| 122 | return HiZ; |
| 123 | if (!MultiScaler::Instance()->IsReady()) |
| 124 | return nullptr; |
| 125 | LastFrameHiZ = currentFrame; |
| 126 | |
| 127 | // Allocate or resize buffer (with full mip-chain) |
| 128 | auto format = PixelFormat::R32_Float; |
| 129 | auto width = fullRes ? _width : Math::Max(_width >> 1, 1); |
| 130 | auto height = fullRes ? _height : Math::Max(_height >> 1, 1); |
| 131 | auto desc = GPUTextureDescription::New2D(width, height, mipLevels, format, GPUTextureFlags::ShaderResource); |
| 132 | bool useCompute = false; // TODO: impl Compute Shader for downscaling depth to HiZ with a single dispatch (eg. FidelityFX Single Pass Downsampler) |
| 133 | if (useCompute) |
| 134 | desc.Flags |= GPUTextureFlags::UnorderedAccess; |
| 135 | else |
| 136 | desc.Flags |= GPUTextureFlags::RenderTarget | GPUTextureFlags::PerMipViews; |
| 137 | if (HiZ && HiZ->GetDescription() != desc) |
| 138 | { |
| 139 | RenderTargetPool::Release(HiZ); |
| 140 | HiZ = nullptr; |
| 141 | } |
| 142 | if (HiZ == nullptr) |
| 143 | { |
| 144 | HiZ = RenderTargetPool::Get(desc); |
| 145 | RENDER_TARGET_POOL_SET_NAME(HiZ, "HiZ"); |
| 146 | #if PLATFORM_WEB |
| 147 | // Hack to fix WebGPU limitation that requires to specify different sampler type manually to load 32-bit float texture |
| 148 | SetWebGPUTextureViewSampler(HiZ->View(), GPU_WEBGPU_SAMPLER_TYPE_UNFILTERABLE_FLOAT); |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | // Downscale |
| 153 | MultiScaler::Instance()->BuildHiZ(context, DepthBuffer, HiZ); |
| 154 | |
| 155 | return HiZ; |
| 156 | } |
| 157 | |
| 158 | PixelFormat RenderBuffers::GetOutputFormat() const |
| 159 | { |