Project

General

Profile

« Previous | Next » 

Revision 56c6ca24

Added by Leszek Koltunski almost 6 years ago

OIT: lots of progress on all fronts. Still a few bugs to solve though!

View differences:

src/main/java/org/distorted/library/main/DistortedOutputSurface.java
78 78

  
79 79
  // Global buffers used for postprocessing.
80 80
  private static DistortedOutputSurface[] mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH];
81
  private static DistortedOutputSurface   mBufferOIT;
81 82

  
82 83
  private long mTime;
83 84
  private float mFOV;
......
184 185

  
185 186
///////////////////////////////////////////////////////////////////////////////////////////////////
186 187

  
187
  private static void createBuffers(int width, int height, float near)
188
  private static void createPostprocessingBuffers(int width, int height, float near)
188 189
    {
189 190
    float mipmap=1.0f;
190 191

  
191
    for(int j=0; j<EffectQuality.LENGTH; j++)
192
    for (int j=0; j<EffectQuality.LENGTH; j++)
192 193
      {
193
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(width*mipmap), (int)(height*mipmap) );
194
      mBuffer[j] = new DistortedFramebuffer(2, BOTH_DEPTH_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
194 195
      mBuffer[j].mMipmap = mipmap;
195
      mBuffer[j].mNear   = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
196
      mBuffer[j].glClearColor(1.0f,1.0f,1.0f,0.0f);
196
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
197
      mBuffer[j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
197 198

  
198 199
      mipmap *= EffectQuality.MULTIPLIER;
199 200
      }
......
202 203

  
203 204
    GLES31.glStencilMask(0xff);
204 205
    GLES31.glDepthMask(true);
205
    GLES31.glColorMask(true,true,true,true);
206
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
206
    GLES31.glColorMask(true, true, true, true);
207
    GLES31.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
207 208
    GLES31.glClearDepthf(1.0f);
208 209
    GLES31.glClearStencil(0);
209 210

  
210
    for(int j=0; j<EffectQuality.LENGTH; j++)
211
    for (int j=0; j<EffectQuality.LENGTH; j++)
211 212
      {
212 213
      mBuffer[j].setAsOutput();
213 214
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
214
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
215
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
215 216
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
216 217
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
217 218
      }
218 219
    }
219 220

  
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

  
223
  private static void initializeOIT(DistortedOutputSurface surface)
224
    {
225
    if( mBufferOIT==null )
226
      {
227
      mBufferOIT = new DistortedFramebuffer(1, BOTH_DEPTH_STENCIL, TYPE_SYST, surface.mWidth, surface.mHeight);
228
      mBufferOIT.mMipmap = 1.0f;
229
      mBufferOIT.mNear = surface.mNear;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
230
      mBufferOIT.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
231

  
232
      DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
233

  
234
      mBufferOIT.setAsOutput();
235
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBufferOIT.mColorH[0], 0);
236
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
237
      }
238

  
239
    if( mBufferOIT.mWidth != surface.mWidth || mBufferOIT.mHeight != surface.mHeight )
240
      {
241
      mBufferOIT.mWidth  = (int)(surface.mWidth *mBufferOIT.mMipmap);
242
      mBufferOIT.mHeight = (int)(surface.mHeight*mBufferOIT.mMipmap);
243

  
244
      mBufferOIT.mNear   = surface.mNear;  // Near plane is independent of the mipmap level
245

  
246
      //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
247

  
248
      mBufferOIT.createProjection();
249

  
250
      int maxw = mBufferOIT.mWidth  > mBufferOIT.mRealWidth  ? mBufferOIT.mWidth  : mBufferOIT.mRealWidth;
251
      int maxh = mBufferOIT.mHeight > mBufferOIT.mRealHeight ? mBufferOIT.mHeight : mBufferOIT.mRealHeight;
252

  
253
      if (maxw > mBufferOIT.mRealWidth || maxh > mBufferOIT.mRealHeight)
254
        {
255
        mBufferOIT.mRealWidth = maxw;
256
        mBufferOIT.mRealHeight = maxh;
257

  
258
        mBufferOIT.recreate();
259
        mBufferOIT.create();
260
        }
261
      }
262

  
263
    if( mBufferOIT.mNear != surface.mNear || mBufferOIT.mFOV != surface.mFOV )
264
      {
265
      mBufferOIT.mNear = surface.mNear;
266
      mBufferOIT.mFOV  = surface.mFOV;
267
      mBufferOIT.createProjection();
268
      }
269

  
270
    GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBufferOIT.mFBOH[0]);
