Debian Codegraph @

Source/Core/VideoCommon/LightingShaderGen.cpp

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/LightingShaderGen.h"
5
6 #include "Common/Assert.h"
7 #include "Common/CommonTypes.h"
8
9 #include "VideoCommon/ShaderGenCommon.h"
10 #include "VideoCommon/XFMemory.h"
11
12 static void GenerateLightShader(ShaderCode& object, const LightingUidData& uid_data, int index,
13 int litchan_index, bool alpha)
14 {
15 const char* swizzle = alpha ? "a" : "rgb";
16 const char* swizzle_components = (alpha) ? "" : "3";
17
18 const auto attnfunc =
19 static_cast<AttenuationFunc>((uid_data.attnfunc >> (2 * litchan_index)) & 0x3);
20 const auto diffusefunc =
21 static_cast<DiffuseFunc>((uid_data.diffusefunc >> (2 * litchan_index)) & 0x3);
22
23 switch (attnfunc)
24 {
25 case AttenuationFunc::None:
26 case AttenuationFunc::Dir:
27 object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
28 object.Write("attn = 1.0;\n");
29 object.Write("if (length(ldir) == 0.0)\n\t ldir = _normal;\n");
30 break;
31 case AttenuationFunc::Spec:
32 object.Write("ldir = normalize(" LIGHT_POS ".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(index));
33 object.Write("attn = (dot(_normal, ldir) >= 0.0) ? max(0.0, dot(_normal, " LIGHT_DIR
34 ".xyz)) : 0.0;\n",
35 LIGHT_DIR_PARAMS(index));
36 object.Write("cosAttn = " LIGHT_COSATT ".xyz;\n", LIGHT_COSATT_PARAMS(index));
37 object.Write("distAttn = {}(" LIGHT_DISTATT ".xyz);\n",
38 (diffusefunc == DiffuseFunc::None) ? "" : "normalize",
39 LIGHT_DISTATT_PARAMS(index));
40 object.Write("attn = max(0.0f, dot(cosAttn, float3(1.0, attn, attn*attn))) / dot(distAttn, "
41 "float3(1.0, attn, attn*attn));\n");
42 break;
43 case AttenuationFunc::Spot:
44 object.Write("ldir = " LIGHT_POS ".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(index));
45 object.Write("dist2 = dot(ldir, ldir);\n"
46 "dist = sqrt(dist2);\n"
47 "ldir = ldir / dist;\n"
48 "attn = max(0.0, dot(ldir, " LIGHT_DIR ".xyz));\n",
49 LIGHT_DIR_PARAMS(index));
50 // attn*attn may overflow
51 object.Write("attn = max(0.0, " LIGHT_COSATT ".x + " LIGHT_COSATT ".y*attn + " LIGHT_COSATT
52 ".z*attn*attn) / dot(" LIGHT_DISTATT ".xyz, float3(1.0,dist,dist2));\n",
53 LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index), LIGHT_COSATT_PARAMS(index),
54 LIGHT_DISTATT_PARAMS(index));
55 break;
56 }
57
58 switch (diffusefunc)
59 {
60 case DiffuseFunc::None:
61 object.Write("lacc.{} += int{}(round(attn * float{}(" LIGHT_COL ")));\n", swizzle,
62 swizzle_components, swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
63 break;
64 case DiffuseFunc::Sign:
65 case DiffuseFunc::Clamp:
66 object.Write("lacc.{} += int{}(round(attn * {}dot(ldir, _normal)) * float{}(" LIGHT_COL
67 ")));\n",
68 swizzle, swizzle_components, diffusefunc != DiffuseFunc::Sign ? "max(0.0," : "(",
69 swizzle_components, LIGHT_COL_PARAMS(index, swizzle));
70 break;
71 default:
72 ASSERT(false);
73 }
74
75 object.Write("\n");
76 }
77
78 // vertex shader
79 // lights/colors
80 // materials name is I_MATERIALS in vs and I_PMATERIALS in ps
81 void GenerateLightingShaderHeader(ShaderCode& object, const LightingUidData& uid_data)
82 {
83 for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
84 {
85 object.Write("vec4 dolphin_calculate_lighting_chn{}(vec4 base_color, vec3 pos, vec3 _normal)\n",
86 j);
87 object.Write("{{\n");
88
89 object.Write("\tint4 lacc;\n"
90 "\tvec3 ldir, h, cosAttn, distAttn;\n"
91 "\tfloat dist, dist2, attn;\n");
92
93 const bool colormatsource = !!(uid_data.matsource & (1 << j));
94 if (colormatsource) // from vertex
95 object.Write("\tint4 mat = int4(round(base_color * 255.0));\n");
96 else // from color
97 object.Write("\tint4 mat = {}[{}];\n", I_MATERIALS, j + 2);
98
99 if ((uid_data.enablelighting & (1 << j)) != 0)
100 {
101 if ((uid_data.ambsource & (1 << j)) != 0) // from vertex
102 object.Write("\tlacc = int4(round(base_color * 255.0));\n");
103 else // from color
104 object.Write("\tlacc = {}[{}];\n", I_MATERIALS, j);
105 }
106 else
107 {
108 object.Write("\tlacc = int4(255, 255, 255, 255);\n");
109 }
110
111 // check if alpha is different
112 const bool alphamatsource = !!(uid_data.matsource & (1 << (j + 2)));
113 if (alphamatsource != colormatsource)
114 {
115 if (alphamatsource) // from vertex
116 object.Write("\tmat.w = int(round(base_color.w * 255.0));\n");
117 else // from color
118 object.Write("\tmat.w = {}[{}].w;\n", I_MATERIALS, j + 2);
119 }
120
121 if ((uid_data.enablelighting & (1 << (j + 2))) != 0)
122 {
123 if ((uid_data.ambsource & (1 << (j + 2))) != 0) // from vertex
124 object.Write("\tlacc.w = int(round(base_color.w * 255.0));\n");
125 else // from color
126 object.Write("\tlacc.w = {}[{}].w;\n", I_MATERIALS, j);
127 }
128 else
129 {
130 object.Write("\tlacc.w = 255;\n");
131 }
132
133 if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
134 {
135 for (int i = 0; i < 8; ++i)
136 {
137 if ((uid_data.light_mask & (1 << (i + 8 * j))) != 0)
138 GenerateLightShader(object, uid_data, i, j, false);
139 }
140 }
141 if ((uid_data.enablelighting & (1 << (j + 2))) != 0) // Alpha lights
142 {
143 for (int i = 0; i < 8; ++i)
144 {
145 if ((uid_data.light_mask & (1 << (i + 8 * (j + 2)))) != 0)
146 GenerateLightShader(object, uid_data, i, j + 2, true);
147 }
148 }
149 object.Write("\tlacc = clamp(lacc, 0, 255);\n");
150 object.Write("\treturn vec4((mat * (lacc + (lacc >> 7))) >> 8) / 255.0;\n");
151 object.Write("}}\n\n");
152 }
153 }
154
155 void GetLightingShaderUid(LightingUidData& uid_data)
156 {
157 for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
158 {
159 uid_data.matsource |= static_cast<u32>(xfmem.color[j].matsource.Value()) << j;
160 uid_data.matsource |= static_cast<u32>(xfmem.alpha[j].matsource.Value()) << (j + 2);
161 uid_data.enablelighting |= xfmem.color[j].enablelighting << j;
162 uid_data.enablelighting |= xfmem.alpha[j].enablelighting << (j + 2);
163
164 if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
165 {
166 uid_data.ambsource |= static_cast<u32>(xfmem.color[j].ambsource.Value()) << j;
167 uid_data.attnfunc |= static_cast<u32>(xfmem.color[j].attnfunc.Value()) << (2 * j);
168 uid_data.diffusefunc |= static_cast<u32>(xfmem.color[j].diffusefunc.Value()) << (2 * j);
169 uid_data.light_mask |= xfmem.color[j].GetFullLightMask() << (8 * j);
170 }
171 if ((uid_data.enablelighting & (1 << (j + 2))) != 0) // Alpha lights
172 {
173 uid_data.ambsource |= static_cast<u32>(xfmem.alpha[j].ambsource.Value()) << (j + 2);
174 uid_data.attnfunc |= static_cast<u32>(xfmem.alpha[j].attnfunc.Value()) << (2 * (j + 2));
175 uid_data.diffusefunc |= static_cast<u32>(xfmem.alpha[j].diffusefunc.Value()) << (2 * (j + 2));
176 uid_data.light_mask |= xfmem.alpha[j].GetFullLightMask() << (8 * (j + 2));
177 }
178 }
179 }
180
181 static void GenerateCustomLightingImpl(ShaderCode* out, const LightingUidData& uid_data, int index,
182 int litchan_index, u32 channel_index, u32 custom_light_index,
183 bool alpha)
184 {
185 const auto attnfunc =
186 static_cast<AttenuationFunc>((uid_data.attnfunc >> (2 * litchan_index)) & 0x3);
187
188 const std::string_view light_type = alpha ? "alpha" : "color";
189 const std::string name = fmt::format("lights_chan{}_{}", channel_index, light_type);
190
191 out->Write("\t{{\n");
192 out->Write("\t\tfrag_input.{}[{}].direction = " LIGHT_DIR ".xyz;\n", name, custom_light_index,
193 LIGHT_DIR_PARAMS(index));
194 out->Write("\t\tfrag_input.{}[{}].position = " LIGHT_POS ".xyz;\n", name, custom_light_index,
195 LIGHT_POS_PARAMS(index));
196 out->Write("\t\tfrag_input.{}[{}].cosatt = " LIGHT_COSATT ";\n", name, custom_light_index,
197 LIGHT_COSATT_PARAMS(index));
198 out->Write("\t\tfrag_input.{}[{}].distatt = " LIGHT_DISTATT ";\n", name, custom_light_index,
199 LIGHT_DISTATT_PARAMS(index));
200 out->Write("\t\tfrag_input.{}[{}].attenuation_type = {};\n", name, custom_light_index,
201 static_cast<u32>(attnfunc));
202 if (alpha)
203 {
204 out->Write("\t\tfrag_input.{}[{}].color = float3(" LIGHT_COL
205 ") / float3(255.0, 255.0, 255.0);\n",
206 name, custom_light_index, LIGHT_COL_PARAMS(index, alpha ? "a" : "rgb"));
207 }
208 else
209 {
210 out->Write("\t\tfrag_input.{}[{}].color = " LIGHT_COL " / float3(255.0, 255.0, 255.0);\n", name,
211 custom_light_index, LIGHT_COL_PARAMS(index, alpha ? "a" : "rgb"));
212 }
213 out->Write("\t}}\n");
214 }
215
216 void GenerateCustomLighting(ShaderCode* out, const LightingUidData& uid_data)
217 {
218 for (u32 i = 0; i < 8; i++)
219 {
220 for (u32 channel_index = 0; channel_index < NUM_XF_COLOR_CHANNELS; channel_index++)
221 {
222 out->Write("\tfrag_input.lights_chan{}_color[{}].direction = float3(0, 0, 0);\n",
223 channel_index, i);
224 out->Write("\tfrag_input.lights_chan{}_color[{}].position = float3(0, 0, 0);\n",
225 channel_index, i);
226 out->Write("\tfrag_input.lights_chan{}_color[{}].color = float3(0, 0, 0);\n", channel_index,
227 i);
228 out->Write("\tfrag_input.lights_chan{}_color[{}].cosatt = float4(0, 0, 0, 0);\n",
229 channel_index, i);
230 out->Write("\tfrag_input.lights_chan{}_color[{}].distatt = float4(0, 0, 0, 0);\n",
231 channel_index, i);
232 out->Write("\tfrag_input.lights_chan{}_color[{}].attenuation_type = 0;\n", channel_index, i);
233
234 out->Write("\tfrag_input.lights_chan{}_alpha[{}].direction = float3(0, 0, 0);\n",
235 channel_index, i);
236 out->Write("\tfrag_input.lights_chan{}_alpha[{}].position = float3(0, 0, 0);\n",
237 channel_index, i);
238 out->Write("\tfrag_input.lights_chan{}_alpha[{}].color = float3(0, 0, 0);\n", channel_index,
239 i);
240 out->Write("\tfrag_input.lights_chan{}_alpha[{}].cosatt = float4(0, 0, 0, 0);\n",
241 channel_index, i);
242 out->Write("\tfrag_input.lights_chan{}_alpha[{}].distatt = float4(0, 0, 0, 0);\n",
243 channel_index, i);
244 out->Write("\tfrag_input.lights_chan{}_alpha[{}].attenuation_type = 0;\n", channel_index, i);
245 }
246 }
247
248 for (u32 j = 0; j < NUM_XF_COLOR_CHANNELS; j++)
249 {
250 const bool colormatsource = !!(uid_data.matsource & (1 << j));
251 if (colormatsource) // from vertex
252 out->Write("frag_input.base_material[{}] = frag_input.color_{};\n", j, j);
253 else // from color
254 out->Write("frag_input.base_material[{}] = {}[{}] / 255.0;\n", j, I_MATERIALS, j + 2);
255
256 if ((uid_data.enablelighting & (1 << j)) != 0)
257 {
258 if ((uid_data.ambsource & (1 << j)) != 0) // from vertex
259 out->Write("frag_input.ambient_lighting[{}] = frag_input.color_{};\n", j, j);
260 else // from color
261 out->Write("frag_input.ambient_lighting[{}] = {}[{}] / 255.0;\n", j, I_MATERIALS, j);
262 }
263 else
264 {
265 out->Write("frag_input.ambient_lighting[{}] = float4(1, 1, 1, 1);\n", j);
266 }
267
268 // check if alpha is different
269 const bool alphamatsource = !!(uid_data.matsource & (1 << (j + 2)));
270 if (alphamatsource != colormatsource)
271 {
272 if (alphamatsource) // from vertex
273 out->Write("frag_input.base_material[{}].w = frag_input.color_{}.w;\n", j, j);
274 else // from color
275 out->Write("frag_input.base_material[{}].w = {}[{}].w / 255.0;\n", j, I_MATERIALS, j + 2);
276 }
277
278 if ((uid_data.enablelighting & (1 << (j + 2))) != 0)
279 {
280 if ((uid_data.ambsource & (1 << (j + 2))) != 0) // from vertex
281 out->Write("frag_input.ambient_lighting[{}].w = frag_input.color_{}.w;\n", j, j);
282 else // from color
283 out->Write("frag_input.ambient_lighting[{}].w = {}[{}].w / 255.0;\n", j, I_MATERIALS, j);
284 }
285 else
286 {
287 out->Write("frag_input.ambient_lighting[{}].w = 1;\n", j);
288 }
289
290 u32 light_count = 0;
291 if ((uid_data.enablelighting & (1 << j)) != 0) // Color lights
292 {
293 for (int i = 0; i < 8; ++i)
294 {
295 if ((uid_data.light_mask & (1 << (i + 8 * j))) != 0)
296 {
297 GenerateCustomLightingImpl(out, uid_data, i, j, j, light_count, false);
298 light_count++;
299 }
300 }
301 }
302 out->Write("\tfrag_input.light_chan{}_color_count = {};\n", j, light_count);
303
304 light_count = 0;
305 if ((uid_data.enablelighting & (1 << (j + 2))) != 0) // Alpha lights
306 {
307 for (int i = 0; i < 8; ++i)
308 {
309 if ((uid_data.light_mask & (1 << (i + 8 * (j + 2)))) != 0)
310 {
311 GenerateCustomLightingImpl(out, uid_data, i, j + 2, j, light_count, true);
312 light_count++;
313 }
314 }
315 }
316 out->Write("\tfrag_input.light_chan{}_alpha_count = {};\n", j, light_count);
317 }
318 }