Skip to content

Commit

Permalink
Initial change for named resource import
Browse files Browse the repository at this point in the history
* Associate an optional name with the vk resource when handle type is NT
* Use the name when available for importing the resource in OpenCL
* Refactor ctor/dtor for WindowsSecurityAttributes class
  • Loading branch information
saurabhnv committed Feb 17, 2024
1 parent d4f9d04 commit 7f02192
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 79 deletions.
103 changes: 74 additions & 29 deletions test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,23 +586,37 @@ clExternalMemory::clExternalMemory(
extMemProperties.push_back((cl_mem_properties)fd);
break;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT:
{
#ifndef _WIN32
ASSERT(0);
log_info(" Opaque NT handles are only supported on Windows\n");
ASSERT(0);
#else
log_info(" Opaque NT handles are only supported on Windows\n");
handle = deviceMemory->getHandle(externalMemoryHandleType);
err = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties.push_back((cl_mem_properties)handle);
const std::wstring &name = deviceMemory->getName();
if (name.size())
{
err = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR);
extMemProperties.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR);
extMemProperties.push_back((cl_mem_properties)name.c_str());
}
else
{
handle = deviceMemory->getHandle(externalMemoryHandleType);
err = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties.push_back((cl_mem_properties)handle);
}
#endif
}
break;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT:
#ifndef _WIN32
log_info("Opaque D3DKMT handles are only supported on Windows\n");
ASSERT(0);
#else
log_info("Opaque D3DKMT handles are only supported on Windows\n");
handle = deviceMemory->getHandle(externalMemoryHandleType);
err = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KMT_KHR);
Expand All @@ -613,8 +627,8 @@ clExternalMemory::clExternalMemory(
#endif
break;
default:
ASSERT(0);
log_error("Unsupported external memory handle type\n");
ASSERT(0);
break;
}
if (CL_SUCCESS != err)
Expand Down Expand Up @@ -666,13 +680,27 @@ clExternalMemoryImage::clExternalMemoryImage(
{
#ifdef _WIN32
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT:
log_info("Opaque NT handles are only supported on Windows\n");
handle = deviceMemory.getHandle(externalMemoryHandleType);
errcode_ret = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties1.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties1.push_back((cl_mem_properties)handle);
{
log_info("Opaque NT handles are only supported on Windows\n");
const std::wstring &name = deviceMemory.getName();
if (name.size())
{
errcode_ret = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR);
extMemProperties1.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR);
extMemProperties1.push_back((cl_mem_properties)name.c_str());
}
else
{
handle = deviceMemory.getHandle(externalMemoryHandleType);
errcode_ret = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties1.push_back(
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR);
extMemProperties1.push_back((cl_mem_properties)handle);
}
}
break;
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT:
log_info("Opaque D3DKMT handles are only supported on Windows\n");
Expand Down Expand Up @@ -777,6 +805,10 @@ clExternalImportableSemaphore::clExternalImportableSemaphore(
switch (externalSemaphoreHandleType)
{
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD:
#ifdef _WIN32
log_info(" Opaque file descriptors are not supported on Windows\n");
ASSERT(0);
#else
fd = (int)semaphore.getHandle(externalSemaphoreHandleType);
err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_FD_KHR);
Expand All @@ -785,25 +817,38 @@ clExternalImportableSemaphore::clExternalImportableSemaphore(
sema_props.push_back((cl_semaphore_properties_khr)fd);
break;
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_NT:
{
#ifndef _WIN32
ASSERT(0);
log_info(" Opaque NT handles are only supported on Windows\n");
ASSERT(0);
#else
log_info(" Opaque NT handles are only supported on Windows\n");
handle = semaphore.getName().size()
? NULL
: semaphore.getHandle(externalSemaphoreHandleType);
err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR);
sema_props.push_back((cl_semaphore_properties_khr)
CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR);
sema_props.push_back((cl_semaphore_properties_khr)handle);
const std::wstring &name = semaphore.getName();
if (name.size())
{
err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_NAME_KHR);
sema_props.push_back((cl_semaphore_properties_khr)
CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_NAME_KHR);
sema_props.push_back(
(cl_semaphore_properties_khr)name.c_str());
}
else
{
handle = semaphore.getHandle(externalSemaphoreHandleType);
err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR);
sema_props.push_back((cl_semaphore_properties_khr)
CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR);
sema_props.push_back((cl_semaphore_properties_khr)handle);
}
#endif
}
break;
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT:
#ifndef _WIN32
log_info(" Opaque D3DKMT handles are only supported on Windows\n");
ASSERT(0);
#else
log_info(" Opaque D3DKMT handles are only supported on Windows\n");
handle = semaphore.getHandle(externalSemaphoreHandleType);
err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KMT_KHR);
Expand All @@ -821,8 +866,8 @@ clExternalImportableSemaphore::clExternalImportableSemaphore(
sema_props.push_back(static_cast<cl_semaphore_properties_khr>(-1));
break;
default:
ASSERT(0);
log_error("Unsupported external memory handle type\n");
ASSERT(0);
break;
}
if (CL_SUCCESS != err)
Expand Down
93 changes: 47 additions & 46 deletions test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1905,35 +1905,42 @@ class WindowsSecurityAttributes {
~WindowsSecurityAttributes();
};


