Project

General

Profile

« Previous | Next » 

Revision f01ab878

Added by Leszek Koltunski 10 days ago

bugfix in the Library:
Before, the mapping of the attributes in various shaders to the MeshBase.bindVertexAttribs() was shaky.
In fact incorrect in case of the Preprocess program. Improve that!

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffectBlurred.java
227 227
    {
228 228
    final String vertex =
229 229

  
230
      "precision lowp float;  \n"+
231
      "in vec2 a_Position;    \n"+
232
      "in vec2 a_TexCoord;    \n"+
233
      "out vec2 v_TexCoord;   \n"+
234
      "uniform float u_Depth; \n"+
235
      "uniform vec2 u_TexCorr;\n"+
230
      "precision lowp float;     \n"+
231
      "in vec2 a_Position;       \n"+
232
      "in vec2 a_TexCoordinate;  \n"+
233
      "out vec2 v_TexCoordinate; \n"+
234
      "uniform float u_Depth;    \n"+
235
      "uniform vec2 u_TexCorr;   \n"+
236 236

  
237 237
      "void main()                                      \n"+
238 238
      "  {                                              \n"+
239
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
239
      "  v_TexCoordinate = a_TexCoordinate * u_TexCorr; \n"+
240 240
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
241 241
      "  }";
242 242

  
......
244 244

  
245 245
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
246 246
      "precision lowp float;              \n"+
247
      "in vec2 v_TexCoord;                \n"+
247
      "in vec2 v_TexCoordinate;           \n"+
248 248
      "out vec4 fragColor;                \n"+
249 249
      "uniform sampler2D u_ColorTexture;  \n"+
250 250
      "uniform float u_Offsets[MAX_BLUR]; \n"+
251 251
      "uniform float u_Weights[MAX_BLUR]; \n"+
252 252
      "uniform int u_Radius;              \n"+
253 253

  
254
      "void main()                                                                                           \n"+
255
      "  {                                                                                                   \n"+
256
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
257
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
258
      "    {                                                                                                 \n"+
259
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
260
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
261
      "    }                                                                                                 \n"+
262
      "  fragColor = pixel;                                                                                  \n"+
254
      "void main()                                                                                                     \n"+
255
      "  {                                                                                                             \n"+
256
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];                                           \n"+
257
      "  for (int i=1; i<=u_Radius; i+=1)                                                                              \n"+
258
      "    {                                                                                                           \n"+
259
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x+u_Offsets[i],v_TexCoordinate.y)) +                 \n"+
260
      "               texture(u_ColorTexture,vec2(v_TexCoordinate.x-u_Offsets[i],v_TexCoordinate.y)) ) * u_Weights[i]; \n"+
261
      "    }                                                                                                           \n"+
262
      "  fragColor = pixel;                                                                                            \n"+
263 263
      "  }";
264 264

  
265 265
    final String fragment2 =
266 266

  
267 267
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
268 268
      "precision lowp float;              \n"+
269
      "in vec2 v_TexCoord;                \n"+
269
      "in vec2 v_TexCoordinate;           \n"+
270 270
      "out vec4 fragColor;                \n"+
271 271
      "uniform sampler2D u_ColorTexture;  \n"+
272 272
      "uniform float u_Offsets[MAX_BLUR]; \n"+
273 273
      "uniform float u_Weights[MAX_BLUR]; \n"+
274 274
      "uniform int u_Radius;              \n"+
275 275

  
276
      "void main()                                                                                           \n"+
277
      "  {                                                                                                   \n"+
278
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
279
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
280
      "    {                                                                                                 \n"+
281
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
282
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
283
      "    }                                                                                                 \n"+
284
      "  fragColor = pixel;                                                                                  \n"+
276
      "void main()                                                                                                     \n"+
277
      "  {                                                                                                             \n"+
278
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoordinate) * u_Weights[0];                                           \n"+
279
      "  for (int i=1; i<=u_Radius; i+=1)                                                                              \n"+
280
      "    {                                                                                                           \n"+
281
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoordinate.x,v_TexCoordinate.y+u_Offsets[i])) +                 \n"+
282
      "               texture(u_ColorTexture,vec2(v_TexCoordinate.x,v_TexCoordinate.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
283
      "    }                                                                                                           \n"+
284
      "  fragColor = pixel;                                                                                            \n"+
285 285
      "  }";
286 286

  
287 287
    mIndex1 = PostprocessEffect.register(prog1, vertex,fragment1);
src/main/java/org/distorted/library/effectqueue/EffectQueue.java
174 174
    {
175 175
    ((EffectQueueMatrix  )queues[0]).send(distance, mipmap, projection, variant);
176 176
    ((EffectQueueVertex  )queues[1]).send(inflate, programH, variant);
177
    ((EffectQueueFragment)queues[2]).send(programH, variant);
177
    ((EffectQueueFragment)queues[2]).send(variant);
178 178
    }
179 179

  
180 180
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/effectqueue/EffectQueueFragment.java
82 82

  
83 83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 84
  
