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, static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_data, int index,
AttenuationFunc attn_func, DiffuseFunc diffuse_func, bool alpha) 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 = alpha ? "a" : "rgb";
const char* swizzle_components = (alpha) ? "" : "3"; const char* swizzle_components = (alpha) ? "" : "3";
@ -20,30 +21,32 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
{ {
case AttenuationFunc::None: case AttenuationFunc::None:
case AttenuationFunc::Dir: case AttenuationFunc::Dir:
object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index)); object.Write(" float3 ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
object.Write("attn = 1.0;\n"); LIGHT_POS_PARAMS(index));
object.Write(" float attn = 1.0;\n");
break; break;
case AttenuationFunc::Spec: case AttenuationFunc::Spec:
object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index)); object.Write(" float3 ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n",
object.Write("attn = (dot(_normal, ldir) >= 0.0) ? max(0.0, dot(_normal, " LIGHT_DIR LIGHT_POS_PARAMS(index));
object.Write(" float attn = (dot(_normal, ldir) >= 0.0) ? max(0.0, dot(_normal, " LIGHT_DIR
".xyz)) : 0.0;\n", ".xyz)) : 0.0;\n",
LIGHT_DIR_PARAMS(index)); LIGHT_DIR_PARAMS(index));
object.Write("cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index)); object.Write(" float3 cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index));
object.Write("distAttn = {}(" LIGHT_DISTATT ".xyz);\n", object.Write(" float3 distAttn = {}(" LIGHT_DISTATT ".xyz);\n",
(diffuse_func == DiffuseFunc::None) ? "" : "normalize", (diffuse_func == DiffuseFunc::None) ? "" : "normalize",
LIGHT_DISTATT_PARAMS(index)); LIGHT_DISTATT_PARAMS(index));
object.Write("attn = max(0.0f, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, " object.Write(" attn = max(0.0f, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, "
"float3(1.0, attn, attn*attn));\n"); "float3(1.0, attn, attn*attn));\n");
break; break;
case AttenuationFunc::Spot: case AttenuationFunc::Spot:
object.Write("ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index)); object.Write(" float3 ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index));
object.Write("dist2 = dot(ldir, ldir);\n" object.Write(" float dist2 = dot(ldir, ldir);\n"
"dist = sqrt(dist2);\n" " float dist = sqrt(dist2);\n"
"ldir = ldir / dist;\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)); LIGHT_DIR_PARAMS(index));
// attn*attn may overflow // attn*attn may overflow
object.Write("attn = max(0.0, " LIGHT_COSATT ".x + " LIGHT_COSATT ".y*attn + " LIGHT_COSATT object.Write(" attn = max(0.0, " LIGHT_COSATT ".x + " LIGHT_COSATT ".y*attn + " LIGHT_COSATT
".z*attn*attn) / dot(" LIGHT_DISTATT ".xyz, float3(1.0,dist,dist2));\n", ".z*attn*attn) / dot(" LIGHT_DISTATT ".xyz, float3(1.0,dist,dist2));\n",
LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index),
LIGHT_DISTATT_PARAMS(index)); LIGHT_DISTATT_PARAMS(index));
@ -53,12 +56,12 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
switch (diffuse_func) switch (diffuse_func)
{ {
case DiffuseFunc::None: case DiffuseFunc::None:
object.Write("lacc.{} += int{}(round(attn * float{}(" LIGHT_COL ")));\n", swizzle, object.Write(" lacc.{} += int{}(round(attn * float{}(" LIGHT_COL ")));\n", swizzle,
swizzle_components, swizzle_components, LIGHT_COL_PARAMS(index, swizzle)); swizzle_components, swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
break; break;
case DiffuseFunc::Sign: case DiffuseFunc::Sign:
case DiffuseFunc::Clamp: case DiffuseFunc::Clamp:
object.Write("lacc.{} += int{}(round(attn * {}dot(ldir, _normal)) * float{}(" LIGHT_COL object.Write(" lacc.{} += int{}(round(attn * {}dot(ldir, _normal)) * float{}(" LIGHT_COL
")));\n", ")));\n",
swizzle, swizzle_components, diffuse_func != DiffuseFunc::Sign ? "max(0.0," : "(", swizzle, swizzle_components, diffuse_func != DiffuseFunc::Sign ? "max(0.0," : "(",
swizzle_components, LIGHT_COL_PARAMS(index, swizzle)); swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
@ -67,7 +70,7 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
ASSERT(false); ASSERT(false);
} }
object.Write("\n"); object.Write(" }}\n");
} }
// vertex shader // vertex shader
@ -91,52 +94,52 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
const bool alpha_enable = ((uid_data.enablelighting >> chan_a) & 1) != 0; const bool alpha_enable = ((uid_data.enablelighting >> chan_a) & 1) != 0;
object.Write("{{\n" object.Write("{{\n"
"// Lighting for channel {}:\n" " // Lighting for channel {}:\n"
"// Color material source: {}\n" " // Color material source: {}\n"
"// Color ambient source: {}\n" " // Color ambient source: {}\n"
"// Color lighting enabled: {}\n" " // Color lighting enabled: {}\n"
"// Alpha material source: {}\n" " // Alpha material source: {}\n"
"// Alpha ambient source: {}\n" " // Alpha ambient source: {}\n"
"// Alpha lighting enabled: {}\n", " // Alpha lighting enabled: {}\n",
chan, color_matsource, color_ambsource, color_enable, alpha_matsource, chan, color_matsource, color_ambsource, color_enable, alpha_matsource,
alpha_ambsource, alpha_enable); alpha_ambsource, alpha_enable);
if (color_matsource == MatSource::Vertex) if (color_matsource == MatSource::Vertex)
object.Write("int4 mat = int4(round({}{} * 255.0));\n", in_color_name, chan); object.Write(" int4 mat = int4(round({}{} * 255.0));\n", in_color_name, chan);
else // from material color register else // from material color register
object.Write("int4 mat = {}[{}];\n", I_MATERIALS, chan + 2); object.Write(" int4 mat = {}[{}];\n", I_MATERIALS, chan + 2);
if (color_enable) if (color_enable)
{ {
if (color_ambsource == AmbSource::Vertex) 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 else // from ambient color register
object.Write("lacc = {}[{}];\n", I_MATERIALS, chan); object.Write(" int4 lacc = {}[{}];\n", I_MATERIALS, chan);
} }
else 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 // check if alpha is different
if (color_matsource != alpha_matsource) if (color_matsource != alpha_matsource)
{ {
if (alpha_matsource == MatSource::Vertex) if (alpha_matsource == MatSource::Vertex)
object.Write("mat.w = int(round({}{}.w * 255.0));\n", in_color_name, chan); object.Write(" mat.w = int(round({}{}.w * 255.0));\n", in_color_name, chan);
else // from material color register else // from material color register
object.Write("mat.w = {}[{}].w;\n", I_MATERIALS, chan + 2); object.Write(" mat.w = {}[{}].w;\n", I_MATERIALS, chan + 2);
} }
if (alpha_enable) if (alpha_enable)
{ {
if (alpha_ambsource == AmbSource::Vertex) // from vertex if (alpha_ambsource == AmbSource::Vertex) // from vertex
object.Write("lacc.w = int(round({}{}.w * 255.0));\n", in_color_name, chan); object.Write(" lacc.w = int(round({}{}.w * 255.0));\n", in_color_name, chan);
else // from ambient color register else // from ambient color register
object.Write("lacc.w = {}[{}].w;\n", I_MATERIALS, chan); object.Write(" lacc.w = {}[{}].w;\n", I_MATERIALS, chan);
} }
else else
{ {
object.Write("lacc.w = 255;\n"); object.Write(" lacc.w = 255;\n");
} }
if (color_enable) if (color_enable)
@ -145,38 +148,36 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
const auto diffusefunc = static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * chan)) & 3); const auto diffusefunc = static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * chan)) & 3);
const u32 light_mask = const u32 light_mask =
(uid_data.light_mask >> (NUM_XF_LIGHTS * chan)) & ((1 << NUM_XF_LIGHTS) - 1); (uid_data.light_mask >> (NUM_XF_LIGHTS * chan)) & ((1 << NUM_XF_LIGHTS) - 1);
object.Write("// Color attenuation function: {}\n", attnfunc); object.Write(" // Color attenuation function: {}\n", attnfunc);
object.Write("// Color diffuse function: {}\n", diffusefunc); object.Write(" // Color diffuse function: {}\n", diffusefunc);
object.Write("// Color light mask: {:08b}\n", light_mask); object.Write(" // Color light mask: {:08b}\n", light_mask);
for (u32 light = 0; light < NUM_XF_LIGHTS; light++) for (u32 light = 0; light < NUM_XF_LIGHTS; light++)
{ {
if ((light_mask & (1 << light)) != 0) if ((light_mask & (1 << light)) != 0)
{ {
object.Write("// Color light {}\n", light);
GenerateLightShader(object, uid_data, light, attnfunc, diffusefunc, false); 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 attnfunc = static_cast<AttenuationFunc>((uid_data.attnfunc >> (2 * chan_a)) & 3);
const auto diffusefunc = static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * chan_a)) & 3); const auto diffusefunc = static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * chan_a)) & 3);
const u32 light_mask = const u32 light_mask =
(uid_data.light_mask >> (NUM_XF_LIGHTS * chan_a)) & ((1 << NUM_XF_LIGHTS) - 1); (uid_data.light_mask >> (NUM_XF_LIGHTS * chan_a)) & ((1 << NUM_XF_LIGHTS) - 1);
object.Write("// Alpha attenuation function: {}\n", attnfunc); object.Write(" // Alpha attenuation function: {}\n", attnfunc);
object.Write("// Alpha diffuse function: {}\n", diffusefunc); object.Write(" // Alpha diffuse function: {}\n", diffusefunc);
object.Write("// Alpha light mask function: {:08b}\n", light_mask); object.Write(" // Alpha light mask function: {:08b}\n", light_mask);
for (u32 light = 0; light < NUM_XF_LIGHTS; light++) for (u32 light = 0; light < NUM_XF_LIGHTS; light++)
{ {
if ((light_mask & (1 << light)) != 0) if ((light_mask & (1 << light)) != 0)
{ {
object.Write("// Alpha light {}\n", light);
GenerateLightShader(object, uid_data, light, attnfunc, diffusefunc, true); GenerateLightShader(object, uid_data, light, attnfunc, diffusefunc, true);
} }
} }
} }
object.Write("lacc = clamp(lacc, 0, 255);\n"); object.Write(" lacc = clamp(lacc, 0, 255);\n");
object.Write("{}{} = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n", dest, chan); object.Write(" {}{} = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n", dest, chan);
object.Write("}}\n"); object.Write("}}\n");
} }
} }

View file

@ -955,10 +955,6 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos
out.Write("\tfloat3 _normal = normalize(Normal.xyz);\n\n" out.Write("\tfloat3 _normal = normalize(Normal.xyz);\n\n"
"\tfloat3 pos = WorldPos;\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. // 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. // 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 // 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 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"); "[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_"); GenerateLightingShaderCode(out, uid_data->lighting, "vertex_color_", "o.colors_");
// transform texcoords // transform texcoords
@ -433,7 +429,7 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
case TexGenType::EmbossMap: // calculate tex coords into bump map case TexGenType::EmbossMap: // calculate tex coords into bump map
// transform the light dir into tangent space // 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)); LIGHT_POS_PARAMS(texinfo.embosslightshift));
out.Write( out.Write(
"o.tex{}.xyz = o.tex{}.xyz + float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n", "o.tex{}.xyz = o.tex{}.xyz + float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n",