Project

General

Profile

« Previous | Next » 

Revision e029600f

Added by Leszek Koltunski over 6 years ago

still debugging the OIT

View differences:

src/main/java/org/distorted/library/main/DistortedEffects.java
70 70

  
71 71
  /// BLIT DEPTH PROGRAM ///
72 72
  private static DistortedProgram mBlitDepthProgram;
73
  private static int mBlitDepthTextureH;
74
  private static int mBlitDepthDepthTextureH;
75
  private static int mBlitDepthDepthH;
76
  private static int mBlitDepthTexCorrH;
77 73
  private static int mBlitDepthSizeH;
78
  private static int mBlitDepthNumRecordsH;
79 74

  
80 75
  private static int[] mLinkedListSSBO = new int[1];
81 76
  private static int[] mAtomicCounter = new int[1];
......
95 90

  
96 91
  /// BLIT DEPTH RENDER PROGRAM ///
97 92
  private static DistortedProgram mBlitDepthRenderProgram;
98
  private static int mBlitDepthRenderDepthTextureH;
99
  private static int mBlitDepthRenderDepthH;
100
  private static int mBlitDepthRenderTexCorrH;
101 93
  private static int mBlitDepthRenderSizeH;
102 94

  
103 95
  /// NORMAL PROGRAM /////
......
192 184
      }
193 185

  
194 186
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
195
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
196
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
197
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
198
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
199 187
    mBlitDepthSizeH         = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Size");
200
    mBlitDepthNumRecordsH   = GLES31.glGetUniformLocation( blitDepthProgramH, "u_numRecords");
201 188

  
202 189
    mIntBuffer = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
203 190
    mIntBuffer.put(0,0);
......
237 224
      }
238 225

  
239 226
    int blitDepthRenderProgramH   = mBlitDepthRenderProgram.getProgramHandle();
240
    mBlitDepthRenderDepthTextureH = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_DepthTexture");
241
    mBlitDepthRenderDepthH        = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Depth");
242
    mBlitDepthRenderTexCorrH      = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_TexCorr");
243 227
    mBlitDepthRenderSizeH         = GLES31.glGetUniformLocation( blitDepthRenderProgramH, "u_Size");
244 228

  
245 229
    // NORMAL PROGRAM //////////////////////////////////////
......
402 386
    mBlitDepthProgram.useProgram();
403 387

  
404 388
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
405
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
406
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
407
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
408
    GLES31.glUniform2i(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
409
    GLES31.glUniform1ui(mBlitDepthNumRecordsH, (mBufferSize-surface.mWidth*surface.mHeight)/3 );  // see the fragment shader
410
    GLES31.glUniform1f(mBlitDepthDepthH , 1.0f-surface.mNear);
389
    GLES31.glUniform2f(mBlitDepthSizeH, surface.mWidth, surface.mHeight);
411 390
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
412 391
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
413 392
    }
......
420 399
    {
421 400
    mBlitDepthRenderProgram.useProgram();
422 401

  
423
    analyzeBuffer(surface.mWidth, surface.mHeight);
402
    //analyzeBuffer(surface.mWidth, surface.mHeight);
424 403

  
425 404
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
426
    GLES31.glUniform1i(mBlitDepthRenderDepthTextureH, 1);
427
    GLES31.glUniform2f(mBlitDepthRenderTexCorrH, corrW, corrH );
428
    GLES31.glUniform2i(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
429
    GLES31.glUniform1f( mBlitDepthRenderDepthH , 1.0f-surface.mNear);
405
    GLES31.glUniform2f(mBlitDepthRenderSizeH, surface.mWidth, surface.mHeight);
430 406
    GLES31.glVertexAttribPointer(mBlitDepthRenderProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
431 407
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
432 408

  
......
442 418
  private static void analyzeBuffer(int w, int h)
443 419
    {
444 420
    int ptr;
445
    final int GREEN = (255<<16) + 255;
446
    final int RED   = (255<<24) + 255;
421
    final int GREEN = 1;
422
    final int RED   = 2;
447 423

  
448 424
    int left1=0, left2=0;
449 425
    int right1=0, right2=0;
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
386 386
          numRenders += currQueue.postprocess(mBuffer);
387 387
          numRenders += blitWithDepth(time, mBuffer[quality]);
388 388
          GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS);
389
          //GLES31.glFinish();
389
          //GLES31.glFlush();
390 390
          numRenders += blitWithDepthRender(time,mBuffer[quality]);  // merge the OIT linked list
391 391
          clearBuffer(mBuffer[quality]);
392
          GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS);
393
          //GLES31.glFlush();
394 392
          }
395 393
        }
