2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
#include "D3DBase.h"
|
2011-06-11 19:37:21 +00:00
|
|
|
#include "D3DBlob.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
#include "NativeVertexFormat.h"
|
2010-11-26 09:25:08 +00:00
|
|
|
#include "VertexManager.h"
|
2011-01-24 11:57:17 +00:00
|
|
|
#include "VertexShaderCache.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
class D3DVertexFormat : public NativeVertexFormat
|
|
|
|
{
|
|
|
|
D3D11_INPUT_ELEMENT_DESC m_elems[32];
|
|
|
|
UINT m_num_elems;
|
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
DX11::D3DBlob* m_vs_bytecode;
|
|
|
|
ID3D11InputLayout* m_layout;
|
2011-01-24 11:57:17 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
public:
|
2011-06-11 19:37:21 +00:00
|
|
|
D3DVertexFormat() : m_num_elems(0), m_vs_bytecode(NULL), m_layout(NULL) {}
|
|
|
|
~D3DVertexFormat() { SAFE_RELEASE(m_vs_bytecode); SAFE_RELEASE(m_layout); }
|
2011-01-24 11:57:17 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
void Initialize(const PortableVertexDeclaration &_vtx_decl);
|
2011-01-24 11:57:17 +00:00
|
|
|
void SetupVertexPointers();
|
2010-06-13 19:50:06 +00:00
|
|
|
};
|
|
|
|
|
2010-11-26 09:25:08 +00:00
|
|
|
NativeVertexFormat* VertexManager::CreateNativeVertexFormat()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
return new D3DVertexFormat();
|
|
|
|
}
|
|
|
|
|
2014-01-24 13:46:05 +00:00
|
|
|
static const DXGI_FORMAT d3d_format_lookup[5*4*2] =
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2014-01-24 13:46:05 +00:00
|
|
|
// float formats
|
|
|
|
DXGI_FORMAT_R8_UNORM, DXGI_FORMAT_R8_SNORM, DXGI_FORMAT_R16_UNORM, DXGI_FORMAT_R16_SNORM, DXGI_FORMAT_R32_FLOAT,
|
|
|
|
DXGI_FORMAT_R8G8_UNORM, DXGI_FORMAT_R8G8_SNORM, DXGI_FORMAT_R16G16_UNORM, DXGI_FORMAT_R16G16_SNORM, DXGI_FORMAT_R32G32_FLOAT,
|
|
|
|
DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_R32G32B32_FLOAT,
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_SNORM, DXGI_FORMAT_R16G16B16A16_UNORM, DXGI_FORMAT_R16G16B16A16_SNORM, DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-01-24 13:46:05 +00:00
|
|
|
// integer formats
|
|
|
|
DXGI_FORMAT_R8_UINT, DXGI_FORMAT_R8_SINT, DXGI_FORMAT_R16_UINT, DXGI_FORMAT_R16_SINT, DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_R8G8_UINT, DXGI_FORMAT_R8G8_SINT, DXGI_FORMAT_R16G16_UINT, DXGI_FORMAT_R16G16_SINT, DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_UNKNOWN,
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UINT, DXGI_FORMAT_R8G8B8A8_SINT, DXGI_FORMAT_R16G16B16A16_UINT, DXGI_FORMAT_R16G16B16A16_SINT, DXGI_FORMAT_UNKNOWN,
|
|
|
|
};
|
|
|
|
|
|
|
|
DXGI_FORMAT VarToD3D(VarType t, int size, bool integer)
|
|
|
|
{
|
|
|
|
DXGI_FORMAT retval = d3d_format_lookup[(int)t + 5*(size-1) + 5*4*(int)integer];
|
2010-06-13 19:50:06 +00:00
|
|
|
if (retval == DXGI_FORMAT_UNKNOWN)
|
|
|
|
{
|
2014-01-24 13:46:05 +00:00
|
|
|
PanicAlert("VarToD3D: Invalid type/size combo %i , %i, %i", (int)t, size, (int)integer);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl)
|
|
|
|
{
|
|
|
|
vertex_stride = _vtx_decl.stride;
|
|
|
|
memset(m_elems, 0, sizeof(m_elems));
|
2014-01-24 13:46:05 +00:00
|
|
|
const AttributeFormat* format = &_vtx_decl.position;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2014-01-24 13:46:05 +00:00
|
|
|
if (format->enable)
|
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "POSITION";
|
|
|
|
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;
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2014-01-24 14:16:52 +00:00
|
|
|
format = &_vtx_decl.normals[i];
|
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "NORMAL";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2014-01-24 14:23:50 +00:00
|
|
|
format = &_vtx_decl.colors[i];
|
|
|
|
if (format->enable)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "COLOR";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (_vtx_decl.texcoord_offset[i] > 0)
|
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "TEXCOORD";
|
|
|
|
m_elems[m_num_elems].SemanticIndex = i;
|
|
|
|
m_elems[m_num_elems].AlignedByteOffset = _vtx_decl.texcoord_offset[i];
|
2014-01-24 13:46:05 +00:00
|
|
|
m_elems[m_num_elems].Format = VarToD3D(_vtx_decl.texcoord_gl_type[i], _vtx_decl.texcoord_size[i], false);
|
2010-06-13 19:50:06 +00:00
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_vtx_decl.posmtx_offset != -1)
|
|
|
|
{
|
|
|
|
m_elems[m_num_elems].SemanticName = "BLENDINDICES";
|
|
|
|
m_elems[m_num_elems].AlignedByteOffset = _vtx_decl.posmtx_offset;
|
|
|
|
m_elems[m_num_elems].Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
m_elems[m_num_elems].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
|
|
|
++m_num_elems;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-24 11:57:17 +00:00
|
|
|
void D3DVertexFormat::SetupVertexPointers()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2011-01-29 20:16:51 +00:00
|
|
|
if (m_vs_bytecode != DX11::VertexShaderCache::GetActiveShaderBytecode())
|
2011-01-24 11:57:17 +00:00
|
|
|
{
|
2011-06-11 19:37:21 +00:00
|
|
|
SAFE_RELEASE(m_vs_bytecode);
|
|
|
|
SAFE_RELEASE(m_layout);
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
m_vs_bytecode = DX11::VertexShaderCache::GetActiveShaderBytecode();
|
2011-06-11 19:37:21 +00:00
|
|
|
m_vs_bytecode->AddRef();
|
2011-01-24 11:57:17 +00:00
|
|
|
|
2011-06-11 19:37:21 +00:00
|
|
|
HRESULT hr = DX11::D3D::device->CreateInputLayout(m_elems, m_num_elems, m_vs_bytecode->Data(), m_vs_bytecode->Size(), &m_layout);
|
|
|
|
if (FAILED(hr)) PanicAlert("Failed to create input layout, %s %d\n", __FILE__, __LINE__);
|
|
|
|
DX11::D3D::SetDebugObjectName((ID3D11DeviceChild*)m_layout, "input layout used to emulate the GX pipeline");
|
2011-01-24 11:57:17 +00:00
|
|
|
}
|
2011-06-11 19:37:21 +00:00
|
|
|
DX11::D3D::context->IASetInputLayout(m_layout);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
2011-01-31 01:28:32 +00:00
|
|
|
|
|
|
|
} // namespace DX11
|