aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/CLGLBuffer.java
blob: 40298ad7a2c88f350d309961d9a77742716ca2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.mbien.opencl;

import java.nio.Buffer;


import static com.mbien.opencl.CLGLI.*;

/**
 * Shared buffer between OpenGL and OpenCL contexts.
 * @author Michael Bien
 */
public final class CLGLBuffer<B extends Buffer> extends CLBuffer<B> {

    /**
     * The OpenGL object handle.
     */
    public final int GLID;

    private CLGLBuffer(CLContext context, B directBuffer, long id, int glObject) {
        super(context, directBuffer, id);
        this.GLID = glObject;
    }


    static <B extends Buffer> CLGLBuffer<B> create(CLContext context, B directBuffer, int flags, int glObject) {
        if(directBuffer != null && !directBuffer.isDirect())
            throw new IllegalArgumentException("buffer is not a direct buffer");

        if(isHostPointerFlag(flags)) {
            throw new IllegalArgumentException(
                    "CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers.");
        }
        
        CL cl = context.cl;
        int[] result = new int[1];
        CLGLI clgli = (CLGLI)cl;
        
        long id = clgli.clCreateFromGLBuffer(context.ID, flags, glObject, result, 0);

        return new CLGLBuffer<B>(context, directBuffer, id, glObject);
    }

    @Override
    public <T extends Buffer> CLGLBuffer<T> cloneWith(T directBuffer) {
        return new CLGLBuffer<T>(context, directBuffer, ID, GLID);
    }

    /**
     * Returns the OpenGL buffer type of this shared buffer.
     */
    public GLObjectType getGLObjectType() {
        int[] array = new int[1];
        int ret = ((CLGLI)cl).clGetGLObjectInfo(ID, array, 0, null, 0);
        CLException.checkForError(ret, "error while asking for gl object info");
        return GLObjectType.valueOf(array[0]);
    }

    /**
     * Returns the OpenGL object id of this shared buffer.
     */
    public int getGLObjectID() {
        int[] array = new int[1];
        int ret = ((CLGLI)cl).clGetGLObjectInfo(ID, null, 0, array, 0);
        CLException.checkForError(ret, "error while asking for gl object info");
        return array[0];
    }

    @Override
    public String toString() {
        return "CLMemory [id: " + ID+" glID: "+GLID+"]";
    }

    public enum GLObjectType {

        GL_OBJECT_BUFFER(CL_GL_OBJECT_BUFFER),
        GL_OBJECT_TEXTURE2D(CL_GL_OBJECT_TEXTURE2D),
        GL_OBJECT_TEXTURE3D(CL_GL_OBJECT_TEXTURE3D),
        GL_OBJECT_RENDERBUFFER(CL_GL_OBJECT_RENDERBUFFER);

        public final int TYPE;

        private GLObjectType(int type) {
            this.TYPE = type;
        }

        public static GLObjectType valueOf(int type) {
            if(type == CL_GL_OBJECT_BUFFER)
                return GL_OBJECT_BUFFER;
            else if(type == CL_GL_OBJECT_TEXTURE2D)
                return GL_OBJECT_TEXTURE2D;
            else if(type == CL_GL_OBJECT_TEXTURE3D)
                return GL_OBJECT_TEXTURE3D;
            else if(type == CL_GL_OBJECT_RENDERBUFFER)
                return GL_OBJECT_RENDERBUFFER;
            return null;
        }
    }

}