Skip to content

Commit

Permalink
Background alpha==0
Browse files Browse the repository at this point in the history
Script sync from SHA: 504318b0243bd66f1feeb5854048c434cd3c7935
  • Loading branch information
mklefrancois committed Oct 16, 2024
1 parent 621f123 commit c1d619c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
8 changes: 4 additions & 4 deletions shaders/pathtrace.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions shaders/pathtrace.rgen.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand Down
31 changes: 18 additions & 13 deletions shaders/rt_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ bool traceShadow(Ray r, float maxDist, inout uint seed);

struct SampleResult
{
vec3 radiance;
vec4 radiance;
vec3 normal;
float depth;
};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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
{
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -331,7 +336,7 @@ SampleResult pathTrace(Ray r, inout uint seed)
#endif
}

sampleResult.radiance = radiance;
sampleResult.radiance.xyz = radiance;
return sampleResult;
}

Expand All @@ -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;
Expand Down

0 comments on commit c1d619c

Please sign in to comment.