Project

General

Profile

« Previous | Next » 

Revision c1e24646

Added by Leszek Koltunski over 7 years ago

Introduce Effects.blitPriv() (faster than previous drawNoEffectsPriv)

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
54 54
  private static final int NORMAL_DATA_SIZE  = 3; // Main Program: size of the normal data in elements
55 55
  private static final int TEX_DATA_SIZE     = 2; // Main Program: size of the texture coordinate data in elements.
56 56

  
57
  private static DistortedProgram mProgram;
57
  /// MAIN PROGRAM ///
58
  private static DistortedProgram mMainProgram;
59
  private static int mMainTextureH;
58 60
  private static boolean[] mEffectEnabled = new boolean[EffectNames.size()];
59 61

  
60 62
  static
......
67 69
      }
68 70
    }
69 71

  
70
  /// DEBUG ONLY /////
71
  private static DistortedProgram mDebugProgram;
72
  /// BLIT PROGRAM ///
73
  private static DistortedProgram mBlitProgram;
74
  private static int mBlitTextureH;
75
  private static int mBlitObjDH;
76
  private static int mBlitMVPMatrixH;
72 77
  private static final FloatBuffer mQuadPositions;
73 78

  
74 79
  static
......
78 83
    mQuadPositions.put(positionData).position(0);
79 84
    }
80 85

  
81
  private static int mObjDH;
82
  private static int mMVPMatrixH;
86
  /// DEBUG ONLY /////
87
  private static DistortedProgram mDebugProgram;
88

  
89
  private static int mDebugObjDH;
90
  private static int mDebugMVPMatrixH;
83 91
  /// END DEBUG //////
84 92

  
85 93
  private static float[] mMVPMatrix = new float[16];
......
139 147
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
140 148
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
141 149

  
142
    mProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader);
150
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader);
143 151

  
144
    int mainProgramH = mProgram.getProgramHandle();
152
    int mainProgramH = mMainProgram.getProgramHandle();
145 153
    EffectQueueFragment.getUniforms(mainProgramH);
146 154
    EffectQueueVertex.getUniforms(mainProgramH);
147 155
    EffectQueueMatrix.getUniforms(mainProgramH);
148
    DistortedTexture.getUniforms(mainProgramH);
156
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
157

  
158
    // BLIT PROGRAM ////////////////////////////////////
159
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
160
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
161

  
162
    String blitVertHeader= ("#version 100\n#define NUM_VERTEX 0\n"  );
163
    String blitFragHeader= ("#version 100\n#define NUM_FRAGMENT 0\n");
164

  
165
    mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader);
166

  
167
    int blitProgramH = mBlitProgram.getProgramHandle();
168
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
169
    mBlitObjDH     = GLES30.glGetUniformLocation( blitProgramH, "u_objD");
170
    mBlitMVPMatrixH= GLES30.glGetUniformLocation( blitProgramH, "u_MVPMatrix");
149 171

  
150 172
    // DEBUG ONLY //////////////////////////////////////
151 173
    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
......
154 176
    mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, "#version 100\n", "#version 100\n");
155 177

  
156 178
    int debugProgramH = mDebugProgram.getProgramHandle();
157
    mObjDH      = GLES30.glGetUniformLocation( debugProgramH, "u_objD");
158
    mMVPMatrixH = GLES30.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
179
    mDebugObjDH = GLES30.glGetUniformLocation( debugProgramH, "u_objD");
180
    mDebugMVPMatrixH = GLES30.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
159 181
    // END DEBUG  //////////////////////////////////////
160 182
    }
161 183

  
......
254 276
    Matrix.translateM  ( mTmpMatrix, 0, halfX,-halfY, 0);
255 277
    Matrix.multiplyMM  ( mMVPMatrix, 0, surface.mProjectionMatrix, 0, mTmpMatrix, 0);
256 278

  
257
    GLES30.glUniform2f( mObjDH , 2*halfX, 2*halfY);
258
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
279
    GLES30.glUniform2f(mDebugObjDH, 2*halfX, 2*halfY);
280
    GLES30.glUniformMatrix4fv(mDebugMVPMatrixH, 1, false, mMVPMatrix , 0);
259 281

  
260 282
    GLES30.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
261 283
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
......
263 285

  
264 286
///////////////////////////////////////////////////////////////////////////////////////////////////
265 287

  
266
  void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
288
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
267 289
    {
268 290
    mM.compute(currTime);
269 291
    mV.compute(currTime);
270 292
    mF.compute(currTime);
271 293
    mP.compute(currTime);
272 294

  
273
    float halfZ = halfInputW*mesh.zFactor;
295
    float halfZ = halfW*mesh.zFactor;
274 296
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight);
275 297

  
276 298
    if( mP.mNumEffects==0 )
277 299
      {
278
      mProgram.useProgram();
300
      mMainProgram.useProgram();
301
      GLES30.glUniform1i(mMainTextureH, 0);
279 302
      surface.setAsOutput();
280
      mM.send(surface,halfInputW,halfInputH,halfZ);
281
      mV.send(halfInputW,halfInputH,halfZ);
282
      mF.send(halfInputW,halfInputH);
283
      GLES30.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
284
      GLES30.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
285
      GLES30.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
303
      mM.send(surface,halfW,halfH,halfZ);
304
      mV.send(halfW,halfH,halfZ);
305
      mF.send(halfW,halfH);
306
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
307
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
308
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
286 309
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
287 310
      }
288 311
    else
