Project

General

Profile

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

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

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