Project

General

Profile

« Previous | Next » 

Revision 79921db2

Added by Leszek Koltunski over 7 years ago

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

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

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

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

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

  
98

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

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

  
157
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL);
149
    String[] feedback = { "v_Position", "v_Normal" };
150

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

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

  
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

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

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

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

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

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

  
356
    mFeedbackProgram.useProgram();
357
    surface.setAsOutput(currTime);
358
    mM.send(FEED_PROGRAM,surface,halfW,halfH,halfZ);
359
    mV.send(FEED_PROGRAM,halfW,halfH,halfZ);
337
    mMainProgram.useProgram();
338

  
339
    mM.send(surface,halfW,halfH,halfZ);
340
    mV.send(halfW,halfH,halfZ);
360 341

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

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

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

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

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

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

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

  
414

  
415

  
416

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

  
423 403
    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       = 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];
49
  private static int mObjDH;
50
  private static int mMVPMatrixH;
51
  private static int mMVMatrixH;
52 52
  
53 53
///////////////////////////////////////////////////////////////////////////////////////////////////
54 54
   
......
191 191

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

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

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

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

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

  
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);
266
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
267
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
268
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
269 269
    }
270 270

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

  
273
  synchronized void sendFeedback(int index,DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
273
  synchronized void sendZero()
274 274
    {
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);
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);
280 279
    }
281 280

  
282 281
///////////////////////////////////////////////////////////////////////////////////////////////////
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 = 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];
48
  private static int mNumEffectsH;
49
  private static int mTypeH;
50
  private static int mUniformsH;
51 51
  
52 52
///////////////////////////////////////////////////////////////////////////////////////////////////
53 53
   
......
58 58

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

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

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

  
136
  synchronized void send(int index, float halfX, float halfY, float halfZ)
136
  synchronized void send(float halfX, float halfY, float halfZ)
137 137
    {
138
    GLES30.glUniform1i( mNumEffectsH[index], mNumEffects);
138
    GLES30.glUniform1i( mNumEffectsH, 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[index]    ,                 mNumEffects, mName    ,0);
150
      GLES30.glUniform4fv( mUniformsH[index],(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
149
      GLES30.glUniform1iv( mTypeH    ,                 mNumEffects, mName    ,0);
150
      GLES30.glUniform4fv( mUniformsH,(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
151 151
      }
152 152
    }
153 153

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

  
156
  synchronized void sendZero(int index)
156
  synchronized void sendZero()
157 157
    {
158
    GLES30.glUniform1i( mNumEffectsH[index], 0);
158
    GLES30.glUniform1i( mNumEffectsH, 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
48 49

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

  
89 90
     if( mPosTBO[0]<0 )
......
92 93
       GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, dataLength*POSITION_DATA_SIZE*BYTES_PER_FLOAT, null, GLES30.GL_STATIC_READ);
93 94
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
94 95
       }
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
       }
95 102

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

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

  
136 149
///////////////////////////////////////////////////////////////////////////////////////////////////
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_INTERLEAVED_ATTRIBS);
61
        GLES30.glTransformFeedbackVaryings(programHandle, feedbackVaryings, GLES30.GL_SEPARATE_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
   
557

  
558
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
558 559
  v_Position      = v;
559 560
  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