transform_feedback.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <unordered_map>
  7. #include "common/assert.h"
  8. #include "common/common_types.h"
  9. #include "video_core/engines/maxwell_3d.h"
  10. #include "video_core/shader/registry.h"
  11. #include "video_core/shader/transform_feedback.h"
  12. namespace VideoCommon::Shader {
  13. namespace {
  14. using Maxwell = Tegra::Engines::Maxwell3D::Regs;
  15. // TODO(Rodrigo): Change this to constexpr std::unordered_set in C++20
  16. /// Attribute offsets that describe a vector
  17. constexpr std::array VECTORS = {
  18. 28, // gl_Position
  19. 32, // Generic 0
  20. 36, // Generic 1
  21. 40, // Generic 2
  22. 44, // Generic 3
  23. 48, // Generic 4
  24. 52, // Generic 5
  25. 56, // Generic 6
  26. 60, // Generic 7
  27. 64, // Generic 8
  28. 68, // Generic 9
  29. 72, // Generic 10
  30. 76, // Generic 11
  31. 80, // Generic 12
  32. 84, // Generic 13
  33. 88, // Generic 14
  34. 92, // Generic 15
  35. 96, // Generic 16
  36. 100, // Generic 17
  37. 104, // Generic 18
  38. 108, // Generic 19
  39. 112, // Generic 20
  40. 116, // Generic 21
  41. 120, // Generic 22
  42. 124, // Generic 23
  43. 128, // Generic 24
  44. 132, // Generic 25
  45. 136, // Generic 26
  46. 140, // Generic 27
  47. 144, // Generic 28
  48. 148, // Generic 29
  49. 152, // Generic 30
  50. 156, // Generic 31
  51. 160, // gl_FrontColor
  52. 164, // gl_FrontSecondaryColor
  53. 160, // gl_BackColor
  54. 164, // gl_BackSecondaryColor
  55. 192, // gl_TexCoord[0]
  56. 196, // gl_TexCoord[1]
  57. 200, // gl_TexCoord[2]
  58. 204, // gl_TexCoord[3]
  59. 208, // gl_TexCoord[4]
  60. 212, // gl_TexCoord[5]
  61. 216, // gl_TexCoord[6]
  62. 220, // gl_TexCoord[7]
  63. };
  64. } // namespace
  65. std::unordered_map<u8, VaryingTFB> BuildTransformFeedback(const GraphicsInfo& info) {
  66. std::unordered_map<u8, VaryingTFB> tfb;
  67. for (std::size_t buffer = 0; buffer < Maxwell::NumTransformFeedbackBuffers; ++buffer) {
  68. const auto& locations = info.tfb_varying_locs[buffer];
  69. const auto& layout = info.tfb_layouts[buffer];
  70. const std::size_t varying_count = layout.varying_count;
  71. std::size_t highest = 0;
  72. for (std::size_t offset = 0; offset < varying_count; ++offset) {
  73. const std::size_t base_offset = offset;
  74. const u8 location = locations[offset];
  75. VaryingTFB varying;
  76. varying.buffer = layout.stream;
  77. varying.stride = layout.stride;
  78. varying.offset = offset * sizeof(u32);
  79. varying.components = 1;
  80. if (std::find(VECTORS.begin(), VECTORS.end(), location / 4 * 4) != VECTORS.end()) {
  81. UNIMPLEMENTED_IF_MSG(location % 4 != 0, "Unaligned TFB");
  82. const u8 base_index = location / 4;
  83. while (offset + 1 < varying_count && base_index == locations[offset + 1] / 4) {
  84. ++offset;
  85. ++varying.components;
  86. }
  87. }
  88. [[maybe_unused]] const bool inserted = tfb.emplace(location, varying).second;
  89. UNIMPLEMENTED_IF_MSG(!inserted, "Varying already stored");
  90. highest = std::max(highest, (base_offset + varying.components) * sizeof(u32));
  91. }
  92. UNIMPLEMENTED_IF(highest != layout.stride);
  93. }
  94. return tfb;
  95. }
  96. } // namespace VideoCommon::Shader