Project

General

Profile

« Previous | Next » 

Revision f80337b5

Added by Leszek Koltunski over 7 years ago

Revert "Some progress with Transform Feedback. Still many bugs all over."

This reverts commit 79921db22e4ff5a3b2e6e3466a39445f5607a695.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
52 52
 */
53 53
public class DistortedEffects
54 54
  {
55
  static final int MAIN_PROGRAM = 0;
56
  static final int FEED_PROGRAM = 1;
57
  static final int NUM_PROGRAMS = 2;
58

  
55 59
  // THIS IS FOR MAIN AND FEEDBACK PROGRAMS ///
56 60
  private static boolean[] mEffectEnabled = new boolean[EffectNames.size()];
57 61

  
......
88 92
  private static int mDebugObjDH;
89 93
  private static int mDebugMVPMatrixH;
90 94

  
95
  /// FEEDBACK PROGRAM //
96
  private static DistortedProgram mFeedbackProgram;
97

  
98

  
91 99
  private static float[] mMVPMatrix = new float[16];
92 100
  private static float[] mTmpMatrix = new float[16];
93 101

  
......
146 154
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
147 155
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
148 156

  
149
    String[] feedback = { "v_Position", "v_Normal" };
150

  
151
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback);
157
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL);
152 158

  
153 159
    int mainProgramH = mMainProgram.getProgramHandle();
154 160
    EffectQueueFragment.getUniforms(mainProgramH);
155
    EffectQueueVertex.getUniforms(mainProgramH);
156
    EffectQueueMatrix.getUniforms(mainProgramH);
161
    EffectQueueVertex.getUniforms(MAIN_PROGRAM,mainProgramH);
162
    EffectQueueMatrix.getUniforms(MAIN_PROGRAM,mainProgramH);
157 163
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
158 164

  
165
    //////////////////////////////////////////////////////////////////////////////////////
166
    ////////// FEEDBACK PROGRAM //////////////////////////////////////////////////////////
167
    //////////////////////////////////////////////////////////////////////////////////////
168
    final InputStream feedVertStream = resources.openRawResource(R.raw.main_vertex_shader);
169
    final InputStream feedFragStream = resources.openRawResource(R.raw.feedback_fragment_shader);
170

  
171
    String[] feedback = { "v_Position" };
172

  
173
    mFeedbackProgram = new DistortedProgram(feedVertStream,feedFragStream, mainVertHeader, Distorted.GLSL_VERSION, Distorted.GLSL, feedback);
174

  
175
    int feedProgramH = mFeedbackProgram.getProgramHandle();
176
    EffectQueueFragment.getUniforms(feedProgramH);
177
    EffectQueueVertex.getUniforms(FEED_PROGRAM,feedProgramH);
178
    EffectQueueMatrix.getUniforms(FEED_PROGRAM,feedProgramH);
179

  
159 180
    //////////////////////////////////////////////////////////////////////////////////////
160 181
    ////////// BLIT PROGRAM //////////////////////////////////////////////////////////////
161 182
    //////////////////////////////////////////////////////////////////////////////////////
......
305 326
    mMainProgram.useProgram();
306 327
    GLES30.glUniform1i(mMainTextureH, 0);
307 328
    surface.setAsOutput(currTime);
308
    mM.send(surface,halfW,halfH,halfZ);
309
    mV.send(halfW,halfH,halfZ);
329
    mM.send(MAIN_PROGRAM,surface,halfW,halfH,halfZ);
330
    mV.send(MAIN_PROGRAM,halfW,halfH,halfZ);
310 331
    mF.send(halfW,halfH);
