Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 8426bd6a

1 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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;
21
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 194ab46f Leszek Koltunski
import android.opengl.GLES30;
24 4c1dd6e9 Leszek Koltunski
25
import org.distorted.library.message.EffectMessage;
26 8fa96e69 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
27 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
28
import org.distorted.library.program.FragmentUniformsException;
29
import org.distorted.library.program.LinkingException;
30
import org.distorted.library.program.VertexCompilationException;
31
import org.distorted.library.program.VertexUniformsException;
32 4c1dd6e9 Leszek Koltunski
import org.distorted.library.type.Data1D;
33
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Static1D;
35
36 c90b9e01 Leszek Koltunski
import java.io.InputStream;
37 8fa96e69 Leszek Koltunski
import java.nio.ByteBuffer;
38
import java.nio.ByteOrder;
39
import java.nio.FloatBuffer;
40
41 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43
class EffectQueuePostprocess extends EffectQueue
44
  {
45 9d5bb851 Leszek Koltunski
  private static final int POS_DATA_SIZE= 2; // Post Program: size of the position data in elements
46
  private static final int TEX_DATA_SIZE= 2; // Post Program: size of the texture coordinate data in elements.
47 8fa96e69 Leszek Koltunski
48 667a1ad9 Leszek Koltunski
  private static final int NUM_UNIFORMS = 4;
49 4c1dd6e9 Leszek Koltunski
  private static final int NUM_CACHE    = 0;
50
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
51
52 ed5bf324 Leszek Koltunski
  private static final FloatBuffer mQuadPositions, mQuadTexture;
53 8fa96e69 Leszek Koltunski
54
  static
55
    {
56 667a1ad9 Leszek Koltunski
    int dataLength      = 4;
57
    int bytes_per_float = 4;
58 8fa96e69 Leszek Koltunski
59 1c67457f Leszek Koltunski
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
60
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
61 8fa96e69 Leszek Koltunski
62 9d5bb851 Leszek Koltunski
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
63 1c67457f Leszek Koltunski
    mQuadPositions.put(position).position(0);
64 ed5bf324 Leszek Koltunski
    mQuadTexture= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
65
    mQuadTexture.put(textureNor).position(0);
66 8fa96e69 Leszek Koltunski
    }
67 4c1dd6e9 Leszek Koltunski
68 0d81d0fb Leszek Koltunski
  // BLUR effect
69 ed841982 Leszek Koltunski
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
70
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
71
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
72
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
73
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
74
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
75
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
76
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
77
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
78
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
79
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
80
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
81
    0.004432f, 0.000000f
82
    };
83
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
84
85
  // Support blurs consisting of the present pixel and up to MAX_BLUR pixels in each direction
86
  private static final int MAX_BLUR = 50;
87
88 464e53fa Leszek Koltunski
  // 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,...
89 ed841982 Leszek Koltunski
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
90
  // in all 4 directions)
91
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
92
  private static float[] weightsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4];
93
  private static float[] offsetsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4];
94
95 f2367b75 Leszek Koltunski
  private static DistortedProgram mBlur1Program, mBlur2Program;
96 b7dba709 Leszek Koltunski
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
97
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H, mDepthTexture2H;
98 ed841982 Leszek Koltunski
  private static float[] mWeights = new float[MAX_BLUR];
99
  private static float[] mOffsets = new float[MAX_BLUR];
100 0d81d0fb Leszek Koltunski
  // another effect ....
101
102 8426bd6a Leszek Koltunski
  int mQuality;
103
104 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  EffectQueuePostprocess(long id)
107
    { 
108
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
109 8426bd6a Leszek Koltunski
110
    mQuality = 0;
111 4c1dd6e9 Leszek Koltunski
    }
112
113 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
  static void createProgram(Resources resources)
