pica.h 24 KB

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