Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueuePostprocess.java @ 163b8d7d

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.message.EffectMessage;
31
import org.distorted.library.program.DistortedProgram;
32

    
33
import java.io.InputStream;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
42
  private int mHalo;
43
  private float mR, mG, mB, mA;
44

    
45
  private static DistortedProgram mPreProgram;
46
  private static int mPreColorH;
47
  private static int mPreTextureH;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  EffectQueuePostprocess(long id)
52
    { 
53
    super(id,NUM_UNIFORMS,INDEX );
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
64
    mR = mG = mB = mA = 0.0f;
65
    mHalo = 0;
66
    int halo;
67

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

    
72
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
73
        {
74
        for(int j=0; j<mNumListeners; j++)
75
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
76

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

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

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

    
99
    mTime = currTime;
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
    int numV = VertexEffect.getNumEnabled();
110

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

    
114
    String enabledEffectV= VertexEffect.getGLSL();
115

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

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// TODO  (now only really works in case of 1 effect!)
136

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

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

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

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
157
    if( input.setAsInput() )
158
      {
159
      MeshObject mesh = node.getMesh();
160
      float    margin = mHalo*buffer.mMipmap;
161

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

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

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

    
170
      mPreProgram.useProgram();
171

    
172
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.getVBO() );
173
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
174
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
175
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
176
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
177

    
178
      node.getEffects().send(halfW, halfH, halfZ, margin, buffer, 2);
179

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

    
186
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
187

    
188
      DistortedRenderState.unsetUpStencilMark();
189

    
190
      return 1;
191
      }
192
    return 0;
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  int postprocess(DistortedFramebuffer buffer)
198
    {
199
    int numRenders = 0;
200

    
201
    GLES31.glDisable(GLES31.GL_BLEND);
202

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

    
208
    GLES31.glEnable(GLES31.GL_BLEND);
209

    
210
    return numRenders;
211
    }
212
  }
(17-17/21)