Project

General

Profile

« Previous | Next » 

Revision 1dfc9074

Added by Leszek Koltunski over 7 years ago

All knowledge about Postporcessing moved to the respective Effect classes.

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffect.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import org.distorted.library.main.DistortedFramebuffer;
23

  
24
import java.nio.ByteBuffer;
25
import java.nio.ByteOrder;
26
import java.nio.FloatBuffer;
27

  
22 28
///////////////////////////////////////////////////////////////////////////////////////////////////
23 29
// POSTPROCESSING EFFECTS.
24 30

  
25

  
26 31
public abstract class PostprocessEffect extends Effect
27 32
  {
28 33
  public static final int NUM_UNIFORMS = 5; // 5 per-effect interpolated values.
29 34

  
35
  static final int POS_DATA_SIZE= 2; // Blur Program: size of the position data in elements
36
  static final int TEX_DATA_SIZE= 2; // Blur Program: size of the texture coordinate data in elements.
37

  
38
  static final FloatBuffer mQuadPositions, mQuadTexture, mQuadTextureInv;
39

  
40
  static
41
    {
42
    int dataLength      = 4;
43
    int bytes_per_float = 4;
44

  
45
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
46
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
47
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
48

  
49
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
50
    mQuadPositions.put(position).position(0);
51
    mQuadTexture= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
52
    mQuadTexture.put(textureNor).position(0);
53
    mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
54
    mQuadTextureInv.put(textureInv).position(0);
55
    }
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
  public abstract int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer);
60

  
30 61
///////////////////////////////////////////////////////////////////////////////////////////////////
31 62

  
32 63
  PostprocessEffect(EffectName name)
src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
24

  
25
import org.distorted.library.R;
26
import org.distorted.library.main.Distorted;
27
import org.distorted.library.main.DistortedFramebuffer;
28
import org.distorted.library.main.DistortedRenderState;
29
import org.distorted.library.program.DistortedProgram;
30
import org.distorted.library.program.FragmentCompilationException;
31
import org.distorted.library.program.FragmentUniformsException;
32
import org.distorted.library.program.LinkingException;
33
import org.distorted.library.program.VertexCompilationException;
34
import org.distorted.library.program.VertexUniformsException;
22 35
import org.distorted.library.type.Data1D;
23 36

  
37
import java.io.InputStream;
38

  
24 39
///////////////////////////////////////////////////////////////////////////////////////////////////
25 40

  
26 41
public class PostprocessEffectBlur extends PostprocessEffect
27 42
  {
43
  private static final int MAX_HALO = 50;    // Support effects creating up to MAX_HALO pixels wide 'halo' around the object.
44

  
28 45
  private Data1D mBlurRadius;
29 46

  
47
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
48
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
49
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
50
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
51
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
52
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
53
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
54
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
55
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
56
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
57
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
58
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
59
    0.004432f, 0.000000f
60
    };
61
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
62

  
63
  // The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,...
64
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
65
  // in all 4 directions)
66
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
67
  private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
68
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
69

  
70
  private static DistortedProgram mBlur1Program, mBlur2Program;
71
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
72
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H;
73
  private static float[] mWeights = new float[MAX_HALO];
74
  private static float[] mOffsets = new float[MAX_HALO];
