Skip to content

Commit 910789e

Browse files
Dynamically fetch instance level vulkan functions
1 parent 149cf26 commit 910789e

3 files changed

Lines changed: 50 additions & 26 deletions

File tree

src/VK/UpscaleContext_FSR2_API.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ void UpscaleContext_FSR2_API::OnCreateWindowSizeDependentResources(
106106
UpscaleContext::OnCreateWindowSizeDependentResources(input, output, renderWidth, renderHeight, displayWidth, displayHeight, hdr);
107107

108108
// Setup VK interface.
109-
const size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeVK(m_pDevice->GetPhysicalDevice());
109+
const size_t scratchBufferSize = ffxFsr2GetScratchMemorySizeVK(m_pDevice->GetPhysicalDevice(), vkEnumerateDeviceExtensionProperties);
110110
void* scratchBuffer = malloc(scratchBufferSize);
111-
FfxErrorCode errorCode = ffxFsr2GetInterfaceVK(&initializationParameters.callbacks, scratchBuffer, scratchBufferSize, m_pDevice->GetPhysicalDevice(), vkGetDeviceProcAddr);
111+
FfxErrorCode errorCode = ffxFsr2GetInterfaceVK(&initializationParameters.callbacks, scratchBuffer, scratchBufferSize, m_pDevice->GetInstance(), m_pDevice->GetPhysicalDevice(), vkGetInstanceProcAddr);
112112
FFX_ASSERT(errorCode == FFX_OK);
113113

114114
initializationParameters.device = ffxGetDeviceVK(m_pDevice->GetDevice());

src/ffx-fsr2-api/vk/ffx_fsr2_vk.cpp

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ typedef struct BackendContext_VK {
9090
} PipelineLayout;
9191

9292
typedef struct VKFunctionTable
93-
{
93+
{
94+
// instance functions
95+
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 0;
96+
PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties = 0;
97+
PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties = 0;
98+
PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties = 0;
99+
PFN_vkGetPhysicalDeviceProperties2 vkGetPhysicalDeviceProperties2 = 0;
100+
PFN_vkGetPhysicalDeviceFeatures2 vkGetPhysicalDeviceFeatures2 = 0;
101+
102+
// device functions
94103
PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr = 0;
95104
PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT = 0;
96105
PFN_vkCreateDescriptorPool vkCreateDescriptorPool = 0;
@@ -132,6 +141,7 @@ typedef struct BackendContext_VK {
132141
PFN_vkCmdClearColorImage vkCmdClearColorImage = 0;
133142
} VkFunctionTable;
134143

144+
VkInstance instance = nullptr;
135145
VkPhysicalDevice physicalDevice = nullptr;
136146
VkDevice device = nullptr;
137147
VkFunctionTable vkFunctionTable = {};
@@ -170,7 +180,7 @@ typedef struct BackendContext_VK {
170180

171181
} BackendContext_VK;
172182

173-
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice)
183+
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice, PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties)
174184
{
175185
uint32_t numExtensions = 0;
176186

@@ -184,8 +194,9 @@ FfxErrorCode ffxFsr2GetInterfaceVK(
184194
FfxFsr2Interface* outInterface,
185195
void* scratchBuffer,
186196
size_t scratchBufferSize,
197+
VkInstance instance,
187198
VkPhysicalDevice physicalDevice,
188-
PFN_vkGetDeviceProcAddr getDeviceProcAddr)
199+
PFN_vkGetInstanceProcAddr getInstanceProcAddr)
189200
{
190201
FFX_RETURN_ON_ERROR(
191202
outInterface,
@@ -194,7 +205,7 @@ FfxErrorCode ffxFsr2GetInterfaceVK(
194205
scratchBuffer,
195206
FFX_ERROR_INVALID_POINTER);
196207
FFX_RETURN_ON_ERROR(
197-
scratchBufferSize >= ffxFsr2GetScratchMemorySizeVK(physicalDevice),
208+
scratchBufferSize >= ffxFsr2GetScratchMemorySizeVK(physicalDevice, (PFN_vkEnumerateDeviceExtensionProperties)getInstanceProcAddr(instance, "vkEnumerateDeviceExtensionProperties")),
198209
FFX_ERROR_INSUFFICIENT_MEMORY);
199210

200211
outInterface->fpGetDeviceCapabilities = GetDeviceCapabilitiesVK;
@@ -214,15 +225,25 @@ FfxErrorCode ffxFsr2GetInterfaceVK(
214225

215226
BackendContext_VK* context = (BackendContext_VK*)scratchBuffer;
216227

228+
context->instance = instance;
217229
context->physicalDevice = physicalDevice;
218-
context->vkFunctionTable.vkGetDeviceProcAddr = getDeviceProcAddr;
230+
context->vkFunctionTable.vkGetInstanceProcAddr = getInstanceProcAddr;
219231

220232
return FFX_OK;
221233
}
222234

223-
void loadVKFunctions(BackendContext_VK* backendContext, PFN_vkGetDeviceProcAddr getDeviceProcAddr)
235+
void loadVKFunctions(BackendContext_VK* backendContext, PFN_vkGetInstanceProcAddr getInstanceProcAddr)
224236
{
225237
FFX_ASSERT(NULL != backendContext);
238+
239+
backendContext->vkFunctionTable.vkGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)getInstanceProcAddr(backendContext->instance, "vkGetDeviceProcAddr");
240+
backendContext->vkFunctionTable.vkEnumerateDeviceExtensionProperties = (PFN_vkEnumerateDeviceExtensionProperties)getInstanceProcAddr(backendContext->instance, "vkEnumerateDeviceExtensionProperties");
241+
backendContext->vkFunctionTable.vkGetPhysicalDeviceMemoryProperties = (PFN_vkGetPhysicalDeviceMemoryProperties)getInstanceProcAddr(backendContext->instance, "vkGetPhysicalDeviceMemoryProperties");
242+
backendContext->vkFunctionTable.vkGetPhysicalDeviceProperties = (PFN_vkGetPhysicalDeviceProperties)getInstanceProcAddr(backendContext->instance, "vkGetPhysicalDeviceProperties");
243+
backendContext->vkFunctionTable.vkGetPhysicalDeviceProperties2 = (PFN_vkGetPhysicalDeviceProperties2)getInstanceProcAddr(backendContext->instance, "vkGetPhysicalDeviceProperties2");
244+
backendContext->vkFunctionTable.vkGetPhysicalDeviceFeatures2 = (PFN_vkGetPhysicalDeviceFeatures2)getInstanceProcAddr(backendContext->instance, "vkGetPhysicalDeviceFeatures2");
245+
246+
PFN_vkGetDeviceProcAddr getDeviceProcAddr = backendContext->vkFunctionTable.vkGetDeviceProcAddr;
226247

227248
backendContext->vkFunctionTable.vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)getDeviceProcAddr(backendContext->device, "vkSetDebugUtilsObjectNameEXT");
228249
backendContext->vkFunctionTable.vkFlushMappedMemoryRanges = (PFN_vkFlushMappedMemoryRanges)getDeviceProcAddr(backendContext->device, "vkFlushMappedMemoryRanges");
@@ -444,13 +465,10 @@ FfxSurfaceFormat ffxGetSurfaceFormatVK(VkFormat fmt)
444465
}
445466
}
446467

