scene_forward_mobile.glsl 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. /* Include our forward mobile UBOs definitions etc. */
  5. #include "scene_forward_mobile_inc.glsl"
  6. /* INPUT ATTRIBS */
  7. layout(location = 0) in vec3 vertex_attrib;
  8. //only for pure render depth when normal is not used
  9. #ifdef NORMAL_USED
  10. layout(location = 1) in vec3 normal_attrib;
  11. #endif
  12. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  13. layout(location = 2) in vec4 tangent_attrib;
  14. #endif
  15. #if defined(COLOR_USED)
  16. layout(location = 3) in vec4 color_attrib;
  17. #endif
  18. #ifdef UV_USED
  19. layout(location = 4) in vec2 uv_attrib;
  20. #endif
  21. #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
  22. layout(location = 5) in vec2 uv2_attrib;
  23. #endif // MODE_RENDER_MATERIAL
  24. #if defined(CUSTOM0_USED)
  25. layout(location = 6) in vec4 custom0_attrib;
  26. #endif
  27. #if defined(CUSTOM1_USED)
  28. layout(location = 7) in vec4 custom1_attrib;
  29. #endif
  30. #if defined(CUSTOM2_USED)
  31. layout(location = 8) in vec4 custom2_attrib;
  32. #endif
  33. #if defined(CUSTOM3_USED)
  34. layout(location = 9) in vec4 custom3_attrib;
  35. #endif
  36. #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
  37. layout(location = 10) in uvec4 bone_attrib;
  38. #endif
  39. #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
  40. layout(location = 11) in vec4 weight_attrib;
  41. #endif
  42. /* Varyings */
  43. layout(location = 0) highp out vec3 vertex_interp;
  44. #ifdef NORMAL_USED
  45. layout(location = 1) mediump out vec3 normal_interp;
  46. #endif
  47. #if defined(COLOR_USED)
  48. layout(location = 2) mediump out vec4 color_interp;
  49. #endif
  50. #ifdef UV_USED
  51. layout(location = 3) mediump out vec2 uv_interp;
  52. #endif
  53. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  54. layout(location = 4) mediump out vec2 uv2_interp;
  55. #endif
  56. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  57. layout(location = 5) mediump out vec3 tangent_interp;
  58. layout(location = 6) mediump out vec3 binormal_interp;
  59. #endif
  60. #ifdef MATERIAL_UNIFORMS_USED
  61. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  62. #MATERIAL_UNIFORMS
  63. } material;
  64. #endif
  65. #ifdef MODE_DUAL_PARABOLOID
  66. layout(location = 8) out highp float dp_clip;
  67. #endif
  68. #ifdef USE_MULTIVIEW
  69. #ifdef has_VK_KHR_multiview
  70. #define ViewIndex gl_ViewIndex
  71. #else
  72. // !BAS! This needs to become an input once we implement our fallback!
  73. #define ViewIndex 0
  74. #endif
  75. #else
  76. // Set to zero, not supported in non stereo
  77. #define ViewIndex 0
  78. #endif //USE_MULTIVIEW
  79. invariant gl_Position;
  80. #GLOBALS
  81. void main() {
  82. vec4 instance_custom = vec4(0.0);
  83. #if defined(COLOR_USED)
  84. color_interp = color_attrib;
  85. #endif
  86. bool is_multimesh = bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH);
  87. mat4 world_matrix = draw_call.transform;
  88. mat3 world_normal_matrix;
  89. if (bool(draw_call.flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
  90. world_normal_matrix = transpose(inverse(mat3(world_matrix)));
  91. } else {
  92. world_normal_matrix = mat3(world_matrix);
  93. }
  94. if (is_multimesh) {
  95. //multimesh, instances are for it
  96. mat4 matrix;
  97. #ifdef USE_PARTICLE_TRAILS
  98. uint trail_size = (draw_call.flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
  99. uint stride = 3 + 1 + 1; //particles always uses this format
  100. uint offset = trail_size * stride * gl_InstanceIndex;
  101. #ifdef COLOR_USED
  102. vec4 pcolor;
  103. #endif
  104. {
  105. uint boffset = offset + bone_attrib.x * stride;
  106. matrix = mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.x;
  107. #ifdef COLOR_USED
  108. pcolor = transforms.data[boffset + 3] * weight_attrib.x;
  109. #endif
  110. }
  111. if (weight_attrib.y > 0.001) {
  112. uint boffset = offset + bone_attrib.y * stride;
  113. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.y;
  114. #ifdef COLOR_USED
  115. pcolor += transforms.data[boffset + 3] * weight_attrib.y;
  116. #endif
  117. }
  118. if (weight_attrib.z > 0.001) {
  119. uint boffset = offset + bone_attrib.z * stride;
  120. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.z;
  121. #ifdef COLOR_USED
  122. pcolor += transforms.data[boffset + 3] * weight_attrib.z;
  123. #endif
  124. }
  125. if (weight_attrib.w > 0.001) {
  126. uint boffset = offset + bone_attrib.w * stride;
  127. matrix += mat4(transforms.data[boffset + 0], transforms.data[boffset + 1], transforms.data[boffset + 2], vec4(0.0, 0.0, 0.0, 1.0)) * weight_attrib.w;
  128. #ifdef COLOR_USED
  129. pcolor += transforms.data[boffset + 3] * weight_attrib.w;
  130. #endif
  131. }
  132. instance_custom = transforms.data[offset + 4];
  133. #ifdef COLOR_USED
  134. color_interp *= pcolor;
  135. #endif
  136. #else
  137. uint stride = 0;
  138. {
  139. //TODO implement a small lookup table for the stride
  140. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  141. stride += 2;
  142. } else {
  143. stride += 3;
  144. }
  145. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  146. stride += 1;
  147. }
  148. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  149. stride += 1;
  150. }
  151. }
  152. uint offset = stride * gl_InstanceIndex;
  153. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  154. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  155. offset += 2;
  156. } else {
  157. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
  158. offset += 3;
  159. }
  160. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  161. #ifdef COLOR_USED
  162. color_interp *= transforms.data[offset];
  163. #endif
  164. offset += 1;
  165. }
  166. if (bool(draw_call.flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  167. instance_custom = transforms.data[offset];
  168. }
  169. #endif
  170. //transpose
  171. matrix = transpose(matrix);
  172. world_matrix = world_matrix * matrix;
  173. world_normal_matrix = world_normal_matrix * mat3(matrix);
  174. }
  175. vec3 vertex = vertex_attrib;
  176. #ifdef NORMAL_USED
  177. vec3 normal = normal_attrib * 2.0 - 1.0;
  178. #endif
  179. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  180. vec3 tangent = tangent_attrib.xyz * 2.0 - 1.0;
  181. float binormalf = tangent_attrib.a * 2.0 - 1.0;
  182. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  183. #endif
  184. #ifdef UV_USED
  185. uv_interp = uv_attrib;
  186. #endif
  187. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  188. uv2_interp = uv2_attrib;
  189. #endif
  190. #ifdef OVERRIDE_POSITION
  191. vec4 position;
  192. #endif
  193. #ifdef USE_MULTIVIEW
  194. mat4 projection_matrix = scene_data.projection_matrix_view[ViewIndex];
  195. mat4 inv_projection_matrix = scene_data.inv_projection_matrix_view[ViewIndex];
  196. #else
  197. mat4 projection_matrix = scene_data.projection_matrix;
  198. mat4 inv_projection_matrix = scene_data.inv_projection_matrix;
  199. #endif //USE_MULTIVIEW
  200. //using world coordinates
  201. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  202. vertex = (world_matrix * vec4(vertex, 1.0)).xyz;
  203. normal = world_normal_matrix * normal;
  204. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  205. tangent = world_normal_matrix * tangent;
  206. binormal = world_normal_matrix * binormal;
  207. #endif
  208. #endif
  209. float roughness = 1.0;
  210. mat4 modelview = scene_data.inv_camera_matrix * world_matrix;
  211. mat3 modelview_normal = mat3(scene_data.inv_camera_matrix) * world_normal_matrix;
  212. {
  213. #CODE : VERTEX
  214. }
  215. /* output */
  216. // using local coordinates (default)
  217. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  218. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  219. #ifdef NORMAL_USED
  220. normal = modelview_normal * normal;
  221. #endif
  222. #endif
  223. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  224. binormal = modelview_normal * binormal;
  225. tangent = modelview_normal * tangent;
  226. #endif
  227. //using world coordinates
  228. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  229. vertex = (scene_data.inv_camera_matrix * vec4(vertex, 1.0)).xyz;
  230. normal = mat3(scene_data.inverse_normal_matrix) * normal;
  231. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  232. binormal = mat3(scene_data.camera_inverse_binormal_matrix) * binormal;
  233. tangent = mat3(scene_data.camera_inverse_tangent_matrix) * tangent;
  234. #endif
  235. #endif
  236. vertex_interp = vertex;
  237. #ifdef NORMAL_USED
  238. normal_interp = normal;
  239. #endif
  240. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  241. tangent_interp = tangent;
  242. binormal_interp = binormal;
  243. #endif
  244. #ifdef MODE_RENDER_DEPTH
  245. #ifdef MODE_DUAL_PARABOLOID
  246. vertex_interp.z *= scene_data.dual_paraboloid_side;
  247. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  248. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  249. vec3 vtx = vertex_interp;
  250. float distance = length(vtx);
  251. vtx = normalize(vtx);
  252. vtx.xy /= 1.0 - vtx.z;
  253. vtx.z = (distance / scene_data.z_far);
  254. vtx.z = vtx.z * 2.0 - 1.0;
  255. vertex_interp = vtx;
  256. #endif
  257. #endif //MODE_RENDER_DEPTH
  258. #ifdef OVERRIDE_POSITION
  259. gl_Position = position;
  260. #else
  261. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  262. #endif // OVERRIDE_POSITION
  263. #ifdef MODE_RENDER_DEPTH
  264. if (scene_data.pancake_shadows) {
  265. if (gl_Position.z <= 0.00001) {
  266. gl_Position.z = 0.00001;
  267. }
  268. }
  269. #endif // MODE_RENDER_DEPTH
  270. #ifdef MODE_RENDER_MATERIAL
  271. if (scene_data.material_uv2_mode) {
  272. vec2 uv_offset = draw_call.lightmap_uv_scale.xy; // we are abusing lightmap_uv_scale here, we shouldn't have a lightmap during a depth pass...
  273. gl_Position.xy = (uv2_attrib.xy + uv_offset) * 2.0 - 1.0;
  274. gl_Position.z = 0.00001;
  275. gl_Position.w = 1.0;
  276. }
  277. #endif // MODE_RENDER_MATERIAL
  278. }
  279. #[fragment]
  280. #version 450
  281. #VERSION_DEFINES
  282. /* Specialization Constants */
  283. #if !defined(MODE_RENDER_DEPTH)
  284. #if !defined(MODE_UNSHADED)
  285. layout(constant_id = 0) const bool sc_use_light_projector = false;
  286. layout(constant_id = 1) const bool sc_use_light_soft_shadows = false;
  287. layout(constant_id = 2) const bool sc_use_directional_soft_shadows = false;
  288. layout(constant_id = 3) const uint sc_soft_shadow_samples = 4;
  289. layout(constant_id = 4) const uint sc_penumbra_shadow_samples = 4;
  290. layout(constant_id = 5) const uint sc_directional_soft_shadow_samples = 4;
  291. layout(constant_id = 6) const uint sc_directional_penumbra_shadow_samples = 4;
  292. layout(constant_id = 8) const bool sc_projector_use_mipmaps = true;
  293. layout(constant_id = 9) const bool sc_disable_omni_lights = false;
  294. layout(constant_id = 10) const bool sc_disable_spot_lights = false;
  295. layout(constant_id = 11) const bool sc_disable_reflection_probes = false;
  296. layout(constant_id = 12) const bool sc_disable_directional_lights = false;
  297. #endif //!MODE_UNSHADED
  298. layout(constant_id = 7) const bool sc_decal_use_mipmaps = true;
  299. layout(constant_id = 13) const bool sc_disable_decals = false;
  300. layout(constant_id = 14) const bool sc_disable_fog = false;
  301. #endif //!MODE_RENDER_DEPTH
  302. layout(constant_id = 15) const float sc_luminance_multiplier = 2.0;
  303. /* Include our forward mobile UBOs definitions etc. */
  304. #include "scene_forward_mobile_inc.glsl"
  305. /* Varyings */
  306. layout(location = 0) highp in vec3 vertex_interp;
  307. #ifdef NORMAL_USED
  308. layout(location = 1) mediump in vec3 normal_interp;
  309. #endif
  310. #if defined(COLOR_USED)
  311. layout(location = 2) mediump in vec4 color_interp;
  312. #endif
  313. #ifdef UV_USED
  314. layout(location = 3) mediump in vec2 uv_interp;
  315. #endif
  316. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  317. layout(location = 4) mediump in vec2 uv2_interp;
  318. #endif
  319. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  320. layout(location = 5) mediump in vec3 tangent_interp;
  321. layout(location = 6) mediump in vec3 binormal_interp;
  322. #endif
  323. #ifdef MODE_DUAL_PARABOLOID
  324. layout(location = 8) highp in float dp_clip;
  325. #endif
  326. #ifdef USE_MULTIVIEW
  327. #ifdef has_VK_KHR_multiview
  328. #define ViewIndex gl_ViewIndex
  329. #else
  330. // !BAS! This needs to become an input once we implement our fallback!
  331. #define ViewIndex 0
  332. #endif
  333. #else
  334. // Set to zero, not supported in non stereo
  335. #define ViewIndex 0
  336. #endif //USE_MULTIVIEW
  337. //defines to keep compatibility with vertex
  338. #define world_matrix draw_call.transform
  339. #ifdef USE_MULTIVIEW
  340. #define projection_matrix scene_data.projection_matrix_view[ViewIndex]
  341. #else
  342. #define projection_matrix scene_data.projection_matrix
  343. #endif
  344. #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
  345. //both required for transmittance to be enabled
  346. #define LIGHT_TRANSMITTANCE_USED
  347. #endif
  348. #ifdef MATERIAL_UNIFORMS_USED
  349. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  350. #MATERIAL_UNIFORMS
  351. } material;
  352. #endif
  353. #GLOBALS
  354. /* clang-format on */
  355. #ifdef MODE_RENDER_DEPTH
  356. #ifdef MODE_RENDER_MATERIAL
  357. layout(location = 0) out vec4 albedo_output_buffer;
  358. layout(location = 1) out vec4 normal_output_buffer;
  359. layout(location = 2) out vec4 orm_output_buffer;
  360. layout(location = 3) out vec4 emission_output_buffer;
  361. layout(location = 4) out float depth_output_buffer;
  362. #endif // MODE_RENDER_MATERIAL
  363. #else // RENDER DEPTH
  364. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  365. layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
  366. layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
  367. #else
  368. layout(location = 0) out mediump vec4 frag_color;
  369. #endif // MODE_MULTIPLE_RENDER_TARGETS
  370. #endif // RENDER DEPTH
  371. #include "scene_forward_aa_inc.glsl"
  372. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  373. /* Make a default specular mode SPECULAR_SCHLICK_GGX. */
  374. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_BLINN) && !defined(SPECULAR_PHONG) && !defined(SPECULAR_TOON)
  375. #define SPECULAR_SCHLICK_GGX
  376. #endif
  377. #include "scene_forward_lights_inc.glsl"
  378. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  379. #ifndef MODE_RENDER_DEPTH
  380. /*
  381. Only supporting normal fog here.
  382. */
  383. vec4 fog_process(vec3 vertex) {
  384. vec3 fog_color = scene_data.fog_light_color;
  385. if (scene_data.fog_aerial_perspective > 0.0) {
  386. vec3 sky_fog_color = vec3(0.0);
  387. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  388. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  389. float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near));
  390. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  391. float lod, blend;
  392. blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
  393. sky_fog_color = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod)).rgb;
  394. sky_fog_color = mix(sky_fog_color, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod + 1)).rgb, blend);
  395. #else
  396. sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
  397. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  398. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  399. }
  400. if (scene_data.fog_sun_scatter > 0.001) {
  401. vec4 sun_scatter = vec4(0.0);
  402. float sun_total = 0.0;
  403. vec3 view = normalize(vertex);
  404. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  405. vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
  406. float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
  407. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  408. }
  409. }
  410. float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  411. if (abs(scene_data.fog_height_density) >= 0.0001) {
  412. float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y;
  413. float y_dist = y - scene_data.fog_height;
  414. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  415. fog_amount = max(vfog_amount, fog_amount);
  416. }
  417. return vec4(fog_color, fog_amount);
  418. }
  419. #endif //!MODE_RENDER DEPTH
  420. void main() {
  421. #ifdef MODE_DUAL_PARABOLOID
  422. if (dp_clip > 0.0)
  423. discard;
  424. #endif
  425. //lay out everything, whathever is unused is optimized away anyway
  426. vec3 vertex = vertex_interp;
  427. vec3 view = -normalize(vertex_interp);
  428. vec3 albedo = vec3(1.0);
  429. vec3 backlight = vec3(0.0);
  430. vec4 transmittance_color = vec4(0.0);
  431. float transmittance_depth = 0.0;
  432. float transmittance_boost = 0.0;
  433. float metallic = 0.0;
  434. float specular = 0.5;
  435. vec3 emission = vec3(0.0);
  436. float roughness = 1.0;
  437. float rim = 0.0;
  438. float rim_tint = 0.0;
  439. float clearcoat = 0.0;
  440. float clearcoat_gloss = 0.0;
  441. float anisotropy = 0.0;
  442. vec2 anisotropy_flow = vec2(1.0, 0.0);
  443. vec4 fog = vec4(0.0);
  444. #if defined(CUSTOM_RADIANCE_USED)
  445. vec4 custom_radiance = vec4(0.0);
  446. #endif
  447. #if defined(CUSTOM_IRRADIANCE_USED)
  448. vec4 custom_irradiance = vec4(0.0);
  449. #endif
  450. float ao = 1.0;
  451. float ao_light_affect = 0.0;
  452. float alpha = 1.0;
  453. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  454. vec3 binormal = normalize(binormal_interp);
  455. vec3 tangent = normalize(tangent_interp);
  456. #else
  457. vec3 binormal = vec3(0.0);
  458. vec3 tangent = vec3(0.0);
  459. #endif
  460. #ifdef NORMAL_USED
  461. vec3 normal = normalize(normal_interp);
  462. #if defined(DO_SIDE_CHECK)
  463. if (!gl_FrontFacing) {
  464. normal = -normal;
  465. }
  466. #endif
  467. #endif //NORMAL_USED
  468. #ifdef UV_USED
  469. vec2 uv = uv_interp;
  470. #endif
  471. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  472. vec2 uv2 = uv2_interp;
  473. #endif
  474. #if defined(COLOR_USED)
  475. vec4 color = color_interp;
  476. #endif
  477. #if defined(NORMAL_MAP_USED)
  478. vec3 normal_map = vec3(0.5);
  479. #endif
  480. float normal_map_depth = 1.0;
  481. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size + scene_data.screen_pixel_size * 0.5; //account for center
  482. float sss_strength = 0.0;
  483. #ifdef ALPHA_SCISSOR_USED
  484. float alpha_scissor_threshold = 1.0;
  485. #endif // ALPHA_SCISSOR_USED
  486. #ifdef ALPHA_HASH_USED
  487. float alpha_hash_scale = 1.0;
  488. #endif // ALPHA_HASH_USED
  489. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  490. float alpha_antialiasing_edge = 0.0;
  491. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  492. #endif // ALPHA_ANTIALIASING_EDGE_USED
  493. {
  494. #CODE : FRAGMENT
  495. }
  496. #ifdef LIGHT_TRANSMITTANCE_USED
  497. #ifdef SSS_MODE_SKIN
  498. transmittance_color.a = sss_strength;
  499. #else
  500. transmittance_color.a *= sss_strength;
  501. #endif
  502. #endif
  503. #ifndef USE_SHADOW_TO_OPACITY
  504. #ifdef ALPHA_SCISSOR_USED
  505. if (alpha < alpha_scissor_threshold) {
  506. discard;
  507. }
  508. #endif // ALPHA_SCISSOR_USED
  509. // alpha hash can be used in unison with alpha antialiasing
  510. #ifdef ALPHA_HASH_USED
  511. if (alpha < compute_alpha_hash_threshold(vertex, alpha_hash_scale)) {
  512. discard;
  513. }
  514. #endif // ALPHA_HASH_USED
  515. // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
  516. #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED)
  517. alpha = 1.0;
  518. #endif
  519. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  520. // If alpha scissor is used, we must further the edge threshold, otherwise we won't get any edge feather
  521. #ifdef ALPHA_SCISSOR_USED
  522. alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, 0.0, 1.0);
  523. #endif
  524. alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
  525. #endif // ALPHA_ANTIALIASING_EDGE_USED
  526. #ifdef USE_OPAQUE_PREPASS
  527. if (alpha < opaque_prepass_threshold) {
  528. discard;
  529. }
  530. #endif // USE_OPAQUE_PREPASS
  531. #endif // !USE_SHADOW_TO_OPACITY
  532. #ifdef NORMAL_MAP_USED
  533. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  534. normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc.
  535. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  536. #endif
  537. #ifdef LIGHT_ANISOTROPY_USED
  538. if (anisotropy > 0.01) {
  539. //rotation matrix
  540. mat3 rot = mat3(tangent, binormal, normal);
  541. //make local to space
  542. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  543. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  544. }
  545. #endif
  546. #ifdef ENABLE_CLIP_ALPHA
  547. if (albedo.a < 0.99) {
  548. //used for doublepass and shadowmapping
  549. discard;
  550. }
  551. #endif
  552. /////////////////////// FOG //////////////////////
  553. #ifndef MODE_RENDER_DEPTH
  554. #ifndef CUSTOM_FOG_USED
  555. // fog must be processed as early as possible and then packed.
  556. // to maximize VGPR usage
  557. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  558. if (!sc_disable_fog && scene_data.fog_enabled) {
  559. fog = fog_process(vertex);
  560. }
  561. #endif //!CUSTOM_FOG_USED
  562. uint fog_rg = packHalf2x16(fog.rg);
  563. uint fog_ba = packHalf2x16(fog.ba);
  564. #endif //!MODE_RENDER_DEPTH
  565. /////////////////////// DECALS ////////////////////////////////
  566. #ifndef MODE_RENDER_DEPTH
  567. vec3 vertex_ddx = dFdx(vertex);
  568. vec3 vertex_ddy = dFdy(vertex);
  569. if (!sc_disable_decals) { //Decals
  570. // must implement
  571. uint decal_indices = draw_call.decals.x;
  572. for (uint i = 0; i < 8; i++) {
  573. uint decal_index = decal_indices & 0xFF;
  574. if (i == 4) {
  575. decal_indices = draw_call.decals.y;
  576. } else {
  577. decal_indices = decal_indices >> 8;
  578. }
  579. if (decal_index == 0xFF) {
  580. break;
  581. }
  582. vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
  583. if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
  584. continue; //out of decal
  585. }
  586. float fade = pow(1.0 - (uv_local.y > 0.0 ? uv_local.y : -uv_local.y), uv_local.y > 0.0 ? decals.data[decal_index].upper_fade : decals.data[decal_index].lower_fade);
  587. if (decals.data[decal_index].normal_fade > 0.0) {
  588. fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(normal_interp, decals.data[decal_index].normal) * 0.5 + 0.5);
  589. }
  590. //we need ddx/ddy for mipmaps, so simulate them
  591. vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
  592. vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
  593. if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
  594. //has albedo
  595. vec4 decal_albedo;
  596. if (sc_decal_use_mipmaps) {
  597. decal_albedo = textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, ddx * decals.data[decal_index].albedo_rect.zw, ddy * decals.data[decal_index].albedo_rect.zw);
  598. } else {
  599. decal_albedo = textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].albedo_rect.zw + decals.data[decal_index].albedo_rect.xy, 0.0);
  600. }
  601. decal_albedo *= decals.data[decal_index].modulate;
  602. decal_albedo.a *= fade;
  603. albedo = mix(albedo, decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix);
  604. if (decals.data[decal_index].normal_rect != vec4(0.0)) {
  605. vec3 decal_normal;
  606. if (sc_decal_use_mipmaps) {
  607. decal_normal = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, ddx * decals.data[decal_index].normal_rect.zw, ddy * decals.data[decal_index].normal_rect.zw).xyz;
  608. } else {
  609. decal_normal = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].normal_rect.zw + decals.data[decal_index].normal_rect.xy, 0.0).xyz;
  610. }
  611. decal_normal.xy = decal_normal.xy * vec2(2.0, -2.0) - vec2(1.0, -1.0); //users prefer flipped y normal maps in most authoring software
  612. decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
  613. //convert to view space, use xzy because y is up
  614. decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
  615. normal = normalize(mix(normal, decal_normal, decal_albedo.a));
  616. }
  617. if (decals.data[decal_index].orm_rect != vec4(0.0)) {
  618. vec3 decal_orm;
  619. if (sc_decal_use_mipmaps) {
  620. decal_orm = textureGrad(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, ddx * decals.data[decal_index].orm_rect.zw, ddy * decals.data[decal_index].orm_rect.zw).xyz;
  621. } else {
  622. decal_orm = textureLod(sampler2D(decal_atlas, decal_sampler), uv_local.xz * decals.data[decal_index].orm_rect.zw + decals.data[decal_index].orm_rect.xy, 0.0).xyz;
  623. }
  624. ao = mix(ao, decal_orm.r, decal_albedo.a);
  625. roughness = mix(roughness, decal_orm.g, decal_albedo.a);
  626. metallic = mix(metallic, decal_orm.b, decal_albedo.a);
  627. }
  628. }
  629. if (decals.data[decal_index].emission_rect != vec4(0.0)) {
  630. //emission is additive, so its independent from albedo
  631. if (sc_decal_use_mipmaps) {
  632. emission += textureGrad(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, ddx * decals.data[decal_index].emission_rect.zw, ddy * decals.data[decal_index].emission_rect.zw).xyz * decals.data[decal_index].emission_energy * fade;
  633. } else {
  634. emission += textureLod(sampler2D(decal_atlas_srgb, decal_sampler), uv_local.xz * decals.data[decal_index].emission_rect.zw + decals.data[decal_index].emission_rect.xy, 0.0).xyz * decals.data[decal_index].emission_energy * fade;
  635. }
  636. }
  637. }
  638. } //Decals
  639. #endif //!MODE_RENDER_DEPTH
  640. /////////////////////// LIGHTING //////////////////////////////
  641. #ifdef NORMAL_USED
  642. if (scene_data.roughness_limiter_enabled) {
  643. //https://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
  644. float roughness2 = roughness * roughness;
  645. vec3 dndu = dFdx(normal), dndv = dFdy(normal);
  646. float variance = scene_data.roughness_limiter_amount * (dot(dndu, dndu) + dot(dndv, dndv));
  647. float kernelRoughness2 = min(2.0 * variance, scene_data.roughness_limiter_limit); //limit effect
  648. float filteredRoughness2 = min(1.0, roughness2 + kernelRoughness2);
  649. roughness = sqrt(filteredRoughness2);
  650. }
  651. #endif // NORMAL_USED
  652. //apply energy conservation
  653. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  654. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  655. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  656. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  657. if (scene_data.use_reflection_cubemap) {
  658. vec3 ref_vec = reflect(-view, normal);
  659. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  660. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  661. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  662. float lod, blend;
  663. blend = modf(roughness * MAX_ROUGHNESS_LOD, lod);
  664. specular_light = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod)).rgb;
  665. specular_light = mix(specular_light, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod + 1)).rgb, blend);
  666. #else // USE_RADIANCE_CUBEMAP_ARRAY
  667. specular_light = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ref_vec, roughness * MAX_ROUGHNESS_LOD).rgb;
  668. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  669. specular_light *= horizon * horizon;
  670. specular_light *= scene_data.ambient_light_color_energy.a;
  671. }
  672. #if defined(CUSTOM_RADIANCE_USED)
  673. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  674. #endif // CUSTOM_RADIANCE_USED
  675. #ifndef USE_LIGHTMAP
  676. //lightmap overrides everything
  677. if (scene_data.use_ambient_light) {
  678. ambient_light = scene_data.ambient_light_color_energy.rgb;
  679. if (scene_data.use_ambient_cubemap) {
  680. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  681. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  682. vec3 cubemap_ambient = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ambient_dir, MAX_ROUGHNESS_LOD)).rgb;
  683. #else
  684. vec3 cubemap_ambient = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ambient_dir, MAX_ROUGHNESS_LOD).rgb;
  685. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  686. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  687. }
  688. }
  689. #endif // !USE_LIGHTMAP
  690. #if defined(CUSTOM_IRRADIANCE_USED)
  691. ambient_light = mix(specular_light, custom_irradiance.rgb, custom_irradiance.a);
  692. #endif // CUSTOM_IRRADIANCE_USED
  693. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  694. //radiance
  695. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  696. #ifdef USE_LIGHTMAP
  697. //lightmap
  698. if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
  699. uint index = draw_call.gi_offset;
  700. vec3 wnormal = mat3(scene_data.camera_matrix) * normal;
  701. const float c1 = 0.429043;
  702. const float c2 = 0.511664;
  703. const float c3 = 0.743125;
  704. const float c4 = 0.886227;
  705. const float c5 = 0.247708;
  706. ambient_light += (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
  707. c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
  708. c4 * lightmap_captures.data[index].sh[0].rgb -
  709. c5 * lightmap_captures.data[index].sh[6].rgb +
  710. 2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
  711. 2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
  712. 2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
  713. 2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
  714. 2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
  715. 2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z);
  716. } else if (bool(draw_call.flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
  717. bool uses_sh = bool(draw_call.flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
  718. uint ofs = draw_call.gi_offset & 0xFFFF;
  719. vec3 uvw;
  720. uvw.xy = uv2 * draw_call.lightmap_uv_scale.zw + draw_call.lightmap_uv_scale.xy;
  721. uvw.z = float((draw_call.gi_offset >> 16) & 0xFFFF);
  722. if (uses_sh) {
  723. uvw.z *= 4.0; //SH textures use 4 times more data
  724. vec3 lm_light_l0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;
  725. vec3 lm_light_l1n1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb;
  726. vec3 lm_light_l1_0 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 2.0), 0.0).rgb;
  727. vec3 lm_light_l1p1 = textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw + vec3(0.0, 0.0, 3.0), 0.0).rgb;
  728. uint idx = draw_call.gi_offset >> 20;
  729. vec3 n = normalize(lightmaps.data[idx].normal_xform * normal);
  730. ambient_light += lm_light_l0 * 0.282095f;
  731. ambient_light += lm_light_l1n1 * 0.32573 * n.y;
  732. ambient_light += lm_light_l1_0 * 0.32573 * n.z;
  733. ambient_light += lm_light_l1p1 * 0.32573 * n.x;
  734. if (metallic > 0.01) { // since the more direct bounced light is lost, we can kind of fake it with this trick
  735. vec3 r = reflect(normalize(-vertex), normal);
  736. specular_light += lm_light_l1n1 * 0.32573 * r.y;
  737. specular_light += lm_light_l1_0 * 0.32573 * r.z;
  738. specular_light += lm_light_l1p1 * 0.32573 * r.x;
  739. }
  740. } else {
  741. ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb;
  742. }
  743. }
  744. // No GI nor non low end mode...
  745. #endif // USE_LIGHTMAP
  746. // skipping ssao, do we remove ssao totally?
  747. if (!sc_disable_reflection_probes) { //Reflection probes
  748. vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
  749. vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
  750. uint reflection_indices = draw_call.reflection_probes.x;
  751. for (uint i = 0; i < 8; i++) {
  752. uint reflection_index = reflection_indices & 0xFF;
  753. if (i == 4) {
  754. reflection_indices = draw_call.reflection_probes.y;
  755. } else {
  756. reflection_indices = reflection_indices >> 8;
  757. }
  758. if (reflection_index == 0xFF) {
  759. break;
  760. }
  761. reflection_process(reflection_index, vertex, normal, roughness, ambient_light, specular_light, ambient_accum, reflection_accum);
  762. }
  763. if (reflection_accum.a > 0.0) {
  764. specular_light = reflection_accum.rgb / reflection_accum.a;
  765. }
  766. } //Reflection probes
  767. // finalize ambient light here
  768. ambient_light *= albedo.rgb;
  769. ambient_light *= ao;
  770. // convert ao to direct light ao
  771. ao = mix(1.0, ao, ao_light_affect);
  772. //this saves some VGPRs
  773. vec3 f0 = F0(metallic, specular, albedo);
  774. {
  775. #if defined(DIFFUSE_TOON)
  776. //simplify for toon, as
  777. specular_light *= specular * metallic * albedo * 2.0;
  778. #else
  779. // scales the specular reflections, needs to be computed before lighting happens,
  780. // but after environment, GI, and reflection probes are added
  781. // Environment brdf approximation (Lazarov 2013)
  782. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  783. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  784. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  785. vec4 r = roughness * c0 + c1;
  786. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  787. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  788. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  789. specular_light *= env.x * f0 + env.y;
  790. #endif
  791. }
  792. #endif // !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  793. #if !defined(MODE_RENDER_DEPTH)
  794. //this saves some VGPRs
  795. uint orms = packUnorm4x8(vec4(ao, roughness, metallic, specular));
  796. #endif
  797. // LIGHTING
  798. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  799. if (!sc_disable_directional_lights) { //directional light
  800. #ifndef SHADOWS_DISABLED
  801. // Do shadow and lighting in two passes to reduce register pressure
  802. uint shadow0 = 0;
  803. uint shadow1 = 0;
  804. for (uint i = 0; i < 8; i++) {
  805. if (i >= scene_data.directional_light_count) {
  806. break;
  807. }
  808. if (!bool(directional_lights.data[i].mask & draw_call.layer_mask)) {
  809. continue; //not masked
  810. }
  811. float shadow = 1.0;
  812. // Directional light shadow code is basically the same as forward clustered at this point in time minus `LIGHT_TRANSMITTANCE_USED` support.
  813. // Not sure if there is a reason to change this seeing directional lights are part of our global data
  814. // Should think about whether we may want to move this code into an include file or function??
  815. #ifdef USE_SOFT_SHADOWS
  816. //version with soft shadows, more expensive
  817. if (directional_lights.data[i].shadow_enabled) {
  818. float depth_z = -vertex.z;
  819. vec4 pssm_coord;
  820. vec3 shadow_color = vec3(0.0);
  821. vec3 light_dir = directional_lights.data[i].direction;
  822. #define BIAS_FUNC(m_var, m_idx) \
  823. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  824. vec3 normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp)))) * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  825. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  826. m_var.xyz += normal_bias;
  827. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  828. vec4 v = vec4(vertex, 1.0);
  829. BIAS_FUNC(v, 0)
  830. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  831. pssm_coord /= pssm_coord.w;
  832. if (directional_lights.data[i].softshadow_angle > 0) {
  833. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  834. float range_begin = directional_lights.data[i].shadow_range_begin.x;
  835. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  836. vec2 tex_scale = directional_lights.data[i].uv_scale1 * test_radius;
  837. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  838. } else {
  839. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  840. }
  841. shadow_color = directional_lights.data[i].shadow_color1.rgb;
  842. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  843. vec4 v = vec4(vertex, 1.0);
  844. BIAS_FUNC(v, 1)
  845. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  846. pssm_coord /= pssm_coord.w;
  847. if (directional_lights.data[i].softshadow_angle > 0) {
  848. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  849. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  850. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  851. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  852. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  853. } else {
  854. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  855. }
  856. shadow_color = directional_lights.data[i].shadow_color2.rgb;
  857. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  858. vec4 v = vec4(vertex, 1.0);
  859. BIAS_FUNC(v, 2)
  860. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  861. pssm_coord /= pssm_coord.w;
  862. if (directional_lights.data[i].softshadow_angle > 0) {
  863. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  864. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  865. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  866. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  867. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  868. } else {
  869. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  870. }
  871. shadow_color = directional_lights.data[i].shadow_color3.rgb;
  872. } else {
  873. vec4 v = vec4(vertex, 1.0);
  874. BIAS_FUNC(v, 3)
  875. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  876. pssm_coord /= pssm_coord.w;
  877. if (directional_lights.data[i].softshadow_angle > 0) {
  878. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  879. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  880. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  881. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  882. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  883. } else {
  884. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  885. }
  886. shadow_color = directional_lights.data[i].shadow_color4.rgb;
  887. }
  888. if (directional_lights.data[i].blend_splits) {
  889. vec3 shadow_color_blend = vec3(0.0);
  890. float pssm_blend;
  891. float shadow2;
  892. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  893. vec4 v = vec4(vertex, 1.0);
  894. BIAS_FUNC(v, 1)
  895. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  896. pssm_coord /= pssm_coord.w;
  897. if (directional_lights.data[i].softshadow_angle > 0) {
  898. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  899. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  900. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  901. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  902. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  903. } else {
  904. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  905. }
  906. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  907. shadow_color_blend = directional_lights.data[i].shadow_color2.rgb;
  908. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  909. vec4 v = vec4(vertex, 1.0);
  910. BIAS_FUNC(v, 2)
  911. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  912. pssm_coord /= pssm_coord.w;
  913. if (directional_lights.data[i].softshadow_angle > 0) {
  914. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  915. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  916. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  917. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  918. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  919. } else {
  920. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  921. }
  922. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  923. shadow_color_blend = directional_lights.data[i].shadow_color3.rgb;
  924. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  925. vec4 v = vec4(vertex, 1.0);
  926. BIAS_FUNC(v, 3)
  927. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  928. pssm_coord /= pssm_coord.w;
  929. if (directional_lights.data[i].softshadow_angle > 0) {
  930. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  931. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  932. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  933. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  934. shadow2 = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  935. } else {
  936. shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  937. }
  938. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  939. shadow_color_blend = directional_lights.data[i].shadow_color4.rgb;
  940. } else {
  941. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  942. }
  943. pssm_blend = sqrt(pssm_blend);
  944. shadow = mix(shadow, shadow2, pssm_blend);
  945. shadow_color = mix(shadow_color, shadow_color_blend, pssm_blend);
  946. }
  947. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  948. #undef BIAS_FUNC
  949. }
  950. #else
  951. // Soft shadow disabled version
  952. if (directional_lights.data[i].shadow_enabled) {
  953. float depth_z = -vertex.z;
  954. vec4 pssm_coord;
  955. vec3 light_dir = directional_lights.data[i].direction;
  956. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp))));
  957. #define BIAS_FUNC(m_var, m_idx) \
  958. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  959. vec3 normal_bias = base_normal_bias * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  960. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  961. m_var.xyz += normal_bias;
  962. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  963. vec4 v = vec4(vertex, 1.0);
  964. BIAS_FUNC(v, 0)
  965. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  966. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  967. vec4 v = vec4(vertex, 1.0);
  968. BIAS_FUNC(v, 1)
  969. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  970. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  971. vec4 v = vec4(vertex, 1.0);
  972. BIAS_FUNC(v, 2)
  973. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  974. } else {
  975. vec4 v = vec4(vertex, 1.0);
  976. BIAS_FUNC(v, 3)
  977. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  978. }
  979. pssm_coord /= pssm_coord.w;
  980. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  981. if (directional_lights.data[i].blend_splits) {
  982. float pssm_blend;
  983. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  984. vec4 v = vec4(vertex, 1.0);
  985. BIAS_FUNC(v, 1)
  986. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  987. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  988. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  989. vec4 v = vec4(vertex, 1.0);
  990. BIAS_FUNC(v, 2)
  991. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  992. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  993. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  994. vec4 v = vec4(vertex, 1.0);
  995. BIAS_FUNC(v, 3)
  996. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  997. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  998. } else {
  999. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  1000. }
  1001. pssm_coord /= pssm_coord.w;
  1002. float shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  1003. shadow = mix(shadow, shadow2, pssm_blend);
  1004. }
  1005. shadow = mix(shadow, 1.0, smoothstep(directional_lights.data[i].fade_from, directional_lights.data[i].fade_to, vertex.z)); //done with negative values for performance
  1006. #undef BIAS_FUNC
  1007. }
  1008. #endif
  1009. if (i < 4) {
  1010. shadow0 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << (i * 8);
  1011. } else {
  1012. shadow1 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << ((i - 4) * 8);
  1013. }
  1014. }
  1015. #endif // SHADOWS_DISABLED
  1016. for (uint i = 0; i < 8; i++) {
  1017. if (i >= scene_data.directional_light_count) {
  1018. break;
  1019. }
  1020. if (!bool(directional_lights.data[i].mask & draw_call.layer_mask)) {
  1021. continue; //not masked
  1022. }
  1023. // We're not doing light transmittence
  1024. float shadow = 1.0;
  1025. #ifndef SHADOWS_DISABLED
  1026. if (i < 4) {
  1027. shadow = float(shadow0 >> (i * 8) & 0xFF) / 255.0;
  1028. } else {
  1029. shadow = float(shadow1 >> ((i - 4) * 8) & 0xFF) / 255.0;
  1030. }
  1031. #endif
  1032. blur_shadow(shadow);
  1033. light_compute(normal, directional_lights.data[i].direction, normalize(view), 0.0, directional_lights.data[i].color * directional_lights.data[i].energy, shadow, f0, orms, 1.0,
  1034. #ifdef LIGHT_BACKLIGHT_USED
  1035. backlight,
  1036. #endif
  1037. /* not supported here
  1038. #ifdef LIGHT_TRANSMITTANCE_USED
  1039. transmittance_color,
  1040. transmittance_depth,
  1041. transmittance_boost,
  1042. transmittance_z,
  1043. #endif
  1044. */
  1045. #ifdef LIGHT_RIM_USED
  1046. rim, rim_tint, albedo,
  1047. #endif
  1048. #ifdef LIGHT_CLEARCOAT_USED
  1049. clearcoat, clearcoat_gloss,
  1050. #endif
  1051. #ifdef LIGHT_ANISOTROPY_USED
  1052. binormal, tangent, anisotropy,
  1053. #endif
  1054. #ifdef USE_SOFT_SHADOW
  1055. directional_lights.data[i].size,
  1056. #endif
  1057. #ifdef USE_SHADOW_TO_OPACITY
  1058. alpha,
  1059. #endif
  1060. diffuse_light,
  1061. specular_light);
  1062. }
  1063. } //directional light
  1064. if (!sc_disable_omni_lights) { //omni lights
  1065. uint light_indices = draw_call.omni_lights.x;
  1066. for (uint i = 0; i < 8; i++) {
  1067. uint light_index = light_indices & 0xFF;
  1068. if (i == 4) {
  1069. light_indices = draw_call.omni_lights.y;
  1070. } else {
  1071. light_indices = light_indices >> 8;
  1072. }
  1073. if (light_index == 0xFF) {
  1074. break;
  1075. }
  1076. float shadow = light_process_omni_shadow(light_index, vertex, normal);
  1077. shadow = blur_shadow(shadow);
  1078. light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  1079. #ifdef LIGHT_BACKLIGHT_USED
  1080. backlight,
  1081. #endif
  1082. /*
  1083. #ifdef LIGHT_TRANSMITTANCE_USED
  1084. transmittance_color,
  1085. transmittance_depth,
  1086. transmittance_boost,
  1087. #endif
  1088. */
  1089. #ifdef LIGHT_RIM_USED
  1090. rim,
  1091. rim_tint,
  1092. albedo,
  1093. #endif
  1094. #ifdef LIGHT_CLEARCOAT_USED
  1095. clearcoat, clearcoat_gloss,
  1096. #endif
  1097. #ifdef LIGHT_ANISOTROPY_USED
  1098. tangent, binormal, anisotropy,
  1099. #endif
  1100. #ifdef USE_SHADOW_TO_OPACITY
  1101. alpha,
  1102. #endif
  1103. diffuse_light, specular_light);
  1104. }
  1105. } //omni lights
  1106. if (!sc_disable_spot_lights) { //spot lights
  1107. uint light_indices = draw_call.spot_lights.x;
  1108. for (uint i = 0; i < 8; i++) {
  1109. uint light_index = light_indices & 0xFF;
  1110. if (i == 4) {
  1111. light_indices = draw_call.spot_lights.y;
  1112. } else {
  1113. light_indices = light_indices >> 8;
  1114. }
  1115. if (light_index == 0xFF) {
  1116. break;
  1117. }
  1118. float shadow = light_process_spot_shadow(light_index, vertex, normal);
  1119. shadow = blur_shadow(shadow);
  1120. light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  1121. #ifdef LIGHT_BACKLIGHT_USED
  1122. backlight,
  1123. #endif
  1124. /*
  1125. #ifdef LIGHT_TRANSMITTANCE_USED
  1126. transmittance_color,
  1127. transmittance_depth,
  1128. transmittance_boost,
  1129. #endif
  1130. */
  1131. #ifdef LIGHT_RIM_USED
  1132. rim,
  1133. rim_tint,
  1134. albedo,
  1135. #endif
  1136. #ifdef LIGHT_CLEARCOAT_USED
  1137. clearcoat, clearcoat_gloss,
  1138. #endif
  1139. #ifdef LIGHT_ANISOTROPY_USED
  1140. tangent, binormal, anisotropy,
  1141. #endif
  1142. #ifdef USE_SHADOW_TO_OPACITY
  1143. alpha,
  1144. #endif
  1145. diffuse_light, specular_light);
  1146. }
  1147. } //spot lights
  1148. #ifdef USE_SHADOW_TO_OPACITY
  1149. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1150. #if defined(ALPHA_SCISSOR_USED)
  1151. if (alpha < alpha_scissor) {
  1152. discard;
  1153. }
  1154. #endif // ALPHA_SCISSOR_USED
  1155. #ifdef USE_OPAQUE_PREPASS
  1156. if (alpha < opaque_prepass_threshold) {
  1157. discard;
  1158. }
  1159. #endif // USE_OPAQUE_PREPASS
  1160. #endif // USE_SHADOW_TO_OPACITY
  1161. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1162. #ifdef MODE_RENDER_DEPTH
  1163. #ifdef MODE_RENDER_MATERIAL
  1164. albedo_output_buffer.rgb = albedo;
  1165. albedo_output_buffer.a = alpha;
  1166. normal_output_buffer.rgb = normal * 0.5 + 0.5;
  1167. normal_output_buffer.a = 0.0;
  1168. depth_output_buffer.r = -vertex.z;
  1169. orm_output_buffer.r = ao;
  1170. orm_output_buffer.g = roughness;
  1171. orm_output_buffer.b = metallic;
  1172. orm_output_buffer.a = sss_strength;
  1173. emission_output_buffer.rgb = emission;
  1174. emission_output_buffer.a = 0.0;
  1175. #endif // MODE_RENDER_MATERIAL
  1176. #else // MODE_RENDER_DEPTH
  1177. // multiply by albedo
  1178. diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
  1179. // apply direct light AO
  1180. ao = unpackUnorm4x8(orms).x;
  1181. specular_light *= ao;
  1182. diffuse_light *= ao;
  1183. // apply metallic
  1184. metallic = unpackUnorm4x8(orms).z;
  1185. diffuse_light *= 1.0 - metallic;
  1186. ambient_light *= 1.0 - metallic;
  1187. //restore fog
  1188. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1189. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  1190. #ifdef MODE_UNSHADED
  1191. diffuse_buffer = vec4(albedo.rgb, 0.0);
  1192. specular_buffer = vec4(0.0);
  1193. #else // MODE_UNSHADED
  1194. #ifdef SSS_MODE_SKIN
  1195. sss_strength = -sss_strength;
  1196. #endif // SSS_MODE_SKIN
  1197. diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
  1198. specular_buffer = vec4(specular_light, metallic);
  1199. #endif // MODE_UNSHADED
  1200. diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
  1201. specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
  1202. #else //MODE_MULTIPLE_RENDER_TARGETS
  1203. #ifdef MODE_UNSHADED
  1204. frag_color = vec4(albedo, alpha);
  1205. #else // MODE_UNSHADED
  1206. frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha);
  1207. #endif // MODE_UNSHADED
  1208. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  1209. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1210. // On mobile we use a UNORM buffer with 10bpp which results in a range from 0.0 - 1.0 resulting in HDR breaking
  1211. // We divide by sc_luminance_multiplier to support a range from 0.0 - 2.0 both increasing precision on bright and darker images
  1212. frag_color.rgb = frag_color.rgb / sc_luminance_multiplier;
  1213. #endif //MODE_MULTIPLE_RENDER_TARGETS
  1214. #endif //MODE_RENDER_DEPTH
  1215. }