Dav1dde / gl3n

OpenGL Maths for D (not glm for D).
http://dav1dde.github.com/gl3n/
Other
103 stars 49 forks source link

Matrix invert optimization #5

Closed PhilipWitte closed 12 years ago

PhilipWitte commented 12 years ago

This needs to be tested, but I believe it should work:

private Matrix invert(ref Matrix mat) const {
    mt d =  1.0 / det;

    mat.matrix = [[
        ( ... ) * d,
        // etc...
    ]];

    return mat;
}
Dav1dde commented 12 years ago

I implemented it, but just for matrix inversion, since the determinant/magnitude/length can be 0, and as we all know dividing by 0 is not what we want. This optimization can be disabled with -version=NoReciprocalMul, it is enabled by default.

PhilipWitte commented 12 years ago

You might think about using an assert and having a compiler variable instead of a compiler flag:

private auto invert(bool rmul = true)(ref Matrix mat) const {
    static if (rmul && isFloatingPoint!mt) {
        assert(det != 0);
        mt d = 1 / det;
        enum op = "*";
    }
    else {
        mt d = det;
        enum op = "/";
    }

    ...
}

That way you can use both, and only use '/' when & where the program fails in debug.

[EDIT] I forget this method is private :-/ I see why you just went with the compiler flag.