396 394

  
src/main/res/raw/blit_depth_fragment_shader.glsl
20 20
precision highp float;
21 21
precision highp int;
22 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
23
out vec4 fragColor;
24
in vec2 v_TexCoordinate;
35 25

  
36
uniform sampler2D u_Texture;
37
uniform sampler2D u_DepthTexture;
26
uniform vec2 u_Size;
38 27

  
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 insert2( vec2 ij, uint depth, uint rgba )
60
  {
61
/*
62
  uint index = uint(ij.x + ij.y * float(u_Size.x));
63
  u_Records[index] = (255u<<24u)+ uint( (int(255*int(ij.y)/u_Size.y))<<16u ) + (255u);
64
  discard;
65
*/
66

  
67
  uint ptr = atomicCounterIncrement(u_Counter);
68

  
69
  if( ptr<u_numRecords )
70
    {
71
    ptr = ptr /*uint(ij.x + ij.y * float(u_Size.x))*/ + uint(u_Size.x*u_Size.y);
72

  
73
    u_Records[ptr   ] = (255u<<( (2*int(ij.x) > u_Size.x) ? 24u:16u)) + (255u);
74
    //u_Records[ptr+1u] = depth;
75
    //u_Records[ptr+2u] = rgba;
76

  
77
    uint index = uint(ij.x + ij.y * float(u_Size.x));
78
    u_Records[index] = ptr;
79
    discard;
80
    }
81
  }
82

  
83
void insert( vec2 ij, uint depth, uint rgba )
84
  {
85
  uint ptr = atomicCounterIncrement(u_Counter);
28
layout (binding=0, offset=0) uniform atomic_uint u_Counter;
86 29

  
87
  if( ptr<u_numRecords )
88
    {
89
    ptr = 3u*ptr + uint(u_Size.x*u_Size.y);
90

  
91
    u_Records[ptr   ] = 0u;
92
    u_Records[ptr+1u] = depth;
93
    u_Records[ptr+2u] = rgba;
94

  
95
    uint index = uint(ij.x + ij.y * float(u_Size.x));
96

  
97
    u_Records[index] = ptr;
98
    }
99
/*
100
  uint ptr = atomicCounterIncrement(u_Counter)-1u;
101

  
102
  if( ptr<u_numRecords )
103
    {
104
    ptr = 3u*ptr + uint(u_Size.x*u_Size.y);
105

  
106
    u_Records[ptr+1u] = depth;
107
    u_Records[ptr+2u] = rgba;
108

  
109
    memoryBarrier();
110

  
111
    uint prev = uint(ij.x + ij.y * float(u_Size.x));
112
    uint curr = u_Records[prev];
113
/*
114
    while (true)
115
      {
116
      if ( curr==0u || depth > u_Records[curr+1u] )  // need to insert here
117
        {
118

  
119
        u_Records[ptr] = curr; // next of new record is curr
120
        memoryBarrier();
121
        uint res = atomicCompSwap( u_Records[prev], curr, ptr );
122
/*
123
        if (res==curr) break;      // done!
124
        else           curr = res; // could not insert! retry from same place in list
125
        }
126
      else // advance in list
127
        {
128
        prev = curr;
129
        curr = u_Records[prev];
130
        }
131
      }
132
*/
133
//    }
134

  
135

  
136
  }
137

  
138
//////////////////////////////////////////////////////////////////////////////////////////////
139

  
140
uint convert(vec4 c)
30
layout (std430,binding=1) buffer linkedlist
141 31
  {
142
  return ((uint(255.0*c.r))<<24u) + ((uint(255.0*c.g))<<16u) + ((uint(255.0*c.b))<<8u) + uint(255.0*c.a);
143
  }
32
  uint u_Records[];
33
  };
144 34

  
145 35
//////////////////////////////////////////////////////////////////////////////////////////////
146 36

  
147 37
void main()                    		
148 38
  {
149
  vec4 frag  = TEXTURE(u_Texture     , v_TexCoordinate);
150
  float depth= TEXTURE(u_DepthTexture, v_TexCoordinate).r;
151
/*
152
  if( frag.a > 0.95 )
153
    {
154
    gl_FragDepth = depth;
155
    FRAG_COLOR   = frag;
156
    }
157
  else if( frag.a > 0.0 )
158
    {*/
159
    const float S= 2147483647.0; // max signed int. Could probably be max unsigned int but this is enough.
160
    insert2(v_Pixel, uint(S*(1.0-depth)/2.0), convert(frag) );
161
    //}
39
  uint pixelX = uint(v_TexCoordinate.x * u_Size.x);
40
  uint pixelY = uint(v_TexCoordinate.y * u_Size.y);
41
  uint index = pixelX + pixelY * uint(u_Size.x);
42

  
43
  uint ptr = atomicCounterIncrement(u_Counter) + uint(u_Size.x*u_Size.y);
44
  u_Records[ptr  ] = (v_TexCoordinate.x>0.5?2u:1u);
45
  u_Records[index] = ptr;
46
  discard;
162 47
  }
