1 //-------------------------------------------------------------------------------------
2 // DirectXTexNormalMaps.cpp
4 // DirectX Texture Library - Normal map operations
6 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
7 // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
11 // Copyright (c) Microsoft Corporation. All rights reserved.
13 // http://go.microsoft.com/fwlink/?LinkId=248926
14 //-------------------------------------------------------------------------------------
16 #include "DirectXTexP.h"
21 #pragma prefast(suppress : 25000, "FXMVECTOR is 16 bytes")
22 static inline float _EvaluateColor( _In_ FXMVECTOR val, _In_ DWORD flags )
26 static XMVECTORF32 lScale = { 0.2125f, 0.7154f, 0.0721f, 1.f };
28 static_assert( CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask" );
32 case CNMAP_CHANNEL_RED: return XMVectorGetX( val );
33 case CNMAP_CHANNEL_GREEN: return XMVectorGetY( val );
34 case CNMAP_CHANNEL_BLUE: return XMVectorGetZ( val );
35 case CNMAP_CHANNEL_ALPHA: return XMVectorGetW( val );
37 case CNMAP_CHANNEL_LUMINANCE:
39 XMVECTOR v = XMVectorMultiply( val, lScale );
40 XMStoreFloat4A( &f, v );
41 return f.x + f.y + f.z;
51 static void _EvaluateRow( _In_count_(width) const XMVECTOR* pSource, _Out_cap_(width+2) float* pDest,
52 _In_ size_t width, _In_ DWORD flags )
54 assert( pSource && pDest );
57 for( size_t x = 0; x < width; ++x )
59 pDest[x+1] = _EvaluateColor( pSource[x], flags );
62 if ( flags & CNMAP_MIRROR_U )
65 pDest[0] = _EvaluateColor( pSource[0], flags );
66 pDest[width+1] = _EvaluateColor( pSource[width-1], flags );
71 pDest[0] = _EvaluateColor( pSource[width-1], flags );
72 pDest[width+1] = _EvaluateColor( pSource[0], flags );
76 static HRESULT _ComputeNMap( _In_ const Image& srcImage, _In_ DWORD flags, _In_ float amplitude,
77 _In_ DXGI_FORMAT format, _In_ const Image& normalMap )
79 if ( !srcImage.pixels || !normalMap.pixels )
82 assert( !IsCompressed(format) && !IsTypeless( format ) );
84 const DWORD convFlags = _GetConvertFlags( format );
88 if ( !( convFlags & (CONVF_UNORM | CONVF_SNORM | CONVF_FLOAT) ) )
89 HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
91 const size_t width = srcImage.width;
92 const size_t height = srcImage.height;
93 if ( width != normalMap.width || height != normalMap.height )
96 // Allocate temporary space (4 scanlines and 3 evaluated rows)
97 ScopedAlignedArrayXMVECTOR scanline( reinterpret_cast<XMVECTOR*>( _aligned_malloc( (sizeof(XMVECTOR)*width*4), 16 ) ) );
101 ScopedAlignedArrayFloat buffer( reinterpret_cast<float*>( _aligned_malloc( ( ( sizeof(float) * ( width + 2 ) ) * 3 ), 16 ) ) );
103 return E_OUTOFMEMORY;
105 uint8_t* pDest = normalMap.pixels;
109 XMVECTOR* row0 = scanline.get();
110 XMVECTOR* row1 = row0 + width;
111 XMVECTOR* row2 = row1 + width;
112 XMVECTOR* target = row2 + width;
114 float* val0 = buffer.get();
115 float* val1 = val0 + width + 2;
116 float* val2 = val1 + width + 2;
118 const size_t rowPitch = srcImage.rowPitch;
119 const uint8_t* pSrc = srcImage.pixels;
121 // Read first scanline row into 'row1'
122 if ( !_LoadScanline( row1, width, pSrc, rowPitch, srcImage.format ) )
126 if ( flags & CNMAP_MIRROR_V )
129 memcpy_s( row0, rowPitch, row1, rowPitch );
133 // Read last row (Wrap V)
134 if ( !_LoadScanline( row0, width, pSrc + (rowPitch * (height-1)), rowPitch, srcImage.format ) )
138 // Evaluate the initial rows
139 _EvaluateRow( row0, val0, width, flags );
140 _EvaluateRow( row1, val1, width, flags );
144 for( size_t y = 0; y < height; ++y )
146 // Load next scanline of source image
147 if ( y < (height-1) )
149 if ( !_LoadScanline( row2, width, pSrc, rowPitch, srcImage.format ) )
154 if ( flags & CNMAP_MIRROR_V )
156 // Use last row of source image
157 if ( !_LoadScanline( row2, width, srcImage.pixels + (rowPitch * (height-1)), rowPitch, srcImage.format ) )
162 // Use first row of source image (Wrap V)
163 if ( !_LoadScanline( row2, width, srcImage.pixels, rowPitch, srcImage.format ) )
169 _EvaluateRow( row2, val2, width, flags );
171 // Generate target scanline
172 XMVECTOR *dptr = target;
173 for( size_t x = 0; x < width; ++x )
175 // Compute normal via central differencing
176 float totDelta = ( val0[x] - val0[x+2] ) + ( val1[x] - val1[x+2] ) + ( val2[x] - val2[x+2] );
177 float deltaZX = totDelta * amplitude / 6.f;
179 totDelta = ( val0[x] - val2[x] ) + ( val0[x+1] - val2[x+1] ) + ( val0[x+2] - val2[x+2] );
180 float deltaZY = totDelta * amplitude / 6.f;
182 XMVECTOR vx = XMVectorSetZ( g_XMNegIdentityR0, deltaZX ); // (-1.0f, 0.0f, deltaZX)
183 XMVECTOR vy = XMVectorSetZ( g_XMNegIdentityR1, deltaZY ); // (0.0f, -1.0f, deltaZY)
185 XMVECTOR normal = XMVector3Normalize( XMVector3Cross( vx, vy ) );
187 // Compute alpha (1.0 or an occlusion term)
190 if ( flags & CNMAP_COMPUTE_OCCLUSION )
195 float t = val0[x] - c; if ( t > 0.f ) delta += t;
196 t = val0[x+1] - c; if ( t > 0.f ) delta += t;
197 t = val0[x+2] - c; if ( t > 0.f ) delta += t;
198 t = val1[x] - c; if ( t > 0.f ) delta += t;
199 // Skip current pixel
200 t = val1[x+2] - c; if ( t > 0.f ) delta += t;
201 t = val2[x] - c; if ( t > 0.f ) delta += t;
202 t = val2[x+1] - c; if ( t > 0.f ) delta += t;
203 t = val2[x+2] - c; if ( t > 0.f ) delta += t;
205 // Average delta (divide by 8, scale by amplitude factor)
206 delta *= 0.125f * amplitude;
209 // If < 0, then no occlusion
210 float r = sqrtf( 1.f + delta*delta );
211 alpha = (r - delta) / r;
215 // Encode based on target format
216 if ( convFlags & CONVF_UNORM )
218 // 0.5f*normal + 0.5f -or- invert sign case: -0.5f*normal + 0.5f
219 XMVECTOR n1 = XMVectorMultiplyAdd( (flags & CNMAP_INVERT_SIGN) ? g_XMNegativeOneHalf : g_XMOneHalf, normal, g_XMOneHalf );
220 *dptr++ = XMVectorSetW( n1, alpha );
222 else if ( flags & CNMAP_INVERT_SIGN )
224 *dptr++ = XMVectorSetW( XMVectorNegate( normal ), alpha );
228 *dptr++ = XMVectorSetW( normal, alpha );
232 if ( !_StoreScanline( pDest, normalMap.rowPitch, format, target, width ) )
242 pDest += normalMap.rowPitch;
249 //=====================================================================================
251 //=====================================================================================
253 //-------------------------------------------------------------------------------------
254 // Generates a normal map from a height-map
255 //-------------------------------------------------------------------------------------
256 HRESULT ComputeNormalMap( const Image& srcImage, DWORD flags, float amplitude,
257 DXGI_FORMAT format, ScratchImage& normalMap )
259 if ( !srcImage.pixels || !IsValid(format) || IsCompressed( format ) || IsTypeless( format ) )
262 static_assert( CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask" );
263 switch( flags & 0xf )
266 case CNMAP_CHANNEL_RED:
267 case CNMAP_CHANNEL_GREEN:
268 case CNMAP_CHANNEL_BLUE:
269 case CNMAP_CHANNEL_ALPHA:
270 case CNMAP_CHANNEL_LUMINANCE:
277 if ( IsCompressed( srcImage.format ) || IsTypeless( srcImage.format ) )
278 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
280 // Setup target image
283 HRESULT hr = normalMap.Initialize2D( format, srcImage.width, srcImage.height, 1, 1 );
287 const Image *img = normalMap.GetImage( 0, 0, 0 );
294 hr = _ComputeNMap( srcImage, flags, amplitude, format, *img );
304 HRESULT ComputeNormalMap( const Image* srcImages, size_t nimages, const TexMetadata& metadata,
305 DWORD flags, float amplitude, DXGI_FORMAT format, ScratchImage& normalMaps )
307 if ( !srcImages || !nimages )
310 if ( !IsValid(format) || IsCompressed(format) || IsTypeless(format) )
313 static_assert( CNMAP_CHANNEL_RED == 0x1, "CNMAP_CHANNEL_ flag values don't match mask" );
314 switch( flags & 0xf )
317 case CNMAP_CHANNEL_RED:
318 case CNMAP_CHANNEL_GREEN:
319 case CNMAP_CHANNEL_BLUE:
320 case CNMAP_CHANNEL_ALPHA:
321 case CNMAP_CHANNEL_LUMINANCE:
328 normalMaps.Release();
330 TexMetadata mdata2 = metadata;
331 mdata2.format = format;
332 HRESULT hr = normalMaps.Initialize( mdata2 );
336 if ( nimages != normalMaps.GetImageCount() )
338 normalMaps.Release();
342 const Image* dest = normalMaps.GetImages();
345 normalMaps.Release();
349 for( size_t index=0; index < nimages; ++index )
351 assert( dest[ index ].format == format );
353 const Image& src = srcImages[ index ];
354 if ( IsCompressed( src.format ) || IsTypeless( src.format ) )
356 normalMaps.Release();
357 return HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
360 if ( src.width != dest[ index ].width || src.height != dest[ index ].height )
362 normalMaps.Release();
366 hr = _ComputeNMap( src, flags, amplitude, format, dest[ index ] );
369 normalMaps.Release();