gl_PointSize and uniform array problem

  ... discussions regarding GLES2 ShaderPrograms (VertexShaders and FragmentShaders).

gl_PointSize and uniform array problem

Postby Andruum » Wed Aug 14, 2013 3:57 pm

Hi all. I have questions :
1) function gl_PointSize not work in Vertex ShaderProgramm, what i do wrong?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static final String VERTEXSHADER =
  2.                         "uniform mat4 u_modelViewProjectionMatrix;\n" +
  3.                         "attribute vec4 a_position;\n" +
  4.                         "attribute vec2 a_textureCoordinates;\n" +
  5.                         "varying vec2 v_textureCoordinates;\n" +
  6.                         "void main() {\n" +
  7.                         "       v_textureCoordinates = a_textureCoordinates;\n" +
  8.                         "       gl_PointSize = 10.0;\n" +
  9.                         "       gl_Position =  u_modelViewProjectionMatrix * a_position;\n" +
  10.                         "}";
Parsed in 0.018 seconds, using GeSHi 1.0.8.4


2) Not working uniform arrays.
array: "uniform vec3 u_objectsdata[10]"
get location(not work): sUniformObjects = this.getUniformLocation("u_objectsdata");
get location(work): sUniformObjects = this.getUniformLocation("u_objectsdata[0]");
Log:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. (2416): org.andengine.opengl.shader.exception.ShaderProgramException: Unexpected uniform: 'u_objectsdata'. Existing uniforms: {u_objectsdata[0]=1, u_modelViewProjectionMatrix=0}
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

Available only element with index = 0.
Andruum
 
Posts: 3
Joined: Wed Aug 14, 2013 3:47 pm

Re: gl_PointSize and uniform array problem

Postby alb » Thu Aug 15, 2013 1:35 pm

Hi,

Shaders are a nightmare to debug in my experience.

I think the following are the important parts of using uniforms:

First declare a string to use as a name

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private static final String UNIFORM_OBJECTSDATA = "u_objectsdata";
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


And an int location (this is initially -1 but will change when you call getUniformLocation()). I think this might be the location in an array of all the uniforms.

Syntax: [ Download ] [ Hide ]
  1. public static int sObjectsData = ShaderProgramConstants.LOCATION_INVALID; 


In the link method, get the uniform location

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MyShader.sObjectsData = this.getUniformLocation(MyShader.UNIFORM_OBJECTSDATA);
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


In the bind method, you connect the location with the data in your code

Syntax: [ Download ] [ Hide ]
  1. GLES20.glUniform1f(MyShader.sObjectsData, MainActivity.someFloat); 


or

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. GLES20.glUniform3f(MyShader.sObjectsData, MainActivity.someFloat, MainActivity.someOtherFloat, MainActivity.aThirdFloat);
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


Use glUniform1f for one float - this will be declared as uniform float... in the vertex shader.
Use glUniform3f for three floats - this would be declared as uniform vec3... in the vertex shader.

Finally in the actual vertex shader you need to use the uniform. I'm not certain about this but I think it will always crash if you've declared a uniform (given it a location) and not used it in your shader.

I hope that helps.

Edit: I forgot to mention that I don't know anything about using gl_PointSize.
alb
 
Posts: 134
Joined: Tue Jan 29, 2013 10:45 pm

Re: gl_PointSize and uniform array problem

Postby Andruum » Thu Aug 15, 2013 5:10 pm

I understand it. Not working Uniform Arrays - "uniform vec3 u_objectsdata[10]" . Me need send array in shader programm. If i use simple variables ("uniform vec u_objectsdata") it work, but it not need for me.
Andruum
 
Posts: 3
Joined: Wed Aug 14, 2013 3:47 pm

Re: gl_PointSize and uniform array problem

Postby alb » Thu Aug 15, 2013 5:28 pm

As far as I know, you can only use float, vec2, vec3, vec4 or mat4 for uniforms.

If you can post your entire shader class and a description of what you're trying to do, I might be able to offer some better advice.
alb
 
Posts: 134
Joined: Tue Jan 29, 2013 10:45 pm

Re: gl_PointSize and uniform array problem

