Project

General

Profile

« Previous | Next » 

Revision 66103fb2

Added by Leszek Koltunski about 4 years ago

Speedup: only allocate one postprocessing buffer set of the quality we need, not all in one go.

View differences:

src/main/java/org/distorted/library/main/InternalOutputSurface.java
38 38
  public static final int DEPTH_NO_STENCIL    = 1;
39 39
  public static final int BOTH_DEPTH_STENCIL  = 2;
40 40

  
41
  public static final float DEFAULT_FOV = 60.0f;
42
  public static final float DEFAULT_NEAR=  0.1f;
41
  static final float DEFAULT_FOV = 60.0f;
42
  static final float DEFAULT_NEAR=  0.1f;
43 43

  
44
  float mFOV, mDistance, mNear, mMipmap;
44
  private float mFOV;
45

  
46
  private long[] mTime;
47
  private float mClearR, mClearG, mClearB, mClearA, mClearDepth;
48
  private int mClear, mClearStencil;
49
  private boolean mRenderWayOIT;
50
  private InternalChildrenList mChildren;
51

  
52
  // Global buffers used for postprocessing
53
  private static DistortedFramebuffer[] mBuffer= new DistortedFramebuffer[EffectQuality.LENGTH];
54

  
55
  float mDistance, mNear, mMipmap;
45 56
  float[] mProjectionMatrix;
46 57
  int mDepthStencilCreated, mDepthStencil;
47 58
  int[] mDepthStencilH, mFBOH;
......
50 61
  int mCurrFBO;     // internal current FBO (see DistortedLibrary.FBO_QUEUE_SIZE)
51 62
  int mWidth, mHeight;
52 63

  
53
  private static DistortedFramebuffer[] mBuffer=null; // Global buffers used for postprocessing.
54
  private long[] mTime;
55
  private float mClearR, mClearG, mClearB, mClearA, mClearDepth;
56
  private int mClear, mClearStencil;
57
  private boolean mRenderWayOIT;
58
  private InternalChildrenList mChildren;
59

  
60 64
///////////////////////////////////////////////////////////////////////////////////////////////////
61 65

  
62 66
  InternalOutputSurface(int width, int height, int createColor, int numfbos, int numcolors, int depthStencil, int fbo, int type)
......
142 146

  
143 147
///////////////////////////////////////////////////////////////////////////////////////////////////
144 148

  
145
  private static void createPostprocessingBuffers(int width, int height, float near)
149
  private static void createPostprocessingBuffers(int quality, int width, int height, float near)
146 150
    {
147 151
    final float CLEAR_R = 1.0f;
148 152
    final float CLEAR_G = 1.0f;
......
151 155
    final float CLEAR_D = 1.0f;
152 156
    final int   CLEAR_S = 0;
153 157

  
154
    mBuffer = new DistortedFramebuffer[EffectQuality.LENGTH];
155 158
    float mipmap=1.0f;
156 159

  
157
    for (int j=0; j<EffectQuality.LENGTH; j++)
158
      {
159
      mBuffer[j] = new DistortedFramebuffer(DistortedLibrary.FBO_QUEUE_SIZE,2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
160
      mBuffer[j].mMipmap = mipmap;
161
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
162
      mBuffer[j].glClearColor(CLEAR_R, CLEAR_G, CLEAR_B, CLEAR_A);
160
    for (int j=0; j<quality; j++) mipmap *= EffectQuality.MULTIPLIER;
163 161

  
164
      mipmap *= EffectQuality.MULTIPLIER;
165
      }
162
    mBuffer[quality] = new DistortedFramebuffer(DistortedLibrary.FBO_QUEUE_SIZE,2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
163
    mBuffer[quality].mMipmap = mipmap;
164
    mBuffer[quality].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
165
    mBuffer[quality].glClearColor(CLEAR_R, CLEAR_G, CLEAR_B, CLEAR_A);
166 166

  
167 167
    InternalObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
168 168

  
......
171 171
    GLES31.glClearDepthf(CLEAR_D);
172 172
    GLES31.glClearStencil(CLEAR_S);
173 173

  
174
    for (int j=0; j<EffectQuality.LENGTH; j++)
174
    for(int k = 0; k< DistortedLibrary.FBO_QUEUE_SIZE; k++)
175 175
      {
176
      for(int k = 0; k< DistortedLibrary.FBO_QUEUE_SIZE; k++)
177
        {
178
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBuffer[j].mFBOH[k]);
179
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k+1], 0);
180
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
181
        GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[2*k  ], 0);
182
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
183
        }
176
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBuffer[quality].mFBOH[k]);
177
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[quality].mColorH[2*k+1], 0);
178
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
179
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[quality].mColorH[2*k  ], 0);
180
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
184 181
      }
185 182

  
186 183
    InternalRenderState.colorDepthStencilRestore();
......
192 189

  
193 190
  static synchronized void onDestroy()
194 191
    {
195
    if( mBuffer!=null )
196
      {
197
      for (int j = 0; j < EffectQuality.LENGTH; j++)
192
    for (int j=0; j<EffectQuality.LENGTH; j++)
193
      if( mBuffer[j]!=null )
198 194
        {
195
        mBuffer[j].markForDeletion();
199 196
        mBuffer[j] = null;
200 197
        }
201

  
202
      mBuffer = null;
203
      }
204 198
    }
