mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-11-14 21:37:52 -07:00
a3951dc2d7
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.
148 lines
5.7 KiB
C++
148 lines
5.7 KiB
C++
// Copyright 2019 Dolphin Emulator Project
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include "VideoBackends/D3D12/DX12VertexFormat.h"
|
|
|
|
#include "Common/Assert.h"
|
|
#include "Common/EnumMap.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
namespace DX12
|
|
{
|
|
static DXGI_FORMAT VarToDXGIFormat(ComponentFormat t, u32 components, bool integer)
|
|
{
|
|
using ComponentArray = std::array<DXGI_FORMAT, 4>;
|
|
static constexpr auto f = [](ComponentArray a) { return a; }; // Deduction helper
|
|
|
|
// NOTE: 3-component formats are not valid.
|
|
static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7>
|
|
float_type_lookup = {
|
|
f({DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8G8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
DXGI_FORMAT_R8G8B8A8_UNORM}), // UByte
|
|
f({DXGI_FORMAT_R8_SNORM, DXGI_FORMAT_R8G8_SNORM, DXGI_FORMAT_R8G8B8A8_SNORM,
|
|
DXGI_FORMAT_R8G8B8A8_SNORM}), // Byte
|
|
f({DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16G16_UNORM, DXGI_FORMAT_R16G16B16A16_UNORM,
|
|
DXGI_FORMAT_R16G16B16A16_UNORM}), // UShort
|
|
f({DXGI_FORMAT_R16_SNORM, DXGI_FORMAT_R16G16_SNORM, DXGI_FORMAT_R16G16B16A16_SNORM,
|
|
DXGI_FORMAT_R16G16B16A16_SNORM}), // Short
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Float
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
};
|
|
|
|
static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7>
|
|
integer_type_lookup = {
|
|
f({DXGI_FORMAT_R8_UINT, DXGI_FORMAT_R8G8_UINT, DXGI_FORMAT_R8G8B8A8_UINT,
|
|
DXGI_FORMAT_R8G8B8A8_UINT}), // UByte
|
|
f({DXGI_FORMAT_R8_SINT, DXGI_FORMAT_R8G8_SINT, DXGI_FORMAT_R8G8B8A8_SINT,
|
|
DXGI_FORMAT_R8G8B8A8_SINT}), // Byte
|
|
f({DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R16G16_UINT, DXGI_FORMAT_R16G16B16A16_UINT,
|
|
DXGI_FORMAT_R16G16B16A16_UINT}), // UShort
|
|
f({DXGI_FORMAT_R16_SINT, DXGI_FORMAT_R16G16_SINT, DXGI_FORMAT_R16G16B16A16_SINT,
|
|
DXGI_FORMAT_R16G16B16A16_SINT}), // Short
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Float
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
f({DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT}), // Invalid
|
|
};
|
|
|
|
ASSERT(components > 0 && components <= 4);
|
|
return integer ? integer_type_lookup[t][components - 1] : float_type_lookup[t][components - 1];
|
|
}
|
|
|
|
DXVertexFormat::DXVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
|
: NativeVertexFormat(vtx_decl)
|
|
{
|
|
MapAttributes();
|
|
}
|
|
|
|
void DXVertexFormat::GetInputLayoutDesc(D3D12_INPUT_LAYOUT_DESC* desc) const
|
|
{
|
|
desc->pInputElementDescs = m_attribute_descriptions.data();
|
|
desc->NumElements = m_num_attributes;
|
|
}
|
|
|
|
void DXVertexFormat::AddAttribute(const char* semantic_name, ShaderAttrib semantic_index, u32 slot,
|
|
DXGI_FORMAT format, u32 offset)
|
|
{
|
|
ASSERT(m_num_attributes < MAX_VERTEX_ATTRIBUTES);
|
|
|
|
auto* attr_desc = &m_attribute_descriptions[m_num_attributes];
|
|
attr_desc->SemanticName = semantic_name;
|
|
attr_desc->SemanticIndex = static_cast<u32>(semantic_index);
|
|
attr_desc->Format = format;
|
|
attr_desc->InputSlot = slot;
|
|
attr_desc->AlignedByteOffset = offset;
|
|
attr_desc->InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
|
attr_desc->InstanceDataStepRate = 0;
|
|
|
|
m_num_attributes++;
|
|
}
|
|
|
|
void DXVertexFormat::MapAttributes()
|
|
{
|
|
m_num_attributes = 0;
|
|
|
|
if (m_decl.position.enable)
|
|
{
|
|
AddAttribute(
|
|
"TEXCOORD", ShaderAttrib::Position, 0,
|
|
VarToDXGIFormat(m_decl.position.type, m_decl.position.components, m_decl.position.integer),
|
|
m_decl.position.offset);
|
|
}
|
|
|
|
for (u32 i = 0; i < 3; i++)
|
|
{
|
|
if (m_decl.normals[i].enable)
|
|
{
|
|
AddAttribute("TEXCOORD", ShaderAttrib::Normal + i, 0,
|
|
VarToDXGIFormat(m_decl.normals[i].type, m_decl.normals[i].components,
|
|
m_decl.normals[i].integer),
|
|
m_decl.normals[i].offset);
|
|
}
|
|
}
|
|
|
|
for (u32 i = 0; i < 2; i++)
|
|
{
|
|
if (m_decl.colors[i].enable)
|
|
{
|
|
AddAttribute("TEXCOORD", ShaderAttrib::Color0 + i, 0,
|
|
VarToDXGIFormat(m_decl.colors[i].type, m_decl.colors[i].components,
|
|
m_decl.colors[i].integer),
|
|
m_decl.colors[i].offset);
|
|
}
|
|
}
|
|
|
|
for (u32 i = 0; i < 8; i++)
|
|
{
|
|
if (m_decl.texcoords[i].enable)
|
|
{
|
|
AddAttribute("TEXCOORD", ShaderAttrib::TexCoord0 + i, 0,
|
|
VarToDXGIFormat(m_decl.texcoords[i].type, m_decl.texcoords[i].components,
|
|
m_decl.texcoords[i].integer),
|
|
m_decl.texcoords[i].offset);
|
|
}
|
|
}
|
|
|
|
if (m_decl.posmtx.enable)
|
|
{
|
|
AddAttribute(
|
|
"TEXCOORD", ShaderAttrib::PositionMatrix, 0,
|
|
VarToDXGIFormat(m_decl.posmtx.type, m_decl.posmtx.components, m_decl.posmtx.integer),
|
|
m_decl.posmtx.offset);
|
|
}
|
|
}
|
|
|
|
} // namespace DX12
|