Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueuePostprocess.java @ e49b26ba

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.main;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES31;
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.mesh.MeshBase;
31
import org.distorted.library.message.EffectMessage;
32
import org.distorted.library.message.EffectMessageSender;
33
import org.distorted.library.program.DistortedProgram;
34

    
35
import java.io.InputStream;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class EffectQueuePostprocess extends EffectQueue
40
  {
41
  private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
42
  private static final int INDEX = EffectType.POSTPROCESS.ordinal();
43

    
44
  private int mHalo;
45
  private float mR, mG, mB, mA;
46

    
47
  private static DistortedProgram mPreProgram;
48
  private static int mPreColorH;
49
  private static int mPreTextureH;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  EffectQueuePostprocess(long id)
54
    { 
55
    super(id,NUM_UNIFORMS,INDEX );
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  void compute(long currTime)
61
    {
62
    if( currTime==mTime ) return;
63
    if( mTime==0 ) mTime = currTime;
64
    long step = (currTime-mTime);
65

    
66
    mR = mG = mB = mA = 0.0f;
67
    mHalo = 0;
68
    int halo;
69

    
70
    for(int i=0; i<mNumEffects; i++)
71
      {
72
      mCurrentDuration[i] += step;
73

    
74
      // first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
75
      // and if previously there was a GLOW effect here then mA would be non-zero and we don't want
76
      // that (see preprocess())
77
      mUniforms[NUM_UNIFORMS*i+4]=0.0f;
78

    
79
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
80
        {
81
        for(int j=0; j<mNumListeners; j++)
82
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
83

    
84
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
85
          {
86
          remove(i--);
87
          mNumEffectsToBe--;
88
          regenerateIDandSort();
89
          continue;
90
          }
91
        }
92

    
93
      halo = (int)mUniforms[NUM_UNIFORMS*i];
94
      if( halo>mHalo ) mHalo = halo;
95
      }
96

    
97
    // TODO  (now only really works in case of 1 effect!)
98
    if( mNumEffects>0 )
99
      {
100
      mR = mUniforms[1];
101
      mG = mUniforms[2];
102
      mB = mUniforms[3];
103
      mA = mUniforms[4];
104
      }
105

    
106
    mTime = currTime;
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  static void createPrograms(Resources resources)
112
    {
113
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
114
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
115

    
116
    int numV = VertexEffect.getNumEnabled();
117

    
118
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? DistortedEffects.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
119
    String mainFragHeader= Distorted.GLSL_VERSION + "\n";
120

    
121
    String enabledEffectV= VertexEffect.getGLSL();
122

    
123
    try
124
      {
125
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
126
                                         enabledEffectV, null, Distorted.GLSL, null);
127
      }
128
    catch(Exception e)
129
      {
130
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
131
      throw new RuntimeException(e.getMessage());
132
      }
133

    
134
    int preProgramH = mPreProgram.getProgramHandle();
135
    EffectQueueVertex.getUniforms( preProgramH,2 );
136
    EffectQueueMatrix.getUniforms( preProgramH,2 );
137
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
138
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// TODO  (now only really works in case of 1 effect!)
143

    
144
  int getQuality()
145
    {
146
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
147
    }
148

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

    
152
  boolean getRender()
153
    {
154
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  int preprocess(DistortedOutputSurface buffer, DistortedNode node)
160
    {
161
    buffer.setAsOutput();
162
    DistortedSurface input = node.getInternalSurface();
163

    
164
    if( input.setAsInput() )
165
      {
166
      MeshBase mesh = node.getMesh();
167

    
168
      float halfW = input.getWidth() / 2.0f;
169
      float halfH = input.getHeight()/ 2.0f;
170
      float halfZ = halfW*mesh.getZFactor();
171

    
172
      DistortedRenderState.setUpStencilMark(mA!=0.0f);
173

    
174
      GLES31.glViewport(0, 0, buffer.mWidth, buffer.mHeight );
175

    
176
      mPreProgram.useProgram();
177

    
178
      mesh.bindVertexAttribs(mPreProgram);
179

    
180
      node.getEffects().send(halfW, halfH, halfZ, mHalo, buffer, 2);
181

    
182
      if( mA!=0.0f )
183
        {
184
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
185
        GLES31.glUniform1i(mPreTextureH, 0);
186
        }
187

    
188
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
189

    
190
      DistortedRenderState.unsetUpStencilMark();
191

    
192
      return 1;
193
      }
194
    return 0;
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  int postprocess(DistortedFramebuffer buffer)
200
    {
201
    int numRenders = 0;
202

    
203
    GLES31.glDisable(GLES31.GL_BLEND);
204

    
205
    for(int i=0; i<mNumEffects; i++)
206
      {
207
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
208
      }
209

    
210
    GLES31.glEnable(GLES31.GL_BLEND);
211

    
212
    return numRenders;
213
    }
214
  }
(16-16/17)