Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Created April 15, 2017 12:51
Show Gist options
  • Save reinsteam/fdb6eeb872432ff5fd4b07a37652a4de to your computer and use it in GitHub Desktop.
Save reinsteam/fdb6eeb872432ff5fd4b07a37652a4de to your computer and use it in GitHub Desktop.
Simplified version of creating morton codes from 2 numbers in range [0; 8). Useful for converting local thread index in compute shader to a flattened one for downsampling
#include <stdio.h>
int MortonShuffle3Bit(int x)
{
return (x & 0x1) | ((x & 0x2) << 1) | ((x & 0x4) << 2);
}
int EncodeMorton3Bit(int x, int y)
{
return (MortonShuffle3Bit(y) << 1) | MortonShuffle3Bit(x);
}
int main(void)
{
for (int y = 0; y < 8; ++y)
{
for (int x = 0; x < 8; ++x)
{
printf("%2d ", EncodeMorton3Bit(x, y));
}
printf("\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment