LightingShaderGen.cpp 14 KB

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