Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueFragment.java @ 2fce34f4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.opengl.GLES20;
23

    
24
import org.distorted.library.message.EffectMessage;
25
import org.distorted.library.type.Data1D;
26
import org.distorted.library.type.Data4D;
27
import org.distorted.library.type.Dynamic1D;
28
import org.distorted.library.type.Dynamic4D;
29
import org.distorted.library.type.Static1D;
30
import org.distorted.library.type.Static2D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.library.type.Dynamic;
34
import org.distorted.library.type.Dynamic2D;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
class EffectQueueFragment extends EffectQueue
39
  {
40
  private static final int NUM_UNIFORMS = 8;
41
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
42
  private float[] mBuf;
43
  private static int mNumEffectsH;
44
  private static int mTypeH;
45
  private static int mUniformsH;
46
  
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
   
49
  public EffectQueueFragment(DistortedObject obj)
50
    { 
51
    super(obj,NUM_UNIFORMS,INDEX);
52
   
53
    if( mMax[INDEX]>0 )
54
      {
55
      mBuf= new float[4*mMax[INDEX]];
56
      }
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  static void getUniforms(int mProgramH)
62
    {
63
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
64
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
65
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
  
70
  synchronized void compute(long currTime) 
71
    { 
72
    if( currTime==mTime ) return;
73
    if( mTime==0 ) mTime = currTime;
74
    long step = (currTime-mTime);
75
   
76
    for(int i=0; i<mNumEffects; i++)
77
      {
78
      if( mInterI[i]==null ) continue;    
79
      
80
      if( mInterP[i]!=null ) mInterP[i].interpolateMain(mBuf, 4*i, mCurrentDuration[i]);
81
        
82
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
83
        {
84
        for(int j=0; j<mNumListeners; j++)   
85
          EffectMessageSender.newMessage( mListeners.elementAt(j),
86
                                          EffectMessage.EFFECT_FINISHED,
87
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
88
                                          mType[i], 
89
                                          mBitmapID,
90
                                          null);
91
      
92
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
93
          {
94
          remove(i);
95
          i--;
96
          continue;
97
          }
98
        }
99
           
100
      mCurrentDuration[i] += step;
101
      }
102
   
103
    mTime = currTime;  
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  protected void moveEffect(int index)
109
    {
110
    mBuf[4*index  ] = mBuf[4*index+4];
111
    mBuf[4*index+1] = mBuf[4*index+5];
112
    mBuf[4*index+2] = mBuf[4*index+6];
113
    mBuf[4*index+3] = mBuf[4*index+7];
114
              
115
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
116
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
117
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
118
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
119
    }
120
  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
  
123
  synchronized void send() 
124
    {
125
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
126
      
127
    if( mNumEffects>0 )
128
      {     
129
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
130
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
131
      }  
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  synchronized void sendZero() 
137
    {
138
    GLES20.glUniform1i( mNumEffectsH, 0);
139
    }
140
    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// Do various post-processing on already computed effects.
143
// 1) move all Points and scale all Region radii by a ModelView matrix
144
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
145
  
146
  void postprocess(float[] MVmatrix)
147
    {
148
    if( mNumEffects>0 )
149
      {
150
      float tx,ty;   
151
      float w = (float)Math.sqrt(MVmatrix[0]*MVmatrix[0] + MVmatrix[4]*MVmatrix[4]);  // The scale factors are the lengths of the first 3 vectors of the upper-left 3x3 submatrix; here
152
      float h = (float)Math.sqrt(MVmatrix[1]*MVmatrix[1] + MVmatrix[5]*MVmatrix[5]);  // m[2]=m[6]=m[8]=m[9]=0 so it is really only the upper-left 2x2 matrix.
153
   
154
      for(int i=0; i<mNumEffects; i++)
155
        {   
156
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
157
        ty =-mBuf[4*i+1]+mObjHalfY; //
158

    
159
        mUniforms[NUM_UNIFORMS*i+4] = MVmatrix[0]*tx + MVmatrix[4]*ty + MVmatrix[12]; // multiply the ModelView matrix times the (x,y,0,1) point, i.e. the (x,y) point on the surface of the bitmap.
160
        mUniforms[NUM_UNIFORMS*i+5] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //
161
        mUniforms[NUM_UNIFORMS*i+6] = w*mBuf[4*i+2];                                  // in fragment shader rx and ry radii are the last two values of the second vec4
162
        mUniforms[NUM_UNIFORMS*i+7] = h*mBuf[4*i+3];                                  //
163

    
164
        if( mType[i]==EffectNames.MACROBLOCK.ordinal() ) // fill up the .y and .z components of the Interpolated values already to avoid having to compute this in the fragment shader
165
          {
166
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
167
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
168
          }
169
        }
170
      }
171
    }
172
  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
// macroblock, alpha, brightness, contrast, saturation
175

    
176
  synchronized long add(EffectNames eln, Data1D data)
177
    {
178
    if( mMax[INDEX]>mNumEffects )
179
      {
180
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
181

    
182
      if( data instanceof Dynamic1D)
183
        mInterI[mNumEffects] = (Dynamic1D)data;
184
      else if( data instanceof Static1D )
185
        {
186
        mInterI[mNumEffects] = null;
187
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
188
        }
189
      else return -1;
190

    
191
      mInterP[mNumEffects] = null;
192
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
193
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
194

    
195
      return addBase(eln); 
196
      }
197
      
198
    return -1;
199
    }
200
  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
// macroblock, alpha, brightness, contrast, saturation
203

    
204
  synchronized long add(EffectNames eln, Data1D data, Data4D region)
205
    {
206
    if( mMax[INDEX]>mNumEffects )
207
      {
208
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
209

    
210
      if( data instanceof Dynamic1D)
211
        mInterI[mNumEffects] = (Dynamic1D)data;
212
      else if( data instanceof Static1D )
213
        {
214
        mInterI[mNumEffects] = null;
215
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
216
        }
217
      else return -1;
218

    
219
      if( region instanceof Dynamic4D)
220
        mInterP[mNumEffects] = (Dynamic4D)region;
221
      else if( region instanceof Static4D )
222
        {
223
        mInterP[mNumEffects]  = null;
224
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
225
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
226
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
227
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
228
        }
229
      else return -1;
230

    
231
      return addBase(eln);
232
      }
233
      
234
    return -1;
235
    }
236
  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// chroma
239

    
240
  synchronized long add(EffectNames eln, Data1D level, Static3D color, Data4D region)
241
    {
242
    if( mMax[INDEX]>mNumEffects )
243
      {
244
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
245

    
246
      if( level instanceof Dynamic1D)
247
        mInterI[mNumEffects] = (Dynamic1D)level;
248
      else if( level instanceof Static1D )
249
        {
250
        mInterI[mNumEffects] = null;
251
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
252
        }
253
      else return -1;
254

    
255
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = color.getX();
256
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = color.getY();
257
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = color.getZ();
258

    
259
      if( region instanceof Dynamic4D)
260
        mInterP[mNumEffects] = (Dynamic4D)region;
261
      else if( region instanceof Static4D )
262
        {
263
        mInterP[mNumEffects]  = null;
264
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
265
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
266
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
267
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
268
        }
269
      else return -1;
270

    
271
      return addBase(eln); 
272
      }
273
      
274
    return -1;
275
    }
276
  
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
// chroma
279

    
280
  synchronized long add(EffectNames eln, Data1D level, Static3D color)
281
    {
282
    if( mMax[INDEX]>mNumEffects )
283
      {
284
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
285

    
286
      if( level instanceof Dynamic1D)
287
        mInterI[mNumEffects] = (Dynamic1D)level;
288
      else if( level instanceof Static1D )
289
        {
290
        mInterI[mNumEffects] = null;
291
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
292
        }
293
      else return -1;
294

    
295
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = color.getX();
296
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = color.getY();
297
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = color.getZ();
298

    
299
      mInterP[mNumEffects]  = null;            //
300
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
301
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
302

    
303
      return addBase(eln);
304
      }
305
       
306
    return -1;
307
    }
308
  
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310
// chroma
311

    
312
  synchronized long add(EffectNames eln, Data4D chroma, Data4D region)
313
    {
314
    if( mMax[INDEX]>mNumEffects )
315
      {
316
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
317

    
318
      if( chroma instanceof Dynamic4D)
319
        mInterI[mNumEffects] = (Dynamic4D)chroma;
320
      else if( chroma instanceof Static4D )
321
        {
322
        mInterI[mNumEffects] = null;
323
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)chroma).getX();
324
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)chroma).getY();
325
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)chroma).getZ();
326
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)chroma).getW();
327
        }