WindowsSecurityAttributes::WindowsSecurityAttributes()
{
m_winPSecurityDescriptor = (PSECURITY_DESCRIPTOR)calloc(
1, SECURITY_DESCRIPTOR_MIN_LENGTH + 2 * sizeof(void **));
// CHECK_NEQ(m_winPSecurityDescriptor, (PSECURITY_DESCRIPTOR)NULL);
PSID *ppSID = (PSID *)((PBYTE)m_winPSecurityDescriptor
+ SECURITY_DESCRIPTOR_MIN_LENGTH);
PACL *ppACL = (PACL *)((PBYTE)ppSID + sizeof(PSID *));
InitializeSecurityDescriptor(m_winPSecurityDescriptor,
SECURITY_DESCRIPTOR_REVISION);
SID_IDENTIFIER_AUTHORITY sidIdentifierAuthority =
SECURITY_WORLD_SID_AUTHORITY;
AllocateAndInitializeSid(&sidIdentifierAuthority, 1, SECURITY_WORLD_RID, 0,
0, 0, 0, 0, 0, 0, ppSID);
EXPLICIT_ACCESS explicitAccess;
ZeroMemory(&explicitAccess, sizeof(EXPLICIT_ACCESS));
explicitAccess.grfAccessPermissions =
STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL;
explicitAccess.grfAccessMode = SET_ACCESS;
explicitAccess.grfInheritance = INHERIT_ONLY;
explicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID;
explicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
explicitAccess.Trustee.ptstrName = (LPTSTR)*ppSID;
SetEntriesInAcl(1, &explicitAccess, NULL, ppACL);
SetSecurityDescriptorDacl(m_winPSecurityDescriptor, TRUE, *ppACL, FALSE);
m_winSecurityAttributes.nLength = sizeof(m_winSecurityAttributes);
#define CHECK(ok, msg) \
if (!ok) { \
throw std::runtime_error(msg); \
}

BOOL ok;
HANDLE tokenHandle;

ok = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tokenHandle);
CHECK(ok, "Failed to open process access token");

DWORD tokenInformationLength = 0;
GetTokenInformation(tokenHandle, TokenDefaultDacl, NULL, 0, &tokenInformationLength);
CHECK(tokenInformationLength, "Failed to retrieve TokenDefaultDacl info buffer length");

m_winPSecurityDescriptor = (PSECURITY_DESCRIPTOR)calloc(1, SECURITY_DESCRIPTOR_MIN_LENGTH + tokenInformationLength);
assert(m_winPSecurityDescriptor != (PSECURITY_DESCRIPTOR)NULL);

TOKEN_DEFAULT_DACL* pTokenDefaultDacl = reinterpret_cast<TOKEN_DEFAULT_DACL*>((PBYTE)m_winPSecurityDescriptor + SECURITY_DESCRIPTOR_MIN_LENGTH);
ok = GetTokenInformation(tokenHandle, TokenDefaultDacl, pTokenDefaultDacl, tokenInformationLength, &tokenInformationLength);
CHECK(ok, "Failed to retrieve TokenDefaultDacl info of access token");

ok = InitializeSecurityDescriptor(m_winPSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION);
CHECK(ok, "Failed to init security descriptor");

ok = SetSecurityDescriptorDacl(m_winPSecurityDescriptor, TRUE, pTokenDefaultDacl->DefaultDacl, FALSE);
CHECK(ok, "Failed to set DACL info for given security descriptor");

m_winSecurityAttributes.nLength = sizeof(m_winSecurityAttributes);
m_winSecurityAttributes.lpSecurityDescriptor = m_winPSecurityDescriptor;
m_winSecurityAttributes.bInheritHandle = TRUE;
m_winSecurityAttributes.bInheritHandle = TRUE;

CloseHandle(tokenHandle);
#undef CHECK_WIN
}

SECURITY_ATTRIBUTES *WindowsSecurityAttributes::operator&()
Expand All @@ -1943,17 +1950,6 @@ SECURITY_ATTRIBUTES *WindowsSecurityAttributes::operator&()

WindowsSecurityAttributes::~WindowsSecurityAttributes()
{
PSID *ppSID = (PSID *)((PBYTE)m_winPSecurityDescriptor
+ SECURITY_DESCRIPTOR_MIN_LENGTH);
PACL *ppACL = (PACL *)((PBYTE)ppSID + sizeof(PSID *));
if (*ppSID)
{
FreeSid(*ppSID);
}
if (*ppACL)
{
LocalFree(*ppACL);
}
free(m_winPSecurityDescriptor);
}

