Skip to content

Commit 32e26ff

Browse files
committed
Merge branch 'dev/ralston/ttu_lint_fix' into 'main'
Ray tMax NaN Fix See merge request lightspeedrtx/dxvk-remix-nv!1174
2 parents dbe69dc + 2807e0f commit 32e26ff

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

src/dxvk/shaders/rtx/algorithm/rtxdi/RtxdiApplicationBridge.slangh

+1
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ DecodedPolymorphicLight RAB_GetDecodedPolymorphicLightWithTypeHint(uint lightID,
582582
return decodedPolymorphicLight;
583583
}
584584

585+
// Note: Assumes lightID is valid (e.g. not the invalid light ID reservoirs use to indicate they are invalid).
585586
RAB_LightSample RAB_GetLightSample(uint lightID, float2 lightUV, MinimalSurfaceInteraction minimalSurfaceInteraction, bool usePreviousLights = false)
586587
{
587588
const MemoryPolymorphicLight memoryPolymorphicLight = RAB_GetMemoryPolymorphicLight(lightID, usePreviousLights);

src/dxvk/shaders/rtx/concept/ray/ray.slangh

+7-3
Original file line numberDiff line numberDiff line change
@@ -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

src/dxvk/shaders/rtx/utility/math.slangh

+27-13
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,33 @@ GENERIC_SAFER_POSITIVE_DIVIDE(float, float)
127127
GENERIC_SAFER_POSITIVE_DIVIDE(f16vec3, float16_t)
128128
GENERIC_SAFER_POSITIVE_DIVIDE(vec3, float)
129129

130-
// Normalizes a vector "safely" by falling back to another vector in the case of
131-
// an inability to normalize (to avoid NaNs from normalization).
132-
#define GENERIC_SAFE_NORMALIZE(type, lengthType) \
133-
type safeNormalize(type vector, type fallbackVector) \
134-
{ \
135-
const lengthType vectorLength = length(vector); \
136-
\
137-
if (vectorLength == lengthType(0.0f)) \
138-
{ \
139-
return fallbackVector; \
140-
} \
141-
\
142-
return vector / vectorLength; \
130+
// Normalizes a vector "safely" by falling back to another (ideally normalized) vector in the case of
131+
// an inability to normalize (to avoid NaNs from normalization). Also calculates
132+
// and outputs the length of the input vector. If the vector is invalid (the zero vector)
133+
// then the length will be 0.
134+
#define GENERIC_SAFE_NORMALIZE_GET_LENGTH(type, lengthType) \
135+
type safeNormalizeGetLength(type vector, type fallbackVector, inout lengthType vectorLength) \
136+
{ \
137+
vectorLength = length(vector); \
138+
\
139+
if (vectorLength == lengthType(0.0f)) \
140+
{ \
141+
return fallbackVector; \
142+
} \
143+
\
144+
return vector / vectorLength; \
145+
}
146+
147+
GENERIC_SAFE_NORMALIZE_GET_LENGTH(f16vec3, float16_t)
148+
GENERIC_SAFE_NORMALIZE_GET_LENGTH(vec3, float)
149+
150+
// Same as the get length variant of the safe normalize function, just without a length output.
151+
#define GENERIC_SAFE_NORMALIZE(type, lengthType) \
152+
type safeNormalize(type vector, type fallbackVector) \
153+
{ \
154+
lengthType dummyLength; \
155+
\
156+
return safeNormalizeGetLength(vector, fallbackVector, dummyLength); \
143157
}
144158

145159
GENERIC_SAFE_NORMALIZE(f16vec3, float16_t)

submodules/rtxdi

Submodule rtxdi updated from 37ea43d to ed737de

0 commit comments

Comments
 (0)