@@ -159,7 +159,11 @@ Ray rayCreatePosition(
159159 vec3 targetPosition, bool penetrateSurface)
160160{
161161 const vec3 rayVector = targetPosition - minimalSurfaceInteraction.position;
162- const f16vec3 direction = f16vec3(normalize(rayVector));
162+ // Note: safeNormalizeGetLength not used here as while it could be used to calculate tMax, the length that should be returned
163+ // is a bit ambigious. If it's supposed to be the length of the input vector then this would fork fine as the length would be
164+ // 0 for zero vectors, but if it's set to 1 to match the fallback vector then the ray will have an incorrect tMax. As such we
165+ // just calculate the tMax manually for clarity and hope the compiler optimizes the redundant length calculations together.
166+ const f16vec3 direction = f16vec3(safeNormalize(rayVector, vec3(0.0f, 0.0f, 1.0f)));
163167 const float tMax = length(rayVector);
164168
165169 return rayCreateInternal(minimalRayInteraction, minimalSurfaceInteraction, viewRay, direction, tMax, penetrateSurface);
@@ -170,7 +174,7 @@ Ray rayCreatePosition(
170174 vec3 targetPosition)
171175{
172176 const vec3 rayVector = targetPosition - volumeInteraction.position;
173- const f16vec3 direction = f16vec3(normalize (rayVector));
177+ const f16vec3 direction = f16vec3(safeNormalize (rayVector, vec3(0.0f, 0.0f, 1.0f) ));
174178 const float tMax = length(rayVector);
175179
176180 return rayCreateInternal(minimalRayInteraction, volumeInteraction, viewRay, direction, tMax);
@@ -181,7 +185,7 @@ Ray rayCreatePositionSubsurface(
181185 vec3 targetPosition, vec3 shadingNormal)
182186{
183187 const vec3 rayVector = targetPosition - minimalSurfaceInteraction.position;
184- const f16vec3 direction = f16vec3(normalize (rayVector));
188+ const f16vec3 direction = f16vec3(safeNormalize (rayVector, vec3(0.0f, 0.0f, 1.0f) ));
185189 const float tMax = length(rayVector);
186190
187191 // If the new tracing ray is on the other side of SSS surface, treat the surface as penetrateSurface
0 commit comments