Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 5ccafcca

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

    
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
24
import android.opengl.Matrix;
25

    
26
import org.distorted.library.message.EffectMessage;
27
import org.distorted.library.program.DistortedProgram;
28
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
33
import org.distorted.library.type.Data1D;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36

    
37
import java.io.InputStream;
38
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class EffectQueuePostprocess extends EffectQueue
45
  {
46
  private static final int MAX_BLUR = 50;
47

    
48
  private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
49
  private static final int TEX_DATA_SIZE     = 2; // Post Program: size of the texture coordinate data in elements.
50

    
51
  private static final int NUM_UNIFORMS = 4;
52
  private static final int NUM_CACHE    = 0;
53
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
54

    
55
  private static final FloatBuffer mQuadPositions, mQuadTextureNor, mQuadTextureInv;
56

    
57
  static
58
    {
59
    int dataLength      = 4;
60
    int bytes_per_float = 4;
61

    
62
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
63
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
64
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
65

    
66
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
67
    mQuadPositions.put(position).position(0);
68
    mQuadTextureNor  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadTextureNor.put(textureNor).position(0);
70
    mQuadTextureInv  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
71
    mQuadTextureInv.put(textureInv).position(0);
72
    }
73

    
74
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
75
  private static float[] mMVPMatrix = new float[16];
76
  private static float[] mTmpMatrix = new float[16];
77

    
78
  // BLUR effect
79
  private static DistortedProgram mBlurProgram;
80
  private static int mRadiusH,mOffsetsH,mWeightsH,mObjDH,mMVPMatrixH;
81
  private float[] mWeights = new float[MAX_BLUR];
82
  private float[] mOffsets = new float[MAX_BLUR];
83
  // another effect ....
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  EffectQueuePostprocess(long id)
88
    { 
89
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  static void createProgram(Resources resources)
95
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
96
    {
97
    final InputStream postVertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
98
    final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader);
99

    
100
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream,
101
                                        "#version 100\n",
102
                                        "#version 100\n#define MAX_BLUR "+MAX_BLUR);
103

    
104
    int blurProgramH = mBlurProgram.getProgramHandle();
105
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
106
    mOffsetsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets");
107
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
108
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
109
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
  
114
  synchronized void compute(long currTime) 
115
    {
116
    if( currTime==mTime ) return;
117
    if( mTime==0 ) mTime = currTime;
118
    long step = (currTime-mTime);
119
   
120
    for(int i=0; i<mNumEffects; i++)
121
      {
122
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
123
        {
124
        for(int j=0; j<mNumListeners; j++)
125
          EffectMessageSender.newMessage( mListeners.elementAt(j),
126
                                          EffectMessage.EFFECT_FINISHED,
127
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
128
                                          mName[i],
129
                                          mObjectID);
130

    
131
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
132
          {
133
          remove(i);
134
          i--;
135
          continue;
136
          }
137
        else mInter[0][i] = null;
138
        }
139

    
140
      mCurrentDuration[i] += step;
141
      }
142
     
143
    mTime = currTime;  
144
    }  
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  protected void moveEffect(int index)
149
    {
150
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
151
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
152
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
153
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
// w,h - width and height of the input texture. MVP - Model-View-Projection matrix to apply to the
158
// texture; df - output FBO.
159

    
160
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
161
    {
162
    mBlurProgram.useProgram();
163

    
164
    int radius = (int)mUniforms[0];
165
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
166

    
167
    for(int i=0; i<=radius; i++)
168
      {
169
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
170
      mOffsets[i] = i/h;
171
      }
172

    
173
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
174
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
175
    GLES30.glUniform1i( mRadiusH, radius);
176
    GLES30.glUniform2f( mObjDH , w, h );
177

    
178
    mBufferFBO.resizeFast( (int)w, (int)h);
179
    mBufferFBO.setAsOutput();
180
    GLES30.glViewport(0, 0, (int)w, (int)h);
181

    
182
    Matrix.setIdentityM(mTmpMatrix, 0);
183
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -mBufferFBO.mDistance);
184
    Matrix.multiplyMM(mMVPMatrix, 0, mBufferFBO.mProjectionMatrix, 0, mTmpMatrix, 0);
185

    
186
    // horizontal blur
187

    
188
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
189
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
190
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
191
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
192

    
193
    mBufferFBO.setAsInput();
194
    df.setAsOutput();
195
    GLES30.glViewport(0, 0, df.mWidth, df.mHeight);
196

    
197
    float adjust = h/w;
198
    for(int i=0; i<=radius; i++) mOffsets[i] *= adjust;
199

    
200
    // vertical blur
201
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
202
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
203
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
204
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
205
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
// blur
210

    
211
  synchronized long add(EffectNames eln, Data1D degree)
212
    {
213
    if( mMax[INDEX]>mNumEffects )
214
      {
215
      if( degree instanceof Dynamic1D)
216
        {
217
        mInter[0][mNumEffects] = (Dynamic1D)degree;
218
        }
219
      else if( degree instanceof Static1D)
220
        {
221
        mInter[0][mNumEffects] = null;
222
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
223
        }
224
      else return -1;
225

    
226
      mInter[1][mNumEffects] = null;
227
      mInter[2][mNumEffects] = null;
228

    
229
      return addBase(eln);
230
      }
231
      
232
    return -1;
233
    }
234
  }
(11-11/16)