gpu.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 <cstddef>
  6. #include "common/common_types.h"
  7. #include "common/bit_field.h"
  8. namespace GPU {
  9. // Returns index corresponding to the Regs member labeled by field_name
  10. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  11. // when used with array elements (e.g. GPU_REG_INDEX(memory_fill_config[0])).
  12. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  13. // Hopefully, this will be fixed sometime in the future.
  14. // For lack of better alternatives, we currently hardcode the offsets when constant
  15. // expressions are needed via GPU_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  16. // will then make sure the offsets indeed match the automatically calculated ones).
  17. #define GPU_REG_INDEX(field_name) (offsetof(GPU::Regs, field_name) / sizeof(u32))
  18. #if defined(_MSC_VER)
  19. #define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  20. #else
  21. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  22. // really is this annoying. This macro just forwards its first argument to GPU_REG_INDEX
  23. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  24. // field offset. Otherwise, the compiler will fail to compile this code.
  25. #define GPU_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  26. ((typename std::enable_if<backup_workaround_index == GPU_REG_INDEX(field_name), size_t>::type)GPU_REG_INDEX(field_name))
  27. #endif
  28. // MMIO region 0x1EFxxxxx
  29. struct Regs {
  30. // helper macro to make sure the defined structures are of the expected size.
  31. #if defined(_MSC_VER)
  32. // TODO: MSVC does not support using sizeof() on non-static data members even though this
  33. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  34. // support for that.
  35. #define ASSERT_MEMBER_SIZE(name, size_in_bytes)
  36. #else
  37. #define ASSERT_MEMBER_SIZE(name, size_in_bytes) \
  38. static_assert(sizeof(name) == size_in_bytes, \
  39. "Structure size and register block length don't match")
  40. #endif
  41. // Components are laid out in reverse byte order, most significant bits first.
  42. enum class PixelFormat : u32 {
  43. RGBA8 = 0,
  44. RGB8 = 1,
  45. RGB565 = 2,
  46. RGB5A1 = 3,
  47. RGBA4 = 4,
  48. };
  49. /**
  50. * Returns the number of bytes per pixel.
  51. */
  52. static int BytesPerPixel(PixelFormat format) {
  53. switch (format) {
  54. case PixelFormat::RGBA8:
  55. return 4;
  56. case PixelFormat::RGB8:
  57. return 3;
  58. case PixelFormat::RGB565:
  59. case PixelFormat::RGB5A1:
  60. case PixelFormat::RGBA4:
  61. return 2;
  62. default:
  63. UNIMPLEMENTED();
  64. }
  65. }
  66. INSERT_PADDING_WORDS(0x4);
  67. struct {
  68. u32 address_start;
  69. u32 address_end;
  70. union {
  71. u32 value_32bit;
  72. BitField<0, 16, u32> value_16bit;
  73. // TODO: Verify component order
  74. BitField< 0, 8, u32> value_24bit_r;
  75. BitField< 8, 8, u32> value_24bit_g;
  76. BitField<16, 8, u32> value_24bit_b;
  77. };
  78. union {
  79. u32 control;
  80. // Setting this field to 1 triggers the memory fill.
  81. // This field also acts as a status flag, and gets reset to 0 upon completion.
  82. BitField<0, 1, u32> trigger;
  83. // Set to 1 upon completion.
  84. BitField<0, 1, u32> finished;
  85. // If both of these bits are unset, then it will fill the memory with a 16 bit value
  86. // 1: fill with 24-bit wide values
  87. BitField<8, 1, u32> fill_24bit;
  88. // 1: fill with 32-bit wide values
  89. BitField<9, 1, u32> fill_32bit;
  90. };
  91. inline u32 GetStartAddress() const {
  92. return DecodeAddressRegister(address_start);
  93. }
  94. inline u32 GetEndAddress() const {
  95. return DecodeAddressRegister(address_end);
  96. }
  97. } memory_fill_config[2];
  98. ASSERT_MEMBER_SIZE(memory_fill_config[0], 0x10);
  99. INSERT_PADDING_WORDS(0x10b);
  100. struct FramebufferConfig {
  101. union {
  102. u32 size;
  103. BitField< 0, 16, u32> width;
  104. BitField<16, 16, u32> height;
  105. };
  106. INSERT_PADDING_WORDS(0x2);
  107. u32 address_left1;
  108. u32 address_left2;
  109. union {
  110. u32 format;
  111. BitField< 0, 3, PixelFormat> color_format;
  112. };
  113. INSERT_PADDING_WORDS(0x1);
  114. union {
  115. u32 active_fb;
  116. // 0: Use parameters ending with "1"
  117. // 1: Use parameters ending with "2"
  118. BitField<0, 1, u32> second_fb_active;
  119. };
  120. INSERT_PADDING_WORDS(0x5);
  121. // Distance between two pixel rows, in bytes
  122. u32 stride;
  123. u32 address_right1;
  124. u32 address_right2;
  125. INSERT_PADDING_WORDS(0x30);
  126. } framebuffer_config[2];
  127. ASSERT_MEMBER_SIZE(framebuffer_config[0], 0x100);
  128. INSERT_PADDING_WORDS(0x169);
  129. struct {
  130. u32 input_address;
  131. u32 output_address;
  132. inline u32 GetPhysicalInputAddress() const {
  133. return DecodeAddressRegister(input_address);
  134. }
  135. inline u32 GetPhysicalOutputAddress() const {
  136. return DecodeAddressRegister(output_address);
  137. }
  138. union {
  139. u32 output_size;
  140. BitField< 0, 16, u32> output_width;
  141. BitField<16, 16, u32> output_height;
  142. };
  143. union {
  144. u32 input_size;
  145. BitField< 0, 16, u32> input_width;
  146. BitField<16, 16, u32> input_height;
  147. };
  148. union {
  149. u32 flags;
  150. BitField< 0, 1, u32> flip_data; // flips input data horizontally (TODO) if true
  151. BitField< 1, 1, u32> output_tiled; // Converts from linear to tiled format
  152. BitField< 3, 1, u32> raw_copy; // Copies the data without performing any processing
  153. BitField< 8, 3, PixelFormat> input_format;
  154. BitField<12, 3, PixelFormat> output_format;
  155. BitField<24, 1, u32> scale_horizontally;
  156. BitField<25, 1, u32> scale_vertically;
  157. };
  158. INSERT_PADDING_WORDS(0x1);
  159. // it seems that writing to this field triggers the display transfer
  160. u32 trigger;
  161. } display_transfer_config;
  162. ASSERT_MEMBER_SIZE(display_transfer_config, 0x1c);
  163. INSERT_PADDING_WORDS(0x331);
  164. struct {
  165. // command list size (in bytes)
  166. u32 size;
  167. INSERT_PADDING_WORDS(0x1);
  168. // command list address
  169. u32 address;
  170. INSERT_PADDING_WORDS(0x1);
  171. // it seems that writing to this field triggers command list processing
  172. u32 trigger;
  173. inline u32 GetPhysicalAddress() const {
  174. return DecodeAddressRegister(address);
  175. }
  176. } command_processor_config;
  177. ASSERT_MEMBER_SIZE(command_processor_config, 0x14);
  178. INSERT_PADDING_WORDS(0x9c3);
  179. static inline size_t NumIds() {
  180. return sizeof(Regs) / sizeof(u32);
  181. }
  182. u32& operator [] (int index) const {
  183. u32* content = (u32*)this;
  184. return content[index];
  185. }
  186. u32& operator [] (int index) {
  187. u32* content = (u32*)this;
  188. return content[index];
  189. }
  190. private:
  191. /*
  192. * Most physical addresses which GPU registers refer to are 8-byte aligned.
  193. * This function should be used to get the address from a raw register value.
  194. */
  195. static inline u32 DecodeAddressRegister(u32 register_value) {
  196. return register_value * 8;
  197. }
  198. };
  199. static_assert(std::is_standard_layout<Regs>::value, "Structure does not use standard layout");
  200. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  201. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  202. // support for that.
  203. #ifndef _MSC_VER
  204. #define ASSERT_REG_POSITION(field_name, position) \
  205. static_assert(offsetof(Regs, field_name) == position * 4, \
  206. "Field "#field_name" has invalid position")
  207. ASSERT_REG_POSITION(memory_fill_config[0], 0x00004);
  208. ASSERT_REG_POSITION(memory_fill_config[1], 0x00008);
  209. ASSERT_REG_POSITION(framebuffer_config[0], 0x00117);
  210. ASSERT_REG_POSITION(framebuffer_config[1], 0x00157);
  211. ASSERT_REG_POSITION(display_transfer_config, 0x00300);
  212. ASSERT_REG_POSITION(command_processor_config, 0x00638);
  213. #undef ASSERT_REG_POSITION
  214. #endif // !defined(_MSC_VER)
  215. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  216. static_assert(sizeof(Regs) == 0x1000 * sizeof(u32), "Invalid total size of register set");
  217. extern Regs g_regs;
  218. extern bool g_skip_frame;
  219. template <typename T>
  220. void Read(T &var, const u32 addr);
  221. template <typename T>
  222. void Write(u32 addr, const T data);
  223. /// Initialize hardware
  224. void Init();
  225. /// Shutdown hardware
  226. void Shutdown();
  227. } // namespace