Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / PostprocessEffectBlur.java @ 5ec42229

1 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 125cee3d Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 125cee3d Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 125cee3d Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.effect;
21
22 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
23 1dfc9074 leszek
24
import org.distorted.library.main.DistortedFramebuffer;
25 7602a827 Leszek Koltunski
import org.distorted.library.main.InternalRenderState;
26 1dfc9074 leszek
import org.distorted.library.program.DistortedProgram;
27 bb4755e2 Leszek Koltunski
import org.distorted.library.type.Data2D;
28 125cee3d Leszek Koltunski
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30 faa3ff56 Leszek Koltunski
/**
31
 * Blur the Framebuffer.
32
 */
33 125cee3d Leszek Koltunski
public class PostprocessEffectBlur extends PostprocessEffect
34
  {
35 bb4755e2 Leszek Koltunski
  private static final int MAX_RADIUS = 50;
36 1dfc9074 leszek
37 5ec42229 Leszek Koltunski
  private final Data2D mBlurHaloAndRadius;
38 0dd98279 Leszek Koltunski
39 b7074bc6 Leszek Koltunski
  private static final float[] GAUSSIAN =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
40 1dfc9074 leszek
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
41
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
42
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
43
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
44
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
45
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
46
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
47
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
48
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
49
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
50
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
51
    0.004432f, 0.000000f
52
    };
53
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
54
55
  // 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,...
56
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
57
  // in all 4 directions)
58
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
59 5ec42229 Leszek Koltunski
  private static final float[] weightsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
60
  private static final float[] offsetsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
61
  private static final float[] mWeights = new float[MAX_RADIUS];
62
  private static final float[] mOffsets = new float[MAX_RADIUS];
63 1dfc9074 leszek
64 1149be8f leszek
  private static DistortedProgram mProgram1, mProgram2;
65
  private static int mIndex1, mIndex2;
66 aa2f0486 Leszek Koltunski
67 2f1f7570 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
// Clean up of static variables on exit.
69
// called by reflection from super class.
70
71
  @SuppressWarnings("unused")
72
  static void destroyStatics()
73
    {
74
    mProgram1 = null;
75
    mProgram2 = null;
76
77
    mIndex1 = 0;
78
    mIndex2 = 0;
79
    }
80
81 125cee3d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
82 faa3ff56 Leszek Koltunski
// This implements the 'Better separable implementation using GPU fixed function sampling' from
83
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
84
85
  private void computeGaussianKernel(int radius)
86 125cee3d Leszek Koltunski
    {
87 faa3ff56 Leszek Koltunski
    int offset = radius + radius*radius/4;
88
89
    if( weightsCache[offset]==0.0f )
90
      {
91
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
92
      mWeights[0] = GAUSSIAN[0];
93
      float sum   = GAUSSIAN[0];
94
      int j;
95
96
      for(int i=1; i<=radius; i++)
97
        {
98
        x += P;
99
        j = (int)x;
100
        z = x-j;
101
102
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
103
        sum += 2*mWeights[i];
104
        }
105
106
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
107
108
      int numloops = radius/2;
109
      weightsCache[offset] = mWeights[0];
110
      offsetsCache[offset] = 0.0f;
111
112
      for(int i=0; i<numloops; i++)
113
        {
114
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
115
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
116
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
117
        }
118
119
      if( radius%2 == 1 )
120
        {
121
        int index = offset + radius/2 +1;
122
        offsetsCache[index]=radius;
123
        weightsCache[index]=mWeights[radius];
124
        }
125
      }
126 125cee3d Leszek Koltunski
    }
127 15aa7d94 Leszek Koltunski
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129 faa3ff56 Leszek Koltunski
/**
130
 * Only for use by the library itself.
131
 *
132
 * @y.exclude
133
 */
134 15aa7d94 Leszek Koltunski
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
135
    {
136 bb4755e2 Leszek Koltunski
    return mBlurHaloAndRadius.get(uniforms,index,currentDuration,step);
137 15aa7d94 Leszek Koltunski
    }
138 1dfc9074 leszek
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140 143095f7 Leszek Koltunski
/**
141
 * Only for use by the library itself.
142
 *
143
 * @y.exclude
144
 */
145 9e771d06 Leszek Koltunski
  public boolean getRender()
