Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ c90aca24

1 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 4c1dd6e9 Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 4c1dd6e9 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 4c1dd6e9 Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 809dcae3 Leszek Koltunski
package org.distorted.library.effectqueue;
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 c90aca24 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
31 7602a827 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
32 809dcae3 Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
33
import org.distorted.library.main.DistortedNode;
34 7602a827 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
35
import org.distorted.library.main.InternalRenderState;
36
import org.distorted.library.main.InternalSurface;
37 715e7726 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
38 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
39 a13dde77 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
40
41
import java.io.InputStream;
42 8fa96e69 Leszek Koltunski
43 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
44 809dcae3 Leszek Koltunski
/**
45
 * Not part of public API, do not document
46
 *
47
 * @y.exclude
48
 */
49
public class EffectQueuePostprocess extends EffectQueue
50 4c1dd6e9 Leszek Koltunski
  {
51 15aa7d94 Leszek Koltunski
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
52 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
53 4c1dd6e9 Leszek Koltunski
54 fff1110e leszek
  private int mHalo;
55 9e771d06 Leszek Koltunski
  private float mR, mG, mB, mA;
56 a31dbc5c Leszek Koltunski
57 247d8225 Leszek Koltunski
  private static DistortedProgram mPreProgram;
58
  private static int mPreColorH;
59 9e771d06 Leszek Koltunski
  private static int mPreTextureH;
60 a13dde77 Leszek Koltunski
61 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 20dbec0e Leszek Koltunski
  EffectQueuePostprocess()
64 4c1dd6e9 Leszek Koltunski
    { 
65 20dbec0e Leszek Koltunski
    super(NUM_UNIFORMS,INDEX );
66 4c1dd6e9 Leszek Koltunski
    }
67
68 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 1149be8f leszek
  void compute(long currTime)
71 4c1dd6e9 Leszek Koltunski
    {
72 15aa7d94 Leszek Koltunski
    if( currTime==mTime ) return;
73 4c1dd6e9 Leszek Koltunski
    if( mTime==0 ) mTime = currTime;
74
    long step = (currTime-mTime);
75 e02264ff leszek
76 9e771d06 Leszek Koltunski
    mR = mG = mB = mA = 0.0f;
77 fff1110e leszek
    mHalo = 0;
78
    int halo;
79
80 4c1dd6e9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
81
      {
82 3a70bd6d leszek
      mCurrentDuration[i] += step;
83
84 e49b26ba Leszek Koltunski
      // 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 15aa7d94 Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
90 4c1dd6e9 Leszek Koltunski
        {
91 20dbec0e Leszek Koltunski
        EffectMessageSender.newMessage(mEffects[i]);
92 4c1dd6e9 Leszek Koltunski
        }
93 a31dbc5c Leszek Koltunski
94 fff1110e leszek
      halo = (int)mUniforms[NUM_UNIFORMS*i];
95
      if( halo>mHalo ) mHalo = halo;
96 4c1dd6e9 Leszek Koltunski
      }
97 e02264ff leszek
98 9e771d06 Leszek Koltunski
    // TODO  (now only really works in case of 1 effect!)
99
    if( mNumEffects>0 )
100
      {
101
      mR = mUniforms[1];
102
      mG = mUniforms[2];
103
      mB = mUniforms[3];
104
      mA = mUniforms[4];
105
      }
106
107 95c441a2 leszek
    mTime = currTime;
108 4c1dd6e9 Leszek Koltunski
    }
109
110 984dc935 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111 247d8225 Leszek Koltunski
112 809dcae3 Leszek Koltunski
  public static void createPrograms(Resources resources)
113 a13dde77 Leszek Koltunski
    {
114
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
115
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
116
117
    int numV = VertexEffect.getNumEnabled();
118
119 7602a827 Leszek Koltunski
    String mainVertHeader= DistortedLibrary.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
120
    String mainFragHeader= DistortedLibrary.GLSL_VERSION + "\n";
121 a13dde77 Leszek Koltunski
122
    String enabledEffectV= VertexEffect.getGLSL();
123
124
    try
125
      {
126
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
127 7602a827 Leszek Koltunski
                                         enabledEffectV, null, DistortedLibrary.GLSL, null);
128 a13dde77 Leszek Koltunski
      }
129
    catch(Exception e)
130
      {
131
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
132
      throw new RuntimeException(e.getMessage());
133
      }
134
135
    int preProgramH = mPreProgram.getProgramHandle();
136
    EffectQueueVertex.getUniforms( preProgramH,2 );
137
    EffectQueueMatrix.getUniforms( preProgramH,2 );
138 9e771d06 Leszek Koltunski
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
139
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
140 86d322b5 Leszek Koltunski
    }
