Project

General

Profile

« Previous | Next » 

Revision 7170e4eb

Added by Leszek Koltunski almost 7 years ago

Move on to Stencil on both stages of the BLUR + separate 'blit with depth'.

This works wonders on Nexus 5X ( r=50 quality=HIGHIEST 1,4,8 cubes: 59.3,43.6,41.0 FPS! ) but it somehow fails to clear the DEPTH and STENCIL buffers on Nexus 4....

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
78 78
    mQuadPositions.put(positionData).position(0);
79 79
    }
80 80

  
81
  /// BLIT DEPTH PROGRAM ///
82
  private static DistortedProgram mBlitDepthProgram;
83
  private static int mBlitDepthTextureH;
84
  private static int mBlitDepthDepthTextureH;
85
  private static int mBlitDepthDepthH;
86

  
81 87
  /// NORMAL PROGRAM /////
82 88
  private static DistortedProgram mNormalProgram;
83 89
  private static int mNormalMVPMatrixH;
......
166 172
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
167 173
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
168 174

  
175
    // BLIT DEPTH PROGRAM ////////////////////////////////////
176
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
177
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
178

  
179
    try
180
      {
181
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
182
      }
183
    catch(Exception e)
184
      {
185
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
186
      throw new RuntimeException(e.getMessage());
187
      }
188

  
189
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
190
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
191
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
192
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
193

  
169 194
    // NORMAL PROGRAM //////////////////////////////////////
170 195
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
171 196
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
......
297 322
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
298 323
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
299 324
    }
300
    
325

  
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

  
328
  static void blitDepthPriv(DistortedOutputSurface surface)
329
    {
330
    mBlitDepthProgram.useProgram();
331

  
332
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
333
    GLES30.glUniform1i(mBlitDepthTextureH, 0);
334
    GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
335
    GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
336
    GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
337
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
338
    }
339

  
301 340
///////////////////////////////////////////////////////////////////////////////////////////////////
302 341
   
303 342
  private void releasePriv()
src/main/java/org/distorted/library/DistortedNode.java
271 271
      float w = mSurface.getWidth() /2.0f;
272 272
      float h = mSurface.getHeight()/2.0f;
273 273

  
274
      // Actually draw our object.
274
      // Draw the color buffer of the object.
275 275
      buffer1.setAsOutput();
276 276
      mState.apply();
277 277
      mEffects.drawPriv(w, h, mMesh, buffer1, currTime, 0);
278 278

  
279
      // Mark area of our object + marginInPixels pixels around with 1s in Stencil buffer
279
      // Draw stencil + depth buffers of the object enlarged by HALO pixels around.
280 280
      buffer2.setAsOutput();
281 281
      DistortedRenderState.setUpStencilMark();
282 282
      mEffects.drawPriv(w, h, mMesh, buffer2, currTime, effects.getHalo()*buffer2.mMipmap);
src/main/java/org/distorted/library/EffectQueuePostprocess.java
94 94

  
95 95
  private static DistortedProgram mBlur1Program, mBlur2Program;
96 96
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
97
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H, mDepthTexture2H;
97
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H;
98 98
  private static float[] mWeights = new float[MAX_BLUR];
99 99
  private static float[] mOffsets = new float[MAX_BLUR];
100 100
  // another effect ....
......
161 161
    mWeights2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights");
162 162
    mDepth2H        = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth");
163 163
    mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture");
164
    mDepthTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_DepthTexture");
165 164
    }
166 165

  
167 166
///////////////////////////////////////////////////////////////////////////////////////////////////
......
307 306
      DistortedRenderState.unuseStencilMark();
308 307

  
309 308
      // vertical blur
310
      GLES30.glViewport(0, 0, (int)w2, (int)h2);
311 309
      mBlur2Program.useProgram();
312 310
      buffer2.setAsInput();
313
      buffer2.setAsDepth();
314
      surface.setAsOutput(time);
311
      buffer1.setAsOutput();
