Project

General

Profile

« Previous | Next » 

Revision baa3989b

Added by Leszek Koltunski over 6 years ago

Revert last changes to the postprocessing FBO.

This reverts commit 2dbed690bdbee80d9bfb22c800073855eb5d0946.

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffect.java
139 139
 *
140 140
 * @y.exclude
141 141
 */
142
  public abstract int apply(float[] uniforms, int index, DistortedOutputSurface[][] buffers);
142
  public abstract int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers);
143 143

  
144 144
///////////////////////////////////////////////////////////////////////////////////////////////////
145 145
/**
src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
140 140
 *
141 141
 * @y.exclude
142 142
 */
143
  public int apply(float[] uniforms, int index, DistortedOutputSurface[][] buffers)
143
  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers)
144 144
    {
145 145
    if( mProgram1 ==null)
146 146
      {
......
157 157

  
158 158
    DistortedRenderState.useStencilMark();
159 159

  
160
    DistortedFramebuffer buffer0 = (DistortedFramebuffer)buffers[mQualityLevel][0];
161
    DistortedFramebuffer buffer1 = (DistortedFramebuffer)buffers[mQualityLevel][1];
160
    DistortedFramebuffer buffer = (DistortedFramebuffer)buffers[mQualityLevel];
162 161

  
163
    float w= buffer0.getWidth();
164
    float h= buffer0.getHeight();
165
    float n= 1.0f - buffer0.getNear();
162
    buffer.setAsOutput();
166 163

  
167
    float corrW = buffer0.getWidthCorrection();
168
    float corrH = buffer0.getHeightCorrection();
164
    float w= buffer.getWidth();
165
    float h= buffer.getHeight();
166
    float n= 1.0f - buffer.getNear();
167

  
168
    float corrW = buffer.getWidthCorrection();
169
    float corrH = buffer.getHeightCorrection();
169 170
    float offsetCorrW = corrW/w;
170 171
    float offsetCorrH = corrH/h;
171 172

  
......
181 182
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
182 183

  
183 184
    mProgram1.useProgram();
184
    buffer1.setAsOutput();
185
    buffer0.setAsInput();
185
    buffer.bindForOutput(1);
186
    buffer.setAsInput(0);
186 187

  
187 188
    GLES31.glColorMask(true,true,true,true);
188 189
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
......
202 203
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
203 204

  
204 205
    mProgram2.useProgram();
205
    buffer0.setAsOutput();
206
    buffer1.setAsInput();
206
    buffer.bindForOutput(0);
207
    buffer.setAsInput(1);
207 208

  
208 209
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
209 210

  
src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
144 144
 *
145 145
 * @y.exclude
146 146
 */
147
  public int apply(float[] uniforms, int index, DistortedOutputSurface[][] buffers)
147
  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers)
148 148
    {
149 149
    if( mProgram1 ==null)
150 150
      {
......
161 161

  
162 162
    DistortedRenderState.useStencilMark();
163 163

  
164
    DistortedFramebuffer  inBuffer  = (DistortedFramebuffer)buffers[0][0];
165
    DistortedFramebuffer outBuffer0 = (DistortedFramebuffer)buffers[mQualityLevel][0];
166
    DistortedFramebuffer outBuffer1 = (DistortedFramebuffer)buffers[mQualityLevel][1];
164
    DistortedFramebuffer  inBuffer = (DistortedFramebuffer)buffers[0];
165
    DistortedFramebuffer outBuffer = (DistortedFramebuffer)buffers[mQualityLevel];
167 166

  
168
    float w= outBuffer0.getWidth();
169
    float h= outBuffer0.getHeight();
170
    float n= 1.0f - outBuffer0.getNear();
167
    float w= outBuffer.getWidth();
168
    float h= outBuffer.getHeight();
169
    float n= 1.0f - outBuffer.getNear();
171 170

  
172 171
    float corrW = inBuffer.getWidthCorrection();
173 172
    float corrH = inBuffer.getHeightCorrection();
......
181 180
    int offset = radius + radius*radius/4;
182 181
    radius = (radius+1)/2;
183 182

  
183
    outBuffer.setAsOutput();
184 184
    GLES31.glViewport(0, 0, (int)w, (int)h);
185 185

  
186 186
    // horizontal glow
187 187
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
188 188

  
189 189
    mProgram1.useProgram();
190
    outBuffer1.setAsOutput();
191
    inBuffer.setAsInput();
190
    outBuffer.bindForOutput(1);
191
    inBuffer.setAsInput(0);
192 192

  
193 193
    GLES31.glColorMask(true,true,true,true);
194 194
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
......
209 209
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
210 210

  
211 211
    mProgram2.useProgram();
212
    outBuffer0.setAsOutput();
213
    outBuffer1.setAsInput();
212
    outBuffer.bindForOutput(0);
213
    outBuffer.setAsInput(1);
214 214

  
215 215
    GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
216 216
    GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
......
229 229
    if( mQualityLevel>0 )
230 230
      {
231 231
      inBuffer.setAsOutput();
232
      outBuffer0.setAsInput();
232
      outBuffer.setAsInput(0);
233 233
      GLES31.glEnable(GLES31.GL_BLEND);
234 234
      DistortedEffects.blitPriv(inBuffer);
235 235
      GLES31.glDisable(GLES31.GL_BLEND);
src/main/java/org/distorted/library/main/DistortedFramebuffer.java
39 39
    if( mColorCreated==NOT_CREATED_YET )
40 40
      {
41 41
      GLES31.glGenTextures( mNumColors, mColorH, 0);
42
      GLES31.glGenFramebuffers(1, mFBOH, 0);
43
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
42 44

  
43 45
      for(int i=0; i<mNumColors; i++)
44 46
        {
......
49 51
        GLES31.glTexParameterf(GLES31.GL_TEXTURE_2D, GLES31.GL_TEXTURE_MAG_FILTER, GLES31.GL_LINEAR);
50 52
        GLES31.glTexImage2D(GLES31.GL_TEXTURE_2D, 0, GLES31.GL_RGBA, mRealWidth, mRealHeight, 0, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, null);
51 53
        }
52
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
53

  
54
      GLES31.glGenFramebuffers(1, mFBOH, 0);
55
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
56 54
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mColorH[0], 0);
55
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
57 56
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
58 57

  
59 58
      mColorCreated = checkStatus("color");
......
78 77
        }
79 78

  
80 79
      GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
81

  
82 80
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, mFBOH[0]);
83 81

  
84 82
      if( mDepthStencil==DEPTH_NO_STENCIL )
......
107 105
      GLES31.glDeleteTextures(1, mDepthStencilH, 0);
108 106
      mDepthStencilH[0]=0;
109 107
      }
110
    if( mDepthStencilCreated==COPIED && mDepthStencilH[0]>0 )
111
      {
112
      mDepthStencilCreated = DONT_CREATE;
113
      mDepthStencilH[0]=0;
114
      }
115 108
    }
