2009-02-28 22:10:38 +00:00
|
|
|
|
2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "D3DBase.h"
|
|
|
|
|
|
|
|
#include "x64Emitter.h"
|
2013-02-26 19:49:00 +00:00
|
|
|
#include "x64ABI.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
#include "MemoryUtil.h"
|
2009-02-28 22:10:38 +00:00
|
|
|
#include "VertexShaderGen.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
#include "CPMemory.h"
|
|
|
|
#include "NativeVertexFormat.h"
|
2010-11-26 09:25:08 +00:00
|
|
|
#include "VertexManager.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
namespace DX9
|
|
|
|
{
|
|
|
|
|
2009-02-23 06:15:48 +00:00
|
|
|
class D3DVertexFormat : public NativeVertexFormat
|
|
|
|
{
|
|
|
|
LPDIRECT3DVERTEXDECLARATION9 d3d_decl;
|
|
|
|
|
|
|
|
public:
|
2010-11-26 09:25:08 +00:00
|
|
|
D3DVertexFormat() : d3d_decl(NULL) {}
|
2009-02-23 06:15:48 +00:00
|
|
|
~D3DVertexFormat();
|
|
|
|
virtual void Initialize(const PortableVertexDeclaration &_vtx_decl);
|
2011-01-24 11:57:17 +00:00
|
|
|
virtual void SetupVertexPointers();
|
2010-11-29 16:16:48 +00:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
D3DVERTEXELEMENT9 elements[32];
|
|
|
|
int num_elements;
|
|
|
|
#endif
|
2009-02-23 06:15:48 +00:00
|
|
|
};
|
|
|
|
|
2010-11-26 09:25:08 +00:00
|
|
|
NativeVertexFormat* VertexManager::CreateNativeVertexFormat()
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
return new D3DVertexFormat();
|
|
|
|
}
|
|
|
|
|
2010-11-29 16:16:48 +00:00
|
|
|
void DX9::VertexManager::GetElements(NativeVertexFormat* format, D3DVERTEXELEMENT9** elems, int* num)
|
|
|
|
{
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
*elems = ((D3DVertexFormat*)format)->elements;
|
|
|
|
*num = ((D3DVertexFormat*)format)->num_elements;
|
|
|
|
#else
|
|
|
|
*elems = NULL;
|
|
|
|
*num = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-23 06:15:48 +00:00
|
|
|
D3DVertexFormat::~D3DVertexFormat()
|
|
|
|
{
|
|
|
|
if (d3d_decl)
|
|
|
|
{
|
|
|
|
d3d_decl->Release();
|
|
|
|
d3d_decl = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-10 22:51:17 +00:00
|
|
|
D3DDECLTYPE VarToD3D(VarType t, int size)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2010-01-11 23:47:32 +00:00
|
|
|
if (t < 0 || t > 4) {
|
|
|
|
PanicAlert("VarToD3D: Invalid VarType %i", t);
|
|
|
|
}
|
2009-03-10 22:51:17 +00:00
|
|
|
static const D3DDECLTYPE lookup1[5] = {
|
|
|
|
D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_FLOAT1,
|
|
|
|
};
|
|
|
|
static const D3DDECLTYPE lookup2[5] = {
|
|
|
|
D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_SHORT2N, D3DDECLTYPE_USHORT2N, D3DDECLTYPE_FLOAT2,
|
|
|
|
};
|
|
|
|
static const D3DDECLTYPE lookup3[5] = {
|
|
|
|
D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_UNUSED, D3DDECLTYPE_FLOAT3,
|
2009-02-23 06:15:48 +00:00
|
|
|
};
|
2009-09-13 21:18:04 +00:00
|
|
|
// Sadly, D3D9 has no SBYTE4N. D3D10 does, though.
|
2009-03-10 22:51:17 +00:00
|
|
|
static const D3DDECLTYPE lookup4[5] = {
|
2009-09-13 21:18:04 +00:00
|
|
|
D3DDECLTYPE_UNUSED, D3DDECLTYPE_UBYTE4N, D3DDECLTYPE_SHORT4N, D3DDECLTYPE_USHORT4N, D3DDECLTYPE_FLOAT4,
|
2009-03-10 22:51:17 +00:00
|
|
|
};
|
|
|
|
D3DDECLTYPE retval = D3DDECLTYPE_UNUSED;
|
2010-09-28 02:15:02 +00:00
|
|
|
switch (size)
|
|
|
|
{
|
2009-03-10 22:51:17 +00:00
|
|
|
case 1: retval = lookup1[t]; break;
|
|
|
|
case 2: retval = lookup2[t]; break;
|
|
|
|
case 3: retval = lookup3[t]; break;
|
|
|
|
case 4: retval = lookup4[t]; break;
|
2010-09-28 02:15:02 +00:00
|
|
|
default: break;
|
2009-03-10 22:51:17 +00:00
|
|
|
}
|
|
|
|
if (retval == D3DDECLTYPE_UNUSED) {
|
|
|
|
PanicAlert("VarToD3D: Invalid type/size combo %i , %i", (int)t, size);
|
|
|
|
}
|
|
|
|
return retval;
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void D3DVertexFormat::Initialize(const PortableVertexDeclaration &_vtx_decl)
|
|
|
|
{
|
2009-02-28 22:10:38 +00:00
|
|
|
vertex_stride = _vtx_decl.stride;
|
|
|
|
|
2010-11-26 09:25:08 +00:00
|
|
|
D3DVERTEXELEMENT9 elems[32];
|
|
|
|
memset(elems, 0, sizeof(elems));
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
// There's only one stream and it's 0, so the above memset takes care of that - no need to set Stream.
|
|
|
|
// Same for method.
|
|
|
|
|
|
|
|
// So, here we go. First position:
|
|
|
|
int elem_idx = 0;
|
2009-03-20 18:13:31 +00:00
|
|
|
elems[elem_idx].Offset = 0; // Positions are always first, at position 0. Always float3.
|
2009-02-23 06:15:48 +00:00
|
|
|
elems[elem_idx].Type = D3DDECLTYPE_FLOAT3;
|
|
|
|
elems[elem_idx].Usage = D3DDECLUSAGE_POSITION;
|
|
|
|
++elem_idx;
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
2009-03-09 23:24:53 +00:00
|
|
|
if (_vtx_decl.normal_offset[i] > 0)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
elems[elem_idx].Offset = _vtx_decl.normal_offset[i];
|
2009-03-10 22:51:17 +00:00
|
|
|
elems[elem_idx].Type = VarToD3D(_vtx_decl.normal_gl_type, _vtx_decl.normal_gl_size);
|
2009-02-23 06:15:48 +00:00
|
|
|
elems[elem_idx].Usage = D3DDECLUSAGE_NORMAL;
|
|
|
|
elems[elem_idx].UsageIndex = i;
|
|
|
|
++elem_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2009-03-09 23:24:53 +00:00
|
|
|
if (_vtx_decl.color_offset[i] > 0)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
elems[elem_idx].Offset = _vtx_decl.color_offset[i];
|
2009-03-10 22:51:17 +00:00
|
|
|
elems[elem_idx].Type = VarToD3D(_vtx_decl.color_gl_type, 4);
|
2009-02-23 06:15:48 +00:00
|
|
|
elems[elem_idx].Usage = D3DDECLUSAGE_COLOR;
|
|
|
|
elems[elem_idx].UsageIndex = i;
|
|
|
|
++elem_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2009-03-09 23:24:53 +00:00
|
|
|
if (_vtx_decl.texcoord_offset[i] > 0)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
elems[elem_idx].Offset = _vtx_decl.texcoord_offset[i];
|
2009-03-10 22:51:17 +00:00
|
|
|
elems[elem_idx].Type = VarToD3D(_vtx_decl.texcoord_gl_type[i], _vtx_decl.texcoord_size[i]);
|
2009-02-23 06:15:48 +00:00
|
|
|
elems[elem_idx].Usage = D3DDECLUSAGE_TEXCOORD;
|
|
|
|
elems[elem_idx].UsageIndex = i;
|
|
|
|
++elem_idx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
if (_vtx_decl.posmtx_offset != -1)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
elems[elem_idx].Offset = _vtx_decl.posmtx_offset;
|
|
|
|
elems[elem_idx].Usage = D3DDECLUSAGE_BLENDINDICES;
|
2009-08-31 06:35:43 +00:00
|
|
|
elems[elem_idx].Type = D3DDECLTYPE_D3DCOLOR;
|
2009-02-23 06:15:48 +00:00
|
|
|
elems[elem_idx].UsageIndex = 0;
|
|
|
|
++elem_idx;
|
|
|
|
}
|
|
|
|
|
2009-02-28 22:10:38 +00:00
|
|
|
// End marker
|
|
|
|
elems[elem_idx].Stream = 0xff;
|
|
|
|
elems[elem_idx].Type = D3DDECLTYPE_UNUSED;
|
|
|
|
++elem_idx;
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
if (FAILED(DX9::D3D::dev->CreateVertexDeclaration(elems, &d3d_decl)))
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
|
|
|
PanicAlert("Failed to create D3D vertex declaration!");
|
|
|
|
return;
|
|
|
|
}
|
2010-11-29 16:16:48 +00:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
memcpy(&elements, elems, sizeof(elems));
|
|
|
|
num_elements = elem_idx;
|
|
|
|
#endif
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2011-01-24 11:57:17 +00:00
|
|
|
void D3DVertexFormat::SetupVertexPointers()
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2009-09-01 19:48:45 +00:00
|
|
|
if (d3d_decl)
|
2011-01-29 20:16:51 +00:00
|
|
|
DX9::D3D::SetVertexDeclaration(d3d_decl);
|
2009-09-01 19:48:45 +00:00
|
|
|
else
|
|
|
|
ERROR_LOG(VIDEO, "invalid d3d decl");
|
2010-06-02 20:35:12 +00:00
|
|
|
}
|
2011-01-31 01:28:32 +00:00
|
|
|
|
|
|
|
} // namespace DX9
|