| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/UberShaderCommon.h" |
| 5 | |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "VideoCommon/NativeVertexFormat.h" |
| 9 | #include "VideoCommon/ShaderGenCommon.h" |
| 10 | #include "VideoCommon/VideoCommon.h" |
| 11 | #include "VideoCommon/XFMemory.h" |
| 12 | |
| 13 | namespace UberShader |
| 14 | { |
| 15 | void WriteLightingFunction(ShaderCode& out) |
| 16 | { |
| 17 | // ============================================== |
| 18 | // Lighting channel calculation helper |
| 19 | // ============================================== |
| 20 | out.Write("int4 CalculateLighting(uint index, uint attnfunc, uint diffusefunc, float3 pos, " |
| 21 | "float3 normal) {{\n" |
| 22 | " float3 ldir, h, cosAttn, distAttn;\n" |
| 23 | " float dist, dist2, attn;\n" |
| 24 | "\n" |
| 25 | " switch (attnfunc) {{\n"); |
| 26 | out.Write(" case {:s}:\n", AttenuationFunc::None); |
| 27 | out.Write(" case {:s}:\n", AttenuationFunc::Dir); |
| 28 | out.Write(" ldir = normalize(" I_LIGHTS "[index].pos.xyz - pos.xyz);\n" |
| 29 | " attn = 1.0;\n" |
| 30 | " if (length(ldir) == 0.0)\n" |
| 31 | " ldir = normal;\n" |
| 32 | " break;\n\n"); |
| 33 | out.Write(" case {:s}:\n", AttenuationFunc::Spec); |
| 34 | out.Write(" ldir = normalize(" I_LIGHTS "[index].pos.xyz - pos.xyz);\n" |
| 35 | " attn = (dot(normal, ldir) >= 0.0) ? max(0.0, dot(normal, " I_LIGHTS |
| 36 | "[index].dir.xyz)) : 0.0;\n" |
| 37 | " cosAttn = " I_LIGHTS "[index].cosatt.xyz;\n"); |
| 38 | out.Write(" if (diffusefunc == {:s})\n", DiffuseFunc::None); |
| 39 | out.Write(" distAttn = " I_LIGHTS "[index].distatt.xyz;\n" |
| 40 | " else\n" |
| 41 | " distAttn = normalize(" I_LIGHTS "[index].distatt.xyz);\n" |
| 42 | " attn = max(0.0, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, " |
| 43 | "float3(1.0, attn, attn*attn));\n" |
| 44 | " break;\n\n"); |
| 45 | out.Write(" case {:s}:\n", AttenuationFunc::Spot); |
| 46 | out.Write(" ldir = " I_LIGHTS "[index].pos.xyz - pos.xyz;\n" |
| 47 | " dist2 = dot(ldir, ldir);\n" |
| 48 | " dist = sqrt(dist2);\n" |
| 49 | " ldir = ldir / dist;\n" |
| 50 | " attn = max(0.0, dot(ldir, " I_LIGHTS "[index].dir.xyz));\n" |
| 51 | " attn = max(0.0, " I_LIGHTS "[index].cosatt.x + " I_LIGHTS |
| 52 | "[index].cosatt.y * attn + " I_LIGHTS "[index].cosatt.z * attn * attn) / dot(" I_LIGHTS |
| 53 | "[index].distatt.xyz, float3(1.0, dist, dist2));\n" |
| 54 | " break;\n\n"); |
| 55 | out.Write(" default:\n" |
| 56 | " attn = 1.0;\n" |
| 57 | " ldir = normal;\n" |
| 58 | " break;\n" |
| 59 | " }}\n" |
| 60 | "\n" |
| 61 | " switch (diffusefunc) {{\n"); |
| 62 | out.Write(" case {:s}:\n", DiffuseFunc::None); |
| 63 | out.Write(" return int4(round(attn * float4(" I_LIGHTS "[index].color)));\n\n"); |
| 64 | out.Write(" case {:s}:\n", DiffuseFunc::Sign); |
| 65 | out.Write(" return int4(round(attn * dot(ldir, normal) * float4(" I_LIGHTS |
| 66 | "[index].color)));\n\n"); |
| 67 | out.Write(" case {:s}:\n", DiffuseFunc::Clamp); |
| 68 | out.Write(" return int4(round(attn * max(0.0, dot(ldir, normal)) * float4(" I_LIGHTS |
| 69 | "[index].color)));\n\n"); |
| 70 | out.Write(" default:\n" |
| 71 | " return int4(0, 0, 0, 0);\n" |
| 72 | " }}\n" |
| 73 | "}}\n\n"); |
| 74 | } |
| 75 | |
| 76 | void WriteVertexLighting(ShaderCode& out, APIType api_type, std::string_view world_pos_var, |
| 77 | std::string_view normal_var, std::string_view in_color_0_var, |
| 78 | std::string_view in_color_1_var, std::string_view out_color_0_var, |
| 79 | std::string_view out_color_1_var) |
| 80 | { |
| 81 | out.Write("// Lighting\n"); |
| 82 | out.Write("for (uint chan = 0u; chan < {}u; chan++) {{\n", NUM_XF_COLOR_CHANNELS); |
| 83 | out.Write(" uint colorreg = xfmem_color(chan);\n" |
| 84 | " uint alphareg = xfmem_alpha(chan);\n" |
| 85 | " int4 mat = " I_MATERIALS "[chan + 2u]; \n" |
| 86 | " int4 lacc = int4(255, 255, 255, 255);\n" |
| 87 | "\n"); |
| 88 | |
| 89 | out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::matsource>("colorreg")); |
| 90 | out.Write(" mat.xyz = int3(round(((chan == 0u) ? {}.xyz : {}.xyz) * 255.0));\n", |
| 91 | in_color_0_var, in_color_1_var); |
| 92 | |
| 93 | out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::matsource>("alphareg")); |
| 94 | out.Write(" mat.w = int(round(((chan == 0u) ? {}.w : {}.w) * 255.0));\n", in_color_0_var, |
| 95 | in_color_1_var); |
| 96 | out.Write(" else\n" |
| 97 | " mat.w = " I_MATERIALS " [chan + 2u].w;\n" |
| 98 | "\n"); |
| 99 | |
| 100 | out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::enablelighting>("colorreg")); |
| 101 | out.Write(" if ({} != 0u)\n", BitfieldExtract<&LitChannel::ambsource>("colorreg")); |
| 102 | out.Write(" lacc.xyz = int3(round(((chan == 0u) ? {}.xyz : {}.xyz) * 255.0));\n", |
| 103 | in_color_0_var, in_color_1_var); |
| 104 | out.Write(" else\n" |
| 105 | " lacc.xyz = " I_MATERIALS " [chan].xyz;\n" |
| 106 | "\n"); |
| 107 | out.Write(" uint light_mask = {} | ({} << 4u);\n", |
| 108 | BitfieldExtract<&LitChannel::lightMask0_3>("colorreg"), |
| 109 | BitfieldExtract<&LitChannel::lightMask4_7>("colorreg")); |
| 110 | out.Write(" uint attnfunc = {};\n", BitfieldExtract<&LitChannel::attnfunc>("colorreg")); |
| 111 | out.Write(" uint diffusefunc = {};\n", BitfieldExtract<&LitChannel::diffusefunc>("colorreg")); |
| 112 | out.Write( |
| 113 | " for (uint light_index = 0u; light_index < 8u; light_index++) {{\n" |
| 114 | " if ((light_mask & (1u << light_index)) != 0u)\n" |
| 115 | " lacc.xyz += CalculateLighting(light_index, attnfunc, diffusefunc, {}, {}).xyz;\n", |
| 116 | world_pos_var, normal_var); |
| 117 | out.Write(" }}\n" |
| 118 | " }}\n" |
| 119 | "\n"); |
| 120 | |
| 121 | out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::enablelighting>("alphareg")); |
| 122 | out.Write(" if ({} != 0u) {{\n", BitfieldExtract<&LitChannel::ambsource>("alphareg")); |
| 123 | out.Write(" if ((components & ({}u << chan)) != 0u) // VB_HAS_COL0\n", |
| 124 | std::to_underlying(VB_HAS_COL0)); |
| 125 | out.Write(" lacc.w = int(round(((chan == 0u) ? {}.w : {}.w) * 255.0));\n", in_color_0_var, |
| 126 | in_color_1_var); |
| 127 | out.Write(" else if ((components & {}u) != 0u) // VB_HAS_COLO0\n", |
| 128 | std::to_underlying(VB_HAS_COL0)); |
| 129 | out.Write(" lacc.w = int(round({}.w * 255.0));\n", in_color_0_var); |
| 130 | out.Write(" else\n" |
| 131 | " lacc.w = 255;\n" |
| 132 | " }} else {{\n" |
| 133 | " lacc.w = " I_MATERIALS " [chan].w;\n" |
| 134 | " }}\n" |
| 135 | "\n"); |
| 136 | out.Write(" uint light_mask = {} | ({} << 4u);\n", |
| 137 | BitfieldExtract<&LitChannel::lightMask0_3>("alphareg"), |
| 138 | BitfieldExtract<&LitChannel::lightMask4_7>("alphareg")); |
| 139 | out.Write(" uint attnfunc = {};\n", BitfieldExtract<&LitChannel::attnfunc>("alphareg")); |
| 140 | out.Write(" uint diffusefunc = {};\n", BitfieldExtract<&LitChannel::diffusefunc>("alphareg")); |
| 141 | out.Write(" for (uint light_index = 0u; light_index < 8u; light_index++) {{\n\n" |
| 142 | " if ((light_mask & (1u << light_index)) != 0u)\n\n" |
| 143 | " lacc.w += CalculateLighting(light_index, attnfunc, diffusefunc, {}, {}).w;\n", |
| 144 | world_pos_var, normal_var); |
| 145 | out.Write(" }}\n" |
| 146 | " }}\n" |
| 147 | "\n"); |
| 148 | |
| 149 | out.Write(" lacc = clamp(lacc, 0, 255);\n" |
| 150 | "\n" |
| 151 | " // Hopefully GPUs that can support dynamic indexing will optimize this.\n" |
| 152 | " float4 lit_color = float4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n" |
| 153 | " switch (chan) {{\n" |
| 154 | " case 0u: {} = lit_color; break;\n", |
| 155 | out_color_0_var); |
| 156 | out.Write(" case 1u: {} = lit_color; break;\n", out_color_1_var); |
| 157 | out.Write(" }}\n" |
| 158 | "}}\n" |
| 159 | "\n"); |
| 160 | } |
| 161 | } // namespace UberShader |