Project

General

Profile

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

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

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

    
44
  static DistortedProgram mPreProgram;
45
  static int mPreColorH;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  EffectQueuePostprocess(long id)
50
    { 
51
    super(id,NUM_UNIFORMS,INDEX );
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
62
    mHalo = 0;
63
    int halo;
64

    
65
    for(int i=0; i<mNumEffects; i++)
66
      {
67
      mCurrentDuration[i] += step;
68

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

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

    
83
      halo = (int)mUniforms[NUM_UNIFORMS*i];
84
      if( halo>mHalo ) mHalo = halo;
85
      }
86

    
87
    mTime = currTime;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
  /**
92
   * Only for use by the library itself.
93
   *
94
   * @y.exclude
95
   */
96
  public static void createPrograms(Resources resources)
97
    {
98
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
99
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
100

    
101
    int numV = VertexEffect.getNumEnabled();
102

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

    
106
    String enabledEffectV= VertexEffect.getGLSL();
107

    
108
    try
109
      {
110
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
111
                                         enabledEffectV, null, Distorted.GLSL, null);
112
      }
113
    catch(Exception e)
114
      {
115
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
116
      throw new RuntimeException(e.getMessage());
117
      }
118

    
119
    int preProgramH = mPreProgram.getProgramHandle();
120
    EffectQueueVertex.getUniforms( preProgramH,2 );
121
    EffectQueueMatrix.getUniforms( preProgramH,2 );
122
    mPreColorH= GLES31.glGetUniformLocation( preProgramH, "u_Color");
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// TODO
127

    
128
  private boolean getWriteColor()
129
    {
130
    return false;
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  private int getHalo()
136
    {
137
    return mNumEffects>0 ? mHalo : 0;
138
    }
139

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

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

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

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

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node)
159
    {
160
    int quality = getInternalQuality();
161
    DistortedOutputSurface surface = buffers[quality];
162

    
163
    surface.setAsOutput();
164
    DistortedSurface input = node.getInternalSurface();
165

    
166
    if( input.setAsInput() )
167
      {
168
      MeshObject    mesh = node.getMesh();
169
      boolean writeColor = getWriteColor();
170
      float       margin = getHalo()*surface.mMipmap;
171

    
172
      float halfW = input.getWidth() / 2.0f;
173
      float halfH = input.getHeight()/ 2.0f;
174
      float halfZ = halfW*mesh.zFactor;
175

    
176
      DistortedRenderState.setUpStencilMark(writeColor);
177

    
178
      GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
179

    
180
      mPreProgram.useProgram();
181

    
182
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
183
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
184
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
185
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
186
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
187

    
188
      node.getEffects().send(halfW, halfH, halfZ, margin, surface, 2);
189

    
190
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
191

    
192
      DistortedRenderState.unsetUpStencilMark();
193

    
194
      return 1;
195
      }
196
    return 0;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  int postprocess(DistortedOutputSurface[] buffers)
202
    {
203
    int numRenders = 0;
204

    
205
    GLES31.glDisable(GLES31.GL_BLEND);
206

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

    
212
    GLES31.glEnable(GLES31.GL_BLEND);
213

    
214
    return numRenders;
215
    }
216
  }
(16-16/20)