Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueue.java @ 72ef21f7

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 long mID;
57
  private int mIndex;
58
  private boolean mCreated;
59

    
60
  private static class Job
61
    {
62
    int type;
63
    int num1, num2;
64
    boolean notify;
65
    Effect effect;
66

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

    
77
  private ArrayList<Job> mJobs = new ArrayList<>();
78

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

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

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// this is not thread safe! The 'source' might change while we're copying it.
98

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

    
112
      int numJobs = source.mJobs.size();
113

    
114
      for(int i=0; i<numJobs; i++)
115
        {
116
        Job job = source.mJobs.get(i);
117
        mJobs.add(job);
118
        }
119

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

    
133
      int max = InternalStackFrameList.getMax(mIndex);
134

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

    
141
        if( mNumEffects>=0 )
142
          {
143
          System.arraycopy(source.mEffects, 0, mEffects, 0, mNumEffects);
144
          }
145

    
146
        if( mNumIntUniforms*mNumEffects>=0 )
147
          {
148
          System.arraycopy(source.mIntUniforms, 0, mIntUniforms, 0, mNumIntUniforms*mNumEffects);
149
          }
150
        }
151
      }
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

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

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// variant: 0 --> MAIN  1 --> OIT  2 --> prePOST  3 --> FULL
186

    
187
  public static void getUniforms(int programH, int variant)
188
    {
189
    EffectQueueFragment.uniforms(programH,variant);
190
    EffectQueueVertex  .uniforms(programH,variant);
191
    EffectQueueMatrix  .uniforms(programH,variant);
192
    }
193

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

    
199
  private void regenerateID()
