You are not logged in.

Read the FAQ and Knowledge Base before posting.
We won't make a 3DS/2DS emulator.



#1 2014-09-18 15:22:44

hcf
Member
Registered: 2014-02-18
Posts: 23

Matrix elements sorting

This is just a matter of curiosity. I am trying to convert the matrix management of Desmume (currently made by software) to Direct3D in our Xbox port, to try to improve speed, and I have seen a thing in your code that has made me curious.

You define every matrix as a one-dimension array of 16 positions, like this one:

float mtxTemporal[16];

This represents a 4x4 matrix like this one:

0  1  2  3
4  5  6  7
8  9 10 11
12 13 14 15

But now, when we are going to store the elements of the matrix in your array, we have 2 choices. We can sort the elements going over the matrix  "by rows":

mtxTemporal = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }

or by columns:

mtxTemporal = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 }

Almost all of the code samples that I have seen, use the "by rows" approach... but if I am not mistaken, I think that you are sorting the elements "by columns" (I say this after having seen the functions to multiply matrixes, and a matrix by a vector), or am I wrong?

If this is correct, I would like to know why you decided to follow that approach. Please, note that I am not questioning your decission. I ask this question because Desmume is being very didactic for me, I am learning a lot of things while doing this port, and I would like to understand why did you decide to do that... because if I was doing this from scratch, I would have probably used the other method (by rows) which seems more natural to me, specially when multiplying a matrix by a vector, when we would go over the matrix in a sequential way. That is why it would be very interesting for me to know why you decided the other choice smile

Thanks for your awesome work!

Offline

#2 2014-09-18 17:54:52

zeromus
Radical Ninja
Registered: 2009-01-05
Posts: 6,169

Re: Matrix elements sorting

exactly 50% of code in the world uses column major and the other 50% uses row major. If you think the ratio is different, you need to look at more code. There is usually no net reason to prefer one over the other. 

It's done the way it is in desmume because it that's the way the nds does it. For example:
mtxCurrent[mode][ML4x4ind] = v;
They come to us in that order, so we store them in that order.

Offline

#3 2014-09-18 21:43:13

hcf
Member
Registered: 2014-02-18
Posts: 23

Re: Matrix elements sorting

Thank you very much for your answer, it was clear and concise, and very didactic again!

I was thinking in the natural way to go over the matrix when we multiply it by a vector... and I totally forgot about the natural way to go over the matrix when we are setting its values. Looking at how the Load functions are called (from the position 0 to 15) its clear that the natural way in your project, is the way that you chose smile

Also, I think that you are right in your statement about the popularity about row major and column major. After having thought about it, I remember that the row major code may be more popular... when we are using two dimensional arrays (you know: they say that going over a 2-dimensional array by rows is faster than doing it by columns, if you need to read all the elements), but in your case when you are using a 1-dimensional array, obviously this doesn't apply. Thanks again!  wink

Offline

#4 2014-09-18 21:48:11

zeromus
Radical Ninja
Registered: 2009-01-05
Posts: 6,169

Re: Matrix elements sorting

theres no real difference between a two dimensional array and a one dimensional array. theres more involved than just the transform (matrix * vector) scenario. the ideal choice for speed isnt clear and will differ depending on algorithm and cache behaviour of cpu, etc.. it isnt worth worrying about in most cases.

Offline

Board footer

Powered by FluxBB