1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski leszek@koltunski.pl //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// This library is free software; you can redistribute it and/or //
|
7
|
// modify it under the terms of the GNU Lesser General Public //
|
8
|
// License as published by the Free Software Foundation; either //
|
9
|
// version 2.1 of the License, or (at your option) any later version. //
|
10
|
// //
|
11
|
// This library 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 GNU //
|
14
|
// Lesser General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU Lesser General Public //
|
17
|
// License along with this library; if not, write to the Free Software //
|
18
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA //
|
19
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
20
|
|
21
|
package org.distorted.library.effectqueue;
|
22
|
|
23
|
import android.opengl.GLES30;
|
24
|
|
25
|
import org.distorted.library.effect.EffectType;
|
26
|
import org.distorted.library.effect.PostprocessEffect;
|
27
|
import org.distorted.library.effect.VertexEffect;
|
28
|
import org.distorted.library.main.DistortedEffects;
|
29
|
import org.distorted.library.main.DistortedLibrary;
|
30
|
import org.distorted.library.main.DistortedFramebuffer;
|
31
|
import org.distorted.library.main.DistortedNode;
|
32
|
import org.distorted.library.main.InternalOutputSurface;
|
33
|
import org.distorted.library.main.InternalRenderState;
|
34
|
import org.distorted.library.mesh.MeshBase;
|
35
|
import org.distorted.library.message.EffectMessageSender;
|
36
|
import org.distorted.library.program.DistortedProgram;
|
37
|
|
38
|
import java.io.InputStream;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
/**
|
42
|
* Not part of public API, do not document
|
43
|
*
|
44
|
* @y.exclude
|
45
|
*/
|
46
|
public class EffectQueuePostprocess extends EffectQueue
|
47
|
{
|
48
|
private static final int NUM_FLOAT_UNIFORMS = PostprocessEffect.NUM_FLOAT_UNIFORMS;
|
49
|
private static final int NUM_INT_UNIFORMS = PostprocessEffect.NUM_INT_UNIFORMS;
|
50
|
private static final boolean USE_UBO = false;
|
51
|
private static final int INDEX = EffectType.POSTPROCESS.ordinal();
|
52
|
|
53
|
private int mHalo;
|
54
|
private boolean mUseHaloDepth;
|
55
|
private float mR, mG, mB, mA;
|
56
|
|
57
|
private static DistortedProgram mPreProgram;
|
58
|
private static int mPreColorH;
|
59
|
private static int mPreTextureH;
|
60
|
private static int mPreProgramH;
|
61
|
|
62
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
63
|
|
64
|
EffectQueuePostprocess()
|
65
|
{
|
66
|
super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, USE_UBO, INDEX );
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
EffectQueuePostprocess(EffectQueuePostprocess source)
|
72
|
{
|
73
|
super(source);
|
74
|
|
75
|
mHalo = source.mHalo;
|
76
|
mUseHaloDepth = source.mUseHaloDepth;
|
77
|
mR = source.mR;
|
78
|
mG = source.mG;
|
79
|
mB = source.mB;
|
80
|
mA = source.mA;
|
81
|
}
|
82
|
|
83
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84
|
|
85
|
void compute(long currTime, long step)
|
86
|
{
|
87
|
mR = mG = mB = mA = 0.0f;
|
88
|
mHalo = 0;
|
89
|
int halo;
|
90
|
float[] array = mUBF.getBackingArray();
|
91
|
|
92
|
for(int i=0; i<mNumEffects; i++)
|
93
|
{
|
94
|
// first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
|
95
|
// and if previously there was a GLOW effect here then mA would be non-zero and we don't want
|
96
|
// that (see preprocess())
|
97
|
array[NUM_FLOAT_UNIFORMS*i+5]=0.0f;
|
98
|
|
99
|
PostprocessEffect effect = (PostprocessEffect)mEffects[i];
|
100
|
|
101
|
if( effect.compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
|
102
|
{
|
103
|
EffectMessageSender.newMessage(mEffects[i]);
|
104
|
}
|
105
|
|
106
|
halo = (int)array[NUM_FLOAT_UNIFORMS*i];
|
107
|
if( halo>mHalo ) mHalo = halo;
|
108
|
|
109
|
// TODO (now only really works in case of 1 effect!)
|
110
|
mUseHaloDepth = effect.getHaloDepth();
|
111
|
}
|
112
|
|
113
|
// TODO (now only really works in case of 1 effect!)
|
114
|
if( mNumEffects>0 )
|
115
|
{
|
116
|
mR = array[2];
|
117
|
mG = array[3];
|
118
|
mB = array[4];
|
119
|
mA = array[5];
|
120
|
}
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
public static void createPrograms(InputStream vert, InputStream frag, int GLSL)
|
126
|
{
|
127
|
int numV = VertexEffect.getNumEnabled();
|
128
|
|
129
|
String version = "#version "+GLSL+" es\n";
|
130
|
String mainVertHeader= version + ("#define NUM_VERTEX " + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
131
|
String mainFragHeader= version + "\n";
|
132
|
|
133
|
mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
|
134
|
if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
|
135
|
|
136
|
String enabledEffectV= VertexEffect.getGLSL();
|
137
|
|
138
|
try
|
139
|
{
|
140
|
mPreProgram = new DistortedProgram(vert, frag, mainVertHeader, mainFragHeader,
|
141
|
enabledEffectV, null, GLSL, null);
|
142
|
}
|
143
|
catch(Exception e)
|
144
|
{
|
145
|
DistortedLibrary.logMessage("EffectQueuePostprocess "+ e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
|
146
|
throw new RuntimeException(e.getMessage());
|
147
|
}
|
148
|
|
149
|
mPreProgramH = mPreProgram.getProgramHandle();
|
150
|
EffectQueue.getUniforms( mPreProgramH,2 );
|
151
|
MeshBase.getUniforms( mPreProgramH,2 );
|
152
|
mPreColorH = GLES30.glGetUniformLocation( mPreProgramH, "u_Color" );
|
153
|
mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
// TODO (now only really works in case of 1 effect!)
|
158
|
|
159
|
public int getQuality()
|
160
|
{
|
161
|
return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
|
162
|
}
|
163
|
|
164
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
165
|
// TODO (now only really works in case of 1 effect!)
|
166
|
|
167
|
public boolean getRenderDirectly()
|
168
|
{
|
169
|
return mNumEffects>0 && ((PostprocessEffect) mEffects[0]).getRenderDirectly();
|
170
|
}
|
171
|
|
172
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
173
|
|
174
|
public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
|
175
|
{
|
176
|
MeshBase mesh = node.getMesh();
|
177
|
DistortedEffects effects = node.getEffects();
|
178
|
|
179
|
int width = buffer.getWidth();
|
180
|
int height = buffer.getHeight();
|
181
|
|
182
|
InternalRenderState.setUpStencilMark(mA!=0.0f);
|
183
|
InternalRenderState.disableBlending();
|
184
|
if( !mUseHaloDepth ) GLES30.glDepthMask(false);
|
185
|
|
186
|
GLES30.glViewport(0, 0, width, height );
|
187
|
|
188
|
mPreProgram.useProgram();
|
189
|
|
190
|
mesh.bindVertexAttribs(mPreProgram);
|
191
|
mesh.send(mPreProgramH,2);
|
192
|
|
193
|
EffectQueue[] queues = effects.getQueues();
|
194
|
EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
|
195
|
EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
|
196
|
|
197
|
matrix.send(distance, mipmap, projection, 2);
|
198
|
vertex.send(mHalo*0.01f,mPreProgramH,2);
|
199
|
|
200
|
if( mA!=0.0f )
|
201
|
{
|
202
|
GLES30.glUniform4f(mPreColorH, mR, mG, mB, mA);
|
203
|
GLES30.glUniform1i(mPreTextureH, 0);
|
204
|
}
|
205
|
|
206
|
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
207
|
mPreProgram.stopUsingProgram();
|
208
|
|
209
|
InternalRenderState.restoreBlending();
|
210
|
InternalRenderState.unsetUpStencilMark();
|
211
|
if( !mUseHaloDepth ) GLES30.glDepthMask(true);
|
212
|
|
213
|
return 1;
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
public int postprocess(DistortedFramebuffer buffer)
|
219
|
{
|
220
|
int numRenders = 0;
|
221
|
float[] array = mUBF.getBackingArray();
|
222
|
|
223
|
GLES30.glDisable(GLES30.GL_BLEND);
|
224
|
|
225
|
for(int i=0; i<mNumEffects; i++)
|
226
|
{
|
227
|
numRenders += ((PostprocessEffect)mEffects[i]).postprocess(array,NUM_FLOAT_UNIFORMS*i, buffer);
|
228
|
}
|
229
|
|
230
|
GLES30.glEnable(GLES30.GL_BLEND);
|
231
|
|
232
|
return numRenders;
|
233
|
}
|
234
|
}
|