Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library is distributed in the hope that it will be useful,                               //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effectqueue;
22

    
23
import android.content.res.Resources;
24
import android.opengl.GLES30;
25
import android.util.Log;
26

    
27
import org.distorted.library.R;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.PostprocessEffect;
30
import org.distorted.library.effect.VertexEffect;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedFramebuffer;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.InternalOutputSurface;
36
import org.distorted.library.main.InternalRenderState;
37
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.message.EffectMessageSender;
39
import org.distorted.library.program.DistortedProgram;
40

    
41
import java.io.InputStream;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
/**
45
 * Not part of public API, do not document
46
 *
47
 * @y.exclude
48
 */
49
public class EffectQueuePostprocess extends EffectQueue
50
  {
51
  private static final int NUM_FLOAT_UNIFORMS = PostprocessEffect.NUM_FLOAT_UNIFORMS;
52
  private static final int NUM_INT_UNIFORMS   = PostprocessEffect.NUM_INT_UNIFORMS;
53
  private static final boolean USE_UBO        = false;
54
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
55

    
56
  private int mHalo;
57
  private boolean mUseHaloDepth;
58
  private float mR, mG, mB, mA;
59

    
60
  private static DistortedProgram mPreProgram;
61
  private static int mPreColorH;
62
  private static int mPreTextureH;
63
  private static int mPreProgramH;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  EffectQueuePostprocess()
68
    { 
69
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, USE_UBO, INDEX );
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  EffectQueuePostprocess(EffectQueuePostprocess source)
75
    {
76
    super(source);
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  void compute(long currTime, long step)
82
    {
83
    mR = mG = mB = mA = 0.0f;
84
    mHalo = 0;
85
    int halo;
86
    float[] array = mUBF.getBackingArray();
87

    
88
    for(int i=0; i<mNumEffects; i++)
89
      {
90
      // 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
      array[NUM_FLOAT_UNIFORMS*i+5]=0.0f;
94

    
95
      PostprocessEffect effect = (PostprocessEffect)mEffects[i];
96

    
97
      if( effect.compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
98
        {
99
        EffectMessageSender.newMessage(mEffects[i]);
100
        }
101

    
102
      halo = (int)array[NUM_FLOAT_UNIFORMS*i];
103
      if( halo>mHalo ) mHalo = halo;
104

    
105
      // TODO  (now only really works in case of 1 effect!)
106
      mUseHaloDepth = effect.getHaloDepth();
107
      }
108

    
109
    // TODO  (now only really works in case of 1 effect!)
110
    if( mNumEffects>0 )
111
      {
112
      mR = array[2];
113
      mG = array[3];
114
      mB = array[4];
115
      mA = array[5];
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public static void createPrograms(Resources resources, int GLSL)
122
    {
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
    String version = "#version "+GLSL+" es\n";
129
    String mainVertHeader= version + ("#define NUM_VERTEX " + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
130
    String mainFragHeader= version + "\n";
131

    
132
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
133
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
134

    
135
    String enabledEffectV= VertexEffect.getGLSL();
136

    
137
    try
138
      {
139
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
140
                                         enabledEffectV, null, GLSL, null);
141
      }
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
    mPreProgramH = mPreProgram.getProgramHandle();
149
    EffectQueue.getUniforms( mPreProgramH,2 );
150
    MeshBase.getUniforms( mPreProgramH,2 );
151
    mPreColorH  = GLES30.glGetUniformLocation( mPreProgramH, "u_Color"  );
152
    mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// TODO  (now only really works in case of 1 effect!)
157

    
158
  public int getQuality()
159
    {
160
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// TODO  (now only really works in case of 1 effect!)
165

    
166
  public boolean getRenderDirectly()
167
    {
168
    return mNumEffects>0 && ((PostprocessEffect) mEffects[0]).getRenderDirectly();
169
    }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
  public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
174
    {
175
    MeshBase mesh = node.getMesh();
176
    DistortedEffects effects = node.getEffects();
177

    
178
    int width   = buffer.getWidth();
179
    int height  = buffer.getHeight();
180

    
181
    InternalRenderState.setUpStencilMark(mA!=0.0f);
182
    InternalRenderState.disableBlending();
183
    if( !mUseHaloDepth ) GLES30.glDepthMask(false);
184

    
185
    GLES30.glViewport(0, 0, width, height );
186

    
187
    mPreProgram.useProgram();
188

    
189
    mesh.bindVertexAttribs(mPreProgram);
190
    mesh.send(mPreProgramH,2);
191

    
192
    EffectQueue[] queues = effects.getQueues();
193
    EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
194
    EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
195

    
196
    matrix.send(distance, mipmap, projection, 2);
197
    vertex.send(mHalo*0.01f,mPreProgramH,2);
198

    
199
    if( mA!=0.0f )
200
      {
201
      GLES30.glUniform4f(mPreColorH, mR, mG, mB, mA);
202
      GLES30.glUniform1i(mPreTextureH, 0);
203
      }
204

    
205
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
206
    mPreProgram.stopUsingProgram();
207

    
208
    InternalRenderState.restoreBlending();
209
    InternalRenderState.unsetUpStencilMark();
210
    if( !mUseHaloDepth ) GLES30.glDepthMask(true);
211

    
212
    return 1;
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public int postprocess(DistortedFramebuffer buffer)
218
    {
219
    int numRenders = 0;
220
    float[] array = mUBF.getBackingArray();
221

    
222
    GLES30.glDisable(GLES30.GL_BLEND);
223

    
224
    for(int i=0; i<mNumEffects; i++)
225
      {
226
      numRenders += ((PostprocessEffect)mEffects[i]).postprocess(array,NUM_FLOAT_UNIFORMS*i, buffer);
227
      }
228

    
229
    GLES30.glEnable(GLES30.GL_BLEND);
230

    
231
    return numRenders;
232
    }
233
  }
(4-4/5)