2
0

scene_forward_clustered.glsl 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #include "scene_forward_clustered_inc.glsl"
  5. /* INPUT ATTRIBS */
  6. layout(location = 0) in vec3 vertex_attrib;
  7. //only for pure render depth when normal is not used
  8. #ifdef NORMAL_USED
  9. layout(location = 1) in vec3 normal_attrib;
  10. #endif
  11. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  12. layout(location = 2) in vec4 tangent_attrib;
  13. #endif
  14. #if defined(COLOR_USED)
  15. layout(location = 3) in vec4 color_attrib;
  16. #endif
  17. #ifdef UV_USED
  18. layout(location = 4) in vec2 uv_attrib;
  19. #endif
  20. #if defined(UV2_USED) || defined(USE_LIGHTMAP) || defined(MODE_RENDER_MATERIAL)
  21. layout(location = 5) in vec2 uv2_attrib;
  22. #endif
  23. #if defined(CUSTOM0_USED)
  24. layout(location = 6) in vec4 custom0_attrib;
  25. #endif
  26. #if defined(CUSTOM1_USED)
  27. layout(location = 7) in vec4 custom1_attrib;
  28. #endif
  29. #if defined(CUSTOM2_USED)
  30. layout(location = 8) in vec4 custom2_attrib;
  31. #endif
  32. #if defined(CUSTOM3_USED)
  33. layout(location = 9) in vec4 custom3_attrib;
  34. #endif
  35. #if defined(BONES_USED) || defined(USE_PARTICLE_TRAILS)
  36. layout(location = 10) in uvec4 bone_attrib;
  37. #endif
  38. #if defined(WEIGHTS_USED) || defined(USE_PARTICLE_TRAILS)
  39. layout(location = 11) in vec4 weight_attrib;
  40. #endif
  41. /* Varyings */
  42. layout(location = 0) out vec3 vertex_interp;
  43. #ifdef NORMAL_USED
  44. layout(location = 1) out vec3 normal_interp;
  45. #endif
  46. #if defined(COLOR_USED)
  47. layout(location = 2) out vec4 color_interp;
  48. #endif
  49. #ifdef UV_USED
  50. layout(location = 3) out vec2 uv_interp;
  51. #endif
  52. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  53. layout(location = 4) out vec2 uv2_interp;
  54. #endif
  55. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  56. layout(location = 5) out vec3 tangent_interp;
  57. layout(location = 6) out vec3 binormal_interp;
  58. #endif
  59. #ifdef MATERIAL_UNIFORMS_USED
  60. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  61. #MATERIAL_UNIFORMS
  62. } material;
  63. #endif
  64. #ifdef MODE_DUAL_PARABOLOID
  65. layout(location = 8) out float dp_clip;
  66. #endif
  67. layout(location = 9) out flat uint instance_index_interp;
  68. invariant gl_Position;
  69. #GLOBALS
  70. void main() {
  71. vec4 instance_custom = vec4(0.0);
  72. #if defined(COLOR_USED)
  73. color_interp = color_attrib;
  74. #endif
  75. uint instance_index = draw_call.instance_index;
  76. bool is_multimesh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH);
  77. if (!is_multimesh) {
  78. instance_index += gl_InstanceIndex;
  79. }
  80. instance_index_interp = instance_index;
  81. mat4 world_matrix = instances.data[instance_index].transform;
  82. mat3 world_normal_matrix;
  83. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_NON_UNIFORM_SCALE)) {
  84. world_normal_matrix = transpose(inverse(mat3(world_matrix)));
  85. } else {
  86. world_normal_matrix = mat3(world_matrix);
  87. }
  88. if (is_multimesh) {
  89. //multimesh, instances are for it
  90. mat4 matrix;
  91. #ifdef USE_PARTICLE_TRAILS
  92. uint trail_size = (instances.data[instance_index].flags >> INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT) & INSTANCE_FLAGS_PARTICLE_TRAIL_MASK;
  93. uint stride = 3 + 1 + 1; //particles always uses this format
  94. uint offset = trail_size * stride * gl_InstanceIndex;
  95. #ifdef COLOR_USED
  96. vec4 pcolor;
  97. #endif
  98. {
  99. uint boffset = offset + bone_attrib.x * stride;
  100. 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;
  101. #ifdef COLOR_USED
  102. pcolor = transforms.data[boffset + 3] * weight_attrib.x;
  103. #endif
  104. }
  105. if (weight_attrib.y > 0.001) {
  106. uint boffset = offset + bone_attrib.y * stride;
  107. 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;
  108. #ifdef COLOR_USED
  109. pcolor += transforms.data[boffset + 3] * weight_attrib.y;
  110. #endif
  111. }
  112. if (weight_attrib.z > 0.001) {
  113. uint boffset = offset + bone_attrib.z * stride;
  114. 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;
  115. #ifdef COLOR_USED
  116. pcolor += transforms.data[boffset + 3] * weight_attrib.z;
  117. #endif
  118. }
  119. if (weight_attrib.w > 0.001) {
  120. uint boffset = offset + bone_attrib.w * stride;
  121. 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;
  122. #ifdef COLOR_USED
  123. pcolor += transforms.data[boffset + 3] * weight_attrib.w;
  124. #endif
  125. }
  126. instance_custom = transforms.data[offset + 4];
  127. #ifdef COLOR_USED
  128. color_interp *= pcolor;
  129. #endif
  130. #else
  131. uint stride = 0;
  132. {
  133. //TODO implement a small lookup table for the stride
  134. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  135. stride += 2;
  136. } else {
  137. stride += 3;
  138. }
  139. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  140. stride += 1;
  141. }
  142. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  143. stride += 1;
  144. }
  145. }
  146. uint offset = stride * gl_InstanceIndex;
  147. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_FORMAT_2D)) {
  148. 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));
  149. offset += 2;
  150. } else {
  151. matrix = mat4(transforms.data[offset + 0], transforms.data[offset + 1], transforms.data[offset + 2], vec4(0.0, 0.0, 0.0, 1.0));
  152. offset += 3;
  153. }
  154. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_COLOR)) {
  155. #ifdef COLOR_USED
  156. color_interp *= transforms.data[offset];
  157. #endif
  158. offset += 1;
  159. }
  160. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA)) {
  161. instance_custom = transforms.data[offset];
  162. }
  163. #endif
  164. //transpose
  165. matrix = transpose(matrix);
  166. world_matrix = world_matrix * matrix;
  167. world_normal_matrix = world_normal_matrix * mat3(matrix);
  168. }
  169. vec3 vertex = vertex_attrib;
  170. #ifdef NORMAL_USED
  171. vec3 normal = normal_attrib * 2.0 - 1.0;
  172. #endif
  173. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  174. vec3 tangent = tangent_attrib.xyz * 2.0 - 1.0;
  175. float binormalf = tangent_attrib.a * 2.0 - 1.0;
  176. vec3 binormal = normalize(cross(normal, tangent) * binormalf);
  177. #endif
  178. #ifdef UV_USED
  179. uv_interp = uv_attrib;
  180. #endif
  181. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  182. uv2_interp = uv2_attrib;
  183. #endif
  184. #ifdef OVERRIDE_POSITION
  185. vec4 position;
  186. #endif
  187. mat4 projection_matrix = scene_data.projection_matrix;
  188. //using world coordinates
  189. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  190. vertex = (world_matrix * vec4(vertex, 1.0)).xyz;
  191. normal = world_normal_matrix * normal;
  192. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  193. tangent = world_normal_matrix * tangent;
  194. binormal = world_normal_matrix * binormal;
  195. #endif
  196. #endif
  197. float roughness = 1.0;
  198. mat4 modelview = scene_data.inv_camera_matrix * world_matrix;
  199. mat3 modelview_normal = mat3(scene_data.inv_camera_matrix) * world_normal_matrix;
  200. {
  201. #CODE : VERTEX
  202. }
  203. // using local coordinates (default)
  204. #if !defined(SKIP_TRANSFORM_USED) && !defined(VERTEX_WORLD_COORDS_USED)
  205. vertex = (modelview * vec4(vertex, 1.0)).xyz;
  206. #ifdef NORMAL_USED
  207. normal = modelview_normal * normal;
  208. #endif
  209. #endif
  210. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  211. binormal = modelview_normal * binormal;
  212. tangent = modelview_normal * tangent;
  213. #endif
  214. //using world coordinates
  215. #if !defined(SKIP_TRANSFORM_USED) && defined(VERTEX_WORLD_COORDS_USED)
  216. vertex = (scene_data.inv_camera_matrix * vec4(vertex, 1.0)).xyz;
  217. normal = mat3(scene_data.inverse_normal_matrix) * normal;
  218. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  219. binormal = mat3(scene_data.camera_inverse_binormal_matrix) * binormal;
  220. tangent = mat3(scene_data.camera_inverse_tangent_matrix) * tangent;
  221. #endif
  222. #endif
  223. vertex_interp = vertex;
  224. #ifdef NORMAL_USED
  225. normal_interp = normal;
  226. #endif
  227. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  228. tangent_interp = tangent;
  229. binormal_interp = binormal;
  230. #endif
  231. #ifdef MODE_RENDER_DEPTH
  232. #ifdef MODE_DUAL_PARABOLOID
  233. vertex_interp.z *= scene_data.dual_paraboloid_side;
  234. dp_clip = vertex_interp.z; //this attempts to avoid noise caused by objects sent to the other parabolloid side due to bias
  235. //for dual paraboloid shadow mapping, this is the fastest but least correct way, as it curves straight edges
  236. vec3 vtx = vertex_interp;
  237. float distance = length(vtx);
  238. vtx = normalize(vtx);
  239. vtx.xy /= 1.0 - vtx.z;
  240. vtx.z = (distance / scene_data.z_far);
  241. vtx.z = vtx.z * 2.0 - 1.0;
  242. vertex_interp = vtx;
  243. #endif
  244. #endif //MODE_RENDER_DEPTH
  245. #ifdef OVERRIDE_POSITION
  246. gl_Position = position;
  247. #else
  248. gl_Position = projection_matrix * vec4(vertex_interp, 1.0);
  249. #endif
  250. #ifdef MODE_RENDER_DEPTH
  251. if (scene_data.pancake_shadows) {
  252. if (gl_Position.z <= 0.00001) {
  253. gl_Position.z = 0.00001;
  254. }
  255. }
  256. #endif
  257. #ifdef MODE_RENDER_MATERIAL
  258. if (scene_data.material_uv2_mode) {
  259. vec2 uv_offset = unpackHalf2x16(draw_call.uv_offset);
  260. gl_Position.xy = (uv2_attrib.xy + uv_offset) * 2.0 - 1.0;
  261. gl_Position.z = 0.00001;
  262. gl_Position.w = 1.0;
  263. }
  264. #endif
  265. }
  266. #[fragment]
  267. #version 450
  268. #VERSION_DEFINES
  269. /* Specialization Constants (Toggles) */
  270. layout(constant_id = 0) const bool sc_use_forward_gi = false;
  271. layout(constant_id = 1) const bool sc_use_light_projector = false;
  272. layout(constant_id = 2) const bool sc_use_light_soft_shadows = false;
  273. layout(constant_id = 3) const bool sc_use_directional_soft_shadows = false;
  274. /* Specialization Constants (Values) */
  275. layout(constant_id = 6) const uint sc_soft_shadow_samples = 4;
  276. layout(constant_id = 7) const uint sc_penumbra_shadow_samples = 4;
  277. layout(constant_id = 8) const uint sc_directional_soft_shadow_samples = 4;
  278. layout(constant_id = 9) const uint sc_directional_penumbra_shadow_samples = 4;
  279. layout(constant_id = 10) const bool sc_decal_use_mipmaps = true;
  280. layout(constant_id = 11) const bool sc_projector_use_mipmaps = true;
  281. // not used in clustered renderer but we share some code with the mobile renderer that requires this.
  282. const float sc_luminance_multiplier = 1.0;
  283. #include "scene_forward_clustered_inc.glsl"
  284. /* Varyings */
  285. layout(location = 0) in vec3 vertex_interp;
  286. #ifdef NORMAL_USED
  287. layout(location = 1) in vec3 normal_interp;
  288. #endif
  289. #if defined(COLOR_USED)
  290. layout(location = 2) in vec4 color_interp;
  291. #endif
  292. #ifdef UV_USED
  293. layout(location = 3) in vec2 uv_interp;
  294. #endif
  295. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  296. layout(location = 4) in vec2 uv2_interp;
  297. #endif
  298. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  299. layout(location = 5) in vec3 tangent_interp;
  300. layout(location = 6) in vec3 binormal_interp;
  301. #endif
  302. #ifdef MODE_DUAL_PARABOLOID
  303. layout(location = 8) in float dp_clip;
  304. #endif
  305. layout(location = 9) in flat uint instance_index_interp;
  306. //defines to keep compatibility with vertex
  307. #define world_matrix instances.data[instance_index].transform
  308. #define projection_matrix scene_data.projection_matrix
  309. #if defined(ENABLE_SSS) && defined(ENABLE_TRANSMITTANCE)
  310. //both required for transmittance to be enabled
  311. #define LIGHT_TRANSMITTANCE_USED
  312. #endif
  313. #ifdef MATERIAL_UNIFORMS_USED
  314. layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms{
  315. #MATERIAL_UNIFORMS
  316. } material;
  317. #endif
  318. #GLOBALS
  319. #ifdef MODE_RENDER_DEPTH
  320. #ifdef MODE_RENDER_MATERIAL
  321. layout(location = 0) out vec4 albedo_output_buffer;
  322. layout(location = 1) out vec4 normal_output_buffer;
  323. layout(location = 2) out vec4 orm_output_buffer;
  324. layout(location = 3) out vec4 emission_output_buffer;
  325. layout(location = 4) out float depth_output_buffer;
  326. #endif // MODE_RENDER_MATERIAL
  327. #ifdef MODE_RENDER_NORMAL_ROUGHNESS
  328. layout(location = 0) out vec4 normal_roughness_output_buffer;
  329. #ifdef MODE_RENDER_VOXEL_GI
  330. layout(location = 1) out uvec2 voxel_gi_buffer;
  331. #endif
  332. #endif //MODE_RENDER_NORMAL
  333. #else // RENDER DEPTH
  334. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  335. layout(location = 0) out vec4 diffuse_buffer; //diffuse (rgb) and roughness
  336. layout(location = 1) out vec4 specular_buffer; //specular and SSS (subsurface scatter)
  337. #else
  338. layout(location = 0) out vec4 frag_color;
  339. #endif // MODE_MULTIPLE_RENDER_TARGETS
  340. #endif // RENDER DEPTH
  341. #include "scene_forward_aa_inc.glsl"
  342. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  343. /* Make a default specular mode SPECULAR_SCHLICK_GGX. */
  344. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_BLINN) && !defined(SPECULAR_PHONG) && !defined(SPECULAR_TOON)
  345. #define SPECULAR_SCHLICK_GGX
  346. #endif
  347. #include "scene_forward_lights_inc.glsl"
  348. #include "scene_forward_gi_inc.glsl"
  349. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  350. #ifndef MODE_RENDER_DEPTH
  351. vec4 volumetric_fog_process(vec2 screen_uv, float z) {
  352. vec3 fog_pos = vec3(screen_uv, z * scene_data.volumetric_fog_inv_length);
  353. if (fog_pos.z < 0.0) {
  354. return vec4(0.0);
  355. } else if (fog_pos.z < 1.0) {
  356. fog_pos.z = pow(fog_pos.z, scene_data.volumetric_fog_detail_spread);
  357. }
  358. return texture(sampler3D(volumetric_fog_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), fog_pos);
  359. }
  360. vec4 fog_process(vec3 vertex) {
  361. vec3 fog_color = scene_data.fog_light_color;
  362. if (scene_data.fog_aerial_perspective > 0.0) {
  363. vec3 sky_fog_color = vec3(0.0);
  364. vec3 cube_view = scene_data.radiance_inverse_xform * vertex;
  365. // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred
  366. 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));
  367. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  368. float lod, blend;
  369. blend = modf(mip_level * MAX_ROUGHNESS_LOD, lod);
  370. sky_fog_color = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(cube_view, lod)).rgb;
  371. 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);
  372. #else
  373. sky_fog_color = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), cube_view, mip_level * MAX_ROUGHNESS_LOD).rgb;
  374. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  375. fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective);
  376. }
  377. if (scene_data.fog_sun_scatter > 0.001) {
  378. vec4 sun_scatter = vec4(0.0);
  379. float sun_total = 0.0;
  380. vec3 view = normalize(vertex);
  381. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  382. vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
  383. float light_amount = pow(max(dot(view, directional_lights.data[i].direction), 0.0), 8.0);
  384. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  385. }
  386. }
  387. float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
  388. if (abs(scene_data.fog_height_density) >= 0.0001) {
  389. float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y;
  390. float y_dist = y - scene_data.fog_height;
  391. float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density));
  392. fog_amount = max(vfog_amount, fog_amount);
  393. }
  394. return vec4(fog_color, fog_amount);
  395. }
  396. void cluster_get_item_range(uint p_offset, out uint item_min, out uint item_max, out uint item_from, out uint item_to) {
  397. uint item_min_max = cluster_buffer.data[p_offset];
  398. item_min = item_min_max & 0xFFFF;
  399. item_max = item_min_max >> 16;
  400. ;
  401. item_from = item_min >> 5;
  402. item_to = (item_max == 0) ? 0 : ((item_max - 1) >> 5) + 1; //side effect of how it is stored, as item_max 0 means no elements
  403. }
  404. uint cluster_get_range_clip_mask(uint i, uint z_min, uint z_max) {
  405. int local_min = clamp(int(z_min) - int(i) * 32, 0, 31);
  406. int mask_width = min(int(z_max) - int(z_min), 32 - local_min);
  407. return bitfieldInsert(uint(0), uint(0xFFFFFFFF), local_min, mask_width);
  408. }
  409. #endif //!MODE_RENDER DEPTH
  410. void main() {
  411. #ifdef MODE_DUAL_PARABOLOID
  412. if (dp_clip > 0.0)
  413. discard;
  414. #endif
  415. uint instance_index = instance_index_interp;
  416. //lay out everything, whathever is unused is optimized away anyway
  417. vec3 vertex = vertex_interp;
  418. vec3 view = -normalize(vertex_interp);
  419. vec3 albedo = vec3(1.0);
  420. vec3 backlight = vec3(0.0);
  421. vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0);
  422. float transmittance_depth = 0.0;
  423. float transmittance_boost = 0.0;
  424. float metallic = 0.0;
  425. float specular = 0.5;
  426. vec3 emission = vec3(0.0);
  427. float roughness = 1.0;
  428. float rim = 0.0;
  429. float rim_tint = 0.0;
  430. float clearcoat = 0.0;
  431. float clearcoat_gloss = 0.0;
  432. float anisotropy = 0.0;
  433. vec2 anisotropy_flow = vec2(1.0, 0.0);
  434. vec4 fog = vec4(0.0);
  435. #if defined(CUSTOM_RADIANCE_USED)
  436. vec4 custom_radiance = vec4(0.0);
  437. #endif
  438. #if defined(CUSTOM_IRRADIANCE_USED)
  439. vec4 custom_irradiance = vec4(0.0);
  440. #endif
  441. float ao = 1.0;
  442. float ao_light_affect = 0.0;
  443. float alpha = float(instances.data[instance_index].flags >> INSTANCE_FLAGS_FADE_SHIFT) / float(255.0);
  444. #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED)
  445. vec3 binormal = normalize(binormal_interp);
  446. vec3 tangent = normalize(tangent_interp);
  447. #else
  448. vec3 binormal = vec3(0.0);
  449. vec3 tangent = vec3(0.0);
  450. #endif
  451. #ifdef NORMAL_USED
  452. vec3 normal = normalize(normal_interp);
  453. #if defined(DO_SIDE_CHECK)
  454. if (!gl_FrontFacing) {
  455. normal = -normal;
  456. }
  457. #endif
  458. #endif //NORMAL_USED
  459. #ifdef UV_USED
  460. vec2 uv = uv_interp;
  461. #endif
  462. #if defined(UV2_USED) || defined(USE_LIGHTMAP)
  463. vec2 uv2 = uv2_interp;
  464. #endif
  465. #if defined(COLOR_USED)
  466. vec4 color = color_interp;
  467. #endif
  468. #if defined(NORMAL_MAP_USED)
  469. vec3 normal_map = vec3(0.5);
  470. #endif
  471. float normal_map_depth = 1.0;
  472. vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size + scene_data.screen_pixel_size * 0.5; //account for center
  473. float sss_strength = 0.0;
  474. #ifdef ALPHA_SCISSOR_USED
  475. float alpha_scissor_threshold = 1.0;
  476. #endif // ALPHA_SCISSOR_USED
  477. #ifdef ALPHA_HASH_USED
  478. float alpha_hash_scale = 1.0;
  479. #endif // ALPHA_HASH_USED
  480. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  481. float alpha_antialiasing_edge = 0.0;
  482. vec2 alpha_texture_coordinate = vec2(0.0, 0.0);
  483. #endif // ALPHA_ANTIALIASING_EDGE_USED
  484. {
  485. #CODE : FRAGMENT
  486. }
  487. #ifdef LIGHT_TRANSMITTANCE_USED
  488. transmittance_color.a *= sss_strength;
  489. #endif
  490. #ifndef USE_SHADOW_TO_OPACITY
  491. #ifdef ALPHA_SCISSOR_USED
  492. if (alpha < alpha_scissor_threshold) {
  493. discard;
  494. }
  495. #endif // ALPHA_SCISSOR_USED
  496. // alpha hash can be used in unison with alpha antialiasing
  497. #ifdef ALPHA_HASH_USED
  498. if (alpha < compute_alpha_hash_threshold(vertex, alpha_hash_scale)) {
  499. discard;
  500. }
  501. #endif // ALPHA_HASH_USED
  502. // If we are not edge antialiasing, we need to remove the output alpha channel from scissor and hash
  503. #if (defined(ALPHA_SCISSOR_USED) || defined(ALPHA_HASH_USED)) && !defined(ALPHA_ANTIALIASING_EDGE_USED)
  504. alpha = 1.0;
  505. #endif
  506. #ifdef ALPHA_ANTIALIASING_EDGE_USED
  507. // If alpha scissor is used, we must further the edge threshold, otherwise we won't get any edge feather
  508. #ifdef ALPHA_SCISSOR_USED
  509. alpha_antialiasing_edge = clamp(alpha_scissor_threshold + alpha_antialiasing_edge, 0.0, 1.0);
  510. #endif
  511. alpha = compute_alpha_antialiasing_edge(alpha, alpha_texture_coordinate, alpha_antialiasing_edge);
  512. #endif // ALPHA_ANTIALIASING_EDGE_USED
  513. #ifdef USE_OPAQUE_PREPASS
  514. if (alpha < opaque_prepass_threshold) {
  515. discard;
  516. }
  517. #endif // USE_OPAQUE_PREPASS
  518. #endif // !USE_SHADOW_TO_OPACITY
  519. #ifdef NORMAL_MAP_USED
  520. normal_map.xy = normal_map.xy * 2.0 - 1.0;
  521. 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.
  522. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth));
  523. #endif
  524. #ifdef LIGHT_ANISOTROPY_USED
  525. if (anisotropy > 0.01) {
  526. //rotation matrix
  527. mat3 rot = mat3(tangent, binormal, normal);
  528. //make local to space
  529. tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0));
  530. binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0));
  531. }
  532. #endif
  533. #ifdef ENABLE_CLIP_ALPHA
  534. if (albedo.a < 0.99) {
  535. //used for doublepass and shadowmapping
  536. discard;
  537. }
  538. #endif
  539. /////////////////////// FOG //////////////////////
  540. #ifndef MODE_RENDER_DEPTH
  541. #ifndef CUSTOM_FOG_USED
  542. // fog must be processed as early as possible and then packed.
  543. // to maximize VGPR usage
  544. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  545. if (scene_data.fog_enabled) {
  546. fog = fog_process(vertex);
  547. }
  548. if (scene_data.volumetric_fog_enabled) {
  549. vec4 volumetric_fog = volumetric_fog_process(screen_uv, -vertex.z);
  550. if (scene_data.fog_enabled) {
  551. //must use the full blending equation here to blend fogs
  552. vec4 res;
  553. float sa = 1.0 - volumetric_fog.a;
  554. res.a = fog.a * sa + volumetric_fog.a;
  555. if (res.a == 0.0) {
  556. res.rgb = vec3(0.0);
  557. } else {
  558. res.rgb = (fog.rgb * fog.a * sa + volumetric_fog.rgb * volumetric_fog.a) / res.a;
  559. }
  560. fog = res;
  561. } else {
  562. fog = volumetric_fog;
  563. }
  564. }
  565. #endif //!CUSTOM_FOG_USED
  566. uint fog_rg = packHalf2x16(fog.rg);
  567. uint fog_ba = packHalf2x16(fog.ba);
  568. #endif //!MODE_RENDER_DEPTH
  569. /////////////////////// DECALS ////////////////////////////////
  570. #ifndef MODE_RENDER_DEPTH
  571. uvec2 cluster_pos = uvec2(gl_FragCoord.xy) >> scene_data.cluster_shift;
  572. uint cluster_offset = (scene_data.cluster_width * cluster_pos.y + cluster_pos.x) * (scene_data.max_cluster_element_count_div_32 + 32);
  573. uint cluster_z = uint(clamp((-vertex.z / scene_data.z_far) * 32.0, 0.0, 31.0));
  574. //used for interpolating anything cluster related
  575. vec3 vertex_ddx = dFdx(vertex);
  576. vec3 vertex_ddy = dFdy(vertex);
  577. { // process decals
  578. uint cluster_decal_offset = cluster_offset + scene_data.cluster_type_size * 2;
  579. uint item_min;
  580. uint item_max;
  581. uint item_from;
  582. uint item_to;
  583. cluster_get_item_range(cluster_decal_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  584. #ifdef USE_SUBGROUPS
  585. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  586. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  587. #endif
  588. for (uint i = item_from; i < item_to; i++) {
  589. uint mask = cluster_buffer.data[cluster_decal_offset + i];
  590. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  591. #ifdef USE_SUBGROUPS
  592. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  593. #else
  594. uint merged_mask = mask;
  595. #endif
  596. while (merged_mask != 0) {
  597. uint bit = findMSB(merged_mask);
  598. merged_mask &= ~(1 << bit);
  599. #ifdef USE_SUBGROUPS
  600. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  601. continue;
  602. }
  603. #endif
  604. uint decal_index = 32 * i + bit;
  605. if (!bool(decals.data[decal_index].mask & instances.data[instance_index].layer_mask)) {
  606. continue; //not masked
  607. }
  608. vec3 uv_local = (decals.data[decal_index].xform * vec4(vertex, 1.0)).xyz;
  609. if (any(lessThan(uv_local, vec3(0.0, -1.0, 0.0))) || any(greaterThan(uv_local, vec3(1.0)))) {
  610. continue; //out of decal
  611. }
  612. 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);
  613. if (decals.data[decal_index].normal_fade > 0.0) {
  614. fade *= smoothstep(decals.data[decal_index].normal_fade, 1.0, dot(normal_interp, decals.data[decal_index].normal) * 0.5 + 0.5);
  615. }
  616. //we need ddx/ddy for mipmaps, so simulate them
  617. vec2 ddx = (decals.data[decal_index].xform * vec4(vertex_ddx, 0.0)).xz;
  618. vec2 ddy = (decals.data[decal_index].xform * vec4(vertex_ddy, 0.0)).xz;
  619. if (decals.data[decal_index].albedo_rect != vec4(0.0)) {
  620. //has albedo
  621. vec4 decal_albedo;
  622. if (sc_decal_use_mipmaps) {
  623. 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);
  624. } else {
  625. 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);
  626. }
  627. decal_albedo *= decals.data[decal_index].modulate;
  628. decal_albedo.a *= fade;
  629. albedo = mix(albedo, decal_albedo.rgb, decal_albedo.a * decals.data[decal_index].albedo_mix);
  630. if (decals.data[decal_index].normal_rect != vec4(0.0)) {
  631. vec3 decal_normal;
  632. if (sc_decal_use_mipmaps) {
  633. 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;
  634. } else {
  635. 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;
  636. }
  637. 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
  638. decal_normal.z = sqrt(max(0.0, 1.0 - dot(decal_normal.xy, decal_normal.xy)));
  639. //convert to view space, use xzy because y is up
  640. decal_normal = (decals.data[decal_index].normal_xform * decal_normal.xzy).xyz;
  641. normal = normalize(mix(normal, decal_normal, decal_albedo.a));
  642. }
  643. if (decals.data[decal_index].orm_rect != vec4(0.0)) {
  644. vec3 decal_orm;
  645. if (sc_decal_use_mipmaps) {
  646. 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;
  647. } else {
  648. 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;
  649. }
  650. ao = mix(ao, decal_orm.r, decal_albedo.a);
  651. roughness = mix(roughness, decal_orm.g, decal_albedo.a);
  652. metallic = mix(metallic, decal_orm.b, decal_albedo.a);
  653. }
  654. }
  655. if (decals.data[decal_index].emission_rect != vec4(0.0)) {
  656. //emission is additive, so its independent from albedo
  657. if (sc_decal_use_mipmaps) {
  658. 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;
  659. } else {
  660. 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;
  661. }
  662. }
  663. }
  664. }
  665. }
  666. //pack albedo until needed again, saves 2 VGPRs in the meantime
  667. #endif //not render depth
  668. /////////////////////// LIGHTING //////////////////////////////
  669. #ifdef NORMAL_USED
  670. if (scene_data.roughness_limiter_enabled) {
  671. //https://www.jp.square-enix.com/tech/library/pdf/ImprovedGeometricSpecularAA.pdf
  672. float roughness2 = roughness * roughness;
  673. vec3 dndu = dFdx(normal), dndv = dFdy(normal);
  674. float variance = scene_data.roughness_limiter_amount * (dot(dndu, dndu) + dot(dndv, dndv));
  675. float kernelRoughness2 = min(2.0 * variance, scene_data.roughness_limiter_limit); //limit effect
  676. float filteredRoughness2 = min(1.0, roughness2 + kernelRoughness2);
  677. roughness = sqrt(filteredRoughness2);
  678. }
  679. #endif
  680. //apply energy conservation
  681. vec3 specular_light = vec3(0.0, 0.0, 0.0);
  682. vec3 diffuse_light = vec3(0.0, 0.0, 0.0);
  683. vec3 ambient_light = vec3(0.0, 0.0, 0.0);
  684. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  685. if (scene_data.use_reflection_cubemap) {
  686. vec3 ref_vec = reflect(-view, normal);
  687. float horizon = min(1.0 + dot(ref_vec, normal), 1.0);
  688. ref_vec = scene_data.radiance_inverse_xform * ref_vec;
  689. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  690. float lod, blend;
  691. blend = modf(roughness * MAX_ROUGHNESS_LOD, lod);
  692. specular_light = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod)).rgb;
  693. specular_light = mix(specular_light, texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ref_vec, lod + 1)).rgb, blend);
  694. #else
  695. specular_light = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ref_vec, roughness * MAX_ROUGHNESS_LOD).rgb;
  696. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  697. specular_light *= horizon * horizon;
  698. specular_light *= scene_data.ambient_light_color_energy.a;
  699. }
  700. #if defined(CUSTOM_RADIANCE_USED)
  701. specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a);
  702. #endif
  703. #ifndef USE_LIGHTMAP
  704. //lightmap overrides everything
  705. if (scene_data.use_ambient_light) {
  706. ambient_light = scene_data.ambient_light_color_energy.rgb;
  707. if (scene_data.use_ambient_cubemap) {
  708. vec3 ambient_dir = scene_data.radiance_inverse_xform * normal;
  709. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  710. vec3 cubemap_ambient = texture(samplerCubeArray(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), vec4(ambient_dir, MAX_ROUGHNESS_LOD)).rgb;
  711. #else
  712. vec3 cubemap_ambient = textureLod(samplerCube(radiance_cubemap, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), ambient_dir, MAX_ROUGHNESS_LOD).rgb;
  713. #endif //USE_RADIANCE_CUBEMAP_ARRAY
  714. ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix);
  715. }
  716. }
  717. #endif // USE_LIGHTMAP
  718. #if defined(CUSTOM_IRRADIANCE_USED)
  719. ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a);
  720. #endif
  721. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  722. //radiance
  723. /// GI ///
  724. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  725. #ifdef USE_LIGHTMAP
  726. //lightmap
  727. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE)) { //has lightmap capture
  728. uint index = instances.data[instance_index].gi_offset;
  729. vec3 wnormal = mat3(scene_data.camera_matrix) * normal;
  730. const float c1 = 0.429043;
  731. const float c2 = 0.511664;
  732. const float c3 = 0.743125;
  733. const float c4 = 0.886227;
  734. const float c5 = 0.247708;
  735. ambient_light += (c1 * lightmap_captures.data[index].sh[8].rgb * (wnormal.x * wnormal.x - wnormal.y * wnormal.y) +
  736. c3 * lightmap_captures.data[index].sh[6].rgb * wnormal.z * wnormal.z +
  737. c4 * lightmap_captures.data[index].sh[0].rgb -
  738. c5 * lightmap_captures.data[index].sh[6].rgb +
  739. 2.0 * c1 * lightmap_captures.data[index].sh[4].rgb * wnormal.x * wnormal.y +
  740. 2.0 * c1 * lightmap_captures.data[index].sh[7].rgb * wnormal.x * wnormal.z +
  741. 2.0 * c1 * lightmap_captures.data[index].sh[5].rgb * wnormal.y * wnormal.z +
  742. 2.0 * c2 * lightmap_captures.data[index].sh[3].rgb * wnormal.x +
  743. 2.0 * c2 * lightmap_captures.data[index].sh[1].rgb * wnormal.y +
  744. 2.0 * c2 * lightmap_captures.data[index].sh[2].rgb * wnormal.z);
  745. } else if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) { // has actual lightmap
  746. bool uses_sh = bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SH_LIGHTMAP);
  747. uint ofs = instances.data[instance_index].gi_offset & 0xFFFF;
  748. vec3 uvw;
  749. uvw.xy = uv2 * instances.data[instance_index].lightmap_uv_scale.zw + instances.data[instance_index].lightmap_uv_scale.xy;
  750. uvw.z = float((instances.data[instance_index].gi_offset >> 16) & 0xFFFF);
  751. if (uses_sh) {
  752. uvw.z *= 4.0; //SH textures use 4 times more data
  753. 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;
  754. 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;
  755. 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;
  756. 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;
  757. uint idx = instances.data[instance_index].gi_offset >> 20;
  758. vec3 n = normalize(lightmaps.data[idx].normal_xform * normal);
  759. ambient_light += lm_light_l0 * 0.282095f;
  760. ambient_light += lm_light_l1n1 * 0.32573 * n.y;
  761. ambient_light += lm_light_l1_0 * 0.32573 * n.z;
  762. ambient_light += lm_light_l1p1 * 0.32573 * n.x;
  763. if (metallic > 0.01) { // since the more direct bounced light is lost, we can kind of fake it with this trick
  764. vec3 r = reflect(normalize(-vertex), normal);
  765. specular_light += lm_light_l1n1 * 0.32573 * r.y;
  766. specular_light += lm_light_l1_0 * 0.32573 * r.z;
  767. specular_light += lm_light_l1p1 * 0.32573 * r.x;
  768. }
  769. } else {
  770. ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], material_samplers[SAMPLER_LINEAR_CLAMP]), uvw, 0.0).rgb;
  771. }
  772. }
  773. #else
  774. if (sc_use_forward_gi && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_SDFGI)) { //has lightmap capture
  775. //make vertex orientation the world one, but still align to camera
  776. vec3 cam_pos = mat3(scene_data.camera_matrix) * vertex;
  777. vec3 cam_normal = mat3(scene_data.camera_matrix) * normal;
  778. vec3 cam_reflection = mat3(scene_data.camera_matrix) * reflect(-view, normal);
  779. //apply y-mult
  780. cam_pos.y *= sdfgi.y_mult;
  781. cam_normal.y *= sdfgi.y_mult;
  782. cam_normal = normalize(cam_normal);
  783. cam_reflection.y *= sdfgi.y_mult;
  784. cam_normal = normalize(cam_normal);
  785. cam_reflection = normalize(cam_reflection);
  786. vec4 light_accum = vec4(0.0);
  787. float weight_accum = 0.0;
  788. vec4 light_blend_accum = vec4(0.0);
  789. float weight_blend_accum = 0.0;
  790. float blend = -1.0;
  791. // helper constants, compute once
  792. uint cascade = 0xFFFFFFFF;
  793. vec3 cascade_pos;
  794. vec3 cascade_normal;
  795. for (uint i = 0; i < sdfgi.max_cascades; i++) {
  796. cascade_pos = (cam_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe;
  797. if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) {
  798. continue; //skip cascade
  799. }
  800. cascade = i;
  801. break;
  802. }
  803. if (cascade < SDFGI_MAX_CASCADES) {
  804. bool use_specular = true;
  805. float blend;
  806. vec3 diffuse, specular;
  807. sdfgi_process(cascade, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse, specular, blend);
  808. if (blend > 0.0) {
  809. //blend
  810. if (cascade == sdfgi.max_cascades - 1) {
  811. diffuse = mix(diffuse, ambient_light, blend);
  812. if (use_specular) {
  813. specular = mix(specular, specular_light, blend);
  814. }
  815. } else {
  816. vec3 diffuse2, specular2;
  817. float blend2;
  818. cascade_pos = (cam_pos - sdfgi.cascades[cascade + 1].position) * sdfgi.cascades[cascade + 1].to_probe;
  819. sdfgi_process(cascade + 1, cascade_pos, cam_pos, cam_normal, cam_reflection, use_specular, roughness, diffuse2, specular2, blend2);
  820. diffuse = mix(diffuse, diffuse2, blend);
  821. if (use_specular) {
  822. specular = mix(specular, specular2, blend);
  823. }
  824. }
  825. }
  826. ambient_light = diffuse;
  827. if (use_specular) {
  828. specular_light = specular;
  829. }
  830. }
  831. }
  832. if (sc_use_forward_gi && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_VOXEL_GI)) { // process voxel_gi_instances
  833. uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
  834. vec3 ref_vec = normalize(reflect(normalize(vertex), normal));
  835. //find arbitrary tangent and bitangent, then build a matrix
  836. vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0);
  837. vec3 tangent = normalize(cross(v0, normal));
  838. vec3 bitangent = normalize(cross(tangent, normal));
  839. mat3 normal_mat = mat3(tangent, bitangent, normal);
  840. vec4 amb_accum = vec4(0.0);
  841. vec4 spec_accum = vec4(0.0);
  842. voxel_gi_compute(index1, vertex, normal, ref_vec, normal_mat, roughness * roughness, ambient_light, specular_light, spec_accum, amb_accum);
  843. uint index2 = instances.data[instance_index].gi_offset >> 16;
  844. if (index2 != 0xFFFF) {
  845. voxel_gi_compute(index2, vertex, normal, ref_vec, normal_mat, roughness * roughness, ambient_light, specular_light, spec_accum, amb_accum);
  846. }
  847. if (amb_accum.a > 0.0) {
  848. amb_accum.rgb /= amb_accum.a;
  849. }
  850. if (spec_accum.a > 0.0) {
  851. spec_accum.rgb /= spec_accum.a;
  852. }
  853. specular_light = spec_accum.rgb;
  854. ambient_light = amb_accum.rgb;
  855. }
  856. if (!sc_use_forward_gi && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_GI_BUFFERS)) { //use GI buffers
  857. vec2 coord;
  858. if (scene_data.gi_upscale_for_msaa) {
  859. vec2 base_coord = screen_uv;
  860. vec2 closest_coord = base_coord;
  861. float closest_ang = dot(normal, textureLod(sampler2D(normal_roughness_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), base_coord, 0.0).xyz * 2.0 - 1.0);
  862. for (int i = 0; i < 4; i++) {
  863. const vec2 neighbours[4] = vec2[](vec2(-1, 0), vec2(1, 0), vec2(0, -1), vec2(0, 1));
  864. vec2 neighbour_coord = base_coord + neighbours[i] * scene_data.screen_pixel_size;
  865. float neighbour_ang = dot(normal, textureLod(sampler2D(normal_roughness_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), neighbour_coord, 0.0).xyz * 2.0 - 1.0);
  866. if (neighbour_ang > closest_ang) {
  867. closest_ang = neighbour_ang;
  868. closest_coord = neighbour_coord;
  869. }
  870. }
  871. coord = closest_coord;
  872. } else {
  873. coord = screen_uv;
  874. }
  875. vec4 buffer_ambient = textureLod(sampler2D(ambient_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), coord, 0.0);
  876. vec4 buffer_reflection = textureLod(sampler2D(reflection_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), coord, 0.0);
  877. ambient_light = mix(ambient_light, buffer_ambient.rgb, buffer_ambient.a);
  878. specular_light = mix(specular_light, buffer_reflection.rgb, buffer_reflection.a);
  879. }
  880. #endif // !USE_LIGHTMAP
  881. if (scene_data.ssao_enabled) {
  882. float ssao = texture(sampler2D(ao_buffer, material_samplers[SAMPLER_LINEAR_CLAMP]), screen_uv).r;
  883. ao = min(ao, ssao);
  884. ao_light_affect = mix(ao_light_affect, max(ao_light_affect, scene_data.ssao_light_affect), scene_data.ssao_ao_affect);
  885. }
  886. { // process reflections
  887. vec4 reflection_accum = vec4(0.0, 0.0, 0.0, 0.0);
  888. vec4 ambient_accum = vec4(0.0, 0.0, 0.0, 0.0);
  889. uint cluster_reflection_offset = cluster_offset + scene_data.cluster_type_size * 3;
  890. uint item_min;
  891. uint item_max;
  892. uint item_from;
  893. uint item_to;
  894. cluster_get_item_range(cluster_reflection_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  895. #ifdef USE_SUBGROUPS
  896. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  897. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  898. #endif
  899. for (uint i = item_from; i < item_to; i++) {
  900. uint mask = cluster_buffer.data[cluster_reflection_offset + i];
  901. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  902. #ifdef USE_SUBGROUPS
  903. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  904. #else
  905. uint merged_mask = mask;
  906. #endif
  907. while (merged_mask != 0) {
  908. uint bit = findMSB(merged_mask);
  909. merged_mask &= ~(1 << bit);
  910. #ifdef USE_SUBGROUPS
  911. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  912. continue;
  913. }
  914. #endif
  915. uint reflection_index = 32 * i + bit;
  916. if (!bool(reflections.data[reflection_index].mask & instances.data[instance_index].layer_mask)) {
  917. continue; //not masked
  918. }
  919. reflection_process(reflection_index, vertex, normal, roughness, ambient_light, specular_light, ambient_accum, reflection_accum);
  920. }
  921. }
  922. if (reflection_accum.a > 0.0) {
  923. specular_light = reflection_accum.rgb / reflection_accum.a;
  924. }
  925. #if !defined(USE_LIGHTMAP)
  926. if (ambient_accum.a > 0.0) {
  927. ambient_light = ambient_accum.rgb / ambient_accum.a;
  928. }
  929. #endif
  930. }
  931. //finalize ambient light here
  932. ambient_light *= albedo.rgb;
  933. ambient_light *= ao;
  934. // convert ao to direct light ao
  935. ao = mix(1.0, ao, ao_light_affect);
  936. //this saves some VGPRs
  937. vec3 f0 = F0(metallic, specular, albedo);
  938. {
  939. #if defined(DIFFUSE_TOON)
  940. //simplify for toon, as
  941. specular_light *= specular * metallic * albedo * 2.0;
  942. #else
  943. // scales the specular reflections, needs to be computed before lighting happens,
  944. // but after environment, GI, and reflection probes are added
  945. // Environment brdf approximation (Lazarov 2013)
  946. // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile
  947. const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
  948. const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);
  949. vec4 r = roughness * c0 + c1;
  950. float ndotv = clamp(dot(normal, view), 0.0, 1.0);
  951. float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y;
  952. vec2 env = vec2(-1.04, 1.04) * a004 + r.zw;
  953. specular_light *= env.x * f0 + env.y;
  954. #endif
  955. }
  956. #endif //GI !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  957. #if !defined(MODE_RENDER_DEPTH)
  958. //this saves some VGPRs
  959. uint orms = packUnorm4x8(vec4(ao, roughness, metallic, specular));
  960. #endif
  961. // LIGHTING
  962. #if !defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  963. { // Directional light.
  964. // Do shadow and lighting in two passes to reduce register pressure.
  965. #ifndef SHADOWS_DISABLED
  966. uint shadow0 = 0;
  967. uint shadow1 = 0;
  968. for (uint i = 0; i < 8; i++) {
  969. if (i >= scene_data.directional_light_count) {
  970. break;
  971. }
  972. if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
  973. continue; //not masked
  974. }
  975. if (directional_lights.data[i].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
  976. continue; // Statically baked light and object uses lightmap, skip
  977. }
  978. float shadow = 1.0;
  979. if (directional_lights.data[i].shadow_enabled) {
  980. float depth_z = -vertex.z;
  981. vec3 light_dir = directional_lights.data[i].direction;
  982. vec3 base_normal_bias = normalize(normal_interp) * (1.0 - max(0.0, dot(light_dir, -normalize(normal_interp))));
  983. #define BIAS_FUNC(m_var, m_idx) \
  984. m_var.xyz += light_dir * directional_lights.data[i].shadow_bias[m_idx]; \
  985. vec3 normal_bias = base_normal_bias * directional_lights.data[i].shadow_normal_bias[m_idx]; \
  986. normal_bias -= light_dir * dot(light_dir, normal_bias); \
  987. m_var.xyz += normal_bias;
  988. //version with soft shadows, more expensive
  989. if (sc_use_directional_soft_shadows && directional_lights.data[i].softshadow_angle > 0) {
  990. uint blend_count = 0;
  991. const uint blend_max = directional_lights.data[i].blend_splits ? 2 : 1;
  992. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  993. vec4 v = vec4(vertex, 1.0);
  994. BIAS_FUNC(v, 0)
  995. vec4 pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  996. pssm_coord /= pssm_coord.w;
  997. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  998. float range_begin = directional_lights.data[i].shadow_range_begin.x;
  999. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  1000. vec2 tex_scale = directional_lights.data[i].uv_scale1 * test_radius;
  1001. shadow = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  1002. blend_count++;
  1003. }
  1004. if (blend_count < blend_max && depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1005. vec4 v = vec4(vertex, 1.0);
  1006. BIAS_FUNC(v, 1)
  1007. vec4 pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  1008. pssm_coord /= pssm_coord.w;
  1009. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  1010. float range_begin = directional_lights.data[i].shadow_range_begin.y;
  1011. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  1012. vec2 tex_scale = directional_lights.data[i].uv_scale2 * test_radius;
  1013. float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  1014. if (blend_count == 0) {
  1015. shadow = s;
  1016. } else {
  1017. //blend
  1018. float blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  1019. shadow = mix(shadow, s, blend);
  1020. }
  1021. blend_count++;
  1022. }
  1023. if (blend_count < blend_max && depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1024. vec4 v = vec4(vertex, 1.0);
  1025. BIAS_FUNC(v, 2)
  1026. vec4 pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  1027. pssm_coord /= pssm_coord.w;
  1028. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  1029. float range_begin = directional_lights.data[i].shadow_range_begin.z;
  1030. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  1031. vec2 tex_scale = directional_lights.data[i].uv_scale3 * test_radius;
  1032. float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  1033. if (blend_count == 0) {
  1034. shadow = s;
  1035. } else {
  1036. //blend
  1037. float blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  1038. shadow = mix(shadow, s, blend);
  1039. }
  1040. blend_count++;
  1041. }
  1042. if (blend_count < blend_max) {
  1043. vec4 v = vec4(vertex, 1.0);
  1044. BIAS_FUNC(v, 3)
  1045. vec4 pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  1046. pssm_coord /= pssm_coord.w;
  1047. float range_pos = dot(directional_lights.data[i].direction, v.xyz);
  1048. float range_begin = directional_lights.data[i].shadow_range_begin.w;
  1049. float test_radius = (range_pos - range_begin) * directional_lights.data[i].softshadow_angle;
  1050. vec2 tex_scale = directional_lights.data[i].uv_scale4 * test_radius;
  1051. float s = sample_directional_soft_shadow(directional_shadow_atlas, pssm_coord.xyz, tex_scale * directional_lights.data[i].soft_shadow_scale);
  1052. if (blend_count == 0) {
  1053. shadow = s;
  1054. } else {
  1055. //blend
  1056. float blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  1057. shadow = mix(shadow, s, blend);
  1058. }
  1059. }
  1060. } else { //no soft shadows
  1061. vec4 pssm_coord;
  1062. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  1063. vec4 v = vec4(vertex, 1.0);
  1064. BIAS_FUNC(v, 0)
  1065. pssm_coord = (directional_lights.data[i].shadow_matrix1 * v);
  1066. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1067. vec4 v = vec4(vertex, 1.0);
  1068. BIAS_FUNC(v, 1)
  1069. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  1070. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1071. vec4 v = vec4(vertex, 1.0);
  1072. BIAS_FUNC(v, 2)
  1073. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  1074. } else {
  1075. vec4 v = vec4(vertex, 1.0);
  1076. BIAS_FUNC(v, 3)
  1077. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  1078. }
  1079. pssm_coord /= pssm_coord.w;
  1080. shadow = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  1081. if (directional_lights.data[i].blend_splits) {
  1082. float pssm_blend;
  1083. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  1084. vec4 v = vec4(vertex, 1.0);
  1085. BIAS_FUNC(v, 1)
  1086. pssm_coord = (directional_lights.data[i].shadow_matrix2 * v);
  1087. pssm_blend = smoothstep(0.0, directional_lights.data[i].shadow_split_offsets.x, depth_z);
  1088. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1089. vec4 v = vec4(vertex, 1.0);
  1090. BIAS_FUNC(v, 2)
  1091. pssm_coord = (directional_lights.data[i].shadow_matrix3 * v);
  1092. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.x, directional_lights.data[i].shadow_split_offsets.y, depth_z);
  1093. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1094. vec4 v = vec4(vertex, 1.0);
  1095. BIAS_FUNC(v, 3)
  1096. pssm_coord = (directional_lights.data[i].shadow_matrix4 * v);
  1097. pssm_blend = smoothstep(directional_lights.data[i].shadow_split_offsets.y, directional_lights.data[i].shadow_split_offsets.z, depth_z);
  1098. } else {
  1099. pssm_blend = 0.0; //if no blend, same coord will be used (divide by z will result in same value, and already cached)
  1100. }
  1101. pssm_coord /= pssm_coord.w;
  1102. float shadow2 = sample_directional_pcf_shadow(directional_shadow_atlas, scene_data.directional_shadow_pixel_size * directional_lights.data[i].soft_shadow_scale, pssm_coord);
  1103. shadow = mix(shadow, shadow2, pssm_blend);
  1104. }
  1105. }
  1106. 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
  1107. #undef BIAS_FUNC
  1108. } // shadows
  1109. if (i < 4) {
  1110. shadow0 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << (i * 8);
  1111. } else {
  1112. shadow1 |= uint(clamp(shadow * 255.0, 0.0, 255.0)) << ((i - 4) * 8);
  1113. }
  1114. }
  1115. #endif // SHADOWS_DISABLED
  1116. for (uint i = 0; i < 8; i++) {
  1117. if (i >= scene_data.directional_light_count) {
  1118. break;
  1119. }
  1120. if (!bool(directional_lights.data[i].mask & instances.data[instance_index].layer_mask)) {
  1121. continue; //not masked
  1122. }
  1123. #ifdef LIGHT_TRANSMITTANCE_USED
  1124. float transmittance_z = transmittance_depth;
  1125. if (directional_lights.data[i].shadow_enabled) {
  1126. float depth_z = -vertex.z;
  1127. if (depth_z < directional_lights.data[i].shadow_split_offsets.x) {
  1128. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.x, 1.0);
  1129. vec4 trans_coord = directional_lights.data[i].shadow_matrix1 * trans_vertex;
  1130. trans_coord /= trans_coord.w;
  1131. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  1132. shadow_z *= directional_lights.data[i].shadow_z_range.x;
  1133. float z = trans_coord.z * directional_lights.data[i].shadow_z_range.x;
  1134. transmittance_z = z - shadow_z;
  1135. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.y) {
  1136. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.y, 1.0);
  1137. vec4 trans_coord = directional_lights.data[i].shadow_matrix2 * trans_vertex;
  1138. trans_coord /= trans_coord.w;
  1139. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  1140. shadow_z *= directional_lights.data[i].shadow_z_range.y;
  1141. float z = trans_coord.z * directional_lights.data[i].shadow_z_range.y;
  1142. transmittance_z = z - shadow_z;
  1143. } else if (depth_z < directional_lights.data[i].shadow_split_offsets.z) {
  1144. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.z, 1.0);
  1145. vec4 trans_coord = directional_lights.data[i].shadow_matrix3 * trans_vertex;
  1146. trans_coord /= trans_coord.w;
  1147. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  1148. shadow_z *= directional_lights.data[i].shadow_z_range.z;
  1149. float z = trans_coord.z * directional_lights.data[i].shadow_z_range.z;
  1150. transmittance_z = z - shadow_z;
  1151. } else {
  1152. vec4 trans_vertex = vec4(vertex - normalize(normal_interp) * directional_lights.data[i].shadow_transmittance_bias.w, 1.0);
  1153. vec4 trans_coord = directional_lights.data[i].shadow_matrix4 * trans_vertex;
  1154. trans_coord /= trans_coord.w;
  1155. float shadow_z = textureLod(sampler2D(directional_shadow_atlas, material_samplers[SAMPLER_LINEAR_CLAMP]), trans_coord.xy, 0.0).r;
  1156. shadow_z *= directional_lights.data[i].shadow_z_range.w;
  1157. float z = trans_coord.z * directional_lights.data[i].shadow_z_range.w;
  1158. transmittance_z = z - shadow_z;
  1159. }
  1160. }
  1161. #endif
  1162. float shadow = 1.0;
  1163. #ifndef SHADOWS_DISABLED
  1164. if (i < 4) {
  1165. shadow = float(shadow0 >> (i * 8) & 0xFF) / 255.0;
  1166. } else {
  1167. shadow = float(shadow1 >> ((i - 4) * 8) & 0xFF) / 255.0;
  1168. }
  1169. #endif
  1170. blur_shadow(shadow);
  1171. float size_A = sc_use_light_soft_shadows ? directional_lights.data[i].size : 0.0;
  1172. light_compute(normal, directional_lights.data[i].direction, normalize(view), size_A, directional_lights.data[i].color * directional_lights.data[i].energy, shadow, f0, orms, 1.0,
  1173. #ifdef LIGHT_BACKLIGHT_USED
  1174. backlight,
  1175. #endif
  1176. #ifdef LIGHT_TRANSMITTANCE_USED
  1177. transmittance_color,
  1178. transmittance_depth,
  1179. transmittance_boost,
  1180. transmittance_z,
  1181. #endif
  1182. #ifdef LIGHT_RIM_USED
  1183. rim, rim_tint, albedo,
  1184. #endif
  1185. #ifdef LIGHT_CLEARCOAT_USED
  1186. clearcoat, clearcoat_gloss,
  1187. #endif
  1188. #ifdef LIGHT_ANISOTROPY_USED
  1189. binormal, tangent, anisotropy,
  1190. #endif
  1191. #ifdef USE_SHADOW_TO_OPACITY
  1192. alpha,
  1193. #endif
  1194. diffuse_light,
  1195. specular_light);
  1196. }
  1197. }
  1198. { //omni lights
  1199. uint cluster_omni_offset = cluster_offset;
  1200. uint item_min;
  1201. uint item_max;
  1202. uint item_from;
  1203. uint item_to;
  1204. cluster_get_item_range(cluster_omni_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  1205. #ifdef USE_SUBGROUPS
  1206. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  1207. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  1208. #endif
  1209. for (uint i = item_from; i < item_to; i++) {
  1210. uint mask = cluster_buffer.data[cluster_omni_offset + i];
  1211. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  1212. #ifdef USE_SUBGROUPS
  1213. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  1214. #else
  1215. uint merged_mask = mask;
  1216. #endif
  1217. while (merged_mask != 0) {
  1218. uint bit = findMSB(merged_mask);
  1219. merged_mask &= ~(1 << bit);
  1220. #ifdef USE_SUBGROUPS
  1221. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  1222. continue;
  1223. }
  1224. #endif
  1225. uint light_index = 32 * i + bit;
  1226. if (!bool(omni_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
  1227. continue; //not masked
  1228. }
  1229. if (omni_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
  1230. continue; // Statically baked light and object uses lightmap, skip
  1231. }
  1232. float shadow = light_process_omni_shadow(light_index, vertex, normal);
  1233. shadow = blur_shadow(shadow);
  1234. light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  1235. #ifdef LIGHT_BACKLIGHT_USED
  1236. backlight,
  1237. #endif
  1238. #ifdef LIGHT_TRANSMITTANCE_USED
  1239. transmittance_color,
  1240. transmittance_depth,
  1241. transmittance_boost,
  1242. #endif
  1243. #ifdef LIGHT_RIM_USED
  1244. rim,
  1245. rim_tint,
  1246. albedo,
  1247. #endif
  1248. #ifdef LIGHT_CLEARCOAT_USED
  1249. clearcoat, clearcoat_gloss,
  1250. #endif
  1251. #ifdef LIGHT_ANISOTROPY_USED
  1252. tangent, binormal, anisotropy,
  1253. #endif
  1254. #ifdef USE_SHADOW_TO_OPACITY
  1255. alpha,
  1256. #endif
  1257. diffuse_light, specular_light);
  1258. }
  1259. }
  1260. }
  1261. { //spot lights
  1262. uint cluster_spot_offset = cluster_offset + scene_data.cluster_type_size;
  1263. uint item_min;
  1264. uint item_max;
  1265. uint item_from;
  1266. uint item_to;
  1267. cluster_get_item_range(cluster_spot_offset + scene_data.max_cluster_element_count_div_32 + cluster_z, item_min, item_max, item_from, item_to);
  1268. #ifdef USE_SUBGROUPS
  1269. item_from = subgroupBroadcastFirst(subgroupMin(item_from));
  1270. item_to = subgroupBroadcastFirst(subgroupMax(item_to));
  1271. #endif
  1272. for (uint i = item_from; i < item_to; i++) {
  1273. uint mask = cluster_buffer.data[cluster_spot_offset + i];
  1274. mask &= cluster_get_range_clip_mask(i, item_min, item_max);
  1275. #ifdef USE_SUBGROUPS
  1276. uint merged_mask = subgroupBroadcastFirst(subgroupOr(mask));
  1277. #else
  1278. uint merged_mask = mask;
  1279. #endif
  1280. while (merged_mask != 0) {
  1281. uint bit = findMSB(merged_mask);
  1282. merged_mask &= ~(1 << bit);
  1283. #ifdef USE_SUBGROUPS
  1284. if (((1 << bit) & mask) == 0) { //do not process if not originally here
  1285. continue;
  1286. }
  1287. #endif
  1288. uint light_index = 32 * i + bit;
  1289. if (!bool(spot_lights.data[light_index].mask & instances.data[instance_index].layer_mask)) {
  1290. continue; //not masked
  1291. }
  1292. if (spot_lights.data[light_index].bake_mode == LIGHT_BAKE_STATIC && bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_LIGHTMAP)) {
  1293. continue; // Statically baked light and object uses lightmap, skip
  1294. }
  1295. float shadow = light_process_spot_shadow(light_index, vertex, normal);
  1296. shadow = blur_shadow(shadow);
  1297. light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, shadow,
  1298. #ifdef LIGHT_BACKLIGHT_USED
  1299. backlight,
  1300. #endif
  1301. #ifdef LIGHT_TRANSMITTANCE_USED
  1302. transmittance_color,
  1303. transmittance_depth,
  1304. transmittance_boost,
  1305. #endif
  1306. #ifdef LIGHT_RIM_USED
  1307. rim,
  1308. rim_tint,
  1309. albedo,
  1310. #endif
  1311. #ifdef LIGHT_CLEARCOAT_USED
  1312. clearcoat, clearcoat_gloss,
  1313. #endif
  1314. #ifdef LIGHT_ANISOTROPY_USED
  1315. tangent, binormal, anisotropy,
  1316. #endif
  1317. #ifdef USE_SHADOW_TO_OPACITY
  1318. alpha,
  1319. #endif
  1320. diffuse_light, specular_light);
  1321. }
  1322. }
  1323. }
  1324. #ifdef USE_SHADOW_TO_OPACITY
  1325. alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0));
  1326. #if defined(ALPHA_SCISSOR_USED)
  1327. if (alpha < alpha_scissor) {
  1328. discard;
  1329. }
  1330. #endif // ALPHA_SCISSOR_USED
  1331. #ifdef USE_OPAQUE_PREPASS
  1332. if (alpha < opaque_prepass_threshold) {
  1333. discard;
  1334. }
  1335. #endif // USE_OPAQUE_PREPASS
  1336. #endif // USE_SHADOW_TO_OPACITY
  1337. #endif //!defined(MODE_RENDER_DEPTH) && !defined(MODE_UNSHADED)
  1338. #ifdef MODE_RENDER_DEPTH
  1339. #ifdef MODE_RENDER_SDF
  1340. {
  1341. vec3 local_pos = (scene_data.sdf_to_bounds * vec4(vertex, 1.0)).xyz;
  1342. ivec3 grid_pos = scene_data.sdf_offset + ivec3(local_pos * vec3(scene_data.sdf_size));
  1343. uint albedo16 = 0x1; //solid flag
  1344. albedo16 |= clamp(uint(albedo.r * 31.0), 0, 31) << 11;
  1345. albedo16 |= clamp(uint(albedo.g * 31.0), 0, 31) << 6;
  1346. albedo16 |= clamp(uint(albedo.b * 31.0), 0, 31) << 1;
  1347. imageStore(albedo_volume_grid, grid_pos, uvec4(albedo16));
  1348. uint facing_bits = 0;
  1349. const vec3 aniso_dir[6] = vec3[](
  1350. vec3(1, 0, 0),
  1351. vec3(0, 1, 0),
  1352. vec3(0, 0, 1),
  1353. vec3(-1, 0, 0),
  1354. vec3(0, -1, 0),
  1355. vec3(0, 0, -1));
  1356. vec3 cam_normal = mat3(scene_data.camera_matrix) * normalize(normal_interp);
  1357. float closest_dist = -1e20;
  1358. for (uint i = 0; i < 6; i++) {
  1359. float d = dot(cam_normal, aniso_dir[i]);
  1360. if (d > closest_dist) {
  1361. closest_dist = d;
  1362. facing_bits = (1 << i);
  1363. }
  1364. }
  1365. imageAtomicOr(geom_facing_grid, grid_pos, facing_bits); //store facing bits
  1366. if (length(emission) > 0.001) {
  1367. float lumas[6];
  1368. vec3 light_total = vec3(0);
  1369. for (int i = 0; i < 6; i++) {
  1370. float strength = max(0.0, dot(cam_normal, aniso_dir[i]));
  1371. vec3 light = emission * strength;
  1372. light_total += light;
  1373. lumas[i] = max(light.r, max(light.g, light.b));
  1374. }
  1375. float luma_total = max(light_total.r, max(light_total.g, light_total.b));
  1376. uint light_aniso = 0;
  1377. for (int i = 0; i < 6; i++) {
  1378. light_aniso |= min(31, uint((lumas[i] / luma_total) * 31.0)) << (i * 5);
  1379. }
  1380. //compress to RGBE9995 to save space
  1381. const float pow2to9 = 512.0f;
  1382. const float B = 15.0f;
  1383. const float N = 9.0f;
  1384. const float LN2 = 0.6931471805599453094172321215;
  1385. float cRed = clamp(light_total.r, 0.0, 65408.0);
  1386. float cGreen = clamp(light_total.g, 0.0, 65408.0);
  1387. float cBlue = clamp(light_total.b, 0.0, 65408.0);
  1388. float cMax = max(cRed, max(cGreen, cBlue));
  1389. float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B;
  1390. float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f);
  1391. float exps = expp + 1.0f;
  1392. if (0.0 <= sMax && sMax < pow2to9) {
  1393. exps = expp;
  1394. }
  1395. float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f);
  1396. float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f);
  1397. float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f);
  1398. //store as 8985 to have 2 extra neighbour bits
  1399. uint light_rgbe = ((uint(sRed) & 0x1FF) >> 1) | ((uint(sGreen) & 0x1FF) << 8) | (((uint(sBlue) & 0x1FF) >> 1) << 17) | ((uint(exps) & 0x1F) << 25);
  1400. imageStore(emission_grid, grid_pos, uvec4(light_rgbe));
  1401. imageStore(emission_aniso_grid, grid_pos, uvec4(light_aniso));
  1402. }
  1403. }
  1404. #endif
  1405. #ifdef MODE_RENDER_MATERIAL
  1406. albedo_output_buffer.rgb = albedo;
  1407. albedo_output_buffer.a = alpha;
  1408. normal_output_buffer.rgb = normal * 0.5 + 0.5;
  1409. normal_output_buffer.a = 0.0;
  1410. depth_output_buffer.r = -vertex.z;
  1411. orm_output_buffer.r = ao;
  1412. orm_output_buffer.g = roughness;
  1413. orm_output_buffer.b = metallic;
  1414. orm_output_buffer.a = sss_strength;
  1415. emission_output_buffer.rgb = emission;
  1416. emission_output_buffer.a = 0.0;
  1417. #endif
  1418. #ifdef MODE_RENDER_NORMAL_ROUGHNESS
  1419. normal_roughness_output_buffer = vec4(normal * 0.5 + 0.5, roughness);
  1420. #ifdef MODE_RENDER_VOXEL_GI
  1421. if (bool(instances.data[instance_index].flags & INSTANCE_FLAGS_USE_VOXEL_GI)) { // process voxel_gi_instances
  1422. uint index1 = instances.data[instance_index].gi_offset & 0xFFFF;
  1423. uint index2 = instances.data[instance_index].gi_offset >> 16;
  1424. voxel_gi_buffer.x = index1 & 0xFF;
  1425. voxel_gi_buffer.y = index2 & 0xFF;
  1426. } else {
  1427. voxel_gi_buffer.x = 0xFF;
  1428. voxel_gi_buffer.y = 0xFF;
  1429. }
  1430. #endif
  1431. #endif //MODE_RENDER_NORMAL_ROUGHNESS
  1432. //nothing happens, so a tree-ssa optimizer will result in no fragment shader :)
  1433. #else
  1434. // multiply by albedo
  1435. diffuse_light *= albedo; // ambient must be multiplied by albedo at the end
  1436. // apply direct light AO
  1437. ao = unpackUnorm4x8(orms).x;
  1438. specular_light *= ao;
  1439. diffuse_light *= ao;
  1440. // apply metallic
  1441. metallic = unpackUnorm4x8(orms).z;
  1442. diffuse_light *= 1.0 - metallic;
  1443. ambient_light *= 1.0 - metallic;
  1444. //restore fog
  1445. fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba));
  1446. #ifdef MODE_MULTIPLE_RENDER_TARGETS
  1447. #ifdef MODE_UNSHADED
  1448. diffuse_buffer = vec4(albedo.rgb, 0.0);
  1449. specular_buffer = vec4(0.0);
  1450. #else
  1451. #ifdef SSS_MODE_SKIN
  1452. sss_strength = -sss_strength;
  1453. #endif
  1454. diffuse_buffer = vec4(emission + diffuse_light + ambient_light, sss_strength);
  1455. specular_buffer = vec4(specular_light, metallic);
  1456. #endif
  1457. diffuse_buffer.rgb = mix(diffuse_buffer.rgb, fog.rgb, fog.a);
  1458. specular_buffer.rgb = mix(specular_buffer.rgb, vec3(0.0), fog.a);
  1459. #else //MODE_MULTIPLE_RENDER_TARGETS
  1460. #ifdef MODE_UNSHADED
  1461. frag_color = vec4(albedo, alpha);
  1462. #else
  1463. frag_color = vec4(emission + ambient_light + diffuse_light + specular_light, alpha);
  1464. //frag_color = vec4(1.0);
  1465. #endif //USE_NO_SHADING
  1466. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  1467. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  1468. #endif //MODE_MULTIPLE_RENDER_TARGETS
  1469. #endif //MODE_RENDER_DEPTH
  1470. }