kepler_compute.h 7.9 KB

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