maxwell_3d.h 53 KB

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