Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueuePostprocess.java @ 7a5e538a

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
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
75
        {
76
        for(int j=0; j<mNumListeners; j++)
77
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
78

    
79
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
80
          {
81
          remove(i--);
82
          mNumEffectsToBe--;
83
          regenerateIDandSort();
84
          continue;
85
          }
86
        }
87

    
88
      halo = (int)mUniforms[NUM_UNIFORMS*i];
89
      if( halo>mHalo ) mHalo = halo;
90
      }
91

    
92
    // TODO  (now only really works in case of 1 effect!)
93
    if( mNumEffects>0 )
94
      {
95
      mR = mUniforms[1];
96
      mG = mUniforms[2];
97
      mB = mUniforms[3];
98
      mA = mUniforms[4];
99
      }
100

    
101
    mTime = currTime;
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  static void createPrograms(Resources resources)
107
    {
108
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
109
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
110

    
111
    int numV = VertexEffect.getNumEnabled();
112

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

    
116
    String enabledEffectV= VertexEffect.getGLSL();
117

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

    
129
    int preProgramH = mPreProgram.getProgramHandle();
130
    EffectQueueVertex.getUniforms( preProgramH,2 );
131
    EffectQueueMatrix.getUniforms( preProgramH,2 );
132
    mPreColorH  = GLES31.glGetUniformLocation( preProgramH, "u_Color"  );
133
    mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// TODO  (now only really works in case of 1 effect!)
138

    
139
  int getQuality()
140
    {
141
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
// TODO  (now only really works in case of 1 effect!)
146

    
147
  boolean getRender()
148
    {
149
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  int preprocess(DistortedOutputSurface buffer, DistortedNode node)
155
    {
156
    buffer.setAsOutput();
157
    DistortedSurface input = node.getInternalSurface();
158

    
159
    if( input.setAsInput() )
160
      {
161
      MeshBase mesh = node.getMesh();
162

    
163
      float halfW = input.getWidth() / 2.0f;
164
      float halfH = input.getHeight()/ 2.0f;
165
      float halfZ = halfW*mesh.getZFactor();
166

    
167
      DistortedRenderState.setUpStencilMark(mA!=0.0f);
168

    
169
      GLES31.glViewport(0, 0, buffer.mWidth, buffer.mHeight );
170

    
171
      mPreProgram.useProgram();
172

    
173
      mesh.bindVertexAttribs(mPreProgram);
174

    
175
      node.getEffects().send(halfW, halfH, halfZ, mHalo, buffer, 2);
176

    
177
      if( mA!=0.0f )
178
        {
179
        GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
180
        GLES31.glUniform1i(mPreTextureH, 0);
181
        }
182

    
183
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
184

    
185
      DistortedRenderState.unsetUpStencilMark();
186

    
187
      return 1;
188
      }
189
    return 0;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  int postprocess(DistortedFramebuffer buffer)
195
    {
196
    int numRenders = 0;
197

    
198
    GLES31.glDisable(GLES31.GL_BLEND);
199

    
200
    for(int i=0; i<mNumEffects; i++)
201
      {
202
      numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
203
      }
204

    
205
    GLES31.glEnable(GLES31.GL_BLEND);
206

    
207
    return numRenders;
208
    }
209
  }
(16-16/17)