85
  void send(int programH, int variant)
85
  void send(int variant)
86 86
    {
87 87
    GLES30.glUniform1i( mNumEffectsH[variant], mNumEffects);
88 88

  
src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
131 131
    String mainFragHeader= version + "\n";
132 132

  
133 133
    mainVertHeader += "#define MAX_COMPON " + MeshBase.getMaxEffComponents() + "\n";
134
    if( MeshBase.getUseCenters() ) mainVertHeader += "#define COMP_CENTERS\n";
134
    if( MeshBase.getUseCenters() )      mainVertHeader += "#define COMP_CENTERS\n";
135
    if( DistortedLibrary.isUBOBuggy() ) mainVertHeader += "#define BUGGY_UBOS\n";
135 136

  
136 137
    String enabledEffectV= VertexEffect.getGLSL();
137 138

  
src/main/java/org/distorted/library/main/DistortedLibrary.java
616 616
      GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
617 617

  
618 618
      mMainProgram.useProgram();
619
      GLES30.glUniform1i(DistortedLibrary.mMainTextureH, 0);
620
      mesh.bindVertexAttribs(DistortedLibrary.mMainProgram);
619
      GLES30.glUniform1i(mMainTextureH, 0);
620
      mesh.bindVertexAttribs(mMainProgram);
621 621
      mesh.send(mMainProgramH,0);
622 622

  
623 623
      float inflate     = mesh.getInflate();
......
1002 1002
    return mGLSL;
1003 1003
    }
1004 1004

  
1005
///////////////////////////////////////////////////////////////////////////////////////////////////
1006
/**
1007
 * Are we running this on hardware where UBOs are buggy?
1008
 */
1009
  public static boolean isUBOBuggy()
1010
    {
1011
    return mBuggyUBOs;
1012
    }
1013

  
1005 1014
///////////////////////////////////////////////////////////////////////////////////////////////////
1006 1015
/**
1007 1016
 * When OpenGL context gets created, call this method so that the library can initialise its internal data structures.
src/main/java/org/distorted/library/mesh/MeshBase.java
858 858
     int index2 = mVBO2.createImmediatelyFloat(mNumVertices*VERT2_SIZE, mVertAttribs2);
859 859
     int[] attr = program.mAttribute;
860 860

  
861
     switch( attr.length )
861
     switch(program.mAttributeLayout )
862 862
       {
863
       // 'normal' case
864
       case 4: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
863
       case DistortedProgram.ATTR_LAYOUT_PNTC:
864
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
865 865
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
866 866
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
867 867
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
868 868
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
869 869
               GLES30.glVertexAttribPointer(attr[3], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
870 870
               break;
871
       // if we are not using component centers and there are no vertex effects enabled,
872
       // then component attribute does not exist in the Vertex Shader
873
       case 3: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
871
       case DistortedProgram.ATTR_LAYOUT_PNT:
872
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
874 873
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
875 874
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
876 875
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
877 876
               GLES30.glVertexAttribPointer(attr[2], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
878 877
               break;
879
       // pre program used in EffectQueuePostprocessing does not have v_Normal attribute
880
       // (it is not used in the fragment shader)
881
       case 2: GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
878
       case DistortedProgram.ATTR_LAYOUT_PNC:
879
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
880
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
881
               GLES30.glVertexAttribPointer(attr[1], NOR_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_NOR);
882
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
883
               GLES30.glVertexAttribPointer(attr[2], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
884
               break;
885
       case DistortedProgram.ATTR_LAYOUT_PTC:
886
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
887
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
888
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
889
               GLES30.glVertexAttribPointer(attr[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
890
               GLES30.glVertexAttribPointer(attr[2], COM_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_COM);
891
               break;
892
       case DistortedProgram.ATTR_LAYOUT_PT:
893
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index1 );
882 894
               GLES30.glVertexAttribPointer(attr[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, VERT1_SIZE, OFFSET_POS);
883 895
               GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, index2 );
884 896
               GLES30.glVertexAttribPointer(attr[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, VERT2_SIZE, OFFSET_TEX);
885 897
               break;
898
       case DistortedProgram.ATTR_LAYOUT_UNK:
899
               DistortedLibrary.logMessage("Unknown attribute layout: "+program.mAttributeLayout);
886 900
       }
887 901

  
888 902
     GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
src/main/java/org/distorted/library/program/DistortedProgram.java
35 35
 */
