pica.h 24 KB

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