Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueuePostprocess.java @ bfe45b4a

1 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 4c1dd6e9 Leszek Koltunski
22 a13dde77 Leszek Koltunski
import android.content.res.Resources;
23 8dccc3c2 Leszek Koltunski
import android.opengl.GLES31;
24 a13dde77 Leszek Koltunski
import android.util.Log;
25 8dccc3c2 Leszek Koltunski
26 a13dde77 Leszek Koltunski
import org.distorted.library.R;
27 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectType;
28 fe82a979 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffect;
29 a13dde77 Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
30 715e7726 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
31 4c1dd6e9 Leszek Koltunski
import org.distorted.library.message.EffectMessage;
32 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
33 a13dde77 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
34
35
import java.io.InputStream;
36 8fa96e69 Leszek Koltunski
37 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39
class EffectQueuePostprocess extends EffectQueue
40
  {
41 15aa7d94 Leszek Koltunski
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
42 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
43 4c1dd6e9 Leszek Koltunski
44 fff1110e leszek
  private int mHalo;
45 9e771d06 Leszek Koltunski
  private float mR, mG, mB, mA;
46 a31dbc5c Leszek Koltunski
47 247d8225 Leszek Koltunski
  private static DistortedProgram mPreProgram;
48
  private static int mPreColorH;
49 9e771d06 Leszek Koltunski
  private static int mPreTextureH;
50 a13dde77 Leszek Koltunski
51 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
  EffectQueuePostprocess(long id)
54
    { 
55 15aa7d94 Leszek Koltunski
    super(id,NUM_UNIFORMS,INDEX );
56 4c1dd6e9 Leszek Koltunski
    }
57
58 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 1149be8f leszek
  void compute(long currTime)
61 4c1dd6e9 Leszek Koltunski
    {
62 15aa7d94 Leszek Koltunski
    if( currTime==mTime ) return;
63 4c1dd6e9 Leszek Koltunski
    if( mTime==0 ) mTime = currTime;
64
    long step = (currTime-mTime);
65 e02264ff leszek
66 9e771d06 Leszek Koltunski
    mR = mG = mB = mA = 0.0f;
67 fff1110e leszek
    mHalo = 0;
68
    int halo;
69
70 4c1dd6e9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
71
      {
72 3a70bd6d leszek
      mCurrentDuration[i] += step;
73
74 e49b26ba Leszek Koltunski
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
75
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
76
      // that (see preprocess())
77
      mUniforms[NUM_UNIFORMS*i+4]=0.0f;
78
79 15aa7d94 Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
80 4c1dd6e9 Leszek Koltunski
        {
81
        for(int j=0; j<mNumListeners; j++)
82 26a4e5f6 leszek
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
83 4c1dd6e9 Leszek Koltunski
        }
84 a31dbc5c Leszek Koltunski
85 fff1110e leszek
      halo = (int)mUniforms[NUM_UNIFORMS*i];
86
      if( halo>mHalo ) mHalo = halo;
87 4c1dd6e9 Leszek Koltunski
      }
88 e02264ff leszek
89 9e771d06 Leszek Koltunski
    // TODO  (now only really works in case of 1 effect!)
90
    if( mNumEffects>0 )
91
      {
92
      mR = mUniforms[1];
93
      mG = mUniforms[2];
94
      mB = mUniforms[3];
95
      mA = mUniforms[4];
96
      }
97
98 95c441a2 leszek
    mTime = currTime;
99 4c1dd6e9 Leszek Koltunski
    }
100
101 984dc935 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102 247d8225 Leszek Koltunski
103
  static void createPrograms(Resources resources)
104 a13dde77 Leszek Koltunski
    {
105
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
106
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
107
108
    int numV = VertexEffect.getNumEnabled();
109
110 bfe45b4a Leszek Koltunski
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? Distorted.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
111 a13dde77 Leszek Koltunski
    String mainFragHeader= Distorted.GLSL_VERSION + "\n";
112
113
    String enabledEffectV= VertexEffect.getGLSL();
114
115
    try
116
      {
117
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
118
                                         enabledEffectV, null, Distorted.GLSL, null);
119
      }
120
    catch(Exception e)
121
      {
122
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
123
      throw new RuntimeException(e.getMessage());
124
      }
125
126
    int preProgramH = mPreProgram.getProgramHandle();
127
    EffectQueueVertex.getUniforms( preProgramH,2 );
128
    EffectQueueMatrix.getUniforms( preProgramH,2 );
129 9e771d06 Leszek Koltunski
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
130
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
131 86d322b5 Leszek Koltunski
    }
