JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLGLBuffer.java
Go to the documentation of this file.
1/*
2 * Copyright 2009 - 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl.gl;
30
31import com.jogamp.opencl.CLBuffer;
32import com.jogamp.opencl.CLCommandQueue;
33import com.jogamp.opencl.CLContext;
34import com.jogamp.opencl.CLException;
35import com.jogamp.opencl.llb.CL;
36
37import java.nio.Buffer;
38import com.jogamp.opengl.GLContext;
39
40
41/**
42 * Shared buffer between OpenGL and OpenCL contexts.
43 * @author Michael Bien, et.al.
44 */
45public final class CLGLBuffer<B extends Buffer> extends CLBuffer<B> implements CLGLObject {
46
47
48 /**
49 * The OpenGL object handle.
50 */
51 public final int GLID;
52
53 private CLGLBuffer(final CLContext context, final B directBuffer, final long id, final int glObject, final long size, final int flags) {
54 super(context, directBuffer, size, id, flags);
55 this.GLID = glObject;
56 }
57
58
59 static <B extends Buffer> CLGLBuffer<B> create(final CLContext context, final B directBuffer, final long size, final int flags, final int glBuffer) {
60 checkBuffer(directBuffer, flags);
61
62 final CL cl = getCL(context);
63 final int[] result = new int[1];
64 final long id = cl.clCreateFromGLBuffer(context.ID, flags, glBuffer, result, 0);
65 CLException.checkForError(result[0], "can not create CLGLObject from glBuffer #"+glBuffer);
66
67 return new CLGLBuffer<B>(context, directBuffer, id, glBuffer, size, flags);
68 }
69
70 static <B extends Buffer> void checkBuffer(final B directBuffer, final int flags) throws IllegalArgumentException {
71 if (directBuffer != null && !directBuffer.isDirect()) {
72 throw new IllegalArgumentException("buffer is not a direct buffer");
73 }
74 if (isHostPointerFlag(flags)) {
75 throw new IllegalArgumentException("CL_MEM_COPY_HOST_PTR or CL_MEM_USE_HOST_PTR can not be used with OpenGL Buffers.");
76 }
77 }
78
79 /**
80 * Updates the size of this CLGLBuffer by querying OpenGL.
81 * This method may only be called if this memory object has been acquired by calling
82 * {@link CLCommandQueue#putAcquireGLObject(com.jogamp.opencl.gl.CLGLObject)}.
83 */
84 public void updateSize() {
85 size = getSizeImpl(context, ID);
87 }
88
89 @Override
90 public int getGLObjectID() {
91 return GLID;
92 }
93
94 @Override
97 }
98
99 @Override
101 return (CLGLContext) super.getContext();
102 }
103
104 @Override
105 public GLContext getGLContext() {
106 return getContext().getGLContext();
107 }
108
109 @Override
110 public <T extends Buffer> CLGLBuffer<T> cloneWith(final T directBuffer) {
111 return new CLGLBuffer<T>(context, directBuffer, ID, GLID, size, FLAGS);
112 }
113
114 @Override
115 public String toString() {
116 return getClass().getSimpleName()+" [id: " + ID+" glID: "+GLID+"]";
117 }
118
119}
OpenCL buffer object wrapping an optional NIO buffer.
Definition: CLBuffer.java:46
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
Main Exception type for runtime OpenCL errors and failed function calls (e.g.
static void checkForError(final int status, final String message)
Throws a CLException when status != CL_SUCCESS.
static CL getCL(final CLContext context)
Definition: CLMemory.java:99
static boolean isHostPointerFlag(final int flags)
Returns true if a host pointer must be specified on mem object creation.
Definition: CLMemory.java:86
static long getSizeImpl(final CLContext context, final long id)
Definition: CLMemory.java:91
final long ID
The OpenCL object handle.
Definition: CLObject.java:41
Shared buffer between OpenGL and OpenCL contexts.
Definition: CLGLBuffer.java:45
int getGLObjectID()
Returns the OpenGL object id of this shared object.
Definition: CLGLBuffer.java:90
GLObjectType getGLObjectType()
Returns the OpenGL buffer type of this shared object.
Definition: CLGLBuffer.java:95
CLGLContext getContext()
Returns the OpenCL context of this shared object.
void updateSize()
Updates the size of this CLGLBuffer by querying OpenGL.
Definition: CLGLBuffer.java:84
GLContext getGLContext()
Returns the OpenGL context of this shared object.
final int GLID
The OpenGL object handle.
Definition: CLGLBuffer.java:51
OpenCL Context supporting JOGL-JOCL interoperablity.
GLContext getGLContext()
Returns the OpenGL context this context was shared with.
CLGLContext getContext()
Returns the context for this OpenCL object.
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26
long clCreateFromGLBuffer(long context, long flags, int bufobj, IntBuffer errcode_ret)
Interface to C language function: cl_mem {@native clCreateFromGLBuffer}(cl_context context,...