Project

General

Profile

« Previous | Next » 

Revision 8777ce17

Added by Leszek Koltunski over 6 years ago

Order Independent Transparency. Does not work yet.

View differences:

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
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
390 390
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
391 391
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
392 392

  
393
    // clear buffers
393
    return 1;
394
    }
395

  
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397

  
398
  private int blitWithDepthRender(long currTime, DistortedOutputSurface buffer)
399
    {
400
    GLES31.glViewport(0, 0, mWidth, mHeight);
401
    setAsOutput(currTime);
402
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
403
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
404

  
405
    DistortedRenderState.enableStencil();
406

  
407
    DistortedEffects.blitDepthRenderPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
408
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
409
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
410

  
411
    DistortedRenderState.restoreStencil();
412

  
413
    return 1;
414
    }
415

  
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

  
418
  private void clearBuffer(DistortedOutputSurface buffer)
419
    {
394 420
    GLES31.glStencilMask(0xff);
395 421
    GLES31.glDepthMask(true);
396 422
    GLES31.glColorMask(true,true,true,true);
......
403 429
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
404 430
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[0], 0);
405 431
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
406

  
407
    return 1;
408 432
    }
409 433

  
410 434
///////////////////////////////////////////////////////////////////////////////////////////////////
......
442 466

  
443 467
            numRenders += lastQueue.postprocess(mBuffer);
444 468
            numRenders += blitWithDepth(time, mBuffer[quality]);
445

  
446
            mBuffer[quality].setAsOutputAndClear(time);
469
            clearBuffer(mBuffer[quality]);
447 470
            }
448 471

  
449 472
          internalQuality = currQueue.getInternalQuality();
......
465 488

  
466 489
          numRenders += currQueue.postprocess(mBuffer);
467 490
          numRenders += blitWithDepth(time, mBuffer[quality]);
491
          numRenders += blitWithDepthRender(time,mBuffer[quality]);  // merge the OIT linked list
492
          clearBuffer(mBuffer[quality]);
468 493
          }
469 494
        }
470 495

  
......
504 529
    return (float)mHeight/mRealHeight;
505 530
    }
506 531

  
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
/**
509
 * Bind this Surface as a Framebuffer we can render to; always clear it's color bit.
510
 *
511
 * Useful for drawing to the postprocessing buffer, which must sometimes be cleared multiple times
512
 * per frame.
513
 */
514

  
515
  private void setAsOutputAndClear(long time)
516
    {
517
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
518

  
519
    mTime = time;    // have to do this otherwise on the next setAsOutput() we would clear
520
    DistortedRenderState.colorDepthStencilOn();
521
    GLES31.glClearColor(mClearR, mClearG, mClearB, mClearA);
522
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
523
    DistortedRenderState.colorDepthStencilRestore();
524
    }
525

  
526 532
///////////////////////////////////////////////////////////////////////////////////////////////////
527 533
// PUBLIC API
528 534
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/main/DistortedRenderState.java
199 199
    GLES31.glDisable(GLES31.GL_SCISSOR_TEST);
200 200
    }
201 201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

  
204
  static void enableStencil()
205
    {
206
    sState.stencilTest = cState.stencilTest;
207

  
208
    if (cState.stencilTest != 1)
209
      {
210
      cState.stencilTest = 1;
211
      GLES31.glEnable(GLES31.GL_STENCIL_TEST);
212
      }
213

  
214
    sState.stencilFuncFunc = cState.stencilFuncFunc;
215
    sState.stencilFuncRef  = cState.stencilFuncRef;
216
    sState.stencilFuncMask = cState.stencilFuncMask;
217

  
218
    if( cState.stencilFuncFunc!=GLES31.GL_EQUAL || cState.stencilFuncRef!=1 || cState.stencilFuncMask!=STENCIL_MASK )
219
      {
220
      cState.stencilFuncFunc = GLES31.GL_EQUAL;
221
      cState.stencilFuncRef  = 1;
222
      cState.stencilFuncMask = STENCIL_MASK;
223
      GLES31.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
224
      }
225

  
226
    sState.stencilMask = cState.stencilMask;
227

  
228
    if( cState.stencilMask!= 0x00 )
229
      {
230
      cState.stencilMask = 0x00;
231
      GLES31.glStencilMask(cState.stencilMask);
232
      }
233
    }
234

  
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

  
237
  static void restoreStencil()
