Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 46d463a4

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// 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
// Distorted 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                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.library.effectqueue;
21

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

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

    
40
import java.io.InputStream;
41

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

    
55
  private int mHalo;
56
  private float mR, mG, mB, mA;
57

    
58
  private static DistortedProgram mPreProgram;
59
  private static int mPreColorH;
60
  private static int mPreTextureH;
61
  private static int mPreProgramH;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  EffectQueuePostprocess()
66
    { 
67
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, USE_UBO, INDEX );
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  EffectQueuePostprocess(EffectQueuePostprocess source)
73
    {
74
    super(source);
75
    }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
  void compute(long currTime)
80
    {
81
    if( currTime==mTime ) return;
82
    if( mTime==0 ) mTime = currTime;
83
    long step = (currTime-mTime);
84

    
85
    mR = mG = mB = mA = 0.0f;
86
    mHalo = 0;
87
    int halo;
88
    float[] array = mUBF.getBackingArray();
89

    
90
    for(int i=0; i<mNumEffects; i++)
91
      {
92
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
93
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
94
      // that (see preprocess())
95
      array[NUM_FLOAT_UNIFORMS*i+5]=0.0f;
96

    
97
      if( mEffects[i].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

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

    
115
    mTime = currTime;
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

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

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

    
134
    String enabledEffectV= VertexEffect.getGLSL();
135

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

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

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

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

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

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

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

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

    
180
    InternalRenderState.setUpStencilMark(mA!=0.0f);
181
    InternalRenderState.disableBlending();
182

    
183
    GLES30.glViewport(0, 0, width, height );
184

    
185
    mPreProgram.useProgram();
186

    
187
    mesh.bindVertexAttribs(mPreProgram);
188
    mesh.send(mPreProgramH,2);
189

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

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

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

    
203
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
204

    
205
    InternalRenderState.restoreBlending();
206
    InternalRenderState.unsetUpStencilMark();
207

    
208
    return 1;
209
    }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
  public int postprocess(DistortedFramebuffer buffer)
214
    {
215
    int numRenders = 0;
216
    float[] array = mUBF.getBackingArray();
217

    
218
    GLES30.glDisable(GLES30.GL_BLEND);
219

    
220
    for(int i=0; i<mNumEffects; i++)
221
      {
222
      numRenders += ((PostprocessEffect)mEffects[i]).apply(array,NUM_FLOAT_UNIFORMS*i, buffer);
223
      }
224

    
225
    GLES30.glEnable(GLES30.GL_BLEND);
226

    
227
    return numRenders;
228
    }
229
  }
(4-4/5)