// Copyright 2008 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include "VideoCommon/VertexLoader_Normal.h" #include #include #include "Common/CommonTypes.h" #include "Common/EnumMap.h" #include "VideoCommon/DataReader.h" #include "VideoCommon/VertexLoader.h" #include "VideoCommon/VertexLoaderManager.h" #include "VideoCommon/VertexLoaderUtils.h" // warning: mapping buffer should be disabled to use this #define LOG_NORM() // PRIM_LOG("norm: {} {} {}, ", ((float*)g_vertex_manager_write_ptr)[-3], // ((float*)g_vertex_manager_write_ptr)[-2], // ((float*)g_vertex_manager_write_ptr)[-1]); namespace { template constexpr float FracAdjust(T val) { // auto const S8FRAC = 1.f / (1u << 6); // auto const U8FRAC = 1.f / (1u << 7); // auto const S16FRAC = 1.f / (1u << 14); // auto const U16FRAC = 1.f / (1u << 15); // TODO: is this right? return val / float(1u << (sizeof(T) * 8 - std::is_signed_v - 1)); } template <> constexpr float FracAdjust(float val) { return val; } template void ReadIndirect(VertexLoader* loader, const T* data) { static_assert(3 == N || 9 == N, "N is only sane as 3 or 9!"); DataReader dst(g_vertex_manager_write_ptr, nullptr); for (u32 i = 0; i < N; ++i) { const float value = FracAdjust(Common::FromBigEndian(data[i])); if (loader->m_remaining == 0) { if (i >= 3 && i < 6) VertexLoaderManager::tangent_cache[i - 3] = value; else if (i >= 6 && i < 9) VertexLoaderManager::binormal_cache[i - 6] = value; } dst.Write(value); } g_vertex_manager_write_ptr = dst.GetPointer(); LOG_NORM(); } template struct Normal_Direct { static void function(VertexLoader* loader) { const auto source = reinterpret_cast(DataGetPosition()); ReadIndirect(loader, source); DataSkip(); } static constexpr u32 size = sizeof(T) * N * 3; }; template void Normal_Index_Offset(VertexLoader* loader) { static_assert(std::is_unsigned_v, "Only unsigned I is sane!"); const auto index = DataRead(); const auto data = reinterpret_cast( VertexLoaderManager::cached_arraybases[CPArray::Normal] + (index * g_main_cp_state.array_strides[CPArray::Normal]) + sizeof(T) * 3 * Offset); ReadIndirect(loader, data); } template struct Normal_Index { static void function(VertexLoader* loader) { Normal_Index_Offset(loader); } static constexpr u32 size = sizeof(I); }; template struct Normal_Index_Indices3 { static void function(VertexLoader* loader) { Normal_Index_Offset(loader); Normal_Index_Offset(loader); Normal_Index_Offset(loader); } static constexpr u32 size = sizeof(I) * 3; }; struct Set { template constexpr Set& operator=(const T&) { gc_size = T::size; function = T::function; return *this; } u32 gc_size; TPipelineFunction function; }; using Common::EnumMap; using Formats = EnumMap; using Elements = EnumMap; using Indices = std::array; using Types = EnumMap; constexpr Types InitializeTable() { Types table{}; using VCF = VertexComponentFormat; using NCC = NormalComponentCount; using FMT = ComponentFormat; table[VCF::Direct][false][NCC::N][FMT::UByte] = Normal_Direct(); table[VCF::Direct][false][NCC::N][FMT::Byte] = Normal_Direct(); table[VCF::Direct][false][NCC::N][FMT::UShort] = Normal_Direct(); table[VCF::Direct][false][NCC::N][FMT::Short] = Normal_Direct(); table[VCF::Direct][false][NCC::N][FMT::Float] = Normal_Direct(); table[VCF::Direct][false][NCC::NTB][FMT::UByte] = Normal_Direct(); table[VCF::Direct][false][NCC::NTB][FMT::Byte] = Normal_Direct(); table[VCF::Direct][false][NCC::NTB][FMT::UShort] = Normal_Direct(); table[VCF::Direct][false][NCC::NTB][FMT::Short] = Normal_Direct(); table[VCF::Direct][false][NCC::NTB][FMT::Float] = Normal_Direct(); // Same as above, since there are no indices table[VCF::Direct][true][NCC::N][FMT::UByte] = Normal_Direct(); table[VCF::Direct][true][NCC::N][FMT::Byte] = Normal_Direct(); table[VCF::Direct][true][NCC::N][FMT::UShort] = Normal_Direct(); table[VCF::Direct][true][NCC::N][FMT::Short] = Normal_Direct(); table[VCF::Direct][true][NCC::N][FMT::Float] = Normal_Direct(); table[VCF::Direct][true][NCC::NTB][FMT::UByte] = Normal_Direct(); table[VCF::Direct][true][NCC::NTB][FMT::Byte] = Normal_Direct(); table[VCF::Direct][true][NCC::NTB][FMT::UShort] = Normal_Direct(); table[VCF::Direct][true][NCC::NTB][FMT::Short] = Normal_Direct(); table[VCF::Direct][true][NCC::NTB][FMT::Float] = Normal_Direct(); table[VCF::Index8][false][NCC::N][FMT::UByte] = Normal_Index(); table[VCF::Index8][false][NCC::N][FMT::Byte] = Normal_Index(); table[VCF::Index8][false][NCC::N][FMT::UShort] = Normal_Index(); table[VCF::Index8][false][NCC::N][FMT::Short] = Normal_Index(); table[VCF::Index8][false][NCC::N][FMT::Float] = Normal_Index(); table[VCF::Index8][false][NCC::NTB][FMT::UByte] = Normal_Index(); table[VCF::Index8][false][NCC::NTB][FMT::Byte] = Normal_Index(); table[VCF::Index8][false][NCC::NTB][FMT::UShort] = Normal_Index(); table[VCF::Index8][false][NCC::NTB][FMT::Short] = Normal_Index(); table[VCF::Index8][false][NCC::NTB][FMT::Float] = Normal_Index(); // Same for NormalComponentCount::N; differs for NTB table[VCF::Index8][true][NCC::N][FMT::UByte] = Normal_Index(); table[VCF::Index8][true][NCC::N][FMT::Byte] = Normal_Index(); table[VCF::Index8][true][NCC::N][FMT::UShort] = Normal_Index(); table[VCF::Index8][true][NCC::N][FMT::Short] = Normal_Index(); table[VCF::Index8][true][NCC::N][FMT::Float] = Normal_Index(); table[VCF::Index8][true][NCC::NTB][FMT::UByte] = Normal_Index_Indices3(); table[VCF::Index8][true][NCC::NTB][FMT::Byte] = Normal_Index_Indices3(); table[VCF::Index8][true][NCC::NTB][FMT::UShort] = Normal_Index_Indices3(); table[VCF::Index8][true][NCC::NTB][FMT::Short] = Normal_Index_Indices3(); table[VCF::Index8][true][NCC::NTB][FMT::Float] = Normal_Index_Indices3(); table[VCF::Index16][false][NCC::N][FMT::UByte] = Normal_Index(); table[VCF::Index16][false][NCC::N][FMT::Byte] = Normal_Index(); table[VCF::Index16][false][NCC::N][FMT::UShort] = Normal_Index(); table[VCF::Index16][false][NCC::N][FMT::Short] = Normal_Index(); table[VCF::Index16][false][NCC::N][FMT::Float] = Normal_Index(); table[VCF::Index16][false][NCC::NTB][FMT::UByte] = Normal_Index(); table[VCF::Index16][false][NCC::NTB][FMT::Byte] = Normal_Index(); table[VCF::Index16][false][NCC::NTB][FMT::UShort] = Normal_Index(); table[VCF::Index16][false][NCC::NTB][FMT::Short] = Normal_Index(); table[VCF::Index16][false][NCC::NTB][FMT::Float] = Normal_Index(); // Same for NormalComponentCount::N; differs for NTB table[VCF::Index16][true][NCC::N][FMT::UByte] = Normal_Index(); table[VCF::Index16][true][NCC::N][FMT::Byte] = Normal_Index(); table[VCF::Index16][true][NCC::N][FMT::UShort] = Normal_Index(); table[VCF::Index16][true][NCC::N][FMT::Short] = Normal_Index(); table[VCF::Index16][true][NCC::N][FMT::Float] = Normal_Index(); table[VCF::Index16][true][NCC::NTB][FMT::UByte] = Normal_Index_Indices3(); table[VCF::Index16][true][NCC::NTB][FMT::Byte] = Normal_Index_Indices3(); table[VCF::Index16][true][NCC::NTB][FMT::UShort] = Normal_Index_Indices3(); table[VCF::Index16][true][NCC::NTB][FMT::Short] = Normal_Index_Indices3(); table[VCF::Index16][true][NCC::NTB][FMT::Float] = Normal_Index_Indices3(); return table; } constexpr Types s_table = InitializeTable(); } // Anonymous namespace u32 VertexLoader_Normal::GetSize(VertexComponentFormat type, ComponentFormat format, NormalComponentCount elements, bool index3) { return s_table[type][index3][elements][format].gc_size; } TPipelineFunction VertexLoader_Normal::GetFunction(VertexComponentFormat type, ComponentFormat format, NormalComponentCount elements, bool index3) { return s_table[type][index3][elements][format].function; }