utils.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <string>
  6. #include "common/common_types.h"
  7. namespace VideoCore {
  8. /// Structure for the TGA texture format (for dumping)
  9. struct TGAHeader {
  10. char idlength;
  11. char colourmaptype;
  12. char datatypecode;
  13. short int colourmaporigin;
  14. short int colourmaplength;
  15. short int x_origin;
  16. short int y_origin;
  17. short width;
  18. short height;
  19. char bitsperpixel;
  20. char imagedescriptor;
  21. };
  22. /**
  23. * Dumps a texture to TGA
  24. * @param filename String filename to dump texture to
  25. * @param width Width of texture in pixels
  26. * @param height Height of texture in pixels
  27. * @param raw_data Raw RGBA8 texture data to dump
  28. * @todo This should be moved to some general purpose/common code
  29. */
  30. void DumpTGA(std::string filename, short width, short height, u8* raw_data);
  31. /**
  32. * Interleave the lower 3 bits of each coordinate to get the intra-block offsets, which are
  33. * arranged in a Z-order curve. More details on the bit manipulation at:
  34. * https://fgiesen.wordpress.com/2009/12/13/decoding-morton-codes/
  35. */
  36. static inline u32 MortonInterleave(u32 x, u32 y) {
  37. u32 i = (x & 7) | ((y & 7) << 8); // ---- -210
  38. i = (i ^ (i << 2)) & 0x1313; // ---2 --10
  39. i = (i ^ (i << 1)) & 0x1515; // ---2 -1-0
  40. i = (i | (i >> 7)) & 0x3F;
  41. return i;
  42. }
  43. /**
  44. * Calculates the offset of the position of the pixel in Morton order
  45. */
  46. static inline u32 GetMortonOffset(u32 x, u32 y, u32 bytes_per_pixel) {
  47. // Images are split into 8x8 tiles. Each tile is composed of four 4x4 subtiles each
  48. // of which is composed of four 2x2 subtiles each of which is composed of four texels.
  49. // Each structure is embedded into the next-bigger one in a diagonal pattern, e.g.
  50. // texels are laid out in a 2x2 subtile like this:
  51. // 2 3
  52. // 0 1
  53. //
  54. // The full 8x8 tile has the texels arranged like this:
  55. //
  56. // 42 43 46 47 58 59 62 63
  57. // 40 41 44 45 56 57 60 61
  58. // 34 35 38 39 50 51 54 55
  59. // 32 33 36 37 48 49 52 53
  60. // 10 11 14 15 26 27 30 31
  61. // 08 09 12 13 24 25 28 29
  62. // 02 03 06 07 18 19 22 23
  63. // 00 01 04 05 16 17 20 21
  64. //
  65. // This pattern is what's called Z-order curve, or Morton order.
  66. const unsigned int block_width = 8;
  67. const unsigned int block_height = 8;
  68. const unsigned int coarse_x = x & ~7;
  69. const unsigned int coarse_y = y & ~7;
  70. u32 i = VideoCore::MortonInterleave(x, y);
  71. const unsigned int offset = coarse_x * block_height;
  72. return (i + offset) * bytes_per_pixel;
  73. }
  74. } // namespace