maxwell_3d.h 36 KB

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