pica.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <initializer_list>
  8. #include <map>
  9. #include "common/bit_field.h"
  10. #include "common/common_types.h"
  11. #include "core/mem_map.h"
  12. namespace Pica {
  13. // Returns index corresponding to the Regs member labeled by field_name
  14. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  15. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  16. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  17. // Hopefully, this will be fixed sometime in the future.
  18. // For lack of better alternatives, we currently hardcode the offsets when constant
  19. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  20. // will then make sure the offsets indeed match the automatically calculated ones).
  21. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  22. #if defined(_MSC_VER)
  23. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  24. #else
  25. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  26. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  27. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  28. // field offset. Otherwise, the compiler will fail to compile this code.
  29. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  30. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  31. #endif // _MSC_VER
  32. struct Regs {
  33. // helper macro to properly align structure members.
  34. // Calling INSERT_PADDING_WORDS will add a new member variable with a name like "pad121",
  35. // depending on the current source line to make sure variable names are unique.
  36. #define INSERT_PADDING_WORDS_HELPER1(x, y) x ## y
  37. #define INSERT_PADDING_WORDS_HELPER2(x, y) INSERT_PADDING_WORDS_HELPER1(x, y)
  38. #define INSERT_PADDING_WORDS(num_words) u32 INSERT_PADDING_WORDS_HELPER2(pad, __LINE__)[(num_words)];
  39. INSERT_PADDING_WORDS(0x41);
  40. BitField<0, 24, u32> viewport_size_x;
  41. INSERT_PADDING_WORDS(0x1);
  42. BitField<0, 24, u32> viewport_size_y;
  43. INSERT_PADDING_WORDS(0x9);
  44. BitField<0, 24, u32> viewport_depth_range; // float24
  45. BitField<0, 24, u32> viewport_depth_far_plane; // float24
  46. INSERT_PADDING_WORDS(0x1);
  47. union VSOutputAttributes {
  48. // Maps components of output vertex attributes to semantics
  49. enum Semantic : u32
  50. {
  51. POSITION_X = 0,
  52. POSITION_Y = 1,
  53. POSITION_Z = 2,
  54. POSITION_W = 3,
  55. COLOR_R = 8,
  56. COLOR_G = 9,
  57. COLOR_B = 10,
  58. COLOR_A = 11,
  59. TEXCOORD0_U = 12,
  60. TEXCOORD0_V = 13,
  61. TEXCOORD1_U = 14,
  62. TEXCOORD1_V = 15,
  63. TEXCOORD2_U = 22,
  64. TEXCOORD2_V = 23,
  65. INVALID = 31,
  66. };
  67. BitField< 0, 5, Semantic> map_x;
  68. BitField< 8, 5, Semantic> map_y;
  69. BitField<16, 5, Semantic> map_z;
  70. BitField<24, 5, Semantic> map_w;
  71. } vs_output_attributes[7];
  72. INSERT_PADDING_WORDS(0x11);
  73. union {
  74. BitField< 0, 16, u32> x;
  75. BitField<16, 16, u32> y;
  76. } viewport_corner;
  77. INSERT_PADDING_WORDS(0x17);
  78. struct TextureConfig {
  79. INSERT_PADDING_WORDS(0x1);
  80. union {
  81. BitField< 0, 16, u32> height;
  82. BitField<16, 16, u32> width;
  83. };
  84. INSERT_PADDING_WORDS(0x2);
  85. u32 address;
  86. u32 GetPhysicalAddress() {
  87. return DecodeAddressRegister(address) - Memory::FCRAM_PADDR + Memory::HEAP_GSP_VADDR;
  88. }
  89. // texture1 and texture2 store the texture format directly after the address
  90. // whereas texture0 inserts some additional flags inbetween.
  91. // Hence, we store the format separately so that all other parameters can be described
  92. // in a single structure.
  93. };
  94. enum class TextureFormat : u32 {
  95. RGBA8 = 0,
  96. RGB8 = 1,
  97. RGBA5551 = 2,
  98. RGB565 = 3,
  99. RGBA4 = 4,
  100. // TODO: Support for the other formats is not implemented, yet.
  101. // Seems like they are luminance formats and compressed textures.
  102. };
  103. BitField<0, 1, u32> texturing_enable;
  104. TextureConfig texture0;
  105. INSERT_PADDING_WORDS(0x8);
  106. BitField<0, 4, TextureFormat> texture0_format;
  107. INSERT_PADDING_WORDS(0x31);
  108. // 0xc0-0xff: Texture Combiner (akin to glTexEnv)
  109. struct TevStageConfig {
  110. enum class Source : u32 {
  111. PrimaryColor = 0x0,
  112. Texture0 = 0x3,
  113. Texture1 = 0x4,
  114. Texture2 = 0x5,
  115. Texture3 = 0x6,
  116. // 0x7-0xc = primary color??
  117. Constant = 0xe,
  118. Previous = 0xf,
  119. };
  120. enum class ColorModifier : u32 {
  121. SourceColor = 0,
  122. OneMinusSourceColor = 1,
  123. SourceAlpha = 2,
  124. OneMinusSourceAlpha = 3,
  125. // Other values seem to be non-standard extensions
  126. };
  127. enum class AlphaModifier : u32 {
  128. SourceAlpha = 0,
  129. OneMinusSourceAlpha = 1,
  130. // Other values seem to be non-standard extensions
  131. };
  132. enum class Operation : u32 {
  133. Replace = 0,
  134. Modulate = 1,
  135. Add = 2,
  136. AddSigned = 3,
  137. Lerp = 4,
  138. Subtract = 5,
  139. };
  140. union {
  141. BitField< 0, 4, Source> color_source1;
  142. BitField< 4, 4, Source> color_source2;
  143. BitField< 8, 4, Source> color_source3;
  144. BitField<16, 4, Source> alpha_source1;
  145. BitField<20, 4, Source> alpha_source2;
  146. BitField<24, 4, Source> alpha_source3;
  147. };
  148. union {
  149. BitField< 0, 4, ColorModifier> color_modifier1;
  150. BitField< 4, 4, ColorModifier> color_modifier2;
  151. BitField< 8, 4, ColorModifier> color_modifier3;
  152. BitField<12, 3, AlphaModifier> alpha_modifier1;
  153. BitField<16, 3, AlphaModifier> alpha_modifier2;
  154. BitField<20, 3, AlphaModifier> alpha_modifier3;
  155. };
  156. union {
  157. BitField< 0, 4, Operation> color_op;
  158. BitField<16, 4, Operation> alpha_op;
  159. };
  160. union {
  161. BitField< 0, 8, u32> const_r;
  162. BitField< 8, 8, u32> const_g;
  163. BitField<16, 8, u32> const_b;
  164. BitField<24, 8, u32> const_a;
  165. };
  166. INSERT_PADDING_WORDS(0x1);
  167. };
  168. TevStageConfig tev_stage0;
  169. INSERT_PADDING_WORDS(0x3);
  170. TevStageConfig tev_stage1;
  171. INSERT_PADDING_WORDS(0x3);
  172. TevStageConfig tev_stage2;
  173. INSERT_PADDING_WORDS(0x3);
  174. TevStageConfig tev_stage3;
  175. INSERT_PADDING_WORDS(0x13);
  176. TevStageConfig tev_stage4;
  177. INSERT_PADDING_WORDS(0x3);
  178. TevStageConfig tev_stage5;
  179. INSERT_PADDING_WORDS(0x13);
  180. const std::array<Regs::TevStageConfig,6> GetTevStages() const {
  181. return { tev_stage0, tev_stage1,
  182. tev_stage2, tev_stage3,
  183. tev_stage4, tev_stage5 };
  184. };
  185. struct {
  186. enum ColorFormat : u32 {
  187. RGBA8 = 0,
  188. RGB8 = 1,
  189. RGBA5551 = 2,
  190. RGB565 = 3,
  191. RGBA4 = 4,
  192. };
  193. INSERT_PADDING_WORDS(0x6);
  194. u32 depth_format;
  195. u32 color_format;
  196. INSERT_PADDING_WORDS(0x4);
  197. u32 depth_buffer_address;
  198. u32 color_buffer_address;
  199. union {
  200. // Apparently, the framebuffer width is stored as expected,
  201. // while the height is stored as the actual height minus one.
  202. // Hence, don't access these fields directly but use the accessors
  203. // GetWidth() and GetHeight() instead.
  204. BitField< 0, 11, u32> width;
  205. BitField<12, 10, u32> height;
  206. };
  207. INSERT_PADDING_WORDS(0x1);
  208. inline u32 GetColorBufferAddress() const {
  209. return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(color_buffer_address));
  210. }
  211. inline u32 GetDepthBufferAddress() const {
  212. return Memory::PhysicalToVirtualAddress(DecodeAddressRegister(depth_buffer_address));
  213. }
  214. inline u32 GetWidth() const {
  215. return width;
  216. }
  217. inline u32 GetHeight() const {
  218. return height + 1;
  219. }
  220. } framebuffer;
  221. INSERT_PADDING_WORDS(0xe0);
  222. struct {
  223. enum class Format : u64 {
  224. BYTE = 0,
  225. UBYTE = 1,
  226. SHORT = 2,
  227. FLOAT = 3,
  228. };
  229. BitField<0, 29, u32> base_address;
  230. inline u32 GetBaseAddress() const {
  231. // TODO: Ugly, should fix PhysicalToVirtualAddress instead
  232. return DecodeAddressRegister(base_address) - Memory::FCRAM_PADDR + Memory::HEAP_GSP_VADDR;
  233. }
  234. // Descriptor for internal vertex attributes
  235. union {
  236. BitField< 0, 2, Format> format0; // size of one element
  237. BitField< 2, 2, u64> size0; // number of elements minus 1
  238. BitField< 4, 2, Format> format1;
  239. BitField< 6, 2, u64> size1;
  240. BitField< 8, 2, Format> format2;
  241. BitField<10, 2, u64> size2;
  242. BitField<12, 2, Format> format3;
  243. BitField<14, 2, u64> size3;
  244. BitField<16, 2, Format> format4;
  245. BitField<18, 2, u64> size4;
  246. BitField<20, 2, Format> format5;
  247. BitField<22, 2, u64> size5;
  248. BitField<24, 2, Format> format6;
  249. BitField<26, 2, u64> size6;
  250. BitField<28, 2, Format> format7;
  251. BitField<30, 2, u64> size7;
  252. BitField<32, 2, Format> format8;
  253. BitField<34, 2, u64> size8;
  254. BitField<36, 2, Format> format9;
  255. BitField<38, 2, u64> size9;
  256. BitField<40, 2, Format> format10;
  257. BitField<42, 2, u64> size10;
  258. BitField<44, 2, Format> format11;
  259. BitField<46, 2, u64> size11;
  260. BitField<48, 12, u64> attribute_mask;
  261. // number of total attributes minus 1
  262. BitField<60, 4, u64> num_extra_attributes;
  263. };
  264. inline Format GetFormat(int n) const {
  265. Format formats[] = {
  266. format0, format1, format2, format3,
  267. format4, format5, format6, format7,
  268. format8, format9, format10, format11
  269. };
  270. return formats[n];
  271. }
  272. inline int GetNumElements(int n) const {
  273. u64 sizes[] = {
  274. size0, size1, size2, size3,
  275. size4, size5, size6, size7,
  276. size8, size9, size10, size11
  277. };
  278. return (int)sizes[n]+1;
  279. }
  280. inline int GetElementSizeInBytes(int n) const {
  281. return (GetFormat(n) == Format::FLOAT) ? 4 :
  282. (GetFormat(n) == Format::SHORT) ? 2 : 1;
  283. }
  284. inline int GetStride(int n) const {
  285. return GetNumElements(n) * GetElementSizeInBytes(n);
  286. }
  287. inline int GetNumTotalAttributes() const {
  288. return (int)num_extra_attributes+1;
  289. }
  290. // Attribute loaders map the source vertex data to input attributes
  291. // This e.g. allows to load different attributes from different memory locations
  292. struct {
  293. // Source attribute data offset from the base address
  294. u32 data_offset;
  295. union {
  296. BitField< 0, 4, u64> comp0;
  297. BitField< 4, 4, u64> comp1;
  298. BitField< 8, 4, u64> comp2;
  299. BitField<12, 4, u64> comp3;
  300. BitField<16, 4, u64> comp4;
  301. BitField<20, 4, u64> comp5;
  302. BitField<24, 4, u64> comp6;
  303. BitField<28, 4, u64> comp7;
  304. BitField<32, 4, u64> comp8;
  305. BitField<36, 4, u64> comp9;
  306. BitField<40, 4, u64> comp10;
  307. BitField<44, 4, u64> comp11;
  308. // bytes for a single vertex in this loader
  309. BitField<48, 8, u64> byte_count;
  310. BitField<60, 4, u64> component_count;
  311. };
  312. inline int GetComponent(int n) const {
  313. u64 components[] = {
  314. comp0, comp1, comp2, comp3,
  315. comp4, comp5, comp6, comp7,
  316. comp8, comp9, comp10, comp11
  317. };
  318. return (int)components[n];
  319. }
  320. } attribute_loaders[12];
  321. } vertex_attributes;
  322. struct {
  323. enum IndexFormat : u32 {
  324. BYTE = 0,
  325. SHORT = 1,
  326. };
  327. union {
  328. BitField<0, 31, u32> offset; // relative to base attribute address
  329. BitField<31, 1, IndexFormat> format;
  330. };
  331. } index_array;
  332. // Number of vertices to render
  333. u32 num_vertices;
  334. INSERT_PADDING_WORDS(0x5);
  335. // These two trigger rendering of triangles
  336. u32 trigger_draw;
  337. u32 trigger_draw_indexed;
  338. INSERT_PADDING_WORDS(0x2e);
  339. enum class TriangleTopology : u32 {
  340. List = 0,
  341. Strip = 1,
  342. Fan = 2,
  343. ListIndexed = 3, // TODO: No idea if this is correct
  344. };
  345. BitField<8, 2, TriangleTopology> triangle_topology;
  346. INSERT_PADDING_WORDS(0x5b);
  347. // Offset to shader program entry point (in words)
  348. BitField<0, 16, u32> vs_main_offset;
  349. union {
  350. BitField< 0, 4, u64> attribute0_register;
  351. BitField< 4, 4, u64> attribute1_register;
  352. BitField< 8, 4, u64> attribute2_register;
  353. BitField<12, 4, u64> attribute3_register;
  354. BitField<16, 4, u64> attribute4_register;
  355. BitField<20, 4, u64> attribute5_register;
  356. BitField<24, 4, u64> attribute6_register;
  357. BitField<28, 4, u64> attribute7_register;
  358. BitField<32, 4, u64> attribute8_register;
  359. BitField<36, 4, u64> attribute9_register;
  360. BitField<40, 4, u64> attribute10_register;
  361. BitField<44, 4, u64> attribute11_register;
  362. BitField<48, 4, u64> attribute12_register;
  363. BitField<52, 4, u64> attribute13_register;
  364. BitField<56, 4, u64> attribute14_register;
  365. BitField<60, 4, u64> attribute15_register;
  366. int GetRegisterForAttribute(int attribute_index) {
  367. u64 fields[] = {
  368. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  369. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  370. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  371. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  372. };
  373. return (int)fields[attribute_index];
  374. }
  375. } vs_input_register_map;
  376. INSERT_PADDING_WORDS(0x3);
  377. struct {
  378. enum Format : u32
  379. {
  380. FLOAT24 = 0,
  381. FLOAT32 = 1
  382. };
  383. bool IsFloat32() const {
  384. return format == FLOAT32;
  385. }
  386. union {
  387. // Index of the next uniform to write to
  388. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  389. BitField<0, 7, u32> index;
  390. BitField<31, 1, Format> format;
  391. };
  392. // Writing to these registers sets the "current" uniform.
  393. // TODO: It's not clear how the hardware stores what the "current" uniform is.
  394. u32 set_value[8];
  395. } vs_uniform_setup;
  396. INSERT_PADDING_WORDS(0x2);
  397. struct {
  398. u32 begin_load;
  399. // Writing to these registers sets the "current" word in the shader program.
  400. // TODO: It's not clear how the hardware stores what the "current" word is.
  401. u32 set_word[8];
  402. } vs_program;
  403. INSERT_PADDING_WORDS(0x1);
  404. // This register group is used to load an internal table of swizzling patterns,
  405. // which are indexed by each shader instruction to specify vector component swizzling.
  406. struct {
  407. u32 begin_load;
  408. // Writing to these registers sets the "current" swizzle pattern in the table.
  409. // TODO: It's not clear how the hardware stores what the "current" swizzle pattern is.
  410. u32 set_word[8];
  411. } vs_swizzle_patterns;
  412. INSERT_PADDING_WORDS(0x22);
  413. #undef INSERT_PADDING_WORDS_HELPER1
  414. #undef INSERT_PADDING_WORDS_HELPER2
  415. #undef INSERT_PADDING_WORDS
  416. // Map register indices to names readable by humans
  417. // Used for debugging purposes, so performance is not an issue here
  418. static std::string GetCommandName(int index) {
  419. std::map<u32, std::string> map;
  420. Regs regs;
  421. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  422. // is technically allowed since C++11. Hence, this functionality is disabled until
  423. // MSVC properly supports it.
  424. #ifndef _MSC_VER
  425. #define ADD_FIELD(name) \
  426. do { \
  427. map.insert({PICA_REG_INDEX(name), #name}); \
  428. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(regs.name) / 4; ++i) \
  429. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  430. } while(false)
  431. ADD_FIELD(viewport_size_x);
  432. ADD_FIELD(viewport_size_y);
  433. ADD_FIELD(viewport_depth_range);
  434. ADD_FIELD(viewport_depth_far_plane);
  435. ADD_FIELD(viewport_corner);
  436. ADD_FIELD(texturing_enable);
  437. ADD_FIELD(texture0);
  438. ADD_FIELD(texture0_format);
  439. ADD_FIELD(tev_stage0);
  440. ADD_FIELD(tev_stage1);
  441. ADD_FIELD(tev_stage2);
  442. ADD_FIELD(tev_stage3);
  443. ADD_FIELD(tev_stage4);
  444. ADD_FIELD(tev_stage5);
  445. ADD_FIELD(framebuffer);
  446. ADD_FIELD(vertex_attributes);
  447. ADD_FIELD(index_array);
  448. ADD_FIELD(num_vertices);
  449. ADD_FIELD(trigger_draw);
  450. ADD_FIELD(trigger_draw_indexed);
  451. ADD_FIELD(triangle_topology);
  452. ADD_FIELD(vs_main_offset);
  453. ADD_FIELD(vs_input_register_map);
  454. ADD_FIELD(vs_uniform_setup);
  455. ADD_FIELD(vs_program);
  456. ADD_FIELD(vs_swizzle_patterns);
  457. #undef ADD_FIELD
  458. #endif // _MSC_VER
  459. // Return empty string if no match is found
  460. return map[index];
  461. }
  462. static inline int NumIds() {
  463. return sizeof(Regs) / sizeof(u32);
  464. }
  465. u32& operator [] (int index) const {
  466. u32* content = (u32*)this;
  467. return content[index];
  468. }
  469. u32& operator [] (int index) {
  470. u32* content = (u32*)this;
  471. return content[index];
  472. }
  473. private:
  474. /*
  475. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  476. * This function should be used to get the address from a raw register value.
  477. */
  478. static inline u32 DecodeAddressRegister(u32 register_value) {
  479. return register_value * 8;
  480. }
  481. };
  482. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  483. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  484. // support for that.
  485. #ifndef _MSC_VER
  486. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  487. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  488. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  489. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  490. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  491. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  492. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  493. ASSERT_REG_POSITION(viewport_corner, 0x68);
  494. ASSERT_REG_POSITION(texturing_enable, 0x80);
  495. ASSERT_REG_POSITION(texture0, 0x81);
  496. ASSERT_REG_POSITION(texture0_format, 0x8e);
  497. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  498. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  499. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  500. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  501. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  502. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  503. ASSERT_REG_POSITION(framebuffer, 0x110);
  504. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  505. ASSERT_REG_POSITION(index_array, 0x227);
  506. ASSERT_REG_POSITION(num_vertices, 0x228);
  507. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  508. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  509. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  510. ASSERT_REG_POSITION(vs_main_offset, 0x2ba);
  511. ASSERT_REG_POSITION(vs_input_register_map, 0x2bb);
  512. ASSERT_REG_POSITION(vs_uniform_setup, 0x2c0);
  513. ASSERT_REG_POSITION(vs_program, 0x2cb);
  514. ASSERT_REG_POSITION(vs_swizzle_patterns, 0x2d5);
  515. #undef ASSERT_REG_POSITION
  516. #endif // !defined(_MSC_VER)
  517. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  518. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  519. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  520. extern Regs registers; // TODO: Not sure if we want to have one global instance for this
  521. struct float24 {
  522. static float24 FromFloat32(float val) {
  523. float24 ret;
  524. ret.value = val;
  525. return ret;
  526. }
  527. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  528. // TODO: No idea if this works as intended
  529. static float24 FromRawFloat24(u32 hex) {
  530. float24 ret;
  531. if ((hex & 0xFFFFFF) == 0) {
  532. ret.value = 0;
  533. } else {
  534. u32 mantissa = hex & 0xFFFF;
  535. u32 exponent = (hex >> 16) & 0x7F;
  536. u32 sign = hex >> 23;
  537. ret.value = powf(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * powf(2.0f, -16.f));
  538. if (sign)
  539. ret.value = -ret.value;
  540. }
  541. return ret;
  542. }
  543. // Not recommended for anything but logging
  544. float ToFloat32() const {
  545. return value;
  546. }
  547. float24 operator * (const float24& flt) const {
  548. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  549. }
  550. float24 operator / (const float24& flt) const {
  551. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  552. }
  553. float24 operator + (const float24& flt) const {
  554. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  555. }
  556. float24 operator - (const float24& flt) const {
  557. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  558. }
  559. float24 operator - () const {
  560. return float24::FromFloat32(-ToFloat32());
  561. }
  562. bool operator < (const float24& flt) const {
  563. return ToFloat32() < flt.ToFloat32();
  564. }
  565. bool operator > (const float24& flt) const {
  566. return ToFloat32() > flt.ToFloat32();
  567. }
  568. bool operator >= (const float24& flt) const {
  569. return ToFloat32() >= flt.ToFloat32();
  570. }
  571. bool operator <= (const float24& flt) const {
  572. return ToFloat32() <= flt.ToFloat32();
  573. }
  574. private:
  575. // Stored as a regular float, merely for convenience
  576. // TODO: Perform proper arithmetic on this!
  577. float value;
  578. };
  579. union CommandHeader {
  580. CommandHeader(u32 h) : hex(h) {}
  581. u32 hex;
  582. BitField< 0, 16, u32> cmd_id;
  583. BitField<16, 4, u32> parameter_mask;
  584. BitField<20, 11, u32> extra_data_length;
  585. BitField<31, 1, u32> group_commands;
  586. };
  587. } // namespace