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.app.ActivityManager;
|
23
|
import android.content.Context;
|
24
|
import android.content.pm.ConfigurationInfo;
|
25
|
import android.content.res.Resources;
|
26
|
import android.opengl.GLES30;
|
27
|
import android.opengl.GLES31;
|
28
|
import android.util.Log;
|
29
|
|
30
|
import org.distorted.library.R;
|
31
|
import org.distorted.library.effect.Effect;
|
32
|
import org.distorted.library.effectqueue.EffectQueue;
|
33
|
import org.distorted.library.effectqueue.EffectQueuePostprocess;
|
34
|
import org.distorted.library.effect.EffectType;
|
35
|
import org.distorted.library.effect.FragmentEffect;
|
36
|
import org.distorted.library.effect.PostprocessEffect;
|
37
|
import org.distorted.library.effect.VertexEffect;
|
38
|
import org.distorted.library.effectqueue.EffectQueueVertex;
|
39
|
import org.distorted.library.mesh.DeferredJobs;
|
40
|
import org.distorted.library.mesh.MeshBase;
|
41
|
import org.distorted.library.message.EffectMessageSender;
|
42
|
import org.distorted.library.program.DistortedProgram;
|
43
|
import org.distorted.library.program.VertexCompilationException;
|
44
|
import org.distorted.library.type.Dynamic;
|
45
|
|
46
|
import java.io.InputStream;
|
47
|
import java.nio.ByteBuffer;
|
48
|
import java.nio.ByteOrder;
|
49
|
import java.nio.FloatBuffer;
|
50
|
import java.nio.IntBuffer;
|
51
|
import java.util.regex.Matcher;
|
52
|
import java.util.regex.Pattern;
|
53
|
|
54
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55
|
/**
|
56
|
* A singleton class used to control various global dialog_settings.
|
57
|
*/
|
58
|
public class DistortedLibrary
|
59
|
{
|
60
|
private static int mGLSL;
|
61
|
private static String mGLSL_VERSION;
|
62
|
private static boolean mOITCompilationAttempted;
|
63
|
/**
|
64
|
* When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
|
65
|
* backing up our DistortedTexture.
|
66
|
* <p>
|
67
|
* This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
|
68
|
* apply different effects. Used in the copy constructor.
|
69
|
*/
|
70
|
public static final int CLONE_SURFACE = 0x1;
|
71
|
/**
|
72
|
* When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
|
73
|
* <p>
|
74
|
* This way we can have two different DistortedEffects sharing the MATRIX queue.
|
75
|
*/
|
76
|
public static final int CLONE_MATRIX = 0x2;
|
77
|
/**
|
78
|
* When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
|
79
|
* <p>
|
80
|
* This way we can have two different DistortedEffects sharing the VERTEX queue.
|
81
|
*/
|
82
|
public static final int CLONE_VERTEX = 0x4;
|
83
|
/**
|
84
|
* When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
|
85
|
* <p>
|
86
|
* This way we can have two different DistortedEffects sharing the FRAGMENT queue.
|
87
|
*/
|
88
|
public static final int CLONE_FRAGMENT= 0x8;
|
89
|
/**
|
90
|
* When creating an instance of a DistortedEffects from another instance, clone the PostProcess Effects.
|
91
|
* <p>
|
92
|
* This way we can have two different DistortedEffects sharing the POSTPROCESS queue.
|
93
|
*/
|
94
|
public static final int CLONE_POSTPROCESS= 0x10;
|
95
|
/**
|
96
|
* When creating an instance of a DistortedNode from another instance, clone the children Nodes.
|
97
|
* <p>
|
98
|
* This is mainly useful for creating many similar sub-trees and rendering then at different places
|
99
|
* on the screen with (optionally) different Effects.
|
100
|
*/
|
101
|
public static final int CLONE_CHILDREN= 0x20;
|
102
|
|
103
|
/**
|
104
|
* When creating a DistortedScreen (which needs to have mFBOQueueSize FBOs attached), pass this
|
105
|
* constant for 'numOfFBOs' and the number of backing FBOs will be taken from 'mFBOQueueSize'
|
106
|
* (the value of which is most likely unknown at the time of creation of the Screen)
|
107
|
*/
|
108
|
public static final int WAIT_FOR_FBO_QUEUE_SIZE = -1;
|
109
|
/**
|
110
|
* Work around bugs in ARM Mali driver by, instead to a single FBO, rendering to a circular queue
|
111
|
* of mFBOQueueSize FBOs. (otherwise we sometimes get a 'full pipeline flush' and the end result
|
112
|
* might be missing part of the Objects)
|
113
|
*
|
114
|
* This bug only exists on Mali driver r12. (or more precisely it's there in r12 but fixed in r22)
|
115
|
*
|
116
|
* https://community.arm.com/graphics/f/discussions/10285/opengl-es-3-1-on-mali-t880-flashes
|
117
|
*/
|
118
|
private static int mFBOQueueSize;
|
119
|
|
120
|
private static boolean mInitialized=false;
|
121
|
|
122
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
123
|
/// MAIN PROGRAM ///
|
124
|
private static DistortedProgram mMainProgram;
|
125
|
private static int mMainTextureH;
|
126
|
private static int mTransformFeedbackH;
|
127
|
|
128
|
/// NORMAL PROGRAM /////
|
129
|
private static DistortedProgram mNormalProgram;
|
130
|
private static int mNormalProjectionH;
|
131
|
|
132
|
/// MAIN OIT PROGRAM ///
|
133
|
private static DistortedProgram mMainOITProgram;
|
134
|
private static int mMainOITTextureH;
|
135
|
private static int mMainOITSizeH;
|
136
|
private static int mMainOITNumRecordsH;
|
137
|
|
138
|
/// BLIT PROGRAM ///
|
139
|
private static DistortedProgram mBlitProgram;
|
140
|
private static int mBlitTextureH;
|
141
|
private static int mBlitDepthH;
|
142
|
private static final FloatBuffer mQuadPositions;
|
143
|
|
144
|
/// FULL PROGRAM ///
|
145
|
private static DistortedProgram mFullProgram;
|
146
|
|
147
|
static
|
148
|
{
|
149
|
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f };
|
150
|
mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
151
|
mQuadPositions.put(positionData).position(0);
|
152
|
}
|
153
|
|
154
|
/// BLIT DEPTH PROGRAM ///
|
155
|
private static DistortedProgram mBlitDepthProgram;
|
156
|
private static int mBlitDepthTextureH;
|
157
|
private static int mBlitDepthDepthTextureH;
|
158
|
private static int mBlitDepthDepthH;
|
159
|
private static int mBlitDepthTexCorrH;
|
160
|
|
161
|
/// Program Handles ///
|
162
|
private static int mMainProgramH, mFullProgramH, mMainOITProgramH;
|
163
|
|
164
|
/// OIT SSBO BUFFER ///
|
165
|
private static int[] mLinkedListSSBO = new int[1];
|
166
|
private static int[] mAtomicCounter;
|
167
|
private static int mCurrBuffer;
|
168
|
|
169
|
static
|
170
|
{
|
171
|
mLinkedListSSBO[0]= -1;
|
172
|
mCurrBuffer = 0;
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////
|
176
|
// meaning: allocate 1.0 screenful of places for transparent
|
177
|
// fragments in the SSBO backing up the OIT render method.
|
178
|
private static float mBufferSize=1.0f;
|
179
|
|
180
|
/// OIT CLEAR PROGRAM ///
|
181
|
private static DistortedProgram mOITClearProgram;
|
182
|
private static int mOITClearDepthH;
|
183
|
private static int mOITClearTexCorrH;
|
184
|
private static int mOITClearSizeH;
|
185
|
|
186
|
/// OIT BUILD PROGRAM ///
|
187
|
private static DistortedProgram mOITBuildProgram;
|
188
|
private static int mOITBuildTextureH;
|
189
|
private static int mOITBuildDepthTextureH;
|
190
|
private static int mOITBuildDepthH;
|
191
|
private static int mOITBuildTexCorrH;
|
192
|
private static int mOITBuildSizeH;
|
193
|
private static int mOITBuildNumRecordsH;
|
194
|
|
195
|
/// OIT COLLAPSE PROGRAM ///
|
196
|
private static DistortedProgram mOITCollapseProgram;
|
197
|
private static int mOITCollapseDepthTextureH;
|
198
|
private static int mOITCollapseDepthH;
|
199
|
private static int mOITCollapseTexCorrH;
|
200
|
private static int mOITCollapseSizeH;
|
201
|
|
202
|
/// OIT RENDER PROGRAM ///
|
203
|
private static DistortedProgram mOITRenderProgram;
|
204
|
private static int mOITRenderDepthH;
|
205
|
private static int mOITRenderTexCorrH;
|
206
|
private static int mOITRenderSizeH;
|
207
|
|
208
|
/// END PROGRAMS //////
|
209
|
|
210
|
/**
|
211
|
* Every application using the library must implement this interface so that the library can send
|
212
|
* it exceptions that arise. The exceptions may come at any time, for example the library will
|
213
|
* compile its OIT problem only on the first attempt to use the OIT
|
214
|
* Those will mainly be hardware-related: shaders do not compile on particular hardware, the required
|
215
|
* OpenGL ES 3.0 is not supported, etc.
|
216
|
*/
|
217
|
public interface ExceptionListener
|
218
|
{
|
219
|
void distortedException(Exception ex);
|
220
|
}
|
221
|
|
222
|
private static ExceptionListener mListener;
|
223
|
private static Resources mResources;
|
224
|
|
225
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
226
|
// private: hide this from Javadoc
|
227
|
|
228
|
private DistortedLibrary()
|
229
|
{
|
230
|
|
231
|
}
|
232
|
|
233
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
234
|
|
235
|
private static void createMainProgram()
|
236
|
{
|
237
|
// MAIN PROGRAM ////////////////////////////////////
|
238
|
final InputStream mainVertStream = mResources.openRawResource(R.raw.main_vertex_shader);
|
239
|
final InputStream mainFragStream = mResources.openRawResource(R.raw.main_fragment_shader);
|
240
|
|
241
|
int numF = FragmentEffect.getNumEnabled();
|
242
|
int numV = VertexEffect.getNumEnabled();
|
243
|
|
244
|
String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
245
|
String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
|
246
|
|
247
|
mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
|
248
|
|
249
|
String enabledEffectV= VertexEffect.getGLSL();
|
250
|
String enabledEffectF= FragmentEffect.getGLSL();
|
251
|
|
252
|
String[] feedback = { "v_Position", "v_endPosition" };
|
253
|
|
254
|
try
|
255
|
{
|
256
|
mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
257
|
enabledEffectV, enabledEffectF, mGLSL, feedback);
|
258
|
}
|
259
|
catch(Exception e)
|
260
|
{
|
261
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
|
262
|
throw new RuntimeException(e.getMessage());
|
263
|
}
|
264
|
|
265
|
mMainProgramH = mMainProgram.getProgramHandle();
|
266
|
EffectQueue.getUniforms(mMainProgramH,0);
|
267
|
mMainTextureH= GLES30.glGetUniformLocation( mMainProgramH, "u_Texture");
|
268
|
mTransformFeedbackH= GLES30.glGetUniformLocation( mMainProgramH, "u_TransformFeedback");
|
269
|
|
270
|
// BLIT PROGRAM ////////////////////////////////////
|
271
|
final InputStream blitVertStream = mResources.openRawResource(R.raw.blit_vertex_shader);
|
272
|
final InputStream blitFragStream = mResources.openRawResource(R.raw.blit_fragment_shader);
|
273
|
|
274
|
try
|
275
|
{
|
276
|
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
277
|
}
|
278
|
catch(Exception e)
|
279
|
{
|
280
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage());
|
281
|
throw new RuntimeException(e.getMessage());
|
282
|
}
|
283
|
|
284
|
int blitProgramH = mBlitProgram.getProgramHandle();
|
285
|
mBlitTextureH = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
|
286
|
mBlitDepthH = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
|
287
|
|
288
|
// BLIT DEPTH PROGRAM ////////////////////////////////////
|
289
|
final InputStream blitDepthVertStream = mResources.openRawResource(R.raw.blit_depth_vertex_shader);
|
290
|
final InputStream blitDepthFragStream = mResources.openRawResource(R.raw.blit_depth_fragment_shader);
|
291
|
|
292
|
try
|
293
|
{
|
294
|
mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
295
|
}
|
296
|
catch(Exception e)
|
297
|
{
|
298
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage());
|
299
|
throw new RuntimeException(e.getMessage());
|
300
|
}
|
301
|
|
302
|
int blitDepthProgramH = mBlitDepthProgram.getProgramHandle();
|
303
|
mBlitDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
|
304
|
mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
|
305
|
mBlitDepthDepthH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
|
306
|
mBlitDepthTexCorrH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
|
307
|
|
308
|
int[] params = new int[1];
|
309
|
int index = GLES30.glGetUniformBlockIndex(mMainProgramH, "meshAssociation");
|
310
|
GLES30.glGetActiveUniformBlockiv( mMainProgramH, index, GLES30.GL_UNIFORM_BLOCK_DATA_SIZE, params, 0);
|
311
|
MeshBase.setAssociationSize(params[0]);
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
|
316
|
private static void createNormalProgram(Resources resources)
|
317
|
{
|
318
|
// NORMAL PROGRAM //////////////////////////////////////
|
319
|
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader);
|
320
|
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
|
321
|
|
322
|
try
|
323
|
{
|
324
|
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
325
|
}
|
326
|
catch(Exception e)
|
327
|
{
|
328
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage());
|
329
|
throw new RuntimeException(e.getMessage());
|
330
|
}
|
331
|
|
332
|
int normalProgramH = mNormalProgram.getProgramHandle();
|
333
|
mNormalProjectionH = GLES30.glGetUniformLocation( normalProgramH, "u_Projection");
|
334
|
}
|
335
|
|
336
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
337
|
|
338
|
private static void createFullProgram(Resources resources)
|
339
|
{
|
340
|
final InputStream fullVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
341
|
final InputStream fullFragStream = resources.openRawResource(R.raw.main_fragment_shader);
|
342
|
|
343
|
int numV = VertexEffect.getAllEnabled();
|
344
|
|
345
|
String fullVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
346
|
String fullFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + 0 + "\n");
|
347
|
|
348
|
fullVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
|
349
|
|
350
|
String enabledEffectV= VertexEffect.getAllGLSL();
|
351
|
String enabledEffectF= "{}";
|
352
|
|
353
|
fullVertHeader += "#define PREAPPLY\n";
|
354
|
|
355
|
String[] feedback = { "v_Position", "v_endPosition", "v_Inflate" };
|
356
|
|
357
|
try
|
358
|
{
|
359
|
mFullProgram = new DistortedProgram(fullVertStream, fullFragStream, fullVertHeader, fullFragHeader,
|
360
|
enabledEffectV, enabledEffectF, mGLSL, feedback);
|
361
|
}
|
362
|
catch(Exception e)
|
363
|
{
|
364
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile FULL program: "+e.getMessage());
|
365
|
throw new RuntimeException(e.getMessage());
|
366
|
}
|
367
|
|
368
|
mFullProgramH = mFullProgram.getProgramHandle();
|
369
|
EffectQueue.getUniforms(mFullProgramH,3);
|
370
|
}
|
371
|
|
372
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
373
|
|
374
|
private static void createOITProgram(Resources resources)
|
375
|
{
|
376
|
// MAIN OIT PROGRAM ////////////////////////////////
|
377
|
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
378
|
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
|
379
|
|
380
|
int numF = FragmentEffect.getNumEnabled();
|
381
|
int numV = VertexEffect.getNumEnabled();
|
382
|
|
383
|
String mainVertHeader= mGLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n") + ("#define OIT\n");
|
384
|
String mainFragHeader= mGLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + ("#define OIT\n");
|
385
|
|
386
|
mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
|
387
|
|
388
|
String enabledEffectV= VertexEffect.getGLSL();
|
389
|
String enabledEffectF= FragmentEffect.getGLSL();
|
390
|
|
391
|
try
|
392
|
{
|
393
|
mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
394
|
enabledEffectV, enabledEffectF, mGLSL, null);
|
395
|
}
|
396
|
catch(Exception e)
|
397
|
{
|
398
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage());
|
399
|
throw new RuntimeException(e.getMessage());
|
400
|
}
|
401
|
|
402
|
mMainOITProgramH = mMainOITProgram.getProgramHandle();
|
403
|
EffectQueue.getUniforms(mMainOITProgramH,1);
|
404
|
mMainOITTextureH = GLES30.glGetUniformLocation( mMainOITProgramH, "u_Texture");
|
405
|
mMainOITSizeH = GLES30.glGetUniformLocation( mMainOITProgramH, "u_Size");
|
406
|
mMainOITNumRecordsH = GLES30.glGetUniformLocation( mMainOITProgramH, "u_numRecords");
|
407
|
|
408
|
// OIT CLEAR PROGRAM ////////////////////////////////////
|
409
|
final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
410
|
final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader);
|
411
|
|
412
|
try
|
413
|
{
|
414
|
mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
415
|
}
|
416
|
catch(Exception e)
|
417
|
{
|
418
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage());
|
419
|
throw new RuntimeException(e.getMessage());
|
420
|
}
|
421
|
|
422
|
int oitClearProgramH = mOITClearProgram.getProgramHandle();
|
423
|
mOITClearDepthH = GLES30.glGetUniformLocation( oitClearProgramH, "u_Depth");
|
424
|
mOITClearTexCorrH = GLES30.glGetUniformLocation( oitClearProgramH, "u_TexCorr");
|
425
|
mOITClearSizeH = GLES30.glGetUniformLocation( oitClearProgramH, "u_Size");
|
426
|
|
427
|
// OIT BUILD PROGRAM ////////////////////////////////////
|
428
|
final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
429
|
final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader);
|
430
|
|
431
|
try
|
432
|
{
|
433
|
mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
434
|
}
|
435
|
catch(Exception e)
|
436
|
{
|
437
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage());
|
438
|
throw new RuntimeException(e.getMessage());
|
439
|
}
|
440
|
|
441
|
int oitBuildProgramH = mOITBuildProgram.getProgramHandle();
|
442
|
mOITBuildTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Texture");
|
443
|
mOITBuildDepthTextureH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture");
|
444
|
mOITBuildDepthH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Depth");
|
445
|
mOITBuildTexCorrH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_TexCorr");
|
446
|
mOITBuildSizeH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_Size");
|
447
|
mOITBuildNumRecordsH = GLES30.glGetUniformLocation( oitBuildProgramH, "u_numRecords");
|
448
|
|
449
|
// OIT COLLAPSE PROGRAM ///////////////////////////
|
450
|
final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
451
|
final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader);
|
452
|
|
453
|
try
|
454
|
{
|
455
|
mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
456
|
}
|
457
|
catch(Exception e)
|
458
|
{
|
459
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage());
|
460
|
throw new RuntimeException(e.getMessage());
|
461
|
}
|
462
|
|
463
|
int oitCollapseProgramH = mOITCollapseProgram.getProgramHandle();
|
464
|
mOITCollapseDepthTextureH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture");
|
465
|
mOITCollapseDepthH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Depth");
|
466
|
mOITCollapseTexCorrH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr");
|
467
|
mOITCollapseSizeH = GLES30.glGetUniformLocation( oitCollapseProgramH, "u_Size");
|
468
|
|
469
|
// OIT RENDER PROGRAM ///////////////////////////
|
470
|
final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader);
|
471
|
final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader);
|
472
|
|
473
|
try
|
474
|
{
|
475
|
mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream, mGLSL_VERSION, mGLSL_VERSION, mGLSL);
|
476
|
}
|
477
|
catch(Exception e)
|
478
|
{
|
479
|
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage());
|
480
|
throw new RuntimeException(e.getMessage());
|
481
|
}
|
482
|
|
483
|
int oitRenderProgramH = mOITRenderProgram.getProgramHandle();
|
484
|
mOITRenderDepthH = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Depth");
|
485
|
mOITRenderTexCorrH = GLES30.glGetUniformLocation( oitRenderProgramH, "u_TexCorr");
|
486
|
mOITRenderSizeH = GLES30.glGetUniformLocation( oitRenderProgramH, "u_Size");
|
487
|
}
|
488
|
|
489
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
490
|
|
491
|
private static void displayNormals(float[] projection, MeshBase mesh)
|
492
|
{
|
493
|
if( mNormalProgram==null )
|
494
|
{
|
495
|
try
|
496
|
{
|
497
|
createNormalProgram(mResources);
|
498
|
}
|
499
|
catch(Exception ex)
|
500
|
{
|
501
|
mListener.distortedException(ex);
|
502
|
return;
|
503
|
}
|
504
|
}
|
505
|
|
506
|
int num = mesh.getNumVertices();
|
507
|
int tfo = mesh.getTFO();
|
508
|
|
509
|
GLES30.glUniform1i(DistortedLibrary.mTransformFeedbackH, 1);
|
510
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
|
511
|
GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
|
512
|
InternalRenderState.switchOffDrawing();
|
513
|
GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
|
514
|
InternalRenderState.restoreDrawing();
|
515
|
GLES30.glEndTransformFeedback();
|
516
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
|
517
|
|
518
|
mNormalProgram.useProgram();
|
519
|
GLES30.glUniformMatrix4fv(mNormalProjectionH, 1, false, projection, 0);
|
520
|
mesh.bindTransformAttribs(mNormalProgram);
|
521
|
GLES30.glLineWidth(8.0f);
|
522
|
GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*num);
|
523
|
}
|
524
|
|
525
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
526
|
/**
|
527
|
* Execute all VertexEffects and adjust all vertices
|
528
|
*
|
529
|
* @y.exclude
|
530
|
*/
|
531
|
public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
|
532
|
{
|
533
|
if( mFullProgram==null )
|
534
|
{
|
535
|
try
|
536
|
{
|
537
|
createFullProgram(mResources);
|
538
|
}
|
539
|
catch(Exception ex)
|
540
|
{
|
541
|
mListener.distortedException(ex);
|
542
|
return;
|
543
|
}
|
544
|
}
|
545
|
|
546
|
int num = mesh.getNumVertices();
|
547
|
int tfo = mesh.getTFO();
|
548
|
|
549
|
mFullProgram.useProgram();
|
550
|
mesh.bindVertexAttribs(mFullProgram);
|
551
|
queue.compute(1);
|
552
|
queue.send(0.0f,3);
|
553
|
mesh.send(mFullProgramH);
|
554
|
|
555
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
|
556
|
GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
|
557
|
InternalRenderState.switchOffDrawing();
|
558
|
GLES30.glDrawArrays( GLES30.GL_POINTS, 0, num );
|
559
|
InternalRenderState.restoreDrawing();
|
560
|
GLES30.glEndTransformFeedback();
|
561
|
mesh.copyTransformToVertex();
|
562
|
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
|
563
|
}
|
564
|
|
565
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
566
|
|
567
|
static void drawPrivOIT(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
|
568
|
{
|
569
|
if( mMainOITProgram!=null )
|
570
|
{
|
571
|
EffectQueue[] queues = effects.getQueues();
|
572
|
|
573
|
EffectQueue.compute(queues, currTime);
|
574
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
575
|
|
576
|
mMainOITProgram.useProgram();
|
577
|
GLES30.glUniform1i(mMainOITTextureH, 0);
|
578
|
GLES30.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight);
|
579
|
GLES30.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
|
580
|
mesh.bindVertexAttribs(mMainOITProgram);
|
581
|
mesh.send(mMainOITProgramH);
|
582
|
|
583
|
float inflate = mesh.getInflate();
|
584
|
float distance = surface.mDistance;
|
585
|
float mipmap = surface.mMipmap;
|
586
|
float[] projection= surface.mProjectionMatrix;
|
587
|
|
588
|
EffectQueue.send(queues, distance, mipmap, projection, inflate, 1 );
|
589
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
590
|
|
591
|
if( mesh.getShowNormals() )
|
592
|
{
|
593
|
mMainProgram.useProgram();
|
594
|
mesh.send(mMainProgramH);
|
595
|
EffectQueue.send(queues, distance, mipmap, projection, inflate, 0 );
|
596
|
displayNormals(projection,mesh);
|
597
|
}
|
598
|
}
|
599
|
}
|
600
|
|
601
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
602
|
|
603
|
static void drawPriv(DistortedEffects effects, MeshBase mesh, InternalOutputSurface surface, long currTime)
|
604
|
{
|
605
|
if( mMainProgram!=null )
|
606
|
{
|
607
|
EffectQueue[] queues = effects.getQueues();
|
608
|
|
609
|
EffectQueue.compute(queues, currTime);
|
610
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
611
|
|
612
|
mMainProgram.useProgram();
|
613
|
GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
|
614
|
mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
|
615
|
mesh.send(mMainProgramH);
|
616
|
|
617
|
float inflate = mesh.getInflate();
|
618
|
float distance = surface.mDistance;
|
619
|
float mipmap = surface.mMipmap;
|
620
|
float[] projection= surface.mProjectionMatrix;
|
621
|
|
622
|
EffectQueue.send(queues, distance, mipmap, projection, inflate, 0 );
|
623
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
624
|
|
625
|
if( mesh.getShowNormals() ) displayNormals(projection,mesh);
|
626
|
}
|
627
|
}
|
628
|
|
629
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
630
|
|
631
|
static void blitPriv(InternalOutputSurface surface)
|
632
|
{
|
633
|
if( mBlitProgram!=null )
|
634
|
{
|
635
|
mBlitProgram.useProgram();
|
636
|
|
637
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
638
|
GLES30.glUniform1i(mBlitTextureH, 0);
|
639
|
GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
|
640
|
GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
641
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
642
|
}
|
643
|
}
|
644
|
|
645
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
646
|
|
647
|
static void blitDepthPriv(InternalOutputSurface surface, float corrW, float corrH)
|
648
|
{
|
649
|
if( mBlitDepthProgram!=null )
|
650
|
{
|
651
|
mBlitDepthProgram.useProgram();
|
652
|
|
653
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
654
|
GLES30.glUniform1i(mBlitDepthTextureH, 0);
|
655
|
GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
|
656
|
GLES30.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
|
657
|
GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
|
658
|
GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
659
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
660
|
}
|
661
|
}
|
662
|
|
663
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
664
|
// yes it is safe to be mixing 3.0 and 3.1 like that, senior members of the OpenGL discussions forum assert
|
665
|
|
666
|
private static int printPreviousBuffer()
|
667
|
{
|
668
|
int counter = 0;
|
669
|
|
670
|
ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
|
671
|
GLES30.GL_MAP_READ_BIT);
|
672
|
if( atomicBuf!=null )
|
673
|
{
|
674
|
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
|
675
|
counter = atomicIntBuf.get(0);
|
676
|
}
|
677
|
else
|
678
|
{
|
679
|
Log.e("effects", "print: failed to map atomic buffer");
|
680
|
}
|
681
|
|
682
|
GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
|
683
|
|
684
|
return counter;
|
685
|
}
|
686
|
|
687
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
688
|
|
689
|
private static void zeroBuffer()
|
690
|
{
|
691
|
ByteBuffer atomicBuf = (ByteBuffer)GLES30.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
|
692
|
GLES30.GL_MAP_WRITE_BIT|GLES30.GL_MAP_INVALIDATE_BUFFER_BIT);
|
693
|
if( atomicBuf!=null )
|
694
|
{
|
695
|
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
|
696
|
atomicIntBuf.put(0,0);
|
697
|
}
|
698
|
else
|
699
|
{
|
700
|
Log.e("effects", "zero: failed to map atomic buffer");
|
701
|
}
|
702
|
|
703
|
GLES30.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
|
704
|
}
|
705
|
|
706
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
707
|
// reset atomic counter to 0
|
708
|
|
709
|
static int zeroOutAtomic()
|
710
|
{
|
711
|
int counter = 0;
|
712
|
|
713
|
if( mAtomicCounter==null )
|
714
|
{
|
715
|
mAtomicCounter = new int[mFBOQueueSize];
|
716
|
|
717
|
GLES30.glGenBuffers(mFBOQueueSize,mAtomicCounter,0);
|
718
|
|
719
|
for(int i=0; i<mFBOQueueSize; i++)
|
720
|
{
|
721
|
GLES30.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
|
722
|
GLES30.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES30.GL_DYNAMIC_DRAW);
|
723
|
zeroBuffer();
|
724
|
}
|
725
|
}
|
726
|
|
727
|
// reading the value of the buffer on every frame would slow down rendering by
|
728
|
// about 3%; doing it only once every 5 frames affects speed by less than 1%.
|
729
|
if( mCurrBuffer==0 )
|
730
|
{
|
731
|
GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
|
732
|
counter = printPreviousBuffer();
|
733
|
}
|
734
|
|
735
|
if( ++mCurrBuffer>=mFBOQueueSize ) mCurrBuffer = 0;
|
736
|
|
737
|
GLES30.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
|
738
|
zeroBuffer();
|
739
|
|
740
|
return counter;
|
741
|
}
|
742
|
|
743
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
744
|
// Pass1 of the OIT algorithm. Clear per-pixel head-pointers.
|
745
|
|
746
|
static void oitClear(InternalOutputSurface surface, int counter)
|
747
|
{
|
748
|
if( mOITClearProgram==null )
|
749
|
{
|
750
|
if( mGLSL>=310 && !mOITCompilationAttempted )
|
751
|
{
|
752
|
mOITCompilationAttempted = true;
|
753
|
|
754
|
try
|
755
|
{
|
756
|
createOITProgram(mResources);
|
757
|
}
|
758
|
catch(Exception ex)
|
759
|
{
|
760
|
mListener.distortedException(ex);
|
761
|
return;
|
762
|
}
|
763
|
}
|
764
|
else
|
765
|
{
|
766
|
return;
|
767
|
}
|
768
|
}
|
769
|
|
770
|
if( mLinkedListSSBO[0]<0 )
|
771
|
{
|
772
|
GLES30.glGenBuffers(1,mLinkedListSSBO,0);
|
773
|
|
774
|
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
|
775
|
GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
|
776
|
GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
|
777
|
GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
|
778
|
|
779
|
GLES30.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
|
780
|
}
|
781
|
|
782
|
// See if we have overflown the SSBO in one of the previous frames.
|
783
|
// If yes, assume we need to make the SSBO larger.
|
784
|
float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
|
785
|
|
786
|
if( overflow>1.0f )
|
787
|
{
|
788
|
mBufferSize *= (int)(overflow+1.0f);
|
789
|
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
|
790
|
GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
|
791
|
GLES30.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES30.GL_DYNAMIC_READ|GLES30.GL_DYNAMIC_DRAW);
|
792
|
GLES30.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
|
793
|
}
|
794
|
|
795
|
mOITClearProgram.useProgram();
|
796
|
|
797
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
798
|
GLES30.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f ); // corrections do not really matter here - only present because of common vertex shader.
|
799
|
GLES30.glUniform1f( mOITClearDepthH , 1.0f); // likewise depth
|
800
|
GLES30.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight);
|
801
|
GLES30.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
802
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
803
|
}
|
804
|
|
805
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
806
|
// Pass2 of the OIT algorithm - build per-pixel linked lists.
|
807
|
|
808
|
static void oitBuild(InternalOutputSurface surface, float corrW, float corrH)
|
809
|
{
|
810
|
if( mOITBuildProgram!=null )
|
811
|
{
|
812
|
mOITBuildProgram.useProgram();
|
813
|
|
814
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
815
|
GLES30.glUniform1i(mOITBuildTextureH, 0);
|
816
|
GLES30.glUniform1i(mOITBuildDepthTextureH, 1);
|
817
|
GLES30.glUniform2f(mOITBuildTexCorrH, corrW, corrH );
|
818
|
GLES30.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight);
|
819
|
GLES30.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) );
|
820
|
GLES30.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear);
|
821
|
GLES30.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
822
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
823
|
}
|
824
|
}
|
825
|
|
826
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
827
|
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list.
|
828
|
|
829
|
static void oitCollapse(InternalOutputSurface surface, float corrW, float corrH)
|
830
|
{
|
831
|
if( mOITCollapseProgram!=null )
|
832
|
{
|
833
|
mOITCollapseProgram.useProgram();
|
834
|
|
835
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
836
|
GLES30.glUniform1i(mOITCollapseDepthTextureH, 1);
|
837
|
GLES30.glUniform2f(mOITCollapseTexCorrH, corrW, corrH );
|
838
|
GLES30.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight);
|
839
|
GLES30.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear);
|
840
|
GLES30.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
841
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
842
|
}
|
843
|
}
|
844
|
|
845
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
846
|
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists.
|
847
|
|
848
|
static void oitRender(InternalOutputSurface surface, float corrW, float corrH)
|
849
|
{
|
850
|
if( mOITRenderProgram!=null )
|
851
|
{
|
852
|
mOITRenderProgram.useProgram();
|
853
|
|
854
|
GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
|
855
|
GLES30.glUniform2f(mOITRenderTexCorrH, corrW, corrH );
|
856
|
GLES30.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight);
|
857
|
GLES30.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear);
|
858
|
GLES30.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
859
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
860
|
}
|
861
|
}
|
862
|
|
863
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
864
|
|
865
|
static void setSSBOSize(float size)
|
866
|
{
|
867
|
mBufferSize = size;
|
868
|
}
|
869
|
|
870
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
871
|
|
872
|
static int getQueueSize()
|
873
|
{
|
874
|
return mFBOQueueSize;
|
875
|
}
|
876
|
|
877
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
878
|
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22)
|
879
|
// PowerVR GE8100 / GE8300 compiler fails to compile OIT programs.
|
880
|
|
881
|
private static void detectBuggyDriversAndSetQueueSize(int queueSize)
|
882
|
{
|
883
|
String vendor = GLES30.glGetString(GLES30.GL_VENDOR);
|
884
|
String version = GLES30.glGetString(GLES30.GL_VERSION);
|
885
|
String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
|
886
|
|
887
|
mFBOQueueSize = 1;
|
888
|
|
889
|
if( vendor.contains("ARM") )
|
890
|
{
|
891
|
try
|
892
|
{
|
893
|
String regex = ".*r(\\d+)p\\d.*";
|
894
|
Pattern pattern = Pattern.compile(regex);
|
895
|
Matcher matcher = pattern.matcher(version);
|
896
|
|
897
|
if( matcher.find() )
|
898
|
{
|
899
|
String driverVersion = matcher.group(1);
|
900
|
|
901
|
if( driverVersion!=null )
|
902
|
{
|
903
|
int drvVersion = Integer.parseInt(driverVersion);
|
904
|
|
905
|
if( drvVersion<22 )
|
906
|
{
|
907
|
Log.e("DISTORTED", "You are running this on a ARM Mali driver r"+driverVersion+".\n" +
|
908
|
"This is a buggy driver, please update to r22. Inserting workaround which uses a lot of memory.");
|
909
|
|
910
|
mFBOQueueSize = queueSize;
|
911
|
}
|
912
|
}
|
913
|
}
|
914
|
}
|
915
|
catch(Exception ex)
|
916
|
{
|
917
|
android.util.Log.e("library", "exception trying to pattern match version: "+ex.toString());
|
918
|
}
|
919
|
}
|
920
|
else if( vendor.contains("Imagination") )
|
921
|
{
|
922
|
if( renderer.contains("GE8") )
|
923
|
{
|
924
|
Log.e("DISTORTED", "You are running this on a PowerVR GE8XXX.\nDue to a buggy compiler OIT rendering will not work");
|
925
|
Log.e("DISTORTED", "GLSL Version "+GLES30.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION));
|
926
|
}
|
927
|
}
|
928
|
}
|
929
|
|
930
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
931
|
/**
|
932
|
* Return OpenGL ES version supported by the hardware we are running on.
|
933
|
* There are only three possibilities: 300 (OpenGL ES 3.0) or 310 (at least OpenGL ES 3.1)
|
934
|
* or 200 (OpenGL ES 2.0)
|
935
|
*/
|
936
|
public static int getGLSL()
|
937
|
{
|
938
|
return mGLSL;
|
939
|
}
|
940
|
|
941
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
942
|
/**
|
943
|
* Have we called onCreate yet, ie have we initialized the library?
|
944
|
* @return <code>true</code> if the library is initialized and ready for action.
|
945
|
*/
|
946
|
public static boolean isInitialized()
|
947
|
{
|
948
|
return mInitialized;
|
949
|
}
|
950
|
|
951
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
952
|
/**
|
953
|
* When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
|
954
|
* I.e. best called from GLSurfaceView.onCreate().
|
955
|
* <p>
|
956
|
* Needs to be called from a thread holding the OpenGL context.
|
957
|
*
|
958
|
* @param context Context of the App using the library - used to open up Resources and read Shader code.
|
959
|
* @param listener The library will send all (asynchronous!) exceptions there.
|
960
|
*/
|
961
|
public static void onCreate(final Context context, final ExceptionListener listener)
|
962
|
{
|
963
|
onCreate(context,listener,4);
|
964
|
}
|
965
|
|
966
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
967
|
/**
|
968
|
* When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
|
969
|
* I.e. best called from GLSurfaceView.onCreate().
|
970
|
* <p>
|
971
|
* Needs to be called from a thread holding the OpenGL context.
|
972
|
*
|
973
|
* @param context Context of the App using the library - used to open up Resources and read Shader code.
|
974
|
* @param listener The library will send all (asynchronous!) exceptions there.
|
975
|
* @param queueSize the size of the FBO queue, a workaround for the bug on Mali drivers.
|
976
|
*/
|
977
|
public static void onCreate(final Context context, final ExceptionListener listener, int queueSize)
|
978
|
{
|
979
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
980
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
981
|
|
982
|
int glESversion = configurationInfo.reqGlEsVersion;
|
983
|
int major = glESversion >> 16;
|
984
|
int minor = glESversion & 0xff;
|
985
|
|
986
|
mListener = listener;
|
987
|
|
988
|
if( major< 3 )
|
989
|
{
|
990
|
mGLSL = 100*major + 10*minor;
|
991
|
VertexCompilationException ex = new VertexCompilationException("at least OpenGL ES 3.0 required, this device supports only "+major+"."+minor);
|
992
|
mListener.distortedException(ex);
|
993
|
}
|
994
|
else
|
995
|
{
|
996
|
mGLSL = (major==3 && minor==0) ? 300 : 310;
|
997
|
}
|
998
|
|
999
|
android.util.Log.e("DISTORTED", "Using OpenGL ES "+major+"."+minor);
|
1000
|
mGLSL_VERSION = "#version "+mGLSL+" es\n";
|
1001
|
|
1002
|
mInitialized = true;
|
1003
|
mOITCompilationAttempted = false;
|
1004
|
|
1005
|
detectBuggyDriversAndSetQueueSize(queueSize);
|
1006
|
|
1007
|
EffectMessageSender.startSending();
|
1008
|
|
1009
|
mResources = context.getResources();
|
1010
|
|
1011
|
try
|
1012
|
{
|
1013
|
createMainProgram();
|
1014
|
}
|
1015
|
catch(Exception ex)
|
1016
|
{
|
1017
|
mListener.distortedException(ex);
|
1018
|
}
|
1019
|
|
1020
|
try
|
1021
|
{
|
1022
|
EffectQueuePostprocess.createPrograms(mResources, mGLSL);
|
1023
|
}
|
1024
|
catch(Exception ex)
|
1025
|
{
|
1026
|
mListener.distortedException(ex);
|
1027
|
}
|
1028
|
|
1029
|
try
|
1030
|
{
|
1031
|
PostprocessEffect.createPrograms(mGLSL);
|
1032
|
}
|
1033
|
catch(Exception ex)
|
1034
|
{
|
1035
|
mListener.distortedException(ex);
|
1036
|
}
|
1037
|
}
|
1038
|
|
1039
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1040
|
/**
|
1041
|
* Call this so that the Library can release the OpenGL related data that needs to be recreated.
|
1042
|
* Must be called from Activity.onPause().
|
1043
|
*/
|
1044
|
public static void onPause()
|
1045
|
{
|
1046
|
InternalObject.onPause();
|
1047
|
Dynamic.onPause();
|
1048
|
mLinkedListSSBO[0]= -1;
|
1049
|
mAtomicCounter = null;
|
1050
|
|
1051
|
mFullProgram = null;
|
1052
|
mNormalProgram = null;
|
1053
|
mOITRenderProgram = null;
|
1054
|
mOITCollapseProgram = null;
|
1055
|
mOITBuildProgram = null;
|
1056
|
mOITClearProgram = null;
|
1057
|
}
|
1058
|
|
1059
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1060
|
/**
|
1061
|
* Call this so that the Library can release its internal data structures.
|
1062
|
* Must be called from Activity.onDestroy().
|
1063
|
*/
|
1064
|
public static void onDestroy()
|
1065
|
{
|
1066
|
InternalObject.onDestroy();
|
1067
|
InternalNodeData.onDestroy();
|
1068
|
InternalMaster.onDestroy();
|
1069
|
InternalOutputSurface.onDestroy();
|
1070
|
DistortedEffects.onDestroy();
|
1071
|
EffectQueue.onDestroy();
|
1072
|
Effect.onDestroy();
|
1073
|
DeferredJobs.onDestroy();
|
1074
|
EffectMessageSender.stopSending();
|
1075
|
|
1076
|
mInitialized = false;
|
1077
|
mOITCompilationAttempted = false;
|
1078
|
|
1079
|
mNormalProgram = null;
|
1080
|
mMainOITProgram = null;
|
1081
|
mMainProgram = null;
|
1082
|
mFullProgram = null;
|
1083
|
mOITClearProgram = null;
|
1084
|
mOITBuildProgram = null;
|
1085
|
mOITCollapseProgram= null;
|
1086
|
mOITRenderProgram = null;
|
1087
|
mBlitDepthProgram = null;
|
1088
|
mBlitProgram = null;
|
1089
|
}
|
1090
|
|
1091
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1092
|
/**
|
1093
|
* Returns the maximum number of effects of a given type that can be simultaneously applied to a
|
1094
|
* single (InputSurface,MeshBase) combo.
|
1095
|
*
|
1096
|
* @param type {@link EffectType}
|
1097
|
* @return The maximum number of effects of a given type.
|
1098
|
*/
|
1099
|
@SuppressWarnings("unused")
|
1100
|
public static int getMax(EffectType type)
|
1101
|
{
|
1102
|
return EffectQueue.getMax(type.ordinal());
|
1103
|
}
|
1104
|
|
1105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
1106
|
/**
|
1107
|
* Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
|
1108
|
* This can fail if:
|
1109
|
* <ul>
|
1110
|
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE)
|
1111
|
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
|
1112
|
* before the Vertex Shader gets compiled, i.e. before the call to {@link DistortedLibrary#onCreate}. After this
|
1113
|
* time only decreasing the value of 'max' is permitted.
|
1114
|
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
|
1115
|
* </ul>
|
1116
|
*
|
1117
|
* @param type {@link EffectType}
|
1118
|
* @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
|
1119
|
* than Byte.MAX_VALUE
|
1120
|
* @return <code>true</code> if operation was successful, <code>false</code> otherwise.
|
1121
|
*/
|
1122
|
@SuppressWarnings("unused")
|
1123
|
public static boolean setMax(EffectType type, int max)
|
1124
|
{
|
1125
|
return EffectQueue.setMax(type.ordinal(),max);
|
1126
|
}
|
1127
|
}
|