1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.effect;
|
22
|
|
23
|
import android.opengl.GLES30;
|
24
|
|
25
|
import org.distorted.library.main.DistortedFramebuffer;
|
26
|
import org.distorted.library.main.InternalRenderState;
|
27
|
import org.distorted.library.program.DistortedProgram;
|
28
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
/**
|
32
|
* Postprocessed effect in which postprocessing is a Gaussian blur - i.e. Blur or Glow.
|
33
|
*/
|
34
|
abstract public class PostprocessEffectBlurred extends PostprocessEffect
|
35
|
{
|
36
|
private static final int MAX_RADIUS = 50;
|
37
|
|
38
|
private static final float[] GAUSSIAN = // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
|
39
|
{ // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
|
40
|
0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
|
41
|
0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
|
42
|
0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
|
43
|
0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
|
44
|
0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
|
45
|
0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
|
46
|
0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
|
47
|
0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
|
48
|
0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
|
49
|
0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
|
50
|
0.004432f, 0.000000f
|
51
|
};
|
52
|
private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
|
53
|
|
54
|
// 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,...
|
55
|
// i.e. k(i)=floor((i+3)/2). (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
|
56
|
// in all 4 directions)
|
57
|
// We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
|
58
|
private static final float[] weightsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
|
59
|
private static final float[] offsetsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
|
60
|
private static final float[] mWeights = new float[MAX_RADIUS];
|
61
|
private static final float[] mOffsets = new float[MAX_RADIUS];
|
62
|
|
63
|
private static DistortedProgram mProgram1, mProgram2;
|
64
|
private static int mIndex1, mIndex2;
|
65
|
|
66
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
67
|
|
68
|
static void destroyStatics()
|
69
|
{
|
70
|
mProgram1 = null;
|
71
|
mProgram2 = null;
|
72
|
|
73
|
mIndex1 = 0;
|
74
|
mIndex2 = 0;
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
// This implements the 'Better separable implementation using GPU fixed function sampling' from
|
79
|
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
|
80
|
|
81
|
private void computeGaussianKernel(int radius)
|
82
|
{
|
83
|
int offset = radius + radius*radius/4;
|
84
|
|
85
|
if( weightsCache[offset]==0.0f )
|
86
|
{
|
87
|
float z, x= 0.0f, P= (float)NUM_GAUSSIAN / Math.max(radius,3);
|
88
|
mWeights[0] = GAUSSIAN[0];
|
89
|
float sum = GAUSSIAN[0];
|
90
|
int j;
|
91
|
|
92
|
for(int i=1; i<=radius; i++)
|
93
|
{
|
94
|
x += P;
|
95
|
j = (int)x;
|
96
|
z = x-j;
|
97
|
|
98
|
mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
|
99
|
sum += 2*mWeights[i];
|
100
|
}
|
101
|
|
102
|
for(int i=0; i<=radius; i++) mWeights[i] /= sum;
|
103
|
|
104
|
int numloops = radius/2;
|
105
|
weightsCache[offset] = mWeights[0];
|
106
|
offsetsCache[offset] = 0.0f;
|
107
|
|
108
|
for(int i=0; i<numloops; i++)
|
109
|
{
|
110
|
offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
|
111
|
weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
|
112
|
offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
|
113
|
}
|
114
|
|
115
|
if( radius%2 == 1 )
|
116
|
{
|
117
|
int index = offset + radius/2 +1;
|
118
|
offsetsCache[index]=radius;
|
119
|
weightsCache[index]=mWeights[radius];
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
|
124
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
125
|
/**
|
126
|
* Only for use by the library itself.
|
127
|
*
|
128
|
* @y.exclude
|
129
|
*/
|
130
|
public int postprocess(float[] uniforms, int index, DistortedFramebuffer buffer)
|
131
|
{
|
132
|
if( mProgram1 ==null)
|
133
|
{
|
134
|
try
|
135
|
{
|
136
|
mProgram1 = mPrograms.get(mIndex1);
|
137
|
mProgram2 = mPrograms.get(mIndex2);
|
138
|
}
|
139
|
catch(Exception ex)
|
140
|
{
|
141
|
return 0;
|
142
|
}
|
143
|
}
|
144
|
|
145
|
InternalRenderState.useStencilMark();
|
146
|
|
147
|
buffer.setAsOutput();
|
148
|
|
149
|
float w= buffer.getWidth();
|
150
|
float h= buffer.getHeight();
|
151
|
float n= 1.0f - buffer.getNear();
|
152
|
|
153
|
float corrW = buffer.getWidthCorrection();
|
154
|
float corrH = buffer.getHeightCorrection();
|
155
|
float offsetCorrW = corrW/w;
|
156
|
float offsetCorrH = corrH/h;
|
157
|
|
158
|
int radius = (int)(uniforms[index+1]*mQualityScale);
|
159
|
if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
|
160
|
if( radius<=0 ) radius = 1;
|
161
|
computeGaussianKernel(radius);
|
162
|
|
163
|
int offset = radius + radius*radius/4;
|
164
|
radius = (radius+1)/2;
|
165
|
GLES30.glViewport(0, 0, (int)w, (int)h);
|
166
|
|
167
|
// horizontal blur
|
168
|
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
|
169
|
|
170
|
mProgram1.useProgram();
|
171
|
buffer.bindForOutput(1);
|
172
|
buffer.setAsInput(0);
|
173
|
|
174
|
GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
|
175
|
GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
|
176
|
|
177
|
GLES30.glColorMask(true,true,true,true);
|
178
|
GLES30.glClearColor(1.0f,1.0f,1.0f,0.0f);
|
179
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
|
180
|
|
181
|
GLES30.glUniform1f ( mProgram1.mUniform[0] , n );
|
182
|
GLES30.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
|
183
|
GLES30.glUniform1i ( mProgram1.mUniform[2] , 0 );
|
184
|
GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
|
185
|
GLES30.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
|
186
|
GLES30.glUniform1i ( mProgram1.mUniform[5] , radius);
|
187
|
GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
188
|
GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
189
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
190
|
mProgram1.stopUsingProgram();
|
191
|
|
192
|
// vertical blur
|
193
|
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
|
194
|
|
195
|
mProgram2.useProgram();
|
196
|
buffer.bindForOutput(0);
|
197
|
buffer.setAsInput(1);
|
198
|
|
199
|
GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
|
200
|
GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
|
201
|
|
202
|
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
|
203
|
|
204
|
GLES30.glUniform1f ( mProgram2.mUniform[0] , n );
|
205
|
GLES30.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
|
206
|
GLES30.glUniform1i ( mProgram2.mUniform[2] , 0 );
|
207
|
GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
|
208
|
GLES30.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
|
209
|
GLES30.glUniform1i ( mProgram2.mUniform[5] , radius);
|
210
|
GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
|
211
|
GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
|
212
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
|
213
|
mProgram1.stopUsingProgram();
|
214
|
|
215
|
InternalRenderState.unuseStencilMark();
|
216
|
|
217
|
return 2;
|
218
|
}
|
219
|
|
220
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221
|
// PUBLIC API
|
222
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
223
|
/**
|
224
|
* Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
|
225
|
*/
|
226
|
public static void enable(String prog1, String prog2)
|
227
|
{
|
228
|
final String vertex =
|
229
|
|
230
|
"precision lowp float; \n"+
|
231
|
"in vec2 a_Position; \n"+
|
232
|
"in vec2 a_TexCoord; \n"+
|
233
|
"out vec2 v_TexCoord; \n"+
|
234
|
"uniform float u_Depth; \n"+
|
235
|
"uniform vec2 u_TexCorr;\n"+
|
236
|
|
237
|
"void main() \n"+
|
238
|
" { \n"+
|
239
|
" v_TexCoord = a_TexCoord * u_TexCorr; \n"+
|
240
|
" gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
|
241
|
" }";
|
242
|
|
243
|
final String fragment1 =
|
244
|
|
245
|
"#define MAX_BLUR "+MAX_RADIUS+ "\n"+
|
246
|
"precision lowp float; \n"+
|
247
|
"in vec2 v_TexCoord; \n"+
|
248
|
"out vec4 fragColor; \n"+
|
249
|
"uniform sampler2D u_ColorTexture; \n"+
|
250
|
"uniform float u_Offsets[MAX_BLUR]; \n"+
|
251
|
"uniform float u_Weights[MAX_BLUR]; \n"+
|
252
|
"uniform int u_Radius; \n"+
|
253
|
|
254
|
"void main() \n"+
|
255
|
" { \n"+
|
256
|
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+
|
257
|
" for (int i=1; i<=u_Radius; i+=1) \n"+
|
258
|
" { \n"+
|
259
|
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) + \n"+
|
260
|
" texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
|
261
|
" } \n"+
|
262
|
" fragColor = pixel; \n"+
|
263
|
" }";
|
264
|
|
265
|
final String fragment2 =
|
266
|
|
267
|
"#define MAX_BLUR "+MAX_RADIUS+ "\n"+
|
268
|
"precision lowp float; \n"+
|
269
|
"in vec2 v_TexCoord; \n"+
|
270
|
"out vec4 fragColor; \n"+
|
271
|
"uniform sampler2D u_ColorTexture; \n"+
|
272
|
"uniform float u_Offsets[MAX_BLUR]; \n"+
|
273
|
"uniform float u_Weights[MAX_BLUR]; \n"+
|
274
|
"uniform int u_Radius; \n"+
|
275
|
|
276
|
"void main() \n"+
|
277
|
" { \n"+
|
278
|
" vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0]; \n"+
|
279
|
" for (int i=1; i<=u_Radius; i+=1) \n"+
|
280
|
" { \n"+
|
281
|
" pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) + \n"+
|
282
|
" texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
|
283
|
" } \n"+
|
284
|
" fragColor = pixel; \n"+
|
285
|
" }";
|
286
|
|
287
|
mIndex1 = PostprocessEffect.register(prog1, vertex,fragment1);
|
288
|
mIndex2 = PostprocessEffect.register(prog2, vertex,fragment2);
|
289
|
}
|
290
|
|
291
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
292
|
/**
|
293
|
* Default constructor.
|
294
|
*/
|
295
|
public PostprocessEffectBlurred(EffectName name)
|
296
|
{
|
297
|
super(name);
|
298
|
}
|
299
|
}
|