pica.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <cstddef>
  7. #include <string>
  8. #ifndef _MSC_VER
  9. #include <type_traits> // for std::enable_if
  10. #endif
  11. #include "common/assert.h"
  12. #include "common/bit_field.h"
  13. #include "common/common_funcs.h"
  14. #include "common/common_types.h"
  15. #include "common/vector_math.h"
  16. #include "common/logging/log.h"
  17. namespace Pica {
  18. // Returns index corresponding to the Regs member labeled by field_name
  19. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  20. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  21. // For details cf. https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  22. // Hopefully, this will be fixed sometime in the future.
  23. // For lack of better alternatives, we currently hardcode the offsets when constant
  24. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  25. // will then make sure the offsets indeed match the automatically calculated ones).
  26. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  27. #if defined(_MSC_VER)
  28. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  29. #else
  30. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  31. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  32. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  33. // field offset. Otherwise, the compiler will fail to compile this code.
  34. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  35. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), size_t>::type)PICA_REG_INDEX(field_name))
  36. #endif // _MSC_VER
  37. struct Regs {
  38. INSERT_PADDING_WORDS(0x10);
  39. u32 trigger_irq;
  40. INSERT_PADDING_WORDS(0x2f);
  41. enum class CullMode : u32 {
  42. // Select which polygons are considered to be "frontfacing".
  43. KeepAll = 0,
  44. KeepClockWise = 1,
  45. KeepCounterClockWise = 2,
  46. // TODO: What does the third value imply?
  47. };
  48. union {
  49. BitField<0, 2, CullMode> cull_mode;
  50. };
  51. BitField<0, 24, u32> viewport_size_x;
  52. INSERT_PADDING_WORDS(0x1);
  53. BitField<0, 24, u32> viewport_size_y;
  54. INSERT_PADDING_WORDS(0x9);
  55. BitField<0, 24, u32> viewport_depth_range; // float24
  56. BitField<0, 24, u32> viewport_depth_near_plane; // float24
  57. BitField<0, 3, u32> vs_output_total;
  58. union VSOutputAttributes {
  59. // Maps components of output vertex attributes to semantics
  60. enum Semantic : u32
  61. {
  62. POSITION_X = 0,
  63. POSITION_Y = 1,
  64. POSITION_Z = 2,
  65. POSITION_W = 3,
  66. QUATERNION_X = 4,
  67. QUATERNION_Y = 5,
  68. QUATERNION_Z = 6,
  69. QUATERNION_W = 7,
  70. COLOR_R = 8,
  71. COLOR_G = 9,
  72. COLOR_B = 10,
  73. COLOR_A = 11,
  74. TEXCOORD0_U = 12,
  75. TEXCOORD0_V = 13,
  76. TEXCOORD1_U = 14,
  77. TEXCOORD1_V = 15,
  78. // TODO: Not verified
  79. VIEW_X = 18,
  80. VIEW_Y = 19,
  81. VIEW_Z = 20,
  82. TEXCOORD2_U = 22,
  83. TEXCOORD2_V = 23,
  84. INVALID = 31,
  85. };
  86. BitField< 0, 5, Semantic> map_x;
  87. BitField< 8, 5, Semantic> map_y;
  88. BitField<16, 5, Semantic> map_z;
  89. BitField<24, 5, Semantic> map_w;
  90. } vs_output_attributes[7];
  91. INSERT_PADDING_WORDS(0xe);
  92. enum class ScissorMode : u32 {
  93. Disabled = 0,
  94. Exclude = 1, // Exclude pixels inside the scissor box
  95. Include = 3 // Exclude pixels outside the scissor box
  96. };
  97. struct {
  98. BitField<0, 2, ScissorMode> mode;
  99. union {
  100. BitField< 0, 16, u32> x1;
  101. BitField<16, 16, u32> y1;
  102. };
  103. union {
  104. BitField< 0, 16, u32> x2;
  105. BitField<16, 16, u32> y2;
  106. };
  107. } scissor_test;
  108. union {
  109. BitField< 0, 10, s32> x;
  110. BitField<16, 10, s32> y;
  111. } viewport_corner;
  112. INSERT_PADDING_WORDS(0x1);
  113. //TODO: early depth
  114. INSERT_PADDING_WORDS(0x1);
  115. INSERT_PADDING_WORDS(0x2);
  116. enum DepthBuffering : u32 {
  117. WBuffering = 0,
  118. ZBuffering = 1,
  119. };
  120. BitField< 0, 1, DepthBuffering> depthmap_enable;
  121. INSERT_PADDING_WORDS(0x12);
  122. struct TextureConfig {
  123. enum TextureType : u32 {
  124. Texture2D = 0,
  125. TextureCube = 1,
  126. Shadow2D = 2,
  127. Projection2D = 3,
  128. ShadowCube = 4,
  129. Disabled = 5,
  130. };
  131. enum WrapMode : u32 {
  132. ClampToEdge = 0,
  133. ClampToBorder = 1,
  134. Repeat = 2,
  135. MirroredRepeat = 3,
  136. };
  137. enum TextureFilter : u32 {
  138. Nearest = 0,
  139. Linear = 1
  140. };
  141. union {
  142. u32 raw;
  143. BitField< 0, 8, u32> r;
  144. BitField< 8, 8, u32> g;
  145. BitField<16, 8, u32> b;
  146. BitField<24, 8, u32> a;
  147. } border_color;
  148. union {
  149. BitField< 0, 16, u32> height;
  150. BitField<16, 16, u32> width;
  151. };
  152. union {
  153. BitField< 1, 1, TextureFilter> mag_filter;
  154. BitField< 2, 1, TextureFilter> min_filter;
  155. BitField< 8, 2, WrapMode> wrap_t;
  156. BitField<12, 2, WrapMode> wrap_s;
  157. BitField<28, 2, TextureType> type; ///< @note Only valid for texture 0 according to 3DBrew.
  158. };
  159. INSERT_PADDING_WORDS(0x1);
  160. u32 address;
  161. u32 GetPhysicalAddress() const {
  162. return DecodeAddressRegister(address);
  163. }
  164. // texture1 and texture2 store the texture format directly after the address
  165. // whereas texture0 inserts some additional flags inbetween.
  166. // Hence, we store the format separately so that all other parameters can be described
  167. // in a single structure.
  168. };
  169. enum class TextureFormat : u32 {
  170. RGBA8 = 0,
  171. RGB8 = 1,
  172. RGB5A1 = 2,
  173. RGB565 = 3,
  174. RGBA4 = 4,
  175. IA8 = 5,
  176. RG8 = 6, ///< @note Also called HILO8 in 3DBrew.
  177. I8 = 7,
  178. A8 = 8,
  179. IA4 = 9,
  180. I4 = 10,
  181. A4 = 11,
  182. ETC1 = 12, // compressed
  183. ETC1A4 = 13, // compressed
  184. };
  185. enum class LogicOp : u32 {
  186. Clear = 0,
  187. And = 1,
  188. AndReverse = 2,
  189. Copy = 3,
  190. Set = 4,
  191. CopyInverted = 5,
  192. NoOp = 6,
  193. Invert = 7,
  194. Nand = 8,
  195. Or = 9,
  196. Nor = 10,
  197. Xor = 11,
  198. Equiv = 12,
  199. AndInverted = 13,
  200. OrReverse = 14,
  201. OrInverted = 15,
  202. };
  203. static unsigned NibblesPerPixel(TextureFormat format) {
  204. switch (format) {
  205. case TextureFormat::RGBA8:
  206. return 8;
  207. case TextureFormat::RGB8:
  208. return 6;
  209. case TextureFormat::RGB5A1:
  210. case TextureFormat::RGB565:
  211. case TextureFormat::RGBA4:
  212. case TextureFormat::IA8:
  213. case TextureFormat::RG8:
  214. return 4;
  215. case TextureFormat::I4:
  216. case TextureFormat::A4:
  217. return 1;
  218. case TextureFormat::I8:
  219. case TextureFormat::A8:
  220. case TextureFormat::IA4:
  221. default: // placeholder for yet unknown formats
  222. return 2;
  223. }
  224. }
  225. union {
  226. BitField< 0, 1, u32> texture0_enable;
  227. BitField< 1, 1, u32> texture1_enable;
  228. BitField< 2, 1, u32> texture2_enable;
  229. };
  230. TextureConfig texture0;
  231. INSERT_PADDING_WORDS(0x8);
  232. BitField<0, 4, TextureFormat> texture0_format;
  233. BitField<0, 1, u32> fragment_lighting_enable;
  234. INSERT_PADDING_WORDS(0x1);
  235. TextureConfig texture1;
  236. BitField<0, 4, TextureFormat> texture1_format;
  237. INSERT_PADDING_WORDS(0x2);
  238. TextureConfig texture2;
  239. BitField<0, 4, TextureFormat> texture2_format;
  240. INSERT_PADDING_WORDS(0x21);
  241. struct FullTextureConfig {
  242. const bool enabled;
  243. const TextureConfig config;
  244. const TextureFormat format;
  245. };
  246. const std::array<FullTextureConfig, 3> GetTextures() const {
  247. return {{
  248. { texture0_enable.ToBool(), texture0, texture0_format },
  249. { texture1_enable.ToBool(), texture1, texture1_format },
  250. { texture2_enable.ToBool(), texture2, texture2_format }
  251. }};
  252. }
  253. // 0xc0-0xff: Texture Combiner (akin to glTexEnv)
  254. struct TevStageConfig {
  255. enum class Source : u32 {
  256. PrimaryColor = 0x0,
  257. PrimaryFragmentColor = 0x1,
  258. SecondaryFragmentColor = 0x2,
  259. Texture0 = 0x3,
  260. Texture1 = 0x4,
  261. Texture2 = 0x5,
  262. Texture3 = 0x6,
  263. PreviousBuffer = 0xd,
  264. Constant = 0xe,
  265. Previous = 0xf,
  266. };
  267. enum class ColorModifier : u32 {
  268. SourceColor = 0x0,
  269. OneMinusSourceColor = 0x1,
  270. SourceAlpha = 0x2,
  271. OneMinusSourceAlpha = 0x3,
  272. SourceRed = 0x4,
  273. OneMinusSourceRed = 0x5,
  274. SourceGreen = 0x8,
  275. OneMinusSourceGreen = 0x9,
  276. SourceBlue = 0xc,
  277. OneMinusSourceBlue = 0xd,
  278. };
  279. enum class AlphaModifier : u32 {
  280. SourceAlpha = 0x0,
  281. OneMinusSourceAlpha = 0x1,
  282. SourceRed = 0x2,
  283. OneMinusSourceRed = 0x3,
  284. SourceGreen = 0x4,
  285. OneMinusSourceGreen = 0x5,
  286. SourceBlue = 0x6,
  287. OneMinusSourceBlue = 0x7,
  288. };
  289. enum class Operation : u32 {
  290. Replace = 0,
  291. Modulate = 1,
  292. Add = 2,
  293. AddSigned = 3,
  294. Lerp = 4,
  295. Subtract = 5,
  296. Dot3_RGB = 6,
  297. MultiplyThenAdd = 8,
  298. AddThenMultiply = 9,
  299. };
  300. union {
  301. u32 sources_raw;
  302. BitField< 0, 4, Source> color_source1;
  303. BitField< 4, 4, Source> color_source2;
  304. BitField< 8, 4, Source> color_source3;
  305. BitField<16, 4, Source> alpha_source1;
  306. BitField<20, 4, Source> alpha_source2;
  307. BitField<24, 4, Source> alpha_source3;
  308. };
  309. union {
  310. u32 modifiers_raw;
  311. BitField< 0, 4, ColorModifier> color_modifier1;
  312. BitField< 4, 4, ColorModifier> color_modifier2;
  313. BitField< 8, 4, ColorModifier> color_modifier3;
  314. BitField<12, 3, AlphaModifier> alpha_modifier1;
  315. BitField<16, 3, AlphaModifier> alpha_modifier2;
  316. BitField<20, 3, AlphaModifier> alpha_modifier3;
  317. };
  318. union {
  319. u32 ops_raw;
  320. BitField< 0, 4, Operation> color_op;
  321. BitField<16, 4, Operation> alpha_op;
  322. };
  323. union {
  324. u32 const_color;
  325. BitField< 0, 8, u32> const_r;
  326. BitField< 8, 8, u32> const_g;
  327. BitField<16, 8, u32> const_b;
  328. BitField<24, 8, u32> const_a;
  329. };
  330. union {
  331. u32 scales_raw;
  332. BitField< 0, 2, u32> color_scale;
  333. BitField<16, 2, u32> alpha_scale;
  334. };
  335. inline unsigned GetColorMultiplier() const {
  336. return (color_scale < 3) ? (1 << color_scale) : 1;
  337. }
  338. inline unsigned GetAlphaMultiplier() const {
  339. return (alpha_scale < 3) ? (1 << alpha_scale) : 1;
  340. }
  341. };
  342. TevStageConfig tev_stage0;
  343. INSERT_PADDING_WORDS(0x3);
  344. TevStageConfig tev_stage1;
  345. INSERT_PADDING_WORDS(0x3);
  346. TevStageConfig tev_stage2;
  347. INSERT_PADDING_WORDS(0x3);
  348. TevStageConfig tev_stage3;
  349. INSERT_PADDING_WORDS(0x3);
  350. enum class FogMode : u32 {
  351. None = 0,
  352. Fog = 5,
  353. Gas = 7,
  354. };
  355. union {
  356. BitField<0, 3, FogMode> fog_mode;
  357. BitField<16, 1, u32> fog_flip;
  358. union {
  359. // Tev stages 0-3 write their output to the combiner buffer if the corresponding bit in
  360. // these masks are set
  361. BitField< 8, 4, u32> update_mask_rgb;
  362. BitField<12, 4, u32> update_mask_a;
  363. bool TevStageUpdatesCombinerBufferColor(unsigned stage_index) const {
  364. return (stage_index < 4) && (update_mask_rgb & (1 << stage_index));
  365. }
  366. bool TevStageUpdatesCombinerBufferAlpha(unsigned stage_index) const {
  367. return (stage_index < 4) && (update_mask_a & (1 << stage_index));
  368. }
  369. } tev_combiner_buffer_input;
  370. };
  371. union {
  372. u32 raw;
  373. BitField< 0, 8, u32> r;
  374. BitField< 8, 8, u32> g;
  375. BitField<16, 8, u32> b;
  376. } fog_color;
  377. INSERT_PADDING_WORDS(0x4);
  378. BitField<0, 16, u32> fog_lut_offset;
  379. INSERT_PADDING_WORDS(0x1);
  380. u32 fog_lut_data[8];
  381. TevStageConfig tev_stage4;
  382. INSERT_PADDING_WORDS(0x3);
  383. TevStageConfig tev_stage5;
  384. union {
  385. u32 raw;
  386. BitField< 0, 8, u32> r;
  387. BitField< 8, 8, u32> g;
  388. BitField<16, 8, u32> b;
  389. BitField<24, 8, u32> a;
  390. } tev_combiner_buffer_color;
  391. INSERT_PADDING_WORDS(0x2);
  392. const std::array<Regs::TevStageConfig,6> GetTevStages() const {
  393. return {{ tev_stage0, tev_stage1,
  394. tev_stage2, tev_stage3,
  395. tev_stage4, tev_stage5 }};
  396. };
  397. enum class BlendEquation : u32 {
  398. Add = 0,
  399. Subtract = 1,
  400. ReverseSubtract = 2,
  401. Min = 3,
  402. Max = 4,
  403. };
  404. enum class BlendFactor : u32 {
  405. Zero = 0,
  406. One = 1,
  407. SourceColor = 2,
  408. OneMinusSourceColor = 3,
  409. DestColor = 4,
  410. OneMinusDestColor = 5,
  411. SourceAlpha = 6,
  412. OneMinusSourceAlpha = 7,
  413. DestAlpha = 8,
  414. OneMinusDestAlpha = 9,
  415. ConstantColor = 10,
  416. OneMinusConstantColor = 11,
  417. ConstantAlpha = 12,
  418. OneMinusConstantAlpha = 13,
  419. SourceAlphaSaturate = 14,
  420. };
  421. enum class CompareFunc : u32 {
  422. Never = 0,
  423. Always = 1,
  424. Equal = 2,
  425. NotEqual = 3,
  426. LessThan = 4,
  427. LessThanOrEqual = 5,
  428. GreaterThan = 6,
  429. GreaterThanOrEqual = 7,
  430. };
  431. enum class StencilAction : u32 {
  432. Keep = 0,
  433. Zero = 1,
  434. Replace = 2,
  435. Increment = 3,
  436. Decrement = 4,
  437. Invert = 5,
  438. IncrementWrap = 6,
  439. DecrementWrap = 7
  440. };
  441. struct {
  442. union {
  443. // If false, logic blending is used
  444. BitField<8, 1, u32> alphablend_enable;
  445. };
  446. union {
  447. BitField< 0, 8, BlendEquation> blend_equation_rgb;
  448. BitField< 8, 8, BlendEquation> blend_equation_a;
  449. BitField<16, 4, BlendFactor> factor_source_rgb;
  450. BitField<20, 4, BlendFactor> factor_dest_rgb;
  451. BitField<24, 4, BlendFactor> factor_source_a;
  452. BitField<28, 4, BlendFactor> factor_dest_a;
  453. } alpha_blending;
  454. union {
  455. BitField<0, 4, LogicOp> logic_op;
  456. };
  457. union {
  458. u32 raw;
  459. BitField< 0, 8, u32> r;
  460. BitField< 8, 8, u32> g;
  461. BitField<16, 8, u32> b;
  462. BitField<24, 8, u32> a;
  463. } blend_const;
  464. union {
  465. BitField< 0, 1, u32> enable;
  466. BitField< 4, 3, CompareFunc> func;
  467. BitField< 8, 8, u32> ref;
  468. } alpha_test;
  469. struct {
  470. union {
  471. // Raw value of this register
  472. u32 raw_func;
  473. // If true, enable stencil testing
  474. BitField< 0, 1, u32> enable;
  475. // Comparison operation for stencil testing
  476. BitField< 4, 3, CompareFunc> func;
  477. // Mask used to control writing to the stencil buffer
  478. BitField< 8, 8, u32> write_mask;
  479. // Value to compare against for stencil testing
  480. BitField<16, 8, u32> reference_value;
  481. // Mask to apply on stencil test inputs
  482. BitField<24, 8, u32> input_mask;
  483. };
  484. union {
  485. // Raw value of this register
  486. u32 raw_op;
  487. // Action to perform when the stencil test fails
  488. BitField< 0, 3, StencilAction> action_stencil_fail;
  489. // Action to perform when stencil testing passed but depth testing fails
  490. BitField< 4, 3, StencilAction> action_depth_fail;
  491. // Action to perform when both stencil and depth testing pass
  492. BitField< 8, 3, StencilAction> action_depth_pass;
  493. };
  494. } stencil_test;
  495. union {
  496. BitField< 0, 1, u32> depth_test_enable;
  497. BitField< 4, 3, CompareFunc> depth_test_func;
  498. BitField< 8, 1, u32> red_enable;
  499. BitField< 9, 1, u32> green_enable;
  500. BitField<10, 1, u32> blue_enable;
  501. BitField<11, 1, u32> alpha_enable;
  502. BitField<12, 1, u32> depth_write_enable;
  503. };
  504. INSERT_PADDING_WORDS(0x8);
  505. } output_merger;
  506. // Components are laid out in reverse byte order, most significant bits first.
  507. enum class ColorFormat : u32 {
  508. RGBA8 = 0,
  509. RGB8 = 1,
  510. RGB5A1 = 2,
  511. RGB565 = 3,
  512. RGBA4 = 4,
  513. };
  514. enum class DepthFormat : u32 {
  515. D16 = 0,
  516. D24 = 2,
  517. D24S8 = 3,
  518. };
  519. // Returns the number of bytes in the specified color format
  520. static unsigned BytesPerColorPixel(ColorFormat format) {
  521. switch (format) {
  522. case ColorFormat::RGBA8:
  523. return 4;
  524. case ColorFormat::RGB8:
  525. return 3;
  526. case ColorFormat::RGB5A1:
  527. case ColorFormat::RGB565:
  528. case ColorFormat::RGBA4:
  529. return 2;
  530. default:
  531. LOG_CRITICAL(HW_GPU, "Unknown color format %u", format);
  532. UNIMPLEMENTED();
  533. }
  534. }
  535. struct FramebufferConfig {
  536. INSERT_PADDING_WORDS(0x3);
  537. union {
  538. BitField<0, 4, u32> allow_color_write; // 0 = disable, else enable
  539. };
  540. INSERT_PADDING_WORDS(0x1);
  541. union {
  542. BitField<0, 2, u32> allow_depth_stencil_write; // 0 = disable, else enable
  543. };
  544. DepthFormat depth_format; // TODO: Should be a BitField!
  545. BitField<16, 3, ColorFormat> color_format;
  546. INSERT_PADDING_WORDS(0x4);
  547. u32 depth_buffer_address;
  548. u32 color_buffer_address;
  549. union {
  550. // Apparently, the framebuffer width is stored as expected,
  551. // while the height is stored as the actual height minus one.
  552. // Hence, don't access these fields directly but use the accessors
  553. // GetWidth() and GetHeight() instead.
  554. BitField< 0, 11, u32> width;
  555. BitField<12, 10, u32> height;
  556. };
  557. INSERT_PADDING_WORDS(0x1);
  558. inline u32 GetColorBufferPhysicalAddress() const {
  559. return DecodeAddressRegister(color_buffer_address);
  560. }
  561. inline u32 GetDepthBufferPhysicalAddress() const {
  562. return DecodeAddressRegister(depth_buffer_address);
  563. }
  564. inline u32 GetWidth() const {
  565. return width;
  566. }
  567. inline u32 GetHeight() const {
  568. return height + 1;
  569. }
  570. } framebuffer;
  571. // Returns the number of bytes in the specified depth format
  572. static u32 BytesPerDepthPixel(DepthFormat format) {
  573. switch (format) {
  574. case DepthFormat::D16:
  575. return 2;
  576. case DepthFormat::D24:
  577. return 3;
  578. case DepthFormat::D24S8:
  579. return 4;
  580. default:
  581. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  582. UNIMPLEMENTED();
  583. }
  584. }
  585. // Returns the number of bits per depth component of the specified depth format
  586. static u32 DepthBitsPerPixel(DepthFormat format) {
  587. switch (format) {
  588. case DepthFormat::D16:
  589. return 16;
  590. case DepthFormat::D24:
  591. case DepthFormat::D24S8:
  592. return 24;
  593. default:
  594. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  595. UNIMPLEMENTED();
  596. }
  597. }
  598. INSERT_PADDING_WORDS(0x20);
  599. enum class LightingSampler {
  600. Distribution0 = 0,
  601. Distribution1 = 1,
  602. Fresnel = 3,
  603. ReflectBlue = 4,
  604. ReflectGreen = 5,
  605. ReflectRed = 6,
  606. SpotlightAttenuation = 8,
  607. DistanceAttenuation = 16,
  608. };
  609. /**
  610. * Pica fragment lighting supports using different LUTs for each lighting component:
  611. * Reflectance R, G, and B channels, distribution function for specular components 0 and 1,
  612. * fresnel factor, and spotlight attenuation. Furthermore, which LUTs are used for each channel
  613. * (or whether a channel is enabled at all) is specified by various pre-defined lighting
  614. * configurations. With configurations that require more LUTs, more cycles are required on HW to
  615. * perform lighting computations.
  616. */
  617. enum class LightingConfig {
  618. Config0 = 0, ///< Reflect Red, Distribution 0, Spotlight
  619. Config1 = 1, ///< Reflect Red, Fresnel, Spotlight
  620. Config2 = 2, ///< Reflect Red, Distribution 0/1
  621. Config3 = 3, ///< Distribution 0/1, Fresnel
  622. Config4 = 4, ///< Reflect Red/Green/Blue, Distribution 0/1, Spotlight
  623. Config5 = 5, ///< Reflect Red/Green/Blue, Distribution 0, Fresnel, Spotlight
  624. Config6 = 6, ///< Reflect Red, Distribution 0/1, Fresnel, Spotlight
  625. Config7 = 8, ///< Reflect Red/Green/Blue, Distribution 0/1, Fresnel, Spotlight
  626. ///< NOTE: '8' is intentional, '7' does not appear to be a valid configuration
  627. };
  628. /// Selects which lighting components are affected by fresnel
  629. enum class LightingFresnelSelector {
  630. None = 0, ///< Fresnel is disabled
  631. PrimaryAlpha = 1, ///< Primary (diffuse) lighting alpha is affected by fresnel
  632. SecondaryAlpha = 2, ///< Secondary (specular) lighting alpha is affected by fresnel
  633. Both = PrimaryAlpha | SecondaryAlpha, ///< Both primary and secondary lighting alphas are affected by fresnel
  634. };
  635. /// Factor used to scale the output of a lighting LUT
  636. enum class LightingScale {
  637. Scale1 = 0, ///< Scale is 1x
  638. Scale2 = 1, ///< Scale is 2x
  639. Scale4 = 2, ///< Scale is 4x
  640. Scale8 = 3, ///< Scale is 8x
  641. Scale1_4 = 6, ///< Scale is 0.25x
  642. Scale1_2 = 7, ///< Scale is 0.5x
  643. };
  644. enum class LightingLutInput {
  645. NH = 0, // Cosine of the angle between the normal and half-angle vectors
  646. VH = 1, // Cosine of the angle between the view and half-angle vectors
  647. NV = 2, // Cosine of the angle between the normal and the view vector
  648. LN = 3, // Cosine of the angle between the light and the normal vectors
  649. };
  650. enum class LightingBumpMode : u32 {
  651. None = 0,
  652. NormalMap = 1,
  653. TangentMap = 2,
  654. };
  655. union LightColor {
  656. BitField< 0, 10, u32> b;
  657. BitField<10, 10, u32> g;
  658. BitField<20, 10, u32> r;
  659. Math::Vec3f ToVec3f() const {
  660. // These fields are 10 bits wide, however 255 corresponds to 1.0f for each color component
  661. return Math::MakeVec((f32)r / 255.f, (f32)g / 255.f, (f32)b / 255.f);
  662. }
  663. };
  664. /// Returns true if the specified lighting sampler is supported by the current Pica lighting configuration
  665. static bool IsLightingSamplerSupported(LightingConfig config, LightingSampler sampler) {
  666. switch (sampler) {
  667. case LightingSampler::Distribution0:
  668. return (config != LightingConfig::Config1);
  669. case LightingSampler::Distribution1:
  670. return (config != LightingConfig::Config0) && (config != LightingConfig::Config1) && (config != LightingConfig::Config5);
  671. case LightingSampler::Fresnel:
  672. return (config != LightingConfig::Config0) && (config != LightingConfig::Config2) && (config != LightingConfig::Config4);
  673. case LightingSampler::ReflectRed:
  674. return (config != LightingConfig::Config3);
  675. case LightingSampler::ReflectGreen:
  676. case LightingSampler::ReflectBlue:
  677. return (config == LightingConfig::Config4) || (config == LightingConfig::Config5) || (config == LightingConfig::Config7);
  678. default:
  679. UNREACHABLE_MSG("Regs::IsLightingSamplerSupported: Reached "
  680. "unreachable section, sampler should be one "
  681. "of Distribution0, Distribution1, Fresnel, "
  682. "ReflectRed, ReflectGreen or ReflectBlue, instead "
  683. "got %i", static_cast<int>(config));
  684. }
  685. }
  686. struct {
  687. struct LightSrc {
  688. LightColor specular_0; // material.specular_0 * light.specular_0
  689. LightColor specular_1; // material.specular_1 * light.specular_1
  690. LightColor diffuse; // material.diffuse * light.diffuse
  691. LightColor ambient; // material.ambient * light.ambient
  692. // Encoded as 16-bit floating point
  693. union {
  694. BitField< 0, 16, u32> x;
  695. BitField<16, 16, u32> y;
  696. };
  697. union {
  698. BitField< 0, 16, u32> z;
  699. };
  700. INSERT_PADDING_WORDS(0x3);
  701. union {
  702. BitField<0, 1, u32> directional;
  703. BitField<1, 1, u32> two_sided_diffuse; // When disabled, clamp dot-product to 0
  704. } config;
  705. BitField<0, 20, u32> dist_atten_bias;
  706. BitField<0, 20, u32> dist_atten_scale;
  707. INSERT_PADDING_WORDS(0x4);
  708. };
  709. static_assert(sizeof(LightSrc) == 0x10 * sizeof(u32), "LightSrc structure must be 0x10 words");
  710. LightSrc light[8];
  711. LightColor global_ambient; // Emission + (material.ambient * lighting.ambient)
  712. INSERT_PADDING_WORDS(0x1);
  713. BitField<0, 3, u32> num_lights; // Number of enabled lights - 1
  714. union {
  715. BitField< 2, 2, LightingFresnelSelector> fresnel_selector;
  716. BitField< 4, 4, LightingConfig> config;
  717. BitField<22, 2, u32> bump_selector; // 0: Texture 0, 1: Texture 1, 2: Texture 2
  718. BitField<27, 1, u32> clamp_highlights;
  719. BitField<28, 2, LightingBumpMode> bump_mode;
  720. BitField<30, 1, u32> disable_bump_renorm;
  721. } config0;
  722. union {
  723. BitField<16, 1, u32> disable_lut_d0;
  724. BitField<17, 1, u32> disable_lut_d1;
  725. BitField<19, 1, u32> disable_lut_fr;
  726. BitField<20, 1, u32> disable_lut_rr;
  727. BitField<21, 1, u32> disable_lut_rg;
  728. BitField<22, 1, u32> disable_lut_rb;
  729. // Each bit specifies whether distance attenuation should be applied for the
  730. // corresponding light
  731. BitField<24, 1, u32> disable_dist_atten_light_0;
  732. BitField<25, 1, u32> disable_dist_atten_light_1;
  733. BitField<26, 1, u32> disable_dist_atten_light_2;
  734. BitField<27, 1, u32> disable_dist_atten_light_3;
  735. BitField<28, 1, u32> disable_dist_atten_light_4;
  736. BitField<29, 1, u32> disable_dist_atten_light_5;
  737. BitField<30, 1, u32> disable_dist_atten_light_6;
  738. BitField<31, 1, u32> disable_dist_atten_light_7;
  739. } config1;
  740. bool IsDistAttenDisabled(unsigned index) const {
  741. const unsigned disable[] = { config1.disable_dist_atten_light_0, config1.disable_dist_atten_light_1,
  742. config1.disable_dist_atten_light_2, config1.disable_dist_atten_light_3,
  743. config1.disable_dist_atten_light_4, config1.disable_dist_atten_light_5,
  744. config1.disable_dist_atten_light_6, config1.disable_dist_atten_light_7 };
  745. return disable[index] != 0;
  746. }
  747. union {
  748. BitField<0, 8, u32> index; ///< Index at which to set data in the LUT
  749. BitField<8, 5, u32> type; ///< Type of LUT for which to set data
  750. } lut_config;
  751. BitField<0, 1, u32> disable;
  752. INSERT_PADDING_WORDS(0x1);
  753. // When data is written to any of these registers, it gets written to the lookup table of
  754. // the selected type at the selected index, specified above in the `lut_config` register.
  755. // With each write, `lut_config.index` is incremented. It does not matter which of these
  756. // registers is written to, the behavior will be the same.
  757. u32 lut_data[8];
  758. // These are used to specify if absolute (abs) value should be used for each LUT index. When
  759. // abs mode is disabled, LUT indexes are in the range of (-1.0, 1.0). Otherwise, they are in
  760. // the range of (0.0, 1.0).
  761. union {
  762. BitField< 1, 1, u32> disable_d0;
  763. BitField< 5, 1, u32> disable_d1;
  764. BitField< 9, 1, u32> disable_sp;
  765. BitField<13, 1, u32> disable_fr;
  766. BitField<17, 1, u32> disable_rb;
  767. BitField<21, 1, u32> disable_rg;
  768. BitField<25, 1, u32> disable_rr;
  769. } abs_lut_input;
  770. union {
  771. BitField< 0, 3, LightingLutInput> d0;
  772. BitField< 4, 3, LightingLutInput> d1;
  773. BitField< 8, 3, LightingLutInput> sp;
  774. BitField<12, 3, LightingLutInput> fr;
  775. BitField<16, 3, LightingLutInput> rb;
  776. BitField<20, 3, LightingLutInput> rg;
  777. BitField<24, 3, LightingLutInput> rr;
  778. } lut_input;
  779. union {
  780. BitField< 0, 3, LightingScale> d0;
  781. BitField< 4, 3, LightingScale> d1;
  782. BitField< 8, 3, LightingScale> sp;
  783. BitField<12, 3, LightingScale> fr;
  784. BitField<16, 3, LightingScale> rb;
  785. BitField<20, 3, LightingScale> rg;
  786. BitField<24, 3, LightingScale> rr;
  787. static float GetScale(LightingScale scale) {
  788. switch (scale) {
  789. case LightingScale::Scale1:
  790. return 1.0f;
  791. case LightingScale::Scale2:
  792. return 2.0f;
  793. case LightingScale::Scale4:
  794. return 4.0f;
  795. case LightingScale::Scale8:
  796. return 8.0f;
  797. case LightingScale::Scale1_4:
  798. return 0.25f;
  799. case LightingScale::Scale1_2:
  800. return 0.5f;
  801. }
  802. return 0.0f;
  803. }
  804. } lut_scale;
  805. INSERT_PADDING_WORDS(0x6);
  806. union {
  807. // There are 8 light enable "slots", corresponding to the total number of lights
  808. // supported by Pica. For N enabled lights (specified by register 0x1c2, or 'src_num'
  809. // above), the first N slots below will be set to integers within the range of 0-7,
  810. // corresponding to the actual light that is enabled for each slot.
  811. BitField< 0, 3, u32> slot_0;
  812. BitField< 4, 3, u32> slot_1;
  813. BitField< 8, 3, u32> slot_2;
  814. BitField<12, 3, u32> slot_3;
  815. BitField<16, 3, u32> slot_4;
  816. BitField<20, 3, u32> slot_5;
  817. BitField<24, 3, u32> slot_6;
  818. BitField<28, 3, u32> slot_7;
  819. unsigned GetNum(unsigned index) const {
  820. const unsigned enable_slots[] = { slot_0, slot_1, slot_2, slot_3, slot_4, slot_5, slot_6, slot_7 };
  821. return enable_slots[index];
  822. }
  823. } light_enable;
  824. } lighting;
  825. INSERT_PADDING_WORDS(0x26);
  826. enum class VertexAttributeFormat : u64 {
  827. BYTE = 0,
  828. UBYTE = 1,
  829. SHORT = 2,
  830. FLOAT = 3,
  831. };
  832. struct {
  833. BitField<0, 29, u32> base_address;
  834. u32 GetPhysicalBaseAddress() const {
  835. return DecodeAddressRegister(base_address);
  836. }
  837. // Descriptor for internal vertex attributes
  838. union {
  839. BitField< 0, 2, VertexAttributeFormat> format0; // size of one element
  840. BitField< 2, 2, u64> size0; // number of elements minus 1
  841. BitField< 4, 2, VertexAttributeFormat> format1;
  842. BitField< 6, 2, u64> size1;
  843. BitField< 8, 2, VertexAttributeFormat> format2;
  844. BitField<10, 2, u64> size2;
  845. BitField<12, 2, VertexAttributeFormat> format3;
  846. BitField<14, 2, u64> size3;
  847. BitField<16, 2, VertexAttributeFormat> format4;
  848. BitField<18, 2, u64> size4;
  849. BitField<20, 2, VertexAttributeFormat> format5;
  850. BitField<22, 2, u64> size5;
  851. BitField<24, 2, VertexAttributeFormat> format6;
  852. BitField<26, 2, u64> size6;
  853. BitField<28, 2, VertexAttributeFormat> format7;
  854. BitField<30, 2, u64> size7;
  855. BitField<32, 2, VertexAttributeFormat> format8;
  856. BitField<34, 2, u64> size8;
  857. BitField<36, 2, VertexAttributeFormat> format9;
  858. BitField<38, 2, u64> size9;
  859. BitField<40, 2, VertexAttributeFormat> format10;
  860. BitField<42, 2, u64> size10;
  861. BitField<44, 2, VertexAttributeFormat> format11;
  862. BitField<46, 2, u64> size11;
  863. BitField<48, 12, u64> attribute_mask;
  864. // number of total attributes minus 1
  865. BitField<60, 4, u64> num_extra_attributes;
  866. };
  867. inline VertexAttributeFormat GetFormat(int n) const {
  868. VertexAttributeFormat formats[] = {
  869. format0, format1, format2, format3,
  870. format4, format5, format6, format7,
  871. format8, format9, format10, format11
  872. };
  873. return formats[n];
  874. }
  875. inline int GetNumElements(int n) const {
  876. u64 sizes[] = {
  877. size0, size1, size2, size3,
  878. size4, size5, size6, size7,
  879. size8, size9, size10, size11
  880. };
  881. return (int)sizes[n]+1;
  882. }
  883. inline int GetElementSizeInBytes(int n) const {
  884. return (GetFormat(n) == VertexAttributeFormat::FLOAT) ? 4 :
  885. (GetFormat(n) == VertexAttributeFormat::SHORT) ? 2 : 1;
  886. }
  887. inline int GetStride(int n) const {
  888. return GetNumElements(n) * GetElementSizeInBytes(n);
  889. }
  890. inline bool IsDefaultAttribute(int id) const {
  891. return (id >= 12) || (attribute_mask & (1ULL << id)) != 0;
  892. }
  893. inline int GetNumTotalAttributes() const {
  894. return (int)num_extra_attributes+1;
  895. }
  896. // Attribute loaders map the source vertex data to input attributes
  897. // This e.g. allows to load different attributes from different memory locations
  898. struct {
  899. // Source attribute data offset from the base address
  900. u32 data_offset;
  901. union {
  902. BitField< 0, 4, u64> comp0;
  903. BitField< 4, 4, u64> comp1;
  904. BitField< 8, 4, u64> comp2;
  905. BitField<12, 4, u64> comp3;
  906. BitField<16, 4, u64> comp4;
  907. BitField<20, 4, u64> comp5;
  908. BitField<24, 4, u64> comp6;
  909. BitField<28, 4, u64> comp7;
  910. BitField<32, 4, u64> comp8;
  911. BitField<36, 4, u64> comp9;
  912. BitField<40, 4, u64> comp10;
  913. BitField<44, 4, u64> comp11;
  914. // bytes for a single vertex in this loader
  915. BitField<48, 8, u64> byte_count;
  916. BitField<60, 4, u64> component_count;
  917. };
  918. inline int GetComponent(int n) const {
  919. u64 components[] = {
  920. comp0, comp1, comp2, comp3,
  921. comp4, comp5, comp6, comp7,
  922. comp8, comp9, comp10, comp11
  923. };
  924. return (int)components[n];
  925. }
  926. } attribute_loaders[12];
  927. } vertex_attributes;
  928. struct {
  929. enum IndexFormat : u32 {
  930. BYTE = 0,
  931. SHORT = 1,
  932. };
  933. union {
  934. BitField<0, 31, u32> offset; // relative to base attribute address
  935. BitField<31, 1, IndexFormat> format;
  936. };
  937. } index_array;
  938. // Number of vertices to render
  939. u32 num_vertices;
  940. INSERT_PADDING_WORDS(0x1);
  941. // The index of the first vertex to render
  942. u32 vertex_offset;
  943. INSERT_PADDING_WORDS(0x3);
  944. // These two trigger rendering of triangles
  945. u32 trigger_draw;
  946. u32 trigger_draw_indexed;
  947. INSERT_PADDING_WORDS(0x2);
  948. // These registers are used to setup the default "fall-back" vertex shader attributes
  949. struct {
  950. // Index of the current default attribute
  951. u32 index;
  952. // Writing to these registers sets the "current" default attribute.
  953. u32 set_value[3];
  954. } vs_default_attributes_setup;
  955. INSERT_PADDING_WORDS(0x2);
  956. struct {
  957. // There are two channels that can be used to configure the next command buffer, which
  958. // can be then executed by writing to the "trigger" registers. There are two reasons why a
  959. // game might use this feature:
  960. // 1) With this, an arbitrary number of additional command buffers may be executed in
  961. // sequence without requiring any intervention of the CPU after the initial one is
  962. // kicked off.
  963. // 2) Games can configure these registers to provide a command list subroutine mechanism.
  964. BitField< 0, 20, u32> size[2]; ///< Size (in bytes / 8) of each channel's command buffer
  965. BitField< 0, 28, u32> addr[2]; ///< Physical address / 8 of each channel's command buffer
  966. u32 trigger[2]; ///< Triggers execution of the channel's command buffer when written to
  967. unsigned GetSize(unsigned index) const {
  968. ASSERT(index < 2);
  969. return 8 * size[index];
  970. }
  971. PAddr GetPhysicalAddress(unsigned index) const {
  972. ASSERT(index < 2);
  973. return (PAddr)(8 * addr[index]);
  974. }
  975. } command_buffer;
  976. INSERT_PADDING_WORDS(0x07);
  977. enum class GPUMode : u32 {
  978. Drawing = 0,
  979. Configuring = 1
  980. };
  981. GPUMode gpu_mode;
  982. INSERT_PADDING_WORDS(0x18);
  983. enum class TriangleTopology : u32 {
  984. List = 0,
  985. Strip = 1,
  986. Fan = 2,
  987. Shader = 3, // Programmable setup unit implemented in a geometry shader
  988. };
  989. BitField<8, 2, TriangleTopology> triangle_topology;
  990. u32 restart_primitive;
  991. INSERT_PADDING_WORDS(0x20);
  992. struct ShaderConfig {
  993. BitField<0, 16, u32> bool_uniforms;
  994. union {
  995. BitField< 0, 8, u32> x;
  996. BitField< 8, 8, u32> y;
  997. BitField<16, 8, u32> z;
  998. BitField<24, 8, u32> w;
  999. } int_uniforms[4];
  1000. INSERT_PADDING_WORDS(0x4);
  1001. union {
  1002. // Number of input attributes to shader unit - 1
  1003. BitField<0, 4, u32> num_input_attributes;
  1004. };
  1005. // Offset to shader program entry point (in words)
  1006. BitField<0, 16, u32> main_offset;
  1007. union {
  1008. BitField< 0, 4, u64> attribute0_register;
  1009. BitField< 4, 4, u64> attribute1_register;
  1010. BitField< 8, 4, u64> attribute2_register;
  1011. BitField<12, 4, u64> attribute3_register;
  1012. BitField<16, 4, u64> attribute4_register;
  1013. BitField<20, 4, u64> attribute5_register;
  1014. BitField<24, 4, u64> attribute6_register;
  1015. BitField<28, 4, u64> attribute7_register;
  1016. BitField<32, 4, u64> attribute8_register;
  1017. BitField<36, 4, u64> attribute9_register;
  1018. BitField<40, 4, u64> attribute10_register;
  1019. BitField<44, 4, u64> attribute11_register;
  1020. BitField<48, 4, u64> attribute12_register;
  1021. BitField<52, 4, u64> attribute13_register;
  1022. BitField<56, 4, u64> attribute14_register;
  1023. BitField<60, 4, u64> attribute15_register;
  1024. int GetRegisterForAttribute(int attribute_index) const {
  1025. u64 fields[] = {
  1026. attribute0_register, attribute1_register, attribute2_register, attribute3_register,
  1027. attribute4_register, attribute5_register, attribute6_register, attribute7_register,
  1028. attribute8_register, attribute9_register, attribute10_register, attribute11_register,
  1029. attribute12_register, attribute13_register, attribute14_register, attribute15_register,
  1030. };
  1031. return (int)fields[attribute_index];
  1032. }
  1033. } input_register_map;
  1034. BitField<0, 16, u32> output_mask;
  1035. // 0x28E, CODETRANSFER_END
  1036. INSERT_PADDING_WORDS(0x2);
  1037. struct {
  1038. enum Format : u32
  1039. {
  1040. FLOAT24 = 0,
  1041. FLOAT32 = 1
  1042. };
  1043. bool IsFloat32() const {
  1044. return format == FLOAT32;
  1045. }
  1046. union {
  1047. // Index of the next uniform to write to
  1048. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid indices
  1049. // TODO: Maybe the uppermost index is for the geometry shader? Investigate!
  1050. BitField<0, 7, u32> index;
  1051. BitField<31, 1, Format> format;
  1052. };
  1053. // Writing to these registers sets the current uniform.
  1054. u32 set_value[8];
  1055. } uniform_setup;
  1056. INSERT_PADDING_WORDS(0x2);
  1057. struct {
  1058. // Offset of the next instruction to write code to.
  1059. // Incremented with each instruction write.
  1060. u32 offset;
  1061. // Writing to these registers sets the "current" word in the shader program.
  1062. u32 set_word[8];
  1063. } program;
  1064. INSERT_PADDING_WORDS(0x1);
  1065. // This register group is used to load an internal table of swizzling patterns,
  1066. // which are indexed by each shader instruction to specify vector component swizzling.
  1067. struct {
  1068. // Offset of the next swizzle pattern to write code to.
  1069. // Incremented with each instruction write.
  1070. u32 offset;
  1071. // Writing to these registers sets the current swizzle pattern in the table.
  1072. u32 set_word[8];
  1073. } swizzle_patterns;
  1074. INSERT_PADDING_WORDS(0x2);
  1075. };
  1076. ShaderConfig gs;
  1077. ShaderConfig vs;
  1078. INSERT_PADDING_WORDS(0x20);
  1079. // Map register indices to names readable by humans
  1080. // Used for debugging purposes, so performance is not an issue here
  1081. static std::string GetCommandName(int index);
  1082. static constexpr size_t NumIds() {
  1083. return sizeof(Regs) / sizeof(u32);
  1084. }
  1085. const u32& operator [] (int index) const {
  1086. const u32* content = reinterpret_cast<const u32*>(this);
  1087. return content[index];
  1088. }
  1089. u32& operator [] (int index) {
  1090. u32* content = reinterpret_cast<u32*>(this);
  1091. return content[index];
  1092. }
  1093. private:
  1094. /*
  1095. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  1096. * This function should be used to get the address from a raw register value.
  1097. */
  1098. static inline u32 DecodeAddressRegister(u32 register_value) {
  1099. return register_value * 8;
  1100. }
  1101. };
  1102. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  1103. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  1104. // support for that.
  1105. #ifndef _MSC_VER
  1106. #define ASSERT_REG_POSITION(field_name, position) static_assert(offsetof(Regs, field_name) == position * 4, "Field "#field_name" has invalid position")
  1107. ASSERT_REG_POSITION(trigger_irq, 0x10);
  1108. ASSERT_REG_POSITION(cull_mode, 0x40);
  1109. ASSERT_REG_POSITION(viewport_size_x, 0x41);
  1110. ASSERT_REG_POSITION(viewport_size_y, 0x43);
  1111. ASSERT_REG_POSITION(viewport_depth_range, 0x4d);
  1112. ASSERT_REG_POSITION(viewport_depth_near_plane, 0x4e);
  1113. ASSERT_REG_POSITION(vs_output_attributes[0], 0x50);
  1114. ASSERT_REG_POSITION(vs_output_attributes[1], 0x51);
  1115. ASSERT_REG_POSITION(scissor_test, 0x65);
  1116. ASSERT_REG_POSITION(viewport_corner, 0x68);
  1117. ASSERT_REG_POSITION(depthmap_enable, 0x6D);
  1118. ASSERT_REG_POSITION(texture0_enable, 0x80);
  1119. ASSERT_REG_POSITION(texture0, 0x81);
  1120. ASSERT_REG_POSITION(texture0_format, 0x8e);
  1121. ASSERT_REG_POSITION(fragment_lighting_enable, 0x8f);
  1122. ASSERT_REG_POSITION(texture1, 0x91);
  1123. ASSERT_REG_POSITION(texture1_format, 0x96);
  1124. ASSERT_REG_POSITION(texture2, 0x99);
  1125. ASSERT_REG_POSITION(texture2_format, 0x9e);
  1126. ASSERT_REG_POSITION(tev_stage0, 0xc0);
  1127. ASSERT_REG_POSITION(tev_stage1, 0xc8);
  1128. ASSERT_REG_POSITION(tev_stage2, 0xd0);
  1129. ASSERT_REG_POSITION(tev_stage3, 0xd8);
  1130. ASSERT_REG_POSITION(tev_combiner_buffer_input, 0xe0);
  1131. ASSERT_REG_POSITION(fog_mode, 0xe0);
  1132. ASSERT_REG_POSITION(fog_color, 0xe1);
  1133. ASSERT_REG_POSITION(fog_lut_offset, 0xe6);
  1134. ASSERT_REG_POSITION(fog_lut_data, 0xe8);
  1135. ASSERT_REG_POSITION(tev_stage4, 0xf0);
  1136. ASSERT_REG_POSITION(tev_stage5, 0xf8);
  1137. ASSERT_REG_POSITION(tev_combiner_buffer_color, 0xfd);
  1138. ASSERT_REG_POSITION(output_merger, 0x100);
  1139. ASSERT_REG_POSITION(framebuffer, 0x110);
  1140. ASSERT_REG_POSITION(lighting, 0x140);
  1141. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  1142. ASSERT_REG_POSITION(index_array, 0x227);
  1143. ASSERT_REG_POSITION(num_vertices, 0x228);
  1144. ASSERT_REG_POSITION(vertex_offset, 0x22a);
  1145. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  1146. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  1147. ASSERT_REG_POSITION(vs_default_attributes_setup, 0x232);
  1148. ASSERT_REG_POSITION(command_buffer, 0x238);
  1149. ASSERT_REG_POSITION(gpu_mode, 0x245);
  1150. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  1151. ASSERT_REG_POSITION(restart_primitive, 0x25f);
  1152. ASSERT_REG_POSITION(gs, 0x280);
  1153. ASSERT_REG_POSITION(vs, 0x2b0);
  1154. #undef ASSERT_REG_POSITION
  1155. #endif // !defined(_MSC_VER)
  1156. static_assert(sizeof(Regs::ShaderConfig) == 0x30 * sizeof(u32), "ShaderConfig structure has incorrect size");
  1157. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value anyway.
  1158. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32), "Register set structure larger than it should be");
  1159. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32), "Register set structure smaller than it should be");
  1160. /// Initialize Pica state
  1161. void Init();
  1162. /// Shutdown Pica state
  1163. void Shutdown();
  1164. } // namespace