dolphin/Source/Core/VideoCommon/VertexLoader_TextCoord.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.4 KiB
C
Raw Permalink Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "Common/CommonTypes.h"
2022-09-14 21:58:09 +00:00
#include "Common/EnumMap.h"
#include "Common/Inline.h"
2022-09-14 21:58:09 +00:00
#include "VideoCommon/CPMemory.h"
#include "VideoCommon/VertexLoader.h"
class VertexLoader_TextCoord
{
public:
2022-09-14 21:58:09 +00:00
static DOLPHIN_FORCE_INLINE u32 GetSize(VertexComponentFormat type, ComponentFormat format,
TexComponentCount elements)
{
return s_table_size[type][format][elements];
}
static TPipelineFunction GetFunction(VertexComponentFormat type, ComponentFormat format,
TexComponentCount elements);
// It is important to synchronize tcIndex, or else the wrong texture coordinate array will be used
static TPipelineFunction GetDummyFunction();
2022-09-14 21:58:09 +00:00
private:
template <typename T, auto last_member>
using EnumMap = typename Common::EnumMap<T, last_member>;
Fix out of bounds accesses for invalid vertex component formats On all platforms, this would result in out of bounds accesses when getting the component sizes (which uses stuff from VertexLoader_Position.h/VertexLoader_TextCoord.h/VertexLoader_Normal.h). On platforms other than x64 and ARM64, this would also be out of bounds accesses when getting function pointers for the non-JIT vertex loader (in VertexLoader_Position.cpp etc.). Usually both of these would get data from other entries in the same multi-dimensional array, but the last few entries would be truly out of bounds. This does mean that an out of bounds function pointer can be called on platforms that don't have a JIT vertex loader, but it is limited to invalid component formats with values 5/6/7 due to the size of the bitfield the formats come from, so it seems unlikely that this could be exploited in practice. This issue affects a few games; Def Jam: Fight for New York (https://bugs.dolphin-emu.org/issues/12719) and Fifa Street are known to be affected. I have not done any hardware testing for this PR specifically, though I *think* I previously determined that at least a value of 5 behaves the same as float (4). That's what I implemented in any case. I did previously determine that both Def Jam: Fight for New York and Fifa Street use an invalid normal format, but don't actually have lighting enabled when that normal vector is used, so it doesn't change rendering in practice. The color component format also has two invalid values, but VertexLoader_Color.h/.cpp do check for those invalid ones and return a default value instead of doing an out of bounds access.
2024-04-02 04:37:11 +00:00
using SizeTable =
EnumMap<EnumMap<EnumMap<u32, TexComponentCount::ST>, ComponentFormat::InvalidFloat7>,
VertexComponentFormat::Index16>;
2022-09-14 21:58:09 +00:00
static constexpr SizeTable s_table_size = []() consteval
{
SizeTable table{};
using VCF = VertexComponentFormat;
using FMT = ComponentFormat;
table[VCF::Direct][FMT::UByte] = {1, 2};
table[VCF::Direct][FMT::Byte] = {1, 2};
table[VCF::Direct][FMT::UShort] = {2, 4};
table[VCF::Direct][FMT::Short] = {2, 4};
table[VCF::Direct][FMT::Float] = {4, 8};
Fix out of bounds accesses for invalid vertex component formats On all platforms, this would result in out of bounds accesses when getting the component sizes (which uses stuff from VertexLoader_Position.h/VertexLoader_TextCoord.h/VertexLoader_Normal.h). On platforms other than x64 and ARM64, this would also be out of bounds accesses when getting function pointers for the non-JIT vertex loader (in VertexLoader_Position.cpp etc.). Usually both of these would get data from other entries in the same multi-dimensional array, but the last few entries would be truly out of bounds. This does mean that an out of bounds function pointer can be called on platforms that don't have a JIT vertex loader, but it is limited to invalid component formats with values 5/6/7 due to the size of the bitfield the formats come from, so it seems unlikely that this could be exploited in practice. This issue affects a few games; Def Jam: Fight for New York (https://bugs.dolphin-emu.org/issues/12719) and Fifa Street are known to be affected. I have not done any hardware testing for this PR specifically, though I *think* I previously determined that at least a value of 5 behaves the same as float (4). That's what I implemented in any case. I did previously determine that both Def Jam: Fight for New York and Fifa Street use an invalid normal format, but don't actually have lighting enabled when that normal vector is used, so it doesn't change rendering in practice. The color component format also has two invalid values, but VertexLoader_Color.h/.cpp do check for those invalid ones and return a default value instead of doing an out of bounds access.
2024-04-02 04:37:11 +00:00
table[VCF::Direct][FMT::InvalidFloat5] = {4, 8};
table[VCF::Direct][FMT::InvalidFloat6] = {4, 8};
table[VCF::Direct][FMT::InvalidFloat7] = {4, 8};
2022-09-14 21:58:09 +00:00
table[VCF::Index8][FMT::UByte] = {1, 1};
table[VCF::Index8][FMT::Byte] = {1, 1};
table[VCF::Index8][FMT::UShort] = {1, 1};
table[VCF::Index8][FMT::Short] = {1, 1};
table[VCF::Index8][FMT::Float] = {1, 1};
Fix out of bounds accesses for invalid vertex component formats On all platforms, this would result in out of bounds accesses when getting the component sizes (which uses stuff from VertexLoader_Position.h/VertexLoader_TextCoord.h/VertexLoader_Normal.h). On platforms other than x64 and ARM64, this would also be out of bounds accesses when getting function pointers for the non-JIT vertex loader (in VertexLoader_Position.cpp etc.). Usually both of these would get data from other entries in the same multi-dimensional array, but the last few entries would be truly out of bounds. This does mean that an out of bounds function pointer can be called on platforms that don't have a JIT vertex loader, but it is limited to invalid component formats with values 5/6/7 due to the size of the bitfield the formats come from, so it seems unlikely that this could be exploited in practice. This issue affects a few games; Def Jam: Fight for New York (https://bugs.dolphin-emu.org/issues/12719) and Fifa Street are known to be affected. I have not done any hardware testing for this PR specifically, though I *think* I previously determined that at least a value of 5 behaves the same as float (4). That's what I implemented in any case. I did previously determine that both Def Jam: Fight for New York and Fifa Street use an invalid normal format, but don't actually have lighting enabled when that normal vector is used, so it doesn't change rendering in practice. The color component format also has two invalid values, but VertexLoader_Color.h/.cpp do check for those invalid ones and return a default value instead of doing an out of bounds access.
2024-04-02 04:37:11 +00:00
table[VCF::Index8][FMT::InvalidFloat5] = {1, 1};
table[VCF::Index8][FMT::InvalidFloat6] = {1, 1};
table[VCF::Index8][FMT::InvalidFloat7] = {1, 1};
2022-09-14 21:58:09 +00:00
table[VCF::Index16][FMT::UByte] = {2, 2};
table[VCF::Index16][FMT::Byte] = {2, 2};
table[VCF::Index16][FMT::UShort] = {2, 2};
table[VCF::Index16][FMT::Short] = {2, 2};
table[VCF::Index16][FMT::Float] = {2, 2};
Fix out of bounds accesses for invalid vertex component formats On all platforms, this would result in out of bounds accesses when getting the component sizes (which uses stuff from VertexLoader_Position.h/VertexLoader_TextCoord.h/VertexLoader_Normal.h). On platforms other than x64 and ARM64, this would also be out of bounds accesses when getting function pointers for the non-JIT vertex loader (in VertexLoader_Position.cpp etc.). Usually both of these would get data from other entries in the same multi-dimensional array, but the last few entries would be truly out of bounds. This does mean that an out of bounds function pointer can be called on platforms that don't have a JIT vertex loader, but it is limited to invalid component formats with values 5/6/7 due to the size of the bitfield the formats come from, so it seems unlikely that this could be exploited in practice. This issue affects a few games; Def Jam: Fight for New York (https://bugs.dolphin-emu.org/issues/12719) and Fifa Street are known to be affected. I have not done any hardware testing for this PR specifically, though I *think* I previously determined that at least a value of 5 behaves the same as float (4). That's what I implemented in any case. I did previously determine that both Def Jam: Fight for New York and Fifa Street use an invalid normal format, but don't actually have lighting enabled when that normal vector is used, so it doesn't change rendering in practice. The color component format also has two invalid values, but VertexLoader_Color.h/.cpp do check for those invalid ones and return a default value instead of doing an out of bounds access.
2024-04-02 04:37:11 +00:00
table[VCF::Index16][FMT::InvalidFloat5] = {2, 2};
table[VCF::Index16][FMT::InvalidFloat6] = {2, 2};
table[VCF::Index16][FMT::InvalidFloat7] = {2, 2};
2022-09-14 21:58:09 +00:00
return table;
}
();
};