pica.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  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 <vector>
  10. #include "common/bit_field.h"
  11. #include "common/common_types.h"
  12. #include "core/mem_map.h"
  13. namespace Pica {
  14. // Returns index corresponding to the Regs member labeled by field_name
  15. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  16. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  17. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  18. // Hopefully, this will be fixed sometime in the future.
  19. // For lack of better alternatives, we currently hardcode the offsets when constant
  20. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  21. // will then make sure the offsets indeed match the automatically calculated ones).
  22. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  23. #if defined(_MSC_VER)
  24. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  25. #else
  26. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  27. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  28. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  29. // field offset. Otherwise, the compiler will fail to compile this code.
  30. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  31. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  32. #endif // _MSC_VER
  33. struct Regs {
  34. // helper macro to properly align structure members.
  35. // Calling INSERT_PADDING_WORDS will add a new member variable with a name like "pad121",
  36. // depending on the current source line to make sure variable names are unique.
  37. #define INSERT_PADDING_WORDS_HELPER1(x, y) x ## y
  38. #define INSERT_PADDING_WORDS_HELPER2(x, y) INSERT_PADDING_WORDS_HELPER1(x, y)
  39. #define INSERT_PADDING_WORDS(num_words) u32 INSERT_PADDING_WORDS_HELPER2(pad, __LINE__)[(num_words)];
  40. INSERT_PADDING_WORDS(0x10);
  41. u32 trigger_irq;
  42. INSERT_PADDING_WORDS(0x2f);
  43. enum class CullMode : u32 {
  44. // Select which polygons are considered to be "frontfacing".
  45. KeepAll = 0,
  46. KeepClockWise = 1,
  47. KeepCounterClockWise = 2,
  48. // TODO: What does the third value imply?
  49. };
  50. union {
  51. BitField<0, 2, CullMode> cull_mode;
  52. };
  53. BitField<0, 24, u32> viewport_size_x;
  54. INSERT_PADDING_WORDS(0x1);
  55. BitField<0, 24, u32> viewport_size_y;
  56. INSERT_PADDING_WORDS(0x9);
  57. BitField<0, 24, u32> viewport_depth_range; // float24
  58. BitField<0, 24, u32> viewport_depth_far_plane; // float24
  59. INSERT_PADDING_WORDS(0x1);
  60. union VSOutputAttributes {
  61. // Maps components of output vertex attributes to semantics
  62. enum Semantic : u32
  63. {
  64. POSITION_X = 0,
  65. POSITION_Y = 1,
  66. POSITION_Z = 2,
  67. POSITION_W = 3,
  68. COLOR_R = 8,
  69. COLOR_G = 9,
  70. COLOR_B = 10,
  71. COLOR_A = 11,
  72. TEXCOORD0_U = 12,
  73. TEXCOORD0_V = 13,
  74. TEXCOORD1_U = 14,
  75. TEXCOORD1_V = 15,
  76. TEXCOORD2_U = 22,
  77. TEXCOORD2_V = 23,
  78. INVALID = 31,
  79. };
  80. BitField< 0, 5, Semantic> map_x;
  81. BitField< 8, 5, Semantic> map_y;
  82. BitField<16, 5, Semantic> map_z;
  83. BitField<24, 5, Semantic> map_w;
  84. } vs_output_attributes[7];
  85. INSERT_PADDING_WORDS(0x11);
  86. union {
  87. BitField< 0, 16, u32> x;
  88. BitField<16, 16, u32> y;
  89. } viewport_corner;
  90. INSERT_PADDING_WORDS(0x17);
  91. struct TextureConfig {
  92. enum WrapMode : u32 {
  93. ClampToEdge = 0,
  94. Repeat = 2,
  95. };
  96. INSERT_PADDING_WORDS(0x1);
  97. union {
  98. BitField< 0, 16, u32> height;
  99. BitField<16, 16, u32> width;
  100. };
  101. union {
  102. BitField< 8, 2, WrapMode> wrap_s;
  103. BitField<11, 2, WrapMode> wrap_t;
  104. };
  105. INSERT_PADDING_WORDS(0x1);
  106. u32 address;
  107. u32 GetPhysicalAddress() const {
  108. return DecodeAddressRegister(address);
  109. }
  110. // texture1 and texture2 store the texture format directly after the address
  111. // whereas texture0 inserts some additional flags inbetween.
  112. // Hence, we store the format separately so that all other parameters can be described
  113. // in a single structure.
  114. };
  115. enum class TextureFormat : u32 {
  116. RGBA8 = 0,
  117. RGB8 = 1,
  118. RGBA5551 = 2,
  119. RGB565 = 3,
  120. RGBA4 = 4,
  121. IA8 = 5,
  122. I8 = 7,
  123. A8 = 8,
  124. IA4 = 9,
  125. A4 = 11,
  126. ETC1 = 12, // compressed
  127. ETC1A4 = 13, // compressed
  128. };
  129. static unsigned NibblesPerPixel(TextureFormat format) {
  130. switch (format) {
  131. case TextureFormat::RGBA8:
  132. return 8;
  133. case TextureFormat::RGB8:
  134. return 6;
  135. case TextureFormat::RGBA5551:
  136. case TextureFormat::RGB565:
  137. case TextureFormat::RGBA4:
  138. case TextureFormat::IA8:
  139. return 4;
  140. case TextureFormat::A4:
  141. return 1;
  142. case TextureFormat::I8:
  143. case TextureFormat::A8:
  144. case TextureFormat::IA4:
  145. default: // placeholder for yet unknown formats
  146. return 2;
  147. }
  148. }
  149. union {
  150. BitField< 0, 1, u32> texture0_enable;
  151. BitField< 1, 1, u32> texture1_enable;
  152. BitField< 2, 1, u32> texture2_enable;
  153. };
  154. TextureConfig texture0;
  155. INSERT_PADDING_WORDS(0x8);
  156. BitField<0, 4, TextureFormat> texture0_format;
  157. INSERT_PADDING_WORDS(0x2);
  158. TextureConfig texture1;
  159. BitField<0, 4, TextureFormat> texture1_format;
  160. INSERT_PADDING_WORDS(0x2);
  161. TextureConfig texture2;
  162. BitField<0, 4, TextureFormat> texture2_format;
  163. INSERT_PADDING_WORDS(0x21);
  164. struct FullTextureConfig {
  165. const bool enabled;
  166. const TextureConfig config;
  167. const TextureFormat format;
  168. };
  169. const std::array<FullTextureConfig, 3> GetTextures() const {
  170. return {{
  171. { texture0_enable.ToBool(), texture0, texture0_format },
  172. { texture1_enable.ToBool(), texture1, texture1_format },
  173. { texture2_enable.ToBool(), texture2, texture2_format }
  174. }};
  175. }
  176. // 0xc0-0xff: Texture Combiner (akin to glTexEnv)
  177. struct TevStageConfig {
  178. enum class Source : u32 {
  179. PrimaryColor = 0x0,
  180. Texture0 = 0x3,
  181. Texture1 = 0x4,
  182. Texture2 = 0x5,
  183. Texture3 = 0x6,
  184. // 0x7-0xc = primary color??
  185. Constant = 0xe,
  186. Previous = 0xf,
  187. };
  188. enum class ColorModifier : u32 {
  189. SourceColor = 0x0,
  190. OneMinusSourceColor = 0x1,
  191. SourceAlpha = 0x2,
  192. OneMinusSourceAlpha = 0x3,
  193. SourceRed = 0x4,
  194. OneMinusSourceRed = 0x5,
  195. SourceGreen = 0x8,
  196. OneMinusSourceGreen = 0x9,
  197. SourceBlue = 0xc,
  198. OneMinusSourceBlue = 0xd,
  199. };
  200. enum class AlphaModifier : u32 {
  201. SourceAlpha = 0x0,
  202. OneMinusSourceAlpha = 0x1,
  203. SourceRed = 0x2,
  204. OneMinusSourceRed = 0x3,
  205. SourceGreen = 0x4,
  206. OneMinusSourceGreen = 0x5,
  207. SourceBlue = 0x6,
  208. OneMinusSourceBlue = 0x7,
  209. };
  210. enum class Operation : u32 {
  211. Replace = 0,
  212. Modulate = 1,
  213. Add = 2,
  214. AddSigned = 3,
  215. Lerp = 4,
  216. Subtract = 5,
  217. };
  218. union {
  219. BitField< 0, 4, Source> color_source1;
  220. BitField< 4, 4, Source> color_source2;
  221. BitField< 8, 4, Source> color_source3;
  222. BitField<16, 4, Source> alpha_source1;
  223. BitField<20, 4, Source> alpha_source2;
  224. BitField<24, 4, Source> alpha_source3;
  225. };
  226. union {
  227. BitField< 0, 4, ColorModifier> color_modifier1;
  228. BitField< 4, 4, ColorModifier> color_modifier2;
  229. BitField< 8, 4, ColorModifier> color_modifier3;
  230. BitField<12, 3, AlphaModifier> alpha_modifier1;
  231. BitField<16, 3, AlphaModifier> alpha_modifier2;
  232. BitField<20, 3, AlphaModifier> alpha_modifier3;
  233. };
  234. union {
  235. BitField< 0, 4, Operation> color_op;
  236. BitField<16, 4, Operation> alpha_op;
  237. };
  238. union {
  239. BitField< 0, 8, u32> const_r;
  240. BitField< 8, 8, u32> const_g;
  241. BitField<16, 8, u32> const_b;
  242. BitField<24, 8, u32> const_a;
  243. };
  244. INSERT_PADDING_WORDS(0x1);
  245. };
  246. TevStageConfig tev_stage0;
  247. INSERT_PADDING_WORDS(0x3);
  248. TevStageConfig tev_stage1;
  249. INSERT_PADDING_WORDS(0x3);
  250. TevStageConfig tev_stage2;
  251. INSERT_PADDING_WORDS(0x3);
  252. TevStageConfig tev_stage3;
  253. INSERT_PADDING_WORDS(0x13);
  254. TevStageConfig tev_stage4;
  255. INSERT_PADDING_WORDS(0x3);
  256. TevStageConfig tev_stage5;
  257. INSERT_PADDING_WORDS(0x3);
  258. const std::array<Regs::TevStageConfig,6> GetTevStages() const {
  259. return { tev_stage0, tev_stage1,
  260. tev_stage2, tev_stage3,
  261. tev_stage4, tev_stage5 };
  262. };
  263. struct {
  264. enum CompareFunc : u32 {
  265. Never = 0,
  266. Always = 1,
  267. Equal = 2,
  268. NotEqual = 3,
  269. LessThan = 4,
  270. LessThanOrEqual = 5,
  271. GreaterThan = 6,
  272. GreaterThanOrEqual = 7,
  273. };
  274. union {
  275. // If false, logic blending is used
  276. BitField<8, 1, u32> alphablend_enable;
  277. };
  278. union {
  279. enum BlendEquation : u32 {
  280. Add = 0,
  281. Subtract = 1,
  282. ReverseSubtract = 2,
  283. Min = 3,
  284. Max = 4
  285. };
  286. enum BlendFactor : u32 {
  287. Zero = 0,
  288. One = 1,
  289. SourceColor = 2,
  290. OneMinusSourceColor = 3,
  291. DestColor = 4,
  292. OneMinusDestColor = 5,
  293. SourceAlpha = 6,
  294. OneMinusSourceAlpha = 7,
  295. DestAlpha = 8,
  296. OneMinusDestAlpha = 9,
  297. ConstantColor = 10,
  298. OneMinusConstantColor = 11,
  299. ConstantAlpha = 12,
  300. OneMinusConstantAlpha = 13,
  301. SourceAlphaSaturate = 14
  302. };
  303. BitField< 0, 8, BlendEquation> blend_equation_rgb;
  304. BitField< 8, 8, BlendEquation> blend_equation_a;
  305. BitField<16, 4, BlendFactor> factor_source_rgb;
  306. BitField<20, 4, BlendFactor> factor_dest_rgb;
  307. BitField<24, 4, BlendFactor> factor_source_a;
  308. BitField<28, 4, BlendFactor> factor_dest_a;
  309. } alpha_blending;
  310. union {
  311. enum Op {
  312. Set = 4,
  313. };
  314. BitField<0, 4, Op> op;
  315. } logic_op;
  316. union {
  317. BitField< 0, 8, u32> r;
  318. BitField< 8, 8, u32> g;
  319. BitField<16, 8, u32> b;
  320. BitField<24, 8, u32> a;
  321. } blend_const;
  322. union {
  323. BitField< 0, 1, u32> enable;
  324. BitField< 4, 3, CompareFunc> func;
  325. BitField< 8, 8, u32> ref;
  326. } alpha_test;
  327. INSERT_PADDING_WORDS(0x2);
  328. union {
  329. BitField< 0, 1, u32> depth_test_enable;
  330. BitField< 4, 3, CompareFunc> depth_test_func;
  331. BitField< 8, 1, u32> red_enable;
  332. BitField< 9, 1, u32> green_enable;
  333. BitField<10, 1, u32> blue_enable;
  334. BitField<11, 1, u32> alpha_enable;
  335. BitField<12, 1, u32> depth_write_enable;
  336. };
  337. INSERT_PADDING_WORDS(0x8);
  338. } output_merger;
  339. struct {
  340. enum ColorFormat : u32 {
  341. RGBA8 = 0,
  342. RGB8 = 1,
  343. RGBA5551 = 2,
  344. RGB565 = 3,
  345. RGBA4 = 4,
  346. };
  347. INSERT_PADDING_WORDS(0x6);
  348. u32 depth_format;
  349. u32 color_format;
  350. INSERT_PADDING_WORDS(0x4);
  351. u32 depth_buffer_address;
  352. u32 color_buffer_address;
  353. union {
  354. // Apparently, the framebuffer width is stored as expected,
  355. // while the height is stored as the actual height minus one.
  356. // Hence, don't access these fields directly but use the accessors
  357. // GetWidth() and GetHeight() instead.
  358. BitField< 0, 11, u32> width;
  359. BitField<12, 10, u32> height;
  360. };
  361. INSERT_PADDING_WORDS(0x1);
  362. inline u32 GetColorBufferPhysicalAddress() const {
  363. return DecodeAddressRegister(color_buffer_address);
  364. }
  365. inline u32 GetDepthBufferPhysicalAddress() const {
  366. return DecodeAddressRegister(depth_buffer_address);
  367. }
  368. inline u32 GetWidth() const {
  369. return width;
  370. }
  371. inline u32 GetHeight() const {
  372. return height + 1;
  373. }
  374. } framebuffer;
  375. INSERT_PADDING_WORDS(0xe0);
  376. struct {
  377. enum class Format : u64 {
  378. BYTE = 0,
  379. UBYTE = 1,
  380. SHORT = 2,
  381. FLOAT = 3,
  382. };
  383. BitField<0, 29, u32> base_address;
  384. u32 GetPhysicalBaseAddress() const {
  385. return DecodeAddressRegister(base_address);
  386. }
  387. // Descriptor for internal vertex attributes
  388. union {
  389. BitField< 0, 2, Format> format0; // size of one element
  390. BitField< 2, 2, u64> size0; // number of elements minus 1
  391. BitField< 4, 2, Format> format1;
  392. BitField< 6, 2, u64> size1;
  393. BitField< 8, 2, Format> format2;
  394. BitField<10, 2, u64> size2;
  395. BitField<12, 2, Format> format3;
  396. BitField<14, 2, u64> size3;
  397. BitField<16, 2, Format> format4;
  398. BitField<18, 2, u64> size4;
  399. BitField<20, 2, Format> format5;
  400. BitField<22, 2, u64> size5;
  401. BitField<24, 2, Format> format6;
  402. BitField<26, 2, u64> size6;
  403. BitField<28, 2, Format> format7;
  404. BitField<30, 2, u64> size7;
  405. BitField<32, 2, Format> format8;
  406. BitField<34, 2, u64> size8;
  407. BitField<36, 2, Format> format9;
  408. BitField<38, 2, u64> size9;
  409. BitField<40, 2, Format> format10;
  410. BitField<42, 2, u64> size10;
  411. BitField<44, 2, Format> format11;
  412. BitField<46, 2, u64> size11;
  413. BitField<48, 12, u64> attribute_mask;
  414. // number of total attributes minus 1
  415. BitField<60, 4, u64> num_extra_attributes;
  416. };
  417. inline Format GetFormat(int n) const {
  418. Format formats[] = {
  419. format0, format1, format2, format3,
  420. format4, format5, format6, format7,
  421. format8, format9, format10, format11
  422. };
  423. return formats[n];
  424. }
  425. inline int GetNumElements(int n) const {
  426. u64 sizes[] = {
  427. size0, size1, size2, size3,
  428. size4, size5, size6, size7,
  429. size8, size9, size10, size11
  430. };
  431. return (int)sizes[n]+1;
  432. }
  433. inline int GetElementSizeInBytes(int n) const {
  434. return (GetFormat(n) == Format::FLOAT) ? 4 :
  435. (GetFormat(n) == Format::SHORT) ? 2 : 1;
  436. }
  437. inline int GetStride(int n) const {
  438. return GetNumElements(n) * GetElementSizeInBytes(n);
  439. }
  440. inline int GetNumTotalAttributes() const {
  441. return (int)num_extra_attributes+1;
  442. }
  443. // Attribute loaders map the source vertex data to input attributes
  444. // This e.g. allows to load different attributes from different memory locations
  445. struct {
  446. // Source attribute data offset from the base address
  447. u32 data_offset;
  448. union {
  449. BitField< 0, 4, u64> comp0;
  450. BitField< 4, 4, u64> comp1;
  451. BitField< 8, 4, u64> comp2;
  452. BitField<12, 4, u64> comp3;
  453. BitField<16, 4, u64> comp4;
  454. BitField<20, 4, u64> comp5;
  455. BitField<24, 4, u64> comp6;
  456. BitField<28, 4, u64> comp7;
  457. BitField<32, 4, u64> comp8;
  458. BitField<36, 4, u64> comp9;
  459. BitField<40, 4, u64> comp10;
  460. BitField<44, 4, u64> comp11;
  461. // bytes for a single vertex in this loader
  462. BitField<48, 8, u64> byte_count;
  463. BitField<60, 4, u64> component_count;
  464. };
  465. inline int GetComponent(int n) const {
  466. u64 components[] = {
  467. comp0, comp1, comp2, comp3,
  468. comp4, comp5, comp6, comp7,
  469. comp8, comp9, comp10, comp11
  470. };
  471. return (int)components[n];
  472. }
  473. } attribute_loaders[12];
  474. } vertex_attributes;
  475. struct {
  476. enum IndexFormat : u32 {
  477. BYTE = 0,
  478. SHORT = 1,
  479. };
  480. union {
  481. BitField<0, 31, u32> offset; // relative to base attribute address
  482. BitField<31, 1, IndexFormat> format;
  483. };
  484. } index_array;
  485. // Number of vertices to render
  486. u32 num_vertices;
  487. INSERT_PADDING_WORDS(0x5);
  488. // These two trigger rendering of triangles
  489. u32 trigger_draw;
  490. u32 trigger_draw_indexed;
  491. INSERT_PADDING_WORDS(0x2e);
  492. enum class TriangleTopology : u32 {
  493. List = 0,
  494. Strip = 1,
  495. Fan = 2,
  496. ListIndexed = 3, // TODO: No idea if this is correct
  497. };
  498. BitField<8, 2, TriangleTopology> triangle_topology;
  499. INSERT_PADDING_WORDS(0x51);
  500. BitField<0, 16, u32> vs_bool_uniforms;
  501. union {
  502. BitField< 0, 8, u32> x;
  503. BitField< 8, 8, u32> y;
  504. BitField<16, 8, u32> z;
  505. BitField<24, 8, u32> w;
  506. } vs_int_uniforms[4];
  507. INSERT_PADDING_WORDS(0x5);
  508. // Offset to shader program entry point (in words)
  509. BitField<0, 16, u32> vs_main_offset;
  510. union {
  511. BitField< 0, 4, u64> attribute0_register;
  512. BitField< 4, 4, u64> attribute1_register;
  513. BitField< 8, 4, u64> attribute2_register;
  514. BitField<12, 4, u64> attribute3_register;
  515. BitField<16, 4, u64> attribute4_register;
  516. BitField<20, 4, u64> attribute5_register;
  517. BitField<24, 4, u64> attribute6_register;
  518. BitField<28, 4, u64> attribute7_register;
  519. BitField<32, 4, u64> attribute8_register;
  520. BitField<36, 4, u64> attribute9_register;
  521. BitField<40, 4, u64> attribute10_register;
  522. BitField<44, 4, u64> attribute11_register;
  523. BitField<48, 4, u64> attribute12_register;
  524. BitField<52, 4, u64> attribute13_register;
  525. BitField<56, 4, u64> attribute14_register;
  526. BitField<60, 4, u64> attribute15_register;
  527. int GetRegisterForAttribute(int attribute_index) {
  528. u64 fields[] = {
  529. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  530. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  531. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  532. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  533. };
  534. return (int)fields[attribute_index];
  535. }
  536. } vs_input_register_map;
  537. INSERT_PADDING_WORDS(0x3);
  538. struct {
  539. enum Format : u32
  540. {
  541. FLOAT24 = 0,
  542. FLOAT32 = 1
  543. };
  544. bool IsFloat32() const {
  545. return format == FLOAT32;
  546. }
  547. union {
  548. // Index of the next uniform to write to
  549. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  550. BitField<0, 7, u32> index;
  551. BitField<31, 1, Format> format;
  552. };
  553. // Writing to these registers sets the "current" uniform.
  554. // TODO: It's not clear how the hardware stores what the "current" uniform is.
  555. u32 set_value[8];
  556. } vs_uniform_setup;
  557. INSERT_PADDING_WORDS(0x2);
  558. struct {
  559. u32 begin_load;
  560. // Writing to these registers sets the "current" word in the shader program.
  561. // TODO: It's not clear how the hardware stores what the "current" word is.
  562. u32 set_word[8];
  563. } vs_program;
  564. INSERT_PADDING_WORDS(0x1);
  565. // This register group is used to load an internal table of swizzling patterns,
  566. // which are indexed by each shader instruction to specify vector component swizzling.
  567. struct {
  568. u32 begin_load;
  569. // Writing to these registers sets the "current" swizzle pattern in the table.
  570. // TODO: It's not clear how the hardware stores what the "current" swizzle pattern is.
  571. u32 set_word[8];
  572. } vs_swizzle_patterns;
  573. INSERT_PADDING_WORDS(0x22);
  574. #undef INSERT_PADDING_WORDS_HELPER1
  575. #undef INSERT_PADDING_WORDS_HELPER2
  576. #undef INSERT_PADDING_WORDS
  577. // Map register indices to names readable by humans
  578. // Used for debugging purposes, so performance is not an issue here
  579. static std::string GetCommandName(int index) {
  580. std::map<u32, std::string> map;
  581. #define ADD_FIELD(name) \
  582. do { \
  583. map.insert({PICA_REG_INDEX(name), #name}); \
  584. /* TODO: change to Regs::name when VS2015 and other compilers support it */ \
  585. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(Regs().name) / 4; ++i) \
  586. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  587. } while(false)
  588. ADD_FIELD(trigger_irq);
  589. ADD_FIELD(cull_mode);
  590. ADD_FIELD(viewport_size_x);
  591. ADD_FIELD(viewport_size_y);
  592. ADD_FIELD(viewport_depth_range);
  593. ADD_FIELD(viewport_depth_far_plane);
  594. ADD_FIELD(viewport_corner);
  595. ADD_FIELD(texture0_enable);
  596. ADD_FIELD(texture0);
  597. ADD_FIELD(texture0_format);
  598. ADD_FIELD(texture1);
  599. ADD_FIELD(texture1_format);
  600. ADD_FIELD(texture2);
  601. ADD_FIELD(texture2_format);
  602. ADD_FIELD(tev_stage0);
  603. ADD_FIELD(tev_stage1);
  604. ADD_FIELD(tev_stage2);
  605. ADD_FIELD(tev_stage3);
  606. ADD_FIELD(tev_stage4);
  607. ADD_FIELD(tev_stage5);
  608. ADD_FIELD(output_merger);
  609. ADD_FIELD(framebuffer);
  610. ADD_FIELD(vertex_attributes);
  611. ADD_FIELD(index_array);
  612. ADD_FIELD(num_vertices);
  613. ADD_FIELD(trigger_draw);
  614. ADD_FIELD(trigger_draw_indexed);
  615. ADD_FIELD(triangle_topology);
  616. ADD_FIELD(vs_bool_uniforms);
  617. ADD_FIELD(vs_int_uniforms);
  618. ADD_FIELD(vs_main_offset);
  619. ADD_FIELD(vs_input_register_map);
  620. ADD_FIELD(vs_uniform_setup);
  621. ADD_FIELD(vs_program);
  622. ADD_FIELD(vs_swizzle_patterns);
  623. #undef ADD_FIELD
  624. // Return empty string if no match is found
  625. return map[index];
  626. }
  627. static inline size_t NumIds() {
  628. return sizeof(Regs) / sizeof(u32);
  629. }
  630. u32& operator [] (int index) const {
  631. u32* content = (u32*)this;
  632. return content[index];
  633. }
  634. u32& operator [] (int index) {
  635. u32* content = (u32*)this;
  636. return content[index];
  637. }
  638. private:
  639. /*
  640. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  641. * This function should be used to get the address from a raw register value.
  642. */
  643. static inline u32 DecodeAddressRegister(u32 register_value) {
  644. return register_value * 8;
  645. }
  646. };
  647. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  648. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  649. // support for that.
  650. #ifndef _MSC_VER
  651. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  652. ASSERT_REG_POSITION(trigger_irq, 0x10);
  653. ASSERT_REG_POSITION(cull_mode, 0x40);
  654. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  655. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  656. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  657. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  658. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  659. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  660. ASSERT_REG_POSITION(viewport_corner, 0x68);
  661. ASSERT_REG_POSITION(texture0_enable, 0x80);
  662. ASSERT_REG_POSITION(texture0, 0x81);
  663. ASSERT_REG_POSITION(texture0_format, 0x8e);
  664. ASSERT_REG_POSITION(texture1, 0x91);
  665. ASSERT_REG_POSITION(texture1_format, 0x96);
  666. ASSERT_REG_POSITION(texture2, 0x99);
  667. ASSERT_REG_POSITION(texture2_format, 0x9e);
  668. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  669. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  670. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  671. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  672. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  673. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  674. ASSERT_REG_POSITION(output_merger, 0x100);
  675. ASSERT_REG_POSITION(framebuffer, 0x110);
  676. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  677. ASSERT_REG_POSITION(index_array, 0x227);
  678. ASSERT_REG_POSITION(num_vertices, 0x228);
  679. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  680. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  681. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  682. ASSERT_REG_POSITION(vs_bool_uniforms, 0x2b0);
  683. ASSERT_REG_POSITION(vs_int_uniforms, 0x2b1);
  684. ASSERT_REG_POSITION(vs_main_offset, 0x2ba);
  685. ASSERT_REG_POSITION(vs_input_register_map, 0x2bb);
  686. ASSERT_REG_POSITION(vs_uniform_setup, 0x2c0);
  687. ASSERT_REG_POSITION(vs_program, 0x2cb);
  688. ASSERT_REG_POSITION(vs_swizzle_patterns, 0x2d5);
  689. #undef ASSERT_REG_POSITION
  690. #endif // !defined(_MSC_VER)
  691. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  692. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  693. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  694. extern Regs registers; // TODO: Not sure if we want to have one global instance for this
  695. struct float24 {
  696. static float24 FromFloat32(float val) {
  697. float24 ret;
  698. ret.value = val;
  699. return ret;
  700. }
  701. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  702. // TODO: No idea if this works as intended
  703. static float24 FromRawFloat24(u32 hex) {
  704. float24 ret;
  705. if ((hex & 0xFFFFFF) == 0) {
  706. ret.value = 0;
  707. } else {
  708. u32 mantissa = hex & 0xFFFF;
  709. u32 exponent = (hex >> 16) & 0x7F;
  710. u32 sign = hex >> 23;
  711. ret.value = powf(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * powf(2.0f, -16.f));
  712. if (sign)
  713. ret.value = -ret.value;
  714. }
  715. return ret;
  716. }
  717. // Not recommended for anything but logging
  718. float ToFloat32() const {
  719. return value;
  720. }
  721. float24 operator * (const float24& flt) const {
  722. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  723. }
  724. float24 operator / (const float24& flt) const {
  725. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  726. }
  727. float24 operator + (const float24& flt) const {
  728. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  729. }
  730. float24 operator - (const float24& flt) const {
  731. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  732. }
  733. float24& operator *= (const float24& flt) {
  734. value *= flt.ToFloat32();
  735. return *this;
  736. }
  737. float24& operator /= (const float24& flt) {
  738. value /= flt.ToFloat32();
  739. return *this;
  740. }
  741. float24& operator += (const float24& flt) {
  742. value += flt.ToFloat32();
  743. return *this;
  744. }
  745. float24& operator -= (const float24& flt) {
  746. value -= flt.ToFloat32();
  747. return *this;
  748. }
  749. float24 operator - () const {
  750. return float24::FromFloat32(-ToFloat32());
  751. }
  752. bool operator < (const float24& flt) const {
  753. return ToFloat32() < flt.ToFloat32();
  754. }
  755. bool operator > (const float24& flt) const {
  756. return ToFloat32() > flt.ToFloat32();
  757. }
  758. bool operator >= (const float24& flt) const {
  759. return ToFloat32() >= flt.ToFloat32();
  760. }
  761. bool operator <= (const float24& flt) const {
  762. return ToFloat32() <= flt.ToFloat32();
  763. }
  764. bool operator == (const float24& flt) const {
  765. return ToFloat32() == flt.ToFloat32();
  766. }
  767. bool operator != (const float24& flt) const {
  768. return ToFloat32() != flt.ToFloat32();
  769. }
  770. private:
  771. // Stored as a regular float, merely for convenience
  772. // TODO: Perform proper arithmetic on this!
  773. float value;
  774. };
  775. union CommandHeader {
  776. CommandHeader(u32 h) : hex(h) {}
  777. u32 hex;
  778. BitField< 0, 16, u32> cmd_id;
  779. BitField<16, 4, u32> parameter_mask;
  780. BitField<20, 11, u32> extra_data_length;
  781. BitField<31, 1, u32> group_commands;
  782. };
  783. // TODO: Ugly, should fix PhysicalToVirtualAddress instead
  784. inline static u32 PAddrToVAddr(u32 addr) {
  785. if (addr >= Memory::VRAM_PADDR && addr < Memory::VRAM_PADDR + Memory::VRAM_SIZE) {
  786. return addr - Memory::VRAM_PADDR + Memory::VRAM_VADDR;
  787. } else if (addr >= Memory::FCRAM_PADDR && addr < Memory::FCRAM_PADDR + Memory::FCRAM_SIZE) {
  788. return addr - Memory::FCRAM_PADDR + Memory::HEAP_LINEAR_VADDR;
  789. } else {
  790. return 0;
  791. }
  792. }
  793. } // namespace