Project

General

Profile

« Previous | Next » 

Revision ae2802b1

Added by Leszek Koltunski over 6 years ago

Postprocessing buffers mBuffer[] are now shared among all postprocessing operations. This saves a lot of memory, but also means that when doing each particular postprocessing, the textures backing up the mBuffer might be too large. We need to fix two things here: when outputting to those too large textures, we need to adjust the Viewport, and when binding those too large textures as input - we need to adjust the TexCoords to compensate.

This commit does just that. Verified as working by the 'PostprocessTree' app.

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
158 158
    float h= buffer.getHeight();
159 159
    float n= 1.0f - buffer.getNear();
160 160

  
161
    float corrW = buffer.getWidthCorrection();
162
    float corrH = buffer.getHeightCorrection();
163
    float offsetCorrW = corrW/w;
164
    float offsetCorrH = corrH/h;
165

  
161 166
    int radius = (int)(uniforms[index]*mQualityScale);
162 167
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
163 168
    computeGaussianKernel(radius);
......
167 172
    GLES31.glViewport(0, 0, (int)w, (int)h);
168 173

  
169 174
    // horizontal blur
170
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w;
175
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
171 176

  
172 177
    mProgram1.useProgram();
173 178
    buffer.bindForOutput(1);
174 179
    buffer.setAsInput(0);
175 180

  
176 181
    GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
177
    GLES31.glUniform1i ( mProgram1.mUniform[1] , 0 );
178
    GLES31.glUniform1fv( mProgram1.mUniform[2] , radius+1, mOffsets,0);
179
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, weightsCache,offset);
180
    GLES31.glUniform1i ( mProgram1.mUniform[4] , radius);
182
    GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
183
    GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
184
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
185
    GLES31.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
186
    GLES31.glUniform1i ( mProgram1.mUniform[5] , radius);
181 187
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
182 188
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
183 189
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
184 190

  
185 191
    // vertical blur
186
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h;
192
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
187 193

  
188 194
    mProgram2.useProgram();
189 195
    buffer.bindForOutput(0);
......
194 200
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
195 201

  
196 202
    GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
197
    GLES31.glUniform1i ( mProgram2.mUniform[1] , 0 );
198
    GLES31.glUniform1fv( mProgram2.mUniform[2] , radius+1, mOffsets,0);
199
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, weightsCache,offset);
200
    GLES31.glUniform1i ( mProgram2.mUniform[4] , radius);
203
    GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
204
    GLES31.glUniform1i ( mProgram2.mUniform[2] , 0 );
205
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
206
    GLES31.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
207
    GLES31.glUniform1i ( mProgram2.mUniform[5] , radius);
201 208
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
202 209
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
203 210
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
......
223 230
      "in vec2 a_TexCoord;    \n"+
224 231
      "out vec2 v_TexCoord;   \n"+
225 232
      "uniform float u_Depth; \n"+
233
      "uniform vec2 u_TexCorr;\n"+
226 234

  
227 235
      "void main()                                      \n"+
228 236
      "  {                                              \n"+
229
      "  v_TexCoord = a_TexCoord;                       \n"+
237
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
230 238
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
231 239
      "  }";
232 240

  
src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
161 161
    float h= outBuffer.getHeight();
162 162
    float n= 1.0f - outBuffer.getNear();
163 163

  
164
    float corrW = inBuffer.getWidthCorrection();
165
    float corrH = inBuffer.getHeightCorrection();
166
    float offsetCorrW = corrW/w;
167
    float offsetCorrH = corrH/h;
168

  
164 169
    int radius = (int)(uniforms[index]*mQualityScale);
165 170
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
166 171
    computeGaussianKernel(radius);
......
172 177
    GLES31.glViewport(0, 0, (int)w, (int)h);
173 178

  
174 179
    // horizontal glow
175
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w;
180
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
176 181

  
177 182
    mProgram1.useProgram();
178 183
    outBuffer.bindForOutput(1);
179 184
    inBuffer.setAsInput(0);
180 185

  
181 186
    GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
182
    GLES31.glUniform1i ( mProgram1.mUniform[1] , 0 );
183
    GLES31.glUniform1fv( mProgram1.mUniform[2] , radius+1, mOffsets,0);
184
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, weightsCache,offset);
185
    GLES31.glUniform1i ( mProgram1.mUniform[4] , radius);
