pica.h 35 KB

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