238
    {
239
    if (sState.stencilTest != cState.stencilTest)
240
      {
241
      cState.stencilTest = sState.stencilTest;
242

  
243
      if (cState.stencilTest == 0)
244
        {
245
        GLES31.glDisable(GLES31.GL_STENCIL_TEST);
246
        }
247
      else
248
        {
249
        GLES31.glEnable(GLES31.GL_STENCIL_TEST);
250
        }
251
      }
252
    if( sState.stencilFuncFunc!=cState.stencilFuncFunc || sState.stencilFuncRef!=cState.stencilFuncRef || sState.stencilFuncMask!=cState.stencilFuncMask )
253
      {
254
      cState.stencilFuncFunc = sState.stencilFuncFunc;
255
      cState.stencilFuncRef  = sState.stencilFuncRef ;
256
      cState.stencilFuncMask = sState.stencilFuncMask;
257
      GLES31.glStencilFunc(cState.stencilFuncFunc,cState.stencilFuncRef,cState.stencilFuncMask);
258
      }
259
    if( sState.stencilMask!=cState.stencilMask )
260
      {
261
      cState.stencilMask = sState.stencilMask;
262
      GLES31.glStencilMask(cState.stencilMask);
263
      }
264
    }
265

  
202 266
///////////////////////////////////////////////////////////////////////////////////////////////////
203 267

  
204 268
  static void setUpStencilMark()
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
32 32
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
33 33
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
34 34

  
35

  
36 35
  private int mHalo;
