utils.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include "common/common_types.h"
  6. namespace VideoCore {
  7. // 8x8 Z-Order coordinate from 2D coordinates
  8. static inline u32 MortonInterleave(u32 x, u32 y) {
  9. static const u32 xlut[] = {0x00, 0x01, 0x04, 0x05, 0x10, 0x11, 0x14, 0x15};
  10. static const u32 ylut[] = {0x00, 0x02, 0x08, 0x0a, 0x20, 0x22, 0x28, 0x2a};
  11. return xlut[x % 8] + ylut[y % 8];
  12. }
  13. /**
  14. * Calculates the offset of the position of the pixel in Morton order
  15. */
  16. static inline u32 GetMortonOffset(u32 x, u32 y, u32 bytes_per_pixel) {
  17. // Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each
  18. // of which is composed of four 2x2 subtiles each of which is composed of four texels.
  19. // Each structure is embedded into the next-bigger one in a diagonal pattern, e.g.
  20. // texels are laid out in a 2x2 subtile like this:
  21. // 2 3
  22. // 0 1
  23. //
  24. // The full 8x8 tile has the texels arranged like this:
  25. //
  26. // 42 43 46 47 58 59 62 63
  27. // 40 41 44 45 56 57 60 61
  28. // 34 35 38 39 50 51 54 55
  29. // 32 33 36 37 48 49 52 53
  30. // 10 11 14 15 26 27 30 31
  31. // 08 09 12 13 24 25 28 29
  32. // 02 03 06 07 18 19 22 23
  33. // 00 01 04 05 16 17 20 21
  34. //
  35. // This pattern is what's called Z-order curve, or Morton order.
  36. const unsigned int block_height = 8;
  37. const unsigned int coarse_x = x & ~7;
  38. u32 i = VideoCore::MortonInterleave(x, y);
  39. const unsigned int offset = coarse_x * block_height;
  40. return (i + offset) * bytes_per_pixel;
  41. }
  42. } // namespace