146
    {
147
    return false;
148
    }
149 86d322b5 Leszek Koltunski
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * Only for use by the library itself.
153
 *
154
 * @y.exclude
155
 */
156 9e771d06 Leszek Koltunski
  public int apply(float[] uniforms, int index, DistortedFramebuffer buffer)
157 1dfc9074 leszek
    {
158 1149be8f leszek
    if( mProgram1 ==null)
159 aa2f0486 Leszek Koltunski
      {
160 040cd18c Leszek Koltunski
      try
161
        {
162
        mProgram1 = mPrograms.get(mIndex1);
163
        mProgram2 = mPrograms.get(mIndex2);
164
        }
165
      catch(Exception ex)
166
        {
167
        return 0;
168
        }
169 aa2f0486 Leszek Koltunski
      }
170
171 7602a827 Leszek Koltunski
    InternalRenderState.useStencilMark();
172 7266d8ef Leszek Koltunski
173 5f7e4f2c Leszek Koltunski
    buffer.setAsOutput();
174 1dfc9074 leszek
175 33b0fad7 Leszek Koltunski
    float w= buffer.getWidth();
176
    float h= buffer.getHeight();
177
    float n= 1.0f - buffer.getNear();
178 1dfc9074 leszek
179 ae2802b1 Leszek Koltunski
    float corrW = buffer.getWidthCorrection();
180
    float corrH = buffer.getHeightCorrection();
181
    float offsetCorrW = corrW/w;
182
    float offsetCorrH = corrH/h;
183
184 bb4755e2 Leszek Koltunski
    int radius = (int)(uniforms[index+1]*mQualityScale);
185
    if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
186 0273ef2a Leszek Koltunski
    if( radius<=0          ) radius = 1;
187 1dfc9074 leszek
    computeGaussianKernel(radius);
188
189
    int offset = radius + radius*radius/4;
190
    radius = (radius+1)/2;
191 b7074bc6 Leszek Koltunski
    GLES30.glViewport(0, 0, (int)w, (int)h);
192 1dfc9074 leszek
193
    // horizontal blur
194 ae2802b1 Leszek Koltunski
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
195 1149be8f leszek
196
    mProgram1.useProgram();
197 5f7e4f2c Leszek Koltunski
    buffer.bindForOutput(1);
198
    buffer.setAsInput(0);
199 1dfc9074 leszek
200 c1fa9e3c Leszek Koltunski
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
201
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
202
203 b7074bc6 Leszek Koltunski
    GLES30.glColorMask(true,true,true,true);
204
    GLES30.glClearColor(1.0f,1.0f,1.0f,0.0f);
205
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
206 9455da17 Leszek Koltunski
207 b7074bc6 Leszek Koltunski
    GLES30.glUniform1f ( mProgram1.mUniform[0] , n );
208
    GLES30.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
209
    GLES30.glUniform1i ( mProgram1.mUniform[2] , 0 );
210
    GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
211
    GLES30.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
212
    GLES30.glUniform1i ( mProgram1.mUniform[5] , radius);
213
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
214
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
215
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
216 1dfc9074 leszek
217
    // vertical blur
218 ae2802b1 Leszek Koltunski
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
219 1149be8f leszek
220
    mProgram2.useProgram();
221 5f7e4f2c Leszek Koltunski
    buffer.bindForOutput(0);
222
    buffer.setAsInput(1);
223 1dfc9074 leszek
224 c1fa9e3c Leszek Koltunski
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
225
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
226
227 b7074bc6 Leszek Koltunski
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
228 e6519ac8 Leszek Koltunski
229 b7074bc6 Leszek Koltunski
    GLES30.glUniform1f ( mProgram2.mUniform[0] , n );
230
    GLES30.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
231
    GLES30.glUniform1i ( mProgram2.mUniform[2] , 0 );
232
    GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
233
    GLES30.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
234
    GLES30.glUniform1i ( mProgram2.mUniform[5] , radius);
235
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
236
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
237
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
238 1dfc9074 leszek
239 7602a827 Leszek Koltunski
    InternalRenderState.unuseStencilMark();
240 7266d8ef Leszek Koltunski
241 bed13bea leszek
    return 2;
242 1dfc9074 leszek
    }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245 faa3ff56 Leszek Koltunski