Expand All @@ -1968,8 +1964,8 @@ VulkanDeviceMemory::VulkanDeviceMemory(const VulkanDeviceMemory &deviceMemory)
VulkanDeviceMemory::VulkanDeviceMemory(
const VulkanDevice &device, uint64_t size,
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name)
: m_device(device), m_size(size), m_isDedicated(false)
VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name)
: m_device(device), m_size(size), m_isDedicated(false), m_name(name)
{
#if defined(_WIN32) || defined(_WIN64)
WindowsSecurityAttributes winSecurityAttributes;
Expand All @@ -1981,7 +1977,8 @@ VulkanDeviceMemory::VulkanDeviceMemory(
vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes;
vkExportMemoryWin32HandleInfoKHR.dwAccess =
DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;
vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name;
vkExportMemoryWin32HandleInfoKHR.name =
m_name.size() ? (LPCWSTR)m_name.c_str() : NULL;

#endif

Expand Down Expand Up @@ -2012,9 +2009,9 @@ VulkanDeviceMemory::VulkanDeviceMemory(
VulkanDeviceMemory::VulkanDeviceMemory(
const VulkanDevice &device, const VulkanImage &image,
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name)
VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name)
: m_device(device), m_size(image.getSize()),
m_isDedicated(image.isDedicated())
m_isDedicated(image.isDedicated()), m_name(name)
{
#if defined(_WIN32) || defined(_WIN64)
WindowsSecurityAttributes winSecurityAttributes;
Expand All @@ -2026,7 +2023,8 @@ VulkanDeviceMemory::VulkanDeviceMemory(
vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes;
vkExportMemoryWin32HandleInfoKHR.dwAccess =
DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;
vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name;
vkExportMemoryWin32HandleInfoKHR.name =
m_name.size() ? (LPCWSTR)m_name.c_str() : NULL;

#endif

Expand Down Expand Up @@ -2074,9 +2072,9 @@ VulkanDeviceMemory::VulkanDeviceMemory(
VulkanDeviceMemory::VulkanDeviceMemory(
const VulkanDevice &device, const VulkanBuffer &buffer,
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name)
VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name)
: m_device(device), m_size(buffer.getSize()),
m_isDedicated(buffer.isDedicated())
m_isDedicated(buffer.isDedicated()), m_name(name)
{
#if defined(_WIN32) || defined(_WIN64)
WindowsSecurityAttributes winSecurityAttributes;
Expand All @@ -2088,7 +2086,8 @@ VulkanDeviceMemory::VulkanDeviceMemory(
vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes;
vkExportMemoryWin32HandleInfoKHR.dwAccess =
DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE;
vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name;
vkExportMemoryWin32HandleInfoKHR.name =
m_name.size() ? (LPCWSTR)m_name.c_str() : NULL;

#endif

Expand Down Expand Up @@ -2220,6 +2219,8 @@ void VulkanDeviceMemory::bindImage(const VulkanImage &image, uint64_t offset)
vkBindImageMemory(m_device, image, m_vkDeviceMemory, offset);
}

const std::wstring &VulkanDeviceMemory::getName() const { return m_name; }

VulkanDeviceMemory::operator VkDeviceMemory() const { return m_vkDeviceMemory; }

////////////////////////////////////
Expand Down
10 changes: 6 additions & 4 deletions test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "vulkan_list_map.hpp"
#include "vulkan_api_list.hpp"
#include <memory>
#include <cassert>

class VulkanInstance {
friend const VulkanInstance &getVulkanInstance();
Expand Down Expand Up @@ -539,7 +540,7 @@ class VulkanDeviceMemory {
VkDeviceMemory m_vkDeviceMemory;
uint64_t m_size;
bool m_isDedicated;

const std::wstring m_name;

VulkanDeviceMemory(const VulkanDeviceMemory &deviceMemory);

Expand All @@ -548,17 +549,17 @@ class VulkanDeviceMemory {
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType =
VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
const void *name = NULL);
const std::wstring name = L"");
VulkanDeviceMemory(const VulkanDevice &device, const VulkanImage &image,
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType =
VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
const void *name = NULL);
const std::wstring name = L"");
VulkanDeviceMemory(const VulkanDevice &device, const VulkanBuffer &buffer,
const VulkanMemoryType &memoryType,
VulkanExternalMemoryHandleType externalMemoryHandleType =
VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
const void *name = NULL);
const std::wstring name = L"");
virtual ~VulkanDeviceMemory();
uint64_t getSize() const;
#ifdef _WIN32
Expand All @@ -573,6 +574,7 @@ class VulkanDeviceMemory {
void unmap();
void bindBuffer(const VulkanBuffer &buffer, uint64_t offset = 0);
void bindImage(const VulkanImage &image, uint64_t offset = 0);
const std::wstring &getName() const;
operator VkDeviceMemory() const;
};

Expand Down

0 comments on commit 7f02192

Please sign in to comment.