pica.h 40 KB

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