Project

General

Profile

« Previous | Next » 

Revision c731c612

Added by Leszek Koltunski over 6 years ago

Fix the 'OIT' artefacts.

View differences:

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

  
86 86
  private static IntBuffer mIntBuffer;
87 87

  
88
private static ByteBuffer mBuf;
89
private static IntBuffer mIntBuf;
88
private static ByteBuffer mBuf, mAtomicBuf;
89
private static IntBuffer mIntBuf, mAtomicIntBuf;
90 90

  
91 91
  /// BLIT DEPTH RENDER PROGRAM ///
92 92
  private static DistortedProgram mBlitDepthRenderProgram;
......
192 192
    if( mLinkedListSSBO[0]<0 )
193 193
      {
194 194
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
195
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
195 196
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
196 197
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, mBufferSize*4 , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
197

  
198
      mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
199
      mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
200

  
201
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
198
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
202 199
      }
203 200

  
204 201
    if( mAtomicCounter[0]<0 )
205 202
      {
206 203
      GLES31.glGenBuffers(1,mAtomicCounter,0);
204
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[0]);
207 205
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
208 206
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
209 207
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
......
408 406

  
409 407
    // reset atomic counter to 0
410 408
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
411
    GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, mIntBuffer, GLES31.GL_DYNAMIC_DRAW);
409

  
410
    mAtomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
411
                                                      GLES31.GL_MAP_READ_BIT|GLES31.GL_MAP_WRITE_BIT);
412
    if( mAtomicBuf!=null )
413
      {
414
      mAtomicIntBuf = mAtomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
415

  
416
      int counter = mAtomicIntBuf.get(0);
417
      mAtomicIntBuf.put(0, 0);
418
      //android.util.Log.e("counter", "now = "+counter+" w="+surface.mWidth+" h="+surface.mHeight
419
      //                             +" diff="+(counter-surface.mWidth*surface.mHeight));
420
      }
421
    else
422
      {
423
      android.util.Log.e("counter", "failed to map buffer");
424
      }
425

  
426
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
412 427
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
413 428
    }
414 429

  
415 430
///////////////////////////////////////////////////////////////////////////////////////////////////
416 431

  
417

  
418 432
  private static void analyzeBuffer(int w, int h)
419 433
    {
420
    int ptr;
421
    final int GREEN = 1;
422
    final int RED   = 2;
434
    int ptr, index;
435
    int errors = 0;
423 436

  
424
    int left1=0, left2=0;
425
    int right1=0, right2=0;
426
    int overflow = 0;
437
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
438
    mBuf = (ByteBuffer)GLES31.glMapBufferRange(GLES31.GL_SHADER_STORAGE_BUFFER, 0, mBufferSize*4, GLES31.GL_MAP_READ_BIT);
439
    mIntBuf = mBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
427 440

  
428
    for(int row=0; row<h; row++)
429
      for(int col=0; col<w; col++)
441
    for(int col=0; col<w; col++)
442
      for(int row=0; row<h; row++)
430 443
        {
431
        ptr = mIntBuf.get( col+row*w );
444
        index = col+row*w;
445
        ptr = mIntBuf.get(index);
432 446

  
433 447
        if( ptr!=0 )
434 448
          {
435 449
          if( ptr>0 && ptr<mBufferSize )
436 450
            {
437 451
            ptr = mIntBuf.get(ptr);
438

  
439
            if ((2*col>w) && ptr != RED)
440
              {
441
              if (ptr==GREEN) right1++;
442
              else            right2++;
443
              }
444
            if ((2*col<=w) && ptr != GREEN)
452
            if( ptr != index )
445 453
              {
446
              if (ptr==RED) left1++;
447
              else          left2++;
454
              android.util.Log.d("surface", "col="+col+" row="+row+" val="+ptr+" expected: "+index);
455
              errors++;
448 456
              }
449 457
            }
450 458
          else
451 459
            {
452
            overflow++;
460
            android.util.Log.d("surface", "overflow!");
453 461
            }
454 462
          }
455 463
        }
456 464

  
457
    android.util.Log.e("surface", " left1: "+left1+" left2: "+left2+" right1: "+right1+" right2: "+right2+" overflow: "+overflow);
465
    GLES31.glUnmapBuffer(GLES31.GL_SHADER_STORAGE_BUFFER);
466
    GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
467

  
468
    if( errors>0 ) android.util.Log.e("surface", "errors: "+errors);
458 469
    }
459 470

  
460 471
///////////////////////////////////////////////////////////////////////////////////////////////////
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.glFlush();
389
          //GLES31.glFinish();
390 390
          numRenders += blitWithDepthRender(time,mBuffer[quality]);  // merge the OIT linked list
391 391
          clearBuffer(mBuffer[quality]);
392
          //GLES31.glFinish();
392 393
          }
393 394
        }
394 395

  
src/main/res/raw/blit_depth_fragment_shader.glsl
38 38
  {
39 39
  uint pixelX = uint(v_TexCoordinate.x * u_Size.x);
40 40
  uint pixelY = uint(v_TexCoordinate.y * u_Size.y);
41
  uint index = pixelX + pixelY * uint(u_Size.x);
41
  uint index  = pixelX + pixelY * uint(u_Size.x);
42 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);
43
  uint ptr = uint(u_Size.x*u_Size.y) + atomicCounterIncrement(u_Counter);
44
  u_Records[ptr  ] = index;
45 45
  u_Records[index] = ptr;
46 46
  discard;
47 47
  }
src/main/res/raw/blit_depth_render_fragment_shader.glsl
39 39
  uint index  = pixelX + pixelY * uint(u_Size.x);
40 40

  
41 41
  uint ptr = u_Records[index];
42
  uint color = u_Records[ptr];
43
  //u_Records[ptr] = 0u;
42 44

  
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);
45
  if( color==index ) fragColor = vec4(0.0,1.0,0.0,1.0);
46
  else               fragColor = vec4(1.0,0.0,0.0,1.0);
46 47
  }

Also available in: Unified diff