DirectXTex.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. //-------------------------------------------------------------------------------------
  2. // DirectXTex.h
  3. //
  4. // DirectX Texture Library
  5. //
  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
  9. // PARTICULAR PURPOSE.
  10. //
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //
  13. // http://go.microsoft.com/fwlink/?LinkId=248926
  14. //-------------------------------------------------------------------------------------
  15. #if defined(_MSC_VER) && (_MSC_VER > 1000)
  16. #pragma once
  17. #endif
  18. #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (_WIN32_WINNT <= _WIN32_WINNT_WIN8)
  19. #error WIC is not supported on Windows Phone 8.0
  20. #endif
  21. // VS 2010's stdint.h conflicts with intsafe.h
  22. #pragma warning(push)
  23. #pragma warning(disable : 4005)
  24. #include <stdint.h>
  25. #pragma warning(pop)
  26. #include <algorithm>
  27. #include <functional>
  28. #if defined(_XBOX_ONE) && defined(_TITLE) && MONOLITHIC
  29. #include <d3d11_x.h>
  30. #define DCOMMON_H_INCLUDED
  31. #else
  32. #include <d3d11_1.h>
  33. #endif
  34. #include <ocidl.h>
  35. #define DIRECTX_TEX_VERSION 130
  36. namespace DirectX
  37. {
  38. //---------------------------------------------------------------------------------
  39. // DXGI Format Utilities
  40. bool IsValid( _In_ DXGI_FORMAT fmt );
  41. bool IsCompressed( _In_ DXGI_FORMAT fmt );
  42. bool IsPacked( _In_ DXGI_FORMAT fmt );
  43. bool IsVideo( _In_ DXGI_FORMAT fmt );
  44. bool IsPlanar( _In_ DXGI_FORMAT fmt );
  45. bool IsPalettized( _In_ DXGI_FORMAT fmt );
  46. bool IsDepthStencil(_In_ DXGI_FORMAT fmt );
  47. bool IsSRGB( _In_ DXGI_FORMAT fmt );
  48. bool IsTypeless( _In_ DXGI_FORMAT fmt, _In_ bool partialTypeless = true );
  49. bool HasAlpha( _In_ DXGI_FORMAT fmt );
  50. size_t BitsPerPixel( _In_ DXGI_FORMAT fmt );
  51. size_t BitsPerColor( _In_ DXGI_FORMAT fmt );
  52. enum CP_FLAGS
  53. {
  54. CP_FLAGS_NONE = 0x0, // Normal operation
  55. CP_FLAGS_LEGACY_DWORD = 0x1, // Assume pitch is DWORD aligned instead of BYTE aligned
  56. CP_FLAGS_PARAGRAPH = 0x2, // Assume pitch is 16-byte aligned instead of BYTE aligned
  57. CP_FLAGS_24BPP = 0x10000, // Override with a legacy 24 bits-per-pixel format size
  58. CP_FLAGS_16BPP = 0x20000, // Override with a legacy 16 bits-per-pixel format size
  59. CP_FLAGS_8BPP = 0x40000, // Override with a legacy 8 bits-per-pixel format size
  60. };
  61. void ComputePitch( _In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height,
  62. _Out_ size_t& rowPitch, _Out_ size_t& slicePitch, _In_ DWORD flags = CP_FLAGS_NONE );
  63. size_t ComputeScanlines( _In_ DXGI_FORMAT fmt, _In_ size_t height );
  64. DXGI_FORMAT MakeSRGB( _In_ DXGI_FORMAT fmt );
  65. DXGI_FORMAT MakeTypeless( _In_ DXGI_FORMAT fmt );
  66. DXGI_FORMAT MakeTypelessUNORM( _In_ DXGI_FORMAT fmt );
  67. DXGI_FORMAT MakeTypelessFLOAT( _In_ DXGI_FORMAT fmt );
  68. //---------------------------------------------------------------------------------
  69. // Texture metadata
  70. enum TEX_DIMENSION
  71. // Subset here matches D3D10_RESOURCE_DIMENSION and D3D11_RESOURCE_DIMENSION
  72. {
  73. TEX_DIMENSION_TEXTURE1D = 2,
  74. TEX_DIMENSION_TEXTURE2D = 3,
  75. TEX_DIMENSION_TEXTURE3D = 4,
  76. };
  77. enum TEX_MISC_FLAG
  78. // Subset here matches D3D10_RESOURCE_MISC_FLAG and D3D11_RESOURCE_MISC_FLAG
  79. {
  80. TEX_MISC_TEXTURECUBE = 0x4L,
  81. };
  82. enum TEX_MISC_FLAG2
  83. {
  84. TEX_MISC2_ALPHA_MODE_MASK = 0x7L,
  85. };
  86. enum TEX_ALPHA_MODE
  87. // Matches DDS_ALPHA_MODE, encoded in MISC_FLAGS2
  88. {
  89. TEX_ALPHA_MODE_UNKNOWN = 0,
  90. TEX_ALPHA_MODE_STRAIGHT = 1,
  91. TEX_ALPHA_MODE_PREMULTIPLIED = 2,
  92. TEX_ALPHA_MODE_OPAQUE = 3,
  93. TEX_ALPHA_MODE_CUSTOM = 4,
  94. };
  95. struct TexMetadata
  96. {
  97. size_t width;
  98. size_t height; // Should be 1 for 1D textures
  99. size_t depth; // Should be 1 for 1D or 2D textures
  100. size_t arraySize; // For cubemap, this is a multiple of 6
  101. size_t mipLevels;
  102. uint32_t miscFlags;
  103. uint32_t miscFlags2;
  104. DXGI_FORMAT format;
  105. TEX_DIMENSION dimension;
  106. size_t ComputeIndex( _In_ size_t mip, _In_ size_t item, _In_ size_t slice ) const;
  107. // Returns size_t(-1) to indicate an out-of-range error
  108. bool IsCubemap() const { return (miscFlags & TEX_MISC_TEXTURECUBE) != 0; }
  109. // Helper for miscFlags
  110. bool IsPMAlpha() const { return ((miscFlags2 & TEX_MISC2_ALPHA_MODE_MASK) == TEX_ALPHA_MODE_PREMULTIPLIED) != 0; }
  111. void SetAlphaMode( TEX_ALPHA_MODE mode ) { miscFlags2 = (miscFlags2 & ~TEX_MISC2_ALPHA_MODE_MASK) | static_cast<uint32_t>(mode); }
  112. // Helpers for miscFlags2
  113. bool IsVolumemap() const { return (dimension == TEX_DIMENSION_TEXTURE3D); }
  114. // Helper for dimension
  115. };
  116. enum DDS_FLAGS
  117. {
  118. DDS_FLAGS_NONE = 0x0,
  119. DDS_FLAGS_LEGACY_DWORD = 0x1,
  120. // Assume pitch is DWORD aligned instead of BYTE aligned (used by some legacy DDS files)
  121. DDS_FLAGS_NO_LEGACY_EXPANSION = 0x2,
  122. // Do not implicitly convert legacy formats that result in larger pixel sizes (24 bpp, 3:3:2, A8L8, A4L4, P8, A8P8)
  123. DDS_FLAGS_NO_R10B10G10A2_FIXUP = 0x4,
  124. // Do not use work-around for long-standing D3DX DDS file format issue which reversed the 10:10:10:2 color order masks
  125. DDS_FLAGS_FORCE_RGB = 0x8,
  126. // Convert DXGI 1.1 BGR formats to DXGI_FORMAT_R8G8B8A8_UNORM to avoid use of optional WDDM 1.1 formats
  127. DDS_FLAGS_NO_16BPP = 0x10,
  128. // Conversions avoid use of 565, 5551, and 4444 formats and instead expand to 8888 to avoid use of optional WDDM 1.2 formats
  129. DDS_FLAGS_EXPAND_LUMINANCE = 0x20,
  130. // When loading legacy luminance formats expand replicating the color channels rather than leaving them packed (L8, L16, A8L8)
  131. DDS_FLAGS_FORCE_DX10_EXT = 0x10000,
  132. // Always use the 'DX10' header extension for DDS writer (i.e. don't try to write DX9 compatible DDS files)
  133. DDS_FLAGS_FORCE_DX10_EXT_MISC2 = 0x20000,
  134. // DDS_FLAGS_FORCE_DX10_EXT including miscFlags2 information (result may not be compatible with D3DX10 or D3DX11)
  135. };
  136. enum WIC_FLAGS
  137. {
  138. WIC_FLAGS_NONE = 0x0,
  139. WIC_FLAGS_FORCE_RGB = 0x1,
  140. // Loads DXGI 1.1 BGR formats as DXGI_FORMAT_R8G8B8A8_UNORM to avoid use of optional WDDM 1.1 formats
  141. WIC_FLAGS_NO_X2_BIAS = 0x2,
  142. // Loads DXGI 1.1 X2 10:10:10:2 format as DXGI_FORMAT_R10G10B10A2_UNORM
  143. WIC_FLAGS_NO_16BPP = 0x4,
  144. // Loads 565, 5551, and 4444 formats as 8888 to avoid use of optional WDDM 1.2 formats
  145. WIC_FLAGS_ALLOW_MONO = 0x8,
  146. // Loads 1-bit monochrome (black & white) as R1_UNORM rather than 8-bit grayscale
  147. WIC_FLAGS_ALL_FRAMES = 0x10,
  148. // Loads all images in a multi-frame file, converting/resizing to match the first frame as needed, defaults to 0th frame otherwise
  149. WIC_FLAGS_IGNORE_SRGB = 0x20,
  150. // Ignores sRGB metadata if present in the file
  151. WIC_FLAGS_DITHER = 0x10000,
  152. // Use ordered 4x4 dithering for any required conversions
  153. WIC_FLAGS_DITHER_DIFFUSION = 0x20000,
  154. // Use error-diffusion dithering for any required conversions
  155. WIC_FLAGS_FILTER_POINT = 0x100000,
  156. WIC_FLAGS_FILTER_LINEAR = 0x200000,
  157. WIC_FLAGS_FILTER_CUBIC = 0x300000,
  158. WIC_FLAGS_FILTER_FANT = 0x400000, // Combination of Linear and Box filter
  159. // Filtering mode to use for any required image resizing (only needed when loading arrays of differently sized images; defaults to Fant)
  160. };
  161. HRESULT GetMetadataFromDDSMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DWORD flags,
  162. _Out_ TexMetadata& metadata );
  163. HRESULT GetMetadataFromDDSFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
  164. _Out_ TexMetadata& metadata );
  165. HRESULT GetMetadataFromTGAMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size,
  166. _Out_ TexMetadata& metadata );
  167. HRESULT GetMetadataFromTGAFile( _In_z_ LPCWSTR szFile,
  168. _Out_ TexMetadata& metadata );
  169. HRESULT GetMetadataFromWICMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DWORD flags,
  170. _Out_ TexMetadata& metadata );
  171. HRESULT GetMetadataFromWICFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
  172. _Out_ TexMetadata& metadata );
  173. //---------------------------------------------------------------------------------
  174. // Bitmap image container
  175. struct Image
  176. {
  177. size_t width;
  178. size_t height;
  179. DXGI_FORMAT format;
  180. size_t rowPitch;
  181. size_t slicePitch;
  182. uint8_t* pixels;
  183. };
  184. class ScratchImage
  185. {
  186. public:
  187. ScratchImage()
  188. : _nimages(0), _size(0), _image(nullptr), _memory(nullptr) {}
  189. ScratchImage(ScratchImage&& moveFrom)
  190. : _nimages(0), _size(0), _image(nullptr), _memory(nullptr) { *this = std::move(moveFrom); }
  191. ~ScratchImage() { Release(); }
  192. ScratchImage& operator= (ScratchImage&& moveFrom);
  193. HRESULT Initialize( _In_ const TexMetadata& mdata, _In_ DWORD flags = CP_FLAGS_NONE );
  194. HRESULT Initialize1D( _In_ DXGI_FORMAT fmt, _In_ size_t length, _In_ size_t arraySize, _In_ size_t mipLevels, _In_ DWORD flags = CP_FLAGS_NONE );
  195. HRESULT Initialize2D( _In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t arraySize, _In_ size_t mipLevels, _In_ DWORD flags = CP_FLAGS_NONE );
  196. HRESULT Initialize3D( _In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t depth, _In_ size_t mipLevels, _In_ DWORD flags = CP_FLAGS_NONE );
  197. HRESULT InitializeCube( _In_ DXGI_FORMAT fmt, _In_ size_t width, _In_ size_t height, _In_ size_t nCubes, _In_ size_t mipLevels, _In_ DWORD flags = CP_FLAGS_NONE );
  198. HRESULT InitializeFromImage( _In_ const Image& srcImage, _In_ bool allow1D = false, _In_ DWORD flags = CP_FLAGS_NONE );
  199. HRESULT InitializeArrayFromImages( _In_reads_(nImages) const Image* images, _In_ size_t nImages, _In_ bool allow1D = false, _In_ DWORD flags = CP_FLAGS_NONE );
  200. HRESULT InitializeCubeFromImages( _In_reads_(nImages) const Image* images, _In_ size_t nImages, _In_ DWORD flags = CP_FLAGS_NONE );
  201. HRESULT Initialize3DFromImages( _In_reads_(depth) const Image* images, _In_ size_t depth, _In_ DWORD flags = CP_FLAGS_NONE );
  202. void Release();
  203. bool OverrideFormat( _In_ DXGI_FORMAT f );
  204. const TexMetadata& GetMetadata() const { return _metadata; }
  205. const Image* GetImage(_In_ size_t mip, _In_ size_t item, _In_ size_t slice) const;
  206. const Image* GetImages() const { return _image; }
  207. size_t GetImageCount() const { return _nimages; }
  208. uint8_t* GetPixels() const { return _memory; }
  209. size_t GetPixelsSize() const { return _size; }
  210. bool IsAlphaAllOpaque() const;
  211. private:
  212. size_t _nimages;
  213. size_t _size;
  214. TexMetadata _metadata;
  215. Image* _image;
  216. uint8_t* _memory;
  217. // Hide copy constructor and assignment operator
  218. ScratchImage( const ScratchImage& );
  219. ScratchImage& operator=( const ScratchImage& );
  220. };
  221. //---------------------------------------------------------------------------------
  222. // Memory blob (allocated buffer pointer is always 16-byte aligned)
  223. class Blob
  224. {
  225. public:
  226. Blob() : _buffer(nullptr), _size(0) {}
  227. Blob(Blob&& moveFrom) : _buffer(nullptr), _size(0) { *this = std::move(moveFrom); }
  228. ~Blob() { Release(); }
  229. Blob& operator= (Blob&& moveFrom);
  230. HRESULT Initialize( _In_ size_t size );
  231. void Release();
  232. void *GetBufferPointer() const { return _buffer; }
  233. size_t GetBufferSize() const { return _size; }
  234. private:
  235. void* _buffer;
  236. size_t _size;
  237. // Hide copy constructor and assignment operator
  238. Blob( const Blob& );
  239. Blob& operator=( const Blob& );
  240. };
  241. //---------------------------------------------------------------------------------
  242. // Image I/O
  243. // DDS operations
  244. HRESULT LoadFromDDSMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DWORD flags,
  245. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  246. HRESULT LoadFromDDSFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
  247. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  248. HRESULT SaveToDDSMemory( _In_ const Image& image, _In_ DWORD flags,
  249. _Out_ Blob& blob );
  250. HRESULT SaveToDDSMemory( _In_reads_(nimages) const Image* images, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ DWORD flags,
  251. _Out_ Blob& blob );
  252. HRESULT SaveToDDSFile( _In_ const Image& image, _In_ DWORD flags, _In_z_ LPCWSTR szFile );
  253. HRESULT SaveToDDSFile( _In_reads_(nimages) const Image* images, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ DWORD flags, _In_z_ LPCWSTR szFile );
  254. // TGA operations
  255. HRESULT LoadFromTGAMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size,
  256. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  257. HRESULT LoadFromTGAFile( _In_z_ LPCWSTR szFile,
  258. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  259. HRESULT SaveToTGAMemory( _In_ const Image& image, _Out_ Blob& blob );
  260. HRESULT SaveToTGAFile( _In_ const Image& image, _In_z_ LPCWSTR szFile );
  261. // WIC operations
  262. HRESULT LoadFromWICMemory( _In_reads_bytes_(size) LPCVOID pSource, _In_ size_t size, _In_ DWORD flags,
  263. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  264. HRESULT LoadFromWICFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
  265. _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );
  266. HRESULT SaveToWICMemory( _In_ const Image& image, _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
  267. _Out_ Blob& blob, _In_opt_ const GUID* targetFormat = nullptr, _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );
  268. HRESULT SaveToWICMemory( _In_count_(nimages) const Image* images, _In_ size_t nimages, _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
  269. _Out_ Blob& blob, _In_opt_ const GUID* targetFormat = nullptr, _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );
  270. HRESULT SaveToWICFile( _In_ const Image& image, _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
  271. _In_z_ LPCWSTR szFile, _In_opt_ const GUID* targetFormat = nullptr, _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );
  272. HRESULT SaveToWICFile( _In_count_(nimages) const Image* images, _In_ size_t nimages, _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
  273. _In_z_ LPCWSTR szFile, _In_opt_ const GUID* targetFormat = nullptr, _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );
  274. enum WICCodecs
  275. {
  276. WIC_CODEC_BMP =1, // Windows Bitmap (.bmp)
  277. WIC_CODEC_JPEG, // Joint Photographic Experts Group (.jpg, .jpeg)
  278. WIC_CODEC_PNG, // Portable Network Graphics (.png)
  279. WIC_CODEC_TIFF, // Tagged Image File Format (.tif, .tiff)
  280. WIC_CODEC_GIF, // Graphics Interchange Format (.gif)
  281. WIC_CODEC_WMP, // Windows Media Photo / HD Photo / JPEG XR (.hdp, .jxr, .wdp)
  282. WIC_CODEC_ICO, // Windows Icon (.ico)
  283. };
  284. REFGUID GetWICCodec( _In_ WICCodecs codec );
  285. //---------------------------------------------------------------------------------
  286. // Texture conversion, resizing, mipmap generation, and block compression
  287. enum TEX_FR_FLAGS
  288. {
  289. TEX_FR_ROTATE0 = 0x0,
  290. TEX_FR_ROTATE90 = 0x1,
  291. TEX_FR_ROTATE180 = 0x2,
  292. TEX_FR_ROTATE270 = 0x3,
  293. TEX_FR_FLIP_HORIZONTAL = 0x08,
  294. TEX_FR_FLIP_VERTICAL = 0x10,
  295. };
  296. HRESULT FlipRotate( _In_ const Image& srcImage, _In_ DWORD flags, _Out_ ScratchImage& image );
  297. HRESULT FlipRotate( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  298. _In_ DWORD flags, _Out_ ScratchImage& result );
  299. // Flip and/or rotate image
  300. enum TEX_FILTER_FLAGS
  301. {
  302. TEX_FILTER_DEFAULT = 0,
  303. TEX_FILTER_WRAP_U = 0x1,
  304. TEX_FILTER_WRAP_V = 0x2,
  305. TEX_FILTER_WRAP_W = 0x4,
  306. TEX_FILTER_WRAP = ( TEX_FILTER_WRAP_U | TEX_FILTER_WRAP_V | TEX_FILTER_WRAP_W ),
  307. TEX_FILTER_MIRROR_U = 0x10,
  308. TEX_FILTER_MIRROR_V = 0x20,
  309. TEX_FILTER_MIRROR_W = 0x40,
  310. TEX_FILTER_MIRROR = ( TEX_FILTER_MIRROR_U | TEX_FILTER_MIRROR_V | TEX_FILTER_MIRROR_W ),
  311. // Wrap vs. Mirror vs. Clamp filtering options
  312. TEX_FILTER_SEPARATE_ALPHA = 0x100,
  313. // Resize color and alpha channel independently
  314. TEX_FILTER_RGB_COPY_RED = 0x1000,
  315. TEX_FILTER_RGB_COPY_GREEN = 0x2000,
  316. TEX_FILTER_RGB_COPY_BLUE = 0x4000,
  317. // When converting RGB to R, defaults to using grayscale. These flags indicate copying a specific channel instead
  318. // When converting RGB to RG, defaults to copying RED | GREEN. These flags control which channels are selected instead.
  319. TEX_FILTER_DITHER = 0x10000,
  320. // Use ordered 4x4 dithering for any required conversions
  321. TEX_FILTER_DITHER_DIFFUSION = 0x20000,
  322. // Use error-diffusion dithering for any required conversions
  323. TEX_FILTER_POINT = 0x100000,
  324. TEX_FILTER_LINEAR = 0x200000,
  325. TEX_FILTER_CUBIC = 0x300000,
  326. TEX_FILTER_BOX = 0x400000,
  327. TEX_FILTER_FANT = 0x400000, // Equiv to Box filtering for mipmap generation
  328. TEX_FILTER_TRIANGLE = 0x500000,
  329. // Filtering mode to use for any required image resizing
  330. TEX_FILTER_SRGB_IN = 0x1000000,
  331. TEX_FILTER_SRGB_OUT = 0x2000000,
  332. TEX_FILTER_SRGB = ( TEX_FILTER_SRGB_IN | TEX_FILTER_SRGB_OUT ),
  333. // sRGB <-> RGB for use in conversion operations
  334. // if the input format type is IsSRGB(), then SRGB_IN is on by default
  335. // if the output format type is IsSRGB(), then SRGB_OUT is on by default
  336. TEX_FILTER_FORCE_NON_WIC = 0x10000000,
  337. // Forces use of the non-WIC path when both are an option
  338. TEX_FILTER_FORCE_WIC = 0x20000000,
  339. // Forces use of the WIC path even when logic would have picked a non-WIC path when both are an option
  340. };
  341. HRESULT Resize( _In_ const Image& srcImage, _In_ size_t width, _In_ size_t height, _In_ DWORD filter,
  342. _Out_ ScratchImage& image );
  343. HRESULT Resize( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  344. _In_ size_t width, _In_ size_t height, _In_ DWORD filter, _Out_ ScratchImage& result );
  345. // Resize the image to width x height. Defaults to Fant filtering.
  346. // Note for a complex resize, the result will always have mipLevels == 1
  347. HRESULT Convert( _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold,
  348. _Out_ ScratchImage& image );
  349. HRESULT Convert( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  350. _In_ DXGI_FORMAT format, _In_ DWORD filter, _In_ float threshold, _Out_ ScratchImage& result );
  351. // Convert the image to a new format
  352. HRESULT ConvertToSinglePlane( _In_ const Image& srcImage, _Out_ ScratchImage& image );
  353. HRESULT ConvertToSinglePlane( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  354. _Out_ ScratchImage& image );
  355. // Converts the image from a planar format to an equivalent non-planar format
  356. HRESULT GenerateMipMaps( _In_ const Image& baseImage, _In_ DWORD filter, _In_ size_t levels,
  357. _Inout_ ScratchImage& mipChain, _In_ bool allow1D = false );
  358. HRESULT GenerateMipMaps( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  359. _In_ DWORD filter, _In_ size_t levels, _Inout_ ScratchImage& mipChain );
  360. // levels of '0' indicates a full mipchain, otherwise is generates that number of total levels (including the source base image)
  361. // Defaults to Fant filtering which is equivalent to a box filter
  362. HRESULT GenerateMipMaps3D( _In_reads_(depth) const Image* baseImages, _In_ size_t depth, _In_ DWORD filter, _In_ size_t levels,
  363. _Out_ ScratchImage& mipChain );
  364. HRESULT GenerateMipMaps3D( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  365. _In_ DWORD filter, _In_ size_t levels, _Out_ ScratchImage& mipChain );
  366. // levels of '0' indicates a full mipchain, otherwise is generates that number of total levels (including the source base image)
  367. // Defaults to Fant filtering which is equivalent to a box filter
  368. enum TEX_PMALPHA_FLAGS
  369. {
  370. TEX_PMALPHA_DEFAULT = 0,
  371. TEX_PMALPHA_IGNORE_SRGB = 0x1,
  372. // ignores sRGB colorspace conversions
  373. TEX_PMALPHA_SRGB_IN = 0x1000000,
  374. TEX_PMALPHA_SRGB_OUT = 0x2000000,
  375. TEX_PMALPHA_SRGB = ( TEX_PMALPHA_SRGB_IN | TEX_PMALPHA_SRGB_OUT ),
  376. // if the input format type is IsSRGB(), then SRGB_IN is on by default
  377. // if the output format type is IsSRGB(), then SRGB_OUT is on by default
  378. };
  379. HRESULT PremultiplyAlpha( _In_ const Image& srcImage, _In_ DWORD flags, _Out_ ScratchImage& image );
  380. HRESULT PremultiplyAlpha( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ DWORD flags, _Out_ ScratchImage& result );
  381. // Converts to a premultiplied alpha version of the texture
  382. enum TEX_COMPRESS_FLAGS
  383. {
  384. TEX_COMPRESS_DEFAULT = 0,
  385. TEX_COMPRESS_RGB_DITHER = 0x10000,
  386. // Enables dithering RGB colors for BC1-3 compression
  387. TEX_COMPRESS_A_DITHER = 0x20000,
  388. // Enables dithering alpha for BC1-3 compression
  389. TEX_COMPRESS_DITHER = 0x30000,
  390. // Enables both RGB and alpha dithering for BC1-3 compression
  391. TEX_COMPRESS_UNIFORM = 0x40000,
  392. // Uniform color weighting for BC1-3 compression; by default uses perceptual weighting
  393. TEX_COMPRESS_SRGB_IN = 0x1000000,
  394. TEX_COMPRESS_SRGB_OUT = 0x2000000,
  395. TEX_COMPRESS_SRGB = ( TEX_COMPRESS_SRGB_IN | TEX_COMPRESS_SRGB_OUT ),
  396. // if the input format type is IsSRGB(), then SRGB_IN is on by default
  397. // if the output format type is IsSRGB(), then SRGB_OUT is on by default
  398. TEX_COMPRESS_PARALLEL = 0x10000000,
  399. // Compress is free to use multithreading to improve performance (by default it does not use multithreading)
  400. };
  401. HRESULT Compress( _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _In_ DWORD compress, _In_ float alphaRef,
  402. _Out_ ScratchImage& cImage );
  403. HRESULT Compress( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  404. _In_ DXGI_FORMAT format, _In_ DWORD compress, _In_ float alphaRef, _Out_ ScratchImage& cImages );
  405. // Note that alphaRef is only used by BC1. 0.5f is a typical value to use
  406. HRESULT Compress( _In_ ID3D11Device* pDevice, _In_ const Image& srcImage, _In_ DXGI_FORMAT format, _In_ DWORD compress,
  407. _In_ float alphaWeight, _Out_ ScratchImage& image );
  408. HRESULT Compress( _In_ ID3D11Device* pDevice, _In_ const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  409. _In_ DXGI_FORMAT format, _In_ DWORD compress, _In_ float alphaWeight, _Out_ ScratchImage& cImages );
  410. // DirectCompute-based compression (alphaWeight is only used by BC7. 1.0 is the typical value to use)
  411. HRESULT Decompress( _In_ const Image& cImage, _In_ DXGI_FORMAT format, _Out_ ScratchImage& image );
  412. HRESULT Decompress( _In_reads_(nimages) const Image* cImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  413. _In_ DXGI_FORMAT format, _Out_ ScratchImage& images );
  414. //---------------------------------------------------------------------------------
  415. // Normal map operations
  416. enum CNMAP_FLAGS
  417. {
  418. CNMAP_DEFAULT = 0,
  419. CNMAP_CHANNEL_RED = 0x1,
  420. CNMAP_CHANNEL_GREEN = 0x2,
  421. CNMAP_CHANNEL_BLUE = 0x3,
  422. CNMAP_CHANNEL_ALPHA = 0x4,
  423. CNMAP_CHANNEL_LUMINANCE = 0x5,
  424. // Channel selection when evaluting color value for height
  425. // Luminance is a combination of red, green, and blue
  426. CNMAP_MIRROR_U = 0x1000,
  427. CNMAP_MIRROR_V = 0x2000,
  428. CNMAP_MIRROR = 0x3000,
  429. // Use mirror semantics for scanline references (defaults to wrap)
  430. CNMAP_INVERT_SIGN = 0x4000,
  431. // Inverts normal sign
  432. CNMAP_COMPUTE_OCCLUSION = 0x8000,
  433. // Computes a crude occlusion term stored in the alpha channel
  434. };
  435. HRESULT ComputeNormalMap( _In_ const Image& srcImage, _In_ DWORD flags, _In_ float amplitude,
  436. _In_ DXGI_FORMAT format, _Out_ ScratchImage& normalMap );
  437. HRESULT ComputeNormalMap( _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  438. _In_ DWORD flags, _In_ float amplitude, _In_ DXGI_FORMAT format, _Out_ ScratchImage& normalMaps );
  439. //---------------------------------------------------------------------------------
  440. // Misc image operations
  441. struct Rect
  442. {
  443. size_t x;
  444. size_t y;
  445. size_t w;
  446. size_t h;
  447. Rect() {}
  448. Rect( size_t _x, size_t _y, size_t _w, size_t _h ) : x(_x), y(_y), w(_w), h(_h) {}
  449. };
  450. HRESULT CopyRectangle( _In_ const Image& srcImage, _In_ const Rect& srcRect, _In_ const Image& dstImage,
  451. _In_ DWORD filter, _In_ size_t xOffset, _In_ size_t yOffset );
  452. enum CMSE_FLAGS
  453. {
  454. CMSE_DEFAULT = 0,
  455. CMSE_IMAGE1_SRGB = 0x1,
  456. CMSE_IMAGE2_SRGB = 0x2,
  457. // Indicates that image needs gamma correction before comparision
  458. CMSE_IGNORE_RED = 0x10,
  459. CMSE_IGNORE_GREEN = 0x20,
  460. CMSE_IGNORE_BLUE = 0x40,
  461. CMSE_IGNORE_ALPHA = 0x80,
  462. // Ignore the channel when computing MSE
  463. CMSE_IMAGE1_X2_BIAS = 0x100,
  464. CMSE_IMAGE2_X2_BIAS = 0x200,
  465. // Indicates that image should be scaled and biased before comparison (i.e. UNORM -> SNORM)
  466. };
  467. HRESULT ComputeMSE( _In_ const Image& image1, _In_ const Image& image2, _Out_ float& mse, _Out_writes_opt_(4) float* mseV, _In_ DWORD flags = 0 );
  468. //---------------------------------------------------------------------------------
  469. // Direct3D 11 functions
  470. bool IsSupportedTexture( _In_ ID3D11Device* pDevice, _In_ const TexMetadata& metadata );
  471. HRESULT CreateTexture( _In_ ID3D11Device* pDevice, _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  472. _Outptr_ ID3D11Resource** ppResource );
  473. HRESULT CreateShaderResourceView( _In_ ID3D11Device* pDevice, _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  474. _Outptr_ ID3D11ShaderResourceView** ppSRV );
  475. HRESULT CreateTextureEx( _In_ ID3D11Device* pDevice, _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  476. _In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, _In_ bool forceSRGB,
  477. _Outptr_ ID3D11Resource** ppResource );
  478. HRESULT CreateShaderResourceViewEx( _In_ ID3D11Device* pDevice, _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages, _In_ const TexMetadata& metadata,
  479. _In_ D3D11_USAGE usage, _In_ unsigned int bindFlags, _In_ unsigned int cpuAccessFlags, _In_ unsigned int miscFlags, _In_ bool forceSRGB,
  480. _Outptr_ ID3D11ShaderResourceView** ppSRV );
  481. HRESULT CaptureTexture( _In_ ID3D11Device* pDevice, _In_ ID3D11DeviceContext* pContext, _In_ ID3D11Resource* pSource, _Out_ ScratchImage& result );
  482. #include "DirectXTex.inl"
  483. }; // namespace