| 1 |
/* clang-format off */ |
| 2 |
#[modes] |
| 3 |
|
| 4 |
mode_background = |
| 5 |
mode_cubemap = #define USE_CUBEMAP_PASS |
| 6 |
|
| 7 |
#[specializations] |
| 8 |
|
| 9 |
USE_MULTIVIEW = false |
| 10 |
USE_INVERTED_Y = true |
| 11 |
APPLY_TONEMAPPING = true |
| 12 |
USE_QUARTER_RES_PASS = false |
| 13 |
USE_HALF_RES_PASS = false |
| 14 |
|
| 15 |
#[vertex] |
| 16 |
|
| 17 |
layout(location = 0) in vec2 vertex_attrib; |
| 18 |
|
| 19 |
out vec2 uv_interp; |
| 20 |
/* clang-format on */ |
| 21 |
|
| 22 |
void main() { |
| 23 |
#ifdef USE_INVERTED_Y |
| 24 |
uv_interp = vertex_attrib; |
| 25 |
#else |
| 26 |
// We're doing clockwise culling so flip the order |
| 27 |
uv_interp = vec2(vertex_attrib.x, vertex_attrib.y * -1.0); |
| 28 |
#endif |
| 29 |
gl_Position = vec4(uv_interp, -1.0, 1.0); |
| 30 |
} |
| 31 |
|
| 32 |
/* clang-format off */ |
| 33 |
#[fragment] |
| 34 |
|
| 35 |
#define M_PI 3.14159265359 |
| 36 |
|
| 37 |
#include "tonemap_inc.glsl" |
| 38 |
|
| 39 |
in vec2 uv_interp; |
| 40 |
|
| 41 |
/* clang-format on */ |
| 42 |
|
| 43 |
uniform samplerCube radiance; //texunit:-1 |
| 44 |
#ifdef USE_CUBEMAP_PASS |
| 45 |
uniform samplerCube half_res; //texunit:-2 |
| 46 |
uniform samplerCube quarter_res; //texunit:-3 |
| 47 |
#elif defined(USE_MULTIVIEW) |
| 48 |
uniform sampler2DArray half_res; //texunit:-2 |
| 49 |
uniform sampler2DArray quarter_res; //texunit:-3 |
| 50 |
#else |
| 51 |
uniform sampler2D half_res; //texunit:-2 |
| 52 |
uniform sampler2D quarter_res; //texunit:-3 |
| 53 |
#endif |
| 54 |
|
| 55 |
layout(std140) uniform GlobalShaderUniformData { //ubo:1 |
| 56 |
vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; |
| 57 |
}; |
| 58 |
|
| 59 |
struct DirectionalLightData { |
| 60 |
vec4 direction_energy; |
| 61 |
vec4 color_size; |
| 62 |
uint enabled_bake_mode; |
| 63 |
float shadow_opacity; |
| 64 |
float specular; |
| 65 |
uint mask; |
| 66 |
}; |
| 67 |
|
| 68 |
layout(std140) uniform DirectionalLights { //ubo:4 |
| 69 |
DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; |
| 70 |
} |
| 71 |
directional_lights; |
| 72 |
|
| 73 |
#define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0) |
| 74 |
|
| 75 |
/* clang-format off */ |
| 76 |
|
| 77 |
#ifdef MATERIAL_UNIFORMS_USED |
| 78 |
layout(std140) uniform MaterialUniforms{ //ubo:3 |
| 79 |
|
| 80 |
#MATERIAL_UNIFORMS |
| 81 |
|
| 82 |
}; |
| 83 |
#endif |
| 84 |
/* clang-format on */ |
| 85 |
#GLOBALS |
| 86 |
|
| 87 |
#ifdef USE_CUBEMAP_PASS |
| 88 |
#define AT_CUBEMAP_PASS true |
| 89 |
#else |
| 90 |
#define AT_CUBEMAP_PASS false |
| 91 |
#endif |
| 92 |
|
| 93 |
#ifdef USE_HALF_RES_PASS |
| 94 |
#define AT_HALF_RES_PASS true |
| 95 |
#else |
| 96 |
#define AT_HALF_RES_PASS false |
| 97 |
#endif |
| 98 |
|
| 99 |
#ifdef USE_QUARTER_RES_PASS |
| 100 |
#define AT_QUARTER_RES_PASS true |
| 101 |
#else |
| 102 |
#define AT_QUARTER_RES_PASS false |
| 103 |
#endif |
| 104 |
|
| 105 |
// mat4 is a waste of space, but we don't have an easy way to set a mat3 uniform for now |
| 106 |
uniform mat4 orientation; |
| 107 |
uniform vec4 projection; |
| 108 |
uniform vec3 position; |
| 109 |
uniform float time; |
| 110 |
uniform float sky_energy_multiplier; |
| 111 |
uniform float luminance_multiplier; |
| 112 |
|
| 113 |
uniform float fog_aerial_perspective; |
| 114 |
uniform vec4 fog_light_color; |
| 115 |
uniform float fog_sun_scatter; |
| 116 |
uniform bool fog_enabled; |
| 117 |
uniform float fog_density; |
| 118 |
uniform float fog_sky_affect; |
| 119 |
uniform uint directional_light_count; |
| 120 |
|
| 121 |
#ifdef USE_MULTIVIEW |
| 122 |
layout(std140) uniform MultiviewData { // ubo:11 |
| 123 |
highp mat4 projection_matrix_view[MAX_VIEWS]; |
| 124 |
highp mat4 inv_projection_matrix_view[MAX_VIEWS]; |
| 125 |
highp vec4 eye_offset[MAX_VIEWS]; |
| 126 |
} |
| 127 |
multiview_data; |
| 128 |
#endif |
| 129 |
|
| 130 |
layout(location = 0) out vec4 frag_color; |
| 131 |
|
| 132 |
#ifdef USE_DEBANDING |
| 133 |
// https://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare |
| 134 |
vec3 interleaved_gradient_noise(vec2 pos) { |
| 135 |
const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f); |
| 136 |
float res = fract(magic.z * fract(dot(pos, magic.xy))) * 2.0 - 1.0; |
| 137 |
return vec3(res, -res, res) / 255.0; |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
#if !defined(DISABLE_FOG) |
| 142 |
vec4 fog_process(vec3 view, vec3 sky_color) { |
| 143 |
vec3 fog_color = mix(fog_light_color.rgb, sky_color, fog_aerial_perspective); |
| 144 |
|
| 145 |
if (fog_sun_scatter > 0.001) { |
| 146 |
vec4 sun_scatter = vec4(0.0); |
| 147 |
float sun_total = 0.0; |
| 148 |
for (uint i = 0u; i < directional_light_count; i++) { |
| 149 |
vec3 light_color = srgb_to_linear(directional_lights.data[i].color_size.xyz) * directional_lights.data[i].direction_energy.w; |
| 150 |
float light_amount = pow(max(dot(view, directional_lights.data[i].direction_energy.xyz), 0.0), 8.0) * M_PI; |
| 151 |
fog_color += light_color * light_amount * fog_sun_scatter; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return vec4(fog_color, 1.0); |
| 156 |
} |
| 157 |
#endif // !DISABLE_FOG |
| 158 |
|
| 159 |
// Eberly approximations from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. |
| 160 |
// input [-1, 1] and output [0, PI] |
| 161 |
float acos_approx(float p_x) { |
| 162 |
float x = abs(p_x); |
| 163 |
float res = -0.156583f * x + (M_PI / 2.0); |
| 164 |
res *= sqrt(1.0f - x); |
| 165 |
return (p_x >= 0.0) ? res : M_PI - res; |
| 166 |
} |
| 167 |
|
| 168 |
// Based on https://math.stackexchange.com/questions/1098487/atan2-faster-approximation |
| 169 |
// but using the Eberly coefficients from https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/. |
| 170 |
float atan2_approx(float y, float x) { |
| 171 |
float a = min(abs(x), abs(y)) / max(abs(x), abs(y)); |
| 172 |
float s = a * a; |
| 173 |
float poly = 0.0872929f; |
| 174 |
poly = -0.301895f + poly * s; |
| 175 |
poly = 1.0f + poly * s; |
| 176 |
poly = poly * a; |
| 177 |
|
| 178 |
float r = abs(y) > abs(x) ? (M_PI / 2.0) - poly : poly; |
| 179 |
r = x < 0.0 ? M_PI - r : r; |
| 180 |
r = y < 0.0 ? -r : r; |
| 181 |
|
| 182 |
return r; |
| 183 |
} |
| 184 |
|
| 185 |
void main() { |
| 186 |
vec3 cube_normal; |
| 187 |
#ifdef USE_MULTIVIEW |
| 188 |
// In multiview our projection matrices will contain positional and rotational offsets that we need to properly unproject. |
| 189 |
vec4 unproject = vec4(uv_interp.xy, -1.0, 1.0); // unproject at the far plane |
| 190 |
vec4 unprojected = multiview_data.inv_projection_matrix_view[ViewIndex] * unproject; |
| 191 |
cube_normal = unprojected.xyz / unprojected.w; |
| 192 |
|
| 193 |
// Unproject will give us the position between the eyes, need to re-offset. |
| 194 |
cube_normal += multiview_data.eye_offset[ViewIndex].xyz; |
| 195 |
#else |
| 196 |
cube_normal.z = -1.0; |
| 197 |
cube_normal.x = (uv_interp.x + projection.x) / projection.y; |
| 198 |
cube_normal.y = (-uv_interp.y - projection.z) / projection.w; |
| 199 |
#endif |
| 200 |
cube_normal = mat3(orientation) * cube_normal; |
| 201 |
cube_normal = normalize(cube_normal); |
| 202 |
|
| 203 |
vec2 uv = gl_FragCoord.xy; // uv_interp * 0.5 + 0.5; |
| 204 |
|
| 205 |
vec2 panorama_coords = vec2(atan2_approx(cube_normal.x, -cube_normal.z), acos_approx(cube_normal.y)); |
| 206 |
|
| 207 |
if (panorama_coords.x < 0.0) { |
| 208 |
panorama_coords.x += M_PI * 2.0; |
| 209 |
} |
| 210 |
|
| 211 |
panorama_coords /= vec2(M_PI * 2.0, M_PI); |
| 212 |
|
| 213 |
vec3 color = vec3(0.0, 0.0, 0.0); |
| 214 |
float alpha = 1.0; // Only available to subpasses |
| 215 |
vec4 half_res_color = vec4(1.0); |
| 216 |
vec4 quarter_res_color = vec4(1.0); |
| 217 |
vec4 custom_fog = vec4(0.0); |
| 218 |
|
| 219 |
#ifdef USE_CUBEMAP_PASS |
| 220 |
#ifdef USES_HALF_RES_COLOR |
| 221 |
half_res_color = texture(samplerCube(half_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_normal); |
| 222 |
#endif |
| 223 |
#ifdef USES_QUARTER_RES_COLOR |
| 224 |
quarter_res_color = texture(samplerCube(quarter_res, SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), cube_normal); |
| 225 |
#endif |
| 226 |
#else |
| 227 |
#ifdef USES_HALF_RES_COLOR |
| 228 |
#ifdef USE_MULTIVIEW |
| 229 |
half_res_color = textureLod(sampler2DArray(half_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0); |
| 230 |
#else |
| 231 |
half_res_color = textureLod(sampler2D(half_res, SAMPLER_LINEAR_CLAMP), uv, 0.0); |
| 232 |
#endif |
| 233 |
#endif |
| 234 |
#ifdef USES_QUARTER_RES_COLOR |
| 235 |
#ifdef USE_MULTIVIEW |
| 236 |
quarter_res_color = textureLod(sampler2DArray(quarter_res, SAMPLER_LINEAR_CLAMP), vec3(uv, ViewIndex), 0.0); |
| 237 |
#else |
| 238 |
quarter_res_color = textureLod(sampler2D(quarter_res, SAMPLER_LINEAR_CLAMP), uv, 0.0); |
| 239 |
#endif |
| 240 |
#endif |
| 241 |
#endif |
| 242 |
|
| 243 |
{ |
| 244 |
#CODE : SKY |
| 245 |
} |
| 246 |
|
| 247 |
color *= sky_energy_multiplier; |
| 248 |
|
| 249 |
// Convert to Linear for tonemapping so color matches scene shader better |
| 250 |
color = srgb_to_linear(color); |
| 251 |
|
| 252 |
#if !defined(DISABLE_FOG) && !defined(USE_CUBEMAP_PASS) |
| 253 |
|
| 254 |
// Draw "fixed" fog before volumetric fog to ensure volumetric fog can appear in front of the sky. |
| 255 |
if (fog_enabled) { |
| 256 |
vec4 fog = fog_process(cube_normal, color.rgb); |
| 257 |
color.rgb = mix(color.rgb, fog.rgb, fog.a * fog_sky_affect); |
| 258 |
} |
| 259 |
|
| 260 |
if (custom_fog.a > 0.0) { |
| 261 |
color.rgb = mix(color.rgb, custom_fog.rgb, custom_fog.a); |
| 262 |
} |
| 263 |
|
| 264 |
#endif // DISABLE_FOG |
| 265 |
|
| 266 |
color *= exposure; |
| 267 |
#ifdef APPLY_TONEMAPPING |
| 268 |
color = apply_tonemapping(color); |
| 269 |
#endif |
| 270 |
color = linear_to_srgb(color); |
| 271 |
|
| 272 |
frag_color.rgb = color * luminance_multiplier; |
| 273 |
frag_color.a = alpha; |
| 274 |
|
| 275 |
#ifdef USE_DEBANDING |
| 276 |
frag_color.rgb += interleaved_gradient_noise(gl_FragCoord.xy) * sky_energy_multiplier * luminance_multiplier; |
| 277 |
#endif |
| 278 |
} |