Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 5c84d9c2

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 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
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 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 96e3b88a Leszek Koltunski
  private static final int NUM_FLOAT_UNIFORMS = PostprocessEffect.NUM_FLOAT_UNIFORMS;
51
  private static final int NUM_INT_UNIFORMS   = PostprocessEffect.NUM_INT_UNIFORMS;
52 2b7d2abb Leszek Koltunski
  private static final boolean USE_UBO        = false;
53 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
54 4c1dd6e9 Leszek Koltunski
55 fff1110e leszek
  private int mHalo;
56 5c84d9c2 Leszek Koltunski
  private boolean mUseHaloDepth;
57 9e771d06 Leszek Koltunski
  private float mR, mG, mB, mA;
58 a31dbc5c Leszek Koltunski
59 247d8225 Leszek Koltunski
  private static DistortedProgram mPreProgram;
60
  private static int mPreColorH;
61 9e771d06 Leszek Koltunski
  private static int mPreTextureH;
62 0bd9f644 Leszek Koltunski
  private static int mPreProgramH;
63 a13dde77 Leszek Koltunski
64 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 20dbec0e Leszek Koltunski
  EffectQueuePostprocess()
67 4c1dd6e9 Leszek Koltunski
    { 
68 2b7d2abb Leszek Koltunski
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, USE_UBO, INDEX );
69 4c1dd6e9 Leszek Koltunski
    }
70
71 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  EffectQueuePostprocess(EffectQueuePostprocess source)
74
    {
75
    super(source);
76
    }
77
78 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 835b197e Leszek Koltunski
  void compute(long currTime, long step)
