Skip to content

Commit

Permalink
improve ray trace sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhiQing-R committed Dec 6, 2024
1 parent cb0ed4f commit a5c408a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
7 changes: 3 additions & 4 deletions shaders/lightPass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ vec3 hsv2rgb(vec3 c) {
void main()
{
uint primObjID = texelFetch(gbufferPrim, ivec2(gl_FragCoord.xy), 0).r;
vec2 uv = (gl_FragCoord.xy + vec2(0.5)) / vec2(textureSize(indirectLightMap,0));
uv *= 0.5f;
vec2 uv = (gl_FragCoord.xy + vec2(0.5)) / vec2(textureSize(gbufferPrim,0));

uint nodeID = primObjID >> 23;
uint instanceID = sceneNodes[nodeID].primMesh;
Expand Down Expand Up @@ -148,10 +147,10 @@ void main()
bool hit = AnyHit(ray, 100.0);

//vec3 indirectLight = texelFetch(indirectLightMap, ivec2(gl_FragCoord.xy) / 2, 0).rgb;
vec3 indirectLight = texture(indirectLightMap, uv).rgb;
vec3 indirectLight = texture(indirectLightMap, uv * 0.5).rgb;
//vec3 diffuseAlbedo = state.mat.albedo * (M_1_OVER_PI * (1.0 - state.mat.metallic));
vec3 diffuseAlbedo = state.mat.albedo * (1.0 - F_SchlickRoughness(state.mat.f0, max(0.0, dot(-camRay.direction, state.normal)), state.mat.roughness)
* (1.0 - state.mat.metallic)) * 0.5;
* (1.0 - state.mat.metallic));
vec3 directLighting = hit ? vec3(0) : directLight.radiance;
vec3 reflectionColor = texelFetch(filteredReflectionColor, ivec2(gl_FragCoord.xy), 0).rgb;

Expand Down
5 changes: 5 additions & 0 deletions shaders/reflection_generation.comp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ void main()
vec3 radiance = surfelRefelctionTrace(reflectedRay, 1, firstDepth, tmp, weight);
float invPdf = 1.f / reflectBsdfSampleRec.pdf;
radiance *= invPdf * reflectBsdfSampleRec.f * max(0.0, dot(reflectBsdfSampleRec.L, state.ffnormal));
float lum = dot(radiance, vec3(0.212671f, 0.715160f, 0.072169f));
if(lum > rtxState.fireflyClampThreshold)
{
radiance *= rtxState.fireflyClampThreshold / lum;
}

imageStore(reflectionColor, imageCoords, vec4(radiance, weight));
imageStore(reflectionDirection, imageCoords, vec4(vec3(state.matID + 1), invPdf));
Expand Down
11 changes: 5 additions & 6 deletions shaders/surfel_integrate.comp
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ void main()
CellInfo cellInfo = cellBuffer[flattenIndex];
uint cellOffset = cellInfo.surfelOffset;
uint cellSurfelCount = cellInfo.surfelCount;
uint targetShareCount = min(48u, cellSurfelCount);
uint targetShareCount = min(64u, cellSurfelCount);
float cellSurfelCntF = float(cellSurfelCount);
uint passCount = 0;

for (uint i = 0; i < cellSurfelCount; i++)
for (uint i = 0; i < targetShareCount; i++)
{
if (passCount == targetShareCount)
break;
uint surfelIndex = cellToSurfel[cellOffset + i];
uint currIndex = uint(rand(randSeed) * cellSurfelCntF);
uint surfelIndex = cellToSurfel[cellOffset + currIndex];
Surfel neiSurfel = surfelBuffer[surfelIndex];
vec3 neiNor = decompress_unit_vec(neiSurfel.normal);
vec3 distV = neiSurfel.position - surfel.position;
Expand Down Expand Up @@ -238,7 +238,6 @@ void main()
contribution *= smoothstep(0.f, 10.f, float(surfelRecycleInfo[surfelIndex].frame));
sharedRadiance += vec4(neiSurfel.radiance, 1.f) * contribution;

passCount++;

}
if(rtxState.debugging_mode == esNonUniformGrid) break;
Expand Down
8 changes: 6 additions & 2 deletions shaders/temporal_spatial_pass.comp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ layout(push_constant) uniform _RtxState
};

#include "globals.glsl"
#include "random.glsl"

layout(set = 0, binding = 0) uniform image2D reflectionColor;
layout(set = 0, binding = 1) uniform image2D reflectionDirection;
Expand Down Expand Up @@ -126,6 +127,8 @@ void main()

// STAGE 1; Spatial reconstruction filtering
ivec2 halfImageCoords = imageCoords / 2;
uint randUint = tea(halfImageCoords.y * imageRes.x + halfImageCoords.x, rtxState.frame);
int dir = (randUint & 1u) > 0 ? 1 : -1;

vec3 result = vec3(0.0);
float weightSum = 0.0;
Expand Down Expand Up @@ -153,11 +156,12 @@ void main()
weightSum += centerWeight;

uint quadID = (imageCoords.x & 0x1) << 1 + (imageCoords.y & 0x1);
quadID = (quadID + randUint) & 0x3;
float variance = 0.0;

for (int i = 0; i < 16; ++i)
{
ivec2 offset = samplePattern[quadID][i];
ivec2 offset = dir * samplePattern[quadID][i];
ivec2 neighborCoords = halfImageCoords + offset;

vec4 neighborColor = imageLoad(reflectionColor, neighborCoords);
Expand Down Expand Up @@ -196,6 +200,6 @@ void main()
vec3 colPrev = imageLoad(filteredReflectionColor, imageCoords).rgb;
result = mix(colPrev, result, ratio);

imageStore(filteredReflectionColor, imageCoords, vec4(result.rgb, sqrt(variance)));
imageStore(filteredReflectionColor, imageCoords, vec4(result.rgb, weightSum));
//imageStore(filteredReflectionColor, imageCoords, centerColor);
}
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ int main(int argc, char** argv)
#endif

InputParser parser(argc, argv);
//std::string sceneFile = parser.getString("-f", "Sponza/Sponza.gltf");
std::string sceneFile = parser.getString("-f", "Sponza/Sponza.gltf");
//std::string sceneFile = parser.getString("-f", "Street/scene.gltf");
std::string sceneFile = parser.getString("-f", "apocal/apocal.gltf");
//std::string sceneFile = parser.getString("-f", "apocal/apocal.gltf");
//std::string sceneFile = parser.getString("-f", "station/station.gltf");
std::string hdrFilename = parser.getString("-e", "std_env.hdr");

Expand Down

0 comments on commit a5c408a

Please sign in to comment.