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