Skip to content

Commit c7a55b0

Browse files
authored
update Vulkan sample with external memory and semaphore sharing (#59)
* started to add interop functions but still crashes * slightly working version * successfully create an image from external memory * revert a few other testing changes * basic memory sharing is working * fix dummy object codepath with host copy * fix validation errors * start to add external semaphore support * add support for synchronization via semaphores * reduce verbosity by default, start to add Linux support * fix Linux builds * fix dependency on OpenCL extension loader * fix Linux function name typo * remove nvidia workarounds, dependency on extension loader * tidy up command line options, update readme * remove unused vector of wait semaphores * update readme to recommend installing the latest drivers * fix incorrect handle type for external semaphore * fix incorrect app name * add a command-line option for VK_PRESENT_MODE_IMMEDIATE_KHR * try to add support for dma_buf sharing on Linux * fix whitespace * add a command line option to disable device local images
1 parent 42ca564 commit c7a55b0

5 files changed

Lines changed: 565 additions & 62 deletions

File tree

include/CL/opencl.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,18 @@ CL_HPP_DECLARE_PARAM_TRAITS_(cl_command_buffer_info_khr, CL_COMMAND_BUFFER_STATE
15471547
CL_HPP_DECLARE_PARAM_TRAITS_(cl_command_buffer_info_khr, CL_COMMAND_BUFFER_PROPERTIES_ARRAY_KHR, cl::vector<cl_command_buffer_properties_khr>)
15481548
#endif // cl_khr_command_buffer
15491549

1550+
#if defined(cl_khr_external_memory)
1551+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_platform_info, CL_PLATFORM_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_memory_handle_type_khr>)
1552+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_memory_handle_type_khr>)
1553+
#endif // cl_khr_external_memory
1554+
1555+
#if defined(cl_khr_external_semaphore)
1556+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_platform_info, CL_PLATFORM_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_semaphore_handle_type_khr>)
1557+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_platform_info, CL_PLATFORM_SEMAPHORE_EXPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_semaphore_handle_type_khr>)
1558+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_semaphore_handle_type_khr>)
1559+
CL_HPP_DECLARE_PARAM_TRAITS_(cl_device_info, CL_DEVICE_SEMAPHORE_EXPORT_HANDLE_TYPES_KHR, cl::vector<cl_external_semaphore_handle_type_khr>)
1560+
#endif // cl_khr_external_semaphore
1561+
15501562
#ifdef CL_PLATFORM_ICD_SUFFIX_KHR
15511563
CL_HPP_DECLARE_PARAM_TRAITS_(cl_platform_info, CL_PLATFORM_ICD_SUFFIX_KHR, string)
15521564
#endif