37 36

  
38 37
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/res/raw/blit_depth_fragment_shader.glsl
1 1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
2
// Copyright 2018 Leszek Koltunski                                                          //
3 3
//                                                                                          //
4 4
// This file is part of Distorted.                                                          //
5 5
//                                                                                          //
......
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20 20
precision lowp float;
21
precision highp uint;
21 22

  
22 23
#if __VERSION__ != 100
23 24
out vec4 fragColor;           // The output color
24 25
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
25 27
#define TEXTURE texture
26 28
#define FRAG_COLOR fragColor
27 29
#else
28 30
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
31
varying vec2 v_Pixel;         // location of the current fragment, in pixels
29 32
#define TEXTURE texture2D
30 33
#define FRAG_COLOR gl_FragColor
31 34
#endif
......
33 36
uniform sampler2D u_Texture;
34 37
uniform sampler2D u_DepthTexture;
35 38

  
39
//////////////////////////////////////////////////////////////////////////////////////////////
40
// per-pixel linked list. Order Independent Transparency.
41

  
42
uniform ivec2 u_Size;
43
uniform uint u_numRecords;
44

  
45
layout (binding=0, offset=0) uniform atomic_uint u_Counter;  // initialize to 0
46

  
47
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
48
  {                                          // one for each pixel in the Output rectangle.
49
  uint u_Records[];                          //
50
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
51
                                             // triplets of (pointer,depth,rgba).
52

  
53
//////////////////////////////////////////////////////////////////////////////////////////////
54
// Concurrent insert to a linked list. Tim Harris, 'pragmatic implementation of non-blocking
55
// linked-lists', 2001.
56
// This arranges fragments by decreasing 'depth', so one would think - from back to front, but
57
// in main() below the depth is mapped with S*(1-depth)/2, so it is really front to back.
58

  
59
void insert( vec2 ij, uint depth, uint rgba )
60
  {
61
  uint ptr = atomicCounterIncrement(u_Counter)-1u;
62

  
63
  if( ptr<u_numRecords )
64
    {
65
    ptr = 3u*ptr + uint(u_Size.x*u_Size.y);
66

  
67
    u_Records[ptr+1u] = depth;
68
    u_Records[ptr+2u] = rgba;
69

  
70
    memoryBarrier();
71

  
72
    uint prev = uint(ij.x + ij.y * float(u_Size.x));
73
    uint curr = u_Records[prev];
74
/*
75
    while (true)
76
      {
77
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
78
        {
79
*/
80
        u_Records[ptr] = curr; // next of new record is curr
81
        memoryBarrier();
82
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
83
/*
84
        if (res==curr) break;      // done!
85
        else           curr = res; // could not insert! retry from same place in list
86
        }
87
      else // advance in list
88
        {
89
        prev = curr;
90
        curr = u_Records[prev];
91
        }
92
      }
93
*/
94
    }
95
  }
96

  
97
//////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
uint convert(vec4 c)
100
  {
101
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
102
  }
103

  
36 104
//////////////////////////////////////////////////////////////////////////////////////////////
37 105

  
38 106
void main()                    		
39 107
  {
40
  gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r;
41
  FRAG_COLOR   = TEXTURE(u_Texture     ,v_TexCoordinate);
108
  vec4 frag  = TEXTURE(u_Texture     , v_TexCoordinate);
109
  float depth= TEXTURE(u_DepthTexture, v_TexCoordinate).r;
110

  
111
  if( frag.a > 0.95 )
112
    {
113
    gl_FragDepth = depth;
114
    FRAG_COLOR   = frag;
115
    }
116
  else if( frag.a > 0.0 )
117
    {
118
    const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
119
    insert(v_Pixel, uint(S*(1.0-depth)/2.0), convert(frag) );
120
    discard;
121
    }
42 122
  }
src/main/res/raw/blit_depth_render_fragment_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 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
precision lowp float;
21
precision highp uint;
22

  
23
#if __VERSION__ != 100
24
out vec4 fragColor;           // The output color
25
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
26
in vec2 v_Pixel;              // location of the current fragment, in pixels
27
#define TEXTURE texture
28
#define FRAG_COLOR fragColor
29
#else
30
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
31
varying vec2 v_Pixel;         // location of the current fragment, in pixels
32
#define TEXTURE texture2D
33
#define FRAG_COLOR gl_FragColor
34
#endif
35

  
36
uniform sampler2D u_DepthTexture;
37

  
38
//////////////////////////////////////////////////////////////////////////////////////////////
39
// per-pixel linked list. Order Independent Transparency.
40

  
41
uniform ivec2 u_Size;
42

  
43
layout (std430,binding=1) buffer linkedlist  // first (u_Size.x*u_Size.y) uints - head pointers,
44
  {                                          // one for each pixel in the Output rectangle.
45
  uint u_Records[];                          //
46
  };                                         // Next 3*u_numRecords uints - actual linked list, i.e.
47
                                             // triplets of (pointer,depth,rgba).
48

  
49
//////////////////////////////////////////////////////////////////////////////////////////////
50

  
51
vec4 convert(uint rgba)
52
  {
53
  return vec4( float((rgba>>24u)&255u),float((rgba>>16u)&255u),float((rgba>>8u)&255u),float(rgba&255u) ) / 255.0;
54
  }
55

  
56
//////////////////////////////////////////////////////////////////////////////////////////////
57
// https://en.wikipedia.org/wiki/Alpha_compositing (premultiplied)
58

  
59
vec4 blend(vec4 clr,vec4 srf)
60
  {
61
  return clr + (1.0 - clr.a) * vec4(srf.rgb * srf.a , srf.a);
62
  }
63

  
64
//////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
void main()                    		
67
  {
68
  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
69
  uint curr = u_Records[index];
70

  
71
  if (curr == 0u) discard;
72

  
73
  vec4 color= vec4(0.0,0.0,0.0,0.0);
74
  u_Records[index] = 0u;
75

  
76
  while (curr > 0u)                                      // keep walking the linked list
77
    {                                                    // and blending the colors in
78
    curr = u_Records[curr];                              //
79
    //color= blend( color, convert(u_Records[curr+2u]) );  //
80

  
81
    color = convert(u_Records[curr+2u]);
82

  
83
    if( color.a == 0.0 ) color = vec4(0.0,1.0,0.0,1.0);
84
    }
85

  
86
  gl_FragDepth = TEXTURE(u_DepthTexture, v_TexCoordinate).r;
87
  FRAG_COLOR = color;
88

  
89
//else
90
//  FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
91
  }
src/main/res/raw/blit_depth_vertex_shader.glsl
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20 20
precision lowp float;
21
precision highp uint;
21 22

  
22 23
#if __VERSION__ != 100
23 24
in vec2 a_Position;           // Per-vertex position.
24 25
out vec2 v_TexCoordinate;     //
26
out vec2 v_Pixel;             //
25 27
#else
26 28
attribute vec2 a_Position;    // Per-vertex position.
27 29
varying vec2 v_TexCoordinate; //
30
varying vec2 v_Pixel;         //
28 31
#endif
29 32

  
30 33
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
......
33 36
                              // reused!) so we need to compensate here by adjusting the texture
34 37
                              // coords.
35 38

  
39
uniform ivec2 u_Size;         // size of the output surface, in pixels.
40

  
36 41
//////////////////////////////////////////////////////////////////////////////////////////////
37 42

  
38 43
void main()
39 44
  {
40 45
  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
46
  v_Pixel         = v_TexCoordinate * vec2(u_Size);
41 47
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
42 48
  }

Also available in: Unified diff