Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 30094332

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.effectqueue;
21

    
22
import org.distorted.library.effect.Effect;
23
import org.distorted.library.effect.EffectName;
24
import org.distorted.library.effect.EffectType;
25
import org.distorted.library.main.DistortedLibrary;
26
import org.distorted.library.main.InternalMaster;
27
import org.distorted.library.main.InternalStackFrameList;
28

    
29
import java.util.ArrayList;
30
import java.util.HashMap;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * Not part of public API, do not document
35
 *
36
 * @y.exclude
37
 */
38
public abstract class EffectQueue implements InternalMaster.Slave
39
  {
40
  public static final int MAIN_VARIANTS = 4; // Number of Main program variants (ATM 4: MAIN, MAIN OIT, PREPROCESS, FULL)
41

    
42
  private static final int CREATE = 0;
43
  private static final int ATTACH = 1;
44
  private static final int DETACH = 2;
45
  private static final int DETALL = 3;
46

    
47
  long mTime;
48
  int mNumEffects;              // 'ToBe' will be more than mNumEffects if doWork() hasn't
49
  private int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
50
  private int mNumFloatUniforms, mNumIntUniforms;
51
  Effect[] mEffects;
52

    
53
  float[] mFloatUniforms;
54
  int[] mIntUniforms;
55

    
56
  private static long mNextID = 1;
57
  private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
58
                                                                         // single long - the queue ID.
59
  private long mID;
60
  private int mIndex;
61
  private boolean mCreated;
62

    
63
  private static class Job
64
    {
65
    int type;
66
    int num1, num2;
67
    boolean notify;
68
    Effect effect;
69

    
70
    Job(int t, int m1, int m2, boolean n, Effect e)
71
      {
72
      type  = t;
73
      num1  = m1;
74
      num2  = m2;
75
      notify= n;
76
      effect= e;
77
      }
78
    }
79

    
80
  private ArrayList<Job> mJobs = new ArrayList<>();
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
   
84
  EffectQueue(int numFloatUniforms, int numIntUniforms, int index)
85
    {
86
    mCreated          = false;
87
    mTime             = 0;
88
    mID               = 0;
89
    mNumEffects       = 0;
90
    mNumEffectsToBe   = 0;
91
    mIndex            = index;
92
    mNumFloatUniforms = numFloatUniforms;
93
    mNumIntUniforms   = numIntUniforms;
94

    
95
    mJobs.add(new Job(CREATE,numFloatUniforms,numIntUniforms, false,null)); // create the stuff that depends on max number
96
    InternalMaster.newSlave(this);                                          // of uniforms later, on first render.
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// this is not thread safe! The 'source' might change while we're copying it.
101

    
102
  EffectQueue(EffectQueue source)
103
    {
104
    if( !source.mCreated )
105
      {
106
      mCreated          = false;
107
      mTime             = 0;
108
      mID               = 0;
109
      mNumEffects       = 0;
110
      mNumEffectsToBe   = 0;
111
      mIndex            = source.mIndex;
112
      mNumFloatUniforms = source.mNumFloatUniforms;
113
      mNumIntUniforms   = source.mNumIntUniforms;
114

    
115
      int numJobs = source.mJobs.size();
116

    
117
      for(int i=0; i<numJobs; i++)
118
        {
119
        Job job = source.mJobs.get(i);
120
        mJobs.add(job);
121
        }
122

    
123
      InternalMaster.newSlave(this);
124
      }
125
    else
126
      {
127
      mCreated          = true;
128
      mTime             = source.mTime;
129
      mID               = source.mID;
130
      mNumEffects       = source.mNumEffects;
131
      mNumEffectsToBe   = source.mNumEffectsToBe;
132
      mIndex            = source.mIndex;
133
      mNumFloatUniforms = source.mNumFloatUniforms;
134
      mNumIntUniforms   = source.mNumIntUniforms;
135

    
136
      int max = InternalStackFrameList.getMax(mIndex);
137

    
138
      if( max>0 )
139
        {
140
        mEffects       = new Effect[max];
141
        mFloatUniforms = new float[max*source.mNumFloatUniforms];
142
        mIntUniforms   = new int[max*source.mNumIntUniforms];
143

    
144
        if( mNumEffects>=0 )
145
          {
146
          System.arraycopy(source.mEffects, 0, mEffects, 0, mNumEffects);
147
          }
148

    
149
        if( mNumIntUniforms*mNumEffects>=0 )
150
          {
151
          System.arraycopy(source.mIntUniforms, 0, mIntUniforms, 0, mNumIntUniforms*mNumEffects);
152
          }
153
        }
154
      }
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
160
    {
161
    queues[0] = (flags & DistortedLibrary.CLONE_MATRIX     ) != 0 ? from[0] : new EffectQueueMatrix();
162
    queues[1] = (flags & DistortedLibrary.CLONE_VERTEX     ) != 0 ? from[1] : new EffectQueueVertex();
163
    queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT   ) != 0 ? from[2] : new EffectQueueFragment();
164
    queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public static void compute(EffectQueue[] queues, long currTime)
170
    {
171
    ((EffectQueueMatrix     )queues[0]).compute(currTime);
172
    ((EffectQueueVertex     )queues[1]).compute(currTime);
173
    ((EffectQueueFragment   )queues[2]).compute(currTime);
174
    ((EffectQueuePostprocess)queues[3]).compute(currTime);
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  public static void send(EffectQueue[] queues, float distance, float mipmap,
180
                          float[] projection, float inflate, int variant )
181
    {
182
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, variant);
183
    ((EffectQueueVertex  )queues[1]).send(inflate, variant);
184
    ((EffectQueueFragment)queues[2]).send(variant);
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
189

    
190
  public static void getUniforms(int programH, int variant)
191
    {
192
    EffectQueueFragment.uniforms(programH,variant);
193
    EffectQueueVertex  .uniforms(programH,variant);
194
    EffectQueueMatrix  .uniforms(programH,variant);
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
199
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
200
// especially important in case of postprocessing)
201

    
202
  private void regenerateID()
203
    {
204
    if( mNumEffects>0 )
205
      {
206
      ArrayList<Long> list = new ArrayList<>();
207
      for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
208
      Long id = mMapID.get(list);
209

    
210
      if( id!=null )
211
        {
212
        mID = id;
213
        }
214
      else
215
        {
216
        mMapID.put(list,mNextID);
217
        mID = mNextID++;
218
        }
219
      }
220
    else
221
      {
222
      mID = 0;
223
      }
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public long getID()
229
    {
230
    return mID;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  public static boolean setMax(int index, int m)
236
    {
237
    return InternalStackFrameList.setMax(index, Math.max(m,0));
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  public static int getMax(int index)
243
    {
244
    return InternalStackFrameList.getMax(index);
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public static void onDestroy()
250
    {
251
    mNextID = 1;
252
    mMapID.clear();
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
// this assumes 0<=effect
257

    
258
  private void removeNow(int pos)
259
    {
260
    if( mNumEffects>pos )
261
      {
262
      mNumEffects--;
263
      System.arraycopy(mEffects, pos+1, mEffects, pos, mNumEffects-pos);
264
      System.arraycopy(mIntUniforms, mNumIntUniforms*(pos+1), mIntUniforms, mNumIntUniforms*pos, mNumIntUniforms*(mNumEffects-pos) );
265
      mEffects[mNumEffects] = null;
266
      }
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  private void addNow(int pos, Effect effect)
272
    {
273
    mEffects[pos]                     = effect;
274
    mIntUniforms[mNumIntUniforms*pos] = effect.getName().ordinal();
275

    
276
    if( mIndex==EffectType.VERTEX.ordinal() )
277
      {
278
      effect.writeAssociations(mIntUniforms, mNumIntUniforms*pos+1, mNumIntUniforms*pos+3);
279
      }
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  public synchronized int removeByName(EffectName name)
285
    {
286
    int ret = 0;
287

    
288
    for(int i=0; i<mNumEffects; i++)
289
      {
290
      if( mEffects[i].getName() == name )
291
        {
292
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
293
        ret++;
294
        }
295
      }
296

    
297
    if( ret>0 )
298
      {
299
      InternalMaster.newSlave(this);
300
      mNumEffectsToBe-=ret;
301
      }
302

    
303
    return ret;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public synchronized int removeById(long id)
309
    {
310
    for(int i=0; i<mNumEffects; i++)
311
      {
312
      if( mEffects[i].getID() == id )
313
        {
314
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
315
        InternalMaster.newSlave(this);
316
        mNumEffectsToBe--;
317
        return 1;
318
        }
319
      }
320

    
321
    return 0;
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325

    
326
  public synchronized int removeEffect(Effect effect)
327
    {
328
    for(int i=0; i<mNumEffects; i++)
329
      {
330
      if( mEffects[i]==effect )
331
        {
332
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
333
        InternalMaster.newSlave(this);
334
        mNumEffectsToBe--;
335
        return 1;
336
        }
337
      }
338
   
339
    return 0;
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
344
// them if it is the library itself which is releasing resources.
345

    
346
  public synchronized int removeAll(boolean notify)
347
    {
348
    mJobs.add(new Job(DETALL,0,0,notify,null));
349
    InternalMaster.newSlave(this);
350
    mNumEffectsToBe = 0;
351
    return mNumEffects;
352
    }
353

    
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
  
356
  public boolean add(Effect effect)
357
    {
358
    if( InternalStackFrameList.getMax(mIndex)>mNumEffectsToBe || !mCreated )
359
      {
360
      mJobs.add(new Job(ATTACH,-1,0,false,effect));
361
      InternalMaster.newSlave(this);
362
      mNumEffectsToBe++;
363
      return true;
364
      }
365

    
366
    return false;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  public boolean add(Effect effect, int position)
372
    {
373
    if( InternalStackFrameList.getMax(mIndex)>mNumEffectsToBe || !mCreated )
374
      {
375
      mJobs.add(new Job(ATTACH,position,0,false,effect));
376
      InternalMaster.newSlave(this);
377
      mNumEffectsToBe++;
378
      return true;
379
      }
380

    
381
    return false;
382
    }
383

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  public int getNumEffects()
387
    {
388
    return mNumEffects;
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392

    
393
  public int getNumEffectsToBe()
394
    {
395
    return mNumEffectsToBe;
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

    
400
  public Effect getEffect(int position)
401
    {
402
    if( position>=0 && position< mNumEffects )
403
      {
404
      return mEffects[position];
405
      }
406
    else
407
      {
408
      android.util.Log.e("queue", "getEffect: out of range "+position);
409
      return null;
410
      }
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414

    
415
  public void doWork()
416
    {
417
    boolean changed = false;
418
    int num = mJobs.size();
419
    Job job;
420

    
421
    for(int i=0; i<num; i++)
422
      {
423
      job = mJobs.remove(0);
424

    
425
      switch(job.type)
426
        {
427
        case CREATE: int max = InternalStackFrameList.getMax(mIndex);
428
                     if( max>0 )
429
                       {
430
                       mEffects       = new Effect[max];
431
                       mFloatUniforms = new float[max*job.num1];
432
                       mIntUniforms   = new int[max*job.num2];
433
                       }
434
                     mCreated = true;
435

    
436
                     break;
437
        case ATTACH: if( InternalStackFrameList.getMax(mIndex)>mNumEffects ) // it is possible that we have first
438
                       {                                                     // added effects and then lowered mMax
439
                       int position = job.num1;
440

    
441
                       if( position<0 )
442
                         {
443
                         addNow(mNumEffects,job.effect);
444
                         mNumEffects++;
445
                         changed = true;
446
                         }
447
                       else if( position<=mNumEffects )
448
                         {
449
                         System.arraycopy(mEffects    , position, mEffects    , position+1, mNumEffects-position);
450
                         System.arraycopy(mIntUniforms, mNumIntUniforms*position, mIntUniforms, mNumIntUniforms*(position+1), mNumIntUniforms*(mNumEffects-position) );
451
                         addNow(position,job.effect);
452
                         mNumEffects++;
453
                         changed = true;
454
                         }
455
                       }
456
                     else
457
                       {
458
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
459
                       }
460
                     break;
461
        case DETACH: for(int j=0; j<mNumEffects; j++)
462
                       {
463
                       if (mEffects[j] == job.effect)
464
                         {
465
                         removeNow(j);
466
                         changed = true;
467
                         break;
468
                         }
469
                       }
470
                     break;
471
        case DETALL: for(int j=0; j<mNumEffects; j++ )
472
                       {
473
                       changed = true;
474
                       mEffects[j] = null;
475
                       }
476

    
477
                     mNumEffects= 0;
478
                     break;
479
        }
480
      }
481

    
482
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
483
    }
484
  }
(1-1/5)