samples/vulkan/00_juliavk/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2021 Ben Ashbaugh
1+
# Copyright (c) 2021-2022 Ben Ashbaugh
22
#
33
# Permission is hereby granted, free of charge, to any person obtaining a copy
44
# of this software and associated documentation files (the "Software"), to deal
@@ -21,7 +21,7 @@
2121
add_opencl_sample(
2222
NUMBER 00
2323
TARGET juliavk
24-
VERSION 120
24+
VERSION 300 # clCreateImageWithProperties
2525
CATEGORY vulkan
2626
SOURCES main.cpp
2727
KERNELS juliavk.vert.spv juliavk.frag.spv

samples/vulkan/00_juliavk/README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,51 @@ This is a modified version of the earlier Julia set sample.
66
Similar to the earlier Julia Set sample, an OpenCL kernel is used to generate a Julia set image.
77
The main difference between this sample and the earlier sample is that in this sample the Julia set image is used as an Vulkan texture and rendered to the screen instead of writing it to a BMP file.
88

9-
This sample currently copies the result of the OpenCL kernel to the Vulkan texture on the host, however several new extensions were recently provisionally released to enable [interop between OpenCL and Vulkan devices](https://www.khronos.org/blog/khronos-releases-opencl-3.0-extensions-for-neural-network-inferencing-and-opencl-vulkan-interop).
10-
When drivers are available that support these extensions, this sample will be updated to use them!
9+
This sample can create an OpenCL image directly from the Vulkan texture when supported.
10+
In order to share the Vulkan texture with OpenCL, the OpenCL device must support [cl_khr_external_memory](https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html#cl_khr_external_memory).
11+
Additionally, the Vulkan device must support exporting the Vulkan texture as an OS-specific external memory handle, and the OpenCL device must support importing external memory handles of that type.
12+
Creating the OpenCL image directly from the Vulkan texture avoids a memory copy and can improve performance.
13+
14+
For Windows, the external memory handle types that are currently supported are:
15+
16+
* `CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR`
17+
18+
For Linux, the external memory handle types that are currently supported are:
19+
20+
* `CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR`
21+
* `CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR`
22+
23+
This sample can also share semaphores between Vulkan and OpenCL when supported.
24+
In order to share a Vulkan semaphore with OpenCL, the OpenCL device must support [cl_khr_external_semaphore](https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_Ext.html#cl_khr_external_semaphore).
25+
Additionally, the Vulkan device must support exporting the Vulkan semaphore as an OS-specific external semaphore handle, and the OpenCL device must support importing external semaphore handles of that type.
26+
Sharing a semaphore object between Vulkan and OpenCL avoids synchronization on the host and can also improve performance.
27+
28+
For Windows, the external semaphore handle types that are currently supported are:
29+
30+
* `CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR`
31+
32+
For Linux, the external semaphore handle types that are currently supported are:
33+
34+
* `CL_SEMAPHORE_HANDLE_OPAQUE_FD_KHR`
35+
36+
For more information about these extensions, please see [this blog post](https://www.khronos.org/blog/khronos-releases-opencl-3.0-extensions-for-neural-network-inferencing-and-opencl-vulkan-interop).
37+
When the extensions are not supported the sample will still run, although perhaps with lower performance.
38+
39+
Important note: The OpenCL extensions used in this sample are very new!
40+
If the sample does not run correctly please ensure you are using the latest OpenCL drivers for your device, or use the command line option to force copying on the host.
1141

1242
## Key APIs and Concepts
1343

14-
This example shows how to share an Vulkan texture with OpenCL.
44+
This example shows how to share an Vulkan texture and semaphore with OpenCL.
45+
46+
```c
47+
clEnqueueAcquireExternalMemObjectsKHR
48+
clEnqueueReleaseExternalMemObjectsKHR
49+
50+
clCreateSemaphoreWithPropertiesKHR
51+
clEnqueueSignalSemaphoresKHR
52+
clReleaseSemaphoreKHR
53+
```
1554

1655
## Command Line Options
1756

@@ -21,10 +60,14 @@ Note: Many of these command line arguments are identical to the earlier Julia se
2160
|:--|:-:|:--|
2261
| `-d <index>` | 0 | Specify the index of the OpenCL device in the platform to execute on the sample on.
2362
| `-p <index>` | 0 | Specify the index of the OpenCL platform to execute the sample on.
63+
| `--hostcopy` | n/a | Do not use the `cl_khr_external_memory` extension and unconditionally copy on the host.
64+
| `--hostsync` | n/a | Do not use the `cl_khr_external_semaphore` extension and exclusively synchronize on the host.
65+
| `--nodevicelocal` | n/a | Do not use device local images (`VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT`). May be useful for debugging.
2466
| `--gwx <number>` | 512 | Specify the global work size to execute, in the X direction. This also determines the width of the generated image.
2567
| `--gwy <number>` | 512 | Specify the global work size to execute, in the Y direction. This also determines the height of the generated image.
2668
| `--lwx <number>` | 0 | Specify the local work size in the X direction. If either local works size dimension is zero a `NULL` local work size is used.
2769
| `--lwy <number>` | 0 | Specify the local work size in the Y direction. If either local works size dimension is zero a `NULL` local work size is used.
70+
| `--immediate` | n/a | Prefer `VK_PRESENT_MODE_IMMEDIATE_KHR` (no vsync). May result in faster framerates at the cost of visible tearing.
2871

2972
## Controls While Running
3073

0 commit comments

Comments
 (0)