aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/mbien/opencl/gl/CLGLBuffer.java
blob: 97a25626ec467d1066960d9d84a6c2b02af0298f (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
package com.mbien.opencl.gl;

import com.mbien.opencl.CL;
import com.mbien.opencl.CLBuffer;
import com.mbien.opencl.CLContext;
import com.mbien.opencl.CLMemory.GLObjectType;
import java.nio.Buffer;
import javax.media.opengl.GLContext;


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


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

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


    static <B extends Buffer> CLGLBuffer<B> create(CLContext context, B directBuffer, int flags, int glObject) {
        checkBuffer(directBuffer, flags);
        
        CL cl = getCL(context);
        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, flags);
    }

    static <B extends Buffer> void checkBuffer(B directBuffer, int flags) throws IllegalArgumentException {
        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.");
        }
    }

    public int getGLObjectID() {
        return GLID;
    }

    public GLObjectType getGLObjectType() {
        return GLObjectType.GL_OBJECT_BUFFER;
    }

    @Override
    public CLGLContext getContext() {
        return (CLGLContext) super.getContext();
    }

    public GLContext getGLContext() {
        return getContext().getGLContext();
    }

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

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

}