new WebGLFloatArray overhead   6 comments

I did some research on “new WebGLFloatArray”. As it is used (in every example), everytime a uniform matrix is updated. And of course I came to the conclusion that it uses too much memory. Here is an example from learningwebgl.

function setMatrixUniforms()
{
var pUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
gl.uniformMatrix4fv(pUniform, false, new WebGLFloatArray(pMatrix.flatten()));
var mvUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
gl.uniformMatrix4fv(mvUniform, false, new WebGLFloatArray(mvMatrix.flatten()));
}

It is just too slow and uses too much memory usage. This way really uses too much memory at once,also creating a new WebGLFloatArray is time-consuming but I found something interesting in the webGL specs.

5.13.3.9 WebGLFloatArray

WebGLFloatArray represents an array of 32-bit floating point values.

WebGLFloatArray has an element size of 4 bytes.

[
    Constructor(in unsigned long length),
    Constructor(in WebGLFloatArray array),
    Constructor(in sequence<float> array),
    Constructor(in WebGLArrayBuffer buffer,
                [Optional] in unsigned long byteOffset, [Optional] in unsigned long length)
]
interface WebGLFloatArray : WebGLArray {
    [IndexGetter] float get(in unsigned long index);
    [IndexSetter] void set(in unsigned long index, in float value);
    void set(in WebGLFloatArray array, [Optional] in unsigned long offset);
    void set(in sequence<float> array, [Optional] in unsigned long offset);
};

It seems like a WebGLFloatArray has a method set which of course sets the values but it doesn't change the length of the WebGLFloatArray. And it doesn't return itself which would be easy for chaining and less code lines.

So the upper function can be changed towards


// this just need to be an array of 16 elements passed in the new WebGLFloatArray
var FloatArray = new WebGLFloatArray([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]);
function setMatrixUniforms()
{
var pUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
//Because it doesn't return itself it needs an extra line of code
FloatArray.set(pMatrix.flatten());
gl.uniformMatrix4fv(pUniform, false, FloatArray);

var mvUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
//Because it doesn't return itself it needs an extra line of code
FloatArray.set(mvMatrix.flatten());
gl.uniformMatrix4fv(mvUniform, false, new WebGLFloatArray(FloatArray);
}

Unluckily for us, as of today, minefield is not able to use this yet as it has not been implemented fully in minefield.

About these ads

Posted December 20, 2009 by ewgl in webGL

Tagged with , ,

6 responses to “new WebGLFloatArray overhead

Subscribe to comments with RSS.

  1. Hey, ok, I get it, I guess – but does this really work?

  2. This brings me to an idea:…

  3. Damn, that sound’s so easy if you think about it.

  4. I cannot believe this is true!

  5. Hehe, nice one. I’ll use that!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: