mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-24 22:34:54 +00:00
Merge pull request #13347 from iwubcode/custom_shader_overhaul_lighting
VideoCommon: move lighting shader logic to callable functions
This commit is contained in:
commit
e3deb14156
4 changed files with 37 additions and 25 deletions
|
@ -79,31 +79,34 @@ static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_d
|
||||||
// vertex shader
|
// vertex shader
|
||||||
// lights/colors
|
// lights/colors
|
||||||
// materials name is I_MATERIALS in vs and I_PMATERIALS in ps
|
// materials name is I_MATERIALS in vs and I_PMATERIALS in ps
|
||||||
// inColorName is color in vs and colors_ in ps
|
void GenerateLightingShaderHeader(ShaderCode& object, const LightingUidData& uid_data)
|
||||||
// dest is o.colors_ in vs and colors_ in ps
|
|
||||||
void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_data,
|
|
||||||
std::string_view in_color_name, std::string_view dest)
|
|
||||||
{
|
{
|
||||||
for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
|
for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
|
||||||
{
|
{
|
||||||
|
object.Write("vec4 dolphin_calculate_lighting_chn{}(vec4 base_color, vec3 pos, vec3 _normal)\n",
|
||||||
|
j);
|
||||||
object.Write("{{\n");
|
object.Write("{{\n");
|
||||||
|
|
||||||
|
object.Write("\tint4 lacc;\n"
|
||||||
|
"\tvec3 ldir, h, cosAttn, distAttn;\n"
|
||||||
|
"\tfloat dist, dist2, attn;\n");
|
||||||
|
|
||||||
const bool colormatsource = !!(uid_data.matsource & (1 << j));
|
const bool colormatsource = !!(uid_data.matsource & (1 << j));
|
||||||
if (colormatsource) // from vertex
|
if (colormatsource) // from vertex
|
||||||
object.Write("int4 mat = int4(round({}{} * 255.0));\n", in_color_name, j);
|
object.Write("\tint4 mat = int4(round(base_color * 255.0));\n");
|
||||||
else // from color
|
else // from color
|
||||||
object.Write("int4 mat = {}[{}];\n", I_MATERIALS, j + 2);
|
object.Write("\tint4 mat = {}[{}];\n", I_MATERIALS, j + 2);
|
||||||
|
|
||||||
if ((uid_data.enablelighting & (1 << j)) != 0)
|
if ((uid_data.enablelighting & (1 << j)) != 0)
|
||||||
{
|
{
|
||||||
if ((uid_data.ambsource & (1 << j)) != 0) // from vertex
|
if ((uid_data.ambsource & (1 << j)) != 0) // from vertex
|
||||||
object.Write("lacc = int4(round({}{} * 255.0));\n", in_color_name, j);
|
object.Write("\tlacc = int4(round(base_color * 255.0));\n");
|
||||||
else // from color
|
else // from color
|
||||||
object.Write("lacc = {}[{}];\n", I_MATERIALS, j);
|
object.Write("\tlacc = {}[{}];\n", I_MATERIALS, j);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
object.Write("lacc = int4(255, 255, 255, 255);\n");
|
object.Write("\tlacc = int4(255, 255, 255, 255);\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if alpha is different
|
// check if alpha is different
|
||||||
|
@ -111,21 +114,21 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
|
||||||
if (alphamatsource != colormatsource)
|
if (alphamatsource != colormatsource)
|
||||||
{
|
{
|
||||||
if (alphamatsource) // from vertex
|
if (alphamatsource) // from vertex
|
||||||
object.Write("mat.w = int(round({}{}.w * 255.0));\n", in_color_name, j);
|
object.Write("\tmat.w = int(round(base_color.w * 255.0));\n");
|
||||||
else // from color
|
else // from color
|
||||||
object.Write("mat.w = {}[{}].w;\n", I_MATERIALS, j + 2);
|
object.Write("\tmat.w = {}[{}].w;\n", I_MATERIALS, j + 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((uid_data.enablelighting & (1 << (j + 2))) != 0)
|
if ((uid_data.enablelighting & (1 << (j + 2))) != 0)
|
||||||
{
|
{
|
||||||
if ((uid_data.ambsource & (1 << (j + 2))) != 0) // from vertex
|
if ((uid_data.ambsource & (1 << (j + 2))) != 0) // from vertex
|
||||||
object.Write("lacc.w = int(round({}{}.w * 255.0));\n", in_color_name, j);
|
object.Write("\tlacc.w = int(round(base_color.w * 255.0));\n");
|
||||||
else // from color
|
else // from color
|
||||||
object.Write("lacc.w = {}[{}].w;\n", I_MATERIALS, j);
|
object.Write("\tlacc.w = {}[{}].w;\n", I_MATERIALS, j);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
object.Write("lacc.w = 255;\n");
|
object.Write("\tlacc.w = 255;\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
|
if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
|
||||||
|
@ -144,9 +147,9 @@ void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_d
|
||||||
GenerateLightShader(object, uid_data, i, j + 2, true);
|
GenerateLightShader(object, uid_data, i, j + 2, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
object.Write("lacc = clamp(lacc, 0, 255);\n");
|
object.Write("\tlacc = clamp(lacc, 0, 255);\n");
|
||||||
object.Write("{}{} = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n", dest, j);
|
object.Write("\treturn vec4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n");
|
||||||
object.Write("}}\n");
|
object.Write("}}\n\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -44,8 +44,7 @@ constexpr char s_lighting_struct[] = "struct Light {\n"
|
||||||
"\tfloat4 dir;\n"
|
"\tfloat4 dir;\n"
|
||||||
"};\n";
|
"};\n";
|
||||||
|
|
||||||
void GenerateLightingShaderCode(ShaderCode& object, const LightingUidData& uid_data,
|
void GenerateLightingShaderHeader(ShaderCode& object, const LightingUidData& uid_data);
|
||||||
std::string_view in_color_name, std::string_view dest);
|
|
||||||
void GetLightingShaderUid(LightingUidData& uid_data);
|
void GetLightingShaderUid(LightingUidData& uid_data);
|
||||||
|
|
||||||
void GenerateCustomLightingHeaderDetails(ShaderCode* out, u32 enablelighting, u32 light_mask);
|
void GenerateCustomLightingHeaderDetails(ShaderCode* out, u32 enablelighting, u32 light_mask);
|
||||||
|
|
|
@ -1057,6 +1057,11 @@ ShaderCode GeneratePixelShaderCode(APIType api_type, const ShaderHostConfig& hos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (per_pixel_lighting)
|
||||||
|
{
|
||||||
|
GenerateLightingShaderHeader(out, uid_data->lighting);
|
||||||
|
}
|
||||||
|
|
||||||
out.Write("void main()\n{{\n");
|
out.Write("void main()\n{{\n");
|
||||||
out.Write("\tfloat4 rawpos = gl_FragCoord;\n");
|
out.Write("\tfloat4 rawpos = gl_FragCoord;\n");
|
||||||
|
|
||||||
|
@ -1124,16 +1129,15 @@ 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
|
||||||
// out.SetConstantsUsed(C_PLIGHTS, C_PLIGHTS+31); // TODO: Can be optimized further
|
// out.SetConstantsUsed(C_PLIGHTS, C_PLIGHTS+31); // TODO: Can be optimized further
|
||||||
// out.SetConstantsUsed(C_PMATERIALS, C_PMATERIALS+3);
|
// out.SetConstantsUsed(C_PMATERIALS, C_PMATERIALS+3);
|
||||||
GenerateLightingShaderCode(out, uid_data->lighting, "colors_", "col");
|
for (u32 chan = 0; chan < uid_data->numColorChans; chan++)
|
||||||
|
{
|
||||||
|
out.Write("\tcol{0} = dolphin_calculate_lighting_chn{0}(colors_{0}, pos, _normal);\n", chan);
|
||||||
|
}
|
||||||
// The number of colors available to TEV is determined by numColorChans.
|
// The number of colors available to TEV is determined by numColorChans.
|
||||||
// Normally this is performed in the vertex shader after lighting, but with per-pixel lighting,
|
// Normally this is performed in the vertex shader after lighting, but with per-pixel lighting,
|
||||||
// we need to perform it here. (It needs to be done after lighting, as what was originally
|
// we need to perform it here. (It needs to be done after lighting, as what was originally
|
||||||
|
|
|
@ -190,6 +190,7 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
|
||||||
out.Write("}};\n\n");
|
out.Write("}};\n\n");
|
||||||
|
|
||||||
WriteIsNanHeader(out, api_type);
|
WriteIsNanHeader(out, api_type);
|
||||||
|
GenerateLightingShaderHeader(out, uid_data->lighting);
|
||||||
|
|
||||||
if (uid_data->vs_expand == VSExpand::None)
|
if (uid_data->vs_expand == VSExpand::None)
|
||||||
{
|
{
|
||||||
|
@ -434,7 +435,12 @@ ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& ho
|
||||||
"float3 ldir, h, cosAttn, distAttn;\n"
|
"float3 ldir, h, cosAttn, distAttn;\n"
|
||||||
"float dist, dist2, attn;\n");
|
"float dist, dist2, attn;\n");
|
||||||
|
|
||||||
GenerateLightingShaderCode(out, uid_data->lighting, "vertex_color_", "o.colors_");
|
for (u32 chan = 0; chan < NUM_XF_COLOR_CHANNELS; chan++)
|
||||||
|
{
|
||||||
|
out.Write(
|
||||||
|
"\to.colors_{0} = dolphin_calculate_lighting_chn{0}(vertex_color_{0}, pos.xyz, _normal);\n",
|
||||||
|
chan);
|
||||||
|
}
|
||||||
|
|
||||||
// transform texcoords
|
// transform texcoords
|
||||||
out.Write("float4 coord = float4(0.0, 0.0, 1.0, 1.0);\n");
|
out.Write("float4 coord = float4(0.0, 0.0, 1.0, 1.0);\n");
|
||||||
|
|
Loading…
Add table
Reference in a new issue