447-
uint32_t findMemoryTypeIndex(VkPhysicalDevice physicalDevice, VkMemoryRequirements memRequirements, VkMemoryPropertyFlags requestedProperties, VkMemoryPropertyFlags& outProperties)
468+
uint32_t findMemoryTypeIndex(VkPhysicalDevice physicalDevice, VkMemoryRequirements memRequirements, VkMemoryPropertyFlags requestedProperties, VkMemoryPropertyFlags& outProperties, VkPhysicalDeviceMemoryProperties &memProperties)
448469
{
449470
FFX_ASSERT(NULL != physicalDevice);
450471

451-
VkPhysicalDeviceMemoryProperties memProperties;
452-
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
453-
454472
uint32_t bestCandidate = UINT32_MAX;
455473

456474
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
@@ -736,7 +754,7 @@ FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDevi
736754
VkPhysicalDeviceProperties2 deviceProperties2 = {};
737755
deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
738756
deviceProperties2.pNext = &subgroupSizeControlProperties;
739-
vkGetPhysicalDeviceProperties2(backendContext->physicalDevice, &deviceProperties2);
757+
backendContext->vkFunctionTable.vkGetPhysicalDeviceProperties2(backendContext->physicalDevice, &deviceProperties2);
740758

741759
// NOTE: It's important to check requiredSubgroupSizeStages flags (and it's required by the spec).
742760
// As of August 2022, AMD's Vulkan drivers do not support subgroup size selection through Vulkan API
@@ -756,8 +774,7 @@ FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDevi
756774
VkPhysicalDeviceFeatures2 physicalDeviceFeatures2 = {};
757775
physicalDeviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
758776
physicalDeviceFeatures2.pNext = &shaderFloat18Int8Features;
759-
760-
vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
777+
backendContext->vkFunctionTable.vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
761778

762779
deviceCapabilities->fp16Supported = (bool)shaderFloat18Int8Features.shaderFloat16;
763780
}
@@ -770,8 +787,7 @@ FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDevi
770787
VkPhysicalDeviceFeatures2 physicalDeviceFeatures2 = {};
771788
physicalDeviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
772789
physicalDeviceFeatures2.pNext = &accelerationStructureFeatures;
773-
774-
vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
790+
backendContext->vkFunctionTable.vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
775791

