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
|
long[] mCurrentDuration;
|
52
|
Effect[] mEffects;
|
53
|
int[] mName;
|
54
|
int[] mAssociation;
|
55
|
int[] mComponent;
|
56
|
long mTime;
|
57
|
|
58
|
private static int[] mMax = new int[EffectType.LENGTH];
|
59
|
private static long mNextID;
|
60
|
private static HashMap<ArrayList<Long>,Long> mMapID = new HashMap<>(); // maps lists of Effect IDs (longs) to a
|
61
|
// single long - the queue ID.
|
62
|
private long mID;
|
63
|
private int mIndex;
|
64
|
private boolean mCreated;
|
65
|
|
66
|
private static class Job
|
67
|
{
|
68
|
int type;
|
69
|
int num;
|
70
|
boolean notify;
|
71
|
Effect effect;
|
72
|
|
73
|
Job(int t, int m, boolean n, Effect e)
|
74
|
{
|
75
|
type = t;
|
76
|
num = m;
|
77
|
notify= n;
|
78
|
effect= e;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
83
|
|
84
|
static
|
85
|
{
|
86
|
onDestroy();
|
87
|
}
|
88
|
|
89
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
90
|
|
91
|
EffectQueue(int numUniforms, int index)
|
92
|
{
|
93
|
mCreated = false;
|
94
|
mTime = 0;
|
95
|
mID = 0;
|
96
|
mNumEffects = 0;
|
97
|
mNumEffectsToBe = 0;
|
98
|
mIndex = index;
|
99
|
mNumUniforms = numUniforms;
|
100
|
|
101
|
mJobs.add(new Job(CREATE,numUniforms,false,null)); // create the stuff that depends on max number
|
102
|
InternalMaster.newSlave(this); // of uniforms later, on first render.
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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
|
mCurrentDuration = new long[max];
|
144
|
mEffects = new Effect[max];
|
145
|
mName = new int[max];
|
146
|
mAssociation = new int[max];
|
147
|
mComponent = new int[max];
|
148
|
}
|
149
|
|
150
|
for(int i=0; i<mNumEffects; i++ )
|
151
|
{
|
152
|
mEffects[i] = source.mEffects[i];
|
153
|
mCurrentDuration[i] = source.mCurrentDuration[i];
|
154
|
mName[i] = source.mName[i];
|
155
|
mAssociation[i] = source.mAssociation[i];
|
156
|
mComponent[i] = source.mComponent[i];
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
|
161
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
162
|
|
163
|
public static void allocateQueues(EffectQueue[] queues, EffectQueue[] from, int flags)
|
164
|
{
|
165
|
queues[0] = (flags & DistortedLibrary.CLONE_MATRIX ) != 0 ? from[0] : new EffectQueueMatrix();
|
166
|
queues[1] = (flags & DistortedLibrary.CLONE_VERTEX ) != 0 ? from[1] : new EffectQueueVertex();
|
167
|
queues[2] = (flags & DistortedLibrary.CLONE_FRAGMENT ) != 0 ? from[2] : new EffectQueueFragment();
|
168
|
queues[3] = (flags & DistortedLibrary.CLONE_POSTPROCESS) != 0 ? from[3] : new EffectQueuePostprocess();
|
169
|
}
|
170
|
|
171
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
172
|
|
173
|
public static void compute(EffectQueue[] queues, long currTime)
|
174
|
{
|
175
|
((EffectQueueMatrix )queues[0]).compute(currTime);
|
176
|
((EffectQueueVertex )queues[1]).compute(currTime);
|
177
|
((EffectQueueFragment )queues[2]).compute(currTime);
|
178
|
((EffectQueuePostprocess)queues[3]).compute(currTime);
|
179
|
}
|
180
|
|
181
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182
|
|
183
|
public static void send(EffectQueue[] queues, float distance, float mipmap,
|
184
|
float[] projection, float inflate, MeshBase mesh, int variant )
|
185
|
{
|
186
|
((EffectQueueMatrix )queues[0]).send(distance, mipmap, projection, mesh, variant);
|
187
|
((EffectQueueVertex )queues[1]).send(inflate, variant);
|
188
|
((EffectQueueFragment)queues[2]).send(variant);
|
189
|
}
|
190
|
|
191
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
192
|
|
193
|
public static float[] getMVP(EffectQueue[] queues)
|
194
|
{
|
195
|
return ((EffectQueueMatrix)queues[0]).getMVP();
|
196
|
}
|
197
|
|
198
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
199
|
// variant: 0 --> MAIN 1 --> OIT 2 --> prePOST 3 --> FULL
|
200
|
|
201
|
public static void getUniforms(int programH, int variant)
|
202
|
{
|
203
|
EffectQueueFragment.uniforms(programH,variant);
|
204
|
EffectQueueVertex .uniforms(programH,variant);
|
205
|
EffectQueueMatrix .uniforms(programH,variant);
|
206
|
}
|
207
|
|
208
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209
|
// Every effect queue has an ID, which should be the same iff two queues hold the same effects.
|
210
|
// (this is a speedup: then both queues can be applied once, which seriously speeds up stuff -
|
211
|
// especially important in case of postprocessing)
|
212
|
|
213
|
private void regenerateID()
|
214
|
{
|
215
|
if( mNumEffects>0 )
|
216
|
{
|
217
|
ArrayList<Long> list = new ArrayList<>();
|
218
|
for (int i = 0; i < mNumEffects; i++) list.add(mEffects[i].getID());
|
219
|
Long id = mMapID.get(list);
|
220
|
|
221
|
if( id!=null )
|
222
|
{
|
223
|
mID = id;
|
224
|
}
|
225
|
else
|
226
|
{
|
227
|
mMapID.put(list,mNextID);
|
228
|
mID = mNextID++;
|
229
|
}
|
230
|
}
|
231
|
else
|
232
|
{
|
233
|
mID = 0;
|
234
|
}
|
235
|
}
|
236
|
|
237
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238
|
|
239
|
public long getID()
|
240
|
{
|
241
|
return mID;
|
242
|
}
|
243
|
|
244
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
245
|
|
246
|
public static boolean setMax(int index, int m)
|
247
|
{
|
248
|
if( !DistortedLibrary.isInitialized() || m<=mMax[index] )
|
249
|
{
|
250
|
mMax[index] = m<0 ? 0:m;
|
251
|
return true;
|
252
|
}
|
253
|
|
254
|
return false;
|
255
|
}
|
256
|
|
257
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
258
|
|
259
|
public static int getMax(int index)
|
260
|
{
|
261
|
return mMax[index];
|
262
|
}
|
263
|
|
264
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
265
|
|
266
|
public static void onDestroy()
|
267
|
{
|
268
|
mNextID = 1;
|
269
|
mMapID.clear();
|
270
|
EffectType.reset(mMax);
|
271
|
}
|
272
|
|
273
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
274
|
// this assumes 0<=effect<mNumEffects
|
275
|
|
276
|
protected void remove(int effect)
|
277
|
{
|
278
|
mNumEffects--;
|
279
|
|
280
|
for(int j=effect; j<mNumEffects; j++ )
|
281
|
{
|
282
|
mEffects[j] = mEffects[j+1];
|
283
|
mCurrentDuration[j] = mCurrentDuration[j+1];
|
284
|
mName[j] = mName[j+1];
|
285
|
mAssociation[j] = mAssociation[j+1];
|
286
|
mComponent[j] = mComponent[j+1];
|
287
|
}
|
288
|
|
289
|
mEffects[mNumEffects] = null;
|
290
|
}
|
291
|
|
292
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
293
|
|
294
|
public synchronized int removeByName(EffectName name)
|
295
|
{
|
296
|
int ret = 0;
|
297
|
|
298
|
for(int i=0; i<mNumEffects; i++)
|
299
|
{
|
300
|
if( mEffects[i].getName() == name )
|
301
|
{
|
302
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
303
|
ret++;
|
304
|
}
|
305
|
}
|
306
|
|
307
|
if( ret>0 )
|
308
|
{
|
309
|
InternalMaster.newSlave(this);
|
310
|
mNumEffectsToBe-=ret;
|
311
|
}
|
312
|
|
313
|
return ret;
|
314
|
}
|
315
|
|
316
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
317
|
|
318
|
public synchronized int removeById(long id)
|
319
|
{
|
320
|
for(int i=0; i<mNumEffects; i++)
|
321
|
{
|
322
|
if( mEffects[i].getID() == id )
|
323
|
{
|
324
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
325
|
InternalMaster.newSlave(this);
|
326
|
mNumEffectsToBe--;
|
327
|
return 1;
|
328
|
}
|
329
|
}
|
330
|
|
331
|
return 0;
|
332
|
}
|
333
|
|
334
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
335
|
|
336
|
public synchronized int removeEffect(Effect effect)
|
337
|
{
|
338
|
for(int i=0; i<mNumEffects; i++)
|
339
|
{
|
340
|
if( mEffects[i]==effect )
|
341
|
{
|
342
|
mJobs.add(new Job(DETACH,0,true,mEffects[i]));
|
343
|
InternalMaster.newSlave(this);
|
344
|
mNumEffectsToBe--;
|
345
|
return 1;
|
346
|
}
|
347
|
}
|
348
|
|
349
|
return 0;
|
350
|
}
|
351
|
|
352
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
353
|
// we do want to notify Listeners if they called 'abortAll' themselves but don't want to notify
|
354
|
// them if it is the library itself which is releasing resources.
|
355
|
|
356
|
public synchronized int removeAll(boolean notify)
|
357
|
{
|
358
|
mJobs.add(new Job(DETALL,0,notify,null));
|
359
|
InternalMaster.newSlave(this);
|
360
|
mNumEffectsToBe = 0;
|
361
|
return mNumEffects;
|
362
|
}
|
363
|
|
364
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
365
|
|
366
|
public boolean add(Effect effect)
|
367
|
{
|
368
|
if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
|
369
|
{
|
370
|
mJobs.add(new Job(ATTACH,-1,false,effect));
|
371
|
InternalMaster.newSlave(this);
|
372
|
mNumEffectsToBe++;
|
373
|
return true;
|
374
|
}
|
375
|
|
376
|
return false;
|
377
|
}
|
378
|
|
379
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
380
|
|
381
|
public boolean add(Effect effect, int position)
|
382
|
{
|
383
|
if( mMax[mIndex]>mNumEffectsToBe || !mCreated )
|
384
|
{
|
385
|
mJobs.add(new Job(ATTACH,position,false,effect));
|
386
|
InternalMaster.newSlave(this);
|
387
|
mNumEffectsToBe++;
|
388
|
return true;
|
389
|
}
|
390
|
|
391
|
return false;
|
392
|
}
|
393
|
|
394
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
395
|
|
396
|
public int getNumEffects()
|
397
|
{
|
398
|
return mNumEffects;
|
399
|
}
|
400
|
|
401
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
402
|
|
403
|
public Effect getEffect(int position)
|
404
|
{
|
405
|
if( position>=0 && position< mNumEffects )
|
406
|
{
|
407
|
return mEffects[position];
|
408
|
}
|
409
|
else
|
410
|
{
|
411
|
android.util.Log.e("queue", "getEffect: out of range "+position);
|
412
|
return null;
|
413
|
}
|
414
|
}
|
415
|
|
416
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
417
|
|
418
|
public void doWork()
|
419
|
{
|
420
|
boolean changed = false;
|
421
|
int num = mJobs.size();
|
422
|
Job job;
|
423
|
|
424
|
for(int i=0; i<num; i++)
|
425
|
{
|
426
|
job = mJobs.remove(0);
|
427
|
|
428
|
switch(job.type)
|
429
|
{
|
430
|
case CREATE: int max = mMax[mIndex];
|
431
|
if( max>0 )
|
432
|
{
|
433
|
mUniforms = new float[max*job.num];
|
434
|
mCurrentDuration = new long[max];
|
435
|
mEffects = new Effect[max];
|
436
|
mName = new int[max];
|
437
|
mAssociation = new int[max];
|
438
|
mComponent = new int[max];
|
439
|
}
|
440
|
mCreated = true;
|
441
|
|
442
|
break;
|
443
|
case ATTACH: if( mMax[mIndex]>mNumEffects ) // it is possible that we have first
|
444
|
{ // added effects and then lowered mMax
|
445
|
int position = job.num;
|
446
|
|
447
|
if( position==-1 )
|
448
|
{
|
449
|
mCurrentDuration[mNumEffects] = 0;
|
450
|
mEffects[mNumEffects] = job.effect;
|
451
|
mName[mNumEffects] = job.effect.getName().ordinal();
|
452
|
mAssociation[mNumEffects]= job.effect.getAssociation();
|
453
|
mComponent[mNumEffects] = job.effect.getComponent();
|
454
|
|
455
|
mNumEffects++;
|
456
|
changed = true;
|
457
|
}
|
458
|
else if( position>=0 && position<=mNumEffects )
|
459
|
{
|
460
|
for(int j=mNumEffects; j>position; j--)
|
461
|
{
|
462
|
mCurrentDuration[j] = mCurrentDuration[j-1];
|
463
|
mEffects[j] = mEffects[j-1];
|
464
|
mName[j] = mName[j-1];
|
465
|
mAssociation[j] = mAssociation[j-1];
|
466
|
mComponent[j] = mComponent[j-1];
|
467
|
}
|
468
|
|
469
|
mCurrentDuration[position] = 0;
|
470
|
mEffects[position] = job.effect;
|
471
|
mName[position] = job.effect.getName().ordinal();
|
472
|
mAssociation[position]= job.effect.getAssociation();
|
473
|
mComponent[position] = job.effect.getComponent();
|
474
|
|
475
|
mNumEffects++;
|
476
|
changed = true;
|
477
|
}
|
478
|
}
|
479
|
else
|
480
|
{
|
481
|
android.util.Log.e("queue", "failed to add effect "+job.effect.getName());
|
482
|
}
|
483
|
break;
|
484
|
case DETACH: for(int j=0; j<mNumEffects; j++)
|
485
|
{
|
486
|
if (mEffects[j] == job.effect)
|
487
|
{
|
488
|
remove(j);
|
489
|
changed = true;
|
490
|
break;
|
491
|
}
|
492
|
}
|
493
|
break;
|
494
|
case DETALL: for(int j=0; j<mNumEffects; j++ )
|
495
|
{
|
496
|
changed = true;
|
497
|
mEffects[j] = null;
|
498
|
}
|
499
|
|
500
|
mNumEffects= 0;
|
501
|
break;
|
502
|
}
|
503
|
}
|
504
|
|
505
|
if( changed && mIndex==EffectType.POSTPROCESS.ordinal() ) regenerateID();
|
506
|
}
|
507
|
}
|