Project

General

Profile

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

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

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.GLES30;
23

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class PostprocessEffectBlur extends PostprocessEffect
32
  {
33
  private static final int MAX_HALO = 50;    // Support effects creating up to MAX_HALO pixels wide 'halo' around the object.
34

    
35
  private Data1D mBlurRadius;
36

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

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

    
62
  private static DistortedProgram mBlur1Program, mBlur2Program;
63
  private static int mBlur1Index, mBlur2Index;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
/**
67
 * Blur the object.
68
 *
69
 * @param blurRadius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
70
 *                   take into account 10 pixels in each direction.
71
 */
72
  public PostprocessEffectBlur(Data1D blurRadius)
73
    {
74
    super(EffectName.BLUR);
75
    mBlurRadius = blurRadius;
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
81
    {
82
    return mBlurRadius.get(uniforms,index,currentDuration,step);
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer)
88
    {
89
    if( mBlur1Program==null)
90
      {
91
      mBlur1Program = mPrograms.get(mBlur1Index);
92
      mBlur2Program = mPrograms.get(mBlur2Index);
93
      }
94

    
95
    buffer.setAsOutput();
96

    
97
    float w1  = buffer.getWidth();
98
    float h1  = buffer.getHeight();
99
    float near= 1.0f - buffer.getNear();
100

    
101
    int radius = (int)(uniforms[index]*qualityScale);
102
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
103
    computeGaussianKernel(radius);
104

    
105
    int offset = radius + radius*radius/4;
106
    radius = (radius+1)/2;
107

    
108
    // horizontal blur
109
    GLES30.glViewport(0, 0, (int)w1, (int)h1);
110
    mBlur1Program.useProgram();
111
    buffer.bindForOutput(1);
112
    buffer.setAsInput(0);
113

    
114
    GLES30.glUniform1fv( mBlur1Program.mUniform[3], radius+1, weightsCache,offset);
115
    GLES30.glUniform1i( mBlur1Program.mUniform[4], radius);
116
    GLES30.glUniform1f( mBlur1Program.mUniform[0] , near);
117
    GLES30.glUniform1i( mBlur1Program.mUniform[1] , 0 );
118
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
119
    GLES30.glUniform1fv( mBlur1Program.mUniform[2] ,radius+1, mOffsets,0);
120
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
121
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
122

    
123
    DistortedRenderState.useStencilMark();
124
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
125
    DistortedRenderState.unuseStencilMark();
126
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
127
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
128

    
129
    // vertical blur
130
    mBlur2Program.useProgram();
131
    buffer.bindForOutput(0);
132
    buffer.setAsInput(1);
133

    
134
    GLES30.glColorMask(true,true,true,true);
135
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
136
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
137

    
138
    GLES30.glUniform1fv( mBlur2Program.mUniform[3], radius+1, weightsCache,offset);
139
    GLES30.glUniform1i( mBlur2Program.mUniform[4], radius);
140
    GLES30.glUniform1f( mBlur2Program.mUniform[0] , near);
141
    GLES30.glUniform1i( mBlur2Program.mUniform[1] , 0 );
142
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
143
    GLES30.glUniform1fv( mBlur2Program.mUniform[2] ,radius+1, mOffsets,0);
144
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
145
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
146

    
147
    DistortedRenderState.useStencilMark();
148
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
149
    DistortedRenderState.unuseStencilMark();
150
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
151
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
152

    
153
    return 2;
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public static void enable()
159
    {
160
    final String blurVertex =
161

    
162
      "#version 300 es        \n"+
163
      "precision lowp float;  \n"+
164
      "in vec2 a_Position;    \n"+
165
      "in vec2 a_TexCoord;    \n"+
166
      "out vec2 v_TexCoord;   \n"+
167
      "uniform float u_Depth; \n"+
168

    
169
      "void main()                                      \n"+
170
      "  {                                              \n"+
171
      "  v_TexCoord = a_TexCoord;                       \n"+
172
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
173
      "}";
174

    
175
    final String blurFragment1 =
176

    
177
      "#version 300 es                    \n"+
178
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
179
      "precision lowp float;              \n"+
180
      "in vec2 v_TexCoord;                \n"+
181
      "out vec4 fragColor;                \n"+
182
      "uniform sampler2D u_ColorTexture;  \n"+
183
      "uniform float u_Offsets[MAX_BLUR]; \n"+
184
      "uniform float u_Weights[MAX_BLUR]; \n"+
185
      "uniform int u_Radius;              \n"+
186

    
187
      "void main()                                                                                           \n"+
188
      "  {                                                                                                   \n"+
189
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
190
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
191
      "    {                                                                                                 \n"+
192
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
193
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
194
      "    }                                                                                                 \n"+
195
      "fragColor = pixel;                                                                                    \n"+
196
      "}";
197

    
198
    final String blurFragment2 =
199

    
200
      "#version 300 es                    \n"+
201
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
202
      "precision lowp float;              \n"+
203
      "in vec2 v_TexCoord;                \n"+
204
      "out vec4 fragColor;                \n"+
205
      "uniform sampler2D u_ColorTexture;  \n"+
206
      "uniform float u_Offsets[MAX_BLUR]; \n"+
207
      "uniform float u_Weights[MAX_BLUR]; \n"+
208
      "uniform int u_Radius;              \n"+
209

    
210
      "void main()                                                                                           \n"+
211
      "  {                                                                                                   \n"+
212
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
213
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
214
      "    {                                                                                                 \n"+
215
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
216
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
217
      "    }                                                                                                 \n"+
218
      "fragColor = pixel;                                                                                    \n"+
219
      "}";
220

    
221
    mBlur1Index = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
222
    mBlur2Index = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
223
    }
224

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

    
229
  private void computeGaussianKernel(int radius)
230
    {
231
    int offset = radius + radius*radius/4;
232

    
233
    if( weightsCache[offset]==0.0f )
234
      {
235
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
236
      mWeights[0] = GAUSSIAN[0];
237
      float sum   = GAUSSIAN[0];
238
      int j;
239

    
240
      for(int i=1; i<=radius; i++)
241
        {
242
        x += P;
243
        j = (int)x;
244
        z = x-j;
245

    
246
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
247
        sum += 2*mWeights[i];
248
        }
249

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

    
252
      int numloops = radius/2;
253
      weightsCache[offset] = mWeights[0];
254
      offsetsCache[offset] = 0.0f;
255

    
256
      for(int i=0; i<numloops; i++)
257
        {
258
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
259
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
260
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
261
        }
262

    
263
      if( radius%2 == 1 )
264
        {
265
        int index = offset + radius/2 +1;
266
        offsetsCache[index]=radius;
267
        weightsCache[index]=mWeights[radius];
268
        }
269
      }
270
    }
271
  }
(17-17/25)