maxwell_3d.h 55 KB

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