crossxtex/DirectXTex/DirectXTexNormalMaps.cpp

393 lines
12 KiB
C++
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// DirectXTexNormalMaps.cpp
//
// DirectX Texture Library - Normal map operations
//
// Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-24 06:24:46 +00:00
// Licensed under the MIT License.
2016-08-22 18:26:36 +00:00
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
#include "DirectXTexP.h"
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
using namespace DirectX;
namespace
2016-08-22 18:26:36 +00:00
{
#pragma prefast(suppress : 25000, "FXMVECTOR is 16 bytes")
2016-09-09 02:09:46 +00:00
inline float EvaluateColor(_In_ FXMVECTOR val, _In_ DWORD flags)
{
XMFLOAT4A f;
2016-08-22 18:26:36 +00:00
2017-07-12 07:56:51 +00:00
static XMVECTORF32 lScale = { { { 0.2125f, 0.7154f, 0.0721f, 1.f } } };
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
static_assert(CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask");
switch (flags & 0xf)
{
case 0:
case CNMAP_CHANNEL_RED: return XMVectorGetX(val);
case CNMAP_CHANNEL_GREEN: return XMVectorGetY(val);
case CNMAP_CHANNEL_BLUE: return XMVectorGetZ(val);
case CNMAP_CHANNEL_ALPHA: return XMVectorGetW(val);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case CNMAP_CHANNEL_LUMINANCE:
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
XMVECTOR v = XMVectorMultiply(val, lScale);
XMStoreFloat4A(&f, v);
2016-08-22 18:26:36 +00:00
return f.x + f.y + f.z;
}
2016-09-09 02:09:46 +00:00
default:
assert(false);
return 0.f;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
void EvaluateRow(
_In_reads_(width) const XMVECTOR* pSource,
_Out_writes_(width + 2) float* pDest,
size_t width,
DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pSource && pDest);
assert(width > 0);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t x = 0; x < width; ++x)
{
pDest[x + 1] = EvaluateColor(pSource[x], flags);
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & CNMAP_MIRROR_U)
{
// Mirror in U
pDest[0] = EvaluateColor(pSource[0], flags);
pDest[width + 1] = EvaluateColor(pSource[width - 1], flags);
}
else
{
// Wrap in U
pDest[0] = EvaluateColor(pSource[width - 1], flags);
pDest[width + 1] = EvaluateColor(pSource[0], flags);
}
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
HRESULT ComputeNMap(_In_ const Image& srcImage, _In_ DWORD flags, _In_ float amplitude,
_In_ DXGI_FORMAT format, _In_ const Image& normalMap)
{
if (!srcImage.pixels || !normalMap.pixels)
return E_INVALIDARG;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const DWORD convFlags = _GetConvertFlags(format);
if (!convFlags)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (!(convFlags & (CONVF_UNORM | CONVF_SNORM | CONVF_FLOAT)))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const size_t width = srcImage.width;
const size_t height = srcImage.height;
if (width != normalMap.width || height != normalMap.height)
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Allocate temporary space (4 scanlines and 3 evaluated rows)
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayXMVECTOR scanline(static_cast<XMVECTOR*>(_aligned_malloc((sizeof(XMVECTOR)*width * 4), 16)));
2016-09-09 02:09:46 +00:00
if (!scanline)
return E_OUTOFMEMORY;
2016-08-22 18:26:36 +00:00
2018-03-29 19:00:16 +00:00
ScopedAlignedArrayFloat buffer(static_cast<float*>(_aligned_malloc(((sizeof(float) * (width + 2)) * 3), 16)));
2016-09-09 02:09:46 +00:00
if (!buffer)
return E_OUTOFMEMORY;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint8_t* pDest = normalMap.pixels;
if (!pDest)
return E_POINTER;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
XMVECTOR* row0 = scanline.get();
XMVECTOR* row1 = row0 + width;
XMVECTOR* row2 = row1 + width;
XMVECTOR* target = row2 + width;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float* val0 = buffer.get();
float* val1 = val0 + width + 2;
float* val2 = val1 + width + 2;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const size_t rowPitch = srcImage.rowPitch;
const uint8_t* pSrc = srcImage.pixels;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Read first scanline row into 'row1'
if (!_LoadScanline(row1, width, pSrc, rowPitch, srcImage.format))
2016-08-22 18:26:36 +00:00
return E_FAIL;
2016-09-09 02:09:46 +00:00
// Setup 'row0'
if (flags & CNMAP_MIRROR_V)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// Mirror first row
memcpy_s(row0, rowPitch, row1, rowPitch);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
// Read last row (Wrap V)
if (!_LoadScanline(row0, width, pSrc + (rowPitch * (height - 1)), rowPitch, srcImage.format))
return E_FAIL;
}
// Evaluate the initial rows
EvaluateRow(row0, val0, width, flags);
EvaluateRow(row1, val1, width, flags);
pSrc += rowPitch;
for (size_t y = 0; y < height; ++y)
{
// Load next scanline of source image
if (y < (height - 1))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!_LoadScanline(row2, width, pSrc, rowPitch, srcImage.format))
2016-08-22 18:26:36 +00:00
return E_FAIL;
}
else
{
2016-09-09 02:09:46 +00:00
if (flags & CNMAP_MIRROR_V)
{
// Use last row of source image
if (!_LoadScanline(row2, width, srcImage.pixels + (rowPitch * (height - 1)), rowPitch, srcImage.format))
return E_FAIL;
}
else
{
// Use first row of source image (Wrap V)
if (!_LoadScanline(row2, width, srcImage.pixels, rowPitch, srcImage.format))
return E_FAIL;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// Evaluate row
EvaluateRow(row2, val2, width, flags);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Generate target scanline
XMVECTOR *dptr = target;
for (size_t x = 0; x < width; ++x)
{
// Compute normal via central differencing
float totDelta = (val0[x] - val0[x + 2]) + (val1[x] - val1[x + 2]) + (val2[x] - val2[x + 2]);
float deltaZX = totDelta * amplitude / 6.f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
totDelta = (val0[x] - val2[x]) + (val0[x + 1] - val2[x + 1]) + (val0[x + 2] - val2[x + 2]);
float deltaZY = totDelta * amplitude / 6.f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
XMVECTOR vx = XMVectorSetZ(g_XMNegIdentityR0, deltaZX); // (-1.0f, 0.0f, deltaZX)
XMVECTOR vy = XMVectorSetZ(g_XMNegIdentityR1, deltaZY); // (0.0f, -1.0f, deltaZY)
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
XMVECTOR normal = XMVector3Normalize(XMVector3Cross(vx, vy));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Compute alpha (1.0 or an occlusion term)
float alpha = 1.f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & CNMAP_COMPUTE_OCCLUSION)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
float delta = 0.f;
float c = val1[x + 1];
float t = val0[x] - c; if (t > 0.f) delta += t;
t = val0[x + 1] - c; if (t > 0.f) delta += t;
t = val0[x + 2] - c; if (t > 0.f) delta += t;
t = val1[x] - c; if (t > 0.f) delta += t;
// Skip current pixel
t = val1[x + 2] - c; if (t > 0.f) delta += t;
t = val2[x] - c; if (t > 0.f) delta += t;
t = val2[x + 1] - c; if (t > 0.f) delta += t;
t = val2[x + 2] - c; if (t > 0.f) delta += t;
// Average delta (divide by 8, scale by amplitude factor)
delta *= 0.125f * amplitude;
if (delta > 0.f)
{
// If < 0, then no occlusion
float r = sqrtf(1.f + delta*delta);
alpha = (r - delta) / r;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// Encode based on target format
if (convFlags & CONVF_UNORM)
{
// 0.5f*normal + 0.5f -or- invert sign case: -0.5f*normal + 0.5f
XMVECTOR n1 = XMVectorMultiplyAdd((flags & CNMAP_INVERT_SIGN) ? g_XMNegativeOneHalf : g_XMOneHalf, normal, g_XMOneHalf);
*dptr++ = XMVectorSetW(n1, alpha);
}
else if (flags & CNMAP_INVERT_SIGN)
{
*dptr++ = XMVectorSetW(XMVectorNegate(normal), alpha);
}
else
{
*dptr++ = XMVectorSetW(normal, alpha);
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (!_StoreScanline(pDest, normalMap.rowPitch, format, target, width))
return E_FAIL;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Cycle buffers
float* temp = val0;
val0 = val1;
val1 = val2;
val2 = temp;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
pSrc += rowPitch;
pDest += normalMap.rowPitch;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
return S_OK;
}
2016-08-22 18:26:36 +00:00
}
//=====================================================================================
// Entry points
//=====================================================================================
//-------------------------------------------------------------------------------------
// Generates a normal map from a height-map
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::ComputeNormalMap(
const Image& srcImage,
DWORD flags,
float amplitude,
DXGI_FORMAT format,
ScratchImage& normalMap)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!srcImage.pixels || !IsValid(format))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-09 02:09:46 +00:00
static_assert(CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask");
switch (flags & 0xf)
2016-08-22 18:26:36 +00:00
{
case 0:
case CNMAP_CHANNEL_RED:
case CNMAP_CHANNEL_GREEN:
case CNMAP_CHANNEL_BLUE:
case CNMAP_CHANNEL_ALPHA:
case CNMAP_CHANNEL_LUMINANCE:
break;
default:
return E_INVALIDARG;
}
2016-09-09 02:09:46 +00:00
if (IsCompressed(format) || IsCompressed(srcImage.format)
|| IsTypeless(format) || IsTypeless(srcImage.format)
|| IsPlanar(format) || IsPlanar(srcImage.format)
|| IsPalettized(format) || IsPalettized(srcImage.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
// Setup target image
normalMap.Release();
2016-09-09 02:09:46 +00:00
HRESULT hr = normalMap.Initialize2D(format, srcImage.width, srcImage.height, 1, 1);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
2016-09-09 02:09:46 +00:00
const Image *img = normalMap.GetImage(0, 0, 0);
if (!img)
2016-08-22 18:26:36 +00:00
{
normalMap.Release();
return E_POINTER;
}
2016-09-09 02:09:46 +00:00
hr = ComputeNMap(srcImage, flags, amplitude, format, *img);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
normalMap.Release();
return hr;
}
return S_OK;
}
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
HRESULT DirectX::ComputeNormalMap(
const Image* srcImages,
size_t nimages,
const TexMetadata& metadata,
DWORD flags,
float amplitude,
DXGI_FORMAT format,
ScratchImage& normalMaps)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (!srcImages || !nimages || !IsValid(format))
2016-08-22 18:26:36 +00:00
return E_INVALIDARG;
2016-09-09 02:09:46 +00:00
if (IsCompressed(format) || IsCompressed(metadata.format)
|| IsTypeless(format) || IsTypeless(metadata.format)
|| IsPlanar(format) || IsPlanar(metadata.format)
|| IsPalettized(format) || IsPalettized(metadata.format))
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
static_assert(CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask");
switch (flags & 0xf)
2016-08-22 18:26:36 +00:00
{
case 0:
case CNMAP_CHANNEL_RED:
case CNMAP_CHANNEL_GREEN:
case CNMAP_CHANNEL_BLUE:
case CNMAP_CHANNEL_ALPHA:
case CNMAP_CHANNEL_LUMINANCE:
break;
default:
return E_INVALIDARG;
}
normalMaps.Release();
TexMetadata mdata2 = metadata;
mdata2.format = format;
2016-09-09 02:09:46 +00:00
HRESULT hr = normalMaps.Initialize(mdata2);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
return hr;
2016-09-09 02:09:46 +00:00
if (nimages != normalMaps.GetImageCount())
2016-08-22 18:26:36 +00:00
{
normalMaps.Release();
return E_FAIL;
}
const Image* dest = normalMaps.GetImages();
2016-09-09 02:09:46 +00:00
if (!dest)
2016-08-22 18:26:36 +00:00
{
normalMaps.Release();
return E_POINTER;
}
2016-09-09 02:09:46 +00:00
for (size_t index = 0; index < nimages; ++index)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(dest[index].format == format);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const Image& src = srcImages[index];
if (IsCompressed(src.format) || IsTypeless(src.format))
2016-08-22 18:26:36 +00:00
{
normalMaps.Release();
2016-09-09 02:09:46 +00:00
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (src.width != dest[index].width || src.height != dest[index].height)
2016-08-22 18:26:36 +00:00
{
normalMaps.Release();
return E_FAIL;
}
2016-09-09 02:09:46 +00:00
hr = ComputeNMap(src, flags, amplitude, format, dest[index]);
if (FAILED(hr))
2016-08-22 18:26:36 +00:00
{
normalMaps.Release();
return hr;
}
}
return S_OK;
}