pica.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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 <cmath>
  7. #include <cstddef>
  8. #include <map>
  9. #include <string>
  10. #include "common/assert.h"
  11. #include "common/bit_field.h"
  12. #include "common/common_funcs.h"
  13. #include "common/common_types.h"
  14. #include "common/logging/log.h"
  15. #include "common/vector_math.h"
  16. namespace Pica {
  17. // Returns index corresponding to the Regs member labeled by field_name
  18. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  19. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  20. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  21. // Hopefully, this will be fixed sometime in the future.
  22. // For lack of better alternatives, we currently hardcode the offsets when constant
  23. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  24. // will then make sure the offsets indeed match the automatically calculated ones).
  25. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  26. #if defined(_MSC_VER)
  27. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  28. #else
  29. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  30. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  31. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  32. // field offset. Otherwise, the compiler will fail to compile this code.
  33. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  34. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  35. #endif // _MSC_VER
  36. struct Regs {
  37. INSERT_PADDING_WORDS(0x10);
  38. u32 trigger_irq;
  39. INSERT_PADDING_WORDS(0x2f);
  40. enum class CullMode : u32 {
  41. // Select which polygons are considered to be "frontfacing".
  42. KeepAll = 0,
  43. KeepClockWise = 1,
  44. KeepCounterClockWise = 2,
  45. // TODO: What does the third value imply?
  46. };
  47. union {
  48. BitField<0, 2, CullMode> cull_mode;
  49. };
  50. BitField<0, 24, u32> viewport_size_x;
  51. INSERT_PADDING_WORDS(0x1);
  52. BitField<0, 24, u32> viewport_size_y;
  53. INSERT_PADDING_WORDS(0x9);
  54. BitField<0, 24, u32> viewport_depth_range; // float24
  55. BitField<0, 24, u32> viewport_depth_far_plane; // float24
  56. INSERT_PADDING_WORDS(0x1);
  57. union VSOutputAttributes {
  58. // Maps components of output vertex attributes to semantics
  59. enum Semantic : u32
  60. {
  61. POSITION_X = 0,
  62. POSITION_Y = 1,
  63. POSITION_Z = 2,
  64. POSITION_W = 3,
  65. COLOR_R = 8,
  66. COLOR_G = 9,
  67. COLOR_B = 10,
  68. COLOR_A = 11,
  69. TEXCOORD0_U = 12,
  70. TEXCOORD0_V = 13,
  71. TEXCOORD1_U = 14,
  72. TEXCOORD1_V = 15,
  73. TEXCOORD2_U = 22,
  74. TEXCOORD2_V = 23,
  75. INVALID = 31,
  76. };
  77. BitField< 0, 5, Semantic> map_x;
  78. BitField< 8, 5, Semantic> map_y;
  79. BitField<16, 5, Semantic> map_z;
  80. BitField<24, 5, Semantic> map_w;
  81. } vs_output_attributes[7];
  82. INSERT_PADDING_WORDS(0x11);
  83. union {
  84. BitField< 0, 16, u32> x;
  85. BitField<16, 16, u32> y;
  86. } viewport_corner;
  87. INSERT_PADDING_WORDS(0x17);
  88. struct TextureConfig {
  89. enum WrapMode : u32 {
  90. ClampToEdge = 0,
  91. ClampToBorder = 1,
  92. Repeat = 2,
  93. MirroredRepeat = 3,
  94. };
  95. enum TextureFilter : u32 {
  96. Nearest = 0,
  97. Linear = 1
  98. };
  99. union {
  100. BitField< 0, 8, u32> r;
  101. BitField< 8, 8, u32> g;
  102. BitField<16, 8, u32> b;
  103. BitField<24, 8, u32> a;
  104. } border_color;
  105. union {
  106. BitField< 0, 16, u32> height;
  107. BitField<16, 16, u32> width;
  108. };
  109. union {
  110. BitField< 1, 1, TextureFilter> mag_filter;
  111. BitField< 2, 1, TextureFilter> min_filter;
  112. BitField< 8, 2, WrapMode> wrap_t;
  113. BitField<12, 2, WrapMode> wrap_s;
  114. };
  115. INSERT_PADDING_WORDS(0x1);
  116. u32 address;
  117. u32 GetPhysicalAddress() const {
  118. return DecodeAddressRegister(address);
  119. }
  120. // texture1 and texture2 store the texture format directly after the address
  121. // whereas texture0 inserts some additional flags inbetween.
  122. // Hence, we store the format separately so that all other parameters can be described
  123. // in a single structure.
  124. };
  125. enum class TextureFormat : u32 {
  126. RGBA8 = 0,
  127. RGB8 = 1,
  128. RGB5A1 = 2,
  129. RGB565 = 3,
  130. RGBA4 = 4,
  131. IA8 = 5,
  132. I8 = 7,
  133. A8 = 8,
  134. IA4 = 9,
  135. I4 = 10,
  136. A4 = 11,
  137. ETC1 = 12, // compressed
  138. ETC1A4 = 13, // compressed
  139. };
  140. enum class LogicOp : u32 {
  141. Clear = 0,
  142. And = 1,
  143. AndReverse = 2,
  144. Copy = 3,
  145. Set = 4,
  146. CopyInverted = 5,
  147. NoOp = 6,
  148. Invert = 7,
  149. Nand = 8,
  150. Or = 9,
  151. Nor = 10,
  152. Xor = 11,
  153. Equiv = 12,
  154. AndInverted = 13,
  155. OrReverse = 14,
  156. OrInverted = 15,
  157. };
  158. static unsigned NibblesPerPixel(TextureFormat format) {
  159. switch (format) {
  160. case TextureFormat::RGBA8:
  161. return 8;
  162. case TextureFormat::RGB8:
  163. return 6;
  164. case TextureFormat::RGB5A1:
  165. case TextureFormat::RGB565:
  166. case TextureFormat::RGBA4:
  167. case TextureFormat::IA8:
  168. return 4;
  169. case TextureFormat::I4:
  170. case TextureFormat::A4:
  171. return 1;
  172. case TextureFormat::I8:
  173. case TextureFormat::A8:
  174. case TextureFormat::IA4:
  175. default: // placeholder for yet unknown formats
  176. return 2;
  177. }
  178. }
  179. union {
  180. BitField< 0, 1, u32> texture0_enable;
  181. BitField< 1, 1, u32> texture1_enable;
  182. BitField< 2, 1, u32> texture2_enable;
  183. };
  184. TextureConfig texture0;
  185. INSERT_PADDING_WORDS(0x8);
  186. BitField<0, 4, TextureFormat> texture0_format;
  187. INSERT_PADDING_WORDS(0x2);
  188. TextureConfig texture1;
  189. BitField<0, 4, TextureFormat> texture1_format;
  190. INSERT_PADDING_WORDS(0x2);
  191. TextureConfig texture2;
  192. BitField<0, 4, TextureFormat> texture2_format;
  193. INSERT_PADDING_WORDS(0x21);
  194. struct FullTextureConfig {
  195. const bool enabled;
  196. const TextureConfig config;
  197. const TextureFormat format;
  198. };
  199. const std::array<FullTextureConfig, 3> GetTextures() const {
  200. return {{
  201. { texture0_enable.ToBool(), texture0, texture0_format },
  202. { texture1_enable.ToBool(), texture1, texture1_format },
  203. { texture2_enable.ToBool(), texture2, texture2_format }
  204. }};
  205. }
  206. // 0xc0-0xff: Texture Combiner (akin to glTexEnv)
  207. struct TevStageConfig {
  208. enum class Source : u32 {
  209. PrimaryColor = 0x0,
  210. PrimaryFragmentColor = 0x1,
  211. SecondaryFragmentColor = 0x2,
  212. Texture0 = 0x3,
  213. Texture1 = 0x4,
  214. Texture2 = 0x5,
  215. Texture3 = 0x6,
  216. PreviousBuffer = 0xd,
  217. Constant = 0xe,
  218. Previous = 0xf,
  219. };
  220. enum class ColorModifier : u32 {
  221. SourceColor = 0x0,
  222. OneMinusSourceColor = 0x1,
  223. SourceAlpha = 0x2,
  224. OneMinusSourceAlpha = 0x3,
  225. SourceRed = 0x4,
  226. OneMinusSourceRed = 0x5,
  227. SourceGreen = 0x8,
  228. OneMinusSourceGreen = 0x9,
  229. SourceBlue = 0xc,
  230. OneMinusSourceBlue = 0xd,
  231. };
  232. enum class AlphaModifier : u32 {
  233. SourceAlpha = 0x0,
  234. OneMinusSourceAlpha = 0x1,
  235. SourceRed = 0x2,
  236. OneMinusSourceRed = 0x3,
  237. SourceGreen = 0x4,
  238. OneMinusSourceGreen = 0x5,
  239. SourceBlue = 0x6,
  240. OneMinusSourceBlue = 0x7,
  241. };
  242. enum class Operation : u32 {
  243. Replace = 0,
  244. Modulate = 1,
  245. Add = 2,
  246. AddSigned = 3,
  247. Lerp = 4,
  248. Subtract = 5,
  249. Dot3_RGB = 6,
  250. MultiplyThenAdd = 8,
  251. AddThenMultiply = 9,
  252. };
  253. union {
  254. BitField< 0, 4, Source> color_source1;
  255. BitField< 4, 4, Source> color_source2;
  256. BitField< 8, 4, Source> color_source3;
  257. BitField<16, 4, Source> alpha_source1;
  258. BitField<20, 4, Source> alpha_source2;
  259. BitField<24, 4, Source> alpha_source3;
  260. };
  261. union {
  262. BitField< 0, 4, ColorModifier> color_modifier1;
  263. BitField< 4, 4, ColorModifier> color_modifier2;
  264. BitField< 8, 4, ColorModifier> color_modifier3;
  265. BitField<12, 3, AlphaModifier> alpha_modifier1;
  266. BitField<16, 3, AlphaModifier> alpha_modifier2;
  267. BitField<20, 3, AlphaModifier> alpha_modifier3;
  268. };
  269. union {
  270. BitField< 0, 4, Operation> color_op;
  271. BitField<16, 4, Operation> alpha_op;
  272. };
  273. union {
  274. BitField< 0, 8, u32> const_r;
  275. BitField< 8, 8, u32> const_g;
  276. BitField<16, 8, u32> const_b;
  277. BitField<24, 8, u32> const_a;
  278. };
  279. union {
  280. BitField< 0, 2, u32> color_scale;
  281. BitField<16, 2, u32> alpha_scale;
  282. };
  283. inline unsigned GetColorMultiplier() const {
  284. return (color_scale < 3) ? (1 << color_scale) : 1;
  285. }
  286. inline unsigned GetAlphaMultiplier() const {
  287. return (alpha_scale < 3) ? (1 << alpha_scale) : 1;
  288. }
  289. };
  290. TevStageConfig tev_stage0;
  291. INSERT_PADDING_WORDS(0x3);
  292. TevStageConfig tev_stage1;
  293. INSERT_PADDING_WORDS(0x3);
  294. TevStageConfig tev_stage2;
  295. INSERT_PADDING_WORDS(0x3);
  296. TevStageConfig tev_stage3;
  297. INSERT_PADDING_WORDS(0x3);
  298. union {
  299. // Tev stages 0-3 write their output to the combiner buffer if the corresponding bit in
  300. // these masks are set
  301. BitField< 8, 4, u32> update_mask_rgb;
  302. BitField<12, 4, u32> update_mask_a;
  303. bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
  304. return (stage_index < 4) && (update_mask_rgb & (1 << stage_index));
  305. }
  306. bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
  307. return (stage_index < 4) && (update_mask_a & (1 << stage_index));
  308. }
  309. } tev_combiner_buffer_input;
  310. INSERT_PADDING_WORDS(0xf);
  311. TevStageConfig tev_stage4;
  312. INSERT_PADDING_WORDS(0x3);
  313. TevStageConfig tev_stage5;
  314. union {
  315. BitField< 0, 8, u32> r;
  316. BitField< 8, 8, u32> g;
  317. BitField<16, 8, u32> b;
  318. BitField<24, 8, u32> a;
  319. } tev_combiner_buffer_color;
  320. INSERT_PADDING_WORDS(0x2);
  321. const std::array<Regs::TevStageConfig,6> GetTevStages() const {
  322. return {{ tev_stage0, tev_stage1,
  323. tev_stage2, tev_stage3,
  324. tev_stage4, tev_stage5 }};
  325. };
  326. enum class BlendEquation : u32 {
  327. Add = 0,
  328. Subtract = 1,
  329. ReverseSubtract = 2,
  330. Min = 3,
  331. Max = 4,
  332. };
  333. enum class BlendFactor : u32 {
  334. Zero = 0,
  335. One = 1,
  336. SourceColor = 2,
  337. OneMinusSourceColor = 3,
  338. DestColor = 4,
  339. OneMinusDestColor = 5,
  340. SourceAlpha = 6,
  341. OneMinusSourceAlpha = 7,
  342. DestAlpha = 8,
  343. OneMinusDestAlpha = 9,
  344. ConstantColor = 10,
  345. OneMinusConstantColor = 11,
  346. ConstantAlpha = 12,
  347. OneMinusConstantAlpha = 13,
  348. SourceAlphaSaturate = 14,
  349. };
  350. enum class CompareFunc : u32 {
  351. Never = 0,
  352. Always = 1,
  353. Equal = 2,
  354. NotEqual = 3,
  355. LessThan = 4,
  356. LessThanOrEqual = 5,
  357. GreaterThan = 6,
  358. GreaterThanOrEqual = 7,
  359. };
  360. enum class StencilAction : u32 {
  361. Keep = 0,
  362. Xor = 5,
  363. };
  364. struct {
  365. union {
  366. // If false, logic blending is used
  367. BitField<8, 1, u32> alphablend_enable;
  368. };
  369. union {
  370. BitField< 0, 8, BlendEquation> blend_equation_rgb;
  371. BitField< 8, 8, BlendEquation> blend_equation_a;
  372. BitField<16, 4, BlendFactor> factor_source_rgb;
  373. BitField<20, 4, BlendFactor> factor_dest_rgb;
  374. BitField<24, 4, BlendFactor> factor_source_a;
  375. BitField<28, 4, BlendFactor> factor_dest_a;
  376. } alpha_blending;
  377. union {
  378. BitField<0, 4, LogicOp> logic_op;
  379. };
  380. union {
  381. BitField< 0, 8, u32> r;
  382. BitField< 8, 8, u32> g;
  383. BitField<16, 8, u32> b;
  384. BitField<24, 8, u32> a;
  385. } blend_const;
  386. union {
  387. BitField< 0, 1, u32> enable;
  388. BitField< 4, 3, CompareFunc> func;
  389. BitField< 8, 8, u32> ref;
  390. } alpha_test;
  391. struct {
  392. union {
  393. // If true, enable stencil testing
  394. BitField< 0, 1, u32> enable;
  395. // Comparison operation for stencil testing
  396. BitField< 4, 3, CompareFunc> func;
  397. // Value to calculate the new stencil value from
  398. BitField< 8, 8, u32> replacement_value;
  399. // Value to compare against for stencil testing
  400. BitField<16, 8, u32> reference_value;
  401. // Mask to apply on stencil test inputs
  402. BitField<24, 8, u32> mask;
  403. };
  404. union {
  405. // Action to perform when the stencil test fails
  406. BitField< 0, 3, StencilAction> action_stencil_fail;
  407. // Action to perform when stencil testing passed but depth testing fails
  408. BitField< 4, 3, StencilAction> action_depth_fail;
  409. // Action to perform when both stencil and depth testing pass
  410. BitField< 8, 3, StencilAction> action_depth_pass;
  411. };
  412. } stencil_test;
  413. union {
  414. BitField< 0, 1, u32> depth_test_enable;
  415. BitField< 4, 3, CompareFunc> depth_test_func;
  416. BitField< 8, 1, u32> red_enable;
  417. BitField< 9, 1, u32> green_enable;
  418. BitField<10, 1, u32> blue_enable;
  419. BitField<11, 1, u32> alpha_enable;
  420. BitField<12, 1, u32> depth_write_enable;
  421. };
  422. INSERT_PADDING_WORDS(0x8);
  423. } output_merger;
  424. // Components are laid out in reverse byte order, most significant bits first.
  425. enum class ColorFormat : u32 {
  426. RGBA8 = 0,
  427. RGB8 = 1,
  428. RGB5A1 = 2,
  429. RGB565 = 3,
  430. RGBA4 = 4,
  431. };
  432. enum class DepthFormat : u32 {
  433. D16 = 0,
  434. D24 = 2,
  435. D24S8 = 3,
  436. };
  437. // Returns the number of bytes in the specified color format
  438. static unsigned BytesPerColorPixel(ColorFormat format) {
  439. switch (format) {
  440. case ColorFormat::RGBA8:
  441. return 4;
  442. case ColorFormat::RGB8:
  443. return 3;
  444. case ColorFormat::RGB5A1:
  445. case ColorFormat::RGB565:
  446. case ColorFormat::RGBA4:
  447. return 2;
  448. default:
  449. LOG_CRITICAL(HW_GPU, "Unknown color format %u", format);
  450. UNIMPLEMENTED();
  451. }
  452. }
  453. struct {
  454. INSERT_PADDING_WORDS(0x6);
  455. DepthFormat depth_format; // TODO: Should be a BitField!
  456. BitField<16, 3, ColorFormat> color_format;
  457. INSERT_PADDING_WORDS(0x4);
  458. u32 depth_buffer_address;
  459. u32 color_buffer_address;
  460. union {
  461. // Apparently, the framebuffer width is stored as expected,
  462. // while the height is stored as the actual height minus one.
  463. // Hence, don't access these fields directly but use the accessors
  464. // GetWidth() and GetHeight() instead.
  465. BitField< 0, 11, u32> width;
  466. BitField<12, 10, u32> height;
  467. };
  468. INSERT_PADDING_WORDS(0x1);
  469. inline u32 GetColorBufferPhysicalAddress() const {
  470. return DecodeAddressRegister(color_buffer_address);
  471. }
  472. inline u32 GetDepthBufferPhysicalAddress() const {
  473. return DecodeAddressRegister(depth_buffer_address);
  474. }
  475. inline u32 GetWidth() const {
  476. return width;
  477. }
  478. inline u32 GetHeight() const {
  479. return height + 1;
  480. }
  481. } framebuffer;
  482. // Returns the number of bytes in the specified depth format
  483. static u32 BytesPerDepthPixel(DepthFormat format) {
  484. switch (format) {
  485. case DepthFormat::D16:
  486. return 2;
  487. case DepthFormat::D24:
  488. return 3;
  489. case DepthFormat::D24S8:
  490. return 4;
  491. default:
  492. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  493. UNIMPLEMENTED();
  494. }
  495. }
  496. // Returns the number of bits per depth component of the specified depth format
  497. static u32 DepthBitsPerPixel(DepthFormat format) {
  498. switch (format) {
  499. case DepthFormat::D16:
  500. return 16;
  501. case DepthFormat::D24:
  502. case DepthFormat::D24S8:
  503. return 24;
  504. default:
  505. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  506. UNIMPLEMENTED();
  507. }
  508. }
  509. INSERT_PADDING_WORDS(0xe0);
  510. enum class VertexAttributeFormat : u64 {
  511. BYTE = 0,
  512. UBYTE = 1,
  513. SHORT = 2,
  514. FLOAT = 3,
  515. };
  516. struct {
  517. BitField<0, 29, u32> base_address;
  518. u32 GetPhysicalBaseAddress() const {
  519. return DecodeAddressRegister(base_address);
  520. }
  521. // Descriptor for internal vertex attributes
  522. union {
  523. BitField< 0, 2, VertexAttributeFormat> format0; // size of one element
  524. BitField< 2, 2, u64> size0; // number of elements minus 1
  525. BitField< 4, 2, VertexAttributeFormat> format1;
  526. BitField< 6, 2, u64> size1;
  527. BitField< 8, 2, VertexAttributeFormat> format2;
  528. BitField<10, 2, u64> size2;
  529. BitField<12, 2, VertexAttributeFormat> format3;
  530. BitField<14, 2, u64> size3;
  531. BitField<16, 2, VertexAttributeFormat> format4;
  532. BitField<18, 2, u64> size4;
  533. BitField<20, 2, VertexAttributeFormat> format5;
  534. BitField<22, 2, u64> size5;
  535. BitField<24, 2, VertexAttributeFormat> format6;
  536. BitField<26, 2, u64> size6;
  537. BitField<28, 2, VertexAttributeFormat> format7;
  538. BitField<30, 2, u64> size7;
  539. BitField<32, 2, VertexAttributeFormat> format8;
  540. BitField<34, 2, u64> size8;
  541. BitField<36, 2, VertexAttributeFormat> format9;
  542. BitField<38, 2, u64> size9;
  543. BitField<40, 2, VertexAttributeFormat> format10;
  544. BitField<42, 2, u64> size10;
  545. BitField<44, 2, VertexAttributeFormat> format11;
  546. BitField<46, 2, u64> size11;
  547. BitField<48, 12, u64> attribute_mask;
  548. // number of total attributes minus 1
  549. BitField<60, 4, u64> num_extra_attributes;
  550. };
  551. inline VertexAttributeFormat GetFormat(int n) const {
  552. VertexAttributeFormat formats[] = {
  553. format0, format1, format2, format3,
  554. format4, format5, format6, format7,
  555. format8, format9, format10, format11
  556. };
  557. return formats[n];
  558. }
  559. inline int GetNumElements(int n) const {
  560. u64 sizes[] = {
  561. size0, size1, size2, size3,
  562. size4, size5, size6, size7,
  563. size8, size9, size10, size11
  564. };
  565. return (int)sizes[n]+1;
  566. }
  567. inline int GetElementSizeInBytes(int n) const {
  568. return (GetFormat(n) == VertexAttributeFormat::FLOAT) ? 4 :
  569. (GetFormat(n) == VertexAttributeFormat::SHORT) ? 2 : 1;
  570. }
  571. inline int GetStride(int n) const {
  572. return GetNumElements(n) * GetElementSizeInBytes(n);
  573. }
  574. inline bool IsDefaultAttribute(int id) const {
  575. return (id >= 12) || (attribute_mask & (1ULL << id)) != 0;
  576. }
  577. inline int GetNumTotalAttributes() const {
  578. return (int)num_extra_attributes+1;
  579. }
  580. // Attribute loaders map the source vertex data to input attributes
  581. // This e.g. allows to load different attributes from different memory locations
  582. struct {
  583. // Source attribute data offset from the base address
  584. u32 data_offset;
  585. union {
  586. BitField< 0, 4, u64> comp0;
  587. BitField< 4, 4, u64> comp1;
  588. BitField< 8, 4, u64> comp2;
  589. BitField<12, 4, u64> comp3;
  590. BitField<16, 4, u64> comp4;
  591. BitField<20, 4, u64> comp5;
  592. BitField<24, 4, u64> comp6;
  593. BitField<28, 4, u64> comp7;
  594. BitField<32, 4, u64> comp8;
  595. BitField<36, 4, u64> comp9;
  596. BitField<40, 4, u64> comp10;
  597. BitField<44, 4, u64> comp11;
  598. // bytes for a single vertex in this loader
  599. BitField<48, 8, u64> byte_count;
  600. BitField<60, 4, u64> component_count;
  601. };
  602. inline int GetComponent(int n) const {
  603. u64 components[] = {
  604. comp0, comp1, comp2, comp3,
  605. comp4, comp5, comp6, comp7,
  606. comp8, comp9, comp10, comp11
  607. };
  608. return (int)components[n];
  609. }
  610. } attribute_loaders[12];
  611. } vertex_attributes;
  612. struct {
  613. enum IndexFormat : u32 {
  614. BYTE = 0,
  615. SHORT = 1,
  616. };
  617. union {
  618. BitField<0, 31, u32> offset; // relative to base attribute address
  619. BitField<31, 1, IndexFormat> format;
  620. };
  621. } index_array;
  622. // Number of vertices to render
  623. u32 num_vertices;
  624. INSERT_PADDING_WORDS(0x5);
  625. // These two trigger rendering of triangles
  626. u32 trigger_draw;
  627. u32 trigger_draw_indexed;
  628. INSERT_PADDING_WORDS(0x2);
  629. // These registers are used to setup the default "fall-back" vertex shader attributes
  630. struct {
  631. // Index of the current default attribute
  632. u32 index;
  633. // Writing to these registers sets the "current" default attribute.
  634. u32 set_value[3];
  635. } vs_default_attributes_setup;
  636. INSERT_PADDING_WORDS(0x2);
  637. struct {
  638. // There are two channels that can be used to configure the next command buffer, which
  639. // can be then executed by writing to the "trigger" registers. There are two reasons why a
  640. // game might use this feature:
  641. // 1) With this, an arbitrary number of additional command buffers may be executed in
  642. // sequence without requiring any intervention of the CPU after the initial one is
  643. // kicked off.
  644. // 2) Games can configure these registers to provide a command list subroutine mechanism.
  645. BitField< 0, 20, u32> size[2]; ///< Size (in bytes / 8) of each channel's command buffer
  646. BitField< 0, 28, u32> addr[2]; ///< Physical address / 8 of each channel's command buffer
  647. u32 trigger[2]; ///< Triggers execution of the channel's command buffer when written to
  648. unsigned GetSize(unsigned index) const {
  649. ASSERT(index < 2);
  650. return 8 * size[index];
  651. }
  652. PAddr GetPhysicalAddress(unsigned index) const {
  653. ASSERT(index < 2);
  654. return (PAddr)(8 * addr[index]);
  655. }
  656. } command_buffer;
  657. INSERT_PADDING_WORDS(0x20);
  658. enum class TriangleTopology : u32 {
  659. List = 0,
  660. Strip = 1,
  661. Fan = 2,
  662. Shader = 3, // Programmable setup unit implemented in a geometry shader
  663. };
  664. BitField<8, 2, TriangleTopology> triangle_topology;
  665. INSERT_PADDING_WORDS(0x21);
  666. struct ShaderConfig {
  667. BitField<0, 16, u32> bool_uniforms;
  668. union {
  669. BitField< 0, 8, u32> x;
  670. BitField< 8, 8, u32> y;
  671. BitField<16, 8, u32> z;
  672. BitField<24, 8, u32> w;
  673. } int_uniforms[4];
  674. INSERT_PADDING_WORDS(0x5);
  675. // Offset to shader program entry point (in words)
  676. BitField<0, 16, u32> main_offset;
  677. union {
  678. BitField< 0, 4, u64> attribute0_register;
  679. BitField< 4, 4, u64> attribute1_register;
  680. BitField< 8, 4, u64> attribute2_register;
  681. BitField<12, 4, u64> attribute3_register;
  682. BitField<16, 4, u64> attribute4_register;
  683. BitField<20, 4, u64> attribute5_register;
  684. BitField<24, 4, u64> attribute6_register;
  685. BitField<28, 4, u64> attribute7_register;
  686. BitField<32, 4, u64> attribute8_register;
  687. BitField<36, 4, u64> attribute9_register;
  688. BitField<40, 4, u64> attribute10_register;
  689. BitField<44, 4, u64> attribute11_register;
  690. BitField<48, 4, u64> attribute12_register;
  691. BitField<52, 4, u64> attribute13_register;
  692. BitField<56, 4, u64> attribute14_register;
  693. BitField<60, 4, u64> attribute15_register;
  694. int GetRegisterForAttribute(int attribute_index) const {
  695. u64 fields[] = {
  696. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  697. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  698. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  699. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  700. };
  701. return (int)fields[attribute_index];
  702. }
  703. } input_register_map;
  704. // OUTMAP_MASK, 0x28E, CODETRANSFER_END
  705. INSERT_PADDING_WORDS(0x3);
  706. struct {
  707. enum Format : u32
  708. {
  709. FLOAT24 = 0,
  710. FLOAT32 = 1
  711. };
  712. bool IsFloat32() const {
  713. return format == FLOAT32;
  714. }
  715. union {
  716. // Index of the next uniform to write to
  717. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  718. // TODO: Maybe the uppermost index is for the geometry shader? Investigate!
  719. BitField<0, 7, u32> index;
  720. BitField<31, 1, Format> format;
  721. };
  722. // Writing to these registers sets the current uniform.
  723. u32 set_value[8];
  724. } uniform_setup;
  725. INSERT_PADDING_WORDS(0x2);
  726. struct {
  727. // Offset of the next instruction to write code to.
  728. // Incremented with each instruction write.
  729. u32 offset;
  730. // Writing to these registers sets the "current" word in the shader program.
  731. u32 set_word[8];
  732. } program;
  733. INSERT_PADDING_WORDS(0x1);
  734. // This register group is used to load an internal table of swizzling patterns,
  735. // which are indexed by each shader instruction to specify vector component swizzling.
  736. struct {
  737. // Offset of the next swizzle pattern to write code to.
  738. // Incremented with each instruction write.
  739. u32 offset;
  740. // Writing to these registers sets the current swizzle pattern in the table.
  741. u32 set_word[8];
  742. } swizzle_patterns;
  743. INSERT_PADDING_WORDS(0x2);
  744. };
  745. ShaderConfig gs;
  746. ShaderConfig vs;
  747. INSERT_PADDING_WORDS(0x20);
  748. // Map register indices to names readable by humans
  749. // Used for debugging purposes, so performance is not an issue here
  750. static std::string GetCommandName(int index) {
  751. std::map<u32, std::string> map;
  752. #define ADD_FIELD(name) \
  753. do { \
  754. map.insert({PICA_REG_INDEX(name), #name}); \
  755. /* TODO: change to Regs::name when VS2015 and other compilers support it */ \
  756. for (u32 i = PICA_REG_INDEX(name) + 1; i < PICA_REG_INDEX(name) + sizeof(Regs().name) / 4; ++i) \
  757. map.insert({i, #name + std::string("+") + std::to_string(i-PICA_REG_INDEX(name))}); \
  758. } while(false)
  759. ADD_FIELD(trigger_irq);
  760. ADD_FIELD(cull_mode);
  761. ADD_FIELD(viewport_size_x);
  762. ADD_FIELD(viewport_size_y);
  763. ADD_FIELD(viewport_depth_range);
  764. ADD_FIELD(viewport_depth_far_plane);
  765. ADD_FIELD(viewport_corner);
  766. ADD_FIELD(texture0_enable);
  767. ADD_FIELD(texture0);
  768. ADD_FIELD(texture0_format);
  769. ADD_FIELD(texture1);
  770. ADD_FIELD(texture1_format);
  771. ADD_FIELD(texture2);
  772. ADD_FIELD(texture2_format);
  773. ADD_FIELD(tev_stage0);
  774. ADD_FIELD(tev_stage1);
  775. ADD_FIELD(tev_stage2);
  776. ADD_FIELD(tev_stage3);
  777. ADD_FIELD(tev_combiner_buffer_input);
  778. ADD_FIELD(tev_stage4);
  779. ADD_FIELD(tev_stage5);
  780. ADD_FIELD(tev_combiner_buffer_color);
  781. ADD_FIELD(output_merger);
  782. ADD_FIELD(framebuffer);
  783. ADD_FIELD(vertex_attributes);
  784. ADD_FIELD(index_array);
  785. ADD_FIELD(num_vertices);
  786. ADD_FIELD(trigger_draw);
  787. ADD_FIELD(trigger_draw_indexed);
  788. ADD_FIELD(vs_default_attributes_setup);
  789. ADD_FIELD(command_buffer);
  790. ADD_FIELD(triangle_topology);
  791. ADD_FIELD(gs.bool_uniforms);
  792. ADD_FIELD(gs.int_uniforms);
  793. ADD_FIELD(gs.main_offset);
  794. ADD_FIELD(gs.input_register_map);
  795. ADD_FIELD(gs.uniform_setup);
  796. ADD_FIELD(gs.program);
  797. ADD_FIELD(gs.swizzle_patterns);
  798. ADD_FIELD(vs.bool_uniforms);
  799. ADD_FIELD(vs.int_uniforms);
  800. ADD_FIELD(vs.main_offset);
  801. ADD_FIELD(vs.input_register_map);
  802. ADD_FIELD(vs.uniform_setup);
  803. ADD_FIELD(vs.program);
  804. ADD_FIELD(vs.swizzle_patterns);
  805. #undef ADD_FIELD
  806. // Return empty string if no match is found
  807. return map[index];
  808. }
  809. static inline size_t NumIds() {
  810. return sizeof(Regs) / sizeof(u32);
  811. }
  812. u32& operator [] (int index) const {
  813. u32* content = (u32*)this;
  814. return content[index];
  815. }
  816. u32& operator [] (int index) {
  817. u32* content = (u32*)this;
  818. return content[index];
  819. }
  820. private:
  821. /*
  822. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  823. * This function should be used to get the address from a raw register value.
  824. */
  825. static inline u32 DecodeAddressRegister(u32 register_value) {
  826. return register_value * 8;
  827. }
  828. };
  829. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  830. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  831. // support for that.
  832. #ifndef _MSC_VER
  833. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  834. ASSERT_REG_POSITION(trigger_irq, 0x10);
  835. ASSERT_REG_POSITION(cull_mode, 0x40);
  836. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  837. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  838. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  839. ASSERT_REG_POSITION(viewport_depth_far_plane, 0x4e);
  840. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  841. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  842. ASSERT_REG_POSITION(viewport_corner, 0x68);
  843. ASSERT_REG_POSITION(texture0_enable, 0x80);
  844. ASSERT_REG_POSITION(texture0, 0x81);
  845. ASSERT_REG_POSITION(texture0_format, 0x8e);
  846. ASSERT_REG_POSITION(texture1, 0x91);
  847. ASSERT_REG_POSITION(texture1_format, 0x96);
  848. ASSERT_REG_POSITION(texture2, 0x99);
  849. ASSERT_REG_POSITION(texture2_format, 0x9e);
  850. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  851. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  852. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  853. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  854. ASSERT_REG_POSITION(tev_combiner_buffer_input, 0xe0);
  855. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  856. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  857. ASSERT_REG_POSITION(tev_combiner_buffer_color, 0xfd);
  858. ASSERT_REG_POSITION(output_merger, 0x100);
  859. ASSERT_REG_POSITION(framebuffer, 0x110);
  860. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  861. ASSERT_REG_POSITION(index_array, 0x227);
  862. ASSERT_REG_POSITION(num_vertices, 0x228);
  863. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  864. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  865. ASSERT_REG_POSITION(vs_default_attributes_setup, 0x232);
  866. ASSERT_REG_POSITION(command_buffer, 0x238);
  867. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  868. ASSERT_REG_POSITION(gs, 0x280);
  869. ASSERT_REG_POSITION(vs, 0x2b0);
  870. #undef ASSERT_REG_POSITION
  871. #endif // !defined(_MSC_VER)
  872. static_assert(sizeof(Regs::ShaderConfig) == 0x30 * sizeof(u32), "ShaderConfig structure has incorrect size");
  873. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  874. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  875. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  876. struct float24 {
  877. static float24 FromFloat32(float val) {
  878. float24 ret;
  879. ret.value = val;
  880. return ret;
  881. }
  882. // 16 bit mantissa, 7 bit exponent, 1 bit sign
  883. // TODO: No idea if this works as intended
  884. static float24 FromRawFloat24(u32 hex) {
  885. float24 ret;
  886. if ((hex & 0xFFFFFF) == 0) {
  887. ret.value = 0;
  888. } else {
  889. u32 mantissa = hex & 0xFFFF;
  890. u32 exponent = (hex >> 16) & 0x7F;
  891. u32 sign = hex >> 23;
  892. ret.value = std::pow(2.0f, (float)exponent-63.0f) * (1.0f + mantissa * std::pow(2.0f, -16.f));
  893. if (sign)
  894. ret.value = -ret.value;
  895. }
  896. return ret;
  897. }
  898. // Not recommended for anything but logging
  899. float ToFloat32() const {
  900. return value;
  901. }
  902. float24 operator * (const float24& flt) const {
  903. return float24::FromFloat32(ToFloat32() * flt.ToFloat32());
  904. }
  905. float24 operator / (const float24& flt) const {
  906. return float24::FromFloat32(ToFloat32() / flt.ToFloat32());
  907. }
  908. float24 operator + (const float24& flt) const {
  909. return float24::FromFloat32(ToFloat32() + flt.ToFloat32());
  910. }
  911. float24 operator - (const float24& flt) const {
  912. return float24::FromFloat32(ToFloat32() - flt.ToFloat32());
  913. }
  914. float24& operator *= (const float24& flt) {
  915. value *= flt.ToFloat32();
  916. return *this;
  917. }
  918. float24& operator /= (const float24& flt) {
  919. value /= flt.ToFloat32();
  920. return *this;
  921. }
  922. float24& operator += (const float24& flt) {
  923. value += flt.ToFloat32();
  924. return *this;
  925. }
  926. float24& operator -= (const float24& flt) {
  927. value -= flt.ToFloat32();
  928. return *this;
  929. }
  930. float24 operator - () const {
  931. return float24::FromFloat32(-ToFloat32());
  932. }
  933. bool operator < (const float24& flt) const {
  934. return ToFloat32() < flt.ToFloat32();
  935. }
  936. bool operator > (const float24& flt) const {
  937. return ToFloat32() > flt.ToFloat32();
  938. }
  939. bool operator >= (const float24& flt) const {
  940. return ToFloat32() >= flt.ToFloat32();
  941. }
  942. bool operator <= (const float24& flt) const {
  943. return ToFloat32() <= flt.ToFloat32();
  944. }
  945. bool operator == (const float24& flt) const {
  946. return ToFloat32() == flt.ToFloat32();
  947. }
  948. bool operator != (const float24& flt) const {
  949. return ToFloat32() != flt.ToFloat32();
  950. }
  951. private:
  952. // Stored as a regular float, merely for convenience
  953. // TODO: Perform proper arithmetic on this!
  954. float value;
  955. };
  956. /// Struct used to describe current Pica state
  957. struct State {
  958. /// Pica registers
  959. Regs regs;
  960. /// Vertex shader memory
  961. struct ShaderSetup {
  962. struct {
  963. Math::Vec4<float24> f[96];
  964. std::array<bool, 16> b;
  965. std::array<Math::Vec4<u8>, 4> i;
  966. } uniforms;
  967. Math::Vec4<float24> default_attributes[16];
  968. std::array<u32, 1024> program_code;
  969. std::array<u32, 1024> swizzle_data;
  970. };
  971. ShaderSetup vs;
  972. ShaderSetup gs;
  973. /// Current Pica command list
  974. struct {
  975. const u32* head_ptr;
  976. const u32* current_ptr;
  977. u32 length;
  978. } cmd_list;
  979. };
  980. /// Initialize Pica state
  981. void Init();
  982. /// Shutdown Pica state
  983. void Shutdown();
  984. extern State g_state; ///< Current Pica state
  985. } // namespace