// PUBLIC API
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247
/**
248 7602a827 Leszek Koltunski
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
249 faa3ff56 Leszek Koltunski
 */
250 041b6dee Leszek Koltunski
  public static void enable()
251
    {
252
    final String blurVertex =
253
254
      "precision lowp float;  \n"+
255
      "in vec2 a_Position;    \n"+
256
      "in vec2 a_TexCoord;    \n"+
257
      "out vec2 v_TexCoord;   \n"+
258
      "uniform float u_Depth; \n"+
259 ae2802b1 Leszek Koltunski
      "uniform vec2 u_TexCorr;\n"+
260 041b6dee Leszek Koltunski
261
      "void main()                                      \n"+
262
      "  {                                              \n"+
263 ae2802b1 Leszek Koltunski
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
264 041b6dee Leszek Koltunski
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
265 edd8d7a7 Leszek Koltunski
      "  }";
266 041b6dee Leszek Koltunski
267
    final String blurFragment1 =
268
269 bb4755e2 Leszek Koltunski
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
270 041b6dee Leszek Koltunski
      "precision lowp float;              \n"+
271
      "in vec2 v_TexCoord;                \n"+
272
      "out vec4 fragColor;                \n"+
273
      "uniform sampler2D u_ColorTexture;  \n"+
274
      "uniform float u_Offsets[MAX_BLUR]; \n"+
275
      "uniform float u_Weights[MAX_BLUR]; \n"+
276
      "uniform int u_Radius;              \n"+
277
278
      "void main()                                                                                           \n"+
279
      "  {                                                                                                   \n"+
280
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
281
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
282
      "    {                                                                                                 \n"+
283
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
284
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
285
      "    }                                                                                                 \n"+
286 edd8d7a7 Leszek Koltunski
      "  fragColor = pixel;                                                                                  \n"+
287
      "  }";
288 041b6dee Leszek Koltunski
289
    final String blurFragment2 =
290
291 bb4755e2 Leszek Koltunski
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
292 041b6dee Leszek Koltunski
      "precision lowp float;              \n"+
293
      "in vec2 v_TexCoord;                \n"+
294
      "out vec4 fragColor;                \n"+
295
      "uniform sampler2D u_ColorTexture;  \n"+
296
      "uniform float u_Offsets[MAX_BLUR]; \n"+
297
      "uniform float u_Weights[MAX_BLUR]; \n"+
298
      "uniform int u_Radius;              \n"+
299
300
      "void main()                                                                                           \n"+
301
      "  {                                                                                                   \n"+
302
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
303
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
304
      "    {                                                                                                 \n"+
305
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
306
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
307
      "    }                                                                                                 \n"+
308 edd8d7a7 Leszek Koltunski
      "  fragColor = pixel;                                                                                  \n"+
309
      "  }";
310 1dfc9074 leszek
311 1149be8f leszek
    mIndex1 = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
312
    mIndex2 = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
313 1dfc9074 leszek
    }
314
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316 faa3ff56 Leszek Koltunski
/**
317
 * Blur the Framebuffer.
318
 *
319 bb4755e2 Leszek Koltunski
 * @param blurHaloAndRadius First float: the halo.
320
 *                          How far beyond the object does the effect stretch to? Unit: Percentage
321
 *                          of the size of the original object, i.e. Halo=0 --> no halo around, this
322
 *                          would mean sharp edges around the object; Halo=100 --> halo of the size
323
 *                          of the object itself around (in case of blur, this would be - in vast
324
 *                          majority of cases except an object rendered very closely to the near plane-
325
 *                          an overkill).
326
 *                          Second float: the radius.
327
 *                          The 'strength' if the blur of the edges, in pixels. 0 = no blur, 10 =
328
 *                          blur of roughly 10 pixels around the whole halo.
329
330 faa3ff56 Leszek Koltunski
 */
331 bb4755e2 Leszek Koltunski
  public PostprocessEffectBlur(Data2D blurHaloAndRadius)
332 1dfc9074 leszek
    {
333 faa3ff56 Leszek Koltunski
    super(EffectName.BLUR);
334 bb4755e2 Leszek Koltunski
    mBlurHaloAndRadius = blurHaloAndRadius;
335 1dfc9074 leszek
    }
336 125cee3d Leszek Koltunski
  }