Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 8c57d77b

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