Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ de77a6c5

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

    
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, 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

    
133
    String enabledEffectV= VertexEffect.getGLSL();
134

    
135
    try
136
      {
137
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
138
                                         enabledEffectV, null, GLSL, null);
139
      }
140
    catch(Exception e)
141
      {
142
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
143
      throw new RuntimeException(e.getMessage());
144
      }
145

    
146
    mPreProgramH = mPreProgram.getProgramHandle();
147
    EffectQueue.getUniforms( mPreProgramH,2 );
148
    MeshBase.getUniforms( mPreProgramH,2 );
149
    mPreColorH  = GLES30.glGetUniformLocation( mPreProgramH, "u_Color"  );
150
    mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
151
    }
152

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

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

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

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

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

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

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

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

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

    
184
    mPreProgram.useProgram();
185

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

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

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

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

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

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

    
207
    return 1;
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

    
217
    GLES30.glDisable(GLES30.GL_BLEND);
218

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

    
224
    GLES30.glEnable(GLES30.GL_BLEND);
225

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