Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 178983f4

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