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