311 332

  
312 333
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mPosVBO[0]);
......
324 345

  
325 346
  void drawPrivFeedback(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
326 347
    {
327
    int error;
328

  
329 348
    mM.compute(currTime);
330 349
    mV.compute(currTime);
331 350
    mF.compute(currTime);
......
334 353

  
335 354
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
336 355

  
337
    mMainProgram.useProgram();
338

  
339
    mM.send(surface,halfW,halfH,halfZ);
340
    mV.send(halfW,halfH,halfZ);
356
    mFeedbackProgram.useProgram();
357
    surface.setAsOutput(currTime);
358
    mM.send(FEED_PROGRAM,surface,halfW,halfH,halfZ);
359
    mV.send(FEED_PROGRAM,halfW,halfH,halfZ);
341 360

  
342 361
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mPosVBO[0]);
343
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
362
    GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[0], MeshObject.POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
344 363
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mNorVBO[0]);
345
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, 0);
364
    GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[1], MeshObject.NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, 0);
346 365
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mTexVBO[0]);
347
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, 0);
366
    GLES30.glVertexAttribPointer(mFeedbackProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, 0);
348 367

  
349 368
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mPosTBO[0]);
350
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 1, mesh.mNorTBO[0]);
351 369
    GLES30.glEnable(GLES30.GL_RASTERIZER_DISCARD);
352 370
    GLES30.glBeginTransformFeedback(GLES30.GL_POINTS);
353 371
    GLES30.glDrawArrays(GLES30.GL_POINTS, 0, mesh.dataLength);
354 372

  
355
    error = GLES30.glGetError();
373
    int error = GLES30.glGetError();
356 374
    if (error != GLES30.GL_NO_ERROR)
357 375
      {
358 376
      throw new RuntimeException("2 glError 0x" + Integer.toHexString(error));
......
366 384

  
367 385
Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, len, GLES30.GL_MAP_READ_BIT);
368 386
FloatBuffer fb = ((ByteBuffer) mappedBuffer).order(ByteOrder.nativeOrder()).asFloatBuffer();
369
FloatBuffer bb = mesh.mMeshNormals;
387
FloatBuffer bb = mesh.mMeshPositions;
370 388

  
371 389
String msgB = "";
372 390
for(int d=0; d<mesh.dataLength; d++)
373 391
  {
374
  msgB+="("+(2*halfW*bb.get(3*d+0))+","+(2*halfH*bb.get(3*d+1))+","+(bb.get(3*d+2))+")";
392
  msgB+="("+(2*halfW*bb.get(3*d+0))+","+(2*halfH*bb.get(3*d+1))+","+(2*halfZ*bb.get(3*d+2))+")";
375 393
  }
376 394
android.util.Log.d( "Feedback", msgB);
377 395

  
......
392 410
    /// END DEBUG ///////
393 411

  
394 412
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
395
GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 1, 0);
396 413

  
397
    surface.setAsOutput(currTime);
414

  
415

  
416

  
417
    mMainProgram.useProgram();
398 418
    GLES30.glUniform1i(mMainTextureH, 0);
399
    mM.sendZero();
400
    mV.sendZero();
419
    mM.sendFeedback(MAIN_PROGRAM,surface,halfW,halfH,halfZ);
420
    mV.sendZero(MAIN_PROGRAM);
401 421
    mF.send(halfW,halfH);
402 422

  
403 423
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mPosTBO[0]);
src/main/java/org/distorted/library/EffectQueueMatrix.java
46 46
  private static float[] mTmpMatrix = new float[16];
47 47
  private static float[] mViewMatrix= new float[16];
48 48

  
49
  private static int mObjDH;
50
  private static int mMVPMatrixH;
51
  private static int mMVMatrixH;
49
  private static int[] mObjDH       = new int[DistortedEffects.NUM_PROGRAMS];
50
  private static int[] mMVPMatrixH  = new int[DistortedEffects.NUM_PROGRAMS];
51
  private static int[] mMVMatrixH   = new int[DistortedEffects.NUM_PROGRAMS];
52 52
  
53 53
///////////////////////////////////////////////////////////////////////////////////////////////////
54 54
   
......
191 191

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

  
194
  static void getUniforms(int mProgramH)
194
  static void getUniforms(int index,int mProgramH)
