registry.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <tuple>
  6. #include "common/assert.h"
  7. #include "common/common_types.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/shader/registry.h"
  12. namespace VideoCommon::Shader {
  13. using Tegra::Engines::ConstBufferEngineInterface;
  14. using Tegra::Engines::SamplerDescriptor;
  15. using Tegra::Engines::ShaderType;
  16. namespace {
  17. GraphicsInfo MakeGraphicsInfo(ShaderType shader_stage, ConstBufferEngineInterface& engine) {
  18. if (shader_stage == ShaderType::Compute) {
  19. return {};
  20. }
  21. auto& graphics = static_cast<Tegra::Engines::Maxwell3D&>(engine);
  22. GraphicsInfo info;
  23. info.primitive_topology = graphics.regs.draw.topology;
  24. info.tessellation_primitive = graphics.regs.tess_mode.prim;
  25. info.tessellation_spacing = graphics.regs.tess_mode.spacing;
  26. info.tessellation_clockwise = graphics.regs.tess_mode.cw;
  27. return info;
  28. }
  29. ComputeInfo MakeComputeInfo(ShaderType shader_stage, ConstBufferEngineInterface& engine) {
  30. if (shader_stage != ShaderType::Compute) {
  31. return {};
  32. }
  33. auto& compute = static_cast<Tegra::Engines::KeplerCompute&>(engine);
  34. const auto& launch = compute.launch_description;
  35. ComputeInfo info;
  36. info.workgroup_size = {launch.block_dim_x, launch.block_dim_y, launch.block_dim_z};
  37. info.local_memory_size_in_words = launch.local_pos_alloc;
  38. info.shared_memory_size_in_words = launch.shared_alloc;
  39. return info;
  40. }
  41. } // Anonymous namespace
  42. Registry::Registry(Tegra::Engines::ShaderType shader_stage, const SerializedRegistryInfo& info)
  43. : stage{shader_stage}, stored_guest_driver_profile{info.guest_driver_profile},
  44. bound_buffer{info.bound_buffer}, graphics_info{info.graphics}, compute_info{info.compute} {}
  45. Registry::Registry(Tegra::Engines::ShaderType shader_stage,
  46. Tegra::Engines::ConstBufferEngineInterface& engine)
  47. : stage{shader_stage}, engine{&engine}, bound_buffer{engine.GetBoundBuffer()},
  48. graphics_info{MakeGraphicsInfo(shader_stage, engine)}, compute_info{MakeComputeInfo(
  49. shader_stage, engine)} {}
  50. Registry::~Registry() = default;
  51. std::optional<u32> Registry::ObtainKey(u32 buffer, u32 offset) {
  52. const std::pair<u32, u32> key = {buffer, offset};
  53. const auto iter = keys.find(key);
  54. if (iter != keys.end()) {
  55. return iter->second;
  56. }
  57. if (!engine) {
  58. return std::nullopt;
  59. }
  60. const u32 value = engine->AccessConstBuffer32(stage, buffer, offset);
  61. keys.emplace(key, value);
  62. return value;
  63. }
  64. std::optional<SamplerDescriptor> Registry::ObtainBoundSampler(u32 offset) {
  65. const u32 key = offset;
  66. const auto iter = bound_samplers.find(key);
  67. if (iter != bound_samplers.end()) {
  68. return iter->second;
  69. }
  70. if (!engine) {
  71. return std::nullopt;
  72. }
  73. const SamplerDescriptor value = engine->AccessBoundSampler(stage, offset);
  74. bound_samplers.emplace(key, value);
  75. return value;
  76. }
  77. std::optional<Tegra::Engines::SamplerDescriptor> Registry::ObtainBindlessSampler(u32 buffer,
  78. u32 offset) {
  79. const std::pair key = {buffer, offset};
  80. const auto iter = bindless_samplers.find(key);
  81. if (iter != bindless_samplers.end()) {
  82. return iter->second;
  83. }
  84. if (!engine) {
  85. return std::nullopt;
  86. }
  87. const SamplerDescriptor value = engine->AccessBindlessSampler(stage, buffer, offset);
  88. bindless_samplers.emplace(key, value);
  89. return value;
  90. }
  91. void Registry::InsertKey(u32 buffer, u32 offset, u32 value) {
  92. keys.insert_or_assign({buffer, offset}, value);
  93. }
  94. void Registry::InsertBoundSampler(u32 offset, SamplerDescriptor sampler) {
  95. bound_samplers.insert_or_assign(offset, sampler);
  96. }
  97. void Registry::InsertBindlessSampler(u32 buffer, u32 offset, SamplerDescriptor sampler) {
  98. bindless_samplers.insert_or_assign({buffer, offset}, sampler);
  99. }
  100. bool Registry::IsConsistent() const {
  101. if (!engine) {
  102. return true;
  103. }
  104. return std::all_of(keys.begin(), keys.end(),
  105. [this](const auto& pair) {
  106. const auto [cbuf, offset] = pair.first;
  107. const auto value = pair.second;
  108. return value == engine->AccessConstBuffer32(stage, cbuf, offset);
  109. }) &&
  110. std::all_of(bound_samplers.begin(), bound_samplers.end(),
  111. [this](const auto& sampler) {
  112. const auto [key, value] = sampler;
  113. return value == engine->AccessBoundSampler(stage, key);
  114. }) &&
  115. std::all_of(bindless_samplers.begin(), bindless_samplers.end(),
  116. [this](const auto& sampler) {
  117. const auto [cbuf, offset] = sampler.first;
  118. const auto value = sampler.second;
  119. return value == engine->AccessBindlessSampler(stage, cbuf, offset);
  120. });
  121. }
  122. bool Registry::HasEqualKeys(const Registry& rhs) const {
  123. return std::tie(keys, bound_samplers, bindless_samplers) ==
  124. std::tie(rhs.keys, rhs.bound_samplers, rhs.bindless_samplers);
  125. }
  126. const GraphicsInfo& Registry::GetGraphicsInfo() const {
  127. ASSERT(stage != Tegra::Engines::ShaderType::Compute);
  128. return graphics_info;
  129. }
  130. const ComputeInfo& Registry::GetComputeInfo() const {
  131. ASSERT(stage == Tegra::Engines::ShaderType::Compute);
  132. return compute_info;
  133. }
  134. } // namespace VideoCommon::Shader