2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2016-02-16 04:11:43 +00:00
|
|
|
#include <array>
|
|
|
|
|
2021-11-10 23:55:33 +00:00
|
|
|
#include "Common/Assert.h"
|
2021-06-26 19:48:28 +00:00
|
|
|
#include "Common/EnumMap.h"
|
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2020-09-15 12:50:34 +00:00
|
|
|
#include "VideoBackends/D3D/D3DRender.h"
|
2014-10-29 00:19:09 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2020-09-15 12:50:34 +00:00
|
|
|
#include "VideoBackends/D3D/D3DVertexManager.h"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/D3D/DXShader.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
2018-02-24 14:23:24 +00:00
|
|
|
std::mutex s_input_layout_lock;
|
|
|
|
|
2017-02-18 08:14:30 +00:00
|
|
|
std::unique_ptr<NativeVertexFormat>
|
2019-02-15 01:59:50 +00:00
|
|
|
Renderer::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2017-02-18 08:14:30 +00:00
|
|
|
return std::make_unique<D3DVertexFormat>(vtx_decl);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-26 19:48:28 +00:00
|
|
|
DXGI_FORMAT VarToD3D(ComponentFormat t, int size, bool integer)
|
|
|
|
{
|
|
|
|
using FormatMap = Common::EnumMap<DXGI_FORMAT, ComponentFormat::Float>;
|
|
|
|
static constexpr auto f = [](FormatMap a) { return a; }; // Deduction helper
|
|
|
|
|
|
|
|
static constexpr std::array<FormatMap, 4> d3d_float_format_lookup = {
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8_UNORM,
|
|
|
|
DXGI_FORMAT_R8_SNORM,
|
|
|
|
DXGI_FORMAT_R16_UNORM,
|
|
|
|
DXGI_FORMAT_R16_SNORM,
|
|
|
|
DXGI_FORMAT_R32_FLOAT,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8G8_UNORM,
|
|
|
|
DXGI_FORMAT_R8G8_SNORM,
|
|
|
|
DXGI_FORMAT_R16G16_UNORM,
|
|
|
|
DXGI_FORMAT_R16G16_SNORM,
|
|
|
|
DXGI_FORMAT_R32G32_FLOAT,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_R32G32B32_FLOAT,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
|
|
DXGI_FORMAT_R8G8B8A8_SNORM,
|
|
|
|
DXGI_FORMAT_R16G16B16A16_UNORM,
|
|
|
|
DXGI_FORMAT_R16G16B16A16_SNORM,
|
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
|
|
|
}),
|
|
|
|
};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-06-26 19:48:28 +00:00
|
|
|
static constexpr std::array<FormatMap, 4> d3d_integer_format_lookup = {
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8_UINT,
|
|
|
|
DXGI_FORMAT_R8_SINT,
|
|
|
|
DXGI_FORMAT_R16_UINT,
|
|
|
|
DXGI_FORMAT_R16_SINT,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8G8_UINT,
|
|
|
|
DXGI_FORMAT_R8G8_SINT,
|
|
|
|
DXGI_FORMAT_R16G16_UINT,
|
|
|
|
DXGI_FORMAT_R16G16_SINT,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
}),
|
|
|
|
f({
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UINT,
|
|
|
|
DXGI_FORMAT_R8G8B8A8_SINT,
|
|
|
|
DXGI_FORMAT_R16G16B16A16_UINT,
|
|
|
|
DXGI_FORMAT_R16G16B16A16_SINT,
|
|
|
|
DXGI_FORMAT_UNKNOWN,
|
|
|
|
}),
|
|
|
|
};
|
2014-01-24 13:46:05 +00:00
|
|
|
|
2021-06-26 19:48:28 +00:00
|
|
|
DXGI_FORMAT retval =
|
|
|
|
integer ? d3d_integer_format_lookup[size - 1][t] : d3d_float_format_lookup[size - 1][t];
|
2010-06-13 19:50:06 +00:00
|
|
|
if (retval == DXGI_FORMAT_UNKNOWN)
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("VarToD3D: Invalid type/size combo {}, {}, {}", t, size, integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
D3DVertexFormat::D3DVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
|
|
|
: NativeVertexFormat(vtx_decl)
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
|
|
|
const AttributeFormat* format = &vtx_decl.position;
|
2014-01-24 13:46:05 +00:00
|
|
|
if (format->enable)
|
|
|
|
{
|
2022-05-04 05:41:34 +00:00
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = SHADER_POSITION_ATTRIB;
|
2014-01-24 13:46:05 +00:00
|
|
|
m_elems[m_num_elems].AlignedByteOffset = format->offset;
|
|
|
|
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
|
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
format = &vtx_decl.normals[i];
|
2014-01-24 14:16:52 +00:00
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2022-05-04 05:41:34 +00:00
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = SHADER_NORMAL_ATTRIB + i;
|
2014-01-24 14:16:52 +00:00
|
|
|
m_elems[m_num_elems].AlignedByteOffset = format->offset;
|
|
|
|
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
format = &vtx_decl.colors[i];
|
2014-01-24 14:23:50 +00:00
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2022-05-04 05:41:34 +00:00
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = SHADER_COLOR0_ATTRIB + i;
|
2014-01-24 14:23:50 +00:00
|
|
|
m_elems[m_num_elems].AlignedByteOffset = format->offset;
|
|
|
|
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
format = &vtx_decl.texcoords[i];
|
2014-01-24 14:32:27 +00:00
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
2022-05-04 05:41:34 +00:00
|
|
|
m_elems[m_num_elems].SemanticIndex = SHADER_TEXTURE0_ATTRIB + i;
|
2014-01-24 14:32:27 +00:00
|
|
|
m_elems[m_num_elems].AlignedByteOffset = format->offset;
|
|
|
|
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
format = &vtx_decl.posmtx;
|
2014-01-24 14:41:08 +00:00
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2022-05-04 05:41:34 +00:00
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = SHADER_POSMTX_ATTRIB;
|
2014-01-24 14:41:08 +00:00
|
|
|
m_elems[m_num_elems].AlignedByteOffset = format->offset;
|
|
|
|
m_elems[m_num_elems].Format = VarToD3D(format->type, format->components, format->integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:31 +00:00
|
|
|
D3DVertexFormat::~D3DVertexFormat()
|
|
|
|
{
|
2018-02-24 14:23:24 +00:00
|
|
|
ID3D11InputLayout* layout = m_layout.load();
|
2019-03-09 13:31:37 +00:00
|
|
|
if (layout)
|
|
|
|
layout->Release();
|
2017-07-20 05:25:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
ID3D11InputLayout* D3DVertexFormat::GetInputLayout(const void* vs_bytecode, size_t vs_bytecode_size)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2018-02-24 14:23:24 +00:00
|
|
|
// CreateInputLayout requires a shader input, but it only looks at the signature of the shader,
|
|
|
|
// so we don't need to recompute it if the shader changes.
|
|
|
|
ID3D11InputLayout* layout = m_layout.load();
|
|
|
|
if (layout)
|
|
|
|
return layout;
|
2017-09-08 09:42:56 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
HRESULT hr = D3D::device->CreateInputLayout(m_elems.data(), m_num_elems, vs_bytecode,
|
|
|
|
vs_bytecode_size, &layout);
|
2021-12-12 20:50:13 +00:00
|
|
|
ASSERT_MSG(VIDEO, SUCCEEDED(hr), "Failed to create input layout: {}", DX11HRWrap(hr));
|
2018-02-24 14:23:24 +00:00
|
|
|
|
|
|
|
// This method can be called from multiple threads, so ensure that only one thread sets the
|
|
|
|
// cached input layout pointer. If another thread beats this thread, use the existing layout.
|
|
|
|
ID3D11InputLayout* expected = nullptr;
|
|
|
|
if (!m_layout.compare_exchange_strong(expected, layout))
|
|
|
|
{
|
2019-03-09 13:31:37 +00:00
|
|
|
if (layout)
|
|
|
|
layout->Release();
|
|
|
|
|
2018-02-24 14:23:24 +00:00
|
|
|
layout = expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layout;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2011-01-31 01:28:32 +00:00
|
|
|
|
|
|
|
} // namespace DX11
|