pica.h 38 KB

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