Skip to content

Commit 7849e05

Browse files
committed
Add OpenGL interop
1 parent d1aa053 commit 7849e05

File tree

6 files changed

+728
-1
lines changed

6 files changed

+728
-1
lines changed

.wordlist.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ iGPU
7373
inlined
7474
inplace
7575
interop
76-
Interoperation
76+
interoperation
7777
interoperate
7878
interoperation
7979
Interprocess
@@ -162,6 +162,8 @@ unintuitive
162162
UMM
163163
unmap
164164
unmapped
165+
unmapping
166+
unregister
165167
upscaled
166168
variadic
167169
vulkan

docs/how-to/hip_runtime_api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ Here are the various HIP Runtime API high level functions:
4444
* :doc:`./hip_runtime_api/hipgraph`
4545
* :doc:`./hip_runtime_api/call_stack`
4646
* :doc:`./hip_runtime_api/multi_device`
47+
* :doc:`./hip_runtime_api/opengl_interop`
4748
* :doc:`./hip_runtime_api/external_interop`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
.. meta::
2+
:description: HIP provides an OpenGL interoperability API that allows
3+
efficient data sharing between HIP's computing power and
4+
OpenGL's graphics rendering.
5+
:keywords: AMD, ROCm, HIP, OpenGL, interop, interoperability
6+
7+
*******************************************************************************
8+
OpenGL interoperability
9+
*******************************************************************************
10+
11+
The HIP--OpenGL interoperation involves mapping OpenGL resources, such as
12+
buffers and textures, for HIP to interact with OpenGL. This mapping process
13+
enables HIP to utilize these resources directly, bypassing the need for costly
14+
data transfers between the CPU and GPU. This capability is useful in
15+
applications that require both intensive GPU computation and real-time
16+
visualization.
17+
18+
The graphics resources must be registered using functions like
19+
:cpp:func:`hipGraphicsGLRegisterBuffer` or :cpp:func:`hipGraphicsGLRegisterImage`
20+
then they can be mapped to HIP with :cpp:func:`hipGraphicsMapResources`
21+
function.
22+
23+
After mapping, the :cpp:func:`hipGraphicsResourceGetMappedPointer` or
24+
:cpp:func:`hipGraphicsSubResourceGetMappedArray` functions used to retrieve a
25+
device pointer to the mapped resource, which can then be used in HIP kernels.
26+
27+
Unmapping resources with :cpp:func:`hipGraphicsUnmapResources` after
28+
computations ensure proper resource management.
29+
30+
Example
31+
===============================================================================
32+
33+
ROCm examples have a `HIP--OpenGL interoperation example <https://github.com/ROCm/rocm-examples/tree/develop/HIP-Basic/opengl_interop>`_,
34+
where a simple HIP kernel is used to simulate a sine wave and rendered to a
35+
window as a grid of triangles using OpenGL. For a working example, there are
36+
multiple initialization steps needed like creating and opening a window,
37+
initializing OpenGL or selecting the OpenGL-capable device. After the
38+
initialization in the example, the kernel simulates the sinewave and updates
39+
the window's framebuffer in a cycle until the window is closed.
40+
41+
.. note::
42+
43+
The more recent OpenGL functions are loaded with `OpenGL loader <https://github.com/ROCm/rocm-examples/tree/develop/External/glad>`_,
44+
as these are not loaded by default on all platforms. The use of a custom
45+
loader is shown in the following example
46+
47+
.. <!-- spellcheck-disable -->
48+
49+
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
50+
:start-after: // [Sphinx opengl functions load start]
51+
:end-before: // [Sphinx opengl functions load end]
52+
:language: cpp
53+
54+
.. <!-- spellcheck-enable -->
55+
56+
The OpenGL buffer is imported to HIP in the following way:
57+
58+
.. <!-- spellcheck-disable -->
59+
60+
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
61+
:start-after: // [Sphinx buffer register and get start]
62+
:end-before: // [Sphinx buffer register and get end]
63+
:language: cpp
64+
65+
.. <!-- spellcheck-enable -->
66+
67+
The imported pointer is manipulated in the sinewave kernel as shown in the
68+
following example:
69+
70+
.. <!-- spellcheck-disable -->
71+
72+
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
73+
:start-after: /// [Sphinx sinewave kernel start]
74+
:end-before: /// [Sphinx sinewave kernel end]
75+
:language: cpp
76+
77+
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
78+
:start-after: // [Sphinx buffer use in kernel start]
79+
:end-before: // [Sphinx buffer use in kernel end]
80+
:language: cpp
81+
82+
.. <!-- spellcheck-enable -->
83+
84+
The HIP graphics resource that is imported from the OpenGL buffer and is not
85+
needed anymore should be unmapped and unregistered as shown in the following way:
86+
87+
.. <!-- spellcheck-disable -->
88+
89+
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
90+
:start-after: // [Sphinx unregister start]
91+
:end-before: // [Sphinx unregister end]
92+
:language: cpp
93+
94+
.. <!-- spellcheck-enable -->

docs/sphinx/_toc.yml.in

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ subtrees:
5353
- file: how-to/hip_runtime_api/hipgraph
5454
- file: how-to/hip_runtime_api/call_stack
5555
- file: how-to/hip_runtime_api/multi_device
56+
- file: how-to/hip_runtime_api/opengl_interop
5657
- file: how-to/hip_runtime_api/external_interop
5758
- file: how-to/hip_porting_guide
5859
- file: how-to/hip_porting_driver_api

0 commit comments

Comments
 (0)