776792
deviceCapabilities->raytracingSupported = (bool)accelerationStructureFeatures.accelerationStructure;
777793
}
@@ -805,12 +821,13 @@ FfxErrorCode CreateBackendContextVK(FfxFsr2Interface* backendInterface, FfxDevic
805821
backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1;
806822

807823
// load vulkan functions
808-
loadVKFunctions(backendContext, backendContext->vkFunctionTable.vkGetDeviceProcAddr);
824+
loadVKFunctions(backendContext, backendContext->vkFunctionTable.vkGetInstanceProcAddr);
809825

810826
// enumerate all the device extensions
811827
backendContext->numDeviceExtensions = 0;
812-
vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, nullptr);
813-
vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, backendContext->extensionProperties);
828+
829+
backendContext->vkFunctionTable.vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, nullptr);
830+
backendContext->vkFunctionTable.vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, backendContext->extensionProperties);
814831

815832
// create descriptor pool
816833
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = {};
@@ -907,14 +924,17 @@ FfxErrorCode CreateBackendContextVK(FfxFsr2Interface* backendInterface, FfxDevic
907924

908925
VkMemoryPropertyFlags requiredMemoryProperties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
909926

927+
VkPhysicalDeviceMemoryProperties memProperties;
928+
backendContext->vkFunctionTable.vkGetPhysicalDeviceMemoryProperties(backendContext->physicalDevice, &memProperties);
929+
910930
VkMemoryAllocateInfo allocInfo{};
911931
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
912932
allocInfo.allocationSize = FSR2_UBO_MEMORY_BLOCK_SIZE;
913-
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties);
933+
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties, memProperties);
914934

915935
if (allocInfo.memoryTypeIndex == UINT32_MAX) {
916936
requiredMemoryProperties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
917-
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties);
937+
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties, memProperties);
918938

919939
if (allocInfo.memoryTypeIndex == UINT32_MAX) {
920940
return FFX_ERROR_BACKEND_API_ERROR;
@@ -1107,10 +1127,13 @@ FfxErrorCode CreateResourceVK(
11071127
else
11081128
requiredMemoryProperties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
11091129

1130+
VkPhysicalDeviceMemoryProperties memProperties;
1131+
backendContext->vkFunctionTable.vkGetPhysicalDeviceMemoryProperties(backendContext->physicalDevice, &memProperties);
1132+
11101133
VkMemoryAllocateInfo allocInfo{};
11111134
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
11121135
allocInfo.allocationSize = memRequirements.size;
1113-
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, res->memoryProperties);
1136+
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, res->memoryProperties, memProperties);
11141137

11151138
if (allocInfo.memoryTypeIndex == UINT32_MAX) {
11161139
return FFX_ERROR_BACKEND_API_ERROR;
@@ -1298,7 +1321,7 @@ FfxErrorCode CreatePipelineVK(FfxFsr2Interface* backendInterface, FfxFsr2Pass pa
12981321
if (pass == FFX_FSR2_PASS_ACCUMULATE || pass == FFX_FSR2_PASS_ACCUMULATE_SHARPEN)
12991322
{
13001323
VkPhysicalDeviceProperties physicalDeviceProperties = {};
1301-
vkGetPhysicalDeviceProperties(backendContext->physicalDevice, &physicalDeviceProperties);
1324+
backendContext->vkFunctionTable.vkGetPhysicalDeviceProperties(backendContext->physicalDevice, &physicalDeviceProperties);
13021325

13031326
// Workaround: Disable FP16 path for the accumulate pass on NVIDIA due to reduced occupancy and high VRAM throughput.
13041327
if (physicalDeviceProperties.vendorID == 0x10DE)

src/ffx-fsr2-api/vk/ffx_fsr2_vk.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C" {
3434
///
3535
/// @returns
3636
/// The size (in bytes) of the required scratch memory buffer for the VK backend.
37-
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice);
37+
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice, PFN_vkEnumerateDeviceExtensionProperties vkEnumerateDeviceExtensionProperties);
3838

3939
/// Populate an interface with pointers for the VK backend.
4040
///
@@ -55,8 +55,9 @@ extern "C" {
5555
FfxFsr2Interface* outInterface,
5656
void* scratchBuffer,
5757
size_t scratchBufferSize,
58+
VkInstance instance,
5859
VkPhysicalDevice physicalDevice,
59-
PFN_vkGetDeviceProcAddr getDeviceProcAddr);
60+
PFN_vkGetInstanceProcAddr getInstanceProcAddr);
6061

6162
/// Create a <c><i>FfxFsr2Device</i></c> from a <c><i>VkDevice</i></c>.
6263
///

0 commit comments

Comments
 (0)