pica.h 36 KB

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