Revision 8777ce17
Added by Leszek Koltunski about 6 years ago
src/main/java/org/distorted/library/main/DistortedEffects.java | ||
---|---|---|
41 | 41 |
import java.nio.ByteBuffer; |
42 | 42 |
import java.nio.ByteOrder; |
43 | 43 |
import java.nio.FloatBuffer; |
44 |
import java.nio.IntBuffer; |
|
44 | 45 |
|
45 | 46 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
46 | 47 |
/** |
... | ... | |
74 | 75 |
private static int mBlitDepthDepthTextureH; |
75 | 76 |
private static int mBlitDepthDepthH; |
76 | 77 |
private static int mBlitDepthTexCorrH; |
78 |
private static int mBlitDepthSizeH; |
|
79 |
private static int mBlitDepthNumRecordsH; |
|
80 |
|
|
81 |
private static int[] mLinkedListSSBO = new int[1]; |
|
82 |
private static int[] mAtomicCounter = new int[1]; |
|
83 |
|
|
84 |
static |
|
85 |
{ |
|
86 |
mLinkedListSSBO[0]= -1; |
|
87 |
mAtomicCounter[0] = -1; |
|
88 |
} |
|
89 |
|
|
90 |
private static int mBufferSize=(0x1<<23); // 8 million entries |
|
91 |
private static IntBuffer mIntBuffer; |
|
92 |
|
|
93 |
/// BLIT DEPTH RENDER PROGRAM /// |
|
94 |
private static DistortedProgram mBlitDepthRenderProgram; |
|
95 |
private static int mBlitDepthRenderDepthTextureH; |
|
96 |
private static int mBlitDepthRenderDepthH; |
|
97 |
private static int mBlitDepthRenderTexCorrH; |
|
98 |
private static int mBlitDepthRenderSizeH; |
|
77 | 99 |
|
78 | 100 |
/// NORMAL PROGRAM ///// |
79 | 101 |
private static DistortedProgram mNormalProgram; |
... | ... | |
164 | 186 |
mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture"); |
165 | 187 |
mBlitDepthDepthH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth"); |
166 | 188 |
mBlitDepthTexCorrH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr"); |
189 |
mBlitDepthSizeH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size"); |
|
190 |
mBlitDepthNumRecordsH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords"); |
|
191 |
|
|
192 |
mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer(); |
|
193 |
mIntBuffer.put(0,0); |
|
194 |
|
|
195 |
if( mLinkedListSSBO[0]<0 ) |
|
196 |
{ |
|
197 |
GLES31.glGenBuffers(1,mLinkedListSSBO,0); |
|
198 |
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]); |
|
199 |
GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ); |
|
200 |
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]); |
|
201 |
} |
|
202 |
|
|
203 |
if( mAtomicCounter[0]<0 ) |
|
204 |
{ |
|
205 |
GLES31.glGenBuffers(1,mAtomicCounter,0); |
|
206 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] ); |
|
207 |
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW); |
|
208 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0); |
|
209 |
} |
|
210 |
|
|
211 |
// BLIT DEPTH RENDER PROGRAM /////////////////////////// |
|
212 |
final InputStream blitDepthRenderVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader); |
|
213 |
final InputStream blitDepthRenderFragStream = resources.openRawResource(R.raw.blit_depth_render_fragment_shader); |
|
214 |
|
|
215 |
try |
|
216 |
{ |
|
217 |
mBlitDepthRenderProgram = new DistortedProgram(blitDepthRenderVertStream,blitDepthRenderFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL); |
|
218 |
} |
|
219 |
catch(Exception e) |
|
220 |
{ |
|
221 |
Log.e("EFFECTS", "exception trying to compile BLIT DEPTH RENDER program: "+e.getMessage()); |
|
222 |
throw new RuntimeException(e.getMessage()); |
|
223 |
} |
|
224 |
|
|
225 |
int blitDepthRenderProgramH = mBlitDepthRenderProgram.getProgramHandle(); |
|
226 |
mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture"); |
|
227 |
mBlitDepthRenderDepthH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth"); |
|
228 |
mBlitDepthRenderTexCorrH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr"); |
|
229 |
mBlitDepthRenderSizeH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size"); |
|
167 | 230 |
|
168 | 231 |
// NORMAL PROGRAM ////////////////////////////////////// |
169 | 232 |
final InputStream normalVertexStream = resources.openRawResource(R.raw.normal_vertex_shader); |
... | ... | |
287 | 350 |
GLES31.glUniform1i(mMainTextureH, 0); |
288 | 351 |
GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() ); |
289 | 352 |
|
290 |
if( Distorted.GLSL >= 300 ) |
|
291 |
{ |
|
292 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]); |
|
293 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0); |
|
294 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1); |
|
295 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2); |
|
296 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0); |
|
297 |
} |
|
298 |
else |
|
299 |
{ |
|
300 |
mesh.mVertAttribs.position(0); |
|
301 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs); |
|
302 |
mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE); |
|
303 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs); |
|
304 |
mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE); |
|
305 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs); |
|
306 |
} |
|
353 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]); |
|
354 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0); |
|
355 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1); |
|
356 |
GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2); |
|
357 |
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0); |
|
307 | 358 |
|
308 | 359 |
mM.send(surface,halfW,halfH,halfZ,marginInPixels); |
309 | 360 |
mV.send(); |
... | ... | |
341 | 392 |
GLES31.glUniform1i(mBlitDepthTextureH, 0); |
342 | 393 |
GLES31.glUniform1i(mBlitDepthDepthTextureH, 1); |
343 | 394 |
GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH ); |
344 |
GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear); |
|
395 |
GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight); |
|
396 |
GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 ); // see the fragment shader |
|
397 |
GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear); |
|
345 | 398 |
GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
346 | 399 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
400 |
|
|
401 |
|
|
402 |
//android.util.Log.e("effects", "bufferSize: "+mBufferSize+" numRecords: "+((mBufferSize-surface.mWidth*surface.mHeight)/3) ); |
|
403 |
} |
|
404 |
|
|
405 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
406 |
// render all the transparent pixels from the per-pixel linked lists. This is in the 'merge |
|
407 |
// postprocessing buckets' stage. |
|
408 |
|
|
409 |
static void blitDepthRenderPriv(DistortedOutputSurface surface, float corrW, float corrH) |
|
410 |
{ |
|
411 |
mBlitDepthRenderProgram.useProgram(); |
|
412 |
|
|
413 |
GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight ); |
|
414 |
GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1); |
|
415 |
GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH ); |
|
416 |
GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight); |
|
417 |
GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear); |
|
418 |
GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions); |
|
419 |
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4); |
|
420 |
|
|
421 |
// reset atomic counter to 0 |
|
422 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] ); |
|
423 |
GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW); |
|
424 |
GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0); |
|
347 | 425 |
} |
348 | 426 |
|
349 | 427 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
365 | 443 |
|
366 | 444 |
static void onDestroy() |
367 | 445 |
{ |
368 |
mNextID = 0; |
|
446 |
mNextID = 0; |
|
447 |
mLinkedListSSBO[0]= -1; |
|
448 |
mAtomicCounter[0] = -1; |
|
369 | 449 |
} |
370 | 450 |
|
371 | 451 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Order Independent Transparency. Does not work yet.