132
133 7266d8ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 2386a081 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
135 7266d8ef Leszek Koltunski
136 9e771d06 Leszek Koltunski
  int getQuality()
137 7266d8ef Leszek Koltunski
    {
138 9e771d06 Leszek Koltunski
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
139 7266d8ef Leszek Koltunski
    }
140
141 2386a081 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
142 a13dde77 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
143 2386a081 Leszek Koltunski
144 9e771d06 Leszek Koltunski
  boolean getRender()
145 a13dde77 Leszek Koltunski
    {
146 9e771d06 Leszek Koltunski
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
147 a13dde77 Leszek Koltunski
    }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 9e771d06 Leszek Koltunski
  int preprocess(DistortedOutputSurface buffer, DistortedNode node)
152 2386a081 Leszek Koltunski
    {
153 9e771d06 Leszek Koltunski
    buffer.setAsOutput();
154 a13dde77 Leszek Koltunski
    DistortedSurface input = node.getInternalSurface();
155 2386a081 Leszek Koltunski
156 a13dde77 Leszek Koltunski
    if( input.setAsInput() )
157
      {
158 715e7726 Leszek Koltunski
      MeshBase mesh = node.getMesh();
159 2386a081 Leszek Koltunski
160 a13dde77 Leszek Koltunski
      float halfW = input.getWidth() / 2.0f;
161
      float halfH = input.getHeight()/ 2.0f;
162 6c00149d Leszek Koltunski
      float halfZ = halfW*mesh.getZFactor();
163 2386a081 Leszek Koltunski
164 9e771d06 Leszek Koltunski
      DistortedRenderState.setUpStencilMark(mA!=0.0f);
165 977756d7 Leszek Koltunski
      DistortedRenderState.disableBlending();
166 a13dde77 Leszek Koltunski
167 9e771d06 Leszek Koltunski
      GLES31.glViewport(0, 0, buffer.mWidth, buffer.mHeight );
168 a13dde77 Leszek Koltunski
169
      mPreProgram.useProgram();
170
171 da681e7e Leszek Koltunski
      mesh.bindVertexAttribs(mPreProgram);
172 a13dde77 Leszek Koltunski
173 a0397f32 Leszek Koltunski
      EffectQueue[] queues = node.getEffects().getQueues();
174
      EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
175
      EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
176 84d51487 Leszek Koltunski
177
      float inflate=0.0f;
178
179
      matrix.send(buffer,halfW,halfH,halfZ,2);
180
181
      if( mHalo!=0.0f )
182
        {
183
        inflate = matrix.magnify(buffer,halfW,halfH,halfZ,mHalo);
184
        }
185
186
      vertex.send(inflate,2);
187 247d8225 Leszek Koltunski
188 9e771d06 Leszek Koltunski
      if( mA!=0.0f )
189 247d8225 Leszek Koltunski
        {
190 9e771d06 Leszek Koltunski
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
191
        GLES31.glUniform1i(mPreTextureH, 0);
192 247d8225 Leszek Koltunski
        }
193
194 6c00149d Leszek Koltunski
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
195 a13dde77 Leszek Koltunski
196 977756d7 Leszek Koltunski
      DistortedRenderState.restoreBlending();
197 a13dde77 Leszek Koltunski
      DistortedRenderState.unsetUpStencilMark();
198
199
      return 1;
200
      }
201
    return 0;
202 2386a081 Leszek Koltunski
    }
203
204 4b9fe2e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205 cf7394cc leszek
206 9e771d06 Leszek Koltunski
  int postprocess(DistortedFramebuffer buffer)
207 4b9fe2e9 Leszek Koltunski
    {
208 bed13bea leszek
    int numRenders = 0;
209
210 8dccc3c2 Leszek Koltunski
    GLES31.glDisable(GLES31.GL_BLEND);
211
212 1149be8f leszek
    for(int i=0; i<mNumEffects; i++)
213 95c441a2 leszek
      {
214 9e771d06 Leszek Koltunski
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
215 95c441a2 leszek
      }
216 4b9fe2e9 Leszek Koltunski
217 8dccc3c2 Leszek Koltunski
    GLES31.glEnable(GLES31.GL_BLEND);
218
219 bed13bea leszek
    return numRenders;
220 cf7394cc leszek
    }
221 4c1dd6e9 Leszek Koltunski
  }