Fix zminmax not being linear depth

This commit is contained in:
Magnus Norddahl 2024-09-16 01:45:44 +02:00
commit 83009db3ec
9 changed files with 54 additions and 15 deletions

View file

@ -110,7 +110,7 @@ void VkDescriptorSetManager::UpdateLevelMeshSet()
void VkDescriptorSetManager::UpdateLightTilesSet()
{
WriteDescriptors()
.AddStorageImage(LightTiles.Set.get(), 0, fb->GetBuffers()->SceneZMinMax[4].View.get(), VK_IMAGE_LAYOUT_GENERAL)
.AddStorageImage(LightTiles.Set.get(), 0, fb->GetBuffers()->SceneZMinMax[5].View.get(), VK_IMAGE_LAYOUT_GENERAL)
.AddBuffer(LightTiles.Set.get(), 1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetLevelMesh()->GetUniformsBuffer())
.AddBuffer(LightTiles.Set.get(), 2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, fb->GetBuffers()->SceneLightTiles.get())
.Execute(fb->GetDevice());
@ -122,7 +122,7 @@ void VkDescriptorSetManager::UpdateZMinMaxSet()
.AddCombinedImageSampler(ZMinMax.Set[0].get(), 0, fb->GetBuffers()->SceneDepthStencil.DepthOnlyView.get(), fb->GetSamplerManager()->ZMinMaxSampler.get(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
.Execute(fb->GetDevice());
for (int i = 1; i < 5; i++)
for (int i = 1; i < 6; i++)
{
WriteDescriptors()
.AddCombinedImageSampler(ZMinMax.Set[i].get(), 0, fb->GetBuffers()->SceneZMinMax[i - 1].View.get(), fb->GetSamplerManager()->ZMinMaxSampler.get(), VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)
@ -314,8 +314,8 @@ void VkDescriptorSetManager::CreateLightTilesPool()
void VkDescriptorSetManager::CreateZMinMaxPool()
{
ZMinMax.Pool = DescriptorPoolBuilder()
.AddPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 5)
.MaxSets(5)
.AddPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6)
.MaxSets(6)
.DebugName("VkDescriptorSetManager.ZMinMax.Pool")
.Create(fb->GetDevice());
}

View file

@ -110,7 +110,7 @@ private:
struct
{
std::unique_ptr<VulkanDescriptorPool> Pool;
std::unique_ptr<VulkanDescriptorSet> Set[5];
std::unique_ptr<VulkanDescriptorSet> Set[6];
std::unique_ptr<VulkanDescriptorSetLayout> Layout;
} ZMinMax;

View file

@ -189,7 +189,7 @@ void VkRenderPassManager::CreateZMinMaxPipeline()
ZMinMax.Layout = PipelineLayoutBuilder()
.AddSetLayout(fb->GetDescriptorSetManager()->GetZMinMaxLayout())
//.AddPushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(ZMinMaxPushConstants))
.AddPushConstantRange(VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(ZMinMaxPushConstants))
.DebugName("VkRenderPassManager.ZMinMax.Layout")
.Create(fb->GetDevice());

View file

@ -53,6 +53,14 @@ struct PushConstants
int uFogballIndex; // fog balls
};
struct ZMinMaxPushConstants
{
float LinearizeDepthA;
float LinearizeDepthB;
float InverseDepthRangeA;
float InverseDepthRangeB;
};
struct LightTilesPushConstants
{
int additiveCount;

View file

@ -305,7 +305,7 @@ void VkRenderBuffers::CreateSceneZMinMax(int width, int height)
width = (width + 63) / 64 * 64;
height = (height + 63) / 64 * 64;
for (int i = 0; i < 5; i++)
for (int i = 0; i < 6; i++)
{
width >>= 1;
height >>= 1;

View file

@ -37,7 +37,7 @@ public:
VkTextureImage SceneFog;
VkTextureImage SceneLinearDepth;
VkTextureImage SceneZMinMax[5];
VkTextureImage SceneZMinMax[6];
std::unique_ptr<VulkanBuffer> SceneLightTiles;
VkFormat PipelineDepthStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT;

View file

@ -877,6 +877,12 @@ void VkRenderState::RunZMinMaxPass()
int width = ((buffers->GetWidth() + 63) / 64 * 64) >> 1;
int height = ((buffers->GetHeight() + 63) / 64 * 64) >> 1;
ZMinMaxPushConstants pushConstants = {};
pushConstants.LinearizeDepthA = 1.0f / screen->GetZFar() - 1.0f / screen->GetZNear();
pushConstants.LinearizeDepthB = max(1.0f / screen->GetZNear(), 1.e-8f);
pushConstants.InverseDepthRangeA = 1.0f;
pushConstants.InverseDepthRangeB = 0.0f;
VkImageTransition()
.AddImage(&fb->GetBuffers()->SceneDepthStencil, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false)
.AddImage(&fb->GetBuffers()->SceneZMinMax[0], VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, true)
@ -901,10 +907,11 @@ void VkRenderState::RunZMinMaxPass()
cmdbuffer->bindPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines->GetZMinMaxPipeline0(mRenderTarget.Samples));
cmdbuffer->bindDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines->GetZMinMaxLayout(), 0, descriptors->GetZMinMaxSet(0));
cmdbuffer->pushConstants(pipelines->GetZMinMaxLayout(), VK_SHADER_STAGE_FRAGMENT_BIT, 0, (uint32_t)sizeof(ZMinMaxPushConstants), &pushConstants);
cmdbuffer->draw(6, 1, 0, 0);
cmdbuffer->endRenderPass();
for (int i = 1; i < 5; i++)
for (int i = 1; i < 6; i++)
{
VkImageTransition()
.AddImage(&fb->GetBuffers()->SceneZMinMax[i - 1], VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, false)
@ -936,7 +943,7 @@ void VkRenderState::RunZMinMaxPass()
VkImageTransition()
.AddImage(&fb->GetBuffers()->SceneDepthStencil, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, false)
.AddImage(&fb->GetBuffers()->SceneZMinMax[4], VK_IMAGE_LAYOUT_GENERAL, false)
.AddImage(&fb->GetBuffers()->SceneZMinMax[5], VK_IMAGE_LAYOUT_GENERAL, false)
.Execute(cmdbuffer);
}