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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package jogamp.graph.curve.opengl;
import java.nio.FloatBuffer;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL;
import javax.media.opengl.GLUniformData;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import jogamp.graph.curve.opengl.shader.AttributeNames;
import jogamp.graph.curve.opengl.shader.UniformNames;
import com.jogamp.common.nio.Buffers;
import com.jogamp.graph.curve.Region;
import com.jogamp.graph.curve.opengl.GLRegion;
import com.jogamp.graph.curve.opengl.RegionRenderer;
import com.jogamp.graph.curve.opengl.RenderState;
import com.jogamp.opengl.FBObject;
import com.jogamp.opengl.FBObject.Attachment;
import com.jogamp.opengl.FBObject.TextureAttachment;
import com.jogamp.opengl.math.geom.AABBox;
import com.jogamp.opengl.util.GLArrayDataServer;
import com.jogamp.opengl.util.PMVMatrix;
import com.jogamp.opengl.util.glsl.ShaderState;
public class VBORegion2PVBAAES2 extends GLRegion {
private static final boolean DEBUG_FBO_1 = false;
private static final boolean DEBUG_FBO_2 = false;
private GLArrayDataServer verticeTxtAttr;
private GLArrayDataServer texCoordTxtAttr;
private GLArrayDataServer indicesTxtBuffer;
private GLArrayDataServer verticeFboAttr;
private GLArrayDataServer texCoordFboAttr;
private GLArrayDataServer indicesFbo;
private FBObject fbo;
private TextureAttachment texA;
private final PMVMatrix fboPMVMatrix;
GLUniformData mgl_fboPMVMatrix;
private int fboWidth = 0;
private int fboHeight = 0;
GLUniformData mgl_ActiveTexture;
GLUniformData mgl_TextureSize;
final int[] maxTexSize = new int[] { -1 } ;
public VBORegion2PVBAAES2(final int renderModes, final int textureUnit) {
super(renderModes);
final int initialElementCount = 256;
fboPMVMatrix = new PMVMatrix();
mgl_fboPMVMatrix = new GLUniformData(UniformNames.gcu_PMVMatrix, 4, 4, fboPMVMatrix.glGetPMvMatrixf());
mgl_ActiveTexture = new GLUniformData(UniformNames.gcu_TextureUnit, textureUnit);
indicesTxtBuffer = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, initialElementCount, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER);
verticeTxtAttr = GLArrayDataServer.createGLSL(AttributeNames.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT,
false, initialElementCount, GL.GL_STATIC_DRAW);
texCoordTxtAttr = GLArrayDataServer.createGLSL(AttributeNames.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT,
false, initialElementCount, GL.GL_STATIC_DRAW);
}
@Override
protected final void clearImpl(final GL2ES2 gl, final RegionRenderer renderer) {
if(DEBUG_INSTANCE) {
System.err.println("VBORegion2PES2 Clear: " + this);
// Thread.dumpStack();
}
if( null != indicesTxtBuffer ) {
indicesTxtBuffer.seal(gl, false);
indicesTxtBuffer.rewind();
}
if( null != verticeTxtAttr ) {
verticeTxtAttr.seal(gl, false);
verticeTxtAttr.rewind();
}
if( null != texCoordTxtAttr ) {
texCoordTxtAttr.seal(gl, false);
texCoordTxtAttr.rewind();
}
}
@Override
protected final void pushVertex(float[] coords, float[] texParams) {
verticeTxtAttr.putf(coords[0]);
verticeTxtAttr.putf(coords[1]);
verticeTxtAttr.putf(coords[2]);
texCoordTxtAttr.putf(texParams[0]);
texCoordTxtAttr.putf(texParams[1]);
}
@Override
protected final void pushIndex(int idx) {
indicesTxtBuffer.puts((short)idx);
}
@Override
protected void updateImpl(final GL2ES2 gl, final RegionRenderer renderer) {
if(null == indicesFbo) {
final ShaderState st = renderer.getShaderState();
indicesFbo = GLArrayDataServer.createData(3, GL2ES2.GL_SHORT, 2, GL.GL_STATIC_DRAW, GL.GL_ELEMENT_ARRAY_BUFFER);
indicesFbo.puts((short) 0); indicesFbo.puts((short) 1); indicesFbo.puts((short) 3);
indicesFbo.puts((short) 1); indicesFbo.puts((short) 2); indicesFbo.puts((short) 3);
indicesFbo.seal(true);
texCoordFboAttr = GLArrayDataServer.createGLSL(AttributeNames.TEXCOORD_ATTR_NAME, 2, GL2ES2.GL_FLOAT,
false, 4, GL.GL_STATIC_DRAW);
st.ownAttribute(texCoordFboAttr, true);
texCoordFboAttr.putf(0); texCoordFboAttr.putf(0);
texCoordFboAttr.putf(0); texCoordFboAttr.putf(1);
texCoordFboAttr.putf(1); texCoordFboAttr.putf(1);
texCoordFboAttr.putf(1); texCoordFboAttr.putf(0);
texCoordFboAttr.seal(true);
verticeFboAttr = GLArrayDataServer.createGLSL(AttributeNames.VERTEX_ATTR_NAME, 3, GL2ES2.GL_FLOAT,
false, 4, GL.GL_STATIC_DRAW);
st.ownAttribute(verticeFboAttr, true);
st.ownAttribute(verticeTxtAttr, true);
st.ownAttribute(texCoordTxtAttr, true);
if(Region.DEBUG_INSTANCE) {
System.err.println("VBORegion2PVBAAES2 Create: " + this);
}
}
// seal buffers
indicesTxtBuffer.seal(gl, true);
indicesTxtBuffer.enableBuffer(gl, false);
texCoordTxtAttr.seal(gl, true);
texCoordTxtAttr.enableBuffer(gl, false);
verticeTxtAttr.seal(gl, true);
verticeTxtAttr.enableBuffer(gl, false);
// update all bbox related data
verticeFboAttr.seal(gl, false);
verticeFboAttr.rewind();
verticeFboAttr.putf(box.getMinX()); verticeFboAttr.putf(box.getMinY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMinX()); verticeFboAttr.putf(box.getMaxY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMaxX()); verticeFboAttr.putf(box.getMaxY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMaxX()); verticeFboAttr.putf(box.getMinY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.seal(gl, true);
verticeFboAttr.enableBuffer(gl, false);
fboPMVMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
fboPMVMatrix.glLoadIdentity();
fboPMVMatrix.glOrthof(box.getMinX(), box.getMaxX(), box.getMinY(), box.getMaxY(), -1, 1);
// push data 2 GPU ..
indicesFbo.seal(gl, true);
indicesFbo.enableBuffer(gl, false);
// trigger renderRegion2FBO !
fboHeight = 0;
fboWidth = 0;
// the buffers were disabled, since due to real/fbo switching and other vbo usage
}
private final AABBox drawWinBox = new AABBox();
private final int[] drawView = new int[] { 0, 0, 0, 0 };
private final float[] drawTmpV3 = new float[3];
@Override
protected void drawImpl(final GL2ES2 gl, final RegionRenderer renderer, final int[/*1*/] sampleCount) {
if( 0 >= indicesTxtBuffer.getElementCount() ) {
if(DEBUG_INSTANCE) {
System.err.printf("VBORegion2PVBAAES2.drawImpl: Empty%n");
}
return; // empty!
}
if( Float.isInfinite(box.getWidth()) || Float.isInfinite(box.getHeight()) ) {
if(DEBUG_INSTANCE) {
System.err.printf("VBORegion2PVBAAES2.drawImpl: Inf %s%n", box);
}
return; // inf
}
final int width = renderer.getWidth();
final int height = renderer.getHeight();
if(width <=0 || height <= 0 || null==sampleCount || sampleCount[0] <= 0){
renderRegion(gl);
} else {
if(0 > maxTexSize[0]) {
gl.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE, maxTexSize, 0);
}
final RenderState rs = renderer.getRenderState();
final float winWidth, winHeight;
int targetFboWidth, targetFboHeight;
float renderFboWidth, renderFboHeight;
float diffWidth, diffHeight;
{
// Calculate perspective pixel width/height for FBO,
// considering the sampleCount.
drawView[2] = width;
drawView[3] = height;
box.mapToWindow(drawWinBox, renderer.getMatrix(), drawView, true /* useCenterZ */, drawTmpV3);
winWidth = drawWinBox.getWidth();
winHeight = drawWinBox.getHeight();
diffWidth = (float)Math.ceil(winWidth)-winWidth;
diffHeight = (float)Math.ceil(winHeight)-winHeight;
renderFboWidth = winWidth*sampleCount[0];
renderFboHeight = winHeight*sampleCount[0];
targetFboWidth = (int)Math.ceil(renderFboWidth);
targetFboHeight = (int)Math.ceil(renderFboHeight);
if( DEBUG_FBO_2 ) {
System.err.printf("XXX.MinMax obj %s%n", box.toString());
System.err.printf("XXX.MinMax win %s%n", drawWinBox.toString());
System.err.printf("XXX.MinMax view[%d, %d] -> win[%.3f, %.3f]: FBO f[%.3f, %.3f], i[%d x %d], d[%.3f, %.3f], msaa %d%n",
drawView[2], drawView[3],
winWidth, winHeight,
renderFboWidth, renderFboHeight, targetFboWidth, targetFboHeight,
diffWidth, diffHeight, sampleCount[0]);
}
}
if( 0 >= targetFboWidth || 0 >= targetFboHeight ) {
// Nothing ..
return;
}
final int deltaFboWidth = Math.abs(targetFboWidth-fboWidth);
final int deltaFboHeight = Math.abs(targetFboHeight-fboHeight);
final int maxDeltaFbo, maxLengthFbo;
if( deltaFboWidth >= deltaFboHeight ) {
maxDeltaFbo = deltaFboWidth;
maxLengthFbo = fboWidth > 0 ? fboWidth : 1;
} else {
maxDeltaFbo = deltaFboHeight;
maxLengthFbo = fboHeight > 0 ? fboHeight : 1;
}
final float pctFboDelta = (float)maxDeltaFbo / (float)maxLengthFbo;
if( DEBUG_FBO_2 ) {
System.err.printf("XXX.maxDelta: %d / %d = %.3f%n", maxDeltaFbo, maxLengthFbo, pctFboDelta);
}
if( pctFboDelta > 0.1f ) { // more than 10% !
if( DEBUG_FBO_1 ) {
System.err.printf("XXX.maxDelta: %d / %d = %.3f%n", maxDeltaFbo, maxLengthFbo, pctFboDelta);
System.err.printf("XXX.Scale %d * [%f x %f]: %d x %d%n",
sampleCount[0], winWidth, winHeight, targetFboWidth, targetFboHeight);
}
final int maxLength = Math.max(targetFboWidth, targetFboHeight);
if( maxLength > maxTexSize[0] ) {
if( targetFboWidth > targetFboHeight ) {
sampleCount[0] = (int)Math.floor(maxTexSize[0] / winWidth);
} else {
sampleCount[0] = (int)Math.floor(maxTexSize[0] / winHeight);
}
renderFboWidth = winWidth*sampleCount[0];
renderFboHeight = winHeight*sampleCount[0];
targetFboWidth = (int)Math.ceil(renderFboWidth);
targetFboHeight = (int)Math.ceil(renderFboHeight);
if( DEBUG_FBO_1 ) {
System.err.printf("XXX.Rescale (MAX): win[%.3f, %.3f]: FBO f[%.3f, %.3f], i[%d x %d], msaa %d%n",
winWidth, winHeight,
renderFboWidth, renderFboHeight, targetFboWidth, targetFboHeight, sampleCount[0]);
}
if( sampleCount[0] <= 0 ) {
// Last way out!
renderRegion(gl);
return;
}
}
verticeFboAttr.seal(false);
verticeFboAttr.rewind();
verticeFboAttr.putf(box.getMinX()); verticeFboAttr.putf(box.getMinY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMinX()); verticeFboAttr.putf(box.getMaxY()+diffHeight); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMaxX()+diffWidth); verticeFboAttr.putf(box.getMaxY()+diffHeight); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.putf(box.getMaxX()+diffWidth); verticeFboAttr.putf(box.getMinY()); verticeFboAttr.putf(box.getMinZ());
verticeFboAttr.seal(true);
fboPMVMatrix.glLoadIdentity();
fboPMVMatrix.glOrthof(box.getMinX(), box.getMaxX()+diffWidth,
box.getMinY(), box.getMaxY()+diffHeight, -1, 1);
renderRegion2FBO(gl, rs, targetFboWidth, targetFboHeight, sampleCount[0]);
}
// System.out.println("Scale: " + matrix.glGetMatrixf().get(1+4*3) +" " + matrix.glGetMatrixf().get(2+4*3));
renderFBO(gl, rs, width, height, sampleCount[0]);
}
}
private void setTexSize(final GL2ES2 gl, final ShaderState st, boolean firstPass, int sampleCount) {
if(null == mgl_TextureSize) {
mgl_TextureSize = new GLUniformData(UniformNames.gcu_TextureSize, 3, Buffers.newDirectFloatBuffer(3));
}
final FloatBuffer texSize = (FloatBuffer) mgl_TextureSize.getBuffer();
if( firstPass ) {
texSize.put(2, 0f);
} else {
texSize.put(0, fboWidth);
texSize.put(1, fboHeight);
texSize.put(2, sampleCount);
}
st.uniform(gl, mgl_TextureSize);
}
private void renderFBO(final GL2ES2 gl, final RenderState rs, final int width, final int height, int sampleCount) {
final ShaderState st = rs.getShaderState();
gl.glViewport(0, 0, width, height);
st.uniform(gl, mgl_ActiveTexture);
gl.glActiveTexture(GL.GL_TEXTURE0 + mgl_ActiveTexture.intValue());
setTexSize(gl, st, false, sampleCount);
fbo.use(gl, texA);
verticeFboAttr.enableBuffer(gl, true);
texCoordFboAttr.enableBuffer(gl, true);
indicesFbo.bindBuffer(gl, true); // keeps VBO binding
gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesFbo.getElementCount() * indicesFbo.getComponentCount(), GL2ES2.GL_UNSIGNED_SHORT, 0);
indicesFbo.bindBuffer(gl, false);
texCoordFboAttr.enableBuffer(gl, false);
verticeFboAttr.enableBuffer(gl, false);
fbo.unuse(gl);
// setback: gl.glActiveTexture(currentActiveTextureEngine[0]);
}
private void renderRegion2FBO(final GL2ES2 gl, final RenderState rs, final int targetFboWidth, final int targetFboHeight, int sampleCount) {
final ShaderState st = rs.getShaderState();
if( 0 >= targetFboWidth || 0 >= targetFboHeight ) {
throw new IllegalArgumentException("fboSize must be greater than 0: "+targetFboWidth+"x"+targetFboHeight);
}
if(null == fbo) {
fboWidth = targetFboWidth;
fboHeight = targetFboHeight;
fbo = new FBObject();
fbo.reset(gl, fboWidth, fboHeight);
// Shall not use bilinear (GL_LINEAR), due to own VBAA. Result is smooth w/o it now!
// texA = fbo.attachTexture2D(gl, 0, true, GL2ES2.GL_LINEAR, GL2ES2.GL_LINEAR, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE);
texA = fbo.attachTexture2D(gl, 0, true, GL2ES2.GL_NEAREST, GL2ES2.GL_NEAREST, GL2ES2.GL_CLAMP_TO_EDGE, GL2ES2.GL_CLAMP_TO_EDGE);
fbo.attachRenderbuffer(gl, Attachment.Type.DEPTH, 24);
if( DEBUG_FBO_1 ) {
System.err.printf("XXX.createFBO: %dx%d%n%s%n", fboWidth, fboHeight, fbo.toString());
}
} else if( targetFboWidth != fboWidth || targetFboHeight != fboHeight ) {
fbo.reset(gl, targetFboWidth, targetFboHeight);
fbo.bind(gl);
if( DEBUG_FBO_1 ) {
System.err.printf("XXX.resetFBO: %dx%d -> %dx%d%n%s%n", fboWidth, fboHeight, targetFboWidth, targetFboHeight, fbo );
}
fboWidth = targetFboWidth;
fboHeight = targetFboHeight;
} else {
fbo.bind(gl);
}
setTexSize(gl, st, true, sampleCount);
//render texture
gl.glViewport(0, 0, fboWidth, fboHeight);
st.uniform(gl, mgl_fboPMVMatrix); // use orthogonal matrix
gl.glClear(GL2ES2.GL_COLOR_BUFFER_BIT | GL2ES2.GL_DEPTH_BUFFER_BIT);
renderRegion(gl);
fbo.unbind(gl);
st.uniform(gl, rs.getPMVMatrix()); // switch back to real PMV matrix
}
private void renderRegion(final GL2ES2 gl) {
verticeTxtAttr.enableBuffer(gl, true);
texCoordTxtAttr.enableBuffer(gl, true);
indicesTxtBuffer.bindBuffer(gl, true); // keeps VBO binding
gl.glDrawElements(GL2ES2.GL_TRIANGLES, indicesTxtBuffer.getElementCount() * indicesTxtBuffer.getComponentCount(), GL2ES2.GL_UNSIGNED_SHORT, 0);
indicesTxtBuffer.bindBuffer(gl, false);
texCoordTxtAttr.enableBuffer(gl, false);
verticeTxtAttr.enableBuffer(gl, false);
}
@Override
protected void destroyImpl(final GL2ES2 gl, final RegionRenderer renderer) {
if(DEBUG_INSTANCE) {
System.err.println("VBORegion2PES2 Destroy: " + this);
// Thread.dumpStack();
}
final ShaderState st = renderer.getShaderState();
if(null != fbo) {
fbo.destroy(gl);
fbo = null;
texA = null;
}
if(null != verticeTxtAttr) {
st.ownAttribute(verticeTxtAttr, false);
verticeTxtAttr.destroy(gl);
verticeTxtAttr = null;
}
if(null != texCoordTxtAttr) {
st.ownAttribute(texCoordTxtAttr, false);
texCoordTxtAttr.destroy(gl);
texCoordTxtAttr = null;
}
if(null != indicesTxtBuffer) {
indicesTxtBuffer.destroy(gl);
indicesTxtBuffer = null;
}
if(null != verticeFboAttr) {
st.ownAttribute(verticeFboAttr, false);
verticeFboAttr.destroy(gl);
verticeFboAttr = null;
}
if(null != texCoordFboAttr) {
st.ownAttribute(texCoordFboAttr, false);
texCoordFboAttr.destroy(gl);
texCoordFboAttr = null;
}
if(null != indicesFbo) {
indicesFbo.destroy(gl);
indicesFbo = null;
}
}
}
|