VertexLoader_Position.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoCommon/VertexLoader_Position.h"
  4. #include <limits>
  5. #include <type_traits>
  6. #include "Common/CommonTypes.h"
  7. #include "Common/EnumMap.h"
  8. #include "Common/Swap.h"
  9. #include "VideoCommon/VertexLoader.h"
  10. #include "VideoCommon/VertexLoaderManager.h"
  11. #include "VideoCommon/VertexLoaderUtils.h"
  12. #include "VideoCommon/VideoCommon.h"
  13. namespace
  14. {
  15. template <typename T>
  16. constexpr float PosScale(T val, float scale)
  17. {
  18. return val * scale;
  19. }
  20. template <>
  21. constexpr float PosScale(float val, [[maybe_unused]] float scale)
  22. {
  23. return val;
  24. }
  25. template <typename T, int N>
  26. void Pos_ReadDirect(VertexLoader* loader)
  27. {
  28. static_assert(N <= 3, "N > 3 is not sane!");
  29. const auto scale = loader->m_posScale;
  30. for (int i = 0; i < N; ++i)
  31. {
  32. const float value = PosScale(DataRead<T>(), scale);
  33. if (loader->m_remaining < 3)
  34. VertexLoaderManager::position_cache[loader->m_remaining][i] = value;
  35. DataWrite(value);
  36. }
  37. LOG_VTX();
  38. }
  39. template <typename I, typename T, int N>
  40. void Pos_ReadIndex(VertexLoader* loader)
  41. {
  42. static_assert(std::is_unsigned<I>::value, "Only unsigned I is sane!");
  43. static_assert(N <= 3, "N > 3 is not sane!");
  44. const auto index = DataRead<I>();
  45. loader->m_vertexSkip = index == std::numeric_limits<I>::max();
  46. const auto data =
  47. reinterpret_cast<const T*>(VertexLoaderManager::cached_arraybases[CPArray::Position] +
  48. (index * g_main_cp_state.array_strides[CPArray::Position]));
  49. const auto scale = loader->m_posScale;
  50. for (int i = 0; i < N; ++i)
  51. {
  52. const float value = PosScale(Common::FromBigEndian(data[i]), scale);
  53. if (loader->m_remaining < 3 && !loader->m_vertexSkip)
  54. VertexLoaderManager::position_cache[loader->m_remaining][i] = value;
  55. DataWrite(value);
  56. }
  57. LOG_VTX();
  58. }
  59. using ComponentCountRow = Common::EnumMap<TPipelineFunction, CoordComponentCount::XYZ>;
  60. using ComponentFormatTable = Common::EnumMap<ComponentCountRow, ComponentFormat::InvalidFloat7>;
  61. using Table = Common::EnumMap<ComponentFormatTable, VertexComponentFormat::Index16>;
  62. constexpr Table s_table_read_position = {
  63. ComponentFormatTable({
  64. ComponentCountRow(nullptr, nullptr),
  65. ComponentCountRow(nullptr, nullptr),
  66. ComponentCountRow(nullptr, nullptr),
  67. ComponentCountRow(nullptr, nullptr),
  68. ComponentCountRow(nullptr, nullptr),
  69. ComponentCountRow(nullptr, nullptr),
  70. ComponentCountRow(nullptr, nullptr),
  71. ComponentCountRow(nullptr, nullptr),
  72. }),
  73. ComponentFormatTable({
  74. ComponentCountRow(Pos_ReadDirect<u8, 2>, Pos_ReadDirect<u8, 3>),
  75. ComponentCountRow(Pos_ReadDirect<s8, 2>, Pos_ReadDirect<s8, 3>),
  76. ComponentCountRow(Pos_ReadDirect<u16, 2>, Pos_ReadDirect<u16, 3>),
  77. ComponentCountRow(Pos_ReadDirect<s16, 2>, Pos_ReadDirect<s16, 3>),
  78. ComponentCountRow(Pos_ReadDirect<float, 2>, Pos_ReadDirect<float, 3>),
  79. ComponentCountRow(Pos_ReadDirect<float, 2>, Pos_ReadDirect<float, 3>),
  80. ComponentCountRow(Pos_ReadDirect<float, 2>, Pos_ReadDirect<float, 3>),
  81. ComponentCountRow(Pos_ReadDirect<float, 2>, Pos_ReadDirect<float, 3>),
  82. }),
  83. ComponentFormatTable({
  84. ComponentCountRow(Pos_ReadIndex<u8, u8, 2>, Pos_ReadIndex<u8, u8, 3>),
  85. ComponentCountRow(Pos_ReadIndex<u8, s8, 2>, Pos_ReadIndex<u8, s8, 3>),
  86. ComponentCountRow(Pos_ReadIndex<u8, u16, 2>, Pos_ReadIndex<u8, u16, 3>),
  87. ComponentCountRow(Pos_ReadIndex<u8, s16, 2>, Pos_ReadIndex<u8, s16, 3>),
  88. ComponentCountRow(Pos_ReadIndex<u8, float, 2>, Pos_ReadIndex<u8, float, 3>),
  89. ComponentCountRow(Pos_ReadIndex<u8, float, 2>, Pos_ReadIndex<u8, float, 3>),
  90. ComponentCountRow(Pos_ReadIndex<u8, float, 2>, Pos_ReadIndex<u8, float, 3>),
  91. ComponentCountRow(Pos_ReadIndex<u8, float, 2>, Pos_ReadIndex<u8, float, 3>),
  92. }),
  93. ComponentFormatTable({
  94. ComponentCountRow(Pos_ReadIndex<u16, u8, 2>, Pos_ReadIndex<u16, u8, 3>),
  95. ComponentCountRow(Pos_ReadIndex<u16, s8, 2>, Pos_ReadIndex<u16, s8, 3>),
  96. ComponentCountRow(Pos_ReadIndex<u16, u16, 2>, Pos_ReadIndex<u16, u16, 3>),
  97. ComponentCountRow(Pos_ReadIndex<u16, s16, 2>, Pos_ReadIndex<u16, s16, 3>),
  98. ComponentCountRow(Pos_ReadIndex<u16, float, 2>, Pos_ReadIndex<u16, float, 3>),
  99. ComponentCountRow(Pos_ReadIndex<u16, float, 2>, Pos_ReadIndex<u16, float, 3>),
  100. ComponentCountRow(Pos_ReadIndex<u16, float, 2>, Pos_ReadIndex<u16, float, 3>),
  101. ComponentCountRow(Pos_ReadIndex<u16, float, 2>, Pos_ReadIndex<u16, float, 3>),
  102. }),
  103. };
  104. } // Anonymous namespace
  105. TPipelineFunction VertexLoader_Position::GetFunction(VertexComponentFormat type,
  106. ComponentFormat format,
  107. CoordComponentCount elements)
  108. {
  109. return s_table_read_position[type][format][elements];
  110. }