116 109

  
117 110
///////////////////////////////////////////////////////////////////////////////////////////////////
......
135 128
    return CREATED;
136 129
    }
137 130

  
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
// the Framebuffer was created without DEPTH or STENCIL but now we want to copy it from some other.
140
// Framebuffer 'from' MUST be already created!!
141
// Must be called from a thread holding OpenGL Context
142
//
143
// 'Private' , library-only function used only for the postprocessing buffers!
144

  
145
  void copyDepthAndStencil(DistortedFramebuffer from)
146
    {
147
    mDepthStencilCreated = COPIED;
148
    mDepthStencilH[0] = from.mDepthStencilH[0];
149
    }
150

  
151 131
///////////////////////////////////////////////////////////////////////////////////////////////////
152 132
// Must be called from a thread holding OpenGL Context
153 133

  
src/main/java/org/distorted/library/main/DistortedObject.java
39 39
  static final int NOT_CREATED_YET  = 2;
40 40
  static final int DONT_CREATE      = 3;
41 41
  static final int CREATED          = 4;
42
  static final int COPIED           = 5;
43 42

  
44 43
  static final int TYPE_USER = 0;
45 44
  static final int TYPE_TREE = 1;
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
69 69

  
70 70
  private ArrayList<Job> mJobs = new ArrayList<>();
71 71

  
72
  // Global buffers used for postprocessing. MipmapLevels x PING_PONG of each for postprocessing ping-pong.
73
  private static final int PING_PONG = 2;
74
  private static DistortedOutputSurface[][] mBuffer = null;
72
  // Global buffers used for postprocessing.
73
  private static DistortedOutputSurface[] mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH];
75 74

  
76 75
  private long mTime;
77 76
  private float mFOV;
......
172 171

  
173 172
  private static void createPostprocessingBuffers(int width, int height, float near)