200
    {
201
    if( mNumEffects>0 )
202
      {
203
      HashMap<ArrayList<Long>,Long> map = InternalStackFrameList.getMap();
204
      ArrayList<Long> list = new ArrayList<>();
205
      for (int i=0; i<mNumEffects; i++) list.add(mEffects[i].getID());
206
      Long id = map.get(list);
207

    
208
      if( id!=null )
209
        {
210
        mID = id;
211
        }
212
      else
213
        {
214
        mID = InternalStackFrameList.getNextQueueID();
215
        map.put(list,mID);
216
        }
217
      }
218
    else
219
      {
220
      mID = 0;
221
      }
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  public long getID()
227
    {
228
    return mID;
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
// this assumes 0<=effect
247

    
248
  private void removeNow(int pos)
249
    {
250
    if( mNumEffects>pos )
251
      {
252
      mNumEffects--;
253
      mEffects[pos].remQueue(this);
254
      System.arraycopy(mEffects, pos+1, mEffects, pos, mNumEffects-pos);
255
      System.arraycopy(mIntUniforms, mNumIntUniforms*(pos+1), mIntUniforms, mNumIntUniforms*pos, mNumIntUniforms*(mNumEffects-pos) );
256
      mEffects[mNumEffects] = null;
257
      }
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
  private void addNow(int pos, Effect effect)
263
    {
264
    mEffects[pos]                     = effect;
265
    mIntUniforms[mNumIntUniforms*pos] = effect.getName().ordinal();
266

    
267
    effect.addQueue(this);
268

    
269
    if( mIndex==EffectType.VERTEX.ordinal() )
270
      {
271
      effect.writeAssociations(mIntUniforms, mNumIntUniforms*pos+1, mNumIntUniforms*pos+3);
272
      }
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  public synchronized int removeByName(EffectName name)
278
    {
279
    int ret = 0;
280

    
281
    for(int i=0; i<mNumEffects; i++)
282
      {
283
      if( mEffects[i].getName() == name )
284
        {
285
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
286
        ret++;
287
        }
288
      }
289

    
290
    if( ret>0 )
291
      {
292
      InternalMaster.newSlave(this);
293
      mNumEffectsToBe-=ret;
294
      }
295

    
296
    return ret;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public synchronized int removeById(long id)
302
    {
303
    for(int i=0; i<mNumEffects; i++)
304
      {
305
      if( mEffects[i].getID() == id )
306
        {
307
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
308
        InternalMaster.newSlave(this);
309
        mNumEffectsToBe--;
310
        return 1;
311
        }
312
      }
313

    
314
    return 0;
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public synchronized int removeEffect(Effect effect)
320
    {
321
    for(int i=0; i<mNumEffects; i++)
322
      {
323
      if( mEffects[i]==effect )
324
        {
325
        mJobs.add(new Job(DETACH,0,0,true,mEffects[i]));
326
        InternalMaster.newSlave(this);
327
        mNumEffectsToBe--;
328
        return 1;
329
        }
330
      }
331
   
332
    return 0;
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
337
// them if it is the library itself which is releasing resources.
338

    
339
  public synchronized int removeAll(boolean notify)
340
    {
341
    mJobs.add(new Job(DETALL,0,0,notify,null));
342
    InternalMaster.newSlave(this);
343
    mNumEffectsToBe = 0;
344
    return mNumEffects;
345
    }
346

    
347
///////////////////////////////////////////////////////////////////////////////////////////////////
348
  
349
  public boolean add(Effect effect)
350
    {
351
    if( InternalStackFrameList.getMax(mIndex)>mNumEffectsToBe || !mCreated )
352
      {
353
      mJobs.add(new Job(ATTACH,-1,0,false,effect));
354
      InternalMaster.newSlave(this);
355
      mNumEffectsToBe++;
356
      return true;
357
      }
358

    
359
    return false;
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  public boolean add(Effect effect, int position)
365
    {
366
    if( InternalStackFrameList.getMax(mIndex)>mNumEffectsToBe || !mCreated )
367
      {
368
      mJobs.add(new Job(ATTACH,position,0,false,effect));
369
      InternalMaster.newSlave(this);
370
      mNumEffectsToBe++;
371
      return true;
372
      }
373

    
374
    return false;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  public int getNumEffects()
380
    {
381
    return mNumEffects;
382
    }
383

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

    
386
  public int getNumEffectsToBe()
387
    {
388
    return mNumEffectsToBe;
389
    }
390

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

    
393
  public Effect getEffect(int position)
394
    {
395
    if( position>=0 && position< mNumEffects )
396
      {
397
      return mEffects[position];
398
      }
399
    else
400
      {
401
      android.util.Log.e("queue", "getEffect: out of range "+position);
402
      return null;
403
      }
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407

    
408
  public void setAssociation(long effectID)
409
    {
410
    for(int j=0; j<mNumEffects; j++)
411
      {
412
      if (mEffects[j].getID() == effectID)
413
        {
414
        mEffects[j].writeAssociations(mIntUniforms, mNumIntUniforms*j+1, mNumIntUniforms*j+3);
415
        }
416
      }
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public void doWork()
422
    {
423
    boolean changed = false;
424
    int num = mJobs.size();
425
    Job job;
426

    
427
    for(int i=0; i<num; i++)
428
      {
429
      job = mJobs.remove(0);
430

    
431
      switch(job.type)
432
        {
433
        case CREATE: int max = InternalStackFrameList.getMax(mIndex);
434
                     if( max>0 )
435
                       {
436
                       mEffects       = new Effect[max];
437
                       mFloatUniforms = new float[max*job.num1];
438
                       mIntUniforms   = new int[max*job.num2];
439
                       }
440
                     mCreated = true;
441

    
442
                     break;
443
        case ATTACH: if( InternalStackFrameList.getMax(mIndex)>mNumEffects ) // it is possible that we have first
444
                       {                                                     // added effects and then lowered mMax
445
                       int position = job.num1;
446

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

    
483
                     mNumEffects= 0;
484
                     break;
485
        }
486
      }
487

    
488
    if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
489
    }
490
  }
(1-1/5)