312

  
313
      GLES30.glStencilMask(0xff);
314
      GLES30.glDepthMask(true);
315
      GLES30.glColorMask(true,true,true,true);
316
      GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
317
      GLES30.glClearDepthf(1.0f);
318
      GLES30.glClearStencil(0);
319
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
315 320

  
316 321
      GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
317 322
      GLES30.glUniform1i( mRadius2H, radius);
318 323
      GLES30.glUniform1f( mDepth2H , 1.0f-surface.mNear);
319 324
      GLES30.glUniform1i( mColorTexture2H , 0 );
320
      GLES30.glUniform1i( mDepthTexture2H , 1 );
321 325
      for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
322 326
      GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
323 327
      GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
324 328
      GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
329

  
330
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, buffer2.mDepthStencilH[0], 0);
331
      DistortedRenderState.useStencilMark();
325 332
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
333
      DistortedRenderState.unuseStencilMark();
334
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, buffer1.mDepthStencilH[0], 0);
335

  
336
      // blit the results with DEPTH to surface.
337
      GLES30.glViewport(0, 0, (int)w2, (int)h2);
338
      buffer1.setAsInput();
339
      buffer2.setAsDepth();
340
      surface.setAsOutput(time);
341
      DistortedEffects.blitDepthPriv(surface);
326 342

  
327 343
      // after each postprocess, clear buffers
328 344
      GLES30.glStencilMask(0xff);
......
331 347
      GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
332 348
      GLES30.glClearDepthf(1.0f);
333 349
      GLES30.glClearStencil(0);
334

  
335 350
      buffer1.setAsOutput();
336 351
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
337 352
      buffer2.setAsOutput();
338
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT);
353
      GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT|GLES30.GL_DEPTH_BUFFER_BIT|GLES30.GL_STENCIL_BUFFER_BIT);
339 354
      }
340 355

  
341 356
    return mNumEffects;
src/main/res/raw/blit_depth_fragment_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
out vec4 fragColor;           // The output color
24
in vec2 v_TexCoordinate;      // Interpolated texture coordinate per fragment.
25
#define TEXTURE texture
26
#define FRAG_COLOR fragColor
27
#else
28
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
29
#define TEXTURE texture2D
30
#define FRAG_COLOR gl_FragColor
31
#endif
32

  
33
uniform sampler2D u_Texture;
34
uniform sampler2D u_DepthTexture;
35

  
36
//////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
void main()                    		
39
  {
40
  gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r;
41
  FRAG_COLOR   = TEXTURE(u_Texture     ,v_TexCoordinate);
42
  }
src/main/res/raw/blit_depth_vertex_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        // 
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
precision lowp float;
21

  
22
#if __VERSION__ != 100
23
in vec2 a_Position;           // Per-vertex position.
24
out vec2 v_TexCoordinate;     //
25
#else
26
attribute vec2 a_Position;    // Per-vertex position.
27
varying vec2 v_TexCoordinate; //
28
#endif
29

  
30
uniform float u_Depth;        // distance from the near plane to render plane, in clip coords
31

  
32
//////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
void main()
35
  {
36
  v_TexCoordinate = a_Position + 0.5;
37
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
38
  }
src/main/res/raw/blur2_fragment_shader.glsl
31 31
#endif
32 32

  
33 33
uniform sampler2D u_ColorTexture;
34
uniform sampler2D u_DepthTexture;
35 34
uniform float u_Offsets[MAX_BLUR];
36 35
uniform float u_Weights[MAX_BLUR];
37 36
uniform int u_Radius;
......
40 39

  
41 40
void main()
42 41
  {
43
  gl_FragDepth = TEXTURE(u_DepthTexture,v_TexCoordinate).r;
44

  
45 42
  vec4 pixel= TEXTURE(u_ColorTexture,v_TexCoordinate) * u_Weights[0];
46 43

  
47 44
  for (int i=1; i<=u_Radius; i+=1)

Also available in: Unified diff