Project

General

Profile

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

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

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
import org.distorted.library.uniformblock.UniformBlockFloatUniforms;
29
import org.distorted.library.uniformblock.UniformBlockIntUniforms;
30

    
31
import java.util.ArrayList;
32
import java.util.HashMap;
33

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

    
44
  static final int VERT_INT_UBO_BINDING = 5;
45
  static final int VERT_FLO_UBO_BINDING = 6;
46
  static final int FRAG_INT_UBO_BINDING = 7;
47
  static final int FRAG_FLO_UBO_BINDING = 8;
48

    
49
  private static final int CREATE = 0;
50
  private static final int ATTACH = 1;
51
  private static final int DETACH = 2;
52
  private static final int DETALL = 3;
53

    
54
  long mTime;
55
  int mNumEffects;              // 'ToBe' will be more than mNumEffects if doWork() hasn't
56
  private int mNumEffectsToBe;  // added them yet (or less if it hasn't removed some yet)
57
  Effect[] mEffects;
58

    
59
  UniformBlockFloatUniforms mUBF;
60
  UniformBlockIntUniforms mUBI;
61

    
62
  private long mID;
63
  private final int mIndex;
64
  private boolean mCreated;
65

    
66
  private static class Job
67
    {
68
    int type;
69
    int num1, num2;
70
    boolean notify;
71
    Effect effect;
72

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

    
83
  private final ArrayList<Job> mJobs;
84

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

    
96
    mJobs = new ArrayList<>();
97

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

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

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

    
116
      mJobs = new ArrayList<>();
117

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

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

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

    
137
      mJobs = new ArrayList<>();
138

    
139
      int max = InternalStackFrameList.getMax(mIndex);
140

    
141
      if( max>0 )
142
        {
143
        mEffects= new Effect[max];
144
        mUBI  = new UniformBlockIntUniforms(source.mUBI);
145
        mUBF  = new UniformBlockFloatUniforms(source.mUBF);
146

    
147
        if( mNumEffects>=0 )
148
          {
149
          System.arraycopy(source.mEffects, 0, mEffects, 0, mNumEffects);
150
          }
151
        }
152
      }
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

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

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

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

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

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

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

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

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

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

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

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  private void addNow(int pos, Effect effect)
264
    {
265
    mEffects[pos] = effect;
266
    mUBI.addOrdinal(pos, effect.getName().ordinal() );
267
    effect.addQueue(this);
268

    
269
    if( mIndex==EffectType.VERTEX.ordinal() )
270
      {
271
      mUBI.addAssociations(pos,effect);
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
        mUBI.addAssociations(j,mEffects[j]);
415
        }
416
      }
417
    }
418

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

    
421
  public void markForDeletion()
422
    {
423
    mUBI.markForDeletion();
424
    mUBF.markForDeletion();
425
    }
426

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public void doWork()
430
    {
431
    boolean changed = false;
432
    int num = mJobs.size();
433
    Job job;
434

    
435
    for(int i=0; i<num; i++)
436
      {
437
      job = mJobs.remove(0);
438

    
439
      switch(job.type)
440
        {
441
        case CREATE: int max = InternalStackFrameList.getMax(mIndex);
442
                     if( max>0 )
443
                       {
444
                       mEffects= new Effect[max];
445
                       mUBF    = new UniformBlockFloatUniforms(job.num1, max);
446
                       mUBI    = new UniformBlockIntUniforms  (job.num2, max);
447
                       }
448
                     mCreated = true;
449

    
450
                     break;
451
        case ATTACH: if( InternalStackFrameList.getMax(mIndex)>mNumEffects ) // it is possible that we have first
452
                       {                                                     // added effects and then lowered mMax
453
                       int position = job.num1;
454

    
455
                       if( position<0 )
456
                         {
457
                         addNow(mNumEffects,job.effect);
458
                         mNumEffects++;
459
                         changed = true;
460
                         }
461
                       else if( position<=mNumEffects )
462
                         {
463
                         System.arraycopy(mEffects, position, mEffects, position+1, mNumEffects-position);
464
                         mUBI.makeHole(position, mNumEffects);
465
                         addNow(position,job.effect);
466
                         mNumEffects++;
467
                         changed = true;
468
                         }
469
                       }
470
                     else
471
                       {
472
                       android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
473
                       }
474
                     break;
475
        case DETACH: for(int j=0; j<mNumEffects; j++)
476
                       {
477
                       if (mEffects[j] == job.effect)
478
                         {
479
                         removeNow(j);
480
                         changed = true;
481
                         break;
482
                         }
483
                       }
484
                     break;
485
        case DETALL: for(int j=0; j<mNumEffects; j++ )
486
                       {
487
                       changed = true;
488
                       mEffects[j] = null;
489
                       }
490

    
491
                     mNumEffects= 0;
492
                     break;
493
        }
494
      }
495

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