Revision faa3ff56
Added by Leszek Koltunski almost 7 years ago
src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java | ||
---|---|---|
26 | 26 |
import org.distorted.library.type.Data1D; |
27 | 27 |
|
28 | 28 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
29 |
|
|
29 |
/** |
|
30 |
* Blur the Framebuffer. |
|
31 |
*/ |
|
30 | 32 |
public class PostprocessEffectBlur extends PostprocessEffect |
31 | 33 |
{ |
32 | 34 |
private static final int MAX_HALO = 50; |
... | ... | |
62 | 64 |
private static int mIndex1, mIndex2; |
63 | 65 |
|
64 | 66 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
65 |
/** |
|
66 |
* Blur the object. |
|
67 |
* |
|
68 |
* @param blurRadius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel, |
|
69 |
* take into account 10 pixels in each direction. |
|
70 |
*/ |
|
71 |
public PostprocessEffectBlur(Data1D blurRadius) |
|
67 |
// This implements the 'Better separable implementation using GPU fixed function sampling' from |
|
68 |
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms |
|
69 |
|
|
70 |
private void computeGaussianKernel(int radius) |
|
72 | 71 |
{ |
73 |
super(EffectName.BLUR); |
|
74 |
mBlurRadius = blurRadius; |
|
72 |
int offset = radius + radius*radius/4; |
|
73 |
|
|
74 |
if( weightsCache[offset]==0.0f ) |
|
75 |
{ |
|
76 |
float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3); |
|
77 |
mWeights[0] = GAUSSIAN[0]; |
|
78 |
float sum = GAUSSIAN[0]; |
|
79 |
int j; |
|
80 |
|
|
81 |
for(int i=1; i<=radius; i++) |
|
82 |
{ |
|
83 |
x += P; |
|
84 |
j = (int)x; |
|
85 |
z = x-j; |
|
86 |
|
|
87 |
mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1]; |
|
88 |
sum += 2*mWeights[i]; |
|
89 |
} |
|
90 |
|
|
91 |
for(int i=0; i<=radius; i++) mWeights[i] /= sum; |
|
92 |
|
|
93 |
int numloops = radius/2; |
|
94 |
weightsCache[offset] = mWeights[0]; |
|
95 |
offsetsCache[offset] = 0.0f; |
|
96 |
|
|
97 |
for(int i=0; i<numloops; i++) |
|
98 |
{ |
|
99 |
offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2); |
|
100 |
weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2]; |
|
101 |
offsetsCache[offset+i+1]/= weightsCache[offset+i+1]; |
|
102 |
} |
|
103 |
|
|
104 |
if( radius%2 == 1 ) |
|
105 |
{ |
|
106 |
int index = offset + radius/2 +1; |
|
107 |
offsetsCache[index]=radius; |
|
108 |
weightsCache[index]=mWeights[radius]; |
|
109 |
} |
|
110 |
} |
|
75 | 111 |
} |
76 | 112 |
|
77 | 113 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
78 |
|
|
114 |
/** |
|
115 |
* Only for use by the library itself. |
|
116 |
* |
|
117 |
* @y.exclude |
|
118 |
*/ |
|
79 | 119 |
public boolean compute(float[] uniforms, int index, long currentDuration, long step ) |
80 | 120 |
{ |
81 | 121 |
return mBlurRadius.get(uniforms,index,currentDuration,step); |
82 | 122 |
} |
83 | 123 |
|
84 | 124 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
85 |
|
|
125 |
/** |
|
126 |
* Only for use by the library itself. |
|
127 |
* |
|
128 |
* @y.exclude |
|
129 |
*/ |
|
86 | 130 |
public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer) |
87 | 131 |
{ |
88 | 132 |
if( mProgram1 ==null) |
... | ... | |
145 | 189 |
} |
146 | 190 |
|
147 | 191 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
148 |
|
|
192 |
// PUBLIC API |
|
193 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
194 |
/** |
|
195 |
* Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work. |
|
196 |
*/ |
|
149 | 197 |
public static void enable() |
150 | 198 |
{ |
151 | 199 |
final String blurVertex = |
... | ... | |
214 | 262 |
} |
215 | 263 |
|
216 | 264 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
217 |
// This implements the 'Better separable implementation using GPU fixed function sampling' from |
|
218 |
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms |
|
219 |
|
|
220 |
private void computeGaussianKernel(int radius) |
|
265 |
/** |
|
266 |
* Blur the Framebuffer. |
|
267 |
* |
|
268 |
* @param blurRadius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel, |
|
269 |
* take into account 10 pixels in each direction. |
|
270 |
*/ |
|
271 |
public PostprocessEffectBlur(Data1D blurRadius) |
|
221 | 272 |
{ |
222 |
int offset = radius + radius*radius/4; |
|
223 |
|
|
224 |
if( weightsCache[offset]==0.0f ) |
|
225 |
{ |
|
226 |
float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3); |
|
227 |
mWeights[0] = GAUSSIAN[0]; |
|
228 |
float sum = GAUSSIAN[0]; |
|
229 |
int j; |
|
230 |
|
|
231 |
for(int i=1; i<=radius; i++) |
|
232 |
{ |
|
233 |
x += P; |
|
234 |
j = (int)x; |
|
235 |
z = x-j; |
|
236 |
|
|
237 |
mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1]; |
|
238 |
sum += 2*mWeights[i]; |
|
239 |
} |
|
240 |
|
|
241 |
for(int i=0; i<=radius; i++) mWeights[i] /= sum; |
|
242 |
|
|
243 |
int numloops = radius/2; |
|
244 |
weightsCache[offset] = mWeights[0]; |
|
245 |
offsetsCache[offset] = 0.0f; |
|
246 |
|
|
247 |
for(int i=0; i<numloops; i++) |
|
248 |
{ |
|
249 |
offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2); |
|
250 |
weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2]; |
|
251 |
offsetsCache[offset+i+1]/= weightsCache[offset+i+1]; |
|
252 |
} |
|
253 |
|
|
254 |
if( radius%2 == 1 ) |
|
255 |
{ |
|
256 |
int index = offset + radius/2 +1; |
|
257 |
offsetsCache[index]=radius; |
|
258 |
weightsCache[index]=mWeights[radius]; |
|
259 |
} |
|
260 |
} |
|
273 |
super(EffectName.BLUR); |
|
274 |
mBlurRadius = blurRadius; |
|
261 | 275 |
} |
262 | 276 |
} |
Also available in: Unified diff
Javadoc.