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