pica.h 35 KB

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