maxwell_3d.h 30 KB

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