maxwell_3d.h 55 KB

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