Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 0bd9f644

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_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
51
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
52

    
53
  private int mHalo;
54
  private float mR, mG, mB, mA;
55

    
56
  private static DistortedProgram mPreProgram;
57
  private static int mPreColorH;
58
  private static int mPreTextureH;
59
  private static int mPreProgramH;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  EffectQueuePostprocess()
64
    { 
65
    super(NUM_UNIFORMS,INDEX );
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  EffectQueuePostprocess(EffectQueuePostprocess source)
71
    {
72
    super(source);
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

    
83
    mR = mG = mB = mA = 0.0f;
84
    mHalo = 0;
85
    int halo;
86

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

    
94
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
95
        {
96
        EffectMessageSender.newMessage(mEffects[i]);
97
        }
98

    
99
      halo = (int)mUniforms[NUM_UNIFORMS*i];
100
      if( halo>mHalo ) mHalo = halo;
101
      }
102

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

    
112
    mTime = currTime;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  public static void createPrograms(Resources resources, int GLSL)
118
    {
119
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
120
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
121

    
122
    int numV = VertexEffect.getNumEnabled();
123

    
124
    String version = "#version "+GLSL+" es\n";
125
    String mainVertHeader= version + ("#define NUM_VERTEX " + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
126
    String mainFragHeader= version + "\n";
127

    
128
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
129

    
130
    String enabledEffectV= VertexEffect.getGLSL();
131

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

    
143
    mPreProgramH = mPreProgram.getProgramHandle();
144
    EffectQueueVertex.getUniforms( mPreProgramH,2 );
145
    EffectQueueMatrix.getUniforms( mPreProgramH,2 );
146
    mPreColorH  = GLES30.glGetUniformLocation( mPreProgramH, "u_Color"  );
147
    mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// TODO  (now only really works in case of 1 effect!)
152

    
153
  public int getQuality()
154
    {
155
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
// TODO  (now only really works in case of 1 effect!)
160

    
161
  public boolean getRender()
162
    {
163
    return mNumEffects > 0 && ((PostprocessEffect) mEffects[0]).getRender();
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

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

    
173
    int width   = buffer.getWidth();
174
    int height  = buffer.getHeight();
175

    
176
    InternalRenderState.setUpStencilMark(mA!=0.0f);
177
    InternalRenderState.disableBlending();
178

    
179
    GLES30.glViewport(0, 0, width, height );
180

    
181
    mPreProgram.useProgram();
182

    
183
    mesh.bindVertexAttribs(mPreProgram);
184
    mesh.send(mPreProgramH);
185

    
186
    EffectQueue[] queues = effects.getQueues();
187
    EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
188
    EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
189

    
190
    matrix.send(distance, mipmap, projection, 2);
191
    vertex.send(mHalo*0.01f,2);
192

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

    
199
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
200

    
201
    InternalRenderState.restoreBlending();
202
    InternalRenderState.unsetUpStencilMark();
203

    
204
    return 1;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  public int postprocess(DistortedFramebuffer buffer)
210
    {
211
    int numRenders = 0;
212

    
213
    GLES30.glDisable(GLES30.GL_BLEND);
214

    
215
    for(int i=0; i<mNumEffects; i++)
216
      {
217
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
218
      }
219

    
220
    GLES30.glEnable(GLES30.GL_BLEND);
221

    
222
    return numRenders;
223
    }
224
  }
(4-4/5)