maxwell_3d.h 29 KB

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