Project

General

Profile

« Previous | Next » 

Revision cab7c165

Added by Leszek Koltunski over 7 years ago

Beginning of support for actual Transform Feedback.

View differences:

src/main/java/org/distorted/library/DistortedEffects.java
38 38
import org.distorted.library.type.Static3D;
39 39

  
40 40
import java.io.InputStream;
41
import java.nio.Buffer;
41 42
import java.nio.ByteBuffer;
42 43
import java.nio.ByteOrder;
43 44
import java.nio.FloatBuffer;
......
103 104
  static void createProgram(Resources resources)
104 105
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
105 106
    {
107
    // MAIN PROGRAM ////////////////////////////////////
106 108
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
107 109
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
108 110

  
......
140 142
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
141 143
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
142 144

  
143
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL);
145
    String[] feedback = { "v_Position","v_Normal","v_TexCoordinate" };
146

  
147
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback);
144 148

  
145 149
    int mainProgramH = mMainProgram.getProgramHandle();
146 150
    EffectQueueFragment.getUniforms(mainProgramH);
......
169 173
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
170 174
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
171 175

  
172
    // DEBUG ONLY //////////////////////////////////////
176
    // DEBUG PROGRAM //////////////////////////////////////
173 177
    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
174 178
    final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
175 179

  
......
184 188
      }
185 189

  
186 190
    int debugProgramH = mDebugProgram.getProgramHandle();
187
    mDebugObjDH = GLES30.glGetUniformLocation( debugProgramH, "u_objD");
191
    mDebugObjDH      = GLES30.glGetUniformLocation( debugProgramH, "u_objD");
188 192
    mDebugMVPMatrixH = GLES30.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
189
    // END DEBUG  //////////////////////////////////////
190 193
    }
191 194

  
192 195
///////////////////////////////////////////////////////////////////////////////////////////////////
......
280 283
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
281 284
    }
282 285

  
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

  
288
  private void displayTransformFeedback(MeshObject mesh)
289
    {
290
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
291
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
292

  
293
    DistortedRenderState.switchOffDrawing();
294
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
295
    DistortedRenderState.restoreDrawing();
296

  
297
    int error = GLES30.glGetError();
298

  
299
      if( error != GLES30.GL_NO_ERROR )
300
        {
301
        throw new RuntimeException("DrawArrays: glError 0x" + Integer.toHexString(error));
302
        }
303

  
304
    int size = (MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE+MeshObject.TEX_DATA_SIZE)*mesh.numVertices;
305

  
306
    Buffer mappedBuffer =  GLES30.glMapBufferRange(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 4*size, GLES30.GL_MAP_READ_BIT);
307
    FloatBuffer fb = ((ByteBuffer) mappedBuffer).order(ByteOrder.nativeOrder()).asFloatBuffer();
308
    String msg = "";
309

  
310
    for(int i=0; i<size; i++) msg += (" "+fb.get(i));
311

  
312
    android.util.Log.d( "Feedback", msg);
313

  
314
    GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER);
315
    GLES30.glEndTransformFeedback();
316
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
317
    }
318

  
283 319
///////////////////////////////////////////////////////////////////////////////////////////////////
284 320

  
285 321
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
......
293 329
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
294 330

  
295 331
    mMainProgram.useProgram();
296
    GLES30.glUniform1i(mMainTextureH, 0);
297 332
    surface.setAsOutput(currTime);
333
    GLES30.glUniform1i(mMainTextureH, 0);
334

  
335
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
336
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
337
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
338
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
339
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0 );
340

  
298 341
    mM.send(surface,halfW,halfH,halfZ);
299 342
    mV.send(halfW,halfH,halfZ);
343

  
344
    displayTransformFeedback(mesh);
345

  
300 346
    mF.send(halfW,halfH);
301 347

  
302
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
303
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
304
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
305
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
306 348
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
307
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0 );
308 349

  
309 350
    /// DEBUG ONLY //////
310 351
    // displayBoundingRect(halfW, halfH, halfZ, surface, mM.getMVP(), mesh.getBoundingVertices() );
src/main/java/org/distorted/library/DistortedRenderState.java
124 124
      }
125 125
    }
126 126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

  
129
  static void switchOffDrawing()
