[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] Matrix objects in JavaScript




toFloat32Array


On Thu, Mar 21, 2013 at 1:20 PM, Florian Bösch <pyalot@gmail.com> wrote:
The issue of allocations and jittery animations is something that concerns CSS transforms as well. If you want as smooth as possible JS driven animation then allocating new objects to transform things will lead to issues of animation quality if you do it 60x per second. So if it's relevant to WebGL or not as a functionality, the suggestion to make copy-free semantics possible applies all the same.

Agreed. To hit a couple of specific points:

In order to be useful the API should, if at all possible, guarantee that the TypedArrays returned by toFloat32Array and toFloat64Array are the same object each time, implying that each matrix object maintains a single array of each type over it's lifetime. Given that restriction a poperty may be a more appropriate interface than a function. If this cannot be guaranteed then a preferable API would be copyToFloat__Array() which accepts an array that will be populated with Matrix values. Of course this copy step incurs additional overhead, which closes the gap between a JS library and this API performance wise.

The mutable transformation methods (translateBy, etc) feel like a step in the right direction, but their design will probably lead to the need to maintain additional temporaries anyway. As a simple example, assume that I have a scene graph with a tree of transforms. You will frequently need to do operations like:

worldMatrix = parentWorldMatrix * childMatrix;

In this case the developer will want to preserve the parent and child matrices for future calculations, so calling the mutable transforms is not an option. Given the currently proposed API, then, the only option is to create a temporary variable via:

var worldMatrix = parentWorldMatrix.multiply(childMatrix);

Or copy the values into a pre-allocated matrix and then use the mutable function. There's no clean way to do this in the existing API, and the copy is undesirable even if there's an explicit function for it.

worldMatrix.m11 = parentWorldMatrix.m11;
... // Bleh
worldMatrix.m44 = parentWorldMatrix.m44;
worldMatrix.multiplyBy(childMatrix);

Ideally a performance-minded developer wants an API that allows an explicit output target to be specified, such as:

parentWorldMatrix.multiplyInto(worldMatrix, childMatrix);

This is, admittedly, a much less intuitive API but the nature of memory management in _javascript_ demands it. If object construction and collection was sufficiently fast then the current immutable API would be acceptable, but sadly that's simply not the case today.

--Brandon