diff options
Diffstat (limited to 'src/video_core/utils.h')
| -rw-r--r-- | src/video_core/utils.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/video_core/utils.h b/src/video_core/utils.h index 6fd640425..bda793fa5 100644 --- a/src/video_core/utils.h +++ b/src/video_core/utils.h | |||
| @@ -35,4 +35,54 @@ struct TGAHeader { | |||
| 35 | */ | 35 | */ |
| 36 | void DumpTGA(std::string filename, short width, short height, u8* raw_data); | 36 | void DumpTGA(std::string filename, short width, short height, u8* raw_data); |
| 37 | 37 | ||
| 38 | /** | ||
| 39 | * Interleave the lower 3 bits of each coordinate to get the intra-block offsets, which are | ||
| 40 | * arranged in a Z-order curve. More details on the bit manipulation at: | ||
| 41 | * https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/ | ||
| 42 | */ | ||
| 43 | static inline u32 MortonInterleave(u32 x, u32 y) { | ||
| 44 | u32 i = (x & 7) | ((y & 7) << 8); // ---- -210 | ||
| 45 | i = (i ^ (i << 2)) & 0x1313; // ---2 --10 | ||
| 46 | i = (i ^ (i << 1)) & 0x1515; // ---2 -1-0 | ||
| 47 | i = (i | (i >> 7)) & 0x3F; | ||
| 48 | return i; | ||
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Calculates the offset of the position of the pixel in Morton order | ||
| 53 | */ | ||
| 54 | static inline u32 GetMortonOffset(u32 x, u32 y, u32 bytes_per_pixel) { | ||
| 55 | // Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each | ||
| 56 | // of which is composed of four 2x2 subtiles each of which is composed of four texels. | ||
| 57 | // Each structure is embedded into the next-bigger one in a diagonal pattern, e.g. | ||
| 58 | // texels are laid out in a 2x2 subtile like this: | ||
| 59 | // 2 3 | ||
| 60 | // 0 1 | ||
| 61 | // | ||
| 62 | // The full 8x8 tile has the texels arranged like this: | ||
| 63 | // | ||
| 64 | // 42 43 46 47 58 59 62 63 | ||
| 65 | // 40 41 44 45 56 57 60 61 | ||
| 66 | // 34 35 38 39 50 51 54 55 | ||
| 67 | // 32 33 36 37 48 49 52 53 | ||
| 68 | // 10 11 14 15 26 27 30 31 | ||
| 69 | // 08 09 12 13 24 25 28 29 | ||
| 70 | // 02 03 06 07 18 19 22 23 | ||
| 71 | // 00 01 04 05 16 17 20 21 | ||
| 72 | // | ||
| 73 | // This pattern is what's called Z-order curve, or Morton order. | ||
| 74 | |||
| 75 | const unsigned int block_width = 8; | ||
| 76 | const unsigned int block_height = 8; | ||
| 77 | |||
| 78 | const unsigned int coarse_x = x & ~7; | ||
| 79 | const unsigned int coarse_y = y & ~7; | ||
| 80 | |||
| 81 | u32 i = VideoCore::MortonInterleave(x, y); | ||
| 82 | |||
| 83 | const unsigned int offset = coarse_x * block_height; | ||
| 84 | |||
| 85 | return (i + offset) * bytes_per_pixel; | ||
| 86 | } | ||
| 87 | |||
| 38 | } // namespace | 88 | } // namespace |