sky.glsl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #define MAX_VIEWS 2
  5. #if defined(USE_MULTIVIEW) && defined(has_VK_KHR_multiview)
  6. #extension GL_EXT_multiview : enable
  7. #endif
  8. layout(location = 0) out vec2 uv_interp;
  9. layout(push_constant, std430) uniform Params {
  10. mat3 orientation;
  11. vec4 projection; // only applicable if not multiview
  12. vec3 position;
  13. float time;
  14. vec2 pad;
  15. float luminance_multiplier;
  16. float brightness_multiplier;
  17. }
  18. params;
  19. void main() {
  20. vec2 base_arr[3] = vec2[](vec2(-1.0, -3.0), vec2(-1.0, 1.0), vec2(3.0, 1.0));
  21. uv_interp = base_arr[gl_VertexIndex];
  22. gl_Position = vec4(uv_interp, 0.0, 1.0);
  23. }
  24. #[fragment]
  25. #version 450
  26. #VERSION_DEFINES
  27. #ifdef USE_MULTIVIEW
  28. #ifdef has_VK_KHR_multiview
  29. #extension GL_EXT_multiview : enable
  30. #define ViewIndex gl_ViewIndex
  31. #else // has_VK_KHR_multiview
  32. // !BAS! This needs to become an input once we implement our fallback!
  33. #define ViewIndex 0
  34. #endif // has_VK_KHR_multiview
  35. #else // USE_MULTIVIEW
  36. // Set to zero, not supported in non stereo
  37. #define ViewIndex 0
  38. #endif //USE_MULTIVIEW
  39. #define M_PI 3.14159265359
  40. #define MAX_VIEWS 2
  41. layout(location = 0) in vec2 uv_interp;
  42. layout(push_constant, std430) uniform Params {
  43. mat3 orientation;
  44. vec4 projection; // only applicable if not multiview
  45. vec3 position;
  46. float time;
  47. vec2 pad;
  48. float luminance_multiplier;
  49. float brightness_multiplier;
  50. }
  51. params;
  52. #include "../samplers_inc.glsl"
  53. layout(set = 0, binding = 1, std430) restrict readonly buffer GlobalShaderUniformData {
  54. vec4 data[];
  55. }
  56. global_shader_uniforms;
  57. layout(set = 0, binding = 2, std140) uniform SkySceneData {
  58. mat4 combined_reprojection[2];
  59. mat4 view_inv_projections[2];
  60. vec4 view_eye_offsets[2];
  61. bool volumetric_fog_enabled; // 4 - 4
  62. float volumetric_fog_inv_length; // 4 - 8
  63. float volumetric_fog_detail_spread; // 4 - 12
  64. float volumetric_fog_sky_affect; // 4 - 16
  65. bool fog_enabled; // 4 - 20
  66. float fog_sky_affect; // 4 - 24
  67. float fog_density; // 4 - 28
  68. float fog_sun_scatter; // 4 - 32
  69. vec3 fog_light_color; // 12 - 44
  70. float fog_aerial_perspective; // 4 - 48
  71. float z_far; // 4 - 52
  72. uint directional_light_count; // 4 - 56
  73. uint pad1; // 4 - 60
  74. uint pad2; // 4 - 64
  75. }
  76. sky_scene_data;
  77. struct DirectionalLightData {
  78. vec4 direction_energy;
  79. vec4 color_size;
  80. bool enabled;
  81. };
  82. layout(set = 0, binding = 3, std140) uniform DirectionalLights {
  83. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  84. }
  85. directional_lights;
  86. #ifdef MATERIAL_UNIFORMS_USED
  87. /* clang-format off */
  88. layout(set = 1, binding = 0, std140) uniform MaterialUniforms {
  89. #MATERIAL_UNIFORMS
  90. } material;
  91. /* clang-format on */
  92. #endif
  93. layout(set = 2, binding = 0) uniform textureCube radiance;
  94. #ifdef USE_CUBEMAP_PASS
  95. layout(set = 2, binding = 1) uniform textureCube half_res;
  96. layout(set = 2, binding = 2) uniform textureCube quarter_res;
  97. #elif defined(USE_MULTIVIEW)
  98. layout(set = 2, binding = 1) uniform texture2DArray half_res;
  99. layout(set = 2, binding = 2) uniform texture2DArray quarter_res;
  100. #else
  101. layout(set = 2, binding = 1) uniform texture2D half_res;
  102. layout(set = 2, binding = 2) uniform texture2D quarter_res;
  103. #endif
  104. layout(set = 3, binding = 0) uniform texture3D volumetric_fog_texture;
  105. #ifdef USE_CUBEMAP_PASS
  106. #define AT_CUBEMAP_PASS true
  107. #else
  108. #define AT_CUBEMAP_PASS false
  109. #endif
  110. #ifdef USE_HALF_RES_PASS
  111. #define AT_HALF_RES_PASS true
  112. #else
  113. #define AT_HALF_RES_PASS false
  114. #endif
  115. #ifdef USE_QUARTER_RES_PASS
  116. #define AT_QUARTER_RES_PASS true
  117. #else
  118. #define AT_QUARTER_RES_PASS false
  119. #endif
  120. #GLOBALS
  121. layout(location = 0) out vec4 frag_color;
  122. #ifdef USE_DEBANDING
  123. // https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare
  124. vec3 interleaved_gradient_noise(vec2 pos) {
  125. const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);
  126. float res = fract(magic.z * fract(dot(pos, magic.xy))) * 2.0 - 1.0;
  127. return vec3(res, -res, res) / 255.0;
  128. }
  129. #endif
  130. vec4 volumetric_fog_process(vec2 screen_uv) {
  131. #ifdef USE_MULTIVIEW
  132. vec4 reprojected = sky_scene_data.combined_reprojection[ViewIndex] * vec4(screen_uv * 2.0 - 1.0, 0.0, 1.0); // Unproject at the far plane
  133. vec3 fog_pos = vec3(reprojected.xy / reprojected.w, 1.0) * 0.5 + 0.5;
  134. #else
  135. vec3 fog_pos = vec3(screen_uv, 1.0);
  136. #endif
  137. return texture(sampler3D(volumetric_fog_texture, SAMPLER_LINEAR_CLAMP), fog_pos);
  138. }
  139. vec4 fog_process(vec3 view, vec3 sky_color) {
  140. vec3 fog_color = mix(sky_scene_data.fog_light_color, sky_color, sky_scene_data.fog_aerial_perspective);
  141. if (sky_scene_data.fog_sun_scatter > 0.001) {
  142. vec4 sun_scatter = vec4(0.0);
  143. float sun_total = 0.0;
  144. for (uint i = 0; i < sky_scene_data.directional_light_count; i++) {
  145. vec3 light_color = directional_lights.data[i].color_size.xyz * directional_lights.data[i].direction_energy.w;
  146. float light_amount = pow(max(dot(view, directional_lights.data[i].direction_energy.xyz), 0.0), 8.0);
  147. fog_color += light_color * light_amount * sky_scene_data.fog_sun_scatter;
  148. }
  149. }
  150. return vec4(fog_color, 1.0);
  151. }
  152. void main() {
  153. vec3 cube_normal;
  154. #ifdef USE_MULTIVIEW
  155. // In multiview our projection matrices will contain positional and rotational offsets that we need to properly unproject.
  156. vec4 unproject = vec4(uv_interp.x, uv_interp.y, 0.0, 1.0); // unproject at the far plane
  157. vec4 unprojected = sky_scene_data.view_inv_projections[ViewIndex] * unproject;
  158. cube_normal = unprojected.xyz / unprojected.w;
  159. // Unproject will give us the position between the eyes, need to re-offset
  160. cube_normal += sky_scene_data.view_eye_offsets[ViewIndex].xyz;
  161. #else
  162. cube_normal.z = -1.0;
  163. cube_normal.x = (cube_normal.z * (-uv_interp.x - params.projection.x)) / params.projection.y;
  164. cube_normal.y = -(cube_normal.z * (uv_interp.y - params.projection.z)) / params.projection.w;
  165. #endif
  166. cube_normal = mat3(params.orientation) * cube_normal;
  167. cube_normal = normalize(cube_normal);
  168. vec2 uv = uv_interp * 0.5 + 0.5;
  169. vec2 panorama_coords = vec2(atan(cube_normal.x, -cube_normal.z), acos(cube_normal.y));
  170. if (panorama_coords.x < 0.0) {
  171. panorama_coords.x += M_PI * 2.0;
  172. }
  173. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  174. vec3 color = vec3(0.0, 0.0, 0.0);
  175. float alpha = 1.0; // Only available to subpasses
  176. vec4 half_res_color = vec4(1.0);
  177. vec4 quarter_res_color = vec4(1.0);
  178. vec4 custom_fog = vec4(0.0);
  179. #ifdef USE_CUBEMAP_PASS
  180. #ifdef USES_HALF_RES_COLOR
  181. half_res_color = texture(samplerCube(half_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_normal) / params.luminance_multiplier;
  182. #endif
  183. #ifdef USES_QUARTER_RES_COLOR
  184. quarter_res_color = texture(samplerCube(quarter_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_normal) / params.luminance_multiplier;
  185. #endif
  186. #else
  187. #ifdef USES_HALF_RES_COLOR
  188. #ifdef USE_MULTIVIEW
  189. half_res_color = textureLod(sampler2DArray(half_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0) / params.luminance_multiplier;
  190. #else
  191. half_res_color = textureLod(sampler2D(half_res, SAMPLER_LINEAR_CLAMP), uv, 0.0) / params.luminance_multiplier;
  192. #endif // USE_MULTIVIEW
  193. #endif // USES_HALF_RES_COLOR
  194. #ifdef USES_QUARTER_RES_COLOR
  195. #ifdef USE_MULTIVIEW
  196. quarter_res_color = textureLod(sampler2DArray(quarter_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0) / params.luminance_multiplier;
  197. #else
  198. quarter_res_color = textureLod(sampler2D(quarter_res, SAMPLER_LINEAR_CLAMP), uv, 0.0) / params.luminance_multiplier;
  199. #endif // USE_MULTIVIEW
  200. #endif // USES_QUARTER_RES_COLOR
  201. #endif //USE_CUBEMAP_PASS
  202. {
  203. #CODE : SKY
  204. }
  205. frag_color.rgb = color;
  206. frag_color.a = alpha;
  207. // Apply environment 'brightness' setting separately before fog to ensure consistent luminance.
  208. frag_color.rgb = frag_color.rgb * params.brightness_multiplier;
  209. #if !defined(DISABLE_FOG) && !defined(USE_CUBEMAP_PASS)
  210. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  211. if (sky_scene_data.fog_enabled) {
  212. vec4 fog = fog_process(cube_normal, frag_color.rgb);
  213. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a * sky_scene_data.fog_sky_affect);
  214. }
  215. if (sky_scene_data.volumetric_fog_enabled) {
  216. vec4 fog = volumetric_fog_process(uv);
  217. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a * sky_scene_data.volumetric_fog_sky_affect);
  218. }
  219. if (custom_fog.a > 0.0) {
  220. frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a);
  221. }
  222. #endif // DISABLE_FOG
  223. // For mobile renderer we're multiplying by 0.5 as we're using a UNORM buffer.
  224. // For both mobile and clustered, we also bake in the exposure value for the environment and camera.
  225. frag_color.rgb = frag_color.rgb * params.luminance_multiplier;
  226. // Blending is disabled for Sky, so alpha doesn't blend.
  227. // Alpha is used for subsurface scattering so make sure it doesn't get applied to Sky.
  228. if (!AT_CUBEMAP_PASS && !AT_HALF_RES_PASS && !AT_QUARTER_RES_PASS) {
  229. frag_color.a = 0.0;
  230. }
  231. #ifdef USE_DEBANDING
  232. frag_color.rgb += interleaved_gradient_noise(gl_FragCoord.xy) * params.luminance_multiplier;
  233. #endif
  234. }