maxwell_3d.h 53 KB

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