pica.h 37 KB

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