Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueuePostprocess.java @ 835b197e

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, long step)
80
    {
81
    mR = mG = mB = mA = 0.0f;
82
    mHalo = 0;
83
    int halo;
84
    float[] array = mUBF.getBackingArray();
85

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

    
93
      if( mEffects[i].compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
94
        {
95
        EffectMessageSender.newMessage(mEffects[i]);
96
        }
97

    
98
      halo = (int)array[NUM_FLOAT_UNIFORMS*i];
99
      if( halo>mHalo ) mHalo = halo;
100
      }
101

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

    
119
    int numV = VertexEffect.getNumEnabled();
120

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

    
125
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
126
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
127

    
128
    String enabledEffectV= VertexEffect.getGLSL();
129

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

    
141
    mPreProgramH = mPreProgram.getProgramHandle();
142
    EffectQueue.getUniforms( mPreProgramH,2 );
143
    MeshBase.getUniforms( mPreProgramH,2 );
144
    mPreColorH  = GLES30.glGetUniformLocation( mPreProgramH, "u_Color"  );
145
    mPreTextureH= GLES30.glGetUniformLocation( mPreProgramH, "u_Texture");
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// TODO  (now only really works in case of 1 effect!)
150

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

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

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
171
    int width   = buffer.getWidth();
172
    int height  = buffer.getHeight();
173

    
174
    InternalRenderState.setUpStencilMark(mA!=0.0f);
175
    InternalRenderState.disableBlending();
176

    
177
    GLES30.glViewport(0, 0, width, height );
178

    
179
    mPreProgram.useProgram();
180

    
181
    mesh.bindVertexAttribs(mPreProgram);
182
    mesh.send(mPreProgramH,2);
183

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

    
188
    matrix.send(distance, mipmap, projection, 2);
189
    vertex.send(mHalo*0.01f,mPreProgramH,2);
190

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

    
197
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
198

    
199
    InternalRenderState.restoreBlending();
200
    InternalRenderState.unsetUpStencilMark();
201

    
202
    return 1;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public int postprocess(DistortedFramebuffer buffer)
208
    {
209
    int numRenders = 0;
210
    float[] array = mUBF.getBackingArray();
211

    
212
    GLES30.glDisable(GLES30.GL_BLEND);
213

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

    
219
    GLES30.glEnable(GLES30.GL_BLEND);
220

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