141
142 7266d8ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
143 2386a081 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
144 7266d8ef Leszek Koltunski
145 809dcae3 Leszek Koltunski
  public int getQuality()
146 7266d8ef Leszek Koltunski
    {
147 9e771d06 Leszek Koltunski
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
148 7266d8ef Leszek Koltunski
    }
149
150 2386a081 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151 a13dde77 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
152 2386a081 Leszek Koltunski
153 809dcae3 Leszek Koltunski
  public boolean getRender()
154 a13dde77 Leszek Koltunski
    {
155 9e771d06 Leszek Koltunski
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
156 a13dde77 Leszek Koltunski
    }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 7602a827 Leszek Koltunski
  public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
161 2386a081 Leszek Koltunski
    {
162 9e771d06 Leszek Koltunski
    buffer.setAsOutput();
163 eddf0cb7 Leszek Koltunski
    InternalSurface input = node.getSurface();
164 2386a081 Leszek Koltunski
165 a13dde77 Leszek Koltunski
    if( input.setAsInput() )
166
      {
167 715e7726 Leszek Koltunski
      MeshBase mesh = node.getMesh();
168 c90aca24 Leszek Koltunski
      DistortedEffects effects = node.getEffects();
169 2386a081 Leszek Koltunski
170 c90aca24 Leszek Koltunski
      float halfW = effects.getStartchX() / 2.0f;
171
      float halfH = effects.getStartchY() / 2.0f;
172
      float halfZ = effects.getStartchZ() / 2.0f;
173 2386a081 Leszek Koltunski
174 c90aca24 Leszek Koltunski
      int width   = buffer.getWidth();
175
      int height  = buffer.getHeight();
176 809dcae3 Leszek Koltunski
177 7602a827 Leszek Koltunski
      InternalRenderState.setUpStencilMark(mA!=0.0f);
178
      InternalRenderState.disableBlending();
179 a13dde77 Leszek Koltunski
180 809dcae3 Leszek Koltunski
      GLES31.glViewport(0, 0, width, height );
181 a13dde77 Leszek Koltunski
182
      mPreProgram.useProgram();
183
184 da681e7e Leszek Koltunski
      mesh.bindVertexAttribs(mPreProgram);
185 a13dde77 Leszek Koltunski
186 c90aca24 Leszek Koltunski
      EffectQueue[] queues = effects.getQueues();
187 a0397f32 Leszek Koltunski
      EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
188
      EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
189 84d51487 Leszek Koltunski
190
      float inflate=0.0f;
191
192 809dcae3 Leszek Koltunski
      matrix.send(width, height, distance, mipmap, projection, halfW, halfH, halfZ, 2);
193 84d51487 Leszek Koltunski
194
      if( mHalo!=0.0f )
195
        {
196 809dcae3 Leszek Koltunski
        inflate = matrix.magnify(projection, width, height, mipmap, halfW, halfH, halfZ, mHalo);
197 84d51487 Leszek Koltunski
        }
198
199
      vertex.send(inflate,2);
200 247d8225 Leszek Koltunski
201 9e771d06 Leszek Koltunski
      if( mA!=0.0f )
202 247d8225 Leszek Koltunski
        {
203 9e771d06 Leszek Koltunski
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
204
        GLES31.glUniform1i(mPreTextureH, 0);
205 247d8225 Leszek Koltunski
        }
206
207 6c00149d Leszek Koltunski
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
208 a13dde77 Leszek Koltunski
209 7602a827 Leszek Koltunski
      InternalRenderState.restoreBlending();
210
      InternalRenderState.unsetUpStencilMark();
211 a13dde77 Leszek Koltunski
212
      return 1;
213
      }
214
    return 0;
215 2386a081 Leszek Koltunski
    }
216
217 4b9fe2e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218 cf7394cc leszek
219 809dcae3 Leszek Koltunski
  public int postprocess(DistortedFramebuffer buffer)
220 4b9fe2e9 Leszek Koltunski
    {
221 bed13bea leszek
    int numRenders = 0;
222
223 8dccc3c2 Leszek Koltunski
    GLES31.glDisable(GLES31.GL_BLEND);
224
225 1149be8f leszek
    for(int i=0; i<mNumEffects; i++)
226 95c441a2 leszek
      {
227 9e771d06 Leszek Koltunski
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
228 95c441a2 leszek
      }
229 4b9fe2e9 Leszek Koltunski
230 8dccc3c2 Leszek Koltunski
    GLES31.glEnable(GLES31.GL_BLEND);
231
232 bed13bea leszek
    return numRenders;
233 cf7394cc leszek
    }
234 4c1dd6e9 Leszek Koltunski
  }