75

  
30 76
///////////////////////////////////////////////////////////////////////////////////////////////////
31 77
/**
32 78
 * Blur the object.
......
46 92
    {
47 93
    return mBlurRadius.get(uniforms,index,currentDuration,step);
48 94
    }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

  
98
  public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer)
99
    {
100
    buffer.setAsOutput();
101

  
102
    float w1  = buffer.getWidth();
103
    float h1  = buffer.getHeight();
104
    float near= 1.0f - buffer.getNear();
105

  
106
    int radius = (int)(uniforms[index]*qualityScale);
107
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
108
    computeGaussianKernel(radius);
109

  
110
    int offset = radius + radius*radius/4;
111
    radius = (radius+1)/2;
112

  
113
    // horizontal blur
114
    GLES30.glViewport(0, 0, (int)w1, (int)h1);
115
    mBlur1Program.useProgram();
116
    buffer.bindForOutput(1);
117
    buffer.setAsInput(0);
118

  
119
    GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
120
    GLES30.glUniform1i( mRadius1H, radius);
121
    GLES30.glUniform1f( mDepth1H , near);
122
    GLES30.glUniform1i( mColorTexture1H , 0 );
123
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
124
    GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0);
125
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
126
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
127

  
128
    DistortedRenderState.useStencilMark();
129
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
130
    DistortedRenderState.unuseStencilMark();
131
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
132
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
133

  
134
    // vertical blur
135
    mBlur2Program.useProgram();
136
    buffer.bindForOutput(0);
137
    buffer.setAsInput(1);
138

  
139
    GLES30.glColorMask(true,true,true,true);
140
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
141
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
142

  
143
    GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
144
    GLES30.glUniform1i( mRadius2H, radius);
145
    GLES30.glUniform1f( mDepth2H , near);
146
    GLES30.glUniform1i( mColorTexture2H , 0 );
147
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
148
    GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
149
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
150
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
151

  
152
    DistortedRenderState.useStencilMark();
153
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
154
    DistortedRenderState.unuseStencilMark();
155
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
156
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
157

  
158
    return 2;
159
    }
160

  
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

  
163
  public static void createProgram(Resources resources)
164
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
165
    {
166
    int gl             = Distorted.getGlVersion();
167
    int GLSL           = (gl==3 ? 300:100);
168
    String GLSL_VERSION= (gl==3 ? "#version 300 es\n" : "#version 100\n");
169

  
170
    final InputStream blur1VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
171
    final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader);
172

  
173
    try
174
      {
175
      mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
176
                                          GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL);
177
      }
178
    catch(Exception e)
179
      {
180
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage());
181
      throw new RuntimeException(e.getMessage());
182
      }
183

  
184
    int blur1ProgramH = mBlur1Program.getProgramHandle();
185
    mRadius1H       = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius");
186
    mOffsets1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets");
187
    mWeights1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights");
188
    mDepth1H        = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth");
189
    mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture");
190

  
191
    final InputStream blur2VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
192
    final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader);
193

  
194
    try
195
      {
196
      mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
197
                                          GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL);
198
      }
199
    catch(Exception e)
200
      {
201
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
202
      throw new RuntimeException(e.getMessage());
203
      }
204

  
205
    int blur2ProgramH = mBlur2Program.getProgramHandle();
206
    mRadius2H       = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius");
207
    mOffsets2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets");
208
    mWeights2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights");
209
    mDepth2H        = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth");
210
    mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture");
211
    }
212

  
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// This implements the 'Better separable implementation using GPU fixed function sampling' from
215
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
216

  
217
  private void computeGaussianKernel(int radius)
218
    {
219
    int offset = radius + radius*radius/4;
220

  
221
    if( weightsCache[offset]==0.0f )
222
      {
223
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
224
      mWeights[0] = GAUSSIAN[0];
225
      float sum   = GAUSSIAN[0];
226
      int j;
227

  
228
      for(int i=1; i<=radius; i++)
229
        {
230
        x += P;
231
        j = (int)x;
232
        z = x-j;
233

  
234
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
235
        sum += 2*mWeights[i];
236
        }
237

  
238
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
239

  
240
      int numloops = radius/2;
241
      weightsCache[offset] = mWeights[0];
242
      offsetsCache[offset] = 0.0f;
243

  
244
      for(int i=0; i<numloops; i++)
245
        {
246
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
247
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
248
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
249
        }
250

  
251
      if( radius%2 == 1 )
252
        {
253
        int index = offset + radius/2 +1;
254
        offsetsCache[index]=radius;
255
        weightsCache[index]=mWeights[radius];
256
        }
257
      }
258
    }
49 259
  }
src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import org.distorted.library.main.DistortedFramebuffer;
22 23
import org.distorted.library.type.Data1D;
23 24
import org.distorted.library.type.Data4D;
24 25

  
......
51 52
    mColor.get(uniforms,index+1,currentDuration,step);
52 53
    return mGlowRadius.get(uniforms,index,currentDuration,step);
53 54
    }
55

  
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
  public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer)
60
    {
61
    return 0;
62
    }
54 63
  }
src/main/java/org/distorted/library/main/Distorted.java
25 25
import android.content.res.Resources;
26 26

  
27 27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.PostprocessEffectBlur;
28 29
import org.distorted.library.program.*;
29 30

  
30 31
///////////////////////////////////////////////////////////////////////////////////////////////////
......
88 89
///////////////////////////////////////////////////////////////////////////////////////////////////
89 90
/**
90 91
 * Have we called onCreate yet, ie have we initialized the library?
91
 * @return <code>true</code> if the library is initilized and ready for action.
92
 * @return <code>true</code> if the library is initialized and ready for action.
92 93
 */
