pica.h 32 KB

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