maxwell_3d.h 55 KB

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