Skip to content

Commit 9b5e5d6

Browse files
authored
Added bit bias when storing R8_UNORM to be consistent with other 8bpp formats (#671)
1 parent 016c186 commit 9b5e5d6

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

DirectXTex/DirectXTexConvert.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ namespace
194194
const XMVECTORF32 g_Grayscale = { { { 0.2125f, 0.7154f, 0.0721f, 0.0f } } };
195195
const XMVECTORF32 g_HalfMin = { { { -65504.f, -65504.f, -65504.f, -65504.f } } };
196196
const XMVECTORF32 g_HalfMax = { { { 65504.f, 65504.f, 65504.f, 65504.f } } };
197-
const XMVECTORF32 g_8BitBias = { { { 0.5f / 255.f, 0.5f / 255.f, 0.5f / 255.f, 0.5f / 255.f } } };
197+
198+
constexpr float g_8BitBias = 0.5f / 255.f;
199+
const XMVECTORF32 g_8BitBiasV = { { { g_8BitBias, g_8BitBias, g_8BitBias, g_8BitBias } } };
198200
}
199201

200202
//-------------------------------------------------------------------------------------
@@ -1762,7 +1764,7 @@ bool DirectX::Internal::StoreScanline(
17621764
for (size_t icount = 0; icount < (size - sizeof(XMUBYTEN4) + 1); icount += sizeof(XMUBYTEN4))
17631765
{
17641766
if (sPtr >= ePtr) break;
1765-
const XMVECTOR v = XMVectorAdd(*sPtr++, g_8BitBias);
1767+
const XMVECTOR v = XMVectorAdd(*sPtr++, g_8BitBiasV);
17661768
XMStoreUByteN4(dPtr++, v);
17671769
}
17681770
return true;
@@ -1960,7 +1962,7 @@ bool DirectX::Internal::StoreScanline(
19601962
for (size_t icount = 0; icount < size; icount += sizeof(uint8_t))
19611963
{
19621964
if (sPtr >= ePtr) break;
1963-
float v = XMVectorGetX(*sPtr++);
1965+
float v = XMVectorGetX(*sPtr++) + g_8BitBias;
19641966
v = std::max<float>(std::min<float>(v, 1.f), 0.f);
19651967
*(dPtr++) = static_cast<uint8_t>(v * 255.f);
19661968
}
@@ -2020,7 +2022,7 @@ bool DirectX::Internal::StoreScanline(
20202022
for (size_t icount = 0; icount < size; icount += sizeof(uint8_t))
20212023
{
20222024
if (sPtr >= ePtr) break;
2023-
float v = XMVectorGetW(*sPtr++);
2025+
float v = XMVectorGetW(*sPtr++) + g_8BitBias;
20242026
v = std::max<float>(std::min<float>(v, 1.f), 0.f);
20252027
*(dPtr++) = static_cast<uint8_t>(v * 255.f);
20262028
}
@@ -2065,7 +2067,7 @@ bool DirectX::Internal::StoreScanline(
20652067
const XMVECTOR v0 = *sPtr++;
20662068
const XMVECTOR v1 = (sPtr < ePtr) ? XMVectorSplatY(*sPtr++) : XMVectorZero();
20672069
XMVECTOR v = XMVectorSelect(v1, v0, g_XMSelect1110);
2068-
v = XMVectorAdd(v, g_8BitBias);
2070+
v = XMVectorAdd(v, g_8BitBiasV);
20692071
XMStoreUByteN4(dPtr++, v);
20702072
}
20712073
return true;
@@ -2084,7 +2086,7 @@ bool DirectX::Internal::StoreScanline(
20842086
const XMVECTOR v0 = XMVectorSwizzle<1, 0, 3, 2>(*sPtr++);
20852087
const XMVECTOR v1 = (sPtr < ePtr) ? XMVectorSplatY(*sPtr++) : XMVectorZero();
20862088
XMVECTOR v = XMVectorSelect(v1, v0, select1101);
2087-
v = XMVectorAdd(v, g_8BitBias);
2089+
v = XMVectorAdd(v, g_8BitBiasV);
20882090
XMStoreUByteN4(dPtr++, v);
20892091
}
20902092
return true;
@@ -2145,7 +2147,7 @@ bool DirectX::Internal::StoreScanline(
21452147
{
21462148
if (sPtr >= ePtr) break;
21472149
XMVECTOR v = XMVectorSwizzle<2, 1, 0, 3>(*sPtr++);
2148-
v = XMVectorAdd(v, g_8BitBias);
2150+
v = XMVectorAdd(v, g_8BitBiasV);
21492151
XMStoreUByteN4(dPtr++, v);
21502152
}
21512153
return true;
@@ -2161,7 +2163,7 @@ bool DirectX::Internal::StoreScanline(
21612163
{
21622164
if (sPtr >= ePtr) break;
21632165
XMVECTOR v = XMVectorPermute<2, 1, 0, 7>(*sPtr++, g_XMIdentityR3);
2164-
v = XMVectorAdd(v, g_8BitBias);
2166+
v = XMVectorAdd(v, g_8BitBiasV);
21652167
XMStoreUByteN4(dPtr++, v);
21662168
}
21672169
return true;

0 commit comments

Comments
 (0)