pica.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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/logging/log.h"
  16. #include "common/vector_math.h"
  17. #include "video_core/regs_rasterizer.h"
  18. #include "video_core/regs_texturing.h"
  19. namespace Pica {
  20. // Returns index corresponding to the Regs member labeled by field_name
  21. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  22. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  23. // For details cf.
  24. // https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  25. // Hopefully, this will be fixed sometime in the future.
  26. // For lack of better alternatives, we currently hardcode the offsets when constant
  27. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  28. // will then make sure the offsets indeed match the automatically calculated ones).
  29. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  30. #if defined(_MSC_VER)
  31. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  32. #else
  33. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  34. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  35. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  36. // field offset. Otherwise, the compiler will fail to compile this code.
  37. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  38. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), \
  39. size_t>::type)PICA_REG_INDEX(field_name))
  40. #endif // _MSC_VER
  41. struct Regs {
  42. INSERT_PADDING_WORDS(0x10);
  43. u32 trigger_irq;
  44. INSERT_PADDING_WORDS(0x2f);
  45. RasterizerRegs rasterizer;
  46. TexturingRegs texturing;
  47. enum class LogicOp : u32 {
  48. Clear = 0,
  49. And = 1,
  50. AndReverse = 2,
  51. Copy = 3,
  52. Set = 4,
  53. CopyInverted = 5,
  54. NoOp = 6,
  55. Invert = 7,
  56. Nand = 8,
  57. Or = 9,
  58. Nor = 10,
  59. Xor = 11,
  60. Equiv = 12,
  61. AndInverted = 13,
  62. OrReverse = 14,
  63. OrInverted = 15,
  64. };
  65. enum class BlendEquation : u32 {
  66. Add = 0,
  67. Subtract = 1,
  68. ReverseSubtract = 2,
  69. Min = 3,
  70. Max = 4,
  71. };
  72. enum class BlendFactor : u32 {
  73. Zero = 0,
  74. One = 1,
  75. SourceColor = 2,
  76. OneMinusSourceColor = 3,
  77. DestColor = 4,
  78. OneMinusDestColor = 5,
  79. SourceAlpha = 6,
  80. OneMinusSourceAlpha = 7,
  81. DestAlpha = 8,
  82. OneMinusDestAlpha = 9,
  83. ConstantColor = 10,
  84. OneMinusConstantColor = 11,
  85. ConstantAlpha = 12,
  86. OneMinusConstantAlpha = 13,
  87. SourceAlphaSaturate = 14,
  88. };
  89. enum class CompareFunc : u32 {
  90. Never = 0,
  91. Always = 1,
  92. Equal = 2,
  93. NotEqual = 3,
  94. LessThan = 4,
  95. LessThanOrEqual = 5,
  96. GreaterThan = 6,
  97. GreaterThanOrEqual = 7,
  98. };
  99. enum class StencilAction : u32 {
  100. Keep = 0,
  101. Zero = 1,
  102. Replace = 2,
  103. Increment = 3,
  104. Decrement = 4,
  105. Invert = 5,
  106. IncrementWrap = 6,
  107. DecrementWrap = 7,
  108. };
  109. struct {
  110. union {
  111. // If false, logic blending is used
  112. BitField<8, 1, u32> alphablend_enable;
  113. };
  114. union {
  115. BitField<0, 8, BlendEquation> blend_equation_rgb;
  116. BitField<8, 8, BlendEquation> blend_equation_a;
  117. BitField<16, 4, BlendFactor> factor_source_rgb;
  118. BitField<20, 4, BlendFactor> factor_dest_rgb;
  119. BitField<24, 4, BlendFactor> factor_source_a;
  120. BitField<28, 4, BlendFactor> factor_dest_a;
  121. } alpha_blending;
  122. union {
  123. BitField<0, 4, LogicOp> logic_op;
  124. };
  125. union {
  126. u32 raw;
  127. BitField<0, 8, u32> r;
  128. BitField<8, 8, u32> g;
  129. BitField<16, 8, u32> b;
  130. BitField<24, 8, u32> a;
  131. } blend_const;
  132. union {
  133. BitField<0, 1, u32> enable;
  134. BitField<4, 3, CompareFunc> func;
  135. BitField<8, 8, u32> ref;
  136. } alpha_test;
  137. struct {
  138. union {
  139. // Raw value of this register
  140. u32 raw_func;
  141. // If true, enable stencil testing
  142. BitField<0, 1, u32> enable;
  143. // Comparison operation for stencil testing
  144. BitField<4, 3, CompareFunc> func;
  145. // Mask used to control writing to the stencil buffer
  146. BitField<8, 8, u32> write_mask;
  147. // Value to compare against for stencil testing
  148. BitField<16, 8, u32> reference_value;
  149. // Mask to apply on stencil test inputs
  150. BitField<24, 8, u32> input_mask;
  151. };
  152. union {
  153. // Raw value of this register
  154. u32 raw_op;
  155. // Action to perform when the stencil test fails
  156. BitField<0, 3, StencilAction> action_stencil_fail;
  157. // Action to perform when stencil testing passed but depth testing fails
  158. BitField<4, 3, StencilAction> action_depth_fail;
  159. // Action to perform when both stencil and depth testing pass
  160. BitField<8, 3, StencilAction> action_depth_pass;
  161. };
  162. } stencil_test;
  163. union {
  164. BitField<0, 1, u32> depth_test_enable;
  165. BitField<4, 3, CompareFunc> depth_test_func;
  166. BitField<8, 1, u32> red_enable;
  167. BitField<9, 1, u32> green_enable;
  168. BitField<10, 1, u32> blue_enable;
  169. BitField<11, 1, u32> alpha_enable;
  170. BitField<12, 1, u32> depth_write_enable;
  171. };
  172. INSERT_PADDING_WORDS(0x8);
  173. } output_merger;
  174. // Components are laid out in reverse byte order, most significant bits first.
  175. enum class ColorFormat : u32 {
  176. RGBA8 = 0,
  177. RGB8 = 1,
  178. RGB5A1 = 2,
  179. RGB565 = 3,
  180. RGBA4 = 4,
  181. };
  182. enum class DepthFormat : u32 {
  183. D16 = 0,
  184. D24 = 2,
  185. D24S8 = 3,
  186. };
  187. // Returns the number of bytes in the specified color format
  188. static unsigned BytesPerColorPixel(ColorFormat format) {
  189. switch (format) {
  190. case ColorFormat::RGBA8:
  191. return 4;
  192. case ColorFormat::RGB8:
  193. return 3;
  194. case ColorFormat::RGB5A1:
  195. case ColorFormat::RGB565:
  196. case ColorFormat::RGBA4:
  197. return 2;
  198. default:
  199. LOG_CRITICAL(HW_GPU, "Unknown color format %u", format);
  200. UNIMPLEMENTED();
  201. }
  202. }
  203. struct FramebufferConfig {
  204. INSERT_PADDING_WORDS(0x3);
  205. union {
  206. BitField<0, 4, u32> allow_color_write; // 0 = disable, else enable
  207. };
  208. INSERT_PADDING_WORDS(0x1);
  209. union {
  210. BitField<0, 2, u32> allow_depth_stencil_write; // 0 = disable, else enable
  211. };
  212. DepthFormat depth_format; // TODO: Should be a BitField!
  213. BitField<16, 3, ColorFormat> color_format;
  214. INSERT_PADDING_WORDS(0x4);
  215. u32 depth_buffer_address;
  216. u32 color_buffer_address;
  217. union {
  218. // Apparently, the framebuffer width is stored as expected,
  219. // while the height is stored as the actual height minus one.
  220. // Hence, don't access these fields directly but use the accessors
  221. // GetWidth() and GetHeight() instead.
  222. BitField<0, 11, u32> width;
  223. BitField<12, 10, u32> height;
  224. };
  225. INSERT_PADDING_WORDS(0x1);
  226. inline u32 GetColorBufferPhysicalAddress() const {
  227. return DecodeAddressRegister(color_buffer_address);
  228. }
  229. inline u32 GetDepthBufferPhysicalAddress() const {
  230. return DecodeAddressRegister(depth_buffer_address);
  231. }
  232. inline u32 GetWidth() const {
  233. return width;
  234. }
  235. inline u32 GetHeight() const {
  236. return height + 1;
  237. }
  238. } framebuffer;
  239. // Returns the number of bytes in the specified depth format
  240. static u32 BytesPerDepthPixel(DepthFormat format) {
  241. switch (format) {
  242. case DepthFormat::D16:
  243. return 2;
  244. case DepthFormat::D24:
  245. return 3;
  246. case DepthFormat::D24S8:
  247. return 4;
  248. default:
  249. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  250. UNIMPLEMENTED();
  251. }
  252. }
  253. // Returns the number of bits per depth component of the specified depth format
  254. static u32 DepthBitsPerPixel(DepthFormat format) {
  255. switch (format) {
  256. case DepthFormat::D16:
  257. return 16;
  258. case DepthFormat::D24:
  259. case DepthFormat::D24S8:
  260. return 24;
  261. default:
  262. LOG_CRITICAL(HW_GPU, "Unknown depth format %u", format);
  263. UNIMPLEMENTED();
  264. }
  265. }
  266. INSERT_PADDING_WORDS(0x20);
  267. enum class LightingSampler {
  268. Distribution0 = 0,
  269. Distribution1 = 1,
  270. Fresnel = 3,
  271. ReflectBlue = 4,
  272. ReflectGreen = 5,
  273. ReflectRed = 6,
  274. SpotlightAttenuation = 8,
  275. DistanceAttenuation = 16,
  276. };
  277. /**
  278. * Pica fragment lighting supports using different LUTs for each lighting component:
  279. * Reflectance R, G, and B channels, distribution function for specular components 0 and 1,
  280. * fresnel factor, and spotlight attenuation. Furthermore, which LUTs are used for each channel
  281. * (or whether a channel is enabled at all) is specified by various pre-defined lighting
  282. * configurations. With configurations that require more LUTs, more cycles are required on HW to
  283. * perform lighting computations.
  284. */
  285. enum class LightingConfig {
  286. Config0 = 0, ///< Reflect Red, Distribution 0, Spotlight
  287. Config1 = 1, ///< Reflect Red, Fresnel, Spotlight
  288. Config2 = 2, ///< Reflect Red, Distribution 0/1
  289. Config3 = 3, ///< Distribution 0/1, Fresnel
  290. Config4 = 4, ///< Reflect Red/Green/Blue, Distribution 0/1, Spotlight
  291. Config5 = 5, ///< Reflect Red/Green/Blue, Distribution 0, Fresnel, Spotlight
  292. Config6 = 6, ///< Reflect Red, Distribution 0/1, Fresnel, Spotlight
  293. Config7 = 8, ///< Reflect Red/Green/Blue, Distribution 0/1, Fresnel, Spotlight
  294. ///< NOTE: '8' is intentional, '7' does not appear to be a valid configuration
  295. };
  296. /// Selects which lighting components are affected by fresnel
  297. enum class LightingFresnelSelector {
  298. None = 0, ///< Fresnel is disabled
  299. PrimaryAlpha = 1, ///< Primary (diffuse) lighting alpha is affected by fresnel
  300. SecondaryAlpha = 2, ///< Secondary (specular) lighting alpha is affected by fresnel
  301. Both =
  302. PrimaryAlpha |
  303. SecondaryAlpha, ///< Both primary and secondary lighting alphas are affected by fresnel
  304. };
  305. /// Factor used to scale the output of a lighting LUT
  306. enum class LightingScale {
  307. Scale1 = 0, ///< Scale is 1x
  308. Scale2 = 1, ///< Scale is 2x
  309. Scale4 = 2, ///< Scale is 4x
  310. Scale8 = 3, ///< Scale is 8x
  311. Scale1_4 = 6, ///< Scale is 0.25x
  312. Scale1_2 = 7, ///< Scale is 0.5x
  313. };
  314. enum class LightingLutInput {
  315. NH = 0, // Cosine of the angle between the normal and half-angle vectors
  316. VH = 1, // Cosine of the angle between the view and half-angle vectors
  317. NV = 2, // Cosine of the angle between the normal and the view vector
  318. LN = 3, // Cosine of the angle between the light and the normal vectors
  319. };
  320. enum class LightingBumpMode : u32 {
  321. None = 0,
  322. NormalMap = 1,
  323. TangentMap = 2,
  324. };
  325. union LightColor {
  326. BitField<0, 10, u32> b;
  327. BitField<10, 10, u32> g;
  328. BitField<20, 10, u32> r;
  329. Math::Vec3f ToVec3f() const {
  330. // These fields are 10 bits wide, however 255 corresponds to 1.0f for each color
  331. // component
  332. return Math::MakeVec((f32)r / 255.f, (f32)g / 255.f, (f32)b / 255.f);
  333. }
  334. };
  335. /// Returns true if the specified lighting sampler is supported by the current Pica lighting
  336. /// configuration
  337. static bool IsLightingSamplerSupported(LightingConfig config, LightingSampler sampler) {
  338. switch (sampler) {
  339. case LightingSampler::Distribution0:
  340. return (config != LightingConfig::Config1);
  341. case LightingSampler::Distribution1:
  342. return (config != LightingConfig::Config0) && (config != LightingConfig::Config1) &&
  343. (config != LightingConfig::Config5);
  344. case LightingSampler::Fresnel:
  345. return (config != LightingConfig::Config0) && (config != LightingConfig::Config2) &&
  346. (config != LightingConfig::Config4);
  347. case LightingSampler::ReflectRed:
  348. return (config != LightingConfig::Config3);
  349. case LightingSampler::ReflectGreen:
  350. case LightingSampler::ReflectBlue:
  351. return (config == LightingConfig::Config4) || (config == LightingConfig::Config5) ||
  352. (config == LightingConfig::Config7);
  353. default:
  354. UNREACHABLE_MSG("Regs::IsLightingSamplerSupported: Reached "
  355. "unreachable section, sampler should be one "
  356. "of Distribution0, Distribution1, Fresnel, "
  357. "ReflectRed, ReflectGreen or ReflectBlue, instead "
  358. "got %i",
  359. static_cast<int>(config));
  360. }
  361. }
  362. struct {
  363. struct LightSrc {
  364. LightColor specular_0; // material.specular_0 * light.specular_0
  365. LightColor specular_1; // material.specular_1 * light.specular_1
  366. LightColor diffuse; // material.diffuse * light.diffuse
  367. LightColor ambient; // material.ambient * light.ambient
  368. // Encoded as 16-bit floating point
  369. union {
  370. BitField<0, 16, u32> x;
  371. BitField<16, 16, u32> y;
  372. };
  373. union {
  374. BitField<0, 16, u32> z;
  375. };
  376. INSERT_PADDING_WORDS(0x3);
  377. union {
  378. BitField<0, 1, u32> directional;
  379. BitField<1, 1, u32> two_sided_diffuse; // When disabled, clamp dot-product to 0
  380. } config;
  381. BitField<0, 20, u32> dist_atten_bias;
  382. BitField<0, 20, u32> dist_atten_scale;
  383. INSERT_PADDING_WORDS(0x4);
  384. };
  385. static_assert(sizeof(LightSrc) == 0x10 * sizeof(u32),
  386. "LightSrc structure must be 0x10 words");
  387. LightSrc light[8];
  388. LightColor global_ambient; // Emission + (material.ambient * lighting.ambient)
  389. INSERT_PADDING_WORDS(0x1);
  390. BitField<0, 3, u32> max_light_index; // Number of enabled lights - 1
  391. union {
  392. BitField<2, 2, LightingFresnelSelector> fresnel_selector;
  393. BitField<4, 4, LightingConfig> config;
  394. BitField<22, 2, u32> bump_selector; // 0: Texture 0, 1: Texture 1, 2: Texture 2
  395. BitField<27, 1, u32> clamp_highlights;
  396. BitField<28, 2, LightingBumpMode> bump_mode;
  397. BitField<30, 1, u32> disable_bump_renorm;
  398. } config0;
  399. union {
  400. BitField<16, 1, u32> disable_lut_d0;
  401. BitField<17, 1, u32> disable_lut_d1;
  402. BitField<19, 1, u32> disable_lut_fr;
  403. BitField<20, 1, u32> disable_lut_rr;
  404. BitField<21, 1, u32> disable_lut_rg;
  405. BitField<22, 1, u32> disable_lut_rb;
  406. // Each bit specifies whether distance attenuation should be applied for the
  407. // corresponding light
  408. BitField<24, 1, u32> disable_dist_atten_light_0;
  409. BitField<25, 1, u32> disable_dist_atten_light_1;
  410. BitField<26, 1, u32> disable_dist_atten_light_2;
  411. BitField<27, 1, u32> disable_dist_atten_light_3;
  412. BitField<28, 1, u32> disable_dist_atten_light_4;
  413. BitField<29, 1, u32> disable_dist_atten_light_5;
  414. BitField<30, 1, u32> disable_dist_atten_light_6;
  415. BitField<31, 1, u32> disable_dist_atten_light_7;
  416. } config1;
  417. bool IsDistAttenDisabled(unsigned index) const {
  418. const unsigned disable[] = {
  419. config1.disable_dist_atten_light_0, config1.disable_dist_atten_light_1,
  420. config1.disable_dist_atten_light_2, config1.disable_dist_atten_light_3,
  421. config1.disable_dist_atten_light_4, config1.disable_dist_atten_light_5,
  422. config1.disable_dist_atten_light_6, config1.disable_dist_atten_light_7};
  423. return disable[index] != 0;
  424. }
  425. union {
  426. BitField<0, 8, u32> index; ///< Index at which to set data in the LUT
  427. BitField<8, 5, u32> type; ///< Type of LUT for which to set data
  428. } lut_config;
  429. BitField<0, 1, u32> disable;
  430. INSERT_PADDING_WORDS(0x1);
  431. // When data is written to any of these registers, it gets written to the lookup table of
  432. // the selected type at the selected index, specified above in the `lut_config` register.
  433. // With each write, `lut_config.index` is incremented. It does not matter which of these
  434. // registers is written to, the behavior will be the same.
  435. u32 lut_data[8];
  436. // These are used to specify if absolute (abs) value should be used for each LUT index. When
  437. // abs mode is disabled, LUT indexes are in the range of (-1.0, 1.0). Otherwise, they are in
  438. // the range of (0.0, 1.0).
  439. union {
  440. BitField<1, 1, u32> disable_d0;
  441. BitField<5, 1, u32> disable_d1;
  442. BitField<9, 1, u32> disable_sp;
  443. BitField<13, 1, u32> disable_fr;
  444. BitField<17, 1, u32> disable_rb;
  445. BitField<21, 1, u32> disable_rg;
  446. BitField<25, 1, u32> disable_rr;
  447. } abs_lut_input;
  448. union {
  449. BitField<0, 3, LightingLutInput> d0;
  450. BitField<4, 3, LightingLutInput> d1;
  451. BitField<8, 3, LightingLutInput> sp;
  452. BitField<12, 3, LightingLutInput> fr;
  453. BitField<16, 3, LightingLutInput> rb;
  454. BitField<20, 3, LightingLutInput> rg;
  455. BitField<24, 3, LightingLutInput> rr;
  456. } lut_input;
  457. union {
  458. BitField<0, 3, LightingScale> d0;
  459. BitField<4, 3, LightingScale> d1;
  460. BitField<8, 3, LightingScale> sp;
  461. BitField<12, 3, LightingScale> fr;
  462. BitField<16, 3, LightingScale> rb;
  463. BitField<20, 3, LightingScale> rg;
  464. BitField<24, 3, LightingScale> rr;
  465. static float GetScale(LightingScale scale) {
  466. switch (scale) {
  467. case LightingScale::Scale1:
  468. return 1.0f;
  469. case LightingScale::Scale2:
  470. return 2.0f;
  471. case LightingScale::Scale4:
  472. return 4.0f;
  473. case LightingScale::Scale8:
  474. return 8.0f;
  475. case LightingScale::Scale1_4:
  476. return 0.25f;
  477. case LightingScale::Scale1_2:
  478. return 0.5f;
  479. }
  480. return 0.0f;
  481. }
  482. } lut_scale;
  483. INSERT_PADDING_WORDS(0x6);
  484. union {
  485. // There are 8 light enable "slots", corresponding to the total number of lights
  486. // supported by Pica. For N enabled lights (specified by register 0x1c2, or 'src_num'
  487. // above), the first N slots below will be set to integers within the range of 0-7,
  488. // corresponding to the actual light that is enabled for each slot.
  489. BitField<0, 3, u32> slot_0;
  490. BitField<4, 3, u32> slot_1;
  491. BitField<8, 3, u32> slot_2;
  492. BitField<12, 3, u32> slot_3;
  493. BitField<16, 3, u32> slot_4;
  494. BitField<20, 3, u32> slot_5;
  495. BitField<24, 3, u32> slot_6;
  496. BitField<28, 3, u32> slot_7;
  497. unsigned GetNum(unsigned index) const {
  498. const unsigned enable_slots[] = {slot_0, slot_1, slot_2, slot_3,
  499. slot_4, slot_5, slot_6, slot_7};
  500. return enable_slots[index];
  501. }
  502. } light_enable;
  503. } lighting;
  504. INSERT_PADDING_WORDS(0x26);
  505. enum class VertexAttributeFormat : u64 {
  506. BYTE = 0,
  507. UBYTE = 1,
  508. SHORT = 2,
  509. FLOAT = 3,
  510. };
  511. struct {
  512. BitField<0, 29, u32> base_address;
  513. u32 GetPhysicalBaseAddress() const {
  514. return DecodeAddressRegister(base_address);
  515. }
  516. // Descriptor for internal vertex attributes
  517. union {
  518. BitField<0, 2, VertexAttributeFormat> format0; // size of one element
  519. BitField<2, 2, u64> size0; // number of elements minus 1
  520. BitField<4, 2, VertexAttributeFormat> format1;
  521. BitField<6, 2, u64> size1;
  522. BitField<8, 2, VertexAttributeFormat> format2;
  523. BitField<10, 2, u64> size2;
  524. BitField<12, 2, VertexAttributeFormat> format3;
  525. BitField<14, 2, u64> size3;
  526. BitField<16, 2, VertexAttributeFormat> format4;
  527. BitField<18, 2, u64> size4;
  528. BitField<20, 2, VertexAttributeFormat> format5;
  529. BitField<22, 2, u64> size5;
  530. BitField<24, 2, VertexAttributeFormat> format6;
  531. BitField<26, 2, u64> size6;
  532. BitField<28, 2, VertexAttributeFormat> format7;
  533. BitField<30, 2, u64> size7;
  534. BitField<32, 2, VertexAttributeFormat> format8;
  535. BitField<34, 2, u64> size8;
  536. BitField<36, 2, VertexAttributeFormat> format9;
  537. BitField<38, 2, u64> size9;
  538. BitField<40, 2, VertexAttributeFormat> format10;
  539. BitField<42, 2, u64> size10;
  540. BitField<44, 2, VertexAttributeFormat> format11;
  541. BitField<46, 2, u64> size11;
  542. BitField<48, 12, u64> attribute_mask;
  543. // number of total attributes minus 1
  544. BitField<60, 4, u64> max_attribute_index;
  545. };
  546. inline VertexAttributeFormat GetFormat(int n) const {
  547. VertexAttributeFormat formats[] = {format0, format1, format2, format3,
  548. format4, format5, format6, format7,
  549. format8, format9, format10, format11};
  550. return formats[n];
  551. }
  552. inline int GetNumElements(int n) const {
  553. u64 sizes[] = {size0, size1, size2, size3, size4, size5,
  554. size6, size7, size8, size9, size10, size11};
  555. return (int)sizes[n] + 1;
  556. }
  557. inline int GetElementSizeInBytes(int n) const {
  558. return (GetFormat(n) == VertexAttributeFormat::FLOAT)
  559. ? 4
  560. : (GetFormat(n) == VertexAttributeFormat::SHORT) ? 2 : 1;
  561. }
  562. inline int GetStride(int n) const {
  563. return GetNumElements(n) * GetElementSizeInBytes(n);
  564. }
  565. inline bool IsDefaultAttribute(int id) const {
  566. return (id >= 12) || (attribute_mask & (1ULL << id)) != 0;
  567. }
  568. inline int GetNumTotalAttributes() const {
  569. return (int)max_attribute_index + 1;
  570. }
  571. // Attribute loaders map the source vertex data to input attributes
  572. // This e.g. allows to load different attributes from different memory locations
  573. struct {
  574. // Source attribute data offset from the base address
  575. u32 data_offset;
  576. union {
  577. BitField<0, 4, u64> comp0;
  578. BitField<4, 4, u64> comp1;
  579. BitField<8, 4, u64> comp2;
  580. BitField<12, 4, u64> comp3;
  581. BitField<16, 4, u64> comp4;
  582. BitField<20, 4, u64> comp5;
  583. BitField<24, 4, u64> comp6;
  584. BitField<28, 4, u64> comp7;
  585. BitField<32, 4, u64> comp8;
  586. BitField<36, 4, u64> comp9;
  587. BitField<40, 4, u64> comp10;
  588. BitField<44, 4, u64> comp11;
  589. // bytes for a single vertex in this loader
  590. BitField<48, 8, u64> byte_count;
  591. BitField<60, 4, u64> component_count;
  592. };
  593. inline int GetComponent(int n) const {
  594. u64 components[] = {comp0, comp1, comp2, comp3, comp4, comp5,
  595. comp6, comp7, comp8, comp9, comp10, comp11};
  596. return (int)components[n];
  597. }
  598. } attribute_loaders[12];
  599. } vertex_attributes;
  600. struct {
  601. enum IndexFormat : u32 {
  602. BYTE = 0,
  603. SHORT = 1,
  604. };
  605. union {
  606. BitField<0, 31, u32> offset; // relative to base attribute address
  607. BitField<31, 1, IndexFormat> format;
  608. };
  609. } index_array;
  610. // Number of vertices to render
  611. u32 num_vertices;
  612. INSERT_PADDING_WORDS(0x1);
  613. // The index of the first vertex to render
  614. u32 vertex_offset;
  615. INSERT_PADDING_WORDS(0x3);
  616. // These two trigger rendering of triangles
  617. u32 trigger_draw;
  618. u32 trigger_draw_indexed;
  619. INSERT_PADDING_WORDS(0x2);
  620. // These registers are used to setup the default "fall-back" vertex shader attributes
  621. struct {
  622. // Index of the current default attribute
  623. u32 index;
  624. // Writing to these registers sets the "current" default attribute.
  625. u32 set_value[3];
  626. } vs_default_attributes_setup;
  627. INSERT_PADDING_WORDS(0x2);
  628. struct {
  629. // There are two channels that can be used to configure the next command buffer, which
  630. // can be then executed by writing to the "trigger" registers. There are two reasons why a
  631. // game might use this feature:
  632. // 1) With this, an arbitrary number of additional command buffers may be executed in
  633. // sequence without requiring any intervention of the CPU after the initial one is
  634. // kicked off.
  635. // 2) Games can configure these registers to provide a command list subroutine mechanism.
  636. BitField<0, 20, u32> size[2]; ///< Size (in bytes / 8) of each channel's command buffer
  637. BitField<0, 28, u32> addr[2]; ///< Physical address / 8 of each channel's command buffer
  638. u32 trigger[2]; ///< Triggers execution of the channel's command buffer when written to
  639. unsigned GetSize(unsigned index) const {
  640. ASSERT(index < 2);
  641. return 8 * size[index];
  642. }
  643. PAddr GetPhysicalAddress(unsigned index) const {
  644. ASSERT(index < 2);
  645. return (PAddr)(8 * addr[index]);
  646. }
  647. } command_buffer;
  648. INSERT_PADDING_WORDS(4);
  649. /// Number of input attributes to the vertex shader minus 1
  650. BitField<0, 4, u32> max_input_attrib_index;
  651. INSERT_PADDING_WORDS(2);
  652. enum class GPUMode : u32 {
  653. Drawing = 0,
  654. Configuring = 1,
  655. };
  656. GPUMode gpu_mode;
  657. INSERT_PADDING_WORDS(0x18);
  658. enum class TriangleTopology : u32 {
  659. List = 0,
  660. Strip = 1,
  661. Fan = 2,
  662. Shader = 3, // Programmable setup unit implemented in a geometry shader
  663. };
  664. BitField<8, 2, TriangleTopology> triangle_topology;
  665. u32 restart_primitive;
  666. INSERT_PADDING_WORDS(0x20);
  667. struct ShaderConfig {
  668. BitField<0, 16, u32> bool_uniforms;
  669. union {
  670. BitField<0, 8, u32> x;
  671. BitField<8, 8, u32> y;
  672. BitField<16, 8, u32> z;
  673. BitField<24, 8, u32> w;
  674. } int_uniforms[4];
  675. INSERT_PADDING_WORDS(0x4);
  676. union {
  677. // Number of input attributes to shader unit - 1
  678. BitField<0, 4, u32> max_input_attribute_index;
  679. };
  680. // Offset to shader program entry point (in words)
  681. BitField<0, 16, u32> main_offset;
  682. /// Maps input attributes to registers. 4-bits per attribute, specifying a register index
  683. u32 input_attribute_to_register_map_low;
  684. u32 input_attribute_to_register_map_high;
  685. unsigned int GetRegisterForAttribute(unsigned int attribute_index) const {
  686. u64 map = ((u64)input_attribute_to_register_map_high << 32) |
  687. (u64)input_attribute_to_register_map_low;
  688. return (map >> (attribute_index * 4)) & 0b1111;
  689. }
  690. BitField<0, 16, u32> output_mask;
  691. // 0x28E, CODETRANSFER_END
  692. INSERT_PADDING_WORDS(0x2);
  693. struct {
  694. enum Format : u32 {
  695. FLOAT24 = 0,
  696. FLOAT32 = 1,
  697. };
  698. bool IsFloat32() const {
  699. return format == FLOAT32;
  700. }
  701. union {
  702. // Index of the next uniform to write to
  703. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid
  704. // indices
  705. // TODO: Maybe the uppermost index is for the geometry shader? Investigate!
  706. BitField<0, 7, u32> index;
  707. BitField<31, 1, Format> format;
  708. };
  709. // Writing to these registers sets the current uniform.
  710. u32 set_value[8];
  711. } uniform_setup;
  712. INSERT_PADDING_WORDS(0x2);
  713. struct {
  714. // Offset of the next instruction to write code to.
  715. // Incremented with each instruction write.
  716. u32 offset;
  717. // Writing to these registers sets the "current" word in the shader program.
  718. u32 set_word[8];
  719. } program;
  720. INSERT_PADDING_WORDS(0x1);
  721. // This register group is used to load an internal table of swizzling patterns,
  722. // which are indexed by each shader instruction to specify vector component swizzling.
  723. struct {
  724. // Offset of the next swizzle pattern to write code to.
  725. // Incremented with each instruction write.
  726. u32 offset;
  727. // Writing to these registers sets the current swizzle pattern in the table.
  728. u32 set_word[8];
  729. } swizzle_patterns;
  730. INSERT_PADDING_WORDS(0x2);
  731. };
  732. ShaderConfig gs;
  733. ShaderConfig vs;
  734. INSERT_PADDING_WORDS(0x20);
  735. // Map register indices to names readable by humans
  736. // Used for debugging purposes, so performance is not an issue here
  737. static std::string GetCommandName(int index);
  738. static constexpr size_t NumIds() {
  739. return sizeof(Regs) / sizeof(u32);
  740. }
  741. const u32& operator[](int index) const {
  742. const u32* content = reinterpret_cast<const u32*>(this);
  743. return content[index];
  744. }
  745. u32& operator[](int index) {
  746. u32* content = reinterpret_cast<u32*>(this);
  747. return content[index];
  748. }
  749. private:
  750. /*
  751. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  752. * This function should be used to get the address from a raw register value.
  753. */
  754. static inline u32 DecodeAddressRegister(u32 register_value) {
  755. return register_value * 8;
  756. }
  757. };
  758. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  759. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  760. // support for that.
  761. #ifndef _MSC_VER
  762. #define ASSERT_REG_POSITION(field_name, position) \
  763. static_assert(offsetof(Regs, field_name) == position * 4, \
  764. "Field " #field_name " has invalid position")
  765. ASSERT_REG_POSITION(trigger_irq, 0x10);
  766. ASSERT_REG_POSITION(rasterizer, 0x40);
  767. ASSERT_REG_POSITION(rasterizer.cull_mode, 0x40);
  768. ASSERT_REG_POSITION(rasterizer.viewport_size_x, 0x41);
  769. ASSERT_REG_POSITION(rasterizer.viewport_size_y, 0x43);
  770. ASSERT_REG_POSITION(rasterizer.viewport_depth_range, 0x4d);
  771. ASSERT_REG_POSITION(rasterizer.viewport_depth_near_plane, 0x4e);
  772. ASSERT_REG_POSITION(rasterizer.vs_output_attributes[0], 0x50);
  773. ASSERT_REG_POSITION(rasterizer.vs_output_attributes[1], 0x51);
  774. ASSERT_REG_POSITION(rasterizer.scissor_test, 0x65);
  775. ASSERT_REG_POSITION(rasterizer.viewport_corner, 0x68);
  776. ASSERT_REG_POSITION(rasterizer.depthmap_enable, 0x6D);
  777. ASSERT_REG_POSITION(texturing, 0x80);
  778. ASSERT_REG_POSITION(texturing.texture0_enable, 0x80);
  779. ASSERT_REG_POSITION(texturing.texture0, 0x81);
  780. ASSERT_REG_POSITION(texturing.texture0_format, 0x8e);
  781. ASSERT_REG_POSITION(texturing.fragment_lighting_enable, 0x8f);
  782. ASSERT_REG_POSITION(texturing.texture1, 0x91);
  783. ASSERT_REG_POSITION(texturing.texture1_format, 0x96);
  784. ASSERT_REG_POSITION(texturing.texture2, 0x99);
  785. ASSERT_REG_POSITION(texturing.texture2_format, 0x9e);
  786. ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0);
  787. ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8);
  788. ASSERT_REG_POSITION(texturing.tev_stage2, 0xd0);
  789. ASSERT_REG_POSITION(texturing.tev_stage3, 0xd8);
  790. ASSERT_REG_POSITION(texturing.tev_combiner_buffer_input, 0xe0);
  791. ASSERT_REG_POSITION(texturing.fog_mode, 0xe0);
  792. ASSERT_REG_POSITION(texturing.fog_color, 0xe1);
  793. ASSERT_REG_POSITION(texturing.fog_lut_offset, 0xe6);
  794. ASSERT_REG_POSITION(texturing.fog_lut_data, 0xe8);
  795. ASSERT_REG_POSITION(texturing.tev_stage4, 0xf0);
  796. ASSERT_REG_POSITION(texturing.tev_stage5, 0xf8);
  797. ASSERT_REG_POSITION(texturing.tev_combiner_buffer_color, 0xfd);
  798. ASSERT_REG_POSITION(output_merger, 0x100);
  799. ASSERT_REG_POSITION(framebuffer, 0x110);
  800. ASSERT_REG_POSITION(lighting, 0x140);
  801. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  802. ASSERT_REG_POSITION(index_array, 0x227);
  803. ASSERT_REG_POSITION(num_vertices, 0x228);
  804. ASSERT_REG_POSITION(vertex_offset, 0x22a);
  805. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  806. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  807. ASSERT_REG_POSITION(vs_default_attributes_setup, 0x232);
  808. ASSERT_REG_POSITION(command_buffer, 0x238);
  809. ASSERT_REG_POSITION(gpu_mode, 0x245);
  810. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  811. ASSERT_REG_POSITION(restart_primitive, 0x25f);
  812. ASSERT_REG_POSITION(gs, 0x280);
  813. ASSERT_REG_POSITION(vs, 0x2b0);
  814. #undef ASSERT_REG_POSITION
  815. #endif // !defined(_MSC_VER)
  816. static_assert(sizeof(Regs::ShaderConfig) == 0x30 * sizeof(u32),
  817. "ShaderConfig structure has incorrect size");
  818. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value
  819. // anyway.
  820. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32),
  821. "Register set structure larger than it should be");
  822. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32),
  823. "Register set structure smaller than it should be");
  824. /// Initialize Pica state
  825. void Init();
  826. /// Shutdown Pica state
  827. void Shutdown();
  828. } // namespace