maxwell_3d.h 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. std::array<RenderTargetConfig, NumRenderTargets> rt;
  460. struct ViewportTransform {
  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. };
  489. std::array<ViewportTransform, NumViewports> viewport_transform;
  490. struct ViewPort {
  491. union {
  492. BitField<0, 16, u32> x;
  493. BitField<16, 16, u32> width;
  494. };
  495. union {
  496. BitField<0, 16, u32> y;
  497. BitField<16, 16, u32> height;
  498. };
  499. float depth_range_near;
  500. float depth_range_far;
  501. };
  502. std::array<ViewPort, NumViewports> viewports;
  503. INSERT_PADDING_WORDS(0x1D);
  504. struct {
  505. u32 first;
  506. u32 count;
  507. } vertex_buffer;
  508. INSERT_PADDING_WORDS(1);
  509. float clear_color[4];
  510. float clear_depth;
  511. INSERT_PADDING_WORDS(0x3);
  512. s32 clear_stencil;
  513. INSERT_PADDING_WORDS(0x17);
  514. struct ScissorTest {
  515. u32 enable;
  516. union {
  517. BitField<0, 16, u32> min_x;
  518. BitField<16, 16, u32> max_x;
  519. };
  520. union {
  521. BitField<0, 16, u32> min_y;
  522. BitField<16, 16, u32> max_y;
  523. };
  524. u32 fill;
  525. };
  526. std::array<ScissorTest, NumViewports> scissor_test;
  527. INSERT_PADDING_WORDS(0x15);
  528. s32 stencil_back_func_ref;
  529. u32 stencil_back_mask;
  530. u32 stencil_back_func_mask;
  531. INSERT_PADDING_WORDS(0xC);
  532. u32 color_mask_common;
  533. INSERT_PADDING_WORDS(0x6);
  534. u32 rt_separate_frag_data;
  535. INSERT_PADDING_WORDS(0xC);
  536. struct {
  537. u32 address_high;
  538. u32 address_low;
  539. Tegra::DepthFormat format;
  540. union {
  541. BitField<0, 4, u32> block_width;
  542. BitField<4, 4, u32> block_height;
  543. BitField<8, 4, u32> block_depth;
  544. BitField<20, 1, InvMemoryLayout> type;
  545. } memory_layout;
  546. u32 layer_stride;
  547. GPUVAddr Address() const {
  548. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  549. address_low);
  550. }
  551. } zeta;
  552. INSERT_PADDING_WORDS(0x5B);
  553. std::array<VertexAttribute, NumVertexAttributes> vertex_attrib_format;
  554. INSERT_PADDING_WORDS(0xF);
  555. struct {
  556. union {
  557. BitField<0, 4, u32> count;
  558. BitField<4, 3, u32> map_0;
  559. BitField<7, 3, u32> map_1;
  560. BitField<10, 3, u32> map_2;
  561. BitField<13, 3, u32> map_3;
  562. BitField<16, 3, u32> map_4;
  563. BitField<19, 3, u32> map_5;
  564. BitField<22, 3, u32> map_6;
  565. BitField<25, 3, u32> map_7;
  566. };
  567. u32 GetMap(std::size_t index) const {
  568. const std::array<u32, NumRenderTargets> maps{map_0, map_1, map_2, map_3,
  569. map_4, map_5, map_6, map_7};
  570. ASSERT(index < maps.size());
  571. return maps[index];
  572. }
  573. } rt_control;
  574. INSERT_PADDING_WORDS(0x2);
  575. u32 zeta_width;
  576. u32 zeta_height;
  577. INSERT_PADDING_WORDS(0x27);
  578. u32 depth_test_enable;
  579. INSERT_PADDING_WORDS(0x5);
  580. u32 independent_blend_enable;
  581. u32 depth_write_enabled;
  582. u32 alpha_test_enabled;
  583. INSERT_PADDING_WORDS(0x6);
  584. u32 d3d_cull_mode;
  585. ComparisonOp depth_test_func;
  586. float alpha_test_ref;
  587. ComparisonOp alpha_test_func;
  588. u32 draw_tfb_stride;
  589. struct {
  590. float r;
  591. float g;
  592. float b;
  593. float a;
  594. } blend_color;
  595. INSERT_PADDING_WORDS(0x4);
  596. struct {
  597. u32 separate_alpha;
  598. Blend::Equation equation_rgb;
  599. Blend::Factor factor_source_rgb;
  600. Blend::Factor factor_dest_rgb;
  601. Blend::Equation equation_a;
  602. Blend::Factor factor_source_a;
  603. INSERT_PADDING_WORDS(1);
  604. Blend::Factor factor_dest_a;
  605. u32 enable_common;
  606. u32 enable[NumRenderTargets];
  607. } blend;
  608. u32 stencil_enable;
  609. StencilOp stencil_front_op_fail;
  610. StencilOp stencil_front_op_zfail;
  611. StencilOp stencil_front_op_zpass;
  612. ComparisonOp stencil_front_func_func;
  613. s32 stencil_front_func_ref;
  614. u32 stencil_front_func_mask;
  615. u32 stencil_front_mask;
  616. INSERT_PADDING_WORDS(0x2);
  617. u32 frag_color_clamp;
  618. union {
  619. BitField<4, 1, u32> triangle_rast_flip;
  620. } screen_y_control;
  621. INSERT_PADDING_WORDS(0x21);
  622. u32 vb_element_base;
  623. INSERT_PADDING_WORDS(0x38);
  624. float point_size;
  625. INSERT_PADDING_WORDS(0x7);
  626. u32 zeta_enable;
  627. INSERT_PADDING_WORDS(0x8);
  628. struct {
  629. u32 tsc_address_high;
  630. u32 tsc_address_low;
  631. u32 tsc_limit;
  632. GPUVAddr TSCAddress() const {
  633. return static_cast<GPUVAddr>(
  634. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  635. }
  636. } tsc;
  637. INSERT_PADDING_WORDS(0x3);
  638. struct {
  639. u32 tic_address_high;
  640. u32 tic_address_low;
  641. u32 tic_limit;
  642. GPUVAddr TICAddress() const {
  643. return static_cast<GPUVAddr>(
  644. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  645. }
  646. } tic;
  647. INSERT_PADDING_WORDS(0x5);
  648. u32 stencil_two_side_enable;
  649. StencilOp stencil_back_op_fail;
  650. StencilOp stencil_back_op_zfail;
  651. StencilOp stencil_back_op_zpass;
  652. ComparisonOp stencil_back_func_func;
  653. INSERT_PADDING_WORDS(0x4);
  654. u32 framebuffer_srgb;
  655. INSERT_PADDING_WORDS(0x12);
  656. union {
  657. BitField<2, 1, u32> coord_origin;
  658. BitField<3, 10, u32> enable;
  659. } point_coord_replace;
  660. struct {
  661. u32 code_address_high;
  662. u32 code_address_low;
  663. GPUVAddr CodeAddress() const {
  664. return static_cast<GPUVAddr>(
  665. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  666. }
  667. } code_address;
  668. INSERT_PADDING_WORDS(1);
  669. struct {
  670. u32 vertex_end_gl;
  671. union {
  672. u32 vertex_begin_gl;
  673. BitField<0, 16, PrimitiveTopology> topology;
  674. BitField<26, 1, u32> instance_next;
  675. BitField<27, 1, u32> instance_cont;
  676. };
  677. } draw;
  678. INSERT_PADDING_WORDS(0xA);
  679. struct {
  680. u32 enabled;
  681. u32 index;
  682. } primitive_restart;
  683. INSERT_PADDING_WORDS(0x5F);
  684. struct {
  685. u32 start_addr_high;
  686. u32 start_addr_low;
  687. u32 end_addr_high;
  688. u32 end_addr_low;
  689. IndexFormat format;
  690. u32 first;
  691. u32 count;
  692. unsigned FormatSizeInBytes() const {
  693. switch (format) {
  694. case IndexFormat::UnsignedByte:
  695. return 1;
  696. case IndexFormat::UnsignedShort:
  697. return 2;
  698. case IndexFormat::UnsignedInt:
  699. return 4;
  700. }
  701. UNREACHABLE();
  702. }
  703. GPUVAddr StartAddress() const {
  704. return static_cast<GPUVAddr>(
  705. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  706. }
  707. GPUVAddr EndAddress() const {
  708. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  709. end_addr_low);
  710. }
  711. /// Adjust the index buffer offset so it points to the first desired index.
  712. GPUVAddr IndexStart() const {
  713. return StartAddress() + static_cast<size_t>(first) *
  714. static_cast<size_t>(FormatSizeInBytes());
  715. }
  716. } index_array;
  717. INSERT_PADDING_WORDS(0x7);
  718. INSERT_PADDING_WORDS(0x20);
  719. struct {
  720. u32 is_instanced[NumVertexArrays];
  721. /// Returns whether the vertex array specified by index is supposed to be
  722. /// accessed per instance or not.
  723. bool IsInstancingEnabled(u32 index) const {
  724. return is_instanced[index];
  725. }
  726. } instanced_arrays;
  727. INSERT_PADDING_WORDS(0x6);
  728. Cull cull;
  729. INSERT_PADDING_WORDS(0x28);
  730. struct {
  731. u32 enable;
  732. LogicOperation operation;
  733. } logic_op;
  734. INSERT_PADDING_WORDS(0x1);
  735. union {
  736. u32 raw;
  737. BitField<0, 1, u32> Z;
  738. BitField<1, 1, u32> S;
  739. BitField<2, 1, u32> R;
  740. BitField<3, 1, u32> G;
  741. BitField<4, 1, u32> B;
  742. BitField<5, 1, u32> A;
  743. BitField<6, 4, u32> RT;
  744. BitField<10, 11, u32> layer;
  745. } clear_buffers;
  746. INSERT_PADDING_WORDS(0xB);
  747. std::array<ColorMask, NumRenderTargets> color_mask;
  748. INSERT_PADDING_WORDS(0x38);
  749. struct {
  750. u32 query_address_high;
  751. u32 query_address_low;
  752. u32 query_sequence;
  753. union {
  754. u32 raw;
  755. BitField<0, 2, QueryMode> mode;
  756. BitField<4, 1, u32> fence;
  757. BitField<12, 4, QueryUnit> unit;
  758. BitField<16, 1, QuerySyncCondition> sync_cond;
  759. BitField<23, 5, QuerySelect> select;
  760. BitField<28, 1, u32> short_query;
  761. } query_get;
  762. GPUVAddr QueryAddress() const {
  763. return static_cast<GPUVAddr>(
  764. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  765. }
  766. } query;
  767. INSERT_PADDING_WORDS(0x3C);
  768. struct {
  769. union {
  770. BitField<0, 12, u32> stride;
  771. BitField<12, 1, u32> enable;
  772. };
  773. u32 start_high;
  774. u32 start_low;
  775. u32 divisor;
  776. GPUVAddr StartAddress() const {
  777. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  778. start_low);
  779. }
  780. bool IsEnabled() const {
  781. return enable != 0 && StartAddress() != 0;
  782. }
  783. } vertex_array[NumVertexArrays];
  784. Blend independent_blend[NumRenderTargets];
  785. struct {
  786. u32 limit_high;
  787. u32 limit_low;
  788. GPUVAddr LimitAddress() const {
  789. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  790. limit_low);
  791. }
  792. } vertex_array_limit[NumVertexArrays];
  793. struct {
  794. union {
  795. BitField<0, 1, u32> enable;
  796. BitField<4, 4, ShaderProgram> program;
  797. };
  798. u32 offset;
  799. INSERT_PADDING_WORDS(14);
  800. } shader_config[MaxShaderProgram];
  801. INSERT_PADDING_WORDS(0x80);
  802. struct {
  803. u32 cb_size;
  804. u32 cb_address_high;
  805. u32 cb_address_low;
  806. u32 cb_pos;
  807. u32 cb_data[NumCBData];
  808. GPUVAddr BufferAddress() const {
  809. return static_cast<GPUVAddr>(
  810. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  811. }
  812. } const_buffer;
  813. INSERT_PADDING_WORDS(0x10);
  814. struct {
  815. union {
  816. u32 raw_config;
  817. BitField<0, 1, u32> valid;
  818. BitField<4, 5, u32> index;
  819. };
  820. INSERT_PADDING_WORDS(7);
  821. } cb_bind[MaxShaderStage];
  822. INSERT_PADDING_WORDS(0x56);
  823. u32 tex_cb_index;
  824. INSERT_PADDING_WORDS(0x395);
  825. struct {
  826. /// Compressed address of a buffer that holds information about bound SSBOs.
  827. /// This address is usually bound to c0 in the shaders.
  828. u32 buffer_address;
  829. GPUVAddr BufferAddress() const {
  830. return static_cast<GPUVAddr>(buffer_address) << 8;
  831. }
  832. } ssbo_info;
  833. INSERT_PADDING_WORDS(0x11);
  834. struct {
  835. u32 address[MaxShaderStage];
  836. u32 size[MaxShaderStage];
  837. } tex_info_buffers;
  838. INSERT_PADDING_WORDS(0xCC);
  839. };
  840. std::array<u32, NUM_REGS> reg_array;
  841. };
  842. } regs{};
  843. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  844. struct State {
  845. struct ConstBufferInfo {
  846. GPUVAddr address;
  847. u32 index;
  848. u32 size;
  849. bool enabled;
  850. };
  851. struct ShaderStageInfo {
  852. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  853. };
  854. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  855. u32 current_instance = 0; ///< Current instance to be used to simulate instanced rendering.
  856. };
  857. State state{};
  858. MemoryManager& memory_manager;
  859. struct DirtyFlags {
  860. bool vertex_attrib_format = true;
  861. };
  862. DirtyFlags dirty_flags;
  863. /// Reads a register value located at the input method address
  864. u32 GetRegisterValue(u32 method) const;
  865. /// Write the value to the register identified by method.
  866. void WriteReg(u32 method, u32 value, u32 remaining_params);
  867. /// Returns a list of enabled textures for the specified shader stage.
  868. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  869. /// Returns the texture information for a specific texture in a specific shader stage.
  870. Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, std::size_t offset) const;
  871. /// Memory for macro code - it's undetermined how big this is, however 1MB is much larger than
  872. /// we've seen used.
  873. using MacroMemory = std::array<u32, 0x40000>;
  874. /// Gets a reference to macro memory.
  875. const MacroMemory& GetMacroMemory() const {
  876. return macro_memory;
  877. }
  878. private:
  879. void InitializeRegisterDefaults();
  880. VideoCore::RasterizerInterface& rasterizer;
  881. /// Start offsets of each macro in macro_memory
  882. std::unordered_map<u32, u32> macro_offsets;
  883. /// Memory for macro code
  884. MacroMemory macro_memory;
  885. /// Macro method that is currently being executed / being fed parameters.
  886. u32 executing_macro = 0;
  887. /// Parameters that have been submitted to the macro call so far.
  888. std::vector<u32> macro_params;
  889. /// Interpreter for the macro codes uploaded to the GPU.
  890. MacroInterpreter macro_interpreter;
  891. /// Retrieves information about a specific TIC entry from the TIC buffer.
  892. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  893. /// Retrieves information about a specific TSC entry from the TSC buffer.
  894. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  895. /**
  896. * Call a macro on this engine.
  897. * @param method Method to call
  898. * @param parameters Arguments to the method call
  899. */
  900. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  901. /// Handles writes to the macro uploading register.
  902. void ProcessMacroUpload(u32 data);
  903. /// Handles writes to the macro bind register.
  904. void ProcessMacroBind(u32 data);
  905. /// Handles a write to the CLEAR_BUFFERS register.
  906. void ProcessClearBuffers();
  907. /// Handles a write to the QUERY_GET register.
  908. void ProcessQueryGet();
  909. /// Handles a write to the CB_DATA[i] register.
  910. void ProcessCBData(u32 value);
  911. /// Handles a write to the CB_BIND register.
  912. void ProcessCBBind(Regs::ShaderStage stage);
  913. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  914. void DrawArrays();
  915. };
  916. #define ASSERT_REG_POSITION(field_name, position) \
  917. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  918. "Field " #field_name " has invalid position")
  919. ASSERT_REG_POSITION(macros, 0x45);
  920. ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
  921. ASSERT_REG_POSITION(rt, 0x200);
  922. ASSERT_REG_POSITION(viewport_transform, 0x280);
  923. ASSERT_REG_POSITION(viewports, 0x300);
  924. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  925. ASSERT_REG_POSITION(clear_color[0], 0x360);
  926. ASSERT_REG_POSITION(clear_depth, 0x364);
  927. ASSERT_REG_POSITION(clear_stencil, 0x368);
  928. ASSERT_REG_POSITION(scissor_test, 0x380);
  929. ASSERT_REG_POSITION(stencil_back_func_ref, 0x3D5);
  930. ASSERT_REG_POSITION(stencil_back_mask, 0x3D6);
  931. ASSERT_REG_POSITION(stencil_back_func_mask, 0x3D7);
  932. ASSERT_REG_POSITION(color_mask_common, 0x3E4);
  933. ASSERT_REG_POSITION(rt_separate_frag_data, 0x3EB);
  934. ASSERT_REG_POSITION(zeta, 0x3F8);
  935. ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
  936. ASSERT_REG_POSITION(rt_control, 0x487);
  937. ASSERT_REG_POSITION(zeta_width, 0x48a);
  938. ASSERT_REG_POSITION(zeta_height, 0x48b);
  939. ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
  940. ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
  941. ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
  942. ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
  943. ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
  944. ASSERT_REG_POSITION(depth_test_func, 0x4C3);
  945. ASSERT_REG_POSITION(alpha_test_ref, 0x4C4);
  946. ASSERT_REG_POSITION(alpha_test_func, 0x4C5);
  947. ASSERT_REG_POSITION(draw_tfb_stride, 0x4C6);
  948. ASSERT_REG_POSITION(blend_color, 0x4C7);
  949. ASSERT_REG_POSITION(blend, 0x4CF);
  950. ASSERT_REG_POSITION(stencil_enable, 0x4E0);
  951. ASSERT_REG_POSITION(stencil_front_op_fail, 0x4E1);
  952. ASSERT_REG_POSITION(stencil_front_op_zfail, 0x4E2);
  953. ASSERT_REG_POSITION(stencil_front_op_zpass, 0x4E3);
  954. ASSERT_REG_POSITION(stencil_front_func_func, 0x4E4);
  955. ASSERT_REG_POSITION(stencil_front_func_ref, 0x4E5);
  956. ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
  957. ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
  958. ASSERT_REG_POSITION(frag_color_clamp, 0x4EA);
  959. ASSERT_REG_POSITION(screen_y_control, 0x4EB);
  960. ASSERT_REG_POSITION(vb_element_base, 0x50D);
  961. ASSERT_REG_POSITION(point_size, 0x546);
  962. ASSERT_REG_POSITION(zeta_enable, 0x54E);
  963. ASSERT_REG_POSITION(tsc, 0x557);
  964. ASSERT_REG_POSITION(tic, 0x55D);
  965. ASSERT_REG_POSITION(stencil_two_side_enable, 0x565);
  966. ASSERT_REG_POSITION(stencil_back_op_fail, 0x566);
  967. ASSERT_REG_POSITION(stencil_back_op_zfail, 0x567);
  968. ASSERT_REG_POSITION(stencil_back_op_zpass, 0x568);
  969. ASSERT_REG_POSITION(stencil_back_func_func, 0x569);
  970. ASSERT_REG_POSITION(framebuffer_srgb, 0x56E);
  971. ASSERT_REG_POSITION(point_coord_replace, 0x581);
  972. ASSERT_REG_POSITION(code_address, 0x582);
  973. ASSERT_REG_POSITION(draw, 0x585);
  974. ASSERT_REG_POSITION(primitive_restart, 0x591);
  975. ASSERT_REG_POSITION(index_array, 0x5F2);
  976. ASSERT_REG_POSITION(instanced_arrays, 0x620);
  977. ASSERT_REG_POSITION(cull, 0x646);
  978. ASSERT_REG_POSITION(logic_op, 0x671);
  979. ASSERT_REG_POSITION(clear_buffers, 0x674);
  980. ASSERT_REG_POSITION(color_mask, 0x680);
  981. ASSERT_REG_POSITION(query, 0x6C0);
  982. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  983. ASSERT_REG_POSITION(independent_blend, 0x780);
  984. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  985. ASSERT_REG_POSITION(shader_config[0], 0x800);
  986. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  987. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  988. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  989. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  990. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  991. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  992. #undef ASSERT_REG_POSITION
  993. } // namespace Tegra::Engines