# DX11 - Compute shader - wrong index calculation

I have volume (3D image) divided into blocks 16x16x16. Whole volume is 128x128x128. Data are stored in linear memory in my shader. For every block, i store start index within linear memory. Now, I need to recalculate index from 16x16x16 block to index of 128x128x128 data. Every block is iterated with index 0, 1, 2, 3, ....4096 and I need to change this to global linear index.

I am using this formula: [ii, jj, kk] is 1D index from 16x16x16 block converted to 3D index within this block and next line convert this 3D index to 1D index within 128x128x128 data.

``````uint ii = index / 256; //get depth
uint tmp = index % 256;  //convert from 3D to 2D

uint jj = tmp / 16;  //get width
uint kk = tmp % 16;  //get height

return startBlockIndex + ((128 * 128) * ii + (128) * jj  + kk);
``````

However, result is incorrect. Data are filled only in 1/4 of original volume. There is 16 times repeated same pattern, which should be only 1 and stretched across the whole volume. I think that this solution should work, but not quite sure... at least it worked on paper

Thanks for any advice

-
if your local 1D index is calculated from the local 3D 16x16x16 [i, j, k] indices somewhat like `i+16j+256k`, then the way you re-calculate i, j, and k seems fine. What is startBlockIndex though? If it's the 128x128x128 1D index of the first element of your local 16x16x16 chunk, then couldn't you just add your local 1D index to it to get the absolute "world" index? if startBlockIndex is the 8x8x8 chunk start index though, you might need to calculate its 8x8x8 i, j and k, and then do i+128j+128*128k to get the absolute 128x128x128 block start index, then add your local 16x16x16 1D index? –  melak47 May 9 '12 at 17:51
I think you might just be applying the i+128j+128*128k to the local index instead of the global blockstart index. Try calculating ii, jj and kk by kk = x/64, jj = x%64/8, ii = x%64%8; where x is startBlockIndex, and then returning i+(128*j)+(128*128*k) + index. I just noticed though, I have my i, j and k reversed from yours. –  melak47 May 9 '12 at 17:59
Yeah...thanks stupid mistake, I reversed ii and kk :/ –  Martin Perry May 10 '12 at 6:43