Project

General

Profile

Download (13.9 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effect / PostprocessEffectBlur.java @ c25273e0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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
package org.distorted.library.effect;
21

    
22
import android.opengl.GLES31;
23

    
24
import org.distorted.library.main.Distorted;
25
import org.distorted.library.main.DistortedFramebuffer;
26
import org.distorted.library.main.DistortedOutputSurface;
27
import org.distorted.library.main.DistortedRenderState;
28
import org.distorted.library.program.DistortedProgram;
29
import org.distorted.library.type.Data1D;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32
/**
33
 * Blur the Framebuffer.
34
 */
35
public class PostprocessEffectBlur extends PostprocessEffect
36
  {
37
  private static final int MAX_HALO = 50;
38

    
39
  private Data1D mBlurRadius;
40

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

    
57
  // 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,...
58
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
59
  // in all 4 directions)
60
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
61
  private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
62
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
63
  private static float[] mWeights = new float[MAX_HALO];
64
  private static float[] mOffsets = new float[MAX_HALO];
65

    
66
  private static DistortedProgram mProgram1, mProgram2;
67
  private static int mIndex1, mIndex2;
68

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

    
73
  private void computeGaussianKernel(int radius)
74
    {
75
    int offset = radius + radius*radius/4;
76

    
77
    if( weightsCache[offset]==0.0f )
78
      {
79
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
80
      mWeights[0] = GAUSSIAN[0];
81
      float sum   = GAUSSIAN[0];
82
      int j;
83

    
84
      for(int i=1; i<=radius; i++)
85
        {
86
        x += P;
87
        j = (int)x;
88
        z = x-j;
89

    
90
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
91
        sum += 2*mWeights[i];
92
        }
93

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

    
96
      int numloops = radius/2;
97
      weightsCache[offset] = mWeights[0];
98
      offsetsCache[offset] = 0.0f;
99

    
100
      for(int i=0; i<numloops; i++)
101
        {
102
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
103
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
104
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
105
        }
106

    
107
      if( radius%2 == 1 )
108
        {
109
        int index = offset + radius/2 +1;
110
        offsetsCache[index]=radius;
111
        weightsCache[index]=mWeights[radius];
112
        }
113
      }
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
/**
118
 * Only for use by the library itself.
119
 *
120
 * @y.exclude
121
 */
122
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
123
    {
124
    return mBlurRadius.get(uniforms,index,currentDuration,step);
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
/**
129
 * Only for use by the library itself.
130
 *
131
 * @y.exclude
132
 */
133
  public int getQuality()
134
   {
135
   return mQualityLevel;
136
   }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
/**
140
 * Only for use by the library itself.
141
 *
142
 * @y.exclude
143
 */
144
  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers, int fbo)
145
    {
146
    if( mProgram1 ==null)
147
      {
148
      try
149
        {
150
        mProgram1 = mPrograms.get(mIndex1);
151
        mProgram2 = mPrograms.get(mIndex2);
152
        }
153
      catch(Exception ex)
154
        {
155
        return 0;
156
        }
157
      }
158

    
159
    DistortedRenderState.useStencilMark();
160

    
161
    DistortedFramebuffer buffer = (DistortedFramebuffer)buffers[mQualityLevel];
162

    
163
    buffer.setAsOutputFBO(fbo);
164

    
165
    float w= buffer.getWidth();
166
    float h= buffer.getHeight();
167
    float n= 1.0f - buffer.getNear();
168

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

    
174
    int radius = (int)(uniforms[index]*mQualityScale);
175
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
176
    computeGaussianKernel(radius);
177

    
178
    int offset = radius + radius*radius/4;
179
    radius = (radius+1)/2;
180
    GLES31.glViewport(0, 0, (int)w, (int)h);
181

    
182
    // horizontal blur
183
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
184

    
185
    mProgram1.useProgram();
186
    buffer.bindForOutput(2*fbo+1);
187
    buffer.setAsInput(2*fbo);
188

    
189
    GLES31.glColorMask(true,true,true,true);
190
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
191
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
192

    
193
    GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
194
    GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
195
    GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
196
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
197
    GLES31.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
198
    GLES31.glUniform1i ( mProgram1.mUniform[5] , radius);
199
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
200
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
201
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
202

    
203
    // vertical blur
204
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
205

    
206
    mProgram2.useProgram();
207
    buffer.bindForOutput(2*fbo);
208
    buffer.setAsInput(2*fbo+1);
209

    
210
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
211

    
212
    GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
213
    GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
214
    GLES31.glUniform1i ( mProgram2.mUniform[2] , 0 );
215
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
216
    GLES31.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
217
    GLES31.glUniform1i ( mProgram2.mUniform[5] , radius);
218
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
219
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
220
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
221

    
222
    DistortedRenderState.unuseStencilMark();
223

    
224
    return 2;
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
// PUBLIC API
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
/**
231
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
232
 */
233
  public static void enable()
234
    {
235
    final String blurVertex =
236

    
237
        Distorted.GLSL_VERSION   +
238
      "precision lowp float;  \n"+
239
      "in vec2 a_Position;    \n"+
240
      "in vec2 a_TexCoord;    \n"+
241
      "out vec2 v_TexCoord;   \n"+
242
      "uniform float u_Depth; \n"+
243
      "uniform vec2 u_TexCorr;\n"+
244

    
245
      "void main()                                      \n"+
246
      "  {                                              \n"+
247
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
248
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
249
      "  }";
250

    
251
    final String blurFragment1 =
252

    
253
        Distorted.GLSL_VERSION               +
254
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
255
      "precision lowp float;              \n"+
256
      "in vec2 v_TexCoord;                \n"+
257
      "out vec4 fragColor;                \n"+
258
      "uniform sampler2D u_ColorTexture;  \n"+
259
      "uniform float u_Offsets[MAX_BLUR]; \n"+
260
      "uniform float u_Weights[MAX_BLUR]; \n"+
261
      "uniform int u_Radius;              \n"+
262

    
263
      "void main()                                                                                           \n"+
264
      "  {                                                                                                   \n"+
265
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
266
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
267
      "    {                                                                                                 \n"+
268
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
269
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
270
      "    }                                                                                                 \n"+
271
      "  fragColor = pixel;                                                                                  \n"+
272
      "  }";
273

    
274
    final String blurFragment2 =
275

    
276
        Distorted.GLSL_VERSION               +
277
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
278
      "precision lowp float;              \n"+
279
      "in vec2 v_TexCoord;                \n"+
280
      "out vec4 fragColor;                \n"+
281
      "uniform sampler2D u_ColorTexture;  \n"+
282
      "uniform float u_Offsets[MAX_BLUR]; \n"+
283
      "uniform float u_Weights[MAX_BLUR]; \n"+
284
      "uniform int u_Radius;              \n"+
285

    
286
      "void main()                                                                                           \n"+
287
      "  {                                                                                                   \n"+
288
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
289
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
290
      "    {                                                                                                 \n"+
291
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
292
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
293
      "    }                                                                                                 \n"+
294
      "  fragColor = pixel;                                                                                  \n"+
295
      "  }";
296

    
297
    mIndex1 = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
298
    mIndex2 = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
299
    }
300

    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
/**
303
 * Blur the Framebuffer.
304
 *
305
 * @param blurRadius The 'strength' of the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
306
 *                   take into account 10 pixels in each direction.
307
 */
308
  public PostprocessEffectBlur(Data1D blurRadius)
309
    {
310
    super(EffectName.BLUR);
311
    mBlurRadius = blurRadius;
312
    }
313
  }
(18-18/26)