maxwell_3d.h 38 KB

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