Skip to content

Commit

Permalink
Increase the precision of shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
godlikepanos committed Sep 5, 2024
1 parent b00ae16 commit d95b491
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
24 changes: 24 additions & 0 deletions AnKi/Gr/Vulkan/VkGpuMemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,28 @@ void GpuMemoryManager::updateStats() const
g_deviceMemoryAllocationCountStatVar.increment(m_dedicatedAllocationCount.load());
}

void GpuMemoryManager::getImageMemoryRequirements(VkImage image, VkMemoryDedicatedRequirementsKHR& dedicatedRequirements,
VkMemoryRequirements2& requirements)
{
dedicatedRequirements = {};
dedicatedRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR;

requirements = {};
requirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
requirements.pNext = &dedicatedRequirements;

VkImageMemoryRequirementsInfo2 imageRequirements = {};
imageRequirements.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
imageRequirements.image = image;

vkGetImageMemoryRequirements2(getVkDevice(), &imageRequirements, &requirements);

if(requirements.memoryRequirements.size > kClasses.getBack().m_chunkSize)
{
// Allocation to big, force a dedicated allocation
dedicatedRequirements.prefersDedicatedAllocation = true;
dedicatedRequirements.requiresDedicatedAllocation = true;
}
}

} // end namespace anki
2 changes: 2 additions & 0 deletions AnKi/Gr/Vulkan/VkGpuMemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ class GpuMemoryManager : public MakeSingleton<GpuMemoryManager>

GpuMemoryManager& operator=(const GpuMemoryManager&) = delete; // Non-copyable

void getImageMemoryRequirements(VkImage image, VkMemoryDedicatedRequirementsKHR& dedicatedRequirements, VkMemoryRequirements2& requirements);

/// Allocate memory.
void allocateMemory(U32 memTypeIdx, PtrSize size, U32 alignment, GpuMemoryHandle& handle);

Expand Down
15 changes: 3 additions & 12 deletions AnKi/Gr/Vulkan/VkTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,9 @@ Error TextureImpl::initImage(const TextureInitInfo& init)

// Allocate memory
//
VkMemoryDedicatedRequirementsKHR dedicatedRequirements = {};
dedicatedRequirements.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR;

VkMemoryRequirements2 requirements = {};
requirements.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2;
requirements.pNext = &dedicatedRequirements;

VkImageMemoryRequirementsInfo2 imageRequirementsInfo = {};
imageRequirementsInfo.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2;
imageRequirementsInfo.image = m_imageHandle;

vkGetImageMemoryRequirements2(getVkDevice(), &imageRequirementsInfo, &requirements);
VkMemoryDedicatedRequirementsKHR dedicatedRequirements;
VkMemoryRequirements2 requirements;
GpuMemoryManager::getSingleton().getImageMemoryRequirements(m_imageHandle, dedicatedRequirements, requirements);

U32 memIdx = GpuMemoryManager::getSingleton().findMemoryType(requirements.memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
Expand Down
2 changes: 1 addition & 1 deletion AnKi/Renderer/ShadowMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Error ShadowMapping::initInternal()
// RT
const TextureUsageBit usage = TextureUsageBit::kSrvPixel | TextureUsageBit::kSrvCompute | TextureUsageBit::kAllRtvDsv;
TextureInitInfo texinit = getRenderer().create2DRenderTargetInitInfo(
m_tileResolution * m_tileCountBothAxis, m_tileResolution * m_tileCountBothAxis, Format::kD16_Unorm, usage, "ShadowAtlas");
m_tileResolution * m_tileCountBothAxis, m_tileResolution * m_tileCountBothAxis, Format::kD32_Sfloat, usage, "ShadowAtlas");
ClearValue clearVal;
clearVal.m_colorf[0] = 1.0f;
m_atlasTex = getRenderer().createAndClearRenderTarget(texinit, TextureUsageBit::kSrvPixel, clearVal);
Expand Down

0 comments on commit d95b491

Please sign in to comment.