Revision bfe45b4a
Added by Leszek Koltunski over 5 years ago
src/main/java/org/distorted/library/main/Distorted.java | ||
---|---|---|
24 | 24 |
import android.content.pm.ConfigurationInfo; |
25 | 25 |
import android.content.res.Resources; |
26 | 26 |
import android.opengl.GLES31; |
27 |
import android.util.Log; |
|
27 | 28 |
|
29 |
import org.distorted.library.R; |
|
28 | 30 |
import org.distorted.library.effect.Effect; |
31 |
import org.distorted.library.effect.EffectType; |
|
32 |
import org.distorted.library.effect.FragmentEffect; |
|
29 | 33 |
import org.distorted.library.effect.PostprocessEffect; |
34 |
import org.distorted.library.effect.VertexEffect; |
|
35 |
import org.distorted.library.mesh.MeshBase; |
|
30 | 36 |
import org.distorted.library.message.EffectMessageSender; |
37 |
import org.distorted.library.program.DistortedProgram; |
|
38 |
|
|
39 |
import java.io.InputStream; |
|
40 |
import java.nio.ByteBuffer; |
|
41 |
import java.nio.ByteOrder; |
|
42 |
import java.nio.FloatBuffer; |
|
43 |
import java.nio.IntBuffer; |
|
31 | 44 |
|
32 | 45 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
33 | 46 |
/** |
... | ... | |
94 | 107 |
|
95 | 108 |
private static boolean mInitialized=false; |
96 | 109 |
|
110 |
////////////////////////////////////////////////////////////////////////////////////////////// |
|
111 |
/// MAIN PROGRAM /// |
|
112 |
private static DistortedProgram mMainProgram; |
|
113 |
private static int mMainTextureH; |
|
114 |
|
|
115 |
/// NORMAL PROGRAM ///// |
|
116 |
private static DistortedProgram mNormalProgram; |
|
117 |
private static int mNormalMVPMatrixH; |
|
118 |
|
|
119 |
/// MAIN OIT PROGRAM /// |
|
120 |
private static DistortedProgram mMainOITProgram; |
|
121 |
private static int mMainOITTextureH; |
|
122 |
private static int mMainOITSizeH; |
|
123 |
private static int mMainOITNumRecordsH; |
|
124 |
|
|
125 |
/// BLIT PROGRAM /// |
|
126 |
private static DistortedProgram mBlitProgram; |
|
127 |
private static int mBlitTextureH; |
|
128 |
private static int mBlitDepthH; |
|
129 |
private static final FloatBuffer mQuadPositions; |
|
130 |
|
|
131 |
static |
|
132 |
{ |
|
133 |
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f }; |
|
134 |
mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
135 |
mQuadPositions.put(positionData).position(0); |
|
136 |
} |
|
137 |
|
|
138 |
/// BLIT DEPTH PROGRAM /// |
|
139 |
private static DistortedProgram mBlitDepthProgram; |
|
140 |
private static int mBlitDepthTextureH; |
|
141 |
private static int mBlitDepthDepthTextureH; |
|
142 |
private static int mBlitDepthDepthH; |
|
143 |
private static int mBlitDepthTexCorrH; |
|
144 |
|
|
145 |
/// OIT SSBO BUFFER /// |
|
146 |
private static int[] mLinkedListSSBO = new int[1]; |
|
147 |
private static int[] mAtomicCounter = new int[Distorted.FBO_QUEUE_SIZE]; |
|
148 |
private static int mCurrBuffer; |
|
149 |
|
|
150 |
static |
|
151 |
{ |
|
152 |
mLinkedListSSBO[0]= -1; |
|
153 |
mCurrBuffer = 0; |
|
154 |
|
|
155 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1; |
|
156 |
} |
|
157 |
|
|
158 |
/////////////////////////////////////////////////////////////// |
|
159 |
// meaning: allocate 1.0 screenful of places for transparent |
|
160 |
// fragments in the SSBO backing up the OIT render method. |
|
161 |
private static float mBufferSize=1.0f; |
|
162 |
|
|
163 |
/// OIT CLEAR PROGRAM /// |
|
164 |
private static DistortedProgram mOITClearProgram; |
|
165 |
private static int mOITClearDepthH; |
|
166 |
private static int mOITClearTexCorrH; |
|
167 |
private static int mOITClearSizeH; |
|
168 |
|
|
169 |
/// OIT BUILD PROGRAM /// |
|
170 |
private static DistortedProgram mOITBuildProgram; |
|
171 |
private static int mOITBuildTextureH; |
|
172 |
private static int mOITBuildDepthTextureH; |
|
173 |
private static int mOITBuildDepthH; |
|
174 |
private static int mOITBuildTexCorrH; |
|
175 |
private static int mOITBuildSizeH; |
|
176 |
private static int mOITBuildNumRecordsH; |
|
177 |
|
|
178 |
/// OIT COLLAPSE PROGRAM /// |
|
179 |
private static DistortedProgram mOITCollapseProgram; |
|
180 |
private static int mOITCollapseDepthTextureH; |
|
181 |
private static int mOITCollapseDepthH; |
|
182 |
private static int mOITCollapseTexCorrH; |
|
183 |
private static int mOITCollapseSizeH; |
|
184 |
|
|
185 |
/// OIT RENDER PROGRAM /// |
|
186 |
private static DistortedProgram mOITRenderProgram; |
|
187 |
private static int mOITRenderDepthH; |
|
188 |
private static int mOITRenderTexCorrH; |
|
189 |
private static int mOITRenderSizeH; |
|
190 |
|
|
191 |
/// END PROGRAMS ////// |
|
192 |
|
|
97 | 193 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
98 | 194 |
// private: hide this from Javadoc |
99 | 195 |
|
... | ... | |
102 | 198 |
|
103 | 199 |
} |
104 | 200 |
|
201 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
202 |
|
|
203 |
static void createPrograms(Resources resources) |
|
204 |
{ |
|
205 |
// MAIN PROGRAM //////////////////////////////////// |
|
206 |
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader); |
|
207 |
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader); |
|
208 |
|
|
209 |
int numF = FragmentEffect.getNumEnabled(); |
|
210 |
int numV = VertexEffect.getNumEnabled(); |
|
211 |
|
|
212 |
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n"); |
|
213 |
String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n"); |
|
214 |
String enabledEffectV= VertexEffect.getGLSL(); |
|
215 |
String enabledEffectF= FragmentEffect.getGLSL(); |
|
216 |
|
|
217 |
String[] feedback = { "v_Position", "v_endPosition" }; |
|
218 |
|
|
219 |
try |
|
220 |
{ |
|
221 |
mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader, |
|
222 |
enabledEffectV, enabledEffectF, Distorted.GLSL, feedback); |
|
223 |
} |
|
224 |
catch(Exception e) |
|
225 |
{ |
|
226 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage()); |
|
227 |
throw new RuntimeException(e.getMessage()); |
|
228 |
} |
|
229 |
|
|
230 |
int mainProgramH = mMainProgram.getProgramHandle(); |
|
231 |
EffectQueue.getUniforms(mainProgramH,0); |
|
232 |
mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture"); |
|
233 |
|
|
234 |
// BLIT PROGRAM //////////////////////////////////// |
|
235 |
final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader); |
|
236 |
final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader); |
|
237 |
|
|
238 |
try |
|
239 |
{ |
|
240 |
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
241 |
} |
|
242 |
catch(Exception e) |
|
243 |
{ |
|
244 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage()); |
|
245 |
throw new RuntimeException(e.getMessage()); |
|
246 |
} |
|
247 |
|
|
248 |
int blitProgramH = mBlitProgram.getProgramHandle(); |
|
249 |
mBlitTextureH = GLES31.glGetUniformLocation( blitProgramH, "u_Texture"); |
|
250 |
mBlitDepthH = GLES31.glGetUniformLocation( blitProgramH, "u_Depth"); |
|
251 |
|
|
252 |
// BLIT DEPTH PROGRAM //////////////////////////////////// |
|
253 |
final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader); |
|
254 |
final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader); |
|
255 |
|
|
256 |
try |
|
257 |
{ |
|
258 |
mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
259 |
} |
|
260 |
catch(Exception e) |
|
261 |
{ |
|
262 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage()); |
|
263 |
throw new RuntimeException(e.getMessage()); |
|
264 |
} |
|
265 |
|
|
266 |
int blitDepthProgramH = mBlitDepthProgram.getProgramHandle(); |
|
267 |
mBlitDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture"); |
|
268 |
mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture"); |
|
269 |
mBlitDepthDepthH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth"); |
|
270 |
mBlitDepthTexCorrH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr"); |
|
271 |
|
|
272 |
// NORMAL PROGRAM ////////////////////////////////////// |
|
273 |
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader); |
|
274 |
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader); |
|
275 |
|
|
276 |
try |
|
277 |
{ |
|
278 |
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL); |
|
279 |
} |
|
280 |
catch(Exception e) |
|
281 |
{ |
|
282 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage()); |
|
283 |
throw new RuntimeException(e.getMessage()); |
|
284 |
} |
|
285 |
|
|
286 |
int normalProgramH = mNormalProgram.getProgramHandle(); |
|
287 |
mNormalMVPMatrixH = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix"); |
|
288 |
} |
|
289 |
|
|
290 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
291 |
|
|
292 |
static void createProgramsOIT(Resources resources) |
|
293 |
{ |
|
294 |
// MAIN OIT PROGRAM //////////////////////////////// |
|
295 |
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader); |
|
296 |
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader); |
|
297 |
|
|
298 |
int numF = FragmentEffect.getNumEnabled(); |
|
299 |
int numV = VertexEffect.getNumEnabled(); |
|
300 |
|
|
301 |
String mainVertHeader= Distorted.GLSL_VERSION + |
|
302 |
("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n") + |
|
303 |
("#define OIT\n"); |
|
304 |
String mainFragHeader= Distorted.GLSL_VERSION + |
|
305 |
("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + |
|
306 |
("#define OIT\n"); |
|
307 |
|
|
308 |
String enabledEffectV= VertexEffect.getGLSL(); |
|
309 |
String enabledEffectF= FragmentEffect.getGLSL(); |
|
310 |
|
|
311 |
try |
|
312 |
{ |
|
313 |
mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader, |
|
314 |
enabledEffectV, enabledEffectF, Distorted.GLSL, null); |
|
315 |
} |
|
316 |
catch(Exception e) |
|
317 |
{ |
|
318 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage()); |
|
319 |
throw new RuntimeException(e.getMessage()); |
|
320 |
} |
|
321 |
|
|
322 |
int mainOITProgramH = mMainOITProgram.getProgramHandle(); |
|
323 |
EffectQueue.getUniforms(mainOITProgramH,1); |
|
324 |
mMainOITTextureH = GLES31.glGetUniformLocation( mainOITProgramH, "u_Texture"); |
|
325 |
mMainOITSizeH = GLES31.glGetUniformLocation( mainOITProgramH, "u_Size"); |
|
326 |
mMainOITNumRecordsH = GLES31.glGetUniformLocation( mainOITProgramH, "u_numRecords"); |
|
327 |
|
|
328 |
// OIT CLEAR PROGRAM //////////////////////////////////// |
|
329 |
final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
330 |
final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader); |
|
331 |
|
|
332 |
try |
|
333 |
{ |
|
334 |
mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
335 |
} |
|
336 |
catch(Exception e) |
|
337 |
{ |
|
338 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage()); |
|
339 |
throw new RuntimeException(e.getMessage()); |
|
340 |
} |
|
341 |
|
|
342 |
int oitClearProgramH = mOITClearProgram.getProgramHandle(); |
|
343 |
mOITClearDepthH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Depth"); |
|
344 |
mOITClearTexCorrH = GLES31.glGetUniformLocation( oitClearProgramH, "u_TexCorr"); |
|
345 |
mOITClearSizeH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Size"); |
|
346 |
|
|
347 |
// OIT BUILD PROGRAM //////////////////////////////////// |
|
348 |
final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
349 |
final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader); |
|
350 |
|
|
351 |
try |
|
352 |
{ |
|
353 |
mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
354 |
} |
|
355 |
catch(Exception e) |
|
356 |
{ |
|
357 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage()); |
|
358 |
throw new RuntimeException(e.getMessage()); |
|
359 |
} |
|
360 |
|
|
361 |
int oitBuildProgramH = mOITBuildProgram.getProgramHandle(); |
|
362 |
mOITBuildTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Texture"); |
|
363 |
mOITBuildDepthTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture"); |
|
364 |
mOITBuildDepthH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Depth"); |
|
365 |
mOITBuildTexCorrH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_TexCorr"); |
|
366 |
mOITBuildSizeH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Size"); |
|
367 |
mOITBuildNumRecordsH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_numRecords"); |
|
368 |
|
|
369 |
// OIT COLLAPSE PROGRAM /////////////////////////// |
|
370 |
final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
371 |
final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader); |
|
372 |
|
|
373 |
try |
|
374 |
{ |
|
375 |
mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
376 |
} |
|
377 |
catch(Exception e) |
|
378 |
{ |
|
379 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage()); |
|
380 |
throw new RuntimeException(e.getMessage()); |
|
381 |
} |
|
382 |
|
|
383 |
int oitCollapseProgramH = mOITCollapseProgram.getProgramHandle(); |
|
384 |
mOITCollapseDepthTextureH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture"); |
|
385 |
mOITCollapseDepthH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Depth"); |
|
386 |
mOITCollapseTexCorrH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr"); |
|
387 |
mOITCollapseSizeH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Size"); |
|
388 |
|
|
389 |
// OIT RENDER PROGRAM /////////////////////////// |
|
390 |
final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
391 |
final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader); |
|
392 |
|
|
393 |
try |
|
394 |
{ |
|
395 |
mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
396 |
} |
|
397 |
catch(Exception e) |
|
398 |
{ |
|
399 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage()); |
|
400 |
throw new RuntimeException(e.getMessage()); |
|
401 |
} |
|
402 |
|
|
403 |
int oitRenderProgramH = mOITRenderProgram.getProgramHandle(); |
|
404 |
mOITRenderDepthH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Depth"); |
|
405 |
mOITRenderTexCorrH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_TexCorr"); |
|
406 |
mOITRenderSizeH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Size"); |
|
407 |
} |
|
408 |
|
|
409 |
|
|
410 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
411 |
|
|
412 |
private static void displayNormals(EffectQueue[] queues, MeshBase mesh) |
|
413 |
{ |
|
414 |
int num = mesh.getNumVertices(); |
|
415 |
int tfo = mesh.getTFO(); |
|
416 |
|
|
417 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo ); |
|
418 |
GLES31.glBeginTransformFeedback( GLES31.GL_POINTS); |
|
419 |
DistortedRenderState.switchOffDrawing(); |
|
420 |
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, num ); |
|
421 |
DistortedRenderState.restoreDrawing(); |
|
422 |
GLES31.glEndTransformFeedback(); |
|
423 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); |
|
424 |
|
|
425 |
Distorted.mNormalProgram.useProgram(); |
|
426 |
GLES31.glUniformMatrix4fv(Distorted.mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(queues) , 0); |
|
427 |
mesh.bindTransformAttribs(Distorted.mNormalProgram); |
|
428 |
GLES31.glLineWidth(8.0f); |
|
429 |
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*num); |
|
430 |
} |
|
431 |
|
|
432 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
433 |
|
|
434 |
static void drawPrivOIT(EffectQueue[] queues, float halfW, float halfH, MeshBase mesh, DistortedOutputSurface surface, long currTime) |
|
435 |
{ |
|
436 |
float halfZ = halfW*mesh.getZFactor(); |
|
437 |
|
|
438 |
EffectQueue.compute(queues, currTime, halfW, halfH, halfZ ); |
|
439 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
440 |
|
|
441 |
Distorted.mMainOITProgram.useProgram(); |
|
442 |
GLES31.glUniform1i(Distorted.mMainOITTextureH, 0); |
|
443 |
GLES31.glUniform2ui(Distorted.mMainOITSizeH, surface.mWidth, surface.mHeight); |
|
444 |
GLES31.glUniform1ui(Distorted.mMainOITNumRecordsH, (int)(Distorted.mBufferSize*surface.mWidth*surface.mHeight) ); |
|
445 |
|
|
446 |
mesh.bindVertexAttribs(Distorted.mMainOITProgram); |
|
447 |
|
|
448 |
float inflate = mesh.getInflate(); |
|
449 |
|
|
450 |
EffectQueue.send(queues, surface, inflate, halfW, halfH, halfZ, 1 ); |
|
451 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() ); |
|
452 |
|
|
453 |
if( mesh.getShowNormals() ) |
|
454 |
{ |
|
455 |
Distorted.mMainProgram.useProgram(); |
|
456 |
EffectQueue.send(queues, surface, inflate, halfW, halfH, halfZ, 0 ); |
|
457 |
displayNormals(queues,mesh); |
|
458 |
} |
|
459 |
} |
|
460 |
|
|
461 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
462 |
|
|
463 |
static void drawPriv(EffectQueue[] queues, float halfW, float halfH, MeshBase mesh, DistortedOutputSurface surface, long currTime) |
|
464 |
{ |
|
465 |
float halfZ = halfW*mesh.getZFactor(); |
|
466 |
|
|
467 |
EffectQueue.compute(queues, currTime, halfW, halfH, halfZ ); |
|
468 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
469 |
Distorted.mMainProgram.useProgram(); |
|
470 |
GLES31.glUniform1i(Distorted.mMainTextureH, 0); |
|
471 |
mesh.bindVertexAttribs(Distorted.mMainProgram); |
|
472 |
EffectQueue.send(queues, surface, mesh.getInflate(), halfW, halfH, halfZ, 0 ); |
|
473 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() ); |
|
474 |
|
|
475 |
if( mesh.getShowNormals() ) displayNormals(queues,mesh); |
|
476 |
} |
|
477 |
|
|
478 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
479 |
|
|
480 |
static void blitPriv(DistortedOutputSurface surface) |
|
481 |
{ |
|
482 |
mBlitProgram.useProgram(); |
|
483 |
|
|
484 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
485 |
GLES31.glUniform1i(mBlitTextureH, 0); |
|
486 |
GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear); |
|
487 |
GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
488 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
489 |
} |
|
490 |
|
|
491 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
492 |
|
|
493 |
static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH) |
|
494 |
{ |
|
495 |
mBlitDepthProgram.useProgram(); |
|
496 |
|
|
497 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
498 |
GLES31.glUniform1i(mBlitDepthTextureH, 0); |
|
499 |
GLES31.glUniform1i(mBlitDepthDepthTextureH, 1); |
|
500 |
GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH ); |
|
501 |
GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear); |
|
502 |
GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
503 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
504 |
} |
|
505 |
|
|
506 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
507 |
|
|
508 |
private static int printPreviousBuffer() |
|
509 |
{ |
|
510 |
int counter = 0; |
|
511 |
|
|
512 |
ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4, |
|
513 |
GLES31.GL_MAP_READ_BIT); |
|
514 |
if( atomicBuf!=null ) |
|
515 |
{ |
|
516 |
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer(); |
|
517 |
counter = atomicIntBuf.get(0); |
|
518 |
} |
|
519 |
else |
|
520 |
{ |
|
521 |
android.util.Log.e("effects", "print: failed to map atomic buffer"); |
|
522 |
} |
|
523 |
|
|
524 |
GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER); |
|
525 |
|
|
526 |
return counter; |
|
527 |
} |
|
528 |
|
|
529 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
530 |
|
|
531 |
private static void zeroBuffer() |
|
532 |
{ |
|
533 |
ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4, |
|
534 |
GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT); |
|
535 |
if( atomicBuf!=null ) |
|
536 |
{ |
|
537 |
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer(); |
|
538 |
atomicIntBuf.put(0,0); |
|
539 |
} |
|
540 |
else |
|
541 |
{ |
|
542 |
android.util.Log.e("effects", "zero: failed to map atomic buffer"); |
|
543 |
} |
|
544 |
|
|
545 |
GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER); |
|
546 |
} |
|
547 |
|
|
548 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
549 |
// reset atomic counter to 0 |
|
550 |
|
|
551 |
static int zeroOutAtomic() |
|
552 |
{ |
|
553 |
int counter = 0; |
|
554 |
|
|
555 |
if( mAtomicCounter[0]<0 ) |
|
556 |
{ |
|
557 |
GLES31.glGenBuffers(Distorted.FBO_QUEUE_SIZE,mAtomicCounter,0); |
|
558 |
|
|
559 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) |
|
560 |
{ |
|
561 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]); |
|
562 |
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW); |
|
563 |
zeroBuffer(); |
|
564 |
} |
|
565 |
} |
|
566 |
|
|
567 |
// reading the value of the buffer on every frame would slow down rendering by |
|
568 |
// about 3%; doing it only once every 5 frames affects speed by less than 1%. |
|
569 |
if( mCurrBuffer==0 ) |
|
570 |
{ |
|
571 |
GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]); |
|
572 |
counter = printPreviousBuffer(); |
|
573 |
} |
|
574 |
|
|
575 |
if( ++mCurrBuffer>=Distorted.FBO_QUEUE_SIZE ) mCurrBuffer = 0; |
|
576 |
|
|
577 |
GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]); |
|
578 |
zeroBuffer(); |
|
579 |
|
|
580 |
return counter; |
|
581 |
} |
|
582 |
|
|
583 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
584 |
// Pass1 of the OIT algorithm. Clear per-pixel head-poiners. |
|
585 |
|
|
586 |
static void oitClear(DistortedOutputSurface surface, int counter) |
|
587 |
{ |
|
588 |
if( mLinkedListSSBO[0]<0 ) |
|
589 |
{ |
|
590 |
GLES31.glGenBuffers(1,mLinkedListSSBO,0); |
|
591 |
|
|
592 |
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4); |
|
593 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]); |
|
594 |
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW); |
|
595 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0); |
|
596 |
|
|
597 |
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]); |
|
598 |
} |
|
599 |
|
|
600 |
// See if we have overflown the SSBO in one of the previous frames. |
|
601 |
// If yes, assume we need to make the SSBO larger. |
|
602 |
float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight); |
|
603 |
|
|
604 |
if( overflow>1.0f ) |
|
605 |
{ |
|
606 |
//android.util.Log.e("effects", "previous frame rendered "+counter+ |
|
607 |
// " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space"); |
|
608 |
|
|
609 |
mBufferSize *= (int)(overflow+1.0f); |
|
610 |
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4); |
|
611 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]); |
|
612 |
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW); |
|
613 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0); |
|
614 |
} |
|
615 |
|
|
616 |
mOITClearProgram.useProgram(); |
|
617 |
|
|
618 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
619 |
GLES31.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f ); // corrections do not really matter here - only present because of common vertex shader. |
|
620 |
GLES31.glUniform1f( mOITClearDepthH , 1.0f); // likewise depth |
|
621 |
GLES31.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight); |
|
622 |
GLES31.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
623 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
624 |
} |
|
625 |
|
|
626 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
627 |
// Pass2 of the OIT algorithm - build per-pixel linked lists. |
|
628 |
|
|
629 |
static void oitBuild(DistortedOutputSurface surface, float corrW, float corrH) |
|
630 |
{ |
|
631 |
mOITBuildProgram.useProgram(); |
|
632 |
|
|
633 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
634 |
GLES31.glUniform1i(mOITBuildTextureH, 0); |
|
635 |
GLES31.glUniform1i(mOITBuildDepthTextureH, 1); |
|
636 |
GLES31.glUniform2f(mOITBuildTexCorrH, corrW, corrH ); |
|
637 |
GLES31.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight); |
|
638 |
GLES31.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) ); |
|
639 |
GLES31.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear); |
|
640 |
GLES31.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
641 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
642 |
} |
|
643 |
|
|
644 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
645 |
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list. |
|
646 |
|
|
647 |
static void oitCollapse(DistortedOutputSurface surface, float corrW, float corrH) |
|
648 |
{ |
|
649 |
mOITCollapseProgram.useProgram(); |
|
650 |
|
|
651 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
652 |
GLES31.glUniform1i(mOITCollapseDepthTextureH, 1); |
|
653 |
GLES31.glUniform2f(mOITCollapseTexCorrH, corrW, corrH ); |
|
654 |
GLES31.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight); |
|
655 |
GLES31.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear); |
|
656 |
GLES31.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
657 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
658 |
} |
|
659 |
|
|
660 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
661 |
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists. |
|
662 |
|
|
663 |
static void oitRender(DistortedOutputSurface surface, float corrW, float corrH) |
|
664 |
{ |
|
665 |
mOITRenderProgram.useProgram(); |
|
666 |
|
|
667 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
668 |
GLES31.glUniform2f(mOITRenderTexCorrH, corrW, corrH ); |
|
669 |
GLES31.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight); |
|
670 |
GLES31.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear); |
|
671 |
GLES31.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
672 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
673 |
} |
|
674 |
|
|
675 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
676 |
|
|
677 |
static void setSSBOSize(float size) |
|
678 |
{ |
|
679 |
mBufferSize = size; |
|
680 |
} |
|
681 |
|
|
105 | 682 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
106 | 683 |
// ARM Mali driver r12 has problems when we keep swapping many FBOs (fixed in r22) |
107 | 684 |
// PowerVR GE8100 compiler fails to compile OIT programs. |
... | ... | |
172 | 749 |
|
173 | 750 |
try |
174 | 751 |
{ |
175 |
DistortedEffects.createPrograms(resources);
|
|
752 |
createPrograms(resources); |
|
176 | 753 |
} |
177 | 754 |
catch(Exception ex) |
178 | 755 |
{ |
... | ... | |
181 | 758 |
|
182 | 759 |
try |
183 | 760 |
{ |
184 |
DistortedEffects.createProgramsOIT(resources);
|
|
761 |
createProgramsOIT(resources); |
|
185 | 762 |
} |
186 | 763 |
catch(Exception ex) |
187 | 764 |
{ |
... | ... | |
220 | 797 |
public static void onPause() |
221 | 798 |
{ |
222 | 799 |
DistortedObject.onPause(); |
223 |
DistortedEffects.onPause(); |
|
800 |
|
|
801 |
mLinkedListSSBO[0]= -1; |
|
802 |
|
|
803 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1; |
|
224 | 804 |
} |
225 | 805 |
|
226 | 806 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
241 | 821 |
|
242 | 822 |
mInitialized = false; |
243 | 823 |
} |
824 |
|
|
825 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
826 |
/** |
|
827 |
* Returns the maximum number of effects of a given type that can be simultaneously applied to a |
|
828 |
* single (InputSurface,MeshBase) combo. |
|
829 |
* |
|
830 |
* @param type {@link EffectType} |
|
831 |
* @return The maximum number of effects of a given type. |
|
832 |
*/ |
|
833 |
@SuppressWarnings("unused") |
|
834 |
public static int getMax(EffectType type) |
|
835 |
{ |
|
836 |
return EffectQueue.getMax(type.ordinal()); |
|
837 |
} |
|
838 |
|
|
839 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
840 |
/** |
|
841 |
* Sets the maximum number of effects that can be stored in a single EffectQueue at one time. |
|
842 |
* This can fail if: |
|
843 |
* <ul> |
|
844 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
845 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
846 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
847 |
* time only decreasing the value of 'max' is permitted. |
|
848 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
849 |
* </ul> |
|
850 |
* |
|
851 |
* @param type {@link EffectType} |
|
852 |
* @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater |
|
853 |
* than Byte.MAX_VALUE |
|
854 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
855 |
*/ |
|
856 |
@SuppressWarnings("unused") |
|
857 |
public static boolean setMax(EffectType type, int max) |
|
858 |
{ |
|
859 |
return EffectQueue.setMax(type.ordinal(),max); |
|
860 |
} |
|
244 | 861 |
} |
src/main/java/org/distorted/library/main/DistortedEffects.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.library.main; |
21 | 21 |
|
22 |
import android.content.res.Resources; |
|
23 |
import android.opengl.GLES31; |
|
24 |
import android.util.Log; |
|
25 |
|
|
26 |
import org.distorted.library.R; |
|
27 | 22 |
import org.distorted.library.effect.Effect; |
28 | 23 |
import org.distorted.library.effect.EffectName; |
29 | 24 |
import org.distorted.library.effect.EffectType; |
30 |
import org.distorted.library.effect.FragmentEffect; |
|
31 |
import org.distorted.library.effect.VertexEffect; |
|
32 |
import org.distorted.library.mesh.MeshBase; |
|
33 | 25 |
import org.distorted.library.message.EffectListener; |
34 |
import org.distorted.library.program.DistortedProgram; |
|
35 |
|
|
36 |
import java.io.InputStream; |
|
37 |
import java.nio.ByteBuffer; |
|
38 |
import java.nio.ByteOrder; |
|
39 |
import java.nio.FloatBuffer; |
|
40 |
import java.nio.IntBuffer; |
|
41 | 26 |
|
42 | 27 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 | 28 |
/** |
... | ... | |
47 | 32 |
*/ |
48 | 33 |
public class DistortedEffects |
49 | 34 |
{ |
50 |
/// MAIN PROGRAM /// |
|
51 |
private static DistortedProgram mMainProgram; |
|
52 |
private static int mMainTextureH; |
|
53 |
|
|
54 |
/// BLIT PROGRAM /// |
|
55 |
private static DistortedProgram mBlitProgram; |
|
56 |
private static int mBlitTextureH; |
|
57 |
private static int mBlitDepthH; |
|
58 |
private static final FloatBuffer mQuadPositions; |
|
59 |
|
|
60 |
static |
|
61 |
{ |
|
62 |
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f }; |
|
63 |
mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer(); |
|
64 |
mQuadPositions.put(positionData).position(0); |
|
65 |
} |
|
66 |
|
|
67 |
/// BLIT DEPTH PROGRAM /// |
|
68 |
private static DistortedProgram mBlitDepthProgram; |
|
69 |
private static int mBlitDepthTextureH; |
|
70 |
private static int mBlitDepthDepthTextureH; |
|
71 |
private static int mBlitDepthDepthH; |
|
72 |
private static int mBlitDepthTexCorrH; |
|
73 |
|
|
74 |
/// NORMAL PROGRAM ///// |
|
75 |
private static DistortedProgram mNormalProgram; |
|
76 |
private static int mNormalMVPMatrixH; |
|
77 |
|
|
78 |
/// OIT SSBO BUFFER /// |
|
79 |
private static int[] mLinkedListSSBO = new int[1]; |
|
80 |
private static int[] mAtomicCounter = new int[Distorted.FBO_QUEUE_SIZE]; |
|
81 |
private static int mCurrBuffer; |
|
82 |
|
|
83 |
static |
|
84 |
{ |
|
85 |
mLinkedListSSBO[0]= -1; |
|
86 |
mCurrBuffer = 0; |
|
87 |
|
|
88 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////// |
|
92 |
// meaning: allocate 1.0 screenful of places for transparent |
|
93 |
// fragments in the SSBO backing up the OIT render method. |
|
94 |
private static float mBufferSize=1.0f; |
|
95 |
|
|
96 |
/// OIT CLEAR PROGRAM /// |
|
97 |
private static DistortedProgram mOITClearProgram; |
|
98 |
private static int mOITClearDepthH; |
|
99 |
private static int mOITClearTexCorrH; |
|
100 |
private static int mOITClearSizeH; |
|
101 |
|
|
102 |
/// OIT BUILD PROGRAM /// |
|
103 |
private static DistortedProgram mOITBuildProgram; |
|
104 |
private static int mOITBuildTextureH; |
|
105 |
private static int mOITBuildDepthTextureH; |
|
106 |
private static int mOITBuildDepthH; |
|
107 |
private static int mOITBuildTexCorrH; |
|
108 |
private static int mOITBuildSizeH; |
|
109 |
private static int mOITBuildNumRecordsH; |
|
110 |
|
|
111 |
/// OIT COLLAPSE PROGRAM /// |
|
112 |
private static DistortedProgram mOITCollapseProgram; |
|
113 |
private static int mOITCollapseDepthTextureH; |
|
114 |
private static int mOITCollapseDepthH; |
|
115 |
private static int mOITCollapseTexCorrH; |
|
116 |
private static int mOITCollapseSizeH; |
|
117 |
|
|
118 |
/// OIT RENDER PROGRAM /// |
|
119 |
private static DistortedProgram mOITRenderProgram; |
|
120 |
private static int mOITRenderDepthH; |
|
121 |
private static int mOITRenderTexCorrH; |
|
122 |
private static int mOITRenderSizeH; |
|
123 |
|
|
124 |
/// MAIN OIT PROGRAM /// |
|
125 |
private static DistortedProgram mMainOITProgram; |
|
126 |
private static int mMainOITTextureH; |
|
127 |
private static int mMainOITSizeH; |
|
128 |
private static int mMainOITNumRecordsH; |
|
129 |
/// END PROGRAMS ////// |
|
130 |
|
|
131 | 35 |
private static long mNextID =0; |
132 | 36 |
private long mID; |
133 |
|
|
134 | 37 |
private EffectQueue[] mQueues; |
135 | 38 |
|
136 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
137 |
|
|
138 |
static void createPrograms(Resources resources) |
|
139 |
{ |
|
140 |
// MAIN PROGRAM //////////////////////////////////// |
|
141 |
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader); |
|
142 |
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader); |
|
143 |
|
|
144 |
int numF = FragmentEffect.getNumEnabled(); |
|
145 |
int numV = VertexEffect.getNumEnabled(); |
|
146 |
|
|
147 |
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n"); |
|
148 |
String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n"); |
|
149 |
String enabledEffectV= VertexEffect.getGLSL(); |
|
150 |
String enabledEffectF= FragmentEffect.getGLSL(); |
|
151 |
|
|
152 |
String[] feedback = { "v_Position", "v_endPosition" }; |
|
153 |
|
|
154 |
try |
|
155 |
{ |
|
156 |
mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader, |
|
157 |
enabledEffectV, enabledEffectF, Distorted.GLSL, feedback); |
|
158 |
} |
|
159 |
catch(Exception e) |
|
160 |
{ |
|
161 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage()); |
|
162 |
throw new RuntimeException(e.getMessage()); |
|
163 |
} |
|
164 |
|
|
165 |
int mainProgramH = mMainProgram.getProgramHandle(); |
|
166 |
EffectQueue.getUniforms(mainProgramH,0); |
|
167 |
mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture"); |
|
168 |
|
|
169 |
// BLIT PROGRAM //////////////////////////////////// |
|
170 |
final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader); |
|
171 |
final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader); |
|
172 |
|
|
173 |
try |
|
174 |
{ |
|
175 |
mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
176 |
} |
|
177 |
catch(Exception e) |
|
178 |
{ |
|
179 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT program: "+e.getMessage()); |
|
180 |
throw new RuntimeException(e.getMessage()); |
|
181 |
} |
|
182 |
|
|
183 |
int blitProgramH = mBlitProgram.getProgramHandle(); |
|
184 |
mBlitTextureH = GLES31.glGetUniformLocation( blitProgramH, "u_Texture"); |
|
185 |
mBlitDepthH = GLES31.glGetUniformLocation( blitProgramH, "u_Depth"); |
|
186 |
|
|
187 |
// BLIT DEPTH PROGRAM //////////////////////////////////// |
|
188 |
final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader); |
|
189 |
final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader); |
|
190 |
|
|
191 |
try |
|
192 |
{ |
|
193 |
mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
194 |
} |
|
195 |
catch(Exception e) |
|
196 |
{ |
|
197 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile BLIT DEPTH program: "+e.getMessage()); |
|
198 |
throw new RuntimeException(e.getMessage()); |
|
199 |
} |
|
200 |
|
|
201 |
int blitDepthProgramH = mBlitDepthProgram.getProgramHandle(); |
|
202 |
mBlitDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture"); |
|
203 |
mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture"); |
|
204 |
mBlitDepthDepthH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth"); |
|
205 |
mBlitDepthTexCorrH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr"); |
|
206 |
|
|
207 |
// NORMAL PROGRAM ////////////////////////////////////// |
|
208 |
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader); |
|
209 |
final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader); |
|
210 |
|
|
211 |
try |
|
212 |
{ |
|
213 |
mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL); |
|
214 |
} |
|
215 |
catch(Exception e) |
|
216 |
{ |
|
217 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile NORMAL program: "+e.getMessage()); |
|
218 |
throw new RuntimeException(e.getMessage()); |
|
219 |
} |
|
220 |
|
|
221 |
int normalProgramH = mNormalProgram.getProgramHandle(); |
|
222 |
mNormalMVPMatrixH = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix"); |
|
223 |
} |
|
224 |
|
|
225 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
226 |
|
|
227 |
static void createProgramsOIT(Resources resources) |
|
228 |
{ |
|
229 |
// MAIN OIT PROGRAM //////////////////////////////// |
|
230 |
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader); |
|
231 |
final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader); |
|
232 |
|
|
233 |
int numF = FragmentEffect.getNumEnabled(); |
|
234 |
int numV = VertexEffect.getNumEnabled(); |
|
235 |
|
|
236 |
String mainVertHeader= Distorted.GLSL_VERSION + |
|
237 |
("#define NUM_VERTEX " + ( numV>0 ? getMax(EffectType.VERTEX ) : 0 ) + "\n") + |
|
238 |
("#define OIT\n"); |
|
239 |
String mainFragHeader= Distorted.GLSL_VERSION + |
|
240 |
("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n") + |
|
241 |
("#define OIT\n"); |
|
242 |
|
|
243 |
String enabledEffectV= VertexEffect.getGLSL(); |
|
244 |
String enabledEffectF= FragmentEffect.getGLSL(); |
|
245 |
|
|
246 |
try |
|
247 |
{ |
|
248 |
mMainOITProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader, |
|
249 |
enabledEffectV, enabledEffectF, Distorted.GLSL, null); |
|
250 |
} |
|
251 |
catch(Exception e) |
|
252 |
{ |
|
253 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN OIT program: "+e.getMessage()); |
|
254 |
throw new RuntimeException(e.getMessage()); |
|
255 |
} |
|
256 |
|
|
257 |
int mainOITProgramH = mMainOITProgram.getProgramHandle(); |
|
258 |
EffectQueue.getUniforms(mainOITProgramH,1); |
|
259 |
mMainOITTextureH = GLES31.glGetUniformLocation( mainOITProgramH, "u_Texture"); |
|
260 |
mMainOITSizeH = GLES31.glGetUniformLocation( mainOITProgramH, "u_Size"); |
|
261 |
mMainOITNumRecordsH = GLES31.glGetUniformLocation( mainOITProgramH, "u_numRecords"); |
|
262 |
|
|
263 |
// OIT CLEAR PROGRAM //////////////////////////////////// |
|
264 |
final InputStream oitClearVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
265 |
final InputStream oitClearFragStream = resources.openRawResource(R.raw.oit_clear_fragment_shader); |
|
266 |
|
|
267 |
try |
|
268 |
{ |
|
269 |
mOITClearProgram = new DistortedProgram(oitClearVertStream,oitClearFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
270 |
} |
|
271 |
catch(Exception e) |
|
272 |
{ |
|
273 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT CLEAR program: "+e.getMessage()); |
|
274 |
throw new RuntimeException(e.getMessage()); |
|
275 |
} |
|
276 |
|
|
277 |
int oitClearProgramH = mOITClearProgram.getProgramHandle(); |
|
278 |
mOITClearDepthH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Depth"); |
|
279 |
mOITClearTexCorrH = GLES31.glGetUniformLocation( oitClearProgramH, "u_TexCorr"); |
|
280 |
mOITClearSizeH = GLES31.glGetUniformLocation( oitClearProgramH, "u_Size"); |
|
281 |
|
|
282 |
// OIT BUILD PROGRAM //////////////////////////////////// |
|
283 |
final InputStream oitBuildVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
284 |
final InputStream oitBuildFragStream = resources.openRawResource(R.raw.oit_build_fragment_shader); |
|
285 |
|
|
286 |
try |
|
287 |
{ |
|
288 |
mOITBuildProgram = new DistortedProgram(oitBuildVertStream,oitBuildFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
289 |
} |
|
290 |
catch(Exception e) |
|
291 |
{ |
|
292 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT BUILD program: "+e.getMessage()); |
|
293 |
throw new RuntimeException(e.getMessage()); |
|
294 |
} |
|
295 |
|
|
296 |
int oitBuildProgramH = mOITBuildProgram.getProgramHandle(); |
|
297 |
mOITBuildTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Texture"); |
|
298 |
mOITBuildDepthTextureH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_DepthTexture"); |
|
299 |
mOITBuildDepthH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Depth"); |
|
300 |
mOITBuildTexCorrH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_TexCorr"); |
|
301 |
mOITBuildSizeH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_Size"); |
|
302 |
mOITBuildNumRecordsH = GLES31.glGetUniformLocation( oitBuildProgramH, "u_numRecords"); |
|
303 |
|
|
304 |
// OIT COLLAPSE PROGRAM /////////////////////////// |
|
305 |
final InputStream oitCollapseVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
306 |
final InputStream oitCollapseFragStream = resources.openRawResource(R.raw.oit_collapse_fragment_shader); |
|
307 |
|
|
308 |
try |
|
309 |
{ |
|
310 |
mOITCollapseProgram = new DistortedProgram(oitCollapseVertStream,oitCollapseFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
311 |
} |
|
312 |
catch(Exception e) |
|
313 |
{ |
|
314 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT COLLAPSE program: "+e.getMessage()); |
|
315 |
throw new RuntimeException(e.getMessage()); |
|
316 |
} |
|
317 |
|
|
318 |
int oitCollapseProgramH = mOITCollapseProgram.getProgramHandle(); |
|
319 |
mOITCollapseDepthTextureH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_DepthTexture"); |
|
320 |
mOITCollapseDepthH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Depth"); |
|
321 |
mOITCollapseTexCorrH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_TexCorr"); |
|
322 |
mOITCollapseSizeH = GLES31.glGetUniformLocation( oitCollapseProgramH, "u_Size"); |
|
323 |
|
|
324 |
// OIT RENDER PROGRAM /////////////////////////// |
|
325 |
final InputStream oitRenderVertStream = resources.openRawResource(R.raw.oit_vertex_shader); |
|
326 |
final InputStream oitRenderFragStream = resources.openRawResource(R.raw.oit_render_fragment_shader); |
|
327 |
|
|
328 |
try |
|
329 |
{ |
|
330 |
mOITRenderProgram = new DistortedProgram(oitRenderVertStream,oitRenderFragStream,Distorted.GLSL_VERSION,Distorted.GLSL_VERSION, Distorted.GLSL); |
|
331 |
} |
|
332 |
catch(Exception e) |
|
333 |
{ |
|
334 |
Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile OIT RENDER program: "+e.getMessage()); |
|
335 |
throw new RuntimeException(e.getMessage()); |
|
336 |
} |
|
337 |
|
|
338 |
int oitRenderProgramH = mOITRenderProgram.getProgramHandle(); |
|
339 |
mOITRenderDepthH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Depth"); |
|
340 |
mOITRenderTexCorrH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_TexCorr"); |
|
341 |
mOITRenderSizeH = GLES31.glGetUniformLocation( oitRenderProgramH, "u_Size"); |
|
342 |
} |
|
343 |
|
|
344 | 39 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
345 | 40 |
|
346 | 41 |
EffectQueue[] getQueues() |
... | ... | |
362 | 57 |
EffectQueue.removeNode(mQueues,node); |
363 | 58 |
} |
364 | 59 |
|
365 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
366 |
|
|
367 |
private void displayNormals(MeshBase mesh) |
|
368 |
{ |
|
369 |
int num = mesh.getNumVertices(); |
|
370 |
int tfo = mesh.getTFO(); |
|
371 |
|
|
372 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo ); |
|
373 |
GLES31.glBeginTransformFeedback( GLES31.GL_POINTS); |
|
374 |
DistortedRenderState.switchOffDrawing(); |
|
375 |
GLES31.glDrawArrays( GLES31.GL_POINTS, 0, num ); |
|
376 |
DistortedRenderState.restoreDrawing(); |
|
377 |
GLES31.glEndTransformFeedback(); |
|
378 |
GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0); |
|
379 |
|
|
380 |
mNormalProgram.useProgram(); |
|
381 |
GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, EffectQueue.getMVP(mQueues) , 0); |
|
382 |
mesh.bindTransformAttribs(mNormalProgram); |
|
383 |
GLES31.glLineWidth(8.0f); |
|
384 |
GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*num); |
|
385 |
} |
|
386 |
|
|
387 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
388 |
|
|
389 |
void drawPrivOIT(float halfW, float halfH, MeshBase mesh, DistortedOutputSurface surface, long currTime) |
|
390 |
{ |
|
391 |
float halfZ = halfW*mesh.getZFactor(); |
|
392 |
|
|
393 |
EffectQueue.compute(mQueues, currTime, halfW, halfH, halfZ ); |
|
394 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
395 |
|
|
396 |
mMainOITProgram.useProgram(); |
|
397 |
GLES31.glUniform1i(mMainOITTextureH, 0); |
|
398 |
GLES31.glUniform2ui(mMainOITSizeH, surface.mWidth, surface.mHeight); |
|
399 |
GLES31.glUniform1ui(mMainOITNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) ); |
|
400 |
|
|
401 |
mesh.bindVertexAttribs(mMainOITProgram); |
|
402 |
|
|
403 |
float inflate = mesh.getInflate(); |
|
404 |
|
|
405 |
EffectQueue.send(mQueues, surface, inflate, halfW, halfH, halfZ, 1 ); |
|
406 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() ); |
|
407 |
|
|
408 |
if( mesh.getShowNormals() ) |
|
409 |
{ |
|
410 |
mMainProgram.useProgram(); |
|
411 |
EffectQueue.send(mQueues, surface, inflate, halfW, halfH, halfZ, 0 ); |
|
412 |
displayNormals(mesh); |
|
413 |
} |
|
414 |
} |
|
415 |
|
|
416 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
417 |
|
|
418 |
void drawPriv(float halfW, float halfH, MeshBase mesh, DistortedOutputSurface surface, long currTime) |
|
419 |
{ |
|
420 |
float halfZ = halfW*mesh.getZFactor(); |
|
421 |
|
|
422 |
EffectQueue.compute(mQueues, currTime, halfW, halfH, halfZ ); |
|
423 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
424 |
mMainProgram.useProgram(); |
|
425 |
GLES31.glUniform1i(mMainTextureH, 0); |
|
426 |
mesh.bindVertexAttribs(mMainProgram); |
|
427 |
EffectQueue.send(mQueues, surface, mesh.getInflate(), halfW, halfH, halfZ, 0 ); |
|
428 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() ); |
|
429 |
|
|
430 |
if( mesh.getShowNormals() ) displayNormals(mesh); |
|
431 |
} |
|
432 |
|
|
433 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
434 |
|
|
435 |
static void blitPriv(DistortedOutputSurface surface) |
|
436 |
{ |
|
437 |
mBlitProgram.useProgram(); |
|
438 |
|
|
439 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
440 |
GLES31.glUniform1i(mBlitTextureH, 0); |
|
441 |
GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear); |
|
442 |
GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
443 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
444 |
} |
|
445 |
|
|
446 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
447 |
|
|
448 |
static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH) |
|
449 |
{ |
|
450 |
mBlitDepthProgram.useProgram(); |
|
451 |
|
|
452 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
453 |
GLES31.glUniform1i(mBlitDepthTextureH, 0); |
|
454 |
GLES31.glUniform1i(mBlitDepthDepthTextureH, 1); |
|
455 |
GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH ); |
|
456 |
GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear); |
|
457 |
GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
458 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
459 |
} |
|
460 |
|
|
461 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
462 |
|
|
463 |
private static int printPreviousBuffer() |
|
464 |
{ |
|
465 |
int counter = 0; |
|
466 |
|
|
467 |
ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4, |
|
468 |
GLES31.GL_MAP_READ_BIT); |
|
469 |
if( atomicBuf!=null ) |
|
470 |
{ |
|
471 |
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer(); |
|
472 |
counter = atomicIntBuf.get(0); |
|
473 |
} |
|
474 |
else |
|
475 |
{ |
|
476 |
android.util.Log.e("effects", "print: failed to map atomic buffer"); |
|
477 |
} |
|
478 |
|
|
479 |
GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER); |
|
480 |
|
|
481 |
return counter; |
|
482 |
} |
|
483 |
|
|
484 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
485 |
|
|
486 |
private static void zeroBuffer() |
|
487 |
{ |
|
488 |
ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4, |
|
489 |
GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT); |
|
490 |
if( atomicBuf!=null ) |
|
491 |
{ |
|
492 |
IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer(); |
|
493 |
atomicIntBuf.put(0,0); |
|
494 |
} |
|
495 |
else |
|
496 |
{ |
|
497 |
android.util.Log.e("effects", "zero: failed to map atomic buffer"); |
|
498 |
} |
|
499 |
|
|
500 |
GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER); |
|
501 |
} |
|
502 |
|
|
503 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
504 |
// reset atomic counter to 0 |
|
505 |
|
|
506 |
static int zeroOutAtomic() |
|
507 |
{ |
|
508 |
int counter = 0; |
|
509 |
|
|
510 |
if( mAtomicCounter[0]<0 ) |
|
511 |
{ |
|
512 |
GLES31.glGenBuffers(Distorted.FBO_QUEUE_SIZE,mAtomicCounter,0); |
|
513 |
|
|
514 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) |
|
515 |
{ |
|
516 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]); |
|
517 |
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW); |
|
518 |
zeroBuffer(); |
|
519 |
} |
|
520 |
} |
|
521 |
|
|
522 |
// reading the value of the buffer on every frame would slow down rendering by |
|
523 |
// about 3%; doing it only once every 5 frames affects speed by less than 1%. |
|
524 |
if( mCurrBuffer==0 ) |
|
525 |
{ |
|
526 |
GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]); |
|
527 |
counter = printPreviousBuffer(); |
|
528 |
} |
|
529 |
|
|
530 |
if( ++mCurrBuffer>=Distorted.FBO_QUEUE_SIZE ) mCurrBuffer = 0; |
|
531 |
|
|
532 |
GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]); |
|
533 |
zeroBuffer(); |
|
534 |
|
|
535 |
return counter; |
|
536 |
} |
|
537 |
|
|
538 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
539 |
// Pass1 of the OIT algorithm. Clear per-pixel head-poiners. |
|
540 |
|
|
541 |
static void oitClear(DistortedOutputSurface surface, int counter) |
|
542 |
{ |
|
543 |
if( mLinkedListSSBO[0]<0 ) |
|
544 |
{ |
|
545 |
GLES31.glGenBuffers(1,mLinkedListSSBO,0); |
|
546 |
|
|
547 |
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4); |
|
548 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]); |
|
549 |
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW); |
|
550 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0); |
|
551 |
|
|
552 |
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]); |
|
553 |
} |
|
554 |
|
|
555 |
// See if we have overflown the SSBO in one of the previous frames. |
|
556 |
// If yes, assume we need to make the SSBO larger. |
|
557 |
float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight); |
|
558 |
|
|
559 |
if( overflow>1.0f ) |
|
560 |
{ |
|
561 |
//android.util.Log.e("effects", "previous frame rendered "+counter+ |
|
562 |
// " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space"); |
|
563 |
|
|
564 |
mBufferSize *= (int)(overflow+1.0f); |
|
565 |
int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4); |
|
566 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]); |
|
567 |
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW); |
|
568 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0); |
|
569 |
} |
|
570 |
|
|
571 |
mOITClearProgram.useProgram(); |
|
572 |
|
|
573 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
574 |
GLES31.glUniform2f(mOITClearTexCorrH, 1.0f, 1.0f ); // corrections do not really matter here - only present because of common vertex shader. |
|
575 |
GLES31.glUniform1f( mOITClearDepthH , 1.0f); // likewise depth |
|
576 |
GLES31.glUniform2ui(mOITClearSizeH, surface.mWidth, surface.mHeight); |
|
577 |
GLES31.glVertexAttribPointer(mOITClearProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
578 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
579 |
} |
|
580 |
|
|
581 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
582 |
// Pass2 of the OIT algorithm - build per-pixel linked lists. |
|
583 |
|
|
584 |
static void oitBuild(DistortedOutputSurface surface, float corrW, float corrH) |
|
585 |
{ |
|
586 |
mOITBuildProgram.useProgram(); |
|
587 |
|
|
588 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
589 |
GLES31.glUniform1i(mOITBuildTextureH, 0); |
|
590 |
GLES31.glUniform1i(mOITBuildDepthTextureH, 1); |
|
591 |
GLES31.glUniform2f(mOITBuildTexCorrH, corrW, corrH ); |
|
592 |
GLES31.glUniform2ui(mOITBuildSizeH, surface.mWidth, surface.mHeight); |
|
593 |
GLES31.glUniform1ui(mOITBuildNumRecordsH, (int)(mBufferSize*surface.mWidth*surface.mHeight) ); |
|
594 |
GLES31.glUniform1f(mOITBuildDepthH , 1.0f-surface.mNear); |
|
595 |
GLES31.glVertexAttribPointer(mOITBuildProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
596 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
597 |
} |
|
598 |
|
|
599 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
600 |
// Pass3 of the OIT algorithm. Cut occluded parts of the linked list. |
|
601 |
|
|
602 |
static void oitCollapse(DistortedOutputSurface surface, float corrW, float corrH) |
|
603 |
{ |
|
604 |
mOITCollapseProgram.useProgram(); |
|
605 |
|
|
606 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
607 |
GLES31.glUniform1i(mOITCollapseDepthTextureH, 1); |
|
608 |
GLES31.glUniform2f(mOITCollapseTexCorrH, corrW, corrH ); |
|
609 |
GLES31.glUniform2ui(mOITCollapseSizeH, surface.mWidth, surface.mHeight); |
|
610 |
GLES31.glUniform1f( mOITCollapseDepthH , 1.0f-surface.mNear); |
|
611 |
GLES31.glVertexAttribPointer(mOITCollapseProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
612 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
613 |
} |
|
614 |
|
|
615 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
616 |
// Pass4 of the OIT algorithm. Render all the transparent pixels from the per-pixel linked lists. |
|
617 |
|
|
618 |
static void oitRender(DistortedOutputSurface surface, float corrW, float corrH) |
|
619 |
{ |
|
620 |
mOITRenderProgram.useProgram(); |
|
621 |
|
|
622 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
623 |
GLES31.glUniform2f(mOITRenderTexCorrH, corrW, corrH ); |
|
624 |
GLES31.glUniform2ui(mOITRenderSizeH, surface.mWidth, surface.mHeight); |
|
625 |
GLES31.glUniform1f( mOITRenderDepthH , 1.0f-surface.mNear); |
|
626 |
GLES31.glVertexAttribPointer(mOITRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
627 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
628 |
} |
|
629 |
|
|
630 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
631 |
|
|
632 |
static void onPause() |
|
633 |
{ |
|
634 |
mLinkedListSSBO[0]= -1; |
|
635 |
|
|
636 |
for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1; |
|
637 |
} |
|
638 |
|
|
639 | 60 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
640 | 61 |
|
641 | 62 |
static void onDestroy() |
... | ... | |
643 | 64 |
mNextID = 0; |
644 | 65 |
} |
645 | 66 |
|
646 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
647 |
|
|
648 |
static void setSSBOSize(float size) |
|
649 |
{ |
|
650 |
mBufferSize = size; |
|
651 |
} |
|
652 |
|
|
653 | 67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
654 | 68 |
// PUBLIC API |
655 | 69 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
791 | 205 |
return mQueues[num].removeByName(name); |
792 | 206 |
} |
793 | 207 |
|
794 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
795 |
/** |
|
796 |
* Returns the maximum number of effects of a given type that can be simultaneously applied to a |
|
797 |
* single (InputSurface,MeshBase) combo. |
|
798 |
* |
|
799 |
* @param type {@link EffectType} |
|
800 |
* @return The maximum number of effects of a given type. |
|
801 |
*/ |
|
802 |
@SuppressWarnings("unused") |
|
803 |
public static int getMax(EffectType type) |
|
804 |
{ |
|
805 |
return EffectQueue.getMax(type.ordinal()); |
|
806 |
} |
|
807 |
|
|
808 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
809 |
/** |
|
810 |
* Sets the maximum number of effects that can be stored in a single EffectQueue at one time. |
|
811 |
* This can fail if: |
|
812 |
* <ul> |
|
813 |
* <li>the value of 'max' is outside permitted range (0 ≤ max ≤ Byte.MAX_VALUE) |
|
814 |
* <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called |
|
815 |
* before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this |
|
816 |
* time only decreasing the value of 'max' is permitted. |
|
817 |
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created. |
|
818 |
* </ul> |
|
819 |
* |
|
820 |
* @param type {@link EffectType} |
|
821 |
* @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater |
|
822 |
* than Byte.MAX_VALUE |
|
823 |
* @return <code>true</code> if operation was successful, <code>false</code> otherwise. |
|
824 |
*/ |
|
825 |
@SuppressWarnings("unused") |
|
826 |
public static boolean setMax(EffectType type, int max) |
|
827 |
{ |
|
828 |
return EffectQueue.setMax(type.ordinal(),max); |
|
829 |
} |
|
830 |
|
|
831 | 208 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
832 | 209 |
/** |
833 | 210 |
* Add a new Effect to our queue. |
src/main/java/org/distorted/library/main/DistortedNode.java | ||
---|---|---|
163 | 163 |
{ |
164 | 164 |
mState.apply(); |
165 | 165 |
GLES31.glDisable(GLES31.GL_BLEND); |
166 |
mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
166 |
Distorted.drawPriv(mEffects.getQueues(), mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
167 | 167 |
GLES31.glEnable(GLES31.GL_BLEND); |
168 | 168 |
return 1; |
169 | 169 |
} |
... | ... | |
181 | 181 |
if( input.setAsInput() ) |
182 | 182 |
{ |
183 | 183 |
mState.apply(); |
184 |
mEffects.drawPrivOIT(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
184 |
Distorted.drawPrivOIT(mEffects.getQueues(), mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
185 | 185 |
return 1; |
186 | 186 |
} |
187 | 187 |
|
... | ... | |
198 | 198 |
if( input.setAsInput() ) |
199 | 199 |
{ |
200 | 200 |
mState.apply(); |
201 |
mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
201 |
Distorted.drawPriv(mEffects.getQueues(), mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
|
|
202 | 202 |
return 1; |
203 | 203 |
} |
204 | 204 |
|
... | ... | |
226 | 226 |
if( mSurface.setAsInput() ) |
227 | 227 |
{ |
228 | 228 |
numRenders++; |
229 |
DistortedEffects.blitPriv(mData.mFBO);
|
|
229 |
Distorted.blitPriv(mData.mFBO); |
|
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
numRenders += mData.mFBO.renderChildren(currTime,numChildren,mChildren,0, mRenderWayOIT); |
... | ... | |
389 | 389 |
mRenderWayOIT = oit; |
390 | 390 |
|
391 | 391 |
if( initialSize>0.0f && initialSize<10.0f ) |
392 |
DistortedEffects.setSSBOSize(initialSize);
|
|
392 |
Distorted.setSSBOSize(initialSize); |
|
393 | 393 |
} |
394 | 394 |
|
395 | 395 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/main/DistortedOutputSurface.java | ||
---|---|---|
263 | 263 |
GLES31.glDisable(GLES31.GL_STENCIL_TEST); |
264 | 264 |
GLES31.glStencilMask(0x00); |
265 | 265 |
|
266 |
DistortedEffects.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
|
|
266 |
Distorted.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() ); |
|
267 | 267 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE0); |
268 | 268 |
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0); |
269 | 269 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE1); |
... | ... | |
290 | 290 |
|
291 | 291 |
private static void oitClear(DistortedOutputSurface buffer) |
292 | 292 |
{ |
293 |
int counter = DistortedEffects.zeroOutAtomic();
|
|
294 |
DistortedEffects.oitClear(buffer,counter);
|
|
293 |
int counter = Distorted.zeroOutAtomic(); |
|
294 |
Distorted.oitClear(buffer,counter); |
|
295 | 295 |
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT); |
296 | 296 |
} |
297 | 297 |
|
... | ... | |
309 | 309 |
DistortedRenderState.colorDepthStencilOn(); |
310 | 310 |
DistortedRenderState.enableDepthTest(); |
311 | 311 |
|
312 |
DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
|
|
312 |
Distorted.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() ); |
|
313 | 313 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE0); |
314 | 314 |
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0); |
315 | 315 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE1); |
... | ... | |
338 | 338 |
GLES31.glActiveTexture(GLES31.GL_TEXTURE1); |
339 | 339 |
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, mDepthStencilH[fbo]); |
340 | 340 |
DistortedRenderState.switchOffColorDepthStencil(); |
341 |
DistortedEffects.oitCollapse(this, corrW, corrH);
|
|
341 |
Distorted.oitCollapse(this, corrW, corrH); |
|
342 | 342 |
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0); |
343 | 343 |
} |
344 | 344 |
|
345 | 345 |
setAsOutput(currTime); |
346 | 346 |
DistortedRenderState.switchColorDepthOnStencilOff(); |
347 |
DistortedEffects.oitRender(this, corrW, corrH);
|
|
347 |
Distorted.oitRender(this, corrW, corrH); |
|
348 | 348 |
DistortedRenderState.restoreColorDepthStencil(); |
349 | 349 |
|
350 | 350 |
return 1; |
... | ... | |
866 | 866 |
mRenderWayOIT = oit; |
867 | 867 |
|
868 | 868 |
if( initialSize>0.0f && initialSize<10.0f ) |
869 |
DistortedEffects.setSSBOSize(initialSize);
|
|
869 |
Distorted.setSSBOSize(initialSize); |
|
870 | 870 |
} |
871 | 871 |
|
872 | 872 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/library/main/DistortedScreen.java | ||
---|---|---|
129 | 129 |
GLES31.glDisable(GLES31.GL_DEPTH_TEST); |
130 | 130 |
GLES31.glDisable(GLES31.GL_BLEND); |
131 | 131 |
|
132 |
DistortedEffects.blitPriv(this);
|
|
132 |
Distorted.blitPriv(this); |
|
133 | 133 |
|
134 | 134 |
if( mShowFPS && fpsTexture.setAsInput()) |
135 | 135 |
{ |
136 |
fpsEffects.drawPriv(FPS_W / 2.0f, FPS_H / 2.0f, fpsMesh, this, time);
|
|
136 |
Distorted.drawPriv(fpsEffects.getQueues(), FPS_W / 2.0f, FPS_H / 2.0f, fpsMesh, this, time);
|
|
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE ) |
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java | ||
---|---|---|
107 | 107 |
|
108 | 108 |
int numV = VertexEffect.getNumEnabled(); |
109 | 109 |
|
110 |
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? DistortedEffects.getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
|
110 |
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? Distorted.getMax(EffectType.VERTEX ) : 0 ) + "\n"); |
|
111 | 111 |
String mainFragHeader= Distorted.GLSL_VERSION + "\n"; |
112 | 112 |
|
113 | 113 |
String enabledEffectV= VertexEffect.getGLSL(); |
Also available in: Unified diff
Move all Program-related stuff to Distorted. Now the DistortedEffects class is very simple.