1. Practical difference between sequence and a C array
- Posted by ssallen Oct 18, 2012
- 1177 views
Hello everyone, I have been slowly working on Mic's opengl library and wrapping all the needed functions for the modern OpenGl pipeline (VBO support, matrix math, etc.) and I am finding myself doing a LOT of matrix math. Since opengl just uses a length 16 c array to store these matrices I have to constantly convert my sequences into arrays. This obviously degrades performance.
I don't really know a lot about the internals of euphoria memory management so Im curious, how much different is the memory layout of a static, single dimension, sequence of length 16 (using floating pt) than a c array of length 16? Can I make small alterations to a sequence and convert it to an array without reading each element and then poking it directly?
Here is my current process:
function seq_to_matrixf(sequence s1) -- s1 is a seq of floats of length 16 atom x_array if length(s1) != 16 then return -1 -- not correct matrix dimension end if x_array = allocate(64) -- float32s are 4 bytes each * 16 values for i = 1 to length( s1 ) do poke( x_array + (i-1) * 4, atom_to_float32( s1[i] ) ) end for return x_array end function
Is there a faster way to convert same length and single dimensional sequences into arrays?
Thanks!
2. Re: Practical difference between sequence and a C array
- Posted by jimcbrown (admin) Oct 18, 2012
- 1094 views
Hello everyone, I have been slowly working on Mic's opengl library and wrapping all the needed functions for the modern OpenGl pipeline (VBO support, matrix math, etc.) and I am finding myself doing a LOT of matrix math. Since opengl just uses a length 16 c array to store these matrices I have to constantly convert my sequences into arrays. This obviously degrades performance.
I'm thinking that it might be better to use allocate() to allocate memory to create the arrays and use those directly. If eu code needs to look at an element of the array, use peek()/poke() to do it. (Perhaps wrapped safely in a helper function that takes the array and an index).
I don't really know a lot about the internals of euphoria memory management so Im curious, how much different is the memory layout of a static, single dimension, sequence of length 16 (using floating pt) than a c array of length 16?
The array that's used internally is basically an array of pointers, except that the zeroth element is used to store the length. So you're off by one.
Each pointer then points to a eu specific struct that contains the double floating point (but not the single floating point, or float32, that opengl is using).
Can I make small alterations to a sequence and convert it to an array without reading each element and then poking it directly?
If it was an array of 32bit (or rather 31bit) ints, then possibly yes. Otherwise, unfortunately not. Worse, in your case you'd have the extra conversion from float64 to float32 anyways.
Here is my current process:
function seq_to_matrixf(sequence s1) -- s1 is a seq of floats of length 16 atom x_array if length(s1) != 16 then return -1 -- not correct matrix dimension end if x_array = allocate(64) -- float32s are 4 bytes each * 16 values for i = 1 to length( s1 ) do poke( x_array + (i-1) * 4, atom_to_float32( s1[i] ) ) end for return x_array end function
Is there a faster way to convert same length and single dimensional sequences into arrays?
Thanks!
3. Re: Practical difference between sequence and a C array
- Posted by ssallen Oct 18, 2012
- 1117 views
Ah, too bad.
Still, I am pretty impressed with how well Euphoria is performing at this. My hardware was mid-range 3 years ago and Eu is happily chewing up over 8k matrix multiplications a frame at 80 FPS. I was going to be lazy but I think I'll try it your way and just use straight arrays. I have a pretty large library of matrix operation functions I'll have to tweak now but onward and upward I guess.
Thanks for the info!
Steve A.