81 4c1dd6e9 Leszek Koltunski
    {
82 9e771d06 Leszek Koltunski
    mR = mG = mB = mA = 0.0f;
83 fff1110e leszek
    mHalo = 0;
84
    int halo;
85 de77a6c5 Leszek Koltunski
    float[] array = mUBF.getBackingArray();
86 fff1110e leszek
87 4c1dd6e9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
88
      {
89 e49b26ba Leszek Koltunski
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
90
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
91
      // that (see preprocess())
92 de77a6c5 Leszek Koltunski
      array[NUM_FLOAT_UNIFORMS*i+5]=0.0f;
93 e49b26ba Leszek Koltunski
94 5c84d9c2 Leszek Koltunski
      PostprocessEffect effect = (PostprocessEffect)mEffects[i];
95
96
      if( effect.compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
97 4c1dd6e9 Leszek Koltunski
        {
98 20dbec0e Leszek Koltunski
        EffectMessageSender.newMessage(mEffects[i]);
99 4c1dd6e9 Leszek Koltunski
        }
100 a31dbc5c Leszek Koltunski
101 de77a6c5 Leszek Koltunski
      halo = (int)array[NUM_FLOAT_UNIFORMS*i];
102 fff1110e leszek
      if( halo>mHalo ) mHalo = halo;
103 5c84d9c2 Leszek Koltunski
104
      // TODO  (now only really works in case of 1 effect!)
105
      mUseHaloDepth = effect.getHaloDepth();
106 4c1dd6e9 Leszek Koltunski
      }
107 e02264ff leszek
108 9e771d06 Leszek Koltunski
    // TODO  (now only really works in case of 1 effect!)
109
    if( mNumEffects>0 )
110
      {
111 de77a6c5 Leszek Koltunski
      mR = array[2];
112
      mG = array[3];
113
      mB = array[4];
114
      mA = array[5];
115 9e771d06 Leszek Koltunski
      }
116 4c1dd6e9 Leszek Koltunski
    }
117
118 984dc935 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119 247d8225 Leszek Koltunski
120 b7074bc6 Leszek Koltunski
  public static void createPrograms(Resources resources, int GLSL)
121 a13dde77 Leszek Koltunski
    {
122
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
123
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
124
125
    int numV = VertexEffect.getNumEnabled();
126
127 b7074bc6 Leszek Koltunski
    String version = "#version "+GLSL+" es\n";
128 8e3b71e2 Leszek Koltunski
    String mainVertHeader= version + ("#define NUM_VERTEX " + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
129 b7074bc6 Leszek Koltunski
    String mainFragHeader= version + "\n";
130 a13dde77 Leszek Koltunski
131 e8925fcd Leszek Koltunski
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
132 46d463a4 Leszek Koltunski
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
133 36d65d88 Leszek Koltunski
134 a13dde77 Leszek Koltunski
    String enabledEffectV= VertexEffect.getGLSL();
135
136
    try
137
      {
138
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
139 b7074bc6 Leszek Koltunski
                                         enabledEffectV, null, GLSL, null);
140 a13dde77 Leszek Koltunski
      }
141
    catch(Exception e)
142
      {
143
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
144
      throw new RuntimeException(e.getMessage());
145
      }
146
147 0bd9f644 Leszek Koltunski
    mPreProgramH = mPreProgram.getProgramHandle();
148 9f9924f8 Leszek Koltunski
    EffectQueue.getUniforms( mPreProgramH,2 );
149
    MeshBase.getUniforms( mPreProgramH,2 );
150 0bd9f644 Leszek Koltunski
    mPreColorH  = GLES30.glGetUniformLocation( mPreProgramH, "u_Color"  );
151
    mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
152 86d322b5 Leszek Koltunski
    }
153
154 7266d8ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155 2386a081 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
156 7266d8ef Leszek Koltunski
157 809dcae3 Leszek Koltunski
  public int getQuality()
158 7266d8ef Leszek Koltunski
    {
159 9e771d06 Leszek Koltunski
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
160 7266d8ef Leszek Koltunski
    }
161
162 2386a081 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
163 a13dde77 Leszek Koltunski
// TODO  (now only really works in case of 1 effect!)
164 2386a081 Leszek Koltunski
165 d58407a8 Leszek Koltunski
  public boolean getRenderDirectly()
166 a13dde77 Leszek Koltunski
    {
167 d58407a8 Leszek Koltunski
    return mNumEffects > 0 && ((PostprocessEffect) mEffects[0]).getRenderDirectly();
168 a13dde77 Leszek Koltunski
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 7602a827 Leszek Koltunski
  public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
173 2386a081 Leszek Koltunski
    {
174 4bb94a7d Leszek Koltunski
    MeshBase mesh = node.getMesh();
175
    DistortedEffects effects = node.getEffects();
176 2386a081 Leszek Koltunski
177 4bb94a7d Leszek Koltunski
    int width   = buffer.getWidth();
178
    int height  = buffer.getHeight();
179 809dcae3 Leszek Koltunski
180 4bb94a7d Leszek Koltunski
    InternalRenderState.setUpStencilMark(mA!=0.0f);
181
    InternalRenderState.disableBlending();
182 5c84d9c2 Leszek Koltunski
    if( !mUseHaloDepth ) GLES30.glDepthMask(false);
183 a13dde77 Leszek Koltunski
184 b7074bc6 Leszek Koltunski
    GLES30.glViewport(0, 0, width, height );
185 a13dde77 Leszek Koltunski
186 4bb94a7d Leszek Koltunski
    mPreProgram.useProgram();
187 a13dde77 Leszek Koltunski
188 4bb94a7d Leszek Koltunski
    mesh.bindVertexAttribs(mPreProgram);
189 9f9924f8 Leszek Koltunski
    mesh.send(mPreProgramH,2);
190 a13dde77 Leszek Koltunski
191 4bb94a7d Leszek Koltunski
    EffectQueue[] queues = effects.getQueues();
192
    EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
193
    EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
194 84d51487 Leszek Koltunski
195 62c869ad Leszek Koltunski
    matrix.send(distance, mipmap, projection, 2);
196 78ff6ea9 Leszek Koltunski
    vertex.send(mHalo*0.01f,mPreProgramH,2);
197 247d8225 Leszek Koltunski
198 4bb94a7d Leszek Koltunski
    if( mA!=0.0f )
199
      {
200 b7074bc6 Leszek Koltunski
      GLES30.glUniform4f(mPreColorH, mR, mG, mB, mA);
201
      GLES30.glUniform1i(mPreTextureH, 0);
202 4bb94a7d Leszek Koltunski
      }
203 247d8225 Leszek Koltunski
204 b7074bc6 Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
205 a13dde77 Leszek Koltunski
206 4bb94a7d Leszek Koltunski
    InternalRenderState.restoreBlending();
207
    InternalRenderState.unsetUpStencilMark();
208 5c84d9c2 Leszek Koltunski
    if( !mUseHaloDepth ) GLES30.glDepthMask(true);
209 a13dde77 Leszek Koltunski
210 4bb94a7d Leszek Koltunski
    return 1;
211 2386a081 Leszek Koltunski
    }
212
213 4b9fe2e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
214 cf7394cc leszek
215 809dcae3 Leszek Koltunski
  public int postprocess(DistortedFramebuffer buffer)
216 4b9fe2e9 Leszek Koltunski
    {
217 bed13bea leszek
    int numRenders = 0;
218 de77a6c5 Leszek Koltunski
    float[] array = mUBF.getBackingArray();
219 bed13bea leszek
220 b7074bc6 Leszek Koltunski
    GLES30.glDisable(GLES30.GL_BLEND);
221 8dccc3c2 Leszek Koltunski
222 1149be8f leszek
    for(int i=0; i<mNumEffects; i++)
223 95c441a2 leszek
      {
224 d58407a8 Leszek Koltunski
      numRenders += ((PostprocessEffect)mEffects[i]).postprocess(array,NUM_FLOAT_UNIFORMS*i, buffer);
225 95c441a2 leszek
      }
226 4b9fe2e9 Leszek Koltunski
227 b7074bc6 Leszek Koltunski
    GLES30.glEnable(GLES30.GL_BLEND);
228 8dccc3c2 Leszek Koltunski
229 bed13bea leszek
    return numRenders;
230 cf7394cc leszek
    }
231 4c1dd6e9 Leszek Koltunski
  }