93 94
  public static boolean isInitialized()
94 95
    {
......
121 122

  
122 123
    final Resources resources = context.getResources();
123 124
    DistortedEffects.createProgram(resources);
124
    EffectQueuePostprocess.createProgram(resources);
125
    PostprocessEffectBlur.createProgram(resources);
125 126
    EffectMessageSender.startSending();
126 127

  
127 128
    mInitialized = true;
src/main/java/org/distorted/library/main/DistortedFramebuffer.java
225 225
    return false;
226 226
    }
227 227

  
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
231
 *
232
 * @return <code>true</code> if successful.
233
 */
234
  public boolean setAsInput(int texture)
235
    {
236
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
237
      {
238
      GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
239
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[texture]);
240
      return true;
241
      }
242

  
243
    return false;
244
    }
245

  
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

  
248
  public void bindForOutput(int texture)
249
    {
250
    if( texture>=0 && texture<mNumColors && mColorH[texture]>0 )
251
      {
252
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[texture], 0);
253
      }
254
    }
255

  
228 256
///////////////////////////////////////////////////////////////////////////////////////////////////
229 257
/**
230 258
 * Enable.disable DEPTH and STENCIL buffers.
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
181 181
      {
182 182
      mBuffer[j] = new DistortedFramebuffer(2,BOTH_DEPTH_STENCIL,TYPE_SYST, (int)(mWidth*mipmap), (int)(mHeight*mipmap) );
183 183
      mBuffer[j].mMipmap = mipmap;
184
      mBuffer[j].mNear   = mNear;  // copy mNear as well (for blitting- see PostprocessEffectBlur.apply() )
184 185
      mipmap *= EffectQuality.MULTIPLIER;
185 186
      }
186 187

  
......
322 323
    return mChildren;
323 324
    }
324 325

  
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

  
327
  void setAsOutput()
328
    {
329
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
330
    }
331

  
332 326
///////////////////////////////////////////////////////////////////////////////////////////////////
333 327
// PUBLIC API
334 328
///////////////////////////////////////////////////////////////////////////////////////////////////
......
431 425
      }
432 426
    }
433 427

  
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429
/**
430
 * Bind this Surface as a Framebuffer we can render to.
431
 * <p>
432
 * This version does not attempt to clear anything.
433
 */
434

  
435
  public void setAsOutput()
436
    {
437
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
438
    }
439

  
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
/**
442
 * Return the Near plane of the Projection included in the Surface.
443
 *
444
 * @return the Near plane.
445
 */
446
  public float getNear()
447
    {
448
    return mNear;
449
    }
