gpu.h 10 KB

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