Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 62c869ad

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.mesh.MeshBase;
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
  int mNumEffects;              // 'ToBe' will be more than mNumEffects if doWork() hasn't
48
  private int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
49
  float[] mUniforms;
50
  private int mNumUniforms;
51
  Effect[] mEffects;
52
  int[] mName;
53
  int[] mAndAssociation;
54
  int[] mEquAssociation;
55
  long mTime;
56

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

    
65
  private static class Job
66
    {
67
    int type;
68
    int num;
69
    boolean notify;
70
    Effect effect;
71

    
72
    Job(int t, int m, boolean n, Effect e)
73
      {
74
      type  = t;
75
      num   = m;
76
      notify= n;
77
      effect= e;
78
      }
79
    }
80

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

    
83
  static
84
    {
85
    onDestroy();
86
    }
87
  
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
  EffectQueue(int numUniforms, int index)
91
    {
92
    mCreated            = false;
93
    mTime               = 0;
94
    mID                 = 0;
95
    mNumEffects         = 0;
96
    mNumEffectsToBe     = 0;
97
    mIndex              = index;
98
    mNumUniforms        = numUniforms;
99

    
100
    mJobs.add(new Job(CREATE,numUniforms,false,null)); // create the stuff that depends on max number
101
    InternalMaster.newSlave(this);                     // of uniforms later, on first render.
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
// this is not thread safe! The 'source' might change while we're copying it.
106

    
107
  EffectQueue(EffectQueue source)
108
    {
109
    if( !source.mCreated )
110
      {
111
      mCreated            = false;
112
      mTime               = 0;
113
      mID                 = 0;
114
      mNumEffects         = 0;
115
      mNumEffectsToBe     = 0;
116
      mIndex              = source.mIndex;
117
      mNumUniforms        = source.mNumUniforms;
118

    
119
      int numJobs = source.mJobs.size();
120

    
121
      for(int i=0; i<numJobs; i++)
122
        {
123
        Job job = source.mJobs.get(i);
124
        mJobs.add(job);
125
        }
126

    
127
      InternalMaster.newSlave(this);
128
      }
129
    else
130
      {
131
      mCreated            = true;
132
      mTime               = source.mTime;
133
      mID                 = source.mID;
134
      mNumEffects         = source.mNumEffects;
135
      mNumEffectsToBe     = source.mNumEffectsToBe;
136
      mIndex              = source.mIndex;
137
      mNumUniforms        = source.mNumUniforms;
138

    
139
      int max = mMax[mIndex];
140
      if( max>0 )
141
        {
142
        mUniforms        = new float[max*source.mNumUniforms];
143
        mEffects         = new Effect[max];
144
        mName            = new int[max];
145
        mAndAssociation  = new int[max];
146
        mEquAssociation  = new int[max];
147
        }
148

    
149
      for(int i=0; i<mNumEffects; i++ )
150
        {
151
        mEffects[i]         = source.mEffects[i];
152
        mName[i]            = source.mName[i];
153
        mAndAssociation[i]  = source.mAndAssociation[i];
154
        mEquAssociation[i]  = source.mEquAssociation[i];
155
        }
156
      }
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
191

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

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

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

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

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  public long getID()
231
    {
232
    return mID;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public static boolean setMax(int index, int m)
238
    {
239
    if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
240
      {
241
      mMax[index] = m<0 ? 0:m;
242
      return true;
243
      }
244

    
245
    return false;
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public static int getMax(int index)
251
    {
252
    return mMax[index];
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public static void onDestroy()
258
    {
259
    mNextID = 1;
260
    mMapID.clear();
261
    EffectType.reset(mMax);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
// this assumes 0<=effect<mNumEffects
266

    
267
  protected void remove(int effect)
268
    {
269
    mNumEffects--;
270

    
271
    for(int j=effect; j<mNumEffects; j++ )
272
      {
273
      mEffects[j]         = mEffects[j+1];
274
      mName[j]            = mName[j+1];
275
      mAndAssociation[j]  = mAndAssociation[j+1];
276
      mEquAssociation[j]  = mEquAssociation[j+1];
277
      }
278

    
279
    mEffects[mNumEffects] = null;
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,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,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,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,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( mMax[mIndex]>mNumEffectsToBe || !mCreated )
359
      {
360
      mJobs.add(new Job(ATTACH,-1,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( mMax[mIndex]>mNumEffectsToBe || !mCreated )
374
      {
375
      mJobs.add(new Job(ATTACH,position,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 = mMax[mIndex];
428
                     if( max>0 )
429
                       {
430
                       mUniforms        = new float[max*job.num];
431
                       mEffects         = new Effect[max];
432
                       mName            = new int[max];
433
                       mAndAssociation  = new int[max];
434
                       mEquAssociation  = new int[max];
435
                       }
436
                     mCreated = true;
437

    
438
                     break;
439
        case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
440
                       {                            // added effects and then lowered mMax
441
                       int position = job.num;
442

    
443
                       if( position==-1 )
444
                         {
445
                         mEffects[mNumEffects]        = job.effect;
446
                         mName[mNumEffects]           = job.effect.getName().ordinal();
447
                         mAndAssociation[mNumEffects] = job.effect.getAndAssociation();
448
                         mEquAssociation[mNumEffects] = job.effect.getEquAssociation();
449

    
450
                         mNumEffects++;
451
                         changed = true;
452
                         }
453
                       else if( position>=0 && position<=mNumEffects )
454
                         {
455
                         for(int j=mNumEffects; j>position; j--)
456
                           {
457
                           mEffects[j]         = mEffects[j-1];
458
                           mName[j]            = mName[j-1];
459
                           mAndAssociation[j]  = mAndAssociation[j-1];
460
                           mEquAssociation[j]  = mEquAssociation[j-1];
461
                           }
462

    
463
                         mEffects[position]        = job.effect;
464
                         mName[position]           = job.effect.getName().ordinal();
465
                         mAndAssociation[position] = job.effect.getAndAssociation();
466
                         mEquAssociation[position] = job.effect.getEquAssociation();
467

    
468
                         mNumEffects++;
469
                         changed = true;
470
                         }
471
                       }
472
                     else
473
                       {
474
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
475
                       }
476
                     break;
477
        case DETACH: for(int j=0; j<mNumEffects; j++)
478
                       {
479
                       if (mEffects[j] == job.effect)
480
                         {
481
                         remove(j);
482
                         changed = true;
483
                         break;
484
                         }
485
                       }
486
                     break;
487
        case DETALL: for(int j=0; j<mNumEffects; j++ )
488
                       {
489
                       changed = true;
490
                       mEffects[j] = null;
491
                       }
492

    
493
                     mNumEffects= 0;
494
                     break;
495
        }
496
      }
497

    
498
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
499
    }
500
  }
(1-1/5)