maxwell_3d.h 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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 <unordered_map>
  8. #include <vector>
  9. #include "common/assert.h"
  10. #include "common/bit_field.h"
  11. #include "common/common_funcs.h"
  12. #include "common/common_types.h"
  13. #include "common/math_util.h"
  14. #include "video_core/gpu.h"
  15. #include "video_core/macro_interpreter.h"
  16. #include "video_core/textures/texture.h"
  17. namespace Core {
  18. class System;
  19. }
  20. namespace Tegra {
  21. class MemoryManager;
  22. }
  23. namespace VideoCore {
  24. class RasterizerInterface;
  25. }
  26. namespace Tegra::Engines {
  27. #define MAXWELL3D_REG_INDEX(field_name) \
  28. (offsetof(Tegra::Engines::Maxwell3D::Regs, field_name) / sizeof(u32))
  29. class Maxwell3D final {
  30. public:
  31. explicit Maxwell3D(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  32. MemoryManager& memory_manager);
  33. ~Maxwell3D() = default;
  34. /// Register structure of the Maxwell3D engine.
  35. /// TODO(Subv): This structure will need to be made bigger as more registers are discovered.
  36. struct Regs {
  37. static constexpr std::size_t NUM_REGS = 0xE00;
  38. static constexpr std::size_t NumRenderTargets = 8;
  39. static constexpr std::size_t NumViewports = 16;
  40. static constexpr std::size_t NumCBData = 16;
  41. static constexpr std::size_t NumVertexArrays = 32;
  42. static constexpr std::size_t NumVertexAttributes = 32;
  43. static constexpr std::size_t NumTextureSamplers = 32;
  44. static constexpr std::size_t NumClipDistances = 8;
  45. static constexpr std::size_t MaxShaderProgram = 6;
  46. static constexpr std::size_t MaxShaderStage = 5;
  47. // Maximum number of const buffers per shader stage.
  48. static constexpr std::size_t MaxConstBuffers = 18;
  49. enum class QueryMode : u32 {
  50. Write = 0,
  51. Sync = 1,
  52. // TODO(Subv): It is currently unknown what the difference between method 2 and method 0
  53. // is.
  54. Write2 = 2,
  55. };
  56. enum class QueryUnit : u32 {
  57. VFetch = 1,
  58. VP = 2,
  59. Rast = 4,
  60. StrmOut = 5,
  61. GP = 6,
  62. ZCull = 7,
  63. Prop = 10,
  64. Crop = 15,
  65. };
  66. enum class QuerySelect : u32 {
  67. Zero = 0,
  68. };
  69. enum class QuerySyncCondition : u32 {
  70. NotEqual = 0,
  71. GreaterThan = 1,
  72. };
  73. enum class ShaderProgram : u32 {
  74. VertexA = 0,
  75. VertexB = 1,
  76. TesselationControl = 2,
  77. TesselationEval = 3,
  78. Geometry = 4,
  79. Fragment = 5,
  80. };
  81. enum class ShaderStage : u32 {
  82. Vertex = 0,
  83. TesselationControl = 1,
  84. TesselationEval = 2,
  85. Geometry = 3,
  86. Fragment = 4,
  87. };
  88. struct VertexAttribute {
  89. enum class Size : u32 {
  90. Invalid = 0x0,
  91. Size_32_32_32_32 = 0x01,
  92. Size_32_32_32 = 0x02,
  93. Size_16_16_16_16 = 0x03,
  94. Size_32_32 = 0x04,
  95. Size_16_16_16 = 0x05,
  96. Size_8_8_8_8 = 0x0a,
  97. Size_16_16 = 0x0f,
  98. Size_32 = 0x12,
  99. Size_8_8_8 = 0x13,
  100. Size_8_8 = 0x18,
  101. Size_16 = 0x1b,
  102. Size_8 = 0x1d,
  103. Size_10_10_10_2 = 0x30,
  104. Size_11_11_10 = 0x31,
  105. };
  106. enum class Type : u32 {
  107. SignedNorm = 1,
  108. UnsignedNorm = 2,
  109. SignedInt = 3,
  110. UnsignedInt = 4,
  111. UnsignedScaled = 5,
  112. SignedScaled = 6,
  113. Float = 7,
  114. };
  115. union {
  116. BitField<0, 5, u32> buffer;
  117. BitField<6, 1, u32> constant;
  118. BitField<7, 14, u32> offset;
  119. BitField<21, 6, Size> size;
  120. BitField<27, 3, Type> type;
  121. BitField<31, 1, u32> bgra;
  122. u32 hex;
  123. };
  124. u32 ComponentCount() const {
  125. switch (size) {
  126. case Size::Size_32_32_32_32:
  127. return 4;
  128. case Size::Size_32_32_32:
  129. return 3;
  130. case Size::Size_16_16_16_16:
  131. return 4;
  132. case Size::Size_32_32:
  133. return 2;
  134. case Size::Size_16_16_16:
  135. return 3;
  136. case Size::Size_8_8_8_8:
  137. return 4;
  138. case Size::Size_16_16:
  139. return 2;
  140. case Size::Size_32:
  141. return 1;
  142. case Size::Size_8_8_8:
  143. return 3;
  144. case Size::Size_8_8:
  145. return 2;
  146. case Size::Size_16:
  147. return 1;
  148. case Size::Size_8:
  149. return 1;
  150. case Size::Size_10_10_10_2:
  151. return 4;
  152. case Size::Size_11_11_10:
  153. return 3;
  154. default:
  155. UNREACHABLE();
  156. return 1;
  157. }
  158. }
  159. u32 SizeInBytes() const {
  160. switch (size) {
  161. case Size::Size_32_32_32_32:
  162. return 16;
  163. case Size::Size_32_32_32:
  164. return 12;
  165. case Size::Size_16_16_16_16:
  166. return 8;
  167. case Size::Size_32_32:
  168. return 8;
  169. case Size::Size_16_16_16:
  170. return 6;
  171. case Size::Size_8_8_8_8:
  172. return 4;
  173. case Size::Size_16_16:
  174. return 4;
  175. case Size::Size_32:
  176. return 4;
  177. case Size::Size_8_8_8:
  178. return 3;
  179. case Size::Size_8_8:
  180. return 2;
  181. case Size::Size_16:
  182. return 2;
  183. case Size::Size_8:
  184. return 1;
  185. case Size::Size_10_10_10_2:
  186. return 4;
  187. case Size::Size_11_11_10:
  188. return 4;
  189. default:
  190. UNREACHABLE();
  191. }
  192. }
  193. std::string SizeString() const {
  194. switch (size) {
  195. case Size::Size_32_32_32_32:
  196. return "32_32_32_32";
  197. case Size::Size_32_32_32:
  198. return "32_32_32";
  199. case Size::Size_16_16_16_16:
  200. return "16_16_16_16";
  201. case Size::Size_32_32:
  202. return "32_32";
  203. case Size::Size_16_16_16:
  204. return "16_16_16";
  205. case Size::Size_8_8_8_8:
  206. return "8_8_8_8";
  207. case Size::Size_16_16:
  208. return "16_16";
  209. case Size::Size_32:
  210. return "32";
  211. case Size::Size_8_8_8:
  212. return "8_8_8";
  213. case Size::Size_8_8:
  214. return "8_8";
  215. case Size::Size_16:
  216. return "16";
  217. case Size::Size_8:
  218. return "8";
  219. case Size::Size_10_10_10_2:
  220. return "10_10_10_2";
  221. case Size::Size_11_11_10:
  222. return "11_11_10";
  223. }
  224. UNREACHABLE();
  225. return {};
  226. }
  227. std::string TypeString() const {
  228. switch (type) {
  229. case Type::SignedNorm:
  230. return "SNORM";
  231. case Type::UnsignedNorm:
  232. return "UNORM";
  233. case Type::SignedInt:
  234. return "SINT";
  235. case Type::UnsignedInt:
  236. return "UINT";
  237. case Type::UnsignedScaled:
  238. return "USCALED";
  239. case Type::SignedScaled:
  240. return "SSCALED";
  241. case Type::Float:
  242. return "FLOAT";
  243. }
  244. UNREACHABLE();
  245. return {};
  246. }
  247. bool IsNormalized() const {
  248. return (type == Type::SignedNorm) || (type == Type::UnsignedNorm);
  249. }
  250. bool IsValid() const {
  251. return size != Size::Invalid;
  252. }
  253. bool operator<(const VertexAttribute& other) const {
  254. return hex < other.hex;
  255. }
  256. };
  257. enum class PrimitiveTopology : u32 {
  258. Points = 0x0,
  259. Lines = 0x1,
  260. LineLoop = 0x2,
  261. LineStrip = 0x3,
  262. Triangles = 0x4,
  263. TriangleStrip = 0x5,
  264. TriangleFan = 0x6,
  265. Quads = 0x7,
  266. QuadStrip = 0x8,
  267. Polygon = 0x9,
  268. LinesAdjacency = 0xa,
  269. LineStripAdjacency = 0xb,
  270. TrianglesAdjacency = 0xc,
  271. TriangleStripAdjacency = 0xd,
  272. Patches = 0xe,
  273. };
  274. enum class IndexFormat : u32 {
  275. UnsignedByte = 0x0,
  276. UnsignedShort = 0x1,
  277. UnsignedInt = 0x2,
  278. };
  279. enum class ComparisonOp : u32 {
  280. // These values are used by Nouveau and most games, they correspond to the OpenGL token
  281. // values for these operations.
  282. Never = 0x200,
  283. Less = 0x201,
  284. Equal = 0x202,
  285. LessEqual = 0x203,
  286. Greater = 0x204,
  287. NotEqual = 0x205,
  288. GreaterEqual = 0x206,
  289. Always = 0x207,
  290. // These values are used by some games, they seem to be NV04 values.
  291. NeverOld = 1,
  292. LessOld = 2,
  293. EqualOld = 3,
  294. LessEqualOld = 4,
  295. GreaterOld = 5,
  296. NotEqualOld = 6,
  297. GreaterEqualOld = 7,
  298. AlwaysOld = 8,
  299. };
  300. enum class LogicOperation : u32 {
  301. Clear = 0x1500,
  302. And = 0x1501,
  303. AndReverse = 0x1502,
  304. Copy = 0x1503,
  305. AndInverted = 0x1504,
  306. NoOp = 0x1505,
  307. Xor = 0x1506,
  308. Or = 0x1507,
  309. Nor = 0x1508,
  310. Equiv = 0x1509,
  311. Invert = 0x150A,
  312. OrReverse = 0x150B,
  313. CopyInverted = 0x150C,
  314. OrInverted = 0x150D,
  315. Nand = 0x150E,
  316. Set = 0x150F,
  317. };
  318. enum class StencilOp : u32 {
  319. Keep = 1,
  320. Zero = 2,
  321. Replace = 3,
  322. Incr = 4,
  323. Decr = 5,
  324. Invert = 6,
  325. IncrWrap = 7,
  326. DecrWrap = 8,
  327. KeepOGL = 0x1E00,
  328. ZeroOGL = 0,
  329. ReplaceOGL = 0x1E01,
  330. IncrOGL = 0x1E02,
  331. DecrOGL = 0x1E03,
  332. InvertOGL = 0x150A,
  333. IncrWrapOGL = 0x8507,
  334. DecrWrapOGL = 0x8508,
  335. };
  336. enum class MemoryLayout : u32 {
  337. Linear = 0,
  338. BlockLinear = 1,
  339. };
  340. enum class InvMemoryLayout : u32 {
  341. BlockLinear = 0,
  342. Linear = 1,
  343. };
  344. struct Cull {
  345. enum class FrontFace : u32 {
  346. ClockWise = 0x0900,
  347. CounterClockWise = 0x0901,
  348. };
  349. enum class CullFace : u32 {
  350. Front = 0x0404,
  351. Back = 0x0405,
  352. FrontAndBack = 0x0408,
  353. };
  354. u32 enabled;
  355. FrontFace front_face;
  356. CullFace cull_face;
  357. };
  358. struct Blend {
  359. enum class Equation : u32 {
  360. Add = 1,
  361. Subtract = 2,
  362. ReverseSubtract = 3,
  363. Min = 4,
  364. Max = 5,
  365. // These values are used by Nouveau and some games.
  366. AddGL = 0x8006,
  367. SubtractGL = 0x8007,
  368. ReverseSubtractGL = 0x8008,
  369. MinGL = 0x800a,
  370. MaxGL = 0x800b
  371. };
  372. enum class Factor : u32 {
  373. Zero = 0x1,
  374. One = 0x2,
  375. SourceColor = 0x3,
  376. OneMinusSourceColor = 0x4,
  377. SourceAlpha = 0x5,
  378. OneMinusSourceAlpha = 0x6,
  379. DestAlpha = 0x7,
  380. OneMinusDestAlpha = 0x8,
  381. DestColor = 0x9,
  382. OneMinusDestColor = 0xa,
  383. SourceAlphaSaturate = 0xb,
  384. Source1Color = 0x10,
  385. OneMinusSource1Color = 0x11,
  386. Source1Alpha = 0x12,
  387. OneMinusSource1Alpha = 0x13,
  388. ConstantColor = 0x61,
  389. OneMinusConstantColor = 0x62,
  390. ConstantAlpha = 0x63,
  391. OneMinusConstantAlpha = 0x64,
  392. // These values are used by Nouveau and some games.
  393. ZeroGL = 0x4000,
  394. OneGL = 0x4001,
  395. SourceColorGL = 0x4300,
  396. OneMinusSourceColorGL = 0x4301,
  397. SourceAlphaGL = 0x4302,
  398. OneMinusSourceAlphaGL = 0x4303,
  399. DestAlphaGL = 0x4304,
  400. OneMinusDestAlphaGL = 0x4305,
  401. DestColorGL = 0x4306,
  402. OneMinusDestColorGL = 0x4307,
  403. SourceAlphaSaturateGL = 0x4308,
  404. ConstantColorGL = 0xc001,
  405. OneMinusConstantColorGL = 0xc002,
  406. ConstantAlphaGL = 0xc003,
  407. OneMinusConstantAlphaGL = 0xc004,
  408. Source1ColorGL = 0xc900,
  409. OneMinusSource1ColorGL = 0xc901,
  410. Source1AlphaGL = 0xc902,
  411. OneMinusSource1AlphaGL = 0xc903,
  412. };
  413. u32 separate_alpha;
  414. Equation equation_rgb;
  415. Factor factor_source_rgb;
  416. Factor factor_dest_rgb;
  417. Equation equation_a;
  418. Factor factor_source_a;
  419. Factor factor_dest_a;
  420. INSERT_PADDING_WORDS(1);
  421. };
  422. struct RenderTargetConfig {
  423. u32 address_high;
  424. u32 address_low;
  425. u32 width;
  426. u32 height;
  427. Tegra::RenderTargetFormat format;
  428. union {
  429. BitField<0, 3, u32> block_width;
  430. BitField<4, 3, u32> block_height;
  431. BitField<8, 3, u32> block_depth;
  432. BitField<12, 1, InvMemoryLayout> type;
  433. } memory_layout;
  434. union {
  435. BitField<0, 16, u32> array_mode;
  436. BitField<16, 1, u32> volume;
  437. };
  438. u32 layer_stride;
  439. u32 base_layer;
  440. INSERT_PADDING_WORDS(7);
  441. GPUVAddr Address() const {
  442. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  443. address_low);
  444. }
  445. };
  446. struct ColorMask {
  447. union {
  448. u32 raw;
  449. BitField<0, 4, u32> R;
  450. BitField<4, 4, u32> G;
  451. BitField<8, 4, u32> B;
  452. BitField<12, 4, u32> A;
  453. };
  454. };
  455. struct ViewportTransform {
  456. f32 scale_x;
  457. f32 scale_y;
  458. f32 scale_z;
  459. f32 translate_x;
  460. f32 translate_y;
  461. f32 translate_z;
  462. INSERT_PADDING_WORDS(2);
  463. Common::Rectangle<s32> GetRect() const {
  464. return {
  465. GetX(), // left
  466. GetY() + GetHeight(), // top
  467. GetX() + GetWidth(), // right
  468. GetY() // bottom
  469. };
  470. };
  471. s32 GetX() const {
  472. return static_cast<s32>(std::max(0.0f, translate_x - std::fabs(scale_x)));
  473. }
  474. s32 GetY() const {
  475. return static_cast<s32>(std::max(0.0f, translate_y - std::fabs(scale_y)));
  476. }
  477. s32 GetWidth() const {
  478. return static_cast<s32>(translate_x + std::fabs(scale_x)) - GetX();
  479. }
  480. s32 GetHeight() const {
  481. return static_cast<s32>(translate_y + std::fabs(scale_y)) - GetY();
  482. }
  483. };
  484. struct ScissorTest {
  485. u32 enable;
  486. union {
  487. BitField<0, 16, u32> min_x;
  488. BitField<16, 16, u32> max_x;
  489. };
  490. union {
  491. BitField<0, 16, u32> min_y;
  492. BitField<16, 16, u32> max_y;
  493. };
  494. u32 fill;
  495. };
  496. struct ViewPort {
  497. union {
  498. BitField<0, 16, u32> x;
  499. BitField<16, 16, u32> width;
  500. };
  501. union {
  502. BitField<0, 16, u32> y;
  503. BitField<16, 16, u32> height;
  504. };
  505. float depth_range_near;
  506. float depth_range_far;
  507. };
  508. bool IsShaderConfigEnabled(std::size_t index) const {
  509. // The VertexB is always enabled.
  510. if (index == static_cast<std::size_t>(Regs::ShaderProgram::VertexB)) {
  511. return true;
  512. }
  513. return shader_config[index].enable != 0;
  514. }
  515. union {
  516. struct {
  517. INSERT_PADDING_WORDS(0x45);
  518. struct {
  519. u32 upload_address;
  520. u32 data;
  521. u32 entry;
  522. u32 bind;
  523. } macros;
  524. INSERT_PADDING_WORDS(0x188);
  525. u32 tfb_enabled;
  526. INSERT_PADDING_WORDS(0x2E);
  527. std::array<RenderTargetConfig, NumRenderTargets> rt;
  528. std::array<ViewportTransform, NumViewports> viewport_transform;
  529. std::array<ViewPort, NumViewports> viewports;
  530. INSERT_PADDING_WORDS(0x1D);
  531. struct {
  532. u32 first;
  533. u32 count;
  534. } vertex_buffer;
  535. INSERT_PADDING_WORDS(1);
  536. float clear_color[4];
  537. float clear_depth;
  538. INSERT_PADDING_WORDS(0x3);
  539. s32 clear_stencil;
  540. INSERT_PADDING_WORDS(0x7);
  541. u32 polygon_offset_point_enable;
  542. u32 polygon_offset_line_enable;
  543. u32 polygon_offset_fill_enable;
  544. INSERT_PADDING_WORDS(0xD);
  545. std::array<ScissorTest, NumViewports> scissor_test;
  546. INSERT_PADDING_WORDS(0x15);
  547. s32 stencil_back_func_ref;
  548. u32 stencil_back_mask;
  549. u32 stencil_back_func_mask;
  550. INSERT_PADDING_WORDS(0xC);
  551. u32 color_mask_common;
  552. INSERT_PADDING_WORDS(0x6);
  553. u32 rt_separate_frag_data;
  554. INSERT_PADDING_WORDS(0xC);
  555. struct {
  556. u32 address_high;
  557. u32 address_low;
  558. Tegra::DepthFormat format;
  559. union {
  560. BitField<0, 4, u32> block_width;
  561. BitField<4, 4, u32> block_height;
  562. BitField<8, 4, u32> block_depth;
  563. BitField<20, 1, InvMemoryLayout> type;
  564. } memory_layout;
  565. u32 layer_stride;
  566. GPUVAddr Address() const {
  567. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  568. address_low);
  569. }
  570. } zeta;
  571. INSERT_PADDING_WORDS(0x41);
  572. union {
  573. BitField<0, 4, u32> stencil;
  574. BitField<4, 4, u32> unknown;
  575. BitField<8, 4, u32> scissor;
  576. BitField<12, 4, u32> viewport;
  577. } clear_flags;
  578. INSERT_PADDING_WORDS(0x19);
  579. std::array<VertexAttribute, NumVertexAttributes> vertex_attrib_format;
  580. INSERT_PADDING_WORDS(0xF);
  581. struct {
  582. union {
  583. BitField<0, 4, u32> count;
  584. BitField<4, 3, u32> map_0;
  585. BitField<7, 3, u32> map_1;
  586. BitField<10, 3, u32> map_2;
  587. BitField<13, 3, u32> map_3;
  588. BitField<16, 3, u32> map_4;
  589. BitField<19, 3, u32> map_5;
  590. BitField<22, 3, u32> map_6;
  591. BitField<25, 3, u32> map_7;
  592. };
  593. u32 GetMap(std::size_t index) const {
  594. const std::array<u32, NumRenderTargets> maps{map_0, map_1, map_2, map_3,
  595. map_4, map_5, map_6, map_7};
  596. ASSERT(index < maps.size());
  597. return maps[index];
  598. }
  599. } rt_control;
  600. INSERT_PADDING_WORDS(0x2);
  601. u32 zeta_width;
  602. u32 zeta_height;
  603. INSERT_PADDING_WORDS(0x27);
  604. u32 depth_test_enable;
  605. INSERT_PADDING_WORDS(0x5);
  606. u32 independent_blend_enable;
  607. u32 depth_write_enabled;
  608. u32 alpha_test_enabled;
  609. INSERT_PADDING_WORDS(0x6);
  610. u32 d3d_cull_mode;
  611. ComparisonOp depth_test_func;
  612. float alpha_test_ref;
  613. ComparisonOp alpha_test_func;
  614. u32 draw_tfb_stride;
  615. struct {
  616. float r;
  617. float g;
  618. float b;
  619. float a;
  620. } blend_color;
  621. INSERT_PADDING_WORDS(0x4);
  622. struct {
  623. u32 separate_alpha;
  624. Blend::Equation equation_rgb;
  625. Blend::Factor factor_source_rgb;
  626. Blend::Factor factor_dest_rgb;
  627. Blend::Equation equation_a;
  628. Blend::Factor factor_source_a;
  629. INSERT_PADDING_WORDS(1);
  630. Blend::Factor factor_dest_a;
  631. u32 enable_common;
  632. u32 enable[NumRenderTargets];
  633. } blend;
  634. u32 stencil_enable;
  635. StencilOp stencil_front_op_fail;
  636. StencilOp stencil_front_op_zfail;
  637. StencilOp stencil_front_op_zpass;
  638. ComparisonOp stencil_front_func_func;
  639. s32 stencil_front_func_ref;
  640. u32 stencil_front_func_mask;
  641. u32 stencil_front_mask;
  642. INSERT_PADDING_WORDS(0x2);
  643. u32 frag_color_clamp;
  644. union {
  645. BitField<0, 1, u32> y_negate;
  646. BitField<4, 1, u32> triangle_rast_flip;
  647. } screen_y_control;
  648. INSERT_PADDING_WORDS(0x21);
  649. u32 vb_element_base;
  650. INSERT_PADDING_WORDS(0x36);
  651. union {
  652. BitField<0, 1, u32> c0;
  653. BitField<1, 1, u32> c1;
  654. BitField<2, 1, u32> c2;
  655. BitField<3, 1, u32> c3;
  656. BitField<4, 1, u32> c4;
  657. BitField<5, 1, u32> c5;
  658. BitField<6, 1, u32> c6;
  659. BitField<7, 1, u32> c7;
  660. } clip_distance_enabled;
  661. INSERT_PADDING_WORDS(0x1);
  662. float point_size;
  663. INSERT_PADDING_WORDS(0x7);
  664. u32 zeta_enable;
  665. union {
  666. BitField<0, 1, u32> alpha_to_coverage;
  667. BitField<4, 1, u32> alpha_to_one;
  668. } multisample_control;
  669. INSERT_PADDING_WORDS(0x7);
  670. struct {
  671. u32 tsc_address_high;
  672. u32 tsc_address_low;
  673. u32 tsc_limit;
  674. GPUVAddr TSCAddress() const {
  675. return static_cast<GPUVAddr>(
  676. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  677. }
  678. } tsc;
  679. INSERT_PADDING_WORDS(0x1);
  680. float polygon_offset_factor;
  681. INSERT_PADDING_WORDS(0x1);
  682. struct {
  683. u32 tic_address_high;
  684. u32 tic_address_low;
  685. u32 tic_limit;
  686. GPUVAddr TICAddress() const {
  687. return static_cast<GPUVAddr>(
  688. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  689. }
  690. } tic;
  691. INSERT_PADDING_WORDS(0x5);
  692. u32 stencil_two_side_enable;
  693. StencilOp stencil_back_op_fail;
  694. StencilOp stencil_back_op_zfail;
  695. StencilOp stencil_back_op_zpass;
  696. ComparisonOp stencil_back_func_func;
  697. INSERT_PADDING_WORDS(0x4);
  698. u32 framebuffer_srgb;
  699. float polygon_offset_units;
  700. INSERT_PADDING_WORDS(0x11);
  701. union {
  702. BitField<2, 1, u32> coord_origin;
  703. BitField<3, 10, u32> enable;
  704. } point_coord_replace;
  705. struct {
  706. u32 code_address_high;
  707. u32 code_address_low;
  708. GPUVAddr CodeAddress() const {
  709. return static_cast<GPUVAddr>(
  710. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  711. }
  712. } code_address;
  713. INSERT_PADDING_WORDS(1);
  714. struct {
  715. u32 vertex_end_gl;
  716. union {
  717. u32 vertex_begin_gl;
  718. BitField<0, 16, PrimitiveTopology> topology;
  719. BitField<26, 1, u32> instance_next;
  720. BitField<27, 1, u32> instance_cont;
  721. };
  722. } draw;
  723. INSERT_PADDING_WORDS(0xA);
  724. struct {
  725. u32 enabled;
  726. u32 index;
  727. } primitive_restart;
  728. INSERT_PADDING_WORDS(0x5F);
  729. struct {
  730. u32 start_addr_high;
  731. u32 start_addr_low;
  732. u32 end_addr_high;
  733. u32 end_addr_low;
  734. IndexFormat format;
  735. u32 first;
  736. u32 count;
  737. unsigned FormatSizeInBytes() const {
  738. switch (format) {
  739. case IndexFormat::UnsignedByte:
  740. return 1;
  741. case IndexFormat::UnsignedShort:
  742. return 2;
  743. case IndexFormat::UnsignedInt:
  744. return 4;
  745. }
  746. UNREACHABLE();
  747. return 1;
  748. }
  749. GPUVAddr StartAddress() const {
  750. return static_cast<GPUVAddr>(
  751. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  752. }
  753. GPUVAddr EndAddress() const {
  754. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  755. end_addr_low);
  756. }
  757. /// Adjust the index buffer offset so it points to the first desired index.
  758. GPUVAddr IndexStart() const {
  759. return StartAddress() + static_cast<size_t>(first) *
  760. static_cast<size_t>(FormatSizeInBytes());
  761. }
  762. } index_array;
  763. INSERT_PADDING_WORDS(0x7);
  764. INSERT_PADDING_WORDS(0x1F);
  765. float polygon_offset_clamp;
  766. struct {
  767. u32 is_instanced[NumVertexArrays];
  768. /// Returns whether the vertex array specified by index is supposed to be
  769. /// accessed per instance or not.
  770. bool IsInstancingEnabled(u32 index) const {
  771. return is_instanced[index];
  772. }
  773. } instanced_arrays;
  774. INSERT_PADDING_WORDS(0x6);
  775. Cull cull;
  776. u32 pixel_center_integer;
  777. INSERT_PADDING_WORDS(0x1);
  778. u32 viewport_transform_enabled;
  779. INSERT_PADDING_WORDS(0x3);
  780. union {
  781. BitField<0, 1, u32> depth_range_0_1;
  782. BitField<3, 1, u32> depth_clamp_near;
  783. BitField<4, 1, u32> depth_clamp_far;
  784. } view_volume_clip_control;
  785. INSERT_PADDING_WORDS(0x21);
  786. struct {
  787. u32 enable;
  788. LogicOperation operation;
  789. } logic_op;
  790. INSERT_PADDING_WORDS(0x1);
  791. union {
  792. u32 raw;
  793. BitField<0, 1, u32> Z;
  794. BitField<1, 1, u32> S;
  795. BitField<2, 1, u32> R;
  796. BitField<3, 1, u32> G;
  797. BitField<4, 1, u32> B;
  798. BitField<5, 1, u32> A;
  799. BitField<6, 4, u32> RT;
  800. BitField<10, 11, u32> layer;
  801. } clear_buffers;
  802. INSERT_PADDING_WORDS(0xB);
  803. std::array<ColorMask, NumRenderTargets> color_mask;
  804. INSERT_PADDING_WORDS(0x38);
  805. struct {
  806. u32 query_address_high;
  807. u32 query_address_low;
  808. u32 query_sequence;
  809. union {
  810. u32 raw;
  811. BitField<0, 2, QueryMode> mode;
  812. BitField<4, 1, u32> fence;
  813. BitField<12, 4, QueryUnit> unit;
  814. BitField<16, 1, QuerySyncCondition> sync_cond;
  815. BitField<23, 5, QuerySelect> select;
  816. BitField<28, 1, u32> short_query;
  817. } query_get;
  818. GPUVAddr QueryAddress() const {
  819. return static_cast<GPUVAddr>(
  820. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  821. }
  822. } query;
  823. INSERT_PADDING_WORDS(0x3C);
  824. struct {
  825. union {
  826. BitField<0, 12, u32> stride;
  827. BitField<12, 1, u32> enable;
  828. };
  829. u32 start_high;
  830. u32 start_low;
  831. u32 divisor;
  832. GPUVAddr StartAddress() const {
  833. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  834. start_low);
  835. }
  836. bool IsEnabled() const {
  837. return enable != 0 && StartAddress() != 0;
  838. }
  839. } vertex_array[NumVertexArrays];
  840. Blend independent_blend[NumRenderTargets];
  841. struct {
  842. u32 limit_high;
  843. u32 limit_low;
  844. GPUVAddr LimitAddress() const {
  845. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  846. limit_low);
  847. }
  848. } vertex_array_limit[NumVertexArrays];
  849. struct {
  850. union {
  851. BitField<0, 1, u32> enable;
  852. BitField<4, 4, ShaderProgram> program;
  853. };
  854. u32 offset;
  855. INSERT_PADDING_WORDS(14);
  856. } shader_config[MaxShaderProgram];
  857. INSERT_PADDING_WORDS(0x80);
  858. struct {
  859. u32 cb_size;
  860. u32 cb_address_high;
  861. u32 cb_address_low;
  862. u32 cb_pos;
  863. u32 cb_data[NumCBData];
  864. GPUVAddr BufferAddress() const {
  865. return static_cast<GPUVAddr>(
  866. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  867. }
  868. } const_buffer;
  869. INSERT_PADDING_WORDS(0x10);
  870. struct {
  871. union {
  872. u32 raw_config;
  873. BitField<0, 1, u32> valid;
  874. BitField<4, 5, u32> index;
  875. };
  876. INSERT_PADDING_WORDS(7);
  877. } cb_bind[MaxShaderStage];
  878. INSERT_PADDING_WORDS(0x56);
  879. u32 tex_cb_index;
  880. INSERT_PADDING_WORDS(0x395);
  881. struct {
  882. /// Compressed address of a buffer that holds information about bound SSBOs.
  883. /// This address is usually bound to c0 in the shaders.
  884. u32 buffer_address;
  885. GPUVAddr BufferAddress() const {
  886. return static_cast<GPUVAddr>(buffer_address) << 8;
  887. }
  888. } ssbo_info;
  889. INSERT_PADDING_WORDS(0x11);
  890. struct {
  891. u32 address[MaxShaderStage];
  892. u32 size[MaxShaderStage];
  893. } tex_info_buffers;
  894. INSERT_PADDING_WORDS(0xCC);
  895. };
  896. std::array<u32, NUM_REGS> reg_array;
  897. };
  898. } regs{};
  899. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  900. struct State {
  901. struct ConstBufferInfo {
  902. GPUVAddr address;
  903. u32 index;
  904. u32 size;
  905. bool enabled;
  906. };
  907. struct ShaderStageInfo {
  908. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  909. };
  910. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  911. u32 current_instance = 0; ///< Current instance to be used to simulate instanced rendering.
  912. };
  913. State state{};
  914. struct DirtyFlags {
  915. std::bitset<8> color_buffer{0xFF};
  916. std::bitset<32> vertex_array{0xFFFFFFFF};
  917. bool vertex_attrib_format = true;
  918. bool zeta_buffer = true;
  919. bool shaders = true;
  920. void OnMemoryWrite() {
  921. zeta_buffer = true;
  922. shaders = true;
  923. color_buffer.set();
  924. vertex_array.set();
  925. }
  926. };
  927. DirtyFlags dirty_flags;
  928. /// Reads a register value located at the input method address
  929. u32 GetRegisterValue(u32 method) const;
  930. /// Write the value to the register identified by method.
  931. void CallMethod(const GPU::MethodCall& method_call);
  932. /// Returns a list of enabled textures for the specified shader stage.
  933. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  934. /// Returns the texture information for a specific texture in a specific shader stage.
  935. Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, std::size_t offset) const;
  936. /// Memory for macro code - it's undetermined how big this is, however 1MB is much larger than
  937. /// we've seen used.
  938. using MacroMemory = std::array<u32, 0x40000>;
  939. /// Gets a reference to macro memory.
  940. const MacroMemory& GetMacroMemory() const {
  941. return macro_memory;
  942. }
  943. private:
  944. void InitializeRegisterDefaults();
  945. Core::System& system;
  946. VideoCore::RasterizerInterface& rasterizer;
  947. MemoryManager& memory_manager;
  948. /// Start offsets of each macro in macro_memory
  949. std::unordered_map<u32, u32> macro_offsets;
  950. /// Memory for macro code
  951. MacroMemory macro_memory;
  952. /// Macro method that is currently being executed / being fed parameters.
  953. u32 executing_macro = 0;
  954. /// Parameters that have been submitted to the macro call so far.
  955. std::vector<u32> macro_params;
  956. /// Interpreter for the macro codes uploaded to the GPU.
  957. MacroInterpreter macro_interpreter;
  958. /// Retrieves information about a specific TIC entry from the TIC buffer.
  959. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  960. /// Retrieves information about a specific TSC entry from the TSC buffer.
  961. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  962. /**
  963. * Call a macro on this engine.
  964. * @param method Method to call
  965. * @param parameters Arguments to the method call
  966. */
  967. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  968. /// Handles writes to the macro uploading register.
  969. void ProcessMacroUpload(u32 data);
  970. /// Handles writes to the macro bind register.
  971. void ProcessMacroBind(u32 data);
  972. /// Handles a write to the CLEAR_BUFFERS register.
  973. void ProcessClearBuffers();
  974. /// Handles a write to the QUERY_GET register.
  975. void ProcessQueryGet();
  976. /// Handles a write to the CB_DATA[i] register.
  977. void ProcessCBData(u32 value);
  978. /// Handles a write to the CB_BIND register.
  979. void ProcessCBBind(Regs::ShaderStage stage);
  980. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  981. void DrawArrays();
  982. };
  983. #define ASSERT_REG_POSITION(field_name, position) \
  984. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  985. "Field " #field_name " has invalid position")
  986. ASSERT_REG_POSITION(macros, 0x45);
  987. ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
  988. ASSERT_REG_POSITION(rt, 0x200);
  989. ASSERT_REG_POSITION(viewport_transform, 0x280);
  990. ASSERT_REG_POSITION(viewports, 0x300);
  991. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  992. ASSERT_REG_POSITION(clear_color[0], 0x360);
  993. ASSERT_REG_POSITION(clear_depth, 0x364);
  994. ASSERT_REG_POSITION(clear_stencil, 0x368);
  995. ASSERT_REG_POSITION(polygon_offset_point_enable, 0x370);
  996. ASSERT_REG_POSITION(polygon_offset_line_enable, 0x371);
  997. ASSERT_REG_POSITION(polygon_offset_fill_enable, 0x372);
  998. ASSERT_REG_POSITION(scissor_test, 0x380);
  999. ASSERT_REG_POSITION(stencil_back_func_ref, 0x3D5);
  1000. ASSERT_REG_POSITION(stencil_back_mask, 0x3D6);
  1001. ASSERT_REG_POSITION(stencil_back_func_mask, 0x3D7);
  1002. ASSERT_REG_POSITION(color_mask_common, 0x3E4);
  1003. ASSERT_REG_POSITION(rt_separate_frag_data, 0x3EB);
  1004. ASSERT_REG_POSITION(zeta, 0x3F8);
  1005. ASSERT_REG_POSITION(clear_flags, 0x43E);
  1006. ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
  1007. ASSERT_REG_POSITION(rt_control, 0x487);
  1008. ASSERT_REG_POSITION(zeta_width, 0x48a);
  1009. ASSERT_REG_POSITION(zeta_height, 0x48b);
  1010. ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
  1011. ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
  1012. ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
  1013. ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
  1014. ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
  1015. ASSERT_REG_POSITION(depth_test_func, 0x4C3);
  1016. ASSERT_REG_POSITION(alpha_test_ref, 0x4C4);
  1017. ASSERT_REG_POSITION(alpha_test_func, 0x4C5);
  1018. ASSERT_REG_POSITION(draw_tfb_stride, 0x4C6);
  1019. ASSERT_REG_POSITION(blend_color, 0x4C7);
  1020. ASSERT_REG_POSITION(blend, 0x4CF);
  1021. ASSERT_REG_POSITION(stencil_enable, 0x4E0);
  1022. ASSERT_REG_POSITION(stencil_front_op_fail, 0x4E1);
  1023. ASSERT_REG_POSITION(stencil_front_op_zfail, 0x4E2);
  1024. ASSERT_REG_POSITION(stencil_front_op_zpass, 0x4E3);
  1025. ASSERT_REG_POSITION(stencil_front_func_func, 0x4E4);
  1026. ASSERT_REG_POSITION(stencil_front_func_ref, 0x4E5);
  1027. ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
  1028. ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
  1029. ASSERT_REG_POSITION(frag_color_clamp, 0x4EA);
  1030. ASSERT_REG_POSITION(screen_y_control, 0x4EB);
  1031. ASSERT_REG_POSITION(vb_element_base, 0x50D);
  1032. ASSERT_REG_POSITION(clip_distance_enabled, 0x544);
  1033. ASSERT_REG_POSITION(point_size, 0x546);
  1034. ASSERT_REG_POSITION(zeta_enable, 0x54E);
  1035. ASSERT_REG_POSITION(multisample_control, 0x54F);
  1036. ASSERT_REG_POSITION(tsc, 0x557);
  1037. ASSERT_REG_POSITION(polygon_offset_factor, 0x55b);
  1038. ASSERT_REG_POSITION(tic, 0x55D);
  1039. ASSERT_REG_POSITION(stencil_two_side_enable, 0x565);
  1040. ASSERT_REG_POSITION(stencil_back_op_fail, 0x566);
  1041. ASSERT_REG_POSITION(stencil_back_op_zfail, 0x567);
  1042. ASSERT_REG_POSITION(stencil_back_op_zpass, 0x568);
  1043. ASSERT_REG_POSITION(stencil_back_func_func, 0x569);
  1044. ASSERT_REG_POSITION(framebuffer_srgb, 0x56E);
  1045. ASSERT_REG_POSITION(polygon_offset_units, 0x56F);
  1046. ASSERT_REG_POSITION(point_coord_replace, 0x581);
  1047. ASSERT_REG_POSITION(code_address, 0x582);
  1048. ASSERT_REG_POSITION(draw, 0x585);
  1049. ASSERT_REG_POSITION(primitive_restart, 0x591);
  1050. ASSERT_REG_POSITION(index_array, 0x5F2);
  1051. ASSERT_REG_POSITION(polygon_offset_clamp, 0x61F);
  1052. ASSERT_REG_POSITION(instanced_arrays, 0x620);
  1053. ASSERT_REG_POSITION(cull, 0x646);
  1054. ASSERT_REG_POSITION(pixel_center_integer, 0x649);
  1055. ASSERT_REG_POSITION(viewport_transform_enabled, 0x64B);
  1056. ASSERT_REG_POSITION(view_volume_clip_control, 0x64F);
  1057. ASSERT_REG_POSITION(logic_op, 0x671);
  1058. ASSERT_REG_POSITION(clear_buffers, 0x674);
  1059. ASSERT_REG_POSITION(color_mask, 0x680);
  1060. ASSERT_REG_POSITION(query, 0x6C0);
  1061. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  1062. ASSERT_REG_POSITION(independent_blend, 0x780);
  1063. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  1064. ASSERT_REG_POSITION(shader_config[0], 0x800);
  1065. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  1066. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  1067. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  1068. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  1069. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  1070. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  1071. #undef ASSERT_REG_POSITION
  1072. } // namespace Tegra::Engines