pica.h 24 KB

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