Switching to fully guarded memory allocator. Blender 2.82 (sub 7) Build: 2020-03-12 15:41:08 Windows Release argv[0] = blender argv[1] = --debug argv[2] = --debug-cycles argv[3] = --python-expr argv[4] = import bpy; bpy.ops.wm.sysinfo(filepath=r'C:\Users\Viddo\AppData\Local\Temp\blender\debug_logs\blender_system_info.txt') Read prefs: C:\Users\Viddo\AppData\Roaming\Blender Foundation\Blender\2.82\config\userpref.blend read file Version 280 sub 39 date unknown hash unknown AL lib: (EE) UpdateDeviceParams: Failed to set 48000hz, got 192000hz instead found bundled python: C:\Program Files\Blender Foundation\Blender 2.82\2.82\python I0420 17:25:59.085889 6972 blender_python.cpp:184] Debug flags initialized to: CPU flags: AVX2 : True AVX : True SSE4.1 : True SSE3 : True SSE2 : True BVH layout : BVH8 Split : False CUDA flags: Adaptive Compile : False OptiX flags: CUDA streams : 1 OpenCL flags: Device type : ALL Debug : False Memory limit : 0 I0420 17:26:00.133949 6972 device_opencl.cpp:48] CLEW initialization succeeded. OpenCL error: CL_INVALID_VALUE in __clewGetDeviceInfo(device_id, 0x103D, sizeof(data), &data, NULL) (c:\b\win64_cmake_vs2017\win64_cmake_vs2017\blender.git\intern\cycles\device\device_opencl.cpp:206) I0420 17:26:00.146950 6972 device_cuda.cpp:2599] CUEW initialization failed: Error opening the library GPUShader: linking error: ===== shader string 1 ==== 1 #define COMMON_VIEW_LIB 2 #define DRW_RESOURCE_CHUNK_LEN 512 3 4 /* keep in sync with DRWManager.view_data */ 5 layout(std140) uniform viewBlock 6 { 7 /* Same order as DRWViewportMatrixType */ 8 mat4 ViewProjectionMatrix; 9 mat4 ViewProjectionMatrixInverse; 10 mat4 ViewMatrix; 11 mat4 ViewMatrixInverse; 12 mat4 ProjectionMatrix; 13 mat4 ProjectionMatrixInverse; 14 15 vec4 clipPlanes[6]; 16 17 /* TODO move it elsewhere. */ 18 vec4 CameraTexCoFactors; 19 }; 20 21 #ifdef world_clip_planes_calc_clip_distance 22 # undef world_clip_planes_calc_clip_distance 23 # define world_clip_planes_calc_clip_distance(p) \ 24 _world_clip_planes_calc_clip_distance(p, clipPlanes) 25 #endif 26 27 #ifdef COMMON_GLOBALS_LIB 28 float mul_project_m4_v3_zfac(in vec3 co) 29 { 30 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 31 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 32 } 33 #endif 34 35 /* Not the right place but need to be common to all overlay's. 36 * TODO Split to an overlay lib. */ 37 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 38 { 39 const float div = 1.0 / 255.0; 40 int a = int(mat[0][3]); 41 int b = int(mat[1][3]); 42 int c = int(mat[2][3]); 43 int d = int(mat[3][3]); 44 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 45 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 46 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 47 mat[3][3] = 1.0; 48 return mat; 49 } 50 51 /* Same here, Not the right place but need to be common to all overlay's. 52 * TODO Split to an overlay lib. */ 53 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 54 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 55 { 56 vec2 edge = edge_start - edge_pos; 57 float len = length(edge); 58 if (len > 0.0) { 59 edge /= len; 60 vec2 perp = vec2(-edge.y, edge.x); 61 float dist = dot(perp, frag_co - edge_start); 62 /* Add 0.1 to diffenrentiate with cleared pixels. */ 63 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 64 } 65 else { 66 /* Default line if the origin is perfectly aligned with a pixel. */ 67 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 68 } 69 } 70 71 uniform int resourceChunk; 72 73 #ifdef GPU_VERTEX_SHADER 74 # ifdef GL_ARB_shader_draw_parameters 75 # define baseInstance gl_BaseInstanceARB 76 # else /* no ARB_shader_draw_parameters */ 77 uniform int baseInstance; 78 # endif 79 80 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 81 /* When drawing instances of an object at the same position. */ 82 # define instanceId 0 83 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 84 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 85 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 86 * Ignore gl_InstanceID then. */ 87 # define instanceId 0 88 # else 89 # define instanceId gl_InstanceID 90 # endif 91 92 # define resource_id (baseInstance + instanceId) 93 94 /* Use this to declare and pass the value if 95 * the fragment shader uses the resource_id. */ 96 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 97 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 98 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 99 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 100 #endif 101 102 /* If used in a fragment / geometry shader, we pass 103 * resource_id as varying. */ 104 #ifdef GPU_GEOMETRY_SHADER 105 # define RESOURCE_ID_VARYING \ 106 flat out int resourceIDFrag; \ 107 flat in int resourceIDGeom[]; 108 109 # define resource_id resourceIDGeom 110 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 111 #endif 112 113 #ifdef GPU_FRAGMENT_SHADER 114 flat in int resourceIDFrag; 115 # define resource_id resourceIDFrag 116 #endif 117 118 /* Breaking this across multiple lines causes issues for some older GLSL compilers. */ 119 /* clang-format off */ 120 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && !defined(INSTANCED_ATTRIB) 121 /* clang-format on */ 122 struct ObjectMatrices { 123 mat4 drw_modelMatrix; 124 mat4 drw_modelMatrixInverse; 125 }; 126 127 layout(std140) uniform modelBlock 128 { 129 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 130 }; 131 132 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 133 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 134 135 #else /* GPU_INTEL */ 136 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 137 * So for now we just force using the legacy path. */ 138 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 139 * and older amd driver on windows. */ 140 uniform mat4 ModelMatrix; 141 uniform mat4 ModelMatrixInverse; 142 #endif 143 144 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 145 146 /** Transform shortcuts. */ 147 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 148 * will always be decomposed in at least 2 matrix operation. */ 149 150 /** 151 * Some clarification: 152 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 153 * 154 * But since it is slow to multiply matrices we decompose it. Decomposing 155 * inversion and transposition both invert the product order leaving us with 156 * the same original order: 157 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 158 * 159 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 160 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 161 * ViewMatrix * transpose(ModelMatrixInverse) 162 **/ 163 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 164 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 165 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 166 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 167 168 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 169 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 170 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 171 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 172 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 173 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 174 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 175 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 176 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 177 178 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 179 * to make vertex shaders work. even if it's actually dead code. */ 180 #ifdef GPU_INTEL 181 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 182 #else 183 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 184 #endif 185 186 #define DRW_BASE_SELECTED (1 << 1) 187 #define DRW_BASE_FROM_DUPLI (1 << 2) 188 #define DRW_BASE_FROM_SET (1 << 3) 189 #define DRW_BASE_ACTIVE (1 << 4) 190 /** 191 * Library to create hairs dynamically from control points. 192 * This is less bandwidth intensive than fetching the vertex attributes 193 * but does more ALU work per vertex. This also reduces the amount 194 * of data the CPU has to precompute and transfer for each update. 195 */ 196 197 /** 198 * hairStrandsRes: Number of points per hair strand. 199 * 2 - no subdivision 200 * 3+ - 1 or more interpolated points per hair. 201 */ 202 uniform int hairStrandsRes = 8; 203 204 /** 205 * hairThicknessRes : Subdiv around the hair. 206 * 1 - Wire Hair: Only one pixel thick, independent of view distance. 207 * 2 - Polystrip Hair: Correct width, flat if camera is parallel. 208 * 3+ - Cylinder Hair: Massive calculation but potentially perfect. Still need proper support. 209 */ 210 uniform int hairThicknessRes = 1; 211 212 /* Hair thickness shape. */ 213 uniform float hairRadRoot = 0.01; 214 uniform float hairRadTip = 0.0; 215 uniform float hairRadShape = 0.5; 216 uniform bool hairCloseTip = true; 217 218 uniform vec4 hairDupliMatrix[4]; 219 220 /* -- Per control points -- */ 221 uniform samplerBuffer hairPointBuffer; /* RGBA32F */ 222 #define point_position xyz 223 #define point_time w /* Position along the hair length */ 224 225 /* -- Per strands data -- */ 226 uniform usamplerBuffer hairStrandBuffer; /* R32UI */ 227 uniform usamplerBuffer hairStrandSegBuffer; /* R16UI */ 228 229 /* Not used, use one buffer per uv layer */ 230 // uniform samplerBuffer hairUVBuffer; /* RG32F */ 231 // uniform samplerBuffer hairColBuffer; /* RGBA16 linear color */ 232 233 /* -- Subdivision stage -- */ 234 /** 235 * We use a transform feedback to preprocess the strands and add more subdivision to it. 236 * For the moment these are simple smooth interpolation but one could hope to see the full 237 * children particle modifiers being evaluated at this stage. 238 * 239 * If no more subdivision is needed, we can skip this step. 240 */ 241 242 #ifdef HAIR_PHASE_SUBDIV 243 int hair_get_base_id(float local_time, int strand_segments, out float interp_time) 244 { 245 float time_per_strand_seg = 1.0 / float(strand_segments); 246 247 float ratio = local_time / time_per_strand_seg; 248 interp_time = fract(ratio); 249 250 return int(ratio); 251 } 252 253 void hair_get_interp_attrs( 254 out vec4 data0, out vec4 data1, out vec4 data2, out vec4 data3, out float interp_time) 255 { 256 float local_time = float(gl_VertexID % hairStrandsRes) / float(hairStrandsRes - 1); 257 258 int hair_id = gl_VertexID / hairStrandsRes; 259 int strand_offset = int(texelFetch(hairStrandBuffer, hair_id).x); 260 int strand_segments = int(texelFetch(hairStrandSegBuffer, hair_id).x); 261 262 int id = hair_get_base_id(local_time, strand_segments, interp_time); 263 264 int ofs_id = id + strand_offset; 265 266 data0 = texelFetch(hairPointBuffer, ofs_id - 1); 267 data1 = texelFetch(hairPointBuffer, ofs_id); 268 data2 = texelFetch(hairPointBuffer, ofs_id + 1); 269 data3 = texelFetch(hairPointBuffer, ofs_id + 2); 270 271 if (id <= 0) { 272 /* root points. Need to reconstruct previous data. */ 273 data0 = data1 * 2.0 - data2; 274 } 275 if (id + 1 >= strand_segments) { 276 /* tip points. Need to reconstruct next data. */ 277 data3 = data2 * 2.0 - data1; 278 } 279 } 280 #endif 281 282 /* -- Drawing stage -- */ 283 /** 284 * For final drawing, the vertex index and the number of vertex per segment 285 */ 286 287 #ifndef HAIR_PHASE_SUBDIV 288 int hair_get_strand_id(void) 289 { 290 return gl_VertexID / (hairStrandsRes * hairThicknessRes); 291 } 292 293 int hair_get_base_id(void) 294 { 295 return gl_VertexID / hairThicknessRes; 296 } 297 298 /* Copied from cycles. */ 299 float hair_shaperadius(float shape, float root, float tip, float time) 300 { 301 float radius = 1.0 - time; 302 303 if (shape < 0.0) { 304 radius = pow(radius, 1.0 + shape); 305 } 306 else { 307 radius = pow(radius, 1.0 / (1.0 - shape)); 308 } 309 310 if (hairCloseTip && (time > 0.99)) { 311 return 0.0; 312 } 313 314 return (radius * (root - tip)) + tip; 315 } 316 317 # ifdef OS_MAC 318 in float dummy; 319 # endif 320 321 void hair_get_pos_tan_binor_time(bool is_persp, 322 mat4 invmodel_mat, 323 vec3 camera_pos, 324 vec3 camera_z, 325 out vec3 wpos, 326 out vec3 wtan, 327 out vec3 wbinor, 328 out float time, 329 out float thickness, 330 out float thick_time) 331 { 332 int id = hair_get_base_id(); 333 vec4 data = texelFetch(hairPointBuffer, id); 334 wpos = data.point_position; 335 time = data.point_time; 336 337 # ifdef OS_MAC 338 /* Generate a dummy read to avoid the driver bug with shaders having no 339 * vertex reads on macOS (T60171) */ 340 wpos.y += dummy * 0.0; 341 # endif 342 343 if (time == 0.0) { 344 /* Hair root */ 345 wtan = texelFetch(hairPointBuffer, id + 1).point_position - wpos; 346 } 347 else { 348 wtan = wpos - texelFetch(hairPointBuffer, id - 1).point_position; 349 } 350 351 mat4 obmat = mat4( 352 hairDupliMatrix[0], hairDupliMatrix[1], hairDupliMatrix[2], hairDupliMatrix[3]); 353 354 wpos = (obmat * vec4(wpos, 1.0)).xyz; 355 wtan = -normalize(mat3(obmat) * wtan); 356 357 vec3 camera_vec = (is_persp) ? camera_pos - wpos : camera_z; 358 wbinor = normalize(cross(camera_vec, wtan)); 359 360 thickness = hair_shaperadius(hairRadShape, hairRadRoot, hairRadTip, time); 361 362 if (hairThicknessRes > 1) { 363 thick_time = float(gl_VertexID % hairThicknessRes) / float(hairThicknessRes - 1); 364 thick_time = thickness * (thick_time * 2.0 - 1.0); 365 366 /* Take object scale into account. 367 * NOTE: This only works fine with uniform scaling. */ 368 float scale = 1.0 / length(mat3(invmodel_mat) * wbinor); 369 370 wpos += wbinor * thick_time * scale; 371 } 372 } 373 374 vec2 hair_get_customdata_vec2(const samplerBuffer cd_buf) 375 { 376 int id = hair_get_strand_id(); 377 return texelFetch(cd_buf, id).rg; 378 } 379 380 vec3 hair_get_customdata_vec3(const samplerBuffer cd_buf) 381 { 382 int id = hair_get_strand_id(); 383 return texelFetch(cd_buf, id).rgb; 384 } 385 386 vec4 hair_get_customdata_vec4(const samplerBuffer cd_buf) 387 { 388 int id = hair_get_strand_id(); 389 return texelFetch(cd_buf, id).rgba; 390 } 391 392 vec3 hair_get_strand_pos(void) 393 { 394 int id = hair_get_strand_id() * hairStrandsRes; 395 return texelFetch(hairPointBuffer, id).point_position; 396 } 397 398 #endif 399 400 #ifndef HAIR_SHADER 401 in vec3 pos; 402 in vec3 nor; 403 #endif 404 405 out vec3 worldPosition; 406 out vec3 viewPosition; 407 408 out vec3 worldNormal; 409 out vec3 viewNormal; 410 411 #ifdef HAIR_SHADER 412 out vec3 hairTangent; 413 out float hairThickTime; 414 out float hairThickness; 415 out float hairTime; 416 flat out int hairStrandID; 417 #endif 418 419 void main() 420 { 421 #ifdef GPU_INTEL 422 /* Due to some shader compiler bug, we somewhat 423 * need to access gl_VertexID to make it work. even 424 * if it's actually dead code. */ 425 gl_Position.x = float(gl_VertexID); 426 #endif 427 428 #ifdef HAIR_SHADER 429 hairStrandID = hair_get_strand_id(); 430 vec3 pos, binor; 431 hair_get_pos_tan_binor_time((ProjectionMatrix[3][3] == 0.0), 432 ModelMatrixInverse, 433 ViewMatrixInverse[3].xyz, 434 ViewMatrixInverse[2].xyz, 435 pos, 436 hairTangent, 437 binor, 438 hairTime, 439 hairThickness, 440 hairThickTime); 441 worldNormal = cross(hairTangent, binor); 442 worldPosition = pos; 443 #else 444 worldPosition = point_object_to_world(pos); 445 worldNormal = normalize(normal_object_to_world(nor)); 446 #endif 447 448 /* No need to normalize since this is just a rotation. */ 449 viewNormal = normal_world_to_view(worldNormal); 450 451 viewPosition = point_world_to_view(worldPosition); 452 gl_Position = point_world_to_ndc(worldPosition); 453 454 /* Used for planar reflections */ 455 gl_ClipDistance[0] = dot(vec4(worldPosition, 1.0), clipPlanes[0]); 456 457 #ifdef USE_ATTR 458 pass_attr(pos); 459 #endif 460 } Fragment info ------------- 0(1623) : warning C7050: "bent_normal" might be used before being initialized 0(3299) : warning C7050: "ssr_spec" might be used before being initialized Internal error: assembly compile error for fragment shader at offset 284378: -- error message -- line 4000, column 15: error: invalid integer constant -- internal assembly text -- !!NVfp5.0 OPTION ARB_draw_buffers; OPTION ARB_fragment_program_shadow; # cgc version 3.0.0005, build date Feb 18 2011 # command line args: #vendor NVIDIA Corporation #version 3.0.0.05 #profile gp5fp #program main #semantic viewBlock : BUFFER[0] #semantic modelBlock : BUFFER[1] #semantic common_block : BUFFER[2] #semantic probe_block : BUFFER[3] #semantic grid_block : BUFFER[4] #semantic planar_block : BUFFER[5] #semantic shadow_block : BUFFER[6] #semantic light_block : BUFFER[7] #semantic resourceChunk #semantic colorBuffer #semantic depthBuffer #semantic maxzBuffer #semantic minzBuffer #semantic planarDepth #semantic outputSsrId #semantic outputSssId #semantic texHammersley #semantic texJitter #semantic sampleCount #semantic invSampleCount #semantic utilTex #semantic horizonBuffer #semantic irradianceGrid #semantic probePlanars #semantic probeCubes #semantic shadowCubeTexture #semantic shadowCascadeTexture #semantic refractionDepth #semantic inScattering #semantic inTransmittance #semantic basecol #semantic metallic #semantic specular #semantic roughness #var float gl_FrontFacing : $vin.FACE : SSA : -1 : 1 #var float4 gl_FragCoord : $vin.WPOS : WPOS : -1 : 1 #var float4x4 ViewProjectionMatrix : : buffer[0][0], 4 : -1 : 1 #var float4x4 ViewProjectionMatrixInverse : : buffer[0][64], 4 : -1 : 0 #var float4x4 ViewMatrix : : buffer[0][128], 4 : -1 : 1 #var float4x4 ViewMatrixInverse : : buffer[0][192], 4 : -1 : 1 #var float4x4 ProjectionMatrix : : buffer[0][256], 4 : -1 : 1 #var float4x4 ProjectionMatrixInverse : : buffer[0][320], 4 : -1 : 0 #var float4 clipPlanes[0] : : buffer[0][384] : -1 : 0 #var float4 clipPlanes[1] : : buffer[0][400] : -1 : 0 #var float4 clipPlanes[2] : : buffer[0][416] : -1 : 0 #var float4 clipPlanes[3] : : buffer[0][432] : -1 : 0 #var float4 clipPlanes[4] : : buffer[0][448] : -1 : 0 #var float4 clipPlanes[5] : : buffer[0][464] : -1 : 0 #var float4 CameraTexCoFactors : : buffer[0][480] : -1 : 0 #var float4x4 drw_matrices[0].drw_modelMatrix : : buffer[1][0], 4 : -1 : 0 #var float4x4 drw_matrices[0].drw_modelMatrixInverse : : buffer[1][64], 4 : -1 : 0 #var float4x4 drw_matrices[1].drw_modelMatrix : : buffer[1][128], 4 : -1 : 0 #var float4x4 drw_matrices[1].drw_modelMatrixInverse : : buffer[1][192], 4 : -1 : 0 #var float4x4 drw_matrices[2].drw_modelMatrix : : buffer[1][256], 4 : -1 : 0 #var float4x4 drw_matrices[2].drw_modelMatrixInverse : : buffer[1][320], 4 : -1 : 0 #var float4x4 drw_matrices[3].drw_modelMatrix : : buffer[1][384], 4 : -1 : 0 #var float4x4 drw_matrices[3].drw_modelMatrixInverse : : buffer[1][448], 4 : -1 : 0 #var float4x4 drw_matrices[4].drw_modelMatrix : : buffer[1][512], 4 : -1 : 0 #var float4x4 drw_matrices[4].drw_modelMatrixInverse : : buffer[1][576], 4 : -1 : 0 #var float4x4 drw_matrices[5].drw_modelMatrix : : buffer[1][640], 4 : -1 : 0 #var float4x4 drw_matrices[5].drw_modelMatrixInverse : : buffer[1][704], 4 : -1 : 0 #var float4x4 drw_matrices[6].drw_modelMatrix : : buffer[1][768], 4 : -1 : 0 #var float4x4 drw_matrices[6].drw_modelMatrixInverse : : buffer[1][832], 4 : -1 : 0 #var float4x4 drw_matrices[7].drw_modelMatrix : : buffer[1][896], 4 : -1 : 0 #var float4x4 drw_matrices[7].drw_modelMatrixInverse : : buffer[1][960], 4 : -1 : 0 #var float4x4 drw_matrices[8].drw_modelMatrix : : buffer[1][1024], 4 : -1 : 0 #var float4x4 drw_matrices[8].drw_modelMatrixInverse : : buffer[1][1088], 4 : -1 : 0 #var float4x4 drw_matrices[9].drw_modelMatrix : : buffer[1][1152], 4 : -1 : 0 #var float4x4 drw_matrices[9].drw_modelMatrixInverse : : buffer[1][1216], 4 : -1 : 0 #var float4x4 drw_matrices[10].drw_modelMatrix : : buffer[1][1280], 4 : -1 : 0 #var float4x4 drw_matrices[10].drw_modelMatrixInverse : : buffer[1][1344], 4 : -1 : 0 #var float4x4 drw_matrices[11].drw_modelMatrix : : buffer[1][1408], 4 : -1 : 0 #var float4x4 drw_matrices[11].drw_modelMatrixInverse : : buffer[1][1472], 4 : -1 : 0 #var float4x4 drw_matrices[12].drw_modelMatrix : : buffer[1][1536], 4 : -1 : 0 #var float4x4 drw_matrices[12].drw_modelMatrixInverse : : buffer[1][1600], 4 : -1 : 0 #var float4x4 drw_matrices[13].drw_modelMatrix : : buffer[1][1664], 4 : -1 : 0 #var float4x4 drw_matrices[13].drw_modelMatrixInverse : : buffer[1][1728], 4 : -1 : 0 #var float4x4 drw_matrices[14].drw_modelMatrix : : buffer[1][1792], 4 : -1 : 0 #var float4x4 drw_matrices[14].drw_modelMatrixInverse : : buffer[1][1856], 4 : -1 : 0 #var float4x4 drw_matrices[15].drw_modelMatrix : : buffer[1][1920], 4 : -1 : 0 #var float4x4 drw_matrices[15].drw_modelMatrixInverse : : buffer[1][1984], 4 : -1 : 0 #var float4x4 drw_matrices[16].drw_modelMat GPUShader: linking error: ===== shader string 1 ==== 1 #define COMMON_VIEW_LIB 2 #define DRW_RESOURCE_CHUNK_LEN 512 3 4 /* keep in sync with DRWManager.view_data */ 5 layout(std140) uniform viewBlock 6 { 7 /* Same order as DRWViewportMatrixType */ 8 mat4 ViewProjectionMatrix; 9 mat4 ViewProjectionMatrixInverse; 10 mat4 ViewMatrix; 11 mat4 ViewMatrixInverse; 12 mat4 ProjectionMatrix; 13 mat4 ProjectionMatrixInverse; 14 15 vec4 clipPlanes[6]; 16 17 /* TODO move it elsewhere. */ 18 vec4 CameraTexCoFactors; 19 }; 20 21 #ifdef world_clip_planes_calc_clip_distance 22 # undef world_clip_planes_calc_clip_distance 23 # define world_clip_planes_calc_clip_distance(p) \ 24 _world_clip_planes_calc_clip_distance(p, clipPlanes) 25 #endif 26 27 #ifdef COMMON_GLOBALS_LIB 28 float mul_project_m4_v3_zfac(in vec3 co) 29 { 30 return pixelFac * ((ViewProjectionMatrix[0][3] * co.x) + (ViewProjectionMatrix[1][3] * co.y) + 31 (ViewProjectionMatrix[2][3] * co.z) + ViewProjectionMatrix[3][3]); 32 } 33 #endif 34 35 /* Not the right place but need to be common to all overlay's. 36 * TODO Split to an overlay lib. */ 37 mat4 extract_matrix_packed_data(mat4 mat, out vec4 dataA, out vec4 dataB) 38 { 39 const float div = 1.0 / 255.0; 40 int a = int(mat[0][3]); 41 int b = int(mat[1][3]); 42 int c = int(mat[2][3]); 43 int d = int(mat[3][3]); 44 dataA = vec4(a & 0xFF, a >> 8, b & 0xFF, b >> 8) * div; 45 dataB = vec4(c & 0xFF, c >> 8, d & 0xFF, d >> 8) * div; 46 mat[0][3] = mat[1][3] = mat[2][3] = 0.0; 47 mat[3][3] = 1.0; 48 return mat; 49 } 50 51 /* Same here, Not the right place but need to be common to all overlay's. 52 * TODO Split to an overlay lib. */ 53 /* edge_start and edge_pos needs to be in the range [0..sizeViewport]. */ 54 vec4 pack_line_data(vec2 frag_co, vec2 edge_start, vec2 edge_pos) 55 { 56 vec2 edge = edge_start - edge_pos; 57 float len = length(edge); 58 if (len > 0.0) { 59 edge /= len; 60 vec2 perp = vec2(-edge.y, edge.x); 61 float dist = dot(perp, frag_co - edge_start); 62 /* Add 0.1 to diffenrentiate with cleared pixels. */ 63 return vec4(perp * 0.5 + 0.5, dist * 0.25 + 0.5 + 0.1, 0.0); 64 } 65 else { 66 /* Default line if the origin is perfectly aligned with a pixel. */ 67 return vec4(1.0, 0.0, 0.5 + 0.1, 0.0); 68 } 69 } 70 71 uniform int resourceChunk; 72 73 #ifdef GPU_VERTEX_SHADER 74 # ifdef GL_ARB_shader_draw_parameters 75 # define baseInstance gl_BaseInstanceARB 76 # else /* no ARB_shader_draw_parameters */ 77 uniform int baseInstance; 78 # endif 79 80 # if defined(IN_PLACE_INSTANCES) || defined(INSTANCED_ATTRIB) 81 /* When drawing instances of an object at the same position. */ 82 # define instanceId 0 83 # elif defined(GPU_DEPRECATED_AMD_DRIVER) 84 /* A driver bug make it so that when using an attribute with GL_INT_2_10_10_10_REV as format, 85 * the gl_InstanceID is incremented by the 2 bit component of the attrib. 86 * Ignore gl_InstanceID then. */ 87 # define instanceId 0 88 # else 89 # define instanceId gl_InstanceID 90 # endif 91 92 # define resource_id (baseInstance + instanceId) 93 94 /* Use this to declare and pass the value if 95 * the fragment shader uses the resource_id. */ 96 # define RESOURCE_ID_VARYING flat out int resourceIDFrag; 97 # define RESOURCE_ID_VARYING_GEOM flat out int resourceIDGeom; 98 # define PASS_RESOURCE_ID resourceIDFrag = resource_id; 99 # define PASS_RESOURCE_ID_GEOM resourceIDGeom = resource_id; 100 #endif 101 102 /* If used in a fragment / geometry shader, we pass 103 * resource_id as varying. */ 104 #ifdef GPU_GEOMETRY_SHADER 105 # define RESOURCE_ID_VARYING \ 106 flat out int resourceIDFrag; \ 107 flat in int resourceIDGeom[]; 108 109 # define resource_id resourceIDGeom 110 # define PASS_RESOURCE_ID(i) resourceIDFrag = resource_id[i]; 111 #endif 112 113 #ifdef GPU_FRAGMENT_SHADER 114 flat in int resourceIDFrag; 115 # define resource_id resourceIDFrag 116 #endif 117 118 /* Breaking this across multiple lines causes issues for some older GLSL compilers. */ 119 /* clang-format off */ 120 #if !defined(GPU_INTEL) && !defined(GPU_DEPRECATED_AMD_DRIVER) && !defined(OS_MAC) && !defined(INSTANCED_ATTRIB) 121 /* clang-format on */ 122 struct ObjectMatrices { 123 mat4 drw_modelMatrix; 124 mat4 drw_modelMatrixInverse; 125 }; 126 127 layout(std140) uniform modelBlock 128 { 129 ObjectMatrices drw_matrices[DRW_RESOURCE_CHUNK_LEN]; 130 }; 131 132 # define ModelMatrix (drw_matrices[resource_id].drw_modelMatrix) 133 # define ModelMatrixInverse (drw_matrices[resource_id].drw_modelMatrixInverse) 134 135 #else /* GPU_INTEL */ 136 /* Intel GPU seems to suffer performance impact when the model matrix is in UBO storage. 137 * So for now we just force using the legacy path. */ 138 /* Note that this is also a workaround of a problem on osx (amd or nvidia) 139 * and older amd driver on windows. */ 140 uniform mat4 ModelMatrix; 141 uniform mat4 ModelMatrixInverse; 142 #endif 143 144 #define resource_handle (resourceChunk * DRW_RESOURCE_CHUNK_LEN + resource_id) 145 146 /** Transform shortcuts. */ 147 /* Rule of thumb: Try to reuse world positions and normals because converting though viewspace 148 * will always be decomposed in at least 2 matrix operation. */ 149 150 /** 151 * Some clarification: 152 * Usually Normal matrix is transpose(inverse(ViewMatrix * ModelMatrix)) 153 * 154 * But since it is slow to multiply matrices we decompose it. Decomposing 155 * inversion and transposition both invert the product order leaving us with 156 * the same original order: 157 * transpose(ViewMatrixInverse) * transpose(ModelMatrixInverse) 158 * 159 * Knowing that the view matrix is orthogonal, the transpose is also the inverse. 160 * Note: This is only valid because we are only using the mat3 of the ViewMatrixInverse. 161 * ViewMatrix * transpose(ModelMatrixInverse) 162 **/ 163 #define normal_object_to_view(n) (mat3(ViewMatrix) * (transpose(mat3(ModelMatrixInverse)) * n)) 164 #define normal_object_to_world(n) (transpose(mat3(ModelMatrixInverse)) * n) 165 #define normal_world_to_object(n) (transpose(mat3(ModelMatrix)) * n) 166 #define normal_world_to_view(n) (mat3(ViewMatrix) * n) 167 168 #define point_object_to_ndc(p) (ViewProjectionMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)) 169 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)).xyz) 170 #define point_object_to_world(p) ((ModelMatrix * vec4(p, 1.0)).xyz) 171 #define point_view_to_ndc(p) (ProjectionMatrix * vec4(p, 1.0)) 172 #define point_view_to_object(p) ((ModelMatrixInverse * (ViewMatrixInverse * vec4(p, 1.0))).xyz) 173 #define point_view_to_world(p) ((ViewMatrixInverse * vec4(p, 1.0)).xyz) 174 #define point_world_to_ndc(p) (ViewProjectionMatrix * vec4(p, 1.0)) 175 #define point_world_to_object(p) ((ModelMatrixInverse * vec4(p, 1.0)).xyz) 176 #define point_world_to_view(p) ((ViewMatrix * vec4(p, 1.0)).xyz) 177 178 /* Due to some shader compiler bug, we somewhat need to access gl_VertexID 179 * to make vertex shaders work. even if it's actually dead code. */ 180 #ifdef GPU_INTEL 181 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND gl_Position.x = float(gl_VertexID); 182 #else 183 # define GPU_INTEL_VERTEX_SHADER_WORKAROUND 184 #endif 185 186 #define DRW_BASE_SELECTED (1 << 1) 187 #define DRW_BASE_FROM_DUPLI (1 << 2) 188 #define DRW_BASE_FROM_SET (1 << 3) 189 #define DRW_BASE_ACTIVE (1 << 4) 190 191 layout(std140) uniform common_block 192 { 193 mat4 pastViewProjectionMatrix; 194 vec4 viewVecs[2]; 195 vec2 mipRatio[10]; /* To correct mip level texel mis-alignement */ 196 /* Ambient Occlusion */ 197 vec4 aoParameters[2]; 198 /* Volumetric */ 199 ivec4 volTexSize; 200 vec4 volDepthParameters; /* Parameters to the volume Z equation */ 201 vec4 volInvTexSize; 202 vec4 volJitter; 203 vec4 volCoordScale; /* To convert volume uvs to screen uvs */ 204 float volHistoryAlpha; 205 float volLightClamp; 206 float volShadowSteps; 207 bool volUseLights; 208 /* Screen Space Reflections */ 209 vec4 ssrParameters; 210 float ssrBorderFac; 211 float ssrMaxRoughness; 212 float ssrFireflyFac; 213 float ssrBrdfBias; 214 bool ssrToggle; 215 bool ssrefractToggle; 216 /* SubSurface Scattering */ 217 float sssJitterThreshold; 218 bool sssToggle; 219 /* Specular */ 220 bool specToggle; 221 /* Lights */ 222 int laNumLight; 223 /* Probes */ 224 int prbNumPlanar; 225 int prbNumRenderCube; 226 int prbNumRenderGrid; 227 int prbIrradianceVisSize; 228 float prbIrradianceSmooth; 229 float prbLodCubeMax; 230 float prbLodPlanarMax; 231 /* Misc*/ 232 int hizMipOffset; 233 int rayType; 234 float rayDepth; 235 float alphaHashOffset; 236 float alphaHashScale; 237 float pad7; 238 float pad8; 239 }; 240 241 /* rayType (keep in sync with ray_type) */ 242 #define EEVEE_RAY_CAMERA 0 243 #define EEVEE_RAY_SHADOW 1 244 #define EEVEE_RAY_DIFFUSE 2 245 #define EEVEE_RAY_GLOSSY 3 246 247 /* aoParameters */ 248 #define aoDistance aoParameters[0].x 249 #define aoSamples aoParameters[0].y /* UNUSED */ 250 #define aoFactor aoParameters[0].z 251 #define aoInvSamples aoParameters[0].w /* UNUSED */ 252 253 #define aoOffset aoParameters[1].x /* UNUSED */ 254 #define aoBounceFac aoParameters[1].y 255 #define aoQuality aoParameters[1].z 256 #define aoSettings aoParameters[1].w 257 258 /* ssrParameters */ 259 #define ssrQuality ssrParameters.x 260 #define ssrThickness ssrParameters.y 261 #define ssrPixelSize ssrParameters.zw 262 263 #define M_PI 3.14159265358979323846 /* pi */ 264 #define M_2PI 6.28318530717958647692 /* 2*pi */ 265 #define M_PI_2 1.57079632679489661923 /* pi/2 */ 266 #define M_1_PI 0.318309886183790671538 /* 1/pi */ 267 #define M_1_2PI 0.159154943091895335768 /* 1/(2*pi) */ 268 #define M_1_PI2 0.101321183642337771443 /* 1/(pi^2) */ 269 #define FLT_MAX 3.402823e+38 270 271 #define LUT_SIZE 64 272 273 /* Buffers */ 274 uniform sampler2D colorBuffer; 275 uniform sampler2D depthBuffer; 276 uniform sampler2D maxzBuffer; 277 uniform sampler2D minzBuffer; 278 uniform sampler2DArray planarDepth; 279 280 #define cameraForward ViewMatrixInverse[2].xyz 281 #define cameraPos ViewMatrixInverse[3].xyz 282 #define cameraVec \ 283 ((ProjectionMatrix[3][3] == 0.0) ? normalize(cameraPos - worldPosition) : cameraForward) 284 #define viewCameraVec \ 285 ((ProjectionMatrix[3][3] == 0.0) ? normalize(-viewPosition) : vec3(0.0, 0.0, 1.0)) 286 287 /* ------- Structures -------- */ 288 289 /* ------ Lights ----- */ 290 struct LightData { 291 vec4 position_influence; /* w : InfluenceRadius (inversed and squared) */ 292 vec4 color_spec; /* w : Spec Intensity */ 293 vec4 spotdata_radius_shadow; /* x : spot size, y : spot blend, z : radius, w: shadow id */ 294 vec4 rightvec_sizex; /* xyz: Normalized up vector, w: area size X or spot scale X */ 295 vec4 upvec_sizey; /* xyz: Normalized right vector, w: area size Y or spot scale Y */ 296 vec4 forwardvec_type; /* xyz: Normalized forward vector, w: Light Type */ 297 }; 298 299 /* convenience aliases */ 300 #define l_color color_spec.rgb 301 #define l_spec color_spec.a 302 #define l_position position_influence.xyz 303 #define l_influence position_influence.w 304 #define l_sizex rightvec_sizex.w 305 #define l_sizey upvec_sizey.w 306 #define l_right rightvec_sizex.xyz 307 #define l_up upvec_sizey.xyz 308 #define l_forward forwardvec_type.xyz 309 #define l_type forwardvec_type.w 310 #define l_spot_size spotdata_radius_shadow.x 311 #define l_spot_blend spotdata_radius_shadow.y 312 #define l_radius spotdata_radius_shadow.z 313 #define l_shadowid spotdata_radius_shadow.w 314 315 /* ------ Shadows ----- */ 316 #ifndef MAX_CASCADE_NUM 317 # define MAX_CASCADE_NUM 4 318 #endif 319 320 struct ShadowData { 321 vec4 near_far_bias_id; 322 vec4 contact_shadow_data; 323 }; 324 325 struct ShadowCubeData { 326 mat4 shadowmat; 327 vec4 position; 328 }; 329 330 struct ShadowCascadeData { 331 mat4 shadowmat[MAX_CASCADE_NUM]; 332 vec4 split_start_distances; 333 vec4 split_end_distances; 334 vec4 shadow_vec_id; 335 }; 336 337 /* convenience aliases */ 338 #define sh_near near_far_bias_id.x 339 #define sh_far near_far_bias_id.y 340 #define sh_bias near_far_bias_id.z 341 #define sh_data_index near_far_bias_id.w 342 #define sh_contact_dist contact_shadow_data.x 343 #define sh_contact_offset contact_shadow_data.y 344 #define sh_contact_spread contact_shadow_data.z 345 #define sh_contact_thickness contact_shadow_data.w 346 #define sh_shadow_vec shadow_vec_id.xyz 347 #define sh_tex_index shadow_vec_id.w 348 349 /* ------- Convenience functions --------- */ 350 351 vec3 mul(mat3 m, vec3 v) 352 { 353 return m * v; 354 } 355 mat3 mul(mat3 m1, mat3 m2) 356 { 357 return m1 * m2; 358 } 359 vec3 transform_direction(mat4 m, vec3 v) 360 { 361 return mat3(m) * v; 362 } 363 vec3 transform_point(mat4 m, vec3 v) 364 { 365 return (m * vec4(v, 1.0)).xyz; 366 } 367 vec3 project_point(mat4 m, vec3 v) 368 { 369 vec4 tmp = m * vec4(v, 1.0); 370 return tmp.xyz / tmp.w; 371 } 372 373 #define min3(a, b, c) min(a, min(b, c)) 374 #define min4(a, b, c, d) min(a, min3(b, c, d)) 375 #define min5(a, b, c, d, e) min(a, min4(b, c, d, e)) 376 #define min6(a, b, c, d, e, f) min(a, min5(b, c, d, e, f)) 377 #define min7(a, b, c, d, e, f, g) min(a, min6(b, c, d, e, f, g)) 378 #define min8(a, b, c, d, e, f, g, h) min(a, min7(b, c, d, e, f, g, h)) 379 #define min9(a, b, c, d, e, f, g, h, i) min(a, min8(b, c, d, e, f, g, h, i)) 380 381 #define max3(a, b, c) max(a, max(b, c)) 382 #define max4(a, b, c, d) max(a, max3(b, c, d)) 383 #define max5(a, b, c, d, e) max(a, max4(b, c, d, e)) 384 #define max6(a, b, c, d, e, f) max(a, max5(b, c, d, e, f)) 385 #define max7(a, b, c, d, e, f, g) max(a, max6(b, c, d, e, f, g)) 386 #define max8(a, b, c, d, e, f, g, h) max(a, max7(b, c, d, e, f, g, h)) 387 #define max9(a, b, c, d, e, f, g, h, i) max(a, max8(b, c, d, e, f, g, h, i)) 388 389 #define avg3(a, b, c) (a + b + c) * (1.0 / 3.0) 390 #define avg4(a, b, c, d) (a + b + c + d) * (1.0 / 4.0) 391 #define avg5(a, b, c, d, e) (a + b + c + d + e) * (1.0 / 5.0) 392 #define avg6(a, b, c, d, e, f) (a + b + c + d + e + f) * (1.0 / 6.0) 393 #define avg7(a, b, c, d, e, f, g) (a + b + c + d + e + f + g) * (1.0 / 7.0) 394 #define avg8(a, b, c, d, e, f, g, h) (a + b + c + d + e + f + g + h) * (1.0 / 8.0) 395 #define avg9(a, b, c, d, e, f, g, h, i) (a + b + c + d + e + f + g + h + i) * (1.0 / 9.0) 396 397 float min_v2(vec2 v) 398 { 399 return min(v.x, v.y); 400 } 401 float min_v3(vec3 v) 402 { 403 return min(v.x, min(v.y, v.z)); 404 } 405 float min_v4(vec4 v) 406 { 407 return min(min(v.x, v.y), min(v.z, v.w)); 408 } 409 float max_v2(vec2 v) 410 { 411 return max(v.x, v.y); 412 } 413 float max_v3(vec3 v) 414 { 415 return max(v.x, max(v.y, v.z)); 416 } 417 float max_v4(vec4 v) 418 { 419 return max(max(v.x, v.y), max(v.z, v.w)); 420 } 421 422 float sum(vec2 v) 423 { 424 return dot(vec2(1.0), v); 425 } 426 float sum(vec3 v) 427 { 428 return dot(vec3(1.0), v); 429 } 430 float sum(vec4 v) 431 { 432 return dot(vec4(1.0), v); 433 } 434 435 float avg(vec2 v) 436 { 437 return dot(vec2(1.0 / 2.0), v); 438 } 439 float avg(vec3 v) 440 { 441 return dot(vec3(1.0 / 3.0), v); 442 } 443 float avg(vec4 v) 444 { 445 return dot(vec4(1.0 / 4.0), v); 446 } 447 448 float saturate(float a) 449 { 450 return clamp(a, 0.0, 1.0); 451 } 452 vec2 saturate(vec2 a) 453 { 454 return clamp(a, 0.0, 1.0); 455 } 456 vec3 saturate(vec3 a) 457 { 458 return clamp(a, 0.0, 1.0); 459 } 460 vec4 saturate(vec4 a) 461 { 462 return clamp(a, 0.0, 1.0); 463 } 464 465 float distance_squared(vec2 a, vec2 b) 466 { 467 a -= b; 468 return dot(a, a); 469 } 470 float distance_squared(vec3 a, vec3 b) 471 { 472 a -= b; 473 return dot(a, a); 474 } 475 float len_squared(vec3 a) 476 { 477 return dot(a, a); 478 } 479 480 float inverse_distance(vec3 V) 481 { 482 return max(1 / length(V), 1e-8); 483 } 484 485 vec2 mip_ratio_interp(float mip) 486 { 487 float low_mip = floor(mip); 488 return mix(mipRatio[int(low_mip)], mipRatio[int(low_mip + 1.0)], mip - low_mip); 489 } 490 491 /* ------- RNG ------- */ 492 493 float wang_hash_noise(uint s) 494 { 495 s = (s ^ 61u) ^ (s >> 16u); 496 s *= 9u; 497 s = s ^ (s >> 4u); 498 s *= 0x27d4eb2du; 499 s = s ^ (s >> 15u); 500 501 return fract(float(s) / 4294967296.0); 502 } 503 504 /* ------- Fast Math ------- */ 505 506 /* [Drobot2014a] Low Level Optimizations for GCN */ 507 float fast_sqrt(float v) 508 { 509 return intBitsToFloat(0x1fbd1df5 + (floatBitsToInt(v) >> 1)); 510 } 511 512 vec2 fast_sqrt(vec2 v) 513 { 514 return intBitsToFloat(0x1fbd1df5 + (floatBitsToInt(v) >> 1)); 515 } 516 517 /* [Eberly2014] GPGPU Programming for Games and Science */ 518 float fast_acos(float v) 519 { 520 float res = -0.156583 * abs(v) + M_PI_2; 521 res *= fast_sqrt(1.0 - abs(v)); 522 return (v >= 0) ? res : M_PI - res; 523 } 524 525 vec2 fast_acos(vec2 v) 526 { 527 vec2 res = -0.156583 * abs(v) + M_PI_2; 528 res *= fast_sqrt(1.0 - abs(v)); 529 v.x = (v.x >= 0) ? res.x : M_PI - res.x; 530 v.y = (v.y >= 0) ? res.y : M_PI - res.y; 531 return v; 532 } 533 534 float point_plane_projection_dist(vec3 lineorigin, vec3 planeorigin, vec3 planenormal) 535 { 536 return dot(planenormal, planeorigin - lineorigin); 537 } 538 539 float line_plane_intersect_dist(vec3 lineorigin, 540 vec3 linedirection, 541 vec3 planeorigin, 542 vec3 planenormal) 543 { 544 return dot(planenormal, planeorigin - lineorigin) / dot(planenormal, linedirection); 545 } 546 547 float line_plane_intersect_dist(vec3 lineorigin, vec3 linedirection, vec4 plane) 548 { 549 vec3 plane_co = plane.xyz * (-plane.w / len_squared(plane.xyz)); 550 vec3 h = lineorigin - plane_co; 551 return -dot(plane.xyz, h) / dot(plane.xyz, linedirection); 552 } 553 554 vec3 line_plane_intersect(vec3 lineorigin, vec3 linedirection, vec3 planeorigin, vec3 planenormal) 555 { 556 float dist = line_plane_intersect_dist(lineorigin, linedirection, planeorigin, planenormal); 557 return lineorigin + linedirection * dist; 558 } 559 560 vec3 line_plane_intersect(vec3 lineorigin, vec3 linedirection, vec4 plane) 561 { 562 float dist = line_plane_intersect_dist(lineorigin, linedirection, plane); 563 return lineorigin + linedirection * dist; 564 } 565 566 float line_aligned_plane_intersect_dist(vec3 lineorigin, vec3 linedirection, vec3 planeorigin) 567 { 568 /* aligned plane normal */ 569 vec3 L = planeorigin - lineorigin; 570 float diskdist = length(L); 571 vec3 planenormal = -normalize(L); 572 return -diskdist / dot(planenormal, linedirection); 573 } 574 575 vec3 line_aligned_plane_intersect(vec3 lineorigin, vec3 linedirection, vec3 planeorigin) 576 { 577 float dist = line_aligned_plane_intersect_dist(lineorigin, linedirection, planeorigin); 578 if (dist < 0) { 579 /* if intersection is behind we fake the intersection to be 580 * really far and (hopefully) not inside the radius of interest */ 581 dist = 1e16; 582 } 583 return lineorigin + linedirection * dist; 584 } 585 586 float line_unit_sphere_intersect_dist(vec3 lineorigin, vec3 linedirection) 587 { 588 float a = dot(linedirection, linedirection); 589 float b = dot(linedirection, lineorigin); 590 float c = dot(lineorigin, lineorigin) - 1; 591 592 float dist = 1e15; 593 float determinant = b * b - a * c; 594 if (determinant >= 0) { 595 dist = (sqrt(determinant) - b) / a; 596 } 597 598 return dist; 599 } 600 601 float line_unit_box_intersect_dist(vec3 lineorigin, vec3 linedirection) 602 { 603 /* https://seblagarde.wordpress.com/2012/09/29/image-based-lighting-approaches-and-parallax-corrected-cubemap/ 604 */ 605 vec3 firstplane = (vec3(1.0) - lineorigin) / linedirection; 606 vec3 secondplane = (vec3(-1.0) - lineorigin) / linedirection; 607 vec3 furthestplane = max(firstplane, secondplane); 608 609 return min_v3(furthestplane); 610 } 611 612 /* Return texture coordinates to sample Surface LUT */ 613 vec2 lut_coords(float cosTheta, float roughness) 614 { 615 float theta = acos(cosTheta); 616 vec2 coords = vec2(roughness, theta / M_PI_2); 617 618 /* scale and bias coordinates, for correct filtered lookup */ 619 return coords * (LUT_SIZE - 1.0) / LUT_SIZE + 0.5 / LUT_SIZE; 620 } 621 622 vec2 lut_coords_ltc(float cosTheta, float roughness) 623 { 624 vec2 coords = vec2(roughness, sqrt(1.0 - cosTheta)); 625 626 /* scale and bias coordinates, for correct filtered lookup */ 627 return coords * (LUT_SIZE - 1.0) / LUT_SIZE + 0.5 / LUT_SIZE; 628 } 629 630 /* -- Tangent Space conversion -- */ 631 vec3 tangent_to_world(vec3 vector, vec3 N, vec3 T, vec3 B) 632 { 633 return T * vector.x + B * vector.y + N * vector.z; 634 } 635 636 vec3 world_to_tangent(vec3 vector, vec3 N, vec3 T, vec3 B) 637 { 638 return vec3(dot(T, vector), dot(B, vector), dot(N, vector)); 639 } 640 641 void make_orthonormal_basis(vec3 N, out vec3 T, out vec3 B) 642 { 643 vec3 UpVector = abs(N.z) < 0.99999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0); 644 T = normalize(cross(UpVector, N)); 645 B = cross(N, T); 646 } 647 648 /* ---- Opengl Depth conversion ---- */ 649 650 float linear_depth(bool is_persp, float z, float zf, float zn) 651 { 652 if (is_persp) { 653 return (zn * zf) / (z * (zn - zf) + zf); 654 } 655 else { 656 return (z * 2.0 - 1.0) * zf; 657 } 658 } 659 660 float buffer_depth(bool is_persp, float z, float zf, float zn) 661 { 662 if (is_persp) { 663 return (zf * (zn - z)) / (z * (zn - zf)); 664 } 665 else { 666 return (z / (zf * 2.0)) + 0.5; 667 } 668 } 669 670 float get_view_z_from_depth(float depth) 671 { 672 if (ProjectionMatrix[3][3] == 0.0) { 673 float d = 2.0 * depth - 1.0; 674 return -ProjectionMatrix[3][2] / (d + ProjectionMatrix[2][2]); 675 } 676 else { 677 return viewVecs[0].z + depth * viewVecs[1].z; 678 } 679 } 680 681 float get_depth_from_view_z(float z) 682 { 683 if (ProjectionMatrix[3][3] == 0.0) { 684 float d = (-ProjectionMatrix[3][2] / z) - ProjectionMatrix[2][2]; 685 return d * 0.5 + 0.5; 686 } 687 else { 688 return (z - viewVecs[0].z) / viewVecs[1].z; 689 } 690 } 691 692 vec2 get_uvs_from_view(vec3 view) 693 { 694 vec3 ndc = project_point(ProjectionMatrix, view); 695 return ndc.xy * 0.5 + 0.5; 696 } 697 698 vec3 get_view_space_from_depth(vec2 uvcoords, float depth) 699 { 700 if (ProjectionMatrix[3][3] == 0.0) { 701 return vec3(viewVecs[0].xy + uvcoords * viewVecs[1].xy, 1.0) * get_view_z_from_depth(depth); 702 } 703 else { 704 return viewVecs[0].xyz + vec3(uvcoords, depth) * viewVecs[1].xyz; 705 } 706 } 707 708 vec3 get_world_space_from_depth(vec2 uvcoords, float depth) 709 { 710 return (ViewMatrixInverse * vec4(get_view_space_from_depth(uvcoords, depth), 1.0)).xyz; 711 } 712 713 vec3 get_specular_reflection_dominant_dir(vec3 N, vec3 V, float roughness) 714 { 715 vec3 R = -reflect(V, N); 716 float smoothness = 1.0 - roughness; 717 float fac = smoothness * (sqrt(smoothness) + roughness); 718 return normalize(mix(N, R, fac)); 719 } 720 721 float specular_occlusion(float NV, float AO, float roughness) 722 { 723 return saturate(pow(NV + AO, roughness) - 1.0 + AO); 724 } 725 726 /* --- Refraction utils --- */ 727 728 float ior_from_f0(float f0) 729 { 730 float f = sqrt(f0); 731 return (-f - 1.0) / (f - 1.0); 732 } 733 734 float f0_from_ior(float eta) 735 { 736 float A = (eta - 1.0) / (eta + 1.0); 737 return A * A; 738 } 739 740 vec3 get_specular_refraction_dominant_dir(vec3 N, vec3 V, float roughness, float ior) 741 { 742 /* TODO: This a bad approximation. Better approximation should fit 743 * the refracted vector and roughness into the best prefiltered reflection 744 * lobe. */ 745 /* Correct the IOR for ior < 1.0 to not see the abrupt delimitation or the TIR */ 746 ior = (ior < 1.0) ? mix(ior, 1.0, roughness) : ior; 747 float eta = 1.0 / ior; 748 749 float NV = dot(N, -V); 750 751 /* Custom Refraction. */ 752 float k = 1.0 - eta * eta * (1.0 - NV * NV); 753 k = max(0.0, k); /* Only this changes. */ 754 vec3 R = eta * -V - (eta * NV + sqrt(k)) * N; 755 756 return R; 757 } 758 759 float get_btdf_lut(sampler2DArray btdf_lut_tex, float NV, float roughness, float ior) 760 { 761 const vec3 lut_scale_bias_texel_size = vec3((LUT_SIZE - 1.0), 0.5, 1.5) / LUT_SIZE; 762 763 vec3 coords; 764 /* Try to compensate for the low resolution and interpolation error. */ 765 coords.x = (ior > 1.0) ? (0.9 + lut_scale_bias_texel_size.z) + 766 (0.1 - lut_scale_bias_texel_size.z) * f0_from_ior(ior) : 767 (0.9 + lut_scale_bias_texel_size.z) * ior * ior; 768 coords.y = 1.0 - saturate(NV); 769 coords.xy *= lut_scale_bias_texel_size.x; 770 coords.xy += lut_scale_bias_texel_size.y; 771 772 const float lut_lvl_ofs = 4.0; /* First texture lvl of roughness. */ 773 const float lut_lvl_scale = 16.0; /* How many lvl of roughness in the lut. */ 774 775 float mip = roughness * lut_lvl_scale; 776 float mip_floor = floor(mip); 777 778 coords.z = lut_lvl_ofs + mip_floor + 1.0; 779 float btdf_high = textureLod(btdf_lut_tex, coords, 0.0).r; 780 781 coords.z -= 1.0; 782 float btdf_low = textureLod(btdf_lut_tex, coords, 0.0).r; 783 784 float btdf = (ior == 1.0) ? 1.0 : mix(btdf_low, btdf_high, mip - coords.z); 785 786 return btdf; 787 } 788 789 /* ---- Encode / Decode Normal buffer data ---- */ 790 /* From http://aras-p.info/texts/CompactNormalStorage.html 791 * Using Method #4: Spheremap Transform */ 792 vec2 normal_encode(vec3 n, vec3 view) 793 { 794 float p = sqrt(n.z * 8.0 + 8.0); 795 return n.xy / p + 0.5; 796 } 797 798 vec3 normal_decode(vec2 enc, vec3 view) 799 { 800 vec2 fenc = enc * 4.0 - 2.0; 801 float f = dot(fenc, fenc); 802 float g = sqrt(1.0 - f / 4.0); 803 vec3 n; 804 n.xy = fenc * g; 805 n.z = 1 - f / 2; 806 return n; 807 } 808 809 /* ---- RGBM (shared multiplier) encoding ---- */ 810 /* From http://iwasbeingirony.blogspot.fr/2010/06/difference-between-rgbm-and-rgbd.html */ 811 812 /* Higher RGBM_MAX_RANGE gives imprecision issues in low intensity. */ 813 #define RGBM_MAX_RANGE 512.0 814 815 vec4 rgbm_encode(vec3 rgb) 816 { 817 float maxRGB = max_v3(rgb); 818 float M = maxRGB / RGBM_MAX_RANGE; 819 M = ceil(M * 255.0) / 255.0; 820 return vec4(rgb / (M * RGBM_MAX_RANGE), M); 821 } 822 823 vec3 rgbm_decode(vec4 data) 824 { 825 return data.rgb * (data.a * RGBM_MAX_RANGE); 826 } 827 828 /* ---- RGBE (shared exponent) encoding ---- */ 829 vec4 rgbe_encode(vec3 rgb) 830 { 831 float maxRGB = max_v3(rgb); 832 float fexp = ceil(log2(maxRGB)); 833 return vec4(rgb / exp2(fexp), (fexp + 128.0) / 255.0); 834 } 835 836 vec3 rgbe_decode(vec4 data) 837 { 838 float fexp = data.a * 255.0 - 128.0; 839 return data.rgb * exp2(fexp); 840 } 841 842 #if 1 843 # define irradiance_encode rgbe_encode 844 # define irradiance_decode rgbe_decode 845 #else /* No ecoding (when using floating point format) */ 846 # define irradiance_encode(X) (X).rgbb 847 # define irradiance_decode(X) (X).rgb 848 #endif 849 850 /* Irradiance Visibility Encoding */ 851 #if 1 852 vec4 visibility_encode(vec2 accum, float range) 853 { 854 accum /= range; 855 856 vec4 data; 857 data.x = fract(accum.x); 858 data.y = floor(accum.x) / 255.0; 859 data.z = fract(accum.y); 860 data.w = floor(accum.y) / 255.0; 861 862 return data; 863 } 864 865 vec2 visibility_decode(vec4 data, float range) 866 { 867 return (data.xz + data.yw * 255.0) * range; 868 } 869 #else /* No ecoding (when using floating point format) */ 870 vec4 visibility_encode(vec2 accum, float range) 871 { 872 return accum.xyxy; 873 } 874 875 vec2 visibility_decode(vec4 data, float range) 876 { 877 return data.xy; 878 } 879 #endif 880 881 /* Fresnel monochromatic, perfect mirror */ 882 float F_eta(float eta, float cos_theta) 883 { 884 /* compute fresnel reflectance without explicitly computing 885 * the refracted direction */ 886 float c = abs(cos_theta); 887 float g = eta * eta - 1.0 + c * c; 888 float result; 889 890 if (g > 0.0) { 891 g = sqrt(g); 892 vec2 g_c = vec2(g) + vec2(c, -c); 893 float A = g_c.y / g_c.x; 894 A *= A; 895 g_c *= c; 896 float B = (g_c.y - 1.0) / (g_c.x + 1.0); 897 B *= B; 898 result = 0.5 * A * (1.0 + B); 899 } 900 else { 901 result = 1.0; /* TIR (no refracted component) */ 902 } 903 904 return result; 905 } 906 907 /* Fresnel color blend base on fresnel factor */ 908 vec3 F_color_blend(float eta, float fresnel, vec3 f0_color) 909 { 910 float f0 = F_eta(eta, 1.0); 911 float fac = saturate((fresnel - f0) / max(1e-8, 1.0 - f0)); 912 return mix(f0_color, vec3(1.0), fac); 913 } 914 915 /* Fresnel */ 916 vec3 F_schlick(vec3 f0, float cos_theta) 917 { 918 float fac = 1.0 - cos_theta; 919 float fac2 = fac * fac; 920 fac = fac2 * fac2 * fac; 921 922 /* Unreal specular matching : if specular color is below 2% intensity, 923 * (using green channel for intensity) treat as shadowning */ 924 return saturate(50.0 * dot(f0, vec3(0.3, 0.6, 0.1))) * fac + (1.0 - fac) * f0; 925 } 926 927 /* Fresnel approximation for LTC area lights (not MRP) */ 928 vec3 F_area(vec3 f0, vec3 f90, vec2 lut) 929 { 930 /* Unreal specular matching : if specular color is below 2% intensity, 931 * treat as shadowning */ 932 return saturate(50.0 * dot(f0, vec3(0.3, 0.6, 0.1))) * lut.y * f90 + lut.x * f0; 933 } 934 935 /* Fresnel approximation for IBL */ 936 vec3 F_ibl(vec3 f0, vec3 f90, vec2 lut) 937 { 938 /* Unreal specular matching : if specular color is below 2% intensity, 939 * treat as shadowning */ 940 return saturate(50.0 * dot(f0, vec3(0.3, 0.6, 0.1))) * lut.y * f90 + lut.x * f0; 941 } 942 943 /* GGX */ 944 float D_ggx_opti(float NH, float a2) 945 { 946 float tmp = (NH * a2 - NH) * NH + 1.0; 947 return M_PI * tmp * tmp; /* Doing RCP and mul a2 at the end */ 948 } 949 950 float G1_Smith_GGX(float NX, float a2) 951 { 952 /* Using Brian Karis approach and refactoring by NX/NX 953 * this way the (2*NL)*(2*NV) in G = G1(V) * G1(L) gets canceled by the brdf denominator 4*NL*NV 954 * Rcp is done on the whole G later 955 * Note that this is not convenient for the transmission formula */ 956 return NX + sqrt(NX * (NX - NX * a2) + a2); 957 /* return 2 / (1 + sqrt(1 + a2 * (1 - NX*NX) / (NX*NX) ) ); /* Reference function */ 958 } 959 960 float bsdf_ggx(vec3 N, vec3 L, vec3 V, float roughness) 961 { 962 float a = roughness; 963 float a2 = a * a; 964 965 vec3 H = normalize(L + V); 966 float NH = max(dot(N, H), 1e-8); 967 float NL = max(dot(N, L), 1e-8); 968 float NV = max(dot(N, V), 1e-8); 969 970 float G = G1_Smith_GGX(NV, a2) * G1_Smith_GGX(NL, a2); /* Doing RCP at the end */ 971 float D = D_ggx_opti(NH, a2); 972 973 /* Denominator is canceled by G1_Smith */ 974 /* bsdf = D * G / (4.0 * NL * NV); /* Reference function */ 975 return NL * a2 / (D * G); /* NL to Fit cycles Equation : line. 345 in bsdf_microfacet.h */ 976 } 977 978 void accumulate_light(vec3 light, float fac, inout vec4 accum) 979 { 980 accum += vec4(light, 1.0) * min(fac, (1.0 - accum.a)); 981 } 982 983 /* ----------- Cone Aperture Approximation --------- */ 984 985 /* Return a fitted cone angle given the input roughness */ 986 float cone_cosine(float r) 987 { 988 /* Using phong gloss 989 * roughness = sqrt(2/(gloss+2)) */ 990 float gloss = -2 + 2 / (r * r); 991 /* Drobot 2014 in GPUPro5 */ 992 // return cos(2.0 * sqrt(2.0 / (gloss + 2))); 993 /* Uludag 2014 in GPUPro5 */ 994 // return pow(0.244, 1 / (gloss + 1)); 995 /* Jimenez 2016 in Practical Realtime Strategies for Accurate Indirect Occlusion*/ 996 return exp2(-3.32193 * r * r); 997 } 998 999 /* --------- Closure ---------- */ 1000 1001 #ifdef VOLUMETRICS 1002 1003 struct Closure { 1004 vec3 absorption; 1005 vec3 scatter; 1006 vec3 emission; 1007 float anisotropy; 1008 }; 1009 1010 Closure nodetree_exec(void); /* Prototype */ 1011 1012 # define CLOSURE_DEFAULT Closure(vec3(0.0), vec3(0.0), vec3(0.0), 0.0) 1013 1014 Closure closure_mix(Closure cl1, Closure cl2, float fac) 1015 { 1016 Closure cl; 1017 cl.absorption = mix(cl1.absorption, cl2.absorption, fac); 1018 cl.scatter = mix(cl1.scatter, cl2.scatter, fac); 1019 cl.emission = mix(cl1.emission, cl2.emission, fac); 1020 cl.anisotropy = mix(cl1.anisotropy, cl2.anisotropy, fac); 1021 return cl; 1022 } 1023 1024 Closure closure_add(Closure cl1, Closure cl2) 1025 { 1026 Closure cl; 1027 cl.absorption = cl1.absorption + cl2.absorption; 1028 cl.scatter = cl1.scatter + cl2.scatter; 1029 cl.emission = cl1.emission + cl2.emission; 1030 cl.anisotropy = (cl1.anisotropy + cl2.anisotropy) / 2.0; /* Average phase (no multi lobe) */ 1031 return cl; 1032 } 1033 1034 Closure closure_emission(vec3 rgb) 1035 { 1036 Closure cl = CLOSURE_DEFAULT; 1037 cl.emission = rgb; 1038 return cl; 1039 } 1040 1041 #else /* VOLUMETRICS */ 1042 1043 struct Closure { 1044 vec3 radiance; 1045 vec3 transmittance; 1046 float holdout; 1047 # ifdef USE_SSS 1048 vec3 sss_irradiance; 1049 vec3 sss_albedo; 1050 float sss_radius; 1051 # endif 1052 vec4 ssr_data; 1053 vec2 ssr_normal; 1054 int flag; 1055 }; 1056 1057 Closure nodetree_exec(void); /* Prototype */ 1058 1059 # define FLAG_TEST(flag, val) (((flag) & (val)) != 0) 1060 1061 # define CLOSURE_SSR_FLAG 1 1062 # define CLOSURE_SSS_FLAG 2 1063 # define CLOSURE_HOLDOUT_FLAG 4 1064 1065 # ifdef USE_SSS 1066 # define CLOSURE_DEFAULT \ 1067 Closure(vec3(0.0), vec3(0.0), 0.0, vec3(0.0), vec3(0.0), 0.0, vec4(0.0), vec2(0.0), 0) 1068 # else 1069 # define CLOSURE_DEFAULT Closure(vec3(0.0), vec3(0.0), 0.0, vec4(0.0), vec2(0.0), 0) 1070 # endif 1071 1072 uniform int outputSsrId = 1; 1073 uniform int outputSssId = 1; 1074 1075 void closure_load_ssr_data( 1076 vec3 ssr_spec, float roughness, vec3 N, vec3 viewVec, int ssr_id, inout Closure cl) 1077 { 1078 /* Still encode to avoid artifacts in the SSR pass. */ 1079 vec3 vN = normalize(mat3(ViewMatrix) * N); 1080 cl.ssr_normal = normal_encode(vN, viewVec); 1081 1082 if (ssr_id == outputSsrId) { 1083 cl.ssr_data = vec4(ssr_spec, roughness); 1084 cl.flag |= CLOSURE_SSR_FLAG; 1085 } 1086 } 1087 1088 void closure_load_sss_data( 1089 float radius, vec3 sss_irradiance, vec3 sss_albedo, int sss_id, inout Closure cl) 1090 { 1091 # ifdef USE_SSS 1092 if (sss_id == outputSssId) { 1093 cl.sss_irradiance = sss_irradiance; 1094 cl.sss_radius = radius; 1095 cl.sss_albedo = sss_albedo; 1096 cl.flag |= CLOSURE_SSS_FLAG; 1097 } 1098 else 1099 # endif 1100 { 1101 cl.radiance += sss_irradiance * sss_albedo; 1102 } 1103 } 1104 1105 Closure closure_mix(Closure cl1, Closure cl2, float fac) 1106 { 1107 Closure cl; 1108 cl.holdout = mix(cl1.holdout, cl2.holdout, fac); 1109 cl.transmittance = mix(cl1.transmittance, cl2.transmittance, fac); 1110 cl.radiance = mix(cl1.radiance, cl2.radiance, fac); 1111 cl.flag = cl1.flag | cl2.flag; 1112 cl.ssr_data = mix(cl1.ssr_data, cl2.ssr_data, fac); 1113 bool use_cl1_ssr = FLAG_TEST(cl1.flag, CLOSURE_SSR_FLAG); 1114 /* When mixing SSR don't blend roughness and normals but only specular (ssr_data.xyz).*/ 1115 cl.ssr_data.w = (use_cl1_ssr) ? cl1.ssr_data.w : cl2.ssr_data.w; 1116 cl.ssr_normal = (use_cl1_ssr) ? cl1.ssr_normal : cl2.ssr_normal; 1117 1118 # ifdef USE_SSS 1119 cl.sss_albedo = mix(cl1.sss_albedo, cl2.sss_albedo, fac); 1120 bool use_cl1_sss = FLAG_TEST(cl1.flag, CLOSURE_SSS_FLAG); 1121 /* It also does not make sense to mix SSS radius or irradiance. */ 1122 cl.sss_radius = (use_cl1_sss) ? cl1.sss_radius : cl2.sss_radius; 1123 cl.sss_irradiance = (use_cl1_sss) ? cl1.sss_irradiance : cl2.sss_irradiance; 1124 # endif 1125 return cl; 1126 } 1127 1128 Closure closure_add(Closure cl1, Closure cl2) 1129 { 1130 Closure cl; 1131 cl.transmittance = cl1.transmittance + cl2.transmittance; 1132 cl.radiance = cl1.radiance + cl2.radiance; 1133 cl.holdout = cl1.holdout + cl2.holdout; 1134 cl.flag = cl1.flag | cl2.flag; 1135 cl.ssr_data = cl1.ssr_data + cl2.ssr_data; 1136 bool use_cl1_ssr = FLAG_TEST(cl1.flag, CLOSURE_SSR_FLAG); 1137 /* When mixing SSR don't blend roughness and normals.*/ 1138 cl.ssr_data.w = (use_cl1_ssr) ? cl1.ssr_data.w : cl2.ssr_data.w; 1139 cl.ssr_normal = (use_cl1_ssr) ? cl1.ssr_normal : cl2.ssr_normal; 1140 1141 # ifdef USE_SSS 1142 cl.sss_albedo = cl1.sss_albedo + cl2.sss_albedo; 1143 bool use_cl1_sss = FLAG_TEST(cl1.flag, CLOSURE_SSS_FLAG); 1144 /* It also does not make sense to mix SSS radius or irradiance. */ 1145 cl.sss_radius = (use_cl1_sss) ? cl1.sss_radius : cl2.sss_radius; 1146 cl.sss_irradiance = (use_cl1_sss) ? cl1.sss_irradiance : cl2.sss_irradiance; 1147 # endif 1148 return cl; 1149 } 1150 1151 Closure closure_emission(vec3 rgb) 1152 { 1153 Closure cl = CLOSURE_DEFAULT; 1154 cl.radiance = rgb; 1155 return cl; 1156 } 1157 1158 /* Breaking this across multiple lines causes issues for some older GLSL compilers. */ 1159 /* clang-format off */ 1160 # if defined(MESH_SHADER) && !defined(USE_ALPHA_HASH) && !defined(USE_ALPHA_CLIP) && !defined(SHADOW_SHADER) 1161 /* clang-format on */ 1162 # ifndef USE_ALPHA_BLEND 1163 layout(location = 0) out vec4 outRadiance; 1164 layout(location = 1) out vec2 ssrNormals; 1165 layout(location = 2) out vec4 ssrData; 1166 # ifdef USE_SSS 1167 layout(location = 3) out vec3 sssIrradiance; 1168 layout(location = 4) out float sssRadius; 1169 layout(location = 5) out vec3 sssAlbedo; 1170 # endif 1171 # else /* USE_ALPHA_BLEND */ 1172 /* Use dual source blending to be able to make a whole range of effects. */ 1173 layout(location = 0, index = 0) out vec4 outRadiance; 1174 layout(location = 0, index = 1) out vec4 outTransmittance; 1175 # endif /* USE_ALPHA_BLEND */ 1176 1177 # if defined(USE_ALPHA_BLEND) 1178 /* Prototype because this file is included before volumetric_lib.glsl */ 1179 void volumetric_resolve(vec2 frag_uvs, 1180 float frag_depth, 1181 out vec3 transmittance, 1182 out vec3 scattering); 1183 # endif 1184 1185 # define NODETREE_EXEC 1186 void main() 1187 { 1188 Closure cl = nodetree_exec(); 1189 1190 float holdout = 1.0 - saturate(cl.holdout); 1191 float transmit = saturate(avg(cl.transmittance)); 1192 float alpha = 1.0 - transmit; 1193 1194 # ifdef USE_ALPHA_BLEND 1195 vec2 uvs = gl_FragCoord.xy * volCoordScale.zw; 1196 vec3 vol_transmit, vol_scatter; 1197 volumetric_resolve(uvs, gl_FragCoord.z, vol_transmit, vol_scatter); 1198 1199 /* Removes part of the volume scattering that have 1200 * already been added to the destination pixels. 1201 * Since we do that using the blending pipeline we need to account for material transmittance. */ 1202 vol_scatter -= vol_scatter * cl.transmittance; 1203 1204 outRadiance = vec4(cl.radiance * vol_transmit + vol_scatter, alpha * holdout); 1205 outTransmittance = vec4(cl.transmittance, transmit * holdout); 1206 # else 1207 outRadiance = vec4(cl.radiance, holdout); 1208 ssrNormals = cl.ssr_normal; 1209 ssrData = cl.ssr_data; 1210 # ifdef USE_SSS 1211 sssIrradiance = cl.sss_irradiance; 1212 sssRadius = cl.sss_radius; 1213 sssAlbedo = cl.sss_albedo; 1214 # endif 1215 # endif 1216 1217 /* For Probe capture */ 1218 # ifdef USE_SSS 1219 float fac = float(!sssToggle); 1220 1221 /* TODO(fclem) we shouldn't need this. 1222 * Just disable USE_SSS when USE_REFRACTION is enabled. */ 1223 # ifdef USE_REFRACTION 1224 /* SSRefraction pass is done after the SSS pass. 1225 * In order to not loose the diffuse light totally we 1226 * need to merge the SSS radiance to the main radiance. */ 1227 fac = 1.0; 1228 # endif 1229 1230 outRadiance.rgb += cl.sss_irradiance.rgb * cl.sss_albedo.rgb * fac; 1231 # endif 1232 1233 # ifndef USE_ALPHA_BLEND 1234 float alpha_div = 1.0 / max(1e-8, alpha); 1235 outRadiance.rgb *= alpha_div; 1236 ssrData.rgb *= alpha_div; 1237 # ifdef USE_SSS 1238 sssAlbedo.rgb *= alpha_div; 1239 # endif 1240 # endif 1241 } 1242 1243 # endif /* MESH_SHADER && !SHADOW_SHADER */ 1244 1245 #endif /* VOLUMETRICS */ 1246 1247 uniform sampler1D texHammersley; 1248 uniform sampler2D texJitter; 1249 uniform float sampleCount; 1250 uniform float invSampleCount; 1251 1252 vec2 jitternoise = vec2(0.0); 1253 1254 #ifndef UTIL_TEX 1255 # define UTIL_TEX 1256 uniform sampler2DArray utilTex; 1257 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 1258 #endif /* UTIL_TEX */ 1259 1260 void setup_noise(void) 1261 { 1262 jitternoise = texelfetch_noise_tex(gl_FragCoord.xy).rg; /* Global variable */ 1263 } 1264 1265 #ifdef HAMMERSLEY_SIZE 1266 vec3 hammersley_3d(float i, float invsamplenbr) 1267 { 1268 vec3 Xi; /* Theta, cos(Phi), sin(Phi) */ 1269 1270 Xi.x = i * invsamplenbr; /* i/samples */ 1271 Xi.x = fract(Xi.x + jitternoise.x); 1272 1273 int u = int(mod(i + jitternoise.y * HAMMERSLEY_SIZE, HAMMERSLEY_SIZE)); 1274 1275 Xi.yz = texelFetch(texHammersley, u, 0).rg; 1276 1277 return Xi; 1278 } 1279 1280 vec3 hammersley_3d(float i) 1281 { 1282 return hammersley_3d(i, invSampleCount); 1283 } 1284 #endif 1285 1286 /* -------------- BSDFS -------------- */ 1287 1288 float pdf_ggx_reflect(float NH, float a2) 1289 { 1290 return NH * a2 / D_ggx_opti(NH, a2); 1291 } 1292 1293 float pdf_hemisphere() 1294 { 1295 return 0.5 * M_1_PI; 1296 } 1297 1298 vec3 sample_ggx(vec3 rand, float a2) 1299 { 1300 /* Theta is the aperture angle of the cone */ 1301 float z = sqrt((1.0 - rand.x) / (1.0 + a2 * rand.x - rand.x)); /* cos theta */ 1302 float r = sqrt(max(0.0, 1.0f - z * z)); /* sin theta */ 1303 float x = r * rand.y; 1304 float y = r * rand.z; 1305 1306 /* Microfacet Normal */ 1307 return vec3(x, y, z); 1308 } 1309 1310 vec3 sample_ggx(vec3 rand, float a2, vec3 N, vec3 T, vec3 B, out float NH) 1311 { 1312 vec3 Ht = sample_ggx(rand, a2); 1313 NH = Ht.z; 1314 return tangent_to_world(Ht, N, T, B); 1315 } 1316 1317 #ifdef HAMMERSLEY_SIZE 1318 vec3 sample_ggx(float nsample, float a2, vec3 N, vec3 T, vec3 B) 1319 { 1320 vec3 Xi = hammersley_3d(nsample); 1321 vec3 Ht = sample_ggx(Xi, a2); 1322 return tangent_to_world(Ht, N, T, B); 1323 } 1324 1325 vec3 sample_hemisphere(float nsample, vec3 N, vec3 T, vec3 B) 1326 { 1327 vec3 Xi = hammersley_3d(nsample); 1328 1329 float z = Xi.x; /* cos theta */ 1330 float r = sqrt(max(0.0, 1.0f - z * z)); /* sin theta */ 1331 float x = r * Xi.y; 1332 float y = r * Xi.z; 1333 1334 vec3 Ht = vec3(x, y, z); 1335 1336 return tangent_to_world(Ht, N, T, B); 1337 } 1338 1339 vec3 sample_cone(float nsample, float angle, vec3 N, vec3 T, vec3 B) 1340 { 1341 vec3 Xi = hammersley_3d(nsample); 1342 1343 float z = cos(angle * Xi.x); /* cos theta */ 1344 float r = sqrt(max(0.0, 1.0f - z * z)); /* sin theta */ 1345 float x = r * Xi.y; 1346 float y = r * Xi.z; 1347 1348 vec3 Ht = vec3(x, y, z); 1349 1350 return tangent_to_world(Ht, N, T, B); 1351 } 1352 #endif 1353 1354 /* Based on Practical Realtime Strategies for Accurate Indirect Occlusion 1355 * http://blog.selfshadow.com/publications/s2016-shading-course/activision/s2016_pbs_activision_occlusion.pdf 1356 * http://blog.selfshadow.com/publications/s2016-shading-course/activision/s2016_pbs_activision_occlusion.pptx 1357 */ 1358 1359 #if defined(MESH_SHADER) 1360 # if !defined(USE_ALPHA_HASH) 1361 # if !defined(USE_ALPHA_CLIP) 1362 # if !defined(SHADOW_SHADER) 1363 # if !defined(USE_MULTIPLY) 1364 # if !defined(USE_ALPHA_BLEND) 1365 # define ENABLE_DEFERED_AO 1366 # endif 1367 # endif 1368 # endif 1369 # endif 1370 # endif 1371 #endif 1372 1373 #ifndef ENABLE_DEFERED_AO 1374 # if defined(STEP_RESOLVE) 1375 # define ENABLE_DEFERED_AO 1376 # endif 1377 #endif 1378 1379 #define MAX_PHI_STEP 32 1380 #define MAX_SEARCH_ITER 32 1381 #define MAX_LOD 6.0 1382 1383 #ifndef UTIL_TEX 1384 # define UTIL_TEX 1385 uniform sampler2DArray utilTex; 1386 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 1387 #endif /* UTIL_TEX */ 1388 1389 uniform sampler2D horizonBuffer; 1390 1391 /* aoSettings flags */ 1392 #define USE_AO 1 1393 #define USE_BENT_NORMAL 2 1394 #define USE_DENOISE 4 1395 1396 vec4 pack_horizons(vec4 v) 1397 { 1398 return v * 0.5 + 0.5; 1399 } 1400 vec4 unpack_horizons(vec4 v) 1401 { 1402 return v * 2.0 - 1.0; 1403 } 1404 1405 /* Returns maximum screen distance an AO ray can travel for a given view depth */ 1406 vec2 get_max_dir(float view_depth) 1407 { 1408 float homcco = ProjectionMatrix[2][3] * view_depth + ProjectionMatrix[3][3]; 1409 float max_dist = aoDistance / homcco; 1410 return vec2(ProjectionMatrix[0][0], ProjectionMatrix[1][1]) * max_dist; 1411 } 1412 1413 vec2 get_ao_dir(float jitter) 1414 { 1415 /* Only half a turn because we integrate in slices. */ 1416 jitter *= M_PI; 1417 return vec2(cos(jitter), sin(jitter)); 1418 } 1419 1420 void get_max_horizon_grouped(vec4 co1, vec4 co2, vec3 x, float lod, inout float h) 1421 { 1422 int mip = int(lod) + hizMipOffset; 1423 co1 *= mipRatio[mip].xyxy; 1424 co2 *= mipRatio[mip].xyxy; 1425 1426 float depth1 = textureLod(maxzBuffer, co1.xy, floor(lod)).r; 1427 float depth2 = textureLod(maxzBuffer, co1.zw, floor(lod)).r; 1428 float depth3 = textureLod(maxzBuffer, co2.xy, floor(lod)).r; 1429 float depth4 = textureLod(maxzBuffer, co2.zw, floor(lod)).r; 1430 1431 vec4 len, s_h; 1432 1433 vec3 s1 = get_view_space_from_depth(co1.xy, depth1); /* s View coordinate */ 1434 vec3 omega_s1 = s1 - x; 1435 len.x = length(omega_s1); 1436 s_h.x = omega_s1.z / len.x; 1437 1438 vec3 s2 = get_view_space_from_depth(co1.zw, depth2); /* s View coordinate */ 1439 vec3 omega_s2 = s2 - x; 1440 len.y = length(omega_s2); 1441 s_h.y = omega_s2.z / len.y; 1442 1443 vec3 s3 = get_view_space_from_depth(co2.xy, depth3); /* s View coordinate */ 1444 vec3 omega_s3 = s3 - x; 1445 len.z = length(omega_s3); 1446 s_h.z = omega_s3.z / len.z; 1447 1448 vec3 s4 = get_view_space_from_depth(co2.zw, depth4); /* s View coordinate */ 1449 vec3 omega_s4 = s4 - x; 1450 len.w = length(omega_s4); 1451 s_h.w = omega_s4.z / len.w; 1452 1453 /* Blend weight after half the aoDistance to fade artifacts */ 1454 vec4 blend = saturate((1.0 - len / aoDistance) * 2.0); 1455 1456 h = mix(h, max(h, s_h.x), blend.x); 1457 h = mix(h, max(h, s_h.y), blend.y); 1458 h = mix(h, max(h, s_h.z), blend.z); 1459 h = mix(h, max(h, s_h.w), blend.w); 1460 } 1461 1462 vec2 search_horizon_sweep(vec2 t_phi, vec3 pos, vec2 uvs, float jitter, vec2 max_dir) 1463 { 1464 max_dir *= max_v2(abs(t_phi)); 1465 1466 /* Convert to pixel space. */ 1467 t_phi /= vec2(textureSize(maxzBuffer, 0)); 1468 1469 /* Avoid division by 0 */ 1470 t_phi += vec2(1e-5); 1471 1472 jitter *= 0.25; 1473 1474 /* Compute end points */ 1475 vec2 corner1 = min(vec2(1.0) - uvs, max_dir); /* Top right */ 1476 vec2 corner2 = max(vec2(0.0) - uvs, -max_dir); /* Bottom left */ 1477 vec2 iter1 = corner1 / t_phi; 1478 vec2 iter2 = corner2 / t_phi; 1479 1480 vec2 min_iter = max(-iter1, -iter2); 1481 vec2 max_iter = max(iter1, iter2); 1482 1483 vec2 times = vec2(-min_v2(min_iter), min_v2(max_iter)); 1484 1485 vec2 h = vec2(-1.0); /* init at cos(pi) */ 1486 1487 /* This is freaking sexy optimized. */ 1488 for (float i = 0.0, ofs = 4.0, time = -1.0; i < MAX_SEARCH_ITER && time > times.x; 1489 i++, time -= ofs, ofs = min(exp2(MAX_LOD) * 4.0, ofs + ofs * aoQuality)) { 1490 vec4 t = max(times.xxxx, vec4(time) - (vec4(0.25, 0.5, 0.75, 1.0) - jitter) * ofs); 1491 vec4 cos1 = uvs.xyxy + t_phi.xyxy * t.xxyy; 1492 vec4 cos2 = uvs.xyxy + t_phi.xyxy * t.zzww; 1493 float lod = min(MAX_LOD, max(i - jitter * 4.0, 0.0) * aoQuality); 1494 get_max_horizon_grouped(cos1, cos2, pos, lod, h.y); 1495 } 1496 1497 for (float i = 0.0, ofs = 4.0, time = 1.0; i < MAX_SEARCH_ITER && time < times.y; 1498 i++, time += ofs, ofs = min(exp2(MAX_LOD) * 4.0, ofs + ofs * aoQuality)) { 1499 vec4 t = min(times.yyyy, vec4(time) + (vec4(0.25, 0.5, 0.75, 1.0) - jitter) * ofs); 1500 vec4 cos1 = uvs.xyxy + t_phi.xyxy * t.xxyy; 1501 vec4 cos2 = uvs.xyxy + t_phi.xyxy * t.zzww; 1502 float lod = min(MAX_LOD, max(i - jitter * 4.0, 0.0) * aoQuality); 1503 get_max_horizon_grouped(cos1, cos2, pos, lod, h.x); 1504 } 1505 1506 return h; 1507 } 1508 1509 void integrate_slice( 1510 vec3 normal, vec2 t_phi, vec2 horizons, inout float visibility, inout vec3 bent_normal) 1511 { 1512 /* Projecting Normal to Plane P defined by t_phi and omega_o */ 1513 vec3 np = vec3(t_phi.y, -t_phi.x, 0.0); /* Normal vector to Integration plane */ 1514 vec3 t = vec3(-t_phi, 0.0); 1515 vec3 n_proj = normal - np * dot(np, normal); 1516 float n_proj_len = max(1e-16, length(n_proj)); 1517 1518 float cos_n = clamp(n_proj.z / n_proj_len, -1.0, 1.0); 1519 float n = sign(dot(n_proj, t)) * fast_acos(cos_n); /* Angle between view vec and normal */ 1520 1521 /* (Slide 54) */ 1522 vec2 h = fast_acos(horizons); 1523 h.x = -h.x; 1524 1525 /* Clamping thetas (slide 58) */ 1526 h.x = n + max(h.x - n, -M_PI_2); 1527 h.y = n + min(h.y - n, M_PI_2); 1528 1529 /* Solving inner integral */ 1530 vec2 h_2 = 2.0 * h; 1531 vec2 vd = -cos(h_2 - n) + cos_n + h_2 * sin(n); 1532 float vis = saturate((vd.x + vd.y) * 0.25 * n_proj_len); 1533 1534 visibility += vis; 1535 1536 /* O. Klehm, T. Ritschel, E. Eisemann, H.-P. Seidel 1537 * Bent Normals and Cones in Screen-space 1538 * Sec. 3.1 : Bent normals */ 1539 float b_angle = (h.x + h.y) * 0.5; 1540 bent_normal += vec3(sin(b_angle) * -t_phi, cos(b_angle)) * vis; 1541 } 1542 1543 void gtao_deferred( 1544 vec3 normal, vec4 noise, float frag_depth, out float visibility, out vec3 bent_normal) 1545 { 1546 /* Fetch early, hide latency! */ 1547 vec4 horizons = texelFetch(horizonBuffer, ivec2(gl_FragCoord.xy), 0); 1548 1549 vec4 dirs; 1550 dirs.xy = get_ao_dir(noise.x * 0.5); 1551 dirs.zw = get_ao_dir(noise.x * 0.5 + 0.5); 1552 1553 bent_normal = normal * 1e-8; 1554 visibility = 1e-8; 1555 1556 horizons = unpack_horizons(horizons); 1557 1558 integrate_slice(normal, dirs.xy, horizons.xy, visibility, bent_normal); 1559 integrate_slice(normal, dirs.zw, horizons.zw, visibility, bent_normal); 1560 1561 bent_normal = normalize(bent_normal / visibility); 1562 1563 visibility *= 0.5; /* We integrated 2 slices. */ 1564 } 1565 1566 void gtao(vec3 normal, vec3 position, vec4 noise, out float visibility, out vec3 bent_normal) 1567 { 1568 vec2 uvs = get_uvs_from_view(position); 1569 vec2 max_dir = get_max_dir(position.z); 1570 vec2 dir = get_ao_dir(noise.x); 1571 1572 bent_normal = normal * 1e-8; 1573 visibility = 1e-8; 1574 1575 /* Only trace in 2 directions. May lead to a darker result but since it's mostly for 1576 * alpha blended objects that will have overdraw, we limit the performance impact. */ 1577 vec2 horizons = search_horizon_sweep(dir, position, uvs, noise.y, max_dir); 1578 integrate_slice(normal, dir, horizons, visibility, bent_normal); 1579 1580 bent_normal = normalize(bent_normal / visibility); 1581 } 1582 1583 /* Multibounce approximation base on surface albedo. 1584 * Page 78 in the .pdf version. */ 1585 float gtao_multibounce(float visibility, vec3 albedo) 1586 { 1587 if (aoBounceFac == 0.0) { 1588 return visibility; 1589 } 1590 1591 /* Median luminance. Because Colored multibounce looks bad. */ 1592 float lum = dot(albedo, vec3(0.3333)); 1593 1594 float a = 2.0404 * lum - 0.3324; 1595 float b = -4.7951 * lum + 0.6417; 1596 float c = 2.7552 * lum + 0.6903; 1597 1598 float x = visibility; 1599 return max(x, ((x * a + b) * x + c) * x); 1600 } 1601 1602 /* Use the right occlusion */ 1603 float occlusion_compute(vec3 N, vec3 vpos, float user_occlusion, vec4 rand, out vec3 bent_normal) 1604 { 1605 #ifndef USE_REFRACTION 1606 if ((int(aoSettings) & USE_AO) != 0) { 1607 float visibility; 1608 vec3 vnor = mat3(ViewMatrix) * N; 1609 1610 # ifdef ENABLE_DEFERED_AO 1611 gtao_deferred(vnor, rand, gl_FragCoord.z, visibility, bent_normal); 1612 # else 1613 gtao(vnor, vpos, rand, visibility, bent_normal); 1614 # endif 1615 1616 /* Prevent some problems down the road. */ 1617 visibility = max(1e-3, visibility); 1618 1619 if ((int(aoSettings) & USE_BENT_NORMAL) != 0) { 1620 /* The bent normal will show the facet look of the mesh. Try to minimize this. */ 1621 float mix_fac = visibility * visibility * visibility; 1622 bent_normal = normalize(mix(bent_normal, vnor, mix_fac)); 1623 1624 bent_normal = transform_direction(ViewMatrixInverse, bent_normal); 1625 } 1626 else { 1627 bent_normal = N; 1628 } 1629 1630 /* Scale by user factor */ 1631 visibility = pow(visibility, aoFactor); 1632 1633 return min(visibility, user_occlusion); 1634 } 1635 #endif 1636 1637 bent_normal = N; 1638 return user_occlusion; 1639 } 1640 #define MAX_STEP 256 1641 1642 float sample_depth(vec2 uv, int index, float lod) 1643 { 1644 #ifdef PLANAR_PROBE_RAYTRACE 1645 if (index > -1) { 1646 return textureLod(planarDepth, vec3(uv, index), 0.0).r; 1647 } 1648 else { 1649 #endif 1650 /* Correct UVs for mipmaping mis-alignment */ 1651 uv *= mipRatio[int(lod) + hizMipOffset]; 1652 return textureLod(maxzBuffer, uv, lod).r; 1653 #ifdef PLANAR_PROBE_RAYTRACE 1654 } 1655 #endif 1656 } 1657 1658 vec4 sample_depth_grouped(vec4 uv1, vec4 uv2, int index, float lod) 1659 { 1660 vec4 depths; 1661 #ifdef PLANAR_PROBE_RAYTRACE 1662 if (index > -1) { 1663 depths.x = textureLod(planarDepth, vec3(uv1.xy, index), 0.0).r; 1664 depths.y = textureLod(planarDepth, vec3(uv1.zw, index), 0.0).r; 1665 depths.z = textureLod(planarDepth, vec3(uv2.xy, index), 0.0).r; 1666 depths.w = textureLod(planarDepth, vec3(uv2.zw, index), 0.0).r; 1667 } 1668 else { 1669 #endif 1670 depths.x = textureLod(maxzBuffer, uv1.xy, lod).r; 1671 depths.y = textureLod(maxzBuffer, uv1.zw, lod).r; 1672 depths.z = textureLod(maxzBuffer, uv2.xy, lod).r; 1673 depths.w = textureLod(maxzBuffer, uv2.zw, lod).r; 1674 #ifdef PLANAR_PROBE_RAYTRACE 1675 } 1676 #endif 1677 return depths; 1678 } 1679 1680 float refine_isect(float prev_delta, float curr_delta) 1681 { 1682 /** 1683 * Simplification of 2D intersection : 1684 * r0 = (0.0, prev_ss_ray.z); 1685 * r1 = (1.0, curr_ss_ray.z); 1686 * d0 = (0.0, prev_hit_depth_sample); 1687 * d1 = (1.0, curr_hit_depth_sample); 1688 * vec2 r = r1 - r0; 1689 * vec2 d = d1 - d0; 1690 * vec2 isect = ((d * cross(r1, r0)) - (r * cross(d1, d0))) / cross(r,d); 1691 * 1692 * We only want isect.x to know how much stride we need. So it simplifies : 1693 * 1694 * isect_x = (cross(r1, r0) - cross(d1, d0)) / cross(r,d); 1695 * isect_x = (prev_ss_ray.z - prev_hit_depth_sample.z) / cross(r,d); 1696 */ 1697 return saturate(prev_delta / (prev_delta - curr_delta)); 1698 } 1699 1700 void prepare_raycast(vec3 ray_origin, 1701 vec3 ray_dir, 1702 float thickness, 1703 int index, 1704 out vec4 ss_step, 1705 out vec4 ss_ray, 1706 out float max_time) 1707 { 1708 /* Negate the ray direction if it goes towards the camera. 1709 * This way we don't need to care if the projected point 1710 * is behind the near plane. */ 1711 float z_sign = -sign(ray_dir.z); 1712 vec3 ray_end = ray_origin + z_sign * ray_dir; 1713 1714 /* Project into screen space. */ 1715 vec4 ss_start, ss_end; 1716 ss_start.xyz = project_point(ProjectionMatrix, ray_origin); 1717 ss_end.xyz = project_point(ProjectionMatrix, ray_end); 1718 1719 /* We interpolate the ray Z + thickness values to check if depth is within threshold. */ 1720 ray_origin.z -= thickness; 1721 ray_end.z -= thickness; 1722 ss_start.w = project_point(ProjectionMatrix, ray_origin).z; 1723 ss_end.w = project_point(ProjectionMatrix, ray_end).z; 1724 1725 /* XXX This is a hack. A better method is welcome! */ 1726 /* We take the delta between the offsetted depth and the depth and subtract it from the ray 1727 * depth. This will change the world space thickness appearance a bit but we can have negative 1728 * values without worries. We cannot do this in viewspace because of the perspective division. */ 1729 ss_start.w = 2.0 * ss_start.z - ss_start.w; 1730 ss_end.w = 2.0 * ss_end.z - ss_end.w; 1731 1732 ss_step = ss_end - ss_start; 1733 max_time = length(ss_step.xyz); 1734 ss_step = z_sign * ss_step / length(ss_step.xyz); 1735 1736 /* If the line is degenerate, make it cover at least one pixel 1737 * to not have to handle zero-pixel extent as a special case later */ 1738 ss_step.xy += vec2((dot(ss_step.xy, ss_step.xy) < 0.00001) ? 0.001 : 0.0); 1739 1740 /* Make ss_step cover one pixel. */ 1741 ss_step /= max(abs(ss_step.x), abs(ss_step.y)); 1742 ss_step *= (abs(ss_step.x) > abs(ss_step.y)) ? ssrPixelSize.x : ssrPixelSize.y; 1743 1744 /* Clip to segment's end. */ 1745 max_time /= length(ss_step.xyz); 1746 1747 /* Clipping to frustum sides. */ 1748 max_time = min(max_time, line_unit_box_intersect_dist(ss_start.xyz, ss_step.xyz)); 1749 1750 /* Convert to texture coords. Z component included 1751 * since this is how it's stored in the depth buffer. 1752 * 4th component how far we are on the ray */ 1753 #ifdef PLANAR_PROBE_RAYTRACE 1754 /* Planar Reflections have X mirrored. */ 1755 vec2 m = (index > -1) ? vec2(-0.5, 0.5) : vec2(0.5); 1756 #else 1757 const vec2 m = vec2(0.5); 1758 #endif 1759 ss_ray = ss_start * m.xyyy + 0.5; 1760 ss_step *= m.xyyy; 1761 1762 /* take the center of the texel. */ 1763 // ss_ray.xy += sign(ss_ray.xy) * m * ssrPixelSize * (1.0 + hizMipOffset); 1764 } 1765 1766 /* See times_and_deltas. */ 1767 #define curr_time times_and_deltas.x 1768 #define prev_time times_and_deltas.y 1769 #define curr_delta times_and_deltas.z 1770 #define prev_delta times_and_deltas.w 1771 1772 // #define GROUPED_FETCHES /* is still slower, need to see where is the bottleneck. */ 1773 /* Return the hit position, and negate the z component (making it positive) if not hit occurred. */ 1774 /* __ray_dir__ is the ray direction premultiplied by it's maximum length */ 1775 vec3 raycast(int index, 1776 vec3 ray_origin, 1777 vec3 ray_dir, 1778 float thickness, 1779 float ray_jitter, 1780 float trace_quality, 1781 float roughness, 1782 const bool discard_backface) 1783 { 1784 vec4 ss_step, ss_start; 1785 float max_time; 1786 prepare_raycast(ray_origin, ray_dir, thickness, index, ss_step, ss_start, max_time); 1787 1788 float max_trace_time = max(0.01, max_time - 0.01); 1789 1790 #ifdef GROUPED_FETCHES 1791 ray_jitter *= 0.25; 1792 #endif 1793 1794 /* x : current_time, y: previous_time, z: current_delta, w: previous_delta */ 1795 vec4 times_and_deltas = vec4(0.0); 1796 1797 float ray_time = 0.0; 1798 float depth_sample = sample_depth(ss_start.xy, index, 0.0); 1799 curr_delta = depth_sample - ss_start.z; 1800 1801 float lod_fac = saturate(fast_sqrt(roughness) * 2.0 - 0.4); 1802 bool hit = false; 1803 float iter; 1804 for (iter = 1.0; !hit && (ray_time < max_time) && (iter < MAX_STEP); iter++) { 1805 /* Minimum stride of 2 because we are using half res minmax zbuffer. */ 1806 float stride = max(1.0, iter * trace_quality) * 2.0; 1807 float lod = log2(stride * 0.5 * trace_quality) * lod_fac; 1808 ray_time += stride; 1809 1810 /* Save previous values. */ 1811 times_and_deltas.xyzw = times_and_deltas.yxwz; 1812 1813 #ifdef GROUPED_FETCHES 1814 stride *= 4.0; 1815 vec4 jit_stride = mix(vec4(2.0), vec4(stride), vec4(0.0, 0.25, 0.5, 0.75) + ray_jitter); 1816 1817 vec4 times = min(vec4(ray_time) + jit_stride, vec4(max_trace_time)); 1818 1819 vec4 uv1 = ss_start.xyxy + ss_step.xyxy * times.xxyy; 1820 vec4 uv2 = ss_start.xyxy + ss_step.xyxy * times.zzww; 1821 1822 vec4 depth_samples = sample_depth_grouped(uv1, uv2, index, lod); 1823 1824 vec4 ray_z = ss_start.zzzz + ss_step.zzzz * times.xyzw; 1825 vec4 ray_w = ss_start.wwww + ss_step.wwww * vec4(prev_time, times.xyz); 1826 1827 vec4 deltas = depth_samples - ray_z; 1828 /* Same as component wise (curr_delta <= 0.0) && (prev_w <= depth_sample). */ 1829 bvec4 test = equal(step(deltas, vec4(0.0)) * step(ray_w, depth_samples), vec4(1.0)); 1830 hit = any(test); 1831 1832 if (hit) { 1833 vec2 m = vec2(1.0, 0.0); /* Mask */ 1834 1835 vec4 ret_times_and_deltas = times.wzzz * m.xxyy + deltas.wwwz * m.yyxx; 1836 ret_times_and_deltas = (test.z) ? times.zyyy * m.xxyy + deltas.zzzy * m.yyxx : 1837 ret_times_and_deltas; 1838 ret_times_and_deltas = (test.y) ? times.yxxx * m.xxyy + deltas.yyyx * m.yyxx : 1839 ret_times_and_deltas; 1840 times_and_deltas = (test.x) ? times.xxxx * m.xyyy + deltas.xxxx * m.yyxy + 1841 times_and_deltas.yyww * m.yxyx : 1842 ret_times_and_deltas; 1843 1844 depth_sample = depth_samples.w; 1845 depth_sample = (test.z) ? depth_samples.z : depth_sample; 1846 depth_sample = (test.y) ? depth_samples.y : depth_sample; 1847 depth_sample = (test.x) ? depth_samples.x : depth_sample; 1848 } 1849 else { 1850 curr_time = times.w; 1851 curr_delta = deltas.w; 1852 } 1853 #else 1854 float jit_stride = mix(2.0, stride, ray_jitter); 1855 1856 curr_time = min(ray_time + jit_stride, max_trace_time); 1857 vec4 ss_ray = ss_start + ss_step * curr_time; 1858 1859 depth_sample = sample_depth(ss_ray.xy, index, lod); 1860 1861 float prev_w = ss_start.w + ss_step.w * prev_time; 1862 curr_delta = depth_sample - ss_ray.z; 1863 hit = (curr_delta <= 0.0) && (prev_w <= depth_sample); 1864 #endif 1865 } 1866 1867 if (discard_backface) { 1868 /* Discard backface hits */ 1869 hit = hit && (prev_delta > 0.0); 1870 } 1871 1872 /* Reject hit if background. */ 1873 hit = hit && (depth_sample != 1.0); 1874 1875 curr_time = (hit) ? mix(prev_time, curr_time, refine_isect(prev_delta, curr_delta)) : curr_time; 1876 ray_time = (hit) ? curr_time : ray_time; 1877 1878 /* Clip to frustum. */ 1879 ray_time = max(0.001, min(ray_time, max_time - 1.5)); 1880 1881 vec4 ss_ray = ss_start + ss_step * ray_time; 1882 1883 /* Tag Z if ray failed. */ 1884 ss_ray.z *= (hit) ? 1.0 : -1.0; 1885 return ss_ray.xyz; 1886 } 1887 1888 float screen_border_mask(vec2 hit_co) 1889 { 1890 const float margin = 0.003; 1891 float atten = ssrBorderFac + margin; /* Screen percentage */ 1892 hit_co = smoothstep(margin, atten, hit_co) * (1 - smoothstep(1.0 - atten, 1.0 - margin, hit_co)); 1893 1894 float screenfade = hit_co.x * hit_co.y; 1895 1896 return screenfade; 1897 } 1898 /* ------------ Refraction ------------ */ 1899 1900 #define BTDF_BIAS 0.85 1901 1902 vec4 screen_space_refraction( 1903 vec3 viewPosition, vec3 N, vec3 V, float ior, float roughnessSquared, vec4 rand) 1904 { 1905 float a2 = max(5e-6, roughnessSquared * roughnessSquared); 1906 1907 /* Importance sampling bias */ 1908 rand.x = mix(rand.x, 0.0, BTDF_BIAS); 1909 1910 vec3 T, B; 1911 float NH; 1912 make_orthonormal_basis(N, T, B); 1913 vec3 H = sample_ggx(rand.xzw, a2, N, T, B, NH); /* Microfacet normal */ 1914 float pdf = pdf_ggx_reflect(NH, a2); 1915 1916 /* If ray is bad (i.e. going below the plane) regenerate. */ 1917 if (F_eta(ior, dot(H, V)) < 1.0) { 1918 H = sample_ggx(rand.xzw * vec3(1.0, -1.0, -1.0), a2, N, T, B, NH); /* Microfacet normal */ 1919 pdf = pdf_ggx_reflect(NH, a2); 1920 } 1921 1922 vec3 vV = viewCameraVec; 1923 float eta = 1.0 / ior; 1924 if (dot(H, V) < 0.0) { 1925 H = -H; 1926 eta = ior; 1927 } 1928 1929 vec3 R = refract(-V, H, 1.0 / ior); 1930 1931 R = transform_direction(ViewMatrix, R); 1932 1933 vec3 hit_pos = raycast( 1934 -1, viewPosition, R * 1e16, ssrThickness, rand.y, ssrQuality, roughnessSquared, false); 1935 1936 if ((hit_pos.z > 0.0) && (F_eta(ior, dot(H, V)) < 1.0)) { 1937 hit_pos = get_view_space_from_depth(hit_pos.xy, hit_pos.z); 1938 float hit_dist = distance(hit_pos, viewPosition); 1939 1940 float cone_cos = cone_cosine(roughnessSquared); 1941 float cone_tan = sqrt(1 - cone_cos * cone_cos) / cone_cos; 1942 1943 /* Empirical fit for refraction. */ 1944 /* TODO find a better fit or precompute inside the LUT. */ 1945 cone_tan *= 0.5 * fast_sqrt(f0_from_ior((ior < 1.0) ? 1.0 / ior : ior)); 1946 1947 float cone_footprint = hit_dist * cone_tan; 1948 1949 /* find the offset in screen space by multiplying a point 1950 * in camera space at the depth of the point by the projection matrix. */ 1951 float homcoord = ProjectionMatrix[2][3] * hit_pos.z + ProjectionMatrix[3][3]; 1952 /* UV space footprint */ 1953 cone_footprint = BTDF_BIAS * 0.5 * max(ProjectionMatrix[0][0], ProjectionMatrix[1][1]) * 1954 cone_footprint / homcoord; 1955 1956 vec2 hit_uvs = project_point(ProjectionMatrix, hit_pos).xy * 0.5 + 0.5; 1957 1958 /* Texel footprint */ 1959 vec2 texture_size = vec2(textureSize(colorBuffer, 0).xy); 1960 float mip = clamp(log2(cone_footprint * max(texture_size.x, texture_size.y)), 0.0, 9.0); 1961 1962 /* Correct UVs for mipmaping mis-alignment */ 1963 hit_uvs *= mip_ratio_interp(mip); 1964 1965 vec3 spec = textureLod(colorBuffer, hit_uvs, mip).xyz; 1966 float mask = screen_border_mask(hit_uvs); 1967 1968 return vec4(spec, mask); 1969 } 1970 1971 return vec4(0.0); 1972 } 1973 1974 vec2 mapping_octahedron(vec3 cubevec, vec2 texel_size) 1975 { 1976 /* projection onto octahedron */ 1977 cubevec /= dot(vec3(1.0), abs(cubevec)); 1978 1979 /* out-folding of the downward faces */ 1980 if (cubevec.z < 0.0) { 1981 vec2 cubevec_sign = step(0.0, cubevec.xy) * 2.0 - 1.0; 1982 cubevec.xy = (1.0 - abs(cubevec.yx)) * cubevec_sign; 1983 } 1984 1985 /* mapping to [0;1]ˆ2 texture space */ 1986 vec2 uvs = cubevec.xy * (0.5) + 0.5; 1987 1988 /* edge filtering fix */ 1989 uvs = (1.0 - 2.0 * texel_size) * uvs + texel_size; 1990 1991 return uvs; 1992 } 1993 1994 vec4 textureLod_octahedron(sampler2DArray tex, vec4 cubevec, float lod, float lod_max) 1995 { 1996 vec2 texelSize = 1.0 / vec2(textureSize(tex, int(lod_max))); 1997 1998 vec2 uvs = mapping_octahedron(cubevec.xyz, texelSize); 1999 2000 return textureLod(tex, vec3(uvs, cubevec.w), lod); 2001 } 2002 2003 vec4 texture_octahedron(sampler2DArray tex, vec4 cubevec) 2004 { 2005 vec2 texelSize = 1.0 / vec2(textureSize(tex, 0)); 2006 2007 vec2 uvs = mapping_octahedron(cubevec.xyz, texelSize); 2008 2009 return texture(tex, vec3(uvs, cubevec.w)); 2010 } 2011 2012 uniform sampler2DArray irradianceGrid; 2013 2014 #define IRRADIANCE_LIB 2015 2016 #ifdef IRRADIANCE_CUBEMAP 2017 struct IrradianceData { 2018 vec3 color; 2019 }; 2020 #elif defined(IRRADIANCE_SH_L2) 2021 struct IrradianceData { 2022 vec3 shcoefs[9]; 2023 }; 2024 #else /* defined(IRRADIANCE_HL2) */ 2025 struct IrradianceData { 2026 vec3 cubesides[3]; 2027 }; 2028 #endif 2029 2030 IrradianceData load_irradiance_cell(int cell, vec3 N) 2031 { 2032 /* Keep in sync with diffuse_filter_probe() */ 2033 2034 #if defined(IRRADIANCE_CUBEMAP) 2035 2036 # define AMBIANT_CUBESIZE 8 2037 ivec2 cell_co = ivec2(AMBIANT_CUBESIZE); 2038 int cell_per_row = textureSize(irradianceGrid, 0).x / cell_co.x; 2039 cell_co.x *= cell % cell_per_row; 2040 cell_co.y *= cell / cell_per_row; 2041 2042 vec2 texelSize = 1.0 / vec2(AMBIANT_CUBESIZE); 2043 2044 vec2 uvs = mapping_octahedron(N, texelSize); 2045 uvs *= vec2(AMBIANT_CUBESIZE) / vec2(textureSize(irradianceGrid, 0)); 2046 uvs += vec2(cell_co) / vec2(textureSize(irradianceGrid, 0)); 2047 2048 IrradianceData ir; 2049 ir.color = texture(irradianceGrid, vec3(uvs, 0.0)).rgb; 2050 2051 #elif defined(IRRADIANCE_SH_L2) 2052 2053 ivec2 cell_co = ivec2(3, 3); 2054 int cell_per_row = textureSize(irradianceGrid, 0).x / cell_co.x; 2055 cell_co.x *= cell % cell_per_row; 2056 cell_co.y *= cell / cell_per_row; 2057 2058 ivec3 ofs = ivec3(0, 1, 2); 2059 2060 IrradianceData ir; 2061 ir.shcoefs[0] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.xx, 0), 0).rgb; 2062 ir.shcoefs[1] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.yx, 0), 0).rgb; 2063 ir.shcoefs[2] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.zx, 0), 0).rgb; 2064 ir.shcoefs[3] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.xy, 0), 0).rgb; 2065 ir.shcoefs[4] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.yy, 0), 0).rgb; 2066 ir.shcoefs[5] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.zy, 0), 0).rgb; 2067 ir.shcoefs[6] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.xz, 0), 0).rgb; 2068 ir.shcoefs[7] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.yz, 0), 0).rgb; 2069 ir.shcoefs[8] = texelFetch(irradianceGrid, ivec3(cell_co + ofs.zz, 0), 0).rgb; 2070 2071 #else /* defined(IRRADIANCE_HL2) */ 2072 2073 ivec2 cell_co = ivec2(3, 2); 2074 int cell_per_row = textureSize(irradianceGrid, 0).x / cell_co.x; 2075 cell_co.x *= cell % cell_per_row; 2076 cell_co.y *= cell / cell_per_row; 2077 2078 ivec3 is_negative = ivec3(step(0.0, -N)); 2079 2080 IrradianceData ir; 2081 ir.cubesides[0] = irradiance_decode( 2082 texelFetch(irradianceGrid, ivec3(cell_co + ivec2(0, is_negative.x), 0), 0)); 2083 ir.cubesides[1] = irradiance_decode( 2084 texelFetch(irradianceGrid, ivec3(cell_co + ivec2(1, is_negative.y), 0), 0)); 2085 ir.cubesides[2] = irradiance_decode( 2086 texelFetch(irradianceGrid, ivec3(cell_co + ivec2(2, is_negative.z), 0), 0)); 2087 2088 #endif 2089 2090 return ir; 2091 } 2092 2093 float load_visibility_cell(int cell, vec3 L, float dist, float bias, float bleed_bias, float range) 2094 { 2095 /* Keep in sync with diffuse_filter_probe() */ 2096 ivec2 cell_co = ivec2(prbIrradianceVisSize); 2097 ivec2 cell_per_row_col = textureSize(irradianceGrid, 0).xy / prbIrradianceVisSize; 2098 cell_co.x *= (cell % cell_per_row_col.x); 2099 cell_co.y *= (cell / cell_per_row_col.x) % cell_per_row_col.y; 2100 float layer = 1.0 + float((cell / cell_per_row_col.x) / cell_per_row_col.y); 2101 2102 vec2 texel_size = 1.0 / vec2(textureSize(irradianceGrid, 0).xy); 2103 vec2 co = vec2(cell_co) * texel_size; 2104 2105 vec2 uv = mapping_octahedron(-L, vec2(1.0 / float(prbIrradianceVisSize))); 2106 uv *= vec2(prbIrradianceVisSize) * texel_size; 2107 2108 vec4 data = texture(irradianceGrid, vec3(co + uv, layer)); 2109 2110 /* Decoding compressed data */ 2111 vec2 moments = visibility_decode(data, range); 2112 2113 /* Doing chebishev test */ 2114 float variance = abs(moments.x * moments.x - moments.y); 2115 variance = max(variance, bias / 10.0); 2116 2117 float d = dist - moments.x; 2118 float p_max = variance / (variance + d * d); 2119 2120 /* Increase contrast in the weight by squaring it */ 2121 p_max *= p_max; 2122 2123 /* Now reduce light-bleeding by removing the [0, x] tail and linearly rescaling (x, 1] */ 2124 p_max = clamp((p_max - bleed_bias) / (1.0 - bleed_bias), 0.0, 1.0); 2125 2126 return (dist <= moments.x) ? 1.0 : p_max; 2127 } 2128 2129 /* http://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/ */ 2130 vec3 spherical_harmonics_L1(vec3 N, vec3 shcoefs[4]) 2131 { 2132 vec3 sh = vec3(0.0); 2133 2134 sh += 0.282095 * shcoefs[0]; 2135 2136 sh += -0.488603 * N.z * shcoefs[1]; 2137 sh += 0.488603 * N.y * shcoefs[2]; 2138 sh += -0.488603 * N.x * shcoefs[3]; 2139 2140 return sh; 2141 } 2142 2143 vec3 spherical_harmonics_L2(vec3 N, vec3 shcoefs[9]) 2144 { 2145 vec3 sh = vec3(0.0); 2146 2147 sh += 0.282095 * shcoefs[0]; 2148 2149 sh += -0.488603 * N.z * shcoefs[1]; 2150 sh += 0.488603 * N.y * shcoefs[2]; 2151 sh += -0.488603 * N.x * shcoefs[3]; 2152 2153 sh += 1.092548 * N.x * N.z * shcoefs[4]; 2154 sh += -1.092548 * N.z * N.y * shcoefs[5]; 2155 sh += 0.315392 * (3.0 * N.y * N.y - 1.0) * shcoefs[6]; 2156 sh += -1.092548 * N.x * N.y * shcoefs[7]; 2157 sh += 0.546274 * (N.x * N.x - N.z * N.z) * shcoefs[8]; 2158 2159 return sh; 2160 } 2161 2162 vec3 hl2_basis(vec3 N, vec3 cubesides[3]) 2163 { 2164 vec3 irradiance = vec3(0.0); 2165 2166 vec3 n_squared = N * N; 2167 2168 irradiance += n_squared.x * cubesides[0]; 2169 irradiance += n_squared.y * cubesides[1]; 2170 irradiance += n_squared.z * cubesides[2]; 2171 2172 return irradiance; 2173 } 2174 2175 vec3 compute_irradiance(vec3 N, IrradianceData ird) 2176 { 2177 #if defined(IRRADIANCE_CUBEMAP) 2178 return ird.color; 2179 #elif defined(IRRADIANCE_SH_L2) 2180 return spherical_harmonics_L2(N, ird.shcoefs); 2181 #else /* defined(IRRADIANCE_HL2) */ 2182 return hl2_basis(N, ird.cubesides); 2183 #endif 2184 } 2185 2186 vec3 irradiance_from_cell_get(int cell, vec3 ir_dir) 2187 { 2188 IrradianceData ir_data = load_irradiance_cell(cell, ir_dir); 2189 return compute_irradiance(ir_dir, ir_data); 2190 } 2191 /* ----------- Uniforms --------- */ 2192 2193 uniform sampler2DArray probePlanars; 2194 uniform sampler2DArray probeCubes; 2195 2196 /* ----------- Structures --------- */ 2197 2198 struct CubeData { 2199 vec4 position_type; 2200 vec4 attenuation_fac_type; 2201 mat4 influencemat; 2202 mat4 parallaxmat; 2203 }; 2204 2205 #define PROBE_PARALLAX_BOX 1.0 2206 #define PROBE_ATTENUATION_BOX 1.0 2207 2208 #define p_position position_type.xyz 2209 #define p_parallax_type position_type.w 2210 #define p_atten_fac attenuation_fac_type.x 2211 #define p_atten_type attenuation_fac_type.y 2212 2213 struct PlanarData { 2214 vec4 plane_equation; 2215 vec4 clip_vec_x_fade_scale; 2216 vec4 clip_vec_y_fade_bias; 2217 vec4 clip_edges; 2218 vec4 facing_scale_bias; 2219 mat4 reflectionmat; /* transform world space into reflection texture space */ 2220 mat4 unused; 2221 }; 2222 2223 #define pl_plane_eq plane_equation 2224 #define pl_normal plane_equation.xyz 2225 #define pl_facing_scale facing_scale_bias.x 2226 #define pl_facing_bias facing_scale_bias.y 2227 #define pl_fade_scale clip_vec_x_fade_scale.w 2228 #define pl_fade_bias clip_vec_y_fade_bias.w 2229 #define pl_clip_pos_x clip_vec_x_fade_scale.xyz 2230 #define pl_clip_pos_y clip_vec_y_fade_bias.xyz 2231 #define pl_clip_edges clip_edges 2232 2233 struct GridData { 2234 mat4 localmat; 2235 ivec4 resolution_offset; 2236 vec4 ws_corner_atten_scale; /* world space corner position */ 2237 vec4 ws_increment_x_atten_bias; /* world space vector between 2 opposite cells */ 2238 vec4 ws_increment_y_lvl_bias; 2239 vec4 ws_increment_z; 2240 vec4 vis_bias_bleed_range; 2241 }; 2242 2243 #define g_corner ws_corner_atten_scale.xyz 2244 #define g_atten_scale ws_corner_atten_scale.w 2245 #define g_atten_bias ws_increment_x_atten_bias.w 2246 #define g_level_bias ws_increment_y_lvl_bias.w 2247 #define g_increment_x ws_increment_x_atten_bias.xyz 2248 #define g_increment_y ws_increment_y_lvl_bias.xyz 2249 #define g_increment_z ws_increment_z.xyz 2250 #define g_resolution resolution_offset.xyz 2251 #define g_offset resolution_offset.w 2252 #define g_vis_bias vis_bias_bleed_range.x 2253 #define g_vis_bleed vis_bias_bleed_range.y 2254 #define g_vis_range vis_bias_bleed_range.z 2255 2256 #ifndef MAX_PROBE 2257 # define MAX_PROBE 1 2258 #endif 2259 #ifndef MAX_GRID 2260 # define MAX_GRID 1 2261 #endif 2262 #ifndef MAX_PLANAR 2263 # define MAX_PLANAR 1 2264 #endif 2265 2266 #ifndef UTIL_TEX 2267 # define UTIL_TEX 2268 uniform sampler2DArray utilTex; 2269 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 2270 #endif /* UTIL_TEX */ 2271 2272 layout(std140) uniform probe_block 2273 { 2274 CubeData probes_data[MAX_PROBE]; 2275 }; 2276 2277 layout(std140) uniform grid_block 2278 { 2279 GridData grids_data[MAX_GRID]; 2280 }; 2281 2282 layout(std140) uniform planar_block 2283 { 2284 PlanarData planars_data[MAX_PLANAR]; 2285 }; 2286 2287 /* ----------- Functions --------- */ 2288 2289 float probe_attenuation_cube(int pd_id, vec3 W) 2290 { 2291 vec3 localpos = transform_point(probes_data[pd_id].influencemat, W); 2292 2293 float probe_atten_fac = probes_data[pd_id].p_atten_fac; 2294 float fac; 2295 if (probes_data[pd_id].p_atten_type == PROBE_ATTENUATION_BOX) { 2296 vec3 axes_fac = saturate(probe_atten_fac - probe_atten_fac * abs(localpos)); 2297 fac = min_v3(axes_fac); 2298 } 2299 else { 2300 fac = saturate(probe_atten_fac - probe_atten_fac * length(localpos)); 2301 } 2302 2303 return fac; 2304 } 2305 2306 float probe_attenuation_planar(PlanarData pd, vec3 W, vec3 N, float roughness) 2307 { 2308 /* Normal Facing */ 2309 float fac = saturate(dot(pd.pl_normal, N) * pd.pl_facing_scale + pd.pl_facing_bias); 2310 2311 /* Distance from plane */ 2312 fac *= saturate(abs(dot(pd.pl_plane_eq, vec4(W, 1.0))) * pd.pl_fade_scale + pd.pl_fade_bias); 2313 2314 /* Fancy fast clipping calculation */ 2315 vec2 dist_to_clip; 2316 dist_to_clip.x = dot(pd.pl_clip_pos_x, W); 2317 dist_to_clip.y = dot(pd.pl_clip_pos_y, W); 2318 /* compare and add all tests */ 2319 fac *= step(2.0, dot(step(pd.pl_clip_edges, dist_to_clip.xxyy), vec2(-1.0, 1.0).xyxy)); 2320 2321 /* Decrease influence for high roughness */ 2322 fac *= saturate(1.0 - roughness * 10.0); 2323 2324 return fac; 2325 } 2326 2327 float probe_attenuation_grid(GridData gd, mat4 localmat, vec3 W, out vec3 localpos) 2328 { 2329 localpos = transform_point(localmat, W); 2330 vec3 pos_to_edge = max(vec3(0.0), abs(localpos) - 1.0); 2331 float fade = length(pos_to_edge); 2332 return saturate(-fade * gd.g_atten_scale + gd.g_atten_bias); 2333 } 2334 2335 vec3 probe_evaluate_cube(int pd_id, vec3 W, vec3 R, float roughness) 2336 { 2337 /* Correct reflection ray using parallax volume intersection. */ 2338 vec3 localpos = transform_point(probes_data[pd_id].parallaxmat, W); 2339 vec3 localray = transform_direction(probes_data[pd_id].parallaxmat, R); 2340 2341 float dist; 2342 if (probes_data[pd_id].p_parallax_type == PROBE_PARALLAX_BOX) { 2343 dist = line_unit_box_intersect_dist(localpos, localray); 2344 } 2345 else { 2346 dist = line_unit_sphere_intersect_dist(localpos, localray); 2347 } 2348 2349 /* Use Distance in WS directly to recover intersection */ 2350 vec3 intersection = W + R * dist - probes_data[pd_id].p_position; 2351 2352 /* From Frostbite PBR Course 2353 * Distance based roughness 2354 * http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf 2355 */ 2356 float original_roughness = roughness; 2357 float linear_roughness = sqrt(roughness); 2358 float distance_roughness = saturate(dist * linear_roughness / length(intersection)); 2359 linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness); 2360 roughness = linear_roughness * linear_roughness; 2361 2362 float fac = saturate(original_roughness * 2.0 - 1.0); 2363 R = mix(intersection, R, fac * fac); 2364 2365 return textureLod_octahedron( 2366 probeCubes, vec4(R, float(pd_id)), roughness * prbLodCubeMax, prbLodCubeMax) 2367 .rgb; 2368 } 2369 2370 vec3 probe_evaluate_world_spec(vec3 R, float roughness) 2371 { 2372 return textureLod_octahedron(probeCubes, vec4(R, 0.0), roughness * prbLodCubeMax, prbLodCubeMax) 2373 .rgb; 2374 } 2375 2376 vec3 probe_evaluate_planar( 2377 float id, PlanarData pd, vec3 W, vec3 N, vec3 V, float roughness, inout float fade) 2378 { 2379 /* Find view vector / reflection plane intersection. */ 2380 vec3 point_on_plane = line_plane_intersect(W, V, pd.pl_plane_eq); 2381 2382 /* How far the pixel is from the plane. */ 2383 float ref_depth = 1.0; /* TODO parameter */ 2384 2385 /* Compute distorded reflection vector based on the distance to the reflected object. 2386 * In other words find intersection between reflection vector and the sphere center 2387 * around point_on_plane. */ 2388 vec3 proj_ref = reflect(reflect(-V, N) * ref_depth, pd.pl_normal); 2389 2390 /* Final point in world space. */ 2391 vec3 ref_pos = point_on_plane + proj_ref; 2392 2393 /* Reproject to find texture coords. */ 2394 vec4 refco = ViewProjectionMatrix * vec4(ref_pos, 1.0); 2395 refco.xy /= refco.w; 2396 2397 /* TODO: If we support non-ssr planar reflection, we should blur them with gaussian 2398 * and chose the right mip depending on the cone footprint after projection */ 2399 /* NOTE: X is inverted here to compensate inverted drawing. */ 2400 vec3 sample = textureLod(probePlanars, vec3(refco.xy * vec2(-0.5, 0.5) + 0.5, id), 0.0).rgb; 2401 2402 return sample; 2403 } 2404 2405 void fallback_cubemap(vec3 N, 2406 vec3 V, 2407 vec3 W, 2408 vec3 viewPosition, 2409 float roughness, 2410 float roughnessSquared, 2411 inout vec4 spec_accum) 2412 { 2413 /* Specular probes */ 2414 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 2415 2416 #ifdef SSR_AO 2417 vec4 rand = texelfetch_noise_tex(gl_FragCoord.xy); 2418 vec3 bent_normal; 2419 float final_ao = occlusion_compute(N, viewPosition, 1.0, rand, bent_normal); 2420 final_ao = specular_occlusion(dot(N, V), final_ao, roughness); 2421 #else 2422 const float final_ao = 1.0; 2423 #endif 2424 2425 /* Starts at 1 because 0 is world probe */ 2426 for (int i = 1; i < MAX_PROBE && i < prbNumRenderCube && spec_accum.a < 0.999; i++) { 2427 float fade = probe_attenuation_cube(i, W); 2428 2429 if (fade > 0.0) { 2430 vec3 spec = final_ao * probe_evaluate_cube(i, W, spec_dir, roughness); 2431 accumulate_light(spec, fade, spec_accum); 2432 } 2433 } 2434 2435 /* World Specular */ 2436 if (spec_accum.a < 0.999) { 2437 vec3 spec = final_ao * probe_evaluate_world_spec(spec_dir, roughness); 2438 accumulate_light(spec, 1.0, spec_accum); 2439 } 2440 } 2441 2442 #ifdef IRRADIANCE_LIB 2443 vec3 probe_evaluate_grid(GridData gd, vec3 W, vec3 N, vec3 localpos) 2444 { 2445 localpos = localpos * 0.5 + 0.5; 2446 localpos = localpos * vec3(gd.g_resolution) - 0.5; 2447 2448 vec3 localpos_floored = floor(localpos); 2449 vec3 trilinear_weight = fract(localpos); 2450 2451 float weight_accum = 0.0; 2452 vec3 irradiance_accum = vec3(0.0); 2453 2454 /* For each neighbor cells */ 2455 for (int i = 0; i < 8; i++) { 2456 ivec3 offset = ivec3(i, i >> 1, i >> 2) & ivec3(1); 2457 vec3 cell_cos = clamp(localpos_floored + vec3(offset), vec3(0.0), vec3(gd.g_resolution) - 1.0); 2458 2459 /* Keep in sync with update_irradiance_probe */ 2460 ivec3 icell_cos = ivec3(gd.g_level_bias * floor(cell_cos / gd.g_level_bias)); 2461 int cell = gd.g_offset + icell_cos.z + icell_cos.y * gd.g_resolution.z + 2462 icell_cos.x * gd.g_resolution.z * gd.g_resolution.y; 2463 2464 vec3 color = irradiance_from_cell_get(cell, N); 2465 2466 /* We need this because we render probes in world space (so we need light vector in WS). 2467 * And rendering them in local probe space is too much problem. */ 2468 vec3 ws_cell_location = gd.g_corner + 2469 (gd.g_increment_x * cell_cos.x + gd.g_increment_y * cell_cos.y + 2470 gd.g_increment_z * cell_cos.z); 2471 2472 vec3 ws_point_to_cell = ws_cell_location - W; 2473 float ws_dist_point_to_cell = length(ws_point_to_cell); 2474 vec3 ws_light = ws_point_to_cell / ws_dist_point_to_cell; 2475 2476 /* Smooth backface test */ 2477 float weight = saturate(dot(ws_light, N)); 2478 2479 /* Precomputed visibility */ 2480 weight *= load_visibility_cell( 2481 cell, ws_light, ws_dist_point_to_cell, gd.g_vis_bias, gd.g_vis_bleed, gd.g_vis_range); 2482 2483 /* Smoother transition */ 2484 weight += prbIrradianceSmooth; 2485 2486 /* Trilinear weights */ 2487 vec3 trilinear = mix(1.0 - trilinear_weight, trilinear_weight, offset); 2488 weight *= trilinear.x * trilinear.y * trilinear.z; 2489 2490 /* Avoid zero weight */ 2491 weight = max(0.00001, weight); 2492 2493 weight_accum += weight; 2494 irradiance_accum += color * weight; 2495 } 2496 2497 return irradiance_accum / weight_accum; 2498 } 2499 2500 vec3 probe_evaluate_world_diff(vec3 N) 2501 { 2502 return irradiance_from_cell_get(0, N); 2503 } 2504 2505 #endif /* IRRADIANCE_LIB */ 2506 /** 2507 * Adapted from : 2508 * Real-Time Polygonal-Light Shading with Linearly Transformed Cosines. 2509 * Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt. 2510 * ACM Transactions on Graphics (Proceedings of ACM SIGGRAPH 2016) 35(4), 2016. 2511 * Project page: https://eheitzresearch.wordpress.com/415-2/ 2512 */ 2513 2514 #define USE_LTC 2515 2516 #ifndef UTIL_TEX 2517 # define UTIL_TEX 2518 uniform sampler2DArray utilTex; 2519 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 2520 #endif /* UTIL_TEX */ 2521 2522 /* Diffuse *clipped* sphere integral. */ 2523 float diffuse_sphere_integral(float avg_dir_z, float form_factor) 2524 { 2525 #if 1 2526 /* use tabulated horizon-clipped sphere */ 2527 vec2 uv = vec2(avg_dir_z * 0.5 + 0.5, form_factor); 2528 uv = uv * (LUT_SIZE - 1.0) / LUT_SIZE + 0.5 / LUT_SIZE; 2529 2530 return texture(utilTex, vec3(uv, 3.0)).x; 2531 #else 2532 /* Cheap approximation. Less smooth and have energy issues. */ 2533 return max((form_factor * form_factor + avg_dir_z) / (form_factor + 1.0), 0.0); 2534 #endif 2535 } 2536 2537 /** 2538 * An extended version of the implementation from 2539 * "How to solve a cubic equation, revisited" 2540 * http://momentsingraphics.de/?p=105 2541 */ 2542 vec3 solve_cubic(vec4 coefs) 2543 { 2544 /* Normalize the polynomial */ 2545 coefs.xyz /= coefs.w; 2546 /* Divide middle coefficients by three */ 2547 coefs.yz /= 3.0; 2548 2549 float A = coefs.w; 2550 float B = coefs.z; 2551 float C = coefs.y; 2552 float D = coefs.x; 2553 2554 /* Compute the Hessian and the discriminant */ 2555 vec3 delta = vec3(-coefs.zy * coefs.zz + coefs.yx, dot(vec2(coefs.z, -coefs.y), coefs.xy)); 2556 2557 /* Discriminant */ 2558 float discr = dot(vec2(4.0 * delta.x, -delta.y), delta.zy); 2559 2560 /* Clamping avoid NaN output on some platform. (see T67060) */ 2561 float sqrt_discr = sqrt(clamp(discr, 0.0, FLT_MAX)); 2562 2563 vec2 xlc, xsc; 2564 2565 /* Algorithm A */ 2566 { 2567 float A_a = 1.0; 2568 float C_a = delta.x; 2569 float D_a = -2.0 * B * delta.x + delta.y; 2570 2571 /* Take the cubic root of a normalized complex number */ 2572 float theta = atan(sqrt_discr, -D_a) / 3.0; 2573 2574 float _2_sqrt_C_a = 2.0 * sqrt(-C_a); 2575 float x_1a = _2_sqrt_C_a * cos(theta); 2576 float x_3a = _2_sqrt_C_a * cos(theta + (2.0 / 3.0) * M_PI); 2577 2578 float xl; 2579 if ((x_1a + x_3a) > 2.0 * B) { 2580 xl = x_1a; 2581 } 2582 else { 2583 xl = x_3a; 2584 } 2585 2586 xlc = vec2(xl - B, A); 2587 } 2588 2589 /* Algorithm D */ 2590 { 2591 float A_d = D; 2592 float C_d = delta.z; 2593 float D_d = -D * delta.y + 2.0 * C * delta.z; 2594 2595 /* Take the cubic root of a normalized complex number */ 2596 float theta = atan(D * sqrt_discr, -D_d) / 3.0; 2597 2598 float _2_sqrt_C_d = 2.0 * sqrt(-C_d); 2599 float x_1d = _2_sqrt_C_d * cos(theta); 2600 float x_3d = _2_sqrt_C_d * cos(theta + (2.0 / 3.0) * M_PI); 2601 2602 float xs; 2603 if (x_1d + x_3d < 2.0 * C) { 2604 xs = x_1d; 2605 } 2606 else { 2607 xs = x_3d; 2608 } 2609 2610 xsc = vec2(-D, xs + C); 2611 } 2612 2613 float E = xlc.y * xsc.y; 2614 float F = -xlc.x * xsc.y - xlc.y * xsc.x; 2615 float G = xlc.x * xsc.x; 2616 2617 vec2 xmc = vec2(C * F - B * G, -B * F + C * E); 2618 2619 vec3 root = vec3(xsc.x / xsc.y, xmc.x / xmc.y, xlc.x / xlc.y); 2620 2621 if (root.x < root.y && root.x < root.z) { 2622 root.xyz = root.yxz; 2623 } 2624 else if (root.z < root.x && root.z < root.y) { 2625 root.xyz = root.xzy; 2626 } 2627 2628 return root; 2629 } 2630 2631 /* from Real-Time Area Lighting: a Journey from Research to Production 2632 * Stephen Hill and Eric Heitz */ 2633 vec3 edge_integral_vec(vec3 v1, vec3 v2) 2634 { 2635 float x = dot(v1, v2); 2636 float y = abs(x); 2637 2638 float a = 0.8543985 + (0.4965155 + 0.0145206 * y) * y; 2639 float b = 3.4175940 + (4.1616724 + y) * y; 2640 float v = a / b; 2641 2642 float theta_sintheta = (x > 0.0) ? v : 0.5 * inversesqrt(max(1.0 - x * x, 1e-7)) - v; 2643 2644 return cross(v1, v2) * theta_sintheta; 2645 } 2646 2647 mat3 ltc_matrix(vec4 lut) 2648 { 2649 /* load inverse matrix */ 2650 mat3 Minv = mat3(vec3(lut.x, 0, lut.y), vec3(0, 1, 0), vec3(lut.z, 0, lut.w)); 2651 2652 return Minv; 2653 } 2654 2655 void ltc_transform_quad(vec3 N, vec3 V, mat3 Minv, inout vec3 corners[4]) 2656 { 2657 /* Avoid dot(N, V) == 1 in ortho mode, leading T1 normalize to fail. */ 2658 V = normalize(V + 1e-8); 2659 2660 /* construct orthonormal basis around N */ 2661 vec3 T1, T2; 2662 T1 = normalize(V - N * dot(N, V)); 2663 T2 = cross(N, T1); 2664 2665 /* rotate area light in (T1, T2, R) basis */ 2666 Minv = Minv * transpose(mat3(T1, T2, N)); 2667 2668 /* Apply LTC inverse matrix. */ 2669 corners[0] = normalize(Minv * corners[0]); 2670 corners[1] = normalize(Minv * corners[1]); 2671 corners[2] = normalize(Minv * corners[2]); 2672 corners[3] = normalize(Minv * corners[3]); 2673 } 2674 2675 /* If corners have already pass through ltc_transform_quad(), 2676 * then N **MUST** be vec3(0.0, 0.0, 1.0), corresponding to the Up axis of the shading basis. */ 2677 float ltc_evaluate_quad(vec3 corners[4], vec3 N) 2678 { 2679 /* Approximation using a sphere of the same solid angle than the quad. 2680 * Finding the clipped sphere diffuse integral is easier than clipping the quad. */ 2681 vec3 avg_dir; 2682 avg_dir = edge_integral_vec(corners[0], corners[1]); 2683 avg_dir += edge_integral_vec(corners[1], corners[2]); 2684 avg_dir += edge_integral_vec(corners[2], corners[3]); 2685 avg_dir += edge_integral_vec(corners[3], corners[0]); 2686 2687 float form_factor = length(avg_dir); 2688 float avg_dir_z = dot(N, avg_dir / form_factor); 2689 return form_factor * diffuse_sphere_integral(avg_dir_z, form_factor); 2690 } 2691 2692 /* If disk does not need to be transformed and is already front facing. */ 2693 float ltc_evaluate_disk_simple(float disk_radius, float NL) 2694 { 2695 float r_sqr = disk_radius * disk_radius; 2696 float one_r_sqr = 1.0 + r_sqr; 2697 float form_factor = r_sqr * inversesqrt(one_r_sqr * one_r_sqr); 2698 return form_factor * diffuse_sphere_integral(NL, form_factor); 2699 } 2700 2701 /* disk_points are WS vectors from the shading point to the disk "bounding domain" */ 2702 float ltc_evaluate_disk(vec3 N, vec3 V, mat3 Minv, vec3 disk_points[3]) 2703 { 2704 /* Avoid dot(N, V) == 1 in ortho mode, leading T1 normalize to fail. */ 2705 V = normalize(V + 1e-8); 2706 2707 /* construct orthonormal basis around N */ 2708 vec3 T1, T2; 2709 T1 = normalize(V - N * dot(V, N)); 2710 T2 = cross(N, T1); 2711 2712 /* rotate area light in (T1, T2, R) basis */ 2713 mat3 R = transpose(mat3(T1, T2, N)); 2714 2715 /* Intermediate step: init ellipse. */ 2716 vec3 L_[3]; 2717 L_[0] = mul(R, disk_points[0]); 2718 L_[1] = mul(R, disk_points[1]); 2719 L_[2] = mul(R, disk_points[2]); 2720 2721 vec3 C = 0.5 * (L_[0] + L_[2]); 2722 vec3 V1 = 0.5 * (L_[1] - L_[2]); 2723 vec3 V2 = 0.5 * (L_[1] - L_[0]); 2724 2725 /* Transform ellipse by Minv. */ 2726 C = Minv * C; 2727 V1 = Minv * V1; 2728 V2 = Minv * V2; 2729 2730 /* Compute eigenvectors of new ellipse. */ 2731 2732 float d11 = dot(V1, V1); 2733 float d22 = dot(V2, V2); 2734 float d12 = dot(V1, V2); 2735 float a, b; /* Eigenvalues */ 2736 const float threshold = 0.0007; /* Can be adjusted. Fix artifacts. */ 2737 if (abs(d12) / sqrt(d11 * d22) > threshold) { 2738 float tr = d11 + d22; 2739 float det = -d12 * d12 + d11 * d22; 2740 2741 /* use sqrt matrix to solve for eigenvalues */ 2742 det = sqrt(det); 2743 float u = 0.5 * sqrt(tr - 2.0 * det); 2744 float v = 0.5 * sqrt(tr + 2.0 * det); 2745 float e_max = (u + v); 2746 float e_min = (u - v); 2747 e_max *= e_max; 2748 e_min *= e_min; 2749 2750 vec3 V1_, V2_; 2751 if (d11 > d22) { 2752 V1_ = d12 * V1 + (e_max - d11) * V2; 2753 V2_ = d12 * V1 + (e_min - d11) * V2; 2754 } 2755 else { 2756 V1_ = d12 * V2 + (e_max - d22) * V1; 2757 V2_ = d12 * V2 + (e_min - d22) * V1; 2758 } 2759 2760 a = 1.0 / e_max; 2761 b = 1.0 / e_min; 2762 V1 = normalize(V1_); 2763 V2 = normalize(V2_); 2764 } 2765 else { 2766 a = 1.0 / d11; 2767 b = 1.0 / d22; 2768 V1 *= sqrt(a); 2769 V2 *= sqrt(b); 2770 } 2771 2772 /* Now find front facing ellipse with same solid angle. */ 2773 2774 vec3 V3 = normalize(cross(V1, V2)); 2775 if (dot(C, V3) < 0.0) { 2776 V3 *= -1.0; 2777 } 2778 2779 float L = dot(V3, C); 2780 float inv_L = 1.0 / L; 2781 float x0 = dot(V1, C) * inv_L; 2782 float y0 = dot(V2, C) * inv_L; 2783 2784 float L_sqr = L * L; 2785 a *= L_sqr; 2786 b *= L_sqr; 2787 2788 float t = 1.0 + x0 * x0; 2789 float c0 = a * b; 2790 float c1 = c0 * (t + y0 * y0) - a - b; 2791 float c2 = (1.0 - a * t) - b * (1.0 + y0 * y0); 2792 float c3 = 1.0; 2793 2794 vec3 roots = solve_cubic(vec4(c0, c1, c2, c3)); 2795 float e1 = roots.x; 2796 float e2 = roots.y; 2797 float e3 = roots.z; 2798 2799 vec3 avg_dir = vec3(a * x0 / (a - e2), b * y0 / (b - e2), 1.0); 2800 2801 mat3 rotate = mat3(V1, V2, V3); 2802 2803 avg_dir = rotate * avg_dir; 2804 avg_dir = normalize(avg_dir); 2805 2806 /* L1, L2 are the extends of the front facing ellipse. */ 2807 float L1 = sqrt(-e2 / e3); 2808 float L2 = sqrt(-e2 / e1); 2809 2810 /* Find the sphere and compute lighting. */ 2811 float form_factor = max(0.0, L1 * L2 * inversesqrt((1.0 + L1 * L1) * (1.0 + L2 * L2))); 2812 return form_factor * diffuse_sphere_integral(avg_dir.z, form_factor); 2813 } 2814 2815 uniform sampler2DArrayShadow shadowCubeTexture; 2816 uniform sampler2DArrayShadow shadowCascadeTexture; 2817 2818 #define LAMPS_LIB 2819 2820 layout(std140) uniform shadow_block 2821 { 2822 ShadowData shadows_data[MAX_SHADOW]; 2823 ShadowCubeData shadows_cube_data[MAX_SHADOW_CUBE]; 2824 ShadowCascadeData shadows_cascade_data[MAX_SHADOW_CASCADE]; 2825 }; 2826 2827 layout(std140) uniform light_block 2828 { 2829 LightData lights_data[MAX_LIGHT]; 2830 }; 2831 2832 /* type */ 2833 #define POINT 0.0 2834 #define SUN 1.0 2835 #define SPOT 2.0 2836 #define AREA_RECT 4.0 2837 /* Used to define the area light shape, doesn't directly correspond to a Blender light type. */ 2838 #define AREA_ELLIPSE 100.0 2839 2840 float cubeFaceIndexEEVEE(vec3 P) 2841 { 2842 vec3 aP = abs(P); 2843 if (all(greaterThan(aP.xx, aP.yz))) { 2844 return (P.x > 0.0) ? 0.0 : 1.0; 2845 } 2846 else if (all(greaterThan(aP.yy, aP.xz))) { 2847 return (P.y > 0.0) ? 2.0 : 3.0; 2848 } 2849 else { 2850 return (P.z > 0.0) ? 4.0 : 5.0; 2851 } 2852 } 2853 2854 vec2 cubeFaceCoordEEVEE(vec3 P, float face, float scale) 2855 { 2856 if (face < 2.0) { 2857 return (P.zy / P.x) * scale * vec2(-0.5, -sign(P.x) * 0.5) + 0.5; 2858 } 2859 else if (face < 4.0) { 2860 return (P.xz / P.y) * scale * vec2(sign(P.y) * 0.5, 0.5) + 0.5; 2861 } 2862 else { 2863 return (P.xy / P.z) * scale * vec2(0.5, -sign(P.z) * 0.5) + 0.5; 2864 } 2865 } 2866 2867 vec2 cubeFaceCoordEEVEE(vec3 P, float face, sampler2DArray tex) 2868 { 2869 /* Scaling to compensate the 1px border around the face. */ 2870 float cube_res = float(textureSize(tex, 0).x); 2871 float scale = (cube_res) / (cube_res + 1.0); 2872 return cubeFaceCoordEEVEE(P, face, scale); 2873 } 2874 2875 vec2 cubeFaceCoordEEVEE(vec3 P, float face, sampler2DArrayShadow tex) 2876 { 2877 /* Scaling to compensate the 1px border around the face. */ 2878 float cube_res = float(textureSize(tex, 0).x); 2879 float scale = (cube_res) / (cube_res + 1.0); 2880 return cubeFaceCoordEEVEE(P, face, scale); 2881 } 2882 2883 vec4 sample_cube(sampler2DArray tex, vec3 cubevec, float cube) 2884 { 2885 /* Manual Shadow Cube Layer indexing. */ 2886 float face = cubeFaceIndexEEVEE(cubevec); 2887 vec2 uv = cubeFaceCoordEEVEE(cubevec, face, tex); 2888 2889 vec3 coord = vec3(uv, cube * 6.0 + face); 2890 return texture(tex, coord); 2891 } 2892 2893 vec4 sample_cascade(sampler2DArray tex, vec2 co, float cascade_id) 2894 { 2895 return texture(tex, vec3(co, cascade_id)); 2896 } 2897 2898 /* Some driver poorly optimize this code. Use direct reference to matrices. */ 2899 #define sd(x) shadows_data[x] 2900 #define scube(x) shadows_cube_data[x] 2901 #define scascade(x) shadows_cascade_data[x] 2902 2903 float sample_cube_shadow(int shadow_id, vec3 W) 2904 { 2905 int data_id = int(sd(shadow_id).sh_data_index); 2906 vec3 cubevec = transform_point(scube(data_id).shadowmat, W); 2907 float dist = max(sd(shadow_id).sh_near, max_v3(abs(cubevec)) - sd(shadow_id).sh_bias); 2908 dist = buffer_depth(true, dist, sd(shadow_id).sh_far, sd(shadow_id).sh_near); 2909 /* Manual Shadow Cube Layer indexing. */ 2910 /* TODO Shadow Cube Array. */ 2911 float face = cubeFaceIndexEEVEE(cubevec); 2912 vec2 coord = cubeFaceCoordEEVEE(cubevec, face, shadowCubeTexture); 2913 /* tex_id == data_id for cube shadowmap */ 2914 float tex_id = float(data_id); 2915 return texture(shadowCubeTexture, vec4(coord, tex_id * 6.0 + face, dist)); 2916 } 2917 2918 float sample_cascade_shadow(int shadow_id, vec3 W) 2919 { 2920 int data_id = int(sd(shadow_id).sh_data_index); 2921 float tex_id = scascade(data_id).sh_tex_index; 2922 vec4 view_z = vec4(dot(W - cameraPos, cameraForward)); 2923 vec4 weights = 1.0 - smoothstep(scascade(data_id).split_end_distances, 2924 scascade(data_id).split_start_distances.yzwx, 2925 view_z); 2926 float tot_weight = dot(weights.xyz, vec3(1.0)); 2927 2928 int cascade = int(clamp(tot_weight, 0.0, 3.0)); 2929 float blend = fract(tot_weight); 2930 float vis = weights.w; 2931 vec4 coord, shpos; 2932 /* Main cascade. */ 2933 shpos = scascade(data_id).shadowmat[cascade] * vec4(W, 1.0); 2934 coord = vec4(shpos.xy, tex_id + float(cascade), shpos.z - sd(shadow_id).sh_bias); 2935 vis += texture(shadowCascadeTexture, coord) * (1.0 - blend); 2936 2937 cascade = min(3, cascade + 1); 2938 /* Second cascade. */ 2939 shpos = scascade(data_id).shadowmat[cascade] * vec4(W, 1.0); 2940 coord = vec4(shpos.xy, tex_id + float(cascade), shpos.z - sd(shadow_id).sh_bias); 2941 vis += texture(shadowCascadeTexture, coord) * blend; 2942 2943 return saturate(vis); 2944 } 2945 #undef sd 2946 #undef scube 2947 #undef scsmd 2948 2949 /* ----------------------------------------------------------- */ 2950 /* --------------------- Light Functions --------------------- */ 2951 /* ----------------------------------------------------------- */ 2952 2953 /* From Frostbite PBR Course 2954 * Distance based attenuation 2955 * http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */ 2956 float distance_attenuation(float dist_sqr, float inv_sqr_influence) 2957 { 2958 float factor = dist_sqr * inv_sqr_influence; 2959 float fac = saturate(1.0 - factor * factor); 2960 return fac * fac; 2961 } 2962 2963 float spot_attenuation(LightData ld, vec3 l_vector) 2964 { 2965 float z = dot(ld.l_forward, l_vector.xyz); 2966 vec3 lL = l_vector.xyz / z; 2967 float x = dot(ld.l_right, lL) / ld.l_sizex; 2968 float y = dot(ld.l_up, lL) / ld.l_sizey; 2969 float ellipse = inversesqrt(1.0 + x * x + y * y); 2970 float spotmask = smoothstep(0.0, 1.0, (ellipse - ld.l_spot_size) / ld.l_spot_blend); 2971 return spotmask; 2972 } 2973 2974 float light_attenuation(LightData ld, vec4 l_vector) 2975 { 2976 float vis = 1.0; 2977 if (ld.l_type == SPOT) { 2978 vis *= spot_attenuation(ld, l_vector.xyz); 2979 } 2980 if (ld.l_type >= SPOT) { 2981 vis *= step(0.0, -dot(l_vector.xyz, ld.l_forward)); 2982 } 2983 if (ld.l_type != SUN) { 2984 vis *= distance_attenuation(l_vector.w * l_vector.w, ld.l_influence); 2985 } 2986 return vis; 2987 } 2988 2989 float light_visibility(LightData ld, 2990 vec3 W, 2991 #ifndef VOLUMETRICS 2992 vec3 viewPosition, 2993 float tracing_depth, 2994 vec3 true_normal, 2995 float rand_x, 2996 const bool use_contact_shadows, 2997 #endif 2998 vec4 l_vector) 2999 { 3000 float vis = light_attenuation(ld, l_vector); 3001 3002 #if !defined(VOLUMETRICS) || defined(VOLUME_SHADOW) 3003 /* shadowing */ 3004 if (ld.l_shadowid >= 0.0 && vis > 0.001) { 3005 3006 if (ld.l_type == SUN) { 3007 vis *= sample_cascade_shadow(int(ld.l_shadowid), W); 3008 } 3009 else { 3010 vis *= sample_cube_shadow(int(ld.l_shadowid), W); 3011 } 3012 3013 # ifndef VOLUMETRICS 3014 ShadowData sd = shadows_data[int(ld.l_shadowid)]; 3015 /* Only compute if not already in shadow. */ 3016 if (use_contact_shadows && sd.sh_contact_dist > 0.0 && vis > 1e-8) { 3017 /* Contact Shadows. */ 3018 vec3 ray_ori, ray_dir; 3019 float trace_distance; 3020 3021 if (ld.l_type == SUN) { 3022 trace_distance = sd.sh_contact_dist; 3023 ray_dir = shadows_cascade_data[int(sd.sh_data_index)].sh_shadow_vec * trace_distance; 3024 } 3025 else { 3026 ray_dir = shadows_cube_data[int(sd.sh_data_index)].position.xyz - W; 3027 float len = length(ray_dir); 3028 trace_distance = min(sd.sh_contact_dist, len); 3029 ray_dir *= trace_distance / len; 3030 } 3031 3032 ray_dir = transform_direction(ViewMatrix, ray_dir); 3033 ray_ori = vec3(viewPosition.xy, tracing_depth) + true_normal * sd.sh_contact_offset; 3034 3035 vec3 hit_pos = raycast( 3036 -1, ray_ori, ray_dir, sd.sh_contact_thickness, rand_x, 0.1, 0.001, false); 3037 3038 if (hit_pos.z > 0.0) { 3039 hit_pos = get_view_space_from_depth(hit_pos.xy, hit_pos.z); 3040 float hit_dist = distance(viewPosition, hit_pos); 3041 float dist_ratio = hit_dist / trace_distance; 3042 return vis * saturate(dist_ratio * 3.0 - 2.0); 3043 } 3044 } 3045 # endif /* VOLUMETRICS */ 3046 } 3047 #endif 3048 3049 return vis; 3050 } 3051 3052 #ifdef USE_LTC 3053 float light_diffuse(LightData ld, vec3 N, vec3 V, vec4 l_vector) 3054 { 3055 if (ld.l_type == AREA_RECT) { 3056 vec3 corners[4]; 3057 corners[0] = normalize((l_vector.xyz + ld.l_right * -ld.l_sizex) + ld.l_up * ld.l_sizey); 3058 corners[1] = normalize((l_vector.xyz + ld.l_right * -ld.l_sizex) + ld.l_up * -ld.l_sizey); 3059 corners[2] = normalize((l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * -ld.l_sizey); 3060 corners[3] = normalize((l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * ld.l_sizey); 3061 3062 return ltc_evaluate_quad(corners, N); 3063 } 3064 else if (ld.l_type == AREA_ELLIPSE) { 3065 vec3 points[3]; 3066 points[0] = (l_vector.xyz + ld.l_right * -ld.l_sizex) + ld.l_up * -ld.l_sizey; 3067 points[1] = (l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * -ld.l_sizey; 3068 points[2] = (l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * ld.l_sizey; 3069 3070 return ltc_evaluate_disk(N, V, mat3(1.0), points); 3071 } 3072 else { 3073 float radius = ld.l_radius; 3074 radius /= (ld.l_type == SUN) ? 1.0 : l_vector.w; 3075 vec3 L = (ld.l_type == SUN) ? -ld.l_forward : (l_vector.xyz / l_vector.w); 3076 3077 return ltc_evaluate_disk_simple(radius, dot(N, L)); 3078 } 3079 } 3080 3081 float light_specular(LightData ld, vec4 ltc_mat, vec3 N, vec3 V, vec4 l_vector) 3082 { 3083 if (ld.l_type == AREA_RECT) { 3084 vec3 corners[4]; 3085 corners[0] = (l_vector.xyz + ld.l_right * -ld.l_sizex) + ld.l_up * ld.l_sizey; 3086 corners[1] = (l_vector.xyz + ld.l_right * -ld.l_sizex) + ld.l_up * -ld.l_sizey; 3087 corners[2] = (l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * -ld.l_sizey; 3088 corners[3] = (l_vector.xyz + ld.l_right * ld.l_sizex) + ld.l_up * ld.l_sizey; 3089 3090 ltc_transform_quad(N, V, ltc_matrix(ltc_mat), corners); 3091 3092 return ltc_evaluate_quad(corners, vec3(0.0, 0.0, 1.0)); 3093 } 3094 else { 3095 bool is_ellipse = (ld.l_type == AREA_ELLIPSE); 3096 float radius_x = is_ellipse ? ld.l_sizex : ld.l_radius; 3097 float radius_y = is_ellipse ? ld.l_sizey : ld.l_radius; 3098 3099 vec3 L = (ld.l_type == SUN) ? -ld.l_forward : l_vector.xyz; 3100 vec3 Px = ld.l_right; 3101 vec3 Py = ld.l_up; 3102 3103 if (ld.l_type == SPOT || ld.l_type == POINT) { 3104 make_orthonormal_basis(l_vector.xyz / l_vector.w, Px, Py); 3105 } 3106 3107 vec3 points[3]; 3108 points[0] = (L + Px * -radius_x) + Py * -radius_y; 3109 points[1] = (L + Px * radius_x) + Py * -radius_y; 3110 points[2] = (L + Px * radius_x) + Py * radius_y; 3111 3112 return ltc_evaluate_disk(N, V, ltc_matrix(ltc_mat), points); 3113 } 3114 } 3115 #endif 3116 3117 #ifndef LIT_SURFACE_UNIFORM 3118 # define LIT_SURFACE_UNIFORM 3119 3120 uniform float refractionDepth; 3121 3122 # ifndef UTIL_TEX 3123 # define UTIL_TEX 3124 uniform sampler2DArray utilTex; 3125 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 3126 # endif /* UTIL_TEX */ 3127 3128 in vec3 worldPosition; 3129 in vec3 viewPosition; 3130 3131 in vec3 worldNormal; 3132 in vec3 viewNormal; 3133 3134 # ifdef HAIR_SHADER 3135 in vec3 hairTangent; /* world space */ 3136 in float hairThickTime; 3137 in float hairThickness; 3138 in float hairTime; 3139 flat in int hairStrandID; 3140 3141 uniform int hairThicknessRes = 1; 3142 # endif 3143 3144 #endif /* LIT_SURFACE_UNIFORM */ 3145 3146 /** 3147 * AUTO CONFIG 3148 * We include the file multiple times each time with a different configuration. 3149 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 3150 */ 3151 #if !defined(SURFACE_DEFAULT) 3152 # define SURFACE_DEFAULT 3153 # define CLOSURE_NAME eevee_closure_default 3154 # define CLOSURE_DIFFUSE 3155 # define CLOSURE_GLOSSY 3156 #endif /* SURFACE_DEFAULT */ 3157 3158 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 3159 # define SURFACE_DEFAULT_CLEARCOAT 3160 # define CLOSURE_NAME eevee_closure_default_clearcoat 3161 # define CLOSURE_DIFFUSE 3162 # define CLOSURE_GLOSSY 3163 # define CLOSURE_CLEARCOAT 3164 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 3165 3166 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 3167 # define SURFACE_PRINCIPLED 3168 # define CLOSURE_NAME eevee_closure_principled 3169 # define CLOSURE_DIFFUSE 3170 # define CLOSURE_GLOSSY 3171 # define CLOSURE_CLEARCOAT 3172 # define CLOSURE_REFRACTION 3173 # define CLOSURE_SUBSURFACE 3174 #endif /* SURFACE_PRINCIPLED */ 3175 3176 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 3177 # define SURFACE_CLEARCOAT 3178 # define CLOSURE_NAME eevee_closure_clearcoat 3179 # define CLOSURE_GLOSSY 3180 # define CLOSURE_CLEARCOAT 3181 #endif /* SURFACE_CLEARCOAT */ 3182 3183 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 3184 # define SURFACE_DIFFUSE 3185 # define CLOSURE_NAME eevee_closure_diffuse 3186 # define CLOSURE_DIFFUSE 3187 #endif /* SURFACE_DIFFUSE */ 3188 3189 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 3190 # define SURFACE_SUBSURFACE 3191 # define CLOSURE_NAME eevee_closure_subsurface 3192 # define CLOSURE_DIFFUSE 3193 # define CLOSURE_SUBSURFACE 3194 #endif /* SURFACE_SUBSURFACE */ 3195 3196 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 3197 # define SURFACE_SKIN 3198 # define CLOSURE_NAME eevee_closure_skin 3199 # define CLOSURE_DIFFUSE 3200 # define CLOSURE_SUBSURFACE 3201 # define CLOSURE_GLOSSY 3202 #endif /* SURFACE_SKIN */ 3203 3204 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 3205 # define SURFACE_GLOSSY 3206 # define CLOSURE_NAME eevee_closure_glossy 3207 # define CLOSURE_GLOSSY 3208 #endif /* SURFACE_GLOSSY */ 3209 3210 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 3211 # define SURFACE_REFRACT 3212 # define CLOSURE_NAME eevee_closure_refraction 3213 # define CLOSURE_REFRACTION 3214 #endif /* SURFACE_REFRACT */ 3215 3216 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 3217 # define SURFACE_GLASS 3218 # define CLOSURE_NAME eevee_closure_glass 3219 # define CLOSURE_GLOSSY 3220 # define CLOSURE_REFRACTION 3221 #endif /* SURFACE_GLASS */ 3222 3223 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 3224 #ifdef CLOSURE_CLEARCOAT 3225 # ifndef CLOSURE_GLOSSY 3226 # define CLOSURE_GLOSSY 3227 # endif 3228 #endif /* CLOSURE_CLEARCOAT */ 3229 3230 void CLOSURE_NAME(vec3 N 3231 #ifdef CLOSURE_DIFFUSE 3232 , 3233 vec3 albedo 3234 #endif 3235 #ifdef CLOSURE_GLOSSY 3236 , 3237 vec3 f0, 3238 vec3 f90, 3239 int ssr_id 3240 #endif 3241 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 3242 , 3243 float roughness 3244 #endif 3245 #ifdef CLOSURE_CLEARCOAT 3246 , 3247 vec3 C_N, 3248 float C_intensity, 3249 float C_roughness 3250 #endif 3251 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 3252 , 3253 float ao 3254 #endif 3255 #ifdef CLOSURE_SUBSURFACE 3256 , 3257 float sss_scale 3258 #endif 3259 #ifdef CLOSURE_REFRACTION 3260 , 3261 float ior 3262 #endif 3263 , 3264 const bool use_contact_shadows 3265 #ifdef CLOSURE_DIFFUSE 3266 , 3267 out vec3 out_diff 3268 #endif 3269 #ifdef CLOSURE_GLOSSY 3270 , 3271 out vec3 out_spec 3272 #endif 3273 #ifdef CLOSURE_REFRACTION 3274 , 3275 out vec3 out_refr 3276 #endif 3277 #ifdef CLOSURE_GLOSSY 3278 , 3279 out vec3 ssr_spec 3280 #endif 3281 ) 3282 { 3283 #ifdef CLOSURE_DIFFUSE 3284 out_diff = vec3(0.0); 3285 #endif 3286 3287 #ifdef CLOSURE_GLOSSY 3288 out_spec = vec3(0.0); 3289 #endif 3290 3291 #ifdef CLOSURE_REFRACTION 3292 out_refr = vec3(0.0); 3293 #endif 3294 3295 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 3296 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 3297 return; 3298 #else 3299 3300 /* Zero length vectors cause issues, see: T51979. */ 3301 float len = length(N); 3302 if (isnan(len)) { 3303 return; 3304 } 3305 N /= len; 3306 3307 # ifdef CLOSURE_CLEARCOAT 3308 len = length(C_N); 3309 if (isnan(len)) { 3310 return; 3311 } 3312 C_N /= len; 3313 # endif 3314 3315 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 3316 roughness = clamp(roughness, 1e-8, 0.9999); 3317 float roughnessSquared = roughness * roughness; 3318 # endif 3319 3320 # ifdef CLOSURE_CLEARCOAT 3321 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 3322 float C_roughnessSquared = C_roughness * C_roughness; 3323 # endif 3324 3325 vec3 V = cameraVec; 3326 3327 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 3328 3329 /* ---------------------------------------------------------------- */ 3330 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 3331 /* ---------------------------------------------------------------- */ 3332 3333 # ifdef CLOSURE_GLOSSY 3334 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 3335 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 3336 # endif 3337 3338 # ifdef CLOSURE_CLEARCOAT 3339 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 3340 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 3341 vec3 out_spec_clear = vec3(0.0); 3342 # endif 3343 3344 float tracing_depth = gl_FragCoord.z; 3345 /* Constant bias (due to depth buffer precision) */ 3346 /* Magic numbers for 24bits of precision. 3347 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 3348 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 3349 /* Convert to view Z. */ 3350 tracing_depth = get_view_z_from_depth(tracing_depth); 3351 3352 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 3353 3354 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 3355 LightData ld = lights_data[i]; 3356 3357 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 3358 l_vector.xyz = ld.l_position - worldPosition; 3359 l_vector.w = length(l_vector.xyz); 3360 3361 float l_vis = light_visibility(ld, 3362 worldPosition, 3363 viewPosition, 3364 tracing_depth, 3365 true_normal, 3366 rand.x, 3367 use_contact_shadows, 3368 l_vector); 3369 3370 if (l_vis < 1e-8) { 3371 continue; 3372 } 3373 3374 vec3 l_color_vis = ld.l_color * l_vis; 3375 3376 # ifdef CLOSURE_DIFFUSE 3377 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 3378 # endif 3379 3380 # ifdef CLOSURE_GLOSSY 3381 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 3382 # endif 3383 3384 # ifdef CLOSURE_CLEARCOAT 3385 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 3386 ld.l_spec; 3387 # endif 3388 } 3389 3390 # ifdef CLOSURE_GLOSSY 3391 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 3392 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 3393 # endif 3394 3395 # ifdef CLOSURE_CLEARCOAT 3396 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 3397 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 3398 out_spec += out_spec_clear * C_intensity; 3399 # endif 3400 3401 /* ---------------------------------------------------------------- */ 3402 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 3403 /* ---------------------------------------------------------------- */ 3404 3405 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 3406 * BRDF. */ 3407 # ifdef CLOSURE_GLOSSY 3408 vec4 spec_accum = vec4(0.0); 3409 # endif 3410 3411 # ifdef CLOSURE_CLEARCOAT 3412 vec4 C_spec_accum = vec4(0.0); 3413 # endif 3414 3415 # ifdef CLOSURE_REFRACTION 3416 vec4 refr_accum = vec4(0.0); 3417 # endif 3418 3419 # ifdef CLOSURE_GLOSSY 3420 /* ---------------------------- */ 3421 /* Planar Reflections */ 3422 /* ---------------------------- */ 3423 3424 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 3425 PlanarData pd = planars_data[i]; 3426 3427 /* Fade on geometric normal. */ 3428 float fade = probe_attenuation_planar( 3429 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 3430 3431 if (fade > 0.0) { 3432 if (!(ssrToggle && ssr_id == outputSsrId)) { 3433 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 3434 accumulate_light(spec, fade, spec_accum); 3435 } 3436 3437 # ifdef CLOSURE_CLEARCOAT 3438 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 3439 accumulate_light(C_spec, fade, C_spec_accum); 3440 # endif 3441 } 3442 } 3443 # endif 3444 3445 # ifdef CLOSURE_GLOSSY 3446 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 3447 # endif 3448 3449 # ifdef CLOSURE_CLEARCOAT 3450 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 3451 # endif 3452 3453 # ifdef CLOSURE_REFRACTION 3454 /* Refract the view vector using the depth heuristic. 3455 * Then later Refract a second time the already refracted 3456 * ray using the inverse ior. */ 3457 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 3458 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 3459 vec3 refr_pos = (refractionDepth > 0.0) ? 3460 line_plane_intersect( 3461 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 3462 worldPosition; 3463 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 3464 # endif 3465 3466 # ifdef CLOSURE_REFRACTION 3467 /* ---------------------------- */ 3468 /* Screen Space Refraction */ 3469 /* ---------------------------- */ 3470 # ifdef USE_REFRACTION 3471 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 3472 /* Find approximated position of the 2nd refraction event. */ 3473 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 3474 viewPosition; 3475 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 3476 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 3477 accumulate_light(trans.rgb, trans.a, refr_accum); 3478 } 3479 # endif 3480 3481 # endif 3482 3483 /* ---------------------------- */ 3484 /* Specular probes */ 3485 /* ---------------------------- */ 3486 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 3487 3488 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 3489 # define GLASS_ACCUM 1 3490 # define ACCUM min(refr_accum.a, spec_accum.a) 3491 # elif defined(CLOSURE_REFRACTION) 3492 # define GLASS_ACCUM 0 3493 # define ACCUM refr_accum.a 3494 # else 3495 # define GLASS_ACCUM 0 3496 # define ACCUM spec_accum.a 3497 # endif 3498 3499 /* Starts at 1 because 0 is world probe */ 3500 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 3501 float fade = probe_attenuation_cube(i, worldPosition); 3502 3503 if (fade > 0.0) { 3504 3505 # if GLASS_ACCUM 3506 if (spec_accum.a < 0.999) { 3507 # endif 3508 # ifdef CLOSURE_GLOSSY 3509 if (!(ssrToggle && ssr_id == outputSsrId)) { 3510 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 3511 accumulate_light(spec, fade, spec_accum); 3512 } 3513 # endif 3514 3515 # ifdef CLOSURE_CLEARCOAT 3516 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 3517 accumulate_light(C_spec, fade, C_spec_accum); 3518 # endif 3519 # if GLASS_ACCUM 3520 } 3521 # endif 3522 3523 # if GLASS_ACCUM 3524 if (refr_accum.a < 0.999) { 3525 # endif 3526 # ifdef CLOSURE_REFRACTION 3527 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 3528 accumulate_light(trans, fade, refr_accum); 3529 # endif 3530 # if GLASS_ACCUM 3531 } 3532 # endif 3533 } 3534 } 3535 3536 # undef GLASS_ACCUM 3537 # undef ACCUM 3538 3539 /* ---------------------------- */ 3540 /* World Probe */ 3541 /* ---------------------------- */ 3542 # ifdef CLOSURE_GLOSSY 3543 if (spec_accum.a < 0.999) { 3544 if (!(ssrToggle && ssr_id == outputSsrId)) { 3545 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 3546 accumulate_light(spec, 1.0, spec_accum); 3547 } 3548 3549 # ifdef CLOSURE_CLEARCOAT 3550 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 3551 accumulate_light(C_spec, 1.0, C_spec_accum); 3552 # endif 3553 } 3554 # endif 3555 3556 # ifdef CLOSURE_REFRACTION 3557 if (refr_accum.a < 0.999) { 3558 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 3559 accumulate_light(trans, 1.0, refr_accum); 3560 } 3561 # endif 3562 # endif /* Specular probes */ 3563 3564 /* ---------------------------- */ 3565 /* Ambient Occlusion */ 3566 /* ---------------------------- */ 3567 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 3568 if (!use_contact_shadows) { 3569 /* HACK: Fix for translucent BSDF. (see T65631) */ 3570 N = -N; 3571 } 3572 vec3 bent_normal; 3573 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 3574 if (!use_contact_shadows) { 3575 N = -N; 3576 /* Bypass bent normal. */ 3577 bent_normal = N; 3578 } 3579 # endif 3580 3581 /* ---------------------------- */ 3582 /* Specular Output */ 3583 /* ---------------------------- */ 3584 float NV = dot(N, V); 3585 # ifdef CLOSURE_GLOSSY 3586 vec2 uv = lut_coords(NV, roughness); 3587 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 3588 3589 /* This factor is outputted to be used by SSR in order 3590 * to match the intensity of the regular reflections. */ 3591 ssr_spec = F_ibl(f0, f90, brdf_lut); 3592 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 3593 3594 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 3595 if (ssrToggle && ssr_id == outputSsrId) { 3596 spec_occlu = 1.0; 3597 } 3598 3599 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 3600 # endif 3601 3602 # ifdef CLOSURE_REFRACTION 3603 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 3604 3605 out_refr += refr_accum.rgb * btdf; 3606 # endif 3607 3608 # ifdef CLOSURE_CLEARCOAT 3609 NV = dot(C_N, V); 3610 vec2 C_uv = lut_coords(NV, C_roughness); 3611 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 3612 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 3613 specular_occlusion(NV, final_ao, C_roughness); 3614 3615 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 3616 # endif 3617 3618 # ifdef CLOSURE_GLOSSY 3619 /* Global toggle for lightprobe baking. */ 3620 out_spec *= float(specToggle); 3621 # endif 3622 3623 /* ---------------------------------------------------------------- */ 3624 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 3625 /* ---------------------------------------------------------------- */ 3626 3627 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 3628 # ifdef CLOSURE_DIFFUSE 3629 vec4 diff_accum = vec4(0.0); 3630 3631 /* ---------------------------- */ 3632 /* Irradiance Grids */ 3633 /* ---------------------------- */ 3634 /* Start at 1 because 0 is world irradiance */ 3635 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 3636 GridData gd = grids_data[i]; 3637 3638 vec3 localpos; 3639 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 3640 3641 if (fade > 0.0) { 3642 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 3643 accumulate_light(diff, fade, diff_accum); 3644 } 3645 } 3646 3647 /* ---------------------------- */ 3648 /* World Diffuse */ 3649 /* ---------------------------- */ 3650 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 3651 vec3 diff = probe_evaluate_world_diff(bent_normal); 3652 accumulate_light(diff, 1.0, diff_accum); 3653 } 3654 3655 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 3656 # endif 3657 #endif 3658 } 3659 3660 /* Cleanup for next configuration */ 3661 #undef CLOSURE_NAME 3662 3663 #ifdef CLOSURE_DIFFUSE 3664 # undef CLOSURE_DIFFUSE 3665 #endif 3666 3667 #ifdef CLOSURE_GLOSSY 3668 # undef CLOSURE_GLOSSY 3669 #endif 3670 3671 #ifdef CLOSURE_CLEARCOAT 3672 # undef CLOSURE_CLEARCOAT 3673 #endif 3674 3675 #ifdef CLOSURE_REFRACTION 3676 # undef CLOSURE_REFRACTION 3677 #endif 3678 3679 #ifdef CLOSURE_SUBSURFACE 3680 # undef CLOSURE_SUBSURFACE 3681 #endif 3682 3683 #ifndef LIT_SURFACE_UNIFORM 3684 # define LIT_SURFACE_UNIFORM 3685 3686 uniform float refractionDepth; 3687 3688 # ifndef UTIL_TEX 3689 # define UTIL_TEX 3690 uniform sampler2DArray utilTex; 3691 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 3692 # endif /* UTIL_TEX */ 3693 3694 in vec3 worldPosition; 3695 in vec3 viewPosition; 3696 3697 in vec3 worldNormal; 3698 in vec3 viewNormal; 3699 3700 # ifdef HAIR_SHADER 3701 in vec3 hairTangent; /* world space */ 3702 in float hairThickTime; 3703 in float hairThickness; 3704 in float hairTime; 3705 flat in int hairStrandID; 3706 3707 uniform int hairThicknessRes = 1; 3708 # endif 3709 3710 #endif /* LIT_SURFACE_UNIFORM */ 3711 3712 /** 3713 * AUTO CONFIG 3714 * We include the file multiple times each time with a different configuration. 3715 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 3716 */ 3717 #if !defined(SURFACE_DEFAULT) 3718 # define SURFACE_DEFAULT 3719 # define CLOSURE_NAME eevee_closure_default 3720 # define CLOSURE_DIFFUSE 3721 # define CLOSURE_GLOSSY 3722 #endif /* SURFACE_DEFAULT */ 3723 3724 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 3725 # define SURFACE_DEFAULT_CLEARCOAT 3726 # define CLOSURE_NAME eevee_closure_default_clearcoat 3727 # define CLOSURE_DIFFUSE 3728 # define CLOSURE_GLOSSY 3729 # define CLOSURE_CLEARCOAT 3730 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 3731 3732 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 3733 # define SURFACE_PRINCIPLED 3734 # define CLOSURE_NAME eevee_closure_principled 3735 # define CLOSURE_DIFFUSE 3736 # define CLOSURE_GLOSSY 3737 # define CLOSURE_CLEARCOAT 3738 # define CLOSURE_REFRACTION 3739 # define CLOSURE_SUBSURFACE 3740 #endif /* SURFACE_PRINCIPLED */ 3741 3742 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 3743 # define SURFACE_CLEARCOAT 3744 # define CLOSURE_NAME eevee_closure_clearcoat 3745 # define CLOSURE_GLOSSY 3746 # define CLOSURE_CLEARCOAT 3747 #endif /* SURFACE_CLEARCOAT */ 3748 3749 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 3750 # define SURFACE_DIFFUSE 3751 # define CLOSURE_NAME eevee_closure_diffuse 3752 # define CLOSURE_DIFFUSE 3753 #endif /* SURFACE_DIFFUSE */ 3754 3755 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 3756 # define SURFACE_SUBSURFACE 3757 # define CLOSURE_NAME eevee_closure_subsurface 3758 # define CLOSURE_DIFFUSE 3759 # define CLOSURE_SUBSURFACE 3760 #endif /* SURFACE_SUBSURFACE */ 3761 3762 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 3763 # define SURFACE_SKIN 3764 # define CLOSURE_NAME eevee_closure_skin 3765 # define CLOSURE_DIFFUSE 3766 # define CLOSURE_SUBSURFACE 3767 # define CLOSURE_GLOSSY 3768 #endif /* SURFACE_SKIN */ 3769 3770 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 3771 # define SURFACE_GLOSSY 3772 # define CLOSURE_NAME eevee_closure_glossy 3773 # define CLOSURE_GLOSSY 3774 #endif /* SURFACE_GLOSSY */ 3775 3776 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 3777 # define SURFACE_REFRACT 3778 # define CLOSURE_NAME eevee_closure_refraction 3779 # define CLOSURE_REFRACTION 3780 #endif /* SURFACE_REFRACT */ 3781 3782 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 3783 # define SURFACE_GLASS 3784 # define CLOSURE_NAME eevee_closure_glass 3785 # define CLOSURE_GLOSSY 3786 # define CLOSURE_REFRACTION 3787 #endif /* SURFACE_GLASS */ 3788 3789 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 3790 #ifdef CLOSURE_CLEARCOAT 3791 # ifndef CLOSURE_GLOSSY 3792 # define CLOSURE_GLOSSY 3793 # endif 3794 #endif /* CLOSURE_CLEARCOAT */ 3795 3796 void CLOSURE_NAME(vec3 N 3797 #ifdef CLOSURE_DIFFUSE 3798 , 3799 vec3 albedo 3800 #endif 3801 #ifdef CLOSURE_GLOSSY 3802 , 3803 vec3 f0, 3804 vec3 f90, 3805 int ssr_id 3806 #endif 3807 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 3808 , 3809 float roughness 3810 #endif 3811 #ifdef CLOSURE_CLEARCOAT 3812 , 3813 vec3 C_N, 3814 float C_intensity, 3815 float C_roughness 3816 #endif 3817 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 3818 , 3819 float ao 3820 #endif 3821 #ifdef CLOSURE_SUBSURFACE 3822 , 3823 float sss_scale 3824 #endif 3825 #ifdef CLOSURE_REFRACTION 3826 , 3827 float ior 3828 #endif 3829 , 3830 const bool use_contact_shadows 3831 #ifdef CLOSURE_DIFFUSE 3832 , 3833 out vec3 out_diff 3834 #endif 3835 #ifdef CLOSURE_GLOSSY 3836 , 3837 out vec3 out_spec 3838 #endif 3839 #ifdef CLOSURE_REFRACTION 3840 , 3841 out vec3 out_refr 3842 #endif 3843 #ifdef CLOSURE_GLOSSY 3844 , 3845 out vec3 ssr_spec 3846 #endif 3847 ) 3848 { 3849 #ifdef CLOSURE_DIFFUSE 3850 out_diff = vec3(0.0); 3851 #endif 3852 3853 #ifdef CLOSURE_GLOSSY 3854 out_spec = vec3(0.0); 3855 #endif 3856 3857 #ifdef CLOSURE_REFRACTION 3858 out_refr = vec3(0.0); 3859 #endif 3860 3861 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 3862 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 3863 return; 3864 #else 3865 3866 /* Zero length vectors cause issues, see: T51979. */ 3867 float len = length(N); 3868 if (isnan(len)) { 3869 return; 3870 } 3871 N /= len; 3872 3873 # ifdef CLOSURE_CLEARCOAT 3874 len = length(C_N); 3875 if (isnan(len)) { 3876 return; 3877 } 3878 C_N /= len; 3879 # endif 3880 3881 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 3882 roughness = clamp(roughness, 1e-8, 0.9999); 3883 float roughnessSquared = roughness * roughness; 3884 # endif 3885 3886 # ifdef CLOSURE_CLEARCOAT 3887 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 3888 float C_roughnessSquared = C_roughness * C_roughness; 3889 # endif 3890 3891 vec3 V = cameraVec; 3892 3893 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 3894 3895 /* ---------------------------------------------------------------- */ 3896 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 3897 /* ---------------------------------------------------------------- */ 3898 3899 # ifdef CLOSURE_GLOSSY 3900 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 3901 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 3902 # endif 3903 3904 # ifdef CLOSURE_CLEARCOAT 3905 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 3906 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 3907 vec3 out_spec_clear = vec3(0.0); 3908 # endif 3909 3910 float tracing_depth = gl_FragCoord.z; 3911 /* Constant bias (due to depth buffer precision) */ 3912 /* Magic numbers for 24bits of precision. 3913 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 3914 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 3915 /* Convert to view Z. */ 3916 tracing_depth = get_view_z_from_depth(tracing_depth); 3917 3918 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 3919 3920 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 3921 LightData ld = lights_data[i]; 3922 3923 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 3924 l_vector.xyz = ld.l_position - worldPosition; 3925 l_vector.w = length(l_vector.xyz); 3926 3927 float l_vis = light_visibility(ld, 3928 worldPosition, 3929 viewPosition, 3930 tracing_depth, 3931 true_normal, 3932 rand.x, 3933 use_contact_shadows, 3934 l_vector); 3935 3936 if (l_vis < 1e-8) { 3937 continue; 3938 } 3939 3940 vec3 l_color_vis = ld.l_color * l_vis; 3941 3942 # ifdef CLOSURE_DIFFUSE 3943 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 3944 # endif 3945 3946 # ifdef CLOSURE_GLOSSY 3947 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 3948 # endif 3949 3950 # ifdef CLOSURE_CLEARCOAT 3951 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 3952 ld.l_spec; 3953 # endif 3954 } 3955 3956 # ifdef CLOSURE_GLOSSY 3957 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 3958 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 3959 # endif 3960 3961 # ifdef CLOSURE_CLEARCOAT 3962 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 3963 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 3964 out_spec += out_spec_clear * C_intensity; 3965 # endif 3966 3967 /* ---------------------------------------------------------------- */ 3968 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 3969 /* ---------------------------------------------------------------- */ 3970 3971 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 3972 * BRDF. */ 3973 # ifdef CLOSURE_GLOSSY 3974 vec4 spec_accum = vec4(0.0); 3975 # endif 3976 3977 # ifdef CLOSURE_CLEARCOAT 3978 vec4 C_spec_accum = vec4(0.0); 3979 # endif 3980 3981 # ifdef CLOSURE_REFRACTION 3982 vec4 refr_accum = vec4(0.0); 3983 # endif 3984 3985 # ifdef CLOSURE_GLOSSY 3986 /* ---------------------------- */ 3987 /* Planar Reflections */ 3988 /* ---------------------------- */ 3989 3990 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 3991 PlanarData pd = planars_data[i]; 3992 3993 /* Fade on geometric normal. */ 3994 float fade = probe_attenuation_planar( 3995 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 3996 3997 if (fade > 0.0) { 3998 if (!(ssrToggle && ssr_id == outputSsrId)) { 3999 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 4000 accumulate_light(spec, fade, spec_accum); 4001 } 4002 4003 # ifdef CLOSURE_CLEARCOAT 4004 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 4005 accumulate_light(C_spec, fade, C_spec_accum); 4006 # endif 4007 } 4008 } 4009 # endif 4010 4011 # ifdef CLOSURE_GLOSSY 4012 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 4013 # endif 4014 4015 # ifdef CLOSURE_CLEARCOAT 4016 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 4017 # endif 4018 4019 # ifdef CLOSURE_REFRACTION 4020 /* Refract the view vector using the depth heuristic. 4021 * Then later Refract a second time the already refracted 4022 * ray using the inverse ior. */ 4023 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 4024 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 4025 vec3 refr_pos = (refractionDepth > 0.0) ? 4026 line_plane_intersect( 4027 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 4028 worldPosition; 4029 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 4030 # endif 4031 4032 # ifdef CLOSURE_REFRACTION 4033 /* ---------------------------- */ 4034 /* Screen Space Refraction */ 4035 /* ---------------------------- */ 4036 # ifdef USE_REFRACTION 4037 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 4038 /* Find approximated position of the 2nd refraction event. */ 4039 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 4040 viewPosition; 4041 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 4042 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 4043 accumulate_light(trans.rgb, trans.a, refr_accum); 4044 } 4045 # endif 4046 4047 # endif 4048 4049 /* ---------------------------- */ 4050 /* Specular probes */ 4051 /* ---------------------------- */ 4052 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 4053 4054 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 4055 # define GLASS_ACCUM 1 4056 # define ACCUM min(refr_accum.a, spec_accum.a) 4057 # elif defined(CLOSURE_REFRACTION) 4058 # define GLASS_ACCUM 0 4059 # define ACCUM refr_accum.a 4060 # else 4061 # define GLASS_ACCUM 0 4062 # define ACCUM spec_accum.a 4063 # endif 4064 4065 /* Starts at 1 because 0 is world probe */ 4066 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 4067 float fade = probe_attenuation_cube(i, worldPosition); 4068 4069 if (fade > 0.0) { 4070 4071 # if GLASS_ACCUM 4072 if (spec_accum.a < 0.999) { 4073 # endif 4074 # ifdef CLOSURE_GLOSSY 4075 if (!(ssrToggle && ssr_id == outputSsrId)) { 4076 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 4077 accumulate_light(spec, fade, spec_accum); 4078 } 4079 # endif 4080 4081 # ifdef CLOSURE_CLEARCOAT 4082 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 4083 accumulate_light(C_spec, fade, C_spec_accum); 4084 # endif 4085 # if GLASS_ACCUM 4086 } 4087 # endif 4088 4089 # if GLASS_ACCUM 4090 if (refr_accum.a < 0.999) { 4091 # endif 4092 # ifdef CLOSURE_REFRACTION 4093 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 4094 accumulate_light(trans, fade, refr_accum); 4095 # endif 4096 # if GLASS_ACCUM 4097 } 4098 # endif 4099 } 4100 } 4101 4102 # undef GLASS_ACCUM 4103 # undef ACCUM 4104 4105 /* ---------------------------- */ 4106 /* World Probe */ 4107 /* ---------------------------- */ 4108 # ifdef CLOSURE_GLOSSY 4109 if (spec_accum.a < 0.999) { 4110 if (!(ssrToggle && ssr_id == outputSsrId)) { 4111 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 4112 accumulate_light(spec, 1.0, spec_accum); 4113 } 4114 4115 # ifdef CLOSURE_CLEARCOAT 4116 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 4117 accumulate_light(C_spec, 1.0, C_spec_accum); 4118 # endif 4119 } 4120 # endif 4121 4122 # ifdef CLOSURE_REFRACTION 4123 if (refr_accum.a < 0.999) { 4124 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 4125 accumulate_light(trans, 1.0, refr_accum); 4126 } 4127 # endif 4128 # endif /* Specular probes */ 4129 4130 /* ---------------------------- */ 4131 /* Ambient Occlusion */ 4132 /* ---------------------------- */ 4133 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 4134 if (!use_contact_shadows) { 4135 /* HACK: Fix for translucent BSDF. (see T65631) */ 4136 N = -N; 4137 } 4138 vec3 bent_normal; 4139 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 4140 if (!use_contact_shadows) { 4141 N = -N; 4142 /* Bypass bent normal. */ 4143 bent_normal = N; 4144 } 4145 # endif 4146 4147 /* ---------------------------- */ 4148 /* Specular Output */ 4149 /* ---------------------------- */ 4150 float NV = dot(N, V); 4151 # ifdef CLOSURE_GLOSSY 4152 vec2 uv = lut_coords(NV, roughness); 4153 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 4154 4155 /* This factor is outputted to be used by SSR in order 4156 * to match the intensity of the regular reflections. */ 4157 ssr_spec = F_ibl(f0, f90, brdf_lut); 4158 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 4159 4160 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 4161 if (ssrToggle && ssr_id == outputSsrId) { 4162 spec_occlu = 1.0; 4163 } 4164 4165 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 4166 # endif 4167 4168 # ifdef CLOSURE_REFRACTION 4169 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 4170 4171 out_refr += refr_accum.rgb * btdf; 4172 # endif 4173 4174 # ifdef CLOSURE_CLEARCOAT 4175 NV = dot(C_N, V); 4176 vec2 C_uv = lut_coords(NV, C_roughness); 4177 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 4178 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 4179 specular_occlusion(NV, final_ao, C_roughness); 4180 4181 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 4182 # endif 4183 4184 # ifdef CLOSURE_GLOSSY 4185 /* Global toggle for lightprobe baking. */ 4186 out_spec *= float(specToggle); 4187 # endif 4188 4189 /* ---------------------------------------------------------------- */ 4190 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 4191 /* ---------------------------------------------------------------- */ 4192 4193 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 4194 # ifdef CLOSURE_DIFFUSE 4195 vec4 diff_accum = vec4(0.0); 4196 4197 /* ---------------------------- */ 4198 /* Irradiance Grids */ 4199 /* ---------------------------- */ 4200 /* Start at 1 because 0 is world irradiance */ 4201 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 4202 GridData gd = grids_data[i]; 4203 4204 vec3 localpos; 4205 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 4206 4207 if (fade > 0.0) { 4208 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 4209 accumulate_light(diff, fade, diff_accum); 4210 } 4211 } 4212 4213 /* ---------------------------- */ 4214 /* World Diffuse */ 4215 /* ---------------------------- */ 4216 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 4217 vec3 diff = probe_evaluate_world_diff(bent_normal); 4218 accumulate_light(diff, 1.0, diff_accum); 4219 } 4220 4221 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 4222 # endif 4223 #endif 4224 } 4225 4226 /* Cleanup for next configuration */ 4227 #undef CLOSURE_NAME 4228 4229 #ifdef CLOSURE_DIFFUSE 4230 # undef CLOSURE_DIFFUSE 4231 #endif 4232 4233 #ifdef CLOSURE_GLOSSY 4234 # undef CLOSURE_GLOSSY 4235 #endif 4236 4237 #ifdef CLOSURE_CLEARCOAT 4238 # undef CLOSURE_CLEARCOAT 4239 #endif 4240 4241 #ifdef CLOSURE_REFRACTION 4242 # undef CLOSURE_REFRACTION 4243 #endif 4244 4245 #ifdef CLOSURE_SUBSURFACE 4246 # undef CLOSURE_SUBSURFACE 4247 #endif 4248 4249 #ifndef LIT_SURFACE_UNIFORM 4250 # define LIT_SURFACE_UNIFORM 4251 4252 uniform float refractionDepth; 4253 4254 # ifndef UTIL_TEX 4255 # define UTIL_TEX 4256 uniform sampler2DArray utilTex; 4257 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 4258 # endif /* UTIL_TEX */ 4259 4260 in vec3 worldPosition; 4261 in vec3 viewPosition; 4262 4263 in vec3 worldNormal; 4264 in vec3 viewNormal; 4265 4266 # ifdef HAIR_SHADER 4267 in vec3 hairTangent; /* world space */ 4268 in float hairThickTime; 4269 in float hairThickness; 4270 in float hairTime; 4271 flat in int hairStrandID; 4272 4273 uniform int hairThicknessRes = 1; 4274 # endif 4275 4276 #endif /* LIT_SURFACE_UNIFORM */ 4277 4278 /** 4279 * AUTO CONFIG 4280 * We include the file multiple times each time with a different configuration. 4281 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 4282 */ 4283 #if !defined(SURFACE_DEFAULT) 4284 # define SURFACE_DEFAULT 4285 # define CLOSURE_NAME eevee_closure_default 4286 # define CLOSURE_DIFFUSE 4287 # define CLOSURE_GLOSSY 4288 #endif /* SURFACE_DEFAULT */ 4289 4290 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 4291 # define SURFACE_DEFAULT_CLEARCOAT 4292 # define CLOSURE_NAME eevee_closure_default_clearcoat 4293 # define CLOSURE_DIFFUSE 4294 # define CLOSURE_GLOSSY 4295 # define CLOSURE_CLEARCOAT 4296 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 4297 4298 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 4299 # define SURFACE_PRINCIPLED 4300 # define CLOSURE_NAME eevee_closure_principled 4301 # define CLOSURE_DIFFUSE 4302 # define CLOSURE_GLOSSY 4303 # define CLOSURE_CLEARCOAT 4304 # define CLOSURE_REFRACTION 4305 # define CLOSURE_SUBSURFACE 4306 #endif /* SURFACE_PRINCIPLED */ 4307 4308 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 4309 # define SURFACE_CLEARCOAT 4310 # define CLOSURE_NAME eevee_closure_clearcoat 4311 # define CLOSURE_GLOSSY 4312 # define CLOSURE_CLEARCOAT 4313 #endif /* SURFACE_CLEARCOAT */ 4314 4315 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 4316 # define SURFACE_DIFFUSE 4317 # define CLOSURE_NAME eevee_closure_diffuse 4318 # define CLOSURE_DIFFUSE 4319 #endif /* SURFACE_DIFFUSE */ 4320 4321 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 4322 # define SURFACE_SUBSURFACE 4323 # define CLOSURE_NAME eevee_closure_subsurface 4324 # define CLOSURE_DIFFUSE 4325 # define CLOSURE_SUBSURFACE 4326 #endif /* SURFACE_SUBSURFACE */ 4327 4328 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 4329 # define SURFACE_SKIN 4330 # define CLOSURE_NAME eevee_closure_skin 4331 # define CLOSURE_DIFFUSE 4332 # define CLOSURE_SUBSURFACE 4333 # define CLOSURE_GLOSSY 4334 #endif /* SURFACE_SKIN */ 4335 4336 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 4337 # define SURFACE_GLOSSY 4338 # define CLOSURE_NAME eevee_closure_glossy 4339 # define CLOSURE_GLOSSY 4340 #endif /* SURFACE_GLOSSY */ 4341 4342 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 4343 # define SURFACE_REFRACT 4344 # define CLOSURE_NAME eevee_closure_refraction 4345 # define CLOSURE_REFRACTION 4346 #endif /* SURFACE_REFRACT */ 4347 4348 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 4349 # define SURFACE_GLASS 4350 # define CLOSURE_NAME eevee_closure_glass 4351 # define CLOSURE_GLOSSY 4352 # define CLOSURE_REFRACTION 4353 #endif /* SURFACE_GLASS */ 4354 4355 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 4356 #ifdef CLOSURE_CLEARCOAT 4357 # ifndef CLOSURE_GLOSSY 4358 # define CLOSURE_GLOSSY 4359 # endif 4360 #endif /* CLOSURE_CLEARCOAT */ 4361 4362 void CLOSURE_NAME(vec3 N 4363 #ifdef CLOSURE_DIFFUSE 4364 , 4365 vec3 albedo 4366 #endif 4367 #ifdef CLOSURE_GLOSSY 4368 , 4369 vec3 f0, 4370 vec3 f90, 4371 int ssr_id 4372 #endif 4373 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 4374 , 4375 float roughness 4376 #endif 4377 #ifdef CLOSURE_CLEARCOAT 4378 , 4379 vec3 C_N, 4380 float C_intensity, 4381 float C_roughness 4382 #endif 4383 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 4384 , 4385 float ao 4386 #endif 4387 #ifdef CLOSURE_SUBSURFACE 4388 , 4389 float sss_scale 4390 #endif 4391 #ifdef CLOSURE_REFRACTION 4392 , 4393 float ior 4394 #endif 4395 , 4396 const bool use_contact_shadows 4397 #ifdef CLOSURE_DIFFUSE 4398 , 4399 out vec3 out_diff 4400 #endif 4401 #ifdef CLOSURE_GLOSSY 4402 , 4403 out vec3 out_spec 4404 #endif 4405 #ifdef CLOSURE_REFRACTION 4406 , 4407 out vec3 out_refr 4408 #endif 4409 #ifdef CLOSURE_GLOSSY 4410 , 4411 out vec3 ssr_spec 4412 #endif 4413 ) 4414 { 4415 #ifdef CLOSURE_DIFFUSE 4416 out_diff = vec3(0.0); 4417 #endif 4418 4419 #ifdef CLOSURE_GLOSSY 4420 out_spec = vec3(0.0); 4421 #endif 4422 4423 #ifdef CLOSURE_REFRACTION 4424 out_refr = vec3(0.0); 4425 #endif 4426 4427 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 4428 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 4429 return; 4430 #else 4431 4432 /* Zero length vectors cause issues, see: T51979. */ 4433 float len = length(N); 4434 if (isnan(len)) { 4435 return; 4436 } 4437 N /= len; 4438 4439 # ifdef CLOSURE_CLEARCOAT 4440 len = length(C_N); 4441 if (isnan(len)) { 4442 return; 4443 } 4444 C_N /= len; 4445 # endif 4446 4447 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 4448 roughness = clamp(roughness, 1e-8, 0.9999); 4449 float roughnessSquared = roughness * roughness; 4450 # endif 4451 4452 # ifdef CLOSURE_CLEARCOAT 4453 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 4454 float C_roughnessSquared = C_roughness * C_roughness; 4455 # endif 4456 4457 vec3 V = cameraVec; 4458 4459 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 4460 4461 /* ---------------------------------------------------------------- */ 4462 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 4463 /* ---------------------------------------------------------------- */ 4464 4465 # ifdef CLOSURE_GLOSSY 4466 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 4467 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 4468 # endif 4469 4470 # ifdef CLOSURE_CLEARCOAT 4471 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 4472 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 4473 vec3 out_spec_clear = vec3(0.0); 4474 # endif 4475 4476 float tracing_depth = gl_FragCoord.z; 4477 /* Constant bias (due to depth buffer precision) */ 4478 /* Magic numbers for 24bits of precision. 4479 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 4480 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 4481 /* Convert to view Z. */ 4482 tracing_depth = get_view_z_from_depth(tracing_depth); 4483 4484 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 4485 4486 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 4487 LightData ld = lights_data[i]; 4488 4489 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 4490 l_vector.xyz = ld.l_position - worldPosition; 4491 l_vector.w = length(l_vector.xyz); 4492 4493 float l_vis = light_visibility(ld, 4494 worldPosition, 4495 viewPosition, 4496 tracing_depth, 4497 true_normal, 4498 rand.x, 4499 use_contact_shadows, 4500 l_vector); 4501 4502 if (l_vis < 1e-8) { 4503 continue; 4504 } 4505 4506 vec3 l_color_vis = ld.l_color * l_vis; 4507 4508 # ifdef CLOSURE_DIFFUSE 4509 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 4510 # endif 4511 4512 # ifdef CLOSURE_GLOSSY 4513 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 4514 # endif 4515 4516 # ifdef CLOSURE_CLEARCOAT 4517 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 4518 ld.l_spec; 4519 # endif 4520 } 4521 4522 # ifdef CLOSURE_GLOSSY 4523 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 4524 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 4525 # endif 4526 4527 # ifdef CLOSURE_CLEARCOAT 4528 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 4529 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 4530 out_spec += out_spec_clear * C_intensity; 4531 # endif 4532 4533 /* ---------------------------------------------------------------- */ 4534 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 4535 /* ---------------------------------------------------------------- */ 4536 4537 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 4538 * BRDF. */ 4539 # ifdef CLOSURE_GLOSSY 4540 vec4 spec_accum = vec4(0.0); 4541 # endif 4542 4543 # ifdef CLOSURE_CLEARCOAT 4544 vec4 C_spec_accum = vec4(0.0); 4545 # endif 4546 4547 # ifdef CLOSURE_REFRACTION 4548 vec4 refr_accum = vec4(0.0); 4549 # endif 4550 4551 # ifdef CLOSURE_GLOSSY 4552 /* ---------------------------- */ 4553 /* Planar Reflections */ 4554 /* ---------------------------- */ 4555 4556 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 4557 PlanarData pd = planars_data[i]; 4558 4559 /* Fade on geometric normal. */ 4560 float fade = probe_attenuation_planar( 4561 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 4562 4563 if (fade > 0.0) { 4564 if (!(ssrToggle && ssr_id == outputSsrId)) { 4565 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 4566 accumulate_light(spec, fade, spec_accum); 4567 } 4568 4569 # ifdef CLOSURE_CLEARCOAT 4570 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 4571 accumulate_light(C_spec, fade, C_spec_accum); 4572 # endif 4573 } 4574 } 4575 # endif 4576 4577 # ifdef CLOSURE_GLOSSY 4578 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 4579 # endif 4580 4581 # ifdef CLOSURE_CLEARCOAT 4582 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 4583 # endif 4584 4585 # ifdef CLOSURE_REFRACTION 4586 /* Refract the view vector using the depth heuristic. 4587 * Then later Refract a second time the already refracted 4588 * ray using the inverse ior. */ 4589 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 4590 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 4591 vec3 refr_pos = (refractionDepth > 0.0) ? 4592 line_plane_intersect( 4593 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 4594 worldPosition; 4595 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 4596 # endif 4597 4598 # ifdef CLOSURE_REFRACTION 4599 /* ---------------------------- */ 4600 /* Screen Space Refraction */ 4601 /* ---------------------------- */ 4602 # ifdef USE_REFRACTION 4603 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 4604 /* Find approximated position of the 2nd refraction event. */ 4605 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 4606 viewPosition; 4607 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 4608 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 4609 accumulate_light(trans.rgb, trans.a, refr_accum); 4610 } 4611 # endif 4612 4613 # endif 4614 4615 /* ---------------------------- */ 4616 /* Specular probes */ 4617 /* ---------------------------- */ 4618 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 4619 4620 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 4621 # define GLASS_ACCUM 1 4622 # define ACCUM min(refr_accum.a, spec_accum.a) 4623 # elif defined(CLOSURE_REFRACTION) 4624 # define GLASS_ACCUM 0 4625 # define ACCUM refr_accum.a 4626 # else 4627 # define GLASS_ACCUM 0 4628 # define ACCUM spec_accum.a 4629 # endif 4630 4631 /* Starts at 1 because 0 is world probe */ 4632 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 4633 float fade = probe_attenuation_cube(i, worldPosition); 4634 4635 if (fade > 0.0) { 4636 4637 # if GLASS_ACCUM 4638 if (spec_accum.a < 0.999) { 4639 # endif 4640 # ifdef CLOSURE_GLOSSY 4641 if (!(ssrToggle && ssr_id == outputSsrId)) { 4642 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 4643 accumulate_light(spec, fade, spec_accum); 4644 } 4645 # endif 4646 4647 # ifdef CLOSURE_CLEARCOAT 4648 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 4649 accumulate_light(C_spec, fade, C_spec_accum); 4650 # endif 4651 # if GLASS_ACCUM 4652 } 4653 # endif 4654 4655 # if GLASS_ACCUM 4656 if (refr_accum.a < 0.999) { 4657 # endif 4658 # ifdef CLOSURE_REFRACTION 4659 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 4660 accumulate_light(trans, fade, refr_accum); 4661 # endif 4662 # if GLASS_ACCUM 4663 } 4664 # endif 4665 } 4666 } 4667 4668 # undef GLASS_ACCUM 4669 # undef ACCUM 4670 4671 /* ---------------------------- */ 4672 /* World Probe */ 4673 /* ---------------------------- */ 4674 # ifdef CLOSURE_GLOSSY 4675 if (spec_accum.a < 0.999) { 4676 if (!(ssrToggle && ssr_id == outputSsrId)) { 4677 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 4678 accumulate_light(spec, 1.0, spec_accum); 4679 } 4680 4681 # ifdef CLOSURE_CLEARCOAT 4682 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 4683 accumulate_light(C_spec, 1.0, C_spec_accum); 4684 # endif 4685 } 4686 # endif 4687 4688 # ifdef CLOSURE_REFRACTION 4689 if (refr_accum.a < 0.999) { 4690 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 4691 accumulate_light(trans, 1.0, refr_accum); 4692 } 4693 # endif 4694 # endif /* Specular probes */ 4695 4696 /* ---------------------------- */ 4697 /* Ambient Occlusion */ 4698 /* ---------------------------- */ 4699 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 4700 if (!use_contact_shadows) { 4701 /* HACK: Fix for translucent BSDF. (see T65631) */ 4702 N = -N; 4703 } 4704 vec3 bent_normal; 4705 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 4706 if (!use_contact_shadows) { 4707 N = -N; 4708 /* Bypass bent normal. */ 4709 bent_normal = N; 4710 } 4711 # endif 4712 4713 /* ---------------------------- */ 4714 /* Specular Output */ 4715 /* ---------------------------- */ 4716 float NV = dot(N, V); 4717 # ifdef CLOSURE_GLOSSY 4718 vec2 uv = lut_coords(NV, roughness); 4719 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 4720 4721 /* This factor is outputted to be used by SSR in order 4722 * to match the intensity of the regular reflections. */ 4723 ssr_spec = F_ibl(f0, f90, brdf_lut); 4724 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 4725 4726 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 4727 if (ssrToggle && ssr_id == outputSsrId) { 4728 spec_occlu = 1.0; 4729 } 4730 4731 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 4732 # endif 4733 4734 # ifdef CLOSURE_REFRACTION 4735 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 4736 4737 out_refr += refr_accum.rgb * btdf; 4738 # endif 4739 4740 # ifdef CLOSURE_CLEARCOAT 4741 NV = dot(C_N, V); 4742 vec2 C_uv = lut_coords(NV, C_roughness); 4743 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 4744 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 4745 specular_occlusion(NV, final_ao, C_roughness); 4746 4747 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 4748 # endif 4749 4750 # ifdef CLOSURE_GLOSSY 4751 /* Global toggle for lightprobe baking. */ 4752 out_spec *= float(specToggle); 4753 # endif 4754 4755 /* ---------------------------------------------------------------- */ 4756 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 4757 /* ---------------------------------------------------------------- */ 4758 4759 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 4760 # ifdef CLOSURE_DIFFUSE 4761 vec4 diff_accum = vec4(0.0); 4762 4763 /* ---------------------------- */ 4764 /* Irradiance Grids */ 4765 /* ---------------------------- */ 4766 /* Start at 1 because 0 is world irradiance */ 4767 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 4768 GridData gd = grids_data[i]; 4769 4770 vec3 localpos; 4771 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 4772 4773 if (fade > 0.0) { 4774 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 4775 accumulate_light(diff, fade, diff_accum); 4776 } 4777 } 4778 4779 /* ---------------------------- */ 4780 /* World Diffuse */ 4781 /* ---------------------------- */ 4782 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 4783 vec3 diff = probe_evaluate_world_diff(bent_normal); 4784 accumulate_light(diff, 1.0, diff_accum); 4785 } 4786 4787 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 4788 # endif 4789 #endif 4790 } 4791 4792 /* Cleanup for next configuration */ 4793 #undef CLOSURE_NAME 4794 4795 #ifdef CLOSURE_DIFFUSE 4796 # undef CLOSURE_DIFFUSE 4797 #endif 4798 4799 #ifdef CLOSURE_GLOSSY 4800 # undef CLOSURE_GLOSSY 4801 #endif 4802 4803 #ifdef CLOSURE_CLEARCOAT 4804 # undef CLOSURE_CLEARCOAT 4805 #endif 4806 4807 #ifdef CLOSURE_REFRACTION 4808 # undef CLOSURE_REFRACTION 4809 #endif 4810 4811 #ifdef CLOSURE_SUBSURFACE 4812 # undef CLOSURE_SUBSURFACE 4813 #endif 4814 4815 #ifndef LIT_SURFACE_UNIFORM 4816 # define LIT_SURFACE_UNIFORM 4817 4818 uniform float refractionDepth; 4819 4820 # ifndef UTIL_TEX 4821 # define UTIL_TEX 4822 uniform sampler2DArray utilTex; 4823 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 4824 # endif /* UTIL_TEX */ 4825 4826 in vec3 worldPosition; 4827 in vec3 viewPosition; 4828 4829 in vec3 worldNormal; 4830 in vec3 viewNormal; 4831 4832 # ifdef HAIR_SHADER 4833 in vec3 hairTangent; /* world space */ 4834 in float hairThickTime; 4835 in float hairThickness; 4836 in float hairTime; 4837 flat in int hairStrandID; 4838 4839 uniform int hairThicknessRes = 1; 4840 # endif 4841 4842 #endif /* LIT_SURFACE_UNIFORM */ 4843 4844 /** 4845 * AUTO CONFIG 4846 * We include the file multiple times each time with a different configuration. 4847 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 4848 */ 4849 #if !defined(SURFACE_DEFAULT) 4850 # define SURFACE_DEFAULT 4851 # define CLOSURE_NAME eevee_closure_default 4852 # define CLOSURE_DIFFUSE 4853 # define CLOSURE_GLOSSY 4854 #endif /* SURFACE_DEFAULT */ 4855 4856 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 4857 # define SURFACE_DEFAULT_CLEARCOAT 4858 # define CLOSURE_NAME eevee_closure_default_clearcoat 4859 # define CLOSURE_DIFFUSE 4860 # define CLOSURE_GLOSSY 4861 # define CLOSURE_CLEARCOAT 4862 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 4863 4864 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 4865 # define SURFACE_PRINCIPLED 4866 # define CLOSURE_NAME eevee_closure_principled 4867 # define CLOSURE_DIFFUSE 4868 # define CLOSURE_GLOSSY 4869 # define CLOSURE_CLEARCOAT 4870 # define CLOSURE_REFRACTION 4871 # define CLOSURE_SUBSURFACE 4872 #endif /* SURFACE_PRINCIPLED */ 4873 4874 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 4875 # define SURFACE_CLEARCOAT 4876 # define CLOSURE_NAME eevee_closure_clearcoat 4877 # define CLOSURE_GLOSSY 4878 # define CLOSURE_CLEARCOAT 4879 #endif /* SURFACE_CLEARCOAT */ 4880 4881 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 4882 # define SURFACE_DIFFUSE 4883 # define CLOSURE_NAME eevee_closure_diffuse 4884 # define CLOSURE_DIFFUSE 4885 #endif /* SURFACE_DIFFUSE */ 4886 4887 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 4888 # define SURFACE_SUBSURFACE 4889 # define CLOSURE_NAME eevee_closure_subsurface 4890 # define CLOSURE_DIFFUSE 4891 # define CLOSURE_SUBSURFACE 4892 #endif /* SURFACE_SUBSURFACE */ 4893 4894 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 4895 # define SURFACE_SKIN 4896 # define CLOSURE_NAME eevee_closure_skin 4897 # define CLOSURE_DIFFUSE 4898 # define CLOSURE_SUBSURFACE 4899 # define CLOSURE_GLOSSY 4900 #endif /* SURFACE_SKIN */ 4901 4902 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 4903 # define SURFACE_GLOSSY 4904 # define CLOSURE_NAME eevee_closure_glossy 4905 # define CLOSURE_GLOSSY 4906 #endif /* SURFACE_GLOSSY */ 4907 4908 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 4909 # define SURFACE_REFRACT 4910 # define CLOSURE_NAME eevee_closure_refraction 4911 # define CLOSURE_REFRACTION 4912 #endif /* SURFACE_REFRACT */ 4913 4914 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 4915 # define SURFACE_GLASS 4916 # define CLOSURE_NAME eevee_closure_glass 4917 # define CLOSURE_GLOSSY 4918 # define CLOSURE_REFRACTION 4919 #endif /* SURFACE_GLASS */ 4920 4921 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 4922 #ifdef CLOSURE_CLEARCOAT 4923 # ifndef CLOSURE_GLOSSY 4924 # define CLOSURE_GLOSSY 4925 # endif 4926 #endif /* CLOSURE_CLEARCOAT */ 4927 4928 void CLOSURE_NAME(vec3 N 4929 #ifdef CLOSURE_DIFFUSE 4930 , 4931 vec3 albedo 4932 #endif 4933 #ifdef CLOSURE_GLOSSY 4934 , 4935 vec3 f0, 4936 vec3 f90, 4937 int ssr_id 4938 #endif 4939 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 4940 , 4941 float roughness 4942 #endif 4943 #ifdef CLOSURE_CLEARCOAT 4944 , 4945 vec3 C_N, 4946 float C_intensity, 4947 float C_roughness 4948 #endif 4949 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 4950 , 4951 float ao 4952 #endif 4953 #ifdef CLOSURE_SUBSURFACE 4954 , 4955 float sss_scale 4956 #endif 4957 #ifdef CLOSURE_REFRACTION 4958 , 4959 float ior 4960 #endif 4961 , 4962 const bool use_contact_shadows 4963 #ifdef CLOSURE_DIFFUSE 4964 , 4965 out vec3 out_diff 4966 #endif 4967 #ifdef CLOSURE_GLOSSY 4968 , 4969 out vec3 out_spec 4970 #endif 4971 #ifdef CLOSURE_REFRACTION 4972 , 4973 out vec3 out_refr 4974 #endif 4975 #ifdef CLOSURE_GLOSSY 4976 , 4977 out vec3 ssr_spec 4978 #endif 4979 ) 4980 { 4981 #ifdef CLOSURE_DIFFUSE 4982 out_diff = vec3(0.0); 4983 #endif 4984 4985 #ifdef CLOSURE_GLOSSY 4986 out_spec = vec3(0.0); 4987 #endif 4988 4989 #ifdef CLOSURE_REFRACTION 4990 out_refr = vec3(0.0); 4991 #endif 4992 4993 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 4994 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 4995 return; 4996 #else 4997 4998 /* Zero length vectors cause issues, see: T51979. */ 4999 float len = length(N); 5000 if (isnan(len)) { 5001 return; 5002 } 5003 N /= len; 5004 5005 # ifdef CLOSURE_CLEARCOAT 5006 len = length(C_N); 5007 if (isnan(len)) { 5008 return; 5009 } 5010 C_N /= len; 5011 # endif 5012 5013 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 5014 roughness = clamp(roughness, 1e-8, 0.9999); 5015 float roughnessSquared = roughness * roughness; 5016 # endif 5017 5018 # ifdef CLOSURE_CLEARCOAT 5019 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 5020 float C_roughnessSquared = C_roughness * C_roughness; 5021 # endif 5022 5023 vec3 V = cameraVec; 5024 5025 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 5026 5027 /* ---------------------------------------------------------------- */ 5028 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 5029 /* ---------------------------------------------------------------- */ 5030 5031 # ifdef CLOSURE_GLOSSY 5032 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 5033 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 5034 # endif 5035 5036 # ifdef CLOSURE_CLEARCOAT 5037 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 5038 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 5039 vec3 out_spec_clear = vec3(0.0); 5040 # endif 5041 5042 float tracing_depth = gl_FragCoord.z; 5043 /* Constant bias (due to depth buffer precision) */ 5044 /* Magic numbers for 24bits of precision. 5045 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 5046 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 5047 /* Convert to view Z. */ 5048 tracing_depth = get_view_z_from_depth(tracing_depth); 5049 5050 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 5051 5052 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 5053 LightData ld = lights_data[i]; 5054 5055 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 5056 l_vector.xyz = ld.l_position - worldPosition; 5057 l_vector.w = length(l_vector.xyz); 5058 5059 float l_vis = light_visibility(ld, 5060 worldPosition, 5061 viewPosition, 5062 tracing_depth, 5063 true_normal, 5064 rand.x, 5065 use_contact_shadows, 5066 l_vector); 5067 5068 if (l_vis < 1e-8) { 5069 continue; 5070 } 5071 5072 vec3 l_color_vis = ld.l_color * l_vis; 5073 5074 # ifdef CLOSURE_DIFFUSE 5075 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 5076 # endif 5077 5078 # ifdef CLOSURE_GLOSSY 5079 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 5080 # endif 5081 5082 # ifdef CLOSURE_CLEARCOAT 5083 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 5084 ld.l_spec; 5085 # endif 5086 } 5087 5088 # ifdef CLOSURE_GLOSSY 5089 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 5090 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 5091 # endif 5092 5093 # ifdef CLOSURE_CLEARCOAT 5094 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 5095 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 5096 out_spec += out_spec_clear * C_intensity; 5097 # endif 5098 5099 /* ---------------------------------------------------------------- */ 5100 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 5101 /* ---------------------------------------------------------------- */ 5102 5103 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 5104 * BRDF. */ 5105 # ifdef CLOSURE_GLOSSY 5106 vec4 spec_accum = vec4(0.0); 5107 # endif 5108 5109 # ifdef CLOSURE_CLEARCOAT 5110 vec4 C_spec_accum = vec4(0.0); 5111 # endif 5112 5113 # ifdef CLOSURE_REFRACTION 5114 vec4 refr_accum = vec4(0.0); 5115 # endif 5116 5117 # ifdef CLOSURE_GLOSSY 5118 /* ---------------------------- */ 5119 /* Planar Reflections */ 5120 /* ---------------------------- */ 5121 5122 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 5123 PlanarData pd = planars_data[i]; 5124 5125 /* Fade on geometric normal. */ 5126 float fade = probe_attenuation_planar( 5127 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 5128 5129 if (fade > 0.0) { 5130 if (!(ssrToggle && ssr_id == outputSsrId)) { 5131 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 5132 accumulate_light(spec, fade, spec_accum); 5133 } 5134 5135 # ifdef CLOSURE_CLEARCOAT 5136 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 5137 accumulate_light(C_spec, fade, C_spec_accum); 5138 # endif 5139 } 5140 } 5141 # endif 5142 5143 # ifdef CLOSURE_GLOSSY 5144 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 5145 # endif 5146 5147 # ifdef CLOSURE_CLEARCOAT 5148 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 5149 # endif 5150 5151 # ifdef CLOSURE_REFRACTION 5152 /* Refract the view vector using the depth heuristic. 5153 * Then later Refract a second time the already refracted 5154 * ray using the inverse ior. */ 5155 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 5156 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 5157 vec3 refr_pos = (refractionDepth > 0.0) ? 5158 line_plane_intersect( 5159 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 5160 worldPosition; 5161 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 5162 # endif 5163 5164 # ifdef CLOSURE_REFRACTION 5165 /* ---------------------------- */ 5166 /* Screen Space Refraction */ 5167 /* ---------------------------- */ 5168 # ifdef USE_REFRACTION 5169 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 5170 /* Find approximated position of the 2nd refraction event. */ 5171 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 5172 viewPosition; 5173 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 5174 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 5175 accumulate_light(trans.rgb, trans.a, refr_accum); 5176 } 5177 # endif 5178 5179 # endif 5180 5181 /* ---------------------------- */ 5182 /* Specular probes */ 5183 /* ---------------------------- */ 5184 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 5185 5186 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 5187 # define GLASS_ACCUM 1 5188 # define ACCUM min(refr_accum.a, spec_accum.a) 5189 # elif defined(CLOSURE_REFRACTION) 5190 # define GLASS_ACCUM 0 5191 # define ACCUM refr_accum.a 5192 # else 5193 # define GLASS_ACCUM 0 5194 # define ACCUM spec_accum.a 5195 # endif 5196 5197 /* Starts at 1 because 0 is world probe */ 5198 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 5199 float fade = probe_attenuation_cube(i, worldPosition); 5200 5201 if (fade > 0.0) { 5202 5203 # if GLASS_ACCUM 5204 if (spec_accum.a < 0.999) { 5205 # endif 5206 # ifdef CLOSURE_GLOSSY 5207 if (!(ssrToggle && ssr_id == outputSsrId)) { 5208 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 5209 accumulate_light(spec, fade, spec_accum); 5210 } 5211 # endif 5212 5213 # ifdef CLOSURE_CLEARCOAT 5214 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 5215 accumulate_light(C_spec, fade, C_spec_accum); 5216 # endif 5217 # if GLASS_ACCUM 5218 } 5219 # endif 5220 5221 # if GLASS_ACCUM 5222 if (refr_accum.a < 0.999) { 5223 # endif 5224 # ifdef CLOSURE_REFRACTION 5225 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 5226 accumulate_light(trans, fade, refr_accum); 5227 # endif 5228 # if GLASS_ACCUM 5229 } 5230 # endif 5231 } 5232 } 5233 5234 # undef GLASS_ACCUM 5235 # undef ACCUM 5236 5237 /* ---------------------------- */ 5238 /* World Probe */ 5239 /* ---------------------------- */ 5240 # ifdef CLOSURE_GLOSSY 5241 if (spec_accum.a < 0.999) { 5242 if (!(ssrToggle && ssr_id == outputSsrId)) { 5243 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 5244 accumulate_light(spec, 1.0, spec_accum); 5245 } 5246 5247 # ifdef CLOSURE_CLEARCOAT 5248 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 5249 accumulate_light(C_spec, 1.0, C_spec_accum); 5250 # endif 5251 } 5252 # endif 5253 5254 # ifdef CLOSURE_REFRACTION 5255 if (refr_accum.a < 0.999) { 5256 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 5257 accumulate_light(trans, 1.0, refr_accum); 5258 } 5259 # endif 5260 # endif /* Specular probes */ 5261 5262 /* ---------------------------- */ 5263 /* Ambient Occlusion */ 5264 /* ---------------------------- */ 5265 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 5266 if (!use_contact_shadows) { 5267 /* HACK: Fix for translucent BSDF. (see T65631) */ 5268 N = -N; 5269 } 5270 vec3 bent_normal; 5271 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 5272 if (!use_contact_shadows) { 5273 N = -N; 5274 /* Bypass bent normal. */ 5275 bent_normal = N; 5276 } 5277 # endif 5278 5279 /* ---------------------------- */ 5280 /* Specular Output */ 5281 /* ---------------------------- */ 5282 float NV = dot(N, V); 5283 # ifdef CLOSURE_GLOSSY 5284 vec2 uv = lut_coords(NV, roughness); 5285 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 5286 5287 /* This factor is outputted to be used by SSR in order 5288 * to match the intensity of the regular reflections. */ 5289 ssr_spec = F_ibl(f0, f90, brdf_lut); 5290 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 5291 5292 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 5293 if (ssrToggle && ssr_id == outputSsrId) { 5294 spec_occlu = 1.0; 5295 } 5296 5297 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 5298 # endif 5299 5300 # ifdef CLOSURE_REFRACTION 5301 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 5302 5303 out_refr += refr_accum.rgb * btdf; 5304 # endif 5305 5306 # ifdef CLOSURE_CLEARCOAT 5307 NV = dot(C_N, V); 5308 vec2 C_uv = lut_coords(NV, C_roughness); 5309 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 5310 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 5311 specular_occlusion(NV, final_ao, C_roughness); 5312 5313 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 5314 # endif 5315 5316 # ifdef CLOSURE_GLOSSY 5317 /* Global toggle for lightprobe baking. */ 5318 out_spec *= float(specToggle); 5319 # endif 5320 5321 /* ---------------------------------------------------------------- */ 5322 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 5323 /* ---------------------------------------------------------------- */ 5324 5325 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 5326 # ifdef CLOSURE_DIFFUSE 5327 vec4 diff_accum = vec4(0.0); 5328 5329 /* ---------------------------- */ 5330 /* Irradiance Grids */ 5331 /* ---------------------------- */ 5332 /* Start at 1 because 0 is world irradiance */ 5333 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 5334 GridData gd = grids_data[i]; 5335 5336 vec3 localpos; 5337 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 5338 5339 if (fade > 0.0) { 5340 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 5341 accumulate_light(diff, fade, diff_accum); 5342 } 5343 } 5344 5345 /* ---------------------------- */ 5346 /* World Diffuse */ 5347 /* ---------------------------- */ 5348 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 5349 vec3 diff = probe_evaluate_world_diff(bent_normal); 5350 accumulate_light(diff, 1.0, diff_accum); 5351 } 5352 5353 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 5354 # endif 5355 #endif 5356 } 5357 5358 /* Cleanup for next configuration */ 5359 #undef CLOSURE_NAME 5360 5361 #ifdef CLOSURE_DIFFUSE 5362 # undef CLOSURE_DIFFUSE 5363 #endif 5364 5365 #ifdef CLOSURE_GLOSSY 5366 # undef CLOSURE_GLOSSY 5367 #endif 5368 5369 #ifdef CLOSURE_CLEARCOAT 5370 # undef CLOSURE_CLEARCOAT 5371 #endif 5372 5373 #ifdef CLOSURE_REFRACTION 5374 # undef CLOSURE_REFRACTION 5375 #endif 5376 5377 #ifdef CLOSURE_SUBSURFACE 5378 # undef CLOSURE_SUBSURFACE 5379 #endif 5380 5381 #ifndef LIT_SURFACE_UNIFORM 5382 # define LIT_SURFACE_UNIFORM 5383 5384 uniform float refractionDepth; 5385 5386 # ifndef UTIL_TEX 5387 # define UTIL_TEX 5388 uniform sampler2DArray utilTex; 5389 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 5390 # endif /* UTIL_TEX */ 5391 5392 in vec3 worldPosition; 5393 in vec3 viewPosition; 5394 5395 in vec3 worldNormal; 5396 in vec3 viewNormal; 5397 5398 # ifdef HAIR_SHADER 5399 in vec3 hairTangent; /* world space */ 5400 in float hairThickTime; 5401 in float hairThickness; 5402 in float hairTime; 5403 flat in int hairStrandID; 5404 5405 uniform int hairThicknessRes = 1; 5406 # endif 5407 5408 #endif /* LIT_SURFACE_UNIFORM */ 5409 5410 /** 5411 * AUTO CONFIG 5412 * We include the file multiple times each time with a different configuration. 5413 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 5414 */ 5415 #if !defined(SURFACE_DEFAULT) 5416 # define SURFACE_DEFAULT 5417 # define CLOSURE_NAME eevee_closure_default 5418 # define CLOSURE_DIFFUSE 5419 # define CLOSURE_GLOSSY 5420 #endif /* SURFACE_DEFAULT */ 5421 5422 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 5423 # define SURFACE_DEFAULT_CLEARCOAT 5424 # define CLOSURE_NAME eevee_closure_default_clearcoat 5425 # define CLOSURE_DIFFUSE 5426 # define CLOSURE_GLOSSY 5427 # define CLOSURE_CLEARCOAT 5428 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 5429 5430 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 5431 # define SURFACE_PRINCIPLED 5432 # define CLOSURE_NAME eevee_closure_principled 5433 # define CLOSURE_DIFFUSE 5434 # define CLOSURE_GLOSSY 5435 # define CLOSURE_CLEARCOAT 5436 # define CLOSURE_REFRACTION 5437 # define CLOSURE_SUBSURFACE 5438 #endif /* SURFACE_PRINCIPLED */ 5439 5440 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 5441 # define SURFACE_CLEARCOAT 5442 # define CLOSURE_NAME eevee_closure_clearcoat 5443 # define CLOSURE_GLOSSY 5444 # define CLOSURE_CLEARCOAT 5445 #endif /* SURFACE_CLEARCOAT */ 5446 5447 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 5448 # define SURFACE_DIFFUSE 5449 # define CLOSURE_NAME eevee_closure_diffuse 5450 # define CLOSURE_DIFFUSE 5451 #endif /* SURFACE_DIFFUSE */ 5452 5453 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 5454 # define SURFACE_SUBSURFACE 5455 # define CLOSURE_NAME eevee_closure_subsurface 5456 # define CLOSURE_DIFFUSE 5457 # define CLOSURE_SUBSURFACE 5458 #endif /* SURFACE_SUBSURFACE */ 5459 5460 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 5461 # define SURFACE_SKIN 5462 # define CLOSURE_NAME eevee_closure_skin 5463 # define CLOSURE_DIFFUSE 5464 # define CLOSURE_SUBSURFACE 5465 # define CLOSURE_GLOSSY 5466 #endif /* SURFACE_SKIN */ 5467 5468 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 5469 # define SURFACE_GLOSSY 5470 # define CLOSURE_NAME eevee_closure_glossy 5471 # define CLOSURE_GLOSSY 5472 #endif /* SURFACE_GLOSSY */ 5473 5474 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 5475 # define SURFACE_REFRACT 5476 # define CLOSURE_NAME eevee_closure_refraction 5477 # define CLOSURE_REFRACTION 5478 #endif /* SURFACE_REFRACT */ 5479 5480 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 5481 # define SURFACE_GLASS 5482 # define CLOSURE_NAME eevee_closure_glass 5483 # define CLOSURE_GLOSSY 5484 # define CLOSURE_REFRACTION 5485 #endif /* SURFACE_GLASS */ 5486 5487 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 5488 #ifdef CLOSURE_CLEARCOAT 5489 # ifndef CLOSURE_GLOSSY 5490 # define CLOSURE_GLOSSY 5491 # endif 5492 #endif /* CLOSURE_CLEARCOAT */ 5493 5494 void CLOSURE_NAME(vec3 N 5495 #ifdef CLOSURE_DIFFUSE 5496 , 5497 vec3 albedo 5498 #endif 5499 #ifdef CLOSURE_GLOSSY 5500 , 5501 vec3 f0, 5502 vec3 f90, 5503 int ssr_id 5504 #endif 5505 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 5506 , 5507 float roughness 5508 #endif 5509 #ifdef CLOSURE_CLEARCOAT 5510 , 5511 vec3 C_N, 5512 float C_intensity, 5513 float C_roughness 5514 #endif 5515 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 5516 , 5517 float ao 5518 #endif 5519 #ifdef CLOSURE_SUBSURFACE 5520 , 5521 float sss_scale 5522 #endif 5523 #ifdef CLOSURE_REFRACTION 5524 , 5525 float ior 5526 #endif 5527 , 5528 const bool use_contact_shadows 5529 #ifdef CLOSURE_DIFFUSE 5530 , 5531 out vec3 out_diff 5532 #endif 5533 #ifdef CLOSURE_GLOSSY 5534 , 5535 out vec3 out_spec 5536 #endif 5537 #ifdef CLOSURE_REFRACTION 5538 , 5539 out vec3 out_refr 5540 #endif 5541 #ifdef CLOSURE_GLOSSY 5542 , 5543 out vec3 ssr_spec 5544 #endif 5545 ) 5546 { 5547 #ifdef CLOSURE_DIFFUSE 5548 out_diff = vec3(0.0); 5549 #endif 5550 5551 #ifdef CLOSURE_GLOSSY 5552 out_spec = vec3(0.0); 5553 #endif 5554 5555 #ifdef CLOSURE_REFRACTION 5556 out_refr = vec3(0.0); 5557 #endif 5558 5559 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 5560 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 5561 return; 5562 #else 5563 5564 /* Zero length vectors cause issues, see: T51979. */ 5565 float len = length(N); 5566 if (isnan(len)) { 5567 return; 5568 } 5569 N /= len; 5570 5571 # ifdef CLOSURE_CLEARCOAT 5572 len = length(C_N); 5573 if (isnan(len)) { 5574 return; 5575 } 5576 C_N /= len; 5577 # endif 5578 5579 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 5580 roughness = clamp(roughness, 1e-8, 0.9999); 5581 float roughnessSquared = roughness * roughness; 5582 # endif 5583 5584 # ifdef CLOSURE_CLEARCOAT 5585 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 5586 float C_roughnessSquared = C_roughness * C_roughness; 5587 # endif 5588 5589 vec3 V = cameraVec; 5590 5591 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 5592 5593 /* ---------------------------------------------------------------- */ 5594 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 5595 /* ---------------------------------------------------------------- */ 5596 5597 # ifdef CLOSURE_GLOSSY 5598 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 5599 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 5600 # endif 5601 5602 # ifdef CLOSURE_CLEARCOAT 5603 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 5604 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 5605 vec3 out_spec_clear = vec3(0.0); 5606 # endif 5607 5608 float tracing_depth = gl_FragCoord.z; 5609 /* Constant bias (due to depth buffer precision) */ 5610 /* Magic numbers for 24bits of precision. 5611 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 5612 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 5613 /* Convert to view Z. */ 5614 tracing_depth = get_view_z_from_depth(tracing_depth); 5615 5616 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 5617 5618 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 5619 LightData ld = lights_data[i]; 5620 5621 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 5622 l_vector.xyz = ld.l_position - worldPosition; 5623 l_vector.w = length(l_vector.xyz); 5624 5625 float l_vis = light_visibility(ld, 5626 worldPosition, 5627 viewPosition, 5628 tracing_depth, 5629 true_normal, 5630 rand.x, 5631 use_contact_shadows, 5632 l_vector); 5633 5634 if (l_vis < 1e-8) { 5635 continue; 5636 } 5637 5638 vec3 l_color_vis = ld.l_color * l_vis; 5639 5640 # ifdef CLOSURE_DIFFUSE 5641 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 5642 # endif 5643 5644 # ifdef CLOSURE_GLOSSY 5645 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 5646 # endif 5647 5648 # ifdef CLOSURE_CLEARCOAT 5649 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 5650 ld.l_spec; 5651 # endif 5652 } 5653 5654 # ifdef CLOSURE_GLOSSY 5655 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 5656 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 5657 # endif 5658 5659 # ifdef CLOSURE_CLEARCOAT 5660 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 5661 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 5662 out_spec += out_spec_clear * C_intensity; 5663 # endif 5664 5665 /* ---------------------------------------------------------------- */ 5666 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 5667 /* ---------------------------------------------------------------- */ 5668 5669 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 5670 * BRDF. */ 5671 # ifdef CLOSURE_GLOSSY 5672 vec4 spec_accum = vec4(0.0); 5673 # endif 5674 5675 # ifdef CLOSURE_CLEARCOAT 5676 vec4 C_spec_accum = vec4(0.0); 5677 # endif 5678 5679 # ifdef CLOSURE_REFRACTION 5680 vec4 refr_accum = vec4(0.0); 5681 # endif 5682 5683 # ifdef CLOSURE_GLOSSY 5684 /* ---------------------------- */ 5685 /* Planar Reflections */ 5686 /* ---------------------------- */ 5687 5688 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 5689 PlanarData pd = planars_data[i]; 5690 5691 /* Fade on geometric normal. */ 5692 float fade = probe_attenuation_planar( 5693 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 5694 5695 if (fade > 0.0) { 5696 if (!(ssrToggle && ssr_id == outputSsrId)) { 5697 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 5698 accumulate_light(spec, fade, spec_accum); 5699 } 5700 5701 # ifdef CLOSURE_CLEARCOAT 5702 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 5703 accumulate_light(C_spec, fade, C_spec_accum); 5704 # endif 5705 } 5706 } 5707 # endif 5708 5709 # ifdef CLOSURE_GLOSSY 5710 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 5711 # endif 5712 5713 # ifdef CLOSURE_CLEARCOAT 5714 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 5715 # endif 5716 5717 # ifdef CLOSURE_REFRACTION 5718 /* Refract the view vector using the depth heuristic. 5719 * Then later Refract a second time the already refracted 5720 * ray using the inverse ior. */ 5721 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 5722 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 5723 vec3 refr_pos = (refractionDepth > 0.0) ? 5724 line_plane_intersect( 5725 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 5726 worldPosition; 5727 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 5728 # endif 5729 5730 # ifdef CLOSURE_REFRACTION 5731 /* ---------------------------- */ 5732 /* Screen Space Refraction */ 5733 /* ---------------------------- */ 5734 # ifdef USE_REFRACTION 5735 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 5736 /* Find approximated position of the 2nd refraction event. */ 5737 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 5738 viewPosition; 5739 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 5740 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 5741 accumulate_light(trans.rgb, trans.a, refr_accum); 5742 } 5743 # endif 5744 5745 # endif 5746 5747 /* ---------------------------- */ 5748 /* Specular probes */ 5749 /* ---------------------------- */ 5750 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 5751 5752 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 5753 # define GLASS_ACCUM 1 5754 # define ACCUM min(refr_accum.a, spec_accum.a) 5755 # elif defined(CLOSURE_REFRACTION) 5756 # define GLASS_ACCUM 0 5757 # define ACCUM refr_accum.a 5758 # else 5759 # define GLASS_ACCUM 0 5760 # define ACCUM spec_accum.a 5761 # endif 5762 5763 /* Starts at 1 because 0 is world probe */ 5764 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 5765 float fade = probe_attenuation_cube(i, worldPosition); 5766 5767 if (fade > 0.0) { 5768 5769 # if GLASS_ACCUM 5770 if (spec_accum.a < 0.999) { 5771 # endif 5772 # ifdef CLOSURE_GLOSSY 5773 if (!(ssrToggle && ssr_id == outputSsrId)) { 5774 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 5775 accumulate_light(spec, fade, spec_accum); 5776 } 5777 # endif 5778 5779 # ifdef CLOSURE_CLEARCOAT 5780 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 5781 accumulate_light(C_spec, fade, C_spec_accum); 5782 # endif 5783 # if GLASS_ACCUM 5784 } 5785 # endif 5786 5787 # if GLASS_ACCUM 5788 if (refr_accum.a < 0.999) { 5789 # endif 5790 # ifdef CLOSURE_REFRACTION 5791 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 5792 accumulate_light(trans, fade, refr_accum); 5793 # endif 5794 # if GLASS_ACCUM 5795 } 5796 # endif 5797 } 5798 } 5799 5800 # undef GLASS_ACCUM 5801 # undef ACCUM 5802 5803 /* ---------------------------- */ 5804 /* World Probe */ 5805 /* ---------------------------- */ 5806 # ifdef CLOSURE_GLOSSY 5807 if (spec_accum.a < 0.999) { 5808 if (!(ssrToggle && ssr_id == outputSsrId)) { 5809 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 5810 accumulate_light(spec, 1.0, spec_accum); 5811 } 5812 5813 # ifdef CLOSURE_CLEARCOAT 5814 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 5815 accumulate_light(C_spec, 1.0, C_spec_accum); 5816 # endif 5817 } 5818 # endif 5819 5820 # ifdef CLOSURE_REFRACTION 5821 if (refr_accum.a < 0.999) { 5822 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 5823 accumulate_light(trans, 1.0, refr_accum); 5824 } 5825 # endif 5826 # endif /* Specular probes */ 5827 5828 /* ---------------------------- */ 5829 /* Ambient Occlusion */ 5830 /* ---------------------------- */ 5831 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 5832 if (!use_contact_shadows) { 5833 /* HACK: Fix for translucent BSDF. (see T65631) */ 5834 N = -N; 5835 } 5836 vec3 bent_normal; 5837 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 5838 if (!use_contact_shadows) { 5839 N = -N; 5840 /* Bypass bent normal. */ 5841 bent_normal = N; 5842 } 5843 # endif 5844 5845 /* ---------------------------- */ 5846 /* Specular Output */ 5847 /* ---------------------------- */ 5848 float NV = dot(N, V); 5849 # ifdef CLOSURE_GLOSSY 5850 vec2 uv = lut_coords(NV, roughness); 5851 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 5852 5853 /* This factor is outputted to be used by SSR in order 5854 * to match the intensity of the regular reflections. */ 5855 ssr_spec = F_ibl(f0, f90, brdf_lut); 5856 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 5857 5858 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 5859 if (ssrToggle && ssr_id == outputSsrId) { 5860 spec_occlu = 1.0; 5861 } 5862 5863 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 5864 # endif 5865 5866 # ifdef CLOSURE_REFRACTION 5867 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 5868 5869 out_refr += refr_accum.rgb * btdf; 5870 # endif 5871 5872 # ifdef CLOSURE_CLEARCOAT 5873 NV = dot(C_N, V); 5874 vec2 C_uv = lut_coords(NV, C_roughness); 5875 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 5876 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 5877 specular_occlusion(NV, final_ao, C_roughness); 5878 5879 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 5880 # endif 5881 5882 # ifdef CLOSURE_GLOSSY 5883 /* Global toggle for lightprobe baking. */ 5884 out_spec *= float(specToggle); 5885 # endif 5886 5887 /* ---------------------------------------------------------------- */ 5888 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 5889 /* ---------------------------------------------------------------- */ 5890 5891 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 5892 # ifdef CLOSURE_DIFFUSE 5893 vec4 diff_accum = vec4(0.0); 5894 5895 /* ---------------------------- */ 5896 /* Irradiance Grids */ 5897 /* ---------------------------- */ 5898 /* Start at 1 because 0 is world irradiance */ 5899 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 5900 GridData gd = grids_data[i]; 5901 5902 vec3 localpos; 5903 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 5904 5905 if (fade > 0.0) { 5906 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 5907 accumulate_light(diff, fade, diff_accum); 5908 } 5909 } 5910 5911 /* ---------------------------- */ 5912 /* World Diffuse */ 5913 /* ---------------------------- */ 5914 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 5915 vec3 diff = probe_evaluate_world_diff(bent_normal); 5916 accumulate_light(diff, 1.0, diff_accum); 5917 } 5918 5919 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 5920 # endif 5921 #endif 5922 } 5923 5924 /* Cleanup for next configuration */ 5925 #undef CLOSURE_NAME 5926 5927 #ifdef CLOSURE_DIFFUSE 5928 # undef CLOSURE_DIFFUSE 5929 #endif 5930 5931 #ifdef CLOSURE_GLOSSY 5932 # undef CLOSURE_GLOSSY 5933 #endif 5934 5935 #ifdef CLOSURE_CLEARCOAT 5936 # undef CLOSURE_CLEARCOAT 5937 #endif 5938 5939 #ifdef CLOSURE_REFRACTION 5940 # undef CLOSURE_REFRACTION 5941 #endif 5942 5943 #ifdef CLOSURE_SUBSURFACE 5944 # undef CLOSURE_SUBSURFACE 5945 #endif 5946 5947 #ifndef LIT_SURFACE_UNIFORM 5948 # define LIT_SURFACE_UNIFORM 5949 5950 uniform float refractionDepth; 5951 5952 # ifndef UTIL_TEX 5953 # define UTIL_TEX 5954 uniform sampler2DArray utilTex; 5955 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 5956 # endif /* UTIL_TEX */ 5957 5958 in vec3 worldPosition; 5959 in vec3 viewPosition; 5960 5961 in vec3 worldNormal; 5962 in vec3 viewNormal; 5963 5964 # ifdef HAIR_SHADER 5965 in vec3 hairTangent; /* world space */ 5966 in float hairThickTime; 5967 in float hairThickness; 5968 in float hairTime; 5969 flat in int hairStrandID; 5970 5971 uniform int hairThicknessRes = 1; 5972 # endif 5973 5974 #endif /* LIT_SURFACE_UNIFORM */ 5975 5976 /** 5977 * AUTO CONFIG 5978 * We include the file multiple times each time with a different configuration. 5979 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 5980 */ 5981 #if !defined(SURFACE_DEFAULT) 5982 # define SURFACE_DEFAULT 5983 # define CLOSURE_NAME eevee_closure_default 5984 # define CLOSURE_DIFFUSE 5985 # define CLOSURE_GLOSSY 5986 #endif /* SURFACE_DEFAULT */ 5987 5988 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 5989 # define SURFACE_DEFAULT_CLEARCOAT 5990 # define CLOSURE_NAME eevee_closure_default_clearcoat 5991 # define CLOSURE_DIFFUSE 5992 # define CLOSURE_GLOSSY 5993 # define CLOSURE_CLEARCOAT 5994 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 5995 5996 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 5997 # define SURFACE_PRINCIPLED 5998 # define CLOSURE_NAME eevee_closure_principled 5999 # define CLOSURE_DIFFUSE 6000 # define CLOSURE_GLOSSY 6001 # define CLOSURE_CLEARCOAT 6002 # define CLOSURE_REFRACTION 6003 # define CLOSURE_SUBSURFACE 6004 #endif /* SURFACE_PRINCIPLED */ 6005 6006 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 6007 # define SURFACE_CLEARCOAT 6008 # define CLOSURE_NAME eevee_closure_clearcoat 6009 # define CLOSURE_GLOSSY 6010 # define CLOSURE_CLEARCOAT 6011 #endif /* SURFACE_CLEARCOAT */ 6012 6013 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 6014 # define SURFACE_DIFFUSE 6015 # define CLOSURE_NAME eevee_closure_diffuse 6016 # define CLOSURE_DIFFUSE 6017 #endif /* SURFACE_DIFFUSE */ 6018 6019 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 6020 # define SURFACE_SUBSURFACE 6021 # define CLOSURE_NAME eevee_closure_subsurface 6022 # define CLOSURE_DIFFUSE 6023 # define CLOSURE_SUBSURFACE 6024 #endif /* SURFACE_SUBSURFACE */ 6025 6026 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 6027 # define SURFACE_SKIN 6028 # define CLOSURE_NAME eevee_closure_skin 6029 # define CLOSURE_DIFFUSE 6030 # define CLOSURE_SUBSURFACE 6031 # define CLOSURE_GLOSSY 6032 #endif /* SURFACE_SKIN */ 6033 6034 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 6035 # define SURFACE_GLOSSY 6036 # define CLOSURE_NAME eevee_closure_glossy 6037 # define CLOSURE_GLOSSY 6038 #endif /* SURFACE_GLOSSY */ 6039 6040 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 6041 # define SURFACE_REFRACT 6042 # define CLOSURE_NAME eevee_closure_refraction 6043 # define CLOSURE_REFRACTION 6044 #endif /* SURFACE_REFRACT */ 6045 6046 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 6047 # define SURFACE_GLASS 6048 # define CLOSURE_NAME eevee_closure_glass 6049 # define CLOSURE_GLOSSY 6050 # define CLOSURE_REFRACTION 6051 #endif /* SURFACE_GLASS */ 6052 6053 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 6054 #ifdef CLOSURE_CLEARCOAT 6055 # ifndef CLOSURE_GLOSSY 6056 # define CLOSURE_GLOSSY 6057 # endif 6058 #endif /* CLOSURE_CLEARCOAT */ 6059 6060 void CLOSURE_NAME(vec3 N 6061 #ifdef CLOSURE_DIFFUSE 6062 , 6063 vec3 albedo 6064 #endif 6065 #ifdef CLOSURE_GLOSSY 6066 , 6067 vec3 f0, 6068 vec3 f90, 6069 int ssr_id 6070 #endif 6071 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6072 , 6073 float roughness 6074 #endif 6075 #ifdef CLOSURE_CLEARCOAT 6076 , 6077 vec3 C_N, 6078 float C_intensity, 6079 float C_roughness 6080 #endif 6081 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 6082 , 6083 float ao 6084 #endif 6085 #ifdef CLOSURE_SUBSURFACE 6086 , 6087 float sss_scale 6088 #endif 6089 #ifdef CLOSURE_REFRACTION 6090 , 6091 float ior 6092 #endif 6093 , 6094 const bool use_contact_shadows 6095 #ifdef CLOSURE_DIFFUSE 6096 , 6097 out vec3 out_diff 6098 #endif 6099 #ifdef CLOSURE_GLOSSY 6100 , 6101 out vec3 out_spec 6102 #endif 6103 #ifdef CLOSURE_REFRACTION 6104 , 6105 out vec3 out_refr 6106 #endif 6107 #ifdef CLOSURE_GLOSSY 6108 , 6109 out vec3 ssr_spec 6110 #endif 6111 ) 6112 { 6113 #ifdef CLOSURE_DIFFUSE 6114 out_diff = vec3(0.0); 6115 #endif 6116 6117 #ifdef CLOSURE_GLOSSY 6118 out_spec = vec3(0.0); 6119 #endif 6120 6121 #ifdef CLOSURE_REFRACTION 6122 out_refr = vec3(0.0); 6123 #endif 6124 6125 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 6126 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 6127 return; 6128 #else 6129 6130 /* Zero length vectors cause issues, see: T51979. */ 6131 float len = length(N); 6132 if (isnan(len)) { 6133 return; 6134 } 6135 N /= len; 6136 6137 # ifdef CLOSURE_CLEARCOAT 6138 len = length(C_N); 6139 if (isnan(len)) { 6140 return; 6141 } 6142 C_N /= len; 6143 # endif 6144 6145 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6146 roughness = clamp(roughness, 1e-8, 0.9999); 6147 float roughnessSquared = roughness * roughness; 6148 # endif 6149 6150 # ifdef CLOSURE_CLEARCOAT 6151 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 6152 float C_roughnessSquared = C_roughness * C_roughness; 6153 # endif 6154 6155 vec3 V = cameraVec; 6156 6157 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 6158 6159 /* ---------------------------------------------------------------- */ 6160 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 6161 /* ---------------------------------------------------------------- */ 6162 6163 # ifdef CLOSURE_GLOSSY 6164 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 6165 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 6166 # endif 6167 6168 # ifdef CLOSURE_CLEARCOAT 6169 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 6170 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 6171 vec3 out_spec_clear = vec3(0.0); 6172 # endif 6173 6174 float tracing_depth = gl_FragCoord.z; 6175 /* Constant bias (due to depth buffer precision) */ 6176 /* Magic numbers for 24bits of precision. 6177 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 6178 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 6179 /* Convert to view Z. */ 6180 tracing_depth = get_view_z_from_depth(tracing_depth); 6181 6182 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 6183 6184 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 6185 LightData ld = lights_data[i]; 6186 6187 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 6188 l_vector.xyz = ld.l_position - worldPosition; 6189 l_vector.w = length(l_vector.xyz); 6190 6191 float l_vis = light_visibility(ld, 6192 worldPosition, 6193 viewPosition, 6194 tracing_depth, 6195 true_normal, 6196 rand.x, 6197 use_contact_shadows, 6198 l_vector); 6199 6200 if (l_vis < 1e-8) { 6201 continue; 6202 } 6203 6204 vec3 l_color_vis = ld.l_color * l_vis; 6205 6206 # ifdef CLOSURE_DIFFUSE 6207 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 6208 # endif 6209 6210 # ifdef CLOSURE_GLOSSY 6211 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 6212 # endif 6213 6214 # ifdef CLOSURE_CLEARCOAT 6215 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 6216 ld.l_spec; 6217 # endif 6218 } 6219 6220 # ifdef CLOSURE_GLOSSY 6221 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 6222 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 6223 # endif 6224 6225 # ifdef CLOSURE_CLEARCOAT 6226 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 6227 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 6228 out_spec += out_spec_clear * C_intensity; 6229 # endif 6230 6231 /* ---------------------------------------------------------------- */ 6232 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 6233 /* ---------------------------------------------------------------- */ 6234 6235 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 6236 * BRDF. */ 6237 # ifdef CLOSURE_GLOSSY 6238 vec4 spec_accum = vec4(0.0); 6239 # endif 6240 6241 # ifdef CLOSURE_CLEARCOAT 6242 vec4 C_spec_accum = vec4(0.0); 6243 # endif 6244 6245 # ifdef CLOSURE_REFRACTION 6246 vec4 refr_accum = vec4(0.0); 6247 # endif 6248 6249 # ifdef CLOSURE_GLOSSY 6250 /* ---------------------------- */ 6251 /* Planar Reflections */ 6252 /* ---------------------------- */ 6253 6254 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 6255 PlanarData pd = planars_data[i]; 6256 6257 /* Fade on geometric normal. */ 6258 float fade = probe_attenuation_planar( 6259 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 6260 6261 if (fade > 0.0) { 6262 if (!(ssrToggle && ssr_id == outputSsrId)) { 6263 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 6264 accumulate_light(spec, fade, spec_accum); 6265 } 6266 6267 # ifdef CLOSURE_CLEARCOAT 6268 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 6269 accumulate_light(C_spec, fade, C_spec_accum); 6270 # endif 6271 } 6272 } 6273 # endif 6274 6275 # ifdef CLOSURE_GLOSSY 6276 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 6277 # endif 6278 6279 # ifdef CLOSURE_CLEARCOAT 6280 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 6281 # endif 6282 6283 # ifdef CLOSURE_REFRACTION 6284 /* Refract the view vector using the depth heuristic. 6285 * Then later Refract a second time the already refracted 6286 * ray using the inverse ior. */ 6287 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 6288 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 6289 vec3 refr_pos = (refractionDepth > 0.0) ? 6290 line_plane_intersect( 6291 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 6292 worldPosition; 6293 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 6294 # endif 6295 6296 # ifdef CLOSURE_REFRACTION 6297 /* ---------------------------- */ 6298 /* Screen Space Refraction */ 6299 /* ---------------------------- */ 6300 # ifdef USE_REFRACTION 6301 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 6302 /* Find approximated position of the 2nd refraction event. */ 6303 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 6304 viewPosition; 6305 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 6306 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 6307 accumulate_light(trans.rgb, trans.a, refr_accum); 6308 } 6309 # endif 6310 6311 # endif 6312 6313 /* ---------------------------- */ 6314 /* Specular probes */ 6315 /* ---------------------------- */ 6316 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6317 6318 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 6319 # define GLASS_ACCUM 1 6320 # define ACCUM min(refr_accum.a, spec_accum.a) 6321 # elif defined(CLOSURE_REFRACTION) 6322 # define GLASS_ACCUM 0 6323 # define ACCUM refr_accum.a 6324 # else 6325 # define GLASS_ACCUM 0 6326 # define ACCUM spec_accum.a 6327 # endif 6328 6329 /* Starts at 1 because 0 is world probe */ 6330 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 6331 float fade = probe_attenuation_cube(i, worldPosition); 6332 6333 if (fade > 0.0) { 6334 6335 # if GLASS_ACCUM 6336 if (spec_accum.a < 0.999) { 6337 # endif 6338 # ifdef CLOSURE_GLOSSY 6339 if (!(ssrToggle && ssr_id == outputSsrId)) { 6340 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 6341 accumulate_light(spec, fade, spec_accum); 6342 } 6343 # endif 6344 6345 # ifdef CLOSURE_CLEARCOAT 6346 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 6347 accumulate_light(C_spec, fade, C_spec_accum); 6348 # endif 6349 # if GLASS_ACCUM 6350 } 6351 # endif 6352 6353 # if GLASS_ACCUM 6354 if (refr_accum.a < 0.999) { 6355 # endif 6356 # ifdef CLOSURE_REFRACTION 6357 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 6358 accumulate_light(trans, fade, refr_accum); 6359 # endif 6360 # if GLASS_ACCUM 6361 } 6362 # endif 6363 } 6364 } 6365 6366 # undef GLASS_ACCUM 6367 # undef ACCUM 6368 6369 /* ---------------------------- */ 6370 /* World Probe */ 6371 /* ---------------------------- */ 6372 # ifdef CLOSURE_GLOSSY 6373 if (spec_accum.a < 0.999) { 6374 if (!(ssrToggle && ssr_id == outputSsrId)) { 6375 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 6376 accumulate_light(spec, 1.0, spec_accum); 6377 } 6378 6379 # ifdef CLOSURE_CLEARCOAT 6380 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 6381 accumulate_light(C_spec, 1.0, C_spec_accum); 6382 # endif 6383 } 6384 # endif 6385 6386 # ifdef CLOSURE_REFRACTION 6387 if (refr_accum.a < 0.999) { 6388 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 6389 accumulate_light(trans, 1.0, refr_accum); 6390 } 6391 # endif 6392 # endif /* Specular probes */ 6393 6394 /* ---------------------------- */ 6395 /* Ambient Occlusion */ 6396 /* ---------------------------- */ 6397 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 6398 if (!use_contact_shadows) { 6399 /* HACK: Fix for translucent BSDF. (see T65631) */ 6400 N = -N; 6401 } 6402 vec3 bent_normal; 6403 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 6404 if (!use_contact_shadows) { 6405 N = -N; 6406 /* Bypass bent normal. */ 6407 bent_normal = N; 6408 } 6409 # endif 6410 6411 /* ---------------------------- */ 6412 /* Specular Output */ 6413 /* ---------------------------- */ 6414 float NV = dot(N, V); 6415 # ifdef CLOSURE_GLOSSY 6416 vec2 uv = lut_coords(NV, roughness); 6417 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 6418 6419 /* This factor is outputted to be used by SSR in order 6420 * to match the intensity of the regular reflections. */ 6421 ssr_spec = F_ibl(f0, f90, brdf_lut); 6422 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 6423 6424 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 6425 if (ssrToggle && ssr_id == outputSsrId) { 6426 spec_occlu = 1.0; 6427 } 6428 6429 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 6430 # endif 6431 6432 # ifdef CLOSURE_REFRACTION 6433 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 6434 6435 out_refr += refr_accum.rgb * btdf; 6436 # endif 6437 6438 # ifdef CLOSURE_CLEARCOAT 6439 NV = dot(C_N, V); 6440 vec2 C_uv = lut_coords(NV, C_roughness); 6441 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 6442 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 6443 specular_occlusion(NV, final_ao, C_roughness); 6444 6445 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 6446 # endif 6447 6448 # ifdef CLOSURE_GLOSSY 6449 /* Global toggle for lightprobe baking. */ 6450 out_spec *= float(specToggle); 6451 # endif 6452 6453 /* ---------------------------------------------------------------- */ 6454 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 6455 /* ---------------------------------------------------------------- */ 6456 6457 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 6458 # ifdef CLOSURE_DIFFUSE 6459 vec4 diff_accum = vec4(0.0); 6460 6461 /* ---------------------------- */ 6462 /* Irradiance Grids */ 6463 /* ---------------------------- */ 6464 /* Start at 1 because 0 is world irradiance */ 6465 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 6466 GridData gd = grids_data[i]; 6467 6468 vec3 localpos; 6469 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 6470 6471 if (fade > 0.0) { 6472 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 6473 accumulate_light(diff, fade, diff_accum); 6474 } 6475 } 6476 6477 /* ---------------------------- */ 6478 /* World Diffuse */ 6479 /* ---------------------------- */ 6480 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 6481 vec3 diff = probe_evaluate_world_diff(bent_normal); 6482 accumulate_light(diff, 1.0, diff_accum); 6483 } 6484 6485 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 6486 # endif 6487 #endif 6488 } 6489 6490 /* Cleanup for next configuration */ 6491 #undef CLOSURE_NAME 6492 6493 #ifdef CLOSURE_DIFFUSE 6494 # undef CLOSURE_DIFFUSE 6495 #endif 6496 6497 #ifdef CLOSURE_GLOSSY 6498 # undef CLOSURE_GLOSSY 6499 #endif 6500 6501 #ifdef CLOSURE_CLEARCOAT 6502 # undef CLOSURE_CLEARCOAT 6503 #endif 6504 6505 #ifdef CLOSURE_REFRACTION 6506 # undef CLOSURE_REFRACTION 6507 #endif 6508 6509 #ifdef CLOSURE_SUBSURFACE 6510 # undef CLOSURE_SUBSURFACE 6511 #endif 6512 6513 #ifndef LIT_SURFACE_UNIFORM 6514 # define LIT_SURFACE_UNIFORM 6515 6516 uniform float refractionDepth; 6517 6518 # ifndef UTIL_TEX 6519 # define UTIL_TEX 6520 uniform sampler2DArray utilTex; 6521 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 6522 # endif /* UTIL_TEX */ 6523 6524 in vec3 worldPosition; 6525 in vec3 viewPosition; 6526 6527 in vec3 worldNormal; 6528 in vec3 viewNormal; 6529 6530 # ifdef HAIR_SHADER 6531 in vec3 hairTangent; /* world space */ 6532 in float hairThickTime; 6533 in float hairThickness; 6534 in float hairTime; 6535 flat in int hairStrandID; 6536 6537 uniform int hairThicknessRes = 1; 6538 # endif 6539 6540 #endif /* LIT_SURFACE_UNIFORM */ 6541 6542 /** 6543 * AUTO CONFIG 6544 * We include the file multiple times each time with a different configuration. 6545 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 6546 */ 6547 #if !defined(SURFACE_DEFAULT) 6548 # define SURFACE_DEFAULT 6549 # define CLOSURE_NAME eevee_closure_default 6550 # define CLOSURE_DIFFUSE 6551 # define CLOSURE_GLOSSY 6552 #endif /* SURFACE_DEFAULT */ 6553 6554 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 6555 # define SURFACE_DEFAULT_CLEARCOAT 6556 # define CLOSURE_NAME eevee_closure_default_clearcoat 6557 # define CLOSURE_DIFFUSE 6558 # define CLOSURE_GLOSSY 6559 # define CLOSURE_CLEARCOAT 6560 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 6561 6562 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 6563 # define SURFACE_PRINCIPLED 6564 # define CLOSURE_NAME eevee_closure_principled 6565 # define CLOSURE_DIFFUSE 6566 # define CLOSURE_GLOSSY 6567 # define CLOSURE_CLEARCOAT 6568 # define CLOSURE_REFRACTION 6569 # define CLOSURE_SUBSURFACE 6570 #endif /* SURFACE_PRINCIPLED */ 6571 6572 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 6573 # define SURFACE_CLEARCOAT 6574 # define CLOSURE_NAME eevee_closure_clearcoat 6575 # define CLOSURE_GLOSSY 6576 # define CLOSURE_CLEARCOAT 6577 #endif /* SURFACE_CLEARCOAT */ 6578 6579 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 6580 # define SURFACE_DIFFUSE 6581 # define CLOSURE_NAME eevee_closure_diffuse 6582 # define CLOSURE_DIFFUSE 6583 #endif /* SURFACE_DIFFUSE */ 6584 6585 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 6586 # define SURFACE_SUBSURFACE 6587 # define CLOSURE_NAME eevee_closure_subsurface 6588 # define CLOSURE_DIFFUSE 6589 # define CLOSURE_SUBSURFACE 6590 #endif /* SURFACE_SUBSURFACE */ 6591 6592 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 6593 # define SURFACE_SKIN 6594 # define CLOSURE_NAME eevee_closure_skin 6595 # define CLOSURE_DIFFUSE 6596 # define CLOSURE_SUBSURFACE 6597 # define CLOSURE_GLOSSY 6598 #endif /* SURFACE_SKIN */ 6599 6600 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 6601 # define SURFACE_GLOSSY 6602 # define CLOSURE_NAME eevee_closure_glossy 6603 # define CLOSURE_GLOSSY 6604 #endif /* SURFACE_GLOSSY */ 6605 6606 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 6607 # define SURFACE_REFRACT 6608 # define CLOSURE_NAME eevee_closure_refraction 6609 # define CLOSURE_REFRACTION 6610 #endif /* SURFACE_REFRACT */ 6611 6612 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 6613 # define SURFACE_GLASS 6614 # define CLOSURE_NAME eevee_closure_glass 6615 # define CLOSURE_GLOSSY 6616 # define CLOSURE_REFRACTION 6617 #endif /* SURFACE_GLASS */ 6618 6619 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 6620 #ifdef CLOSURE_CLEARCOAT 6621 # ifndef CLOSURE_GLOSSY 6622 # define CLOSURE_GLOSSY 6623 # endif 6624 #endif /* CLOSURE_CLEARCOAT */ 6625 6626 void CLOSURE_NAME(vec3 N 6627 #ifdef CLOSURE_DIFFUSE 6628 , 6629 vec3 albedo 6630 #endif 6631 #ifdef CLOSURE_GLOSSY 6632 , 6633 vec3 f0, 6634 vec3 f90, 6635 int ssr_id 6636 #endif 6637 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6638 , 6639 float roughness 6640 #endif 6641 #ifdef CLOSURE_CLEARCOAT 6642 , 6643 vec3 C_N, 6644 float C_intensity, 6645 float C_roughness 6646 #endif 6647 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 6648 , 6649 float ao 6650 #endif 6651 #ifdef CLOSURE_SUBSURFACE 6652 , 6653 float sss_scale 6654 #endif 6655 #ifdef CLOSURE_REFRACTION 6656 , 6657 float ior 6658 #endif 6659 , 6660 const bool use_contact_shadows 6661 #ifdef CLOSURE_DIFFUSE 6662 , 6663 out vec3 out_diff 6664 #endif 6665 #ifdef CLOSURE_GLOSSY 6666 , 6667 out vec3 out_spec 6668 #endif 6669 #ifdef CLOSURE_REFRACTION 6670 , 6671 out vec3 out_refr 6672 #endif 6673 #ifdef CLOSURE_GLOSSY 6674 , 6675 out vec3 ssr_spec 6676 #endif 6677 ) 6678 { 6679 #ifdef CLOSURE_DIFFUSE 6680 out_diff = vec3(0.0); 6681 #endif 6682 6683 #ifdef CLOSURE_GLOSSY 6684 out_spec = vec3(0.0); 6685 #endif 6686 6687 #ifdef CLOSURE_REFRACTION 6688 out_refr = vec3(0.0); 6689 #endif 6690 6691 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 6692 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 6693 return; 6694 #else 6695 6696 /* Zero length vectors cause issues, see: T51979. */ 6697 float len = length(N); 6698 if (isnan(len)) { 6699 return; 6700 } 6701 N /= len; 6702 6703 # ifdef CLOSURE_CLEARCOAT 6704 len = length(C_N); 6705 if (isnan(len)) { 6706 return; 6707 } 6708 C_N /= len; 6709 # endif 6710 6711 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6712 roughness = clamp(roughness, 1e-8, 0.9999); 6713 float roughnessSquared = roughness * roughness; 6714 # endif 6715 6716 # ifdef CLOSURE_CLEARCOAT 6717 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 6718 float C_roughnessSquared = C_roughness * C_roughness; 6719 # endif 6720 6721 vec3 V = cameraVec; 6722 6723 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 6724 6725 /* ---------------------------------------------------------------- */ 6726 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 6727 /* ---------------------------------------------------------------- */ 6728 6729 # ifdef CLOSURE_GLOSSY 6730 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 6731 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 6732 # endif 6733 6734 # ifdef CLOSURE_CLEARCOAT 6735 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 6736 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 6737 vec3 out_spec_clear = vec3(0.0); 6738 # endif 6739 6740 float tracing_depth = gl_FragCoord.z; 6741 /* Constant bias (due to depth buffer precision) */ 6742 /* Magic numbers for 24bits of precision. 6743 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 6744 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 6745 /* Convert to view Z. */ 6746 tracing_depth = get_view_z_from_depth(tracing_depth); 6747 6748 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 6749 6750 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 6751 LightData ld = lights_data[i]; 6752 6753 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 6754 l_vector.xyz = ld.l_position - worldPosition; 6755 l_vector.w = length(l_vector.xyz); 6756 6757 float l_vis = light_visibility(ld, 6758 worldPosition, 6759 viewPosition, 6760 tracing_depth, 6761 true_normal, 6762 rand.x, 6763 use_contact_shadows, 6764 l_vector); 6765 6766 if (l_vis < 1e-8) { 6767 continue; 6768 } 6769 6770 vec3 l_color_vis = ld.l_color * l_vis; 6771 6772 # ifdef CLOSURE_DIFFUSE 6773 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 6774 # endif 6775 6776 # ifdef CLOSURE_GLOSSY 6777 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 6778 # endif 6779 6780 # ifdef CLOSURE_CLEARCOAT 6781 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 6782 ld.l_spec; 6783 # endif 6784 } 6785 6786 # ifdef CLOSURE_GLOSSY 6787 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 6788 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 6789 # endif 6790 6791 # ifdef CLOSURE_CLEARCOAT 6792 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 6793 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 6794 out_spec += out_spec_clear * C_intensity; 6795 # endif 6796 6797 /* ---------------------------------------------------------------- */ 6798 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 6799 /* ---------------------------------------------------------------- */ 6800 6801 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 6802 * BRDF. */ 6803 # ifdef CLOSURE_GLOSSY 6804 vec4 spec_accum = vec4(0.0); 6805 # endif 6806 6807 # ifdef CLOSURE_CLEARCOAT 6808 vec4 C_spec_accum = vec4(0.0); 6809 # endif 6810 6811 # ifdef CLOSURE_REFRACTION 6812 vec4 refr_accum = vec4(0.0); 6813 # endif 6814 6815 # ifdef CLOSURE_GLOSSY 6816 /* ---------------------------- */ 6817 /* Planar Reflections */ 6818 /* ---------------------------- */ 6819 6820 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 6821 PlanarData pd = planars_data[i]; 6822 6823 /* Fade on geometric normal. */ 6824 float fade = probe_attenuation_planar( 6825 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 6826 6827 if (fade > 0.0) { 6828 if (!(ssrToggle && ssr_id == outputSsrId)) { 6829 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 6830 accumulate_light(spec, fade, spec_accum); 6831 } 6832 6833 # ifdef CLOSURE_CLEARCOAT 6834 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 6835 accumulate_light(C_spec, fade, C_spec_accum); 6836 # endif 6837 } 6838 } 6839 # endif 6840 6841 # ifdef CLOSURE_GLOSSY 6842 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 6843 # endif 6844 6845 # ifdef CLOSURE_CLEARCOAT 6846 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 6847 # endif 6848 6849 # ifdef CLOSURE_REFRACTION 6850 /* Refract the view vector using the depth heuristic. 6851 * Then later Refract a second time the already refracted 6852 * ray using the inverse ior. */ 6853 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 6854 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 6855 vec3 refr_pos = (refractionDepth > 0.0) ? 6856 line_plane_intersect( 6857 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 6858 worldPosition; 6859 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 6860 # endif 6861 6862 # ifdef CLOSURE_REFRACTION 6863 /* ---------------------------- */ 6864 /* Screen Space Refraction */ 6865 /* ---------------------------- */ 6866 # ifdef USE_REFRACTION 6867 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 6868 /* Find approximated position of the 2nd refraction event. */ 6869 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 6870 viewPosition; 6871 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 6872 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 6873 accumulate_light(trans.rgb, trans.a, refr_accum); 6874 } 6875 # endif 6876 6877 # endif 6878 6879 /* ---------------------------- */ 6880 /* Specular probes */ 6881 /* ---------------------------- */ 6882 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 6883 6884 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 6885 # define GLASS_ACCUM 1 6886 # define ACCUM min(refr_accum.a, spec_accum.a) 6887 # elif defined(CLOSURE_REFRACTION) 6888 # define GLASS_ACCUM 0 6889 # define ACCUM refr_accum.a 6890 # else 6891 # define GLASS_ACCUM 0 6892 # define ACCUM spec_accum.a 6893 # endif 6894 6895 /* Starts at 1 because 0 is world probe */ 6896 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 6897 float fade = probe_attenuation_cube(i, worldPosition); 6898 6899 if (fade > 0.0) { 6900 6901 # if GLASS_ACCUM 6902 if (spec_accum.a < 0.999) { 6903 # endif 6904 # ifdef CLOSURE_GLOSSY 6905 if (!(ssrToggle && ssr_id == outputSsrId)) { 6906 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 6907 accumulate_light(spec, fade, spec_accum); 6908 } 6909 # endif 6910 6911 # ifdef CLOSURE_CLEARCOAT 6912 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 6913 accumulate_light(C_spec, fade, C_spec_accum); 6914 # endif 6915 # if GLASS_ACCUM 6916 } 6917 # endif 6918 6919 # if GLASS_ACCUM 6920 if (refr_accum.a < 0.999) { 6921 # endif 6922 # ifdef CLOSURE_REFRACTION 6923 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 6924 accumulate_light(trans, fade, refr_accum); 6925 # endif 6926 # if GLASS_ACCUM 6927 } 6928 # endif 6929 } 6930 } 6931 6932 # undef GLASS_ACCUM 6933 # undef ACCUM 6934 6935 /* ---------------------------- */ 6936 /* World Probe */ 6937 /* ---------------------------- */ 6938 # ifdef CLOSURE_GLOSSY 6939 if (spec_accum.a < 0.999) { 6940 if (!(ssrToggle && ssr_id == outputSsrId)) { 6941 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 6942 accumulate_light(spec, 1.0, spec_accum); 6943 } 6944 6945 # ifdef CLOSURE_CLEARCOAT 6946 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 6947 accumulate_light(C_spec, 1.0, C_spec_accum); 6948 # endif 6949 } 6950 # endif 6951 6952 # ifdef CLOSURE_REFRACTION 6953 if (refr_accum.a < 0.999) { 6954 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 6955 accumulate_light(trans, 1.0, refr_accum); 6956 } 6957 # endif 6958 # endif /* Specular probes */ 6959 6960 /* ---------------------------- */ 6961 /* Ambient Occlusion */ 6962 /* ---------------------------- */ 6963 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 6964 if (!use_contact_shadows) { 6965 /* HACK: Fix for translucent BSDF. (see T65631) */ 6966 N = -N; 6967 } 6968 vec3 bent_normal; 6969 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 6970 if (!use_contact_shadows) { 6971 N = -N; 6972 /* Bypass bent normal. */ 6973 bent_normal = N; 6974 } 6975 # endif 6976 6977 /* ---------------------------- */ 6978 /* Specular Output */ 6979 /* ---------------------------- */ 6980 float NV = dot(N, V); 6981 # ifdef CLOSURE_GLOSSY 6982 vec2 uv = lut_coords(NV, roughness); 6983 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 6984 6985 /* This factor is outputted to be used by SSR in order 6986 * to match the intensity of the regular reflections. */ 6987 ssr_spec = F_ibl(f0, f90, brdf_lut); 6988 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 6989 6990 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 6991 if (ssrToggle && ssr_id == outputSsrId) { 6992 spec_occlu = 1.0; 6993 } 6994 6995 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 6996 # endif 6997 6998 # ifdef CLOSURE_REFRACTION 6999 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 7000 7001 out_refr += refr_accum.rgb * btdf; 7002 # endif 7003 7004 # ifdef CLOSURE_CLEARCOAT 7005 NV = dot(C_N, V); 7006 vec2 C_uv = lut_coords(NV, C_roughness); 7007 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 7008 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 7009 specular_occlusion(NV, final_ao, C_roughness); 7010 7011 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 7012 # endif 7013 7014 # ifdef CLOSURE_GLOSSY 7015 /* Global toggle for lightprobe baking. */ 7016 out_spec *= float(specToggle); 7017 # endif 7018 7019 /* ---------------------------------------------------------------- */ 7020 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 7021 /* ---------------------------------------------------------------- */ 7022 7023 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 7024 # ifdef CLOSURE_DIFFUSE 7025 vec4 diff_accum = vec4(0.0); 7026 7027 /* ---------------------------- */ 7028 /* Irradiance Grids */ 7029 /* ---------------------------- */ 7030 /* Start at 1 because 0 is world irradiance */ 7031 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 7032 GridData gd = grids_data[i]; 7033 7034 vec3 localpos; 7035 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 7036 7037 if (fade > 0.0) { 7038 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 7039 accumulate_light(diff, fade, diff_accum); 7040 } 7041 } 7042 7043 /* ---------------------------- */ 7044 /* World Diffuse */ 7045 /* ---------------------------- */ 7046 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 7047 vec3 diff = probe_evaluate_world_diff(bent_normal); 7048 accumulate_light(diff, 1.0, diff_accum); 7049 } 7050 7051 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 7052 # endif 7053 #endif 7054 } 7055 7056 /* Cleanup for next configuration */ 7057 #undef CLOSURE_NAME 7058 7059 #ifdef CLOSURE_DIFFUSE 7060 # undef CLOSURE_DIFFUSE 7061 #endif 7062 7063 #ifdef CLOSURE_GLOSSY 7064 # undef CLOSURE_GLOSSY 7065 #endif 7066 7067 #ifdef CLOSURE_CLEARCOAT 7068 # undef CLOSURE_CLEARCOAT 7069 #endif 7070 7071 #ifdef CLOSURE_REFRACTION 7072 # undef CLOSURE_REFRACTION 7073 #endif 7074 7075 #ifdef CLOSURE_SUBSURFACE 7076 # undef CLOSURE_SUBSURFACE 7077 #endif 7078 7079 #ifndef LIT_SURFACE_UNIFORM 7080 # define LIT_SURFACE_UNIFORM 7081 7082 uniform float refractionDepth; 7083 7084 # ifndef UTIL_TEX 7085 # define UTIL_TEX 7086 uniform sampler2DArray utilTex; 7087 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 7088 # endif /* UTIL_TEX */ 7089 7090 in vec3 worldPosition; 7091 in vec3 viewPosition; 7092 7093 in vec3 worldNormal; 7094 in vec3 viewNormal; 7095 7096 # ifdef HAIR_SHADER 7097 in vec3 hairTangent; /* world space */ 7098 in float hairThickTime; 7099 in float hairThickness; 7100 in float hairTime; 7101 flat in int hairStrandID; 7102 7103 uniform int hairThicknessRes = 1; 7104 # endif 7105 7106 #endif /* LIT_SURFACE_UNIFORM */ 7107 7108 /** 7109 * AUTO CONFIG 7110 * We include the file multiple times each time with a different configuration. 7111 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 7112 */ 7113 #if !defined(SURFACE_DEFAULT) 7114 # define SURFACE_DEFAULT 7115 # define CLOSURE_NAME eevee_closure_default 7116 # define CLOSURE_DIFFUSE 7117 # define CLOSURE_GLOSSY 7118 #endif /* SURFACE_DEFAULT */ 7119 7120 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 7121 # define SURFACE_DEFAULT_CLEARCOAT 7122 # define CLOSURE_NAME eevee_closure_default_clearcoat 7123 # define CLOSURE_DIFFUSE 7124 # define CLOSURE_GLOSSY 7125 # define CLOSURE_CLEARCOAT 7126 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 7127 7128 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 7129 # define SURFACE_PRINCIPLED 7130 # define CLOSURE_NAME eevee_closure_principled 7131 # define CLOSURE_DIFFUSE 7132 # define CLOSURE_GLOSSY 7133 # define CLOSURE_CLEARCOAT 7134 # define CLOSURE_REFRACTION 7135 # define CLOSURE_SUBSURFACE 7136 #endif /* SURFACE_PRINCIPLED */ 7137 7138 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 7139 # define SURFACE_CLEARCOAT 7140 # define CLOSURE_NAME eevee_closure_clearcoat 7141 # define CLOSURE_GLOSSY 7142 # define CLOSURE_CLEARCOAT 7143 #endif /* SURFACE_CLEARCOAT */ 7144 7145 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 7146 # define SURFACE_DIFFUSE 7147 # define CLOSURE_NAME eevee_closure_diffuse 7148 # define CLOSURE_DIFFUSE 7149 #endif /* SURFACE_DIFFUSE */ 7150 7151 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 7152 # define SURFACE_SUBSURFACE 7153 # define CLOSURE_NAME eevee_closure_subsurface 7154 # define CLOSURE_DIFFUSE 7155 # define CLOSURE_SUBSURFACE 7156 #endif /* SURFACE_SUBSURFACE */ 7157 7158 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 7159 # define SURFACE_SKIN 7160 # define CLOSURE_NAME eevee_closure_skin 7161 # define CLOSURE_DIFFUSE 7162 # define CLOSURE_SUBSURFACE 7163 # define CLOSURE_GLOSSY 7164 #endif /* SURFACE_SKIN */ 7165 7166 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 7167 # define SURFACE_GLOSSY 7168 # define CLOSURE_NAME eevee_closure_glossy 7169 # define CLOSURE_GLOSSY 7170 #endif /* SURFACE_GLOSSY */ 7171 7172 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 7173 # define SURFACE_REFRACT 7174 # define CLOSURE_NAME eevee_closure_refraction 7175 # define CLOSURE_REFRACTION 7176 #endif /* SURFACE_REFRACT */ 7177 7178 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 7179 # define SURFACE_GLASS 7180 # define CLOSURE_NAME eevee_closure_glass 7181 # define CLOSURE_GLOSSY 7182 # define CLOSURE_REFRACTION 7183 #endif /* SURFACE_GLASS */ 7184 7185 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 7186 #ifdef CLOSURE_CLEARCOAT 7187 # ifndef CLOSURE_GLOSSY 7188 # define CLOSURE_GLOSSY 7189 # endif 7190 #endif /* CLOSURE_CLEARCOAT */ 7191 7192 void CLOSURE_NAME(vec3 N 7193 #ifdef CLOSURE_DIFFUSE 7194 , 7195 vec3 albedo 7196 #endif 7197 #ifdef CLOSURE_GLOSSY 7198 , 7199 vec3 f0, 7200 vec3 f90, 7201 int ssr_id 7202 #endif 7203 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 7204 , 7205 float roughness 7206 #endif 7207 #ifdef CLOSURE_CLEARCOAT 7208 , 7209 vec3 C_N, 7210 float C_intensity, 7211 float C_roughness 7212 #endif 7213 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 7214 , 7215 float ao 7216 #endif 7217 #ifdef CLOSURE_SUBSURFACE 7218 , 7219 float sss_scale 7220 #endif 7221 #ifdef CLOSURE_REFRACTION 7222 , 7223 float ior 7224 #endif 7225 , 7226 const bool use_contact_shadows 7227 #ifdef CLOSURE_DIFFUSE 7228 , 7229 out vec3 out_diff 7230 #endif 7231 #ifdef CLOSURE_GLOSSY 7232 , 7233 out vec3 out_spec 7234 #endif 7235 #ifdef CLOSURE_REFRACTION 7236 , 7237 out vec3 out_refr 7238 #endif 7239 #ifdef CLOSURE_GLOSSY 7240 , 7241 out vec3 ssr_spec 7242 #endif 7243 ) 7244 { 7245 #ifdef CLOSURE_DIFFUSE 7246 out_diff = vec3(0.0); 7247 #endif 7248 7249 #ifdef CLOSURE_GLOSSY 7250 out_spec = vec3(0.0); 7251 #endif 7252 7253 #ifdef CLOSURE_REFRACTION 7254 out_refr = vec3(0.0); 7255 #endif 7256 7257 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 7258 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 7259 return; 7260 #else 7261 7262 /* Zero length vectors cause issues, see: T51979. */ 7263 float len = length(N); 7264 if (isnan(len)) { 7265 return; 7266 } 7267 N /= len; 7268 7269 # ifdef CLOSURE_CLEARCOAT 7270 len = length(C_N); 7271 if (isnan(len)) { 7272 return; 7273 } 7274 C_N /= len; 7275 # endif 7276 7277 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 7278 roughness = clamp(roughness, 1e-8, 0.9999); 7279 float roughnessSquared = roughness * roughness; 7280 # endif 7281 7282 # ifdef CLOSURE_CLEARCOAT 7283 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 7284 float C_roughnessSquared = C_roughness * C_roughness; 7285 # endif 7286 7287 vec3 V = cameraVec; 7288 7289 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 7290 7291 /* ---------------------------------------------------------------- */ 7292 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 7293 /* ---------------------------------------------------------------- */ 7294 7295 # ifdef CLOSURE_GLOSSY 7296 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 7297 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 7298 # endif 7299 7300 # ifdef CLOSURE_CLEARCOAT 7301 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 7302 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 7303 vec3 out_spec_clear = vec3(0.0); 7304 # endif 7305 7306 float tracing_depth = gl_FragCoord.z; 7307 /* Constant bias (due to depth buffer precision) */ 7308 /* Magic numbers for 24bits of precision. 7309 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 7310 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 7311 /* Convert to view Z. */ 7312 tracing_depth = get_view_z_from_depth(tracing_depth); 7313 7314 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 7315 7316 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 7317 LightData ld = lights_data[i]; 7318 7319 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 7320 l_vector.xyz = ld.l_position - worldPosition; 7321 l_vector.w = length(l_vector.xyz); 7322 7323 float l_vis = light_visibility(ld, 7324 worldPosition, 7325 viewPosition, 7326 tracing_depth, 7327 true_normal, 7328 rand.x, 7329 use_contact_shadows, 7330 l_vector); 7331 7332 if (l_vis < 1e-8) { 7333 continue; 7334 } 7335 7336 vec3 l_color_vis = ld.l_color * l_vis; 7337 7338 # ifdef CLOSURE_DIFFUSE 7339 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 7340 # endif 7341 7342 # ifdef CLOSURE_GLOSSY 7343 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 7344 # endif 7345 7346 # ifdef CLOSURE_CLEARCOAT 7347 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 7348 ld.l_spec; 7349 # endif 7350 } 7351 7352 # ifdef CLOSURE_GLOSSY 7353 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 7354 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 7355 # endif 7356 7357 # ifdef CLOSURE_CLEARCOAT 7358 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 7359 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 7360 out_spec += out_spec_clear * C_intensity; 7361 # endif 7362 7363 /* ---------------------------------------------------------------- */ 7364 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 7365 /* ---------------------------------------------------------------- */ 7366 7367 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 7368 * BRDF. */ 7369 # ifdef CLOSURE_GLOSSY 7370 vec4 spec_accum = vec4(0.0); 7371 # endif 7372 7373 # ifdef CLOSURE_CLEARCOAT 7374 vec4 C_spec_accum = vec4(0.0); 7375 # endif 7376 7377 # ifdef CLOSURE_REFRACTION 7378 vec4 refr_accum = vec4(0.0); 7379 # endif 7380 7381 # ifdef CLOSURE_GLOSSY 7382 /* ---------------------------- */ 7383 /* Planar Reflections */ 7384 /* ---------------------------- */ 7385 7386 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 7387 PlanarData pd = planars_data[i]; 7388 7389 /* Fade on geometric normal. */ 7390 float fade = probe_attenuation_planar( 7391 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 7392 7393 if (fade > 0.0) { 7394 if (!(ssrToggle && ssr_id == outputSsrId)) { 7395 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 7396 accumulate_light(spec, fade, spec_accum); 7397 } 7398 7399 # ifdef CLOSURE_CLEARCOAT 7400 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 7401 accumulate_light(C_spec, fade, C_spec_accum); 7402 # endif 7403 } 7404 } 7405 # endif 7406 7407 # ifdef CLOSURE_GLOSSY 7408 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 7409 # endif 7410 7411 # ifdef CLOSURE_CLEARCOAT 7412 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 7413 # endif 7414 7415 # ifdef CLOSURE_REFRACTION 7416 /* Refract the view vector using the depth heuristic. 7417 * Then later Refract a second time the already refracted 7418 * ray using the inverse ior. */ 7419 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 7420 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 7421 vec3 refr_pos = (refractionDepth > 0.0) ? 7422 line_plane_intersect( 7423 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 7424 worldPosition; 7425 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 7426 # endif 7427 7428 # ifdef CLOSURE_REFRACTION 7429 /* ---------------------------- */ 7430 /* Screen Space Refraction */ 7431 /* ---------------------------- */ 7432 # ifdef USE_REFRACTION 7433 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 7434 /* Find approximated position of the 2nd refraction event. */ 7435 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 7436 viewPosition; 7437 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 7438 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 7439 accumulate_light(trans.rgb, trans.a, refr_accum); 7440 } 7441 # endif 7442 7443 # endif 7444 7445 /* ---------------------------- */ 7446 /* Specular probes */ 7447 /* ---------------------------- */ 7448 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 7449 7450 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 7451 # define GLASS_ACCUM 1 7452 # define ACCUM min(refr_accum.a, spec_accum.a) 7453 # elif defined(CLOSURE_REFRACTION) 7454 # define GLASS_ACCUM 0 7455 # define ACCUM refr_accum.a 7456 # else 7457 # define GLASS_ACCUM 0 7458 # define ACCUM spec_accum.a 7459 # endif 7460 7461 /* Starts at 1 because 0 is world probe */ 7462 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 7463 float fade = probe_attenuation_cube(i, worldPosition); 7464 7465 if (fade > 0.0) { 7466 7467 # if GLASS_ACCUM 7468 if (spec_accum.a < 0.999) { 7469 # endif 7470 # ifdef CLOSURE_GLOSSY 7471 if (!(ssrToggle && ssr_id == outputSsrId)) { 7472 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 7473 accumulate_light(spec, fade, spec_accum); 7474 } 7475 # endif 7476 7477 # ifdef CLOSURE_CLEARCOAT 7478 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 7479 accumulate_light(C_spec, fade, C_spec_accum); 7480 # endif 7481 # if GLASS_ACCUM 7482 } 7483 # endif 7484 7485 # if GLASS_ACCUM 7486 if (refr_accum.a < 0.999) { 7487 # endif 7488 # ifdef CLOSURE_REFRACTION 7489 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 7490 accumulate_light(trans, fade, refr_accum); 7491 # endif 7492 # if GLASS_ACCUM 7493 } 7494 # endif 7495 } 7496 } 7497 7498 # undef GLASS_ACCUM 7499 # undef ACCUM 7500 7501 /* ---------------------------- */ 7502 /* World Probe */ 7503 /* ---------------------------- */ 7504 # ifdef CLOSURE_GLOSSY 7505 if (spec_accum.a < 0.999) { 7506 if (!(ssrToggle && ssr_id == outputSsrId)) { 7507 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 7508 accumulate_light(spec, 1.0, spec_accum); 7509 } 7510 7511 # ifdef CLOSURE_CLEARCOAT 7512 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 7513 accumulate_light(C_spec, 1.0, C_spec_accum); 7514 # endif 7515 } 7516 # endif 7517 7518 # ifdef CLOSURE_REFRACTION 7519 if (refr_accum.a < 0.999) { 7520 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 7521 accumulate_light(trans, 1.0, refr_accum); 7522 } 7523 # endif 7524 # endif /* Specular probes */ 7525 7526 /* ---------------------------- */ 7527 /* Ambient Occlusion */ 7528 /* ---------------------------- */ 7529 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 7530 if (!use_contact_shadows) { 7531 /* HACK: Fix for translucent BSDF. (see T65631) */ 7532 N = -N; 7533 } 7534 vec3 bent_normal; 7535 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 7536 if (!use_contact_shadows) { 7537 N = -N; 7538 /* Bypass bent normal. */ 7539 bent_normal = N; 7540 } 7541 # endif 7542 7543 /* ---------------------------- */ 7544 /* Specular Output */ 7545 /* ---------------------------- */ 7546 float NV = dot(N, V); 7547 # ifdef CLOSURE_GLOSSY 7548 vec2 uv = lut_coords(NV, roughness); 7549 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 7550 7551 /* This factor is outputted to be used by SSR in order 7552 * to match the intensity of the regular reflections. */ 7553 ssr_spec = F_ibl(f0, f90, brdf_lut); 7554 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 7555 7556 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 7557 if (ssrToggle && ssr_id == outputSsrId) { 7558 spec_occlu = 1.0; 7559 } 7560 7561 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 7562 # endif 7563 7564 # ifdef CLOSURE_REFRACTION 7565 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 7566 7567 out_refr += refr_accum.rgb * btdf; 7568 # endif 7569 7570 # ifdef CLOSURE_CLEARCOAT 7571 NV = dot(C_N, V); 7572 vec2 C_uv = lut_coords(NV, C_roughness); 7573 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 7574 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 7575 specular_occlusion(NV, final_ao, C_roughness); 7576 7577 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 7578 # endif 7579 7580 # ifdef CLOSURE_GLOSSY 7581 /* Global toggle for lightprobe baking. */ 7582 out_spec *= float(specToggle); 7583 # endif 7584 7585 /* ---------------------------------------------------------------- */ 7586 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 7587 /* ---------------------------------------------------------------- */ 7588 7589 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 7590 # ifdef CLOSURE_DIFFUSE 7591 vec4 diff_accum = vec4(0.0); 7592 7593 /* ---------------------------- */ 7594 /* Irradiance Grids */ 7595 /* ---------------------------- */ 7596 /* Start at 1 because 0 is world irradiance */ 7597 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 7598 GridData gd = grids_data[i]; 7599 7600 vec3 localpos; 7601 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 7602 7603 if (fade > 0.0) { 7604 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 7605 accumulate_light(diff, fade, diff_accum); 7606 } 7607 } 7608 7609 /* ---------------------------- */ 7610 /* World Diffuse */ 7611 /* ---------------------------- */ 7612 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 7613 vec3 diff = probe_evaluate_world_diff(bent_normal); 7614 accumulate_light(diff, 1.0, diff_accum); 7615 } 7616 7617 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 7618 # endif 7619 #endif 7620 } 7621 7622 /* Cleanup for next configuration */ 7623 #undef CLOSURE_NAME 7624 7625 #ifdef CLOSURE_DIFFUSE 7626 # undef CLOSURE_DIFFUSE 7627 #endif 7628 7629 #ifdef CLOSURE_GLOSSY 7630 # undef CLOSURE_GLOSSY 7631 #endif 7632 7633 #ifdef CLOSURE_CLEARCOAT 7634 # undef CLOSURE_CLEARCOAT 7635 #endif 7636 7637 #ifdef CLOSURE_REFRACTION 7638 # undef CLOSURE_REFRACTION 7639 #endif 7640 7641 #ifdef CLOSURE_SUBSURFACE 7642 # undef CLOSURE_SUBSURFACE 7643 #endif 7644 7645 #ifndef LIT_SURFACE_UNIFORM 7646 # define LIT_SURFACE_UNIFORM 7647 7648 uniform float refractionDepth; 7649 7650 # ifndef UTIL_TEX 7651 # define UTIL_TEX 7652 uniform sampler2DArray utilTex; 7653 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 7654 # endif /* UTIL_TEX */ 7655 7656 in vec3 worldPosition; 7657 in vec3 viewPosition; 7658 7659 in vec3 worldNormal; 7660 in vec3 viewNormal; 7661 7662 # ifdef HAIR_SHADER 7663 in vec3 hairTangent; /* world space */ 7664 in float hairThickTime; 7665 in float hairThickness; 7666 in float hairTime; 7667 flat in int hairStrandID; 7668 7669 uniform int hairThicknessRes = 1; 7670 # endif 7671 7672 #endif /* LIT_SURFACE_UNIFORM */ 7673 7674 /** 7675 * AUTO CONFIG 7676 * We include the file multiple times each time with a different configuration. 7677 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 7678 */ 7679 #if !defined(SURFACE_DEFAULT) 7680 # define SURFACE_DEFAULT 7681 # define CLOSURE_NAME eevee_closure_default 7682 # define CLOSURE_DIFFUSE 7683 # define CLOSURE_GLOSSY 7684 #endif /* SURFACE_DEFAULT */ 7685 7686 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 7687 # define SURFACE_DEFAULT_CLEARCOAT 7688 # define CLOSURE_NAME eevee_closure_default_clearcoat 7689 # define CLOSURE_DIFFUSE 7690 # define CLOSURE_GLOSSY 7691 # define CLOSURE_CLEARCOAT 7692 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 7693 7694 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 7695 # define SURFACE_PRINCIPLED 7696 # define CLOSURE_NAME eevee_closure_principled 7697 # define CLOSURE_DIFFUSE 7698 # define CLOSURE_GLOSSY 7699 # define CLOSURE_CLEARCOAT 7700 # define CLOSURE_REFRACTION 7701 # define CLOSURE_SUBSURFACE 7702 #endif /* SURFACE_PRINCIPLED */ 7703 7704 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 7705 # define SURFACE_CLEARCOAT 7706 # define CLOSURE_NAME eevee_closure_clearcoat 7707 # define CLOSURE_GLOSSY 7708 # define CLOSURE_CLEARCOAT 7709 #endif /* SURFACE_CLEARCOAT */ 7710 7711 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 7712 # define SURFACE_DIFFUSE 7713 # define CLOSURE_NAME eevee_closure_diffuse 7714 # define CLOSURE_DIFFUSE 7715 #endif /* SURFACE_DIFFUSE */ 7716 7717 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 7718 # define SURFACE_SUBSURFACE 7719 # define CLOSURE_NAME eevee_closure_subsurface 7720 # define CLOSURE_DIFFUSE 7721 # define CLOSURE_SUBSURFACE 7722 #endif /* SURFACE_SUBSURFACE */ 7723 7724 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 7725 # define SURFACE_SKIN 7726 # define CLOSURE_NAME eevee_closure_skin 7727 # define CLOSURE_DIFFUSE 7728 # define CLOSURE_SUBSURFACE 7729 # define CLOSURE_GLOSSY 7730 #endif /* SURFACE_SKIN */ 7731 7732 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 7733 # define SURFACE_GLOSSY 7734 # define CLOSURE_NAME eevee_closure_glossy 7735 # define CLOSURE_GLOSSY 7736 #endif /* SURFACE_GLOSSY */ 7737 7738 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 7739 # define SURFACE_REFRACT 7740 # define CLOSURE_NAME eevee_closure_refraction 7741 # define CLOSURE_REFRACTION 7742 #endif /* SURFACE_REFRACT */ 7743 7744 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 7745 # define SURFACE_GLASS 7746 # define CLOSURE_NAME eevee_closure_glass 7747 # define CLOSURE_GLOSSY 7748 # define CLOSURE_REFRACTION 7749 #endif /* SURFACE_GLASS */ 7750 7751 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 7752 #ifdef CLOSURE_CLEARCOAT 7753 # ifndef CLOSURE_GLOSSY 7754 # define CLOSURE_GLOSSY 7755 # endif 7756 #endif /* CLOSURE_CLEARCOAT */ 7757 7758 void CLOSURE_NAME(vec3 N 7759 #ifdef CLOSURE_DIFFUSE 7760 , 7761 vec3 albedo 7762 #endif 7763 #ifdef CLOSURE_GLOSSY 7764 , 7765 vec3 f0, 7766 vec3 f90, 7767 int ssr_id 7768 #endif 7769 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 7770 , 7771 float roughness 7772 #endif 7773 #ifdef CLOSURE_CLEARCOAT 7774 , 7775 vec3 C_N, 7776 float C_intensity, 7777 float C_roughness 7778 #endif 7779 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 7780 , 7781 float ao 7782 #endif 7783 #ifdef CLOSURE_SUBSURFACE 7784 , 7785 float sss_scale 7786 #endif 7787 #ifdef CLOSURE_REFRACTION 7788 , 7789 float ior 7790 #endif 7791 , 7792 const bool use_contact_shadows 7793 #ifdef CLOSURE_DIFFUSE 7794 , 7795 out vec3 out_diff 7796 #endif 7797 #ifdef CLOSURE_GLOSSY 7798 , 7799 out vec3 out_spec 7800 #endif 7801 #ifdef CLOSURE_REFRACTION 7802 , 7803 out vec3 out_refr 7804 #endif 7805 #ifdef CLOSURE_GLOSSY 7806 , 7807 out vec3 ssr_spec 7808 #endif 7809 ) 7810 { 7811 #ifdef CLOSURE_DIFFUSE 7812 out_diff = vec3(0.0); 7813 #endif 7814 7815 #ifdef CLOSURE_GLOSSY 7816 out_spec = vec3(0.0); 7817 #endif 7818 7819 #ifdef CLOSURE_REFRACTION 7820 out_refr = vec3(0.0); 7821 #endif 7822 7823 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 7824 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 7825 return; 7826 #else 7827 7828 /* Zero length vectors cause issues, see: T51979. */ 7829 float len = length(N); 7830 if (isnan(len)) { 7831 return; 7832 } 7833 N /= len; 7834 7835 # ifdef CLOSURE_CLEARCOAT 7836 len = length(C_N); 7837 if (isnan(len)) { 7838 return; 7839 } 7840 C_N /= len; 7841 # endif 7842 7843 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 7844 roughness = clamp(roughness, 1e-8, 0.9999); 7845 float roughnessSquared = roughness * roughness; 7846 # endif 7847 7848 # ifdef CLOSURE_CLEARCOAT 7849 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 7850 float C_roughnessSquared = C_roughness * C_roughness; 7851 # endif 7852 7853 vec3 V = cameraVec; 7854 7855 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 7856 7857 /* ---------------------------------------------------------------- */ 7858 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 7859 /* ---------------------------------------------------------------- */ 7860 7861 # ifdef CLOSURE_GLOSSY 7862 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 7863 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 7864 # endif 7865 7866 # ifdef CLOSURE_CLEARCOAT 7867 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 7868 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 7869 vec3 out_spec_clear = vec3(0.0); 7870 # endif 7871 7872 float tracing_depth = gl_FragCoord.z; 7873 /* Constant bias (due to depth buffer precision) */ 7874 /* Magic numbers for 24bits of precision. 7875 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 7876 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 7877 /* Convert to view Z. */ 7878 tracing_depth = get_view_z_from_depth(tracing_depth); 7879 7880 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 7881 7882 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 7883 LightData ld = lights_data[i]; 7884 7885 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 7886 l_vector.xyz = ld.l_position - worldPosition; 7887 l_vector.w = length(l_vector.xyz); 7888 7889 float l_vis = light_visibility(ld, 7890 worldPosition, 7891 viewPosition, 7892 tracing_depth, 7893 true_normal, 7894 rand.x, 7895 use_contact_shadows, 7896 l_vector); 7897 7898 if (l_vis < 1e-8) { 7899 continue; 7900 } 7901 7902 vec3 l_color_vis = ld.l_color * l_vis; 7903 7904 # ifdef CLOSURE_DIFFUSE 7905 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 7906 # endif 7907 7908 # ifdef CLOSURE_GLOSSY 7909 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 7910 # endif 7911 7912 # ifdef CLOSURE_CLEARCOAT 7913 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 7914 ld.l_spec; 7915 # endif 7916 } 7917 7918 # ifdef CLOSURE_GLOSSY 7919 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 7920 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 7921 # endif 7922 7923 # ifdef CLOSURE_CLEARCOAT 7924 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 7925 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 7926 out_spec += out_spec_clear * C_intensity; 7927 # endif 7928 7929 /* ---------------------------------------------------------------- */ 7930 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 7931 /* ---------------------------------------------------------------- */ 7932 7933 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 7934 * BRDF. */ 7935 # ifdef CLOSURE_GLOSSY 7936 vec4 spec_accum = vec4(0.0); 7937 # endif 7938 7939 # ifdef CLOSURE_CLEARCOAT 7940 vec4 C_spec_accum = vec4(0.0); 7941 # endif 7942 7943 # ifdef CLOSURE_REFRACTION 7944 vec4 refr_accum = vec4(0.0); 7945 # endif 7946 7947 # ifdef CLOSURE_GLOSSY 7948 /* ---------------------------- */ 7949 /* Planar Reflections */ 7950 /* ---------------------------- */ 7951 7952 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 7953 PlanarData pd = planars_data[i]; 7954 7955 /* Fade on geometric normal. */ 7956 float fade = probe_attenuation_planar( 7957 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 7958 7959 if (fade > 0.0) { 7960 if (!(ssrToggle && ssr_id == outputSsrId)) { 7961 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 7962 accumulate_light(spec, fade, spec_accum); 7963 } 7964 7965 # ifdef CLOSURE_CLEARCOAT 7966 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 7967 accumulate_light(C_spec, fade, C_spec_accum); 7968 # endif 7969 } 7970 } 7971 # endif 7972 7973 # ifdef CLOSURE_GLOSSY 7974 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 7975 # endif 7976 7977 # ifdef CLOSURE_CLEARCOAT 7978 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 7979 # endif 7980 7981 # ifdef CLOSURE_REFRACTION 7982 /* Refract the view vector using the depth heuristic. 7983 * Then later Refract a second time the already refracted 7984 * ray using the inverse ior. */ 7985 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 7986 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 7987 vec3 refr_pos = (refractionDepth > 0.0) ? 7988 line_plane_intersect( 7989 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 7990 worldPosition; 7991 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 7992 # endif 7993 7994 # ifdef CLOSURE_REFRACTION 7995 /* ---------------------------- */ 7996 /* Screen Space Refraction */ 7997 /* ---------------------------- */ 7998 # ifdef USE_REFRACTION 7999 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 8000 /* Find approximated position of the 2nd refraction event. */ 8001 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 8002 viewPosition; 8003 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 8004 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 8005 accumulate_light(trans.rgb, trans.a, refr_accum); 8006 } 8007 # endif 8008 8009 # endif 8010 8011 /* ---------------------------- */ 8012 /* Specular probes */ 8013 /* ---------------------------- */ 8014 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8015 8016 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 8017 # define GLASS_ACCUM 1 8018 # define ACCUM min(refr_accum.a, spec_accum.a) 8019 # elif defined(CLOSURE_REFRACTION) 8020 # define GLASS_ACCUM 0 8021 # define ACCUM refr_accum.a 8022 # else 8023 # define GLASS_ACCUM 0 8024 # define ACCUM spec_accum.a 8025 # endif 8026 8027 /* Starts at 1 because 0 is world probe */ 8028 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 8029 float fade = probe_attenuation_cube(i, worldPosition); 8030 8031 if (fade > 0.0) { 8032 8033 # if GLASS_ACCUM 8034 if (spec_accum.a < 0.999) { 8035 # endif 8036 # ifdef CLOSURE_GLOSSY 8037 if (!(ssrToggle && ssr_id == outputSsrId)) { 8038 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 8039 accumulate_light(spec, fade, spec_accum); 8040 } 8041 # endif 8042 8043 # ifdef CLOSURE_CLEARCOAT 8044 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 8045 accumulate_light(C_spec, fade, C_spec_accum); 8046 # endif 8047 # if GLASS_ACCUM 8048 } 8049 # endif 8050 8051 # if GLASS_ACCUM 8052 if (refr_accum.a < 0.999) { 8053 # endif 8054 # ifdef CLOSURE_REFRACTION 8055 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 8056 accumulate_light(trans, fade, refr_accum); 8057 # endif 8058 # if GLASS_ACCUM 8059 } 8060 # endif 8061 } 8062 } 8063 8064 # undef GLASS_ACCUM 8065 # undef ACCUM 8066 8067 /* ---------------------------- */ 8068 /* World Probe */ 8069 /* ---------------------------- */ 8070 # ifdef CLOSURE_GLOSSY 8071 if (spec_accum.a < 0.999) { 8072 if (!(ssrToggle && ssr_id == outputSsrId)) { 8073 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 8074 accumulate_light(spec, 1.0, spec_accum); 8075 } 8076 8077 # ifdef CLOSURE_CLEARCOAT 8078 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 8079 accumulate_light(C_spec, 1.0, C_spec_accum); 8080 # endif 8081 } 8082 # endif 8083 8084 # ifdef CLOSURE_REFRACTION 8085 if (refr_accum.a < 0.999) { 8086 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 8087 accumulate_light(trans, 1.0, refr_accum); 8088 } 8089 # endif 8090 # endif /* Specular probes */ 8091 8092 /* ---------------------------- */ 8093 /* Ambient Occlusion */ 8094 /* ---------------------------- */ 8095 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 8096 if (!use_contact_shadows) { 8097 /* HACK: Fix for translucent BSDF. (see T65631) */ 8098 N = -N; 8099 } 8100 vec3 bent_normal; 8101 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 8102 if (!use_contact_shadows) { 8103 N = -N; 8104 /* Bypass bent normal. */ 8105 bent_normal = N; 8106 } 8107 # endif 8108 8109 /* ---------------------------- */ 8110 /* Specular Output */ 8111 /* ---------------------------- */ 8112 float NV = dot(N, V); 8113 # ifdef CLOSURE_GLOSSY 8114 vec2 uv = lut_coords(NV, roughness); 8115 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 8116 8117 /* This factor is outputted to be used by SSR in order 8118 * to match the intensity of the regular reflections. */ 8119 ssr_spec = F_ibl(f0, f90, brdf_lut); 8120 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 8121 8122 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 8123 if (ssrToggle && ssr_id == outputSsrId) { 8124 spec_occlu = 1.0; 8125 } 8126 8127 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 8128 # endif 8129 8130 # ifdef CLOSURE_REFRACTION 8131 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 8132 8133 out_refr += refr_accum.rgb * btdf; 8134 # endif 8135 8136 # ifdef CLOSURE_CLEARCOAT 8137 NV = dot(C_N, V); 8138 vec2 C_uv = lut_coords(NV, C_roughness); 8139 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 8140 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 8141 specular_occlusion(NV, final_ao, C_roughness); 8142 8143 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 8144 # endif 8145 8146 # ifdef CLOSURE_GLOSSY 8147 /* Global toggle for lightprobe baking. */ 8148 out_spec *= float(specToggle); 8149 # endif 8150 8151 /* ---------------------------------------------------------------- */ 8152 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 8153 /* ---------------------------------------------------------------- */ 8154 8155 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 8156 # ifdef CLOSURE_DIFFUSE 8157 vec4 diff_accum = vec4(0.0); 8158 8159 /* ---------------------------- */ 8160 /* Irradiance Grids */ 8161 /* ---------------------------- */ 8162 /* Start at 1 because 0 is world irradiance */ 8163 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 8164 GridData gd = grids_data[i]; 8165 8166 vec3 localpos; 8167 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 8168 8169 if (fade > 0.0) { 8170 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 8171 accumulate_light(diff, fade, diff_accum); 8172 } 8173 } 8174 8175 /* ---------------------------- */ 8176 /* World Diffuse */ 8177 /* ---------------------------- */ 8178 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 8179 vec3 diff = probe_evaluate_world_diff(bent_normal); 8180 accumulate_light(diff, 1.0, diff_accum); 8181 } 8182 8183 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 8184 # endif 8185 #endif 8186 } 8187 8188 /* Cleanup for next configuration */ 8189 #undef CLOSURE_NAME 8190 8191 #ifdef CLOSURE_DIFFUSE 8192 # undef CLOSURE_DIFFUSE 8193 #endif 8194 8195 #ifdef CLOSURE_GLOSSY 8196 # undef CLOSURE_GLOSSY 8197 #endif 8198 8199 #ifdef CLOSURE_CLEARCOAT 8200 # undef CLOSURE_CLEARCOAT 8201 #endif 8202 8203 #ifdef CLOSURE_REFRACTION 8204 # undef CLOSURE_REFRACTION 8205 #endif 8206 8207 #ifdef CLOSURE_SUBSURFACE 8208 # undef CLOSURE_SUBSURFACE 8209 #endif 8210 8211 #ifndef LIT_SURFACE_UNIFORM 8212 # define LIT_SURFACE_UNIFORM 8213 8214 uniform float refractionDepth; 8215 8216 # ifndef UTIL_TEX 8217 # define UTIL_TEX 8218 uniform sampler2DArray utilTex; 8219 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 8220 # endif /* UTIL_TEX */ 8221 8222 in vec3 worldPosition; 8223 in vec3 viewPosition; 8224 8225 in vec3 worldNormal; 8226 in vec3 viewNormal; 8227 8228 # ifdef HAIR_SHADER 8229 in vec3 hairTangent; /* world space */ 8230 in float hairThickTime; 8231 in float hairThickness; 8232 in float hairTime; 8233 flat in int hairStrandID; 8234 8235 uniform int hairThicknessRes = 1; 8236 # endif 8237 8238 #endif /* LIT_SURFACE_UNIFORM */ 8239 8240 /** 8241 * AUTO CONFIG 8242 * We include the file multiple times each time with a different configuration. 8243 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 8244 */ 8245 #if !defined(SURFACE_DEFAULT) 8246 # define SURFACE_DEFAULT 8247 # define CLOSURE_NAME eevee_closure_default 8248 # define CLOSURE_DIFFUSE 8249 # define CLOSURE_GLOSSY 8250 #endif /* SURFACE_DEFAULT */ 8251 8252 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 8253 # define SURFACE_DEFAULT_CLEARCOAT 8254 # define CLOSURE_NAME eevee_closure_default_clearcoat 8255 # define CLOSURE_DIFFUSE 8256 # define CLOSURE_GLOSSY 8257 # define CLOSURE_CLEARCOAT 8258 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 8259 8260 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 8261 # define SURFACE_PRINCIPLED 8262 # define CLOSURE_NAME eevee_closure_principled 8263 # define CLOSURE_DIFFUSE 8264 # define CLOSURE_GLOSSY 8265 # define CLOSURE_CLEARCOAT 8266 # define CLOSURE_REFRACTION 8267 # define CLOSURE_SUBSURFACE 8268 #endif /* SURFACE_PRINCIPLED */ 8269 8270 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 8271 # define SURFACE_CLEARCOAT 8272 # define CLOSURE_NAME eevee_closure_clearcoat 8273 # define CLOSURE_GLOSSY 8274 # define CLOSURE_CLEARCOAT 8275 #endif /* SURFACE_CLEARCOAT */ 8276 8277 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 8278 # define SURFACE_DIFFUSE 8279 # define CLOSURE_NAME eevee_closure_diffuse 8280 # define CLOSURE_DIFFUSE 8281 #endif /* SURFACE_DIFFUSE */ 8282 8283 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 8284 # define SURFACE_SUBSURFACE 8285 # define CLOSURE_NAME eevee_closure_subsurface 8286 # define CLOSURE_DIFFUSE 8287 # define CLOSURE_SUBSURFACE 8288 #endif /* SURFACE_SUBSURFACE */ 8289 8290 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 8291 # define SURFACE_SKIN 8292 # define CLOSURE_NAME eevee_closure_skin 8293 # define CLOSURE_DIFFUSE 8294 # define CLOSURE_SUBSURFACE 8295 # define CLOSURE_GLOSSY 8296 #endif /* SURFACE_SKIN */ 8297 8298 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 8299 # define SURFACE_GLOSSY 8300 # define CLOSURE_NAME eevee_closure_glossy 8301 # define CLOSURE_GLOSSY 8302 #endif /* SURFACE_GLOSSY */ 8303 8304 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 8305 # define SURFACE_REFRACT 8306 # define CLOSURE_NAME eevee_closure_refraction 8307 # define CLOSURE_REFRACTION 8308 #endif /* SURFACE_REFRACT */ 8309 8310 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 8311 # define SURFACE_GLASS 8312 # define CLOSURE_NAME eevee_closure_glass 8313 # define CLOSURE_GLOSSY 8314 # define CLOSURE_REFRACTION 8315 #endif /* SURFACE_GLASS */ 8316 8317 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 8318 #ifdef CLOSURE_CLEARCOAT 8319 # ifndef CLOSURE_GLOSSY 8320 # define CLOSURE_GLOSSY 8321 # endif 8322 #endif /* CLOSURE_CLEARCOAT */ 8323 8324 void CLOSURE_NAME(vec3 N 8325 #ifdef CLOSURE_DIFFUSE 8326 , 8327 vec3 albedo 8328 #endif 8329 #ifdef CLOSURE_GLOSSY 8330 , 8331 vec3 f0, 8332 vec3 f90, 8333 int ssr_id 8334 #endif 8335 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8336 , 8337 float roughness 8338 #endif 8339 #ifdef CLOSURE_CLEARCOAT 8340 , 8341 vec3 C_N, 8342 float C_intensity, 8343 float C_roughness 8344 #endif 8345 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 8346 , 8347 float ao 8348 #endif 8349 #ifdef CLOSURE_SUBSURFACE 8350 , 8351 float sss_scale 8352 #endif 8353 #ifdef CLOSURE_REFRACTION 8354 , 8355 float ior 8356 #endif 8357 , 8358 const bool use_contact_shadows 8359 #ifdef CLOSURE_DIFFUSE 8360 , 8361 out vec3 out_diff 8362 #endif 8363 #ifdef CLOSURE_GLOSSY 8364 , 8365 out vec3 out_spec 8366 #endif 8367 #ifdef CLOSURE_REFRACTION 8368 , 8369 out vec3 out_refr 8370 #endif 8371 #ifdef CLOSURE_GLOSSY 8372 , 8373 out vec3 ssr_spec 8374 #endif 8375 ) 8376 { 8377 #ifdef CLOSURE_DIFFUSE 8378 out_diff = vec3(0.0); 8379 #endif 8380 8381 #ifdef CLOSURE_GLOSSY 8382 out_spec = vec3(0.0); 8383 #endif 8384 8385 #ifdef CLOSURE_REFRACTION 8386 out_refr = vec3(0.0); 8387 #endif 8388 8389 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 8390 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 8391 return; 8392 #else 8393 8394 /* Zero length vectors cause issues, see: T51979. */ 8395 float len = length(N); 8396 if (isnan(len)) { 8397 return; 8398 } 8399 N /= len; 8400 8401 # ifdef CLOSURE_CLEARCOAT 8402 len = length(C_N); 8403 if (isnan(len)) { 8404 return; 8405 } 8406 C_N /= len; 8407 # endif 8408 8409 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8410 roughness = clamp(roughness, 1e-8, 0.9999); 8411 float roughnessSquared = roughness * roughness; 8412 # endif 8413 8414 # ifdef CLOSURE_CLEARCOAT 8415 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 8416 float C_roughnessSquared = C_roughness * C_roughness; 8417 # endif 8418 8419 vec3 V = cameraVec; 8420 8421 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 8422 8423 /* ---------------------------------------------------------------- */ 8424 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 8425 /* ---------------------------------------------------------------- */ 8426 8427 # ifdef CLOSURE_GLOSSY 8428 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 8429 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 8430 # endif 8431 8432 # ifdef CLOSURE_CLEARCOAT 8433 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 8434 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 8435 vec3 out_spec_clear = vec3(0.0); 8436 # endif 8437 8438 float tracing_depth = gl_FragCoord.z; 8439 /* Constant bias (due to depth buffer precision) */ 8440 /* Magic numbers for 24bits of precision. 8441 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 8442 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 8443 /* Convert to view Z. */ 8444 tracing_depth = get_view_z_from_depth(tracing_depth); 8445 8446 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 8447 8448 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 8449 LightData ld = lights_data[i]; 8450 8451 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 8452 l_vector.xyz = ld.l_position - worldPosition; 8453 l_vector.w = length(l_vector.xyz); 8454 8455 float l_vis = light_visibility(ld, 8456 worldPosition, 8457 viewPosition, 8458 tracing_depth, 8459 true_normal, 8460 rand.x, 8461 use_contact_shadows, 8462 l_vector); 8463 8464 if (l_vis < 1e-8) { 8465 continue; 8466 } 8467 8468 vec3 l_color_vis = ld.l_color * l_vis; 8469 8470 # ifdef CLOSURE_DIFFUSE 8471 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 8472 # endif 8473 8474 # ifdef CLOSURE_GLOSSY 8475 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 8476 # endif 8477 8478 # ifdef CLOSURE_CLEARCOAT 8479 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 8480 ld.l_spec; 8481 # endif 8482 } 8483 8484 # ifdef CLOSURE_GLOSSY 8485 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 8486 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 8487 # endif 8488 8489 # ifdef CLOSURE_CLEARCOAT 8490 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 8491 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 8492 out_spec += out_spec_clear * C_intensity; 8493 # endif 8494 8495 /* ---------------------------------------------------------------- */ 8496 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 8497 /* ---------------------------------------------------------------- */ 8498 8499 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 8500 * BRDF. */ 8501 # ifdef CLOSURE_GLOSSY 8502 vec4 spec_accum = vec4(0.0); 8503 # endif 8504 8505 # ifdef CLOSURE_CLEARCOAT 8506 vec4 C_spec_accum = vec4(0.0); 8507 # endif 8508 8509 # ifdef CLOSURE_REFRACTION 8510 vec4 refr_accum = vec4(0.0); 8511 # endif 8512 8513 # ifdef CLOSURE_GLOSSY 8514 /* ---------------------------- */ 8515 /* Planar Reflections */ 8516 /* ---------------------------- */ 8517 8518 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 8519 PlanarData pd = planars_data[i]; 8520 8521 /* Fade on geometric normal. */ 8522 float fade = probe_attenuation_planar( 8523 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 8524 8525 if (fade > 0.0) { 8526 if (!(ssrToggle && ssr_id == outputSsrId)) { 8527 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 8528 accumulate_light(spec, fade, spec_accum); 8529 } 8530 8531 # ifdef CLOSURE_CLEARCOAT 8532 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 8533 accumulate_light(C_spec, fade, C_spec_accum); 8534 # endif 8535 } 8536 } 8537 # endif 8538 8539 # ifdef CLOSURE_GLOSSY 8540 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 8541 # endif 8542 8543 # ifdef CLOSURE_CLEARCOAT 8544 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 8545 # endif 8546 8547 # ifdef CLOSURE_REFRACTION 8548 /* Refract the view vector using the depth heuristic. 8549 * Then later Refract a second time the already refracted 8550 * ray using the inverse ior. */ 8551 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 8552 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 8553 vec3 refr_pos = (refractionDepth > 0.0) ? 8554 line_plane_intersect( 8555 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 8556 worldPosition; 8557 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 8558 # endif 8559 8560 # ifdef CLOSURE_REFRACTION 8561 /* ---------------------------- */ 8562 /* Screen Space Refraction */ 8563 /* ---------------------------- */ 8564 # ifdef USE_REFRACTION 8565 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 8566 /* Find approximated position of the 2nd refraction event. */ 8567 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 8568 viewPosition; 8569 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 8570 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 8571 accumulate_light(trans.rgb, trans.a, refr_accum); 8572 } 8573 # endif 8574 8575 # endif 8576 8577 /* ---------------------------- */ 8578 /* Specular probes */ 8579 /* ---------------------------- */ 8580 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8581 8582 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 8583 # define GLASS_ACCUM 1 8584 # define ACCUM min(refr_accum.a, spec_accum.a) 8585 # elif defined(CLOSURE_REFRACTION) 8586 # define GLASS_ACCUM 0 8587 # define ACCUM refr_accum.a 8588 # else 8589 # define GLASS_ACCUM 0 8590 # define ACCUM spec_accum.a 8591 # endif 8592 8593 /* Starts at 1 because 0 is world probe */ 8594 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 8595 float fade = probe_attenuation_cube(i, worldPosition); 8596 8597 if (fade > 0.0) { 8598 8599 # if GLASS_ACCUM 8600 if (spec_accum.a < 0.999) { 8601 # endif 8602 # ifdef CLOSURE_GLOSSY 8603 if (!(ssrToggle && ssr_id == outputSsrId)) { 8604 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 8605 accumulate_light(spec, fade, spec_accum); 8606 } 8607 # endif 8608 8609 # ifdef CLOSURE_CLEARCOAT 8610 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 8611 accumulate_light(C_spec, fade, C_spec_accum); 8612 # endif 8613 # if GLASS_ACCUM 8614 } 8615 # endif 8616 8617 # if GLASS_ACCUM 8618 if (refr_accum.a < 0.999) { 8619 # endif 8620 # ifdef CLOSURE_REFRACTION 8621 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 8622 accumulate_light(trans, fade, refr_accum); 8623 # endif 8624 # if GLASS_ACCUM 8625 } 8626 # endif 8627 } 8628 } 8629 8630 # undef GLASS_ACCUM 8631 # undef ACCUM 8632 8633 /* ---------------------------- */ 8634 /* World Probe */ 8635 /* ---------------------------- */ 8636 # ifdef CLOSURE_GLOSSY 8637 if (spec_accum.a < 0.999) { 8638 if (!(ssrToggle && ssr_id == outputSsrId)) { 8639 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 8640 accumulate_light(spec, 1.0, spec_accum); 8641 } 8642 8643 # ifdef CLOSURE_CLEARCOAT 8644 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 8645 accumulate_light(C_spec, 1.0, C_spec_accum); 8646 # endif 8647 } 8648 # endif 8649 8650 # ifdef CLOSURE_REFRACTION 8651 if (refr_accum.a < 0.999) { 8652 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 8653 accumulate_light(trans, 1.0, refr_accum); 8654 } 8655 # endif 8656 # endif /* Specular probes */ 8657 8658 /* ---------------------------- */ 8659 /* Ambient Occlusion */ 8660 /* ---------------------------- */ 8661 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 8662 if (!use_contact_shadows) { 8663 /* HACK: Fix for translucent BSDF. (see T65631) */ 8664 N = -N; 8665 } 8666 vec3 bent_normal; 8667 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 8668 if (!use_contact_shadows) { 8669 N = -N; 8670 /* Bypass bent normal. */ 8671 bent_normal = N; 8672 } 8673 # endif 8674 8675 /* ---------------------------- */ 8676 /* Specular Output */ 8677 /* ---------------------------- */ 8678 float NV = dot(N, V); 8679 # ifdef CLOSURE_GLOSSY 8680 vec2 uv = lut_coords(NV, roughness); 8681 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 8682 8683 /* This factor is outputted to be used by SSR in order 8684 * to match the intensity of the regular reflections. */ 8685 ssr_spec = F_ibl(f0, f90, brdf_lut); 8686 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 8687 8688 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 8689 if (ssrToggle && ssr_id == outputSsrId) { 8690 spec_occlu = 1.0; 8691 } 8692 8693 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 8694 # endif 8695 8696 # ifdef CLOSURE_REFRACTION 8697 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 8698 8699 out_refr += refr_accum.rgb * btdf; 8700 # endif 8701 8702 # ifdef CLOSURE_CLEARCOAT 8703 NV = dot(C_N, V); 8704 vec2 C_uv = lut_coords(NV, C_roughness); 8705 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 8706 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 8707 specular_occlusion(NV, final_ao, C_roughness); 8708 8709 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 8710 # endif 8711 8712 # ifdef CLOSURE_GLOSSY 8713 /* Global toggle for lightprobe baking. */ 8714 out_spec *= float(specToggle); 8715 # endif 8716 8717 /* ---------------------------------------------------------------- */ 8718 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 8719 /* ---------------------------------------------------------------- */ 8720 8721 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 8722 # ifdef CLOSURE_DIFFUSE 8723 vec4 diff_accum = vec4(0.0); 8724 8725 /* ---------------------------- */ 8726 /* Irradiance Grids */ 8727 /* ---------------------------- */ 8728 /* Start at 1 because 0 is world irradiance */ 8729 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 8730 GridData gd = grids_data[i]; 8731 8732 vec3 localpos; 8733 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 8734 8735 if (fade > 0.0) { 8736 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 8737 accumulate_light(diff, fade, diff_accum); 8738 } 8739 } 8740 8741 /* ---------------------------- */ 8742 /* World Diffuse */ 8743 /* ---------------------------- */ 8744 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 8745 vec3 diff = probe_evaluate_world_diff(bent_normal); 8746 accumulate_light(diff, 1.0, diff_accum); 8747 } 8748 8749 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 8750 # endif 8751 #endif 8752 } 8753 8754 /* Cleanup for next configuration */ 8755 #undef CLOSURE_NAME 8756 8757 #ifdef CLOSURE_DIFFUSE 8758 # undef CLOSURE_DIFFUSE 8759 #endif 8760 8761 #ifdef CLOSURE_GLOSSY 8762 # undef CLOSURE_GLOSSY 8763 #endif 8764 8765 #ifdef CLOSURE_CLEARCOAT 8766 # undef CLOSURE_CLEARCOAT 8767 #endif 8768 8769 #ifdef CLOSURE_REFRACTION 8770 # undef CLOSURE_REFRACTION 8771 #endif 8772 8773 #ifdef CLOSURE_SUBSURFACE 8774 # undef CLOSURE_SUBSURFACE 8775 #endif 8776 8777 #ifndef LIT_SURFACE_UNIFORM 8778 # define LIT_SURFACE_UNIFORM 8779 8780 uniform float refractionDepth; 8781 8782 # ifndef UTIL_TEX 8783 # define UTIL_TEX 8784 uniform sampler2DArray utilTex; 8785 # define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0) 8786 # endif /* UTIL_TEX */ 8787 8788 in vec3 worldPosition; 8789 in vec3 viewPosition; 8790 8791 in vec3 worldNormal; 8792 in vec3 viewNormal; 8793 8794 # ifdef HAIR_SHADER 8795 in vec3 hairTangent; /* world space */ 8796 in float hairThickTime; 8797 in float hairThickness; 8798 in float hairTime; 8799 flat in int hairStrandID; 8800 8801 uniform int hairThicknessRes = 1; 8802 # endif 8803 8804 #endif /* LIT_SURFACE_UNIFORM */ 8805 8806 /** 8807 * AUTO CONFIG 8808 * We include the file multiple times each time with a different configuration. 8809 * This leads to a lot of deadcode. Better idea would be to only generate the one needed. 8810 */ 8811 #if !defined(SURFACE_DEFAULT) 8812 # define SURFACE_DEFAULT 8813 # define CLOSURE_NAME eevee_closure_default 8814 # define CLOSURE_DIFFUSE 8815 # define CLOSURE_GLOSSY 8816 #endif /* SURFACE_DEFAULT */ 8817 8818 #if !defined(SURFACE_DEFAULT_CLEARCOAT) && !defined(CLOSURE_NAME) 8819 # define SURFACE_DEFAULT_CLEARCOAT 8820 # define CLOSURE_NAME eevee_closure_default_clearcoat 8821 # define CLOSURE_DIFFUSE 8822 # define CLOSURE_GLOSSY 8823 # define CLOSURE_CLEARCOAT 8824 #endif /* SURFACE_DEFAULT_CLEARCOAT */ 8825 8826 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME) 8827 # define SURFACE_PRINCIPLED 8828 # define CLOSURE_NAME eevee_closure_principled 8829 # define CLOSURE_DIFFUSE 8830 # define CLOSURE_GLOSSY 8831 # define CLOSURE_CLEARCOAT 8832 # define CLOSURE_REFRACTION 8833 # define CLOSURE_SUBSURFACE 8834 #endif /* SURFACE_PRINCIPLED */ 8835 8836 #if !defined(SURFACE_CLEARCOAT) && !defined(CLOSURE_NAME) 8837 # define SURFACE_CLEARCOAT 8838 # define CLOSURE_NAME eevee_closure_clearcoat 8839 # define CLOSURE_GLOSSY 8840 # define CLOSURE_CLEARCOAT 8841 #endif /* SURFACE_CLEARCOAT */ 8842 8843 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME) 8844 # define SURFACE_DIFFUSE 8845 # define CLOSURE_NAME eevee_closure_diffuse 8846 # define CLOSURE_DIFFUSE 8847 #endif /* SURFACE_DIFFUSE */ 8848 8849 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME) 8850 # define SURFACE_SUBSURFACE 8851 # define CLOSURE_NAME eevee_closure_subsurface 8852 # define CLOSURE_DIFFUSE 8853 # define CLOSURE_SUBSURFACE 8854 #endif /* SURFACE_SUBSURFACE */ 8855 8856 #if !defined(SURFACE_SKIN) && !defined(CLOSURE_NAME) 8857 # define SURFACE_SKIN 8858 # define CLOSURE_NAME eevee_closure_skin 8859 # define CLOSURE_DIFFUSE 8860 # define CLOSURE_SUBSURFACE 8861 # define CLOSURE_GLOSSY 8862 #endif /* SURFACE_SKIN */ 8863 8864 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME) 8865 # define SURFACE_GLOSSY 8866 # define CLOSURE_NAME eevee_closure_glossy 8867 # define CLOSURE_GLOSSY 8868 #endif /* SURFACE_GLOSSY */ 8869 8870 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME) 8871 # define SURFACE_REFRACT 8872 # define CLOSURE_NAME eevee_closure_refraction 8873 # define CLOSURE_REFRACTION 8874 #endif /* SURFACE_REFRACT */ 8875 8876 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME) 8877 # define SURFACE_GLASS 8878 # define CLOSURE_NAME eevee_closure_glass 8879 # define CLOSURE_GLOSSY 8880 # define CLOSURE_REFRACTION 8881 #endif /* SURFACE_GLASS */ 8882 8883 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */ 8884 #ifdef CLOSURE_CLEARCOAT 8885 # ifndef CLOSURE_GLOSSY 8886 # define CLOSURE_GLOSSY 8887 # endif 8888 #endif /* CLOSURE_CLEARCOAT */ 8889 8890 void CLOSURE_NAME(vec3 N 8891 #ifdef CLOSURE_DIFFUSE 8892 , 8893 vec3 albedo 8894 #endif 8895 #ifdef CLOSURE_GLOSSY 8896 , 8897 vec3 f0, 8898 vec3 f90, 8899 int ssr_id 8900 #endif 8901 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8902 , 8903 float roughness 8904 #endif 8905 #ifdef CLOSURE_CLEARCOAT 8906 , 8907 vec3 C_N, 8908 float C_intensity, 8909 float C_roughness 8910 #endif 8911 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 8912 , 8913 float ao 8914 #endif 8915 #ifdef CLOSURE_SUBSURFACE 8916 , 8917 float sss_scale 8918 #endif 8919 #ifdef CLOSURE_REFRACTION 8920 , 8921 float ior 8922 #endif 8923 , 8924 const bool use_contact_shadows 8925 #ifdef CLOSURE_DIFFUSE 8926 , 8927 out vec3 out_diff 8928 #endif 8929 #ifdef CLOSURE_GLOSSY 8930 , 8931 out vec3 out_spec 8932 #endif 8933 #ifdef CLOSURE_REFRACTION 8934 , 8935 out vec3 out_refr 8936 #endif 8937 #ifdef CLOSURE_GLOSSY 8938 , 8939 out vec3 ssr_spec 8940 #endif 8941 ) 8942 { 8943 #ifdef CLOSURE_DIFFUSE 8944 out_diff = vec3(0.0); 8945 #endif 8946 8947 #ifdef CLOSURE_GLOSSY 8948 out_spec = vec3(0.0); 8949 #endif 8950 8951 #ifdef CLOSURE_REFRACTION 8952 out_refr = vec3(0.0); 8953 #endif 8954 8955 #if defined(SHADOW_SHADER) || defined(WORLD_BACKGROUND) 8956 /* This makes shader resources become unused and avoid issues with samplers. (see T59747) */ 8957 return; 8958 #else 8959 8960 /* Zero length vectors cause issues, see: T51979. */ 8961 float len = length(N); 8962 if (isnan(len)) { 8963 return; 8964 } 8965 N /= len; 8966 8967 # ifdef CLOSURE_CLEARCOAT 8968 len = length(C_N); 8969 if (isnan(len)) { 8970 return; 8971 } 8972 C_N /= len; 8973 # endif 8974 8975 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 8976 roughness = clamp(roughness, 1e-8, 0.9999); 8977 float roughnessSquared = roughness * roughness; 8978 # endif 8979 8980 # ifdef CLOSURE_CLEARCOAT 8981 C_roughness = clamp(C_roughness, 1e-8, 0.9999); 8982 float C_roughnessSquared = C_roughness * C_roughness; 8983 # endif 8984 8985 vec3 V = cameraVec; 8986 8987 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 8988 8989 /* ---------------------------------------------------------------- */ 8990 /* -------------------- SCENE LIGHTS LIGHTING --------------------- */ 8991 /* ---------------------------------------------------------------- */ 8992 8993 # ifdef CLOSURE_GLOSSY 8994 vec2 lut_uv = lut_coords_ltc(dot(N, V), roughness); 8995 vec4 ltc_mat = texture(utilTex, vec3(lut_uv, 0.0)).rgba; 8996 # endif 8997 8998 # ifdef CLOSURE_CLEARCOAT 8999 vec2 lut_uv_clear = lut_coords_ltc(dot(C_N, V), C_roughness); 9000 vec4 ltc_mat_clear = texture(utilTex, vec3(lut_uv_clear, 0.0)).rgba; 9001 vec3 out_spec_clear = vec3(0.0); 9002 # endif 9003 9004 float tracing_depth = gl_FragCoord.z; 9005 /* Constant bias (due to depth buffer precision) */ 9006 /* Magic numbers for 24bits of precision. 9007 * From http://terathon.com/gdc07_lengyel.pdf (slide 26) */ 9008 tracing_depth -= mix(2.4e-7, 4.8e-7, gl_FragCoord.z); 9009 /* Convert to view Z. */ 9010 tracing_depth = get_view_z_from_depth(tracing_depth); 9011 9012 vec3 true_normal = normalize(cross(dFdx(viewPosition), dFdy(viewPosition))); 9013 9014 for (int i = 0; i < MAX_LIGHT && i < laNumLight; i++) { 9015 LightData ld = lights_data[i]; 9016 9017 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ 9018 l_vector.xyz = ld.l_position - worldPosition; 9019 l_vector.w = length(l_vector.xyz); 9020 9021 float l_vis = light_visibility(ld, 9022 worldPosition, 9023 viewPosition, 9024 tracing_depth, 9025 true_normal, 9026 rand.x, 9027 use_contact_shadows, 9028 l_vector); 9029 9030 if (l_vis < 1e-8) { 9031 continue; 9032 } 9033 9034 vec3 l_color_vis = ld.l_color * l_vis; 9035 9036 # ifdef CLOSURE_DIFFUSE 9037 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector); 9038 # endif 9039 9040 # ifdef CLOSURE_GLOSSY 9041 out_spec += l_color_vis * light_specular(ld, ltc_mat, N, V, l_vector) * ld.l_spec; 9042 # endif 9043 9044 # ifdef CLOSURE_CLEARCOAT 9045 out_spec_clear += l_color_vis * light_specular(ld, ltc_mat_clear, C_N, V, l_vector) * 9046 ld.l_spec; 9047 # endif 9048 } 9049 9050 # ifdef CLOSURE_GLOSSY 9051 vec2 brdf_lut_lights = texture(utilTex, vec3(lut_uv, 1.0)).ba; 9052 out_spec *= F_area(f0, f90, brdf_lut_lights.xy); 9053 # endif 9054 9055 # ifdef CLOSURE_CLEARCOAT 9056 vec2 brdf_lut_lights_clear = texture(utilTex, vec3(lut_uv_clear, 1.0)).ba; 9057 out_spec_clear *= F_area(vec3(0.04), vec3(1.0), brdf_lut_lights_clear.xy); 9058 out_spec += out_spec_clear * C_intensity; 9059 # endif 9060 9061 /* ---------------------------------------------------------------- */ 9062 /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ 9063 /* ---------------------------------------------------------------- */ 9064 9065 /* Accumulate incoming light from all sources until accumulator is full. Then apply Occlusion and 9066 * BRDF. */ 9067 # ifdef CLOSURE_GLOSSY 9068 vec4 spec_accum = vec4(0.0); 9069 # endif 9070 9071 # ifdef CLOSURE_CLEARCOAT 9072 vec4 C_spec_accum = vec4(0.0); 9073 # endif 9074 9075 # ifdef CLOSURE_REFRACTION 9076 vec4 refr_accum = vec4(0.0); 9077 # endif 9078 9079 # ifdef CLOSURE_GLOSSY 9080 /* ---------------------------- */ 9081 /* Planar Reflections */ 9082 /* ---------------------------- */ 9083 9084 for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; i++) { 9085 PlanarData pd = planars_data[i]; 9086 9087 /* Fade on geometric normal. */ 9088 float fade = probe_attenuation_planar( 9089 pd, worldPosition, (gl_FrontFacing) ? worldNormal : -worldNormal, roughness); 9090 9091 if (fade > 0.0) { 9092 if (!(ssrToggle && ssr_id == outputSsrId)) { 9093 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); 9094 accumulate_light(spec, fade, spec_accum); 9095 } 9096 9097 # ifdef CLOSURE_CLEARCOAT 9098 vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); 9099 accumulate_light(C_spec, fade, C_spec_accum); 9100 # endif 9101 } 9102 } 9103 # endif 9104 9105 # ifdef CLOSURE_GLOSSY 9106 vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); 9107 # endif 9108 9109 # ifdef CLOSURE_CLEARCOAT 9110 vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); 9111 # endif 9112 9113 # ifdef CLOSURE_REFRACTION 9114 /* Refract the view vector using the depth heuristic. 9115 * Then later Refract a second time the already refracted 9116 * ray using the inverse ior. */ 9117 float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; 9118 vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; 9119 vec3 refr_pos = (refractionDepth > 0.0) ? 9120 line_plane_intersect( 9121 worldPosition, refr_V, worldPosition - N * refractionDepth, N) : 9122 worldPosition; 9123 vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); 9124 # endif 9125 9126 # ifdef CLOSURE_REFRACTION 9127 /* ---------------------------- */ 9128 /* Screen Space Refraction */ 9129 /* ---------------------------- */ 9130 # ifdef USE_REFRACTION 9131 if (ssrefractToggle && roughness < ssrMaxRoughness + 0.2) { 9132 /* Find approximated position of the 2nd refraction event. */ 9133 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : 9134 viewPosition; 9135 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand); 9136 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness); 9137 accumulate_light(trans.rgb, trans.a, refr_accum); 9138 } 9139 # endif 9140 9141 # endif 9142 9143 /* ---------------------------- */ 9144 /* Specular probes */ 9145 /* ---------------------------- */ 9146 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION) 9147 9148 # if defined(CLOSURE_GLOSSY) && defined(CLOSURE_REFRACTION) 9149 # define GLASS_ACCUM 1 9150 # define ACCUM min(refr_accum.a, spec_accum.a) 9151 # elif defined(CLOSURE_REFRACTION) 9152 # define GLASS_ACCUM 0 9153 # define ACCUM refr_accum.a 9154 # else 9155 # define GLASS_ACCUM 0 9156 # define ACCUM spec_accum.a 9157 # endif 9158 9159 /* Starts at 1 because 0 is world probe */ 9160 for (int i = 1; ACCUM < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; i++) { 9161 float fade = probe_attenuation_cube(i, worldPosition); 9162 9163 if (fade > 0.0) { 9164 9165 # if GLASS_ACCUM 9166 if (spec_accum.a < 0.999) { 9167 # endif 9168 # ifdef CLOSURE_GLOSSY 9169 if (!(ssrToggle && ssr_id == outputSsrId)) { 9170 vec3 spec = probe_evaluate_cube(i, worldPosition, spec_dir, roughness); 9171 accumulate_light(spec, fade, spec_accum); 9172 } 9173 # endif 9174 9175 # ifdef CLOSURE_CLEARCOAT 9176 vec3 C_spec = probe_evaluate_cube(i, worldPosition, C_spec_dir, C_roughness); 9177 accumulate_light(C_spec, fade, C_spec_accum); 9178 # endif 9179 # if GLASS_ACCUM 9180 } 9181 # endif 9182 9183 # if GLASS_ACCUM 9184 if (refr_accum.a < 0.999) { 9185 # endif 9186 # ifdef CLOSURE_REFRACTION 9187 vec3 trans = probe_evaluate_cube(i, refr_pos, refr_dir, roughnessSquared); 9188 accumulate_light(trans, fade, refr_accum); 9189 # endif 9190 # if GLASS_ACCUM 9191 } 9192 # endif 9193 } 9194 } 9195 9196 # undef GLASS_ACCUM 9197 # undef ACCUM 9198 9199 /* ---------------------------- */ 9200 /* World Probe */ 9201 /* ---------------------------- */ 9202 # ifdef CLOSURE_GLOSSY 9203 if (spec_accum.a < 0.999) { 9204 if (!(ssrToggle && ssr_id == outputSsrId)) { 9205 vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); 9206 accumulate_light(spec, 1.0, spec_accum); 9207 } 9208 9209 # ifdef CLOSURE_CLEARCOAT 9210 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); 9211 accumulate_light(C_spec, 1.0, C_spec_accum); 9212 # endif 9213 } 9214 # endif 9215 9216 # ifdef CLOSURE_REFRACTION 9217 if (refr_accum.a < 0.999) { 9218 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared); 9219 accumulate_light(trans, 1.0, refr_accum); 9220 } 9221 # endif 9222 # endif /* Specular probes */ 9223 9224 /* ---------------------------- */ 9225 /* Ambient Occlusion */ 9226 /* ---------------------------- */ 9227 # if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE) 9228 if (!use_contact_shadows) { 9229 /* HACK: Fix for translucent BSDF. (see T65631) */ 9230 N = -N; 9231 } 9232 vec3 bent_normal; 9233 float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal); 9234 if (!use_contact_shadows) { 9235 N = -N; 9236 /* Bypass bent normal. */ 9237 bent_normal = N; 9238 } 9239 # endif 9240 9241 /* ---------------------------- */ 9242 /* Specular Output */ 9243 /* ---------------------------- */ 9244 float NV = dot(N, V); 9245 # ifdef CLOSURE_GLOSSY 9246 vec2 uv = lut_coords(NV, roughness); 9247 vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; 9248 9249 /* This factor is outputted to be used by SSR in order 9250 * to match the intensity of the regular reflections. */ 9251 ssr_spec = F_ibl(f0, f90, brdf_lut); 9252 float spec_occlu = specular_occlusion(NV, final_ao, roughness); 9253 9254 /* The SSR pass recompute the occlusion to not apply it to the SSR */ 9255 if (ssrToggle && ssr_id == outputSsrId) { 9256 spec_occlu = 1.0; 9257 } 9258 9259 out_spec += spec_accum.rgb * ssr_spec * spec_occlu; 9260 # endif 9261 9262 # ifdef CLOSURE_REFRACTION 9263 float btdf = get_btdf_lut(utilTex, NV, roughness, ior); 9264 9265 out_refr += refr_accum.rgb * btdf; 9266 # endif 9267 9268 # ifdef CLOSURE_CLEARCOAT 9269 NV = dot(C_N, V); 9270 vec2 C_uv = lut_coords(NV, C_roughness); 9271 vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg; 9272 vec3 C_fresnel = F_ibl(vec3(0.04), vec3(1.0), C_brdf_lut) * 9273 specular_occlusion(NV, final_ao, C_roughness); 9274 9275 out_spec += C_spec_accum.rgb * C_fresnel * C_intensity; 9276 # endif 9277 9278 # ifdef CLOSURE_GLOSSY 9279 /* Global toggle for lightprobe baking. */ 9280 out_spec *= float(specToggle); 9281 # endif 9282 9283 /* ---------------------------------------------------------------- */ 9284 /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */ 9285 /* ---------------------------------------------------------------- */ 9286 9287 /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ 9288 # ifdef CLOSURE_DIFFUSE 9289 vec4 diff_accum = vec4(0.0); 9290 9291 /* ---------------------------- */ 9292 /* Irradiance Grids */ 9293 /* ---------------------------- */ 9294 /* Start at 1 because 0 is world irradiance */ 9295 for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; i++) { 9296 GridData gd = grids_data[i]; 9297 9298 vec3 localpos; 9299 float fade = probe_attenuation_grid(gd, grids_data[i].localmat, worldPosition, localpos); 9300 9301 if (fade > 0.0) { 9302 vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); 9303 accumulate_light(diff, fade, diff_accum); 9304 } 9305 } 9306 9307 /* ---------------------------- */ 9308 /* World Diffuse */ 9309 /* ---------------------------- */ 9310 if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) { 9311 vec3 diff = probe_evaluate_world_diff(bent_normal); 9312 accumulate_light(diff, 1.0, diff_accum); 9313 } 9314 9315 out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo); 9316 # endif 9317 #endif 9318 } 9319 9320 /* Cleanup for next configuration */ 9321 #undef CLOSURE_NAME 9322 9323 #ifdef CLOSURE_DIFFUSE 9324 # undef CLOSURE_DIFFUSE 9325 #endif 9326 9327 #ifdef CLOSURE_GLOSSY 9328 # undef CLOSURE_GLOSSY 9329 #endif 9330 9331 #ifdef CLOSURE_CLEARCOAT 9332 # undef CLOSURE_CLEARCOAT 9333 #endif 9334 9335 #ifdef CLOSURE_REFRACTION 9336 # undef CLOSURE_REFRACTION 9337 #endif 9338 9339 #ifdef CLOSURE_SUBSURFACE 9340 # undef CLOSURE_SUBSURFACE 9341 #endif 9342 9343 /* Based on Frosbite Unified Volumetric. 9344 * https://www.ea.com/frostbite/news/physically-based-unified-volumetric-rendering-in-frostbite */ 9345 9346 /* Volume slice to view space depth. */ 9347 float volume_z_to_view_z(float z) 9348 { 9349 if (ProjectionMatrix[3][3] == 0.0) { 9350 /* Exponential distribution */ 9351 return (exp2(z / volDepthParameters.z) - volDepthParameters.x) / volDepthParameters.y; 9352 } 9353 else { 9354 /* Linear distribution */ 9355 return mix(volDepthParameters.x, volDepthParameters.y, z); 9356 } 9357 } 9358 9359 float view_z_to_volume_z(float depth) 9360 { 9361 if (ProjectionMatrix[3][3] == 0.0) { 9362 /* Exponential distribution */ 9363 return volDepthParameters.z * log2(depth * volDepthParameters.y + volDepthParameters.x); 9364 } 9365 else { 9366 /* Linear distribution */ 9367 return (depth - volDepthParameters.x) * volDepthParameters.z; 9368 } 9369 } 9370 9371 /* Volume texture normalized coordinates to NDC (special range [0, 1]). */ 9372 vec3 volume_to_ndc(vec3 cos) 9373 { 9374 cos.z = volume_z_to_view_z(cos.z); 9375 cos.z = get_depth_from_view_z(cos.z); 9376 cos.xy /= volCoordScale.xy; 9377 return cos; 9378 } 9379 9380 vec3 ndc_to_volume(vec3 cos) 9381 { 9382 cos.z = get_view_z_from_depth(cos.z); 9383 cos.z = view_z_to_volume_z(cos.z); 9384 cos.xy *= volCoordScale.xy; 9385 return cos; 9386 } 9387 9388 float phase_function_isotropic() 9389 { 9390 return 1.0 / (4.0 * M_PI); 9391 } 9392 9393 float phase_function(vec3 v, vec3 l, float g) 9394 { 9395 /* Henyey-Greenstein */ 9396 float cos_theta = dot(v, l); 9397 g = clamp(g, -1.0 + 1e-3, 1.0 - 1e-3); 9398 float sqr_g = g * g; 9399 return (1 - sqr_g) / max(1e-8, 4.0 * M_PI * pow(1 + sqr_g - 2 * g * cos_theta, 3.0 / 2.0)); 9400 } 9401 9402 #ifdef LAMPS_LIB 9403 vec3 light_volume(LightData ld, vec4 l_vector) 9404 { 9405 float power; 9406 /* TODO : Area lighting ? */ 9407 /* XXX : Removing Area Power. */ 9408 /* TODO : put this out of the shader. */ 9409 /* See eevee_light_setup(). */ 9410 if (ld.l_type == AREA_RECT || ld.l_type == AREA_ELLIPSE) { 9411 power = (ld.l_sizex * ld.l_sizey * 4.0 * M_PI) * (1.0 / 80.0); 9412 if (ld.l_type == AREA_ELLIPSE) { 9413 power *= M_PI * 0.25; 9414 } 9415 power *= 20.0 * 9416 max(0.0, dot(-ld.l_forward, l_vector.xyz / l_vector.w)); /* XXX ad hoc, empirical */ 9417 } 9418 else if (ld.l_type == SUN) { 9419 power = ld.l_radius * ld.l_radius * M_PI; /* Removing area light power*/ 9420 power /= 1.0f + (ld.l_radius * ld.l_radius * 0.5f); 9421 power *= M_PI * 0.5; /* Matching cycles. */ 9422 } 9423 else { 9424 power = (4.0 * ld.l_radius * ld.l_radius) * (1.0 / 10.0); 9425 power *= M_2PI; /* Matching cycles with point light. */ 9426 } 9427 9428 power /= (l_vector.w * l_vector.w); 9429 9430 /* OPTI: find a better way than calculating this on the fly */ 9431 float lum = dot(ld.l_color, vec3(0.3, 0.6, 0.1)); /* luminance approx. */ 9432 vec3 tint = (lum > 0.0) ? ld.l_color / lum : vec3(1.0); /* normalize lum. to isolate hue+sat */ 9433 9434 lum = min(lum * power, volLightClamp); 9435 9436 return tint * lum; 9437 } 9438 9439 # define VOLUMETRIC_SHADOW_MAX_STEP 32.0 9440 9441 vec3 participating_media_extinction(vec3 wpos, sampler3D volume_extinction) 9442 { 9443 /* Waiting for proper volume shadowmaps and out of frustum shadow map. */ 9444 vec3 ndc = project_point(ViewProjectionMatrix, wpos); 9445 vec3 volume_co = ndc_to_volume(ndc * 0.5 + 0.5); 9446 9447 /* Let the texture be clamped to edge. This reduce visual glitches. */ 9448 return texture(volume_extinction, volume_co).rgb; 9449 } 9450 9451 vec3 light_volume_shadow(LightData ld, vec3 ray_wpos, vec4 l_vector, sampler3D volume_extinction) 9452 { 9453 # if defined(VOLUME_SHADOW) 9454 /* Heterogeneous volume shadows */ 9455 float dd = l_vector.w / volShadowSteps; 9456 vec3 L = l_vector.xyz * l_vector.w; 9457 vec3 shadow = vec3(1.0); 9458 for (float s = 0.5; s < VOLUMETRIC_SHADOW_MAX_STEP && s < (volShadowSteps - 0.1); s += 1.0) { 9459 vec3 pos = ray_wpos + L * (s / volShadowSteps); 9460 vec3 s_extinction = participating_media_extinction(pos, volume_extinction); 9461 shadow *= exp(-s_extinction * dd); 9462 } 9463 return shadow; 9464 # else 9465 return vec3(1.0); 9466 # endif /* VOLUME_SHADOW */ 9467 } 9468 #endif 9469 9470 #ifdef IRRADIANCE_LIB 9471 vec3 irradiance_volumetric(vec3 wpos) 9472 { 9473 # ifdef IRRADIANCE_HL2 9474 IrradianceData ir_data = load_irradiance_cell(0, vec3(1.0)); 9475 vec3 irradiance = ir_data.cubesides[0] + ir_data.cubesides[1] + ir_data.cubesides[2]; 9476 ir_data = load_irradiance_cell(0, vec3(-1.0)); 9477 irradiance += ir_data.cubesides[0] + ir_data.cubesides[1] + ir_data.cubesides[2]; 9478 irradiance *= 0.16666666; /* 1/6 */ 9479 return irradiance; 9480 # else 9481 return vec3(0.0); 9482 # endif 9483 } 9484 #endif 9485 9486 uniform sampler3D inScattering; 9487 uniform sampler3D inTransmittance; 9488 9489 void volumetric_resolve(vec2 frag_uvs, 9490 float frag_depth, 9491 out vec3 transmittance, 9492 out vec3 scattering) 9493 { 9494 vec3 volume_cos = ndc_to_volume(vec3(frag_uvs, frag_depth)); 9495 9496 scattering = texture(inScattering, volume_cos).rgb; 9497 transmittance = texture(inTransmittance, volume_cos).rgb; 9498 } 9499 9500 uniform vec3 basecol; 9501 uniform float metallic; 9502 uniform float specular; 9503 uniform float roughness; 9504 9505 Closure nodetree_exec(void) 9506 { 9507 #ifdef HAIR_SHADER 9508 vec3 B = normalize(cross(worldNormal, hairTangent)); 9509 float cos_theta; 9510 if (hairThicknessRes == 1) { 9511 vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0); 9512 /* Random cosine normal distribution on the hair surface. */ 9513 cos_theta = rand.x * 2.0 - 1.0; 9514 } 9515 else { 9516 /* Shade as a cylinder. */ 9517 cos_theta = hairThickTime / hairThickness; 9518 } 9519 float sin_theta = sqrt(max(0.0, 1.0f - cos_theta * cos_theta)); 9520 vec3 N = normalize(worldNormal * sin_theta + B * cos_theta); 9521 vec3 vN = mat3(ViewMatrix) * N; 9522 #else 9523 vec3 N = normalize(gl_FrontFacing ? worldNormal : -worldNormal); 9524 vec3 vN = normalize(gl_FrontFacing ? viewNormal : -viewNormal); 9525 #endif 9526 9527 vec3 dielectric = vec3(0.034) * specular * 2.0; 9528 vec3 albedo = mix(basecol, vec3(0.0), metallic); 9529 vec3 f0 = mix(dielectric, basecol, metallic); 9530 vec3 f90 = mix(vec3(1.0), f0, (1.0 - specular) * metallic); 9531 vec3 out_diff, out_spec, ssr_spec; 9532 eevee_closure_default(N, albedo, f0, f90, 1, roughness, 1.0, true, out_diff, out_spec, ssr_spec); 9533 9534 Closure cl = CLOSURE_DEFAULT; 9535 cl.radiance = out_spec + out_diff * albedo; 9536 closure_load_ssr_data(ssr_spec, roughness, N, viewCameraVec, 1, cl); 9537 9538 #ifdef LOOKDEV 9539 gl_FragDepth = 0.0; 9540 #endif 9541 9542 #ifdef HOLDOUT 9543 cl = CLOSURE_DEFAULT; 9544 cl.holdout = 1.0; 9545 #endif 9546 9547 return cl; 9548 } Fragment info ------------- 0(1623) : warning C7050: "bent_normal" might be used before being initialized 0(3299) : warning C7050: "ssr_spec" might be used before being initialized Internal error: assembly compile error for fragment shader at offset 284378: -- error message -- line 4000, column 15: error: invalid integer constant -- internal assembly text -- !!NVfp5.0 OPTION ARB_draw_buffers; OPTION ARB_fragment_program_shadow; # cgc version 3.0.0005, build date Feb 18 2011 # command line args: #vendor NVIDIA Corporation #version 3.0.0.05 #profile gp5fp #program main #semantic viewBlock : BUFFER[0] #semantic modelBlock : BUFFER[1] #semantic common_block : BUFFER[2] #semantic probe_block : BUFFER[3] #semantic grid_block : BUFFER[4] #semantic planar_block : BUFFER[5] #semantic shadow_block : BUFFER[6] #semantic light_block : BUFFER[7] #semantic resourceChunk #semantic colorBuffer #semantic depthBuffer #semantic maxzBuffer #semantic minzBuffer #semantic planarDepth #semantic outputSsrId #semantic outputSssId #semantic texHammersley #semantic texJitter #semantic sampleCount #semantic invSampleCount #semantic utilTex #semantic horizonBuffer #semantic irradianceGrid #semantic probePlanars #semantic probeCubes #semantic shadowCubeTexture #semantic shadowCascadeTexture #semantic refractionDepth #semantic inScattering #semantic inTransmittance #semantic basecol #semantic metallic #semantic specular #semantic roughness #var float gl_FrontFacing : $vin.FACE : SSA : -1 : 1 #var float4 gl_FragCoord : $vin.WPOS : WPOS : -1 : 1 #var float4x4 ViewProjectionMatrix : : buffer[0][0], 4 : -1 : 1 #var float4x4 ViewProjectionMatrixInverse : : buffer[0][64], 4 : -1 : 0 #var float4x4 ViewMatrix : : buffer[0][128], 4 : -1 : 1 #var float4x4 ViewMatrixInverse : : buffer[0][192], 4 : -1 : 1 #var float4x4 ProjectionMatrix : : buffer[0][256], 4 : -1 : 1 #var float4x4 ProjectionMatrixInverse : : buffer[0][320], 4 : -1 : 0 #var float4 clipPlanes[0] : : buffer[0][384] : -1 : 0 #var float4 clipPlanes[1] : : buffer[0][400] : -1 : 0 #var float4 clipPlanes[2] : : buffer[0][416] : -1 : 0 #var float4 clipPlanes[3] : : buffer[0][432] : -1 : 0 #var float4 clipPlanes[4] : : buffer[0][448] : -1 : 0 #var float4 clipPlanes[5] : : buffer[0][464] : -1 : 0 #var float4 CameraTexCoFactors : : buffer[0][480] : -1 : 0 #var float4x4 drw_matrices[0].drw_modelMatrix : : buffer[1][0], 4 : -1 : 0 #var float4x4 drw_matrices[0].drw_modelMatrixInverse : : buffer[1][64], 4 : -1 : 0 #var float4x4 drw_matrices[1].drw_modelMatrix : : buffer[1][128], 4 : -1 : 0 #var float4x4 drw_matrices[1].drw_modelMatrixInverse : : buffer[1][192], 4 : -1 : 0 #var float4x4 drw_matrices[2].drw_modelMatrix : : buffer[1][256], 4 : -1 : 0 #var float4x4 drw_matrices[2].drw_modelMatrixInverse : : buffer[1][320], 4 : -1 : 0 #var float4x4 drw_matrices[3].drw_modelMatrix : : buffer[1][384], 4 : -1 : 0 #var float4x4 drw_matrices[3].drw_modelMatrixInverse : : buffer[1][448], 4 : -1 : 0 #var float4x4 drw_matrices[4].drw_modelMatrix : : buffer[1][512], 4 : -1 : 0 #var float4x4 drw_matrices[4].drw_modelMatrixInverse : : buffer[1][576], 4 : -1 : 0 #var float4x4 drw_matrices[5].drw_modelMatrix : : buffer[1][640], 4 : -1 : 0 #var float4x4 drw_matrices[5].drw_modelMatrixInverse : : buffer[1][704], 4 : -1 : 0 #var float4x4 drw_matrices[6].drw_modelMatrix : : buffer[1][768], 4 : -1 : 0 #var float4x4 drw_matrices[6].drw_modelMatrixInverse : : buffer[1][832], 4 : -1 : 0 #var float4x4 drw_matrices[7].drw_modelMatrix : : buffer[1][896], 4 : -1 : 0 #var float4x4 drw_matrices[7].drw_modelMatrixInverse : : buffer[1][960], 4 : -1 : 0 #var float4x4 drw_matrices[8].drw_modelMatrix : : buffer[1][1024], 4 : -1 : 0 #var float4x4 drw_matrices[8].drw_modelMatrixInverse : : buffer[1][1088], 4 : -1 : 0 #var float4x4 drw_matrices[9].drw_modelMatrix : : buffer[1][1152], 4 : -1 : 0 #var float4x4 drw_matrices[9].drw_modelMatrixInverse : : buffer[1][1216], 4 : -1 : 0 #var float4x4 drw_matrices[10].drw_modelMatrix : : buffer[1][1280], 4 : -1 : 0 #var float4x4 drw_matrices[10].drw_modelMatrixInverse : : buffer[1][1344], 4 : -1 : 0 #var float4x4 drw_matrices[11].drw_modelMatrix : : buffer[1][1408], 4 : -1 : 0 #var float4x4 drw_matrices[11].drw_modelMatrixInverse : : buffer[1][1472], 4 : -1 : 0 #var float4x4 drw_matrices[12].drw_modelMatrix : : buffer[1][1536], 4 : -1 : 0 #var float4x4 drw_matrices[12].drw_modelMatrixInverse : : buffer[1][1600], 4 : -1 : 0 #var float4x4 drw_matrices[13].drw_modelMatrix : : buffer[1][1664], 4 : -1 : 0 #var float4x4 drw_matrices[13].drw_modelMatrixInverse : : buffer[1][1728], 4 : -1 : 0 #var float4x4 drw_matrices[14].drw_modelMatrix : : buffer[1][1792], 4 : -1 : 0 #var float4x4 drw_matrices[14].drw_modelMatrixInverse : : buffer[1][1856], 4 : -1 : 0 #var float4x4 drw_matrices[15].drw_modelMatrix : : buffer[1][1920], 4 : -1 : 0 #var float4x4 drw_matrices[15].drw_modelMatrixInverse : : buffer[1][1984], 4 : -1 : 0 #var float4x4 drw_matrices[16].drw_modelMat Error : EXCEPTION_ACCESS_VIOLATION Address : 0x00000001421E7014 Module : C:\Program Files\Blender Foundation\Blender 2.82\blender.exe