Indent lighting code and use proper local variables

This commit is contained in:
Pokechu22 2022-02-25 16:27:21 -08:00
commit 35d02401d9
3 changed files with 46 additions and 53 deletions

View file

@ -13,6 +13,7 @@
static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_data, int index,
AttenuationFunc attn_func, DiffuseFunc diffuse_func, bool alpha)
{
object.Write(" {{ // {} light {}\n", alpha ? "Alpha" : "Color", index);
const char* swizzle = alpha ? "a" : "rgb";
const char* swizzle_components = (alpha) ? "" : "3";
@ -20,27 +21,29 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
{
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(" float3 ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
LIGHT_POS_PARAMS(index));
object.Write(" float attn = 1.0;\n");
break;
case AttenuationFunc::Spec:
object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
object.Write("attn = (dot(_normal, ldir) >= 0.0) ? max(0.0, dot(_normal, " LIGHT_DIR
object.Write(" float3 ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
LIGHT_POS_PARAMS(index));
object.Write(" float attn = (dot(_normal, ldir) >= 0.0) ? max(0.0, dot(_normal, " 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",
object.Write(" float3 cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index));
object.Write(" float3 distAttn = {}(" LIGHT_DISTATT ".xyz);\n",
(diffuse_func == 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 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"
object.Write(" float3 ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index));
object.Write(" float dist2 = dot(ldir, ldir);\n"
" float dist = sqrt(dist2);\n"
" ldir = ldir / dist;\n"
"attn = max(0.0, dot(ldir, " LIGHT_DIR ".xyz));\n",
" float attn = max(0.0, dot(ldir, " LIGHT_DIR ".xyz));\n",
LIGHT_DIR_PARAMS(index));
// attn*attn may overflow
object.Write(" attn = max(0.0, " LIGHT_COSATT ".x + " LIGHT_COSATT ".y*attn + " LIGHT_COSATT
@ -67,7 +70,7 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
ASSERT(false);
}
object.Write("\n");
object.Write(" }}\n");
}
// vertex shader
@ -109,13 +112,13 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
if (color_enable)
{
if (color_ambsource == AmbSource::Vertex)
object.Write("lacc = int4(round({}{} * 255.0));\n", in_color_name, chan);
object.Write(" int4 lacc = int4(round({}{} * 255.0));\n", in_color_name, chan);
else // from ambient color register
object.Write("lacc = {}[{}];\n", I_MATERIALS, chan);
object.Write(" int4 lacc = {}[{}];\n", I_MATERIALS, chan);
}
else
{
object.Write("lacc = int4(255, 255, 255, 255);\n");
object.Write(" int4 lacc = int4(255, 255, 255, 255);\n");
}
// check if alpha is different
@ -152,12 +155,11 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
{
if ((light_mask & (1 << light)) != 0)
{
object.Write("// Color light {}\n", light);
GenerateLightShader(object, uid_data, light, attnfunc, diffusefunc, false);
}
}
}
if (alpha_enable) // Alpha lights
if (alpha_enable)
{
const auto attnfunc = static_cast<AttenuationFunc>((uid_data.attnfunc >> (2 * chan_a)) & 3);
const auto diffusefunc = static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * chan_a)) & 3);
@ -170,7 +172,6 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
{
if ((light_mask & (1 << light)) != 0)
{
object.Write("// Alpha light {}\n", light);
GenerateLightShader(object, uid_data, light, attnfunc, diffusefunc, true);
}
}

View file

@ -955,10 +955,6 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos
out.Write("\tfloat3 _normal = normalize(Normal.xyz);\n\n"
"\tfloat3 pos = WorldPos;\n");
out.Write("\tint4 lacc;\n"
"\tfloat3 ldir, h, cosAttn, distAttn;\n"
"\tfloat dist, dist2, attn;\n");
// TODO: Our current constant usage code isn't able to handle more than one buffer.
// So we can't mark the VS constant as used here. But keep them here as reference.
// out.SetConstantsUsed(C_PLIGHT_COLORS, C_PLIGHT_COLORS+7); // TODO: Can be optimized further

View file

@ -366,10 +366,6 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
out.Write("o.pos = float4(dot(" I_PROJECTION "[0], pos), dot(" I_PROJECTION
"[1], pos), dot(" I_PROJECTION "[2], pos), dot(" I_PROJECTION "[3], pos));\n");
out.Write("int4 lacc;\n"
"float3 ldir, h, cosAttn, distAttn;\n"
"float dist, dist2, attn;\n");
GenerateLightingShaderCode(out, uid_data->lighting, "vertex_color_", "o.colors_");
// transform texcoords
@ -433,7 +429,7 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
case TexGenType::EmbossMap: // calculate tex coords into bump map
// transform the light dir into tangent space
out.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
out.Write("float3 ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
LIGHT_POS_PARAMS(texinfo.embosslightshift));
out.Write(
"o.tex{}.xyz = o.tex{}.xyz + float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n",