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