Revision f2367b75
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/EffectQueuePostprocess.java | ||
---|---|---|
98 | 98 |
private static float[] weightsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4]; |
99 | 99 |
private static float[] offsetsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4]; |
100 | 100 |
|
101 |
private static DistortedProgram mBlurProgram; |
|
102 |
private static int mRadiusH,mOffsetsH,mWeightsH,mDepthH; |
|
101 |
private static DistortedProgram mBlur1Program, mBlur2Program; |
|
102 |
private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H; |
|
103 |
private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H, mDepthTexture2H; |
|
103 | 104 |
private static float[] mWeights = new float[MAX_BLUR]; |
104 | 105 |
private static float[] mOffsets = new float[MAX_BLUR]; |
105 | 106 |
// another effect .... |
... | ... | |
116 | 117 |
static void createProgram(Resources resources) |
117 | 118 |
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException |
118 | 119 |
{ |
119 |
final InputStream postVertexStream = resources.openRawResource(R.raw.blur_vertex_shader); |
|
120 |
final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader); |
|
121 |
|
|
122 |
mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream, |
|
123 |
"#version 100\n", |
|
124 |
"#version 100\n#define MAX_BLUR "+MAX_BLUR); |
|
125 |
|
|
126 |
int blurProgramH = mBlurProgram.getProgramHandle(); |
|
127 |
mRadiusH = GLES30.glGetUniformLocation( blurProgramH, "u_Radius"); |
|
128 |
mOffsetsH = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets"); |
|
129 |
mWeightsH = GLES30.glGetUniformLocation( blurProgramH, "u_Weights"); |
|
130 |
mDepthH = GLES30.glGetUniformLocation( blurProgramH, "u_Depth"); |
|
120 |
final InputStream blur1VertexStream = resources.openRawResource(R.raw.blur_vertex_shader); |
|
121 |
final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader); |
|
122 |
|
|
123 |
try |
|
124 |
{ |
|
125 |
mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream, |
|
126 |
Distorted.glslVersion, |
|
127 |
Distorted.glslVersion+"#define MAX_BLUR "+MAX_BLUR); |
|
128 |
} |
|
129 |
catch(Exception e) |
|
130 |
{ |
|
131 |
android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage()); |
|
132 |
throw new RuntimeException(e.getMessage()); |
|
133 |
} |
|
134 |
|
|
135 |
int blur1ProgramH = mBlur1Program.getProgramHandle(); |
|
136 |
mRadius1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius"); |
|
137 |
mOffsets1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets"); |
|
138 |
mWeights1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights"); |
|
139 |
mDepth1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth"); |
|
140 |
mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture"); |
|
141 |
|
|
142 |
final InputStream blur2VertexStream = resources.openRawResource(R.raw.blur_vertex_shader); |
|
143 |
final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader); |
|
144 |
|
|
145 |
try |
|
146 |
{ |
|
147 |
mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream, |
|
148 |
Distorted.glslVersion, |
|
149 |
Distorted.glslVersion + "#define MAX_BLUR "+MAX_BLUR); |
|
150 |
} |
|
151 |
catch(Exception e) |
|
152 |
{ |
|
153 |
android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage()); |
|
154 |
throw new RuntimeException(e.getMessage()); |
|
155 |
} |
|
156 |
|
|
157 |
int blur2ProgramH = mBlur2Program.getProgramHandle(); |
|
158 |
mRadius2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius"); |
|
159 |
mOffsets2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets"); |
|
160 |
mWeights2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights"); |
|
161 |
mDepth2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth"); |
|
162 |
mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture"); |
|
163 |
mDepthTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_DepthTexture"); |
|
131 | 164 |
} |
132 | 165 |
|
133 | 166 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
239 | 272 |
|
240 | 273 |
int offset = radius + radius*radius/4; |
241 | 274 |
radius = (radius+1)/2; |
242 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h; |
|
243 | 275 |
|
244 |
mPostBuffer.resizeFast( (int)w, (int)h); |
|
245 |
mPostBuffer.setAsOutput(time); |
|
246 | 276 |
GLES30.glViewport(0, 0, (int)w, (int)h); |
247 | 277 |
|
248 |
mBlurProgram.useProgram(); |
|
249 |
GLES30.glUniform1fv( mWeightsH, radius+1, weightsCache,offset); |
|
250 |
GLES30.glUniform1i( mRadiusH, radius); |
|
251 |
GLES30.glUniform1f( mDepthH , 1.0f-surface.mNear); |
|
252 |
|
|
253 | 278 |
// horizontal blur |
254 |
GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0); |
|
255 |
GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
256 |
GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv); |
|
279 |
mBlur1Program.useProgram(); |
|
280 |
mPostBuffer.resizeFast( (int)w, (int)h); |
|
281 |
mPostBuffer.setAsOutput(time); |
|
282 |
|
|
283 |
GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset); |
|
284 |
GLES30.glUniform1i( mRadius1H, radius); |
|
285 |
GLES30.glUniform1f( mDepth1H , 1.0f-surface.mNear); |
|
286 |
GLES30.glUniform1f( mColorTexture1H , 0 ); |
|
287 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h; |
|
288 |
GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0); |
|
289 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
290 |
GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv); |
|
257 | 291 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
258 | 292 |
|
259 | 293 |
// vertical blur |
294 |
mBlur2Program.useProgram(); |
|
260 | 295 |
mPostBuffer.setAsInput(); |
296 |
mMainBuffer.setAsDepth(); |
|
261 | 297 |
surface.setAsOutput(time); |
298 |
|
|
299 |
GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset); |
|
300 |
GLES30.glUniform1i( mRadius2H, radius); |
|
301 |
GLES30.glUniform1f( mDepth2H , 1.0f-surface.mNear); |
|
302 |
GLES30.glUniform1f( mColorTexture2H , 0 ); |
|
303 |
GLES30.glUniform1f( mDepthTexture2H , 1 ); |
|
262 | 304 |
for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w; |
263 |
GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0); |
|
305 |
GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0); |
|
306 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions); |
|
307 |
GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv); |
|
264 | 308 |
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4); |
265 | 309 |
} |
266 | 310 |
|
Also available in: Unified diff
Upgrade from GLSL 1.00 to GLSL 3.00 ES
Introduce separate BLUR1 and BLUR2 shaders, the second one marging (writing to gl_FragDepth still does not work)