Skip to content

“WebGL Matrices vs Math Matrices” is a row-major vs column-major issue #106

Open
@tjol

Description

@tjol

Hi!

I just read webgl-matrix-vs-math.html and just wanted to point you in the direction of some ideas that might help you think more clearly about the issue should you ever want to rework the section in a future update.

Coming from the scientific computing corner, I'm pretty familiar with this issue, which is often described as whether a matrix is stored in "column-major" or "row-major" order.

As you know, if you have a 2D matrix

1 2 3
4 5 6
7 8 9

and you want to store it in 1D memory, you have a number of choices, each with compromises.

  • You can scan through the first column, then the second, and so on: [ 1, 4, 7, 2, 5, 8, 3, 6, 9 ] (“column-major”)
  • You can scan through the first row, second row, etc.: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] (“row-major”)

The first is better for computing linear algebra. The second is nice because you can lay a 1D array out as a matrix in your source code and get the correct order (and it just fits well with C's memory model)

What you might not know is that column-major is the way matrices are stored in FORTRAN. It's also used by some other programming languages with a focus on numerical/scientific computing, like MATLAB and Julia. And, of course, it's the way GL expects you to lay out your matrices in memory.

The second way is what C and C++ do, and what's popular in languages from the C/C++ tradition, even when they don't natively support matrices (like Python and JavaScript). And it's way bitmaps are usually stored. So that's almost everybody.

Anyway, to my mind, it's not so much a programming vs. math problem, it's that the answer to "how do you lay out a 2D object in 1D memory" is arbitrary and C++/JavaScript and GL come from different traditions.

I don't know how to integrate this into the document, and I'm not even sure it would make it any clearer to newcomers. Just thought maybe a bit of a different take on the same issue might be interesting :-)

PS:
This section of the Julia docs might be interesting: https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major (but it probably won't be)
Here is a section of the numpy docs that mentions array memory order: https://numpy.org/devdocs/reference/generated/numpy.ravel.html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions