lighting.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/math_util.h"
  5. #include "video_core/swrasterizer/lighting.h"
  6. namespace Pica {
  7. static float LookupLightingLut(const Pica::State::Lighting& lighting, size_t lut_index, u8 index,
  8. float delta) {
  9. ASSERT_MSG(lut_index < lighting.luts.size(), "Out of range lut");
  10. ASSERT_MSG(index < lighting.luts[lut_index].size(), "Out of range index");
  11. const auto& lut = lighting.luts[lut_index][index];
  12. float lut_value = lut.ToFloat();
  13. float lut_diff = lut.DiffToFloat();
  14. return lut_value + lut_diff * delta;
  15. }
  16. std::tuple<Math::Vec4<u8>, Math::Vec4<u8>> ComputeFragmentsColors(
  17. const Pica::LightingRegs& lighting, const Pica::State::Lighting& lighting_state,
  18. const Math::Quaternion<float>& normquat, const Math::Vec3<float>& view,
  19. const Math::Vec4<u8> (&texture_color)[4]) {
  20. Math::Vec3<float> surface_normal;
  21. Math::Vec3<float> surface_tangent;
  22. if (lighting.config0.bump_mode != LightingRegs::LightingBumpMode::None) {
  23. Math::Vec3<float> perturbation =
  24. texture_color[lighting.config0.bump_selector].xyz().Cast<float>() / 127.5f -
  25. Math::MakeVec(1.0f, 1.0f, 1.0f);
  26. if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::NormalMap) {
  27. if (!lighting.config0.disable_bump_renorm) {
  28. const float z_square = 1 - perturbation.xy().Length2();
  29. perturbation.z = std::sqrt(std::max(z_square, 0.0f));
  30. }
  31. surface_normal = perturbation;
  32. surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f);
  33. } else if (lighting.config0.bump_mode == LightingRegs::LightingBumpMode::TangentMap) {
  34. surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f);
  35. surface_tangent = perturbation;
  36. } else {
  37. LOG_ERROR(HW_GPU, "Unknown bump mode %u", lighting.config0.bump_mode.Value());
  38. }
  39. } else {
  40. surface_normal = Math::MakeVec(0.0f, 0.0f, 1.0f);
  41. surface_tangent = Math::MakeVec(1.0f, 0.0f, 0.0f);
  42. }
  43. // Use the normalized the quaternion when performing the rotation
  44. auto normal = Math::QuaternionRotate(normquat, surface_normal);
  45. auto tangent = Math::QuaternionRotate(normquat, surface_tangent);
  46. Math::Vec4<float> diffuse_sum = {0.0f, 0.0f, 0.0f, 1.0f};
  47. Math::Vec4<float> specular_sum = {0.0f, 0.0f, 0.0f, 1.0f};
  48. for (unsigned light_index = 0; light_index <= lighting.max_light_index; ++light_index) {
  49. unsigned num = lighting.light_enable.GetNum(light_index);
  50. const auto& light_config = lighting.light[num];
  51. Math::Vec3<float> refl_value = {};
  52. Math::Vec3<float> position = {float16::FromRaw(light_config.x).ToFloat32(),
  53. float16::FromRaw(light_config.y).ToFloat32(),
  54. float16::FromRaw(light_config.z).ToFloat32()};
  55. Math::Vec3<float> light_vector;
  56. if (light_config.config.directional)
  57. light_vector = position;
  58. else
  59. light_vector = position + view;
  60. light_vector.Normalize();
  61. Math::Vec3<float> norm_view = view.Normalized();
  62. Math::Vec3<float> half_vector = norm_view + light_vector;
  63. float dist_atten = 1.0f;
  64. if (!lighting.IsDistAttenDisabled(num)) {
  65. auto distance = (-view - position).Length();
  66. float scale = Pica::float20::FromRaw(light_config.dist_atten_scale).ToFloat32();
  67. float bias = Pica::float20::FromRaw(light_config.dist_atten_bias).ToFloat32();
  68. size_t lut =
  69. static_cast<size_t>(LightingRegs::LightingSampler::DistanceAttenuation) + num;
  70. float sample_loc = MathUtil::Clamp(scale * distance + bias, 0.0f, 1.0f);
  71. u8 lutindex =
  72. static_cast<u8>(MathUtil::Clamp(std::floor(sample_loc * 256.0f), 0.0f, 255.0f));
  73. float delta = sample_loc * 256 - lutindex;
  74. dist_atten = LookupLightingLut(lighting_state, lut, lutindex, delta);
  75. }
  76. auto GetLutValue = [&](LightingRegs::LightingLutInput input, bool abs,
  77. LightingRegs::LightingScale scale_enum,
  78. LightingRegs::LightingSampler sampler) {
  79. float result = 0.0f;
  80. switch (input) {
  81. case LightingRegs::LightingLutInput::NH:
  82. result = Math::Dot(normal, half_vector.Normalized());
  83. break;
  84. case LightingRegs::LightingLutInput::VH:
  85. result = Math::Dot(norm_view, half_vector.Normalized());
  86. break;
  87. case LightingRegs::LightingLutInput::NV:
  88. result = Math::Dot(normal, norm_view);
  89. break;
  90. case LightingRegs::LightingLutInput::LN:
  91. result = Math::Dot(light_vector, normal);
  92. break;
  93. case LightingRegs::LightingLutInput::SP: {
  94. Math::Vec3<s32> spot_dir{light_config.spot_x.Value(), light_config.spot_y.Value(),
  95. light_config.spot_z.Value()};
  96. result = Math::Dot(light_vector, spot_dir.Cast<float>() / 2047.0f);
  97. break;
  98. }
  99. case LightingRegs::LightingLutInput::CP:
  100. if (lighting.config0.config == LightingRegs::LightingConfig::Config7) {
  101. const Math::Vec3<float> norm_half_vector = half_vector.Normalized();
  102. const Math::Vec3<float> half_vector_proj =
  103. norm_half_vector - normal * Math::Dot(normal, norm_half_vector);
  104. result = Math::Dot(half_vector_proj, tangent);
  105. } else {
  106. result = 0.0f;
  107. }
  108. break;
  109. default:
  110. LOG_CRITICAL(HW_GPU, "Unknown lighting LUT input %u\n", static_cast<u32>(input));
  111. UNIMPLEMENTED();
  112. result = 0.0f;
  113. }
  114. u8 index;
  115. float delta;
  116. if (abs) {
  117. if (light_config.config.two_sided_diffuse)
  118. result = std::abs(result);
  119. else
  120. result = std::max(result, 0.0f);
  121. float flr = std::floor(result * 256.0f);
  122. index = static_cast<u8>(MathUtil::Clamp(flr, 0.0f, 255.0f));
  123. delta = result * 256 - index;
  124. } else {
  125. float flr = std::floor(result * 128.0f);
  126. s8 signed_index = static_cast<s8>(MathUtil::Clamp(flr, -128.0f, 127.0f));
  127. delta = result * 128.0f - signed_index;
  128. index = static_cast<u8>(signed_index);
  129. }
  130. float scale = lighting.lut_scale.GetScale(scale_enum);
  131. return scale *
  132. LookupLightingLut(lighting_state, static_cast<size_t>(sampler), index, delta);
  133. };
  134. // If enabled, compute spot light attenuation value
  135. float spot_atten = 1.0f;
  136. if (!lighting.IsSpotAttenDisabled(num) &&
  137. LightingRegs::IsLightingSamplerSupported(
  138. lighting.config0.config, LightingRegs::LightingSampler::SpotlightAttenuation)) {
  139. auto lut = LightingRegs::SpotlightAttenuationSampler(num);
  140. spot_atten = GetLutValue(lighting.lut_input.sp, lighting.abs_lut_input.disable_sp == 0,
  141. lighting.lut_scale.sp, lut);
  142. }
  143. // Specular 0 component
  144. float d0_lut_value = 1.0f;
  145. if (lighting.config1.disable_lut_d0 == 0 &&
  146. LightingRegs::IsLightingSamplerSupported(
  147. lighting.config0.config, LightingRegs::LightingSampler::Distribution0)) {
  148. d0_lut_value =
  149. GetLutValue(lighting.lut_input.d0, lighting.abs_lut_input.disable_d0 == 0,
  150. lighting.lut_scale.d0, LightingRegs::LightingSampler::Distribution0);
  151. }
  152. Math::Vec3<float> specular_0 = d0_lut_value * light_config.specular_0.ToVec3f();
  153. // If enabled, lookup ReflectRed value, otherwise, 1.0 is used
  154. if (lighting.config1.disable_lut_rr == 0 &&
  155. LightingRegs::IsLightingSamplerSupported(lighting.config0.config,
  156. LightingRegs::LightingSampler::ReflectRed)) {
  157. refl_value.x =
  158. GetLutValue(lighting.lut_input.rr, lighting.abs_lut_input.disable_rr == 0,
  159. lighting.lut_scale.rr, LightingRegs::LightingSampler::ReflectRed);
  160. } else {
  161. refl_value.x = 1.0f;
  162. }
  163. // If enabled, lookup ReflectGreen value, otherwise, ReflectRed value is used
  164. if (lighting.config1.disable_lut_rg == 0 &&
  165. LightingRegs::IsLightingSamplerSupported(lighting.config0.config,
  166. LightingRegs::LightingSampler::ReflectGreen)) {
  167. refl_value.y =
  168. GetLutValue(lighting.lut_input.rg, lighting.abs_lut_input.disable_rg == 0,
  169. lighting.lut_scale.rg, LightingRegs::LightingSampler::ReflectGreen);
  170. } else {
  171. refl_value.y = refl_value.x;
  172. }
  173. // If enabled, lookup ReflectBlue value, otherwise, ReflectRed value is used
  174. if (lighting.config1.disable_lut_rb == 0 &&
  175. LightingRegs::IsLightingSamplerSupported(lighting.config0.config,
  176. LightingRegs::LightingSampler::ReflectBlue)) {
  177. refl_value.z =
  178. GetLutValue(lighting.lut_input.rb, lighting.abs_lut_input.disable_rb == 0,
  179. lighting.lut_scale.rb, LightingRegs::LightingSampler::ReflectBlue);
  180. } else {
  181. refl_value.z = refl_value.x;
  182. }
  183. // Specular 1 component
  184. float d1_lut_value = 1.0f;
  185. if (lighting.config1.disable_lut_d1 == 0 &&
  186. LightingRegs::IsLightingSamplerSupported(
  187. lighting.config0.config, LightingRegs::LightingSampler::Distribution1)) {
  188. d1_lut_value =
  189. GetLutValue(lighting.lut_input.d1, lighting.abs_lut_input.disable_d1 == 0,
  190. lighting.lut_scale.d1, LightingRegs::LightingSampler::Distribution1);
  191. }
  192. Math::Vec3<float> specular_1 =
  193. d1_lut_value * refl_value * light_config.specular_1.ToVec3f();
  194. // Fresnel
  195. // Note: only the last entry in the light slots applies the Fresnel factor
  196. if (light_index == lighting.max_light_index && lighting.config1.disable_lut_fr == 0 &&
  197. LightingRegs::IsLightingSamplerSupported(lighting.config0.config,
  198. LightingRegs::LightingSampler::Fresnel)) {
  199. float lut_value =
  200. GetLutValue(lighting.lut_input.fr, lighting.abs_lut_input.disable_fr == 0,
  201. lighting.lut_scale.fr, LightingRegs::LightingSampler::Fresnel);
  202. // Enabled for diffuse lighting alpha component
  203. if (lighting.config0.fresnel_selector ==
  204. LightingRegs::LightingFresnelSelector::PrimaryAlpha ||
  205. lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) {
  206. diffuse_sum.a() = lut_value;
  207. }
  208. // Enabled for the specular lighting alpha component
  209. if (lighting.config0.fresnel_selector ==
  210. LightingRegs::LightingFresnelSelector::SecondaryAlpha ||
  211. lighting.config0.fresnel_selector == LightingRegs::LightingFresnelSelector::Both) {
  212. specular_sum.a() = lut_value;
  213. }
  214. }
  215. auto dot_product = Math::Dot(light_vector, normal);
  216. // Calculate clamp highlights before applying the two-sided diffuse configuration to the dot
  217. // product.
  218. float clamp_highlights = 1.0f;
  219. if (lighting.config0.clamp_highlights) {
  220. if (dot_product <= 0.0f)
  221. clamp_highlights = 0.0f;
  222. else
  223. clamp_highlights = 1.0f;
  224. }
  225. if (light_config.config.two_sided_diffuse)
  226. dot_product = std::abs(dot_product);
  227. else
  228. dot_product = std::max(dot_product, 0.0f);
  229. if (light_config.config.geometric_factor_0 || light_config.config.geometric_factor_1) {
  230. float geo_factor = half_vector.Length2();
  231. geo_factor = geo_factor == 0.0f ? 0.0f : std::min(dot_product / geo_factor, 1.0f);
  232. if (light_config.config.geometric_factor_0) {
  233. specular_0 *= geo_factor;
  234. }
  235. if (light_config.config.geometric_factor_1) {
  236. specular_1 *= geo_factor;
  237. }
  238. }
  239. auto diffuse =
  240. light_config.diffuse.ToVec3f() * dot_product + light_config.ambient.ToVec3f();
  241. diffuse_sum += Math::MakeVec(diffuse * dist_atten * spot_atten, 0.0f);
  242. specular_sum += Math::MakeVec(
  243. (specular_0 + specular_1) * clamp_highlights * dist_atten * spot_atten, 0.0f);
  244. }
  245. diffuse_sum += Math::MakeVec(lighting.global_ambient.ToVec3f(), 0.0f);
  246. auto diffuse = Math::MakeVec<float>(MathUtil::Clamp(diffuse_sum.x, 0.0f, 1.0f) * 255,
  247. MathUtil::Clamp(diffuse_sum.y, 0.0f, 1.0f) * 255,
  248. MathUtil::Clamp(diffuse_sum.z, 0.0f, 1.0f) * 255,
  249. MathUtil::Clamp(diffuse_sum.w, 0.0f, 1.0f) * 255)
  250. .Cast<u8>();
  251. auto specular = Math::MakeVec<float>(MathUtil::Clamp(specular_sum.x, 0.0f, 1.0f) * 255,
  252. MathUtil::Clamp(specular_sum.y, 0.0f, 1.0f) * 255,
  253. MathUtil::Clamp(specular_sum.z, 0.0f, 1.0f) * 255,
  254. MathUtil::Clamp(specular_sum.w, 0.0f, 1.0f) * 255)
  255. .Cast<u8>();
  256. return std::make_tuple(diffuse, specular);
  257. }
  258. } // namespace Pica