328
      else return -1;
329

    
330
      if( region instanceof Dynamic4D)
331
        mInterP[mNumEffects] = (Dynamic4D)region;
332
      else if( region instanceof Static4D )
333
        {
334
        mInterP[mNumEffects]  = null;
335
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
336
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
337
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
338
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
339
        }
340
      else return -1;
341

    
342
      return addBase(eln); 
343
      }
344
      
345
    return -1;
346
    }
347
  
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
// chroma
350

    
351
  synchronized long add(EffectNames eln, Data4D chroma)
352
    {
353
    if( mMax[INDEX]>mNumEffects )
354
      {
355
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
356

    
357
      if( chroma instanceof Dynamic4D)
358
        mInterI[mNumEffects] = (Dynamic4D)chroma;
359
      else if( chroma instanceof Static4D )
360
        {
361
        mInterI[mNumEffects] = null;
362
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)chroma).getX();
363
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)chroma).getY();
364
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)chroma).getZ();
365
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)chroma).getW();
366
        }
367
      else return -1;
368

    
369
      mInterP[mNumEffects]  = null;            //
370
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
371
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
372

    
373
      return addBase(eln);
374
      }
375
      
376
    return -1;
377
    }
378
    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
// end of FragmentEffect   
381
  }
(14-14/17)