| 1 |
/* clang-format off */ |
| 2 |
#[vertex] |
| 3 |
|
| 4 |
#version 450 |
| 5 |
|
| 6 |
#VERSION_DEFINES |
| 7 |
|
| 8 |
#include "blur_raster_inc.glsl" |
| 9 |
|
| 10 |
layout(location = 0) out vec2 uv_interp; |
| 11 |
/* clang-format on */ |
| 12 |
|
| 13 |
void main() { |
| 14 |
// old code, ARM driver bug on Mali-GXXx GPUs and Vulkan API 1.3.xxx |
| 15 |
// https://github.com/godotengine/godot/pull/92817#issuecomment-2168625982 |
| 16 |
//vec2 base_arr[3] = vec2[](vec2(-1.0, -1.0), vec2(-1.0, 3.0), vec2(3.0, -1.0)); |
| 17 |
//gl_Position = vec4(base_arr[gl_VertexIndex], 0.0, 1.0); |
| 18 |
//uv_interp = clamp(gl_Position.xy, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 19 |
|
| 20 |
vec2 vertex_base; |
| 21 |
if (gl_VertexIndex == 0) { |
| 22 |
vertex_base = vec2(-1.0, -1.0); |
| 23 |
} else if (gl_VertexIndex == 1) { |
| 24 |
vertex_base = vec2(-1.0, 3.0); |
| 25 |
} else { |
| 26 |
vertex_base = vec2(3.0, -1.0); |
| 27 |
} |
| 28 |
gl_Position = vec4(vertex_base, 0.0, 1.0); |
| 29 |
uv_interp = clamp(vertex_base, vec2(0.0, 0.0), vec2(1.0, 1.0)) * 2.0; // saturate(x) * 2.0 |
| 30 |
} |
| 31 |
|
| 32 |
/* clang-format off */ |
| 33 |
#[fragment] |
| 34 |
|
| 35 |
#version 450 |
| 36 |
|
| 37 |
#VERSION_DEFINES |
| 38 |
|
| 39 |
#include "blur_raster_inc.glsl" |
| 40 |
|
| 41 |
layout(location = 0) in vec2 uv_interp; |
| 42 |
/* clang-format on */ |
| 43 |
|
| 44 |
layout(set = 0, binding = 0) uniform sampler2D source_color; |
| 45 |
|
| 46 |
#ifdef MODE_GLOW_UPSAMPLE |
| 47 |
// When upsampling this is original downsampled texture, not the blended upsampled texture. |
| 48 |
layout(set = 1, binding = 0) uniform sampler2D blend_color; |
| 49 |
layout(constant_id = 0) const bool use_debanding = false; |
| 50 |
layout(constant_id = 1) const bool use_blend_color = false; |
| 51 |
|
| 52 |
// From https://alex.vlachos.com/graphics/Alex_Vlachos_Advanced_VR_Rendering_GDC2015.pdf |
| 53 |
// and https://www.shadertoy.com/view/MslGR8 (5th one starting from the bottom) |
| 54 |
// NOTE: `frag_coord` is in pixels (i.e. not normalized UV). |
| 55 |
// This dithering must be applied after encoding changes (linear/nonlinear) have been applied |
| 56 |
// as the final step before quantization from floating point to integer values. |
| 57 |
vec3 screen_space_dither(vec2 frag_coord, float bit_alignment_diviser) { |
| 58 |
// Iestyn's RGB dither (7 asm instructions) from Portal 2 X360, slightly modified for VR. |
| 59 |
// Removed the time component to avoid passing time into this shader. |
| 60 |
vec3 dither = vec3(dot(vec2(171.0, 231.0), frag_coord)); |
| 61 |
dither.rgb = fract(dither.rgb / vec3(103.0, 71.0, 97.0)); |
| 62 |
|
| 63 |
// Subtract 0.5 to avoid slightly brightening the whole viewport. |
| 64 |
// Use a dither strength of 100% rather than the 37.5% suggested by the original source. |
| 65 |
return (dither.rgb - 0.5) / bit_alignment_diviser; |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
layout(location = 0) out vec4 frag_color; |
| 70 |
|
| 71 |
#ifdef MODE_GLOW_DOWNSAMPLE |
| 72 |
|
| 73 |
// https://www.shadertoy.com/view/mdsyDf |
| 74 |
vec4 BloomDownKernel4(sampler2D Tex, vec2 uv0) { |
| 75 |
vec2 RcpSrcTexRes = blur.source_pixel_size; |
| 76 |
|
| 77 |
vec2 tc = (uv0 * 2.0 + 1.0) * RcpSrcTexRes; |
| 78 |
|
| 79 |
float la = 1.0 / 4.0; |
| 80 |
|
| 81 |
vec2 o = (0.5 + la) * RcpSrcTexRes; |
| 82 |
|
| 83 |
vec4 c = vec4(0.0); |
| 84 |
c += textureLod(Tex, tc + vec2(-1.0, -1.0) * o, 0.0) * 0.25; |
| 85 |
c += textureLod(Tex, tc + vec2(1.0, -1.0) * o, 0.0) * 0.25; |
| 86 |
c += textureLod(Tex, tc + vec2(-1.0, 1.0) * o, 0.0) * 0.25; |
| 87 |
c += textureLod(Tex, tc + vec2(1.0, 1.0) * o, 0.0) * 0.25; |
| 88 |
|
| 89 |
return c; |
| 90 |
} |
| 91 |
|
| 92 |
#endif |
| 93 |
|
| 94 |
#ifdef MODE_GLOW_UPSAMPLE |
| 95 |
|
| 96 |
// https://www.shadertoy.com/view/mdsyDf |
| 97 |
vec4 BloomUpKernel4(sampler2D Tex, vec2 uv0) { |
| 98 |
vec2 RcpSrcTexRes = blur.source_pixel_size; |
| 99 |
|
| 100 |
vec2 uv = uv0 * 0.5 + 0.5; |
| 101 |
|
| 102 |
vec2 uvI = floor(uv); |
| 103 |
vec2 uvF = uv - uvI; |
| 104 |
|
| 105 |
vec2 tc = uvI * RcpSrcTexRes.xy; |
| 106 |
|
| 107 |
// optimal stop-band |
| 108 |
float lw = 0.357386; |
| 109 |
float la = 25.0 / 32.0; // 0.78125 ~ 0.779627; |
| 110 |
float lb = 3.0 / 64.0; // 0.046875 ~ 0.0493871; |
| 111 |
|
| 112 |
vec2 l = vec2(-1.5 + la, 0.5 + lb); |
| 113 |
|
| 114 |
vec2 lx = uvF.x == 0.0 ? l.xy : -l.yx; |
| 115 |
vec2 ly = uvF.y == 0.0 ? l.xy : -l.yx; |
| 116 |
|
| 117 |
lx *= RcpSrcTexRes.xx; |
| 118 |
ly *= RcpSrcTexRes.yy; |
| 119 |
|
| 120 |
vec4 c00 = textureLod(Tex, tc + vec2(lx.x, ly.x), 0.0); |
| 121 |
vec4 c10 = textureLod(Tex, tc + vec2(lx.y, ly.x), 0.0); |
| 122 |
vec4 c01 = textureLod(Tex, tc + vec2(lx.x, ly.y), 0.0); |
| 123 |
vec4 c11 = textureLod(Tex, tc + vec2(lx.y, ly.y), 0.0); |
| 124 |
|
| 125 |
vec2 w = abs(uvF * 2.0 - lw); |
| 126 |
|
| 127 |
vec4 cx0 = c00 * (1.0 - w.x) + (c10 * w.x); |
| 128 |
vec4 cx1 = c01 * (1.0 - w.x) + (c11 * w.x); |
| 129 |
|
| 130 |
vec4 cxy = cx0 * (1.0 - w.y) + (cx1 * w.y); |
| 131 |
|
| 132 |
return cxy; |
| 133 |
} |
| 134 |
|
| 135 |
#endif // MODE_GLOW_UPSAMPLE |
| 136 |
|
| 137 |
void main() { |
| 138 |
// We do not apply our color scale for our mobile renderer here, we'll leave our colors at half brightness and apply scale in the tonemap raster. |
| 139 |
|
| 140 |
#ifdef MODE_MIPMAP |
| 141 |
|
| 142 |
vec2 pix_size = blur.dest_pixel_size; |
| 143 |
vec4 color = texture(source_color, uv_interp + vec2(-0.5, -0.5) * pix_size); |
| 144 |
color += texture(source_color, uv_interp + vec2(0.5, -0.5) * pix_size); |
| 145 |
color += texture(source_color, uv_interp + vec2(0.5, 0.5) * pix_size); |
| 146 |
color += texture(source_color, uv_interp + vec2(-0.5, 0.5) * pix_size); |
| 147 |
frag_color = color / 4.0; |
| 148 |
|
| 149 |
#endif |
| 150 |
|
| 151 |
#ifdef MODE_GAUSSIAN_BLUR |
| 152 |
|
| 153 |
// For Gaussian Blur we use 13 taps in a single pass instead of 12 taps over 2 passes. |
| 154 |
// This minimizes the number of times we change framebuffers which is very important for mobile. |
| 155 |
// Source: http://www.iryoku.com/next-generation-post-processing-in-call-of-duty-advanced-warfare |
| 156 |
vec4 A = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(-1.0, -1.0)); |
| 157 |
vec4 B = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(0.0, -1.0)); |
| 158 |
vec4 C = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(1.0, -1.0)); |
| 159 |
vec4 D = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(-0.5, -0.5)); |
| 160 |
vec4 E = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(0.5, -0.5)); |
| 161 |
vec4 F = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(-1.0, 0.0)); |
| 162 |
vec4 G = texture(source_color, uv_interp); |
| 163 |
vec4 H = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(1.0, 0.0)); |
| 164 |
vec4 I = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(-0.5, 0.5)); |
| 165 |
vec4 J = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(0.5, 0.5)); |
| 166 |
vec4 K = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(-1.0, 1.0)); |
| 167 |
vec4 L = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(0.0, 1.0)); |
| 168 |
vec4 M = texture(source_color, uv_interp + blur.dest_pixel_size * vec2(1.0, 1.0)); |
| 169 |
|
| 170 |
float base_weight = 0.5 / 4.0; |
| 171 |
float lesser_weight = 0.125 / 4.0; |
| 172 |
|
| 173 |
frag_color = (D + E + I + J) * base_weight; |
| 174 |
frag_color += (A + B + G + F) * lesser_weight; |
| 175 |
frag_color += (B + C + H + G) * lesser_weight; |
| 176 |
frag_color += (F + G + L + K) * lesser_weight; |
| 177 |
frag_color += (G + H + M + L) * lesser_weight; |
| 178 |
#endif |
| 179 |
|
| 180 |
#ifdef MODE_GLOW_GATHER |
| 181 |
// First step, go straight to quarter resolution. |
| 182 |
// Don't apply blur, but include thresholding. |
| 183 |
|
| 184 |
vec2 block_pos = floor(gl_FragCoord.xy) * 4.0; |
| 185 |
vec2 end = max(1.0 / blur.source_pixel_size - vec2(4.0), vec2(0.0)); |
| 186 |
block_pos = clamp(block_pos, vec2(0.0), end); |
| 187 |
|
| 188 |
// We skipped a level, so gather 16 closest samples now. |
| 189 |
|
| 190 |
vec4 color = textureLod(source_color, (block_pos + vec2(1.0, 1.0)) * blur.source_pixel_size, 0.0); |
| 191 |
color += textureLod(source_color, (block_pos + vec2(1.0, 3.0)) * blur.source_pixel_size, 0.0); |
| 192 |
color += textureLod(source_color, (block_pos + vec2(3.0, 1.0)) * blur.source_pixel_size, 0.0); |
| 193 |
color += textureLod(source_color, (block_pos + vec2(3.0, 3.0)) * blur.source_pixel_size, 0.0); |
| 194 |
frag_color = color * 0.25; |
| 195 |
|
| 196 |
// Apply strength a second time since it usually gets added at each level. |
| 197 |
frag_color *= blur.glow_strength; |
| 198 |
frag_color *= blur.glow_strength; |
| 199 |
|
| 200 |
// In the first pass bring back to correct color range else we're applying the wrong threshold |
| 201 |
// in subsequent passes we can use it as is as we'd just be undoing it right after. |
| 202 |
frag_color *= blur.luminance_multiplier; |
| 203 |
frag_color *= blur.glow_exposure; |
| 204 |
|
| 205 |
float luminance = max(frag_color.r, max(frag_color.g, frag_color.b)); |
| 206 |
float feedback = max(smoothstep(blur.glow_hdr_threshold, blur.glow_hdr_threshold + blur.glow_hdr_scale, luminance), blur.glow_bloom); |
| 207 |
|
| 208 |
frag_color = min(frag_color * feedback, vec4(blur.glow_luminance_cap)) / blur.luminance_multiplier; |
| 209 |
#endif // MODE_GLOW_GATHER_WIDE |
| 210 |
|
| 211 |
#ifdef MODE_GLOW_DOWNSAMPLE |
| 212 |
// Regular downsample, apply a simple blur. |
| 213 |
frag_color = BloomDownKernel4(source_color, floor(gl_FragCoord.xy)); |
| 214 |
frag_color *= blur.glow_strength; |
| 215 |
#endif // MODE_GLOW_DOWNSAMPLE |
| 216 |
|
| 217 |
#ifdef MODE_GLOW_UPSAMPLE |
| 218 |
|
| 219 |
frag_color = BloomUpKernel4(source_color, floor(gl_FragCoord.xy)) * blur.glow_strength; // "glow_strength" here is actually the glow level. It is always 1.0, except for the first upsample where we need to apply the level to two textures at once. |
| 220 |
if (use_blend_color) { |
| 221 |
vec2 uv = floor(gl_FragCoord.xy) + 0.5; |
| 222 |
frag_color += textureLod(blend_color, uv * blur.dest_pixel_size, 0.0) * blur.glow_level; |
| 223 |
} |
| 224 |
|
| 225 |
if (use_debanding) { |
| 226 |
frag_color.rgb += screen_space_dither(gl_FragCoord.xy, 1023.0); |
| 227 |
} |
| 228 |
#endif // MODE_GLOW_UPSAMPLE |
| 229 |
|
| 230 |
#ifdef MODE_COPY |
| 231 |
vec4 color = textureLod(source_color, uv_interp, 0.0); |
| 232 |
frag_color = color; |
| 233 |
#endif |
| 234 |
} |