pica.h 32 KB

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