From c1d619caac1bca8250efbf06d606d3811ff282eb Mon Sep 17 00:00:00 2001 From: mlefrancois Date: Wed, 16 Oct 2024 09:03:34 +0200 Subject: [PATCH] Background alpha==0 Script sync from SHA: 504318b0243bd66f1feeb5854048c434cd3c7935 --- shaders/pathtrace.comp.glsl | 8 ++++---- shaders/pathtrace.rgen.glsl | 8 ++++---- shaders/rt_common.h | 31 ++++++++++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/shaders/pathtrace.comp.glsl b/shaders/pathtrace.comp.glsl index 2f751ff..a24cc1f 100644 --- a/shaders/pathtrace.comp.glsl +++ b/shaders/pathtrace.comp.glsl @@ -88,7 +88,7 @@ void main() SampleResult sampleResult = samplePixel(seed, samplePos, subpixelJitter, imageSize, frameInfo.projMatrixI, frameInfo.viewMatrixI, focalDistance, aperture); - vec3 pixel_color = sampleResult.radiance; + vec4 pixel_color = sampleResult.radiance; for(int s = 1; s < pc.maxSamples; s++) { subpixelJitter = vec2(rand(seed), rand(seed)); @@ -100,15 +100,15 @@ void main() if(pc.frame == 0) // first frame { - imageStore(image, ivec2(samplePos.xy), vec4(pixel_color, 1.0F)); + imageStore(image, ivec2(samplePos.xy), pixel_color); imageStore(normalDepth, ivec2(samplePos.xy), vec4(sampleResult.normal, sampleResult.depth)); } else { // Do accumulation over time float a = 1.0F / float(pc.frame + 1); - vec3 old_color = imageLoad(image, ivec2(samplePos.xy)).xyz; - imageStore(image, ivec2(samplePos.xy), vec4(mix(old_color, pixel_color, a), 1.0F)); + vec4 old_color = imageLoad(image, ivec2(samplePos.xy)); + imageStore(image, ivec2(samplePos.xy), mix(old_color, pixel_color, a)); // Normal Depth buffer update vec4 oldNormalDepth = imageLoad(normalDepth, ivec2(samplePos.xy)); diff --git a/shaders/pathtrace.rgen.glsl b/shaders/pathtrace.rgen.glsl index 915b13e..9b93648 100644 --- a/shaders/pathtrace.rgen.glsl +++ b/shaders/pathtrace.rgen.glsl @@ -79,7 +79,7 @@ void main() SampleResult sampleResult = samplePixel(seed, samplePos, subpixelJitter, imageSize, frameInfo.projMatrixI, frameInfo.viewMatrixI, focalDistance, aperture); - vec3 pixel_color = sampleResult.radiance; + vec4 pixel_color = sampleResult.radiance; for(int s = 1; s < pc.maxSamples; s++) { subpixelJitter = vec2(rand(seed), rand(seed)); @@ -91,15 +91,15 @@ void main() if(pc.frame == 0) // first frame { - imageStore(image, ivec2(samplePos.xy), vec4(pixel_color, 1.0F)); + imageStore(image, ivec2(samplePos.xy), pixel_color); imageStore(normalDepth, ivec2(samplePos.xy), vec4(sampleResult.normal, sampleResult.depth)); } else { // Do accumulation over time float a = 1.0F / float(pc.frame + 1); - vec3 old_color = imageLoad(image, ivec2(samplePos.xy)).xyz; - imageStore(image, ivec2(samplePos.xy), vec4(mix(old_color, pixel_color, a), 1.0F)); + vec4 old_color = imageLoad(image, ivec2(samplePos.xy)); + imageStore(image, ivec2(samplePos.xy), mix(old_color, pixel_color, a)); // Normal Depth buffer update vec4 oldNormalDepth = imageLoad(normalDepth, ivec2(samplePos.xy)); diff --git a/shaders/rt_common.h b/shaders/rt_common.h index a884940..016c89f 100644 --- a/shaders/rt_common.h +++ b/shaders/rt_common.h @@ -7,7 +7,7 @@ bool traceShadow(Ray r, float maxDist, inout uint seed); struct SampleResult { - vec3 radiance; + vec4 radiance; vec3 normal; float depth; }; @@ -46,11 +46,11 @@ vec3 sampleLights(in vec3 pos, vec3 normal, in vec3 worldRayDirection, inout uin { int lightIndex = min(int(rand(seed) * sceneDesc.numLights), sceneDesc.numLights - 1); Light light = RenderLightBuf(sceneDesc.lightAddress)._[lightIndex]; - LightContrib contrib = singleLightContribution(light, pos, normal, vec2(rand(seed), rand(seed))); - lightDir = -contrib.incidentVector; - lightPdf = (1.0 / sceneDesc.numLights) * lightWeight; - lightRadiance = contrib.intensity / lightPdf; - lightDist = contrib.distance; + LightContrib contrib = singleLightContribution(light, pos, normal, vec2(rand(seed), rand(seed))); + lightDir = -contrib.incidentVector; + lightPdf = (1.0 / sceneDesc.numLights) * lightWeight; + lightRadiance = contrib.intensity / lightPdf; + lightDist = contrib.distance; } // Sample environment @@ -168,7 +168,7 @@ SampleResult pathTrace(Ray r, inout uint seed) SampleResult sampleResult; sampleResult.depth = 0; sampleResult.normal = vec3(0, 0, 0); - sampleResult.radiance = vec3(0, 0, 0); + sampleResult.radiance = vec4(0, 0, 0, 1); GltfMaterialBuf materials = GltfMaterialBuf(sceneDesc.materialAddress); @@ -183,9 +183,13 @@ SampleResult pathTrace(Ray r, inout uint seed) // Hitting the environment, then exit if(hitPayload.hitT == INFINITE) { + // For first hit, it is transparent + if(depth == 0) + sampleResult.radiance.a = 0.0; + if(frameInfo.useSky == 1) { - radiance += throughput * evalPhysicalSky(skyInfo, r.direction); + radiance.xyz += throughput * evalPhysicalSky(skyInfo, r.direction); } else { @@ -197,10 +201,10 @@ SampleResult pathTrace(Ray r, inout uint seed) // We may hit the environment twice: once via sampleLights() and once when hitting the sky while probing // for more indirect hits. This is the counter part of the MIS weighting in sampleLights() float misWeight = (lastSamplePdf == DIRAC) ? 1.0 : (lastSamplePdf / (lastSamplePdf + env.w)); - radiance += throughput * misWeight * env.rgb * frameInfo.envIntensity.xyz; + radiance.xyz += throughput * misWeight * env.rgb * frameInfo.envIntensity.xyz; } - sampleResult.radiance = radiance; + sampleResult.radiance.xyz = radiance; return sampleResult; } @@ -227,7 +231,8 @@ SampleResult pathTrace(Ray r, inout uint seed) if(material.unlit > 0) { radiance += pbrMat.baseColor; - sampleResult.radiance = radiance; + sampleResult.radiance.xyz = radiance; + sampleResult.radiance.a = 1.0; return sampleResult; } @@ -331,7 +336,7 @@ SampleResult pathTrace(Ray r, inout uint seed) #endif } - sampleResult.radiance = radiance; + sampleResult.radiance.xyz = radiance; return sampleResult; } @@ -358,7 +363,7 @@ SampleResult samplePixel(inout uint seed, vec2 samplePos, vec2 subpixelJitter, v // Removing fireflies #if USE_FIREFLY_FILTER - float lum = dot(sampleResult.radiance, vec3(1.0F / 3.0F)); + float lum = dot(sampleResult.radiance.xyz, vec3(1.0F / 3.0F)); if(lum > pc.maxLuminance) { sampleResult.radiance *= pc.maxLuminance / lum;