// MinecraftPT — SH_Tracing_CS // Low-order spherical-harmonics sky tracing: evaluates sky radiance on a small // SH basis and stores it for ambient light estimation (used by IRC seeding). #include "/Lib/Settings.glsl" #include "/Lib/Utilities.glsl" #include "/Lib/BasicFunctions/LightingConstants.glsl" #include "/Lib/BasicFunctions/PrecomputedAtmosphere.glsl" #include "/Lib/PathTracing/Tracer/ShadowTracing.glsl" #include "/Lib/PathTracing/Voxelizer/VoxelProfile.glsl" #ifdef PT_IRC const ivec3 workGroups = ivec3(int(ceil(float(ircResolution) / 4.0))); layout (local_size_x = 4, local_size_y = 4, local_size_z = 4) in; layout (rgba16f) uniform writeonly image3D img_irradianceCache3D; layout (rgba16f) uniform readonly image3D img_irradianceCache3D_Alt; uniform sampler3D voxelData3D; // SH basis Y1 (linear, 3 components) coefficients for sky radiance at a cell void main(){ ivec3 texel = ivec3(gl_GlobalInvocationID.xyz); if (any(greaterThanEqual(texel, ivec3(ircResolution)))){ return; } vec3 cellPos = (vec3(texel) + 0.5) / float(ircResolution); vec3 worldPos = (cellPos - 0.5) * voxelDistance + cameraPosition; vec3 voxelCoord = cellPos * voxelResolution; vec4 voxelData = texelFetch(voxelData3D, ivec3(clamp(voxelCoord, vec3(0.0), vec3(voxelResolution - 1.0))), 0); float voxelID = DecodeVoxelID(voxelData.z); if (voxelID > 1.0 && voxelID < 999.0){ return; } // Trace N directions, accumulate SH coefficients vec3 sh = vec3(0.0); for (int i = 0; i < 6; i++){ float phi = 6.28318 * hash1(vec3(texel) * 1.3 + float(i) * 0.7); float cosTheta = hash1(vec3(texel) * 0.7 + float(i) * 1.9); float sinTheta = sqrt(1.0 - cosTheta * cosTheta); vec3 dir = vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta); float visibility = SimpleShadowTracing(WorldToVoxel(worldPos) + dir * 0.5, dir); vec3 radiance = GetSkyRadiance(dir, GetSunDirWorld()); sh += radiance * visibility * cosTheta; // cos-weighted } sh /= 6.0; imageStore(img_irradianceCache3D, texel, vec4(sh, 1.0)); } #endif