| 1 |
#[vertex] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(location = 0) out vec2 uv_interp; |
| 8 |
|
| 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 |
|
| 20 |
void main() { |
| 21 |
vec2 base_arr[3] = vec2[](vec2(-1.0, -3.0), vec2(-1.0, 1.0), vec2(3.0, 1.0)); |
| 22 |
uv_interp = base_arr[gl_VertexIndex]; |
| 23 |
gl_Position = vec4(uv_interp, 0.0, 1.0); |
| 24 |
} |
| 25 |
|
| 26 |
#[fragment] |
| 27 |
|
| 28 |
#version 450 |
| 29 |
|
| 30 |
#VERSION_DEFINES |
| 31 |
|
| 32 |
#include "../oct_inc.glsl" |
| 33 |
|
| 34 |
#ifdef USE_MULTIVIEW |
| 35 |
#extension GL_EXT_multiview : enable |
| 36 |
#define ViewIndex gl_ViewIndex |
| 37 |
#endif |
| 38 |
|
| 39 |
#define M_PI 3.14159265359 |
| 40 |
|
| 41 |
layout(location = 0) in vec2 uv_interp; |
| 42 |
|
| 43 |
layout(push_constant, std430) uniform Params { |
| 44 |
mat3 orientation; |
| 45 |
vec4 projection; // only applicable if not multiview |
| 46 |
vec3 position; |
| 47 |
float time; |
| 48 |
vec2 border_size; |
| 49 |
float luminance_multiplier; |
| 50 |
float brightness_multiplier; |
| 51 |
} |
| 52 |
params; |
| 53 |
|
| 54 |
#include "../samplers_inc.glsl" |
| 55 |
|
| 56 |
layout(set = 0, binding = 1, std430) restrict readonly buffer GlobalShaderUniformData { |
| 57 |
vec4 data[]; |
| 58 |
} |
| 59 |
global_shader_uniforms; |
| 60 |
|
| 61 |
layout(set = 0, binding = 2, std140) uniform SkySceneData { |
| 62 |
mat4 combined_reprojection[2]; |
| 63 |
mat4 view_inv_projections[2]; |
| 64 |
vec4 view_eye_offsets[2]; |
| 65 |
|
| 66 |
bool volumetric_fog_enabled; // 4 - 4 |
| 67 |
float volumetric_fog_inv_length; // 4 - 8 |
| 68 |
float volumetric_fog_detail_spread; // 4 - 12 |
| 69 |
float volumetric_fog_sky_affect; // 4 - 16 |
| 70 |
|
| 71 |
bool fog_enabled; // 4 - 20 |
| 72 |
float fog_sky_affect; // 4 - 24 |
| 73 |
float fog_density; // 4 - 28 |
| 74 |
float fog_sun_scatter; // 4 - 32 |
| 75 |
|
| 76 |
vec3 fog_light_color; // 12 - 44 |
| 77 |
float fog_aerial_perspective; // 4 - 48 |
| 78 |
|
| 79 |
float z_far; // 4 - 52 |
| 80 |
uint directional_light_count; // 4 - 56 |
| 81 |
bool fog_use_legacy_blending; // 4 - 60 |
| 82 |
uint pad1; // 4 - 64 |
| 83 |
} |
| 84 |
sky_scene_data; |
| 85 |
|
| 86 |
struct DirectionalLightData { |
| 87 |
vec4 direction_energy; |
| 88 |
vec4 color_size; |
| 89 |
bool enabled; |
| 90 |
}; |
| 91 |
|
| 92 |
layout(set = 0, binding = 3, std140) uniform DirectionalLights { |
| 93 |
DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; |
| 94 |
} |
| 95 |
directional_lights; |
| 96 |
|
| 97 |
#ifdef MATERIAL_UNIFORMS_USED |
| 98 |
/* clang-format off */ |
| 99 |
layout(set = 1, binding = 0, std140) uniform MaterialUniforms { |
| 100 |
#MATERIAL_UNIFORMS |
| 101 |
} material; |
| 102 |
/* clang-format on */ |
| 103 |
#endif |
| 104 |
|
| 105 |
layout(set = 2, binding = 0) uniform texture2D radiance; |
| 106 |
#ifdef USE_CUBEMAP_PASS |
| 107 |
layout(set = 2, binding = 1) uniform texture2D half_res; |
| 108 |
layout(set = 2, binding = 2) uniform texture2D quarter_res; |
| 109 |
#elif defined(USE_MULTIVIEW) |
| 110 |
layout(set = 2, binding = 1) uniform texture2DArray half_res; |
| 111 |
layout(set = 2, binding = 2) uniform texture2DArray quarter_res; |
| 112 |
#else |
| 113 |
layout(set = 2, binding = 1) uniform texture2D half_res; |
| 114 |
layout(set = 2, binding = 2) uniform texture2D quarter_res; |
| 115 |
#endif |
| 116 |
|
| 117 |
layout(set = 3, binding = 0) uniform texture3D volumetric_fog_texture; |
| 118 |
|
| 119 |
#ifdef USE_CUBEMAP_PASS |
| 120 |
#define AT_CUBEMAP_PASS true |
| 121 |
#else |
| 122 |
#define AT_CUBEMAP_PASS false |
| 123 |
#endif |
| 124 |
|
| 125 |
#ifdef USE_HALF_RES_PASS |
| 126 |
#define AT_HALF_RES_PASS true |
| 127 |
#else |
| 128 |
#define AT_HALF_RES_PASS false |
| 129 |
#endif |
| 130 |
|
| 131 |
#ifdef USE_QUARTER_RES_PASS |
| 132 |
#define AT_QUARTER_RES_PASS true |
| 133 |
#else |
| 134 |
#define AT_QUARTER_RES_PASS false |
| 135 |
#endif |
| 136 |
|
| 137 |
#GLOBALS |
| 138 |
|
| 139 |
layout(location = 0) out vec4 frag_color; |
| 140 |
|
| 141 |
#ifdef USE_DEBANDING |
| 142 |
// https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare |
| 143 |
vec3 interleaved_gradient_noise(vec2 pos) { |
| 144 |
const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f); |
| 145 |
float res = fract(magic.z * fract(dot(pos, magic.xy))) * 2.0 - 1.0; |
| 146 |
return vec3(res, -res, res) / 255.0; |
| 147 |
} |
| 148 |
#endif |
| 149 |
|
| 150 |
vec4 volumetric_fog_process(vec2 screen_uv) { |
| 151 |
#ifdef USE_MULTIVIEW |
| 152 |
vec4 reprojected = sky_scene_data.combined_reprojection[ViewIndex] * vec4(screen_uv * 2.0 - 1.0, 0.0, 1.0); // Unproject at the far plane |
| 153 |
vec3 fog_pos = vec3(reprojected.xy / reprojected.w, 1.0) * 0.5 + 0.5; |
| 154 |
#else |
| 155 |
vec3 fog_pos = vec3(screen_uv, 1.0); |
| 156 |
#endif |
| 157 |
|
| 158 |
return texture(sampler3D(volumetric_fog_texture, SAMPLER_LINEAR_CLAMP), fog_pos); |
| 159 |
} |
| 160 |
|
| 161 |
vec4 fog_process(vec3 view, vec3 sky_color) { |
| 162 |
vec3 fog_color = mix(sky_scene_data.fog_light_color, sky_color, sky_scene_data.fog_aerial_perspective); |
| 163 |
|
| 164 |
if (sky_scene_data.fog_sun_scatter > 0.001) { |
| 165 |
vec4 sun_scatter = vec4(0.0); |
| 166 |
float sun_total = 0.0; |
| 167 |
for (uint i = 0; i < sky_scene_data.directional_light_count; i++) { |
| 168 |
vec3 light_color = directional_lights.data[i].color_size.xyz * directional_lights.data[i].direction_energy.w; |
| 169 |
float light_amount = pow(max(dot(view, directional_lights.data[i].direction_energy.xyz), 0.0), 8.0) * M_PI; |
| 170 |
fog_color += light_color * light_amount * sky_scene_data.fog_sun_scatter; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return vec4(fog_color, 1.0); |
| 175 |
} |
| 176 |
|
| 177 |
// Eberly approximation from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. |
| 178 |
// input [-1, 1] and output [0, PI] |
| 179 |
float acos_approx(float p_x) { |
| 180 |
float x = abs(p_x); |
| 181 |
float res = -0.156583f * x + (M_PI / 2.0); |
| 182 |
res *= sqrt(1.0f - x); |
| 183 |
return (p_x >= 0.0) ? res : M_PI - res; |
| 184 |
} |
| 185 |
|
| 186 |
// Based on https://math.stackexchange.com/questions/1098487/atan2-faster-approximation |
| 187 |
// but using the Eberly coefficients from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. |
| 188 |
float atan2_approx(float y, float x) { |
| 189 |
float a = min(abs(x), abs(y)) / max(abs(x), abs(y)); |
| 190 |
float s = a * a; |
| 191 |
float poly = 0.0872929f; |
| 192 |
poly = -0.301895f + poly * s; |
| 193 |
poly = 1.0f + poly * s; |
| 194 |
poly = poly * a; |
| 195 |
|
| 196 |
float r = abs(y) > abs(x) ? (M_PI / 2.0) - poly : poly; |
| 197 |
r = x < 0.0 ? M_PI - r : r; |
| 198 |
r = y < 0.0 ? -r : r; |
| 199 |
|
| 200 |
return r; |
| 201 |
} |
| 202 |
|
| 203 |
void main() { |
| 204 |
vec2 uv = uv_interp * 0.5 + 0.5; |
| 205 |
vec3 cube_normal; |
| 206 |
#ifdef USE_CUBEMAP_PASS |
| 207 |
cube_normal = oct_to_vec3_with_border(uv, params.border_size.y); |
| 208 |
#else |
| 209 |
#ifdef USE_MULTIVIEW |
| 210 |
// In multiview our projection matrices will contain positional and rotational offsets that we need to properly unproject. |
| 211 |
vec4 unproject = vec4(uv_interp.x, uv_interp.y, 0.0, 1.0); // unproject at the far plane |
| 212 |
vec4 unprojected = sky_scene_data.view_inv_projections[ViewIndex] * unproject; |
| 213 |
cube_normal = unprojected.xyz / unprojected.w; |
| 214 |
|
| 215 |
// Unproject will give us the position between the eyes, need to re-offset |
| 216 |
cube_normal += sky_scene_data.view_eye_offsets[ViewIndex].xyz; |
| 217 |
#else |
| 218 |
cube_normal.z = -1.0; |
| 219 |
cube_normal.x = (cube_normal.z * (-uv_interp.x - params.projection.x)) / params.projection.y; |
| 220 |
cube_normal.y = -(cube_normal.z * (uv_interp.y - params.projection.z)) / params.projection.w; |
| 221 |
#endif |
| 222 |
cube_normal = mat3(params.orientation) * cube_normal; |
| 223 |
cube_normal = normalize(cube_normal); |
| 224 |
#endif |
| 225 |
|
| 226 |
vec2 panorama_coords = vec2(atan2_approx(cube_normal.x, -cube_normal.z), acos_approx(cube_normal.y)); |
| 227 |
|
| 228 |
if (panorama_coords.x < 0.0) { |
| 229 |
panorama_coords.x += M_PI * 2.0; |
| 230 |
} |
| 231 |
|
| 232 |
panorama_coords /= vec2(M_PI * 2.0, M_PI); |
| 233 |
|
| 234 |
vec3 color = vec3(0.0, 0.0, 0.0); |
| 235 |
float alpha = 1.0; // Only available to subpasses |
| 236 |
vec4 half_res_color = vec4(1.0); |
| 237 |
vec4 quarter_res_color = vec4(1.0); |
| 238 |
vec4 custom_fog = vec4(0.0); |
| 239 |
|
| 240 |
#ifdef USE_CUBEMAP_PASS |
| 241 |
|
| 242 |
#ifdef USES_HALF_RES_COLOR |
| 243 |
half_res_color = texture(sampler2D(half_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3_to_oct_with_border(cube_normal, params.border_size)) / params.luminance_multiplier; |
| 244 |
#endif |
| 245 |
#ifdef USES_QUARTER_RES_COLOR |
| 246 |
quarter_res_color = texture(sampler2D(quarter_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec3_to_oct_with_border(cube_normal, params.border_size)) / params.luminance_multiplier; |
| 247 |
#endif |
| 248 |
|
| 249 |
#else |
| 250 |
|
| 251 |
#ifdef USES_HALF_RES_COLOR |
| 252 |
#ifdef USE_MULTIVIEW |
| 253 |
half_res_color = textureLod(sampler2DArray(half_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0) / params.luminance_multiplier; |
| 254 |
#else |
| 255 |
half_res_color = textureLod(sampler2D(half_res, SAMPLER_LINEAR_CLAMP), uv, 0.0) / params.luminance_multiplier; |
| 256 |
#endif // USE_MULTIVIEW |
| 257 |
#endif // USES_HALF_RES_COLOR |
| 258 |
|
| 259 |
#ifdef USES_QUARTER_RES_COLOR |
| 260 |
#ifdef USE_MULTIVIEW |
| 261 |
quarter_res_color = textureLod(sampler2DArray(quarter_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0) / params.luminance_multiplier; |
| 262 |
#else |
| 263 |
quarter_res_color = textureLod(sampler2D(quarter_res, SAMPLER_LINEAR_CLAMP), uv, 0.0) / params.luminance_multiplier; |
| 264 |
#endif // USE_MULTIVIEW |
| 265 |
#endif // USES_QUARTER_RES_COLOR |
| 266 |
|
| 267 |
#endif //USE_CUBEMAP_PASS |
| 268 |
|
| 269 |
{ |
| 270 |
#CODE : SKY |
| 271 |
} |
| 272 |
|
| 273 |
frag_color.rgb = color; |
| 274 |
frag_color.a = alpha; |
| 275 |
|
| 276 |
// Apply environment 'brightness' setting separately before fog to ensure consistent luminance. |
| 277 |
frag_color.rgb = frag_color.rgb * params.brightness_multiplier; |
| 278 |
|
| 279 |
#if !defined(DISABLE_FOG) && !defined(USE_CUBEMAP_PASS) |
| 280 |
|
| 281 |
// Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky. |
| 282 |
if (sky_scene_data.fog_enabled) { |
| 283 |
vec4 fog = fog_process(cube_normal, frag_color.rgb); |
| 284 |
frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a * sky_scene_data.fog_sky_affect); |
| 285 |
} |
| 286 |
|
| 287 |
if (sky_scene_data.volumetric_fog_enabled) { |
| 288 |
vec4 fog = volumetric_fog_process(uv); |
| 289 |
if (sky_scene_data.fog_use_legacy_blending) { |
| 290 |
frag_color.rgb = mix(frag_color.rgb, fog.rgb, (1.0 - fog.a) * sky_scene_data.volumetric_fog_sky_affect); |
| 291 |
} else { |
| 292 |
fog.rgb = frag_color.rgb * fog.a + fog.rgb; |
| 293 |
frag_color.rgb = mix(frag_color.rgb, fog.rgb, sky_scene_data.volumetric_fog_sky_affect); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (custom_fog.a > 0.0) { |
| 298 |
frag_color.rgb = mix(frag_color.rgb, custom_fog.rgb, custom_fog.a); |
| 299 |
} |
| 300 |
|
| 301 |
#endif // DISABLE_FOG |
| 302 |
|
| 303 |
// For mobile renderer we're multiplying by 0.5 as we're using a UNORM buffer. |
| 304 |
// For both mobile and clustered, we also bake in the exposure value for the environment and camera. |
| 305 |
frag_color.rgb = frag_color.rgb * params.luminance_multiplier; |
| 306 |
|
| 307 |
// Blending is disabled for Sky, so alpha doesn't blend. |
| 308 |
// Alpha is used for subsurface scattering so make sure it doesn't get applied to Sky. |
| 309 |
if (!AT_CUBEMAP_PASS && !AT_HALF_RES_PASS && !AT_QUARTER_RES_PASS) { |
| 310 |
frag_color.a = 0.0; |
| 311 |
} |
| 312 |
|
| 313 |
#ifdef USE_DEBANDING |
| 314 |
frag_color.rgb += interleaved_gradient_noise(gl_FragCoord.xy) * params.luminance_multiplier; |
| 315 |
#endif |
| 316 |
} |