kepler_compute.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <cstddef>
  7. #include <vector>
  8. #include "common/bit_field.h"
  9. #include "common/common_funcs.h"
  10. #include "common/common_types.h"
  11. #include "video_core/engines/engine_interface.h"
  12. #include "video_core/engines/engine_upload.h"
  13. #include "video_core/gpu.h"
  14. #include "video_core/textures/texture.h"
  15. namespace Core {
  16. class System;
  17. }
  18. namespace Tegra {
  19. class MemoryManager;
  20. }
  21. namespace VideoCore {
  22. class RasterizerInterface;
  23. }
  24. namespace Tegra::Engines {
  25. /**
  26. * This Engine is known as GK104_Compute. Documentation can be found in:
  27. * https://github.com/envytools/envytools/blob/master/rnndb/graph/gk104_compute.xml
  28. * https://cgit.freedesktop.org/mesa/mesa/tree/src/gallium/drivers/nouveau/nvc0/nve4_compute.xml.h
  29. */
  30. #define KEPLER_COMPUTE_REG_INDEX(field_name) \
  31. (offsetof(Tegra::Engines::KeplerCompute::Regs, field_name) / sizeof(u32))
  32. class KeplerCompute final : public EngineInterface {
  33. public:
  34. explicit KeplerCompute(Core::System& system, MemoryManager& memory_manager);
  35. ~KeplerCompute();
  36. /// Binds a rasterizer to this engine.
  37. void BindRasterizer(VideoCore::RasterizerInterface* rasterizer);
  38. static constexpr std::size_t NumConstBuffers = 8;
  39. struct Regs {
  40. static constexpr std::size_t NUM_REGS = 0xCF8;
  41. union {
  42. struct {
  43. INSERT_PADDING_WORDS_NOINIT(0x60);
  44. Upload::Registers upload;
  45. struct {
  46. union {
  47. BitField<0, 1, u32> linear;
  48. };
  49. } exec_upload;
  50. u32 data_upload;
  51. INSERT_PADDING_WORDS_NOINIT(0x3F);
  52. struct {
  53. u32 address;
  54. GPUVAddr Address() const {
  55. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address) << 8));
  56. }
  57. } launch_desc_loc;
  58. INSERT_PADDING_WORDS_NOINIT(0x1);
  59. u32 launch;
  60. INSERT_PADDING_WORDS_NOINIT(0x4A7);
  61. struct {
  62. u32 address_high;
  63. u32 address_low;
  64. u32 limit;
  65. GPUVAddr Address() const {
  66. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  67. address_low);
  68. }
  69. } tsc;
  70. INSERT_PADDING_WORDS_NOINIT(0x3);
  71. struct {
  72. u32 address_high;
  73. u32 address_low;
  74. u32 limit;
  75. GPUVAddr Address() const {
  76. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  77. address_low);
  78. }
  79. } tic;
  80. INSERT_PADDING_WORDS_NOINIT(0x22);
  81. struct {
  82. u32 address_high;
  83. u32 address_low;
  84. GPUVAddr Address() const {
  85. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high) << 32) |
  86. address_low);
  87. }
  88. } code_loc;
  89. INSERT_PADDING_WORDS_NOINIT(0x3FE);
  90. u32 tex_cb_index;
  91. INSERT_PADDING_WORDS_NOINIT(0x374);
  92. };
  93. std::array<u32, NUM_REGS> reg_array;
  94. };
  95. } regs{};
  96. struct LaunchParams {
  97. static constexpr std::size_t NUM_LAUNCH_PARAMETERS = 0x40;
  98. INSERT_PADDING_WORDS(0x8);
  99. u32 program_start;
  100. INSERT_PADDING_WORDS(0x2);
  101. BitField<30, 1, u32> linked_tsc;
  102. BitField<0, 31, u32> grid_dim_x;
  103. union {
  104. BitField<0, 16, u32> grid_dim_y;
  105. BitField<16, 16, u32> grid_dim_z;
  106. };
  107. INSERT_PADDING_WORDS(0x3);
  108. BitField<0, 18, u32> shared_alloc;
  109. BitField<16, 16, u32> block_dim_x;
  110. union {
  111. BitField<0, 16, u32> block_dim_y;
  112. BitField<16, 16, u32> block_dim_z;
  113. };
  114. union {
  115. BitField<0, 8, u32> const_buffer_enable_mask;
  116. BitField<29, 2, u32> cache_layout;
  117. };
  118. INSERT_PADDING_WORDS(0x8);
  119. struct ConstBufferConfig {
  120. u32 address_low;
  121. union {
  122. BitField<0, 8, u32> address_high;
  123. BitField<15, 17, u32> size;
  124. };
  125. GPUVAddr Address() const {
  126. return static_cast<GPUVAddr>((static_cast<GPUVAddr>(address_high.Value()) << 32) |
  127. address_low);
  128. }
  129. };
  130. std::array<ConstBufferConfig, NumConstBuffers> const_buffer_config;
  131. union {
  132. BitField<0, 20, u32> local_pos_alloc;
  133. BitField<27, 5, u32> barrier_alloc;
  134. };
  135. union {
  136. BitField<0, 20, u32> local_neg_alloc;
  137. BitField<24, 5, u32> gpr_alloc;
  138. };
  139. union {
  140. BitField<0, 20, u32> local_crs_alloc;
  141. BitField<24, 5, u32> sass_version;
  142. };
  143. INSERT_PADDING_WORDS(0x10);
  144. } launch_description{};
  145. struct {
  146. u32 write_offset = 0;
  147. u32 copy_size = 0;
  148. std::vector<u8> inner_buffer;
  149. } state{};
  150. static_assert(sizeof(Regs) == Regs::NUM_REGS * sizeof(u32),
  151. "KeplerCompute Regs has wrong size");
  152. static_assert(sizeof(LaunchParams) == LaunchParams::NUM_LAUNCH_PARAMETERS * sizeof(u32),
  153. "KeplerCompute LaunchParams has wrong size");
  154. /// Write the value to the register identified by method.
  155. void CallMethod(u32 method, u32 method_argument, bool is_last_call) override;
  156. /// Write multiple values to the register identified by method.
  157. void CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  158. u32 methods_pending) override;
  159. private:
  160. void ProcessLaunch();
  161. /// Retrieves information about a specific TIC entry from the TIC buffer.
  162. Texture::TICEntry GetTICEntry(u32 tic_index) const;
  163. /// Retrieves information about a specific TSC entry from the TSC buffer.
  164. Texture::TSCEntry GetTSCEntry(u32 tsc_index) const;
  165. Core::System& system;
  166. MemoryManager& memory_manager;
  167. VideoCore::RasterizerInterface* rasterizer = nullptr;
  168. Upload::State upload_state;
  169. };
  170. #define ASSERT_REG_POSITION(field_name, position) \
  171. static_assert(offsetof(KeplerCompute::Regs, field_name) == position * 4, \
  172. "Field " #field_name " has invalid position")
  173. #define ASSERT_LAUNCH_PARAM_POSITION(field_name, position) \
  174. static_assert(offsetof(KeplerCompute::LaunchParams, field_name) == position * 4, \
  175. "Field " #field_name " has invalid position")
  176. ASSERT_REG_POSITION(upload, 0x60);
  177. ASSERT_REG_POSITION(exec_upload, 0x6C);
  178. ASSERT_REG_POSITION(data_upload, 0x6D);
  179. ASSERT_REG_POSITION(launch, 0xAF);
  180. ASSERT_REG_POSITION(tsc, 0x557);
  181. ASSERT_REG_POSITION(tic, 0x55D);
  182. ASSERT_REG_POSITION(code_loc, 0x582);
  183. ASSERT_REG_POSITION(tex_cb_index, 0x982);
  184. ASSERT_LAUNCH_PARAM_POSITION(program_start, 0x8);
  185. ASSERT_LAUNCH_PARAM_POSITION(grid_dim_x, 0xC);
  186. ASSERT_LAUNCH_PARAM_POSITION(shared_alloc, 0x11);
  187. ASSERT_LAUNCH_PARAM_POSITION(block_dim_x, 0x12);
  188. ASSERT_LAUNCH_PARAM_POSITION(const_buffer_enable_mask, 0x14);
  189. ASSERT_LAUNCH_PARAM_POSITION(const_buffer_config, 0x1D);
  190. #undef ASSERT_REG_POSITION
  191. } // namespace Tegra::Engines