1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2017 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.effect;
|
21
|
|
22
|
import org.distorted.library.main.DistortedFramebuffer;
|
23
|
import org.distorted.library.main.InternalMaster;
|
24
|
import org.distorted.library.program.DistortedProgram;
|
25
|
|
26
|
import java.lang.reflect.Method;
|
27
|
import java.nio.ByteBuffer;
|
28
|
import java.nio.ByteOrder;
|
29
|
import java.nio.FloatBuffer;
|
30
|
import java.util.ArrayList;
|
31
|
|
32
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
33
|
/**
|
34
|
* Abstract class that represents an Effect that works by running a certain Shader Program(s) on a Framebuffer.
|
35
|
*/
|
36
|
public abstract class PostprocessEffect extends Effect implements InternalMaster.Slave
|
37
|
{
|
38
|
private static final int MIPMAP = 0;
|
39
|
/**
|
40
|
* 5: 5 per-effect interpolated values.
|
41
|
*/
|
42
|
public static final int NUM_UNIFORMS = 5;
|
43
|
|
44
|
static final int POS_DATA_SIZE= 2;
|
45
|
static final int TEX_DATA_SIZE= 2;
|
46
|
|
47
|
static final FloatBuffer mQuadPositions, mQuadTexture, mQuadTextureInv;
|
48
|
|
49
|
static
|
50
|
{
|
51
|
int dataLength = 4;
|
52
|
int bytes_per_float = 4;
|
53
|
|
54
|
float[] position = { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f };
|
55
|
float[] textureNor= { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
|
56
|
float[] textureInv= { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f };
|
57
|
|
58
|
mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
59
|
mQuadPositions.put(position).position(0);
|
60
|
mQuadTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
61
|
mQuadTexture.put(textureNor).position(0);
|
62
|
mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
63
|
mQuadTextureInv.put(textureInv).position(0);
|
64
|
}
|
65
|
|
66
|
private static class Source
|
67
|
{
|
68
|
private String mName, mVertexShader, mFragmentShader;
|
69
|
|
70
|
Source(String name, String vertex, String fragment)
|
71
|
{
|
72
|
mName = name;
|
73
|
mVertexShader = vertex;
|
74
|
mFragmentShader = fragment;
|
75
|
}
|
76
|
}
|
77
|
|
78
|
static ArrayList<DistortedProgram> mPrograms = new ArrayList<>();
|
79
|
private static ArrayList<Source> mSources = new ArrayList<>();
|
80
|
private static int mNumSources = 0;
|
81
|
|
82
|
private class Job
|
83
|
{
|
84
|
int type;
|
85
|
int level;
|
86
|
|
87
|
Job(int t, int l)
|
88
|
{
|
89
|
type = t;
|
90
|
level= l;
|
91
|
}
|
92
|
}
|
93
|
|
94
|
private ArrayList<Job> mJobs = new ArrayList<>();
|
95
|
|
96
|
int mQualityLevel;
|
97
|
float mQualityScale;
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
static int register(final String name, final String vertexShader, final String fragmentShader)
|
102
|
{
|
103
|
mSources.add(new Source(name,vertexShader,fragmentShader));
|
104
|
|
105
|
return mNumSources++;
|
106
|
}
|
107
|
|
108
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
109
|
/**
|
110
|
* Only for use by the library itself.
|
111
|
*
|
112
|
* @y.exclude
|
113
|
*/
|
114
|
public static void createPrograms()
|
115
|
{
|
116
|
Source source;
|
117
|
int len = mSources.size();
|
118
|
|
119
|
for(int i=0; i<len; i++)
|
120
|
{
|
121
|
source = mSources.remove(0);
|
122
|
|
123
|
//android.util.Log.d("postprocess", "compiling: "+source.mName);
|
124
|
|
125
|
try
|
126
|
{
|
127
|
mPrograms.add (new DistortedProgram(source.mVertexShader,source.mFragmentShader));
|
128
|
}
|
129
|
catch(Exception e)
|
130
|
{
|
131
|
android.util.Log.e("Effects", "exception trying to compile "+source.mName+" program: "+e.getMessage());
|
132
|
throw new RuntimeException(e.getMessage());
|
133
|
}
|
134
|
}
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
/**
|
139
|
* Only for use by the library itself.
|
140
|
*
|
141
|
* @y.exclude
|
142
|
*/
|
143
|
public abstract int apply(float[] uniforms, int index, DistortedFramebuffer buffer);
|
144
|
|
145
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
146
|
/**
|
147
|
* Only for use by the library itself.
|
148
|
*
|
149
|
* @y.exclude
|
150
|
*/
|
151
|
public abstract boolean getRender();
|
152
|
|
153
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
154
|
/**
|
155
|
* Only for use by the library itself.
|
156
|
*
|
157
|
* @y.exclude
|
158
|
*/
|
159
|
public int getQuality()
|
160
|
{
|
161
|
return mQualityLevel;
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
/**
|
166
|
* This is not really part of the public API. Has to be public only because it is a part of the
|
167
|
* DistortedSlave interface, which should really be a class that we extend here instead but
|
168
|
* Java has no multiple inheritance.
|
169
|
*
|
170
|
* @y.exclude
|
171
|
*/
|
172
|
public void doWork()
|
173
|
{
|
174
|
int num = mJobs.size();
|
175
|
Job job;
|
176
|
|
177
|
for(int i=0; i<num; i++)
|
178
|
{
|
179
|
job = mJobs.remove(0);
|
180
|
|
181
|
switch(job.type)
|
182
|
{
|
183
|
case MIPMAP: int level = job.level;
|
184
|
mQualityLevel = level;
|
185
|
mQualityScale = 1.0f;
|
186
|
for(int j=0; j<level; j++) mQualityScale*= EffectQuality.MULTIPLIER;
|
187
|
break;
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
|
192
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
193
|
|
194
|
static void destroyStatics()
|
195
|
{
|
196
|
mPrograms.clear();
|
197
|
mSources.clear();
|
198
|
mNumSources = 0;
|
199
|
|
200
|
Method method;
|
201
|
|
202
|
for(EffectName name: EffectName.values())
|
203
|
{
|
204
|
if( name.getType() == EffectType.POSTPROCESS )
|
205
|
{
|
206
|
Class<? extends Effect> cls = name.getEffectClass();
|
207
|
|
208
|
try
|
209
|
{
|
210
|
method = cls.getDeclaredMethod("destroyStatics"); // destroyStatics not public, thus getDeclaredMethod
|
211
|
}
|
212
|
catch(NoSuchMethodException ex)
|
213
|
{
|
214
|
android.util.Log.e("postprocess", cls.getSimpleName()+": exception getting method: "+ex.getMessage());
|
215
|
method = null;
|
216
|
}
|
217
|
|
218
|
try
|
219
|
{
|
220
|
if( method!=null ) method.invoke(null);
|
221
|
}
|
222
|
catch(Exception ex)
|
223
|
{
|
224
|
android.util.Log.e("postprocess", "exception invoking method: "+ex.getMessage());
|
225
|
}
|
226
|
}
|
227
|
}
|
228
|
}
|
229
|
|
230
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
231
|
|
232
|
PostprocessEffect(EffectName name)
|
233
|
{
|
234
|
super(name);
|
235
|
|
236
|
mQualityLevel = 0;
|
237
|
mQualityScale = 1.0f;
|
238
|
}
|
239
|
|
240
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
241
|
// PUBLIC API
|
242
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
243
|
/**
|
244
|
* The higher the quality, the better the effect will look like and the slower it will be.
|
245
|
* <p>
|
246
|
* This works by rendering into smaller and smaller intermediate buffers. Each step renders into a
|
247
|
* buffer that's half the size of the previous one.
|
248
|
* <p>
|
249
|
* We cannot this during mid-render - thus, give it to the Master to assign us back this job on the
|
250
|
* next render.
|
251
|
*/
|
252
|
public void setQuality(EffectQuality quality)
|
253
|
{
|
254
|
mJobs.add(new Job(MIPMAP,quality.getLevel()));
|
255
|
InternalMaster.newSlave(this);
|
256
|
}
|
257
|
}
|