Skip to content

Commit 549e4ba

Browse files
committed
Move half float sanitization out of StoreScanline and to Convert, add sanitization control flags.
1 parent 40b3b10 commit 549e4ba

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

DirectXTex/DirectXTex.h

+6
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ namespace DirectX
450450
TEX_FILTER_FLOAT_X2BIAS = 0x200,
451451
// Enable *2 - 1 conversion cases for unorm<->float and positive-only float formats
452452

453+
TEX_FILTER_FLOAT16_SATURATE_TO_INF = 0x400,
454+
// When converting to half float, saturate out-of-range values to +INF/-INF instead of largest in-range value
455+
456+
TEX_FILTER_FLOAT16_KEEP_NANS = 0x800,
457+
// When converting to half float, preserve NaNs instead of converting to zero
458+
453459
TEX_FILTER_RGB_COPY_RED = 0x1000,
454460
TEX_FILTER_RGB_COPY_GREEN = 0x2000,
455461
TEX_FILTER_RGB_COPY_BLUE = 0x4000,

DirectXTex/DirectXTexConvert.cpp

+20-6
Original file line numberDiff line numberDiff line change
@@ -1648,9 +1648,7 @@ bool DirectX::_StoreScanline(
16481648
for (size_t icount = 0; icount < (size - sizeof(XMHALF4) + 1); icount += sizeof(XMHALF4))
16491649
{
16501650
if (sPtr >= ePtr) break;
1651-
XMVECTOR v = *sPtr++;
1652-
v = XMVectorClamp(v, g_HalfMin, g_HalfMax);
1653-
XMStoreHalf4(dPtr++, v);
1651+
XMStoreHalf4(dPtr++, *sPtr++);
16541652
}
16551653
return true;
16561654
}
@@ -1841,9 +1839,7 @@ bool DirectX::_StoreScanline(
18411839
for (size_t icount = 0; icount < (size - sizeof(HALF) + 1); icount += sizeof(HALF))
18421840
{
18431841
if (sPtr >= ePtr) break;
1844-
float v = XMVectorGetX(*sPtr++);
1845-
v = std::max<float>(std::min<float>(v, 65504.f), -65504.f);
1846-
*(dPtr++) = XMConvertFloatToHalf(v);
1842+
*(dPtr++) = XMConvertFloatToHalf(XMVectorGetX(*sPtr++));
18471843
}
18481844
return true;
18491845
}
@@ -3598,6 +3594,24 @@ void DirectX::_ConvertScanline(
35983594
}
35993595
}
36003596
}
3597+
3598+
// Half-float sanitization
3599+
if (!(out->flags & CONVF_DEPTH) && (out->flags & CONVF_FLOAT) && out->datasize == 16 && (!(flags & TEX_FILTER_FLOAT16_SATURATE_TO_INF) || !(flags & TEX_FILTER_FLOAT16_KEEP_NANS)))
3600+
{
3601+
XMVECTOR* ptr = pBuffer;
3602+
for (size_t i = 0; i < count; ++i, ++ptr)
3603+
{
3604+
XMVECTOR v = *ptr;
3605+
3606+
if (!(flags & TEX_FILTER_FLOAT16_SATURATE_TO_INF))
3607+
v = XMVectorClamp(v, g_HalfMin, g_HalfMax);
3608+
3609+
if (!(flags & TEX_FILTER_FLOAT16_KEEP_NANS))
3610+
v = XMVectorSelect(v, XMVectorZero(), XMVectorIsNaN(v));
3611+
3612+
*ptr = v;
3613+
}
3614+
}
36013615
}
36023616

36033617

0 commit comments

Comments
 (0)