Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
neon60 committed Oct 31, 2024
1 parent ce252e4 commit 3547ab4
Show file tree
Hide file tree
Showing 3 changed files with 662 additions and 117 deletions.
148 changes: 31 additions & 117 deletions docs/how-to/hip_runtime_api/opengl_interop.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,133 +20,47 @@ function and the device pointers are accessed with
:cpp:func:`hipGraphicsSubResourceGetMappedArray` functions.

Unmapping resources with :cpp:func:`hipGraphicsUnmapResources` after
computations ensures proper resource management.
computations ensure proper resource management.

Examples
Example
================================================================================

Two examples are presented with mapping resources, getting pointers -- directly
or with arrays -- and unmapping resources with a OpenGL buffer registration.
ROCm examples have a `HIP--OpenGL interoperation example <https://github.com/ROCm/rocm-examples/tree/develop/HIP-Basic/opengl_interop>`_,
where a simple HIP kernel is used to simulate a sine wave and rendered to a
window as a grid of triangles using OpenGL. For a working example, there are
multiple initialization steps needed like creating and opening a window,
initializing OpenGL or selecting the OpenGL-capable device. After the initialization
in the example, the kernel simulates the sinewave and updates the window's
framebuffer in a cycle until the window is not closed.

.. tab-set::
.. note::

.. tab-item:: with mapped pointer
The more recent recent OpenGL functions loaded with `OpenGL loader <https://github.com/ROCm/rocm-examples/tree/develop/External/glad>`_,
as these are not loaded by default on all platforms. The use of custom loader
shown in the following example

.. code-block:: cpp
:emphasize-lines: 21-24
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
:start-after: // [Sphinx opengl functions load start]
:end-before: // [Sphinx opengl functions load end]

#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
#include <GL/glew.h>
#include <GL/gl.h>
The OpenGL buffer is imported to HIP using as the following way:

int main()
{
// Initialize OpenGL and create a buffer
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
:start-after: // [Sphinx buffer register and get start]
:end-before: // [Sphinx buffer register and get end]

// Register the OpenGL buffer with HIP
hipGraphicsResource* resource;
hipGraphicsGLRegisterBuffer(&resource, buffer, hipGraphicsRegisterFlagsNone);
The imported pointer manipulated in the sinewave kernel as the following way:

// Map the resource for access by HIP
hipGraphicsMapResources(1, &resource, 0);
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
:start-after: /// [Sphinx sinewave kernel start]
:end-before: /// [Sphinx sinewave kernel end]

// Obtain a pointer to the mapped resource
void* devicePtr;
size_t numBytes;
hipGraphicsResourceGetMappedPointer(&devicePtr, &numBytes, resource);
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
:start-after: // [Sphinx buffer use in kernel start]
:end-before: // [Sphinx buffer use in kernel end]

// Use devicePtr in HIP kernels...
The OpenGL buffer is imported to HIP using as the following way:

// Unmap the resources when done
hipGraphicsUnmapResources(1, &resource, 0);
// Cleanup OpenGL resources
glDeleteBuffers(1, &buffer);
return 0;
}
.. tab-item:: with mapped array

.. code-block:: cpp
:emphasize-lines: 21-23
#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
#include <GL/glew.h>
#include <GL/gl.h>
int main()
{
// Initialize OpenGL and create a buffer
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
// Register the OpenGL buffer with HIP
hipGraphicsResource* resource;
hipGraphicsGLRegisterBuffer(&resource, buffer, hipGraphicsRegisterFlagsNone);
// Map the resource for access by HIP
hipGraphicsMapResources(1, &resource, 0);
// Obtain a pointer to the mapped array
hipArray* arrayPtr;
hipGraphicsSubResourceGetMappedArray(&arrayPtr, resource, 0, 0);
// Use arrayPtr in HIP kernels...
// Unmap the resources when done
hipGraphicsUnmapResources(1, &resource, 0);
// Cleanup OpenGL resources
glDeleteBuffers(1, &buffer);
return 0;
}
An other example is with mapping resources, getting pointers and unmapping
resources with a OpenGL image registration.

.. code-block:: cpp
#include <hip/hip_runtime.h>
#include <hip/hip_runtime_api.h>
#include <GL/glew.h>
#include <GL/gl.h>
int main()
{
// Initialize OpenGL and create a texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
// Register the OpenGL texture with HIP
hipGraphicsResource* resource;
hipGraphicsGLRegisterImage(&resource, texture, GL_TEXTURE_2D, hipGraphicsRegisterFlagsNone);
// Map the resource for access by HIP
hipGraphicsMapResources(1, &resource, 0);
// Obtain a pointer to the mapped array
hipArray* arrayPtr;
hipGraphicsSubResourceGetMappedArray(&arrayPtr, resource, 0, 0);
// Use arrayPtr in HIP kernels...
// Unmap the resources when done
hipGraphicsUnmapResources(1, &resource, 0);
// Cleanup OpenGL resources
glDeleteTextures(1, &texture);
return 0;
}
.. literalinclude:: ../../tools/example_codes/opengl_interop.hip
:start-after: // [Sphinx buffer register and get start]
:end-before: // [Sphinx buffer register and get end]
Loading

0 comments on commit 3547ab4

Please sign in to comment.