maxwell_3d.h 43 KB

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