116
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
117
    {
118 f2367b75 Leszek Koltunski
    final InputStream blur1VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
119
    final InputStream blur1FragmentStream = resources.openRawResource(R.raw.blur1_fragment_shader);
120
121
    try
122
      {
123
      mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
124 94f6d472 Leszek Koltunski
                                          Distorted.GLSL_VERSION,
125
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
126 f2367b75 Leszek Koltunski
      }
127
    catch(Exception e)
128
      {
129
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR1 program: "+e.getMessage());
130
      throw new RuntimeException(e.getMessage());
131
      }
132
133
    int blur1ProgramH = mBlur1Program.getProgramHandle();
134
    mRadius1H       = GLES30.glGetUniformLocation( blur1ProgramH, "u_Radius");
135
    mOffsets1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Offsets");
136
    mWeights1H      = GLES30.glGetUniformLocation( blur1ProgramH, "u_Weights");
137
    mDepth1H        = GLES30.glGetUniformLocation( blur1ProgramH, "u_Depth");
138
    mColorTexture1H = GLES30.glGetUniformLocation( blur1ProgramH, "u_ColorTexture");
139
140
    final InputStream blur2VertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
141
    final InputStream blur2FragmentStream = resources.openRawResource(R.raw.blur2_fragment_shader);
142
143
    try
144
      {
145
      mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
146 94f6d472 Leszek Koltunski
                                          Distorted.GLSL_VERSION,
147
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
148 f2367b75 Leszek Koltunski
      }
149
    catch(Exception e)
150
      {
151
      android.util.Log.e("EFFECTS", "exception trying to compile BLUR2 program: "+e.getMessage());
152 94f6d472 Leszek Koltunski
      // run anyway as compiling Blur2 WILL fail on OpenGL 2.0 contexts
153
      mBlur2Program = mBlur1Program;
154 f2367b75 Leszek Koltunski
      }
155
156
    int blur2ProgramH = mBlur2Program.getProgramHandle();
157
    mRadius2H       = GLES30.glGetUniformLocation( blur2ProgramH, "u_Radius");
158
    mOffsets2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Offsets");
159
    mWeights2H      = GLES30.glGetUniformLocation( blur2ProgramH, "u_Weights");
160
    mDepth2H        = GLES30.glGetUniformLocation( blur2ProgramH, "u_Depth");
161
    mColorTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_ColorTexture");
162
    mDepthTexture2H = GLES30.glGetUniformLocation( blur2ProgramH, "u_DepthTexture");
163 4c1dd6e9 Leszek Koltunski
    }
164
165 60c1c622 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167 95c441a2 leszek
  synchronized boolean compute(long currTime)
168 4c1dd6e9 Leszek Koltunski
    {
169 95c441a2 leszek
    if( currTime==mTime ) return false;
170 4c1dd6e9 Leszek Koltunski
    if( mTime==0 ) mTime = currTime;
171
    long step = (currTime-mTime);
172 e02264ff leszek
173 4c1dd6e9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
174
      {
175 3a70bd6d leszek
      mCurrentDuration[i] += step;
176
177 4c1dd6e9 Leszek Koltunski
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
178
        {
179
        for(int j=0; j<mNumListeners; j++)
180
          EffectMessageSender.newMessage( mListeners.elementAt(j),
181
                                          EffectMessage.EFFECT_FINISHED,
182
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
183
                                          mName[i],
184
                                          mObjectID);
185
186
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
187
          {
188
          remove(i);
189
          i--;
190
          continue;
191
          }
192
        else mInter[0][i] = null;
193
        }
194
      }
195 e02264ff leszek
196 95c441a2 leszek
    mTime = currTime;
197
198
    return true;
199 4c1dd6e9 Leszek Koltunski
    }  
200
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
  protected void moveEffect(int index)
204
    {
205
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
206
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
207
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
208 667a1ad9 Leszek Koltunski
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
209 4c1dd6e9 Leszek Koltunski
    }
210
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212 464e53fa Leszek Koltunski
// This implements the 'Better separable implementation using GPU fixed function sampling' from
213
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
214 4c1dd6e9 Leszek Koltunski
215 ed841982 Leszek Koltunski
  private void computeGaussianKernel(int radius)
216 4c1dd6e9 Leszek Koltunski
    {
217 ed841982 Leszek Koltunski
    int offset = radius + radius*radius/4;
218 0d81d0fb Leszek Koltunski
219 ed841982 Leszek Koltunski
    if( weightsCache[offset]==0.0f )
220 a225e5aa Leszek Koltunski
      {
221 ed841982 Leszek Koltunski
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
222
      mWeights[0] = GAUSSIAN[0];
223
      float sum   = GAUSSIAN[0];
224
      int j;
225 9d5bb851 Leszek Koltunski
226 ed841982 Leszek Koltunski
      for(int i=1; i<=radius; i++)
227
        {
228
        x += P;
229
        j = (int)x;
230
        z = x-j;
231 9d5bb851 Leszek Koltunski
232 ed841982 Leszek Koltunski
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
233
        sum += 2*mWeights[i];
234
        }
235 9d5bb851 Leszek Koltunski
236 ed841982 Leszek Koltunski
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
237 a225e5aa Leszek Koltunski
238 ed841982 Leszek Koltunski
      int numloops = radius/2;
239
      weightsCache[offset] = mWeights[0];
240
      offsetsCache[offset] = 0.0f;
241
242
      for(int i=0; i<numloops; i++)
243
        {
244
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
245
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
246
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
247
        }
248
249
      if( radius%2 == 1 )
250
        {
251
        int index = offset + radius/2 +1;
252 fd2db957 Leszek Koltunski
        offsetsCache[index]=radius;
253 ed841982 Leszek Koltunski
        weightsCache[index]=mWeights[radius];
254
        }
255
      }
256 9d5bb851 Leszek Koltunski
    }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260 95c441a2 leszek
  int postprocess(long time, DistortedOutputSurface surface)
