|
| 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 --> |
0 commit comments