| 1 |
/* clang-format off */ |
| 2 |
[vertex] |
| 3 |
|
| 4 |
#ifdef USE_GLES_OVER_GL |
| 5 |
#define lowp |
| 6 |
#define mediump |
| 7 |
#define highp |
| 8 |
#else |
| 9 |
precision highp float; |
| 10 |
precision highp int; |
| 11 |
#endif |
| 12 |
|
| 13 |
layout(location = 0) in vec2 vertex_attrib; |
| 14 |
/* clang-format on */ |
| 15 |
layout(location = 4) in vec2 uv_in; |
| 16 |
|
| 17 |
out vec2 uv_interp; |
| 18 |
|
| 19 |
#ifdef USE_BLUR_SECTION |
| 20 |
|
| 21 |
uniform vec4 blur_section; |
| 22 |
|
| 23 |
#endif |
| 24 |
|
| 25 |
void main() { |
| 26 |
uv_interp = uv_in; |
| 27 |
gl_Position = vec4(vertex_attrib, 0.0, 1.0); |
| 28 |
#ifdef USE_BLUR_SECTION |
| 29 |
|
| 30 |
uv_interp = blur_section.xy + uv_interp * blur_section.zw; |
| 31 |
gl_Position.xy = (blur_section.xy + (gl_Position.xy * 0.5 + 0.5) * blur_section.zw) * 2.0 - 1.0; |
| 32 |
#endif |
| 33 |
} |
| 34 |
|
| 35 |
/* clang-format off */ |
| 36 |
[fragment] |
| 37 |
|
| 38 |
#ifdef USE_GLES_OVER_GL |
| 39 |
#define lowp |
| 40 |
#define mediump |
| 41 |
#define highp |
| 42 |
#else |
| 43 |
#if defined(USE_HIGHP_PRECISION) |
| 44 |
precision highp float; |
| 45 |
precision highp int; |
| 46 |
#else |
| 47 |
precision mediump float; |
| 48 |
precision mediump int; |
| 49 |
#endif |
| 50 |
#endif |
| 51 |
|
| 52 |
in vec2 uv_interp; |
| 53 |
/* clang-format on */ |
| 54 |
uniform sampler2D source_color; //texunit:0 |
| 55 |
|
| 56 |
uniform float lod; |
| 57 |
uniform vec2 pixel_size; |
| 58 |
|
| 59 |
#if defined(GLOW_GAUSSIAN_HORIZONTAL) || defined(GLOW_GAUSSIAN_VERTICAL) |
| 60 |
|
| 61 |
uniform float glow_strength; |
| 62 |
|
| 63 |
#endif |
| 64 |
|
| 65 |
#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) |
| 66 |
|
| 67 |
#ifdef USE_GLES_OVER_GL |
| 68 |
#ifdef DOF_QUALITY_LOW |
| 69 |
const int dof_kernel_size = 5; |
| 70 |
const int dof_kernel_from = 2; |
| 71 |
const float dof_kernel[5] = float[](0.153388, 0.221461, 0.250301, 0.221461, 0.153388); |
| 72 |
#endif |
| 73 |
|
| 74 |
#ifdef DOF_QUALITY_MEDIUM |
| 75 |
const int dof_kernel_size = 11; |
| 76 |
const int dof_kernel_from = 5; |
| 77 |
const float dof_kernel[11] = float[](0.055037, 0.072806, 0.090506, 0.105726, 0.116061, 0.119726, 0.116061, 0.105726, 0.090506, 0.072806, 0.055037); |
| 78 |
|
| 79 |
#endif |
| 80 |
|
| 81 |
#ifdef DOF_QUALITY_HIGH |
| 82 |
const int dof_kernel_size = 21; |
| 83 |
const int dof_kernel_from = 10; |
| 84 |
const float dof_kernel[21] = float[](0.028174, 0.032676, 0.037311, 0.041944, 0.046421, 0.050582, 0.054261, 0.057307, 0.059587, 0.060998, 0.061476, 0.060998, 0.059587, 0.057307, 0.054261, 0.050582, 0.046421, 0.041944, 0.037311, 0.032676, 0.028174); |
| 85 |
#endif |
| 86 |
#endif |
| 87 |
|
| 88 |
uniform sampler2D dof_source_depth; //texunit:1 |
| 89 |
uniform float dof_begin; |
| 90 |
uniform float dof_end; |
| 91 |
uniform vec2 dof_dir; |
| 92 |
uniform float dof_radius; |
| 93 |
|
| 94 |
#endif |
| 95 |
|
| 96 |
#ifdef GLOW_FIRST_PASS |
| 97 |
|
| 98 |
uniform highp float luminance_cap; |
| 99 |
|
| 100 |
uniform float glow_bloom; |
| 101 |
uniform float glow_hdr_threshold; |
| 102 |
uniform float glow_hdr_scale; |
| 103 |
|
| 104 |
#endif |
| 105 |
|
| 106 |
uniform float camera_z_far; |
| 107 |
uniform float camera_z_near; |
| 108 |
|
| 109 |
layout(location = 0) out vec4 frag_color; |
| 110 |
|
| 111 |
void main() { |
| 112 |
#ifdef GLOW_GAUSSIAN_HORIZONTAL |
| 113 |
vec2 pix_size = pixel_size; |
| 114 |
pix_size *= 0.5; //reading from larger buffer, so use more samples |
| 115 |
vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pix_size, lod) * 0.174938; |
| 116 |
color += textureLod(source_color, uv_interp + vec2(1.0, 0.0) * pix_size, lod) * 0.165569; |
| 117 |
color += textureLod(source_color, uv_interp + vec2(2.0, 0.0) * pix_size, lod) * 0.140367; |
| 118 |
color += textureLod(source_color, uv_interp + vec2(3.0, 0.0) * pix_size, lod) * 0.106595; |
| 119 |
color += textureLod(source_color, uv_interp + vec2(-1.0, 0.0) * pix_size, lod) * 0.165569; |
| 120 |
color += textureLod(source_color, uv_interp + vec2(-2.0, 0.0) * pix_size, lod) * 0.140367; |
| 121 |
color += textureLod(source_color, uv_interp + vec2(-3.0, 0.0) * pix_size, lod) * 0.106595; |
| 122 |
color *= glow_strength; |
| 123 |
frag_color = color; |
| 124 |
#endif |
| 125 |
|
| 126 |
#ifdef GLOW_GAUSSIAN_VERTICAL |
| 127 |
vec4 color = textureLod(source_color, uv_interp + vec2(0.0, 0.0) * pixel_size, lod) * 0.288713; |
| 128 |
color += textureLod(source_color, uv_interp + vec2(0.0, 1.0) * pixel_size, lod) * 0.233062; |
| 129 |
color += textureLod(source_color, uv_interp + vec2(0.0, 2.0) * pixel_size, lod) * 0.122581; |
| 130 |
color += textureLod(source_color, uv_interp + vec2(0.0, -1.0) * pixel_size, lod) * 0.233062; |
| 131 |
color += textureLod(source_color, uv_interp + vec2(0.0, -2.0) * pixel_size, lod) * 0.122581; |
| 132 |
color *= glow_strength; |
| 133 |
frag_color = color; |
| 134 |
#endif |
| 135 |
|
| 136 |
#ifndef USE_GLES_OVER_GL |
| 137 |
#if defined(DOF_FAR_BLUR) || defined(DOF_NEAR_BLUR) |
| 138 |
|
| 139 |
#ifdef DOF_QUALITY_LOW |
| 140 |
const int dof_kernel_size = 5; |
| 141 |
const int dof_kernel_from = 2; |
| 142 |
float dof_kernel[5]; |
| 143 |
dof_kernel[0] = 0.153388; |
| 144 |
dof_kernel[1] = 0.221461; |
| 145 |
dof_kernel[2] = 0.250301; |
| 146 |
dof_kernel[3] = 0.221461; |
| 147 |
dof_kernel[4] = 0.153388; |
| 148 |
#endif |
| 149 |
|
| 150 |
#ifdef DOF_QUALITY_MEDIUM |
| 151 |
const int dof_kernel_size = 11; |
| 152 |
const int dof_kernel_from = 5; |
| 153 |
float dof_kernel[11]; |
| 154 |
dof_kernel[0] = 0.055037; |
| 155 |
dof_kernel[1] = 0.072806; |
| 156 |
dof_kernel[2] = 0.090506; |
| 157 |
dof_kernel[3] = 0.105726; |
| 158 |
dof_kernel[4] = 0.116061; |
| 159 |
dof_kernel[5] = 0.119726; |
| 160 |
dof_kernel[6] = 0.116061; |
| 161 |
dof_kernel[7] = 0.105726; |
| 162 |
dof_kernel[8] = 0.090506; |
| 163 |
dof_kernel[9] = 0.072806; |
| 164 |
dof_kernel[10] = 0.055037; |
| 165 |
#endif |
| 166 |
|
| 167 |
#ifdef DOF_QUALITY_HIGH |
| 168 |
const int dof_kernel_size = 21; |
| 169 |
const int dof_kernel_from = 10; |
| 170 |
float dof_kernel[21]; |
| 171 |
dof_kernel[0] = 0.028174; |
| 172 |
dof_kernel[1] = 0.032676; |
| 173 |
dof_kernel[2] = 0.037311; |
| 174 |
dof_kernel[3] = 0.041944; |
| 175 |
dof_kernel[4] = 0.046421; |
| 176 |
dof_kernel[5] = 0.050582; |
| 177 |
dof_kernel[6] = 0.054261; |
| 178 |
dof_kernel[7] = 0.057307; |
| 179 |
dof_kernel[8] = 0.059587; |
| 180 |
dof_kernel[9] = 0.060998; |
| 181 |
dof_kernel[10] = 0.061476; |
| 182 |
dof_kernel[11] = 0.060998; |
| 183 |
dof_kernel[12] = 0.059587; |
| 184 |
dof_kernel[13] = 0.057307; |
| 185 |
dof_kernel[14] = 0.054261; |
| 186 |
dof_kernel[15] = 0.050582; |
| 187 |
dof_kernel[16] = 0.046421; |
| 188 |
dof_kernel[17] = 0.041944; |
| 189 |
dof_kernel[18] = 0.037311; |
| 190 |
dof_kernel[19] = 0.032676; |
| 191 |
dof_kernel[20] = 0.028174; |
| 192 |
#endif |
| 193 |
#endif |
| 194 |
#endif //!USE_GLES_OVER_GL |
| 195 |
|
| 196 |
#ifdef DOF_FAR_BLUR |
| 197 |
|
| 198 |
vec4 color_accum = vec4(0.0); |
| 199 |
|
| 200 |
float depth = textureLod(dof_source_depth, uv_interp, 0.0).r; |
| 201 |
depth = depth * 2.0 - 1.0; |
| 202 |
#ifdef USE_ORTHOGONAL_PROJECTION |
| 203 |
depth = ((depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; |
| 204 |
#else |
| 205 |
depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - depth * (camera_z_far - camera_z_near)); |
| 206 |
#endif |
| 207 |
|
| 208 |
float amount = smoothstep(dof_begin, dof_end, depth); |
| 209 |
float k_accum = 0.0; |
| 210 |
|
| 211 |
for (int i = 0; i < dof_kernel_size; i++) { |
| 212 |
int int_ofs = i - dof_kernel_from; |
| 213 |
vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * amount * dof_radius; |
| 214 |
|
| 215 |
float tap_k = dof_kernel[i]; |
| 216 |
|
| 217 |
float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; |
| 218 |
tap_depth = tap_depth * 2.0 - 1.0; |
| 219 |
#ifdef USE_ORTHOGONAL_PROJECTION |
| 220 |
tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; |
| 221 |
#else |
| 222 |
tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); |
| 223 |
#endif |
| 224 |
float tap_amount = int_ofs == 0 ? 1.0 : smoothstep(dof_begin, dof_end, tap_depth); |
| 225 |
tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect |
| 226 |
|
| 227 |
vec4 tap_color = textureLod(source_color, tap_uv, 0.0) * tap_k; |
| 228 |
|
| 229 |
k_accum += tap_k * tap_amount; |
| 230 |
color_accum += tap_color * tap_amount; |
| 231 |
} |
| 232 |
|
| 233 |
if (k_accum > 0.0) { |
| 234 |
color_accum /= k_accum; |
| 235 |
} |
| 236 |
|
| 237 |
frag_color = color_accum; ///k_accum; |
| 238 |
|
| 239 |
#endif |
| 240 |
|
| 241 |
#ifdef DOF_NEAR_BLUR |
| 242 |
|
| 243 |
vec4 color_accum = vec4(0.0); |
| 244 |
|
| 245 |
float max_accum = 0.0; |
| 246 |
|
| 247 |
for (int i = 0; i < dof_kernel_size; i++) { |
| 248 |
int int_ofs = i - dof_kernel_from; |
| 249 |
vec2 tap_uv = uv_interp + dof_dir * float(int_ofs) * dof_radius; |
| 250 |
float ofs_influence = max(0.0, 1.0 - abs(float(int_ofs)) / float(dof_kernel_from)); |
| 251 |
|
| 252 |
float tap_k = dof_kernel[i]; |
| 253 |
|
| 254 |
vec4 tap_color = textureLod(source_color, tap_uv, 0.0); |
| 255 |
|
| 256 |
float tap_depth = texture(dof_source_depth, tap_uv, 0.0).r; |
| 257 |
tap_depth = tap_depth * 2.0 - 1.0; |
| 258 |
#ifdef USE_ORTHOGONAL_PROJECTION |
| 259 |
tap_depth = ((tap_depth + (camera_z_far + camera_z_near) / (camera_z_far - camera_z_near)) * (camera_z_far - camera_z_near)) / 2.0; |
| 260 |
#else |
| 261 |
tap_depth = 2.0 * camera_z_near * camera_z_far / (camera_z_far + camera_z_near - tap_depth * (camera_z_far - camera_z_near)); |
| 262 |
#endif |
| 263 |
float tap_amount = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); |
| 264 |
tap_amount *= tap_amount * tap_amount; //prevent undesired glow effect |
| 265 |
|
| 266 |
#ifdef DOF_NEAR_FIRST_TAP |
| 267 |
|
| 268 |
tap_color.a = 1.0 - smoothstep(dof_end, dof_begin, tap_depth); |
| 269 |
|
| 270 |
#endif |
| 271 |
|
| 272 |
max_accum = max(max_accum, tap_amount * ofs_influence); |
| 273 |
|
| 274 |
color_accum += tap_color * tap_k; |
| 275 |
} |
| 276 |
|
| 277 |
color_accum.a = max(color_accum.a, sqrt(max_accum)); |
| 278 |
|
| 279 |
frag_color = color_accum; |
| 280 |
|
| 281 |
#endif |
| 282 |
|
| 283 |
#ifdef GLOW_FIRST_PASS |
| 284 |
|
| 285 |
float luminance = max(frag_color.r, max(frag_color.g, frag_color.b)); |
| 286 |
float feedback = max(smoothstep(glow_hdr_threshold, glow_hdr_threshold + glow_hdr_scale, luminance), glow_bloom); |
| 287 |
|
| 288 |
frag_color = min(frag_color * feedback, vec4(luminance_cap)); |
| 289 |
|
| 290 |
#endif |
| 291 |
} |