maxwell_3d.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. };
  319. enum class MemoryLayout : u32 {
  320. Linear = 0,
  321. BlockLinear = 1,
  322. };
  323. enum class InvMemoryLayout : u32 {
  324. BlockLinear = 0,
  325. Linear = 1,
  326. };
  327. struct Cull {
  328. enum class FrontFace : u32 {
  329. ClockWise = 0x0900,
  330. CounterClockWise = 0x0901,
  331. };
  332. enum class CullFace : u32 {
  333. Front = 0x0404,
  334. Back = 0x0405,
  335. FrontAndBack = 0x0408,
  336. };
  337. u32 enabled;
  338. FrontFace front_face;
  339. CullFace cull_face;
  340. };
  341. struct Blend {
  342. enum class Equation : u32 {
  343. Add = 1,
  344. Subtract = 2,
  345. ReverseSubtract = 3,
  346. Min = 4,
  347. Max = 5,
  348. };
  349. enum class Factor : u32 {
  350. Zero = 0x1,
  351. One = 0x2,
  352. SourceColor = 0x3,
  353. OneMinusSourceColor = 0x4,
  354. SourceAlpha = 0x5,
  355. OneMinusSourceAlpha = 0x6,
  356. DestAlpha = 0x7,
  357. OneMinusDestAlpha = 0x8,
  358. DestColor = 0x9,
  359. OneMinusDestColor = 0xa,
  360. SourceAlphaSaturate = 0xb,
  361. Source1Color = 0x10,
  362. OneMinusSource1Color = 0x11,
  363. Source1Alpha = 0x12,
  364. OneMinusSource1Alpha = 0x13,
  365. ConstantColor = 0x61,
  366. OneMinusConstantColor = 0x62,
  367. ConstantAlpha = 0x63,
  368. OneMinusConstantAlpha = 0x64,
  369. // These values are used by Nouveau and some games.
  370. ZeroGL = 0x4000,
  371. OneGL = 0x4001,
  372. SourceColorGL = 0x4300,
  373. OneMinusSourceColorGL = 0x4301,
  374. SourceAlphaGL = 0x4302,
  375. OneMinusSourceAlphaGL = 0x4303,
  376. DestAlphaGL = 0x4304,
  377. OneMinusDestAlphaGL = 0x4305,
  378. DestColorGL = 0x4306,
  379. OneMinusDestColorGL = 0x4307,
  380. SourceAlphaSaturateGL = 0x4308,
  381. ConstantColorGL = 0xc001,
  382. OneMinusConstantColorGL = 0xc002,
  383. ConstantAlphaGL = 0xc003,
  384. OneMinusConstantAlphaGL = 0xc004,
  385. Source1ColorGL = 0xc900,
  386. OneMinusSource1ColorGL = 0xc901,
  387. Source1AlphaGL = 0xc902,
  388. OneMinusSource1AlphaGL = 0xc903,
  389. };
  390. u32 separate_alpha;
  391. Equation equation_rgb;
  392. Factor factor_source_rgb;
  393. Factor factor_dest_rgb;
  394. Equation equation_a;
  395. Factor factor_source_a;
  396. Factor factor_dest_a;
  397. INSERT_PADDING_WORDS(1);
  398. };
  399. struct RenderTargetConfig {
  400. u32 address_high;
  401. u32 address_low;
  402. u32 width;
  403. u32 height;
  404. Tegra::RenderTargetFormat format;
  405. union {
  406. BitField<0, 3, u32> block_width;
  407. BitField<4, 3, u32> block_height;
  408. BitField<8, 3, u32> block_depth;
  409. BitField<12, 1, InvMemoryLayout> type;
  410. } memory_layout;
  411. u32 array_mode;
  412. u32 layer_stride;
  413. u32 base_layer;
  414. INSERT_PADDING_WORDS(7);
  415. GPUVAddr Address() const {
  416. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  417. address_low);
  418. }
  419. };
  420. bool IsShaderConfigEnabled(std::size_t index) const {
  421. // The VertexB is always enabled.
  422. if (index == static_cast<std::size_t>(Regs::ShaderProgram::VertexB)) {
  423. return true;
  424. }
  425. return shader_config[index].enable != 0;
  426. }
  427. union {
  428. struct {
  429. INSERT_PADDING_WORDS(0x45);
  430. struct {
  431. INSERT_PADDING_WORDS(1);
  432. u32 data;
  433. u32 entry;
  434. } macros;
  435. INSERT_PADDING_WORDS(0x189);
  436. u32 tfb_enabled;
  437. INSERT_PADDING_WORDS(0x2E);
  438. RenderTargetConfig rt[NumRenderTargets];
  439. struct {
  440. f32 scale_x;
  441. f32 scale_y;
  442. f32 scale_z;
  443. f32 translate_x;
  444. f32 translate_y;
  445. f32 translate_z;
  446. INSERT_PADDING_WORDS(2);
  447. MathUtil::Rectangle<s32> GetRect() const {
  448. return {
  449. GetX(), // left
  450. GetY() + GetHeight(), // top
  451. GetX() + GetWidth(), // right
  452. GetY() // bottom
  453. };
  454. };
  455. s32 GetX() const {
  456. return static_cast<s32>(std::max(0.0f, translate_x - std::fabs(scale_x)));
  457. }
  458. s32 GetY() const {
  459. return static_cast<s32>(std::max(0.0f, translate_y - std::fabs(scale_y)));
  460. }
  461. s32 GetWidth() const {
  462. return static_cast<s32>(translate_x + std::fabs(scale_x)) - GetX();
  463. }
  464. s32 GetHeight() const {
  465. return static_cast<s32>(translate_y + std::fabs(scale_y)) - GetY();
  466. }
  467. } viewport_transform[NumViewports];
  468. struct {
  469. union {
  470. BitField<0, 16, u32> x;
  471. BitField<16, 16, u32> width;
  472. };
  473. union {
  474. BitField<0, 16, u32> y;
  475. BitField<16, 16, u32> height;
  476. };
  477. float depth_range_near;
  478. float depth_range_far;
  479. } viewport[NumViewports];
  480. INSERT_PADDING_WORDS(0x1D);
  481. struct {
  482. u32 first;
  483. u32 count;
  484. } vertex_buffer;
  485. INSERT_PADDING_WORDS(1);
  486. float clear_color[4];
  487. float clear_depth;
  488. INSERT_PADDING_WORDS(0x3);
  489. s32 clear_stencil;
  490. INSERT_PADDING_WORDS(0x17);
  491. struct {
  492. u32 enable;
  493. union {
  494. BitField<0, 16, u32> min_x;
  495. BitField<16, 16, u32> max_x;
  496. };
  497. union {
  498. BitField<0, 16, u32> min_y;
  499. BitField<16, 16, u32> max_y;
  500. };
  501. } scissor_test;
  502. INSERT_PADDING_WORDS(0x52);
  503. s32 stencil_back_func_ref;
  504. u32 stencil_back_mask;
  505. u32 stencil_back_func_mask;
  506. INSERT_PADDING_WORDS(0x13);
  507. u32 rt_separate_frag_data;
  508. INSERT_PADDING_WORDS(0xC);
  509. struct {
  510. u32 address_high;
  511. u32 address_low;
  512. Tegra::DepthFormat format;
  513. union {
  514. BitField<0, 4, u32> block_width;
  515. BitField<4, 4, u32> block_height;
  516. BitField<8, 4, u32> block_depth;
  517. BitField<20, 1, InvMemoryLayout> type;
  518. } memory_layout;
  519. u32 layer_stride;
  520. GPUVAddr Address() const {
  521. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  522. address_low);
  523. }
  524. } zeta;
  525. INSERT_PADDING_WORDS(0x5B);
  526. std::array<VertexAttribute, NumVertexAttributes> vertex_attrib_format;
  527. INSERT_PADDING_WORDS(0xF);
  528. struct {
  529. union {
  530. BitField<0, 4, u32> count;
  531. BitField<4, 3, u32> map_0;
  532. BitField<7, 3, u32> map_1;
  533. BitField<10, 3, u32> map_2;
  534. BitField<13, 3, u32> map_3;
  535. BitField<16, 3, u32> map_4;
  536. BitField<19, 3, u32> map_5;
  537. BitField<22, 3, u32> map_6;
  538. BitField<25, 3, u32> map_7;
  539. };
  540. u32 GetMap(std::size_t index) const {
  541. const std::array<u32, NumRenderTargets> maps{map_0, map_1, map_2, map_3,
  542. map_4, map_5, map_6, map_7};
  543. ASSERT(index < maps.size());
  544. return maps[index];
  545. }
  546. } rt_control;
  547. INSERT_PADDING_WORDS(0x2);
  548. u32 zeta_width;
  549. u32 zeta_height;
  550. INSERT_PADDING_WORDS(0x27);
  551. u32 depth_test_enable;
  552. INSERT_PADDING_WORDS(0x5);
  553. u32 independent_blend_enable;
  554. u32 depth_write_enabled;
  555. u32 alpha_test_enabled;
  556. INSERT_PADDING_WORDS(0x6);
  557. u32 d3d_cull_mode;
  558. ComparisonOp depth_test_func;
  559. INSERT_PADDING_WORDS(0xB);
  560. struct {
  561. u32 separate_alpha;
  562. Blend::Equation equation_rgb;
  563. Blend::Factor factor_source_rgb;
  564. Blend::Factor factor_dest_rgb;
  565. Blend::Equation equation_a;
  566. Blend::Factor factor_source_a;
  567. INSERT_PADDING_WORDS(1);
  568. Blend::Factor factor_dest_a;
  569. u32 enable_common;
  570. u32 enable[NumRenderTargets];
  571. } blend;
  572. u32 stencil_enable;
  573. StencilOp stencil_front_op_fail;
  574. StencilOp stencil_front_op_zfail;
  575. StencilOp stencil_front_op_zpass;
  576. ComparisonOp stencil_front_func_func;
  577. s32 stencil_front_func_ref;
  578. u32 stencil_front_func_mask;
  579. u32 stencil_front_mask;
  580. INSERT_PADDING_WORDS(0x3);
  581. union {
  582. BitField<4, 1, u32> triangle_rast_flip;
  583. } screen_y_control;
  584. INSERT_PADDING_WORDS(0x21);
  585. u32 vb_element_base;
  586. INSERT_PADDING_WORDS(0x38);
  587. float point_size;
  588. INSERT_PADDING_WORDS(0x7);
  589. u32 zeta_enable;
  590. INSERT_PADDING_WORDS(0x8);
  591. struct {
  592. u32 tsc_address_high;
  593. u32 tsc_address_low;
  594. u32 tsc_limit;
  595. GPUVAddr TSCAddress() const {
  596. return static_cast<GPUVAddr>(
  597. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  598. }
  599. } tsc;
  600. INSERT_PADDING_WORDS(0x3);
  601. struct {
  602. u32 tic_address_high;
  603. u32 tic_address_low;
  604. u32 tic_limit;
  605. GPUVAddr TICAddress() const {
  606. return static_cast<GPUVAddr>(
  607. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  608. }
  609. } tic;
  610. INSERT_PADDING_WORDS(0x5);
  611. u32 stencil_two_side_enable;
  612. StencilOp stencil_back_op_fail;
  613. StencilOp stencil_back_op_zfail;
  614. StencilOp stencil_back_op_zpass;
  615. ComparisonOp stencil_back_func_func;
  616. INSERT_PADDING_WORDS(0x17);
  617. union {
  618. BitField<2, 1, u32> coord_origin;
  619. BitField<3, 10, u32> enable;
  620. } point_coord_replace;
  621. struct {
  622. u32 code_address_high;
  623. u32 code_address_low;
  624. GPUVAddr CodeAddress() const {
  625. return static_cast<GPUVAddr>(
  626. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  627. }
  628. } code_address;
  629. INSERT_PADDING_WORDS(1);
  630. struct {
  631. u32 vertex_end_gl;
  632. union {
  633. u32 vertex_begin_gl;
  634. BitField<0, 16, PrimitiveTopology> topology;
  635. BitField<26, 1, u32> instance_next;
  636. BitField<27, 1, u32> instance_cont;
  637. };
  638. } draw;
  639. INSERT_PADDING_WORDS(0x6B);
  640. struct {
  641. u32 start_addr_high;
  642. u32 start_addr_low;
  643. u32 end_addr_high;
  644. u32 end_addr_low;
  645. IndexFormat format;
  646. u32 first;
  647. u32 count;
  648. unsigned FormatSizeInBytes() const {
  649. switch (format) {
  650. case IndexFormat::UnsignedByte:
  651. return 1;
  652. case IndexFormat::UnsignedShort:
  653. return 2;
  654. case IndexFormat::UnsignedInt:
  655. return 4;
  656. }
  657. UNREACHABLE();
  658. }
  659. GPUVAddr StartAddress() const {
  660. return static_cast<GPUVAddr>(
  661. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  662. }
  663. GPUVAddr EndAddress() const {
  664. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  665. end_addr_low);
  666. }
  667. /// Adjust the index buffer offset so it points to the first desired index.
  668. GPUVAddr IndexStart() const {
  669. return StartAddress() + static_cast<size_t>(first) *
  670. static_cast<size_t>(FormatSizeInBytes());
  671. }
  672. } index_array;
  673. INSERT_PADDING_WORDS(0x7);
  674. INSERT_PADDING_WORDS(0x20);
  675. struct {
  676. u32 is_instanced[NumVertexArrays];
  677. /// Returns whether the vertex array specified by index is supposed to be
  678. /// accessed per instance or not.
  679. bool IsInstancingEnabled(u32 index) const {
  680. return is_instanced[index];
  681. }
  682. } instanced_arrays;
  683. INSERT_PADDING_WORDS(0x6);
  684. Cull cull;
  685. INSERT_PADDING_WORDS(0x28);
  686. struct {
  687. u32 enable;
  688. LogicOperation operation;
  689. } logic_op;
  690. INSERT_PADDING_WORDS(0x1);
  691. union {
  692. u32 raw;
  693. BitField<0, 1, u32> Z;
  694. BitField<1, 1, u32> S;
  695. BitField<2, 1, u32> R;
  696. BitField<3, 1, u32> G;
  697. BitField<4, 1, u32> B;
  698. BitField<5, 1, u32> A;
  699. BitField<6, 4, u32> RT;
  700. BitField<10, 11, u32> layer;
  701. } clear_buffers;
  702. INSERT_PADDING_WORDS(0x4B);
  703. struct {
  704. u32 query_address_high;
  705. u32 query_address_low;
  706. u32 query_sequence;
  707. union {
  708. u32 raw;
  709. BitField<0, 2, QueryMode> mode;
  710. BitField<4, 1, u32> fence;
  711. BitField<12, 4, QueryUnit> unit;
  712. BitField<16, 1, QuerySyncCondition> sync_cond;
  713. BitField<23, 5, QuerySelect> select;
  714. BitField<28, 1, u32> short_query;
  715. } query_get;
  716. GPUVAddr QueryAddress() const {
  717. return static_cast<GPUVAddr>(
  718. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  719. }
  720. } query;
  721. INSERT_PADDING_WORDS(0x3C);
  722. struct {
  723. union {
  724. BitField<0, 12, u32> stride;
  725. BitField<12, 1, u32> enable;
  726. };
  727. u32 start_high;
  728. u32 start_low;
  729. u32 divisor;
  730. GPUVAddr StartAddress() const {
  731. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  732. start_low);
  733. }
  734. bool IsEnabled() const {
  735. return enable != 0 && StartAddress() != 0;
  736. }
  737. } vertex_array[NumVertexArrays];
  738. Blend independent_blend[NumRenderTargets];
  739. struct {
  740. u32 limit_high;
  741. u32 limit_low;
  742. GPUVAddr LimitAddress() const {
  743. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  744. limit_low);
  745. }
  746. } vertex_array_limit[NumVertexArrays];
  747. struct {
  748. union {
  749. BitField<0, 1, u32> enable;
  750. BitField<4, 4, ShaderProgram> program;
  751. };
  752. u32 offset;
  753. INSERT_PADDING_WORDS(14);
  754. } shader_config[MaxShaderProgram];
  755. INSERT_PADDING_WORDS(0x80);
  756. struct {
  757. u32 cb_size;
  758. u32 cb_address_high;
  759. u32 cb_address_low;
  760. u32 cb_pos;
  761. u32 cb_data[NumCBData];
  762. GPUVAddr BufferAddress() const {
  763. return static_cast<GPUVAddr>(
  764. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  765. }
  766. } const_buffer;
  767. INSERT_PADDING_WORDS(0x10);
  768. struct {
  769. union {
  770. u32 raw_config;
  771. BitField<0, 1, u32> valid;
  772. BitField<4, 5, u32> index;
  773. };
  774. INSERT_PADDING_WORDS(7);
  775. } cb_bind[MaxShaderStage];
  776. INSERT_PADDING_WORDS(0x56);
  777. u32 tex_cb_index;
  778. INSERT_PADDING_WORDS(0x395);
  779. struct {
  780. /// Compressed address of a buffer that holds information about bound SSBOs.
  781. /// This address is usually bound to c0 in the shaders.
  782. u32 buffer_address;
  783. GPUVAddr BufferAddress() const {
  784. return static_cast<GPUVAddr>(buffer_address) << 8;
  785. }
  786. } ssbo_info;
  787. INSERT_PADDING_WORDS(0x11);
  788. struct {
  789. u32 address[MaxShaderStage];
  790. u32 size[MaxShaderStage];
  791. } tex_info_buffers;
  792. INSERT_PADDING_WORDS(0xCC);
  793. };
  794. std::array<u32, NUM_REGS> reg_array;
  795. };
  796. } regs{};
  797. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  798. struct State {
  799. struct ConstBufferInfo {
  800. GPUVAddr address;
  801. u32 index;
  802. u32 size;
  803. bool enabled;
  804. };
  805. struct ShaderStageInfo {
  806. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  807. };
  808. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  809. u32 current_instance = 0; ///< Current instance to be used to simulate instanced rendering.
  810. };
  811. State state{};
  812. MemoryManager& memory_manager;
  813. /// Reads a register value located at the input method address
  814. u32 GetRegisterValue(u32 method) const;
  815. /// Write the value to the register identified by method.
  816. void WriteReg(u32 method, u32 value, u32 remaining_params);
  817. /// Returns a list of enabled textures for the specified shader stage.
  818. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  819. /// Returns the texture information for a specific texture in a specific shader stage.
  820. Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, std::size_t offset) const;
  821. private:
  822. VideoCore::RasterizerInterface& rasterizer;
  823. std::unordered_map<u32, std::vector<u32>> uploaded_macros;
  824. /// Macro method that is currently being executed / being fed parameters.
  825. u32 executing_macro = 0;
  826. /// Parameters that have been submitted to the macro call so far.
  827. std::vector<u32> macro_params;
  828. /// Interpreter for the macro codes uploaded to the GPU.
  829. MacroInterpreter macro_interpreter;
  830. /// Retrieves information about a specific TIC entry from the TIC buffer.
  831. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  832. /// Retrieves information about a specific TSC entry from the TSC buffer.
  833. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  834. /**
  835. * Call a macro on this engine.
  836. * @param method Method to call
  837. * @param parameters Arguments to the method call
  838. */
  839. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  840. /// Handles writes to the macro uploading registers.
  841. void ProcessMacroUpload(u32 data);
  842. /// Handles a write to the CLEAR_BUFFERS register.
  843. void ProcessClearBuffers();
  844. /// Handles a write to the QUERY_GET register.
  845. void ProcessQueryGet();
  846. /// Handles a write to the CB_DATA[i] register.
  847. void ProcessCBData(u32 value);
  848. /// Handles a write to the CB_BIND register.
  849. void ProcessCBBind(Regs::ShaderStage stage);
  850. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  851. void DrawArrays();
  852. };
  853. #define ASSERT_REG_POSITION(field_name, position) \
  854. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  855. "Field " #field_name " has invalid position")
  856. ASSERT_REG_POSITION(macros, 0x45);
  857. ASSERT_REG_POSITION(tfb_enabled, 0x1D1);
  858. ASSERT_REG_POSITION(rt, 0x200);
  859. ASSERT_REG_POSITION(viewport_transform[0], 0x280);
  860. ASSERT_REG_POSITION(viewport, 0x300);
  861. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  862. ASSERT_REG_POSITION(clear_color[0], 0x360);
  863. ASSERT_REG_POSITION(clear_depth, 0x364);
  864. ASSERT_REG_POSITION(clear_stencil, 0x368);
  865. ASSERT_REG_POSITION(scissor_test, 0x380);
  866. ASSERT_REG_POSITION(stencil_back_func_ref, 0x3D5);
  867. ASSERT_REG_POSITION(stencil_back_mask, 0x3D6);
  868. ASSERT_REG_POSITION(stencil_back_func_mask, 0x3D7);
  869. ASSERT_REG_POSITION(rt_separate_frag_data, 0x3EB);
  870. ASSERT_REG_POSITION(zeta, 0x3F8);
  871. ASSERT_REG_POSITION(vertex_attrib_format, 0x458);
  872. ASSERT_REG_POSITION(rt_control, 0x487);
  873. ASSERT_REG_POSITION(zeta_width, 0x48a);
  874. ASSERT_REG_POSITION(zeta_height, 0x48b);
  875. ASSERT_REG_POSITION(depth_test_enable, 0x4B3);
  876. ASSERT_REG_POSITION(independent_blend_enable, 0x4B9);
  877. ASSERT_REG_POSITION(depth_write_enabled, 0x4BA);
  878. ASSERT_REG_POSITION(alpha_test_enabled, 0x4BB);
  879. ASSERT_REG_POSITION(d3d_cull_mode, 0x4C2);
  880. ASSERT_REG_POSITION(depth_test_func, 0x4C3);
  881. ASSERT_REG_POSITION(blend, 0x4CF);
  882. ASSERT_REG_POSITION(stencil_enable, 0x4E0);
  883. ASSERT_REG_POSITION(stencil_front_op_fail, 0x4E1);
  884. ASSERT_REG_POSITION(stencil_front_op_zfail, 0x4E2);
  885. ASSERT_REG_POSITION(stencil_front_op_zpass, 0x4E3);
  886. ASSERT_REG_POSITION(stencil_front_func_func, 0x4E4);
  887. ASSERT_REG_POSITION(stencil_front_func_ref, 0x4E5);
  888. ASSERT_REG_POSITION(stencil_front_func_mask, 0x4E6);
  889. ASSERT_REG_POSITION(stencil_front_mask, 0x4E7);
  890. ASSERT_REG_POSITION(screen_y_control, 0x4EB);
  891. ASSERT_REG_POSITION(vb_element_base, 0x50D);
  892. ASSERT_REG_POSITION(point_size, 0x546);
  893. ASSERT_REG_POSITION(zeta_enable, 0x54E);
  894. ASSERT_REG_POSITION(tsc, 0x557);
  895. ASSERT_REG_POSITION(tic, 0x55D);
  896. ASSERT_REG_POSITION(stencil_two_side_enable, 0x565);
  897. ASSERT_REG_POSITION(stencil_back_op_fail, 0x566);
  898. ASSERT_REG_POSITION(stencil_back_op_zfail, 0x567);
  899. ASSERT_REG_POSITION(stencil_back_op_zpass, 0x568);
  900. ASSERT_REG_POSITION(stencil_back_func_func, 0x569);
  901. ASSERT_REG_POSITION(point_coord_replace, 0x581);
  902. ASSERT_REG_POSITION(code_address, 0x582);
  903. ASSERT_REG_POSITION(draw, 0x585);
  904. ASSERT_REG_POSITION(index_array, 0x5F2);
  905. ASSERT_REG_POSITION(instanced_arrays, 0x620);
  906. ASSERT_REG_POSITION(cull, 0x646);
  907. ASSERT_REG_POSITION(logic_op, 0x671);
  908. ASSERT_REG_POSITION(clear_buffers, 0x674);
  909. ASSERT_REG_POSITION(query, 0x6C0);
  910. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  911. ASSERT_REG_POSITION(independent_blend, 0x780);
  912. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  913. ASSERT_REG_POSITION(shader_config[0], 0x800);
  914. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  915. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  916. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  917. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  918. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  919. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  920. #undef ASSERT_REG_POSITION
  921. } // namespace Tegra::Engines