From 610bbbd84eaef7348d1801feb492e1e7b4f013e7 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Wed, 24 Feb 2021 00:20:48 -0800 Subject: [PATCH] texconv -reconstructz updated to handle UNORM properly --- Texconv/texconv.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Texconv/texconv.cpp b/Texconv/texconv.cpp index 2097674..a82c633 100644 --- a/Texconv/texconv.cpp +++ b/Texconv/texconv.cpp @@ -2792,6 +2792,8 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[]) return 1; } + bool isunorm = (FormatDataType(info.format) == FORMAT_TYPE_UNORM) != 0; + hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(), [&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y) { @@ -2803,7 +2805,17 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[]) { XMVECTOR value = inPixels[j]; - XMVECTOR z = XMVectorSqrt(XMVectorSubtract(g_XMOne, XMVector2Dot(value, value))); + XMVECTOR z; + if (isunorm) + { + XMVECTOR x2 = XMVectorMultiplyAdd(value, g_XMTwo, g_XMNegativeOne); + x2 = XMVectorSqrt(XMVectorSubtract(g_XMOne, XMVector2Dot(x2, x2))); + z = XMVectorMultiplyAdd(x2, g_XMOneHalf, g_XMOneHalf); + } + else + { + z = XMVectorSqrt(XMVectorSubtract(g_XMOne, XMVector2Dot(value, value))); + } outPixels[j] = XMVectorSelect(value, z, s_selectz); }