Results 1 to 2 of 2

Thread: Matrix multiplication question

  1. #1
    Junior Member
    Join Date
    Apr 2011

    Matrix multiplication question

    Ok, so lately I have been writing a simple rendering engine for iOS using OpenGL es 2.0 spec. To do anything moderately exciting requires adding back in the model, view and projection matricies.

    One of the most common operations a graphics engine performs is 4x4 matrix multiplication, which in itself performed many times in a given frame is quite computationally expensive:

    Code :
    matrix[0]  = m1[0]*m2[0]  +  m1[1]*m2[4]  + m1[2]*m2[8]   + m1[3]*m2[12];
    matrix[1]  = m1[0]*m2[1]  +  m1[1]*m2[5]  + m1[2]*m2[9]   + m1[3]*m2[13];
    matrix[2]  = m1[0]*m2[2]  +  m1[1]*m2[6]  + m1[2]*m2[10]  + m1[3]*m2[14];
    matrix[15] = m1[12]*m2[3] +  m1[13]*m2[7] + m1[14]*m2[11] + m1[15]*m2[15];

    Now for the sake of optimisation on the CPU side, I could setup a matrix multiplication daemon with threading to split the load.

    However, the shader language gives the inbuilt matrix primitive and operations.

    Code :
    uniform mat4 m_model;
    uniform mat4 m_view;
    uniform mat4 m_projection;
    gl_Position = m_projection * m_view * m_model * v_position;

    Now my question is, is the matrix multiplication as expressed in the shader language (and presumable executed on the GPU) optimised? Does the matrix multiplication happen serially or in parallel? Is it better to send a precomputed on the CPU model view projection matrix to the vertex shader or is what I am doing here ok?

  2. #2

    Re: Matrix multiplication question

    The difference between doing the computation on the CPU vs the vertex shader is that computations done on the vertex shader are done per vertex. If you have a model with 10000 vertices, it's almost always better to compute a combined model-view-projection matrix on the CPU, since it's 10000 times fewer computations (even though GPUs are very efficient at arithmetic). If your models have only 4 vertices, it's going to make very little difference - the bottleneck will be the draw call overheads rather than arithmetic.

    Another tip is that if you are applying several matrices to a vector in a shader, use matrix-vector multiplications. Your example is equivalent to
    Code :
    gl_Position = ((m_projection * m_view) * m_model) * v_position;
    at 64 + 64 + 16 = 144 multiplications, but
    Code :
    gl_Position = m_projection * (m_view * (m_model * v_position));
    needs only 16 + 16 + 16 = 48 multiplications. A smart compiler might make that transformation for you, but it's better to be on the safe side.

Similar Threads

  1. arbitrary size matrix multiplication
    By lxu in forum OpenCL
    Replies: 1
    Last Post: 02-13-2013, 01:17 PM
  2. Matrix Multiplication
    By wrx in forum OpenCL
    Replies: 18
    Last Post: 02-17-2011, 12:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Proudly hosted by Digital Ocean