2017-07-20 07:25:24 +02:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-07-20 07:25:24 +02:00
|
|
|
|
|
|
|
#include "VideoCommon/UberShaderCommon.h"
|
2017-07-27 12:52:20 +02:00
|
|
|
#include "VideoCommon/NativeVertexFormat.h"
|
2019-12-22 19:40:40 +01:00
|
|
|
#include "VideoCommon/ShaderGenCommon.h"
|
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2017-07-27 12:52:20 +02:00
|
|
|
#include "VideoCommon/XFMemory.h"
|
2017-07-20 07:25:24 +02:00
|
|
|
|
|
|
|
namespace UberShader
|
|
|
|
{
|
|
|
|
void WriteUberShaderCommonHeader(ShaderCode& out, APIType api_type,
|
|
|
|
const ShaderHostConfig& host_config)
|
|
|
|
{
|
|
|
|
// ==============================================
|
|
|
|
// BitfieldExtract for APIs which don't have it
|
|
|
|
// ==============================================
|
|
|
|
if (!host_config.backend_bitfield)
|
|
|
|
{
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write("uint bitfieldExtract(uint val, int off, int size) {{\n"
|
|
|
|
" // This built-in function is only support in OpenGL 4.0+ and ES 3.1+\n"
|
|
|
|
" // Microsoft's HLSL compiler automatically optimises this to a bitfield extract "
|
|
|
|
"instruction.\n"
|
|
|
|
" uint mask = uint((1 << size) - 1);\n"
|
|
|
|
" return uint(val >> off) & mask;\n"
|
|
|
|
"}}\n\n");
|
2017-07-20 07:25:24 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-27 12:52:20 +02:00
|
|
|
|
|
|
|
void WriteLightingFunction(ShaderCode& out)
|
|
|
|
{
|
|
|
|
// ==============================================
|
|
|
|
// Lighting channel calculation helper
|
|
|
|
// ==============================================
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write("int4 CalculateLighting(uint index, uint attnfunc, uint diffusefunc, float3 pos, "
|
|
|
|
"float3 normal) {{\n"
|
|
|
|
" float3 ldir, h, cosAttn, distAttn;\n"
|
|
|
|
" float dist, dist2, attn;\n"
|
|
|
|
"\n"
|
|
|
|
" switch (attnfunc) {{\n");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", AttenuationFunc::None);
|
|
|
|
out.Write(" case {:s}:\n", AttenuationFunc::Dir);
|
2020-11-09 08:23:32 +01:00
|
|
|
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");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", AttenuationFunc::Spec);
|
2020-11-09 08:23:32 +01:00
|
|
|
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");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" if (diffusefunc == {:s})\n", DiffuseFunc::None);
|
2020-11-09 08:23:32 +01:00
|
|
|
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");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", AttenuationFunc::Spot);
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" ldir = " I_LIGHTS "[index].pos.xyz - pos.xyz;\n"
|
|
|
|
" dist2 = dot(ldir, ldir);\n"
|
|
|
|
" dist = sqrt(dist2);\n"
|
|
|
|
" ldir = ldir / dist;\n"
|
|
|
|
" attn = max(0.0, dot(ldir, " I_LIGHTS "[index].dir.xyz));\n"
|
|
|
|
" attn = max(0.0, " I_LIGHTS "[index].cosatt.x + " I_LIGHTS
|
|
|
|
"[index].cosatt.y * attn + " I_LIGHTS "[index].cosatt.z * attn * attn) / dot(" I_LIGHTS
|
|
|
|
"[index].distatt.xyz, float3(1.0, dist, dist2));\n"
|
|
|
|
" break;\n\n");
|
|
|
|
out.Write(" default:\n"
|
|
|
|
" attn = 1.0;\n"
|
|
|
|
" ldir = normal;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }}\n"
|
|
|
|
"\n"
|
|
|
|
" switch (diffusefunc) {{\n");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", DiffuseFunc::None);
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" return int4(round(attn * float4(" I_LIGHTS "[index].color)));\n\n");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", DiffuseFunc::Sign);
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" return int4(round(attn * dot(ldir, normal) * float4(" I_LIGHTS
|
|
|
|
"[index].color)));\n\n");
|
2021-02-11 01:01:42 +01:00
|
|
|
out.Write(" case {:s}:\n", DiffuseFunc::Clamp);
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" return int4(round(attn * max(0.0, dot(ldir, normal)) * float4(" I_LIGHTS
|
|
|
|
"[index].color)));\n\n");
|
|
|
|
out.Write(" default:\n"
|
|
|
|
" return int4(0, 0, 0, 0);\n"
|
|
|
|
" }}\n"
|
|
|
|
"}}\n\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
}
|
|
|
|
|
2020-10-20 03:32:20 +02:00
|
|
|
void WriteVertexLighting(ShaderCode& out, APIType api_type, std::string_view world_pos_var,
|
|
|
|
std::string_view normal_var, std::string_view in_color_0_var,
|
|
|
|
std::string_view in_color_1_var, std::string_view out_color_0_var,
|
|
|
|
std::string_view out_color_1_var)
|
2017-07-27 12:52:20 +02:00
|
|
|
{
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write("// Lighting\n");
|
|
|
|
out.Write("{}for (uint chan = 0u; chan < {}u; chan++) {{\n",
|
|
|
|
api_type == APIType::D3D ? "[loop] " : "", NUM_XF_COLOR_CHANNELS);
|
|
|
|
out.Write(" uint colorreg = xfmem_color(chan);\n"
|
|
|
|
" uint alphareg = xfmem_alpha(chan);\n"
|
|
|
|
" int4 mat = " I_MATERIALS "[chan + 2u]; \n"
|
|
|
|
" int4 lacc = int4(255, 255, 255, 255);\n"
|
|
|
|
"\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
|
2021-04-11 04:34:20 +02:00
|
|
|
out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::matsource>("colorreg"));
|
2017-01-29 13:38:48 +01:00
|
|
|
out.Write(" mat.xyz = int3(round(((chan == 0u) ? {}.xyz : {}.xyz) * 255.0));\n",
|
2020-11-09 08:23:32 +01:00
|
|
|
in_color_0_var, in_color_1_var);
|
2017-07-27 12:52:20 +02:00
|
|
|
|
2021-04-11 04:34:20 +02:00
|
|
|
out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::matsource>("alphareg"));
|
2017-01-29 13:38:48 +01:00
|
|
|
out.Write(" mat.w = int(round(((chan == 0u) ? {}.w : {}.w) * 255.0));\n", in_color_0_var,
|
2020-11-09 08:23:32 +01:00
|
|
|
in_color_1_var);
|
2017-01-29 13:38:48 +01:00
|
|
|
out.Write(" else\n"
|
2020-11-09 08:23:32 +01:00
|
|
|
" mat.w = " I_MATERIALS " [chan + 2u].w;\n"
|
|
|
|
"\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
|
2021-04-11 04:34:20 +02:00
|
|
|
out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::enablelighting>("colorreg"));
|
|
|
|
out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::ambsource>("colorreg"));
|
2017-01-29 13:38:48 +01:00
|
|
|
out.Write(" lacc.xyz = int3(round(((chan == 0u) ? {}.xyz : {}.xyz) * 255.0));\n",
|
2020-11-09 08:23:32 +01:00
|
|
|
in_color_0_var, in_color_1_var);
|
2017-01-29 13:38:48 +01:00
|
|
|
out.Write(" else\n"
|
2020-11-09 08:23:32 +01:00
|
|
|
" lacc.xyz = " I_MATERIALS " [chan].xyz;\n"
|
|
|
|
"\n");
|
|
|
|
out.Write(" uint light_mask = {} | ({} << 4u);\n",
|
2021-04-11 04:34:20 +02:00
|
|
|
BitfieldExtract<&LitChannel::lightMask0_3>("colorreg"),
|
|
|
|
BitfieldExtract<&LitChannel::lightMask4_7>("colorreg"));
|
|
|
|
out.Write(" uint attnfunc = {};\n", BitfieldExtract<&LitChannel::attnfunc>("colorreg"));
|
|
|
|
out.Write(" uint diffusefunc = {};\n", BitfieldExtract<&LitChannel::diffusefunc>("colorreg"));
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(
|
2020-10-20 03:32:20 +02:00
|
|
|
" for (uint light_index = 0u; light_index < 8u; light_index++) {{\n"
|
2017-07-27 12:52:20 +02:00
|
|
|
" if ((light_mask & (1u << light_index)) != 0u)\n"
|
2020-10-20 03:32:20 +02:00
|
|
|
" lacc.xyz += CalculateLighting(light_index, attnfunc, diffusefunc, {}, {}).xyz;\n",
|
2017-07-27 12:52:20 +02:00
|
|
|
world_pos_var, normal_var);
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" }}\n"
|
|
|
|
" }}\n"
|
|
|
|
"\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
|
2021-04-11 04:34:20 +02:00
|
|
|
out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::enablelighting>("alphareg"));
|
|
|
|
out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::ambsource>("alphareg"));
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" if ((components & ({}u << chan)) != 0u) // VB_HAS_COL0\n", VB_HAS_COL0);
|
|
|
|
out.Write(" lacc.w = int(round(((chan == 0u) ? {}.w : {}.w) * 255.0));\n", in_color_0_var,
|
|
|
|
in_color_1_var);
|
|
|
|
out.Write(" else if ((components & {}u) != 0u) // VB_HAS_COLO0\n", VB_HAS_COL0);
|
|
|
|
out.Write(" lacc.w = int(round({}.w * 255.0));\n", in_color_0_var);
|
|
|
|
out.Write(" else\n"
|
|
|
|
" lacc.w = 255;\n"
|
|
|
|
" }} else {{\n"
|
|
|
|
" lacc.w = " I_MATERIALS " [chan].w;\n"
|
|
|
|
" }}\n"
|
|
|
|
"\n");
|
|
|
|
out.Write(" uint light_mask = {} | ({} << 4u);\n",
|
2021-04-11 04:34:20 +02:00
|
|
|
BitfieldExtract<&LitChannel::lightMask0_3>("alphareg"),
|
|
|
|
BitfieldExtract<&LitChannel::lightMask4_7>("alphareg"));
|
|
|
|
out.Write(" uint attnfunc = {};\n", BitfieldExtract<&LitChannel::attnfunc>("alphareg"));
|
|
|
|
out.Write(" uint diffusefunc = {};\n", BitfieldExtract<&LitChannel::diffusefunc>("alphareg"));
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" for (uint light_index = 0u; light_index < 8u; light_index++) {{\n\n"
|
|
|
|
" if ((light_mask & (1u << light_index)) != 0u)\n\n"
|
|
|
|
" lacc.w += CalculateLighting(light_index, attnfunc, diffusefunc, {}, {}).w;\n",
|
|
|
|
world_pos_var, normal_var);
|
|
|
|
out.Write(" }}\n"
|
|
|
|
" }}\n"
|
|
|
|
"\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
|
2020-11-09 08:23:32 +01:00
|
|
|
out.Write(" lacc = clamp(lacc, 0, 255);\n"
|
|
|
|
"\n"
|
|
|
|
" // Hopefully GPUs that can support dynamic indexing will optimize this.\n"
|
|
|
|
" float4 lit_color = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n"
|
|
|
|
" switch (chan) {{\n"
|
|
|
|
" case 0u: {} = lit_color; break;\n",
|
|
|
|
out_color_0_var);
|
|
|
|
out.Write(" case 1u: {} = lit_color; break;\n", out_color_1_var);
|
|
|
|
out.Write(" }}\n"
|
|
|
|
"}}\n"
|
|
|
|
"\n");
|
2017-07-27 12:52:20 +02:00
|
|
|
}
|
2019-05-06 01:48:12 +02:00
|
|
|
} // namespace UberShader
|