| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/VertexLoader_Normal.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <type_traits> |
| 8 | |
| 9 | #include "Common/CommonTypes.h" |
| 10 | #include "Common/EnumMap.h" |
| 11 | |
| 12 | #include "VideoCommon/VertexLoader.h" |
| 13 | #include "VideoCommon/VertexLoaderManager.h" |
| 14 | #include "VideoCommon/VertexLoaderUtils.h" |
| 15 | |
| 16 | // warning: mapping buffer should be disabled to use this |
| 17 | #define LOG_NORM() // PRIM_LOG("norm: {} {} {}, ", ((float*)g_vertex_manager_write_ptr)[-3], |
| 18 | // ((float*)g_vertex_manager_write_ptr)[-2], |
| 19 | // ((float*)g_vertex_manager_write_ptr)[-1]); |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | template <typename T> |
| 24 | constexpr float FracAdjust(T val) |
| 25 | { |
| 26 | // auto const S8FRAC = 1.f / (1u << 6); |
| 27 | // auto const U8FRAC = 1.f / (1u << 7); |
| 28 | // auto const S16FRAC = 1.f / (1u << 14); |
| 29 | // auto const U16FRAC = 1.f / (1u << 15); |
| 30 | |
| 31 | // TODO: is this right? |
| 32 | return val / float(1u << (sizeof(T) * 8 - std::is_signed_v<T> - 1)); |
| 33 | } |
| 34 | |
| 35 | template <> |
| 36 | constexpr float FracAdjust(float val) |
| 37 | { |
| 38 | return val; |
| 39 | } |
| 40 | |
| 41 | template <typename T, u32 N, u32 Offset> |
| 42 | void ReadIndirect(VertexLoader* loader, const T* data) |
| 43 | { |
| 44 | static_assert(3 == N || 9 == N, "N is only sane as 3 or 9!"); |
| 45 | static_assert(!(Offset != 0 && 9 == N), "N == 9 only makes sense if offset == 0"); |
| 46 | |
| 47 | for (u32 i = Offset; i < N + Offset; ++i) |
| 48 | { |
| 49 | const float value = FracAdjust(Common::FromBigEndian(data[i])); |
| 50 | if (loader->m_remaining == 0) |
| 51 | { |
| 52 | if (i < 3) |
| 53 | VertexLoaderManager::normal_cache[i] = value; |
| 54 | else if (i >= 3 && i < 6) |
| 55 | VertexLoaderManager::tangent_cache[i - 3] = value; |
| 56 | else if (i >= 6 && i < 9) |
| 57 | VertexLoaderManager::binormal_cache[i - 6] = value; |
| 58 | } |
| 59 | DataWrite(value); |
| 60 | } |
| 61 | |
| 62 | LOG_NORM(); |
| 63 | } |
| 64 | |
| 65 | template <typename T, u32 N> |
| 66 | void Normal_ReadDirect(VertexLoader* loader) |
| 67 | { |
| 68 | const auto source = reinterpret_cast<const T*>(DataGetPosition()); |
| 69 | ReadIndirect<T, N * 3, 0>(loader, source); |
| 70 | DataSkip<N * 3 * sizeof(T)>(); |
| 71 | } |
| 72 | |
| 73 | template <typename I, typename T, u32 N, u32 Offset> |
| 74 | void Normal_ReadIndex_Offset(VertexLoader* loader) |
| 75 | { |
| 76 | static_assert(std::is_unsigned_v<I>, "Only unsigned I is sane!"); |
| 77 | |
| 78 | const auto index = DataRead<I>(); |
| 79 | const auto data = |
| 80 | reinterpret_cast<const T*>(VertexLoaderManager::cached_arraybases[CPArray::Normal] + |
| 81 | (index * g_main_cp_state.array_strides[CPArray::Normal])); |
| 82 | ReadIndirect<T, N * 3, Offset * 3>(loader, data); |
| 83 | } |
| 84 | |
| 85 | template <typename I, typename T, u32 N> |
| 86 | void Normal_ReadIndex(VertexLoader* loader) |
| 87 | { |
| 88 | Normal_ReadIndex_Offset<I, T, N, 0>(loader); |
| 89 | } |
| 90 | |
| 91 | template <typename I, typename T> |
| 92 | void Normal_ReadIndex_Indices3(VertexLoader* loader) |
| 93 | { |
| 94 | Normal_ReadIndex_Offset<I, T, 1, 0>(loader); |
| 95 | Normal_ReadIndex_Offset<I, T, 1, 1>(loader); |
| 96 | Normal_ReadIndex_Offset<I, T, 1, 2>(loader); |
| 97 | } |
| 98 | |
| 99 | using Common::EnumMap; |
| 100 | using Formats = EnumMap<TPipelineFunction, ComponentFormat::InvalidFloat7>; |
| 101 | using Elements = EnumMap<Formats, NormalComponentCount::NTB>; |
| 102 | using Indices = std::array<Elements, 2>; |
| 103 | using Types = EnumMap<Indices, VertexComponentFormat::Index16>; |
| 104 | |
| 105 | consteval Types InitializeTable() |
| 106 | { |
| 107 | Types table{}; |
| 108 | |
| 109 | using VCF = VertexComponentFormat; |
| 110 | using NCC = NormalComponentCount; |
| 111 | using FMT = ComponentFormat; |
| 112 | |
| 113 | table[VCF::Direct][false][NCC::N][FMT::UByte] = Normal_ReadDirect<u8, 1>; |
| 114 | table[VCF::Direct][false][NCC::N][FMT::Byte] = Normal_ReadDirect<s8, 1>; |
| 115 | table[VCF::Direct][false][NCC::N][FMT::UShort] = Normal_ReadDirect<u16, 1>; |
| 116 | table[VCF::Direct][false][NCC::N][FMT::Short] = Normal_ReadDirect<s16, 1>; |
| 117 | table[VCF::Direct][false][NCC::N][FMT::Float] = Normal_ReadDirect<float, 1>; |
| 118 | table[VCF::Direct][false][NCC::N][FMT::InvalidFloat5] = Normal_ReadDirect<float, 1>; |
| 119 | table[VCF::Direct][false][NCC::N][FMT::InvalidFloat6] = Normal_ReadDirect<float, 1>; |
| 120 | table[VCF::Direct][false][NCC::N][FMT::InvalidFloat7] = Normal_ReadDirect<float, 1>; |
| 121 | table[VCF::Direct][false][NCC::NTB][FMT::UByte] = Normal_ReadDirect<u8, 3>; |
| 122 | table[VCF::Direct][false][NCC::NTB][FMT::Byte] = Normal_ReadDirect<s8, 3>; |
| 123 | table[VCF::Direct][false][NCC::NTB][FMT::UShort] = Normal_ReadDirect<u16, 3>; |
| 124 | table[VCF::Direct][false][NCC::NTB][FMT::Short] = Normal_ReadDirect<s16, 3>; |
| 125 | table[VCF::Direct][false][NCC::NTB][FMT::Float] = Normal_ReadDirect<float, 3>; |
| 126 | table[VCF::Direct][false][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadDirect<float, 3>; |
| 127 | table[VCF::Direct][false][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadDirect<float, 3>; |
| 128 | table[VCF::Direct][false][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadDirect<float, 3>; |
| 129 | |
| 130 | // Same as above, since there are no indices |
| 131 | table[VCF::Direct][true][NCC::N][FMT::UByte] = Normal_ReadDirect<u8, 1>; |
| 132 | table[VCF::Direct][true][NCC::N][FMT::Byte] = Normal_ReadDirect<s8, 1>; |
| 133 | table[VCF::Direct][true][NCC::N][FMT::UShort] = Normal_ReadDirect<u16, 1>; |
| 134 | table[VCF::Direct][true][NCC::N][FMT::Short] = Normal_ReadDirect<s16, 1>; |
| 135 | table[VCF::Direct][true][NCC::N][FMT::Float] = Normal_ReadDirect<float, 1>; |
| 136 | table[VCF::Direct][true][NCC::N][FMT::InvalidFloat5] = Normal_ReadDirect<float, 1>; |
| 137 | table[VCF::Direct][true][NCC::N][FMT::InvalidFloat6] = Normal_ReadDirect<float, 1>; |
| 138 | table[VCF::Direct][true][NCC::N][FMT::InvalidFloat7] = Normal_ReadDirect<float, 1>; |
| 139 | table[VCF::Direct][true][NCC::NTB][FMT::UByte] = Normal_ReadDirect<u8, 3>; |
| 140 | table[VCF::Direct][true][NCC::NTB][FMT::Byte] = Normal_ReadDirect<s8, 3>; |
| 141 | table[VCF::Direct][true][NCC::NTB][FMT::UShort] = Normal_ReadDirect<u16, 3>; |
| 142 | table[VCF::Direct][true][NCC::NTB][FMT::Short] = Normal_ReadDirect<s16, 3>; |
| 143 | table[VCF::Direct][true][NCC::NTB][FMT::Float] = Normal_ReadDirect<float, 3>; |
| 144 | table[VCF::Direct][true][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadDirect<float, 3>; |
| 145 | table[VCF::Direct][true][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadDirect<float, 3>; |
| 146 | table[VCF::Direct][true][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadDirect<float, 3>; |
| 147 | |
| 148 | table[VCF::Index8][false][NCC::N][FMT::UByte] = Normal_ReadIndex<u8, u8, 1>; |
| 149 | table[VCF::Index8][false][NCC::N][FMT::Byte] = Normal_ReadIndex<u8, s8, 1>; |
| 150 | table[VCF::Index8][false][NCC::N][FMT::UShort] = Normal_ReadIndex<u8, u16, 1>; |
| 151 | table[VCF::Index8][false][NCC::N][FMT::Short] = Normal_ReadIndex<u8, s16, 1>; |
| 152 | table[VCF::Index8][false][NCC::N][FMT::Float] = Normal_ReadIndex<u8, float, 1>; |
| 153 | table[VCF::Index8][false][NCC::N][FMT::InvalidFloat5] = Normal_ReadIndex<u8, float, 1>; |
| 154 | table[VCF::Index8][false][NCC::N][FMT::InvalidFloat6] = Normal_ReadIndex<u8, float, 1>; |
| 155 | table[VCF::Index8][false][NCC::N][FMT::InvalidFloat7] = Normal_ReadIndex<u8, float, 1>; |
| 156 | table[VCF::Index8][false][NCC::NTB][FMT::UByte] = Normal_ReadIndex<u8, u8, 3>; |
| 157 | table[VCF::Index8][false][NCC::NTB][FMT::Byte] = Normal_ReadIndex<u8, s8, 3>; |
| 158 | table[VCF::Index8][false][NCC::NTB][FMT::UShort] = Normal_ReadIndex<u8, u16, 3>; |
| 159 | table[VCF::Index8][false][NCC::NTB][FMT::Short] = Normal_ReadIndex<u8, s16, 3>; |
| 160 | table[VCF::Index8][false][NCC::NTB][FMT::Float] = Normal_ReadIndex<u8, float, 3>; |
| 161 | table[VCF::Index8][false][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadIndex<u8, float, 3>; |
| 162 | table[VCF::Index8][false][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadIndex<u8, float, 3>; |
| 163 | table[VCF::Index8][false][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadIndex<u8, float, 3>; |
| 164 | |
| 165 | // Same for NormalComponentCount::N; differs for NTB |
| 166 | table[VCF::Index8][true][NCC::N][FMT::UByte] = Normal_ReadIndex<u8, u8, 1>; |
| 167 | table[VCF::Index8][true][NCC::N][FMT::Byte] = Normal_ReadIndex<u8, s8, 1>; |
| 168 | table[VCF::Index8][true][NCC::N][FMT::UShort] = Normal_ReadIndex<u8, u16, 1>; |
| 169 | table[VCF::Index8][true][NCC::N][FMT::Short] = Normal_ReadIndex<u8, s16, 1>; |
| 170 | table[VCF::Index8][true][NCC::N][FMT::Float] = Normal_ReadIndex<u8, float, 1>; |
| 171 | table[VCF::Index8][true][NCC::N][FMT::InvalidFloat5] = Normal_ReadIndex<u8, float, 1>; |
| 172 | table[VCF::Index8][true][NCC::N][FMT::InvalidFloat6] = Normal_ReadIndex<u8, float, 1>; |
| 173 | table[VCF::Index8][true][NCC::N][FMT::InvalidFloat7] = Normal_ReadIndex<u8, float, 1>; |
| 174 | table[VCF::Index8][true][NCC::NTB][FMT::UByte] = Normal_ReadIndex_Indices3<u8, u8>; |
| 175 | table[VCF::Index8][true][NCC::NTB][FMT::Byte] = Normal_ReadIndex_Indices3<u8, s8>; |
| 176 | table[VCF::Index8][true][NCC::NTB][FMT::UShort] = Normal_ReadIndex_Indices3<u8, u16>; |
| 177 | table[VCF::Index8][true][NCC::NTB][FMT::Short] = Normal_ReadIndex_Indices3<u8, s16>; |
| 178 | table[VCF::Index8][true][NCC::NTB][FMT::Float] = Normal_ReadIndex_Indices3<u8, float>; |
| 179 | table[VCF::Index8][true][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadIndex_Indices3<u8, float>; |
| 180 | table[VCF::Index8][true][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadIndex_Indices3<u8, float>; |
| 181 | table[VCF::Index8][true][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadIndex_Indices3<u8, float>; |
| 182 | |
| 183 | table[VCF::Index16][false][NCC::N][FMT::UByte] = Normal_ReadIndex<u16, u8, 1>; |
| 184 | table[VCF::Index16][false][NCC::N][FMT::Byte] = Normal_ReadIndex<u16, s8, 1>; |
| 185 | table[VCF::Index16][false][NCC::N][FMT::UShort] = Normal_ReadIndex<u16, u16, 1>; |
| 186 | table[VCF::Index16][false][NCC::N][FMT::Short] = Normal_ReadIndex<u16, s16, 1>; |
| 187 | table[VCF::Index16][false][NCC::N][FMT::Float] = Normal_ReadIndex<u16, float, 1>; |
| 188 | table[VCF::Index16][false][NCC::N][FMT::InvalidFloat5] = Normal_ReadIndex<u16, float, 1>; |
| 189 | table[VCF::Index16][false][NCC::N][FMT::InvalidFloat6] = Normal_ReadIndex<u16, float, 1>; |
| 190 | table[VCF::Index16][false][NCC::N][FMT::InvalidFloat7] = Normal_ReadIndex<u16, float, 1>; |
| 191 | table[VCF::Index16][false][NCC::NTB][FMT::UByte] = Normal_ReadIndex<u16, u8, 3>; |
| 192 | table[VCF::Index16][false][NCC::NTB][FMT::Byte] = Normal_ReadIndex<u16, s8, 3>; |
| 193 | table[VCF::Index16][false][NCC::NTB][FMT::UShort] = Normal_ReadIndex<u16, u16, 3>; |
| 194 | table[VCF::Index16][false][NCC::NTB][FMT::Short] = Normal_ReadIndex<u16, s16, 3>; |
| 195 | table[VCF::Index16][false][NCC::NTB][FMT::Float] = Normal_ReadIndex<u16, float, 3>; |
| 196 | table[VCF::Index16][false][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadIndex<u16, float, 3>; |
| 197 | table[VCF::Index16][false][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadIndex<u16, float, 3>; |
| 198 | table[VCF::Index16][false][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadIndex<u16, float, 3>; |
| 199 | |
| 200 | // Same for NormalComponentCount::N; differs for NTB |
| 201 | table[VCF::Index16][true][NCC::N][FMT::UByte] = Normal_ReadIndex<u16, u8, 1>; |
| 202 | table[VCF::Index16][true][NCC::N][FMT::Byte] = Normal_ReadIndex<u16, s8, 1>; |
| 203 | table[VCF::Index16][true][NCC::N][FMT::UShort] = Normal_ReadIndex<u16, u16, 1>; |
| 204 | table[VCF::Index16][true][NCC::N][FMT::Short] = Normal_ReadIndex<u16, s16, 1>; |
| 205 | table[VCF::Index16][true][NCC::N][FMT::Float] = Normal_ReadIndex<u16, float, 1>; |
| 206 | table[VCF::Index16][true][NCC::N][FMT::InvalidFloat5] = Normal_ReadIndex<u16, float, 1>; |
| 207 | table[VCF::Index16][true][NCC::N][FMT::InvalidFloat6] = Normal_ReadIndex<u16, float, 1>; |
| 208 | table[VCF::Index16][true][NCC::N][FMT::InvalidFloat7] = Normal_ReadIndex<u16, float, 1>; |
| 209 | table[VCF::Index16][true][NCC::NTB][FMT::UByte] = Normal_ReadIndex_Indices3<u16, u8>; |
| 210 | table[VCF::Index16][true][NCC::NTB][FMT::Byte] = Normal_ReadIndex_Indices3<u16, s8>; |
| 211 | table[VCF::Index16][true][NCC::NTB][FMT::UShort] = Normal_ReadIndex_Indices3<u16, u16>; |
| 212 | table[VCF::Index16][true][NCC::NTB][FMT::Short] = Normal_ReadIndex_Indices3<u16, s16>; |
| 213 | table[VCF::Index16][true][NCC::NTB][FMT::Float] = Normal_ReadIndex_Indices3<u16, float>; |
| 214 | table[VCF::Index16][true][NCC::NTB][FMT::InvalidFloat5] = Normal_ReadIndex_Indices3<u16, float>; |
| 215 | table[VCF::Index16][true][NCC::NTB][FMT::InvalidFloat6] = Normal_ReadIndex_Indices3<u16, float>; |
| 216 | table[VCF::Index16][true][NCC::NTB][FMT::InvalidFloat7] = Normal_ReadIndex_Indices3<u16, float>; |
| 217 | |
| 218 | return table; |
| 219 | } |
| 220 | |
| 221 | constexpr Types s_table_read_normal = InitializeTable(); |
| 222 | } // Anonymous namespace |
| 223 | |
| 224 | TPipelineFunction VertexLoader_Normal::GetFunction(VertexComponentFormat type, |
| 225 | ComponentFormat format, |
| 226 | NormalComponentCount elements, bool index3) |
| 227 | { |
| 228 | return s_table_read_normal[type][index3][elements][format]; |
| 229 | } |