maxwell_3d.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. union {
  233. struct {
  234. INSERT_PADDING_WORDS(0x200);
  235. struct {
  236. u32 address_high;
  237. u32 address_low;
  238. u32 width;
  239. u32 height;
  240. Tegra::RenderTargetFormat format;
  241. u32 block_dimensions;
  242. u32 array_mode;
  243. u32 layer_stride;
  244. u32 base_layer;
  245. INSERT_PADDING_WORDS(7);
  246. GPUVAddr Address() const {
  247. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  248. address_low);
  249. }
  250. } rt[NumRenderTargets];
  251. INSERT_PADDING_WORDS(0x80);
  252. struct {
  253. union {
  254. BitField<0, 16, u32> x;
  255. BitField<16, 16, u32> width;
  256. };
  257. union {
  258. BitField<0, 16, u32> y;
  259. BitField<16, 16, u32> height;
  260. };
  261. float depth_range_near;
  262. float depth_range_far;
  263. MathUtil::Rectangle<s32> GetRect() const {
  264. return {
  265. static_cast<s32>(x), // left
  266. static_cast<s32>(y + height), // top
  267. static_cast<s32>(x + width), // right
  268. static_cast<s32>(y) // bottom
  269. };
  270. };
  271. } viewport[NumViewports];
  272. INSERT_PADDING_WORDS(0x1D);
  273. struct {
  274. u32 first;
  275. u32 count;
  276. } vertex_buffer;
  277. INSERT_PADDING_WORDS(0x99);
  278. struct {
  279. u32 address_high;
  280. u32 address_low;
  281. u32 format;
  282. u32 block_dimensions;
  283. u32 layer_stride;
  284. GPUVAddr Address() const {
  285. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  286. address_low);
  287. }
  288. } zeta;
  289. INSERT_PADDING_WORDS(0x5B);
  290. VertexAttribute vertex_attrib_format[NumVertexAttributes];
  291. INSERT_PADDING_WORDS(0xF);
  292. struct {
  293. union {
  294. BitField<0, 4, u32> count;
  295. };
  296. } rt_control;
  297. INSERT_PADDING_WORDS(0xCF);
  298. struct {
  299. u32 tsc_address_high;
  300. u32 tsc_address_low;
  301. u32 tsc_limit;
  302. GPUVAddr TSCAddress() const {
  303. return static_cast<GPUVAddr>(
  304. (static_cast<GPUVAddr>(tsc_address_high) << 32) | tsc_address_low);
  305. }
  306. } tsc;
  307. INSERT_PADDING_WORDS(0x3);
  308. struct {
  309. u32 tic_address_high;
  310. u32 tic_address_low;
  311. u32 tic_limit;
  312. GPUVAddr TICAddress() const {
  313. return static_cast<GPUVAddr>(
  314. (static_cast<GPUVAddr>(tic_address_high) << 32) | tic_address_low);
  315. }
  316. } tic;
  317. INSERT_PADDING_WORDS(0x22);
  318. struct {
  319. u32 code_address_high;
  320. u32 code_address_low;
  321. GPUVAddr CodeAddress() const {
  322. return static_cast<GPUVAddr>(
  323. (static_cast<GPUVAddr>(code_address_high) << 32) | code_address_low);
  324. }
  325. } code_address;
  326. INSERT_PADDING_WORDS(1);
  327. struct {
  328. u32 vertex_end_gl;
  329. union {
  330. u32 vertex_begin_gl;
  331. BitField<0, 16, PrimitiveTopology> topology;
  332. };
  333. } draw;
  334. INSERT_PADDING_WORDS(0x139);
  335. struct {
  336. u32 query_address_high;
  337. u32 query_address_low;
  338. u32 query_sequence;
  339. union {
  340. u32 raw;
  341. BitField<0, 2, QueryMode> mode;
  342. BitField<4, 1, u32> fence;
  343. BitField<12, 4, u32> unit;
  344. } query_get;
  345. GPUVAddr QueryAddress() const {
  346. return static_cast<GPUVAddr>(
  347. (static_cast<GPUVAddr>(query_address_high) << 32) | query_address_low);
  348. }
  349. } query;
  350. INSERT_PADDING_WORDS(0x3C);
  351. struct {
  352. union {
  353. BitField<0, 12, u32> stride;
  354. BitField<12, 1, u32> enable;
  355. };
  356. u32 start_high;
  357. u32 start_low;
  358. u32 divisor;
  359. GPUVAddr StartAddress() const {
  360. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(start_high) << 32) |
  361. start_low);
  362. }
  363. } vertex_array[NumVertexArrays];
  364. INSERT_PADDING_WORDS(0x40);
  365. struct {
  366. u32 limit_high;
  367. u32 limit_low;
  368. GPUVAddr LimitAddress() const {
  369. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(limit_high) << 32) |
  370. limit_low);
  371. }
  372. } vertex_array_limit[NumVertexArrays];
  373. struct {
  374. union {
  375. BitField<0, 1, u32> enable;
  376. BitField<4, 4, ShaderProgram> program;
  377. };
  378. u32 offset;
  379. INSERT_PADDING_WORDS(14);
  380. } shader_config[MaxShaderProgram];
  381. INSERT_PADDING_WORDS(0x80);
  382. struct {
  383. u32 cb_size;
  384. u32 cb_address_high;
  385. u32 cb_address_low;
  386. u32 cb_pos;
  387. u32 cb_data[NumCBData];
  388. GPUVAddr BufferAddress() const {
  389. return static_cast<GPUVAddr>(
  390. (static_cast<GPUVAddr>(cb_address_high) << 32) | cb_address_low);
  391. }
  392. } const_buffer;
  393. INSERT_PADDING_WORDS(0x10);
  394. struct {
  395. union {
  396. u32 raw_config;
  397. BitField<0, 1, u32> valid;
  398. BitField<4, 5, u32> index;
  399. };
  400. INSERT_PADDING_WORDS(7);
  401. } cb_bind[MaxShaderStage];
  402. INSERT_PADDING_WORDS(0x56);
  403. u32 tex_cb_index;
  404. INSERT_PADDING_WORDS(0x395);
  405. struct {
  406. /// Compressed address of a buffer that holds information about bound SSBOs.
  407. /// This address is usually bound to c0 in the shaders.
  408. u32 buffer_address;
  409. GPUVAddr BufferAddress() const {
  410. return static_cast<GPUVAddr>(buffer_address) << 8;
  411. }
  412. } ssbo_info;
  413. INSERT_PADDING_WORDS(0x11);
  414. struct {
  415. u32 address[MaxShaderStage];
  416. u32 size[MaxShaderStage];
  417. } tex_info_buffers;
  418. INSERT_PADDING_WORDS(0x102);
  419. };
  420. std::array<u32, NUM_REGS> reg_array;
  421. };
  422. } regs{};
  423. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32), "Maxwell3D Regs has wrong size");
  424. struct State {
  425. struct ConstBufferInfo {
  426. GPUVAddr address;
  427. u32 index;
  428. u32 size;
  429. bool enabled;
  430. };
  431. struct ShaderStageInfo {
  432. std::array<ConstBufferInfo, Regs::MaxConstBuffers> const_buffers;
  433. };
  434. std::array<ShaderStageInfo, Regs::MaxShaderStage> shader_stages;
  435. };
  436. State state{};
  437. /// Reads a register value located at the input method address
  438. u32 GetRegisterValue(u32 method) const;
  439. /// Write the value to the register identified by method.
  440. void WriteReg(u32 method, u32 value, u32 remaining_params);
  441. /// Uploads the code for a GPU macro program associated with the specified entry.
  442. void SubmitMacroCode(u32 entry, std::vector<u32> code);
  443. /// Returns a list of enabled textures for the specified shader stage.
  444. std::vector<Texture::FullTextureInfo> GetStageTextures(Regs::ShaderStage stage) const;
  445. private:
  446. MemoryManager& memory_manager;
  447. std::unordered_map<u32, std::vector<u32>> uploaded_macros;
  448. /// Macro method that is currently being executed / being fed parameters.
  449. u32 executing_macro = 0;
  450. /// Parameters that have been submitted to the macro call so far.
  451. std::vector<u32> macro_params;
  452. /// Interpreter for the macro codes uploaded to the GPU.
  453. MacroInterpreter macro_interpreter;
  454. /// Retrieves information about a specific TIC entry from the TIC buffer.
  455. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  456. /// Retrieves information about a specific TSC entry from the TSC buffer.
  457. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  458. /**
  459. * Call a macro on this engine.
  460. * @param method Method to call
  461. * @param parameters Arguments to the method call
  462. */
  463. void CallMacroMethod(u32 method, std::vector<u32> parameters);
  464. /// Handles a write to the QUERY_GET register.
  465. void ProcessQueryGet();
  466. /// Handles a write to the CB_DATA[i] register.
  467. void ProcessCBData(u32 value);
  468. /// Handles a write to the CB_BIND register.
  469. void ProcessCBBind(Regs::ShaderStage stage);
  470. /// Handles a write to the VERTEX_END_GL register, triggering a draw.
  471. void DrawArrays();
  472. };
  473. #define ASSERT_REG_POSITION(field_name, position) \
  474. static_assert(offsetof(Maxwell3D::Regs, field_name) == position * 4, \
  475. "Field " #field_name " has invalid position")
  476. ASSERT_REG_POSITION(rt, 0x200);
  477. ASSERT_REG_POSITION(viewport, 0x300);
  478. ASSERT_REG_POSITION(vertex_buffer, 0x35D);
  479. ASSERT_REG_POSITION(zeta, 0x3F8);
  480. ASSERT_REG_POSITION(vertex_attrib_format[0], 0x458);
  481. ASSERT_REG_POSITION(rt_control, 0x487);
  482. ASSERT_REG_POSITION(tsc, 0x557);
  483. ASSERT_REG_POSITION(tic, 0x55D);
  484. ASSERT_REG_POSITION(code_address, 0x582);
  485. ASSERT_REG_POSITION(draw, 0x585);
  486. ASSERT_REG_POSITION(query, 0x6C0);
  487. ASSERT_REG_POSITION(vertex_array[0], 0x700);
  488. ASSERT_REG_POSITION(vertex_array_limit[0], 0x7C0);
  489. ASSERT_REG_POSITION(shader_config[0], 0x800);
  490. ASSERT_REG_POSITION(const_buffer, 0x8E0);
  491. ASSERT_REG_POSITION(cb_bind[0], 0x904);
  492. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  493. ASSERT_REG_POSITION(ssbo_info, 0xD18);
  494. ASSERT_REG_POSITION(tex_info_buffers.address[0], 0xD2A);
  495. ASSERT_REG_POSITION(tex_info_buffers.size[0], 0xD2F);
  496. #undef ASSERT_REG_POSITION
  497. } // namespace Engines
  498. } // namespace Tegra