195 195
    {
196
    mObjDH     = GLES30.glGetUniformLocation(mProgramH, "u_objD");
197
    mMVPMatrixH= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
198
    mMVMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix");
196
    mObjDH[index]     = GLES30.glGetUniformLocation(mProgramH, "u_objD");
197
    mMVPMatrixH[index]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
198
    mMVMatrixH[index] = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix");
199 199
    }
200 200

  
201 201
///////////////////////////////////////////////////////////////////////////////////////////////////
......
259 259

  
260 260
///////////////////////////////////////////////////////////////////////////////////////////////////
261 261

  
262
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
262
  synchronized void send(int index,DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
263 263
    {
264 264
    constructMatrices(projection,halfX,halfY,halfZ);
265 265

  
266
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
267
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
268
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
266
    GLES30.glUniform3f( mObjDH[index] , halfX, halfY, halfZ);
267
    GLES30.glUniformMatrix4fv(mMVMatrixH[index] , 1, false, mViewMatrix, 0);
268
    GLES30.glUniformMatrix4fv(mMVPMatrixH[index], 1, false, mMVPMatrix , 0);
269 269
    }
270 270

  
271 271
///////////////////////////////////////////////////////////////////////////////////////////////////
272 272

  
273
  synchronized void sendZero()
273
  synchronized void sendFeedback(int index,DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
274 274
    {
275
    Matrix.setIdentityM(mTmpMatrix, 0);
276
    GLES30.glUniform3f( mObjDH , 0.5f, 0.5f, 0.5f);
277
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mTmpMatrix , 0);
278
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
275
    constructMatrices(projection,halfX,halfY,halfZ);
276

  
277
    GLES30.glUniform3f( mObjDH[index] , 0.5f, 0.5f, 0.5f);
278
    GLES30.glUniformMatrix4fv(mMVMatrixH[index] , 1, false, mViewMatrix, 0);
279
    GLES30.glUniformMatrix4fv(mMVPMatrixH[index], 1, false, mMVPMatrix , 0);
279 280
    }
280 281

  
281 282
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/EffectQueueVertex.java
45 45
  private static final int NUM_UNIFORMS = 12;
46 46
  private static final int NUM_CACHE    =  3;
47 47
  private static final int INDEX = EffectTypes.VERTEX.ordinal();
48
  private static int mNumEffectsH;
49
  private static int mTypeH;
50
  private static int mUniformsH;
48
  private static int[] mNumEffectsH = new int[DistortedEffects.NUM_PROGRAMS];
49
  private static int[] mTypeH       = new int[DistortedEffects.NUM_PROGRAMS];
50
  private static int[] mUniformsH   = new int[DistortedEffects.NUM_PROGRAMS];
51 51
  
52 52
///////////////////////////////////////////////////////////////////////////////////////////////////
53 53
   
......
58 58

  
59 59
///////////////////////////////////////////////////////////////////////////////////////////////////
60 60

  
61
  static void getUniforms(int mProgramH)
61
  static void getUniforms(int index, int mProgramH)
62 62
    {
63
    mNumEffectsH= GLES30.glGetUniformLocation( mProgramH, "vNumEffects");
64
    mTypeH      = GLES30.glGetUniformLocation( mProgramH, "vType");
65
    mUniformsH  = GLES30.glGetUniformLocation( mProgramH, "vUniforms");
63
    mNumEffectsH[index]= GLES30.glGetUniformLocation( mProgramH, "vNumEffects");
64
    mTypeH[index]      = GLES30.glGetUniformLocation( mProgramH, "vType");
65
    mUniformsH[index]  = GLES30.glGetUniformLocation( mProgramH, "vUniforms");
66 66
    }
67 67

  
68 68
///////////////////////////////////////////////////////////////////////////////////////////////////
......
133 133
   
134 134
///////////////////////////////////////////////////////////////////////////////////////////////////
135 135

  
136
  synchronized void send(float halfX, float halfY, float halfZ)
136
  synchronized void send(int index, float halfX, float halfY, float halfZ)
137 137
    {
138
    GLES30.glUniform1i( mNumEffectsH, mNumEffects);
138
    GLES30.glUniform1i( mNumEffectsH[index], mNumEffects);
139 139
      
140 140
    if( mNumEffects>0 )
141 141
      {
......
146 146
        mUniforms[NUM_UNIFORMS*i+7] = mCache[NUM_CACHE*i+2]-halfZ;
147 147
        }
148 148

  
149
      GLES30.glUniform1iv( mTypeH    ,                 mNumEffects, mName    ,0);
150
      GLES30.glUniform4fv( mUniformsH,(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
149
      GLES30.glUniform1iv( mTypeH[index]    ,                 mNumEffects, mName    ,0);
150
      GLES30.glUniform4fv( mUniformsH[index],(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
151 151
      }
152 152
    }
153 153

  
154 154
///////////////////////////////////////////////////////////////////////////////////////////////////
155 155

  
156
  synchronized void sendZero()
156
  synchronized void sendZero(int index)
157 157
    {
158
    GLES30.glUniform1i( mNumEffectsH, 0);
158
    GLES30.glUniform1i( mNumEffectsH[index], 0);
159 159
    }
160 160

  
161 161
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/MeshObject.java
45 45
   int[] mTexVBO = new int[1];
46 46

  
47 47
   int[] mPosTBO = new int[1]; // Transform Feedback
48
   int[] mNorTBO = new int[1]; // Transform Feedback
49 48

  
50 49
   final float zFactor; // strange workaround for the fact that we need to somehow store the 'depth'
51 50
                        // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
......
84 83
       {
85 84
       GLES30.glGenBuffers(1, mTexVBO, 0);
86 85
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mTexVBO[0]);
87
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*     TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture  , GLES30.GL_STATIC_DRAW);
86
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, dataLength*    TEX_DATA_SIZE*BYTES_PER_FLOAT, mMeshTexture  , GLES30.GL_STATIC_DRAW);
88 87
       }
89 88

  
90 89
     if( mPosTBO[0]<0 )
......
93 92
       GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, null, GLES30.GL_STATIC_READ);
