GPUOpen-LibrariesAndSDKs / Orochi

MIT License
196 stars 30 forks source link

Register OpenGL buffer for interoperability? #90

Open TomClabault opened 4 months ago

TomClabault commented 4 months ago

How can I register an OpenGL buffer for use in my CUDA/HIP kernel?

oroGraphicsMapResources(int count, oroGraphicsResource_t * resources, oroStream_t stream) expects a registered resource as its second parameter but there is no oroGraphicsRegisterX functions (although oroGraphicsUnregisterResource exists). I would expect the oroGraphicsGLRegisterBuffer and oroGraphicsGLRegisterImage functions to be present in Orochi API but there are no such functions.

The DX12 example of this repo doesn't really help as it uses a sharedHandle created specifically with the DX12 API:

oroExternalMemoryHandleDesc externalMemoryHandleDesc;
externalMemoryHandleDesc.type = oroExternalMemoryHandleTypeD3D12Resource;
externalMemoryHandleDesc.handle.win32.handle = sharedHandle;
externalMemoryHandleDesc.size = actualSize;
externalMemoryHandleDesc.flags = ORO_EXTERNAL_MEMORY_DEDICATED;

oroImportExternalMemory(&gOroExtMem, &externalMemoryHandleDesc);

Is it somehow possible to adapt this example for OpenGL buffers or is there something simpler I missed in Orochi's API?

TomClabault commented 4 months ago

I tried doing it with HIP (not Orochi), but I still cannot get past hipGraphicsGLRegisterBuffer:

#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include "Orochi/Orochi.h"

#include <iostream>

int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    GLFWwindow* window = glfwCreateWindow(1280, 720, "Test OpenGL Interop", NULL, NULL);
    glfwMakeContextCurrent(window);

    glewInit();

    oroInitialize(oroApi::ORO_API_HIP, 0);
    oroInit(0);

    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 1024, nullptr, GL_DYNAMIC_DRAW);

    hipGraphicsResource* buffer_resource;
    if (hipGraphicsGLRegisterBuffer(&buffer_resource, buffer, hipGraphicsRegisterFlagsNone) != hipSuccess)
    {
        std::cerr << hipGetErrorString(hipGetLastError()) << std::endl;
        return 3;
    }

    return 0;
}

The above piece of code prints "invalid argument" as hipGraphicsGLRegisterBuffer returns an hipErrorInvalidValue error.

I also tried textures with hipGraphicsGLRegisterImage but I got the exact same issue.

EDIT:

Calling hipGLGetDevices before hipGraphicsGLRegisterImage has the RegisterImage call working for this simple example main(). What is happening?