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
|
|
58
|
/// BLIT PROGRAM ///
|
59
|
private static DistortedProgram mBlitProgram;
|
60
|
private static int mBlitTextureH;
|
61
|
private static int mBlitDepthH;
|
62
|
private static final FloatBuffer mQuadPositions;
|
63
|
|
64
|
static
|
65
|
{
|
66
|
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f };
|
67
|
mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
68
|
mQuadPositions.put(positionData).position(0);
|
69
|
}
|
70
|
|
71
|
/// OIT SSBO BUFFER ///
|
72
|
private static int[] mLinkedListSSBO = new int[1];
|
73
|
private static int[] mAtomicCounter = new int[1];
|
74
|
|
75
|
static
|
76
|
{
|
77
|
mLinkedListSSBO[0]= -1;
|
78
|
mAtomicCounter[0] = -1;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////
|
82
|
// this has to be at least as big as the number of pixels in
|
83
|
// the screen, lest the oitClear() pass overwrite memory.
|
84
|
private static int mBufferSize=(0x1<<23); // 8 million entries
|
85
|
|
86
|
/// OIT CLEAR PROGRAM ///
|
87
|
private static DistortedProgram mOITClearProgram;
|
88
|
private static int mOITClearDepthH;
|
89
|
private static int mOITClearTexCorrH;
|
90
|
private static int mOITClearSizeH;
|
91
|
|
92
|
/// OIT BUILD PROGRAM ///
|
93
|
private static DistortedProgram mOITBuildProgram;
|
94
|
private static int mOITBuildTextureH;
|
95
|
private static int mOITBuildDepthTextureH;
|
96
|
private static int mOITBuildDepthH;
|
97
|
private static int mOITBuildTexCorrH;
|
98
|
private static int mOITBuildSizeH;
|
99
|
private static int mOITBuildNumRecordsH;
|
100
|
|
101
|
/// OIT COLLAPSE PROGRAM ///
|
102
|
private static DistortedProgram mOITCollapseProgram;
|
103
|
private static int mOITCollapseDepthTextureH;
|
104
|
private static int mOITCollapseDepthH;
|
105
|
private static int mOITCollapseTexCorrH;
|
106
|
private static int mOITCollapseSizeH;
|
107
|
|
108
|
/// OIT RENDER PROGRAM ///
|
109
|
private static DistortedProgram mOITRenderProgram;
|
110
|
private static int mOITRenderDepthH;
|
111
|
private static int mOITRenderTexCorrH;
|
112
|
private static int mOITRenderSizeH;
|
113
|
|
114
|
/// NORMAL PROGRAM /////
|
115
|
private static DistortedProgram mNormalProgram;
|
116
|
private static int mNormalMVPMatrixH;
|
117
|
/// END PROGRAMS //////
|
118
|
|
119
|
private static long mNextID =0;
|
120
|
private long mID;
|
121
|
|
122
|
private EffectQueueMatrix mM;
|
123
|
private EffectQueueFragment mF;
|
124
|
private EffectQueueVertex mV;
|
125
|
private EffectQueuePostprocess mP;
|
126
|
|
127
|
private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
|
128
|
|
129
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
130
|
|
131
|
static void createProgram(Resources resources)
|
132
|
{
|
133
|
// MAIN PROGRAM ////////////////////////////////////
|
134
|
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
135
|
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
|
136
|
|
137
|
int numF = FragmentEffect.getNumEnabled();
|
138
|
int numV = VertexEffect.getNumEnabled();
|
139
|
|
140
|
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
141
|
String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
|
142
|
String enabledEffectV= VertexEffect.getGLSL();
|
143
|
String enabledEffectF= FragmentEffect.getGLSL();
|
144
|
|
145
|
//android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
|
146
|
//android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
|
147
|
//android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
|
148
|
//android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
|
149
|
|
150
|
String[] feedback = { "v_Position", "v_endPosition" };
|
151
|
|
152
|
try
|
153
|
{
|
154
|
mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
155
|
enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
|
156
|
}
|
157
|
catch(Exception e)
|
158
|
{
|
159
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
|
160
|
throw new RuntimeException(e.getMessage());
|
161
|
}
|
162
|
|
163
|
int mainProgramH = mMainProgram.getProgramHandle();
|
164
|
EffectQueueFragment.getUniforms(mainProgramH);
|
165
|
EffectQueueVertex.getUniforms(mainProgramH);
|
166
|
EffectQueueMatrix.getUniforms(mainProgramH);
|
167
|
mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
|
168
|
|
169
|
// BLIT PROGRAM ////////////////////////////////////
|
170
|
final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
|
171
|
final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
|
172
|
|
173
|
try
|
174
|
{
|
175
|
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
|
176
|
}
|
177
|
catch(Exception e)
|
178
|
{
|
179
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
|
180
|
throw new RuntimeException(e.getMessage());
|
181
|
}
|
182
|
|
183
|
int blitProgramH = mBlitProgram.getProgramHandle();
|
184
|
mBlitTextureH = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
|
185
|
mBlitDepthH = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
|
186
|
|
187
|
// OIT CLEAR PROGRAM ////////////////////////////////////
|
188
|
final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
189
|
final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
|
190
|
|
191
|
try
|
192
|
{
|
193
|
mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
|
194
|
}
|
195
|
catch(Exception e)
|
196
|
{
|
197
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
|
198
|
throw new RuntimeException(e.getMessage());
|
199
|
}
|
200
|
|
201
|
int oitClearProgramH = mOITClearProgram.getProgramHandle();
|
202
|
mOITClearDepthH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Depth");
|
203
|
mOITClearTexCorrH = GLES31.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
|
204
|
mOITClearSizeH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Size");
|
205
|
|
206
|
// OIT BUILD PROGRAM ////////////////////////////////////
|
207
|
final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
208
|
final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
|
209
|
|
210
|
try
|
211
|
{
|
212
|
mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
|
213
|
}
|
214
|
catch(Exception e)
|
215
|
{
|
216
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
|
217
|
throw new RuntimeException(e.getMessage());
|
218
|
}
|
219
|
|
220
|
int oitBuildProgramH = mOITBuildProgram.getProgramHandle();
|
221
|
mOITBuildTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Texture");
|
222
|
mOITBuildDepthTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
|
223
|
mOITBuildDepthH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Depth");
|
224
|
mOITBuildTexCorrH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
|
225
|
mOITBuildSizeH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Size");
|
226
|
mOITBuildNumRecordsH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
|
227
|
|
228
|
if( mLinkedListSSBO[0]<0 )
|
229
|
{
|
230
|
GLES31.glGenBuffers(1,mLinkedListSSBO,0);
|
231
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
|
232
|
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
|
233
|
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
|
234
|
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
|
235
|
}
|
236
|
|
237
|
if( mAtomicCounter[0]<0 )
|
238
|
{
|
239
|
GLES31.glGenBuffers(1,mAtomicCounter,0);
|
240
|
GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[0]);
|
241
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
|
242
|
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW);
|
243
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
|
244
|
}
|
245
|
|
246
|
// OIT COLLAPSE PROGRAM ///////////////////////////
|
247
|
final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
248
|
final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
|
249
|
|
250
|
try
|
251
|
{
|
252
|
mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
|
253
|
}
|
254
|
catch(Exception e)
|
255
|
{
|
256
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
|
257
|
throw new RuntimeException(e.getMessage());
|
258
|
}
|
259
|
|
260
|
int oitCollapseProgramH = mOITCollapseProgram.getProgramHandle();
|
261
|
mOITCollapseDepthTextureH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
|
262
|
mOITCollapseDepthH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
|
263
|
mOITCollapseTexCorrH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
|
264
|
mOITCollapseSizeH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Size");
|
265
|
|
266
|
// OIT RENDER PROGRAM ///////////////////////////
|
267
|
final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
268
|
final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
|
269
|
|
270
|
try
|
271
|
{
|
272
|
mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL);
|
273
|
}
|
274
|
catch(Exception e)
|
275
|
{
|
276
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
|
277
|
throw new RuntimeException(e.getMessage());
|
278
|
}
|
279
|
|
280
|
int oitRenderProgramH = mOITRenderProgram.getProgramHandle();
|
281
|
mOITRenderDepthH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Depth");
|
282
|
mOITRenderTexCorrH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
|
283
|
mOITRenderSizeH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Size");
|
284
|
|
285
|
// NORMAL PROGRAM //////////////////////////////////////
|
286
|
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader);
|
287
|
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
|
288
|
|
289
|
try
|
290
|
{
|
291
|
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
|
292
|
}
|
293
|
catch(Exception e)
|
294
|
{
|
295
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
|
296
|
throw new RuntimeException(e.getMessage());
|
297
|
}
|
298
|
|
299
|
int normalProgramH = mNormalProgram.getProgramHandle();
|
300
|
mNormalMVPMatrixH = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
|
301
|
}
|
302
|
|
303
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
304
|
|
305
|
private void initializeEffectLists(DistortedEffects d, int flags)
|
306
|
{
|
307
|
if( (flags & Distorted.CLONE_MATRIX) != 0 )
|
308
|
{
|
309
|
mM = d.mM;
|
310
|
matrixCloned = true;
|
311
|
}
|
312
|
else
|
313
|
{
|
314
|
mM = new EffectQueueMatrix(mID);
|
315
|
matrixCloned = false;
|
316
|
}
|
317
|
|
318
|
if( (flags & Distorted.CLONE_VERTEX) != 0 )
|
319
|
{
|
320
|
mV = d.mV;
|
321
|
vertexCloned = true;
|
322
|
}
|
323
|
else
|
324
|
{
|
325
|
mV = new EffectQueueVertex(mID);
|
326
|
vertexCloned = false;
|
327
|
}
|
328
|
|
329
|
if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
|
330
|
{
|
331
|
mF = d.mF;
|
332
|
fragmentCloned = true;
|
333
|
}
|
334
|
else
|
335
|
{
|
336
|
mF = new EffectQueueFragment(mID);
|
337
|
fragmentCloned = false;
|
338
|
}
|
339
|
|
340
|
if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
|
341
|
{
|
342
|
mP = d.mP;
|
343
|
postprocessCloned = true;
|
344
|
}
|
345
|
else
|
346
|
{
|
347
|
mP = new EffectQueuePostprocess(mID);
|
348
|
postprocessCloned = false;
|
349
|
}
|
350
|
}
|
351
|
|
352
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353
|
|
354
|
EffectQueuePostprocess getPostprocess()
|
355
|
{
|
356
|
return mP;
|
357
|
}
|
358
|
|
359
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
360
|
|
361
|
void newNode(DistortedNode node)
|
362
|
{
|
363
|
mM.newNode(node);
|
364
|
mF.newNode(node);
|
365
|
mV.newNode(node);
|
366
|
mP.newNode(node);
|
367
|
}
|
368
|
|
369
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
370
|
|
371
|
private void displayNormals(MeshObject mesh)
|
372
|
{
|
373
|
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
|
374
|
GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
|
375
|
DistortedRenderState.switchOffDrawing();
|
376
|
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
|
377
|
DistortedRenderState.restoreDrawing();
|
378
|
GLES31.glEndTransformFeedback();
|
379
|
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
|
380
|
|
381
|
mNormalProgram.useProgram();
|
382
|
GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
|
383
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
|
384
|
GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
|
385
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
386
|
GLES31.glLineWidth(8.0f);
|
387
|
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
|
388
|
}
|
389
|
|
390
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
391
|
|
392
|
void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
|
393
|
{
|
394
|
float halfZ = halfW*mesh.zFactor;
|
395
|
|
396
|
mM.compute(currTime);
|
397
|
mV.compute(currTime,halfW,halfH,halfZ);
|
398
|
mF.compute(currTime,halfW,halfH);
|
399
|
mP.compute(currTime);
|
400
|
|
401
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
402
|
|
403
|
mMainProgram.useProgram();
|
404
|
GLES31.glUniform1i(mMainTextureH, 0);
|
405
|
|
406
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
|
407
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
|
408
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
|
409
|
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
|
410
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
411
|
|
412
|
mM.send(surface,halfW,halfH,halfZ,marginInPixels);
|
413
|
mV.send();
|
414
|
mF.send();
|
415
|
|
416
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
|
417
|
|
418
|
if( mesh.mShowNormals ) displayNormals(mesh);
|
419
|
}
|
420
|
|
421
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
422
|
/**
|
423
|
* Only for use by the library itself.
|
424
|
*
|
425
|
* @y.exclude
|
426
|
*/
|
427
|
public static void blitPriv(DistortedOutputSurface surface)
|
428
|
{
|
429
|
mBlitProgram.useProgram();
|
430
|
|
431
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
432
|
GLES31.glUniform1i(mBlitTextureH, 0);
|
433
|
GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
|
434
|
GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
435
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
436
|
}
|
437
|
|
438
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
439
|
// reset atomic counter to 0
|
440
|
|
441
|
static void zeroOutAtomic()
|
442
|
{
|
443
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
|
444
|
|
445
|
ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
|
446
|
GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT);
|
447
|
if( atomicBuf!=null )
|
448
|
{
|
449
|
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
|
450
|
|
451
|
//int counter = atomicIntBuf.get(0);
|
452
|
//android.util.Log.e("counter", "now = "+counter+" w="+surface.mWidth+" h="+surface.mHeight
|
453
|
// +" diff="+(counter-surface.mWidth*surface.mHeight));
|
454
|
atomicIntBuf.put(0,0);
|
455
|
}
|
456
|
else
|
457
|
{
|
458
|
android.util.Log.e("effects", "failed to map atomic buffer");
|
459
|
}
|
460
|
|
461
|
GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
|
462
|
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
|
463
|
}
|
464
|
|
465
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
466
|
// Pass1 of the OIT algorithm. Clear per-pixel head-poiners.
|
467
|
|
468
|
static void oitClear(DistortedOutputSurface surface)
|
469
|
{
|
470
|
mOITClearProgram.useProgram();
|
471
|
|
472
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
473
|
GLES31.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f ); // corrections do not really matter here - only present because of common vertex shader.
|
474
|
GLES31.glUniform1f( mOITClearDepthH , 1.0f); // likewise depth
|
475
|
GLES31.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
|
476
|
GLES31.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
477
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
478
|
}
|
479
|
|
480
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
481
|
// Pass2 of the OIT algorithm - build per-pixel linked lists.
|
482
|
|
483
|
static void oitBuild(DistortedOutputSurface surface, float corrW, float corrH)
|
484
|
{
|
485
|
mOITBuildProgram.useProgram();
|
486
|
|
487
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
488
|
GLES31.glUniform1i(mOITBuildTextureH, 0);
|
489
|
GLES31.glUniform1i(mOITBuildDepthTextureH, 1);
|
490
|
GLES31.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
|
491
|
GLES31.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
|
492
|
GLES31.glUniform1ui(mOITBuildNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 ); // see the fragment shader
|
493
|
GLES31.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
|
494
|
GLES31.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
495
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
496
|
}
|
497
|
|
498
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
499
|
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
|
500
|
|
501
|
static void oitCollapse(DistortedOutputSurface surface, float corrW, float corrH)
|
502
|
{
|
503
|
mOITCollapseProgram.useProgram();
|
504
|
|
505
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
506
|
GLES31.glUniform1i(mOITCollapseDepthTextureH, 1);
|
507
|
GLES31.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
|
508
|
GLES31.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
|
509
|
GLES31.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
|
510
|
GLES31.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
511
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
512
|
}
|
513
|
|
514
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
515
|
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
|
516
|
|
517
|
static void oitRender(DistortedOutputSurface surface, float corrW, float corrH)
|
518
|
{
|
519
|
mOITRenderProgram.useProgram();
|
520
|
|
521
|
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
522
|
GLES31.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
|
523
|
GLES31.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
|
524
|
GLES31.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
|
525
|
GLES31.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
|
526
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
|
527
|
}
|
528
|
|
529
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
530
|
|
531
|
private void releasePriv()
|
532
|
{
|
533
|
if( !matrixCloned ) mM.abortAll(false);
|
534
|
if( !vertexCloned ) mV.abortAll(false);
|
535
|
if( !fragmentCloned ) mF.abortAll(false);
|
536
|
if( !postprocessCloned ) mP.abortAll(false);
|
537
|
|
538
|
mM = null;
|
539
|
mV = null;
|
540
|
mF = null;
|
541
|
mP = null;
|
542
|
}
|
543
|
|
544
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
545
|
|
546
|
static void onPause()
|
547
|
{
|
548
|
mLinkedListSSBO[0]= -1;
|
549
|
mAtomicCounter[0] = -1;
|
550
|
}
|
551
|
|
552
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
553
|
|
554
|
static void onDestroy()
|
555
|
{
|
556
|
mNextID = 0;
|
557
|
}
|
558
|
|
559
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
560
|
// PUBLIC API
|
561
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
562
|
/**
|
563
|
* Create empty effect queue.
|
564
|
*/
|
565
|
public DistortedEffects()
|
566
|
{
|
567
|
mID = ++mNextID;
|
568
|
initializeEffectLists(this,0);
|
569
|
}
|
570
|
|
571
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
572
|
/**
|
573
|
* Copy constructor.
|
574
|
* <p>
|
575
|
* Whatever we do not clone gets created just like in the default constructor.
|
576
|
*
|
577
|
* @param dc Source object to create our object from
|
578
|
* @param flags A bitmask of values specifying what to copy.
|
579
|
* For example, CLONE_VERTEX | CLONE_MATRIX.
|
580
|
*/
|
581
|
public DistortedEffects(DistortedEffects dc, int flags)
|
582
|
{
|
583
|
mID = ++mNextID;
|
584
|
initializeEffectLists(dc,flags);
|
585
|
}
|
586
|
|
587
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
588
|
/**
|
589
|
* Releases all resources. After this call, the queue should not be used anymore.
|
590
|
*/
|
591
|
@SuppressWarnings("unused")
|
592
|
public synchronized void delete()
|
593
|
{
|
594
|
releasePriv();
|
595
|
}
|
596
|
|
597
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
598
|
/**
|
599
|
* Returns unique ID of this instance.
|
600
|
*
|
601
|
* @return ID of the object.
|
602
|
*/
|
603
|
public long getID()
|
604
|
{
|
605
|
return mID;
|
606
|
}
|
607
|
|
608
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
609
|
/**
|
610
|
* Adds the calling class to the list of Listeners that get notified each time some event happens
|
611
|
* to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
|
612
|
*
|
613
|
* @param el A class implementing the EffectListener interface that wants to get notifications.
|
614
|
*/
|
615
|
@SuppressWarnings("unused")
|
616
|
public void registerForMessages(EffectListener el)
|
617
|
{
|
618
|
mM.registerForMessages(el);
|
619
|
mV.registerForMessages(el);
|
620
|
mF.registerForMessages(el);
|
621
|
mP.registerForMessages(el);
|
622
|
}
|
623
|
|
624
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
625
|
/**
|
626
|
* Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
|
627
|
*
|
628
|
* @param el A class implementing the EffectListener interface that no longer wants to get notifications.
|
629
|
*/
|
630
|
@SuppressWarnings("unused")
|
631
|
public void deregisterForMessages(EffectListener el)
|
632
|
{
|
633
|
mM.deregisterForMessages(el);
|
634
|
mV.deregisterForMessages(el);
|
635
|
mF.deregisterForMessages(el);
|
636
|
mP.deregisterForMessages(el);
|
637
|
}
|
638
|
|
639
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
640
|
/**
|
641
|
* Aborts all Effects.
|
642
|
* @return Number of effects aborted.
|
643
|
*/
|
644
|
public int abortAllEffects()
|
645
|
{
|
646
|
return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
|
647
|
}
|
648
|
|
649
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
650
|
/**
|
651
|
* Aborts all Effects of a given type, for example all MATRIX Effects.
|
652
|
*
|
653
|
* @param type one of the constants defined in {@link EffectType}
|
654
|
* @return Number of effects aborted.
|
655
|
*/
|
656
|
public int abortByType(EffectType type)
|
657
|
{
|
658
|
switch(type)
|
659
|
{
|
660
|
case MATRIX : return mM.abortAll(true);
|
661
|
case VERTEX : return mV.abortAll(true);
|
662
|
case FRAGMENT : return mF.abortAll(true);
|
663
|
case POSTPROCESS: return mP.abortAll(true);
|
664
|
default : return 0;
|
665
|
}
|
666
|
}
|
667
|
|
668
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
669
|
/**
|
670
|
* Aborts an Effect by its ID.
|
671
|
*
|
672
|
* @param id the Id of the Effect to be removed, as returned by getID().
|
673
|
* @return Number of effects aborted.
|
674
|
*/
|
675
|
public int abortById(long id)
|
676
|
{
|
677
|
long type = id&EffectType.MASK;
|
678
|
|
679
|
if( type == EffectType.MATRIX.ordinal() ) return mM.removeById(id);
|
680
|
if( type == EffectType.VERTEX.ordinal() ) return mV.removeById(id);
|
681
|
if( type == EffectType.FRAGMENT.ordinal() ) return mF.removeById(id);
|
682
|
if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
|
683
|
|
684
|
return 0;
|
685
|
}
|
686
|
|
687
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
688
|
/**
|
689
|
* Aborts a single Effect.
|
690
|
*
|
691
|
* @param effect the Effect we want to abort.
|
692
|
* @return number of Effects aborted. Always either 0 or 1.
|
693
|
*/
|
694
|
public int abortEffect(Effect effect)
|
695
|
{
|
696
|
switch(effect.getType())
|
697
|
{
|
698
|
case MATRIX : return mM.removeEffect(effect);
|
699
|
case VERTEX : return mV.removeEffect(effect);
|
700
|
case FRAGMENT : return mF.removeEffect(effect);
|
701
|
case POSTPROCESS: return mP.removeEffect(effect);
|
702
|
default : return 0;
|
703
|
}
|
704
|
}
|
705
|
|
706
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
707
|
/**
|
708
|
* Abort all Effects of a given name, for example all rotations.
|
709
|
*
|
710
|
* @param name one of the constants defined in {@link EffectName}
|
711
|
* @return number of Effects aborted.
|
712
|
*/
|
713
|
public int abortByName(EffectName name)
|
714
|
{
|
715
|
switch(name.getType())
|
716
|
{
|
717
|
case MATRIX : return mM.removeByName(name);
|
718
|
case VERTEX : return mV.removeByName(name);
|
719
|
case FRAGMENT : return mF.removeByName(name);
|
720
|
case POSTPROCESS: return mP.removeByName(name);
|
721
|
default : return 0;
|
722
|
}
|
723
|
}
|
724
|
|
725
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
726
|
/**
|
727
|
* Returns the maximum number of effects of a given type that can be simultaneously applied to a
|
728
|
* single (InputSurface,MeshObject) combo.
|
729
|
*
|
730
|
* @param type {@link EffectType}
|
731
|
* @return The maximum number of effects of a given type.
|
732
|
*/
|
733
|
@SuppressWarnings("unused")
|
734
|
public static int getMax(EffectType type)
|
735
|
{
|
736
|
return EffectQueue.getMax(type.ordinal());
|
737
|
}
|
738
|
|
739
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
740
|
/**
|
741
|
* Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
|
742
|
* This can fail if:
|
743
|
* <ul>
|
744
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
745
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
746
|
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
|
747
|
* time only decreasing the value of 'max' is permitted.
|
748
|
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
|
749
|
* </ul>
|
750
|
*
|
751
|
* @param type {@link EffectType}
|
752
|
* @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
|
753
|
* than Byte.MAX_VALUE
|
754
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
755
|
*/
|
756
|
@SuppressWarnings("unused")
|
757
|
public static boolean setMax(EffectType type, int max)
|
758
|
{
|
759
|
return EffectQueue.setMax(type.ordinal(),max);
|
760
|
}
|
761
|
|
762
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
763
|
/**
|
764
|
* Add a new Effect to our queue.
|
765
|
*
|
766
|
* @param effect The Effect to add.
|
767
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
768
|
*/
|
769
|
public boolean apply(Effect effect)
|
770
|
{
|
771
|
switch(effect.getType())
|
772
|
{
|
773
|
case MATRIX : return mM.add(effect);
|
774
|
case VERTEX : return mV.add(effect);
|
775
|
case FRAGMENT : return mF.add(effect);
|
776
|
case POSTPROCESS : return mP.add(effect);
|
777
|
}
|
778
|
|
779
|
return false;
|
780
|
}
|
781
|
}
|