Jump to content
Posted 17 February 2013 - 02:29 PM
Posted 17 February 2013 - 02:37 PM
This is commonly done in graphics hardware for textures, where it's called tiling or swizzling. You could apply the same process to arrays.
http://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ explains the process well.
Posted 17 February 2013 - 08:02 PM
Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.
Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.
Posted 18 February 2013 - 02:17 AM
Everything stated so far is of course correct but misses an obvious item. Frob makes the proper suggestion which is that access pattern is key and all the swizzling in the world won't make a difference if the access is out of expectations. More importantly, a "bug" in the given equation needs to be brought up: "x*XSize+y". That's likely a typo. Should be: "x+y*XSize". Ok, why is that important? As Frob pointed out, your access pattern is critical and the initial, incorrect?, equation would need to have a memory layout biased in vertical bars which could actually be mostly solved by storing the image rotated 90 degree's and exchanging x/y without any fancy swizzles.
Sorry folks, I just figured it's proper to ask if the standard strided image format formula was misstyped before suggesting complicated solutions.
Assuming the equation was a typo, I disagree with the original hypothesis that "iteration" is inefficient. Again, as Frob mentions, this depends on access patterns. Simple iterations over the pixels, you will likely slow things down using other packing methods. Performing bi-linear operations are also likely to slow down with alternative layouts using AMD/Intel CPU's due to the number of lines of associativity they maintain in the cache. I.e. they maintain all three lines of data in separate cache lines without purging and reloading them constantly and each cache line is likely to contain at least say 4 pixels (likely more unless you are on a REALLY dated CPU) so the predictor is busy loading the next section from all three memory areas while the CPU is actively processing the loaded data.
Getting back on track. As Frob points out, it's all about what you want to do with the data. If you wanted to perform a gaussian blur on the image you might think swizzling is the way to go. You'd be wrong in that case. Execute the gaussian blur only in left to right so you get linear memory access on multiple lines, rotate the original image 90 degree's in memory, apply the blur again, unrotate and then average the images. Mathematically identical results, baring rounding errors. But MUCH faster than accessing memory beyond cache line association limits. (NOTE: Think that's the correct fast method, been a while.. And there are more tricks to reduce memory access top to bottom to keep it cache efficient just about no matter what filter size you want.)
So, accessing arrays/memory is all about how you need to access it and what you intend to do with it. I use the gaussian blur because it is a big "window" of access, but also you can break the math into several stages and that's what allows you to optimize your memory access. Truly random access, forget it, linear is best.
Posted 18 February 2013 - 05:07 AM
Posted 18 February 2013 - 11:50 AM
Are you thinking of something like a Hilbert curve?
You actually could use a Hilbert curve for a layout of a 2D array and it would have the property that (x,y)'s that are near each other in the Euclidean sense would tend to be near each other in the array; there would be exceptions but generally this would be the case.
Would only work with power of two sized arrays however. Also not sure about the time efficiency of going from index -> (x,y) and (x,y)->index; might be log(n) over the size of the array but not sure.
Edited by jwezorek, 19 February 2013 - 01:14 PM.
Posted 18 February 2013 - 06:54 PM
Wait till towards the end of your project, profile your code, then determine if the array access is even a bottleneck worth optimizing.