Skip to content

Commit

Permalink
Convert XFMemory to BitField and enum class
Browse files Browse the repository at this point in the history
Additionally a new ClipDisable union has been added (though it is not currently used by Dolphin).
  • Loading branch information
Pokechu22 committed Mar 7, 2021
1 parent 953e094 commit aab81d5
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 202 deletions.
88 changes: 45 additions & 43 deletions Source/Core/VideoBackends/Software/TransformUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void TransformPosition(const InputVertexData* src, OutputVertexData* dst)
const float* mat = &xfmem.posMatrices[src->posMtx * 4];
MultiplyVec3Mat34(src->position, mat, dst->mvPosition);

if (xfmem.projection.type == GX_PERSPECTIVE)
if (xfmem.projection.type == ProjectionType::Perspective)
{
MultipleVec3Perspective(dst->mvPosition, xfmem.projection.rawProjection,
dst->projectedPosition);
Expand Down Expand Up @@ -115,39 +115,42 @@ static void TransformTexCoordRegular(const TexMtxInfo& texinfo, int coordNum,
Vec3 src;
switch (texinfo.sourcerow)
{
case XF_SRCGEOM_INROW:
case SourceRow::Geom:
src = srcVertex->position;
break;
case XF_SRCNORMAL_INROW:
case SourceRow::Normal:
src = srcVertex->normal[0];
break;
case XF_SRCBINORMAL_T_INROW:
case SourceRow::BinormalT:
src = srcVertex->normal[1];
break;
case XF_SRCBINORMAL_B_INROW:
case SourceRow::BinormalB:
src = srcVertex->normal[2];
break;
default:
ASSERT(texinfo.sourcerow >= XF_SRCTEX0_INROW && texinfo.sourcerow <= XF_SRCTEX7_INROW);
src.x = srcVertex->texCoords[texinfo.sourcerow - XF_SRCTEX0_INROW][0];
src.y = srcVertex->texCoords[texinfo.sourcerow - XF_SRCTEX0_INROW][1];
{
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];
src.z = 1.0f;
break;
}
}

const float* mat = &xfmem.posMatrices[srcVertex->texMtx[coordNum] * 4];
Vec3* dst = &dstVertex->texCoords[coordNum];

if (texinfo.projection == XF_TEXPROJ_ST)
if (texinfo.projection == TexSize::ST)
{
if (texinfo.inputform == XF_TEXINPUT_AB11)
if (texinfo.inputform == TexInputForm::AB11)
MultiplyVec2Mat24(src, mat, *dst);
else
MultiplyVec3Mat24(src, mat, *dst);
}
else // texinfo.projection == XF_TEXPROJ_STQ
else // texinfo.projection == TexSize::STQ
{
if (texinfo.inputform == XF_TEXINPUT_AB11)
if (texinfo.inputform == TexInputForm::AB11)
MultiplyVec2Mat34(src, mat, *dst);
else
MultiplyVec3Mat34(src, mat, *dst);
Expand Down Expand Up @@ -209,28 +212,28 @@ static float CalculateLightAttn(const LightPointer* light, Vec3* _ldir, const Ve

switch (chan.attnfunc)
{
case LIGHTATTN_NONE:
case LIGHTATTN_DIR:
case AttenuationFunc::None:
case AttenuationFunc::Dir:
{
ldir = ldir.Normalized();
if (ldir == Vec3(0.0f, 0.0f, 0.0f))
ldir = normal;
break;
}
case LIGHTATTN_SPEC:
case AttenuationFunc::Spec:
{
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;
if (chan.diffusefunc != LIGHTDIF_NONE)
if (chan.diffusefunc != DiffuseFunc::None)
distAttn = distAttn.Normalized();

attn = SafeDivide(std::max(0.0f, attLen * cosAttn), attLen * distAttn);
break;
}
case LIGHTATTN_SPOT:
case AttenuationFunc::Spot:
{
float dist2 = ldir.Length2();
float dist = sqrtf(dist2);
Expand All @@ -243,7 +246,7 @@ static float CalculateLightAttn(const LightPointer* light, Vec3* _ldir, const Ve
break;
}
default:
PanicAlertFmt("LightColor");
PanicAlertFmt("Invalid attnfunc: {}", chan.attnfunc);
}

return attn;
Expand All @@ -260,18 +263,18 @@ static void LightColor(const Vec3& pos, const Vec3& normal, u8 lightNum, const L
float difAttn = ldir * normal;
switch (chan.diffusefunc)
{
case LIGHTDIF_NONE:
case DiffuseFunc::None:
AddScaledIntegerColor(light->color, attn, lightCol);
break;
case LIGHTDIF_SIGN:
case DiffuseFunc::Sign:
AddScaledIntegerColor(light->color, attn * difAttn, lightCol);
break;
case LIGHTDIF_CLAMP:
case DiffuseFunc::Clamp:
difAttn = std::max(0.0f, difAttn);
AddScaledIntegerColor(light->color, attn * difAttn, lightCol);
break;
default:
ASSERT(0);
PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc);
}
}

Expand All @@ -286,18 +289,18 @@ static void LightAlpha(const Vec3& pos, const Vec3& normal, u8 lightNum, const L
float difAttn = ldir * normal;
switch (chan.diffusefunc)
{
case LIGHTDIF_NONE:
case DiffuseFunc::None:
lightCol += light->color[0] * attn;
break;
case LIGHTDIF_SIGN:
case DiffuseFunc::Sign:
lightCol += light->color[0] * attn * difAttn;
break;
case LIGHTDIF_CLAMP:
case DiffuseFunc::Clamp:
difAttn = std::max(0.0f, difAttn);
lightCol += light->color[0] * attn * difAttn;
break;
default:
ASSERT(0);
PanicAlertFmt("Invalid diffusefunc: {}", chan.attnfunc);
}
}

Expand All @@ -311,17 +314,16 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst)

// color
const LitChannel& colorchan = xfmem.color[chan];
if (colorchan.matsource)
matcolor = src->color[chan]; // vertex
if (colorchan.matsource == MatSource::Vertex)
matcolor = src->color[chan];
else
std::memcpy(matcolor.data(), &xfmem.matColor[chan], sizeof(u32));

if (colorchan.enablelighting)
{
Vec3 lightCol;
if (colorchan.ambsource)
if (colorchan.ambsource == AmbSource::Vertex)
{
// vertex
lightCol.x = src->color[chan][1];
lightCol.y = src->color[chan][2];
lightCol.z = src->color[chan][3];
Expand Down Expand Up @@ -355,16 +357,16 @@ void TransformColor(const InputVertexData* src, OutputVertexData* dst)

// alpha
const LitChannel& alphachan = xfmem.alpha[chan];
if (alphachan.matsource)
matcolor[0] = src->color[chan][0]; // vertex
if (alphachan.matsource == MatSource::Vertex)
matcolor[0] = src->color[chan][0];
else
matcolor[0] = xfmem.matColor[chan] & 0xff;

if (xfmem.alpha[chan].enablelighting)
{
float lightCol;
if (alphachan.ambsource)
lightCol = src->color[chan][0]; // vertex
if (alphachan.ambsource == AmbSource::Vertex)
lightCol = src->color[chan][0];
else
lightCol = static_cast<float>(xfmem.ambColor[chan] & 0xff);

Expand Down Expand Up @@ -397,10 +399,10 @@ void TransformTexCoord(const InputVertexData* src, OutputVertexData* dst)

switch (texinfo.texgentype)
{
case XF_TEXGEN_REGULAR:
case TexGenType::Regular:
TransformTexCoordRegular(texinfo, coordNum, src, dst);
break;
case XF_TEXGEN_EMBOSS_MAP:
case TexGenType::EmbossMap:
{
const LightPointer* light = (const LightPointer*)&xfmem.lights[texinfo.embosslightshift];

Expand All @@ -413,22 +415,22 @@ void TransformTexCoord(const InputVertexData* src, OutputVertexData* dst)
dst->texCoords[coordNum].z = dst->texCoords[texinfo.embosssourceshift].z;
}
break;
case XF_TEXGEN_COLOR_STRGBC0:
ASSERT(texinfo.sourcerow == XF_SRCCOLORS_INROW);
ASSERT(texinfo.inputform == XF_TEXINPUT_AB11);
case TexGenType::Color0:
ASSERT(texinfo.sourcerow == SourceRow::Colors);
ASSERT(texinfo.inputform == TexInputForm::AB11);
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;
case XF_TEXGEN_COLOR_STRGBC1:
ASSERT(texinfo.sourcerow == XF_SRCCOLORS_INROW);
ASSERT(texinfo.inputform == XF_TEXINPUT_AB11);
case TexGenType::Color1:
ASSERT(texinfo.sourcerow == SourceRow::Colors);
ASSERT(texinfo.inputform == TexInputForm::AB11);
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:
ERROR_LOG_FMT(VIDEO, "Bad tex gen type {}", texinfo.texgentype.Value());
ERROR_LOG_FMT(VIDEO, "Bad tex gen type {}", texinfo.texgentype);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoCommon/GeometryShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void GeometryShaderManager::SetConstants()
{
s_projection_changed = false;

if (xfmem.projection.type == GX_PERSPECTIVE)
if (xfmem.projection.type == ProjectionType::Perspective)
{
float offset = (g_ActiveConfig.iStereoDepth / 1000.0f) *
(g_ActiveConfig.iStereoDepthPercentage / 100.0f);
Expand Down
41 changes: 22 additions & 19 deletions Source/Core/VideoCommon/LightingShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,32 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
const char* swizzle = alpha ? "a" : "rgb";
const char* swizzle_components = (alpha) ? "" : "3";

const u32 attnfunc = (uid_data.attnfunc >> (2 * litchan_index)) & 0x3;
const u32 diffusefunc = (uid_data.diffusefunc >> (2 * litchan_index)) & 0x3;
const auto attnfunc =
static_cast<AttenuationFunc>((uid_data.attnfunc >> (2 * litchan_index)) & 0x3);
const auto diffusefunc =
static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * litchan_index)) & 0x3);

switch (attnfunc)
{
case LIGHTATTN_NONE:
case LIGHTATTN_DIR:
case AttenuationFunc::None:
case AttenuationFunc::Dir:
object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
object.Write("attn = 1.0;\n");
object.Write("if (length(ldir) == 0.0)\n\t ldir = _norm0;\n");
break;
case LIGHTATTN_SPEC:
case AttenuationFunc::Spec:
object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
object.Write("attn = (dot(_norm0, ldir) >= 0.0) ? max(0.0, dot(_norm0, " LIGHT_DIR
".xyz)) : 0.0;\n",
LIGHT_DIR_PARAMS(index));
object.Write("cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index));
object.Write("distAttn = {}(" LIGHT_DISTATT ".xyz);\n",
(diffusefunc == LIGHTDIF_NONE) ? "" : "normalize", LIGHT_DISTATT_PARAMS(index));
(diffusefunc == DiffuseFunc::None) ? "" : "normalize",
LIGHT_DISTATT_PARAMS(index));
object.Write("attn = max(0.0f, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, "
"float3(1.0, attn, attn*attn));\n");
break;
case LIGHTATTN_SPOT:
case AttenuationFunc::Spot:
object.Write("ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index));
object.Write("dist2 = dot(ldir, ldir);\n"
"dist = sqrt(dist2);\n"
Expand All @@ -56,14 +59,14 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d

switch (diffusefunc)
{
case LIGHTDIF_NONE:
case DiffuseFunc::None:
object.Write("lacc.{} += int{}(round(attn * float{}(" LIGHT_COL ")));\n", swizzle,
swizzle_components, swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
break;
case LIGHTDIF_SIGN:
case LIGHTDIF_CLAMP:
case DiffuseFunc::Sign:
case DiffuseFunc::Clamp:
object.Write("lacc.{} += int{}(round(attn * {}dot(ldir, _norm0)) * float{}(" LIGHT_COL ")));\n",
swizzle, swizzle_components, diffusefunc != LIGHTDIF_SIGN ? "max(0.0," : "(",
swizzle, swizzle_components, diffusefunc != DiffuseFunc::Sign ? "max(0.0," : "(",
swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
break;
default:
Expand Down Expand Up @@ -151,23 +154,23 @@ void GetLightingShaderUid(LightingUidData& uid_data)
{
for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
{
uid_data.matsource |= xfmem.color[j].matsource << j;
uid_data.matsource |= xfmem.alpha[j].matsource << (j + 2);
uid_data.matsource |= static_cast<u32>(xfmem.color[j].matsource.Value()) << j;
uid_data.matsource |= static_cast<u32>(xfmem.alpha[j].matsource.Value()) << (j + 2);
uid_data.enablelighting |= xfmem.color[j].enablelighting << j;
uid_data.enablelighting |= xfmem.alpha[j].enablelighting << (j + 2);

if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
{
uid_data.ambsource |= xfmem.color[j].ambsource << j;
uid_data.attnfunc |= xfmem.color[j].attnfunc << (2 * j);
uid_data.diffusefunc |= xfmem.color[j].diffusefunc << (2 * j);
uid_data.ambsource |= static_cast<u32>(xfmem.color[j].ambsource.Value()) << j;
uid_data.attnfunc |= static_cast<u32>(xfmem.color[j].attnfunc.Value()) << (2 * j);
uid_data.diffusefunc |= static_cast<u32>(xfmem.color[j].diffusefunc.Value()) << (2 * j);
uid_data.light_mask |= xfmem.color[j].GetFullLightMask() << (8 * j);
}
if ((uid_data.enablelighting & (1 << (j + 2))) != 0) // Alpha lights
{
uid_data.ambsource |= xfmem.alpha[j].ambsource << (j + 2);
uid_data.attnfunc |= xfmem.alpha[j].attnfunc << (2 * (j + 2));
uid_data.diffusefunc |= xfmem.alpha[j].diffusefunc << (2 * (j + 2));
uid_data.ambsource |= static_cast<u32>(xfmem.alpha[j].ambsource.Value()) << (j + 2);
uid_data.attnfunc |= static_cast<u32>(xfmem.alpha[j].attnfunc.Value()) << (2 * (j + 2));
uid_data.diffusefunc |= static_cast<u32>(xfmem.alpha[j].diffusefunc.Value()) << (2 * (j + 2));
uid_data.light_mask |= xfmem.alpha[j].GetFullLightMask() << (8 * (j + 2));
}
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/VideoCommon/PixelShaderGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ PixelShaderUid GetPixelShaderUid()
for (unsigned int i = 0; i < uid_data->genMode_numtexgens; ++i)
{
// optional perspective divides
uid_data->texMtxInfo_n_projection |= xfmem.texMtxInfo[i].projection << i;
uid_data->texMtxInfo_n_projection |= static_cast<u32>(xfmem.texMtxInfo[i].projection.Value())
<< i;
}
}

Expand Down
16 changes: 8 additions & 8 deletions Source/Core/VideoCommon/UberShaderCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ void WriteLightingFunction(ShaderCode& out)
" float dist, dist2, attn;\n"
"\n"
" switch (attnfunc) {{\n");
out.Write(" case {}u: // LIGNTATTN_NONE\n", LIGHTATTN_NONE);
out.Write(" case {}u: // LIGHTATTN_DIR\n", LIGHTATTN_DIR);
out.Write(" case {:s}:\n", AttenuationFunc::None);
out.Write(" case {:s}:\n", AttenuationFunc::Dir);
out.Write(" ldir = normalize(" I_LIGHTS "[index].pos.xyz - pos.xyz);\n"
" attn = 1.0;\n"
" if (length(ldir) == 0.0)\n"
" ldir = normal;\n"
" break;\n\n");
out.Write(" case {}u: // LIGHTATTN_SPEC\n", LIGHTATTN_SPEC);
out.Write(" case {:s}:\n", AttenuationFunc::Spec);
out.Write(" ldir = normalize(" I_LIGHTS "[index].pos.xyz - pos.xyz);\n"
" attn = (dot(normal, ldir) >= 0.0) ? max(0.0, dot(normal, " I_LIGHTS
"[index].dir.xyz)) : 0.0;\n"
" cosAttn = " I_LIGHTS "[index].cosatt.xyz;\n");
out.Write(" if (diffusefunc == {}u) // LIGHTDIF_NONE\n", LIGHTDIF_NONE);
out.Write(" if (diffusefunc == {:s})\n", DiffuseFunc::None);
out.Write(" distAttn = " I_LIGHTS "[index].distatt.xyz;\n"
" else\n"
" distAttn = normalize(" I_LIGHTS "[index].distatt.xyz);\n"
" attn = max(0.0, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, "
"float3(1.0, attn, attn*attn));\n"
" break;\n\n");
out.Write(" case {}u: // LIGHTATTN_SPOT\n", LIGHTATTN_SPOT);
out.Write(" case {:s}:\n", AttenuationFunc::Spot);
out.Write(" ldir = " I_LIGHTS "[index].pos.xyz - pos.xyz;\n"
" dist2 = dot(ldir, ldir);\n"
" dist = sqrt(dist2);\n"
Expand All @@ -75,12 +75,12 @@ void WriteLightingFunction(ShaderCode& out)
" }}\n"
"\n"
" switch (diffusefunc) {{\n");
out.Write(" case {}u: // LIGHTDIF_NONE\n", LIGHTDIF_NONE);
out.Write(" case {:s}:\n", DiffuseFunc::None);
out.Write(" return int4(round(attn * float4(" I_LIGHTS "[index].color)));\n\n");
out.Write(" case {}u: // LIGHTDIF_SIGN\n", LIGHTDIF_SIGN);
out.Write(" case {:s}:\n", DiffuseFunc::Sign);
out.Write(" return int4(round(attn * dot(ldir, normal) * float4(" I_LIGHTS
"[index].color)));\n\n");
out.Write(" case {}u: // LIGHTDIF_CLAMP\n", LIGHTDIF_CLAMP);
out.Write(" case {:s}:\n", DiffuseFunc::Clamp);
out.Write(" return int4(round(attn * max(0.0, dot(ldir, normal)) * float4(" I_LIGHTS
"[index].color)));\n\n");
out.Write(" default:\n"
Expand Down
Loading

0 comments on commit aab81d5

Please sign in to comment.