maxwell_3d.h 34 KB

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