450

  
434 451
///////////////////////////////////////////////////////////////////////////////////////////////////
435 452
/**
436 453
 * Set mipmap level.
......
542 559
      mNear=0.99f;
543 560
      }
544 561

  
562
    if( mBuffer[0]!=null )
563
      {
564
      for(int j=0; j<EffectQuality.LENGTH; j++) mBuffer[j].mNear = mNear;
565
      }
566

  
545 567
    createProjection();
546 568
    }
547 569

  
src/main/java/org/distorted/library/main/DistortedRenderState.java
259 259

  
260 260
///////////////////////////////////////////////////////////////////////////////////////////////////
261 261

  
262
  static void useStencilMark()
262
  public static void useStencilMark()
263 263
    {
264 264
    if( cState.stencilTest!=1 )
265 265
      {
......
299 299

  
300 300
///////////////////////////////////////////////////////////////////////////////////////////////////
301 301

  
302
  static void unuseStencilMark()
302
  public static void unuseStencilMark()
303 303
    {
304 304
    if( sState.stencilTest!=cState.stencilTest )
305 305
      {
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
19 19

  
20 20
package org.distorted.library.main;
21 21

  
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
24

  
25
import org.distorted.library.effect.EffectName;
26 22
import org.distorted.library.effect.EffectType;
27 23
import org.distorted.library.effect.PostprocessEffect;
28
import org.distorted.library.R;
29 24
import org.distorted.library.message.EffectMessage;
30
import org.distorted.library.program.DistortedProgram;
31
import org.distorted.library.program.FragmentCompilationException;
32
import org.distorted.library.program.FragmentUniformsException;
33
import org.distorted.library.program.LinkingException;
34
import org.distorted.library.program.VertexCompilationException;
35
import org.distorted.library.program.VertexUniformsException;
36

  
37
import java.io.InputStream;
38
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41 25

  
42 26
///////////////////////////////////////////////////////////////////////////////////////////////////
43 27

  
44 28
class EffectQueuePostprocess extends EffectQueue
45 29
  {
46
  private static final int MAX_HALO = 50;    // Support effects creating up to MAX_HALO pixels wide 'halo' around the object.
47

  
48
  private static final int POS_DATA_SIZE= 2; // Post Program: size of the position data in elements
49
  private static final int TEX_DATA_SIZE= 2; // Post Program: size of the texture coordinate data in elements.
50

  
51 30
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
52 31
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
53 32

  
54
  private static final FloatBuffer mQuadPositions, mQuadTexture, mQuadTextureInv;
55

  
56
  static
57
    {
58
    int dataLength      = 4;
59
    int bytes_per_float = 4;
60

  
61
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
62
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
63
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
64

  
65
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
66
    mQuadPositions.put(position).position(0);
67
    mQuadTexture= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadTexture.put(textureNor).position(0);
69
    mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
    mQuadTextureInv.put(textureInv).position(0);
71
    }
72

  
73 33
  int mQualityLevel;
74 34
  float mQualityScale;
75 35
  private int mHalo;
76 36

  
77
  /////////////////////////////////////////////////////////////////////////////////
78
  // BLUR effect
79
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
80
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
81
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
82
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
83
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
84
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
85
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
86
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
87
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
88
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
89
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
90
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
91
    0.004432f, 0.000000f
92
    };
93
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
94

  
95
  // The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,...
96
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
97
  // in all 4 directions)
98
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
99
  private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
100
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
101

  
102
  private static DistortedProgram mBlur1Program, mBlur2Program;
103
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
104
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H;
105
  private static float[] mWeights = new float[MAX_HALO];
106
  private static float[] mOffsets = new float[MAX_HALO];
107
  /////////////////////////////////////////////////////////////////////////////////
108
  // GLOW effect
109

  
110

  
111 37
///////////////////////////////////////////////////////////////////////////////////////////////////
112 38

  
113 39
  EffectQueuePostprocess(long id)
......
120 46

  
121 47
///////////////////////////////////////////////////////////////////////////////////////////////////
122 48

  
123
  static void createProgram(Resources resources)
124
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
125
    {
126
    final InputStream blur1VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
127
    final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader);
128

  
129
    try
130
      {
131
      mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
132
                                          Distorted.GLSL_VERSION,
133
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, Distorted.GLSL);
134
      }
135
    catch(Exception e)
136
      {
137
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage());
138
      throw new RuntimeException(e.getMessage());
139
      }
140

  
141
    int blur1ProgramH = mBlur1Program.getProgramHandle();
142
    mRadius1H       = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius");
143
    mOffsets1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets");
144
    mWeights1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights");
145
    mDepth1H        = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth");
146
    mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture");
147

  
148
    final InputStream blur2VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
149
    final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader);
150

  
151
    try
152
      {
153
      mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
154
                                          Distorted.GLSL_VERSION,
155
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, Distorted.GLSL);
156
      }
157
    catch(Exception e)
158
      {
159
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
160
      // run anyway as compiling Blur2 WILL fail on OpenGL 2.0 contexts
161
      mBlur2Program = mBlur1Program;
162
      }
163

  
164
    int blur2ProgramH = mBlur2Program.getProgramHandle();
165
    mRadius2H       = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius");
166
    mOffsets2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets");
167
    mWeights2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights");
168
    mDepth2H        = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth");
169
    mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture");
170
    }
171

  
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

  
174
  private synchronized void compute(long currTime)
49
  private void compute(long currTime)
175 50
    {
176 51
    if( currTime==mTime ) return;
177 52
    if( mTime==0 ) mTime = currTime;
......
204 79
    mTime = currTime;
205 80
    }
206 81

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// This implements the 'Better separable implementation using GPU fixed function sampling' from
209
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
210

  
211
  private void computeGaussianKernel(int radius)
212
    {
213
    int offset = radius + radius*radius/4;
214

  
215
    if( weightsCache[offset]==0.0f )
216
      {
217
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
218
      mWeights[0] = GAUSSIAN[0];
219
      float sum   = GAUSSIAN[0];
220
      int j;
221

  
222
      for(int i=1; i<=radius; i++)
223
        {
224
        x += P;
225
        j = (int)x;
226
        z = x-j;
227

  
228
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
229
        sum += 2*mWeights[i];
230
        }
231

  
232
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
233

  
234
      int numloops = radius/2;
235
      weightsCache[offset] = mWeights[0];
236
      offsetsCache[offset] = 0.0f;
237

  
238
      for(int i=0; i<numloops; i++)
239
        {
240
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
241
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
242
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
243
        }
244

  
245
      if( radius%2 == 1 )
246
        {
247
        int index = offset + radius/2 +1;
248
        offsetsCache[index]=radius;
249
        weightsCache[index]=mWeights[radius];
250
        }
251
      }
252
    }
253

  
254 82
///////////////////////////////////////////////////////////////////////////////////////////////////
255 83

  
256 84
  int getHalo()
......
270 98

  
271 99
      for(int i=0; i<mNumEffects; i++)
272 100
        {
273
        if (mName[i] == EffectName.BLUR.ordinal() )
274
          {
275
          blur(NUM_UNIFORMS*i,surface);
276
          numRenders += 2;
277
          }
278
        else if (mName[i] == EffectName.GLOW.ordinal() )
279
          {
280
          glow(NUM_UNIFORMS*i,surface);
281
          numRenders += 2;
282
          }
101
        numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i,mQualityScale,surface.mBuffer[mQualityLevel]);
283 102
        }
284 103
      }
285 104

  
286 105
    return numRenders;
287 106
    }
288

  
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

  
291
  private void blur(int index, DistortedOutputSurface surface)
292
    {
293
    DistortedFramebuffer buffer = surface.mBuffer[mQualityLevel];
294
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, buffer.mFBOH[0]);
295

  
296
    float w1 = buffer.mWidth;
297
    float h1 = buffer.mHeight;
298

  
299
    int radius = (int)(mUniforms[index]*mQualityScale);
300
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
301
    computeGaussianKernel(radius);
302

  
303
    int offset = radius + radius*radius/4;
304
    radius = (radius+1)/2;
305

  
306
    // horizontal blur
307
    GLES30.glViewport(0, 0, (int)w1, (int)h1);
308
    mBlur1Program.useProgram();
309
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[1], 0);
310
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
311
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[0]);
312

  
313
    GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
314
    GLES30.glUniform1i( mRadius1H, radius);
315
    GLES30.glUniform1f( mDepth1H , 1.0f-surface.mNear);
316
    GLES30.glUniform1i( mColorTexture1H , 0 );
317
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
318
    GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0);
319
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
320
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
321

  
322
    DistortedRenderState.useStencilMark();
323
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
324
    DistortedRenderState.unuseStencilMark();
325
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
326
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
327

  
328
    // vertical blur
329
    mBlur2Program.useProgram();
330
    GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, buffer.mColorH[0], 0);
331
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
332
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, buffer.mColorH[1]);
333

  
334
    GLES30.glColorMask(true,true,true,true);
335
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
336
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
337

  
338
    GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
339
    GLES30.glUniform1i( mRadius2H, radius);
340
    GLES30.glUniform1f( mDepth2H , 1.0f-surface.mNear);
341
    GLES30.glUniform1i( mColorTexture2H , 0 );
342
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
343
    GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
344
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
345
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
346

  
347
    DistortedRenderState.useStencilMark();
348
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
349
    DistortedRenderState.unuseStencilMark();
350
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
351
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
352
    }
353

  
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355

  
356
  private void glow(int index, DistortedOutputSurface surface)
357
    {
358

  
359
    }
360 107
  }

Also available in: Unified diff