2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-22 16:25:05 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2014-07-30 00:55:07 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Software/CPMemLoader.h"
|
2014-10-21 06:01:38 +00:00
|
|
|
#include "VideoBackends/Software/NativeVertexFormat.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/SetupUnit.h"
|
|
|
|
#include "VideoBackends/Software/SWStatistics.h"
|
|
|
|
#include "VideoBackends/Software/SWVertexLoader.h"
|
|
|
|
#include "VideoBackends/Software/TransformUnit.h"
|
|
|
|
#include "VideoBackends/Software/XFMemLoader.h"
|
|
|
|
|
2014-12-13 00:51:14 +00:00
|
|
|
#include "VideoCommon/VertexLoaderBase.h"
|
2015-05-29 12:42:45 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-12-13 00:51:14 +00:00
|
|
|
#include "VideoCommon/VertexLoaderUtils.h"
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2015-05-29 12:42:45 +00:00
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
SWVertexLoader::SWVertexLoader() :
|
2014-12-07 20:51:45 +00:00
|
|
|
m_VertexSize(0)
|
2014-07-30 00:55:07 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
m_SetupUnit = new SetupUnit;
|
2014-07-30 00:55:07 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
SWVertexLoader::~SWVertexLoader()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2013-04-14 03:54:02 +00:00
|
|
|
delete m_SetupUnit;
|
2014-03-09 20:14:26 +00:00
|
|
|
m_SetupUnit = nullptr;
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWVertexLoader::SetFormat(u8 attributeIndex, u8 primitiveType)
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2015-12-05 11:18:31 +00:00
|
|
|
memset(&m_Vertex, 0, sizeof(m_Vertex));
|
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
m_attributeIndex = attributeIndex;
|
|
|
|
|
|
|
|
VertexLoaderUID uid(g_main_cp_state.vtx_desc, g_main_cp_state.vtx_attr[m_attributeIndex]);
|
|
|
|
m_CurrentLoader = m_VertexLoaderMap[uid].get();
|
|
|
|
|
|
|
|
if (!m_CurrentLoader)
|
|
|
|
{
|
2015-12-23 00:59:32 +00:00
|
|
|
m_VertexLoaderMap[uid] = VertexLoaderBase::CreateVertexLoader(g_main_cp_state.vtx_desc, g_main_cp_state.vtx_attr[m_attributeIndex]);
|
|
|
|
m_CurrentLoader = m_VertexLoaderMap[uid].get();
|
2014-12-07 20:51:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:23:54 +00:00
|
|
|
m_VertexSize = m_CurrentLoader->m_VertexSize;
|
2014-12-07 20:51:45 +00:00
|
|
|
m_CurrentVat = &g_main_cp_state.vtx_attr[m_attributeIndex];
|
|
|
|
|
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
// matrix index from xf regs or cp memory?
|
2014-08-27 17:26:06 +00:00
|
|
|
if (xfmem.MatrixIndexA.PosNormalMtxIdx != g_main_cp_state.matrix_index_a.PosNormalMtxIdx ||
|
|
|
|
xfmem.MatrixIndexA.Tex0MtxIdx != g_main_cp_state.matrix_index_a.Tex0MtxIdx ||
|
|
|
|
xfmem.MatrixIndexA.Tex1MtxIdx != g_main_cp_state.matrix_index_a.Tex1MtxIdx ||
|
|
|
|
xfmem.MatrixIndexA.Tex2MtxIdx != g_main_cp_state.matrix_index_a.Tex2MtxIdx ||
|
|
|
|
xfmem.MatrixIndexA.Tex3MtxIdx != g_main_cp_state.matrix_index_a.Tex3MtxIdx ||
|
|
|
|
xfmem.MatrixIndexB.Tex4MtxIdx != g_main_cp_state.matrix_index_b.Tex4MtxIdx ||
|
|
|
|
xfmem.MatrixIndexB.Tex5MtxIdx != g_main_cp_state.matrix_index_b.Tex5MtxIdx ||
|
|
|
|
xfmem.MatrixIndexB.Tex6MtxIdx != g_main_cp_state.matrix_index_b.Tex6MtxIdx ||
|
|
|
|
xfmem.MatrixIndexB.Tex7MtxIdx != g_main_cp_state.matrix_index_b.Tex7MtxIdx)
|
2010-12-01 04:26:21 +00:00
|
|
|
{
|
2015-09-20 11:13:47 +00:00
|
|
|
ERROR_LOG(VIDEO, "Matrix indices don't match");
|
2010-12-01 04:26:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 18:59:04 +00:00
|
|
|
m_Vertex.posMtx = xfmem.MatrixIndexA.PosNormalMtxIdx;
|
|
|
|
m_Vertex.texMtx[0] = xfmem.MatrixIndexA.Tex0MtxIdx;
|
|
|
|
m_Vertex.texMtx[1] = xfmem.MatrixIndexA.Tex1MtxIdx;
|
|
|
|
m_Vertex.texMtx[2] = xfmem.MatrixIndexA.Tex2MtxIdx;
|
|
|
|
m_Vertex.texMtx[3] = xfmem.MatrixIndexA.Tex3MtxIdx;
|
|
|
|
m_Vertex.texMtx[4] = xfmem.MatrixIndexB.Tex4MtxIdx;
|
|
|
|
m_Vertex.texMtx[5] = xfmem.MatrixIndexB.Tex5MtxIdx;
|
|
|
|
m_Vertex.texMtx[6] = xfmem.MatrixIndexB.Tex6MtxIdx;
|
|
|
|
m_Vertex.texMtx[7] = xfmem.MatrixIndexB.Tex7MtxIdx;
|
2014-12-07 20:51:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
// special case if only pos and tex coord 0 and tex coord input is AB11
|
|
|
|
m_TexGenSpecialCase =
|
|
|
|
((g_main_cp_state.vtx_desc.Hex & 0x60600L) == g_main_cp_state.vtx_desc.Hex) && // only pos and tex coord 0
|
|
|
|
(g_main_cp_state.vtx_desc.Tex0Coord != NOT_PRESENT) &&
|
|
|
|
(xfmem.texMtxInfo[0].projection == XF_TEXPROJ_ST);
|
|
|
|
|
|
|
|
m_SetupUnit->Init(primitiveType);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename I>
|
|
|
|
static T ReadNormalized(I value)
|
|
|
|
{
|
|
|
|
T casted = (T) value;
|
|
|
|
if (!std::numeric_limits<T>::is_integer && std::numeric_limits<I>::is_integer)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2014-12-07 20:51:45 +00:00
|
|
|
// normalize if non-float is converted to a float
|
|
|
|
casted *= (T) (1.0 / std::numeric_limits<I>::max());
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2014-12-07 20:51:45 +00:00
|
|
|
return casted;
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
template <typename T, bool swap = false>
|
2015-03-14 09:15:19 +00:00
|
|
|
static void ReadVertexAttribute(T* dst, DataReader src, const AttributeFormat& format, int base_component, int components, bool reverse)
|
2014-12-07 20:51:45 +00:00
|
|
|
{
|
|
|
|
if (format.enable)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2014-12-07 20:51:45 +00:00
|
|
|
src.Skip(format.offset);
|
|
|
|
src.Skip(base_component * (1<<(format.type>>1)));
|
|
|
|
|
2015-03-14 09:15:19 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < std::min(format.components - base_component, components); i++)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
2015-03-14 09:15:19 +00:00
|
|
|
int i_dst = reverse ? components - i - 1 : i;
|
2014-12-07 20:51:45 +00:00
|
|
|
switch (format.type)
|
|
|
|
{
|
|
|
|
case VAR_UNSIGNED_BYTE:
|
|
|
|
dst[i_dst] = ReadNormalized<T, u8>(src.Read<u8, swap>());
|
|
|
|
break;
|
|
|
|
case VAR_BYTE:
|
|
|
|
dst[i_dst] = ReadNormalized<T, s8>(src.Read<s8, swap>());
|
|
|
|
break;
|
|
|
|
case VAR_UNSIGNED_SHORT:
|
|
|
|
dst[i_dst] = ReadNormalized<T, u16>(src.Read<u16, swap>());
|
|
|
|
break;
|
|
|
|
case VAR_SHORT:
|
|
|
|
dst[i_dst] = ReadNormalized<T, s16>(src.Read<s16, swap>());
|
|
|
|
break;
|
|
|
|
case VAR_FLOAT:
|
|
|
|
dst[i_dst] = ReadNormalized<T, float>(src.Read<float, swap>());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_assert_msg_(VIDEO, !format.integer || format.type != VAR_FLOAT, "only non-float values are allowed to be streamed as integer");
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2015-03-14 09:15:19 +00:00
|
|
|
for (; i < components; i++)
|
|
|
|
{
|
|
|
|
int i_dst = reverse ? components - i - 1 : i;
|
|
|
|
dst[i_dst] = i == 3;
|
|
|
|
}
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2014-12-07 20:51:45 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
void SWVertexLoader::ParseVertex(const PortableVertexDeclaration& vdec)
|
|
|
|
{
|
|
|
|
DataReader src(m_LoadedVertices.data(), m_LoadedVertices.data() + m_LoadedVertices.size());
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
ReadVertexAttribute<float>(&m_Vertex.position[0], src, vdec.position, 0, 3, false);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
ReadVertexAttribute<float>(&m_Vertex.normal[i][0], src, vdec.normals[i], 0, 3, false);
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
{
|
2014-12-07 20:51:45 +00:00
|
|
|
ReadVertexAttribute<u8>(m_Vertex.color[i], src, vdec.colors[i], 0, 4, true);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2014-12-07 20:51:45 +00:00
|
|
|
ReadVertexAttribute<float>(m_Vertex.texCoords[i], src, vdec.texcoords[i], 0, 2, false);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
// the texmtr is stored as third component of the texCoord
|
|
|
|
if (vdec.texcoords[i].components >= 3)
|
|
|
|
{
|
|
|
|
ReadVertexAttribute<u8>(&m_Vertex.texMtx[i], src, vdec.texcoords[i], 2, 1, false);
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
ReadVertexAttribute<u8>(&m_Vertex.posMtx, src, vdec.posmtx, 0, 1, false);
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-03 19:55:30 +00:00
|
|
|
void SWVertexLoader::LoadVertex()
|
2010-06-09 01:37:08 +00:00
|
|
|
{
|
2014-12-12 23:23:54 +00:00
|
|
|
const PortableVertexDeclaration& vdec = m_CurrentLoader->m_native_vtx_decl;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
// reserve memory for the destination of the vertex loader
|
|
|
|
m_LoadedVertices.resize(vdec.stride + 4);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2015-05-29 12:42:45 +00:00
|
|
|
VertexLoaderManager::UpdateVertexArrayPointers();
|
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
// convert the vertex from the gc format to the videocommon (hardware optimized) format
|
|
|
|
u8* old = g_video_buffer_read_ptr;
|
2015-01-18 03:22:36 +00:00
|
|
|
int converted_vertices = m_CurrentLoader->RunVertices(
|
2014-12-07 20:51:45 +00:00
|
|
|
DataReader(g_video_buffer_read_ptr, nullptr), // src
|
2015-02-13 00:50:10 +00:00
|
|
|
DataReader(m_LoadedVertices.data(), m_LoadedVertices.data() + m_LoadedVertices.size()), // dst
|
2015-04-06 09:44:13 +00:00
|
|
|
1 // vertices
|
2014-12-07 20:51:45 +00:00
|
|
|
);
|
2014-12-12 23:23:54 +00:00
|
|
|
g_video_buffer_read_ptr = old + m_CurrentLoader->m_VertexSize;
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2015-01-18 03:22:36 +00:00
|
|
|
if (converted_vertices == 0)
|
|
|
|
return;
|
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
// parse the videocommon format to our own struct format (m_Vertex)
|
|
|
|
ParseVertex(vdec);
|
|
|
|
|
|
|
|
// transform this vertex so that it can be used for rasterization (outVertex)
|
|
|
|
OutputVertexData* outVertex = m_SetupUnit->GetVertex();
|
|
|
|
TransformUnit::TransformPosition(&m_Vertex, outVertex);
|
2015-12-19 09:51:42 +00:00
|
|
|
memset(&outVertex->normal, 0, sizeof(outVertex->normal));
|
2014-08-27 17:26:06 +00:00
|
|
|
if (g_main_cp_state.vtx_desc.Normal != NOT_PRESENT)
|
2013-04-14 03:54:02 +00:00
|
|
|
{
|
|
|
|
TransformUnit::TransformNormal(&m_Vertex, m_CurrentVat->g0.NormalElements, outVertex);
|
|
|
|
}
|
|
|
|
TransformUnit::TransformColor(&m_Vertex, outVertex);
|
|
|
|
TransformUnit::TransformTexCoord(&m_Vertex, outVertex, m_TexGenSpecialCase);
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2014-12-07 20:51:45 +00:00
|
|
|
// assemble and rasterize the primitive
|
2013-04-14 03:54:02 +00:00
|
|
|
m_SetupUnit->SetupVertex();
|
2010-06-09 01:37:08 +00:00
|
|
|
|
2013-04-14 03:54:02 +00:00
|
|
|
INCSTAT(swstats.thisFrame.numVerticesLoaded)
|
2010-06-09 01:37:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 01:05:02 +00:00
|
|
|
void SWVertexLoader::DoState(PointerWrap &p)
|
|
|
|
{
|
|
|
|
p.Do(m_VertexSize);
|
2013-02-26 04:49:24 +00:00
|
|
|
p.Do(*m_CurrentVat);
|
2013-02-26 01:05:02 +00:00
|
|
|
m_SetupUnit->DoState(p);
|
|
|
|
p.Do(m_TexGenSpecialCase);
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|