130
    {
131
    GLES30.glEnable(GLES30.GL_SCISSOR_TEST);
132
    GLES30.glScissor(0,0,0,0);
133
    }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

  
137
  static void restoreDrawing()
138
    {
139
    GLES30.glDisable(GLES30.GL_SCISSOR_TEST);
140
    }
141

  
127 142
///////////////////////////////////////////////////////////////////////////////////////////////////
128 143

  
129 144
  void apply()
src/main/java/org/distorted/library/MeshCubes.java
891 891
  private void build(boolean frontOnly)
892 892
     {
893 893
     int vertSoFar=0;
894
     float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
894
     float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
895 895

  
896 896
     //android.util.Log.d("MeshCubes","building front grid...");
897 897

  
src/main/java/org/distorted/library/MeshFlat.java
188 188
    super(0.0f);
189 189
    computeNumberOfVertices(cols,rows);
190 190

  
191
    float[] attribs= new float[(POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
191
    float[] attribs= new float[(POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*numVertices];
192 192

  
193 193
    buildGrid(attribs);
194 194

  
src/main/java/org/distorted/library/MeshObject.java
35 35
   {
36 36
   private static final int BYTES_PER_FLOAT = 4;
37 37

  
38
   static final int POSITION_DATA_SIZE= 3;
39
   static final int NORMAL_DATA_SIZE  = 3;
40
   static final int TEX_DATA_SIZE     = 2;
38
   static final int POS_DATA_SIZE= 3;
39
   static final int NOR_DATA_SIZE= 3;
40
   static final int TEX_DATA_SIZE= 2;
41 41

  
42
   static final int OFFSET0 =                                                                   0;
43
   static final int OFFSET1 = (POSITION_DATA_SIZE                               )*BYTES_PER_FLOAT;
44
   static final int OFFSET2 = (POSITION_DATA_SIZE+NORMAL_DATA_SIZE              )*BYTES_PER_FLOAT;
45
   static final int VERTSIZE= (POSITION_DATA_SIZE+NORMAL_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
42
   static final int OFFSET0 =                                                           0;
43
   static final int OFFSET1 = (POS_DATA_SIZE                            )*BYTES_PER_FLOAT;
44
   static final int OFFSET2 = (POS_DATA_SIZE+NOR_DATA_SIZE              )*BYTES_PER_FLOAT;
45
   static final int VERTSIZE= (POS_DATA_SIZE+NOR_DATA_SIZE+TEX_DATA_SIZE)*BYTES_PER_FLOAT;
46 46

  
47 47
   int numVertices;
48 48
   FloatBuffer mVertAttribs;   // packed: PosX,PosY,PosZ, NorX, NorY,NorZ, TexS, TexT
49 49
   int[] mAttVBO = new int[1]; // server-side packed vertex attributes
50
   int[] mAttTFO = new int[1]; // transform feedback
50 51

  
51 52
   final float zFactor;        // strange workaround for the fact that we need to somehow store the 'depth'
52 53
                               // of the Mesh. Used in DistortedEffects. See DistortedTexture.getDepth().
......
76 77
       GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, numVertices*VERTSIZE, mVertAttribs, GLES30.GL_STATIC_READ);
77 78
       GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
78 79
       }
80
     if( mAttTFO[0]<0 )
81
       {
82
       GLES30.glGenBuffers(1, mAttTFO, 0);
83
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, mAttTFO[0]);
84
       GLES30.glBufferData(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, numVertices*VERTSIZE, null, GLES30.GL_STATIC_READ);
85
       GLES30.glBindBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0);
86
       }
79 87
     }
80 88

  
81 89
///////////////////////////////////////////////////////////////////////////////////////////////////
......
88 96
       GLES30.glDeleteBuffers(1, mAttVBO, 0);
89 97
       mAttVBO[0] = -1;
90 98
       }
99
     if( mAttTFO[0]>=0 )
100
       {
101
       GLES30.glDeleteBuffers(1, mAttTFO, 0);
102
       mAttTFO[0] = -1;
103
       }
91 104
     }
92 105

  
93 106
///////////////////////////////////////////////////////////////////////////////////////////////////
......
95 108
   void recreate()
96 109
     {
97 110
     mAttVBO[0] = -1;
111
     mAttTFO[0] = -1;
98 112
     }
99 113

  
100 114
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff