pica.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 <string>
  8. #ifndef _MSC_VER
  9. #include <type_traits> // for std::enable_if
  10. #endif
  11. #include "common/assert.h"
  12. #include "common/bit_field.h"
  13. #include "common/common_funcs.h"
  14. #include "common/common_types.h"
  15. #include "common/logging/log.h"
  16. #include "common/vector_math.h"
  17. #include "video_core/regs_framebuffer.h"
  18. #include "video_core/regs_rasterizer.h"
  19. #include "video_core/regs_texturing.h"
  20. namespace Pica {
  21. // Returns index corresponding to the Regs member labeled by field_name
  22. // TODO: Due to Visual studio bug 209229, offsetof does not return constant expressions
  23. // when used with array elements (e.g. PICA_REG_INDEX(vs_uniform_setup.set_value[1])).
  24. // For details cf.
  25. // https://connect.microsoft.com/VisualStudio/feedback/details/209229/offsetof-does-not-produce-a-constant-expression-for-array-members
  26. // Hopefully, this will be fixed sometime in the future.
  27. // For lack of better alternatives, we currently hardcode the offsets when constant
  28. // expressions are needed via PICA_REG_INDEX_WORKAROUND (on sane compilers, static_asserts
  29. // will then make sure the offsets indeed match the automatically calculated ones).
  30. #define PICA_REG_INDEX(field_name) (offsetof(Pica::Regs, field_name) / sizeof(u32))
  31. #if defined(_MSC_VER)
  32. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) (backup_workaround_index)
  33. #else
  34. // NOTE: Yeah, hacking in a static_assert here just to workaround the lacking MSVC compiler
  35. // really is this annoying. This macro just forwards its first argument to PICA_REG_INDEX
  36. // and then performs a (no-op) cast to size_t iff the second argument matches the expected
  37. // field offset. Otherwise, the compiler will fail to compile this code.
  38. #define PICA_REG_INDEX_WORKAROUND(field_name, backup_workaround_index) \
  39. ((typename std::enable_if<backup_workaround_index == PICA_REG_INDEX(field_name), \
  40. size_t>::type)PICA_REG_INDEX(field_name))
  41. #endif // _MSC_VER
  42. struct Regs {
  43. INSERT_PADDING_WORDS(0x10);
  44. u32 trigger_irq;
  45. INSERT_PADDING_WORDS(0x2f);
  46. RasterizerRegs rasterizer;
  47. TexturingRegs texturing;
  48. FramebufferRegs framebuffer;
  49. enum class LightingSampler {
  50. Distribution0 = 0,
  51. Distribution1 = 1,
  52. Fresnel = 3,
  53. ReflectBlue = 4,
  54. ReflectGreen = 5,
  55. ReflectRed = 6,
  56. SpotlightAttenuation = 8,
  57. DistanceAttenuation = 16,
  58. };
  59. /**
  60. * Pica fragment lighting supports using different LUTs for each lighting component:
  61. * Reflectance R, G, and B channels, distribution function for specular components 0 and 1,
  62. * fresnel factor, and spotlight attenuation. Furthermore, which LUTs are used for each channel
  63. * (or whether a channel is enabled at all) is specified by various pre-defined lighting
  64. * configurations. With configurations that require more LUTs, more cycles are required on HW to
  65. * perform lighting computations.
  66. */
  67. enum class LightingConfig {
  68. Config0 = 0, ///< Reflect Red, Distribution 0, Spotlight
  69. Config1 = 1, ///< Reflect Red, Fresnel, Spotlight
  70. Config2 = 2, ///< Reflect Red, Distribution 0/1
  71. Config3 = 3, ///< Distribution 0/1, Fresnel
  72. Config4 = 4, ///< Reflect Red/Green/Blue, Distribution 0/1, Spotlight
  73. Config5 = 5, ///< Reflect Red/Green/Blue, Distribution 0, Fresnel, Spotlight
  74. Config6 = 6, ///< Reflect Red, Distribution 0/1, Fresnel, Spotlight
  75. Config7 = 8, ///< Reflect Red/Green/Blue, Distribution 0/1, Fresnel, Spotlight
  76. ///< NOTE: '8' is intentional, '7' does not appear to be a valid configuration
  77. };
  78. /// Selects which lighting components are affected by fresnel
  79. enum class LightingFresnelSelector {
  80. None = 0, ///< Fresnel is disabled
  81. PrimaryAlpha = 1, ///< Primary (diffuse) lighting alpha is affected by fresnel
  82. SecondaryAlpha = 2, ///< Secondary (specular) lighting alpha is affected by fresnel
  83. Both =
  84. PrimaryAlpha |
  85. SecondaryAlpha, ///< Both primary and secondary lighting alphas are affected by fresnel
  86. };
  87. /// Factor used to scale the output of a lighting LUT
  88. enum class LightingScale {
  89. Scale1 = 0, ///< Scale is 1x
  90. Scale2 = 1, ///< Scale is 2x
  91. Scale4 = 2, ///< Scale is 4x
  92. Scale8 = 3, ///< Scale is 8x
  93. Scale1_4 = 6, ///< Scale is 0.25x
  94. Scale1_2 = 7, ///< Scale is 0.5x
  95. };
  96. enum class LightingLutInput {
  97. NH = 0, // Cosine of the angle between the normal and half-angle vectors
  98. VH = 1, // Cosine of the angle between the view and half-angle vectors
  99. NV = 2, // Cosine of the angle between the normal and the view vector
  100. LN = 3, // Cosine of the angle between the light and the normal vectors
  101. };
  102. enum class LightingBumpMode : u32 {
  103. None = 0,
  104. NormalMap = 1,
  105. TangentMap = 2,
  106. };
  107. union LightColor {
  108. BitField<0, 10, u32> b;
  109. BitField<10, 10, u32> g;
  110. BitField<20, 10, u32> r;
  111. Math::Vec3f ToVec3f() const {
  112. // These fields are 10 bits wide, however 255 corresponds to 1.0f for each color
  113. // component
  114. return Math::MakeVec((f32)r / 255.f, (f32)g / 255.f, (f32)b / 255.f);
  115. }
  116. };
  117. /// Returns true if the specified lighting sampler is supported by the current Pica lighting
  118. /// configuration
  119. static bool IsLightingSamplerSupported(LightingConfig config, LightingSampler sampler) {
  120. switch (sampler) {
  121. case LightingSampler::Distribution0:
  122. return (config != LightingConfig::Config1);
  123. case LightingSampler::Distribution1:
  124. return (config != LightingConfig::Config0) && (config != LightingConfig::Config1) &&
  125. (config != LightingConfig::Config5);
  126. case LightingSampler::Fresnel:
  127. return (config != LightingConfig::Config0) && (config != LightingConfig::Config2) &&
  128. (config != LightingConfig::Config4);
  129. case LightingSampler::ReflectRed:
  130. return (config != LightingConfig::Config3);
  131. case LightingSampler::ReflectGreen:
  132. case LightingSampler::ReflectBlue:
  133. return (config == LightingConfig::Config4) || (config == LightingConfig::Config5) ||
  134. (config == LightingConfig::Config7);
  135. default:
  136. UNREACHABLE_MSG("Regs::IsLightingSamplerSupported: Reached "
  137. "unreachable section, sampler should be one "
  138. "of Distribution0, Distribution1, Fresnel, "
  139. "ReflectRed, ReflectGreen or ReflectBlue, instead "
  140. "got %i",
  141. static_cast<int>(config));
  142. }
  143. }
  144. struct {
  145. struct LightSrc {
  146. LightColor specular_0; // material.specular_0 * light.specular_0
  147. LightColor specular_1; // material.specular_1 * light.specular_1
  148. LightColor diffuse; // material.diffuse * light.diffuse
  149. LightColor ambient; // material.ambient * light.ambient
  150. // Encoded as 16-bit floating point
  151. union {
  152. BitField<0, 16, u32> x;
  153. BitField<16, 16, u32> y;
  154. };
  155. union {
  156. BitField<0, 16, u32> z;
  157. };
  158. INSERT_PADDING_WORDS(0x3);
  159. union {
  160. BitField<0, 1, u32> directional;
  161. BitField<1, 1, u32> two_sided_diffuse; // When disabled, clamp dot-product to 0
  162. } config;
  163. BitField<0, 20, u32> dist_atten_bias;
  164. BitField<0, 20, u32> dist_atten_scale;
  165. INSERT_PADDING_WORDS(0x4);
  166. };
  167. static_assert(sizeof(LightSrc) == 0x10 * sizeof(u32),
  168. "LightSrc structure must be 0x10 words");
  169. LightSrc light[8];
  170. LightColor global_ambient; // Emission + (material.ambient * lighting.ambient)
  171. INSERT_PADDING_WORDS(0x1);
  172. BitField<0, 3, u32> max_light_index; // Number of enabled lights - 1
  173. union {
  174. BitField<2, 2, LightingFresnelSelector> fresnel_selector;
  175. BitField<4, 4, LightingConfig> config;
  176. BitField<22, 2, u32> bump_selector; // 0: Texture 0, 1: Texture 1, 2: Texture 2
  177. BitField<27, 1, u32> clamp_highlights;
  178. BitField<28, 2, LightingBumpMode> bump_mode;
  179. BitField<30, 1, u32> disable_bump_renorm;
  180. } config0;
  181. union {
  182. BitField<16, 1, u32> disable_lut_d0;
  183. BitField<17, 1, u32> disable_lut_d1;
  184. BitField<19, 1, u32> disable_lut_fr;
  185. BitField<20, 1, u32> disable_lut_rr;
  186. BitField<21, 1, u32> disable_lut_rg;
  187. BitField<22, 1, u32> disable_lut_rb;
  188. // Each bit specifies whether distance attenuation should be applied for the
  189. // corresponding light
  190. BitField<24, 1, u32> disable_dist_atten_light_0;
  191. BitField<25, 1, u32> disable_dist_atten_light_1;
  192. BitField<26, 1, u32> disable_dist_atten_light_2;
  193. BitField<27, 1, u32> disable_dist_atten_light_3;
  194. BitField<28, 1, u32> disable_dist_atten_light_4;
  195. BitField<29, 1, u32> disable_dist_atten_light_5;
  196. BitField<30, 1, u32> disable_dist_atten_light_6;
  197. BitField<31, 1, u32> disable_dist_atten_light_7;
  198. } config1;
  199. bool IsDistAttenDisabled(unsigned index) const {
  200. const unsigned disable[] = {
  201. config1.disable_dist_atten_light_0, config1.disable_dist_atten_light_1,
  202. config1.disable_dist_atten_light_2, config1.disable_dist_atten_light_3,
  203. config1.disable_dist_atten_light_4, config1.disable_dist_atten_light_5,
  204. config1.disable_dist_atten_light_6, config1.disable_dist_atten_light_7};
  205. return disable[index] != 0;
  206. }
  207. union {
  208. BitField<0, 8, u32> index; ///< Index at which to set data in the LUT
  209. BitField<8, 5, u32> type; ///< Type of LUT for which to set data
  210. } lut_config;
  211. BitField<0, 1, u32> disable;
  212. INSERT_PADDING_WORDS(0x1);
  213. // When data is written to any of these registers, it gets written to the lookup table of
  214. // the selected type at the selected index, specified above in the `lut_config` register.
  215. // With each write, `lut_config.index` is incremented. It does not matter which of these
  216. // registers is written to, the behavior will be the same.
  217. u32 lut_data[8];
  218. // These are used to specify if absolute (abs) value should be used for each LUT index. When
  219. // abs mode is disabled, LUT indexes are in the range of (-1.0, 1.0). Otherwise, they are in
  220. // the range of (0.0, 1.0).
  221. union {
  222. BitField<1, 1, u32> disable_d0;
  223. BitField<5, 1, u32> disable_d1;
  224. BitField<9, 1, u32> disable_sp;
  225. BitField<13, 1, u32> disable_fr;
  226. BitField<17, 1, u32> disable_rb;
  227. BitField<21, 1, u32> disable_rg;
  228. BitField<25, 1, u32> disable_rr;
  229. } abs_lut_input;
  230. union {
  231. BitField<0, 3, LightingLutInput> d0;
  232. BitField<4, 3, LightingLutInput> d1;
  233. BitField<8, 3, LightingLutInput> sp;
  234. BitField<12, 3, LightingLutInput> fr;
  235. BitField<16, 3, LightingLutInput> rb;
  236. BitField<20, 3, LightingLutInput> rg;
  237. BitField<24, 3, LightingLutInput> rr;
  238. } lut_input;
  239. union {
  240. BitField<0, 3, LightingScale> d0;
  241. BitField<4, 3, LightingScale> d1;
  242. BitField<8, 3, LightingScale> sp;
  243. BitField<12, 3, LightingScale> fr;
  244. BitField<16, 3, LightingScale> rb;
  245. BitField<20, 3, LightingScale> rg;
  246. BitField<24, 3, LightingScale> rr;
  247. static float GetScale(LightingScale scale) {
  248. switch (scale) {
  249. case LightingScale::Scale1:
  250. return 1.0f;
  251. case LightingScale::Scale2:
  252. return 2.0f;
  253. case LightingScale::Scale4:
  254. return 4.0f;
  255. case LightingScale::Scale8:
  256. return 8.0f;
  257. case LightingScale::Scale1_4:
  258. return 0.25f;
  259. case LightingScale::Scale1_2:
  260. return 0.5f;
  261. }
  262. return 0.0f;
  263. }
  264. } lut_scale;
  265. INSERT_PADDING_WORDS(0x6);
  266. union {
  267. // There are 8 light enable "slots", corresponding to the total number of lights
  268. // supported by Pica. For N enabled lights (specified by register 0x1c2, or 'src_num'
  269. // above), the first N slots below will be set to integers within the range of 0-7,
  270. // corresponding to the actual light that is enabled for each slot.
  271. BitField<0, 3, u32> slot_0;
  272. BitField<4, 3, u32> slot_1;
  273. BitField<8, 3, u32> slot_2;
  274. BitField<12, 3, u32> slot_3;
  275. BitField<16, 3, u32> slot_4;
  276. BitField<20, 3, u32> slot_5;
  277. BitField<24, 3, u32> slot_6;
  278. BitField<28, 3, u32> slot_7;
  279. unsigned GetNum(unsigned index) const {
  280. const unsigned enable_slots[] = {slot_0, slot_1, slot_2, slot_3,
  281. slot_4, slot_5, slot_6, slot_7};
  282. return enable_slots[index];
  283. }
  284. } light_enable;
  285. } lighting;
  286. INSERT_PADDING_WORDS(0x26);
  287. enum class VertexAttributeFormat : u64 {
  288. BYTE = 0,
  289. UBYTE = 1,
  290. SHORT = 2,
  291. FLOAT = 3,
  292. };
  293. struct {
  294. BitField<0, 29, u32> base_address;
  295. u32 GetPhysicalBaseAddress() const {
  296. return DecodeAddressRegister(base_address);
  297. }
  298. // Descriptor for internal vertex attributes
  299. union {
  300. BitField<0, 2, VertexAttributeFormat> format0; // size of one element
  301. BitField<2, 2, u64> size0; // number of elements minus 1
  302. BitField<4, 2, VertexAttributeFormat> format1;
  303. BitField<6, 2, u64> size1;
  304. BitField<8, 2, VertexAttributeFormat> format2;
  305. BitField<10, 2, u64> size2;
  306. BitField<12, 2, VertexAttributeFormat> format3;
  307. BitField<14, 2, u64> size3;
  308. BitField<16, 2, VertexAttributeFormat> format4;
  309. BitField<18, 2, u64> size4;
  310. BitField<20, 2, VertexAttributeFormat> format5;
  311. BitField<22, 2, u64> size5;
  312. BitField<24, 2, VertexAttributeFormat> format6;
  313. BitField<26, 2, u64> size6;
  314. BitField<28, 2, VertexAttributeFormat> format7;
  315. BitField<30, 2, u64> size7;
  316. BitField<32, 2, VertexAttributeFormat> format8;
  317. BitField<34, 2, u64> size8;
  318. BitField<36, 2, VertexAttributeFormat> format9;
  319. BitField<38, 2, u64> size9;
  320. BitField<40, 2, VertexAttributeFormat> format10;
  321. BitField<42, 2, u64> size10;
  322. BitField<44, 2, VertexAttributeFormat> format11;
  323. BitField<46, 2, u64> size11;
  324. BitField<48, 12, u64> attribute_mask;
  325. // number of total attributes minus 1
  326. BitField<60, 4, u64> max_attribute_index;
  327. };
  328. inline VertexAttributeFormat GetFormat(int n) const {
  329. VertexAttributeFormat formats[] = {format0, format1, format2, format3,
  330. format4, format5, format6, format7,
  331. format8, format9, format10, format11};
  332. return formats[n];
  333. }
  334. inline int GetNumElements(int n) const {
  335. u64 sizes[] = {size0, size1, size2, size3, size4, size5,
  336. size6, size7, size8, size9, size10, size11};
  337. return (int)sizes[n] + 1;
  338. }
  339. inline int GetElementSizeInBytes(int n) const {
  340. return (GetFormat(n) == VertexAttributeFormat::FLOAT)
  341. ? 4
  342. : (GetFormat(n) == VertexAttributeFormat::SHORT) ? 2 : 1;
  343. }
  344. inline int GetStride(int n) const {
  345. return GetNumElements(n) * GetElementSizeInBytes(n);
  346. }
  347. inline bool IsDefaultAttribute(int id) const {
  348. return (id >= 12) || (attribute_mask & (1ULL << id)) != 0;
  349. }
  350. inline int GetNumTotalAttributes() const {
  351. return (int)max_attribute_index + 1;
  352. }
  353. // Attribute loaders map the source vertex data to input attributes
  354. // This e.g. allows to load different attributes from different memory locations
  355. struct {
  356. // Source attribute data offset from the base address
  357. u32 data_offset;
  358. union {
  359. BitField<0, 4, u64> comp0;
  360. BitField<4, 4, u64> comp1;
  361. BitField<8, 4, u64> comp2;
  362. BitField<12, 4, u64> comp3;
  363. BitField<16, 4, u64> comp4;
  364. BitField<20, 4, u64> comp5;
  365. BitField<24, 4, u64> comp6;
  366. BitField<28, 4, u64> comp7;
  367. BitField<32, 4, u64> comp8;
  368. BitField<36, 4, u64> comp9;
  369. BitField<40, 4, u64> comp10;
  370. BitField<44, 4, u64> comp11;
  371. // bytes for a single vertex in this loader
  372. BitField<48, 8, u64> byte_count;
  373. BitField<60, 4, u64> component_count;
  374. };
  375. inline int GetComponent(int n) const {
  376. u64 components[] = {comp0, comp1, comp2, comp3, comp4, comp5,
  377. comp6, comp7, comp8, comp9, comp10, comp11};
  378. return (int)components[n];
  379. }
  380. } attribute_loaders[12];
  381. } vertex_attributes;
  382. struct {
  383. enum IndexFormat : u32 {
  384. BYTE = 0,
  385. SHORT = 1,
  386. };
  387. union {
  388. BitField<0, 31, u32> offset; // relative to base attribute address
  389. BitField<31, 1, IndexFormat> format;
  390. };
  391. } index_array;
  392. // Number of vertices to render
  393. u32 num_vertices;
  394. INSERT_PADDING_WORDS(0x1);
  395. // The index of the first vertex to render
  396. u32 vertex_offset;
  397. INSERT_PADDING_WORDS(0x3);
  398. // These two trigger rendering of triangles
  399. u32 trigger_draw;
  400. u32 trigger_draw_indexed;
  401. INSERT_PADDING_WORDS(0x2);
  402. // These registers are used to setup the default "fall-back" vertex shader attributes
  403. struct {
  404. // Index of the current default attribute
  405. u32 index;
  406. // Writing to these registers sets the "current" default attribute.
  407. u32 set_value[3];
  408. } vs_default_attributes_setup;
  409. INSERT_PADDING_WORDS(0x2);
  410. struct {
  411. // There are two channels that can be used to configure the next command buffer, which
  412. // can be then executed by writing to the "trigger" registers. There are two reasons why a
  413. // game might use this feature:
  414. // 1) With this, an arbitrary number of additional command buffers may be executed in
  415. // sequence without requiring any intervention of the CPU after the initial one is
  416. // kicked off.
  417. // 2) Games can configure these registers to provide a command list subroutine mechanism.
  418. BitField<0, 20, u32> size[2]; ///< Size (in bytes / 8) of each channel's command buffer
  419. BitField<0, 28, u32> addr[2]; ///< Physical address / 8 of each channel's command buffer
  420. u32 trigger[2]; ///< Triggers execution of the channel's command buffer when written to
  421. unsigned GetSize(unsigned index) const {
  422. ASSERT(index < 2);
  423. return 8 * size[index];
  424. }
  425. PAddr GetPhysicalAddress(unsigned index) const {
  426. ASSERT(index < 2);
  427. return (PAddr)(8 * addr[index]);
  428. }
  429. } command_buffer;
  430. INSERT_PADDING_WORDS(4);
  431. /// Number of input attributes to the vertex shader minus 1
  432. BitField<0, 4, u32> max_input_attrib_index;
  433. INSERT_PADDING_WORDS(2);
  434. enum class GPUMode : u32 {
  435. Drawing = 0,
  436. Configuring = 1,
  437. };
  438. GPUMode gpu_mode;
  439. INSERT_PADDING_WORDS(0x18);
  440. enum class TriangleTopology : u32 {
  441. List = 0,
  442. Strip = 1,
  443. Fan = 2,
  444. Shader = 3, // Programmable setup unit implemented in a geometry shader
  445. };
  446. BitField<8, 2, TriangleTopology> triangle_topology;
  447. u32 restart_primitive;
  448. INSERT_PADDING_WORDS(0x20);
  449. struct ShaderConfig {
  450. BitField<0, 16, u32> bool_uniforms;
  451. union {
  452. BitField<0, 8, u32> x;
  453. BitField<8, 8, u32> y;
  454. BitField<16, 8, u32> z;
  455. BitField<24, 8, u32> w;
  456. } int_uniforms[4];
  457. INSERT_PADDING_WORDS(0x4);
  458. union {
  459. // Number of input attributes to shader unit - 1
  460. BitField<0, 4, u32> max_input_attribute_index;
  461. };
  462. // Offset to shader program entry point (in words)
  463. BitField<0, 16, u32> main_offset;
  464. /// Maps input attributes to registers. 4-bits per attribute, specifying a register index
  465. u32 input_attribute_to_register_map_low;
  466. u32 input_attribute_to_register_map_high;
  467. unsigned int GetRegisterForAttribute(unsigned int attribute_index) const {
  468. u64 map = ((u64)input_attribute_to_register_map_high << 32) |
  469. (u64)input_attribute_to_register_map_low;
  470. return (map >> (attribute_index * 4)) & 0b1111;
  471. }
  472. BitField<0, 16, u32> output_mask;
  473. // 0x28E, CODETRANSFER_END
  474. INSERT_PADDING_WORDS(0x2);
  475. struct {
  476. enum Format : u32 {
  477. FLOAT24 = 0,
  478. FLOAT32 = 1,
  479. };
  480. bool IsFloat32() const {
  481. return format == FLOAT32;
  482. }
  483. union {
  484. // Index of the next uniform to write to
  485. // TODO: ctrulib uses 8 bits for this, however that seems to yield lots of invalid
  486. // indices
  487. // TODO: Maybe the uppermost index is for the geometry shader? Investigate!
  488. BitField<0, 7, u32> index;
  489. BitField<31, 1, Format> format;
  490. };
  491. // Writing to these registers sets the current uniform.
  492. u32 set_value[8];
  493. } uniform_setup;
  494. INSERT_PADDING_WORDS(0x2);
  495. struct {
  496. // Offset of the next instruction to write code to.
  497. // Incremented with each instruction write.
  498. u32 offset;
  499. // Writing to these registers sets the "current" word in the shader program.
  500. u32 set_word[8];
  501. } program;
  502. INSERT_PADDING_WORDS(0x1);
  503. // This register group is used to load an internal table of swizzling patterns,
  504. // which are indexed by each shader instruction to specify vector component swizzling.
  505. struct {
  506. // Offset of the next swizzle pattern to write code to.
  507. // Incremented with each instruction write.
  508. u32 offset;
  509. // Writing to these registers sets the current swizzle pattern in the table.
  510. u32 set_word[8];
  511. } swizzle_patterns;
  512. INSERT_PADDING_WORDS(0x2);
  513. };
  514. ShaderConfig gs;
  515. ShaderConfig vs;
  516. INSERT_PADDING_WORDS(0x20);
  517. // Map register indices to names readable by humans
  518. // Used for debugging purposes, so performance is not an issue here
  519. static std::string GetCommandName(int index);
  520. static constexpr size_t NumIds() {
  521. return sizeof(Regs) / sizeof(u32);
  522. }
  523. const u32& operator[](int index) const {
  524. const u32* content = reinterpret_cast<const u32*>(this);
  525. return content[index];
  526. }
  527. u32& operator[](int index) {
  528. u32* content = reinterpret_cast<u32*>(this);
  529. return content[index];
  530. }
  531. private:
  532. /*
  533. * Most physical addresses which Pica registers refer to are 8-byte aligned.
  534. * This function should be used to get the address from a raw register value.
  535. */
  536. static inline u32 DecodeAddressRegister(u32 register_value) {
  537. return register_value * 8;
  538. }
  539. };
  540. // TODO: MSVC does not support using offsetof() on non-static data members even though this
  541. // is technically allowed since C++11. This macro should be enabled once MSVC adds
  542. // support for that.
  543. #ifndef _MSC_VER
  544. #define ASSERT_REG_POSITION(field_name, position) \
  545. static_assert(offsetof(Regs, field_name) == position * 4, \
  546. "Field " #field_name " has invalid position")
  547. ASSERT_REG_POSITION(trigger_irq, 0x10);
  548. ASSERT_REG_POSITION(rasterizer, 0x40);
  549. ASSERT_REG_POSITION(rasterizer.cull_mode, 0x40);
  550. ASSERT_REG_POSITION(rasterizer.viewport_size_x, 0x41);
  551. ASSERT_REG_POSITION(rasterizer.viewport_size_y, 0x43);
  552. ASSERT_REG_POSITION(rasterizer.viewport_depth_range, 0x4d);
  553. ASSERT_REG_POSITION(rasterizer.viewport_depth_near_plane, 0x4e);
  554. ASSERT_REG_POSITION(rasterizer.vs_output_attributes[0], 0x50);
  555. ASSERT_REG_POSITION(rasterizer.vs_output_attributes[1], 0x51);
  556. ASSERT_REG_POSITION(rasterizer.scissor_test, 0x65);
  557. ASSERT_REG_POSITION(rasterizer.viewport_corner, 0x68);
  558. ASSERT_REG_POSITION(rasterizer.depthmap_enable, 0x6D);
  559. ASSERT_REG_POSITION(texturing, 0x80);
  560. ASSERT_REG_POSITION(texturing.texture0_enable, 0x80);
  561. ASSERT_REG_POSITION(texturing.texture0, 0x81);
  562. ASSERT_REG_POSITION(texturing.texture0_format, 0x8e);
  563. ASSERT_REG_POSITION(texturing.fragment_lighting_enable, 0x8f);
  564. ASSERT_REG_POSITION(texturing.texture1, 0x91);
  565. ASSERT_REG_POSITION(texturing.texture1_format, 0x96);
  566. ASSERT_REG_POSITION(texturing.texture2, 0x99);
  567. ASSERT_REG_POSITION(texturing.texture2_format, 0x9e);
  568. ASSERT_REG_POSITION(texturing.tev_stage0, 0xc0);
  569. ASSERT_REG_POSITION(texturing.tev_stage1, 0xc8);
  570. ASSERT_REG_POSITION(texturing.tev_stage2, 0xd0);
  571. ASSERT_REG_POSITION(texturing.tev_stage3, 0xd8);
  572. ASSERT_REG_POSITION(texturing.tev_combiner_buffer_input, 0xe0);
  573. ASSERT_REG_POSITION(texturing.fog_mode, 0xe0);
  574. ASSERT_REG_POSITION(texturing.fog_color, 0xe1);
  575. ASSERT_REG_POSITION(texturing.fog_lut_offset, 0xe6);
  576. ASSERT_REG_POSITION(texturing.fog_lut_data, 0xe8);
  577. ASSERT_REG_POSITION(texturing.tev_stage4, 0xf0);
  578. ASSERT_REG_POSITION(texturing.tev_stage5, 0xf8);
  579. ASSERT_REG_POSITION(texturing.tev_combiner_buffer_color, 0xfd);
  580. ASSERT_REG_POSITION(framebuffer, 0x100);
  581. ASSERT_REG_POSITION(framebuffer.output_merger, 0x100);
  582. ASSERT_REG_POSITION(framebuffer.framebuffer, 0x110);
  583. ASSERT_REG_POSITION(lighting, 0x140);
  584. ASSERT_REG_POSITION(vertex_attributes, 0x200);
  585. ASSERT_REG_POSITION(index_array, 0x227);
  586. ASSERT_REG_POSITION(num_vertices, 0x228);
  587. ASSERT_REG_POSITION(vertex_offset, 0x22a);
  588. ASSERT_REG_POSITION(trigger_draw, 0x22e);
  589. ASSERT_REG_POSITION(trigger_draw_indexed, 0x22f);
  590. ASSERT_REG_POSITION(vs_default_attributes_setup, 0x232);
  591. ASSERT_REG_POSITION(command_buffer, 0x238);
  592. ASSERT_REG_POSITION(gpu_mode, 0x245);
  593. ASSERT_REG_POSITION(triangle_topology, 0x25e);
  594. ASSERT_REG_POSITION(restart_primitive, 0x25f);
  595. ASSERT_REG_POSITION(gs, 0x280);
  596. ASSERT_REG_POSITION(vs, 0x2b0);
  597. #undef ASSERT_REG_POSITION
  598. #endif // !defined(_MSC_VER)
  599. static_assert(sizeof(Regs::ShaderConfig) == 0x30 * sizeof(u32),
  600. "ShaderConfig structure has incorrect size");
  601. // The total number of registers is chosen arbitrarily, but let's make sure it's not some odd value
  602. // anyway.
  603. static_assert(sizeof(Regs) <= 0x300 * sizeof(u32),
  604. "Register set structure larger than it should be");
  605. static_assert(sizeof(Regs) >= 0x300 * sizeof(u32),
  606. "Register set structure smaller than it should be");
  607. /// Initialize Pica state
  608. void Init();
  609. /// Shutdown Pica state
  610. void Shutdown();
  611. } // namespace