36 36
public class DistortedProgram
37 37
  {
38
  public static final int ATTR_LAYOUT_PNTC = 0;
39
  public static final int ATTR_LAYOUT_PTC  = 1;
40
  public static final int ATTR_LAYOUT_PNT  = 2;
41
  public static final int ATTR_LAYOUT_PNC  = 3;
42
  public static final int ATTR_LAYOUT_PT   = 4;
43
  public static final int ATTR_LAYOUT_P    = 5;
44
  public static final int ATTR_LAYOUT_UNK  = 6;
45

  
38 46
  private String mAttributeStr, mUniformStr, mUniList;
39 47
  private int mAttributeLen, mUniformLen;
40 48
  private int mNumAttributes;
......
42 50
  private String[] mAttributeName;
43 51
  private final int mProgramHandle;
44 52

  
53
/**
54
 * Various programs have different attributes (because even if the source is the same, some of them might
55
 * be unused).
56
 * Main, Full have 4 attributes in the order (position,normal,texcoord, component)
57
 * Pre has 3 attributes (position,texcoord,component)
58
 * Maybe there are other possibilities (only position and normal? 3- position,normal,texcoord?)
59
 */
60
  public int mAttributeLayout;
45 61
/**
46 62
 * List of Attributes (OpenGL ES 3.0: 'in' variables), in the same order as declared in the shader source.
47 63
 */
......
211 227

  
212 228
    for (String line : lines)
213 229
      {
214
      if (doAttributes)
230
      if( doAttributes )
215 231
        {
216 232
        attribute = parseOutAttribute(line);
217 233

  
218 234
        if (attribute != null)
219 235
          {
220
          if (attrList.length() > 0) attrList += " ";
236
          if( !attrList.isEmpty() ) attrList += " ";
221 237
          attrList += attribute;
222 238
          }
223 239
        }
......
226 242

  
227 243
      if (uniform != null)
228 244
        {
229
        if (mUniList.length() > 0) mUniList += " ";
245
        if( !mUniList.isEmpty() ) mUniList += " ";
230 246
        mUniList += uniform;
231 247
        }
232 248
      }
......
262 278
          if( attribute!=null )
263 279
            {
264 280
            //DistortedLibrary.logMessage("DistortedProgram: new attribute: "+attribute);
265
            if( attrList.length()>0 ) attrList+=" ";
281
            if( !attrList.isEmpty() ) attrList += " ";
266 282
            attrList += attribute;
267 283
            }
268 284
          }
......
375 391
      {
376 392
      mAttribute = att;
377 393
      }
394

  
395
    setUpAttributeLayout();
396
    }
397

  
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

  
400
  private void setUpAttributeLayout()
401
    {
402
    switch(mNumAttributes)
403
      {
404
      case 4: mAttributeLayout = ATTR_LAYOUT_PNTC;
405
              break;
406
      case 3: if( mAttributeName[2].equals("a_TexCoordinate") ) mAttributeLayout = ATTR_LAYOUT_PNT;
407
              else if( mAttributeName[2].equals("a_Component") )
408
                {
409
                if( mAttributeName[1].equals("a_TexCoordinate") ) mAttributeLayout = ATTR_LAYOUT_PTC;
410
                else if( mAttributeName[1].equals("a_Normal"  ) ) mAttributeLayout = ATTR_LAYOUT_PNC;
411
                else
412
                  {
413
                  mAttributeLayout = ATTR_LAYOUT_UNK;
414
                  DistortedLibrary.logMessage("DistortedProgram: 1 Error in attribute layout: "+mAttributeName[1]);
415
                  }
416
                }
417
              else
418
                {
419
                mAttributeLayout = ATTR_LAYOUT_UNK;
420
                DistortedLibrary.logMessage("DistortedProgram: 2 Error in attribute layout: "+mAttributeName[2]);
421
                }
422
              break;
423
      case 2: if( mAttributeName[1].equals("a_TexCoordinate") ) mAttributeLayout = ATTR_LAYOUT_PT;
424
              else
425
                {
426
                mAttributeLayout = ATTR_LAYOUT_UNK;
427
                DistortedLibrary.logMessage("DistortedProgram: 3 Error in attribute layout: "+mAttributeName[1]);
428
                }
429
              break;
430
      case 1: if( mAttributeName[0].equals("a_Position") ) mAttributeLayout = ATTR_LAYOUT_P;
431
              else
432
                {
433
                mAttributeLayout = ATTR_LAYOUT_UNK;
434
                DistortedLibrary.logMessage("DistortedProgram: 4 Error in attribute layout: "+mAttributeName[0]);
435
                }
436
              break;
437
      default:mAttributeLayout = ATTR_LAYOUT_UNK;
438
              DistortedLibrary.logMessage("DistortedProgram: 5 Error in attribute layout: "+mNumAttributes);
439
      }
378 440
    }
379 441

  
380 442
///////////////////////////////////////////////////////////////////////////////////////////////////
......
500 562
///////////////////////////////////////////////////////////////////////////////////////////////////
501 563
/**
502 564
 * Use the program and enable all vertex attribute arrays.
503
 *
504 565
 * Needs to be called from a thread holding the OpenGL context.
505 566
 */
506 567
  public void useProgram()
......
516 577
///////////////////////////////////////////////////////////////////////////////////////////////////
517 578
/**
518 579
 * Disable all vertex attribute arrays.
519
 *
520 580
 * Needs to be called from a thread holding the OpenGL context.
521 581
 */
522 582
  public void stopUsingProgram()

Also available in: Unified diff