JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
GLUniformData.java
Go to the documentation of this file.
1/**
2 * Copyright 2009-2023 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 */
28package com.jogamp.opengl;
29
30import java.nio.Buffer;
31import java.nio.FloatBuffer;
32import java.nio.IntBuffer;
33
34import com.jogamp.common.nio.Buffers;
35import com.jogamp.math.FloatUtil;
36import com.jogamp.math.util.SyncAction;
37import com.jogamp.math.util.SyncBuffer;
38
39/**
40 * GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
41 */
42public final class GLUniformData {
43
44 /**
45 * int atom
46 *
47 * Number of objects is 1
48 *
49 * @param name the uniform name as used in the shader
50 */
51 public GLUniformData(final String name, final int val) {
52 initScalar(name, 1, Integer.valueOf(val));
53 }
54
55 /**
56 * float atom
57 *
58 * Number of objects is 1
59 *
60 * @param name the uniform name as used in the shader
61 */
62 public GLUniformData(final String name, final float val) {
63 initScalar(name, 1, Float.valueOf(val));
64 }
65
66 /**
67 * Multiple IntBuffer Vector
68 *
69 * Number of objects is calculated by data.limit()/components
70 *
71 * @param name the uniform name as used in the shader
72 * @param components number of elements of one object, ie 4 for GL_FLOAT_VEC4,
73 * @param data the data
74 */
75 public GLUniformData(final String name, final int components, final IntBuffer data) {
76 initBuffer(name, components, data, null);
77 }
78
79 /**
80 * Multiple FloatBuffer Vector
81 *
82 * Number of objects is calculated by data.limit()/components
83 *
84 * @param name the uniform name as used in the shader
85 * @param components number of elements of one object, ie 4 for GL_FLOAT_VEC4,
86 * @param data the underlying data
87 */
88 public GLUniformData(final String name, final int components, final FloatBuffer data) {
89 initBuffer(name, components, data, null);
90 }
91
92 /**
93 * Multiple IntBuffer or FloatBuffer Vector
94 *
95 * Number of objects is calculated by data.limit()/components
96 *
97 * @param name the uniform name as used in the shader
98 * @param components number of elements of one object, ie 4 for GL_FLOAT_VEC4,
99 * @param syncBuffer {@link SyncBuffer} providing {@link SyncAction} and {@link Buffer}, allowing to sync the buffer with the underlying data, see {@link #getBuffer()}
100 */
101 public GLUniformData(final String name, final int components, final SyncBuffer syncBuffer) {
102 initBuffer(name, components, syncBuffer.getBuffer(), syncBuffer.getAction());
103 }
104
105 private GLUniformData(final int components, final String name) {
106 initBuffer(name, components, null, null);
107 }
108
109 public static GLUniformData creatEmptyVector(final String name, final int components) {
110 return new GLUniformData(components, name);
111 }
112
113 public static GLUniformData creatEmptyMatrix(final String name, final int rows, final int columns) {
114 return new GLUniformData(name, rows, columns, (FloatBuffer)null);
115 }
116
117 /**
118 * Multiple FloatBuffer Matrix
119 *
120 * Number of objects is calculated by data.limit()/(rows*columns)
121 *
122 * @param name the uniform name as used in the shader
123 * @param rows the matrix rows
124 * @param column the matrix column
125 * @param data the underlying data
126 */
127 public GLUniformData(final String name, final int rows, final int columns, final FloatBuffer data) {
128 initBuffer(name, rows, columns, data, null);
129 }
130
131 /**
132 * Multiple FloatBuffer Matrix
133 *
134 * Number of objects is calculated by data.limit()/(rows*columns)
135 *
136 * @param name the uniform name as used in the shader
137 * @param rows the matrix rows
138 * @param column the matrix column
139 * @param syncBuffer {@link SyncBuffer} providing {@link SyncAction} and {@link Buffer}, allowing to sync the buffer with the underlying data, see {@link #getBuffer()}
140 */
141 public GLUniformData(final String name, final int rows, final int columns, final SyncBuffer syncBuffer) {
142 initBuffer(name, rows, columns, syncBuffer.getBuffer(), syncBuffer.getAction());
143 }
144
145 public GLUniformData setData(final int data) { initScalar(Integer.valueOf(data)); return this; }
146 public GLUniformData setData(final float data) { initScalar(Float.valueOf(data)); return this; }
147
148 public GLUniformData setData(final IntBuffer data) { initBuffer(data, null); return this; }
149 public GLUniformData setData(final FloatBuffer data) { initBuffer(data, null); return this; }
150 public GLUniformData setData(final SyncBuffer syncedBuffer) { initBuffer(syncedBuffer.getBuffer(), syncedBuffer.getAction()); return this; }
151
152 public int intValue() { return ((Integer)data).intValue(); };
153 public float floatValue() { return ((Float)data).floatValue(); };
154 public IntBuffer intBufferValue() { return (IntBuffer)data; };
155 public FloatBuffer floatBufferValue() { return (FloatBuffer)data; };
156
157 @SuppressWarnings("deprecation")
158 public StringBuilder toString(StringBuilder sb) {
159 if(null == sb) {
160 sb = new StringBuilder();
161 }
162 sb.append("GLUniformData[name ").append(name).
163 append(", location ").append(location).
164 append(", size ").append(rows).append("x").append(columns).
165 append(", count ").append(count).
166 append(", data ");
167 if(isMatrix() && data instanceof FloatBuffer) {
168 sb.append("\n");
169 final FloatBuffer fb = (FloatBuffer)getBuffer();
170 for(int i=0; i<count; i++) {
171 FloatUtil.matrixToString(sb, i+": ", "%10.5f", fb, i*rows*columns, rows, columns, false);
172 sb.append(",\n");
173 }
174 } else if(isBuffer()) {
175 Buffers.toString(sb, null, getBuffer());
176 } else {
177 sb.append(data);
178 }
179 sb.append("]");
180 return sb;
181 }
182
183 @Override
184 public String toString() {
185 return toString(null).toString();
186 }
187
188 private void initBuffer(final String name, final int rows, final int columns, final Buffer buffer, final SyncAction syncAction) {
189 if( 2>rows || rows>4 || 2>columns || columns>4 ) {
190 throw new GLException("rowsXcolumns must be within [2..4]X[2..4], is: "+rows+"X"+columns);
191 }
192 this.name=name;
193 this.rows=rows;
194 this.columns=columns;
195 this.bits=BIT_MATRIX;
196 this.location=-1;
197 initBuffer(buffer, syncAction);
198 }
199 private void initScalar(final String name, final int components, final Object data) {
200 if( 1>components || components>4 ) {
201 throw new GLException("components must be within [1..4], is: "+components);
202 }
203 this.name=name;
204 this.columns=components;
205 this.rows=1;
206 this.bits=0;
207 this.location=-1;
208 initScalar(data);
209 }
210 private void initBuffer(final String name, final int components, final Buffer buffer, final SyncAction syncAction) {
211 if( 1>components || components>4 ) {
212 throw new GLException("components must be within [1..4], is: "+components);
213 }
214 this.name=name;
215 this.columns=components;
216 this.rows=1;
217 this.bits=0;
218 this.location=-1;
219 initBuffer(buffer, syncAction);
220 }
221
222 private void initScalar(final Object data) {
223 if(data instanceof Buffer) {
224 initBuffer((Buffer)data, null);
225 } else if( null != data ) {
226 if( isMatrix() ) {
227 throw new GLException("Atom type not allowed for matrix : "+this);
228 }
229 this.count=1;
230 this.data=data;
231 } else {
232 this.count=0;
233 this.data=data;
234 }
235 }
236
237 private void initBuffer(final Buffer buffer, final SyncAction syncAction) {
238 if( null != buffer ) {
239 this.bits |= BIT_BUFFER;
240 final int sz = rows*columns;
241 if(buffer.remaining()<sz || 0!=buffer.remaining()%sz) {
242 throw new GLException("remaining data buffer size invalid: buffer: "+buffer.toString()+"\n\t"+this);
243 }
244 this.count=buffer.remaining()/sz;
245 this.data=buffer;
246 } else {
247 this.count=0;
248 this.data=null;
249 }
250 this.syncAction = syncAction;
251 }
252
253 /** Return the uniform name as used in the shader */
254 public String getName() { return name; }
255
256 public int getLocation() { return location; }
257
258 /**
259 * Sets the given location of the shader uniform.
260 * @return the given location
261 */
262 public int setLocation(final int location) { this.location=location; return location; }
263
264 /**
265 * Retrieves the location of the shader uniform with {@link #getName()} from the linked shader program.
266 * <p>
267 * No validation is performed within the implementation.
268 * </p>
269 * @param gl
270 * @param program
271 * @return &ge;0 denotes a valid uniform location as found and used in the given shader program.
272 * &lt;0 denotes an invalid location, i.e. not found or used in the given shader program.
273 */
274 public int setLocation(final GL2ES2 gl, final int program) {
275 location = gl.glGetUniformLocation(program, name);
276 return location;
277 }
278
279 /**
280 * Returns the data object.
281 * <p>
282 * In case a {@link SyncAction} has been set,
283 * it is invoked to {@link SyncAction#sync() synchronize} the object with the underlying data before returning the object.
284 * </p>
285 * @return the data object.
286 * @see SyncAction#sync()
287 */
288 public Object getObject() {
289 if( null != syncAction ) {
290 syncAction.sync();
291 }
292 return data;
293 }
294
295 /**
296 * Returns the data buffer.
297 * <p>
298 * In case a {@link SyncAction} has been set,
299 * it is invoked to {@link SyncAction#sync() synchronize} the buffer with the underlying data before returning the buffer.
300 * </p>
301 * @return the data buffer.
302 * @see SyncAction#sync()
303 */
304 public Buffer getBuffer() {
305 if( null != syncAction ) {
306 syncAction.sync();
307 }
308 return (data instanceof Buffer)?(Buffer)data:null;
309 }
310
311 public boolean isMatrix() { return 0 != ( BIT_MATRIX & bits ); }
312 public boolean isBuffer() { return 0 != ( BIT_BUFFER & bits ); }
313
314 public int count() { return count; }
315 public int components() { return rows*columns; }
316 public int rows() { return rows; }
317 public int columns() { return columns; }
318
319 private static final short BIT_MATRIX = 0b0000000000000001;
320 private static final short BIT_BUFFER = 0b0000000000000010;
321
322 private String name;
323 private int location;
324 private int rows, columns;
325 private int count;
326 private Object data;
327 private short bits;
328 private SyncAction syncAction;
329}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static StringBuilder matrixToString(StringBuilder sb, final String rowPrefix, final String f, final FloatBuffer a, final int aOffset, final int rows, final int columns, final boolean rowMajorOrder)
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
GLSL uniform data wrapper encapsulating data to be uploaded to the GPU as a uniform.
static GLUniformData creatEmptyVector(final String name, final int components)
GLUniformData(final String name, final int components, final FloatBuffer data)
Multiple FloatBuffer Vector.
GLUniformData(final String name, final float val)
float atom
GLUniformData(final String name, final int val)
int atom
static GLUniformData creatEmptyMatrix(final String name, final int rows, final int columns)
GLUniformData(final String name, final int components, final IntBuffer data)
Multiple IntBuffer Vector.
Buffer getBuffer()
Returns the data buffer.
String getName()
Return the uniform name as used in the shader.
GLUniformData setData(final float data)
GLUniformData(final String name, final int rows, final int columns, final SyncBuffer syncBuffer)
Multiple FloatBuffer Matrix.
Object getObject()
Returns the data object.
GLUniformData(final String name, final int rows, final int columns, final FloatBuffer data)
Multiple FloatBuffer Matrix.
GLUniformData setData(final SyncBuffer syncedBuffer)
int setLocation(final GL2ES2 gl, final int program)
Retrieves the location of the shader uniform with getName() from the linked shader program.
int setLocation(final int location)
Sets the given location of the shader uniform.
GLUniformData setData(final int data)
GLUniformData(final String name, final int components, final SyncBuffer syncBuffer)
Multiple IntBuffer or FloatBuffer Vector.
GLUniformData setData(final FloatBuffer data)
GLUniformData setData(final IntBuffer data)
Specific data synchronization action implemented by the data provider to update the buffer with the u...
Definition: SyncAction.java:38
Convenient tuple of a SyncAction and Buffer.
Definition: SyncBuffer.java:40
Buffer getBuffer()
Return the Buffer, i.e.
SyncAction getAction()
Return the SyncAction.
int glGetUniformLocation(int program, String name)
Entry point to C language function: GLint {@native glGetUniformLocation}(GLuint program,...