261 9d5bb851 Leszek Koltunski
    {
262 e02264ff leszek
    if( mNumEffects>0 )
263 95c441a2 leszek
      {
264 e02264ff leszek
      compute(time);
265
266 8426bd6a Leszek Koltunski
      DistortedFramebuffer buffer1 = surface.mBuffer1[mQuality];
267
      DistortedFramebuffer buffer2 = surface.mBuffer1[mQuality];
268
269
      float w1 = buffer1.mWidth;
270
      float h1 = buffer1.mHeight;
271 78db8663 Leszek Koltunski
      float w2 = surface.mWidth;
272
      float h2 = surface.mHeight;
273 95c441a2 leszek
274
      int radius = (int)mUniforms[0];
275
      if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
276
      computeGaussianKernel(radius);
277
278
      int offset = radius + radius*radius/4;
279
      radius = (radius+1)/2;
280
281
      // horizontal blur
282 78db8663 Leszek Koltunski
      GLES30.glViewport(0, 0, (int)w1, (int)h1);
283 f2367b75 Leszek Koltunski
      mBlur1Program.useProgram();
284 8426bd6a Leszek Koltunski
      buffer1.setAsInput();
285
      buffer2.setAsOutput(time);
286 f2367b75 Leszek Koltunski
287
      GLES30.glUniform1fv( mWeights1H, radius+1, weightsCache,offset);
288
      GLES30.glUniform1i( mRadius1H, radius);
289
      GLES30.glUniform1f( mDepth1H , 1.0f-surface.mNear);
290 a93931da Leszek Koltunski
      GLES30.glUniform1i( mColorTexture1H , 0 );
291 78db8663 Leszek Koltunski
      for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
292 f2367b75 Leszek Koltunski
      GLES30.glUniform1fv( mOffsets1H ,radius+1, mOffsets,0);
293
      GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
294 ed5bf324 Leszek Koltunski
      GLES30.glVertexAttribPointer(mBlur1Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
295 95c441a2 leszek
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
296
297
      // vertical blur
298 78db8663 Leszek Koltunski
      GLES30.glViewport(0, 0, (int)w2, (int)h2);
299 f2367b75 Leszek Koltunski
      mBlur2Program.useProgram();
300 8426bd6a Leszek Koltunski
      buffer2.setAsInput();
301
      buffer1.setAsDepth();
302 95c441a2 leszek
      surface.setAsOutput(time);
303 f2367b75 Leszek Koltunski
304
      GLES30.glUniform1fv( mWeights2H, radius+1, weightsCache,offset);
305
      GLES30.glUniform1i( mRadius2H, radius);
306
      GLES30.glUniform1f( mDepth2H , 1.0f-surface.mNear);
307 a93931da Leszek Koltunski
      GLES30.glUniform1i( mColorTexture2H , 0 );
308
      GLES30.glUniform1i( mDepthTexture2H , 1 );
309 78db8663 Leszek Koltunski
      for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
310 f2367b75 Leszek Koltunski
      GLES30.glUniform1fv( mOffsets2H ,radius+1, mOffsets,0);
311
      GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
312 ed5bf324 Leszek Koltunski
      GLES30.glVertexAttribPointer(mBlur2Program.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
313 95c441a2 leszek
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
314
      }
315 93dca704 Leszek Koltunski
316 95c441a2 leszek
    return mNumEffects;
317 d6e94c84 Leszek Koltunski
    }
318
319 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
320
// blur
321
322 85cbbc5e Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D degree)
323 4c1dd6e9 Leszek Koltunski
    {
324
    if( mMax[INDEX]>mNumEffects )
325
      {
326
      if( degree instanceof Dynamic1D)
327
        {
328
        mInter[0][mNumEffects] = (Dynamic1D)degree;
329
        }
330
      else if( degree instanceof Static1D)
331
        {
332
        mInter[0][mNumEffects] = null;
333
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
334
        }
335
      else return -1;
336
337 85cbbc5e Leszek Koltunski
      mInter[1][mNumEffects] = null;
338
      mInter[2][mNumEffects] = null;
339 4c1dd6e9 Leszek Koltunski
340
      return addBase(eln);
341
      }
342
      
343
    return -1;
344
    }
345
  }