kepler_compute.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(const GPU::MethodCall& method_call) {
  23. ASSERT_MSG(method_call.method < Regs::NUM_REGS,
  24. "Invalid KeplerCompute register, increase the size of the Regs structure");
  25. regs.reg_array[method_call.method] = method_call.argument;
  26. switch (method_call.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. const bool is_last_call = method_call.IsLastCall();
  33. upload_state.ProcessData(method_call.argument, is_last_call);
  34. break;
  35. }
  36. case KEPLER_COMPUTE_REG_INDEX(launch):
  37. ProcessLaunch();
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. Texture::FullTextureInfo KeplerCompute::GetTexture(std::size_t offset) const {
  44. const std::bitset<8> cbuf_mask = launch_description.const_buffer_enable_mask.Value();
  45. ASSERT(cbuf_mask[regs.tex_cb_index]);
  46. const auto& texinfo = launch_description.const_buffer_config[regs.tex_cb_index];
  47. ASSERT(texinfo.Address() != 0);
  48. const GPUVAddr address = texinfo.Address() + offset * sizeof(Texture::TextureHandle);
  49. ASSERT(address < texinfo.Address() + texinfo.size);
  50. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(address)};
  51. return GetTextureInfo(tex_handle);
  52. }
  53. Texture::FullTextureInfo KeplerCompute::GetTextureInfo(Texture::TextureHandle tex_handle) const {
  54. return Texture::FullTextureInfo{GetTICEntry(tex_handle.tic_id), GetTSCEntry(tex_handle.tsc_id)};
  55. }
  56. u32 KeplerCompute::AccessConstBuffer32(ShaderType stage, u64 const_buffer, u64 offset) const {
  57. ASSERT(stage == ShaderType::Compute);
  58. const auto& buffer = launch_description.const_buffer_config[const_buffer];
  59. u32 result;
  60. std::memcpy(&result, memory_manager.GetPointer(buffer.Address() + offset), sizeof(u32));
  61. return result;
  62. }
  63. SamplerDescriptor KeplerCompute::AccessBoundSampler(ShaderType stage, u64 offset) const {
  64. return AccessBindlessSampler(stage, regs.tex_cb_index, offset * sizeof(Texture::TextureHandle));
  65. }
  66. SamplerDescriptor KeplerCompute::AccessBindlessSampler(ShaderType stage, u64 const_buffer,
  67. u64 offset) const {
  68. ASSERT(stage == ShaderType::Compute);
  69. const auto& tex_info_buffer = launch_description.const_buffer_config[const_buffer];
  70. const GPUVAddr tex_info_address = tex_info_buffer.Address() + offset;
  71. const Texture::TextureHandle tex_handle{memory_manager.Read<u32>(tex_info_address)};
  72. const Texture::FullTextureInfo tex_info = GetTextureInfo(tex_handle);
  73. SamplerDescriptor result = SamplerDescriptor::FromTicTexture(tex_info.tic.texture_type.Value());
  74. result.is_shadow.Assign(tex_info.tsc.depth_compare_enabled.Value());
  75. return result;
  76. }
  77. VideoCore::GuestDriverProfile& KeplerCompute::AccessGuestDriverProfile() {
  78. return rasterizer.AccessGuestDriverProfile();
  79. }
  80. const VideoCore::GuestDriverProfile& KeplerCompute::AccessGuestDriverProfile() const {
  81. return rasterizer.AccessGuestDriverProfile();
  82. }
  83. void KeplerCompute::ProcessLaunch() {
  84. const GPUVAddr launch_desc_loc = regs.launch_desc_loc.Address();
  85. memory_manager.ReadBlockUnsafe(launch_desc_loc, &launch_description,
  86. LaunchParams::NUM_LAUNCH_PARAMETERS * sizeof(u32));
  87. const GPUVAddr code_addr = regs.code_loc.Address() + launch_description.program_start;
  88. LOG_TRACE(HW_GPU, "Compute invocation launched at address 0x{:016x}", code_addr);
  89. rasterizer.DispatchCompute(code_addr);
  90. }
  91. Texture::TICEntry KeplerCompute::GetTICEntry(u32 tic_index) const {
  92. const GPUVAddr tic_address_gpu{regs.tic.Address() + tic_index * sizeof(Texture::TICEntry)};
  93. Texture::TICEntry tic_entry;
  94. memory_manager.ReadBlockUnsafe(tic_address_gpu, &tic_entry, sizeof(Texture::TICEntry));
  95. const auto r_type{tic_entry.r_type.Value()};
  96. const auto g_type{tic_entry.g_type.Value()};
  97. const auto b_type{tic_entry.b_type.Value()};
  98. const auto a_type{tic_entry.a_type.Value()};
  99. // TODO(Subv): Different data types for separate components are not supported
  100. DEBUG_ASSERT(r_type == g_type && r_type == b_type && r_type == a_type);
  101. return tic_entry;
  102. }
  103. Texture::TSCEntry KeplerCompute::GetTSCEntry(u32 tsc_index) const {
  104. const GPUVAddr tsc_address_gpu{regs.tsc.Address() + tsc_index * sizeof(Texture::TSCEntry)};
  105. Texture::TSCEntry tsc_entry;
  106. memory_manager.ReadBlockUnsafe(tsc_address_gpu, &tsc_entry, sizeof(Texture::TSCEntry));
  107. return tsc_entry;
  108. }
  109. } // namespace Tegra::Engines