205 199

  
206 200
///////////////////////////////////////////////////////////////////////////////////////////////////
......
212 206
// Also, adjust the Buffers so their Projection is the same like the surface we are supposed to be
213 207
// rendering to.
214 208

  
215
  private static void clonePostprocessingViewportAndProjection(InternalOutputSurface from)
209
  private static void clonePostprocessingViewportAndProjection(InternalOutputSurface surface, InternalOutputSurface from)
216 210
    {
217
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight ||
218
        mBuffer[0].mFOV   != from.mFOV   || mBuffer[0].mNear   != from.mNear    )
211
    if( surface.mWidth != from.mWidth || surface.mHeight != from.mHeight ||
212
        surface.mFOV   != from.mFOV   || surface.mNear   != from.mNear    )
219 213
      {
220
      InternalOutputSurface surface;
221

  
222
      for(int i=0; i<EffectQuality.LENGTH; i++)
223
        {
224
        surface = mBuffer[i];
214
      surface.mWidth  = (int)(from.mWidth *surface.mMipmap);
215
      surface.mHeight = (int)(from.mHeight*surface.mMipmap);
216
      surface.mFOV    = from.mFOV;
217
      surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
225 218

  
226
        surface.mWidth  = (int)(from.mWidth *surface.mMipmap);
227
        surface.mHeight = (int)(from.mHeight*surface.mMipmap);
228
        surface.mFOV    = from.mFOV;
229
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
219
      surface.createProjection();
230 220

  
231
        surface.createProjection();
221
      int maxw = Math.max(surface.mWidth , surface.mRealWidth );
222
      int maxh = Math.max(surface.mHeight, surface.mRealHeight);
232 223

  
233
        int maxw = Math.max(surface.mWidth , surface.mRealWidth );
234
        int maxh = Math.max(surface.mHeight, surface.mRealHeight);
235

  
236
        if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
237
          {
238
          surface.mRealWidth = maxw;
239
          surface.mRealHeight = maxh;
224
      if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
225
        {
226
        surface.mRealWidth = maxw;
227
        surface.mRealHeight = maxh;
240 228

  
241
          surface.recreate();
242
          surface.create();
243
          }
229
        surface.recreate();
230
        surface.create();
244 231
        }
245 232
      }
246 233
    }
......
382 369

  
383 370
    setCurrFBO(fbo);
384 371

  
385
    if( mBuffer!=null )
386
      {
387
      for (int i=0; i<EffectQuality.LENGTH; i++) mBuffer[i].setCurrFBO(fbo);
388
      }
389

  
390 372
    if( oit && numChildren>0 )
391 373
      {
392 374
      oitClear(this);
......
414 396
        }
415 397
      else
416 398
        {
417
        if( mBuffer==null )
418
          {
419
          createPostprocessingBuffers(mWidth,mHeight,mNear);
420
          for (int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].setCurrFBO(fbo);
421
          }
399
        int currQuality = currQueue.getQuality();
400

  
401
        if( mBuffer[currQuality]==null ) createPostprocessingBuffers(currQuality, mWidth, mHeight, mNear);
402
        mBuffer[currQuality].setCurrFBO(fbo);
422 403

  
423 404
        if( lastBucket!=currBucket )
424 405
          {
425 406
          if( lastBucket==0 )
426 407
            {
427
            clonePostprocessingViewportAndProjection(this);
408
            clonePostprocessingViewportAndProjection(mBuffer[currQuality],this);
428 409
            }
429 410
          else
430 411
            {
......
452 433
            buffer.clearBuffer(fbo);
453 434
            }
454 435

  
455
          buffer= mBuffer[currQueue.getQuality()];
436
          buffer= mBuffer[currQuality];
456 437
          bucketChange= i;
457 438
          renderDirectly = currQueue.getRender();
458 439
          }
......
759 740
      mNear=0.99f;
760 741
      }
761 742

  
762
    if( mBuffer!=null )
743
    for(int j=0; j<EffectQuality.LENGTH; j++)
763 744
      {
764
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
745
      if( mBuffer[j]!=null ) mBuffer[j].mNear = mNear;
765 746
      }
766 747

  
767 748
    createProjection();
......
857 838
 *                    transparent fragments than it has space for and readjust its internal buffers,
858 839
 *                    but only after a few frames during which one will probably see missing objects.
859 840
 */
860
public void setOrderIndependentTransparency(boolean oit, float initialSize)
861
  {
862
  mRenderWayOIT = oit;
841
  public void setOrderIndependentTransparency(boolean oit, float initialSize)
842
    {
843
    mRenderWayOIT = oit;
863 844

  
864
  if( initialSize>0.0f && initialSize<10.0f )
865
    DistortedLibrary.setSSBOSize(initialSize);
866
  }
845
    if( initialSize>0.0f && initialSize<10.0f )
846
      {
847
      DistortedLibrary.setSSBOSize(initialSize);
848
      }
849
    }
867 850

  
868 851
///////////////////////////////////////////////////////////////////////////////////////////////////
869 852
/**

Also available in: Unified diff