registry.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = dynamic_cast<Tegra::Engines::Maxwell3D&>(engine);
  22. return {
  23. .tfb_layouts = graphics.regs.tfb_layouts,
  24. .tfb_varying_locs = graphics.regs.tfb_varying_locs,
  25. .primitive_topology = graphics.regs.draw.topology,
  26. .tessellation_primitive = graphics.regs.tess_mode.prim,
  27. .tessellation_spacing = graphics.regs.tess_mode.spacing,
  28. .tfb_enabled = graphics.regs.tfb_enabled != 0,
  29. .tessellation_clockwise = graphics.regs.tess_mode.cw.Value() != 0,
  30. };
  31. }
  32. ComputeInfo MakeComputeInfo(ShaderType shader_stage, ConstBufferEngineInterface& engine) {
  33. if (shader_stage != ShaderType::Compute) {
  34. return {};
  35. }
  36. auto& compute = dynamic_cast<Tegra::Engines::KeplerCompute&>(engine);
  37. const auto& launch = compute.launch_description;
  38. return {
  39. .workgroup_size = {launch.block_dim_x, launch.block_dim_y, launch.block_dim_z},
  40. .shared_memory_size_in_words = launch.shared_alloc,
  41. .local_memory_size_in_words = launch.local_pos_alloc,
  42. };
  43. }
  44. } // Anonymous namespace
  45. Registry::Registry(ShaderType shader_stage, const SerializedRegistryInfo& info)
  46. : stage{shader_stage}, stored_guest_driver_profile{info.guest_driver_profile},
  47. bound_buffer{info.bound_buffer}, graphics_info{info.graphics}, compute_info{info.compute} {}
  48. Registry::Registry(ShaderType shader_stage, ConstBufferEngineInterface& engine_)
  49. : stage{shader_stage}, engine{&engine_}, bound_buffer{engine_.GetBoundBuffer()},
  50. graphics_info{MakeGraphicsInfo(shader_stage, engine_)}, compute_info{MakeComputeInfo(
  51. shader_stage, engine_)} {}
  52. Registry::~Registry() = default;
  53. std::optional<u32> Registry::ObtainKey(u32 buffer, u32 offset) {
  54. const std::pair<u32, u32> key = {buffer, offset};
  55. const auto iter = keys.find(key);
  56. if (iter != keys.end()) {
  57. return iter->second;
  58. }
  59. if (!engine) {
  60. return std::nullopt;
  61. }
  62. const u32 value = engine->AccessConstBuffer32(stage, buffer, offset);
  63. keys.emplace(key, value);
  64. return value;
  65. }
  66. std::optional<SamplerDescriptor> Registry::ObtainBoundSampler(u32 offset) {
  67. const u32 key = offset;
  68. const auto iter = bound_samplers.find(key);
  69. if (iter != bound_samplers.end()) {
  70. return iter->second;
  71. }
  72. if (!engine) {
  73. return std::nullopt;
  74. }
  75. const SamplerDescriptor value = engine->AccessBoundSampler(stage, offset);
  76. bound_samplers.emplace(key, value);
  77. return value;
  78. }
  79. std::optional<Tegra::Engines::SamplerDescriptor> Registry::ObtainSeparateSampler(
  80. std::pair<u32, u32> buffers, std::pair<u32, u32> offsets) {
  81. SeparateSamplerKey key;
  82. key.buffers = buffers;
  83. key.offsets = offsets;
  84. const auto iter = separate_samplers.find(key);
  85. if (iter != separate_samplers.end()) {
  86. return iter->second;
  87. }
  88. if (!engine) {
  89. return std::nullopt;
  90. }
  91. const u32 handle_1 = engine->AccessConstBuffer32(stage, key.buffers.first, key.offsets.first);
  92. const u32 handle_2 = engine->AccessConstBuffer32(stage, key.buffers.second, key.offsets.second);
  93. const SamplerDescriptor value = engine->AccessSampler(handle_1 | handle_2);
  94. separate_samplers.emplace(key, value);
  95. return value;
  96. }
  97. std::optional<SamplerDescriptor> Registry::ObtainBindlessSampler(u32 buffer, u32 offset) {
  98. const std::pair key = {buffer, offset};
  99. const auto iter = bindless_samplers.find(key);
  100. if (iter != bindless_samplers.end()) {
  101. return iter->second;
  102. }
  103. if (!engine) {
  104. return std::nullopt;
  105. }
  106. const SamplerDescriptor value = engine->AccessBindlessSampler(stage, buffer, offset);
  107. bindless_samplers.emplace(key, value);
  108. return value;
  109. }
  110. void Registry::InsertKey(u32 buffer, u32 offset, u32 value) {
  111. keys.insert_or_assign({buffer, offset}, value);
  112. }
  113. void Registry::InsertBoundSampler(u32 offset, SamplerDescriptor sampler) {
  114. bound_samplers.insert_or_assign(offset, sampler);
  115. }
  116. void Registry::InsertBindlessSampler(u32 buffer, u32 offset, SamplerDescriptor sampler) {
  117. bindless_samplers.insert_or_assign({buffer, offset}, sampler);
  118. }
  119. bool Registry::IsConsistent() const {
  120. if (!engine) {
  121. return true;
  122. }
  123. return std::all_of(keys.begin(), keys.end(),
  124. [this](const auto& pair) {
  125. const auto [cbuf, offset] = pair.first;
  126. const auto value = pair.second;
  127. return value == engine->AccessConstBuffer32(stage, cbuf, offset);
  128. }) &&
  129. std::all_of(bound_samplers.begin(), bound_samplers.end(),
  130. [this](const auto& sampler) {
  131. const auto [key, value] = sampler;
  132. return value == engine->AccessBoundSampler(stage, key);
  133. }) &&
  134. std::all_of(bindless_samplers.begin(), bindless_samplers.end(),
  135. [this](const auto& sampler) {
  136. const auto [cbuf, offset] = sampler.first;
  137. const auto value = sampler.second;
  138. return value == engine->AccessBindlessSampler(stage, cbuf, offset);
  139. });
  140. }
  141. bool Registry::HasEqualKeys(const Registry& rhs) const {
  142. return std::tie(keys, bound_samplers, bindless_samplers) ==
  143. std::tie(rhs.keys, rhs.bound_samplers, rhs.bindless_samplers);
  144. }
  145. const GraphicsInfo& Registry::GetGraphicsInfo() const {
  146. ASSERT(stage != Tegra::Engines::ShaderType::Compute);
  147. return graphics_info;
  148. }
  149. const ComputeInfo& Registry::GetComputeInfo() const {
  150. ASSERT(stage == Tegra::Engines::ShaderType::Compute);
  151. return compute_info;
  152. }
  153. } // namespace VideoCommon::Shader