94 93
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
95 94
       }
96
     if( mNorTBO[0]<0 )
97
       {
98
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mNorTBO[0]);
99
       GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, dataLength*  NORMAL_DATA_SIZE*BYTES_PER_FLOAT, null, GLES30.GL_STATIC_READ);
100
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
101
       }
102 95

  
103 96
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
104 97
     }
......
128 121
       GLES30.glDeleteBuffers(1, mPosTBO, 0);
129 122
       mPosTBO[0] = -1;
130 123
       }
131
     if( mNorTBO[0]>=0 )
132
       {
133
       GLES30.glDeleteBuffers(1, mNorTBO, 0);
134
       mNorTBO[0] = -1;
135
       }
136 124
     }
137 125

  
138 126
///////////////////////////////////////////////////////////////////////////////////////////////////
......
143 131
     mNorVBO[0] = -1;
144 132
     mTexVBO[0] = -1;
145 133
     mPosTBO[0] = -1;
146
     mNorTBO[0] = -1;
147 134
     }
148 135

  
149 136
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/program/DistortedProgram.java
58 58

  
59 59
      if( feedbackVaryings!=null )
60 60
        {
61
        GLES30.glTransformFeedbackVaryings(programHandle, feedbackVaryings, GLES30.GL_SEPARATE_ATTRIBS);
61
        GLES30.glTransformFeedbackVaryings(programHandle, feedbackVaryings, GLES30.GL_INTERLEAVED_ATTRIBS);
62 62
        }
63 63

  
64 64
      if (attributes != null)
src/main/res/raw/main_vertex_shader.glsl
554 554
    j+=3;
555 555
    }
556 556
#endif
557

  
558
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
557
   
559 558
  v_Position      = v;
560 559
  v_TexCoordinate = a_TexCoordinate;
560
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
561 561
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
562 562
  }                               

Also available in: Unified diff