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