diff options
author | Bastiaan Olij <mux213@gmail.com> | 2022-06-17 13:55:23 +1000 |
---|---|---|
committer | Bastiaan Olij <mux213@gmail.com> | 2022-06-24 18:04:28 +1000 |
commit | 6224b003655df4dbae2b5dc1f45473edfdbda10f (patch) | |
tree | 85cb7c370f5040ec9f4c2fc1c5bbd1ff831b7be4 /servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl | |
parent | 051fb86fb0061acc2441eeb60a13cde671b8c092 (diff) |
Moved cube_to_dp and cubemap logic into CopyEffects
Diffstat (limited to 'servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl')
-rw-r--r-- | servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl b/servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl new file mode 100644 index 0000000000..1d46f59408 --- /dev/null +++ b/servers/rendering/renderer_rd/shaders/effects/cubemap_roughness.glsl @@ -0,0 +1,63 @@ +#[compute] + +#version 450 + +#VERSION_DEFINES + +#define GROUP_SIZE 8 + +layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = 1) in; + +layout(set = 0, binding = 0) uniform samplerCube source_cube; + +layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly imageCube dest_cubemap; + +#include "cubemap_roughness_inc.glsl" + +void main() { + uvec3 id = gl_GlobalInvocationID; + id.z += params.face_id; + + vec2 uv = ((vec2(id.xy) * 2.0 + 1.0) / (params.face_size) - 1.0); + vec3 N = texelCoordToVec(uv, id.z); + + if (params.use_direct_write) { + imageStore(dest_cubemap, ivec3(id), vec4(texture(source_cube, N).rgb, 1.0)); + } else { + vec4 sum = vec4(0.0, 0.0, 0.0, 0.0); + + float solid_angle_texel = 4.0 * M_PI / (6.0 * params.face_size * params.face_size); + float roughness2 = params.roughness * params.roughness; + float roughness4 = roughness2 * roughness2; + vec3 UpVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0); + mat3 T; + T[0] = normalize(cross(UpVector, N)); + T[1] = cross(N, T[0]); + T[2] = N; + + for (uint sampleNum = 0u; sampleNum < params.sample_count; sampleNum++) { + vec2 xi = Hammersley(sampleNum, params.sample_count); + + vec3 H = T * ImportanceSampleGGX(xi, roughness4); + float NdotH = dot(N, H); + vec3 L = (2.0 * NdotH * H - N); + + float ndotl = clamp(dot(N, L), 0.0, 1.0); + + if (ndotl > 0.0) { + float D = DistributionGGX(NdotH, roughness4); + float pdf = D * NdotH / (4.0 * NdotH) + 0.0001; + + float solid_angle_sample = 1.0 / (float(params.sample_count) * pdf + 0.0001); + + float mipLevel = params.roughness == 0.0 ? 0.0 : 0.5 * log2(solid_angle_sample / solid_angle_texel); + + sum.rgb += textureLod(source_cube, L, mipLevel).rgb * ndotl; + sum.a += ndotl; + } + } + sum /= sum.a; + + imageStore(dest_cubemap, ivec3(id), vec4(sum.rgb, 1.0)); + } +} |