1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.main;
|
21
|
|
22
|
import android.content.res.Resources;
|
23
|
import android.opengl.GLES31;
|
24
|
import android.util.Log;
|
25
|
|
26
|
import org.distorted.library.R;
|
27
|
import org.distorted.library.effect.Effect;
|
28
|
import org.distorted.library.effect.EffectName;
|
29
|
import org.distorted.library.effect.EffectType;
|
30
|
import org.distorted.library.effect.FragmentEffect;
|
31
|
import org.distorted.library.effect.VertexEffect;
|
32
|
import org.distorted.library.message.EffectListener;
|
33
|
import org.distorted.library.program.DistortedProgram;
|
34
|
import org.distorted.library.program.FragmentCompilationException;
|
35
|
import org.distorted.library.program.FragmentUniformsException;
|
36
|
import org.distorted.library.program.LinkingException;
|
37
|
import org.distorted.library.program.VertexCompilationException;
|
38
|
import org.distorted.library.program.VertexUniformsException;
|
39
|
|
40
|
import java.io.InputStream;
|
41
|
import java.nio.ByteBuffer;
|
42
|
import java.nio.ByteOrder;
|
43
|
import java.nio.FloatBuffer;
|
44
|
import java.nio.IntBuffer;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
/**
|
48
|
* Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
|
49
|
* <p>
|
50
|
* The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
|
51
|
*/
|
52
|
public class DistortedEffects
|
53
|
{
|
54
|
/// MAIN PROGRAM ///
|
55
|
private static DistortedProgram mMainProgram;
|
56
|
private static int mMainTextureH;
|
57
|
private static int mCountIndexH;
|
58
|
|
59
|
/// BLIT PROGRAM ///
|
60
|
private static DistortedProgram mBlitProgram;
|
61
|
private static int mBlitTextureH;
|
62
|
private static int mBlitDepthH;
|
63
|
private static final FloatBuffer mQuadPositions;
|
64
|
|
65
|
static
|
66
|
{
|
67
|
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f };
|
68
|
mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
69
|
mQuadPositions.put(positionData).position(0);
|
70
|
}
|
71
|
|
72
|
/// BLIT DEPTH PROGRAM ///
|
73
|
private static DistortedProgram mBlitDepthProgram;
|
74
|
private static int mBlitDepthTextureH;
|
75
|
private static int mBlitDepthDepthTextureH;
|
76
|
private static int mBlitDepthDepthH;
|
77
|
private static int mBlitDepthTexCorrH;
|
78
|
private static int mBlitDepthSizeH;
|
79
|
private static int mBlitDepthNumRecordsH;
|
80
|
|
81
|
private static int[] mLinkedListSSBO = new int[1];
|
82
|
private static int[] mAtomicCounter = new int[1];
|
83
|
|
84
|
static
|
85
|
{
|
86
|
mLinkedListSSBO[0]= -1;
|
87
|
mAtomicCounter[0] = -1;
|
88
|
}
|
89
|
|
90
|
private static int mBufferSize=(0x1<<23); // 8 million entries
|
91
|
private static IntBuffer mIntBuffer;
|
92
|
|
93
|
/// BLIT DEPTH RENDER PROGRAM ///
|
94
|
private static DistortedProgram mBlitDepthRenderProgram;
|
95
|
private static int mBlitDepthRenderDepthTextureH;
|
96
|
private static int mBlitDepthRenderDepthH;
|
97
|
private static int mBlitDepthRenderTexCorrH;
|
98
|
private static int mBlitDepthRenderSizeH;
|
99
|
|
100
|
/// NORMAL PROGRAM /////
|
101
|
private static DistortedProgram mNormalProgram;
|
102
|
private static int mNormalMVPMatrixH;
|
103
|
/// END PROGRAMS //////
|
104
|
|
105
|
private static long mNextID =0;
|
106
|
private long mID;
|
107
|
|
108
|
private EffectQueueMatrix mM;
|
109
|
private EffectQueueFragment mF;
|
110
|
private EffectQueueVertex mV;
|
111
|
private EffectQueuePostprocess mP;
|
112
|
|
113
|
private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
|
114
|
|
115
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
116
|
|
117
|
static void createProgram(Resources resources)
|
118
|
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
|
119
|
{
|
120
|
// MAIN PROGRAM ////////////////////////////////////
|
121
|
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
122
|
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
|
123
|
|
124
|
int numF = FragmentEffect.getNumEnabled();
|
125
|
int numV = VertexEffect.getNumEnabled();
|
126
|
|
127
|
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
128
|
String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
|
129
|
String enabledEffectV= VertexEffect.getGLSL();
|
130
|
String enabledEffectF= FragmentEffect.getGLSL();
|
131
|
|
132
|
//android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
|
133
|
//android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
|
134
|
//android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
|
135
|
//android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
|
136
|
|
137
|
String[] feedback = { "v_Position", "v_endPosition" };
|
138
|
|
139
|
mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
140
|
enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
|
141
|
|
142
|
int mainProgramH = mMainProgram.getProgramHandle();
|
143
|
EffectQueueFragment.getUniforms(mainProgramH);
|
144
|
EffectQueueVertex.getUniforms(mainProgramH);
|
145
|
EffectQueueMatrix.getUniforms(mainProgramH);
|
146
|
mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
|
147
|
mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
|
148
|
|
149
|
// BLIT PROGRAM ////////////////////////////////////
|
150
|
final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
|
151
|
final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
|
152
|
|
153
|
String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n" );
|
154
|
String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
|
155
|
|
156
|
try
|
157
|
{
|
158
|
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
|
159
|
}
|
160
|
catch(Exception e)
|
161
|
{
|
162
|
Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
|
163
|
throw new RuntimeException(e.getMessage());
|
164
|
}
|
165
|
|
166
|
int blitProgramH = mBlitProgram.getProgramHandle();
|
167
|
mBlitTextureH = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
|
168
|
mBlitDepthH = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
|
169
|
|
170
|
// BLIT DEPTH PROGRAM ////////////////////////////////////
|
171
|
final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
|
172
|
final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
|
173
|
|
174
|
try
|
175
|
{
|
176
|
mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
|
177
|
}
|
178
|
catch(Exception e)
|
179
|
{
|
180
|
Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
|
181
|
throw new RuntimeException(e.getMessage());
|
182
|
}
|
183
|
|
184
|
int blitDepthProgramH = mBlitDepthProgram.getProgramHandle();
|
185
|
mBlitDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
|
186
|
mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
|
187
|
mBlitDepthDepthH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
|
188
|
mBlitDepthTexCorrH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
|
189
|
mBlitDepthSizeH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
|
190
|
mBlitDepthNumRecordsH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords");
|
191
|
|
192
|
mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
|
193
|
mIntBuffer.put(0,0);
|
194
|
|
195
|
if( mLinkedListSSBO[0]<0 )
|
196
|
{
|
197
|
GLES31.glGenBuffers(1,mLinkedListSSBO,0);
|
198
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
|
199
|
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ);
|
200
|
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
|
201
|
}
|
202
|
|
203
|
if( mAtomicCounter[0]<0 )
|
204
|
{
|
205
|
GLES31.glGenBuffers(1,mAtomicCounter,0);
|
206
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
|
207
|
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
|
208
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
|
209
|
}
|
210
|
|
211
|
// BLIT DEPTH RENDER PROGRAM ///////////////////////////
|
212
|
final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
|
213
|
final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader);
|
214
|
|
215
|
try
|
216
|
{
|
217
|
mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
|
218
|
}
|
219
|
catch(Exception e)
|
220
|
{
|
221
|
Log.e("EFFECTS", "exception trying to compile BLIT DEPTH RENDER program: "+e.getMessage());
|
222
|
throw new RuntimeException(e.getMessage());
|
223
|
}
|
224
|
|
225
|
int blitDepthRenderProgramH = mBlitDepthRenderProgram.getProgramHandle();
|
226
|
mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture");
|
227
|
mBlitDepthRenderDepthH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth");
|
228
|
mBlitDepthRenderTexCorrH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr");
|
229
|
mBlitDepthRenderSizeH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
|
230
|
|
231
|
// NORMAL PROGRAM //////////////////////////////////////
|
232
|
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader);
|
233
|
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
|
234
|
|
235
|
try
|
236
|
{
|
237
|
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
|
238
|
}
|
239
|
catch(Exception e)
|
240
|
{
|
241
|
Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
|
242
|
throw new RuntimeException(e.getMessage());
|
243
|
}
|
244
|
|
245
|
int normalProgramH = mNormalProgram.getProgramHandle();
|
246
|
mNormalMVPMatrixH = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
|
247
|
}
|
248
|
|
249
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
250
|
|
251
|
private void initializeEffectLists(DistortedEffects d, int flags)
|
252
|
{
|
253
|
if( (flags & Distorted.CLONE_MATRIX) != 0 )
|
254
|
{
|
255
|
mM = d.mM;
|
256
|
matrixCloned = true;
|
257
|
}
|
258
|
else
|
259
|
{
|
260
|
mM = new EffectQueueMatrix(mID);
|
261
|
matrixCloned = false;
|
262
|
}
|
263
|
|
264
|
if( (flags & Distorted.CLONE_VERTEX) != 0 )
|
265
|
{
|
266
|
mV = d.mV;
|
267
|
vertexCloned = true;
|
268
|
}
|
269
|
else
|
270
|
{
|
271
|
mV = new EffectQueueVertex(mID);
|
272
|
vertexCloned = false;
|
273
|
}
|
274
|
|
275
|
if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
|
276
|
{
|
277
|
mF = d.mF;
|
278
|
fragmentCloned = true;
|
279
|
}
|
280
|
else
|
281
|
{
|
282
|
mF = new EffectQueueFragment(mID);
|
283
|
fragmentCloned = false;
|
284
|
}
|
285
|
|
286
|
if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
|
287
|
{
|
288
|
mP = d.mP;
|
289
|
postprocessCloned = true;
|
290
|
}
|
291
|
else
|
292
|
{
|
293
|
mP = new EffectQueuePostprocess(mID);
|
294
|
postprocessCloned = false;
|
295
|
}
|
296
|
}
|
297
|
|
298
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
299
|
|
300
|
EffectQueuePostprocess getPostprocess()
|
301
|
{
|
302
|
return mP;
|
303
|
}
|
304
|
|
305
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
306
|
|
307
|
void newNode(DistortedNode node)
|
308
|
{
|
309
|
mM.newNode(node);
|
310
|
mF.newNode(node);
|
311
|
mV.newNode(node);
|
312
|
mP.newNode(node);
|
313
|
}
|
314
|
|
315
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
316
|
|
317
|
private void displayNormals(MeshObject mesh)
|
318
|
{
|
319
|
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
|
320
|
GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
|
321
|
DistortedRenderState.switchOffDrawing();
|
322
|
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
|
323
|
DistortedRenderState.restoreDrawing();
|
324
|
GLES31.glEndTransformFeedback();
|
325
|
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
|
326
|
|
327
|
mNormalProgram.useProgram();
|
328
|
GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
|
329
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
|
330
|
GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
|
331
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
332
|
GLES31.glLineWidth(8.0f);
|
333
|
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
|
339
|
{
|
340
|
float halfZ = halfW*mesh.zFactor;
|
341
|
|
342
|
mM.compute(currTime);
|
343
|
mV.compute(currTime,halfW,halfH,halfZ);
|
344
|
mF.compute(currTime,halfW,halfH);
|
345
|
mP.compute(currTime);
|
346
|
|
347
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
348
|
|
349
|
mMainProgram.useProgram();
|
350
|
GLES31.glUniform1i(mMainTextureH, 0);
|
351
|
GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
|
352
|
|
353
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
|
354
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
|
355
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
|
356
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
|
357
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
358
|
|
359
|
mM.send(surface,halfW,halfH,halfZ,marginInPixels);
|
360
|
mV.send();
|
361
|
mF.send();
|
362
|
|
363
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
|
364
|
|
365
|
if( mesh.mShowNormals ) displayNormals(mesh);
|
366
|
}
|
367
|
|
368
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
369
|
/**
|
370
|
* Only for use by the library itself.
|
371
|
*
|
372
|
* @y.exclude
|
373
|
*/
|
374
|
public static void blitPriv(DistortedOutputSurface surface)
|
375
|
{
|
376
|
mBlitProgram.useProgram();
|
377
|
|
378
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
379
|
GLES31.glUniform1i(mBlitTextureH, 0);
|
380
|
GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
|
381
|
GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
382
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
383
|
}
|
384
|
|
385
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
386
|
|
387
|
static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
|
388
|
{
|
389
|
mBlitDepthProgram.useProgram();
|
390
|
|
391
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
392
|
GLES31.glUniform1i(mBlitDepthTextureH, 0);
|
393
|
GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
|
394
|
GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
|
395
|
GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
|
396
|
GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 ); // see the fragment shader
|
397
|
GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
|
398
|
GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
399
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
400
|
|
401
|
|
402
|
//android.util.Log.e("effects", "bufferSize: "+mBufferSize+" numRecords: "+((mBufferSize-surface.mWidth*surface.mHeight)/3) );
|
403
|
}
|
404
|
|
405
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
406
|
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge
|
407
|
// postprocessing buckets' stage.
|
408
|
|
409
|
static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH)
|
410
|
{
|
411
|
mBlitDepthRenderProgram.useProgram();
|
412
|
|
413
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
414
|
GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
|
415
|
GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
|
416
|
GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
|
417
|
GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear);
|
418
|
GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
419
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
420
|
|
421
|
// reset atomic counter to 0
|
422
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
|
423
|
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
|
424
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
|
425
|
}
|
426
|
|
427
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
428
|
|
429
|
private void releasePriv()
|
430
|
{
|
431
|
if( !matrixCloned ) mM.abortAll(false);
|
432
|
if( !vertexCloned ) mV.abortAll(false);
|
433
|
if( !fragmentCloned ) mF.abortAll(false);
|
434
|
if( !postprocessCloned) mP.abortAll(false);
|
435
|
|
436
|
mM = null;
|
437
|
mV = null;
|
438
|
mF = null;
|
439
|
mP = null;
|
440
|
}
|
441
|
|
442
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
443
|
|
444
|
static void onDestroy()
|
445
|
{
|
446
|
mNextID = 0;
|
447
|
mLinkedListSSBO[0]= -1;
|
448
|
mAtomicCounter[0] = -1;
|
449
|
}
|
450
|
|
451
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
452
|
// PUBLIC API
|
453
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
454
|
/**
|
455
|
* Create empty effect queue.
|
456
|
*/
|
457
|
public DistortedEffects()
|
458
|
{
|
459
|
mID = ++mNextID;
|
460
|
initializeEffectLists(this,0);
|
461
|
}
|
462
|
|
463
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
464
|
/**
|
465
|
* Copy constructor.
|
466
|
* <p>
|
467
|
* Whatever we do not clone gets created just like in the default constructor.
|
468
|
*
|
469
|
* @param dc Source object to create our object from
|
470
|
* @param flags A bitmask of values specifying what to copy.
|
471
|
* For example, CLONE_VERTEX | CLONE_MATRIX.
|
472
|
*/
|
473
|
public DistortedEffects(DistortedEffects dc, int flags)
|
474
|
{
|
475
|
mID = ++mNextID;
|
476
|
initializeEffectLists(dc,flags);
|
477
|
}
|
478
|
|
479
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
480
|
/**
|
481
|
* Releases all resources. After this call, the queue should not be used anymore.
|
482
|
*/
|
483
|
@SuppressWarnings("unused")
|
484
|
public synchronized void delete()
|
485
|
{
|
486
|
releasePriv();
|
487
|
}
|
488
|
|
489
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
490
|
/**
|
491
|
* Returns unique ID of this instance.
|
492
|
*
|
493
|
* @return ID of the object.
|
494
|
*/
|
495
|
public long getID()
|
496
|
{
|
497
|
return mID;
|
498
|
}
|
499
|
|
500
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
501
|
/**
|
502
|
* Adds the calling class to the list of Listeners that get notified each time some event happens
|
503
|
* to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
|
504
|
*
|
505
|
* @param el A class implementing the EffectListener interface that wants to get notifications.
|
506
|
*/
|
507
|
@SuppressWarnings("unused")
|
508
|
public void registerForMessages(EffectListener el)
|
509
|
{
|
510
|
mM.registerForMessages(el);
|
511
|
mV.registerForMessages(el);
|
512
|
mF.registerForMessages(el);
|
513
|
mP.registerForMessages(el);
|
514
|
}
|
515
|
|
516
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
517
|
/**
|
518
|
* Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
|
519
|
*
|
520
|
* @param el A class implementing the EffectListener interface that no longer wants to get notifications.
|
521
|
*/
|
522
|
@SuppressWarnings("unused")
|
523
|
public void deregisterForMessages(EffectListener el)
|
524
|
{
|
525
|
mM.deregisterForMessages(el);
|
526
|
mV.deregisterForMessages(el);
|
527
|
mF.deregisterForMessages(el);
|
528
|
mP.deregisterForMessages(el);
|
529
|
}
|
530
|
|
531
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
532
|
/**
|
533
|
* Aborts all Effects.
|
534
|
* @return Number of effects aborted.
|
535
|
*/
|
536
|
public int abortAllEffects()
|
537
|
{
|
538
|
return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
|
539
|
}
|
540
|
|
541
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
542
|
/**
|
543
|
* Aborts all Effects of a given type, for example all MATRIX Effects.
|
544
|
*
|
545
|
* @param type one of the constants defined in {@link EffectType}
|
546
|
* @return Number of effects aborted.
|
547
|
*/
|
548
|
public int abortByType(EffectType type)
|
549
|
{
|
550
|
switch(type)
|
551
|
{
|
552
|
case MATRIX : return mM.abortAll(true);
|
553
|
case VERTEX : return mV.abortAll(true);
|
554
|
case FRAGMENT : return mF.abortAll(true);
|
555
|
case POSTPROCESS: return mP.abortAll(true);
|
556
|
default : return 0;
|
557
|
}
|
558
|
}
|
559
|
|
560
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
561
|
/**
|
562
|
* Aborts an Effect by its ID.
|
563
|
*
|
564
|
* @param id the Id of the Effect to be removed, as returned by getID().
|
565
|
* @return Number of effects aborted.
|
566
|
*/
|
567
|
public int abortById(long id)
|
568
|
{
|
569
|
long type = id&EffectType.MASK;
|
570
|
|
571
|
if( type == EffectType.MATRIX.ordinal() ) return mM.removeById(id);
|
572
|
if( type == EffectType.VERTEX.ordinal() ) return mV.removeById(id);
|
573
|
if( type == EffectType.FRAGMENT.ordinal() ) return mF.removeById(id);
|
574
|
if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
|
575
|
|
576
|
return 0;
|
577
|
}
|
578
|
|
579
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
580
|
/**
|
581
|
* Aborts a single Effect.
|
582
|
*
|
583
|
* @param effect the Effect we want to abort.
|
584
|
* @return number of Effects aborted. Always either 0 or 1.
|
585
|
*/
|
586
|
public int abortEffect(Effect effect)
|
587
|
{
|
588
|
switch(effect.getType())
|
589
|
{
|
590
|
case MATRIX : return mM.removeEffect(effect);
|
591
|
case VERTEX : return mV.removeEffect(effect);
|
592
|
case FRAGMENT : return mF.removeEffect(effect);
|
593
|
case POSTPROCESS: return mP.removeEffect(effect);
|
594
|
default : return 0;
|
595
|
}
|
596
|
}
|
597
|
|
598
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
599
|
/**
|
600
|
* Abort all Effects of a given name, for example all rotations.
|
601
|
*
|
602
|
* @param name one of the constants defined in {@link EffectName}
|
603
|
* @return number of Effects aborted.
|
604
|
*/
|
605
|
public int abortByName(EffectName name)
|
606
|
{
|
607
|
switch(name.getType())
|
608
|
{
|
609
|
case MATRIX : return mM.removeByName(name);
|
610
|
case VERTEX : return mV.removeByName(name);
|
611
|
case FRAGMENT : return mF.removeByName(name);
|
612
|
case POSTPROCESS: return mP.removeByName(name);
|
613
|
default : return 0;
|
614
|
}
|
615
|
}
|
616
|
|
617
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
618
|
/**
|
619
|
* Returns the maximum number of effects of a given type that can be simultaneously applied to a
|
620
|
* single (InputSurface,MeshObject) combo.
|
621
|
*
|
622
|
* @param type {@link EffectType}
|
623
|
* @return The maximum number of effects of a given type.
|
624
|
*/
|
625
|
@SuppressWarnings("unused")
|
626
|
public static int getMax(EffectType type)
|
627
|
{
|
628
|
return EffectQueue.getMax(type.ordinal());
|
629
|
}
|
630
|
|
631
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
632
|
/**
|
633
|
* Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
|
634
|
* This can fail if:
|
635
|
* <ul>
|
636
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
637
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
638
|
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
|
639
|
* time only decreasing the value of 'max' is permitted.
|
640
|
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
|
641
|
* </ul>
|
642
|
*
|
643
|
* @param type {@link EffectType}
|
644
|
* @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
|
645
|
* than Byte.MAX_VALUE
|
646
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
647
|
*/
|
648
|
@SuppressWarnings("unused")
|
649
|
public static boolean setMax(EffectType type, int max)
|
650
|
{
|
651
|
return EffectQueue.setMax(type.ordinal(),max);
|
652
|
}
|
653
|
|
654
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
655
|
/**
|
656
|
* Add a new Effect to our queue.
|
657
|
*
|
658
|
* @param effect The Effect to add.
|
659
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
660
|
*/
|
661
|
public boolean apply(Effect effect)
|
662
|
{
|
663
|
switch(effect.getType())
|
664
|
{
|
665
|
case MATRIX : return mM.add(effect);
|
666
|
case VERTEX : return mV.add(effect);
|
667
|
case FRAGMENT : return mF.add(effect);
|
668
|
case POSTPROCESS : return mP.add(effect);
|
669
|
}
|
670
|
|
671
|
return false;
|
672
|
}
|
673
|
}
|