Skip to content

Commit

Permalink
Merge pull request #5944 from lioncash/sw
Browse files Browse the repository at this point in the history
SW NativeVertexFormat: Utilize std::array where applicable
  • Loading branch information
degasus committed Aug 23, 2017
2 parents c124187 + bc57ab3 commit 24e919a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
10 changes: 3 additions & 7 deletions Source/Core/VideoBackends/Software/Clipper.cpp
Expand Up @@ -338,15 +338,11 @@ static void CopyVertex(OutputVertexData* dst, const OutputVertexData* src, float
dst->screenPosition.y = src->screenPosition.y + dy;
dst->screenPosition.z = src->screenPosition.z;

for (int i = 0; i < 3; ++i)
dst->normal[i] = src->normal[i];

for (int i = 0; i < 4; ++i)
dst->color[0][i] = src->color[0][i];
dst->normal = src->normal;
dst->color = src->color;

// todo - s offset
for (int i = 0; i < 8; ++i)
dst->texCoords[i] = src->texCoords[i];
dst->texCoords = src->texCoords;
}

void ProcessLine(OutputVertexData* lineV0, OutputVertexData* lineV1)
Expand Down
25 changes: 14 additions & 11 deletions Source/Core/VideoBackends/Software/NativeVertexFormat.h
Expand Up @@ -4,6 +4,9 @@

#pragma once

#include <array>
#include <cstddef>

#include "Common/CommonTypes.h"
#include "VideoBackends/Software/Vec3.h"

Expand All @@ -18,12 +21,12 @@ struct Vec4
struct InputVertexData
{
u8 posMtx;
u8 texMtx[8];
std::array<u8, 8> texMtx;

Vec3 position;
Vec3 normal[3];
u8 color[2][4];
float texCoords[8][2];
std::array<Vec3, 3> normal;
std::array<std::array<u8, 4>, 2> color;
std::array<std::array<float, 2>, 8> texCoords;
};

struct OutputVertexData
Expand All @@ -40,9 +43,9 @@ struct OutputVertexData
Vec3 mvPosition = {};
Vec4 projectedPosition = {};
Vec3 screenPosition = {};
Vec3 normal[3] = {};
u8 color[2][4] = {};
Vec3 texCoords[8] = {};
std::array<Vec3, 3> normal{};
std::array<std::array<u8, 4>, 2> color{};
std::array<Vec3, 8> texCoords{};

void Lerp(float t, const OutputVertexData* a, const OutputVertexData* b)
{
Expand All @@ -57,19 +60,19 @@ struct OutputVertexData
projectedPosition.z = LINTERP(t, a->projectedPosition.z, b->projectedPosition.z);
projectedPosition.w = LINTERP(t, a->projectedPosition.w, b->projectedPosition.w);

for (int i = 0; i < 3; ++i)
for (std::size_t i = 0; i < normal.size(); ++i)
{
normal[i] = LINTERP(t, a->normal[i], b->normal[i]);
}

u16 t_int = (u16)(t * 256);
for (int i = 0; i < 4; ++i)
const u16 t_int = static_cast<u16>(t * 256);
for (std::size_t i = 0; i < color[0].size(); ++i)
{
color[0][i] = LINTERP_INT(t_int, a->color[0][i], b->color[0][i]);
color[1][i] = LINTERP_INT(t_int, a->color[1][i], b->color[1][i]);
}

for (int i = 0; i < 8; ++i)
for (std::size_t i = 0; i < texCoords.size(); ++i)
{
texCoords[i] = LINTERP(t, a->texCoords[i], b->texCoords[i]);
}
Expand Down
15 changes: 8 additions & 7 deletions Source/Core/VideoBackends/Software/SWVertexLoader.cpp
Expand Up @@ -4,6 +4,7 @@

#include "VideoBackends/Software/SWVertexLoader.h"

#include <cstddef>
#include <limits>

#include "Common/Assert.h"
Expand Down Expand Up @@ -97,7 +98,7 @@ void SWVertexLoader::vFlush()
memset(&m_Vertex, 0, sizeof(m_Vertex));

// Super Mario Sunshine requires those to be zero for those debug boxes.
memset(&m_Vertex.color, 0, sizeof(m_Vertex.color));
m_Vertex.color = {};

// parse the videocommon format to our own struct format (m_Vertex)
SetFormat(g_main_cp_state.last_id, primitiveType);
Expand All @@ -106,7 +107,7 @@ void SWVertexLoader::vFlush()
// transform this vertex so that it can be used for rasterization (outVertex)
OutputVertexData* outVertex = m_SetupUnit.GetVertex();
TransformUnit::TransformPosition(&m_Vertex, outVertex);
memset(&outVertex->normal, 0, sizeof(outVertex->normal));
outVertex->normal = {};
if (VertexLoaderManager::g_current_components & VB_HAS_NRM0)
{
TransformUnit::TransformNormal(
Expand Down Expand Up @@ -218,19 +219,19 @@ void SWVertexLoader::ParseVertex(const PortableVertexDeclaration& vdec, int inde

ReadVertexAttribute<float>(&m_Vertex.position[0], src, vdec.position, 0, 3, false);

for (int i = 0; i < 3; i++)
for (std::size_t i = 0; i < m_Vertex.normal.size(); i++)
{
ReadVertexAttribute<float>(&m_Vertex.normal[i][0], src, vdec.normals[i], 0, 3, false);
}

for (int i = 0; i < 2; i++)
for (std::size_t i = 0; i < m_Vertex.color.size(); i++)
{
ReadVertexAttribute<u8>(m_Vertex.color[i], src, vdec.colors[i], 0, 4, true);
ReadVertexAttribute<u8>(m_Vertex.color[i].data(), src, vdec.colors[i], 0, 4, true);
}

for (int i = 0; i < 8; i++)
for (std::size_t i = 0; i < m_Vertex.texCoords.size(); i++)
{
ReadVertexAttribute<float>(m_Vertex.texCoords[i], src, vdec.texcoords[i], 0, 2, false);
ReadVertexAttribute<float>(m_Vertex.texCoords[i].data(), src, vdec.texcoords[i], 0, 2, false);

// the texmtr is stored as third component of the texCoord
if (vdec.texcoords[i].components >= 3)
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoBackends/Software/TransformUnit.cpp
Expand Up @@ -330,7 +330,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst)
// color
const LitChannel& colorchan = xfmem.color[chan];
if (colorchan.matsource)
std::memcpy(matcolor.data(), src->color[chan], sizeof(u32)); // vertex
matcolor = src->color[chan]; // vertex
else
std::memcpy(matcolor.data(), &xfmem.matColor[chan], sizeof(u32));

Expand Down Expand Up @@ -403,7 +403,7 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst)

// abgr -> rgba
const u32 rgba_color = Common::swap32(chancolor.data());
std::memcpy(dst->color[chan], &rgba_color, sizeof(u32));
std::memcpy(dst->color[chan].data(), &rgba_color, sizeof(u32));
}
}

Expand Down

0 comments on commit 24e919a

Please sign in to comment.