JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLBuffer.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 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;
30
31import com.jogamp.common.nio.Buffers;
32
33import java.util.List;
34
35import com.jogamp.common.nio.PointerBuffer;
36import com.jogamp.opencl.llb.CL;
37
38import java.nio.Buffer;
39import java.util.ArrayList;
40import java.util.Collections;
41
42/**
43 * OpenCL buffer object wrapping an optional NIO buffer.
44 * @author Michael Bien, et al.
45 */
46public class CLBuffer<B extends Buffer> extends CLMemory<B> {
47
48 private List<CLSubBuffer<B>> childs;
49
50 protected CLBuffer(final CLContext context, final long size, final long id, final int flags) {
51 this(context, null, size, id, flags);
52 }
53
54 protected CLBuffer(final CLContext context, final B directBuffer, final long size, final long id, final int flags) {
55 super(context, directBuffer, size, id, flags);
56 }
57
58 @SuppressWarnings("rawtypes")
59 static CLBuffer<?> create(final CLContext context, final int size, final int flags) {
60
61 if(isHostPointerFlag(flags)) {
62 throw new IllegalArgumentException("no host pointer defined");
63 }
64
65 final CL binding = context.getPlatform().getCLBinding();
66 final int[] result = new int[1];
67 final long id = binding.clCreateBuffer(context.ID, flags, size, null, result, 0);
68 CLException.checkForError(result[0], "can not create cl buffer");
69
70 return new CLBuffer(context, size, id, flags);
71 }
72
73 static <B extends Buffer> CLBuffer<B> create(final CLContext context, final B directBuffer, final int flags) {
74
75 if(!directBuffer.isDirect())
76 throw new IllegalArgumentException("buffer is not direct");
77
78 B host_ptr = null;
79 if(isHostPointerFlag(flags)) {
80 host_ptr = directBuffer;
81 }
82
83 final CL binding = context.getPlatform().getCLBinding();
84 final int[] result = new int[1];
85 final int size = Buffers.sizeOfBufferElem(directBuffer) * directBuffer.limit();
86 final long id = binding.clCreateBuffer(context.ID, flags, size, host_ptr, result, 0);
87 CLException.checkForError(result[0], "can not create cl buffer");
88
89 return new CLBuffer<B>(context, directBuffer, size, id, flags);
90 }
91
92 /**
93 * Creates a sub buffer with the specified region from this buffer.
94 * If this buffer contains a NIO buffer, the sub buffer will also contain a slice
95 * matching the specified region of the parent buffer. The region is specified
96 * by the offset and size in buffer elements or bytes if this buffer does not
97 * contain any NIO buffer.
98 * @param offset The offset in buffer elements.
99 * @param size The size in buffer elements.
100 */
101 public CLSubBuffer<B> createSubBuffer(int offset, int size, final Mem... flags) {
102
103 final B slice;
104 if(buffer != null) {
105 slice = Buffers.slice(buffer, offset, size);
106 final int elemSize = Buffers.sizeOfBufferElem(buffer);
107 offset *= elemSize;
108 size *= elemSize;
109 } else {
110 slice = null;
111 }
112
113 final PointerBuffer info = PointerBuffer.allocateDirect(2);
114 info.put(0, offset);
115 info.put(1, size);
116 final int bitset = Mem.flagsToInt(flags);
117
118 final CL binding = getPlatform().getCLBinding();
119 final int[] err = new int[1];
120 final long subID = binding.clCreateSubBuffer(ID, bitset, CL.CL_BUFFER_CREATE_TYPE_REGION, info.getBuffer(), err, 0);
121 CLException.checkForError(err[0], "can not create sub buffer");
122
123 final CLSubBuffer<B> clSubBuffer = new CLSubBuffer<B>(this, offset, size, slice, subID, bitset);
124 if(childs == null) {
125 childs = new ArrayList<CLSubBuffer<B>>();
126 }
127 childs.add(clSubBuffer);
128 return clSubBuffer;
129 }
130
131 @Override
132 public void release() {
133 if(childs != null) {
134 while(!childs.isEmpty()) {
135 childs.get(0).release();
136 }
137 }
138 super.release();
139 }
140
141 void onReleaseSubBuffer(final CLSubBuffer<?> sub) {
142 childs.remove(sub);
143 }
144
145 /**
146 * Returns the list of subbuffers.
147 */
148 @SuppressWarnings("unchecked")
149 public List<CLSubBuffer<B>> getSubBuffers() {
150 if(childs == null) {
151 return Collections.EMPTY_LIST;
152 }else{
153 return Collections.unmodifiableList(childs);
154 }
155 }
156
157 /**
158 * Returns true if this is a sub buffer.
159 */
160 public boolean isSubBuffer() {
161 return false;
162 }
163
164 @Override
165 public <T extends Buffer> CLBuffer<T> cloneWith(final T directBuffer) {
166 return new CLBuffer<T>(context, directBuffer, size, ID, FLAGS);
167 }
168
169}
OpenCL buffer object wrapping an optional NIO buffer.
Definition: CLBuffer.java:46
CLSubBuffer< B > createSubBuffer(int offset, int size, final Mem... flags)
Creates a sub buffer with the specified region from this buffer.
Definition: CLBuffer.java:101
List< CLSubBuffer< B > > getSubBuffers()
Returns the list of subbuffers.
Definition: CLBuffer.java:149
CLBuffer(final CLContext context, final long size, final long id, final int flags)
Definition: CLBuffer.java:50
CLBuffer(final CLContext context, final B directBuffer, final long size, final long id, final int flags)
Definition: CLBuffer.java:54
boolean isSubBuffer()
Returns true if this is a sub buffer.
Definition: CLBuffer.java:160
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.
Common superclass for all OpenCL memory types.
Definition: CLMemory.java:49
static boolean isHostPointerFlag(final int flags)
Returns true if a host pointer must be specified on mem object creation.
Definition: CLMemory.java:86
A sub buffer of a CLBuffer.
Memory settings for configuring CLMemory.
Definition: CLMemory.java:289
static int flagsToInt(final Mem[] flags)
Definition: CLMemory.java:382
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26
long clCreateSubBuffer(long buffer, long flags, int buffer_create_type, Buffer buffer_create_info, IntBuffer errcode_ret)
Interface to C language function: cl_mem {@native clCreateSubBuffer}(cl_mem buffer,...
static final int CL_BUFFER_CREATE_TYPE_REGION
Define "CL_BUFFER_CREATE_TYPE_REGION" with expression '0x1220', CType: int.
Definition: CL.java:589