186
    GLES31.glUniform4f ( mProgram1.mUniform[5] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
187
    GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
188
    GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
189
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
190
    GLES31.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
191
    GLES31.glUniform1i ( mProgram1.mUniform[5] , radius);
192
    GLES31.glUniform4f ( mProgram1.mUniform[6] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
187 193
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
188 194
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
189 195
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
190 196

  
191 197
    // vertical glow
192
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h;
198
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
193 199

  
194 200
    mProgram2.useProgram();
195 201
    outBuffer.bindForOutput(0);
196 202
    outBuffer.setAsInput(1);
197 203

  
198 204
    GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
199
    GLES31.glUniform1i ( mProgram2.mUniform[1] , 0 );
200
    GLES31.glUniform1fv( mProgram2.mUniform[2] , radius+1, mOffsets,0);
201
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, weightsCache,offset);
202
    GLES31.glUniform1i ( mProgram2.mUniform[4] , radius);
203
    GLES31.glUniform4f ( mProgram1.mUniform[5] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
205
    GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
206
    GLES31.glUniform1i ( mProgram2.mUniform[2] , 0 );
207
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
208
    GLES31.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
209
    GLES31.glUniform1i ( mProgram2.mUniform[5] , radius);
210
    GLES31.glUniform4f ( mProgram1.mUniform[6] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
204 211
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
205 212
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
206 213
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
......
235 242
      "in vec2 a_TexCoord;    \n"+
236 243
      "out vec2 v_TexCoord;   \n"+
237 244
      "uniform float u_Depth; \n"+
245
      "uniform vec2 u_TexCorr;\n"+
238 246

  
239 247
      "void main()                                      \n"+
240 248
      "  {                                              \n"+
241
      "  v_TexCoord = a_TexCoord;                       \n"+
249
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
242 250
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
243 251
      "  }";
244 252

  
src/main/java/org/distorted/library/main/DistortedEffects.java
73 73
  private static int mBlitDepthTextureH;
74 74
  private static int mBlitDepthDepthTextureH;
75 75
  private static int mBlitDepthDepthH;
76
  private static int mBlitDepthTexCorrH;
76 77

  
77 78
  /// NORMAL PROGRAM /////
78 79
  private static DistortedProgram mNormalProgram;
......
162 163
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
163 164
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
164 165
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
166
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
165 167

  
166 168
    // NORMAL PROGRAM //////////////////////////////////////
167 169
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
......
331 333

  
332 334
///////////////////////////////////////////////////////////////////////////////////////////////////
333 335

  
334
  static void blitDepthPriv(DistortedOutputSurface surface)
336
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
335 337
    {
336 338
    mBlitDepthProgram.useProgram();
337 339

  
338 340
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
339 341
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
340 342
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
343
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
341 344
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
342 345
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
343 346
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
331 331
    }
332 332

  
333 333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// The postprocessing buffers mBuffer[] are generally speaking too large (there's just one static
335
// set of them) so before we use them for output, we need to adjust the Vieport as if they were
336
// smaller. That takes care of outputting pixels to them. When we use them as input, we have to
337
// adjust the texture coords - see the get{Width|Height}Correction functions.
334 338

  
335 339
  private static void cloneViewport(DistortedOutputSurface from)
336 340
    {
......
347 351

  
348 352
        surface.mNear   = from.mNear;  // Near plane is independent of the mipmap level
349 353

  
350
        android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
354
        //android.util.Log.e("surface", "viewport "+i+" to ("+from.mWidth+"x"+from.mHeight+")");
351 355

  
352 356
        surface.createProjection();
353 357

  
......
380 384
    GLES31.glDisable(GLES31.GL_STENCIL_TEST);
381 385
    GLES31.glStencilMask(0x00);
382 386

  
383
    DistortedEffects.blitDepthPriv(this);
387
    DistortedEffects.blitDepthPriv(this, buffer.getWidthCorrection(), buffer.getHeightCorrection() );
384 388
    GLES31.glActiveTexture(GLES31.GL_TEXTURE0);
385 389
    GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, 0);
386 390
    GLES31.glActiveTexture(GLES31.GL_TEXTURE1);
......
476 480
    return mChildren;
477 481
    }
478 482

  
483
///////////////////////////////////////////////////////////////////////////////////////////////////
484
/**
485
 * Not part of the Public API.
486
 *
487
 * @y.exclude
488
 */
489
  public float getWidthCorrection()
490
    {
491
    return (float)mWidth/mRealWidth;
492
    }
493

  
494
///////////////////////////////////////////////////////////////////////////////////////////////////
495
/**
496
 * Not part of the Public API.
497
 *
498
 * @y.exclude
499
 */
500
  public float getHeightCorrection()
501
    {
502
    return (float)mHeight/mRealHeight;
503
    }
504

  
479 505
///////////////////////////////////////////////////////////////////////////////////////////////////
480 506
// PUBLIC API
481 507
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/res/raw/blit_depth_vertex_shader.glsl
28 28
#endif
29 29

  
30 30
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
31
uniform vec2  u_TexCorr;      // when we blit from postprocessing buffers, the buffers can be
32
                              // larger than necessary (there is just one static set being
33
                              // reused!) so we need to compensate here by adjusting the texture
34
                              // coords.
31 35

  
32 36
//////////////////////////////////////////////////////////////////////////////////////////////
33 37

  
34 38
void main()
35 39
  {
36
  v_TexCoordinate = a_Position + 0.5;
40
  v_TexCoordinate = (a_Position + 0.5) * u_TexCorr;
37 41
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
38 42
  }

Also available in: Unified diff