gpu.h 9.4 KB

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