289 312
      {
290 313
      if( mV.mNumEffects==0 && mF.mNumEffects==0 && (mesh instanceof MeshFlat) && mM.canUseShortcut() )
291 314
        {
292
        mM.constructMatrices(surface,halfInputW,halfInputH);
293
        mP.render(2*halfInputW, 2*halfInputH, mM.getMVP(), surface);
315
        mM.constructMatrices(surface,halfW,halfH);
316
        mP.render(2*halfW, 2*halfH, mM.getMVP(), surface);
294 317
        }
295 318
      else
296 319
        {
297
        mProgram.useProgram();
320
        mMainProgram.useProgram();
321
        GLES30.glUniform1i(mMainTextureH, 0);
298 322
        mBufferFBO.resizeFast(surface.mWidth, surface.mHeight);
299 323
        mBufferFBO.setAsOutput();
300 324
        GLES30.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
301 325
        GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
302
        mM.send(mBufferFBO,halfInputW,halfInputH,halfZ);
303
        mV.send(halfInputW,halfInputH,halfZ);
304
        mF.send(halfInputW,halfInputH);
305
        GLES30.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
306
        GLES30.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
307
        GLES30.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
326
        mM.send(mBufferFBO,halfW,halfH,halfZ);
327
        mV.send(halfW,halfH,halfZ);
328
        mF.send(halfW,halfH);
329
        GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
330
        GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
331
        GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
308 332
        GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
309 333

  
310 334
        Matrix.setIdentityM(mTmpMatrix, 0);
......
323 347

  
324 348
///////////////////////////////////////////////////////////////////////////////////////////////////
325 349
   
326
  static void drawNoEffectsPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedOutputSurface projection)
350
  static void blitPriv(DistortedOutputSurface projection)
327 351
    {
328 352
    GLES30.glViewport(0, 0, projection.mWidth, projection.mHeight);
329 353

  
354
    mBlitProgram.useProgram();
355

  
330 356
    Matrix.setIdentityM(mTmpMatrix, 0);
331 357
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -projection.mDistance);
332 358
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mTmpMatrix, 0);
333 359

  
334
    EffectQueueMatrix.sendZero(projection,halfInputW,halfInputH,halfInputW*mesh.zFactor);
335
    EffectQueueVertex.sendZero();
336
    EffectQueueFragment.sendZero();
337

  
338
    GLES30.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
339
    GLES30.glVertexAttribPointer(mProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
340
    GLES30.glVertexAttribPointer(mProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
341
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
360
    GLES30.glUniform1i(mBlitTextureH, 0);
361
    GLES30.glUniform2f( mBlitObjDH , projection.mWidth/2, projection.mHeight/2);
362
    GLES30.glUniformMatrix4fv(mBlitMVPMatrixH, 1, false, mMVPMatrix, 0);
363
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
364
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
342 365
    }
343 366
    
344 367
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/DistortedNode.java
200 200
  int drawRecursive(int renderNum, long currTime, DistortedOutputSurface surface)
201 201
    {
202 202
    int ret = 0;
203
    float halfX = mSurface.getWidth()/2.0f;
204
    float halfY = mSurface.getHeight()/2.0f;
205 203

  
206 204
    if( mNumChildren[0]>0 && mData.numRender!=renderNum )
207 205
      {
......
214 212
      if( mSurface.setAsInput() )
215 213
        {
216 214
        ret++;
217
        DistortedEffects.drawNoEffectsPriv(halfX, halfY, mMesh, mData.mFBO);
215
        DistortedEffects.blitPriv(mData.mFBO);
218 216
        }
219 217

  
220 218
      for(int i=0; i<mNumChildren[0]; i++)
......
229 227
      {
230 228
      ret++;
231 229
      mState.apply();
232
      mEffects.drawPriv(halfX, halfY, mMesh, surface, currTime);
230
      mEffects.drawPriv(mSurface.getWidth()/2, mSurface.getHeight()/2, mMesh, surface, currTime);
233 231
      }
234 232

  
235 233
    return ret;
src/main/java/org/distorted/library/DistortedTexture.java
101 101
    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
102 102
    }
103 103

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

  
106
  static void getUniforms(int mProgramH)
107
    {
108
    int textureH= GLES30.glGetUniformLocation( mProgramH, "u_Texture");
109

  
110
    GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
111
    GLES30.glUniform1i(textureH, 0);
112
    }
113

  
114 104
///////////////////////////////////////////////////////////////////////////////////////////////////
115 105
// create SYSTEM or TREE textures (those are just like normal Textures, just hold information
116 106
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
src/main/res/raw/blit_fragment_shader.glsl
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
precision lowp float;
21
  
22
uniform sampler2D u_Texture;  // The input texture.
23
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
24

  
25
//////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
void main()                    		
28
  {  
29
  gl_FragColor = texture2D(u_Texture,v_TexCoordinate);
30
  }
src/main/res/raw/blit_vertex_shader.glsl
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
precision lowp float;
21

  
22
uniform vec2 u_objD;          // half of object width x half of object height
23
uniform mat4 u_MVPMatrix;     // the combined model/view/projection matrix.
24
attribute vec2 a_Position;    // Per-vertex position.
25
varying vec2 v_TexCoordinate; //
26

  
27
//////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
void main()
30
  {
31
  v_TexCoordinate = a_Position + 0.5;
32
  //gl_Position     = vec4(2.0*a_Position,0.0,1.0);
33
  gl_Position     = u_MVPMatrix*vec4(2.0*u_objD*a_Position,0.0,1.0);
34
  }                               

Also available in: Unified diff