sky.glsl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, binding = 1, std430) uniform Params {
  10. mat3 orientation;
  11. vec4 projections[MAX_VIEWS];
  12. vec4 position_multiplier;
  13. float time;
  14. float luminance_multiplier;
  15. float pad[2];
  16. }
  17. params;
  18. void main() {
  19. vec2 base_arr[4] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0), vec2(1.0, -1.0));
  20. uv_interp = base_arr[gl_VertexIndex];
  21. gl_Position = vec4(uv_interp, 1.0, 1.0);
  22. }
  23. #[fragment]
  24. #version 450
  25. #VERSION_DEFINES
  26. #ifdef USE_MULTIVIEW
  27. #ifdef has_VK_KHR_multiview
  28. #extension GL_EXT_multiview : enable
  29. #define ViewIndex gl_ViewIndex
  30. #else // has_VK_KHR_multiview
  31. // !BAS! This needs to become an input once we implement our fallback!
  32. #define ViewIndex 0
  33. #endif // has_VK_KHR_multiview
  34. #else // USE_MULTIVIEW
  35. // Set to zero, not supported in non stereo
  36. #define ViewIndex 0
  37. #endif //USE_MULTIVIEW
  38. #define M_PI 3.14159265359
  39. #define MAX_VIEWS 2
  40. layout(location = 0) in vec2 uv_interp;
  41. layout(push_constant, binding = 1, std430) uniform Params {
  42. mat3 orientation;
  43. vec4 projections[MAX_VIEWS];
  44. vec4 position_multiplier;
  45. float time;
  46. float luminance_multiplier;
  47. float pad[2];
  48. }
  49. params;
  50. #define SAMPLER_NEAREST_CLAMP 0
  51. #define SAMPLER_LINEAR_CLAMP 1
  52. #define SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP 2
  53. #define SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP 3
  54. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP 4
  55. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP 5
  56. #define SAMPLER_NEAREST_REPEAT 6
  57. #define SAMPLER_LINEAR_REPEAT 7
  58. #define SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT 8
  59. #define SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT 9
  60. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT 10
  61. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT 11
  62. layout(set = 0, binding = 0) uniform sampler material_samplers[12];
  63. layout(set = 0, binding = 1, std430) restrict readonly buffer GlobalVariableData {
  64. vec4 data[];
  65. }
  66. global_variables;
  67. layout(set = 0, binding = 2, std140) uniform SceneData {
  68. bool volumetric_fog_enabled;
  69. float volumetric_fog_inv_length;
  70. float volumetric_fog_detail_spread;
  71. float fog_aerial_perspective;
  72. vec3 fog_light_color;
  73. float fog_sun_scatter;
  74. bool fog_enabled;
  75. float fog_density;
  76. float z_far;
  77. uint directional_light_count;
  78. }
  79. scene_data;
  80. struct DirectionalLightData {
  81. vec4 direction_energy;
  82. vec4 color_size;
  83. bool enabled;
  84. };
  85. layout(set = 0, binding = 3, std140) uniform DirectionalLights {
  86. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  87. }
  88. directional_lights;
  89. #ifdef MATERIAL_UNIFORMS_USED
  90. layout(set = 1, binding = 0, std140) uniform MaterialUniforms{
  91. #MATERIAL_UNIFORMS
  92. } material;
  93. #endif
  94. layout(set = 2, binding = 0) uniform textureCube radiance;
  95. #ifdef USE_CUBEMAP_PASS
  96. layout(set = 2, binding = 1) uniform textureCube half_res;
  97. layout(set = 2, binding = 2) uniform textureCube quarter_res;
  98. #else
  99. layout(set = 2, binding = 1) uniform texture2D half_res;
  100. layout(set = 2, binding = 2) uniform texture2D quarter_res;
  101. #endif
  102. layout(set = 3, binding = 0) uniform texture3D volumetric_fog_texture;
  103. #ifdef USE_CUBEMAP_PASS
  104. #define AT_CUBEMAP_PASS true
  105. #else
  106. #define AT_CUBEMAP_PASS false
  107. #endif
  108. #ifdef USE_HALF_RES_PASS
  109. #define AT_HALF_RES_PASS true
  110. #else
  111. #define AT_HALF_RES_PASS false
  112. #endif
  113. #ifdef USE_QUARTER_RES_PASS
  114. #define AT_QUARTER_RES_PASS true
  115. #else
  116. #define AT_QUARTER_RES_PASS false
  117. #endif
  118. #GLOBALS
  119. layout(location = 0) out vec4 frag_color;
  120. vec4 volumetric_fog_process(vec2 screen_uv) {
  121. vec3 fog_pos = vec3(screen_uv, 1.0);
  122. return texture(sampler3D(volumetric_fog_texture, material_samplers[SAMPLER_LINEAR_CLAMP]), fog_pos);
  123. }
  124. vec4 fog_process(vec3 view, vec3 sky_color) {
  125. vec3 fog_color = mix(scene_data.fog_light_color, sky_color, scene_data.fog_aerial_perspective);
  126. if (scene_data.fog_sun_scatter > 0.001) {
  127. vec4 sun_scatter = vec4(0.0);
  128. float sun_total = 0.0;
  129. for (uint i = 0; i < scene_data.directional_light_count; i++) {
  130. vec3 light_color = directional_lights.data[i].color_size.xyz * directional_lights.data[i].direction_energy.w;
  131. float light_amount = pow(max(dot(view, directional_lights.data[i].direction_energy.xyz), 0.0), 8.0);
  132. fog_color += light_color * light_amount * scene_data.fog_sun_scatter;
  133. }
  134. }
  135. float fog_amount = clamp(1.0 - exp(-scene_data.z_far * scene_data.fog_density), 0.0, 1.0);
  136. return vec4(fog_color, fog_amount);
  137. }
  138. void main() {
  139. vec3 cube_normal;
  140. cube_normal.z = -1.0;
  141. cube_normal.x = (cube_normal.z * (-uv_interp.x - params.projections[ViewIndex].x)) / params.projections[ViewIndex].y;
  142. cube_normal.y = -(cube_normal.z * (-uv_interp.y - params.projections[ViewIndex].z)) / params.projections[ViewIndex].w;
  143. cube_normal = mat3(params.orientation) * cube_normal;
  144. cube_normal.z = -cube_normal.z;
  145. cube_normal = normalize(cube_normal);
  146. vec2 uv = uv_interp * 0.5 + 0.5;
  147. vec2 panorama_coords = vec2(atan(cube_normal.x, cube_normal.z), acos(cube_normal.y));
  148. if (panorama_coords.x < 0.0) {
  149. panorama_coords.x += M_PI * 2.0;
  150. }
  151. panorama_coords /= vec2(M_PI * 2.0, M_PI);
  152. vec3 color = vec3(0.0, 0.0, 0.0);
  153. float alpha = 1.0; // Only available to subpasses
  154. vec4 half_res_color = vec4(1.0);
  155. vec4 quarter_res_color = vec4(1.0);
  156. vec4 custom_fog = vec4(0.0);
  157. #ifdef USE_CUBEMAP_PASS
  158. vec3 inverted_cube_normal = cube_normal;
  159. inverted_cube_normal.z *= -1.0;
  160. #ifdef USES_HALF_RES_COLOR
  161. half_res_color = texture(samplerCube(half_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * params.luminance_multiplier;
  162. #endif
  163. #ifdef USES_QUARTER_RES_COLOR
  164. quarter_res_color = texture(samplerCube(quarter_res, material_samplers[SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP]), inverted_cube_normal) * params.luminance_multiplier;
  165. #endif
  166. #else
  167. #ifdef USES_HALF_RES_COLOR
  168. half_res_color = textureLod(sampler2D(half_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0) * params.luminance_multiplier;
  169. #endif
  170. #ifdef USES_QUARTER_RES_COLOR
  171. quarter_res_color = textureLod(sampler2D(quarter_res, material_samplers[SAMPLER_LINEAR_CLAMP]), uv, 0.0) * params.luminance_multiplier;
  172. #endif
  173. #endif
  174. {
  175. #CODE : SKY
  176. }
  177. frag_color.rgb = color * params.position_multiplier.w;
  178. frag_color.a = alpha;
  179. #if !defined(DISABLE_FOG) && !defined(USE_CUBEMAP_PASS)
  180. // Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky.
  181. if (scene_data.fog_enabled) {
  182. vec4 fog = fog_process(cube_normal, frag_color.rgb);
  183. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  184. }
  185. if (scene_data.volumetric_fog_enabled) {
  186. vec4 fog = volumetric_fog_process(uv);
  187. frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a);
  188. }
  189. if (custom_fog.a > 0.0) {
  190. frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a);
  191. }
  192. #endif // DISABLE_FOG
  193. // Blending is disabled for Sky, so alpha doesn't blend
  194. // alpha is used for subsurface scattering so make sure it doesn't get applied to Sky
  195. if (!AT_CUBEMAP_PASS && !AT_HALF_RES_PASS && !AT_QUARTER_RES_PASS) {
  196. frag_color.a = 0.0;
  197. }
  198. // For mobile renderer we're dividing by 2.0 as we're using a UNORM buffer
  199. frag_color.rgb = frag_color.rgb / params.luminance_multiplier;
  200. }