kepler_compute.h 8.6 KB

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