## D - Scientific, Graphics, and Game programming performance

- Craig Black (18/18) Jun 25 2002 I would like to toss around some ideas that would benefit scientific,
- Sean L. Palmer (30/48) Jun 25 2002 D already has everything we need except operator overloading.
- Craig Black (17/33) Jun 25 2002 essentially
- Sean L. Palmer (14/24) Jun 26 2002 I don't know about the scientific end of things but in graphics we mainl...

I would like to toss around some ideas that would benefit scientific, graphics, and game programming. Consider the code below. vector3 a(0, 1, 2), b(3, 4, 5), c(6, 7, 8), d; d = a + b - c * a; C++ uses expression templates for vector operations with arbitrary size. C++ expression templates could transform the second line of code into: for (int i = 0; i < 3; i++) d[i] = a[i] + b[i] - c[i] * a[i]; This is good because it eliminates temporary variables. However, for small vectors it would be ideal to unroll the for loop to get: d.x = a.x + b.x - c.x * a.x; d.y = a.y + b.y - c.y * a.y; d.z = a.z + b.z - c.z * a.z; How could this kind of thing be done? One way to solve this problem is with generics like C++ expression templates. Another solution may be to have built-in vector, and matrix types, like with NVidia Cg. Perhaps Cg's solution is better because you can also make use of the graphics card instructions, boosting performance even further.

Jun 25 2002

D already has everything we need except operator overloading. In D you'd just write: alias float[3] vector3; vector3 a = {0,1,2}, b = {3,4,5}, c = {6,7,8}, d; d = a + b - c * a; And the idea is that the compiler will already try to do exactly what you want. However this isn't implemented in the current D compiler. (and the array initialization syntax will crash the compiler. ;) But Walter has mentioned that he will do it like this. What I want is to be able to make Dot and Cross product operators, and matrix multiply, inner product, and outer product should be part of the language, they're so common. Very fundamental vector math. And quaternion is just an extension of Complex which is already in the D language. Why stop there? Tensor math is basically what computer graphicists use already. Walter won't go for geometric algebra support but tensor math is essentially inner product, outer product, and matrix multiply and matrix by vector transforms. And it uses square matrices which is what we want in graphics most of the time. Just about everything else (invert, transpose, etc) we could write as functions. I don't think Cg types have a place in standard D. Graphics is too specialized. D programs run on the main CPU, not on the GPU. Cg programs run on the GPU only. Instead of Cg types we need the compiler to recognize the special cases of float[4], double[2], byte[16], short[8], int[4], etc and generate SIMD (SSE or 3DNOW!) code. Heck it could generate SIMD code anyway if it wanted. Sean "Craig Black" <cblack ara.com> wrote in message news:afa36a$10nt$1 digitaldaemon.com...I would like to toss around some ideas that would benefit scientific, graphics, and game programming. Consider the code below. vector3 a(0, 1, 2), b(3, 4, 5), c(6, 7, 8), d; d = a + b - c * a; C++ uses expression templates for vector operations with arbitrary size. C++ expression templates could transform the second line of code into: for (int i = 0; i < 3; i++) d[i] = a[i] + b[i] - c[i] * a[i]; This is good because it eliminates temporary variables. However, for small vectors it would be ideal to unroll the for loop to get: d.x = a.x + b.x - c.x * a.x; d.y = a.y + b.y - c.y * a.y; d.z = a.z + b.z - c.z * a.z; How could this kind of thing be done? One way to solve this problem is with generics like C++ expression templates. Another solution may be to have built-in vector, and matrix types, like with NVidia Cg. Perhaps Cg's solution is better because youcanalso make use of the graphics card instructions, boosting performance even further.

Jun 25 2002

What I want is to be able to make Dot and Cross product operators, and matrix multiply, inner product, and outer product should be part of the language, they're so common. Very fundamental vector math. Andquaternionis just an extension of Complex which is already in the D language. Why stop there? Tensor math is basically what computer graphicists usealready.Walter won't go for geometric algebra support but tensor math isessentiallyinner product, outer product, and matrix multiply and matrix by vector transforms. And it uses square matrices which is what we want in graphics most of the time. Just about everything else (invert, transpose, etc) we could write as functions.Sounds good, but how do you plan on writing invert, transpose, etc for arbitrary matrix dimensions? Would you pass the dimensions of the matrix as parameters? Isn't this kinda messy? Or could we use a template function like you can in C++. template<int a, int b> float [b][a] Inverse(float [a][b] matrix) { ... } Or you could describe it this way: float [b][a] Inverse<int a, int b>(float [a][b] matrix) { ... } This breed of template is nice but I presume would be difficult to implement?I don't think Cg types have a place in standard D. Graphics is too specialized. D programs run on the main CPU, not on the GPU. Cg programs run on the GPU only.Yes Cg is based solely on NVidia chipset.Instead of Cg types we need the compiler to recognize the special cases of float[4], double[2], byte[16], short[8], int[4], etc and generate SIMD(SSEor 3DNOW!) code. Heck it could generate SIMD code anyway if it wanted.Sounds good. --Craig

Jun 25 2002

I don't know about the scientific end of things but in graphics we mainly need either 3x3, 3x4, or 4x4 matrices. D already supports function overloading so we can just overload those three cases: void Invert(inout float[3][3]); void Invert(inout float[4][3]); void Invert(inout float[4][4]); I haven't given that much thought to generics syntax. I use C++ templates all the time so that syntax is tolerable to me, but I know it could be improved. I just haven't sat down and experimented enough to know which way would be best. Admittedly that's a very personal yardstick. ;) Sean "Craig Black" <cblack ara.com> wrote in message news:afad7l$1t22$1 digitaldaemon.com...Sounds good, but how do you plan on writing invert, transpose, etc for arbitrary matrix dimensions? Would you pass the dimensions of the matrixasparameters? Isn't this kinda messy? Or could we use a template function like you can in C++. template<int a, int b> float [b][a] Inverse(float [a][b] matrix) { ... } Or you could describe it this way: float [b][a] Inverse<int a, int b>(float [a][b] matrix) { ... } This breed of template is nice but I presume would be difficult to implement?

Jun 26 2002