maxwell_3d.h 35 KB

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