Postby Andruum » Thu Aug 15, 2013 8:06 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static class FogofWarShaderProgram extends ShaderProgram {
  2.                
  3.                 private static FogofWarShaderProgram INSTANCE;
  4.                
  5.                
  6.                 public static final String VERTEXSHADER =
  7.                         "uniform mat4 u_modelViewProjectionMatrix;\n" +
  8.                         "attribute vec4 a_position;\n" +
  9.                         "attribute vec2 a_textureCoordinates;\n" +
  10.                         "varying vec2 v_textureCoordinates;\n" +
  11.                         "void main() {\n" +
  12.                         "       v_textureCoordinates = a_textureCoordinates;\n" +
  13.                         "       gl_Position =  u_modelViewProjectionMatrix * a_position;\n" +
  14.                         "}";
  15.  
  16.  
  17.                 public static String FRAGMENTSHADER =
  18.                                 "precision lowp float;\n" +
  19.                                 "varying mediump vec2 v_textureCoordinates;\n"+
  20.                                 "uniform vec2 u_fog_size;\n" +
  21.                                 "uniform float u_lookrange;\n" +
  22.                                 "uniform float u_fog_border;\n" +
  23.                                 "uniform int u_objectsize;\n" +
  24.                                 "uniform lowp vec3 u_objects[30];\n" +
  25.                                 "const float alpha_fog = 0.4;\n" +
  26.                                        
  27.                                 "vec3 cur_object;\n" +
  28.                                 "float cur_alpha = alpha_fog;\n" +
  29.                                 "bool visible = false;\n"+
  30.                                 "void calculate(){\n" +
  31.                                                 "       bool border = false;\n" +
  32.                                                 "       float alpha = alpha_fog;\n" +
  33.                                                 "       float dist = sqrt(pow(v_textureCoordinates.x*u_fog_size.x-cur_object.x,2.0) + pow(v_textureCoordinates.y*u_fog_size.y-cur_object.y,2.0));\n" +
  34.                                                 "       if(dist<u_lookrange){\n" +
  35.                                                 "               alpha = 0.0;\n" +
  36.                                                 "               visible = true;\n" +
  37.                                                 "       }\n"+
  38.                                                 "       else if(dist<(u_lookrange+u_fog_border)){\n" +
  39.                                                 "               alpha = alpha_fog*((dist-u_lookrange)/u_fog_border);\n" +
  40.                                                 "               border = true;\n" +
  41.                                                 "       }\n"+
  42.                                                 "       if(visible)cur_alpha = 0.0;\n"+
  43.                                                 "       else if(border && cur_alpha == alpha_fog) cur_alpha = alpha;\n"+
  44.                                                 "       else if(border && cur_alpha < alpha_fog) cur_alpha = min(cur_alpha,alpha);\n"+
  45.                                                 "}\n" +
  46.                                                
  47.                                                
  48.                                                 "void main() {\n" +
  49.                                                 "       for(int i = 0;i<u_objectsize;i++){\n" +
  50.                                                 "               if(visible){\n" +
  51.                                                 "                       break;\n" +
  52.                                                 "               }\n" +
  53.                                                 "               else {\n" +
  54.                                                 "                       cur_object = u_objects[i];\n" +
  55.                                                 "                       calculate();\n" +
  56.                                                 "               }\n" +
  57.                                                 "       }\n" +
  58.                                                 "       gl_FragColor = vec4(0,0,0,cur_alpha);\n" +
  59.                                                 "}";
  60.  
  61.                 ;
  62.                        
  63.                        
  64.  
  65.  
  66.                 public static int sUniformModelViewPositionMatrixLocation = ShaderProgramConstants.LOCATION_INVALID;
  67.                 public static int sUniformFogSize = ShaderProgramConstants.LOCATION_INVALID;
  68.                 public static int sUniformFogBorder = ShaderProgramConstants.LOCATION_INVALID;
  69.                 public static int sUniformLookRange = ShaderProgramConstants.LOCATION_INVALID;
  70.                 public static int sUniformObjectSize = ShaderProgramConstants.LOCATION_INVALID;
  71.                
  72.  
  73.                 private FogofWarShaderProgram() {
  74.                         super(FogofWarShaderProgram.VERTEXSHADER, FogofWarShaderProgram.FRAGMENTSHADER);
  75.                 }
  76.  
  77.                 public static FogofWarShaderProgram getInstance() {
  78.                         if(FogofWarShaderProgram.INSTANCE == null) {
  79.                                 FogofWarShaderProgram.INSTANCE = new FogofWarShaderProgram();
  80.                         }
  81.                         return FogofWarShaderProgram.INSTANCE;
  82.                 }
  83.                
  84.  
  85.                 protected void link(final GLState pGLState) throws ShaderProgramLinkException {
  86.                         GLES20.glBindAttribLocation(this.mProgramID, 0, "a_position");
  87.                         GLES20.glBindAttribLocation(this.mProgramID, 3, "a_textureCoordinates");
  88.                         super.link(pGLState);
  89.                         FogofWarShaderProgram.sUniformModelViewPositionMatrixLocation = this.getUniformLocation("u_modelViewProjectionMatrix");
  90.                         FogofWarShaderProgram.sUniformFogSize = this.getUniformLocation("u_fog_size");
  91.                         FogofWarShaderProgram.sUniformFogBorder = this.getUniformLocation("u_fog_border");
  92.                         FogofWarShaderProgram.sUniformLookRange = this.getUniformLocation("u_lookrange");
  93.                         FogofWarShaderProgram.sUniformObjectSize = this.getUniformLocation("u_objectsize");
  94.                 }
  95.  
  96.                 @Override
  97.                 public void bind(final GLState pGLState, final VertexBufferObjectAttributes pVertexBufferObjectAttributes) {
  98.                         GLES20.glDisableVertexAttribArray(1);
  99.                         super.bind(pGLState, pVertexBufferObjectAttributes);
  100.                         GLES20.glUniformMatrix4fv(FogofWarShaderProgram.sUniformModelViewPositionMatrixLocation, 1, false, pGLState.getModelViewProjectionGLMatrix(), 0);
  101.                         if(firstframe){
  102.                                 GLES20.glUniform2f(FogofWarShaderProgram.sUniformFogSize,fog_size[0],fog_size[1]);
  103.                                 GLES20.glUniform1f(FogofWarShaderProgram.sUniformLookRange,200);
  104.                                 GLES20.glUniform1f(FogofWarShaderProgram.sUniformFogBorder,25);
  105.                                 firstframe= false;
  106.                         }
  107.                         int counter = 0;
  108.                         Enumeration<Unit> en = GSC.GOM.GO_Hash_Unit.elements();
  109.                         while(en.hasMoreElements()){
  110.                                 Unit U = en.nextElement();
  111.                                 if(U.side == GSC.Team_side){
  112.                                         counter++;
  113.                                 }
  114.                         }
  115.                         GLES20.glUniform1i(FogofWarShaderProgram.sUniformObjectSize,counter);
  116.                 }
  117.  
  118.                 @Override
  119.                 public void unbind(final GLState pGLState) throws ShaderProgramException {
  120.                         GLES20.glEnableVertexAttribArray(1);
  121.                         super.unbind(pGLState);
  122.                 }
  123.         }
  124.  
Parsed in 0.025 seconds, using GeSHi 1.0.8.4


I make fog of war for strategy game. uniform need for send object x,y,type. If use several simple variables FPS decreases, also if amount iterations increases. May be have more faster way for do it without array ?
Andruum
 
Posts: 3
Joined: Wed Aug 14, 2013 3:47 pm

Re: gl_PointSize and uniform array problem

Postby alb » Fri Aug 16, 2013 7:12 pm

Hmm, I'm not sure I fully understand the problem. It's quite a complex shader you've got there.

Can you use a vec3 for x, y and type?

You could also combine your FogSize, LookRange and FogBorder into a single vec4. I'm not sure how much this will help performance as the amount of data doesn't seem excessive.
alb
 
Posts: 134
Joined: Tue Jan 29, 2013 10:45 pm


Return to ShaderPrograms

Who is online

Users browsing this forum: No registered users and 2 guests