Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vulkan: Add external-memory-host support #1803

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions core/rend/vulkan/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ static inline u32 findMemoryType(vk::PhysicalDeviceMemoryProperties const& memor
return typeIndex;
}

static inline void addPointerToChain(void* head, const void* ptr)
{
vk::BaseInStructure* prevInStructure = static_cast<vk::BaseInStructure*>(head);
while (prevInStructure->pNext)
{
// Structure already in chain
if (prevInStructure->pNext == ptr)
{
return;
}
prevInStructure = const_cast<vk::BaseInStructure*>(prevInStructure->pNext);
}

// Add structure to end
prevInStructure->pNext = static_cast<const vk::BaseInStructure*>(ptr);
}

static const char GouraudSource[] = R"(
#if pp_Gouraud == 0
#define INTERPOLATION flat
Expand Down
35 changes: 32 additions & 3 deletions core/rend/vulkan/vulkan_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "vulkan_renderer.h"
#include "imgui.h"
#include "imgui_impl_vulkan.h"
#include "stdclass.h"
#include "ui/gui.h"
#ifdef USE_SDL
#include <sdl/sdl.h>
Expand Down Expand Up @@ -465,17 +466,19 @@ bool VulkanContext::InitDevice()
{
// Enable VK_EXT_provoking_vertex if available
provokingVertexSupported = tryAddDeviceExtension(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);

// Enable VK_EXT_external_memory_host if available
externalMemoryHostSupported = tryAddDeviceExtension(VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME);
}

// Get device features

vk::PhysicalDeviceFeatures2 featuresChain{};
vk::PhysicalDeviceFeatures& features = featuresChain.features;

vk::PhysicalDeviceProvokingVertexFeaturesEXT provokingVertexFeatures{};
if (provokingVertexSupported)
{
featuresChain.pNext = &provokingVertexFeatures;
addPointerToChain(&featuresChain, &provokingVertexFeatures);
}

// Get the physical device's features
Expand Down Expand Up @@ -574,7 +577,33 @@ bool VulkanContext::InitDevice()
quadRotatePipeline = std::make_unique<QuadPipeline>(true, true);
quadRotateDrawer = std::make_unique<QuadDrawer>();

vk::PhysicalDeviceProperties props = physicalDevice.getProperties();

vk::PhysicalDeviceProperties2 properties2;
vk::PhysicalDeviceProperties& props = properties2.properties;

vk::PhysicalDeviceExternalMemoryHostPropertiesEXT externalMemoryHostProperties{};
if (externalMemoryHostSupported)
{
addPointerToChain(&properties2, &externalMemoryHostProperties);
}

if (getPhysicalDeviceProperties2Supported && properties2.pNext)
{
physicalDevice.getProperties2(&properties2);
}
else
{
props = physicalDevice.getProperties();
}

if (externalMemoryHostSupported)
{
// Only allow usage of VK_EXT_external_memory_host if the imported alignment
// is the same as the system's page size(any pointer can be imported)
externalMemoryHostSupported &= (externalMemoryHostProperties.minImportedHostPointerAlignment == PAGE_SIZE);
}


driverName = (const char *)props.deviceName;
#ifdef __APPLE__
driverVersion = std::to_string(VK_API_VERSION_MAJOR(props.apiVersion)) + "."
Expand Down
2 changes: 2 additions & 0 deletions core/rend/vulkan/vulkan_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class VulkanContext : public GraphicsContext, public FlightManager
}
bool hasPerPixel() override { return fragmentStoresAndAtomics; }
bool hasProvokingVertex() { return provokingVertexSupported; }
bool hasExternalMemoryHost() { return externalMemoryHostSupported; }
bool recreateSwapChainIfNeeded();
void addToFlight(Deletable *object) override {
inFlightObjects[GetCurrentImageIndex()].emplace_back(object);
Expand Down Expand Up @@ -231,6 +232,7 @@ class VulkanContext : public GraphicsContext, public FlightManager
float maxSamplerAnisotropy = 0.f;
bool dedicatedAllocationSupported = false;
bool provokingVertexSupported = false;
bool externalMemoryHostSupported = false;
u32 vendorID = 0;
int swapInterval = 1;
vk::UniqueDevice device;
Expand Down
Loading