maxwell_3d.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 Tegra {
  18. namespace Engines {
  19. class Maxwell3D final {
  20. public:
  21. explicit Maxwell3D(MemoryManager& memory_manager);
  22. ~Maxwell3D() = default;
  23. /// Register structure of the Maxwell3D engine.
  24. /// TODO(Subv): This structure will need to be made bigger as more registers are discovered.
  25. struct Regs {
  26. static constexpr size_t NUM_REGS = 0xE36;
  27. static constexpr size_t NumRenderTargets = 8;
  28. static constexpr size_t NumViewports = 16;
  29. static constexpr size_t NumCBData = 16;
  30. static constexpr size_t NumVertexArrays = 32;
  31. static constexpr size_t NumVertexAttributes = 32;
  32. static constexpr size_t MaxShaderProgram = 6;
  33. static constexpr size_t MaxShaderStage = 5;
  34. // Maximum number of const buffers per shader stage.
  35. static constexpr size_t MaxConstBuffers = 16;
  36. enum class QueryMode : u32 {
  37. Write = 0,
  38. Sync = 1,
  39. };
  40. enum class ShaderProgram : u32 {
  41. VertexA = 0,
  42. VertexB = 1,
  43. TesselationControl = 2,
  44. TesselationEval = 3,
  45. Geometry = 4,
  46. Fragment = 5,
  47. };
  48. enum class ShaderStage : u32 {
  49. Vertex = 0,
  50. TesselationControl = 1,
  51. TesselationEval = 2,
  52. Geometry = 3,
  53. Fragment = 4,
  54. };
  55. struct VertexAttribute {
  56. enum class Size : u32 {
  57. Size_32_32_32_32 = 0x01,
  58. Size_32_32_32 = 0x02,
  59. Size_16_16_16_16 = 0x03,
  60. Size_32_32 = 0x04,
  61. Size_16_16_16 = 0x05,
  62. Size_8_8_8_8 = 0x0a,
  63. Size_16_16 = 0x0f,
  64. Size_32 = 0x12,
  65. Size_8_8_8 = 0x13,
  66. Size_8_8 = 0x18,
  67. Size_16 = 0x1b,
  68. Size_8 = 0x1d,
  69. Size_10_10_10_2 = 0x30,
  70. Size_11_11_10 = 0x31,
  71. };
  72. enum class Type : u32 {
  73. SignedNorm = 1,
  74. UnsignedNorm = 2,
  75. SignedInt = 3,
  76. UnsignedInt = 4,
  77. UnsignedScaled = 5,
  78. SignedScaled = 6,
  79. Float = 7,
  80. };
  81. union {
  82. BitField<0, 5, u32> buffer;
  83. BitField<6, 1, u32> constant;
  84. BitField<7, 14, u32> offset;
  85. BitField<21, 6, Size> size;
  86. BitField<27, 3, Type> type;
  87. BitField<31, 1, u32> bgra;
  88. };
  89. u32 ComponentCount() const {
  90. switch (size) {
  91. case Size::Size_32_32_32_32:
  92. return 4;
  93. case Size::Size_32_32_32:
  94. return 3;
  95. case Size::Size_16_16_16_16:
  96. return 4;
  97. case Size::Size_32_32:
  98. return 2;
  99. case Size::Size_16_16_16:
  100. return 3;
  101. case Size::Size_8_8_8_8:
  102. return 4;
  103. case Size::Size_16_16:
  104. return 2;
  105. case Size::Size_32:
  106. return 1;
  107. case Size::Size_8_8_8:
  108. return 3;
  109. case Size::Size_8_8:
  110. return 2;
  111. case Size::Size_16:
  112. return 1;
  113. case Size::Size_8:
  114. return 1;
  115. case Size::Size_10_10_10_2:
  116. return 4;
  117. case Size::Size_11_11_10:
  118. return 3;
  119. default:
  120. UNREACHABLE();
  121. }
  122. }
  123. u32 SizeInBytes() const {
  124. switch (size) {
  125. case Size::Size_32_32_32_32:
  126. return 16;
  127. case Size::Size_32_32_32:
  128. return 12;
  129. case Size::Size_16_16_16_16:
  130. return 8;
  131. case Size::Size_32_32:
  132. return 8;
  133. case Size::Size_16_16_16:
  134. return 6;
  135. case Size::Size_8_8_8_8:
  136. return 4;
  137. case Size::Size_16_16:
  138. return 4;
  139. case Size::Size_32:
  140. return 4;
  141. case Size::Size_8_8_8:
  142. return 3;
  143. case Size::Size_8_8:
  144. return 2;
  145. case Size::Size_16:
  146. return 2;
  147. case Size::Size_8:
  148. return 1;
  149. case Size::Size_10_10_10_2:
  150. return 4;
  151. case Size::Size_11_11_10:
  152. return 4;
  153. default:
  154. UNREACHABLE();
  155. }
  156. }
  157. std::string SizeString() const {
  158. switch (size) {
  159. case Size::Size_32_32_32_32:
  160. return "32_32_32_32";
  161. case Size::Size_32_32_32:
  162. return "32_32_32";
  163. case Size::Size_16_16_16_16:
  164. return "16_16_16_16";
  165. case Size::Size_32_32:
  166. return "32_32";
  167. case Size::Size_16_16_16:
  168. return "16_16_16";
  169. case Size::Size_8_8_8_8:
  170. return "8_8_8_8";
  171. case Size::Size_16_16:
  172. return "16_16";
  173. case Size::Size_32:
  174. return "32";
  175. case Size::Size_8_8_8:
  176. return "8_8_8";
  177. case Size::Size_8_8:
  178. return "8_8";
  179. case Size::Size_16:
  180. return "16";
  181. case Size::Size_8:
  182. return "8";
  183. case Size::Size_10_10_10_2:
  184. return "10_10_10_2";
  185. case Size::Size_11_11_10:
  186. return "11_11_10";
  187. }
  188. UNREACHABLE();
  189. return {};
  190. }
  191. std::string TypeString() const {
  192. switch (type) {
  193. case Type::SignedNorm:
  194. return "SNORM";
  195. case Type::UnsignedNorm:
  196. return "UNORM";
  197. case Type::SignedInt:
  198. return "SINT";
  199. case Type::UnsignedInt:
  200. return "UINT";
  201. case Type::UnsignedScaled:
  202. return "USCALED";
  203. case Type::SignedScaled:
  204. return "SSCALED";
  205. case Type::Float:
  206. return "FLOAT";
  207. }
  208. UNREACHABLE();
  209. return {};
  210. }
  211. bool IsNormalized() const {
  212. return (type == Type::SignedNorm) || (type == Type::UnsignedNorm);
  213. }
  214. };
  215. enum class PrimitiveTopology : u32 {
  216. Points = 0x0,
  217. Lines = 0x1,
  218. LineLoop = 0x2,
  219. LineStrip = 0x3,
  220. Triangles = 0x4,
  221. TriangleStrip = 0x5,
  222. TriangleFan = 0x6,
  223. Quads = 0x7,
  224. QuadStrip = 0x8,
  225. Polygon = 0x9,
  226. LinesAdjacency = 0xa,
  227. LineStripAdjacency = 0xb,
  228. TrianglesAdjacency = 0xc,
  229. TriangleStripAdjacency = 0xd,
  230. Patches = 0xe,
  231. };
  232. enum class IndexFormat : u32 {
  233. UnsignedByte = 0x0,
  234. UnsignedShort = 0x1,
  235. UnsignedInt = 0x2,
  236. };
  237. union {
  238. struct {
  239. INSERT_PADDING_WORDS(0x200);
  240. struct {
  241. u32 address_high;
  242. u32 address_low;
  243. u32 width;
  244. u32 height;
  245. Tegra::RenderTargetFormat format;
  246. u32 block_dimensions;
  247. u32 array_mode;
  248. u32 layer_stride;
  249. u32 base_layer;
  250. INSERT_PADDING_WORDS(7);
  251. GPUVAddr Address() const {
  252. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  253. address_low);
  254. }
  255. } rt[NumRenderTargets];
  256. INSERT_PADDING_WORDS(0x80);
  257. struct {
  258. union {
  259. BitField<0, 16, u32> x;
  260. BitField<16, 16, u32> width;
  261. };
  262. union {
  263. BitField<0, 16, u32> y;
  264. BitField<16, 16, u32> height;
  265. };
  266. float depth_range_near;
  267. float depth_range_far;
  268. MathUtil::Rectangle<s32> GetRect() const {
  269. return {
  270. static_cast<s32>(x), // left
  271. static_cast<s32>(y + height), // top
  272. static_cast<s32>(x + width), // right
  273. static_cast<s32>(y) // bottom
  274. };
  275. };
  276. } viewport[NumViewports];
  277. INSERT_PADDING_WORDS(0x1D);
  278. struct {
  279. u32 first;
  280. u32 count;
  281. } vertex_buffer;
  282. INSERT_PADDING_WORDS(0x99);
  283. struct {
  284. u32 address_high;
  285. u32 address_low;
  286. u32 format;
  287. u32 block_dimensions;
  288. u32 layer_stride;
  289. GPUVAddr Address() const {
  290. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  291. address_low);
  292. }
  293. } zeta;
  294. INSERT_PADDING_WORDS(0x5B);
  295. VertexAttribute vertex_attrib_format[NumVertexAttributes];
  296. INSERT_PADDING_WORDS(0xF);
  297. struct {
  298. union {
  299. BitField<0, 4, u32> count;
  300. };
  301. } rt_control;
  302. INSERT_PADDING_WORDS(0xCF);
  303. struct {
  304. u32 tsc_address_high;
  305. u32 tsc_address_low;
  306. u32 tsc_limit;
  307. GPUVAddr TSCAddress() const {
  308. return static_cast<GPUVAddr>(
  309. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  310. }
  311. } tsc;
  312. INSERT_PADDING_WORDS(0x3);
  313. struct {
  314. u32 tic_address_high;
  315. u32 tic_address_low;
  316. u32 tic_limit;
  317. GPUVAddr TICAddress() const {
  318. return static_cast<GPUVAddr>(
  319. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  320. }
  321. } tic;
  322. INSERT_PADDING_WORDS(0x22);
  323. struct {
  324. u32 code_address_high;
  325. u32 code_address_low;
  326. GPUVAddr CodeAddress() const {
  327. return static_cast<GPUVAddr>(
  328. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  329. }
  330. } code_address;
  331. INSERT_PADDING_WORDS(1);
  332. struct {
  333. u32 vertex_end_gl;
  334. union {
  335. u32 vertex_begin_gl;
  336. BitField<0, 16, PrimitiveTopology> topology;
  337. };
  338. } draw;
  339. INSERT_PADDING_WORDS(0x6B);
  340. struct {
  341. u32 start_addr_high;
  342. u32 start_addr_low;
  343. u32 end_addr_high;
  344. u32 end_addr_low;
  345. IndexFormat format;
  346. u32 first;
  347. u32 count;
  348. unsigned FormatSizeInBytes() const {
  349. switch (format) {
  350. case IndexFormat::UnsignedByte:
  351. return 1;
  352. case IndexFormat::UnsignedShort:
  353. return 2;
  354. case IndexFormat::UnsignedInt:
  355. return 4;
  356. }
  357. UNREACHABLE();
  358. }
  359. GPUVAddr StartAddress() const {
  360. return static_cast<GPUVAddr>(
  361. (static_cast<GPUVAddr>(start_addr_high) << 32) | start_addr_low);
  362. }
  363. GPUVAddr EndAddress() const {
  364. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(end_addr_high) << 32) |
  365. end_addr_low);
  366. }
  367. } index_array;
  368. INSERT_PADDING_WORDS(0xC7);
  369. struct {
  370. u32 query_address_high;
  371. u32 query_address_low;
  372. u32 query_sequence;
  373. union {
  374. u32 raw;
  375. BitField<0, 2, QueryMode> mode;
  376. BitField<4, 1, u32> fence;
  377. BitField<12, 4, u32> unit;
  378. } query_get;
  379. GPUVAddr QueryAddress() const {
  380. return static_cast<GPUVAddr>(
  381. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  382. }
  383. } query;
  384. INSERT_PADDING_WORDS(0x3C);
  385. struct {
  386. union {
  387. BitField<0, 12, u32> stride;
  388. BitField<12, 1, u32> enable;
  389. };
  390. u32 start_high;
  391. u32 start_low;
  392. u32 divisor;
  393. GPUVAddr StartAddress() const {
  394. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  395. start_low);
  396. }
  397. } vertex_array[NumVertexArrays];
  398. INSERT_PADDING_WORDS(0x40);
  399. struct {
  400. u32 limit_high;
  401. u32 limit_low;
  402. GPUVAddr LimitAddress() const {
  403. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  404. limit_low);
  405. }
  406. } vertex_array_limit[NumVertexArrays];
  407. struct {
  408. union {
  409. BitField<0, 1, u32> enable;
  410. BitField<4, 4, ShaderProgram> program;
  411. };
  412. u32 offset;
  413. INSERT_PADDING_WORDS(14);
  414. } shader_config[MaxShaderProgram];
  415. INSERT_PADDING_WORDS(0x80);
  416. struct {
  417. u32 cb_size;
  418. u32 cb_address_high;
  419. u32 cb_address_low;
  420. u32 cb_pos;
  421. u32 cb_data[NumCBData];
  422. GPUVAddr BufferAddress() const {
  423. return static_cast<GPUVAddr>(
  424. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  425. }
  426. } const_buffer;
  427. INSERT_PADDING_WORDS(0x10);
  428. struct {
  429. union {
  430. u32 raw_config;
  431. BitField<0, 1, u32> valid;
  432. BitField<4, 5, u32> index;
  433. };
  434. INSERT_PADDING_WORDS(7);
  435. } cb_bind[MaxShaderStage];
  436. INSERT_PADDING_WORDS(0x56);
  437. u32 tex_cb_index;
  438. INSERT_PADDING_WORDS(0x395);
  439. struct {
  440. /// Compressed address of a buffer that holds information about bound SSBOs.
  441. /// This address is usually bound to c0 in the shaders.
  442. u32 buffer_address;
  443. GPUVAddr BufferAddress() const {
  444. return static_cast<GPUVAddr>(buffer_address) << 8;
  445. }
  446. } ssbo_info;
  447. INSERT_PADDING_WORDS(0x11);
  448. struct {
  449. u32 address[MaxShaderStage];
  450. u32 size[MaxShaderStage];
  451. } tex_info_buffers;
  452. INSERT_PADDING_WORDS(0x102);
  453. };
  454. std::array<u32, NUM_REGS> reg_array;
  455. };
  456. } regs{};
  457. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  458. struct State {
  459. struct ConstBufferInfo {
  460. GPUVAddr address;
  461. u32 index;
  462. u32 size;
  463. bool enabled;
  464. };
  465. struct ShaderStageInfo {
  466. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  467. };
  468. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  469. };
  470. State state{};
  471. MemoryManager& memory_manager;
  472. /// Reads a register value located at the input method address
  473. u32 GetRegisterValue(u32 method) const;
  474. /// Write the value to the register identified by method.
  475. void WriteReg(u32 method, u32 value, u32 remaining_params);
  476. /// Uploads the code for a GPU macro program associated with the specified entry.
  477. void SubmitMacroCode(u32 entry, std::vector<u32> code);
  478. /// Returns a list of enabled textures for the specified shader stage.
  479. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  480. /// Returns whether the specified shader stage is enabled or not.
  481. bool IsShaderStageEnabled(Regs::ShaderStage stage) const;
  482. private:
  483. std::unordered_map<u32, std::vector<u32>> uploaded_macros;
  484. /// Macro method that is currently being executed / being fed parameters.
  485. u32 executing_macro = 0;
  486. /// Parameters that have been submitted to the macro call so far.
  487. std::vector<u32> macro_params;
  488. /// Interpreter for the macro codes uploaded to the GPU.
  489. MacroInterpreter macro_interpreter;
  490. /// Retrieves information about a specific TIC entry from the TIC buffer.
  491. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  492. /// Retrieves information about a specific TSC entry from the TSC buffer.
  493. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  494. /**
  495. * Call a macro on this engine.
  496. * @param method Method to call
  497. * @param parameters Arguments to the method call
  498. */
  499. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  500. /// Handles a write to the QUERY_GET register.
  501. void ProcessQueryGet();
  502. /// Handles a write to the CB_DATA[i] register.
  503. void ProcessCBData(u32 value);
  504. /// Handles a write to the CB_BIND register.
  505. void ProcessCBBind(Regs::ShaderStage stage);
  506. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  507. void DrawArrays();
  508. };
  509. #define ASSERT_REG_POSITION(field_name, position) \
  510. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  511. "Field " #field_name " has invalid position")
  512. ASSERT_REG_POSITION(rt, 0x200);
  513. ASSERT_REG_POSITION(viewport, 0x300);
  514. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  515. ASSERT_REG_POSITION(zeta, 0x3F8);
  516. ASSERT_REG_POSITION(vertex_attrib_format[0], 0x458);
  517. ASSERT_REG_POSITION(rt_control, 0x487);
  518. ASSERT_REG_POSITION(tsc, 0x557);
  519. ASSERT_REG_POSITION(tic, 0x55D);
  520. ASSERT_REG_POSITION(code_address, 0x582);
  521. ASSERT_REG_POSITION(draw, 0x585);
  522. ASSERT_REG_POSITION(index_array, 0x5F2);
  523. ASSERT_REG_POSITION(query, 0x6C0);
  524. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  525. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  526. ASSERT_REG_POSITION(shader_config[0], 0x800);
  527. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  528. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  529. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  530. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  531. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  532. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  533. #undef ASSERT_REG_POSITION
  534. } // namespace Engines
  535. } // namespace Tegra