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.main;
|
21
|
|
22
|
import android.content.res.Resources;
|
23
|
import android.opengl.GLES31;
|
24
|
import android.util.Log;
|
25
|
|
26
|
import org.distorted.library.R;
|
27
|
import org.distorted.library.effect.EffectType;
|
28
|
import org.distorted.library.effect.PostprocessEffect;
|
29
|
import org.distorted.library.effect.VertexEffect;
|
30
|
import org.distorted.library.mesh.MeshBase;
|
31
|
import org.distorted.library.message.EffectMessage;
|
32
|
import org.distorted.library.program.DistortedProgram;
|
33
|
|
34
|
import java.io.InputStream;
|
35
|
|
36
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
37
|
|
38
|
class EffectQueuePostprocess extends EffectQueue
|
39
|
{
|
40
|
private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
|
41
|
private static final int INDEX = EffectType.POSTPROCESS.ordinal();
|
42
|
|
43
|
private int mHalo;
|
44
|
private float mR, mG, mB, mA;
|
45
|
|
46
|
private static DistortedProgram mPreProgram;
|
47
|
private static int mPreColorH;
|
48
|
private static int mPreTextureH;
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
EffectQueuePostprocess(long id)
|
53
|
{
|
54
|
super(id,NUM_UNIFORMS,INDEX );
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
void compute(long currTime)
|
60
|
{
|
61
|
if( currTime==mTime ) return;
|
62
|
if( mTime==0 ) mTime = currTime;
|
63
|
long step = (currTime-mTime);
|
64
|
|
65
|
mR = mG = mB = mA = 0.0f;
|
66
|
mHalo = 0;
|
67
|
int halo;
|
68
|
|
69
|
for(int i=0; i<mNumEffects; i++)
|
70
|
{
|
71
|
mCurrentDuration[i] += step;
|
72
|
|
73
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
74
|
{
|
75
|
for(int j=0; j<mNumListeners; j++)
|
76
|
EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
|
77
|
|
78
|
if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
|
79
|
{
|
80
|
remove(i--);
|
81
|
mNumEffectsToBe--;
|
82
|
regenerateIDandSort();
|
83
|
continue;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
halo = (int)mUniforms[NUM_UNIFORMS*i];
|
88
|
if( halo>mHalo ) mHalo = halo;
|
89
|
}
|
90
|
|
91
|
// TODO (now only really works in case of 1 effect!)
|
92
|
if( mNumEffects>0 )
|
93
|
{
|
94
|
mR = mUniforms[1];
|
95
|
mG = mUniforms[2];
|
96
|
mB = mUniforms[3];
|
97
|
mA = mUniforms[4];
|
98
|
}
|
99
|
|
100
|
mTime = currTime;
|
101
|
}
|
102
|
|
103
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
104
|
|
105
|
static void createPrograms(Resources resources)
|
106
|
{
|
107
|
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
108
|
final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
|
109
|
|
110
|
int numV = VertexEffect.getNumEnabled();
|
111
|
|
112
|
String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? DistortedEffects.getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
113
|
String mainFragHeader= Distorted.GLSL_VERSION + "\n";
|
114
|
|
115
|
String enabledEffectV= VertexEffect.getGLSL();
|
116
|
|
117
|
try
|
118
|
{
|
119
|
mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
120
|
enabledEffectV, null, Distorted.GLSL, null);
|
121
|
}
|
122
|
catch(Exception e)
|
123
|
{
|
124
|
Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
|
125
|
throw new RuntimeException(e.getMessage());
|
126
|
}
|
127
|
|
128
|
int preProgramH = mPreProgram.getProgramHandle();
|
129
|
EffectQueueVertex.getUniforms( preProgramH,2 );
|
130
|
EffectQueueMatrix.getUniforms( preProgramH,2 );
|
131
|
mPreColorH = GLES31.glGetUniformLocation( preProgramH, "u_Color" );
|
132
|
mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
// TODO (now only really works in case of 1 effect!)
|
137
|
|
138
|
int getQuality()
|
139
|
{
|
140
|
return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
// TODO (now only really works in case of 1 effect!)
|
145
|
|
146
|
boolean getRender()
|
147
|
{
|
148
|
return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
int preprocess(DistortedOutputSurface buffer, DistortedNode node)
|
154
|
{
|
155
|
buffer.setAsOutput();
|
156
|
DistortedSurface input = node.getInternalSurface();
|
157
|
|
158
|
if( input.setAsInput() )
|
159
|
{
|
160
|
MeshBase mesh = node.getMesh();
|
161
|
float margin = mHalo*buffer.mMipmap;
|
162
|
|
163
|
float halfW = input.getWidth() / 2.0f;
|
164
|
float halfH = input.getHeight()/ 2.0f;
|
165
|
float halfZ = halfW*mesh.getZFactor();
|
166
|
|
167
|
DistortedRenderState.setUpStencilMark(mA!=0.0f);
|
168
|
|
169
|
GLES31.glViewport(0, 0, buffer.mWidth, buffer.mHeight );
|
170
|
|
171
|
mPreProgram.useProgram();
|
172
|
|
173
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
|
174
|
GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshBase.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET0);
|
175
|
GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshBase.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET1);
|
176
|
GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshBase.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshBase.VERTSIZE, MeshBase.OFFSET2);
|
177
|
GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
|
178
|
|
179
|
node.getEffects().send(halfW, halfH, halfZ, margin, buffer, 2);
|
180
|
|
181
|
if( mA!=0.0f )
|
182
|
{
|
183
|
GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
|
184
|
GLES31.glUniform1i(mPreTextureH, 0);
|
185
|
}
|
186
|
|
187
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
188
|
|
189
|
DistortedRenderState.unsetUpStencilMark();
|
190
|
|
191
|
return 1;
|
192
|
}
|
193
|
return 0;
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
|
198
|
int postprocess(DistortedFramebuffer buffer)
|
199
|
{
|
200
|
int numRenders = 0;
|
201
|
|
202
|
GLES31.glDisable(GLES31.GL_BLEND);
|
203
|
|
204
|
for(int i=0; i<mNumEffects; i++)
|
205
|
{
|
206
|
numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
|
207
|
}
|
208
|
|
209
|
GLES31.glEnable(GLES31.GL_BLEND);
|
210
|
|
211
|
return numRenders;
|
212
|
}
|
213
|
}
|