Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Jocl中使用cl_khr_gl_共享?_Java_Opengl_Opencl_Jogl_Jocl - Fatal编程技术网

Java 如何在Jocl中使用cl_khr_gl_共享?

Java 如何在Jocl中使用cl_khr_gl_共享?,java,opengl,opencl,jogl,jocl,Java,Opengl,Opencl,Jogl,Jocl,我想写实时光线跟踪器。 我使用OpenGL和OpenCL的Java和Jogamp绑定(称为Jogl和Jocl)。 我的.cl内核中已经有了光线跟踪代码,它运行得很好。我将输出作为FloatBuffer,并通过glTexImage2D将其传递给OpenGL纹理。现在我想实现实时,为了实现这一点,我想删除程序中两次发生的FloatBuffer复制(第一次-从OpenCL内核结果到RAM,第二次从RAM到OpenGL纹理)。显然有一种方法可以直接从OpenGL纹理指向OpenCL缓冲区,因为所有的计算

我想写实时光线跟踪器。 我使用OpenGL和OpenCL的Java和Jogamp绑定(称为Jogl和Jocl)。 我的.cl内核中已经有了光线跟踪代码,它运行得很好。我将输出作为FloatBuffer,并通过glTexImage2D将其传递给OpenGL纹理。现在我想实现实时,为了实现这一点,我想删除程序中两次发生的FloatBuffer复制(第一次-从OpenCL内核结果到RAM,第二次从RAM到OpenGL纹理)。显然有一种方法可以直接从OpenGL纹理指向OpenCL缓冲区,因为所有的计算都是在GPU上进行的。
我知道OpenCL有cl\u khr\u gl\u共享扩展,可以满足我的需求。但我不明白如何在JavaJogamp绑定(jocl/jogl)中使用它。有人能帮助我还是提供一些java代码(不是C++,它的细节有什么不同)?

,所以经过几天的研究,我发现了如何做。为任何感兴趣的人发布答案

在Jogl的GLEventListener的“init”方法中创建GL上下文。您还必须在该方法中创建CL上下文。 我的示例代码如下:

public void init(GLAutoDrawable drawable) {
    GL4 gl4 = drawable.getGL().getGL4();

    gl4.glDisable(GL4.GL_DEPTH_TEST);
    gl4.glEnable(GL4.GL_CULL_FACE);
    gl4.glCullFace(GL4.GL_BACK);
    buildScreenVAO(gl4);

    FloatBuffer pixelBuffer = GLBuffers.newDirectFloatBuffer(width * height * 4);
    this.textureIndex = GLUtils.initTexture(gl4, width, height, pixelBuffer);
    this.samplerIndex = GLUtils.initSimpleSampler(gl4);

    if (clContext == null) {
        try {
            gl4.glFinish();
            this.clContext = CLGLContext.create(gl4.getContext());
            this.clDevice = clContext.getMaxFlopsDevice();
            //if (device.getExtensions().contains("cl_khr_gl_sharing"))
            this.clCommandQueue = clDevice.createCommandQueue();

            this.clProgram = clContext.createProgram(new FileInputStream(new File(ResourceLocator.getInstance().kernelsPath + "raytracer.cl"))).build(); // load sources, create and build program
            this.clKernel = clProgram.createCLKernel("main");

            this.clTexture = (CLGLTexture2d<FloatBuffer>) clContext.createFromGLTexture2d(GL4.GL_TEXTURE_2D, textureIndex, 0, Mem.WRITE_ONLY);
            this.viewTransform = clContext.createFloatBuffer(16 * 4, Mem.READ_ONLY);
            this.w = clContext.createFloatBuffer(1, Mem.READ_ONLY);

            clKernel.putArg(clTexture).putArg(width).putArg(height).putArg(viewTransform).putArg(w);
            fillViewTransform(viewTransform);
            fillW(w);

            clCommandQueue.putWriteBuffer(viewTransform, false);
            clCommandQueue.putWriteBuffer(w, false);
            clCommandQueue.putAcquireGLObject(clTexture);
            clCommandQueue.put1DRangeKernel(clKernel, 0, width * height, 0);
            clCommandQueue.putReleaseGLObject(clTexture);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    buildShaderProgram(gl4);
    bindObjects(gl4); 
}
最后,您必须在OpenCL内核中使用正确的纹理参数数据类型。在我的例子中,内核方法具有以下签名:

kernel void main(write_only image2d_t dst, const uint width, const uint height, global float* viewTransform, global float* w){                                                                            
我在OpenCL方法中使用write_imagef build将浮点数据(0.0f-1.0f)写入该纹理


如果您感兴趣,请随时询问我有关此方法的信息。

提示:您调查过opengl计算着色器吗?因为它非常适合你的案子你看过Github上的jogl演示吗?
kernel void main(write_only image2d_t dst, const uint width, const uint height, global float* viewTransform, global float* w){