Project

General

Profile

« Previous | Next » 

Revision 12f9e4bb

Added by Leszek Koltunski almost 6 years ago

a mix of two changes:

1) remove the DistortedInputSurface interface (now every Surface is Input)
2) make the OIT SSBO self-adjustable in size

View differences:

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

  
77 77
  /// OIT SSBO BUFFER ///
78 78
  private static int[] mLinkedListSSBO = new int[1];
79
  private static int[] mAtomicCounter = new int[1];
79
  private static int[] mAtomicCounter = new int[Distorted.FBO_QUEUE_SIZE];
80
  private static int   mCurrBuffer;
80 81

  
81 82
  static
82 83
    {
83 84
    mLinkedListSSBO[0]= -1;
84
    mAtomicCounter[0] = -1;
85
    mCurrBuffer       =  0;
86

  
87
    for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++)  mAtomicCounter[i] = -1;
85 88
    }
86 89

  
87 90
  ///////////////////////////////////////////////////////////////
......
535 538
    }
536 539

  
537 540
///////////////////////////////////////////////////////////////////////////////////////////////////
538
// reset atomic counter to 0
539 541

  
540
  static void zeroOutAtomic()
542
  private static int printPreviousBuffer()
541 543
    {
542
    if( mAtomicCounter[0]<0 )
544
    int counter = 0;
545

  
546
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
547
                                                                GLES31.GL_MAP_READ_BIT);
548
    if( atomicBuf!=null )
549
      {
550
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
551
      counter = atomicIntBuf.get(0);
552
      }
553
    else
543 554
      {
544
      GLES31.glGenBuffers(1,mAtomicCounter,0);
545
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[0]);
546
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
547
      GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW);
548
      GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
555
      android.util.Log.e("effects", "print: failed to map atomic buffer");
549 556
      }
550 557

  
551
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[0] );
558
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
552 559

  
560
    return counter;
561
    }
562

  
563
///////////////////////////////////////////////////////////////////////////////////////////////////
564

  
565
  private static void zeroBuffer()
566
    {
553 567
    ByteBuffer atomicBuf = (ByteBuffer)GLES31.glMapBufferRange( GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, 4,
554
                                                                GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT);
568
        GLES31.GL_MAP_WRITE_BIT|GLES31.GL_MAP_INVALIDATE_BUFFER_BIT);
555 569
    if( atomicBuf!=null )
556 570
      {
557 571
      IntBuffer atomicIntBuf = atomicBuf.order(ByteOrder.nativeOrder()).asIntBuffer();
558

  
559
      //int counter = atomicIntBuf.get(0);
560
      //android.util.Log.e("counter", "now = "+counter+" w="+surface.mWidth+" h="+surface.mHeight
561
      //                             +" diff="+(counter-surface.mWidth*surface.mHeight));
562 572
      atomicIntBuf.put(0,0);
563 573
      }
564 574
    else
565 575
      {
566
      android.util.Log.e("effects", "failed to map atomic buffer");
576
      android.util.Log.e("effects", "zero: failed to map atomic buffer");
567 577
      }
568 578

  
569 579
    GLES31.glUnmapBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER);
570
    GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0);
580
    }
581

  
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
// reset atomic counter to 0
584

  
585
  static int zeroOutAtomic()
586
    {
587
    int counter = 0;
588

  
589
    if( mAtomicCounter[0]<0 )
590
      {
591
      GLES31.glGenBuffers(Distorted.FBO_QUEUE_SIZE,mAtomicCounter,0);
592

  
593
      for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++)
594
        {
595
        GLES31.glBindBuffer(GLES31.GL_ATOMIC_COUNTER_BUFFER, mAtomicCounter[i]);
596
        GLES31.glBufferData(GLES31.GL_ATOMIC_COUNTER_BUFFER, 4, null, GLES31.GL_DYNAMIC_DRAW);
597
        zeroBuffer();
598
        }
599
      }
600

  
601
    // reading the value of the buffer on every frame would slow down rendering by
602
    // about 3%; doing it only once every 5 frames affects speed by less than 1%.
603
    if( mCurrBuffer==0 )
604
      {
605
      GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
606
      counter = printPreviousBuffer();
607
      }
608

  
609
    if( ++mCurrBuffer>=Distorted.FBO_QUEUE_SIZE ) mCurrBuffer = 0;
610

  
611
    GLES31.glBindBufferBase(GLES31.GL_ATOMIC_COUNTER_BUFFER, 0, mAtomicCounter[mCurrBuffer]);
612
    zeroBuffer();
613

  
614
    return counter;
571 615
    }
572 616

  
573 617
///////////////////////////////////////////////////////////////////////////////////////////////////
574 618
// Pass1 of the OIT algorithm. Clear per-pixel head-poiners.
575 619

  
576
  static void oitClear(DistortedOutputSurface surface)
620
  static void oitClear(DistortedOutputSurface surface, int counter)
577 621
    {
578 622
    if( mLinkedListSSBO[0]<0 )
579 623
      {
580
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
581

  
582 624
      GLES31.glGenBuffers(1,mLinkedListSSBO,0);
625

  
626
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
583 627
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
584 628
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
629
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
630

  
585 631
      GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mLinkedListSSBO[0]);
632
      }
633

  
634
    // See if we have overflown the SSBO in one of the previous frames.
635
    // If yes, assume we need to make the SSBO larger.
636
    float overflow = counter/(mBufferSize*surface.mWidth*surface.mHeight);
637

  
638
    if( overflow>1.0f )
639
      {
640
      //android.util.Log.e("effects", "previous frame rendered "+counter+
641
      //                   " fragments, but there was only "+(mBufferSize*surface.mWidth*surface.mHeight)+" space");
642

  
643
      mBufferSize *= (int)(overflow+1.0f);
644
      int size = (int)(surface.mWidth*surface.mHeight*(3*mBufferSize+1)*4);
645
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mLinkedListSSBO[0]);
646
      GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, size, null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
586 647
      GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
587 648
      }
588 649

  
......
665 726
  static void onPause()
666 727
    {
667 728
    mLinkedListSSBO[0]= -1;
668
    mAtomicCounter[0] = -1;
729

  
730
    for(int i=0; i<Distorted.FBO_QUEUE_SIZE; i++) mAtomicCounter[i] = -1;
669 731
    }
670 732

  
671 733
///////////////////////////////////////////////////////////////////////////////////////////////////
......
675 737
    mNextID =  0;
676 738
    }
677 739

  
740
///////////////////////////////////////////////////////////////////////////////////////////////////
741

  
742
  static void setSSBOSize(float size)
743
    {
744
    mBufferSize = size;
745
    }
746

  
678 747
///////////////////////////////////////////////////////////////////////////////////////////////////
679 748
// PUBLIC API
680 749
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff