maxwell_3d.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // Copyright 2018 yuzu 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 <unordered_map>
  7. #include <vector>
  8. #include "common/assert.h"
  9. #include "common/bit_field.h"
  10. #include "common/common_funcs.h"
  11. #include "common/common_types.h"
  12. #include "common/math_util.h"
  13. #include "video_core/gpu.h"
  14. #include "video_core/macro_interpreter.h"
  15. #include "video_core/memory_manager.h"
  16. #include "video_core/textures/texture.h"
  17. namespace VideoCore {
  18. class RasterizerInterface;
  19. }
  20. namespace Tegra::Engines {
  21. #define MAXWELL3D_REG_INDEX(field_name) \
  22. (offsetof(Tegra::Engines::Maxwell3D::Regs, field_name) / sizeof(u32))
  23. class Maxwell3D final {
  24. public:
  25. explicit Maxwell3D(VideoCore::RasterizerInterface& rasterizer, MemoryManager& memory_manager);
  26. ~Maxwell3D() = default;
  27. /// Register structure of the Maxwell3D engine.
  28. /// TODO(Subv): This structure will need to be made bigger as more registers are discovered.
  29. struct Regs {
  30. static constexpr std::size_t NUM_REGS = 0xE00;
  31. static constexpr std::size_t NumRenderTargets = 8;
  32. static constexpr std::size_t NumViewports = 16;
  33. static constexpr std::size_t NumCBData = 16;
  34. static constexpr std::size_t NumVertexArrays = 32;
  35. static constexpr std::size_t NumVertexAttributes = 32;
  36. static constexpr std::size_t NumTextureSamplers = 32;
  37. static constexpr std::size_t MaxShaderProgram = 6;
  38. static constexpr std::size_t MaxShaderStage = 5;
  39. // Maximum number of const buffers per shader stage.
  40. static constexpr std::size_t MaxConstBuffers = 18;
  41. enum class QueryMode : u32 {
  42. Write = 0,
  43. Sync = 1,
  44. // TODO(Subv): It is currently unknown what the difference between method 2 and method 0
  45. // is.
  46. Write2 = 2,
  47. };
  48. enum class QueryUnit : u32 {
  49. VFetch = 1,
  50. VP = 2,
  51. Rast = 4,
  52. StrmOut = 5,
  53. GP = 6,
  54. ZCull = 7,
  55. Prop = 10,
  56. Crop = 15,
  57. };
  58. enum class QuerySelect : u32 {
  59. Zero = 0,
  60. };
  61. enum class QuerySyncCondition : u32 {
  62. NotEqual = 0,
  63. GreaterThan = 1,
  64. };
  65. enum class ShaderProgram : u32 {
  66. VertexA = 0,
  67. VertexB = 1,
  68. TesselationControl = 2,
  69. TesselationEval = 3,
  70. Geometry = 4,
  71. Fragment = 5,
  72. };
  73. enum class ShaderStage : u32 {
  74. Vertex = 0,
  75. TesselationControl = 1,
  76. TesselationEval = 2,
  77. Geometry = 3,
  78. Fragment = 4,
  79. };
  80. struct VertexAttribute {
  81. enum class Size : u32 {
  82. Invalid = 0x0,
  83. Size_32_32_32_32 = 0x01,
  84. Size_32_32_32 = 0x02,
  85. Size_16_16_16_16 = 0x03,
  86. Size_32_32 = 0x04,
  87. Size_16_16_16 = 0x05,
  88. Size_8_8_8_8 = 0x0a,
  89. Size_16_16 = 0x0f,
  90. Size_32 = 0x12,
  91. Size_8_8_8 = 0x13,
  92. Size_8_8 = 0x18,
  93. Size_16 = 0x1b,
  94. Size_8 = 0x1d,
  95. Size_10_10_10_2 = 0x30,
  96. Size_11_11_10 = 0x31,
  97. };
  98. enum class Type : u32 {
  99. SignedNorm = 1,
  100. UnsignedNorm = 2,
  101. SignedInt = 3,
  102. UnsignedInt = 4,
  103. UnsignedScaled = 5,
  104. SignedScaled = 6,
  105. Float = 7,
  106. };
  107. union {
  108. BitField<0, 5, u32> buffer;
  109. BitField<6, 1, u32> constant;
  110. BitField<7, 14, u32> offset;
  111. BitField<21, 6, Size> size;
  112. BitField<27, 3, Type> type;
  113. BitField<31, 1, u32> bgra;
  114. u32 hex;
  115. };
  116. u32 ComponentCount() const {
  117. switch (size) {
  118. case Size::Size_32_32_32_32:
  119. return 4;
  120. case Size::Size_32_32_32:
  121. return 3;
  122. case Size::Size_16_16_16_16:
  123. return 4;
  124. case Size::Size_32_32:
  125. return 2;
  126. case Size::Size_16_16_16:
  127. return 3;
  128. case Size::Size_8_8_8_8:
  129. return 4;
  130. case Size::Size_16_16:
  131. return 2;
  132. case Size::Size_32:
  133. return 1;
  134. case Size::Size_8_8_8:
  135. return 3;
  136. case Size::Size_8_8:
  137. return 2;
  138. case Size::Size_16:
  139. return 1;
  140. case Size::Size_8:
  141. return 1;
  142. case Size::Size_10_10_10_2:
  143. return 4;
  144. case Size::Size_11_11_10:
  145. return 3;
  146. default:
  147. UNREACHABLE();
  148. }
  149. }
  150. u32 SizeInBytes() const {
  151. switch (size) {
  152. case Size::Size_32_32_32_32:
  153. return 16;
  154. case Size::Size_32_32_32:
  155. return 12;
  156. case Size::Size_16_16_16_16:
  157. return 8;
  158. case Size::Size_32_32:
  159. return 8;
  160. case Size::Size_16_16_16:
  161. return 6;
  162. case Size::Size_8_8_8_8:
  163. return 4;
  164. case Size::Size_16_16:
  165. return 4;
  166. case Size::Size_32:
  167. return 4;
  168. case Size::Size_8_8_8:
  169. return 3;
  170. case Size::Size_8_8:
  171. return 2;
  172. case Size::Size_16:
  173. return 2;
  174. case Size::Size_8:
  175. return 1;
  176. case Size::Size_10_10_10_2:
  177. return 4;
  178. case Size::Size_11_11_10:
  179. return 4;
  180. default:
  181. UNREACHABLE();
  182. }
  183. }
  184. std::string SizeString() const {
  185. switch (size) {
  186. case Size::Size_32_32_32_32:
  187. return "32_32_32_32";
  188. case Size::Size_32_32_32:
  189. return "32_32_32";
  190. case Size::Size_16_16_16_16:
  191. return "16_16_16_16";
  192. case Size::Size_32_32:
  193. return "32_32";
  194. case Size::Size_16_16_16:
  195. return "16_16_16";
  196. case Size::Size_8_8_8_8:
  197. return "8_8_8_8";
  198. case Size::Size_16_16:
  199. return "16_16";
  200. case Size::Size_32:
  201. return "32";
  202. case Size::Size_8_8_8:
  203. return "8_8_8";
  204. case Size::Size_8_8:
  205. return "8_8";
  206. case Size::Size_16:
  207. return "16";
  208. case Size::Size_8:
  209. return "8";
  210. case Size::Size_10_10_10_2:
  211. return "10_10_10_2";
  212. case Size::Size_11_11_10:
  213. return "11_11_10";
  214. }
  215. UNREACHABLE();
  216. return {};
  217. }
  218. std::string TypeString() const {
  219. switch (type) {
  220. case Type::SignedNorm:
  221. return "SNORM";
  222. case Type::UnsignedNorm:
  223. return "UNORM";
  224. case Type::SignedInt:
  225. return "SINT";
  226. case Type::UnsignedInt:
  227. return "UINT";
  228. case Type::UnsignedScaled:
  229. return "USCALED";
  230. case Type::SignedScaled:
  231. return "SSCALED";
  232. case Type::Float:
  233. return "FLOAT";
  234. }
  235. UNREACHABLE();
  236. return {};
  237. }
  238. bool IsNormalized() const {
  239. return (type == Type::SignedNorm) || (type == Type::UnsignedNorm);
  240. }
  241. bool IsValid() const {
  242. return size != Size::Invalid;
  243. }
  244. bool operator<(const VertexAttribute& other) const {
  245. return hex < other.hex;
  246. }
  247. };
  248. enum class PrimitiveTopology : u32 {
  249. Points = 0x0,
  250. Lines = 0x1,
  251. LineLoop = 0x2,
  252. LineStrip = 0x3,
  253. Triangles = 0x4,
  254. TriangleStrip = 0x5,
  255. TriangleFan = 0x6,
  256. Quads = 0x7,
  257. QuadStrip = 0x8,
  258. Polygon = 0x9,
  259. LinesAdjacency = 0xa,
  260. LineStripAdjacency = 0xb,
  261. TrianglesAdjacency = 0xc,
  262. TriangleStripAdjacency = 0xd,
  263. Patches = 0xe,
  264. };
  265. enum class IndexFormat : u32 {
  266. UnsignedByte = 0x0,
  267. UnsignedShort = 0x1,
  268. UnsignedInt = 0x2,
  269. };
  270. enum class ComparisonOp : u32 {
  271. // These values are used by Nouveau and most games, they correspond to the OpenGL token
  272. // values for these operations.
  273. Never = 0x200,
  274. Less = 0x201,
  275. Equal = 0x202,
  276. LessEqual = 0x203,
  277. Greater = 0x204,
  278. NotEqual = 0x205,
  279. GreaterEqual = 0x206,
  280. Always = 0x207,
  281. // These values are used by some games, they seem to be NV04 values.
  282. NeverOld = 1,
  283. LessOld = 2,
  284. EqualOld = 3,
  285. LessEqualOld = 4,
  286. GreaterOld = 5,
  287. NotEqualOld = 6,
  288. GreaterEqualOld = 7,
  289. AlwaysOld = 8,
  290. };
  291. enum class LogicOperation : u32 {
  292. Clear = 0x1500,
  293. And = 0x1501,
  294. AndReverse = 0x1502,
  295. Copy = 0x1503,
  296. AndInverted = 0x1504,
  297. NoOp = 0x1505,
  298. Xor = 0x1506,
  299. Or = 0x1507,
  300. Nor = 0x1508,
  301. Equiv = 0x1509,
  302. Invert = 0x150A,
  303. OrReverse = 0x150B,
  304. CopyInverted = 0x150C,
  305. OrInverted = 0x150D,
  306. Nand = 0x150E,
  307. Set = 0x150F,
  308. };
  309. enum class StencilOp : u32 {
  310. Keep = 1,
  311. Zero = 2,
  312. Replace = 3,
  313. Incr = 4,
  314. Decr = 5,
  315. Invert = 6,
  316. IncrWrap = 7,
  317. DecrWrap = 8,
  318. KeepOGL = 0x1E00,
  319. ZeroOGL = 0,
  320. ReplaceOGL = 0x1E01,
  321. IncrOGL = 0x1E02,
  322. DecrOGL = 0x1E03,
  323. InvertOGL = 0x150A,
  324. IncrWrapOGL = 0x8507,
  325. DecrWrapOGL = 0x8508,
  326. };
  327. enum class MemoryLayout : u32 {
  328. Linear = 0,
  329. BlockLinear = 1,
  330. };
  331. enum class InvMemoryLayout : u32 {
  332. BlockLinear = 0,
  333. Linear = 1,
  334. };
  335. struct Cull {
  336. enum class FrontFace : u32 {
  337. ClockWise = 0x0900,
  338. CounterClockWise = 0x0901,
  339. };
  340. enum class CullFace : u32 {
  341. Front = 0x0404,
  342. Back = 0x0405,
  343. FrontAndBack = 0x0408,
  344. };
  345. u32 enabled;
  346. FrontFace front_face;
  347. CullFace cull_face;
  348. };
  349. struct Blend {
  350. enum class Equation : u32 {
  351. Add = 1,
  352. Subtract = 2,
  353. ReverseSubtract = 3,
  354. Min = 4,
  355. Max = 5,
  356. // These values are used by Nouveau and some games.
  357. AddGL = 0x8006,
  358. SubtractGL = 0x8007,
  359. ReverseSubtractGL = 0x8008,
  360. MinGL = 0x800a,
  361. MaxGL = 0x800b
  362. };
  363. enum class Factor : u32 {
  364. Zero = 0x1,
  365. One = 0x2,
  366. SourceColor = 0x3,
  367. OneMinusSourceColor = 0x4,
  368. SourceAlpha = 0x5,
  369. OneMinusSourceAlpha = 0x6,
  370. DestAlpha = 0x7,
  371. OneMinusDestAlpha = 0x8,
  372. DestColor = 0x9,
  373. OneMinusDestColor = 0xa,
  374. SourceAlphaSaturate = 0xb,
  375. Source1Color = 0x10,
  376. OneMinusSource1Color = 0x11,
  377. Source1Alpha = 0x12,
  378. OneMinusSource1Alpha = 0x13,
  379. ConstantColor = 0x61,
  380. OneMinusConstantColor = 0x62,
  381. ConstantAlpha = 0x63,
  382. OneMinusConstantAlpha = 0x64,
  383. // These values are used by Nouveau and some games.
  384. ZeroGL = 0x4000,
  385. OneGL = 0x4001,
  386. SourceColorGL = 0x4300,
  387. OneMinusSourceColorGL = 0x4301,
  388. SourceAlphaGL = 0x4302,
  389. OneMinusSourceAlphaGL = 0x4303,
  390. DestAlphaGL = 0x4304,
  391. OneMinusDestAlphaGL = 0x4305,
  392. DestColorGL = 0x4306,
  393. OneMinusDestColorGL = 0x4307,
  394. SourceAlphaSaturateGL = 0x4308,
  395. ConstantColorGL = 0xc001,
  396. OneMinusConstantColorGL = 0xc002,
  397. ConstantAlphaGL = 0xc003,
  398. OneMinusConstantAlphaGL = 0xc004,
  399. Source1ColorGL = 0xc900,
  400. OneMinusSource1ColorGL = 0xc901,
  401. Source1AlphaGL = 0xc902,
  402. OneMinusSource1AlphaGL = 0xc903,
  403. };
  404. u32 separate_alpha;
  405. Equation equation_rgb;
  406. Factor factor_source_rgb;
  407. Factor factor_dest_rgb;
  408. Equation equation_a;
  409. Factor factor_source_a;
  410. Factor factor_dest_a;
  411. INSERT_PADDING_WORDS(1);
  412. };
  413. struct RenderTargetConfig {
  414. u32 address_high;
  415. u32 address_low;
  416. u32 width;
  417. u32 height;
  418. Tegra::RenderTargetFormat format;
  419. union {
  420. BitField<0, 3, u32> block_width;
  421. BitField<4, 3, u32> block_height;
  422. BitField<8, 3, u32> block_depth;
  423. BitField<12, 1, InvMemoryLayout> type;
  424. } memory_layout;
  425. union {
  426. BitField<0, 16, u32> array_mode;
  427. BitField<16, 1, u32> volume;
  428. };
  429. u32 layer_stride;
  430. u32 base_layer;
  431. INSERT_PADDING_WORDS(7);
  432. GPUVAddr Address() const {
  433. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  434. address_low);
  435. }
  436. };
  437. struct ColorMask {
  438. union {
  439. u32 raw;
  440. BitField<0, 4, u32> R;
  441. BitField<4, 4, u32> G;
  442. BitField<8, 4, u32> B;
  443. BitField<12, 4, u32> A;
  444. };
  445. };
  446. struct ViewportTransform {
  447. f32 scale_x;
  448. f32 scale_y;
  449. f32 scale_z;
  450. f32 translate_x;
  451. f32 translate_y;
  452. f32 translate_z;
  453. INSERT_PADDING_WORDS(2);
  454. MathUtil::Rectangle<s32> GetRect() const {
  455. return {
  456. GetX(), // left
  457. GetY() + GetHeight(), // top
  458. GetX() + GetWidth(), // right
  459. GetY() // bottom
  460. };
  461. };
  462. s32 GetX() const {
  463. return static_cast<s32>(std::max(0.0f, translate_x - std::fabs(scale_x)));
  464. }
  465. s32 GetY() const {
  466. return static_cast<s32>(std::max(0.0f, translate_y - std::fabs(scale_y)));
  467. }
  468. s32 GetWidth() const {
  469. return static_cast<s32>(translate_x + std::fabs(scale_x)) - GetX();
  470. }
  471. s32 GetHeight() const {
  472. return static_cast<s32>(translate_y + std::fabs(scale_y)) - GetY();
  473. }
  474. };
  475. struct ScissorTest {
  476. u32 enable;
  477. union {
  478. BitField<0, 16, u32> min_x;
  479. BitField<16, 16, u32> max_x;
  480. };
  481. union {
  482. BitField<0, 16, u32> min_y;
  483. BitField<16, 16, u32> max_y;
  484. };
  485. u32 fill;
  486. };
  487. struct ViewPort {
  488. union {
  489. BitField<0, 16, u32> x;
  490. BitField<16, 16, u32> width;
  491. };
  492. union {
  493. BitField<0, 16, u32> y;
  494. BitField<16, 16, u32> height;
  495. };
  496. float depth_range_near;
  497. float depth_range_far;
  498. };
  499. bool IsShaderConfigEnabled(std::size_t index) const {
  500. // The VertexB is always enabled.
  501. if (index == static_cast<std::size_t>(Regs::ShaderProgram::VertexB)) {
  502. return true;
  503. }
  504. return shader_config[index].enable != 0;
  505. }
  506. union {
  507. struct {
  508. INSERT_PADDING_WORDS(0x45);
  509. struct {
  510. u32 upload_address;
  511. u32 data;
  512. u32 entry;
  513. u32 bind;
  514. } macros;
  515. INSERT_PADDING_WORDS(0x188);
  516. u32 tfb_enabled;
  517. INSERT_PADDING_WORDS(0x2E);
  518. std::array<RenderTargetConfig, NumRenderTargets> rt;
  519. std::array<ViewportTransform, NumViewports> viewport_transform;
  520. std::array<ViewPort, NumViewports> viewports;
  521. INSERT_PADDING_WORDS(0x1D);
  522. struct {
  523. u32 first;
  524. u32 count;
  525. } vertex_buffer;
  526. INSERT_PADDING_WORDS(1);
  527. float clear_color[4];
  528. float clear_depth;
  529. INSERT_PADDING_WORDS(0x3);
  530. s32 clear_stencil;
  531. INSERT_PADDING_WORDS(0x7);
  532. u32 polygon_offset_point_enable;
  533. u32 polygon_offset_line_enable;
  534. u32 polygon_offset_fill_enable;
  535. INSERT_PADDING_WORDS(0xD);
  536. std::array<ScissorTest, NumViewports> scissor_test;
  537. INSERT_PADDING_WORDS(0x15);
  538. s32 stencil_back_func_ref;
  539. u32 stencil_back_mask;
  540. u32 stencil_back_func_mask;
  541. INSERT_PADDING_WORDS(0xC);
  542. u32 color_mask_common;
  543. INSERT_PADDING_WORDS(0x6);
  544. u32 rt_separate_frag_data;
  545. INSERT_PADDING_WORDS(0xC);
  546. struct {
  547. u32 address_high;
  548. u32 address_low;
  549. Tegra::DepthFormat format;
  550. union {
  551. BitField<0, 4, u32> block_width;
  552. BitField<4, 4, u32> block_height;
  553. BitField<8, 4, u32> block_depth;
  554. BitField<20, 1, InvMemoryLayout> type;
  555. } memory_layout;
  556. u32 layer_stride;
  557. GPUVAddr Address() const {
  558. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  559. address_low);
  560. }
  561. } zeta;
  562. INSERT_PADDING_WORDS(0x41);
  563. union {
  564. BitField<0, 4, u32> stencil;
  565. BitField<4, 4, u32> unknown;
  566. BitField<8, 4, u32> scissor;
  567. BitField<12, 4, u32> viewport;
  568. } clear_flags;
  569. INSERT_PADDING_WORDS(0x19);
  570. std::array<VertexAttribute, NumVertexAttributes> vertex_attrib_format;
  571. INSERT_PADDING_WORDS(0xF);
  572. struct {
  573. union {
  574. BitField<0, 4, u32> count;
  575. BitField<4, 3, u32> map_0;
  576. BitField<7, 3, u32> map_1;
  577. BitField<10, 3, u32> map_2;
  578. BitField<13, 3, u32> map_3;
  579. BitField<16, 3, u32> map_4;
  580. BitField<19, 3, u32> map_5;
  581. BitField<22, 3, u32> map_6;
  582. BitField<25, 3, u32> map_7;
  583. };
  584. u32 GetMap(std::size_t index) const {
  585. const std::array<u32, NumRenderTargets> maps{map_0, map_1, map_2, map_3,
  586. map_4, map_5, map_6, map_7};
  587. ASSERT(index < maps.size());
  588. return maps[index];
  589. }
  590. } rt_control;
  591. INSERT_PADDING_WORDS(0x2);
  592. u32 zeta_width;
  593. u32 zeta_height;
  594. INSERT_PADDING_WORDS(0x27);
  595. u32 depth_test_enable;
  596. INSERT_PADDING_WORDS(0x5);
  597. u32 independent_blend_enable;
  598. u32 depth_write_enabled;
  599. u32 alpha_test_enabled;
  600. INSERT_PADDING_WORDS(0x6);
  601. u32 d3d_cull_mode;
  602. ComparisonOp depth_test_func;
  603. float alpha_test_ref;
  604. ComparisonOp alpha_test_func;
  605. u32 draw_tfb_stride;
  606. struct {
  607. float r;
  608. float g;
  609. float b;
  610. float a;
  611. } blend_color;
  612. INSERT_PADDING_WORDS(0x4);
  613. struct {
  614. u32 separate_alpha;
  615. Blend::Equation equation_rgb;
  616. Blend::Factor factor_source_rgb;
  617. Blend::Factor factor_dest_rgb;
  618. Blend::Equation equation_a;
  619. Blend::Factor factor_source_a;
  620. INSERT_PADDING_WORDS(1);
  621. Blend::Factor factor_dest_a;
  622. u32 enable_common;
  623. u32 enable[NumRenderTargets];
  624. } blend;
  625. u32 stencil_enable;
  626. StencilOp stencil_front_op_fail;
  627. StencilOp stencil_front_op_zfail;
  628. StencilOp stencil_front_op_zpass;
  629. ComparisonOp stencil_front_func_func;
  630. s32 stencil_front_func_ref;
  631. u32 stencil_front_func_mask;
  632. u32 stencil_front_mask;
  633. INSERT_PADDING_WORDS(0x2);
  634. u32 frag_color_clamp;
  635. union {
  636. BitField<0, 1, u32> y_negate;
  637. BitField<4, 1, u32> triangle_rast_flip;
  638. } screen_y_control;
  639. INSERT_PADDING_WORDS(0x21);
  640. u32 vb_element_base;
  641. INSERT_PADDING_WORDS(0x38);
  642. float point_size;
  643. INSERT_PADDING_WORDS(0x7);
  644. u32 zeta_enable;
  645. union {
  646. BitField<0, 1, u32> alpha_to_coverage;
  647. BitField<4, 1, u32> alpha_to_one;
  648. } multisample_control;
  649. INSERT_PADDING_WORDS(0x7);
  650. struct {
  651. u32 tsc_address_high;
  652. u32 tsc_address_low;
  653. u32 tsc_limit;
  654. GPUVAddr TSCAddress() const {
  655. return static_cast<GPUVAddr>(
  656. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  657. }
  658. } tsc;
  659. INSERT_PADDING_WORDS(0x1);
  660. float polygon_offset_factor;
  661. INSERT_PADDING_WORDS(0x1);
  662. struct {
  663. u32 tic_address_high;
  664. u32 tic_address_low;
  665. u32 tic_limit;
  666. GPUVAddr TICAddress() const {
  667. return static_cast<GPUVAddr>(
  668. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  669. }
  670. } tic;
  671. INSERT_PADDING_WORDS(0x5);
  672. u32 stencil_two_side_enable;
  673. StencilOp stencil_back_op_fail;
  674. StencilOp stencil_back_op_zfail;
  675. StencilOp stencil_back_op_zpass;
  676. ComparisonOp stencil_back_func_func;
  677. INSERT_PADDING_WORDS(0x4);
  678. u32 framebuffer_srgb;
  679. float polygon_offset_units;
  680. INSERT_PADDING_WORDS(0x11);
  681. union {
  682. BitField<2, 1, u32> coord_origin;
  683. BitField<3, 10, u32> enable;
  684. } point_coord_replace;
  685. struct {
  686. u32 code_address_high;
  687. u32 code_address_low;
  688. GPUVAddr CodeAddress() const {
  689. return static_cast<GPUVAddr>(
  690. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  691. }
  692. } code_address;
  693. INSERT_PADDING_WORDS(1);
  694. struct {
  695. u32 vertex_end_gl;
  696. union {
  697. u32 vertex_begin_gl;
  698. BitField<0, 16, PrimitiveTopology> topology;
  699. BitField<26, 1, u32> instance_next;
  700. BitField<27, 1, u32> instance_cont;
  701. };
  702. } draw;
  703. INSERT_PADDING_WORDS(0xA);
  704. struct {
  705. u32 enabled;
  706. u32 index;
  707. } primitive_restart;
  708. INSERT_PADDING_WORDS(0x5F);
  709. struct {
  710. u32 start_addr_high;
  711. u32 start_addr_low;
  712. u32 end_addr_high;
  713. u32 end_addr_low;
  714. IndexFormat format;
  715. u32 first;
  716. u32 count;
  717. unsigned FormatSizeInBytes() const {
  718. switch (format) {
  719. case IndexFormat::UnsignedByte:
  720. return 1;
  721. case IndexFormat::UnsignedShort:
  722. return 2;
  723. case IndexFormat::UnsignedInt:
  724. return 4;
  725. }
  726. UNREACHABLE();
  727. }
  728. GPUVAddr StartAddress() const {
  729. return static_cast<GPUVAddr>(
  730. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  731. }
  732. GPUVAddr EndAddress() const {
  733. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  734. end_addr_low);
  735. }
  736. /// Adjust the index buffer offset so it points to the first desired index.
  737. GPUVAddr IndexStart() const {
  738. return StartAddress() + static_cast<size_t>(first) *
  739. static_cast<size_t>(FormatSizeInBytes());
  740. }
  741. } index_array;
  742. INSERT_PADDING_WORDS(0x7);
  743. INSERT_PADDING_WORDS(0x1F);
  744. float polygon_offset_clamp;
  745. struct {
  746. u32 is_instanced[NumVertexArrays];
  747. /// Returns whether the vertex array specified by index is supposed to be
  748. /// accessed per instance or not.
  749. bool IsInstancingEnabled(u32 index) const {
  750. return is_instanced[index];
  751. }
  752. } instanced_arrays;
  753. INSERT_PADDING_WORDS(0x6);
  754. Cull cull;
  755. INSERT_PADDING_WORDS(0x28);
  756. struct {
  757. u32 enable;
  758. LogicOperation operation;
  759. } logic_op;
  760. INSERT_PADDING_WORDS(0x1);
  761. union {
  762. u32 raw;
  763. BitField<0, 1, u32> Z;
  764. BitField<1, 1, u32> S;
  765. BitField<2, 1, u32> R;
  766. BitField<3, 1, u32> G;
  767. BitField<4, 1, u32> B;
  768. BitField<5, 1, u32> A;
  769. BitField<6, 4, u32> RT;
  770. BitField<10, 11, u32> layer;
  771. } clear_buffers;
  772. INSERT_PADDING_WORDS(0xB);
  773. std::array<ColorMask, NumRenderTargets> color_mask;
  774. INSERT_PADDING_WORDS(0x38);
  775. struct {
  776. u32 query_address_high;
  777. u32 query_address_low;
  778. u32 query_sequence;
  779. union {
  780. u32 raw;
  781. BitField<0, 2, QueryMode> mode;
  782. BitField<4, 1, u32> fence;
  783. BitField<12, 4, QueryUnit> unit;
  784. BitField<16, 1, QuerySyncCondition> sync_cond;
  785. BitField<23, 5, QuerySelect> select;
  786. BitField<28, 1, u32> short_query;
  787. } query_get;
  788. GPUVAddr QueryAddress() const {
  789. return static_cast<GPUVAddr>(
  790. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  791. }
  792. } query;
  793. INSERT_PADDING_WORDS(0x3C);
  794. struct {
  795. union {
  796. BitField<0, 12, u32> stride;
  797. BitField<12, 1, u32> enable;
  798. };
  799. u32 start_high;
  800. u32 start_low;
  801. u32 divisor;
  802. GPUVAddr StartAddress() const {
  803. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  804. start_low);
  805. }
  806. bool IsEnabled() const {
  807. return enable != 0 && StartAddress() != 0;
  808. }
  809. } vertex_array[NumVertexArrays];
  810. Blend independent_blend[NumRenderTargets];
  811. struct {
  812. u32 limit_high;
  813. u32 limit_low;
  814. GPUVAddr LimitAddress() const {
  815. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  816. limit_low);
  817. }
  818. } vertex_array_limit[NumVertexArrays];
  819. struct {
  820. union {
  821. BitField<0, 1, u32> enable;
  822. BitField<4, 4, ShaderProgram> program;
  823. };
  824. u32 offset;
  825. INSERT_PADDING_WORDS(14);
  826. } shader_config[MaxShaderProgram];
  827. INSERT_PADDING_WORDS(0x80);
  828. struct {
  829. u32 cb_size;
  830. u32 cb_address_high;
  831. u32 cb_address_low;
  832. u32 cb_pos;
  833. u32 cb_data[NumCBData];
  834. GPUVAddr BufferAddress() const {
  835. return static_cast<GPUVAddr>(
  836. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  837. }
  838. } const_buffer;
  839. INSERT_PADDING_WORDS(0x10);
  840. struct {
  841. union {
  842. u32 raw_config;
  843. BitField<0, 1, u32> valid;
  844. BitField<4, 5, u32> index;
  845. };
  846. INSERT_PADDING_WORDS(7);
  847. } cb_bind[MaxShaderStage];
  848. INSERT_PADDING_WORDS(0x56);
  849. u32 tex_cb_index;
  850. INSERT_PADDING_WORDS(0x395);
  851. struct {
  852. /// Compressed address of a buffer that holds information about bound SSBOs.
  853. /// This address is usually bound to c0 in the shaders.
  854. u32 buffer_address;
  855. GPUVAddr BufferAddress() const {
  856. return static_cast<GPUVAddr>(buffer_address) << 8;
  857. }
  858. } ssbo_info;
  859. INSERT_PADDING_WORDS(0x11);
  860. struct {
  861. u32 address[MaxShaderStage];
  862. u32 size[MaxShaderStage];
  863. } tex_info_buffers;
  864. INSERT_PADDING_WORDS(0xCC);
  865. };
  866. std::array<u32, NUM_REGS> reg_array;
  867. };
  868. } regs{};
  869. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  870. struct State {
  871. struct ConstBufferInfo {
  872. GPUVAddr address;
  873. u32 index;
  874. u32 size;
  875. bool enabled;
  876. };
  877. struct ShaderStageInfo {
  878. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  879. };
  880. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  881. u32 current_instance = 0; ///< Current instance to be used to simulate instanced rendering.
  882. };
  883. State state{};
  884. MemoryManager& memory_manager;
  885. struct DirtyFlags {
  886. bool vertex_attrib_format = true;
  887. };
  888. DirtyFlags dirty_flags;
  889. /// Reads a register value located at the input method address
  890. u32 GetRegisterValue(u32 method) const;
  891. /// Write the value to the register identified by method.
  892. void WriteReg(u32 method, u32 value, u32 remaining_params);
  893. /// Returns a list of enabled textures for the specified shader stage.
  894. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  895. /// Returns the texture information for a specific texture in a specific shader stage.
  896. Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, std::size_t offset) const;
  897. /// Memory for macro code - it's undetermined how big this is, however 1MB is much larger than
  898. /// we've seen used.
  899. using MacroMemory = std::array<u32, 0x40000>;
  900. /// Gets a reference to macro memory.
  901. const MacroMemory& GetMacroMemory() const {
  902. return macro_memory;
  903. }
  904. private:
  905. void InitializeRegisterDefaults();
  906. VideoCore::RasterizerInterface& rasterizer;
  907. /// Start offsets of each macro in macro_memory
  908. std::unordered_map<u32, u32> macro_offsets;
  909. /// Memory for macro code
  910. MacroMemory macro_memory;
  911. /// Macro method that is currently being executed / being fed parameters.
  912. u32 executing_macro = 0;
  913. /// Parameters that have been submitted to the macro call so far.
  914. std::vector<u32> macro_params;
  915. /// Interpreter for the macro codes uploaded to the GPU.
  916. MacroInterpreter macro_interpreter;
  917. /// Retrieves information about a specific TIC entry from the TIC buffer.
  918. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  919. /// Retrieves information about a specific TSC entry from the TSC buffer.
  920. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  921. /**
  922. * Call a macro on this engine.
  923. * @param method Method to call
  924. * @param parameters Arguments to the method call
  925. */
  926. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  927. /// Handles writes to the macro uploading register.
  928. void ProcessMacroUpload(u32 data);
  929. /// Handles writes to the macro bind register.
  930. void ProcessMacroBind(u32 data);
  931. /// Handles a write to the CLEAR_BUFFERS register.
  932. void ProcessClearBuffers();
  933. /// Handles a write to the QUERY_GET register.
  934. void ProcessQueryGet();
  935. /// Handles a write to the CB_DATA[i] register.
  936. void ProcessCBData(u32 value);
  937. /// Handles a write to the CB_BIND register.
  938. void ProcessCBBind(Regs::ShaderStage stage);
  939. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  940. void DrawArrays();
  941. };
  942. #define ASSERT_REG_POSITION(field_name, position) \
  943. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  944. "Field " #field_name " has invalid position")
  945. ASSERT_REG_POSITION(macros, 0x45);
  946. ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
  947. ASSERT_REG_POSITION(rt, 0x200);
  948. ASSERT_REG_POSITION(viewport_transform, 0x280);
  949. ASSERT_REG_POSITION(viewports, 0x300);
  950. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  951. ASSERT_REG_POSITION(clear_color[0], 0x360);
  952. ASSERT_REG_POSITION(clear_depth, 0x364);
  953. ASSERT_REG_POSITION(clear_stencil, 0x368);
  954. ASSERT_REG_POSITION(polygon_offset_point_enable, 0x370);
  955. ASSERT_REG_POSITION(polygon_offset_line_enable, 0x371);
  956. ASSERT_REG_POSITION(polygon_offset_fill_enable, 0x372);
  957. ASSERT_REG_POSITION(scissor_test, 0x380);
  958. ASSERT_REG_POSITION(stencil_back_func_ref, 0x3D5);
  959. ASSERT_REG_POSITION(stencil_back_mask, 0x3D6);
  960. ASSERT_REG_POSITION(stencil_back_func_mask, 0x3D7);
  961. ASSERT_REG_POSITION(color_mask_common, 0x3E4);
  962. ASSERT_REG_POSITION(rt_separate_frag_data, 0x3EB);
  963. ASSERT_REG_POSITION(zeta, 0x3F8);
  964. ASSERT_REG_POSITION(clear_flags, 0x43E);
  965. ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
  966. ASSERT_REG_POSITION(rt_control, 0x487);
  967. ASSERT_REG_POSITION(zeta_width, 0x48a);
  968. ASSERT_REG_POSITION(zeta_height, 0x48b);
  969. ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
  970. ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
  971. ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
  972. ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
  973. ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
  974. ASSERT_REG_POSITION(depth_test_func, 0x4C3);
  975. ASSERT_REG_POSITION(alpha_test_ref, 0x4C4);
  976. ASSERT_REG_POSITION(alpha_test_func, 0x4C5);
  977. ASSERT_REG_POSITION(draw_tfb_stride, 0x4C6);
  978. ASSERT_REG_POSITION(blend_color, 0x4C7);
  979. ASSERT_REG_POSITION(blend, 0x4CF);
  980. ASSERT_REG_POSITION(stencil_enable, 0x4E0);
  981. ASSERT_REG_POSITION(stencil_front_op_fail, 0x4E1);
  982. ASSERT_REG_POSITION(stencil_front_op_zfail, 0x4E2);
  983. ASSERT_REG_POSITION(stencil_front_op_zpass, 0x4E3);
  984. ASSERT_REG_POSITION(stencil_front_func_func, 0x4E4);
  985. ASSERT_REG_POSITION(stencil_front_func_ref, 0x4E5);
  986. ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
  987. ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
  988. ASSERT_REG_POSITION(frag_color_clamp, 0x4EA);
  989. ASSERT_REG_POSITION(screen_y_control, 0x4EB);
  990. ASSERT_REG_POSITION(vb_element_base, 0x50D);
  991. ASSERT_REG_POSITION(point_size, 0x546);
  992. ASSERT_REG_POSITION(zeta_enable, 0x54E);
  993. ASSERT_REG_POSITION(multisample_control, 0x54F);
  994. ASSERT_REG_POSITION(tsc, 0x557);
  995. ASSERT_REG_POSITION(polygon_offset_factor, 0x55b);
  996. ASSERT_REG_POSITION(tic, 0x55D);
  997. ASSERT_REG_POSITION(stencil_two_side_enable, 0x565);
  998. ASSERT_REG_POSITION(stencil_back_op_fail, 0x566);
  999. ASSERT_REG_POSITION(stencil_back_op_zfail, 0x567);
  1000. ASSERT_REG_POSITION(stencil_back_op_zpass, 0x568);
  1001. ASSERT_REG_POSITION(stencil_back_func_func, 0x569);
  1002. ASSERT_REG_POSITION(framebuffer_srgb, 0x56E);
  1003. ASSERT_REG_POSITION(polygon_offset_units, 0x56F);
  1004. ASSERT_REG_POSITION(point_coord_replace, 0x581);
  1005. ASSERT_REG_POSITION(code_address, 0x582);
  1006. ASSERT_REG_POSITION(draw, 0x585);
  1007. ASSERT_REG_POSITION(primitive_restart, 0x591);
  1008. ASSERT_REG_POSITION(index_array, 0x5F2);
  1009. ASSERT_REG_POSITION(polygon_offset_clamp, 0x61F);
  1010. ASSERT_REG_POSITION(instanced_arrays, 0x620);
  1011. ASSERT_REG_POSITION(cull, 0x646);
  1012. ASSERT_REG_POSITION(logic_op, 0x671);
  1013. ASSERT_REG_POSITION(clear_buffers, 0x674);
  1014. ASSERT_REG_POSITION(color_mask, 0x680);
  1015. ASSERT_REG_POSITION(query, 0x6C0);
  1016. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  1017. ASSERT_REG_POSITION(independent_blend, 0x780);
  1018. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  1019. ASSERT_REG_POSITION(shader_config[0], 0x800);
  1020. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  1021. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  1022. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  1023. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  1024. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  1025. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  1026. #undef ASSERT_REG_POSITION
  1027. } // namespace Tegra::Engines