174 173
    {
175
    mBuffer = new DistortedOutputSurface[EffectQuality.LENGTH][PING_PONG];
176 174
    float mipmap=1.0f;
177 175

  
178
    for (int i=0; i<EffectQuality.LENGTH; i++)
176
    for (int j=0; j<EffectQuality.LENGTH; j++)
179 177
      {
180
      mBuffer[i][0] = new DistortedFramebuffer(1, BOTH_DEPTH_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
181
      mBuffer[i][0].mMipmap = mipmap;
182
      mBuffer[i][0].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
183
      mBuffer[i][0].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
184

  
185
      for(int j=1; j<PING_PONG; j++)
186
        {
187
        mBuffer[i][j] = new DistortedFramebuffer(1, NO_DEPTH_NO_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
188
        mBuffer[i][j].mMipmap = mipmap;
189
        mBuffer[i][j].mNear = near;
190
        mBuffer[i][j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
191
        }
178
      mBuffer[j] = new DistortedFramebuffer(2, BOTH_DEPTH_STENCIL, TYPE_SYST, (int) (width * mipmap), (int) (height * mipmap));
179
      mBuffer[j].mMipmap = mipmap;
180
      mBuffer[j].mNear = near;  // copy mNear as well (for blitting- see PostprocessEffect.apply() )
181
      mBuffer[j].glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
192 182

  
193 183
      mipmap *= EffectQuality.MULTIPLIER;
194 184
      }
195 185

  
196 186
    DistortedObject.toDo(); // create the FBOs immediately. This is safe as we must be holding the OpenGL context now.
197 187

  
198
    for (int i=0; i<EffectQuality.LENGTH; i++)
199
      {
200
      for(int j=1; j<PING_PONG; j++)
201
        {
202
        ( (DistortedFramebuffer)mBuffer[i][j]).copyDepthAndStencil( (DistortedFramebuffer)mBuffer[i][0] );
203
        }
204
      }
205

  
206 188
    GLES31.glStencilMask(0xff);
207 189
    GLES31.glDepthMask(true);
208 190
    GLES31.glColorMask(true, true, true, true);
......
210 192
    GLES31.glClearDepthf(1.0f);
211 193
    GLES31.glClearStencil(0);
212 194

  
213
    for (int i=0; i<EffectQuality.LENGTH; i++)
195
    for (int j=0; j<EffectQuality.LENGTH; j++)
214 196
      {
215
      mBuffer[i][0].setAsOutput();
197
      mBuffer[j].setAsOutput();
198
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[1], 0);
216 199
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT | GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_STENCIL_BUFFER_BIT);
217

  
218
      for(int j=1; j<PING_PONG; j++)
219
        {
220
        mBuffer[i][j].setAsOutput();
221
        GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
222
        }
200
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, mBuffer[j].mColorH[0], 0);
201
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
223 202
      }
224 203
    }
225 204

  
......
227 206

  
228 207
  static synchronized void onDestroy()
229 208
    {
230
    if( mBuffer!=null )
209
    for(int j=0; j<EffectQuality.LENGTH; j++)
231 210
      {
232
      for (int i=0; i<EffectQuality.LENGTH; i++)
233
        {
234
        for (int j=1; j<PING_PONG; j++)
235
          {
236
          mBuffer[i][j] = null;
237
          }
238
        }
239

  
240
      mBuffer = null;
211
      mBuffer[j] = null;
241 212
      }
242 213
    }
243 214

  
......
249 220

  
250 221
  private static void clonePostprocessingViewport(DistortedOutputSurface from)
