transform_feedback.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <vector>
  6. #include "common/alignment.h"
  7. #include "common/assert.h"
  8. #include "common/polyfill_ranges.h"
  9. #include "shader_recompiler/shader_info.h"
  10. #include "video_core/transform_feedback.h"
  11. namespace VideoCommon {
  12. std::pair<std::array<Shader::TransformFeedbackVarying, 256>, u32> MakeTransformFeedbackVaryings(
  13. const TransformFeedbackState& state) {
  14. static constexpr std::array VECTORS{
  15. 28U, // gl_Position
  16. 32U, // Generic 0
  17. 36U, // Generic 1
  18. 40U, // Generic 2
  19. 44U, // Generic 3
  20. 48U, // Generic 4
  21. 52U, // Generic 5
  22. 56U, // Generic 6
  23. 60U, // Generic 7
  24. 64U, // Generic 8
  25. 68U, // Generic 9
  26. 72U, // Generic 10
  27. 76U, // Generic 11
  28. 80U, // Generic 12
  29. 84U, // Generic 13
  30. 88U, // Generic 14
  31. 92U, // Generic 15
  32. 96U, // Generic 16
  33. 100U, // Generic 17
  34. 104U, // Generic 18
  35. 108U, // Generic 19
  36. 112U, // Generic 20
  37. 116U, // Generic 21
  38. 120U, // Generic 22
  39. 124U, // Generic 23
  40. 128U, // Generic 24
  41. 132U, // Generic 25
  42. 136U, // Generic 26
  43. 140U, // Generic 27
  44. 144U, // Generic 28
  45. 148U, // Generic 29
  46. 152U, // Generic 30
  47. 156U, // Generic 31
  48. 160U, // gl_FrontColor
  49. 164U, // gl_FrontSecondaryColor
  50. 160U, // gl_BackColor
  51. 164U, // gl_BackSecondaryColor
  52. 192U, // gl_TexCoord[0]
  53. 196U, // gl_TexCoord[1]
  54. 200U, // gl_TexCoord[2]
  55. 204U, // gl_TexCoord[3]
  56. 208U, // gl_TexCoord[4]
  57. 212U, // gl_TexCoord[5]
  58. 216U, // gl_TexCoord[6]
  59. 220U, // gl_TexCoord[7]
  60. };
  61. std::array<Shader::TransformFeedbackVarying, 256> xfb{};
  62. u32 count{0};
  63. for (size_t buffer = 0; buffer < state.layouts.size(); ++buffer) {
  64. const auto& locations = state.varyings[buffer];
  65. const auto& layout = state.layouts[buffer];
  66. const u32 varying_count = layout.varying_count;
  67. u32 highest = 0;
  68. for (u32 offset = 0; offset < varying_count; ++offset) {
  69. const auto get_attribute = [&locations](u32 index) -> u32 {
  70. switch (index % 4) {
  71. case 0:
  72. return locations[index / 4].attribute0.Value();
  73. case 1:
  74. return locations[index / 4].attribute1.Value();
  75. case 2:
  76. return locations[index / 4].attribute2.Value();
  77. case 3:
  78. return locations[index / 4].attribute3.Value();
  79. }
  80. UNREACHABLE();
  81. return 0;
  82. };
  83. UNIMPLEMENTED_IF_MSG(layout.stream != 0, "Stream is not zero: {}", layout.stream);
  84. Shader::TransformFeedbackVarying varying{
  85. .buffer = static_cast<u32>(buffer),
  86. .stride = layout.stride,
  87. .offset = offset * 4,
  88. .components = 1,
  89. };
  90. const u32 base_offset = offset;
  91. const auto attribute{get_attribute(offset)};
  92. if (std::ranges::find(VECTORS, Common::AlignDown(attribute, 4)) != VECTORS.end()) {
  93. UNIMPLEMENTED_IF_MSG(attribute % 4 != 0, "Unaligned TFB {}", attribute);
  94. const auto base_index = attribute / 4;
  95. while (offset + 1 < varying_count && base_index == get_attribute(offset + 1) / 4) {
  96. ++offset;
  97. ++varying.components;
  98. }
  99. }
  100. xfb[attribute] = varying;
  101. count = std::max(count, attribute);
  102. highest = std::max(highest, (base_offset + varying.components) * 4);
  103. }
  104. UNIMPLEMENTED_IF(highest != layout.stride);
  105. }
  106. return {xfb, count + 1};
  107. }
  108. } // namespace VideoCommon