pica.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <initializer_list>
  7. #include <map>
  8. #include "common/bit_field.h"
  9. #include "common/common_types.h"
  10. #include "core/mem_map.h"
  11. namespace Pica {
  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. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  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 PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  19. // will then make sure the offsets indeed match the automatically calculated ones).
  20. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  21. #if defined(_MSC_VER)
  22. #define PICA_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 PICA_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 PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  29. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  30. #endif // _MSC_VER
  31. struct Regs {
  32. // helper macro to properly align structure members.
  33. // Calling INSERT_PADDING_WORDS will add a new member variable with a name like "pad121",
  34. // depending on the current source line to make sure variable names are unique.
  35. #define INSERT_PADDING_WORDS_HELPER1(x, y) x ## y
  36. #define INSERT_PADDING_WORDS_HELPER2(x, y) INSERT_PADDING_WORDS_HELPER1(x, y)
  37. #define INSERT_PADDING_WORDS(num_words) u32 INSERT_PADDING_WORDS_HELPER2(pad, __LINE__)[(num_words)];
  38. INSERT_PADDING_WORDS(0x41);
  39. BitField<0, 24, u32> viewport_size_x;
  40. INSERT_PADDING_WORDS(0x1);
  41. BitField<0, 24, u32> viewport_size_y;
  42. INSERT_PADDING_WORDS(0x9);
  43. BitField<0, 24, u32> viewport_depth_range; // float24
  44. BitField<0, 24, u32> viewport_depth_far_plane; // float24
  45. INSERT_PADDING_WORDS(0x1);
  46. union {
  47. // Maps components of output vertex attributes to semantics
  48. enum Semantic : u32
  49. {
  50. POSITION_X = 0,
  51. POSITION_Y = 1,
  52. POSITION_Z = 2,
  53. POSITION_W = 3,
  54. COLOR_R = 8,
  55. COLOR_G = 9,
  56. COLOR_B = 10,
  57. COLOR_A = 11,
  58. TEXCOORD0_U = 12,
  59. TEXCOORD0_V = 13,
  60. TEXCOORD1_U = 14,
  61. TEXCOORD1_V = 15,
  62. TEXCOORD2_U = 22,
  63. TEXCOORD2_V = 23,
  64. INVALID = 31,
  65. };
  66. BitField< 0, 5, Semantic> map_x;
  67. BitField< 8, 5, Semantic> map_y;
  68. BitField<16, 5, Semantic> map_z;
  69. BitField<24, 5, Semantic> map_w;
  70. } vs_output_attributes[7];
  71. INSERT_PADDING_WORDS(0x11);
  72. union {
  73. BitField< 0, 16, u32> x;
  74. BitField<16, 16, u32> y;
  75. } viewport_corner;
  76. INSERT_PADDING_WORDS(0xa7);
  77. struct {
  78. enum ColorFormat : u32 {
  79. RGBA8 = 0,
  80. RGB8 = 1,
  81. RGBA5551 = 2,
  82. RGB565 = 3,
  83. RGBA4 = 4,
  84. };
  85. INSERT_PADDING_WORDS(0x6);
  86. u32 depth_format;
  87. u32 color_format;
  88. INSERT_PADDING_WORDS(0x4);
  89. u32 depth_buffer_address;
  90. u32 color_buffer_address;
  91. union {
  92. // Apparently, the framebuffer width is stored as expected,
  93. // while the height is stored as the actual height minus one.
  94. // Hence, don't access these fields directly but use the accessors
  95. // GetWidth() and GetHeight() instead.
  96. BitField< 0, 11, u32> width;
  97. BitField<12, 10, u32> height;
  98. };
  99. INSERT_PADDING_WORDS(0x1);
  100. inline u32 GetColorBufferAddress() const {
  101. return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(color_buffer_address));
  102. }
  103. inline u32 GetDepthBufferAddress() const {
  104. return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(depth_buffer_address));
  105. }
  106. inline u32 GetWidth() const {
  107. return width;
  108. }
  109. inline u32 GetHeight() const {
  110. return height + 1;
  111. }
  112. } framebuffer;
  113. INSERT_PADDING_WORDS(0xe0);
  114. struct {
  115. enum class Format : u64 {
  116. BYTE = 0,
  117. UBYTE = 1,
  118. SHORT = 2,
  119. FLOAT = 3,
  120. };
  121. BitField<0, 29, u32> base_address;
  122. inline u32 GetBaseAddress() const {
  123. // TODO: Ugly, should fix PhysicalToVirtualAddress instead
  124. return DecodeAddressRegister(base_address) - Memory::FCRAM_PADDR + Memory::HEAP_GSP_VADDR;
  125. }
  126. // Descriptor for internal vertex attributes
  127. union {
  128. BitField< 0, 2, Format> format0; // size of one element
  129. BitField< 2, 2, u64> size0; // number of elements minus 1
  130. BitField< 4, 2, Format> format1;
  131. BitField< 6, 2, u64> size1;
  132. BitField< 8, 2, Format> format2;
  133. BitField<10, 2, u64> size2;
  134. BitField<12, 2, Format> format3;
  135. BitField<14, 2, u64> size3;
  136. BitField<16, 2, Format> format4;
  137. BitField<18, 2, u64> size4;
  138. BitField<20, 2, Format> format5;
  139. BitField<22, 2, u64> size5;
  140. BitField<24, 2, Format> format6;
  141. BitField<26, 2, u64> size6;
  142. BitField<28, 2, Format> format7;
  143. BitField<30, 2, u64> size7;
  144. BitField<32, 2, Format> format8;
  145. BitField<34, 2, u64> size8;
  146. BitField<36, 2, Format> format9;
  147. BitField<38, 2, u64> size9;
  148. BitField<40, 2, Format> format10;
  149. BitField<42, 2, u64> size10;
  150. BitField<44, 2, Format> format11;
  151. BitField<46, 2, u64> size11;
  152. BitField<48, 12, u64> attribute_mask;
  153. // number of total attributes minus 1
  154. BitField<60, 4, u64> num_extra_attributes;
  155. };
  156. inline Format GetFormat(int n) const {
  157. Format formats[] = {
  158. format0, format1, format2, format3,
  159. format4, format5, format6, format7,
  160. format8, format9, format10, format11
  161. };
  162. return formats[n];
  163. }
  164. inline int GetNumElements(int n) const {
  165. u64 sizes[] = {
  166. size0, size1, size2, size3,
  167. size4, size5, size6, size7,
  168. size8, size9, size10, size11
  169. };
  170. return (int)sizes[n]+1;
  171. }
  172. inline int GetElementSizeInBytes(int n) const {
  173. return (GetFormat(n) == Format::FLOAT) ? 4 :
  174. (GetFormat(n) == Format::SHORT) ? 2 : 1;
  175. }
  176. inline int GetStride(int n) const {
  177. return GetNumElements(n) * GetElementSizeInBytes(n);
  178. }
  179. inline int GetNumTotalAttributes() const {
  180. return (int)num_extra_attributes+1;
  181. }
  182. // Attribute loaders map the source vertex data to input attributes
  183. // This e.g. allows to load different attributes from different memory locations
  184. struct {
  185. // Source attribute data offset from the base address
  186. u32 data_offset;
  187. union {
  188. BitField< 0, 4, u64> comp0;
  189. BitField< 4, 4, u64> comp1;
  190. BitField< 8, 4, u64> comp2;
  191. BitField<12, 4, u64> comp3;
  192. BitField<16, 4, u64> comp4;
  193. BitField<20, 4, u64> comp5;
  194. BitField<24, 4, u64> comp6;
  195. BitField<28, 4, u64> comp7;
  196. BitField<32, 4, u64> comp8;
  197. BitField<36, 4, u64> comp9;
  198. BitField<40, 4, u64> comp10;
  199. BitField<44, 4, u64> comp11;
  200. // bytes for a single vertex in this loader
  201. BitField<48, 8, u64> byte_count;
  202. BitField<60, 4, u64> component_count;
  203. };
  204. inline int GetComponent(int n) const {
  205. u64 components[] = {
  206. comp0, comp1, comp2, comp3,
  207. comp4, comp5, comp6, comp7,
  208. comp8, comp9, comp10, comp11
  209. };
  210. return (int)components[n];
  211. }
  212. } attribute_loaders[12];
  213. } vertex_attributes;
  214. struct {
  215. enum IndexFormat : u32 {
  216. BYTE = 0,
  217. SHORT = 1,
  218. };
  219. union {
  220. BitField<0, 31, u32> offset; // relative to base attribute address
  221. BitField<31, 1, IndexFormat> format;
  222. };
  223. } index_array;
  224. // Number of vertices to render
  225. u32 num_vertices;
  226. INSERT_PADDING_WORDS(0x5);
  227. // These two trigger rendering of triangles
  228. u32 trigger_draw;
  229. u32 trigger_draw_indexed;
  230. INSERT_PADDING_WORDS(0x2e);
  231. enum class TriangleTopology : u32 {
  232. List = 0,
  233. Strip = 1,
  234. Fan = 2,
  235. ListIndexed = 3, // TODO: No idea if this is correct
  236. };
  237. BitField<8, 2, TriangleTopology> triangle_topology;
  238. INSERT_PADDING_WORDS(0x5b);
  239. // Offset to shader program entry point (in words)
  240. BitField<0, 16, u32> vs_main_offset;
  241. union {
  242. BitField< 0, 4, u64> attribute0_register;
  243. BitField< 4, 4, u64> attribute1_register;
  244. BitField< 8, 4, u64> attribute2_register;
  245. BitField<12, 4, u64> attribute3_register;
  246. BitField<16, 4, u64> attribute4_register;
  247. BitField<20, 4, u64> attribute5_register;
  248. BitField<24, 4, u64> attribute6_register;
  249. BitField<28, 4, u64> attribute7_register;
  250. BitField<32, 4, u64> attribute8_register;
  251. BitField<36, 4, u64> attribute9_register;
  252. BitField<40, 4, u64> attribute10_register;
  253. BitField<44, 4, u64> attribute11_register;
  254. BitField<48, 4, u64> attribute12_register;
  255. BitField<52, 4, u64> attribute13_register;
  256. BitField<56, 4, u64> attribute14_register;
  257. BitField<60, 4, u64> attribute15_register;
  258. int GetRegisterForAttribute(int attribute_index) {
  259. u64 fields[] = {
  260. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  261. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  262. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  263. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  264. };
  265. return (int)fields[attribute_index];
  266. }
  267. } vs_input_register_map;
  268. INSERT_PADDING_WORDS(0x3);
  269. struct {
  270. enum Format : u32
  271. {
  272. FLOAT24 = 0,
  273. FLOAT32 = 1
  274. };
  275. bool IsFloat32() const {
  276. return format == FLOAT32;
  277. }
  278. union {
  279. // Index of the next uniform to write to
  280. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  281. BitField<0, 7, u32> index;
  282. BitField<31, 1, Format> format;
  283. };
  284. // Writing to these registers sets the "current" uniform.
  285. // TODO: It's not clear how the hardware stores what the "current" uniform is.
  286. u32 set_value[8];
  287. } vs_uniform_setup;
  288. INSERT_PADDING_WORDS(0x2);
  289. struct {
  290. u32 begin_load;
  291. // Writing to these registers sets the "current" word in the shader program.
  292. // TODO: It's not clear how the hardware stores what the "current" word is.
  293. u32 set_word[8];
  294. } vs_program;
  295. INSERT_PADDING_WORDS(0x1);
  296. // This register group is used to load an internal table of swizzling patterns,
  297. // which are indexed by each shader instruction to specify vector component swizzling.
  298. struct {
  299. u32 begin_load;
  300. // Writing to these registers sets the "current" swizzle pattern in the table.
  301. // TODO: It's not clear how the hardware stores what the "current" swizzle pattern is.
  302. u32 set_word[8];
  303. } vs_swizzle_patterns;
  304. INSERT_PADDING_WORDS(0x22);
  305. #undef INSERT_PADDING_WORDS_HELPER1
  306. #undef INSERT_PADDING_WORDS_HELPER2
  307. #undef INSERT_PADDING_WORDS
  308. // Map register indices to names readable by humans
  309. // Used for debugging purposes, so performance is not an issue here
  310. static std::string GetCommandName(int index) {
  311. std::map<u32, std::string> map;
  312. Regs regs;
  313. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  314. // is technically allowed since C++11. Hence, this functionality is disabled until
  315. // MSVC properly supports it.
  316. #ifndef _MSC_VER
  317. #define ADD_FIELD(name) \
  318. do { \
  319. map.insert({PICA_REG_INDEX(name), #name}); \
  320. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(regs.name) / 4; ++i) \
  321. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  322. } while(false)
  323. ADD_FIELD(viewport_size_x);
  324. ADD_FIELD(viewport_size_y);
  325. ADD_FIELD(viewport_depth_range);
  326. ADD_FIELD(viewport_depth_far_plane);
  327. ADD_FIELD(viewport_corner);
  328. ADD_FIELD(framebuffer);
  329. ADD_FIELD(vertex_attributes);
  330. ADD_FIELD(index_array);
  331. ADD_FIELD(num_vertices);
  332. ADD_FIELD(trigger_draw);
  333. ADD_FIELD(trigger_draw_indexed);
  334. ADD_FIELD(triangle_topology);
  335. ADD_FIELD(vs_main_offset);
  336. ADD_FIELD(vs_input_register_map);
  337. ADD_FIELD(vs_uniform_setup);
  338. ADD_FIELD(vs_program);
  339. ADD_FIELD(vs_swizzle_patterns);
  340. #undef ADD_FIELD
  341. #endif // _MSC_VER
  342. // Return empty string if no match is found
  343. return map[index];
  344. }
  345. static inline int NumIds() {
  346. return sizeof(Regs) / sizeof(u32);
  347. }
  348. u32& operator [] (int index) const {
  349. u32* content = (u32*)this;
  350. return content[index];
  351. }
  352. u32& operator [] (int index) {
  353. u32* content = (u32*)this;
  354. return content[index];
  355. }
  356. private:
  357. /*
  358. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  359. * This function should be used to get the address from a raw register value.
  360. */
  361. static inline u32 DecodeAddressRegister(u32 register_value) {
  362. return register_value * 8;
  363. }
  364. };
  365. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  366. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  367. // support for that.
  368. #ifndef _MSC_VER
  369. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  370. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  371. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  372. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  373. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  374. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  375. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  376. ASSERT_REG_POSITION(viewport_corner, 0x68);
  377. ASSERT_REG_POSITION(framebuffer, 0x110);
  378. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  379. ASSERT_REG_POSITION(index_array, 0x227);
  380. ASSERT_REG_POSITION(num_vertices, 0x228);
  381. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  382. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  383. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  384. ASSERT_REG_POSITION(vs_main_offset, 0x2ba);
  385. ASSERT_REG_POSITION(vs_input_register_map, 0x2bb);
  386. ASSERT_REG_POSITION(vs_uniform_setup, 0x2c0);
  387. ASSERT_REG_POSITION(vs_program, 0x2cb);
  388. ASSERT_REG_POSITION(vs_swizzle_patterns, 0x2d5);
  389. #undef ASSERT_REG_POSITION
  390. #endif // !defined(_MSC_VER)
  391. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  392. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  393. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  394. extern Regs registers; // TODO: Not sure if we want to have one global instance for this
  395. struct float24 {
  396. static float24 FromFloat32(float val) {
  397. float24 ret;
  398. ret.value = val;
  399. return ret;
  400. }
  401. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  402. // TODO: No idea if this works as intended
  403. static float24 FromRawFloat24(u32 hex) {
  404. float24 ret;
  405. if ((hex & 0xFFFFFF) == 0) {
  406. ret.value = 0;
  407. } else {
  408. u32 mantissa = hex & 0xFFFF;
  409. u32 exponent = (hex >> 16) & 0x7F;
  410. u32 sign = hex >> 23;
  411. ret.value = powf(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * powf(2.0f, -16.f));
  412. if (sign)
  413. ret.value = -ret.value;
  414. }
  415. return ret;
  416. }
  417. // Not recommended for anything but logging
  418. float ToFloat32() const {
  419. return value;
  420. }
  421. float24 operator * (const float24& flt) const {
  422. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  423. }
  424. float24 operator / (const float24& flt) const {
  425. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  426. }
  427. float24 operator + (const float24& flt) const {
  428. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  429. }
  430. float24 operator - (const float24& flt) const {
  431. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  432. }
  433. float24 operator - () const {
  434. return float24::FromFloat32(-ToFloat32());
  435. }
  436. bool operator < (const float24& flt) const {
  437. return ToFloat32() < flt.ToFloat32();
  438. }
  439. bool operator > (const float24& flt) const {
  440. return ToFloat32() > flt.ToFloat32();
  441. }
  442. bool operator >= (const float24& flt) const {
  443. return ToFloat32() >= flt.ToFloat32();
  444. }
  445. bool operator <= (const float24& flt) const {
  446. return ToFloat32() <= flt.ToFloat32();
  447. }
  448. private:
  449. float24() = default;
  450. // Stored as a regular float, merely for convenience
  451. // TODO: Perform proper arithmetic on this!
  452. float value;
  453. };
  454. union CommandHeader {
  455. CommandHeader(u32 h) : hex(h) {}
  456. u32 hex;
  457. BitField< 0, 16, u32> cmd_id;
  458. BitField<16, 4, u32> parameter_mask;
  459. BitField<20, 11, u32> extra_data_length;
  460. BitField<31, 1, u32> group_commands;
  461. };
  462. } // namespace