Skip to content

Commit

Permalink
Semaphore types bug fixes revised (#1822)
Browse files Browse the repository at this point in the history
* Added support for SYNC_FD and other handle types

* Fix consistency test

Deleted test cases that are no longer testable
according to the spec.

* Fix multi-import tests

-Delete obsolete code relating to offsets
-Propagate dedicated memory change

* Fix error handling

Some subtests did not fail on incorrect result.
Changes to macros to fail, so this does not occur
again.

* Delete invalid test cases

Test cases are not related to this extension.

* External memory test

Add support for any handle type supported by
the platform.

Change-Id: I6765fde5e7929988f49bfbf2df2f41d5263b6abc

* Update multi-import tests to use new semaphore types

* Fix formatting

* Addressed review comments. Deleted VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_NT_KMT as it appears to be redundant.
  • Loading branch information
joshqti authored Nov 29, 2023
1 parent 5815e2c commit f5bd92b
Show file tree
Hide file tree
Showing 16 changed files with 1,536 additions and 1,445 deletions.
15 changes: 15 additions & 0 deletions test_common/harness/errorHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ static int vlog_win32(const char *format, ...);
log_error(msg, ##__VA_ARGS__); \
return TEST_FAIL; \
}
#define test_fail_and_cleanup(errRet, cleanup, msg, ...) \
{ \
log_error(msg, ##__VA_ARGS__); \
errRet = TEST_FAIL; \
goto cleanup; \
}
#define test_error_and_cleanup(errCode, cleanup, msg, ...) \
{ \
auto errCodeResult = errCode; \
if (errCodeResult != CL_SUCCESS) \
{ \
print_error(errCodeResult, msg); \
goto cleanup; \
} \
}
#define test_error(errCode, msg) test_error_ret(errCode, msg, errCode)
#define test_error_fail(errCode, msg) test_error_ret(errCode, msg, TEST_FAIL)
#define test_error_ret(errCode, msg, retValue) \
Expand Down
156 changes: 105 additions & 51 deletions test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pfnclEnqueueAcquireExternalMemObjectsKHR
pfnclEnqueueReleaseExternalMemObjectsKHR
clEnqueueReleaseExternalMemObjectsKHRptr;
pfnclReleaseSemaphoreKHR clReleaseSemaphoreKHRptr;
pfnclGetSemaphoreHandleForTypeKHR clGetSemaphoreHandleForTypeKHRptr;

void init_cl_vk_ext(cl_platform_id opencl_platform)
{
Expand Down Expand Up @@ -69,6 +70,15 @@ void init_cl_vk_ext(cl_platform_id opencl_platform)
throw std::runtime_error("Failed to get the function pointer of "
"clCreateSemaphoreWithPropertiesKHRptr!");
}

clGetSemaphoreHandleForTypeKHRptr = (pfnclGetSemaphoreHandleForTypeKHR)
clGetExtensionFunctionAddressForPlatform(
opencl_platform, "clGetSemaphoreHandleForTypeKHR");
if (NULL == clGetSemaphoreHandleForTypeKHRptr)
{
throw std::runtime_error("Failed to get the function pointer of "
"clGetSemaphoreHandleForTypeKHRptr!");
}
}

cl_int setMaxImageDimensions(cl_device_id deviceID, size_t &max_width,
Expand Down Expand Up @@ -522,8 +532,8 @@ clExternalMemory::clExternalMemory(const clExternalMemory &externalMemory)

clExternalMemory::clExternalMemory(
const VulkanDeviceMemory *deviceMemory,
VulkanExternalMemoryHandleType externalMemoryHandleType, uint64_t offset,
uint64_t size, cl_context context, cl_device_id deviceId)
VulkanExternalMemoryHandleType externalMemoryHandleType, uint64_t size,
cl_context context, cl_device_id deviceId)
{
int err = 0;
m_externalMemory = NULL;
Expand All @@ -548,9 +558,9 @@ clExternalMemory::clExternalMemory(
{
case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD:
#ifdef _WIN32
log_info("Opaque file descriptors are not supported on Windows\n");
ASSERT(0);
#endif
log_info("Opaque file descriptors are not supported on Windows\n");
fd = (int)deviceMemory->getHandle(externalMemoryHandleType);
err = check_external_memory_handle_type(
devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR);
Expand Down Expand Up @@ -728,56 +738,19 @@ clExternalMemoryImage::clExternalMemoryImage() {}
// clExternalSemaphore implementation //
//////////////////////////////////////////

clExternalSemaphore::clExternalSemaphore(
const clExternalSemaphore &externalSemaphore)
: m_externalSemaphore(externalSemaphore.m_externalSemaphore)
{}

clExternalSemaphore::clExternalSemaphore(
const VulkanSemaphore &semaphore, cl_context context,
VulkanExternalSemaphoreHandleType externalSemaphoreHandleType,
cl_device_id deviceId)
: m_deviceSemaphore(semaphore)
{

cl_int err = 0;
cl_device_id devList[] = { deviceId, NULL };

switch (externalSemaphoreHandleType)
{
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD:
if (!is_extension_available(devList[0],
"cl_khr_external_semaphore_opaque_fd"))
{
throw std::runtime_error("Device does not support "
"cl_khr_external_semaphore_opaque_fd "
"extension \n");
}
break;
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_NT:
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT:
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_NT_KMT:
if (!is_extension_available(devList[0],
"cl_khr_external_semaphore_win32"))
{
throw std::runtime_error(
"Device does not support "
"cl_khr_external_semaphore_win32 extension\n");
}
break;
case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD:
if (!is_extension_available(devList[0],
"cl_khr_external_semaphore_sync_fd"))
{
throw std::runtime_error(
"Device does not support cl_khr_external_semaphore_sync_fd "
"extension \n");
}
break;
default:
throw std::runtime_error(
"Unsupported external semaphore handle type\n");
break;
}
m_externalHandleType = externalSemaphoreHandleType;
m_externalSemaphore = nullptr;
m_device = deviceId;
m_context = context;

std::vector<cl_semaphore_properties_khr> sema_props{
(cl_semaphore_properties_khr)CL_SEMAPHORE_TYPE_KHR,
Expand Down Expand Up @@ -873,16 +846,97 @@ clExternalSemaphore::~clExternalSemaphore() noexcept(false)
}
}

void clExternalSemaphore::signal(cl_command_queue cmd_queue)
int clExternalSemaphore::signal(cl_command_queue cmd_queue)
{
clEnqueueSignalSemaphoresKHRptr(cmd_queue, 1, &m_externalSemaphore, NULL, 0,
NULL, NULL);
int err = clEnqueueSignalSemaphoresKHRptr(
cmd_queue, 1, &m_externalSemaphore, NULL, 0, NULL, nullptr);
if (err != CL_SUCCESS)
{
return err;
}

if (m_externalHandleType == VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD)
{
err = clGetSemaphoreHandleForTypeKHRptr(m_externalSemaphore, m_device,
CL_SEMAPHORE_HANDLE_SYNC_FD_KHR,
sizeof(int), &fd, nullptr);
if (err != CL_SUCCESS)
{
log_error("Failed to export fd from semaphore\n");
return err;
}

VkImportSemaphoreFdInfoKHR import = {};
import.sType = VK_STRUCTURE_TYPE_IMPORT_SEMAPHORE_FD_INFO_KHR;
import.semaphore = m_deviceSemaphore;
import.fd = fd;
import.pNext = nullptr;
import.handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT_KHR;
import.flags = 0;

VkResult res =
vkImportSemaphoreFdKHR(m_deviceSemaphore.getDevice(), &import);
ASSERT(res == VK_SUCCESS);
if (res != VK_SUCCESS)
{
err = CL_INVALID_OPERATION;
}
}

return err;
}

void clExternalSemaphore::wait(cl_command_queue cmd_queue)
int clExternalSemaphore::wait(cl_command_queue cmd_queue)
{
clEnqueueWaitSemaphoresKHRptr(cmd_queue, 1, &m_externalSemaphore, NULL, 0,
NULL, NULL);
int err = CL_SUCCESS;
if (m_externalHandleType == VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD)
{
cl_int err = 0;
cl_device_id devList[] = { m_device, NULL };
std::vector<cl_semaphore_properties_khr> sema_props{
(cl_semaphore_properties_khr)CL_SEMAPHORE_TYPE_KHR,
(cl_semaphore_properties_khr)CL_SEMAPHORE_TYPE_BINARY_KHR,
};
fd = (int)m_deviceSemaphore.getHandle(m_externalHandleType);

err = check_external_semaphore_handle_type(
devList[0], CL_SEMAPHORE_HANDLE_SYNC_FD_KHR);
if (CL_SUCCESS != err)
{
log_error("CL_SEMAPHORE_HANDLE_SYNC_FD_KHR not supported\n");
return err;
}

sema_props.push_back(
(cl_semaphore_properties_khr)CL_SEMAPHORE_HANDLE_SYNC_FD_KHR);
sema_props.push_back((cl_semaphore_properties_khr)fd);

sema_props.push_back(0);

if (m_externalSemaphore)
{
err = clReleaseSemaphoreKHRptr(m_externalSemaphore);
if (err != CL_SUCCESS)
{
log_error("Failed to release CL external semaphore\n");
return err;
}
m_externalSemaphore = nullptr;
}

m_externalSemaphore = clCreateSemaphoreWithPropertiesKHRptr(
m_context, sema_props.data(), &err);
if (CL_SUCCESS != err)
{
log_error("clCreateSemaphoreWithPropertiesKHRptr failed with %d\n",
err);
return err;
}
}

err = clEnqueueWaitSemaphoresKHRptr(cmd_queue, 1, &m_externalSemaphore,
NULL, 0, NULL, NULL);
return err;
}

cl_semaphore_khr &clExternalSemaphore::getCLSemaphore()
Expand Down
16 changes: 11 additions & 5 deletions test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ typedef cl_int (*pfnclEnqueueReleaseExternalMemObjectsKHR)(
const cl_mem *mem_objects, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event);
typedef cl_int (*pfnclReleaseSemaphoreKHR)(cl_semaphore_khr sema_object);
typedef cl_int (*pfnclGetSemaphoreHandleForTypeKHR)(
cl_semaphore_khr sema_object, cl_device_id device,
cl_external_semaphore_handle_type_khr handleType, size_t handle_size,
void *handle, size_t *handleSize);

extern pfnclCreateSemaphoreWithPropertiesKHR
clCreateSemaphoreWithPropertiesKHRptr;
Expand Down Expand Up @@ -83,8 +87,7 @@ class clExternalMemory {
clExternalMemory();
clExternalMemory(const VulkanDeviceMemory *deviceMemory,
VulkanExternalMemoryHandleType externalMemoryHandleType,
uint64_t offset, uint64_t size, cl_context context,
cl_device_id deviceId);
uint64_t size, cl_context context, cl_device_id deviceId);

virtual ~clExternalMemory();
cl_mem getExternalMemoryBuffer();
Expand All @@ -111,18 +114,21 @@ class clExternalMemoryImage {
class clExternalSemaphore {
protected:
cl_semaphore_khr m_externalSemaphore;
VulkanExternalSemaphoreHandleType m_externalHandleType;
cl_device_id m_device;
cl_context m_context;
const VulkanSemaphore &m_deviceSemaphore;
int fd;
void *handle;
clExternalSemaphore(const clExternalSemaphore &externalSemaphore);

public:
clExternalSemaphore(
const VulkanSemaphore &deviceSemaphore, cl_context context,
VulkanExternalSemaphoreHandleType externalSemaphoreHandleType,
cl_device_id deviceId);
virtual ~clExternalSemaphore() noexcept(false);
void signal(cl_command_queue command_queue);
void wait(cl_command_queue command_queue);
int signal(cl_command_queue command_queue);
int wait(cl_command_queue command_queue);
cl_semaphore_khr &getCLSemaphore();
// operator openclExternalSemaphore_t() const;
};
Expand Down
9 changes: 6 additions & 3 deletions test_conformance/common/vulkan_wrapper/vulkan_api_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@
VK_FUNC_DECL(vkGetPhysicalDeviceSurfaceFormatsKHR) \
VK_FUNC_DECL(vkGetPhysicalDeviceSurfacePresentModesKHR) \
VK_FUNC_DECL(vkEnumerateDeviceExtensionProperties) \
VK_FUNC_DECL(vkGetPhysicalDeviceSurfaceSupportKHR)

VK_FUNC_DECL(vkGetPhysicalDeviceSurfaceSupportKHR) \
VK_FUNC_DECL(vkImportSemaphoreFdKHR) \
VK_FUNC_DECL(vkGetPhysicalDeviceExternalSemaphorePropertiesKHR)
#define VK_WINDOWS_FUNC_LIST \
VK_FUNC_DECL(vkGetMemoryWin32HandleKHR) \
VK_FUNC_DECL(vkGetSemaphoreWin32HandleKHR)
Expand Down Expand Up @@ -192,7 +193,9 @@
_vkEnumerateDeviceExtensionProperties
#define vkGetPhysicalDeviceSurfaceSupportKHR \
_vkGetPhysicalDeviceSurfaceSupportKHR

#define vkImportSemaphoreFdKHR _vkImportSemaphoreFdKHR
#define vkGetPhysicalDeviceExternalSemaphorePropertiesKHR \
_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR
#define vkGetMemoryWin32HandleKHR _vkGetMemoryWin32HandleKHR
#define vkGetSemaphoreWin32HandleKHR _vkGetSemaphoreWin32HandleKHR

Expand Down
2 changes: 2 additions & 0 deletions test_conformance/common/vulkan_wrapper/vulkan_list_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,8 @@ const VulkanWrapper &
return (m_wrapperList.size() > 0) ? m_wrapperList[idx].get()
: m_constWrapperList[idx].get();
}

throw std::runtime_error("Out of bounds operator access");
}

template <class VulkanWrapper, class VulkanNative>
Expand Down
Loading

0 comments on commit f5bd92b

Please sign in to comment.