maxwell_3d.h 54 KB

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