pica.h 34 KB

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