2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2016-09-24 03:16:52 -06:00
|
|
|
#include "VideoBackends/Software/SWVertexLoader.h"
|
|
|
|
|
2017-08-19 09:08:09 -06:00
|
|
|
#include <cstddef>
|
2014-12-22 09:25:05 -07:00
|
|
|
#include <limits>
|
|
|
|
|
2016-09-24 03:16:52 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-09-24 03:16:52 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
2015-10-09 12:50:36 -06:00
|
|
|
#include "VideoBackends/Software/DebugUtil.h"
|
2014-10-21 00:01:38 -06:00
|
|
|
#include "VideoBackends/Software/NativeVertexFormat.h"
|
2015-10-09 12:50:36 -06:00
|
|
|
#include "VideoBackends/Software/Rasterizer.h"
|
|
|
|
#include "VideoBackends/Software/Tev.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/Software/TransformUnit.h"
|
|
|
|
|
2016-01-31 12:51:55 -07:00
|
|
|
#include "VideoCommon/DataReader.h"
|
2015-10-09 12:50:36 -06:00
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/OpcodeDecoding.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-12-12 17:51:14 -07:00
|
|
|
#include "VideoCommon/VertexLoaderBase.h"
|
2015-05-29 06:42:45 -06:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2016-01-17 14:54:31 -07:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2015-10-09 12:50:36 -06:00
|
|
|
#include "VideoCommon/XFMemory.h"
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2015-10-09 12:50:36 -06:00
|
|
|
class NullNativeVertexFormat : public NativeVertexFormat
|
|
|
|
{
|
|
|
|
public:
|
2016-06-24 02:43:46 -06:00
|
|
|
NullNativeVertexFormat(const PortableVertexDeclaration& _vtx_decl) { vtx_decl = _vtx_decl; }
|
2015-10-09 12:50:36 -06:00
|
|
|
};
|
|
|
|
|
2017-02-18 01:14:30 -07:00
|
|
|
std::unique_ptr<NativeVertexFormat>
|
2016-06-24 02:43:46 -06:00
|
|
|
SWVertexLoader::CreateNativeVertexFormat(const PortableVertexDeclaration& vtx_decl)
|
2015-10-09 12:50:36 -06:00
|
|
|
{
|
2017-02-18 01:14:30 -07:00
|
|
|
return std::make_unique<NullNativeVertexFormat>(vtx_decl);
|
2015-10-09 12:50:36 -06:00
|
|
|
}
|
2015-05-29 06:42:45 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
SWVertexLoader::SWVertexLoader()
|
|
|
|
: m_local_vertex_buffer(MAXVBUFFERSIZE), m_local_index_buffer(MAXIBUFFERSIZE)
|
2014-07-29 18:55:07 -06:00
|
|
|
{
|
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2011-02-03 12:55:30 -07:00
|
|
|
SWVertexLoader::~SWVertexLoader()
|
2010-06-08 19:37:08 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-27 00:16:53 -07:00
|
|
|
void SWVertexLoader::UploadUtilityUniforms(const void* uniforms, u32 uniforms_size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWVertexLoader::ResetBuffer(u32 vertex_stride, bool cull_all)
|
2010-06-08 19:37:08 -06:00
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = m_local_vertex_buffer.data();
|
|
|
|
m_end_buffer_pointer = m_cur_buffer_pointer + m_local_vertex_buffer.size();
|
2017-09-09 13:19:46 -06:00
|
|
|
IndexGenerator::Start(m_local_index_buffer.data());
|
2015-10-09 12:50:36 -06:00
|
|
|
}
|
2015-12-05 04:18:31 -07:00
|
|
|
|
2018-11-27 00:16:53 -07:00
|
|
|
void SWVertexLoader::CommitBuffer(u32 num_vertices, u32 vertex_stride, u32 num_indices,
|
|
|
|
u32* out_base_vertex, u32* out_base_index)
|
|
|
|
{
|
|
|
|
*out_base_vertex = 0;
|
|
|
|
*out_base_index = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWVertexLoader::UploadConstants()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWVertexLoader::DrawCurrentBatch(u32 base_index, u32 num_indices, u32 base_vertex)
|
2015-10-09 12:50:36 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
DebugUtil::OnObjectBegin();
|
|
|
|
|
|
|
|
u8 primitiveType = 0;
|
2016-08-21 21:46:52 -06:00
|
|
|
switch (m_current_primitive_type)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Points:
|
2017-02-07 21:11:04 -07:00
|
|
|
primitiveType = OpcodeDecoder::GX_DRAW_POINTS;
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Lines:
|
2017-02-07 21:11:04 -07:00
|
|
|
primitiveType = OpcodeDecoder::GX_DRAW_LINES;
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Triangles:
|
|
|
|
primitiveType = OpcodeDecoder::GX_DRAW_TRIANGLES;
|
|
|
|
break;
|
|
|
|
case PrimitiveType::TriangleStrip:
|
|
|
|
primitiveType = OpcodeDecoder::GX_DRAW_TRIANGLE_STRIP;
|
2016-06-24 02:43:46 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
m_setup_unit.Init(primitiveType);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// set all states with are stored within video sw
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
Rasterizer::SetTevReg(i, Tev::RED_C, PixelShaderManager::constants.kcolors[i][0]);
|
|
|
|
Rasterizer::SetTevReg(i, Tev::GRN_C, PixelShaderManager::constants.kcolors[i][1]);
|
|
|
|
Rasterizer::SetTevReg(i, Tev::BLU_C, PixelShaderManager::constants.kcolors[i][2]);
|
|
|
|
Rasterizer::SetTevReg(i, Tev::ALP_C, PixelShaderManager::constants.kcolors[i][3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u32 i = 0; i < IndexGenerator::GetIndexLen(); i++)
|
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
const u16 index = m_local_index_buffer[i];
|
2018-10-14 16:09:17 -06:00
|
|
|
memset(static_cast<void*>(&m_vertex), 0, sizeof(m_vertex));
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// Super Mario Sunshine requires those to be zero for those debug boxes.
|
2017-09-09 13:13:17 -06:00
|
|
|
m_vertex.color = {};
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
// parse the videocommon format to our own struct format (m_vertex)
|
2016-06-24 02:43:46 -06:00
|
|
|
SetFormat(g_main_cp_state.last_id, primitiveType);
|
|
|
|
ParseVertex(VertexLoaderManager::GetCurrentVertexFormat()->GetVertexDeclaration(), index);
|
|
|
|
|
|
|
|
// transform this vertex so that it can be used for rasterization (outVertex)
|
2017-09-09 13:13:17 -06:00
|
|
|
OutputVertexData* outVertex = m_setup_unit.GetVertex();
|
|
|
|
TransformUnit::TransformPosition(&m_vertex, outVertex);
|
2017-08-19 09:08:09 -06:00
|
|
|
outVertex->normal = {};
|
2016-06-24 02:43:46 -06:00
|
|
|
if (VertexLoaderManager::g_current_components & VB_HAS_NRM0)
|
|
|
|
{
|
|
|
|
TransformUnit::TransformNormal(
|
2017-09-09 13:13:17 -06:00
|
|
|
&m_vertex, (VertexLoaderManager::g_current_components & VB_HAS_NRM2) != 0, outVertex);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2017-09-09 13:13:17 -06:00
|
|
|
TransformUnit::TransformColor(&m_vertex, outVertex);
|
|
|
|
TransformUnit::TransformTexCoord(&m_vertex, outVertex, m_tex_gen_special_case);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// assemble and rasterize the primitive
|
2017-09-09 13:13:17 -06:00
|
|
|
m_setup_unit.SetupVertex();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
INCSTAT(stats.thisFrame.numVerticesLoaded)
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugUtil::OnObjectEnd();
|
2015-10-09 12:50:36 -06:00
|
|
|
}
|
2014-12-07 13:51:45 -07:00
|
|
|
|
2015-10-09 12:50:36 -06:00
|
|
|
void SWVertexLoader::SetFormat(u8 attributeIndex, u8 primitiveType)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// matrix index from xf regs or cp memory?
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Matrix indices don't match");
|
|
|
|
}
|
|
|
|
|
2017-09-09 13:13:17 -06: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;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
// special case if only pos and tex coord 0 and tex coord input is AB11
|
|
|
|
// http://libogc.devkitpro.org/gx_8h.html#a55a426a3ff796db584302bddd829f002
|
2017-09-09 13:13:17 -06:00
|
|
|
m_tex_gen_special_case = VertexLoaderManager::g_current_components == VB_HAS_UV0 &&
|
|
|
|
xfmem.texMtxInfo[0].projection == XF_TEXPROJ_ST;
|
2014-12-07 13:51:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename I>
|
|
|
|
static T ReadNormalized(I value)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
T casted = (T)value;
|
|
|
|
if (!std::numeric_limits<T>::is_integer && std::numeric_limits<I>::is_integer)
|
|
|
|
{
|
|
|
|
// normalize if non-float is converted to a float
|
|
|
|
casted *= (T)(1.0 / std::numeric_limits<I>::max());
|
|
|
|
}
|
|
|
|
return casted;
|
2014-12-07 13:51:45 -07:00
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2014-12-07 13:51:45 -07:00
|
|
|
template <typename T, bool swap = false>
|
2016-06-24 02:43:46 -06:00
|
|
|
static void ReadVertexAttribute(T* dst, DataReader src, const AttributeFormat& format,
|
|
|
|
int base_component, int components, bool reverse)
|
2014-12-07 13:51:45 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
if (format.enable)
|
|
|
|
{
|
|
|
|
src.Skip(format.offset);
|
|
|
|
src.Skip(base_component * (1 << (format.type >> 1)));
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < std::min(format.components - base_component, components); i++)
|
|
|
|
{
|
|
|
|
int i_dst = reverse ? components - i - 1 : i;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-14 18:34:35 -06:00
|
|
|
ASSERT_MSG(VIDEO, !format.integer || format.type != VAR_FLOAT,
|
|
|
|
"only non-float values are allowed to be streamed as integer");
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
for (; i < components; i++)
|
|
|
|
{
|
|
|
|
int i_dst = reverse ? components - i - 1 : i;
|
|
|
|
dst[i_dst] = i == 3;
|
|
|
|
}
|
|
|
|
}
|
2014-12-07 13:51:45 -07:00
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2015-10-09 12:50:36 -06:00
|
|
|
void SWVertexLoader::ParseVertex(const PortableVertexDeclaration& vdec, int index)
|
2014-12-07 13:51:45 -07:00
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
DataReader src(m_local_vertex_buffer.data(),
|
|
|
|
m_local_vertex_buffer.data() + m_local_vertex_buffer.size());
|
2016-06-24 02:43:46 -06:00
|
|
|
src.Skip(index * vdec.stride);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<float>(&m_vertex.position[0], src, vdec.position, 0, 3, false);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
for (std::size_t i = 0; i < m_vertex.normal.size(); i++)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<float>(&m_vertex.normal[i][0], src, vdec.normals[i], 0, 3, false);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
for (std::size_t i = 0; i < m_vertex.color.size(); i++)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<u8>(m_vertex.color[i].data(), src, vdec.colors[i], 0, 4, true);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
for (std::size_t i = 0; i < m_vertex.texCoords.size(); i++)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<float>(m_vertex.texCoords[i].data(), src, vdec.texcoords[i], 0, 2, false);
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// the texmtr is stored as third component of the texCoord
|
|
|
|
if (vdec.texcoords[i].components >= 3)
|
|
|
|
{
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<u8>(&m_vertex.texMtx[i], src, vdec.texcoords[i], 2, 1, false);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-08 19:37:08 -06:00
|
|
|
|
2017-09-09 13:13:17 -06:00
|
|
|
ReadVertexAttribute<u8>(&m_vertex.posMtx, src, vdec.posmtx, 0, 1, false);
|
2010-06-08 19:37:08 -06:00
|
|
|
}
|