godot/servers/rendering/renderer_rd/shaders/cubemap_roughness.glsl

64 lines
1.8 KiB
Plaintext
Raw Normal View History

#[compute]
2019-08-26 20:43:58 +00:00
#version 450
#VERSION_DEFINES
2019-08-26 20:43:58 +00:00
2020-03-01 01:16:50 +00:00
#define GROUP_SIZE 8
2019-08-26 20:43:58 +00:00
2020-03-01 01:16:50 +00:00
layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = 1) in;
2019-08-26 20:43:58 +00:00
layout(set = 0, binding = 0) uniform samplerCube source_cube;
2019-08-26 20:43:58 +00:00
2020-03-01 01:16:50 +00:00
layout(rgba16f, set = 1, binding = 0) uniform restrict writeonly imageCube dest_cubemap;
#include "cubemap_roughness_inc.glsl"
2019-08-26 20:43:58 +00:00
void main() {
2020-03-01 01:16:50 +00:00
uvec3 id = gl_GlobalInvocationID;
id.z += params.face_id;
2019-08-26 20:43:58 +00:00
2020-03-01 01:16:50 +00:00
vec2 uv = ((vec2(id.xy) * 2.0 + 1.0) / (params.face_size) - 1.0);
vec3 N = texelCoordToVec(uv, id.z);
2019-08-26 20:43:58 +00:00
if (params.use_direct_write) {
2020-03-01 01:16:50 +00:00
imageStore(dest_cubemap, ivec3(id), vec4(texture(source_cube, N).rgb, 1.0));
2019-08-26 20:43:58 +00:00
} else {
vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
2022-02-16 08:54:08 +00:00
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;
2019-08-26 20:43:58 +00:00
for (uint sampleNum = 0u; sampleNum < params.sample_count; sampleNum++) {
vec2 xi = Hammersley(sampleNum, params.sample_count);
2022-02-16 08:54:08 +00:00
vec3 H = T * ImportanceSampleGGX(xi, roughness4);
float NdotH = dot(N, H);
vec3 L = (2.0 * NdotH * H - N);
2019-08-26 20:43:58 +00:00
float ndotl = clamp(dot(N, L), 0.0, 1.0);
if (ndotl > 0.0) {
2022-02-16 08:54:08 +00:00
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;
2019-08-26 20:43:58 +00:00
sum.a += ndotl;
}
}
sum /= sum.a;
2020-03-01 01:16:50 +00:00
imageStore(dest_cubemap, ivec3(id), vec4(sum.rgb, 1.0));
2019-08-26 20:43:58 +00:00
}
}