maxwell_3d.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  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 <limits>
  8. #include <optional>
  9. #include <type_traits>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "common/assert.h"
  13. #include "common/bit_field.h"
  14. #include "common/common_funcs.h"
  15. #include "common/common_types.h"
  16. #include "common/math_util.h"
  17. #include "video_core/engines/const_buffer_engine_interface.h"
  18. #include "video_core/engines/const_buffer_info.h"
  19. #include "video_core/engines/engine_interface.h"
  20. #include "video_core/engines/engine_upload.h"
  21. #include "video_core/engines/shader_type.h"
  22. #include "video_core/gpu.h"
  23. #include "video_core/macro/macro.h"
  24. #include "video_core/textures/texture.h"
  25. namespace Core {
  26. class System;
  27. }
  28. namespace Tegra {
  29. class MemoryManager;
  30. }
  31. namespace VideoCore {
  32. class RasterizerInterface;
  33. }
  34. namespace Tegra::Engines {
  35. /**
  36. * This Engine is known as GF100_3D. Documentation can be found in:
  37. * https://github.com/envytools/envytools/blob/master/rnndb/graph/gf100_3d.xml
  38. * https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nvc0/nvc0_3d.xml.h
  39. */
  40. #define MAXWELL3D_REG_INDEX(field_name) \
  41. (offsetof(Tegra::Engines::Maxwell3D::Regs, field_name) / sizeof(u32))
  42. class Maxwell3D final : public ConstBufferEngineInterface, public EngineInterface {
  43. public:
  44. explicit Maxwell3D(Core::System& system, MemoryManager& memory_manager);
  45. ~Maxwell3D();
  46. /// Binds a rasterizer to this engine.
  47. void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
  48. /// Register structure of the Maxwell3D engine.
  49. /// TODO(Subv): This structure will need to be made bigger as more registers are discovered.
  50. struct Regs {
  51. static constexpr std::size_t NUM_REGS = 0xE00;
  52. static constexpr std::size_t NumRenderTargets = 8;
  53. static constexpr std::size_t NumViewports = 16;
  54. static constexpr std::size_t NumCBData = 16;
  55. static constexpr std::size_t NumVertexArrays = 32;
  56. static constexpr std::size_t NumVertexAttributes = 32;
  57. static constexpr std::size_t NumVaryings = 31;
  58. static constexpr std::size_t NumImages = 8; // TODO(Rodrigo): Investigate this number
  59. static constexpr std::size_t NumClipDistances = 8;
  60. static constexpr std::size_t NumTransformFeedbackBuffers = 4;
  61. static constexpr std::size_t MaxShaderProgram = 6;
  62. static constexpr std::size_t MaxShaderStage = 5;
  63. // Maximum number of const buffers per shader stage.
  64. static constexpr std::size_t MaxConstBuffers = 18;
  65. static constexpr std::size_t MaxConstBufferSize = 0x10000;
  66. enum class QueryOperation : u32 {
  67. Release = 0,
  68. Acquire = 1,
  69. Counter = 2,
  70. Trap = 3,
  71. };
  72. enum class QueryUnit : u32 {
  73. VFetch = 1,
  74. VP = 2,
  75. Rast = 4,
  76. StrmOut = 5,
  77. GP = 6,
  78. ZCull = 7,
  79. Prop = 10,
  80. Crop = 15,
  81. };
  82. enum class QuerySelect : u32 {
  83. Zero = 0,
  84. TimeElapsed = 2,
  85. TransformFeedbackPrimitivesGenerated = 11,
  86. PrimitivesGenerated = 18,
  87. SamplesPassed = 21,
  88. TransformFeedbackUnknown = 26,
  89. };
  90. struct QueryCompare {
  91. u32 initial_sequence;
  92. u32 initial_mode;
  93. u32 unknown1;
  94. u32 unknown2;
  95. u32 current_sequence;
  96. u32 current_mode;
  97. };
  98. enum class QuerySyncCondition : u32 {
  99. NotEqual = 0,
  100. GreaterThan = 1,
  101. };
  102. enum class ConditionMode : u32 {
  103. Never = 0,
  104. Always = 1,
  105. ResNonZero = 2,
  106. Equal = 3,
  107. NotEqual = 4,
  108. };
  109. enum class ShaderProgram : u32 {
  110. VertexA = 0,
  111. VertexB = 1,
  112. TesselationControl = 2,
  113. TesselationEval = 3,
  114. Geometry = 4,
  115. Fragment = 5,
  116. };
  117. struct VertexAttribute {
  118. enum class Size : u32 {
  119. Invalid = 0x0,
  120. Size_32_32_32_32 = 0x01,
  121. Size_32_32_32 = 0x02,
  122. Size_16_16_16_16 = 0x03,
  123. Size_32_32 = 0x04,
  124. Size_16_16_16 = 0x05,
  125. Size_8_8_8_8 = 0x0a,
  126. Size_16_16 = 0x0f,
  127. Size_32 = 0x12,
  128. Size_8_8_8 = 0x13,
  129. Size_8_8 = 0x18,
  130. Size_16 = 0x1b,
  131. Size_8 = 0x1d,
  132. Size_10_10_10_2 = 0x30,
  133. Size_11_11_10 = 0x31,
  134. };
  135. enum class Type : u32 {
  136. SignedNorm = 1,
  137. UnsignedNorm = 2,
  138. SignedInt = 3,
  139. UnsignedInt = 4,
  140. UnsignedScaled = 5,
  141. SignedScaled = 6,
  142. Float = 7,
  143. };
  144. union {
  145. BitField<0, 5, u32> buffer;
  146. BitField<6, 1, u32> constant;
  147. BitField<7, 14, u32> offset;
  148. BitField<21, 6, Size> size;
  149. BitField<27, 3, Type> type;
  150. BitField<31, 1, u32> bgra;
  151. u32 hex;
  152. };
  153. u32 ComponentCount() const {
  154. switch (size) {
  155. case Size::Size_32_32_32_32:
  156. return 4;
  157. case Size::Size_32_32_32:
  158. return 3;
  159. case Size::Size_16_16_16_16:
  160. return 4;
  161. case Size::Size_32_32:
  162. return 2;
  163. case Size::Size_16_16_16:
  164. return 3;
  165. case Size::Size_8_8_8_8:
  166. return 4;
  167. case Size::Size_16_16:
  168. return 2;
  169. case Size::Size_32:
  170. return 1;
  171. case Size::Size_8_8_8:
  172. return 3;
  173. case Size::Size_8_8:
  174. return 2;
  175. case Size::Size_16:
  176. return 1;
  177. case Size::Size_8:
  178. return 1;
  179. case Size::Size_10_10_10_2:
  180. return 4;
  181. case Size::Size_11_11_10:
  182. return 3;
  183. default:
  184. UNREACHABLE();
  185. return 1;
  186. }
  187. }
  188. u32 SizeInBytes() const {
  189. switch (size) {
  190. case Size::Size_32_32_32_32:
  191. return 16;
  192. case Size::Size_32_32_32:
  193. return 12;
  194. case Size::Size_16_16_16_16:
  195. return 8;
  196. case Size::Size_32_32:
  197. return 8;
  198. case Size::Size_16_16_16:
  199. return 6;
  200. case Size::Size_8_8_8_8:
  201. return 4;
  202. case Size::Size_16_16:
  203. return 4;
  204. case Size::Size_32:
  205. return 4;
  206. case Size::Size_8_8_8:
  207. return 3;
  208. case Size::Size_8_8:
  209. return 2;
  210. case Size::Size_16:
  211. return 2;
  212. case Size::Size_8:
  213. return 1;
  214. case Size::Size_10_10_10_2:
  215. return 4;
  216. case Size::Size_11_11_10:
  217. return 4;
  218. default:
  219. UNREACHABLE();
  220. return 1;
  221. }
  222. }
  223. std::string SizeString() const {
  224. switch (size) {
  225. case Size::Size_32_32_32_32:
  226. return "32_32_32_32";
  227. case Size::Size_32_32_32:
  228. return "32_32_32";
  229. case Size::Size_16_16_16_16:
  230. return "16_16_16_16";
  231. case Size::Size_32_32:
  232. return "32_32";
  233. case Size::Size_16_16_16:
  234. return "16_16_16";
  235. case Size::Size_8_8_8_8:
  236. return "8_8_8_8";
  237. case Size::Size_16_16:
  238. return "16_16";
  239. case Size::Size_32:
  240. return "32";
  241. case Size::Size_8_8_8:
  242. return "8_8_8";
  243. case Size::Size_8_8:
  244. return "8_8";
  245. case Size::Size_16:
  246. return "16";
  247. case Size::Size_8:
  248. return "8";
  249. case Size::Size_10_10_10_2:
  250. return "10_10_10_2";
  251. case Size::Size_11_11_10:
  252. return "11_11_10";
  253. default:
  254. UNREACHABLE();
  255. return {};
  256. }
  257. }
  258. std::string TypeString() const {
  259. switch (type) {
  260. case Type::SignedNorm:
  261. return "SNORM";
  262. case Type::UnsignedNorm:
  263. return "UNORM";
  264. case Type::SignedInt:
  265. return "SINT";
  266. case Type::UnsignedInt:
  267. return "UINT";
  268. case Type::UnsignedScaled:
  269. return "USCALED";
  270. case Type::SignedScaled:
  271. return "SSCALED";
  272. case Type::Float:
  273. return "FLOAT";
  274. }
  275. UNREACHABLE();
  276. return {};
  277. }
  278. bool IsNormalized() const {
  279. return (type == Type::SignedNorm) || (type == Type::UnsignedNorm);
  280. }
  281. bool IsConstant() const {
  282. return constant;
  283. }
  284. bool IsValid() const {
  285. return size != Size::Invalid;
  286. }
  287. bool operator<(const VertexAttribute& other) const {
  288. return hex < other.hex;
  289. }
  290. };
  291. struct MsaaSampleLocation {
  292. union {
  293. BitField<0, 4, u32> x0;
  294. BitField<4, 4, u32> y0;
  295. BitField<8, 4, u32> x1;
  296. BitField<12, 4, u32> y1;
  297. BitField<16, 4, u32> x2;
  298. BitField<20, 4, u32> y2;
  299. BitField<24, 4, u32> x3;
  300. BitField<28, 4, u32> y3;
  301. };
  302. constexpr std::pair<u32, u32> Location(int index) const {
  303. switch (index) {
  304. case 0:
  305. return {x0, y0};
  306. case 1:
  307. return {x1, y1};
  308. case 2:
  309. return {x2, y2};
  310. case 3:
  311. return {x3, y3};
  312. default:
  313. UNREACHABLE();
  314. return {0, 0};
  315. }
  316. }
  317. };
  318. enum class DepthMode : u32 {
  319. MinusOneToOne = 0,
  320. ZeroToOne = 1,
  321. };
  322. enum class PrimitiveTopology : u32 {
  323. Points = 0x0,
  324. Lines = 0x1,
  325. LineLoop = 0x2,
  326. LineStrip = 0x3,
  327. Triangles = 0x4,
  328. TriangleStrip = 0x5,
  329. TriangleFan = 0x6,
  330. Quads = 0x7,
  331. QuadStrip = 0x8,
  332. Polygon = 0x9,
  333. LinesAdjacency = 0xa,
  334. LineStripAdjacency = 0xb,
  335. TrianglesAdjacency = 0xc,
  336. TriangleStripAdjacency = 0xd,
  337. Patches = 0xe,
  338. };
  339. enum class IndexFormat : u32 {
  340. UnsignedByte = 0x0,
  341. UnsignedShort = 0x1,
  342. UnsignedInt = 0x2,
  343. };
  344. enum class ComparisonOp : u32 {
  345. // These values are used by Nouveau and most games, they correspond to the OpenGL token
  346. // values for these operations.
  347. Never = 0x200,
  348. Less = 0x201,
  349. Equal = 0x202,
  350. LessEqual = 0x203,
  351. Greater = 0x204,
  352. NotEqual = 0x205,
  353. GreaterEqual = 0x206,
  354. Always = 0x207,
  355. // These values are used by some games, they seem to be NV04 values.
  356. NeverOld = 1,
  357. LessOld = 2,
  358. EqualOld = 3,
  359. LessEqualOld = 4,
  360. GreaterOld = 5,
  361. NotEqualOld = 6,
  362. GreaterEqualOld = 7,
  363. AlwaysOld = 8,
  364. };
  365. enum class LogicOperation : u32 {
  366. Clear = 0x1500,
  367. And = 0x1501,
  368. AndReverse = 0x1502,
  369. Copy = 0x1503,
  370. AndInverted = 0x1504,
  371. NoOp = 0x1505,
  372. Xor = 0x1506,
  373. Or = 0x1507,
  374. Nor = 0x1508,
  375. Equiv = 0x1509,
  376. Invert = 0x150A,
  377. OrReverse = 0x150B,
  378. CopyInverted = 0x150C,
  379. OrInverted = 0x150D,
  380. Nand = 0x150E,
  381. Set = 0x150F,
  382. };
  383. enum class StencilOp : u32 {
  384. Keep = 1,
  385. Zero = 2,
  386. Replace = 3,
  387. Incr = 4,
  388. Decr = 5,
  389. Invert = 6,
  390. IncrWrap = 7,
  391. DecrWrap = 8,
  392. KeepOGL = 0x1E00,
  393. ZeroOGL = 0,
  394. ReplaceOGL = 0x1E01,
  395. IncrOGL = 0x1E02,
  396. DecrOGL = 0x1E03,
  397. InvertOGL = 0x150A,
  398. IncrWrapOGL = 0x8507,
  399. DecrWrapOGL = 0x8508,
  400. };
  401. enum class CounterReset : u32 {
  402. SampleCnt = 0x01,
  403. Unk02 = 0x02,
  404. Unk03 = 0x03,
  405. Unk04 = 0x04,
  406. EmittedPrimitives = 0x10, // Not tested
  407. Unk11 = 0x11,
  408. Unk12 = 0x12,
  409. Unk13 = 0x13,
  410. Unk15 = 0x15,
  411. Unk16 = 0x16,
  412. Unk17 = 0x17,
  413. Unk18 = 0x18,
  414. Unk1A = 0x1A,
  415. Unk1B = 0x1B,
  416. Unk1C = 0x1C,
  417. Unk1D = 0x1D,
  418. Unk1E = 0x1E,
  419. GeneratedPrimitives = 0x1F,
  420. };
  421. enum class FrontFace : u32 {
  422. ClockWise = 0x0900,
  423. CounterClockWise = 0x0901,
  424. };
  425. enum class CullFace : u32 {
  426. Front = 0x0404,
  427. Back = 0x0405,
  428. FrontAndBack = 0x0408,
  429. };
  430. struct Blend {
  431. enum class Equation : u32 {
  432. Add = 1,
  433. Subtract = 2,
  434. ReverseSubtract = 3,
  435. Min = 4,
  436. Max = 5,
  437. // These values are used by Nouveau and some games.
  438. AddGL = 0x8006,
  439. SubtractGL = 0x8007,
  440. ReverseSubtractGL = 0x8008,
  441. MinGL = 0x800a,
  442. MaxGL = 0x800b
  443. };
  444. enum class Factor : u32 {
  445. Zero = 0x1,
  446. One = 0x2,
  447. SourceColor = 0x3,
  448. OneMinusSourceColor = 0x4,
  449. SourceAlpha = 0x5,
  450. OneMinusSourceAlpha = 0x6,
  451. DestAlpha = 0x7,
  452. OneMinusDestAlpha = 0x8,
  453. DestColor = 0x9,
  454. OneMinusDestColor = 0xa,
  455. SourceAlphaSaturate = 0xb,
  456. Source1Color = 0x10,
  457. OneMinusSource1Color = 0x11,
  458. Source1Alpha = 0x12,
  459. OneMinusSource1Alpha = 0x13,
  460. ConstantColor = 0x61,
  461. OneMinusConstantColor = 0x62,
  462. ConstantAlpha = 0x63,
  463. OneMinusConstantAlpha = 0x64,
  464. // These values are used by Nouveau and some games.
  465. ZeroGL = 0x4000,
  466. OneGL = 0x4001,
  467. SourceColorGL = 0x4300,
  468. OneMinusSourceColorGL = 0x4301,
  469. SourceAlphaGL = 0x4302,
  470. OneMinusSourceAlphaGL = 0x4303,
  471. DestAlphaGL = 0x4304,
  472. OneMinusDestAlphaGL = 0x4305,
  473. DestColorGL = 0x4306,
  474. OneMinusDestColorGL = 0x4307,
  475. SourceAlphaSaturateGL = 0x4308,
  476. ConstantColorGL = 0xc001,
  477. OneMinusConstantColorGL = 0xc002,
  478. ConstantAlphaGL = 0xc003,
  479. OneMinusConstantAlphaGL = 0xc004,
  480. Source1ColorGL = 0xc900,
  481. OneMinusSource1ColorGL = 0xc901,
  482. Source1AlphaGL = 0xc902,
  483. OneMinusSource1AlphaGL = 0xc903,
  484. };
  485. u32 separate_alpha;
  486. Equation equation_rgb;
  487. Factor factor_source_rgb;
  488. Factor factor_dest_rgb;
  489. Equation equation_a;
  490. Factor factor_source_a;
  491. Factor factor_dest_a;
  492. INSERT_PADDING_WORDS_NOINIT(1);
  493. };
  494. enum class TessellationPrimitive : u32 {
  495. Isolines = 0,
  496. Triangles = 1,
  497. Quads = 2,
  498. };
  499. enum class TessellationSpacing : u32 {
  500. Equal = 0,
  501. FractionalOdd = 1,
  502. FractionalEven = 2,
  503. };
  504. enum class PolygonMode : u32 {
  505. Point = 0x1b00,
  506. Line = 0x1b01,
  507. Fill = 0x1b02,
  508. };
  509. enum class ShadowRamControl : u32 {
  510. // write value to shadow ram
  511. Track = 0,
  512. // write value to shadow ram ( with validation ??? )
  513. TrackWithFilter = 1,
  514. // only write to real hw register
  515. Passthrough = 2,
  516. // write value from shadow ram to real hw register
  517. Replay = 3,
  518. };
  519. enum class ViewportSwizzle : u32 {
  520. PositiveX = 0,
  521. NegativeX = 1,
  522. PositiveY = 2,
  523. NegativeY = 3,
  524. PositiveZ = 4,
  525. NegativeZ = 5,
  526. PositiveW = 6,
  527. NegativeW = 7,
  528. };
  529. enum class SamplerIndex : u32 {
  530. Independently = 0,
  531. ViaHeaderIndex = 1,
  532. };
  533. struct TileMode {
  534. union {
  535. BitField<0, 4, u32> block_width;
  536. BitField<4, 4, u32> block_height;
  537. BitField<8, 4, u32> block_depth;
  538. BitField<12, 1, u32> is_pitch_linear;
  539. BitField<16, 1, u32> is_3d;
  540. };
  541. };
  542. static_assert(sizeof(TileMode) == 4);
  543. struct RenderTargetConfig {
  544. u32 address_high;
  545. u32 address_low;
  546. u32 width;
  547. u32 height;
  548. Tegra::RenderTargetFormat format;
  549. TileMode tile_mode;
  550. union {
  551. BitField<0, 16, u32> depth;
  552. BitField<16, 1, u32> volume;
  553. };
  554. u32 layer_stride;
  555. u32 base_layer;
  556. INSERT_PADDING_WORDS_NOINIT(7);
  557. GPUVAddr Address() const {
  558. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  559. address_low);
  560. }
  561. };
  562. struct ColorMask {
  563. union {
  564. u32 raw;
  565. BitField<0, 4, u32> R;
  566. BitField<4, 4, u32> G;
  567. BitField<8, 4, u32> B;
  568. BitField<12, 4, u32> A;
  569. };
  570. };
  571. struct ViewportTransform {
  572. f32 scale_x;
  573. f32 scale_y;
  574. f32 scale_z;
  575. f32 translate_x;
  576. f32 translate_y;
  577. f32 translate_z;
  578. union {
  579. u32 raw;
  580. BitField<0, 3, ViewportSwizzle> x;
  581. BitField<4, 3, ViewportSwizzle> y;
  582. BitField<8, 3, ViewportSwizzle> z;
  583. BitField<12, 3, ViewportSwizzle> w;
  584. } swizzle;
  585. INSERT_PADDING_WORDS_NOINIT(1);
  586. Common::Rectangle<f32> GetRect() const {
  587. return {
  588. GetX(), // left
  589. GetY() + GetHeight(), // top
  590. GetX() + GetWidth(), // right
  591. GetY() // bottom
  592. };
  593. }
  594. f32 GetX() const {
  595. return std::max(0.0f, translate_x - std::fabs(scale_x));
  596. }
  597. f32 GetY() const {
  598. return std::max(0.0f, translate_y - std::fabs(scale_y));
  599. }
  600. f32 GetWidth() const {
  601. return translate_x + std::fabs(scale_x) - GetX();
  602. }
  603. f32 GetHeight() const {
  604. return translate_y + std::fabs(scale_y) - GetY();
  605. }
  606. };
  607. struct ScissorTest {
  608. u32 enable;
  609. union {
  610. BitField<0, 16, u32> min_x;
  611. BitField<16, 16, u32> max_x;
  612. };
  613. union {
  614. BitField<0, 16, u32> min_y;
  615. BitField<16, 16, u32> max_y;
  616. };
  617. u32 fill;
  618. };
  619. struct ViewPort {
  620. union {
  621. BitField<0, 16, u32> x;
  622. BitField<16, 16, u32> width;
  623. };
  624. union {
  625. BitField<0, 16, u32> y;
  626. BitField<16, 16, u32> height;
  627. };
  628. float depth_range_near;
  629. float depth_range_far;
  630. };
  631. struct TransformFeedbackBinding {
  632. u32 buffer_enable;
  633. u32 address_high;
  634. u32 address_low;
  635. s32 buffer_size;
  636. s32 buffer_offset;
  637. INSERT_PADDING_WORDS_NOINIT(3);
  638. GPUVAddr Address() const {
  639. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  640. address_low);
  641. }
  642. };
  643. static_assert(sizeof(TransformFeedbackBinding) == 32);
  644. struct TransformFeedbackLayout {
  645. u32 stream;
  646. u32 varying_count;
  647. u32 stride;
  648. INSERT_PADDING_WORDS_NOINIT(1);
  649. };
  650. static_assert(sizeof(TransformFeedbackLayout) == 16);
  651. bool IsShaderConfigEnabled(std::size_t index) const {
  652. // The VertexB is always enabled.
  653. if (index == static_cast<std::size_t>(Regs::ShaderProgram::VertexB)) {
  654. return true;
  655. }
  656. return shader_config[index].enable != 0;
  657. }
  658. bool IsShaderConfigEnabled(Regs::ShaderProgram type) const {
  659. return IsShaderConfigEnabled(static_cast<std::size_t>(type));
  660. }
  661. union {
  662. struct {
  663. INSERT_PADDING_WORDS_NOINIT(0x44);
  664. u32 wait_for_idle;
  665. struct {
  666. u32 upload_address;
  667. u32 data;
  668. u32 entry;
  669. u32 bind;
  670. } macros;
  671. ShadowRamControl shadow_ram_control;
  672. INSERT_PADDING_WORDS_NOINIT(0x16);
  673. Upload::Registers upload;
  674. struct {
  675. union {
  676. BitField<0, 1, u32> linear;
  677. };
  678. } exec_upload;
  679. u32 data_upload;
  680. INSERT_PADDING_WORDS_NOINIT(0x16);
  681. u32 force_early_fragment_tests;
  682. INSERT_PADDING_WORDS_NOINIT(0x2D);
  683. struct {
  684. union {
  685. BitField<0, 16, u32> sync_point;
  686. BitField<16, 1, u32> unknown;
  687. BitField<20, 1, u32> increment;
  688. };
  689. } sync_info;
  690. INSERT_PADDING_WORDS_NOINIT(0x15);
  691. union {
  692. BitField<0, 2, TessellationPrimitive> prim;
  693. BitField<4, 2, TessellationSpacing> spacing;
  694. BitField<8, 1, u32> cw;
  695. BitField<9, 1, u32> connected;
  696. } tess_mode;
  697. std::array<f32, 4> tess_level_outer;
  698. std::array<f32, 2> tess_level_inner;
  699. INSERT_PADDING_WORDS_NOINIT(0x10);
  700. u32 rasterize_enable;
  701. std::array<TransformFeedbackBinding, NumTransformFeedbackBuffers> tfb_bindings;
  702. INSERT_PADDING_WORDS_NOINIT(0xC0);
  703. std::array<TransformFeedbackLayout, NumTransformFeedbackBuffers> tfb_layouts;
  704. INSERT_PADDING_WORDS_NOINIT(0x1);
  705. u32 tfb_enabled;
  706. INSERT_PADDING_WORDS_NOINIT(0x2E);
  707. std::array<RenderTargetConfig, NumRenderTargets> rt;
  708. std::array<ViewportTransform, NumViewports> viewport_transform;
  709. std::array<ViewPort, NumViewports> viewports;
  710. INSERT_PADDING_WORDS_NOINIT(0x1D);
  711. struct {
  712. u32 first;
  713. u32 count;
  714. } vertex_buffer;
  715. DepthMode depth_mode;
  716. float clear_color[4];
  717. float clear_depth;
  718. INSERT_PADDING_WORDS_NOINIT(0x3);
  719. s32 clear_stencil;
  720. INSERT_PADDING_WORDS_NOINIT(0x2);
  721. PolygonMode polygon_mode_front;
  722. PolygonMode polygon_mode_back;
  723. INSERT_PADDING_WORDS_NOINIT(0x3);
  724. u32 polygon_offset_point_enable;
  725. u32 polygon_offset_line_enable;
  726. u32 polygon_offset_fill_enable;
  727. u32 patch_vertices;
  728. INSERT_PADDING_WORDS_NOINIT(0x4);
  729. u32 fragment_barrier;
  730. INSERT_PADDING_WORDS_NOINIT(0x7);
  731. std::array<ScissorTest, NumViewports> scissor_test;
  732. INSERT_PADDING_WORDS_NOINIT(0x15);
  733. s32 stencil_back_func_ref;
  734. u32 stencil_back_mask;
  735. u32 stencil_back_func_mask;
  736. INSERT_PADDING_WORDS_NOINIT(0x5);
  737. u32 invalidate_texture_data_cache;
  738. INSERT_PADDING_WORDS_NOINIT(0x1);
  739. u32 tiled_cache_barrier;
  740. INSERT_PADDING_WORDS_NOINIT(0x4);
  741. u32 color_mask_common;
  742. INSERT_PADDING_WORDS_NOINIT(0x2);
  743. f32 depth_bounds[2];
  744. INSERT_PADDING_WORDS_NOINIT(0x2);
  745. u32 rt_separate_frag_data;
  746. INSERT_PADDING_WORDS_NOINIT(0x1);
  747. u32 multisample_raster_enable;
  748. u32 multisample_raster_samples;
  749. std::array<u32, 4> multisample_sample_mask;
  750. INSERT_PADDING_WORDS_NOINIT(0x5);
  751. struct {
  752. u32 address_high;
  753. u32 address_low;
  754. Tegra::DepthFormat format;
  755. TileMode tile_mode;
  756. u32 layer_stride;
  757. GPUVAddr Address() const {
  758. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  759. address_low);
  760. }
  761. } zeta;
  762. struct {
  763. union {
  764. BitField<0, 16, u32> x;
  765. BitField<16, 16, u32> width;
  766. };
  767. union {
  768. BitField<0, 16, u32> y;
  769. BitField<16, 16, u32> height;
  770. };
  771. } render_area;
  772. INSERT_PADDING_WORDS_NOINIT(0x3F);
  773. union {
  774. BitField<0, 4, u32> stencil;
  775. BitField<4, 4, u32> unknown;
  776. BitField<8, 4, u32> scissor;
  777. BitField<12, 4, u32> viewport;
  778. } clear_flags;
  779. INSERT_PADDING_WORDS_NOINIT(0x10);
  780. u32 fill_rectangle;
  781. INSERT_PADDING_WORDS_NOINIT(0x8);
  782. std::array<VertexAttribute, NumVertexAttributes> vertex_attrib_format;
  783. std::array<MsaaSampleLocation, 4> multisample_sample_locations;
  784. INSERT_PADDING_WORDS_NOINIT(0x2);
  785. union {
  786. BitField<0, 1, u32> enable;
  787. BitField<4, 3, u32> target;
  788. } multisample_coverage_to_color;
  789. INSERT_PADDING_WORDS_NOINIT(0x8);
  790. struct {
  791. union {
  792. BitField<0, 4, u32> count;
  793. BitField<4, 3, u32> map_0;
  794. BitField<7, 3, u32> map_1;
  795. BitField<10, 3, u32> map_2;
  796. BitField<13, 3, u32> map_3;
  797. BitField<16, 3, u32> map_4;
  798. BitField<19, 3, u32> map_5;
  799. BitField<22, 3, u32> map_6;
  800. BitField<25, 3, u32> map_7;
  801. };
  802. u32 Map(std::size_t index) const {
  803. const std::array<u32, NumRenderTargets> maps{map_0, map_1, map_2, map_3,
  804. map_4, map_5, map_6, map_7};
  805. ASSERT(index < maps.size());
  806. return maps[index];
  807. }
  808. } rt_control;
  809. INSERT_PADDING_WORDS_NOINIT(0x2);
  810. u32 zeta_width;
  811. u32 zeta_height;
  812. union {
  813. BitField<0, 16, u32> zeta_depth;
  814. BitField<16, 1, u32> zeta_volume;
  815. };
  816. SamplerIndex sampler_index;
  817. INSERT_PADDING_WORDS_NOINIT(0x25);
  818. u32 depth_test_enable;
  819. INSERT_PADDING_WORDS_NOINIT(0x5);
  820. u32 independent_blend_enable;
  821. u32 depth_write_enabled;
  822. u32 alpha_test_enabled;
  823. INSERT_PADDING_WORDS_NOINIT(0x6);
  824. u32 d3d_cull_mode;
  825. ComparisonOp depth_test_func;
  826. float alpha_test_ref;
  827. ComparisonOp alpha_test_func;
  828. u32 draw_tfb_stride;
  829. struct {
  830. float r;
  831. float g;
  832. float b;
  833. float a;
  834. } blend_color;
  835. INSERT_PADDING_WORDS_NOINIT(0x4);
  836. struct {
  837. u32 separate_alpha;
  838. Blend::Equation equation_rgb;
  839. Blend::Factor factor_source_rgb;
  840. Blend::Factor factor_dest_rgb;
  841. Blend::Equation equation_a;
  842. Blend::Factor factor_source_a;
  843. INSERT_PADDING_WORDS_NOINIT(1);
  844. Blend::Factor factor_dest_a;
  845. u32 enable_common;
  846. u32 enable[NumRenderTargets];
  847. } blend;
  848. u32 stencil_enable;
  849. StencilOp stencil_front_op_fail;
  850. StencilOp stencil_front_op_zfail;
  851. StencilOp stencil_front_op_zpass;
  852. ComparisonOp stencil_front_func_func;
  853. s32 stencil_front_func_ref;
  854. u32 stencil_front_func_mask;
  855. u32 stencil_front_mask;
  856. INSERT_PADDING_WORDS_NOINIT(0x2);
  857. u32 frag_color_clamp;
  858. union {
  859. BitField<0, 1, u32> y_negate;
  860. BitField<4, 1, u32> triangle_rast_flip;
  861. } screen_y_control;
  862. float line_width_smooth;
  863. float line_width_aliased;
  864. INSERT_PADDING_WORDS_NOINIT(0x1B);
  865. u32 invalidate_sampler_cache_no_wfi;
  866. u32 invalidate_texture_header_cache_no_wfi;
  867. INSERT_PADDING_WORDS_NOINIT(0x2);
  868. u32 vb_element_base;
  869. u32 vb_base_instance;
  870. INSERT_PADDING_WORDS_NOINIT(0x35);
  871. u32 clip_distance_enabled;
  872. u32 samplecnt_enable;
  873. float point_size;
  874. INSERT_PADDING_WORDS_NOINIT(0x1);
  875. u32 point_sprite_enable;
  876. INSERT_PADDING_WORDS_NOINIT(0x3);
  877. CounterReset counter_reset;
  878. u32 multisample_enable;
  879. u32 zeta_enable;
  880. union {
  881. BitField<0, 1, u32> alpha_to_coverage;
  882. BitField<4, 1, u32> alpha_to_one;
  883. } multisample_control;
  884. INSERT_PADDING_WORDS_NOINIT(0x4);
  885. struct {
  886. u32 address_high;
  887. u32 address_low;
  888. ConditionMode mode;
  889. GPUVAddr Address() const {
  890. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  891. address_low);
  892. }
  893. } condition;
  894. struct {
  895. u32 address_high;
  896. u32 address_low;
  897. u32 limit;
  898. GPUVAddr Address() const {
  899. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  900. address_low);
  901. }
  902. } tsc;
  903. INSERT_PADDING_WORDS_NOINIT(0x1);
  904. float polygon_offset_factor;
  905. u32 line_smooth_enable;
  906. struct {
  907. u32 address_high;
  908. u32 address_low;
  909. u32 limit;
  910. GPUVAddr Address() const {
  911. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  912. address_low);
  913. }
  914. } tic;
  915. INSERT_PADDING_WORDS_NOINIT(0x5);
  916. u32 stencil_two_side_enable;
  917. StencilOp stencil_back_op_fail;
  918. StencilOp stencil_back_op_zfail;
  919. StencilOp stencil_back_op_zpass;
  920. ComparisonOp stencil_back_func_func;
  921. INSERT_PADDING_WORDS_NOINIT(0x4);
  922. u32 framebuffer_srgb;
  923. float polygon_offset_units;
  924. INSERT_PADDING_WORDS_NOINIT(0x4);
  925. Tegra::Texture::MsaaMode multisample_mode;
  926. INSERT_PADDING_WORDS_NOINIT(0xC);
  927. union {
  928. BitField<2, 1, u32> coord_origin;
  929. BitField<3, 10, u32> enable;
  930. } point_coord_replace;
  931. struct {
  932. u32 code_address_high;
  933. u32 code_address_low;
  934. GPUVAddr CodeAddress() const {
  935. return static_cast<GPUVAddr>(
  936. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  937. }
  938. } code_address;
  939. INSERT_PADDING_WORDS_NOINIT(1);
  940. struct {
  941. u32 vertex_end_gl;
  942. union {
  943. u32 vertex_begin_gl;
  944. BitField<0, 16, PrimitiveTopology> topology;
  945. BitField<26, 1, u32> instance_next;
  946. BitField<27, 1, u32> instance_cont;
  947. };
  948. } draw;
  949. INSERT_PADDING_WORDS_NOINIT(0xA);
  950. struct {
  951. u32 enabled;
  952. u32 index;
  953. } primitive_restart;
  954. INSERT_PADDING_WORDS_NOINIT(0x5F);
  955. struct {
  956. u32 start_addr_high;
  957. u32 start_addr_low;
  958. u32 end_addr_high;
  959. u32 end_addr_low;
  960. IndexFormat format;
  961. u32 first;
  962. u32 count;
  963. unsigned FormatSizeInBytes() const {
  964. switch (format) {
  965. case IndexFormat::UnsignedByte:
  966. return 1;
  967. case IndexFormat::UnsignedShort:
  968. return 2;
  969. case IndexFormat::UnsignedInt:
  970. return 4;
  971. }
  972. UNREACHABLE();
  973. return 1;
  974. }
  975. GPUVAddr StartAddress() const {
  976. return static_cast<GPUVAddr>(
  977. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  978. }
  979. GPUVAddr EndAddress() const {
  980. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  981. end_addr_low);
  982. }
  983. /// Adjust the index buffer offset so it points to the first desired index.
  984. GPUVAddr IndexStart() const {
  985. return StartAddress() + static_cast<size_t>(first) *
  986. static_cast<size_t>(FormatSizeInBytes());
  987. }
  988. } index_array;
  989. INSERT_PADDING_WORDS_NOINIT(0x7);
  990. INSERT_PADDING_WORDS_NOINIT(0x1F);
  991. float polygon_offset_clamp;
  992. struct {
  993. u32 is_instanced[NumVertexArrays];
  994. /// Returns whether the vertex array specified by index is supposed to be
  995. /// accessed per instance or not.
  996. bool IsInstancingEnabled(std::size_t index) const {
  997. return is_instanced[index];
  998. }
  999. } instanced_arrays;
  1000. INSERT_PADDING_WORDS_NOINIT(0x4);
  1001. union {
  1002. BitField<0, 1, u32> enable;
  1003. BitField<4, 8, u32> unk4;
  1004. } vp_point_size;
  1005. INSERT_PADDING_WORDS_NOINIT(1);
  1006. u32 cull_test_enabled;
  1007. FrontFace front_face;
  1008. CullFace cull_face;
  1009. u32 pixel_center_integer;
  1010. INSERT_PADDING_WORDS_NOINIT(0x1);
  1011. u32 viewport_transform_enabled;
  1012. INSERT_PADDING_WORDS_NOINIT(0x3);
  1013. union {
  1014. BitField<0, 1, u32> depth_range_0_1;
  1015. BitField<3, 1, u32> depth_clamp_near;
  1016. BitField<4, 1, u32> depth_clamp_far;
  1017. BitField<11, 1, u32> depth_clamp_disabled;
  1018. } view_volume_clip_control;
  1019. INSERT_PADDING_WORDS_NOINIT(0x1F);
  1020. u32 depth_bounds_enable;
  1021. INSERT_PADDING_WORDS_NOINIT(1);
  1022. struct {
  1023. u32 enable;
  1024. LogicOperation operation;
  1025. } logic_op;
  1026. INSERT_PADDING_WORDS_NOINIT(0x1);
  1027. union {
  1028. u32 raw;
  1029. BitField<0, 1, u32> Z;
  1030. BitField<1, 1, u32> S;
  1031. BitField<2, 1, u32> R;
  1032. BitField<3, 1, u32> G;
  1033. BitField<4, 1, u32> B;
  1034. BitField<5, 1, u32> A;
  1035. BitField<6, 4, u32> RT;
  1036. BitField<10, 11, u32> layer;
  1037. } clear_buffers;
  1038. INSERT_PADDING_WORDS_NOINIT(0xB);
  1039. std::array<ColorMask, NumRenderTargets> color_mask;
  1040. INSERT_PADDING_WORDS_NOINIT(0x38);
  1041. struct {
  1042. u32 query_address_high;
  1043. u32 query_address_low;
  1044. u32 query_sequence;
  1045. union {
  1046. u32 raw;
  1047. BitField<0, 2, QueryOperation> operation;
  1048. BitField<4, 1, u32> fence;
  1049. BitField<12, 4, QueryUnit> unit;
  1050. BitField<16, 1, QuerySyncCondition> sync_cond;
  1051. BitField<23, 5, QuerySelect> select;
  1052. BitField<28, 1, u32> short_query;
  1053. } query_get;
  1054. GPUVAddr QueryAddress() const {
  1055. return static_cast<GPUVAddr>(
  1056. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  1057. }
  1058. } query;
  1059. INSERT_PADDING_WORDS_NOINIT(0x3C);
  1060. struct {
  1061. union {
  1062. BitField<0, 12, u32> stride;
  1063. BitField<12, 1, u32> enable;
  1064. };
  1065. u32 start_high;
  1066. u32 start_low;
  1067. u32 divisor;
  1068. GPUVAddr StartAddress() const {
  1069. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  1070. start_low);
  1071. }
  1072. bool IsEnabled() const {
  1073. return enable != 0 && StartAddress() != 0;
  1074. }
  1075. } vertex_array[NumVertexArrays];
  1076. Blend independent_blend[NumRenderTargets];
  1077. struct {
  1078. u32 limit_high;
  1079. u32 limit_low;
  1080. GPUVAddr LimitAddress() const {
  1081. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  1082. limit_low);
  1083. }
  1084. } vertex_array_limit[NumVertexArrays];
  1085. struct {
  1086. union {
  1087. BitField<0, 1, u32> enable;
  1088. BitField<4, 4, ShaderProgram> program;
  1089. };
  1090. u32 offset;
  1091. INSERT_PADDING_WORDS_NOINIT(14);
  1092. } shader_config[MaxShaderProgram];
  1093. INSERT_PADDING_WORDS_NOINIT(0x60);
  1094. u32 firmware[0x20];
  1095. struct {
  1096. u32 cb_size;
  1097. u32 cb_address_high;
  1098. u32 cb_address_low;
  1099. u32 cb_pos;
  1100. std::array<u32, NumCBData> cb_data;
  1101. GPUVAddr BufferAddress() const {
  1102. return static_cast<GPUVAddr>(
  1103. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  1104. }
  1105. } const_buffer;
  1106. INSERT_PADDING_WORDS_NOINIT(0x10);
  1107. struct {
  1108. union {
  1109. u32 raw_config;
  1110. BitField<0, 1, u32> valid;
  1111. BitField<4, 5, u32> index;
  1112. };
  1113. INSERT_PADDING_WORDS_NOINIT(7);
  1114. } cb_bind[MaxShaderStage];
  1115. INSERT_PADDING_WORDS_NOINIT(0x56);
  1116. u32 tex_cb_index;
  1117. INSERT_PADDING_WORDS_NOINIT(0x7D);
  1118. std::array<std::array<u8, 128>, NumTransformFeedbackBuffers> tfb_varying_locs;
  1119. INSERT_PADDING_WORDS_NOINIT(0x298);
  1120. struct {
  1121. /// Compressed address of a buffer that holds information about bound SSBOs.
  1122. /// This address is usually bound to c0 in the shaders.
  1123. u32 buffer_address;
  1124. GPUVAddr BufferAddress() const {
  1125. return static_cast<GPUVAddr>(buffer_address) << 8;
  1126. }
  1127. } ssbo_info;
  1128. INSERT_PADDING_WORDS_NOINIT(0x11);
  1129. struct {
  1130. u32 address[MaxShaderStage];
  1131. u32 size[MaxShaderStage];
  1132. } tex_info_buffers;
  1133. INSERT_PADDING_WORDS_NOINIT(0xCC);
  1134. };
  1135. std::array<u32, NUM_REGS> reg_array;
  1136. };
  1137. };
  1138. Regs regs{};
  1139. /// Store temporary hw register values, used by some calls to restore state after a operation
  1140. Regs shadow_state;
  1141. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  1142. static_assert(std::is_trivially_copyable_v<Regs>, "Maxwell3D Regs must be trivially copyable");
  1143. struct State {
  1144. struct ShaderStageInfo {
  1145. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  1146. };
  1147. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  1148. u32 current_instance = 0; ///< Current instance to be used to simulate instanced rendering.
  1149. };
  1150. State state{};
  1151. /// Reads a register value located at the input method address
  1152. u32 GetRegisterValue(u32 method) const;
  1153. /// Write the value to the register identified by method.
  1154. void CallMethod(u32 method, u32 method_argument, bool is_last_call) override;
  1155. /// Write multiple values to the register identified by method.
  1156. void CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  1157. u32 methods_pending) override;
  1158. /// Write the value to the register identified by method.
  1159. void CallMethodFromMME(u32 method, u32 method_argument);
  1160. void FlushMMEInlineDraw();
  1161. u32 AccessConstBuffer32(ShaderType stage, u64 const_buffer, u64 offset) const override;
  1162. SamplerDescriptor AccessBoundSampler(ShaderType stage, u64 offset) const override;
  1163. SamplerDescriptor AccessBindlessSampler(ShaderType stage, u64 const_buffer,
  1164. u64 offset) const override;
  1165. SamplerDescriptor AccessSampler(u32 handle) const override;
  1166. u32 GetBoundBuffer() const override {
  1167. return regs.tex_cb_index;
  1168. }
  1169. VideoCore::GuestDriverProfile& AccessGuestDriverProfile() override;
  1170. const VideoCore::GuestDriverProfile& AccessGuestDriverProfile() const override;
  1171. bool ShouldExecute() const {
  1172. return execute_on;
  1173. }
  1174. VideoCore::RasterizerInterface& Rasterizer() {
  1175. return *rasterizer;
  1176. }
  1177. const VideoCore::RasterizerInterface& Rasterizer() const {
  1178. return *rasterizer;
  1179. }
  1180. enum class MMEDrawMode : u32 {
  1181. Undefined,
  1182. Array,
  1183. Indexed,
  1184. };
  1185. struct MMEDrawState {
  1186. MMEDrawMode current_mode{MMEDrawMode::Undefined};
  1187. u32 current_count{};
  1188. u32 instance_count{};
  1189. bool instance_mode{};
  1190. bool gl_begin_consume{};
  1191. u32 gl_end_count{};
  1192. } mme_draw;
  1193. struct DirtyState {
  1194. using Flags = std::bitset<std::numeric_limits<u8>::max()>;
  1195. using Table = std::array<u8, Regs::NUM_REGS>;
  1196. using Tables = std::array<Table, 2>;
  1197. Flags flags;
  1198. Tables tables{};
  1199. } dirty;
  1200. private:
  1201. void InitializeRegisterDefaults();
  1202. void ProcessMacro(u32 method, const u32* base_start, u32 amount, bool is_last_call);
  1203. u32 ProcessShadowRam(u32 method, u32 argument);
  1204. void ProcessDirtyRegisters(u32 method, u32 argument);
  1205. void ProcessMethodCall(u32 method, u32 argument, u32 nonshadow_argument, bool is_last_call);
  1206. /// Retrieves information about a specific TIC entry from the TIC buffer.
  1207. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  1208. /// Retrieves information about a specific TSC entry from the TSC buffer.
  1209. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  1210. /**
  1211. * Call a macro on this engine.
  1212. *
  1213. * @param method Method to call
  1214. * @param parameters Arguments to the method call
  1215. */
  1216. void CallMacroMethod(u32 method, const std::vector<u32>& parameters);
  1217. /// Handles writes to the macro uploading register.
  1218. void ProcessMacroUpload(u32 data);
  1219. /// Handles writes to the macro bind register.
  1220. void ProcessMacroBind(u32 data);
  1221. /// Handles firmware blob 4
  1222. void ProcessFirmwareCall4();
  1223. /// Handles a write to the CLEAR_BUFFERS register.
  1224. void ProcessClearBuffers();
  1225. /// Handles a write to the QUERY_GET register.
  1226. void ProcessQueryGet();
  1227. /// Writes the query result accordingly.
  1228. void StampQueryResult(u64 payload, bool long_query);
  1229. /// Handles conditional rendering.
  1230. void ProcessQueryCondition();
  1231. /// Handles counter resets.
  1232. void ProcessCounterReset();
  1233. /// Handles writes to syncing register.
  1234. void ProcessSyncPoint();
  1235. /// Handles a write to the CB_DATA[i] register.
  1236. void StartCBData(u32 method);
  1237. void ProcessCBData(u32 value);
  1238. void ProcessCBMultiData(u32 method, const u32* start_base, u32 amount);
  1239. void FinishCBData();
  1240. /// Handles a write to the CB_BIND register.
  1241. void ProcessCBBind(size_t stage_index);
  1242. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  1243. void DrawArrays();
  1244. // Handles a instance drawcall from MME
  1245. void StepInstance(MMEDrawMode expected_mode, u32 count);
  1246. /// Returns a query's value or an empty object if the value will be deferred through a cache.
  1247. std::optional<u64> GetQueryResult();
  1248. Core::System& system;
  1249. MemoryManager& memory_manager;
  1250. VideoCore::RasterizerInterface* rasterizer = nullptr;
  1251. /// Start offsets of each macro in macro_memory
  1252. std::array<u32, 0x80> macro_positions{};
  1253. std::array<bool, Regs::NUM_REGS> mme_inline{};
  1254. /// Macro method that is currently being executed / being fed parameters.
  1255. u32 executing_macro = 0;
  1256. /// Parameters that have been submitted to the macro call so far.
  1257. std::vector<u32> macro_params;
  1258. /// Interpreter for the macro codes uploaded to the GPU.
  1259. std::unique_ptr<MacroEngine> macro_engine;
  1260. static constexpr u32 null_cb_data = 0xFFFFFFFF;
  1261. struct CBDataState {
  1262. std::array<std::array<u32, 0x4000>, 16> buffer;
  1263. u32 current{null_cb_data};
  1264. u32 id{null_cb_data};
  1265. u32 start_pos{};
  1266. u32 counter{};
  1267. };
  1268. CBDataState cb_data_state;
  1269. Upload::State upload_state;
  1270. bool execute_on{true};
  1271. };
  1272. #define ASSERT_REG_POSITION(field_name, position) \
  1273. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  1274. "Field " #field_name " has invalid position")
  1275. ASSERT_REG_POSITION(wait_for_idle, 0x44);
  1276. ASSERT_REG_POSITION(macros, 0x45);
  1277. ASSERT_REG_POSITION(shadow_ram_control, 0x49);
  1278. ASSERT_REG_POSITION(upload, 0x60);
  1279. ASSERT_REG_POSITION(exec_upload, 0x6C);
  1280. ASSERT_REG_POSITION(data_upload, 0x6D);
  1281. ASSERT_REG_POSITION(force_early_fragment_tests, 0x84);
  1282. ASSERT_REG_POSITION(sync_info, 0xB2);
  1283. ASSERT_REG_POSITION(tess_mode, 0xC8);
  1284. ASSERT_REG_POSITION(tess_level_outer, 0xC9);
  1285. ASSERT_REG_POSITION(tess_level_inner, 0xCD);
  1286. ASSERT_REG_POSITION(rasterize_enable, 0xDF);
  1287. ASSERT_REG_POSITION(tfb_bindings, 0xE0);
  1288. ASSERT_REG_POSITION(tfb_layouts, 0x1C0);
  1289. ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
  1290. ASSERT_REG_POSITION(rt, 0x200);
  1291. ASSERT_REG_POSITION(viewport_transform, 0x280);
  1292. ASSERT_REG_POSITION(viewports, 0x300);
  1293. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  1294. ASSERT_REG_POSITION(depth_mode, 0x35F);
  1295. ASSERT_REG_POSITION(clear_color[0], 0x360);
  1296. ASSERT_REG_POSITION(clear_depth, 0x364);
  1297. ASSERT_REG_POSITION(clear_stencil, 0x368);
  1298. ASSERT_REG_POSITION(polygon_mode_front, 0x36B);
  1299. ASSERT_REG_POSITION(polygon_mode_back, 0x36C);
  1300. ASSERT_REG_POSITION(polygon_offset_point_enable, 0x370);
  1301. ASSERT_REG_POSITION(polygon_offset_line_enable, 0x371);
  1302. ASSERT_REG_POSITION(polygon_offset_fill_enable, 0x372);
  1303. ASSERT_REG_POSITION(patch_vertices, 0x373);
  1304. ASSERT_REG_POSITION(fragment_barrier, 0x378);
  1305. ASSERT_REG_POSITION(scissor_test, 0x380);
  1306. ASSERT_REG_POSITION(stencil_back_func_ref, 0x3D5);
  1307. ASSERT_REG_POSITION(stencil_back_mask, 0x3D6);
  1308. ASSERT_REG_POSITION(stencil_back_func_mask, 0x3D7);
  1309. ASSERT_REG_POSITION(invalidate_texture_data_cache, 0x3DD);
  1310. ASSERT_REG_POSITION(tiled_cache_barrier, 0x3DF);
  1311. ASSERT_REG_POSITION(color_mask_common, 0x3E4);
  1312. ASSERT_REG_POSITION(depth_bounds, 0x3E7);
  1313. ASSERT_REG_POSITION(rt_separate_frag_data, 0x3EB);
  1314. ASSERT_REG_POSITION(multisample_raster_enable, 0x3ED);
  1315. ASSERT_REG_POSITION(multisample_raster_samples, 0x3EE);
  1316. ASSERT_REG_POSITION(multisample_sample_mask, 0x3EF);
  1317. ASSERT_REG_POSITION(zeta, 0x3F8);
  1318. ASSERT_REG_POSITION(render_area, 0x3FD);
  1319. ASSERT_REG_POSITION(clear_flags, 0x43E);
  1320. ASSERT_REG_POSITION(fill_rectangle, 0x44F);
  1321. ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
  1322. ASSERT_REG_POSITION(multisample_sample_locations, 0x478);
  1323. ASSERT_REG_POSITION(multisample_coverage_to_color, 0x47E);
  1324. ASSERT_REG_POSITION(rt_control, 0x487);
  1325. ASSERT_REG_POSITION(zeta_width, 0x48a);
  1326. ASSERT_REG_POSITION(zeta_height, 0x48b);
  1327. ASSERT_REG_POSITION(zeta_depth, 0x48c);
  1328. ASSERT_REG_POSITION(sampler_index, 0x48D);
  1329. ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
  1330. ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
  1331. ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
  1332. ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
  1333. ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
  1334. ASSERT_REG_POSITION(depth_test_func, 0x4C3);
  1335. ASSERT_REG_POSITION(alpha_test_ref, 0x4C4);
  1336. ASSERT_REG_POSITION(alpha_test_func, 0x4C5);
  1337. ASSERT_REG_POSITION(draw_tfb_stride, 0x4C6);
  1338. ASSERT_REG_POSITION(blend_color, 0x4C7);
  1339. ASSERT_REG_POSITION(blend, 0x4CF);
  1340. ASSERT_REG_POSITION(stencil_enable, 0x4E0);
  1341. ASSERT_REG_POSITION(stencil_front_op_fail, 0x4E1);
  1342. ASSERT_REG_POSITION(stencil_front_op_zfail, 0x4E2);
  1343. ASSERT_REG_POSITION(stencil_front_op_zpass, 0x4E3);
  1344. ASSERT_REG_POSITION(stencil_front_func_func, 0x4E4);
  1345. ASSERT_REG_POSITION(stencil_front_func_ref, 0x4E5);
  1346. ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
  1347. ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
  1348. ASSERT_REG_POSITION(frag_color_clamp, 0x4EA);
  1349. ASSERT_REG_POSITION(screen_y_control, 0x4EB);
  1350. ASSERT_REG_POSITION(line_width_smooth, 0x4EC);
  1351. ASSERT_REG_POSITION(line_width_aliased, 0x4ED);
  1352. ASSERT_REG_POSITION(invalidate_sampler_cache_no_wfi, 0x509);
  1353. ASSERT_REG_POSITION(invalidate_texture_header_cache_no_wfi, 0x50A);
  1354. ASSERT_REG_POSITION(vb_element_base, 0x50D);
  1355. ASSERT_REG_POSITION(vb_base_instance, 0x50E);
  1356. ASSERT_REG_POSITION(clip_distance_enabled, 0x544);
  1357. ASSERT_REG_POSITION(samplecnt_enable, 0x545);
  1358. ASSERT_REG_POSITION(point_size, 0x546);
  1359. ASSERT_REG_POSITION(point_sprite_enable, 0x548);
  1360. ASSERT_REG_POSITION(counter_reset, 0x54C);
  1361. ASSERT_REG_POSITION(multisample_enable, 0x54D);
  1362. ASSERT_REG_POSITION(zeta_enable, 0x54E);
  1363. ASSERT_REG_POSITION(multisample_control, 0x54F);
  1364. ASSERT_REG_POSITION(condition, 0x554);
  1365. ASSERT_REG_POSITION(tsc, 0x557);
  1366. ASSERT_REG_POSITION(polygon_offset_factor, 0x55B);
  1367. ASSERT_REG_POSITION(line_smooth_enable, 0x55C);
  1368. ASSERT_REG_POSITION(tic, 0x55D);
  1369. ASSERT_REG_POSITION(stencil_two_side_enable, 0x565);
  1370. ASSERT_REG_POSITION(stencil_back_op_fail, 0x566);
  1371. ASSERT_REG_POSITION(stencil_back_op_zfail, 0x567);
  1372. ASSERT_REG_POSITION(stencil_back_op_zpass, 0x568);
  1373. ASSERT_REG_POSITION(stencil_back_func_func, 0x569);
  1374. ASSERT_REG_POSITION(framebuffer_srgb, 0x56E);
  1375. ASSERT_REG_POSITION(polygon_offset_units, 0x56F);
  1376. ASSERT_REG_POSITION(multisample_mode, 0x574);
  1377. ASSERT_REG_POSITION(point_coord_replace, 0x581);
  1378. ASSERT_REG_POSITION(code_address, 0x582);
  1379. ASSERT_REG_POSITION(draw, 0x585);
  1380. ASSERT_REG_POSITION(primitive_restart, 0x591);
  1381. ASSERT_REG_POSITION(index_array, 0x5F2);
  1382. ASSERT_REG_POSITION(polygon_offset_clamp, 0x61F);
  1383. ASSERT_REG_POSITION(instanced_arrays, 0x620);
  1384. ASSERT_REG_POSITION(vp_point_size, 0x644);
  1385. ASSERT_REG_POSITION(cull_test_enabled, 0x646);
  1386. ASSERT_REG_POSITION(front_face, 0x647);
  1387. ASSERT_REG_POSITION(cull_face, 0x648);
  1388. ASSERT_REG_POSITION(pixel_center_integer, 0x649);
  1389. ASSERT_REG_POSITION(viewport_transform_enabled, 0x64B);
  1390. ASSERT_REG_POSITION(view_volume_clip_control, 0x64F);
  1391. ASSERT_REG_POSITION(depth_bounds_enable, 0x66F);
  1392. ASSERT_REG_POSITION(logic_op, 0x671);
  1393. ASSERT_REG_POSITION(clear_buffers, 0x674);
  1394. ASSERT_REG_POSITION(color_mask, 0x680);
  1395. ASSERT_REG_POSITION(query, 0x6C0);
  1396. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  1397. ASSERT_REG_POSITION(independent_blend, 0x780);
  1398. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  1399. ASSERT_REG_POSITION(shader_config[0], 0x800);
  1400. ASSERT_REG_POSITION(firmware, 0x8C0);
  1401. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  1402. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  1403. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  1404. ASSERT_REG_POSITION(tfb_varying_locs, 0xA00);
  1405. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  1406. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  1407. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  1408. #undef ASSERT_REG_POSITION
  1409. } // namespace Tegra::Engines