kepler_compute.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <bitset>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/core.h"
  8. #include "video_core/engines/kepler_compute.h"
  9. #include "video_core/engines/maxwell_3d.h"
  10. #include "video_core/engines/shader_type.h"
  11. #include "video_core/memory_manager.h"
  12. #include "video_core/rasterizer_interface.h"
  13. #include "video_core/renderer_base.h"
  14. #include "video_core/textures/decoders.h"
  15. namespace Tegra::Engines {
  16. KeplerCompute::KeplerCompute(Core::System& system, VideoCore::RasterizerInterface& rasterizer,
  17. MemoryManager& memory_manager)
  18. : system{system}, rasterizer{rasterizer}, memory_manager{memory_manager}, upload_state{
  19. memory_manager,
  20. regs.upload} {}
  21. KeplerCompute::~KeplerCompute() = default;
  22. void KeplerCompute::CallMethod(u32 method, u32 method_argument, bool is_last_call) {
  23. ASSERT_MSG(method < Regs::NUM_REGS,
  24. "Invalid KeplerCompute register, increase the size of the Regs structure");
  25. regs.reg_array[method] = method_argument;
  26. switch (method) {
  27. case KEPLER_COMPUTE_REG_INDEX(exec_upload): {
  28. upload_state.ProcessExec(regs.exec_upload.linear != 0);
  29. break;
  30. }
  31. case KEPLER_COMPUTE_REG_INDEX(data_upload): {
  32. upload_state.ProcessData(method_argument, is_last_call);
  33. if (is_last_call) {
  34. system.GPU().Maxwell3D().OnMemoryWrite();
  35. }
  36. break;
  37. }
  38. case KEPLER_COMPUTE_REG_INDEX(launch):
  39. ProcessLaunch();
  40. break;
  41. default:
  42. break;
  43. }
  44. }
  45. void KeplerCompute::CallMultiMethod(u32 method, const u32* base_start, u32 amount,
  46. u32 methods_pending) {
  47. for (std::size_t i = 0; i < amount; i++) {
  48. CallMethod(method, base_start[i], methods_pending - static_cast<u32>(i) <= 1);
  49. }
  50. }
  51. Texture::FullTextureInfo KeplerCompute::GetTexture(std::size_t offset) const {
  52. const std::bitset<8> cbuf_mask = launch_description.const_buffer_enable_mask.Value();
  53. ASSERT(cbuf_mask[regs.tex_cb_index]);
  54. const auto& texinfo = launch_description.const_buffer_config[regs.tex_cb_index];
  55. ASSERT(texinfo.Address() != 0);
  56. const GPUVAddr address = texinfo.Address() + offset * sizeof(Texture::TextureHandle);
  57. ASSERT(address < texinfo.Address() + texinfo.size);
  58. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(address)};
  59. return GetTextureInfo(tex_handle);
  60. }
  61. Texture::FullTextureInfo KeplerCompute::GetTextureInfo(Texture::TextureHandle tex_handle) const {
  62. return Texture::FullTextureInfo{GetTICEntry(tex_handle.tic_id), GetTSCEntry(tex_handle.tsc_id)};
  63. }
  64. u32 KeplerCompute::AccessConstBuffer32(ShaderType stage, u64 const_buffer, u64 offset) const {
  65. ASSERT(stage == ShaderType::Compute);
  66. const auto& buffer = launch_description.const_buffer_config[const_buffer];
  67. u32 result;
  68. std::memcpy(&result, memory_manager.GetPointer(buffer.Address() + offset), sizeof(u32));
  69. return result;
  70. }
  71. SamplerDescriptor KeplerCompute::AccessBoundSampler(ShaderType stage, u64 offset) const {
  72. return AccessBindlessSampler(stage, regs.tex_cb_index, offset * sizeof(Texture::TextureHandle));
  73. }
  74. SamplerDescriptor KeplerCompute::AccessBindlessSampler(ShaderType stage, u64 const_buffer,
  75. u64 offset) const {
  76. ASSERT(stage == ShaderType::Compute);
  77. const auto& tex_info_buffer = launch_description.const_buffer_config[const_buffer];
  78. const GPUVAddr tex_info_address = tex_info_buffer.Address() + offset;
  79. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  80. const Texture::FullTextureInfo tex_info = GetTextureInfo(tex_handle);
  81. SamplerDescriptor result = SamplerDescriptor::FromTIC(tex_info.tic);
  82. result.is_shadow.Assign(tex_info.tsc.depth_compare_enabled.Value());
  83. return result;
  84. }
  85. VideoCore::GuestDriverProfile& KeplerCompute::AccessGuestDriverProfile() {
  86. return rasterizer.AccessGuestDriverProfile();
  87. }
  88. const VideoCore::GuestDriverProfile& KeplerCompute::AccessGuestDriverProfile() const {
  89. return rasterizer.AccessGuestDriverProfile();
  90. }
  91. void KeplerCompute::ProcessLaunch() {
  92. const GPUVAddr launch_desc_loc = regs.launch_desc_loc.Address();
  93. memory_manager.ReadBlockUnsafe(launch_desc_loc, &launch_description,
  94. LaunchParams::NUM_LAUNCH_PARAMETERS * sizeof(u32));
  95. const GPUVAddr code_addr = regs.code_loc.Address() + launch_description.program_start;
  96. LOG_TRACE(HW_GPU, "Compute invocation launched at address 0x{:016x}", code_addr);
  97. rasterizer.DispatchCompute(code_addr);
  98. }
  99. Texture::TICEntry KeplerCompute::GetTICEntry(u32 tic_index) const {
  100. const GPUVAddr tic_address_gpu{regs.tic.Address() + tic_index * sizeof(Texture::TICEntry)};
  101. Texture::TICEntry tic_entry;
  102. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  103. return tic_entry;
  104. }
  105. Texture::TSCEntry KeplerCompute::GetTSCEntry(u32 tsc_index) const {
  106. const GPUVAddr tsc_address_gpu{regs.tsc.Address() + tsc_index * sizeof(Texture::TSCEntry)};
  107. Texture::TSCEntry tsc_entry;
  108. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  109. return tsc_entry;
  110. }
  111. } // namespace Tegra::Engines