Skip to content

Commit

Permalink
Move half float sanitization out of StoreScanline and to Convert, add…
Browse files Browse the repository at this point in the history
… sanitization control flags.
  • Loading branch information
elasota committed May 20, 2018
1 parent 40b3b10 commit 549e4ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions DirectXTex/DirectXTex.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,12 @@ namespace DirectX
TEX_FILTER_FLOAT_X2BIAS = 0x200,
// Enable *2 - 1 conversion cases for unorm<->float and positive-only float formats

TEX_FILTER_FLOAT16_SATURATE_TO_INF = 0x400,
// When converting to half float, saturate out-of-range values to +INF/-INF instead of largest in-range value

TEX_FILTER_FLOAT16_KEEP_NANS = 0x800,
// When converting to half float, preserve NaNs instead of converting to zero

TEX_FILTER_RGB_COPY_RED = 0x1000,
TEX_FILTER_RGB_COPY_GREEN = 0x2000,
TEX_FILTER_RGB_COPY_BLUE = 0x4000,
Expand Down
26 changes: 20 additions & 6 deletions DirectXTex/DirectXTexConvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1648,9 +1648,7 @@ bool DirectX::_StoreScanline(
for (size_t icount = 0; icount < (size - sizeof(XMHALF4) + 1); icount += sizeof(XMHALF4))
{
if (sPtr >= ePtr) break;
XMVECTOR v = *sPtr++;
v = XMVectorClamp(v, g_HalfMin, g_HalfMax);
XMStoreHalf4(dPtr++, v);
XMStoreHalf4(dPtr++, *sPtr++);
}
return true;
}
Expand Down Expand Up @@ -1841,9 +1839,7 @@ bool DirectX::_StoreScanline(
for (size_t icount = 0; icount < (size - sizeof(HALF) + 1); icount += sizeof(HALF))
{
if (sPtr >= ePtr) break;
float v = XMVectorGetX(*sPtr++);
v = std::max<float>(std::min<float>(v, 65504.f), -65504.f);
*(dPtr++) = XMConvertFloatToHalf(v);
*(dPtr++) = XMConvertFloatToHalf(XMVectorGetX(*sPtr++));
}
return true;
}
Expand Down Expand Up @@ -3598,6 +3594,24 @@ void DirectX::_ConvertScanline(
}
}
}

// Half-float sanitization
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)))
{
XMVECTOR* ptr = pBuffer;
for (size_t i = 0; i < count; ++i, ++ptr)
{
XMVECTOR v = *ptr;

if (!(flags & TEX_FILTER_FLOAT16_SATURATE_TO_INF))
v = XMVectorClamp(v, g_HalfMin, g_HalfMax);

if (!(flags & TEX_FILTER_FLOAT16_KEEP_NANS))
v = XMVectorSelect(v, XMVectorZero(), XMVectorIsNaN(v));

*ptr = v;
}
}
}


Expand Down

0 comments on commit 549e4ba

Please sign in to comment.