2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-07-06 16:16:07 +00:00
|
|
|
|
2016-09-24 09:16:52 +00:00
|
|
|
#include "VideoBackends/Software/TransformUnit.h"
|
|
|
|
|
2014-05-03 02:47:04 +00:00
|
|
|
#include <algorithm>
|
2017-08-06 23:01:08 +00:00
|
|
|
#include <array>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <cmath>
|
2017-08-06 23:01:08 +00:00
|
|
|
#include <cstring>
|
2010-07-06 16:16:07 +00:00
|
|
|
|
2016-09-24 09:16:52 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-09-24 09:16:52 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2017-03-03 19:43:52 +00:00
|
|
|
#include "Common/Swap.h"
|
2014-05-11 19:53:02 +00:00
|
|
|
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/Software/NativeVertexFormat.h"
|
|
|
|
#include "VideoBackends/Software/Vec3.h"
|
2010-07-06 16:16:07 +00:00
|
|
|
|
2015-10-09 18:50:36 +00:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
#include "VideoCommon/XFMemory.h"
|
2010-07-06 16:16:07 +00:00
|
|
|
|
|
|
|
namespace TransformUnit
|
|
|
|
{
|
2014-07-08 12:29:26 +00:00
|
|
|
static void MultiplyVec2Mat24(const Vec3& vec, const float* mat, Vec3& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] + mat[3];
|
|
|
|
result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] + mat[7];
|
2010-12-01 04:26:21 +00:00
|
|
|
result.z = 1.0f;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void MultiplyVec2Mat34(const Vec3& vec, const float* mat, Vec3& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] + mat[3];
|
|
|
|
result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] + mat[7];
|
|
|
|
result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] + mat[11];
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void MultiplyVec3Mat33(const Vec3& vec, const float* mat, Vec3& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z;
|
|
|
|
result.y = mat[3] * vec.x + mat[4] * vec.y + mat[5] * vec.z;
|
|
|
|
result.z = mat[6] * vec.x + mat[7] * vec.y + mat[8] * vec.z;
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void MultiplyVec3Mat24(const Vec3& vec, const float* mat, Vec3& result)
|
2010-12-01 04:26:21 +00:00
|
|
|
{
|
|
|
|
result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z + mat[3];
|
|
|
|
result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z + mat[7];
|
|
|
|
result.z = 1.0f;
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void MultiplyVec3Mat34(const Vec3& vec, const float* mat, Vec3& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z + mat[3];
|
|
|
|
result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z + mat[7];
|
|
|
|
result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] * vec.z + mat[11];
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:42:16 +00:00
|
|
|
static void MultipleVec3Perspective(const Vec3& vec, const Projection::Raw& proj, Vec4& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = proj[0] * vec.x + proj[1] * vec.z;
|
|
|
|
result.y = proj[2] * vec.y + proj[3] * vec.z;
|
|
|
|
// result.z = (proj[4] * vec.z + proj[5]);
|
|
|
|
result.z = (proj[4] * vec.z + proj[5]) * (1.0f - (float)1e-7);
|
|
|
|
result.w = -vec.z;
|
|
|
|
}
|
|
|
|
|
2019-06-20 11:42:16 +00:00
|
|
|
static void MultipleVec3Ortho(const Vec3& vec, const Projection::Raw& proj, Vec4& result)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
result.x = proj[0] * vec.x + proj[1];
|
|
|
|
result.y = proj[2] * vec.y + proj[3];
|
|
|
|
result.z = proj[4] * vec.z + proj[5];
|
|
|
|
result.w = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformPosition(const InputVertexData* src, OutputVertexData* dst)
|
|
|
|
{
|
2015-09-21 05:16:21 +00:00
|
|
|
const float* mat = &xfmem.posMatrices[src->posMtx * 4];
|
2010-07-06 16:16:07 +00:00
|
|
|
MultiplyVec3Mat34(src->position, mat, dst->mvPosition);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 00:01:42 +00:00
|
|
|
if (xfmem.projection.type == ProjectionType::Perspective)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-04-27 18:59:04 +00:00
|
|
|
MultipleVec3Perspective(dst->mvPosition, xfmem.projection.rawProjection,
|
|
|
|
dst->projectedPosition);
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-04-27 18:59:04 +00:00
|
|
|
MultipleVec3Ortho(dst->mvPosition, xfmem.projection.rawProjection, dst->projectedPosition);
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 05:03:34 +00:00
|
|
|
void TransformNormal(const InputVertexData* src, OutputVertexData* dst)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2015-09-21 05:16:21 +00:00
|
|
|
const float* mat = &xfmem.normalMatrices[(src->posMtx & 31) * 3];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2022-04-14 05:03:34 +00:00
|
|
|
MultiplyVec3Mat33(src->normal[0], mat, dst->normal[0]);
|
|
|
|
MultiplyVec3Mat33(src->normal[1], mat, dst->normal[1]);
|
|
|
|
MultiplyVec3Mat33(src->normal[2], mat, dst->normal[2]);
|
2022-04-20 00:46:20 +00:00
|
|
|
// The scale of the transform matrix is used to control the size of the emboss map effect, by
|
|
|
|
// changing the scale of the transformed binormals (which only get used by emboss map texgens).
|
|
|
|
// By normalising the first transformed normal (which is used by lighting calculations and needs
|
|
|
|
// to be unit length), the same transform matrix can do double duty, scaling for emboss mapping,
|
|
|
|
// and not scaling for lighting.
|
2022-04-14 05:03:34 +00:00
|
|
|
dst->normal[0].Normalize();
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 07:22:48 +00:00
|
|
|
static void TransformTexCoordRegular(const TexMtxInfo& texinfo, int coordNum,
|
2014-07-08 12:29:26 +00:00
|
|
|
const InputVertexData* srcVertex, OutputVertexData* dstVertex)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2016-03-29 09:49:15 +00:00
|
|
|
Vec3 src;
|
2010-07-06 16:16:07 +00:00
|
|
|
switch (texinfo.sourcerow)
|
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
case SourceRow::Geom:
|
2016-03-29 09:49:15 +00:00
|
|
|
src = srcVertex->position;
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case SourceRow::Normal:
|
2016-03-29 09:49:15 +00:00
|
|
|
src = srcVertex->normal[0];
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case SourceRow::BinormalT:
|
2016-03-29 09:49:15 +00:00
|
|
|
src = srcVertex->normal[1];
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case SourceRow::BinormalB:
|
2016-03-29 09:49:15 +00:00
|
|
|
src = srcVertex->normal[2];
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-02-11 00:01:42 +00:00
|
|
|
{
|
|
|
|
ASSERT(texinfo.sourcerow >= SourceRow::Tex0 && texinfo.sourcerow <= SourceRow::Tex7);
|
|
|
|
u32 texnum = static_cast<u32>(texinfo.sourcerow.Value()) - static_cast<u32>(SourceRow::Tex0);
|
|
|
|
src.x = srcVertex->texCoords[texnum][0];
|
|
|
|
src.y = srcVertex->texCoords[texnum][1];
|
2016-03-29 09:49:15 +00:00
|
|
|
src.z = 1.0f;
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-02-11 00:01:42 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-07-21 02:00:29 +00:00
|
|
|
// Convert NaNs to 1 - needed to fix eyelids in Shadow the Hedgehog during cutscenes
|
|
|
|
// See https://bugs.dolphin-emu.org/issues/11458
|
|
|
|
if (std::isnan(src.x))
|
|
|
|
src.x = 1;
|
|
|
|
if (std::isnan(src.y))
|
|
|
|
src.y = 1;
|
|
|
|
if (std::isnan(src.z))
|
|
|
|
src.z = 1;
|
|
|
|
|
2015-09-21 05:16:21 +00:00
|
|
|
const float* mat = &xfmem.posMatrices[srcVertex->texMtx[coordNum] * 4];
|
|
|
|
Vec3* dst = &dstVertex->texCoords[coordNum];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-02-11 00:01:42 +00:00
|
|
|
if (texinfo.projection == TexSize::ST)
|
2010-12-01 04:26:21 +00:00
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
if (texinfo.inputform == TexInputForm::AB11)
|
2016-03-29 09:49:15 +00:00
|
|
|
MultiplyVec2Mat24(src, mat, *dst);
|
2010-12-01 04:26:21 +00:00
|
|
|
else
|
2016-03-29 09:49:15 +00:00
|
|
|
MultiplyVec3Mat24(src, mat, *dst);
|
2010-12-01 04:26:21 +00:00
|
|
|
}
|
2021-02-11 00:01:42 +00:00
|
|
|
else // texinfo.projection == TexSize::STQ
|
2010-12-01 04:26:21 +00:00
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
if (texinfo.inputform == TexInputForm::AB11)
|
2016-03-29 09:49:15 +00:00
|
|
|
MultiplyVec2Mat34(src, mat, *dst);
|
2010-12-01 04:26:21 +00:00
|
|
|
else
|
2016-03-29 09:49:15 +00:00
|
|
|
MultiplyVec3Mat34(src, mat, *dst);
|
2010-12-01 04:26:21 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-04-27 18:59:04 +00:00
|
|
|
if (xfmem.dualTexTrans.enabled)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
Vec3 tempCoord;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-08-31 07:14:51 +00:00
|
|
|
// normalize
|
|
|
|
const PostMtxInfo& postInfo = xfmem.postMtxInfo[coordNum];
|
|
|
|
const float* postMat = &xfmem.postMatrices[postInfo.index * 4];
|
|
|
|
|
2021-01-04 07:22:48 +00:00
|
|
|
if (postInfo.normalize)
|
|
|
|
tempCoord = dst->Normalized();
|
2010-07-06 16:16:07 +00:00
|
|
|
else
|
2021-01-04 07:22:48 +00:00
|
|
|
tempCoord = *dst;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-01-04 07:22:48 +00:00
|
|
|
MultiplyVec3Mat34(tempCoord, postMat, *dst);
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
2016-03-13 07:38:44 +00:00
|
|
|
|
2016-08-31 07:44:36 +00:00
|
|
|
// When q is 0, the GameCube appears to have a special case
|
|
|
|
// This can be seen in devkitPro's neheGX Lesson08 example for Wii
|
|
|
|
// Makes differences in Rogue Squadron 3 (Hoth sky) and The Last Story (shadow culling)
|
2016-07-23 11:15:03 +00:00
|
|
|
if (dst->z == 0.0f)
|
2016-03-13 07:38:44 +00:00
|
|
|
{
|
2017-12-25 23:38:44 +00:00
|
|
|
dst->x = std::clamp(dst->x / 2.0f, -1.0f, 1.0f);
|
|
|
|
dst->y = std::clamp(dst->y / 2.0f, -1.0f, 1.0f);
|
2016-03-13 07:38:44 +00:00
|
|
|
}
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LightPointer
|
|
|
|
{
|
|
|
|
u32 reserved[3];
|
|
|
|
u8 color[4];
|
|
|
|
Vec3 cosatt;
|
|
|
|
Vec3 distatt;
|
|
|
|
Vec3 pos;
|
|
|
|
Vec3 dir;
|
|
|
|
};
|
|
|
|
|
2014-08-11 01:51:05 +00:00
|
|
|
static inline void AddScaledIntegerColor(const u8* src, float scale, Vec3& dst)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
dst.x += src[1] * scale;
|
|
|
|
dst.y += src[2] * scale;
|
|
|
|
dst.z += src[3] * scale;
|
|
|
|
}
|
|
|
|
|
2014-08-11 01:51:05 +00:00
|
|
|
static inline float SafeDivide(float n, float d)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
return (d == 0) ? (n > 0 ? 1 : 0) : n / d;
|
|
|
|
}
|
|
|
|
|
2015-02-08 00:16:04 +00:00
|
|
|
static float CalculateLightAttn(const LightPointer* light, Vec3* _ldir, const Vec3& normal,
|
|
|
|
const LitChannel& chan)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2015-02-07 17:40:21 +00:00
|
|
|
float attn = 1.0f;
|
2015-02-08 00:16:04 +00:00
|
|
|
Vec3& ldir = *_ldir;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-07 17:40:21 +00:00
|
|
|
switch (chan.attnfunc)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
case AttenuationFunc::None:
|
|
|
|
case AttenuationFunc::Dir:
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2015-02-07 17:40:21 +00:00
|
|
|
ldir = ldir.Normalized();
|
2015-02-08 00:16:04 +00:00
|
|
|
if (ldir == Vec3(0.0f, 0.0f, 0.0f))
|
|
|
|
ldir = normal;
|
2015-02-07 17:40:21 +00:00
|
|
|
break;
|
2013-04-14 03:54:02 +00:00
|
|
|
}
|
2021-02-11 00:01:42 +00:00
|
|
|
case AttenuationFunc::Spec:
|
2015-02-07 17:40:21 +00:00
|
|
|
{
|
|
|
|
ldir = ldir.Normalized();
|
|
|
|
attn = (ldir * normal) >= 0.0 ? std::max(0.0f, light->dir * normal) : 0;
|
|
|
|
Vec3 attLen = Vec3(1.0, attn, attn * attn);
|
|
|
|
Vec3 cosAttn = light->cosatt;
|
|
|
|
Vec3 distAttn = light->distatt;
|
2021-02-11 00:01:42 +00:00
|
|
|
if (chan.diffusefunc != DiffuseFunc::None)
|
2015-02-07 17:40:21 +00:00
|
|
|
distAttn = distAttn.Normalized();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-07 17:40:21 +00:00
|
|
|
attn = SafeDivide(std::max(0.0f, attLen * cosAttn), attLen * distAttn);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-11 00:01:42 +00:00
|
|
|
case AttenuationFunc::Spot:
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-08-11 01:34:34 +00:00
|
|
|
float dist2 = ldir.Length2();
|
2010-07-06 16:16:07 +00:00
|
|
|
float dist = sqrtf(dist2);
|
|
|
|
ldir = ldir / dist;
|
2014-05-03 02:47:04 +00:00
|
|
|
attn = std::max(0.0f, ldir * light->dir);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
float cosAtt = light->cosatt.x + (light->cosatt.y * attn) + (light->cosatt.z * attn * attn);
|
|
|
|
float distAtt = light->distatt.x + (light->distatt.y * dist) + (light->distatt.z * dist2);
|
2014-05-03 02:47:04 +00:00
|
|
|
attn = SafeDivide(std::max(0.0f, cosAtt), distAtt);
|
2015-02-07 17:40:21 +00:00
|
|
|
break;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
2015-02-07 17:40:21 +00:00
|
|
|
default:
|
2021-02-11 00:01:42 +00:00
|
|
|
PanicAlertFmt("Invalid attnfunc: {}", chan.attnfunc);
|
2015-02-07 17:40:21 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-08 00:16:04 +00:00
|
|
|
return attn;
|
|
|
|
}
|
|
|
|
|
2017-08-06 22:14:51 +00:00
|
|
|
static void LightColor(const Vec3& pos, const Vec3& normal, u8 lightNum, const LitChannel& chan,
|
2015-02-08 00:16:04 +00:00
|
|
|
Vec3& lightCol)
|
|
|
|
{
|
|
|
|
const LightPointer* light = (const LightPointer*)&xfmem.lights[lightNum];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-08 00:16:04 +00:00
|
|
|
Vec3 ldir = light->pos - pos;
|
|
|
|
float attn = CalculateLightAttn(light, &ldir, normal, chan);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-07 17:40:21 +00:00
|
|
|
float difAttn = ldir * normal;
|
|
|
|
switch (chan.diffusefunc)
|
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::None:
|
2015-02-07 17:40:21 +00:00
|
|
|
AddScaledIntegerColor(light->color, attn, lightCol);
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::Sign:
|
2015-02-07 17:40:21 +00:00
|
|
|
AddScaledIntegerColor(light->color, attn * difAttn, lightCol);
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::Clamp:
|
2015-02-07 17:40:21 +00:00
|
|
|
difAttn = std::max(0.0f, difAttn);
|
|
|
|
AddScaledIntegerColor(light->color, attn * difAttn, lightCol);
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-11 00:01:42 +00:00
|
|
|
PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc);
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 12:29:26 +00:00
|
|
|
static void LightAlpha(const Vec3& pos, const Vec3& normal, u8 lightNum, const LitChannel& chan,
|
|
|
|
float& lightCol)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-05-30 12:54:16 +00:00
|
|
|
const LightPointer* light = (const LightPointer*)&xfmem.lights[lightNum];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-07 17:40:21 +00:00
|
|
|
Vec3 ldir = light->pos - pos;
|
2015-02-08 00:16:04 +00:00
|
|
|
float attn = CalculateLightAttn(light, &ldir, normal, chan);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-07 17:40:21 +00:00
|
|
|
float difAttn = ldir * normal;
|
|
|
|
switch (chan.diffusefunc)
|
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::None:
|
2015-02-07 17:40:21 +00:00
|
|
|
lightCol += light->color[0] * attn;
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::Sign:
|
2015-02-07 17:40:21 +00:00
|
|
|
lightCol += light->color[0] * attn * difAttn;
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case DiffuseFunc::Clamp:
|
2015-02-07 17:40:21 +00:00
|
|
|
difAttn = std::max(0.0f, difAttn);
|
|
|
|
lightCol += light->color[0] * attn * difAttn;
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-11 00:01:42 +00:00
|
|
|
PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc);
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransformColor(const InputVertexData* src, OutputVertexData* dst)
|
|
|
|
{
|
2017-11-21 08:54:11 +00:00
|
|
|
for (u32 chan = 0; chan < NUM_XF_COLOR_CHANNELS; chan++)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
// abgr
|
2017-08-06 23:01:08 +00:00
|
|
|
std::array<u8, 4> matcolor;
|
|
|
|
std::array<u8, 4> chancolor;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
// color
|
2017-08-06 22:14:51 +00:00
|
|
|
const LitChannel& colorchan = xfmem.color[chan];
|
2021-02-11 00:01:42 +00:00
|
|
|
if (colorchan.matsource == MatSource::Vertex)
|
|
|
|
matcolor = src->color[chan];
|
2010-07-06 16:16:07 +00:00
|
|
|
else
|
2017-08-06 23:01:08 +00:00
|
|
|
std::memcpy(matcolor.data(), &xfmem.matColor[chan], sizeof(u32));
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
if (colorchan.enablelighting)
|
|
|
|
{
|
|
|
|
Vec3 lightCol;
|
2021-02-11 00:01:42 +00:00
|
|
|
if (colorchan.ambsource == AmbSource::Vertex)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
lightCol.x = src->color[chan][1];
|
|
|
|
lightCol.y = src->color[chan][2];
|
|
|
|
lightCol.z = src->color[chan][3];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 22:14:51 +00:00
|
|
|
const u8* ambColor = reinterpret_cast<u8*>(&xfmem.ambColor[chan]);
|
2010-07-06 16:16:07 +00:00
|
|
|
lightCol.x = ambColor[1];
|
|
|
|
lightCol.y = ambColor[2];
|
|
|
|
lightCol.z = ambColor[3];
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
u8 mask = colorchan.GetFullLightMask();
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
if (mask & (1 << i))
|
|
|
|
LightColor(dst->mvPosition, dst->normal[0], i, colorchan, lightCol);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-12-25 23:38:44 +00:00
|
|
|
int light_x = std::clamp(static_cast<int>(lightCol.x), 0, 255);
|
|
|
|
int light_y = std::clamp(static_cast<int>(lightCol.y), 0, 255);
|
|
|
|
int light_z = std::clamp(static_cast<int>(lightCol.z), 0, 255);
|
2014-05-11 19:53:02 +00:00
|
|
|
chancolor[1] = (matcolor[1] * (light_x + (light_x >> 7))) >> 8;
|
|
|
|
chancolor[2] = (matcolor[2] * (light_y + (light_y >> 7))) >> 8;
|
|
|
|
chancolor[3] = (matcolor[3] * (light_z + (light_z >> 7))) >> 8;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-06 23:01:08 +00:00
|
|
|
chancolor = matcolor;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
// alpha
|
2017-08-06 22:14:51 +00:00
|
|
|
const LitChannel& alphachan = xfmem.alpha[chan];
|
2021-02-11 00:01:42 +00:00
|
|
|
if (alphachan.matsource == MatSource::Vertex)
|
|
|
|
matcolor[0] = src->color[chan][0];
|
2010-07-06 16:16:07 +00:00
|
|
|
else
|
2014-04-27 18:59:04 +00:00
|
|
|
matcolor[0] = xfmem.matColor[chan] & 0xff;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-04-27 18:59:04 +00:00
|
|
|
if (xfmem.alpha[chan].enablelighting)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
float lightCol;
|
2021-02-11 00:01:42 +00:00
|
|
|
if (alphachan.ambsource == AmbSource::Vertex)
|
|
|
|
lightCol = src->color[chan][0];
|
2010-07-06 16:16:07 +00:00
|
|
|
else
|
2017-08-06 23:01:08 +00:00
|
|
|
lightCol = static_cast<float>(xfmem.ambColor[chan] & 0xff);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
u8 mask = alphachan.GetFullLightMask();
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
if (mask & (1 << i))
|
|
|
|
LightAlpha(dst->mvPosition, dst->normal[0], i, alphachan, lightCol);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-12-25 23:38:44 +00:00
|
|
|
int light_a = std::clamp(static_cast<int>(lightCol), 0, 255);
|
2014-05-11 19:53:02 +00:00
|
|
|
chancolor[0] = (matcolor[0] * (light_a + (light_a >> 7))) >> 8;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chancolor[0] = matcolor[0];
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
// abgr -> rgba
|
2017-08-06 23:01:08 +00:00
|
|
|
const u32 rgba_color = Common::swap32(chancolor.data());
|
2017-08-19 15:08:09 +00:00
|
|
|
std::memcpy(dst->color[chan].data(), &rgba_color, sizeof(u32));
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 07:22:48 +00:00
|
|
|
void TransformTexCoord(const InputVertexData* src, OutputVertexData* dst)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-04-27 18:59:04 +00:00
|
|
|
for (u32 coordNum = 0; coordNum < xfmem.numTexGen.numTexGens; coordNum++)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-04-27 18:59:04 +00:00
|
|
|
const TexMtxInfo& texinfo = xfmem.texMtxInfo[coordNum];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
switch (texinfo.texgentype)
|
|
|
|
{
|
2021-02-11 00:01:42 +00:00
|
|
|
case TexGenType::Regular:
|
2021-01-04 07:22:48 +00:00
|
|
|
TransformTexCoordRegular(texinfo, coordNum, src, dst);
|
2010-07-06 16:16:07 +00:00
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case TexGenType::EmbossMap:
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
2014-05-30 12:54:16 +00:00
|
|
|
const LightPointer* light = (const LightPointer*)&xfmem.lights[texinfo.embosslightshift];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-11 01:34:34 +00:00
|
|
|
Vec3 ldir = (light->pos - dst->mvPosition).Normalized();
|
2010-07-06 16:16:07 +00:00
|
|
|
float d1 = ldir * dst->normal[1];
|
|
|
|
float d2 = ldir * dst->normal[2];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2010-07-06 16:16:07 +00:00
|
|
|
dst->texCoords[coordNum].x = dst->texCoords[texinfo.embosssourceshift].x + d1;
|
|
|
|
dst->texCoords[coordNum].y = dst->texCoords[texinfo.embosssourceshift].y + d2;
|
|
|
|
dst->texCoords[coordNum].z = dst->texCoords[texinfo.embosssourceshift].z;
|
|
|
|
}
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case TexGenType::Color0:
|
|
|
|
ASSERT(texinfo.inputform == TexInputForm::AB11);
|
2010-07-06 16:16:07 +00:00
|
|
|
dst->texCoords[coordNum].x = (float)dst->color[0][0] / 255.0f;
|
|
|
|
dst->texCoords[coordNum].y = (float)dst->color[0][1] / 255.0f;
|
|
|
|
dst->texCoords[coordNum].z = 1.0f;
|
|
|
|
break;
|
2021-02-11 00:01:42 +00:00
|
|
|
case TexGenType::Color1:
|
|
|
|
ASSERT(texinfo.inputform == TexInputForm::AB11);
|
2010-07-06 16:16:07 +00:00
|
|
|
dst->texCoords[coordNum].x = (float)dst->color[1][0] / 255.0f;
|
|
|
|
dst->texCoords[coordNum].y = (float)dst->color[1][1] / 255.0f;
|
|
|
|
dst->texCoords[coordNum].z = 1.0f;
|
|
|
|
break;
|
|
|
|
default:
|
2021-02-11 00:01:42 +00:00
|
|
|
ERROR_LOG_FMT(VIDEO, "Bad tex gen type {}", texinfo.texgentype);
|
2020-11-09 08:13:59 +00:00
|
|
|
break;
|
2010-07-06 16:16:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-04-27 18:59:04 +00:00
|
|
|
for (u32 coordNum = 0; coordNum < xfmem.numTexGen.numTexGens; coordNum++)
|
2010-07-06 16:16:07 +00:00
|
|
|
{
|
|
|
|
dst->texCoords[coordNum][0] *= (bpmem.texcoords[coordNum].s.scale_minus_1 + 1);
|
|
|
|
dst->texCoords[coordNum][1] *= (bpmem.texcoords[coordNum].t.scale_minus_1 + 1);
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace TransformUnit
|