• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# WebGL - How to send an array of matrices to the vertex shader?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1Winged  Members

Posted 28 June 2014 - 05:56 AM

I want to send an array of matrices (to be more specific: a dynamically changing between draw calls Float32Array of 4x4 Float32Arrays) to a vertex shader with multiple model matrices (size of this.modelMatrices depends on how many objects are moving, so there may be only one model matrix and just in the next frame there may be 30 of them).

When I just call:
gl.uniformMatrix4fv(program.mMatrixUniform, false, this.modelMatrices);
where this.modelMatrices is an Float32Array, and program.mMatrixUniform is declared like this:
program.mMatrixUniform = gl.getUniformLocation(program, 'uMMatrix');
it throws:
gl.INVALID_VALUE was caused by call to: uniformMatrix4fv.

In OpenGL uniformMatrix4fv there is an additional count attribute, which allows to specify the amount of matrices sent while in WebGL there isn't. Also, the question is, does dynamically changing this.modelMatrices causes a problem too?

What is a proper way of doing it in WebGL?

### #2JohnnyCode  Members

Posted 28 June 2014 - 07:13 AM

depends on how many objects are moving, so there may be only one model matrix and just in the next frame there may be 30 of them).

You need to define in shader the static length of the uniform array of matricies, and since that, the array of floats you pass to uniform setting function cannot be shorter than that (but can be longer since some updates). There is also inablitity to specify offset in the array, so it will always fill the uniform from the begining of array (this is so silly its hard to believe, but I just cannot find the offset accepting functions in specs!)

But there are more ways to set matricies to shaders, you can simply create a vec4 array, and set that, and use it to construct the matricies or just use them as rows the raw way in shader, doting 4d vectors with them.

### #3Winged  Members

Posted 28 June 2014 - 08:04 AM

@JohnnyCode

I like the second method, but I'm not sure what would be more computational expensive - creating matrices from an array of vec4 on GPU or sending on each draw call filled Float32Array with let's say 32 matrices? Also I don't need to offset anything since I found a clever way of managing model matrices, so it is not a problem for now.

### #4eduardo_costa  Members

Posted 28 June 2014 - 12:18 PM

I don't remember if I succeeded in sending Matrix4 arrays.

My engine is sending Vec4 arrays for now.

The reason behind this is that Transformation matrix can be 3x4 which spare you 1 Vec4 per matrix.

Considering you have a limit of uniforms in the vertex shader, this is mostly welcome!

### #5HappyCoder  Members

Posted 28 June 2014 - 03:21 PM

In your shader code you need to create the array of elements you want to use

uniform mat4[4] uMMatrix;


In reality, webgl just created four seperate uniforms with the names uMMatrix[0], uMMatrix[1], uMMatrix[2],and uMMatrix[3]. You need to bind them seperately.

program.mMatrixUniform[0] = gl.getUniformLocation(program, 'uMMatrix[0]');
gl.uniformMatrix4fv(program.mMatrixUniform[0], false, this.modelMatrices[0]);

There is more discussion on the subject here

My current game project Platform RPG

### #6Winged  Members

Posted 29 June 2014 - 09:12 AM

Sending an array of floats and saving uniform vectors slots sounds more promising to me than sending ready matrices and binding every one of them separately, so I think I'd rather go that way.

BTW Is there a way to define size of an array in the vertex shader basing on MAX_VERTEX_UNIFORM_VECTORS value, so it would be more 'cross-hardware'?

### #7JohnnyCode  Members

Posted 29 June 2014 - 09:51 AM

BTW Is there a way to define size of an array in the vertex shader basing on MAX_VERTEX_UNIFORM_VECTORS value, so it would be more 'cross-hardware'?

I would like to correct you here that you should establish as many vectors in a compiled shader as you need, not a single bit more.

If amount you expect shrinks or grows it will bogus up your API calls. (no count or offset parameters in webgl specs uniform setters)

So establish as many vec4a as you need, not more, and check with gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS)) to see if the current setup actualy even supports you least needed amount. If the MVUV provided in outer API of yours is smaller than your needs, you need to fallback on your own responsibility and pick lesser shaders - if even.

### #8Winged  Members

Posted 29 June 2014 - 12:37 PM

@JohnnyCode

I'm a little bit confused now.

GLSL specification states - "If an array is indexed with an expression that is not an integral constant expression, or if an array is passed as an argument to a function, then its size must be declared before any such use."

So unless I'd make a long long if-else ladder (which is strongly dissuaded on older GLSL) which would look like this:

else if (matrixIndex == 1)
return mat4(rawMatrices[12], rawMatrices[13], ..., rawMatrices[23], 0, 0, 0, 1);
else if (matrixIndex == 2)
return mat4(rawMatrices[24], rawMatrices[25], ..., rawMatrices[35], 0, 0, 0, 1);
...
else if (matrixIndex == 63)
return mat4(rawMatrices[756], rawMatrices[757], ..., rawMatrices[767], 0, 0, 0, 1); 

I'm bound to declare it's size first, and because of this I can't really see any advantageous solution which would establish as many floats as I need (because of the fact that the amount of needed floats would dynamically change at the runtime).

Edited by Winged, 29 June 2014 - 12:43 PM.

### #9JohnnyCode  Members

Posted 29 June 2014 - 02:43 PM

(because of the fact that the amount of needed floats would dynamically change at the runtime).

Ah I see what you ment. You want to maximize the array , as much as it can get.

Well if you want to declare an array size of unifrom declaration of a shader to be maximal on a system, you may call gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS)) and put this value to your shader code string, before you compile it (and also knowinng this value for your very aplication) .

Wheather this constant (MAX_VERTEX_UNIFORM_VECTORS) is available in GLSL code compiler I do not know .

But bare in mind that once shader gets compiled, the uniform array keeps its size, so change at the runtime will not be possible unless you recompile, but I do not see why would someone need to change an unifrom array length through runtime? (just fill the unsed rest always,  even bogus,  you cannot specify count in webgl so always pass conforming array of floats)

You can index vec4 array with an expresion in vertex shader I am sure.

like :

uniform vec4 u_vecs[256];

....

int ind = 6 ; // sixth matrix

mat4 themat=mat4(u_vecs[ind*4],u_vecs[ind*4+1],u_vecs[ind*4+2],u_vecs[ind*4+3]);

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.