mirror of
https://github.com/doitsujin/dxvk.git
synced 2025-03-14 22:29:15 +01:00
[dxvk] Implement sparse image creation
This commit is contained in:
parent
f7c255de2a
commit
2d124b811b
@ -5,7 +5,7 @@
|
|||||||
namespace dxvk {
|
namespace dxvk {
|
||||||
|
|
||||||
DxvkImage::DxvkImage(
|
DxvkImage::DxvkImage(
|
||||||
const DxvkDevice* device,
|
DxvkDevice* device,
|
||||||
const DxvkImageCreateInfo& createInfo,
|
const DxvkImageCreateInfo& createInfo,
|
||||||
DxvkMemoryAllocator& memAlloc,
|
DxvkMemoryAllocator& memAlloc,
|
||||||
VkMemoryPropertyFlags memFlags)
|
VkMemoryPropertyFlags memFlags)
|
||||||
@ -47,6 +47,7 @@ namespace dxvk {
|
|||||||
"DxvkImage: Failed to create image:",
|
"DxvkImage: Failed to create image:",
|
||||||
"\n Type: ", info.imageType,
|
"\n Type: ", info.imageType,
|
||||||
"\n Format: ", info.format,
|
"\n Format: ", info.format,
|
||||||
|
"\n Flags: ", info.flags,
|
||||||
"\n Extent: ", "(", info.extent.width,
|
"\n Extent: ", "(", info.extent.width,
|
||||||
",", info.extent.height,
|
",", info.extent.height,
|
||||||
",", info.extent.depth, ")",
|
",", info.extent.depth, ")",
|
||||||
@ -56,79 +57,107 @@ namespace dxvk {
|
|||||||
"\n Usage: ", info.usage,
|
"\n Usage: ", info.usage,
|
||||||
"\n Tiling: ", info.tiling));
|
"\n Tiling: ", info.tiling));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get memory requirements for the image and ask driver
|
|
||||||
// whether we need to use a dedicated allocation.
|
|
||||||
DxvkMemoryRequirements memoryRequirements = { };
|
|
||||||
memoryRequirements.dedicated = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS };
|
|
||||||
memoryRequirements.core = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, &memoryRequirements.dedicated };
|
|
||||||
|
|
||||||
VkImageMemoryRequirementsInfo2 memoryRequirementInfo = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2 };
|
VkImageMemoryRequirementsInfo2 memoryRequirementInfo = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2 };
|
||||||
memoryRequirementInfo.image = m_image.image;
|
memoryRequirementInfo.image = m_image.image;
|
||||||
|
|
||||||
m_vkd->vkGetImageMemoryRequirements2(m_vkd->device(),
|
if (!(info.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)) {
|
||||||
&memoryRequirementInfo, &memoryRequirements.core);
|
// Get memory requirements for the image and ask driver
|
||||||
|
// whether we need to use a dedicated allocation.
|
||||||
|
DxvkMemoryRequirements memoryRequirements = { };
|
||||||
|
memoryRequirements.dedicated = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS };
|
||||||
|
memoryRequirements.core = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2, &memoryRequirements.dedicated };
|
||||||
|
|
||||||
// Fill in desired memory properties
|
m_vkd->vkGetImageMemoryRequirements2(m_vkd->device(),
|
||||||
DxvkMemoryProperties memoryProperties = { };
|
&memoryRequirementInfo, &memoryRequirements.core);
|
||||||
memoryProperties.flags = m_memFlags;
|
|
||||||
|
|
||||||
if (m_shared) {
|
// Fill in desired memory properties
|
||||||
memoryRequirements.dedicated.prefersDedicatedAllocation = VK_TRUE;
|
DxvkMemoryProperties memoryProperties = { };
|
||||||
memoryRequirements.dedicated.requiresDedicatedAllocation = VK_TRUE;
|
memoryProperties.flags = m_memFlags;
|
||||||
|
|
||||||
if (createInfo.sharing.mode == DxvkSharedHandleMode::Export) {
|
if (m_shared) {
|
||||||
memoryProperties.sharedExport = { VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO };
|
memoryRequirements.dedicated.prefersDedicatedAllocation = VK_TRUE;
|
||||||
memoryProperties.sharedExport.handleTypes = createInfo.sharing.type;
|
memoryRequirements.dedicated.requiresDedicatedAllocation = VK_TRUE;
|
||||||
|
|
||||||
|
if (createInfo.sharing.mode == DxvkSharedHandleMode::Export) {
|
||||||
|
memoryProperties.sharedExport = { VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO };
|
||||||
|
memoryProperties.sharedExport.handleTypes = createInfo.sharing.type;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (createInfo.sharing.mode == DxvkSharedHandleMode::Import) {
|
||||||
|
memoryProperties.sharedImportWin32 = { VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
|
||||||
|
memoryProperties.sharedImportWin32.handleType = createInfo.sharing.type;
|
||||||
|
memoryProperties.sharedImportWin32.handle = createInfo.sharing.handle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (createInfo.sharing.mode == DxvkSharedHandleMode::Import) {
|
if (memoryRequirements.dedicated.prefersDedicatedAllocation) {
|
||||||
memoryProperties.sharedImportWin32 = { VK_STRUCTURE_TYPE_IMPORT_MEMORY_WIN32_HANDLE_INFO_KHR };
|
memoryProperties.dedicated = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO };
|
||||||
memoryProperties.sharedImportWin32.handleType = createInfo.sharing.type;
|
memoryProperties.dedicated.image = m_image.image;
|
||||||
memoryProperties.sharedImportWin32.handle = createInfo.sharing.handle;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (memoryRequirements.dedicated.prefersDedicatedAllocation) {
|
// If there's a chance we won't create the image with a dedicated
|
||||||
memoryProperties.dedicated = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO };
|
// allocation, enforce strict alignment for tiled images to not
|
||||||
memoryProperties.dedicated.image = m_image.image;
|
// violate the bufferImageGranularity requirement on some GPUs.
|
||||||
}
|
if (info.tiling != VK_IMAGE_TILING_LINEAR && !memoryRequirements.dedicated.requiresDedicatedAllocation) {
|
||||||
|
VkDeviceSize granularity = memAlloc.bufferImageGranularity();
|
||||||
|
|
||||||
// If there's a chance we won't create the image with a dedicated
|
auto& core = memoryRequirements.core.memoryRequirements;
|
||||||
// allocation, enforce strict alignment for tiled images to not
|
core.size = align(core.size, granularity);
|
||||||
// violate the bufferImageGranularity requirement on some GPUs.
|
core.alignment = align(core.alignment, granularity);
|
||||||
if (info.tiling != VK_IMAGE_TILING_LINEAR && !memoryRequirements.dedicated.requiresDedicatedAllocation) {
|
}
|
||||||
VkDeviceSize granularity = memAlloc.bufferImageGranularity();
|
|
||||||
|
|
||||||
auto& core = memoryRequirements.core.memoryRequirements;
|
// Use high memory priority for GPU-writable resources
|
||||||
core.size = align(core.size, granularity);
|
bool isGpuWritable = (m_info.access & (
|
||||||
core.alignment = align(core.alignment, granularity);
|
VK_ACCESS_SHADER_WRITE_BIT |
|
||||||
}
|
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
|
||||||
|
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
|
||||||
|
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
||||||
|
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT)) != 0;
|
||||||
|
|
||||||
// Use high memory priority for GPU-writable resources
|
DxvkMemoryFlags hints(DxvkMemoryFlag::GpuReadable);
|
||||||
bool isGpuWritable = (m_info.access & (
|
|
||||||
VK_ACCESS_SHADER_WRITE_BIT |
|
|
||||||
VK_ACCESS_COLOR_ATTACHMENT_READ_BIT |
|
|
||||||
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
|
|
||||||
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
|
|
||||||
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT)) != 0;
|
|
||||||
|
|
||||||
DxvkMemoryFlags hints(DxvkMemoryFlag::GpuReadable);
|
|
||||||
|
|
||||||
if (isGpuWritable)
|
if (isGpuWritable)
|
||||||
hints.set(DxvkMemoryFlag::GpuWritable);
|
hints.set(DxvkMemoryFlag::GpuWritable);
|
||||||
|
|
||||||
m_image.memory = memAlloc.alloc(memoryRequirements, memoryProperties, hints);
|
m_image.memory = memAlloc.alloc(memoryRequirements, memoryProperties, hints);
|
||||||
|
|
||||||
// Try to bind the allocated memory slice to the image
|
// Try to bind the allocated memory slice to the image
|
||||||
if (m_vkd->vkBindImageMemory(m_vkd->device(), m_image.image,
|
if (m_vkd->vkBindImageMemory(m_vkd->device(), m_image.image,
|
||||||
m_image.memory.memory(), m_image.memory.offset()) != VK_SUCCESS)
|
m_image.memory.memory(), m_image.memory.offset()) != VK_SUCCESS)
|
||||||
throw DxvkError("DxvkImage::DxvkImage: Failed to bind device memory");
|
throw DxvkError("DxvkImage::DxvkImage: Failed to bind device memory");
|
||||||
|
} else {
|
||||||
|
// Initialize sparse info. We do not immediately bind the metadata
|
||||||
|
// aspects of the image here, the caller needs to explicitly do that.
|
||||||
|
m_sparsePageTable = DxvkSparsePageTable(device, this);
|
||||||
|
|
||||||
|
// Allocate memory for sparse metadata if necessary
|
||||||
|
auto properties = m_sparsePageTable.getProperties();
|
||||||
|
|
||||||
|
if (properties.metadataPageCount) {
|
||||||
|
DxvkMemoryRequirements memoryRequirements = { };
|
||||||
|
memoryRequirements.core = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2 };
|
||||||
|
|
||||||
|
m_vkd->vkGetImageMemoryRequirements2(m_vkd->device(),
|
||||||
|
&memoryRequirementInfo, &memoryRequirements.core);
|
||||||
|
|
||||||
|
DxvkMemoryProperties memoryProperties = { };
|
||||||
|
memoryProperties.flags = m_memFlags;
|
||||||
|
|
||||||
|
// Set size and alignment to match the metadata requirements
|
||||||
|
auto& core = memoryRequirements.core.memoryRequirements;
|
||||||
|
core.size = SparseMemoryPageSize * properties.metadataPageCount;
|
||||||
|
core.alignment = SparseMemoryPageSize;
|
||||||
|
|
||||||
|
m_image.memory = memAlloc.alloc(memoryRequirements,
|
||||||
|
memoryProperties, DxvkMemoryFlag::GpuReadable);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DxvkImage::DxvkImage(
|
DxvkImage::DxvkImage(
|
||||||
const DxvkDevice* device,
|
DxvkDevice* device,
|
||||||
const DxvkImageCreateInfo& info,
|
const DxvkImageCreateInfo& info,
|
||||||
VkImage image)
|
VkImage image)
|
||||||
: m_vkd(device->vkd()), m_device(device), m_info(info), m_image({ image }) {
|
: m_vkd(device->vkd()), m_device(device), m_info(info), m_image({ image }) {
|
||||||
@ -143,12 +172,13 @@ namespace dxvk {
|
|||||||
DxvkImage::~DxvkImage() {
|
DxvkImage::~DxvkImage() {
|
||||||
// This is a bit of a hack to determine whether
|
// This is a bit of a hack to determine whether
|
||||||
// the image is implementation-handled or not
|
// the image is implementation-handled or not
|
||||||
if (m_image.memory.memory() != VK_NULL_HANDLE)
|
if ((m_image.memory.memory())
|
||||||
|
|| (m_info.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT))
|
||||||
m_vkd->vkDestroyImage(m_vkd->device(), m_image.image, nullptr);
|
m_vkd->vkDestroyImage(m_vkd->device(), m_image.image, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool DxvkImage::canShareImage(const VkImageCreateInfo& createInfo, const DxvkSharedHandleInfo& sharingInfo) const {
|
bool DxvkImage::canShareImage(const VkImageCreateInfo& createInfo, const DxvkSharedHandleInfo& sharingInfo) const {
|
||||||
if (sharingInfo.mode == DxvkSharedHandleMode::None)
|
if (sharingInfo.mode == DxvkSharedHandleMode::None)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -157,6 +187,9 @@ namespace dxvk {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (createInfo.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT)
|
||||||
|
return false;
|
||||||
|
|
||||||
VkPhysicalDeviceExternalImageFormatInfo externalImageFormatInfo = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO };
|
VkPhysicalDeviceExternalImageFormatInfo externalImageFormatInfo = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO };
|
||||||
externalImageFormatInfo.handleType = sharingInfo.type;
|
externalImageFormatInfo.handleType = sharingInfo.type;
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "dxvk_format.h"
|
#include "dxvk_format.h"
|
||||||
#include "dxvk_memory.h"
|
#include "dxvk_memory.h"
|
||||||
#include "dxvk_resource.h"
|
#include "dxvk_resource.h"
|
||||||
|
#include "dxvk_sparse.h"
|
||||||
#include "dxvk_util.h"
|
#include "dxvk_util.h"
|
||||||
|
|
||||||
namespace dxvk {
|
namespace dxvk {
|
||||||
@ -127,7 +128,7 @@ namespace dxvk {
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
DxvkImage(
|
DxvkImage(
|
||||||
const DxvkDevice* device,
|
DxvkDevice* device,
|
||||||
const DxvkImageCreateInfo& createInfo,
|
const DxvkImageCreateInfo& createInfo,
|
||||||
DxvkMemoryAllocator& memAlloc,
|
DxvkMemoryAllocator& memAlloc,
|
||||||
VkMemoryPropertyFlags memFlags);
|
VkMemoryPropertyFlags memFlags);
|
||||||
@ -141,7 +142,7 @@ namespace dxvk {
|
|||||||
* otherwise some image operations may fail.
|
* otherwise some image operations may fail.
|
||||||
*/
|
*/
|
||||||
DxvkImage(
|
DxvkImage(
|
||||||
const DxvkDevice* device,
|
DxvkDevice* device,
|
||||||
const DxvkImageCreateInfo& info,
|
const DxvkImageCreateInfo& info,
|
||||||
VkImage image);
|
VkImage image);
|
||||||
|
|
||||||
@ -323,6 +324,16 @@ namespace dxvk {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Queries sparse page table
|
||||||
|
* \returns Page table, or \c nullptr for a non-sparse resource
|
||||||
|
*/
|
||||||
|
DxvkSparsePageTable* getSparsePageTable() {
|
||||||
|
return m_info.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT
|
||||||
|
? &m_sparsePageTable
|
||||||
|
: nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Create a new shared handle to dedicated memory backing the image
|
* \brief Create a new shared handle to dedicated memory backing the image
|
||||||
* \returns The shared handle with the type given by DxvkSharedHandleInfo::type
|
* \returns The shared handle with the type given by DxvkSharedHandleInfo::type
|
||||||
@ -336,6 +347,8 @@ namespace dxvk {
|
|||||||
DxvkImageCreateInfo m_info;
|
DxvkImageCreateInfo m_info;
|
||||||
VkMemoryPropertyFlags m_memFlags;
|
VkMemoryPropertyFlags m_memFlags;
|
||||||
DxvkPhysicalImage m_image;
|
DxvkPhysicalImage m_image;
|
||||||
|
DxvkSparsePageTable m_sparsePageTable;
|
||||||
|
|
||||||
bool m_shared = false;
|
bool m_shared = false;
|
||||||
|
|
||||||
small_vector<VkFormat, 4> m_viewFormats;
|
small_vector<VkFormat, 4> m_viewFormats;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user