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