src/main/res/raw/blit_depth_render_fragment_shader.glsl
20 20
precision highp float;
21 21
precision highp int;
22 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
23
out vec4 fragColor;
24
in vec2 v_TexCoordinate;
35 25

  
36
uniform sampler2D u_DepthTexture;
26
uniform vec2 u_Size;
37 27

  
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)
28
layout (std430,binding=1) buffer linkedlist
52 29
  {
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
  }
30
  uint u_Records[];
31
  };
63 32

  
64 33
//////////////////////////////////////////////////////////////////////////////////////////////
65 34

  
66 35
void main()                    		
67 36
  {
68
  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
69
  uint ptr = u_Records[index];
37
  uint pixelX = uint(v_TexCoordinate.x * u_Size.x);
38
  uint pixelY = uint(v_TexCoordinate.y * u_Size.y);
39
  uint index  = pixelX + pixelY * uint(u_Size.x);
70 40

  
71
  if( ptr == 0u ) discard;
72
  else
73
    {
74
    u_Records[index] = 0u;
75
    vec4 color = convert(u_Records[ptr]);
76

  
77
         if( (2*int(v_Pixel.x) > u_Size.x) && color.r==1.0 ) FRAG_COLOR = color;
78
    else if( (2*int(v_Pixel.x) <=u_Size.x) && color.g==1.0 ) FRAG_COLOR = color;
79
    else FRAG_COLOR = vec4(0.0,0.0,1.0,1.0);
80
    //FRAG_COLOR = convert(u_Records[index]);
81
    //u_Records[index] = 0u;
82
    }
83

  
84
  /*
85
  uint index = uint(v_Pixel.x + v_Pixel.y * float(u_Size.x));
86
  uint curr = u_Records[index];
87

  
88
  if (curr == 0u) discard;
89

  
90
  vec4 color= vec4(0.0,0.0,0.0,0.0);
91
  u_Records[index] = 0u;
92

  
93
  while (curr > 0u)                                      // keep walking the linked list
94
    {                                                    // and blending the colors in
95
    curr = u_Records[curr];                              //
96
    //color= blend( color, convert(u_Records[curr+2u]) );  //
97

  
98
    color = convert(u_Records[curr+2u]);
99

  
100
    if( color.a == 0.0 ) color = vec4(0.0,1.0,0.0,1.0);
101
    }
102

  
103

  
104
  if( curr>0u )
105
    {
106
    curr = u_Records[curr];
107
    color= convert(u_Records[curr+2u]);
108
    }
109

  
110
  gl_FragDepth = TEXTURE(u_DepthTexture, v_TexCoordinate).r;
111
  FRAG_COLOR = color;
41
  uint ptr = u_Records[index];
112 42

  
113
//else
114
//  FRAG_COLOR = vec4(1.0,0.0,0.0,1.0);
115
*/
43
       if( u_Records[ptr]==2u ) fragColor = vec4(1.0,0.0,0.0,1.0);
44
  else if( u_Records[ptr]==1u ) fragColor = vec4(0.0,1.0,0.0,1.0);
45
  else                          fragColor = vec4(0.0,0.0,1.0,1.0);
116 46
  }
src/main/res/raw/blit_depth_vertex_shader.glsl
20 20
precision highp float;
21 21
precision highp int;
22 22

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

  
33
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
34
uniform vec2  u_TexCorr;      // when we blit from postprocessing buffers, the buffers can be
35
                              // larger than necessary (there is just one static set being
36
                              // reused!) so we need to compensate here by adjusting the texture
37
                              // coords.
38

  
39
uniform ivec2 u_Size;         // size of the output surface, in pixels.
23
in vec2 a_Position;
24
out vec2 v_TexCoordinate;
40 25

  
41 26
//////////////////////////////////////////////////////////////////////////////////////////////
42 27

  
43 28
void main()
44 29
  {
45
  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
46
  v_Pixel         = v_TexCoordinate * vec2(u_Size);
47
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
30
  v_TexCoordinate = a_Position + 0.5;
31
  gl_Position     = vec4(2.0*a_Position,1.0,1.0);
48 32
  }
src/main/res/raw/main_fragment_shader.glsl
17 17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
precision mediump float;
20
precision highp float;
21 21

  
22 22
#if __VERSION__ != 100
23 23
in vec3 v_Position;                     // Interpolated position for this fragment.
src/main/res/raw/main_vertex_shader.glsl
17 17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20
precision mediump float;
20
precision highp float;
21 21

  
22 22
#if __VERSION__ != 100
23 23
in vec3 a_Position;                  // Per-vertex position.

Also available in: Unified diff