251 222
    {
252
    if( mBuffer[0][0].mWidth != from.mWidth || mBuffer[0][0].mHeight != from.mHeight )
223
    if( mBuffer[0].mWidth != from.mWidth || mBuffer[0].mHeight != from.mHeight )
253 224
      {
254 225
      DistortedOutputSurface surface;
255 226

  
256 227
      for(int i=0; i<EffectQuality.LENGTH; i++)
257 228
        {
258
        for(int j=0; j<PING_PONG; j++)
259
          {
260
          surface = mBuffer[i][j];
229
        surface = mBuffer[i];
261 230

  
262
          surface.mWidth  = (int) (from.mWidth  * surface.mMipmap);
263
          surface.mHeight = (int) (from.mHeight * surface.mMipmap);
231
        surface.mWidth  = (int)(from.mWidth *surface.mMipmap);
232
        surface.mHeight = (int)(from.mHeight*surface.mMipmap);
264 233

  
265
          surface.mNear = from.mNear;  // Near plane is independent of the mipmap level
234
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
266 235

  
267
          //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
236
        //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
268 237

  
269
          surface.createProjection();
238
        surface.createProjection();
270 239

  
271
          int maxw = surface.mWidth  > surface.mRealWidth  ? surface.mWidth  : surface.mRealWidth;
272
          int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
240
        int maxw = surface.mWidth  > surface.mRealWidth  ? surface.mWidth  : surface.mRealWidth;
241
        int maxh = surface.mHeight > surface.mRealHeight ? surface.mHeight : surface.mRealHeight;
273 242

  
274
          if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
275
            {
276
            surface.mRealWidth = maxw;
277
            surface.mRealHeight = maxh;
243
        if (maxw > surface.mRealWidth || maxh > surface.mRealHeight)
244
          {
245
          surface.mRealWidth = maxw;
246
          surface.mRealHeight = maxh;
278 247

  
279
            surface.recreate();
280
            surface.create();
281
            }
248
          surface.recreate();
249
          surface.create();
282 250
          }
283 251
        }
284 252
      }
......
320 288
    }
321 289

  
322 290
///////////////////////////////////////////////////////////////////////////////////////////////////
323
// two phases: 1. collapse the SSBO 2. blend the SSBO's color
291
// two phases: 1. collapse the SSBO 2. blend the ssbo's color
324 292

  
325 293
  private int oitRender(long currTime)
326 294
    {
......
350 318

  
351 319
///////////////////////////////////////////////////////////////////////////////////////////////////
352 320

  
353
  private static void clearPostprocessingBuffer(int quality)
321
  private static void clearBuffer(DistortedOutputSurface buffer)
354 322
    {
355 323
    GLES31.glStencilMask(0xff);
356 324
    GLES31.glDepthMask(true);
......
359 327
    GLES31.glClearDepthf(1.0f);
360 328
    GLES31.glClearStencil(0);
361 329

  
362
    mBuffer[quality][0].setAsOutput();
330
    buffer.setAsOutput();
331
    GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[buffer.mNumColors-1], 0);
363 332
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT|GLES31.GL_DEPTH_BUFFER_BIT|GLES31.GL_STENCIL_BUFFER_BIT);
364 333

  
365
    for(int j=1; j<PING_PONG; j++)
334
    for(int i=buffer.mNumColors-2; i>=0; i--)
366 335
      {
367
      mBuffer[quality][j].setAsOutput();
336
      GLES31.glFramebufferTexture2D(GLES31.GL_FRAMEBUFFER, GLES31.GL_COLOR_ATTACHMENT0, GLES31.GL_TEXTURE_2D, buffer.mColorH[i], 0);
368 337
      GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
369 338
      }
370 339
    }
......
406 375
        }
407 376
      else
408 377
        {
409
        if( mBuffer==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
378
        if( mBuffer[0]==null ) createPostprocessingBuffers(mWidth,mHeight,mNear);
410 379

  
411 380
        if( lastBucket!=currBucket )
412 381
          {
......
420 389
            for(int j=bucketChange; j<i; j++)
421 390
              {
422 391
              child2 = children.get(j);
423
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality][0],lastQueue);
392
              numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],lastQueue);
424 393
              }
425 394

  
426 395
            numRenders += lastQueue.postprocess(mBuffer);
427
            numRenders += oitBuild(mBuffer[quality][0]);
396
            numRenders += oitBuild(mBuffer[quality]);
428 397
            GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
429
            clearPostprocessingBuffer(quality);
398
            clearBuffer(mBuffer[quality]);
430 399
            }
431 400

  
432 401
          internalQuality = currQueue.getInternalQuality();
......
434 403
          bucketChange    = i;
435 404
          }
436 405

  
437
        mBuffer[quality][0].setAsOutput(time);
438
        child1.drawNoBlend(time,mBuffer[quality][0]);
406
        mBuffer[quality].setAsOutput(time);
407
        child1.drawNoBlend(time,mBuffer[quality]);
439 408

  
440 409
        if( i==numChildren-1 )
441 410
          {
442 411
          for(int j=bucketChange; j<numChildren; j++)
443 412
            {
444 413
            child2 = children.get(j);
445
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality][0],currQueue);
414
            numRenders += child2.markStencilAndDepth(time,mBuffer[internalQuality],currQueue);
446 415
            }
447 416

  
448 417
          numRenders += currQueue.postprocess(mBuffer);
449
          numRenders += oitBuild(mBuffer[quality][0]);
418
          numRenders += oitBuild(mBuffer[quality]);
450 419
          GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT|GLES31.GL_ATOMIC_COUNTER_BARRIER_BIT);
451 420
          numRenders += oitRender(time);  // merge the OIT linked list
452
          clearPostprocessingBuffer(quality);
421
          clearBuffer(mBuffer[quality]);
453 422
          }
454 423
        } // end else (postprocessed child)
455 424

  
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
100 100

  
101 101
///////////////////////////////////////////////////////////////////////////////////////////////////
102 102

  
103
  int postprocess(DistortedOutputSurface[][] buffers)
103
  int postprocess(DistortedOutputSurface[] buffers)
104 104
    {
105 105
    int numRenders = 0;
106 106

  

Also available in: Unified diff