Project

General

Profile

« Previous | Next » 

Revision 041b6dee

Added by Leszek Koltunski almost 7 years ago

Simplification in PostprocessEffects

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import android.content.res.Resources;
23 22
import android.opengl.GLES30;
24 23

  
25
import org.distorted.library.R;
26
import org.distorted.library.main.Distorted;
27 24
import org.distorted.library.main.DistortedFramebuffer;
28 25
import org.distorted.library.main.DistortedRenderState;
29 26
import org.distorted.library.program.DistortedProgram;
30
import org.distorted.library.program.FragmentCompilationException;
31
import org.distorted.library.program.FragmentUniformsException;
32
import org.distorted.library.program.LinkingException;
33
import org.distorted.library.program.VertexCompilationException;
34
import org.distorted.library.program.VertexUniformsException;
35 27
import org.distorted.library.type.Data1D;
36 28

  
37
import java.io.InputStream;
38

  
39 29
///////////////////////////////////////////////////////////////////////////////////////////////////
40 30

  
41 31
public class PostprocessEffectBlur extends PostprocessEffect
......
68 58
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
69 59

  
70 60
  private static DistortedProgram mBlur1Program, mBlur2Program;
71
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
72
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H;
73 61
  private static float[] mWeights = new float[MAX_HALO];
74 62
  private static float[] mOffsets = new float[MAX_HALO];
75 63

  
......
86 74
    mBlurRadius = blurRadius;
87 75
    }
88 76

  
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
  public static void enable()
92
    {
93

  
94
    }
95

  
96 77
///////////////////////////////////////////////////////////////////////////////////////////////////
97 78

  
98 79
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
......
123 104
    buffer.bindForOutput(1);
124 105
    buffer.setAsInput(0);
125 106

  
126
    GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
127
    GLES30.glUniform1i( mRadius1H, radius);
128
    GLES30.glUniform1f( mDepth1H , near);
129
    GLES30.glUniform1i( mColorTexture1H , 0 );
107
    GLES30.glUniform1fv( mBlur1Program.mUniform[3], radius+1, weightsCache,offset);
108
    GLES30.glUniform1i( mBlur1Program.mUniform[4], radius);
109
    GLES30.glUniform1f( mBlur1Program.mUniform[0] , near);
110
    GLES30.glUniform1i( mBlur1Program.mUniform[1] , 0 );
130 111
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
131
    GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0);
112
    GLES30.glUniform1fv( mBlur1Program.mUniform[2] ,radius+1, mOffsets,0);
132 113
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
133 114
    GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
134 115

  
......
147 128
    GLES30.glClearColor(0.0f,0.0f,0.0f,0.0f);
148 129
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
149 130

  
150
    GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
151
    GLES30.glUniform1i( mRadius2H, radius);
152
    GLES30.glUniform1f( mDepth2H , near);
153
    GLES30.glUniform1i( mColorTexture2H , 0 );
131
    GLES30.glUniform1fv( mBlur2Program.mUniform[3], radius+1, weightsCache,offset);
132
    GLES30.glUniform1i( mBlur2Program.mUniform[4], radius);
133
    GLES30.glUniform1f( mBlur2Program.mUniform[0] , near);
134
    GLES30.glUniform1i( mBlur2Program.mUniform[1] , 0 );
154 135
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
155
    GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
136
    GLES30.glUniform1fv( mBlur2Program.mUniform[2] ,radius+1, mOffsets,0);
156 137
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
157 138
    GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
158 139

  
......
167 148

  
168 149
///////////////////////////////////////////////////////////////////////////////////////////////////
169 150

  
170
  public static void createProgram(Resources resources)
171
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
151
  public static void enable()
172 152
    {
173
    int gl             = Distorted.getGlVersion();
174
    int GLSL           = (gl==3 ? 300:100);
175
    String GLSL_VERSION= (gl==3 ? "#version 300 es\n" : "#version 100\n");
153
    }
154

  
155
///////////////////////////////////////////////////////////////////////////////////////////////////
176 156

  
177
    final InputStream blur1VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
178
    final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader);
157
  public static void createProgram()
158
    {
159
    final String blurVertex =
160

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

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

  
174
    final String blurFragment1 =
175

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

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

  
197
    final String blurFragment2 =
198

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

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

  
180 220
    try
181 221
      {
182
      mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
183
                                          GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL);
222
      mBlur1Program = new DistortedProgram(blurVertex,blurFragment1);
184 223
      }
185 224
    catch(Exception e)
186 225
      {
187
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage());
226
      android.util.Log.e("Effects", "exception trying to compile BLUR1 program: "+e.getMessage());
188 227
      throw new RuntimeException(e.getMessage());
189 228
      }
190 229

  
191
    int blur1ProgramH = mBlur1Program.getProgramHandle();
192
    mRadius1H       = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius");
193
    mOffsets1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets");
194
    mWeights1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights");
195
    mDepth1H        = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth");
196
    mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture");
197

  
198
    final InputStream blur2VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
199
    final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader);
200

  
201 230
    try
202 231
      {
203
      mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
204
                                          GLSL_VERSION, GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, GLSL);
232
      mBlur2Program = new DistortedProgram(blurVertex,blurFragment2);
205 233
      }
206 234
    catch(Exception e)
207 235
      {
208
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
236
      android.util.Log.e("Effects", "exception trying to compile BLUR2 program: "+e.getMessage());
209 237
      throw new RuntimeException(e.getMessage());
210 238
      }
211

  
212
    int blur2ProgramH = mBlur2Program.getProgramHandle();
213
    mRadius2H       = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius");
214
    mOffsets2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets");
215
    mWeights2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights");
216
    mDepth2H        = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth");
217
    mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture");
218 239
    }
219 240

  
220 241
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff