maxwell_3d.h 34 KB

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