Project

General

Profile

Download (8.57 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 7602a827

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of DistortedLibrary.                                                               //
5
//                                                                                               //
6
// DistortedLibrary 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
// DistortedLibrary 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 DistortedLibrary.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.effectqueue;
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.main.DistortedLibrary;
31
import org.distorted.library.main.DistortedFramebuffer;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.InternalOutputSurface;
34
import org.distorted.library.main.InternalRenderState;
35
import org.distorted.library.main.InternalSurface;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.message.EffectMessage;
38
import org.distorted.library.message.EffectMessageSender;
39
import org.distorted.library.program.DistortedProgram;
40

    
41
import java.io.InputStream;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
/**
45
 * Not part of public API, do not document
46
 *
47
 * @y.exclude
48
 */
49
public class EffectQueuePostprocess extends EffectQueue
50
  {
51
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
52
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
53

    
54
  private int mHalo;
55
  private float mR, mG, mB, mA;
56

    
57
  private static DistortedProgram mPreProgram;
58
  private static int mPreColorH;
59
  private static int mPreTextureH;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  EffectQueuePostprocess(long id)
64
    { 
65
    super(id,NUM_UNIFORMS,INDEX );
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void compute(long currTime)
71
    {
72
    if( currTime==mTime ) return;
73
    if( mTime==0 ) mTime = currTime;
74
    long step = (currTime-mTime);
75

    
76
    mR = mG = mB = mA = 0.0f;
77
    mHalo = 0;
78
    int halo;
79

    
80
    for(int i=0; i<mNumEffects; i++)
81
      {
82
      mCurrentDuration[i] += step;
83

    
84
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
85
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
86
      // that (see preprocess())
87
      mUniforms[NUM_UNIFORMS*i+4]=0.0f;
88

    
89
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
90
        {
91
        for(int j=0; j<mNumListeners; j++)
92
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
93
        }
94

    
95
      halo = (int)mUniforms[NUM_UNIFORMS*i];
96
      if( halo>mHalo ) mHalo = halo;
97
      }
98

    
99
    // TODO  (now only really works in case of 1 effect!)
100
    if( mNumEffects>0 )
101
      {
102
      mR = mUniforms[1];
103
      mG = mUniforms[2];
104
      mB = mUniforms[3];
105
      mA = mUniforms[4];
106
      }
107

    
108
    mTime = currTime;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  public static void createPrograms(Resources resources)
114
    {
115
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
116
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
117

    
118
    int numV = VertexEffect.getNumEnabled();
119

    
120
    String mainVertHeader= DistortedLibrary.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
121
    String mainFragHeader= DistortedLibrary.GLSL_VERSION + "\n";
122

    
123
    String enabledEffectV= VertexEffect.getGLSL();
124

    
125
    try
126
      {
127
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
128
                                         enabledEffectV, null, DistortedLibrary.GLSL, null);
129
      }
130
    catch(Exception e)
131
      {
132
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
133
      throw new RuntimeException(e.getMessage());
134
      }
135

    
136
    int preProgramH = mPreProgram.getProgramHandle();
137
    EffectQueueVertex.getUniforms( preProgramH,2 );
138
    EffectQueueMatrix.getUniforms( preProgramH,2 );
139
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
140
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
// TODO  (now only really works in case of 1 effect!)
145

    
146
  public int getQuality()
147
    {
148
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// TODO  (now only really works in case of 1 effect!)
153

    
154
  public boolean getRender()
155
    {
156
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
162
    {
163
    buffer.setAsOutput();
164
    InternalSurface input = node.getInternalSurface();
165

    
166
    if( input.setAsInput() )
167
      {
168
      MeshBase mesh = node.getMesh();
169

    
170
      float halfW = input.getWidth() / 2.0f;
171
      float halfH = input.getHeight()/ 2.0f;
172
      float halfZ = halfW*mesh.getZFactor();
173

    
174
      int width = buffer.getWidth();
175
      int height = buffer.getHeight();
176

    
177
      InternalRenderState.setUpStencilMark(mA!=0.0f);
178
      InternalRenderState.disableBlending();
179

    
180
      GLES31.glViewport(0, 0, width, height );
181

    
182
      mPreProgram.useProgram();
183

    
184
      mesh.bindVertexAttribs(mPreProgram);
185

    
186
      EffectQueue[] queues = node.getEffects().getQueues();
187
      EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
188
      EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
189

    
190
      float inflate=0.0f;
191

    
192
      matrix.send(width, height, distance, mipmap, projection, halfW, halfH, halfZ, 2);
193

    
194
      if( mHalo!=0.0f )
195
        {
196
        inflate = matrix.magnify(projection, width, height, mipmap, halfW, halfH, halfZ, mHalo);
197
        }
198

    
199
      vertex.send(inflate,2);
200

    
201
      if( mA!=0.0f )
202
        {
203
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
204
        GLES31.glUniform1i(mPreTextureH, 0);
205
        }
206

    
207
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
208

    
209
      InternalRenderState.restoreBlending();
210
      InternalRenderState.unsetUpStencilMark();
211

    
212
      return 1;
213
      }
214
    return 0;
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
  public int postprocess(DistortedFramebuffer buffer)
220
    {
221
    int numRenders = 0;
222

    
223
    GLES31.glDisable(GLES31.GL_BLEND);
224

    
225
    for(int i=0; i<mNumEffects; i++)
226
      {
227
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
228
      }
229

    
230
    GLES31.glEnable(GLES31.GL_BLEND);
231

    
232
    return numRenders;
233
    }
234
  }
(4-4/5)