271

  
272
    DistortedRenderState.colorDepthStencilOn();
273
    GLES31.glClearColor(surface.mClearR, surface.mClearG, surface.mClearB, surface.mClearA);
274
    GLES31.glClearDepthf(surface.mClearDepth);
275
    GLES31.glClearStencil(surface.mClearStencil);
276
    GLES31.glClear(surface.mClear);
277
    DistortedRenderState.colorDepthStencilRestore();
278

  
279
    DistortedEffects.zeroOutAtomic();
280
    DistortedEffects.oitClear(surface);
281

  
282
    GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
283
    }
284

  
220 285
///////////////////////////////////////////////////////////////////////////////////////////////////
221 286

  
222 287
  static synchronized void onDestroy()
......
225 290
      {
226 291
      mBuffer[j] = null;
227 292
      }
293

  
294
    mBufferOIT = null;
228 295
    }
229 296

  
230 297
///////////////////////////////////////////////////////////////////////////////////////////////////
......
233 300
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
234 301
// adjust the texture coords - see the get{Width|Height}Correction functions.
235 302

  
236
  private static void cloneViewport(DistortedOutputSurface from)
303
  private static void clonePostprocessingViewport(DistortedOutputSurface from)
237 304
    {
238
    if( mBuffer[0].mWidth != from.mWidth )
305
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight )
239 306
      {
240 307
      DistortedOutputSurface surface;
241 308

  
......
269 336

  
270 337
///////////////////////////////////////////////////////////////////////////////////////////////////
271 338

  
272
  private int blitWithDepth(long currTime, DistortedOutputSurface buffer)
339
  private int oitBuild(long currTime, DistortedOutputSurface buffer)
273 340
    {
274 341
    GLES31.glViewport(0, 0, mWidth, mHeight);
275 342
    setAsOutput(currTime);
......
278 345
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
279 346
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
280 347

  
281
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
282
    GLES31.glStencilMask(0x00);
348
    //GLES31.glDisable(GLES31.GL_STENCIL_TEST);
349
    //GLES31.glStencilMask(0x00);
350

  
351
    DistortedRenderState.colorDepthStencilOn();
352
    DistortedRenderState.enableDepthTest();
283 353

  
284
    DistortedEffects.blitDepth(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
354
    DistortedEffects.oitBuild(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
285 355
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
286 356
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
287 357
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
288 358
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
289 359

  
360
    DistortedRenderState.colorDepthStencilRestore();
361
    DistortedRenderState.restoreDepthTest();
362

  
290 363
    return 1;
291 364
    }
292 365

  
293 366
///////////////////////////////////////////////////////////////////////////////////////////////////
294 367

  
295
  private int mergeOIT(long currTime, DistortedOutputSurface buffer)
368
  private int oitRender(long currTime, DistortedOutputSurface buffer)
296 369
    {
297 370
    GLES31.glViewport(0, 0, mWidth, mHeight);
298 371
    setAsOutput(currTime);
372
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
373
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mColorH[0]);
299 374
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
300 375
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, buffer.mDepthStencilH[0]);
301 376

  
302 377
    DistortedRenderState.enableStencil();
303 378

  
304
    DistortedEffects.mergeOIT(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
379
    DistortedEffects.oitRender(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
305 380
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
306 381
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
307 382

  
......
331 406
///////////////////////////////////////////////////////////////////////////////////////////////////
332 407
// Render all children, one by one. If there are no postprocessing effects, just render to THIS.
333 408
// Otherwise, render to a buffer and on each change of Postprocessing Bucket, apply the postprocessing
334
// to a whole buffer (lastQueue.postprocess) and merge it (this.blitWithDepth).
409
// to a whole buffer (lastQueue.postprocess) and merge it (this.oitBuild).
335 410

  
336 411
  int renderChildren(long time, int numChildren, ArrayList<DistortedNode> children)
337 412
    {
338 413
    int quality=0, internalQuality = 0, numRenders = 0, bucketChange = 0;
339 414
    DistortedNode child1, child2;
340 415
    EffectQueuePostprocess lastQueue=null, currQueue;
341
    long lastBucket=0, currBucket;
416
    long lastBucket=0, currBucket=0;
417

  
418
    initializeOIT(this);
342 419

  
343 420
    for(int i=0; i<numChildren; i++)
344 421
      {
......
346 423
      currQueue = child1.getPostprocessQueue();
347 424
      currBucket= currQueue.getID();
348 425

  
349
      if( currBucket==0 ) numRenders += child1.draw(time,this);
426
      if( currBucket==0 )
427
        {
428
        GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mBufferOIT.mFBOH[0]);
429
        numRenders += child1.draw(time, mBufferOIT);
430

  
431
        //setAsOutput(time);
432
        //numRenders += child1.draw(time,this);
433
        }
350 434
      else
351 435
        {
352
        if( mBuffer[0]==null ) createBuffers(mWidth,mHeight,mNear);
436
        if( mBuffer[0]==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
353 437

  
354 438
        if( lastBucket!=currBucket )
355 439
          {
356 440
          if( lastBucket==0 )
357 441
            {
358
            DistortedEffects.zeroOutAtomic();
442
            clonePostprocessingViewport(this);
359 443
            }
360 444
          else
361 445
            {
......
366 450
              }
367 451

  
368 452
            numRenders += lastQueue.postprocess(mBuffer);
369
            numRenders += blitWithDepth(time, mBuffer[quality]);
453
            numRenders += mBufferOIT.oitBuild(time, mBuffer[quality]);
370 454
            clearBuffer(mBuffer[quality]);
371 455
            }
372 456

  
373 457
          internalQuality = currQueue.getInternalQuality();
374 458
          quality         = currQueue.getQuality();
375 459
          bucketChange    = i;
376

  
377
          cloneViewport(this);
378 460
          }
379 461

  
462
        mBuffer[quality].setAsOutput(time);
380 463
        child1.drawNoBlend(time,mBuffer[quality]);
381 464

  
382 465
        if( i==numChildren-1 )
......
388 471
            }
389 472

  
390 473
          numRenders += currQueue.postprocess(mBuffer);
391
          numRenders += blitWithDepth(time, mBuffer[quality]);
392
          GLES31.glMemoryBarrier(GLES31.GL_ALL_BARRIER_BITS);
393
          numRenders += mergeOIT(time,mBuffer[quality]);  // merge the OIT linked list
474
          numRenders += mBufferOIT.oitBuild(time, mBuffer[quality]);
475
          GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT);
476
          numRenders += oitRender(time,mBufferOIT);  // merge the OIT linked list
394 477
          clearBuffer(mBuffer[quality]);
395 478
          }
396
        }
479
        } // end else (postprocessed child)
397 480

  
398 481
      lastQueue = currQueue;
399 482
      lastBucket= currBucket;
483
      } // end main for loop
484

  
485
    if( currBucket==0 ) // there was no postprocessing - we need to merge the main buffer
486
      {
487
      numRenders += oitRender(time,mBufferOIT);
400 488
      }
401 489

  
402 490
    return numRenders;
......
669 757
      mNear=0.99f;
670 758
      }
671 759

  
672
    if( mBuffer[0]!=null )
673
      {
674
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
675
      }
676

  
677 760
    createProjection();
678 761
    }
679 762

  

Also available in: Unified diff