2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
#include "VideoCommon/VertexLoader.h"
|
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2015-06-01 17:58:27 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "VideoCommon/VertexLoaderUtils.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VertexLoader_Color.h"
|
|
|
|
#include "VideoCommon/VertexLoader_Normal.h"
|
|
|
|
#include "VideoCommon/VertexLoader_Position.h"
|
|
|
|
#include "VideoCommon/VertexLoader_TextCoord.h"
|
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2014-12-09 07:30:38 +00:00
|
|
|
// This pointer is used as the source/dst for all fixed function loader calls
|
2022-11-23 00:54:05 +00:00
|
|
|
const u8* g_video_buffer_read_ptr;
|
2014-12-09 07:30:38 +00:00
|
|
|
u8* g_vertex_manager_write_ptr;
|
2014-11-27 22:53:11 +00:00
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void PosMtx_ReadDirect_UByte(VertexLoader* loader)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2015-10-01 07:28:19 +00:00
|
|
|
u32 posmtx = DataRead<u8>() & 0x3f;
|
2022-04-14 19:01:57 +00:00
|
|
|
if (loader->m_remaining < 3)
|
|
|
|
VertexLoaderManager::position_matrix_index_cache[loader->m_remaining] = posmtx;
|
2015-01-15 00:46:23 +00:00
|
|
|
DataWrite<u32>(posmtx);
|
2020-11-14 03:33:26 +00:00
|
|
|
PRIM_LOG("posmtx: {}, ", posmtx);
|
2009-06-28 20:04:07 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void TexMtx_ReadDirect_UByte(VertexLoader* loader)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2015-10-01 07:28:19 +00:00
|
|
|
loader->m_curtexmtx[loader->m_texmtxread] = DataRead<u8>() & 0x3f;
|
2014-09-14 16:52:51 +00:00
|
|
|
|
2020-11-14 03:33:26 +00:00
|
|
|
PRIM_LOG("texmtx{}: {}, ", loader->m_texmtxread, loader->m_curtexmtx[loader->m_texmtxread]);
|
2014-12-13 09:57:46 +00:00
|
|
|
loader->m_texmtxread++;
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void TexMtx_Write_Float(VertexLoader* loader)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2014-12-13 09:57:46 +00:00
|
|
|
DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void TexMtx_Write_Float2(VertexLoader* loader)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2013-02-22 08:19:59 +00:00
|
|
|
DataWrite(0.f);
|
2014-12-13 09:57:46 +00:00
|
|
|
DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void TexMtx_Write_Float3(VertexLoader* loader)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2013-02-22 08:19:59 +00:00
|
|
|
DataWrite(0.f);
|
|
|
|
DataWrite(0.f);
|
2014-12-13 09:57:46 +00:00
|
|
|
DataWrite(float(loader->m_curtexmtx[loader->m_texmtxwrite++]));
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-06-19 19:18:16 +00:00
|
|
|
static void SkipVertex(VertexLoader* loader)
|
2014-12-21 13:29:44 +00:00
|
|
|
{
|
|
|
|
if (loader->m_vertexSkip)
|
|
|
|
{
|
|
|
|
// reset the output buffer
|
|
|
|
g_vertex_manager_write_ptr -= loader->m_native_vtx_decl.stride;
|
|
|
|
|
|
|
|
loader->m_skippedVertices++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-25 15:53:43 +00:00
|
|
|
VertexLoader::VertexLoader(const TVtxDesc& vtx_desc, const VAT& vtx_attr)
|
2014-12-13 00:51:14 +00:00
|
|
|
: VertexLoaderBase(vtx_desc, vtx_attr)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2013-03-06 17:58:15 +00:00
|
|
|
CompileVertexTranslator();
|
2014-12-13 09:57:46 +00:00
|
|
|
|
|
|
|
// generate frac factors
|
2021-03-11 06:15:43 +00:00
|
|
|
m_posScale = 1.0f / (1U << m_VtxAttr.g0.PosFrac);
|
|
|
|
for (u32 i = 0; i < 8; i++)
|
|
|
|
m_tcScale[i] = 1.0f / (1U << m_VtxAttr.GetTexFrac(i));
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-11-23 00:12:44 +00:00
|
|
|
void VertexLoader::CompileVertexTranslator()
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2014-02-23 14:14:27 +00:00
|
|
|
// Reset pipeline
|
|
|
|
m_numPipelineStages = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-11-30 22:47:06 +00:00
|
|
|
// Position in pc vertex format.
|
|
|
|
int nat_offset = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-10-25 15:53:43 +00:00
|
|
|
// Position Matrix Index
|
2021-02-08 23:22:10 +00:00
|
|
|
if (m_VtxDesc.low.PosMatIdx)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
2008-11-23 17:46:14 +00:00
|
|
|
WriteCall(PosMtx_ReadDirect_UByte);
|
2015-01-15 00:46:23 +00:00
|
|
|
m_native_vtx_decl.posmtx.components = 4;
|
|
|
|
m_native_vtx_decl.posmtx.enable = true;
|
|
|
|
m_native_vtx_decl.posmtx.offset = nat_offset;
|
2021-06-26 19:48:28 +00:00
|
|
|
m_native_vtx_decl.posmtx.type = ComponentFormat::UByte;
|
2015-01-15 00:46:23 +00:00
|
|
|
m_native_vtx_decl.posmtx.integer = true;
|
|
|
|
nat_offset += 4;
|
2008-10-25 15:53:43 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-11 20:55:25 +00:00
|
|
|
for (auto texmtxidx : m_VtxDesc.low.TexMatIdx)
|
2015-02-15 19:43:31 +00:00
|
|
|
{
|
2021-03-11 20:55:25 +00:00
|
|
|
if (texmtxidx)
|
|
|
|
WriteCall(TexMtx_ReadDirect_UByte);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
// Write vertex position loader
|
2021-03-11 06:15:43 +00:00
|
|
|
WriteCall(VertexLoader_Position::GetFunction(m_VtxDesc.low.Position, m_VtxAttr.g0.PosFormat,
|
|
|
|
m_VtxAttr.g0.PosElements));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-03-11 06:15:43 +00:00
|
|
|
int pos_elements = m_VtxAttr.g0.PosElements == CoordComponentCount::XY ? 2 : 3;
|
2011-03-21 05:46:33 +00:00
|
|
|
m_native_vtx_decl.position.components = pos_elements;
|
|
|
|
m_native_vtx_decl.position.enable = true;
|
2015-01-19 16:38:40 +00:00
|
|
|
m_native_vtx_decl.position.offset = nat_offset;
|
2021-06-26 19:48:28 +00:00
|
|
|
m_native_vtx_decl.position.type = ComponentFormat::Float;
|
2011-03-21 05:46:33 +00:00
|
|
|
m_native_vtx_decl.position.integer = false;
|
2015-03-17 06:05:19 +00:00
|
|
|
nat_offset += pos_elements * sizeof(float);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2011-03-21 05:46:33 +00:00
|
|
|
// Normals
|
2021-02-08 23:22:10 +00:00
|
|
|
if (m_VtxDesc.low.Normal != VertexComponentFormat::NotPresent)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2021-02-08 23:22:10 +00:00
|
|
|
TPipelineFunction pFunc =
|
2021-03-11 06:15:43 +00:00
|
|
|
VertexLoader_Normal::GetFunction(m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat,
|
|
|
|
m_VtxAttr.g0.NormalElements, m_VtxAttr.g0.NormalIndex3);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
if (pFunc == nullptr)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
2020-11-14 03:33:26 +00:00
|
|
|
PanicAlertFmt("VertexLoader_Normal::GetFunction({} {} {} {}) returned zero!",
|
2021-03-11 06:15:43 +00:00
|
|
|
m_VtxDesc.low.Normal, m_VtxAttr.g0.NormalFormat, m_VtxAttr.g0.NormalElements,
|
|
|
|
m_VtxAttr.g0.NormalIndex3);
|
2008-10-25 15:53:43 +00:00
|
|
|
}
|
|
|
|
WriteCall(pFunc);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-05-17 18:54:24 +00:00
|
|
|
for (int i = 0; i < (m_VtxAttr.g0.NormalElements == NormalComponentCount::NTB ? 3 : 1); i++)
|
2011-01-19 13:57:15 +00:00
|
|
|
{
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.normals[i].components = 3;
|
|
|
|
m_native_vtx_decl.normals[i].enable = true;
|
|
|
|
m_native_vtx_decl.normals[i].offset = nat_offset;
|
2021-06-26 19:48:28 +00:00
|
|
|
m_native_vtx_decl.normals[i].type = ComponentFormat::Float;
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.normals[i].integer = false;
|
2011-01-19 13:57:15 +00:00
|
|
|
nat_offset += 12;
|
|
|
|
}
|
2008-10-25 15:53:43 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
for (size_t i = 0; i < m_VtxDesc.low.Color.Size(); i++)
|
2008-11-30 22:47:06 +00:00
|
|
|
{
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.colors[i].components = 4;
|
2021-06-26 19:48:28 +00:00
|
|
|
m_native_vtx_decl.colors[i].type = ComponentFormat::UByte;
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.colors[i].integer = false;
|
2021-03-11 01:11:45 +00:00
|
|
|
|
|
|
|
TPipelineFunction pFunc =
|
2021-03-11 06:15:43 +00:00
|
|
|
VertexLoader_Color::GetFunction(m_VtxDesc.low.Color[i], m_VtxAttr.GetColorFormat(i));
|
2021-03-11 01:11:45 +00:00
|
|
|
|
|
|
|
if (pFunc != nullptr)
|
|
|
|
WriteCall(pFunc);
|
|
|
|
else
|
|
|
|
ASSERT(m_VtxDesc.low.Color[i] == VertexComponentFormat::NotPresent);
|
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
if (m_VtxDesc.low.Color[i] != VertexComponentFormat::NotPresent)
|
2010-02-28 11:36:00 +00:00
|
|
|
{
|
2010-04-09 15:13:42 +00:00
|
|
|
m_native_vtx_decl.colors[i].offset = nat_offset;
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.colors[i].enable = true;
|
2008-10-25 15:53:43 +00:00
|
|
|
nat_offset += 4;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-25 15:53:43 +00:00
|
|
|
// Texture matrix indices (remove if corresponding texture coordinate isn't enabled)
|
2021-02-08 23:22:10 +00:00
|
|
|
for (size_t i = 0; i < m_VtxDesc.high.TexCoord.Size(); i++)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2008-10-25 15:53:43 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].offset = nat_offset;
|
2021-06-26 19:48:28 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].type = ComponentFormat::Float;
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].integer = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
const auto tc = m_VtxDesc.high.TexCoord[i].Value();
|
2021-03-11 06:15:43 +00:00
|
|
|
const auto format = m_VtxAttr.GetTexFormat(i);
|
|
|
|
const auto elements = m_VtxAttr.GetTexElements(i);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
if (tc != VertexComponentFormat::NotPresent)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2021-02-08 23:22:10 +00:00
|
|
|
ASSERT_MSG(VIDEO, VertexComponentFormat::Direct <= tc && tc <= VertexComponentFormat::Index16,
|
2021-11-10 22:42:49 +00:00
|
|
|
"Invalid texture coordinates!\n(tc = {})", tc);
|
2021-02-08 23:22:10 +00:00
|
|
|
ASSERT_MSG(VIDEO, ComponentFormat::UByte <= format && format <= ComponentFormat::Float,
|
2021-11-10 22:42:49 +00:00
|
|
|
"Invalid texture coordinates format!\n(format = {})", format);
|
2021-02-08 23:22:10 +00:00
|
|
|
ASSERT_MSG(VIDEO, elements == TexComponentCount::S || elements == TexComponentCount::ST,
|
2021-11-10 22:42:49 +00:00
|
|
|
"Invalid number of texture coordinates elements!\n(elements = {})", elements);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
WriteCall(VertexLoader_TextCoord::GetFunction(tc, format, elements));
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 20:55:25 +00:00
|
|
|
if (m_VtxDesc.low.TexMatIdx[i])
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2008-10-25 15:53:43 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].enable = true;
|
2021-03-11 20:55:25 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].components = 3;
|
|
|
|
nat_offset += 12;
|
2021-02-08 23:22:10 +00:00
|
|
|
if (tc != VertexComponentFormat::NotPresent)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
|
|
|
// if texmtx is included, texcoord will always be 3 floats, z will be the texmtx index
|
2021-03-11 06:15:43 +00:00
|
|
|
WriteCall(elements == TexComponentCount::ST ? TexMtx_Write_Float : TexMtx_Write_Float2);
|
2008-10-25 15:53:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-30 22:47:06 +00:00
|
|
|
WriteCall(TexMtx_Write_Float3);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-11-30 22:47:06 +00:00
|
|
|
{
|
2021-02-08 23:22:10 +00:00
|
|
|
if (tc != VertexComponentFormat::NotPresent)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].enable = true;
|
2021-03-11 06:15:43 +00:00
|
|
|
m_native_vtx_decl.texcoords[i].components = elements == TexComponentCount::ST ? 2 : 1;
|
|
|
|
nat_offset += 4 * (elements == TexComponentCount::ST ? 2 : 1);
|
2008-10-25 15:53:43 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
if (tc == VertexComponentFormat::NotPresent)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
|
|
|
// if there's more tex coords later, have to write a dummy call
|
2021-03-11 20:55:25 +00:00
|
|
|
bool has_more = false;
|
|
|
|
for (size_t j = 0; j < m_VtxDesc.high.TexCoord.Size(); ++j)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
2021-02-08 23:22:10 +00:00
|
|
|
if (m_VtxDesc.high.TexCoord[j] != VertexComponentFormat::NotPresent)
|
2008-10-25 15:53:43 +00:00
|
|
|
{
|
2021-03-11 20:55:25 +00:00
|
|
|
has_more = true;
|
2010-04-09 15:13:42 +00:00
|
|
|
WriteCall(VertexLoader_TextCoord::GetDummyFunction()); // important to get indices right!
|
2008-10-25 15:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-03-11 20:55:25 +00:00
|
|
|
else if (m_VtxDesc.low.TexMatIdx[i])
|
|
|
|
{
|
|
|
|
has_more = true;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2021-03-11 20:55:25 +00:00
|
|
|
if (!has_more)
|
2008-11-30 22:47:06 +00:00
|
|
|
{
|
|
|
|
// no more tex coords and tex matrices, so exit loop
|
2008-10-25 15:53:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 23:22:10 +00:00
|
|
|
// indexed position formats may skip the vertex
|
|
|
|
if (IsIndexed(m_VtxDesc.low.Position))
|
2014-12-21 13:29:44 +00:00
|
|
|
{
|
|
|
|
WriteCall(SkipVertex);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-07-25 23:10:44 +00:00
|
|
|
m_native_vtx_decl.stride = nat_offset;
|
2008-07-17 20:29:04 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2008-10-24 23:08:46 +00:00
|
|
|
void VertexLoader::WriteCall(TPipelineFunction func)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2008-10-25 12:35:55 +00:00
|
|
|
m_PipelineStages[m_numPipelineStages++] = func;
|
2009-01-10 23:10:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-23 00:54:05 +00:00
|
|
|
int VertexLoader::RunVertices(const u8* src, u8* dst, int count)
|
2008-07-17 20:29:04 +00:00
|
|
|
{
|
2022-11-23 00:54:05 +00:00
|
|
|
g_vertex_manager_write_ptr = dst;
|
|
|
|
g_video_buffer_read_ptr = src;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2008-11-25 21:15:31 +00:00
|
|
|
m_numLoadedVertices += count;
|
2014-12-21 13:29:44 +00:00
|
|
|
m_skippedVertices = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-04-14 19:01:57 +00:00
|
|
|
for (m_remaining = count - 1; m_remaining >= 0; m_remaining--)
|
2013-02-21 10:36:10 +00:00
|
|
|
{
|
2014-12-13 09:57:46 +00:00
|
|
|
m_tcIndex = 0;
|
|
|
|
m_colIndex = 0;
|
|
|
|
m_texmtxwrite = m_texmtxread = 0;
|
2013-02-21 10:36:10 +00:00
|
|
|
for (int i = 0; i < m_numPipelineStages; i++)
|
2014-12-13 09:57:46 +00:00
|
|
|
m_PipelineStages[i](this);
|
2013-02-21 10:36:10 +00:00
|
|
|
PRIM_LOG("\n");
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-12-21 13:29:44 +00:00
|
|
|
return count - m_skippedVertices;
|
2014-01-30 13:48:23 +00:00
|
|
|
}
|