### Install CoPilot and DirectXTex CLI Skill Source: https://github.com/microsoft/directxtex/blob/main/README.md Installs GitHub Copilot and CLI, then installs the DirectXTex CoPilot skill for command-line assistance. ```bash winget install GitHub.Copilot winget install GitHub.cli gh skill install microsoft/directxtex copilot /skills list ``` -------------------------------- ### vcpkg Installation Source: https://github.com/microsoft/directxtex/wiki/Using-JPEG-PNG-OSS Instructions for installing DirectXTex with PNG or JPEG support using vcpkg. ```APIDOC ## vcpkg Installation ### Description Instructions for installing DirectXTex with PNG or JPEG support using vcpkg. ### Commands - To install with PNG support: ``` vcpkg install directxtex[png] ``` - To install with JPEG support: ``` vcpkg install directxtex[jpeg] ``` ``` -------------------------------- ### Install DirectXTex (64-bit) Source: https://github.com/microsoft/directxtex/wiki/Integration Installs the 64-bit version of the DirectXTex library using a specific vcpkg triplet. ```bash vcpkg install directxtex:x64-windows ``` -------------------------------- ### Install DirectXTex (System-wide) Source: https://github.com/microsoft/directxtex/wiki/Integration Installs the DirectXTex library for system-wide access using vcpkg. ```bash vcpkg install directxtex ``` -------------------------------- ### Install pkg-config file Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Installs the pkg-config file for the DirectXTex library to the appropriate directory. ```cmake install(FILES "${CMAKE_CURRENT_BINARY_DIR}/DirectXTex.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) ``` -------------------------------- ### Install DirectXTex (UWP) Source: https://github.com/microsoft/directxtex/wiki/Integration Installs Universal Windows Platform (UWP) versions of the DirectXTex library using a specific vcpkg triplet. ```bash vcpkg install directxtex:x64-uwp ``` -------------------------------- ### Install DirectXTex (WSL) Source: https://github.com/microsoft/directxtex/wiki/Integration Installs DirectXTex for Windows Subsystem for Linux (WSL) using the appropriate vcpkg triplet. ```bash ./vcpkg install directxtex:x64-linux ``` ```bash ./vcpkg install directxtex:arm64-linux ``` -------------------------------- ### Install DirectXTex with PNG support via vcpkg Source: https://github.com/microsoft/directxtex/wiki/Using-JPEG-PNG-OSS Use this command to install the DirectXTex library with PNG support enabled for Linux environments. ```bash vcpkg install directxtex[png] ``` -------------------------------- ### Basic Image Conversion Example Source: https://github.com/microsoft/directxtex/wiki/Convert This example demonstrates a basic conversion of an image to the DXGI_FORMAT_R8G8B8A8_UNORM format using default filter and threshold values. ```cpp ScratchImage srcImage; ... ScratchImage destImage; hr = Convert( srcImage.GetImages(), srcImage.GetImageCount(), srcImage.GetMetadata(), DXGI_FORMAT_R8G8B8A8_UNORM, TEX_FILTER_DEFAULT, TEX_THRESHOLD_DEFAULT, destImage ); if (FAILED(hr)) ... ``` -------------------------------- ### Image Conversion with Options and Status Callback Example Source: https://github.com/microsoft/directxtex/wiki/Convert This example shows how to use `ConvertEx` with a `ConvertOptions` struct and a lambda function for the status callback to monitor conversion progress. ```cpp ... ConvertOptions opts = {}; opts.filter = TEX_FILTER_DEFAULT; opts.threshold = TEX_THRESHOLD_DEFAULT; hr = ConvertEx( srcImage.GetImages(), srcImage.GetImageCount(), srcImage.GetMetadata(), DXGI_FORMAT_R8G8B8A8_UNORM, opts, destImage, [&](size_t current, size_t count) -> bool { // Do status update here current of count return true; }); if (FAILED(hr)) ``` -------------------------------- ### Load Image and Create Shader Resource View Example Source: https://github.com/microsoft/directxtex/wiki/CreateShaderResourceView This example demonstrates loading different image file types (.dds, .tga, .hdr, or WIC-compatible) and then creating a Direct3D 11 shader resource view from the loaded image data. Ensure the device and filename are correctly initialized. ```cpp wchar_t ext[_MAX_EXT] = {}; _wsplitpath_s( filename, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT ); ScratchImage image; HRESULT hr; if ( _wcsicmp( ext, L".dds" ) == 0 ) { hr = LoadFromDDSFile( filename, DDS_FLAGS_NONE, nullptr, image ); } else if ( _wcsicmp( ext, L".tga" ) == 0 ) { hr = LoadFromTGAFile( filename, nullptr, image ); } else if ( _wcsicmp( ext, L".hdr" ) == 0 ) { hr = LoadFromHDRFile( filename, nullptr, image ); } else { hr = LoadFromWICFile( filename, WIC_FLAGS_NONE, nullptr, image ); } if ( SUCCEEDED(hr) ) { ID3D11ShaderResourceView* pSRV = nullptr; hr = CreateShaderResourceView( device, image.GetImages(), image.GetImageCount(), image.GetMetadata(), &pSRV ); if ( FAILED(hr) ) { ... } } ``` -------------------------------- ### Install DirectXTex with JPEG support via vcpkg Source: https://github.com/microsoft/directxtex/wiki/Using-JPEG-PNG-OSS Use this command to install the DirectXTex library with JPEG support enabled for Linux environments. ```bash vcpkg install directxtex[jpeg] ``` -------------------------------- ### Install texconv with winget Source: https://github.com/microsoft/directxtex/wiki/Texconv Installs the texconv utility using the Windows Package Manager. ```bash winget install Microsoft.DirectXTex.Texconv ``` -------------------------------- ### Example Usage of GenerateMipMaps3D Source: https://github.com/microsoft/directxtex/wiki/GenerateMipMaps3D This example demonstrates how to call GenerateMipMaps3D with existing image data and metadata. Check the HRESULT for success or failure. ```cpp ScratchImage baseImages; ... ScratchImage mipChain; hr = GenerateMipMaps3D( baseImages.GetImages(), baseImages.GetImageCount(), baseImages.GetMetadata(), TEX_FILTER_DEAFULT, 0, mipChain ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Install Texassemble with Winget Source: https://github.com/microsoft/directxtex/wiki/Texassemble Installs the Texassemble utility using the Windows Package Manager. ```bash winget install Microsoft.DirectXTex.Texassemble ``` -------------------------------- ### Install DirectXTex with OpenEXR support via vcpkg Source: https://github.com/microsoft/directxtex/wiki/Adding-OpenEXR Use this command to install the DirectXTex port with OpenEXR and tools support using the vcpkg package manager. ```bash vcpkg install directxtex[openexr,tools] ``` -------------------------------- ### Install Texdiag using winget Source: https://github.com/microsoft/directxtex/wiki/Texdiag Install the texdiag command-line utility using the Windows Package Manager. ```bash winget install Microsoft.DirectXTex.Texdiag ``` -------------------------------- ### Texassemble Cube Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Example of creating a cubemap from six individual image files. The order of faces is crucial: positive-x, negative-x, positive-y, negative-y, positive-z, negative-z. ```bash texassemble cube -o MyCube.dds +x.png -x.png +y.png -y.png +z.png -z.png ``` -------------------------------- ### Example Usage of PremultiplyAlpha Source: https://github.com/microsoft/directxtex/wiki/PremultiplyAlpha This example demonstrates how to use the PremultiplyAlpha function with a ScratchImage object. Ensure the input image data is valid before calling. ```cpp ScratchImage srcImage; ... ScratchImage destImage; hr = PremultiplyAlpha( srcImage.GetImages(), srcImage.GetImageCount(), srcImage.GetMetadata(), TEX_PMALPHA_DEFAULT, destImage ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Texassemble Volume Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Example of creating a volume map from multiple image files. The number of input images determines the depth of the volume map. ```bash texassemble volume -o MyVolume.dds image0.png image1.png image2.png ``` -------------------------------- ### Install Texconv using vcpkg Source: https://github.com/microsoft/directxtex/blob/main/skills/texture-converter/SKILL.md Use this command to install the texconv tool via the vcpkg package manager. ```bash vcpkg install directxtex[tools] ``` -------------------------------- ### Install DirectXTex with JPEG/PNG for Linux Source: https://github.com/microsoft/directxtex/wiki/Integration Installs DirectXTex with JPEG and PNG auxiliary functions for Linux using vcpkg, specifying features and triplet. ```bash ./vcpkg install directxtex[jpeg,png]:x64-linux ``` -------------------------------- ### TransformImage Chroma Key Example Source: https://github.com/microsoft/directxtex/wiki/TransformImage This example demonstrates using TransformImage to perform a chroma key transformation, setting pixels matching a specific color to transparent. ```cpp ScratchImage result; hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(), [](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t width, size_t y) { static const XMVECTORF32 s_chromaKey = { 0.f, 1.f, 0.f, 0.f }; static const XMVECTORF32 s_tolerance = { 0.2f, 0.2f, 0.2f, 0.f }; UNREFERENCED_PARAMETER(y); for (size_t j = 0; j < width; ++j) { XMVECTOR value = inPixels[j]; if (XMVector3NearEqual(value, s_chromaKey, s_tolerance)) { value = g_XMZero; } else { value = XMVectorSelect(g_XMOne, value, g_XMSelect1110); } outPixels[j] = value; } }, result); if (FAILED(hr)) ... ``` -------------------------------- ### Texassemble Cubearray Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Example of creating a cubemap array. Input must be a multiple of six images. Loading requires feature level 10.1 or better hardware if more than one cubemap is present. ```bash texassemble cubearray -o MyCubeArray.dds *.png ``` -------------------------------- ### Example Usage of ScaleMipMapsAlphaForCoverage Source: https://github.com/microsoft/directxtex/wiki/ScaleMipMapsAlphaForCoverage This example demonstrates how to generate mipmaps, initialize a scratch image for coverage mipmaps, and then iterate through array items to scale alpha values using ScaleMipMapsAlphaForCoverage. Error handling for HRESULT is crucial. ```cpp ScratchImage baseImage; ... ScratchImage mipChain; hr = GenerateMipMaps( baseImage.GetImages(), baseImage.GetImageCount(), baseImage.GetMetadata(), TEX_FILTER_DEFAULT, 0, mipChain ); if ( FAILED(hr) ) … auto& info = mipChain.GetMetadata(); ScratchImage coverageMipChain; hr = coverageMipChain.Initialize(info); if (FAILED(hr)) … for (size_t item = 0; item < info.arraySize; ++item) { auto img = mipChain.GetImage(0, item, 0); assert(img); hr = ScaleMipMapsAlphaForCoverage(img, info.mipLevels, info, item, 0.5f, coverageMipChain); if (FAILED(hr)) … } ``` -------------------------------- ### Define Public and System Interface Include Directories Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Configures include directories for the project. PUBLIC specifies directories for both build and install, while SYSTEM INTERFACE specifies directories for system-wide inclusion during installation. ```cmake target_include_directories(${PROJECT_NAME} PUBLIC $) target_include_directories(${PROJECT_NAME} SYSTEM INTERFACE $) ``` -------------------------------- ### Example of FlipRotate Usage Source: https://github.com/microsoft/directxtex/wiki/FlipRotate This example demonstrates how to use the FlipRotate function to apply a horizontal flip to an image. Ensure the HRESULT is checked for errors. ```cpp ScratchImage srcImage; ... ScratchImage destImage; hr = FlipRotate( srcImage.GetImages(), srcImage.GetImageCount(), srcImage.GetMetadata(), TEX_FR_FLIP_HORIZONTAL, destImage ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Generate Mipmap Chain Example Source: https://github.com/microsoft/directxtex/wiki/GenerateMipMaps Example demonstrating how to use the GenerateMipMaps function to create a mipmap chain from a base image. Ensure to check the HRESULT for success. ```cpp ScratchImage baseImage; ... ScratchImage mipChain; hr = GenerateMipMaps( baseImage.GetImages(), baseImage.GetImageCount(), baseImage.GetMetadata(), TEX_FILTER_DEFAULT, 0, mipChain ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Texassemble Array Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Example of creating a 1D or 2D texture array from multiple image files. Requires hardware with feature level 10.0 or better to load the resulting DDS file. ```bash texassemble array -o MyArray.dds image0.png image1.png image2.png image3.png ``` -------------------------------- ### Example of Decompress Usage Source: https://github.com/microsoft/directxtex/wiki/Decompress This example demonstrates how to call the Decompress function with a ScratchImage object. Ensure that the hr variable is checked for failure after the call. ```cpp ScratchImage bcImage; ... ScratchImage destImage; hr = Decompress( bcImage.GetImages(), bcImage.GetImageCount(), bcImage.GetMetadata(), DXGI_FORMAT_UNKNOWN, destImage ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Simple HDR File Loading Example Source: https://github.com/microsoft/directxtex/wiki/HDR-I-O-Functions Demonstrates loading an HDR image from a file using LoadFromHDRFile. The TexMetadata is optional as it's redundant for single HDR images. ```cpp auto image = std::make_unique(); HRESULT hr = LoadFromHDRFile( L"uffizi_cross.hdr", nullptr, *image ); if ( FAILED(hr) ) // error ``` -------------------------------- ### Texassemble Cube from Horizontal Tee Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Reconstructs a cubemap from a horizontal 'T' image layout. ```bash texassemble cube-from-ht -o MyReconstructedCube.dds MyHTee.bmp ``` -------------------------------- ### Install DirectXTex for Xbox Series X|S Source: https://github.com/microsoft/directxtex/wiki/Integration Installs DirectXTex for Xbox Series X|S using the 'x64-xbox-scarlett' vcpkg triplet. Requires the Microsoft GDK with Xbox Extensions. ```bash vcpkg install directxtex:x64-xbox-scarlett ``` -------------------------------- ### Texassemble Create Mipmapped Cubemap Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a cubemap texture with custom mip images. ```bash texassemble cube-from-mips -o MipmapCube.dds +x_mip0.png -x_mip0.png +y_mip0.png -y_mip0.png +z_mip0.png -z_mip0.png ``` -------------------------------- ### TransformImage Tonemap Operator Example Source: https://github.com/microsoft/directxtex/wiki/TransformImage This example shows how to apply a tonemap operator using TransformImage to adjust image brightness and contrast based on maximum luminance. ```cpp XMVECTOR maxLum = XMVectorZero(); HRESULT hr = EvaluateImage(*image.GetImage(0, 0, 0), [&](const XMVECTOR* pixels, size_t width, size_t y) { UNREFERENCED_PARAMETER(y); for (size_t j = 0; j < width; ++j) { static const XMVECTORF32 s_luminance = { 0.3f, 0.59f, 0.11f, 0.f }; XMVECTOR v = *pixels++; v = XMVector3Dot(v, s_luminance); maxLum = XMVectorMax(v, maxLum); } }); if (FAILED(hr)) ... maxLum = XMVectorMultiply(maxLum, maxLum); ScratchImage result; hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(), [](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t width, size_t y) { UNREFERENCED_PARAMETER(y); for (size_t j = 0; j < width; ++j) { XMVECTOR value = inPixels[j]; XMVECTOR scale = XMVectorDivide( XMVectorAdd(g_XMOne, XMVectorDivide(value, maxLum)), XMVectorAdd(g_XMOne, value)); XMVECTOR nvalue = XMVectorMultiply(value, scale); value = XMVectorSelect(value, nvalue, g_XMSelect1110); outPixels[j] = value; } }, result); if (FAILED(hr)) ... ``` -------------------------------- ### Texassemble Cube from Vertical Strip Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Reconstructs a cubemap from a vertical strip image layout. ```bash texassemble cube-from-vs -o MyReconstructedCube.dds MyVStrip.bmp ``` -------------------------------- ### Save Image to HDR File Example Source: https://github.com/microsoft/directxtex/wiki/HDR-I-O-Functions Shows how to save an image to an HDR file using SaveToHDRFile after loading it. ```cpp const Image* img = image->GetImage(0,0,0); assert( img ); HRESULT hr = SaveToHDRFile( *img, L"NEW_IMAGE.HDR" ); if ( FAILED(hr) ) // error ``` -------------------------------- ### Texassemble Cube from Horizontal Strip Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Reconstructs a cubemap from a horizontal strip image layout. ```bash texassemble cube-from-hs -o MyReconstructedCube.dds MyHStrip.bmp ``` -------------------------------- ### Loading Textures from Various File Formats Source: https://github.com/microsoft/directxtex/blob/main/skills/directxtex-usage/SKILL.md Provides examples for loading textures from DDS, WIC (PNG, BMP, JPEG), TGA, HDR files, and from memory buffers. ```cpp // From DDS file ScratchImage image; HRESULT hr = LoadFromDDSFile(L"texture.dds", DDS_FLAGS_NONE, nullptr, image); ``` ```cpp // From WIC file (PNG, BMP, JPEG, TIFF, etc.) — Windows only ScratchImage image; HRESULT hr = LoadFromWICFile(L"texture.png", WIC_FLAGS_NONE, nullptr, image); ``` ```cpp // From TGA file ScratchImage image; HRESULT hr = LoadFromTGAFile(L"texture.tga", TGA_FLAGS_NONE, nullptr, image); ``` ```cpp // From HDR file ScratchImage image; HRESULT hr = LoadFromHDRFile(L"texture.hdr", nullptr, image); ``` ```cpp // From memory ScratchImage image; HRESULT hr = LoadFromDDSMemory(pData, dataSize, DDS_FLAGS_NONE, nullptr, image); ``` -------------------------------- ### Resize Single Image Example Source: https://github.com/microsoft/directxtex/wiki/Resize Demonstrates resizing a single image from a 640x480 BGRA format to 320x200 using TEX_FILTER_DEFAULT. Ensure to check the HRESULT for errors. ```cpp Image image; image.width = 640; image.height = 480; image.format = DXGI_FORMAT_B8G8R8A8_UNORM; image.rowPitch = sizeof(uint32_t) * 640; image.slicePitch = sizeof(uint32_t) * 640 * 480; image.pixels = reinterpret_cast( pixelData ); ScratchImage destImage; hr = Resize( image, 320, 200, TEX_FILTER_DEFAULT, destImage ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Texassemble Horizontal Strip Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a horizontal strip image layout from an input cubemap DDS file. ```bash texassemble h-strip -o MyHStrip.bmp MyCube.dds ``` -------------------------------- ### Texassemble Create Mipmapped Texture Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a 2D mipmapped texture using custom mip images. ```bash texassemble from-mips -o MipmapTexture.dds mip0.png mip1.png mip2.png ``` -------------------------------- ### Texassemble Vertical Strip Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a vertical strip image layout from an input cubemap DDS file. ```bash texassemble v-strip -o MyVStrip.bmp MyCube.dds ``` -------------------------------- ### Texassemble Cube from Horizontal Cross Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Reconstructs a cubemap from a horizontal cross image layout. ```bash texassemble cube-from-hc -o MyReconstructedCube.dds MyHCross.bmp ``` -------------------------------- ### Resize Multiple Images Example Source: https://github.com/microsoft/directxtex/wiki/Resize Shows how to resize all images within a ScratchImage object. This is useful for batch processing image arrays, cubemaps, or mipmap chains. ```cpp ScratchImage srcImage; ... ScratchImage destImage; hr = Resize( srcImage.GetImages(), srcImage.GetImageCount(), srcImage.GetMetadata(), 100, 50, TEX_FILTER_DEFAULT, destImage ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Texassemble Horizontal Tee Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a horizontal 'T' image layout from an input cubemap DDS file. ```bash texassemble h-tee -o MyHTee.bmp MyCube.dds ``` -------------------------------- ### Get WIC Codec GUID Source: https://github.com/microsoft/directxtex/wiki/GetWICCodec Use this function to retrieve a WIC GUID for a specified image codec. This function is not supported on Linux or WSL. It is optional, and you can use the WIC container GUID directly. ```cpp REFGUID GetWICCodec( _In_ WICCodecs codec ); ``` -------------------------------- ### Print Image Metadata with texdiag Source: https://github.com/microsoft/directxtex/wiki/Texdiag Use 'texdiag info' to display metadata such as dimensions, format, and mip levels for a given image file. No special setup is required beyond having the tool installed. ```bash texdiag info reftexture.dds ``` -------------------------------- ### Basic Texture Processing with DirectXTex C++ API Source: https://github.com/microsoft/directxtex/wiki/Getting-Started A C++ "Hello, World" example demonstrating loading a PNG, generating mipmaps, compressing to BC3 format, and saving as a DDS file using the DirectXTex library. Requires COM initialization. ```cpp #include #include "DirectXTex.h" #include using namespace DirectX; int main() { HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); if (FAILED(hr)) { wprintf(L"Failed to initialize COM (%08X)\n", static_cast(hr)); return 1; } ScratchImage image; hr = LoadFromWICFile(L"texture.png", WIC_FLAGS_NONE, nullptr, image); if (FAILED(hr)) { wprintf(L"Failed to load texture.png (%08X)\n", static_cast(hr)); return 1; } ScratchImage timage; hr = GenerateMipMaps(*image.GetImage(0,0,0), TEX_FILTER_DEFAULT, 0, timage); if (FAILED(hr)) { wprintf(L"Failed to generate mipmaps (%08X)\n", static_cast(hr)); return 1; } image.Release(); hr = Compress(timage.GetImages(), timage.GetImageCount(), timage.GetMetadata(), DXGI_FORMAT_BC3_UNORM, TEX_COMPRESS_DEFAULT, TEX_THRESHOLD_DEFAULT, image); if (FAILED(hr)) { wprintf(L"Failed to compress texture (%08X)\n", static_cast(hr)); return 1; } hr = SaveToDDSFile(image.GetImages(), image.GetImageCount(), image.GetMetadata(), DDS_FLAGS_NONE, L"texture.dds"); if (FAILED(hr)) { wprintf(L"Failed to write texture to DDS (%08X)\n", static_cast(hr)); return 1; } return 0; } ``` -------------------------------- ### Load Image File Based on Extension Source: https://github.com/microsoft/directxtex/wiki/CreateTexture This example demonstrates loading an image file (DDS, TGA, HDR, or WIC) based on its file extension. It uses helper functions like LoadFromDDSFile, LoadFromTGAFile, LoadFromHDRFile, and LoadFromWICFile. ```cpp wchar_t ext[_MAX_EXT] = {}; _wsplitpath_s( filename, nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT ); ScratchImage image; HRESULT hr; if ( _wcsicmp( ext, L".dds" ) == 0 ) { hr = LoadFromDDSFile( filename, DDS_FLAGS_NONE, nullptr, image ); } else if ( _wcsicmp( ext, L".tga" ) == 0 ) { hr = LoadFromTGAFile( filename, nullptr, image ); } else if ( _wcsicmp( ext, L".hdr" ) == 0 ) { hr = LoadFromHDRFile( filename, nullptr, image ); } else { hr = LoadFromWICFile( filename, WIC_FLAGS_NONE, nullptr, image ); } ... ``` -------------------------------- ### ComputeNormalMap Example Usage Source: https://github.com/microsoft/directxtex/wiki/ComputeNormalMap Example demonstrating how to use the ComputeNormalMap function to convert a height map to a normal map with specific flags and format. ```cpp ScratchImage hmapImage; ... ScratchImage normalMap; hr = ComputeNormalMap( hmapImage.GetImage(0,0,0), CNMAP_CHANNEL_LUMINANCE | CNMAP_COMPUTE_OCCLUSION, 2.f, DXGI_FORMAT_R8G8B8A8_UNORM, normalMap ); if ( FAILED(hr) ) ... ``` -------------------------------- ### Install DirectXTex for Xbox One Source: https://github.com/microsoft/directxtex/wiki/Integration Installs DirectXTex for Xbox One using the 'x64-xbox-xboxone' vcpkg triplet. Requires the Microsoft GDK with Xbox Extensions. ```bash vcpkg install directxtex:x64-xbox-xboxone ``` -------------------------------- ### Initialize COM for UWP/WinRT Applications Source: https://github.com/microsoft/directxtex/wiki/DirectXTex For UWP apps, the C/C++ Run-Time initializes COM. For C++/WinRT, use winrt::init_apartment(). For Windows 10/11 desktop apps needing Windows Runtime functionality, RoInitializeWrapper is used. ```cpp #if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/) Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); if (FAILED(initialize)) // error #else HRESULT hr = CoInitializeEx(nullptr, COINITBASE_MULTITHREADED); if (FAILED(hr)) // error #endif ``` -------------------------------- ### Load HDR File from Windows Store App Picker Source: https://github.com/microsoft/directxtex/wiki/HDR-I-O-Functions Example for Windows Store apps using C++/CX and PPL to load an HDR file selected via a user picker. It involves copying the file to a temporary location first. ```cpp // Using C++/CX (/ZW) and the Parallel Patterns Library (PPL) create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file) { if (file) { auto tempFolder = Windows::Storage::ApplicationData::Current->TemporaryFolder; create_task(file->CopyAsync( tempFolder, file->Name, NameCollisionOption::GenerateUniqueName )).then([this](StorageFile^ tempFile) { if ( tempFile ) { HRESULT hr = LoadFromHDRFile( ..., tempFile->Path->Data(), ... ); DX::ThrowIfFailed(hr); } }); } }); ``` -------------------------------- ### Create Texture Array, Add Mips, and Compress Source: https://github.com/microsoft/directxtex/blob/main/skills/texture-assembler/reference/examples.md Assemble a texture array from multiple PNG sprites, then use texconv to compress it with BC7_UNORM_SRGB. ```plaintext texassemble array -w 1024 -h 1024 -o sprites_raw.dds sprite*.png texconv -f BC7_UNORM_SRGB -srgb -m 0 -y sprites_raw.dds ``` -------------------------------- ### Compute Maximum Luminance Example Source: https://github.com/microsoft/directxtex/wiki/EvaluateImage This example demonstrates how to use EvaluateImage to compute the maximum luminance of an input image. The callback function iterates through pixels, calculates luminance, and updates the maximum value. ```cpp XMVECTOR maxLum = XMVectorZero(); HRESULT hr = EvaluateImage(*image.GetImage(0, 0, 0), [&](const XMVECTOR* pixels, size_t width, size_t y) { UNREFERENCED_PARAMETER(y); for (size_t j = 0; j < width; ++j) { static const XMVECTORF32 s_luminance = { 0.3f, 0.59f, 0.11f, 0.f }; XMVECTOR v = *pixels++; v = XMVector3Dot(v, s_luminance); maxLum = XMVectorMax(v, maxLum); } }); if (FAILED(hr)) ... ``` -------------------------------- ### Shader Compilation Setup for DX11 Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Configures shader compilation for DX11 on Windows, setting up output directories and finding the FXC tool. Includes a check for prebuilt shaders. ```cmake if(BUILD_DX11 AND WIN32 AND (NOT (XBOX_CONSOLE_TARGET STREQUAL "durango"))) if(NOT COMPILED_SHADERS) if(USE_PREBUILT_SHADERS) message(FATAL_ERROR "ERROR: Using prebuilt shaders requires the COMPILED_SHADERS variable is set") endif() set(COMPILED_SHADERS ${CMAKE_CURRENT_BINARY_DIR}/Shaders/Compiled) file(MAKE_DIRECTORY ${COMPILED_SHADERS}) else() file(TO_CMAKE_PATH ${COMPILED_SHADERS} COMPILED_SHADERS) endif() list(APPEND LIBRARY_SOURCES ${COMPILED_SHADERS}/BC6HEncode_EncodeBlockCS.inc) if(NOT USE_PREBUILT_SHADERS) find_program(DIRECTX_FXC_TOOL FXC.EXE ``` -------------------------------- ### GetWICCodec Function Source: https://github.com/microsoft/directxtex/wiki/GetWICCodec Returns a WIC GUID for a given file container using a simple enumeration value. This function is optional; you can use the WIC container GUID directly instead. This function is not supported on Linux or Windows Subsystem for Linux. ```APIDOC ## GetWICCodec Function ### Description Returns a WIC GUID for a given file container given a simple enumeration value. This function is optional and you can instead use the WIC container GUID directly instead. ### Method C++ Function Signature ### Endpoint N/A (C++ function) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```cpp REFGUID GetWICCodec( _In_ WICCodecs codec ); ``` ### Response #### Success Response (GUID) - **GUID** (GUID) - The WIC GUID for the specified codec. #### Response Example N/A (Returns a GUID) ### Remarks The primary benefit of this function is that you do not have to include `` for every client module that calls DirectXTex's [[WIC I/O Functions]]. ### Platform notes Do not use `WIC_CODEC_WMP` for Xbox One XDK applications as the WMP / HD Photo WIC codec is not supported by that platform. ### Parameters Enumeration #### `WICCodecs` - **WIC_CODEC_BMP** (Enumeration) - Windows Bitmap (``.bmp``) - **WIC_CODEC_JPEG** (Enumeration) - Joint Photographic Experts Group (``.jpg``, ``.jpeg``) - **WIC_CODEC_PNG** (Enumeration) - Portable Network Graphics (``.png``) - **WIC_CODEC_TIFF** (Enumeration) - Tagged Image File Format (``.tif``, ``.tiff``) - **WIC_CODEC_GIF** (Enumeration) - Graphics Interchange Format (``.gif``) - **WIC_CODEC_WMP** (Enumeration) - Windows Media Photo / HD Photo / JPEG XR (``.hdp``, ``.jxr``, ``.wdp``). - **WIC_CODEC_ICO** (Enumeration) - Windows Icon (``.ico``) ### Returns Returns a `GUID` or a `GUID_NULL`. ### Exceptions This function is marked `noexcept`, and does not throw C++ exceptions. ``` -------------------------------- ### Configure pkg-config File Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Configures the DirectXTex.pc.in template file to generate the final DirectXTex.pc file. This uses CMake's configure_file command with @ONLY variable substitution. ```cmake configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/build/DirectXTex.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/DirectXTex.pc" @ONLY ) ``` -------------------------------- ### Add ddsview sample executable Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Defines the ddsview sample application, linking DirectX 11 and OLE libraries. ```cmake list(APPEND TOOL_EXES ddsview) add_executable(ddsview WIN32 DDSView/ddsview.cpp DDSView/ddsview.rc ${COMPILED_SHADERS}/ddsview_ps1D.inc) target_link_libraries(ddsview PRIVATE ${PROJECT_NAME} d3d11.lib ole32.lib) source_group(ddsview REGULAR_EXPRESSION DDSView/*.*) target_include_directories(ddsview PRIVATE ${COMPILED_SHADERS}) ``` -------------------------------- ### Typical Texture Processing Pipeline Source: https://github.com/microsoft/directxtex/blob/main/skills/directxtex-usage/SKILL.md Demonstrates a common workflow for loading, resizing, generating mipmaps, compressing, and saving textures using DirectXTex. ```cpp // 1. Load source image ScratchImage source; HRESULT hr = LoadFromWICFile(L"diffuse.png", WIC_FLAGS_NONE, nullptr, source); if (FAILED(hr)) return hr; // 2. Resize if needed ScratchImage resized; if (source.GetMetadata().width != 1024 || source.GetMetadata().height != 1024) { hr = Resize(*source.GetImage(0, 0, 0), 1024, 1024, TEX_FILTER_DEFAULT, resized); if (FAILED(hr)) return hr; } else { resized = std::move(source); } // 3. Generate mipmaps ScratchImage mipChain; hr = GenerateMipMaps(*resized.GetImage(0, 0, 0), TEX_FILTER_DEFAULT, 0, mipChain); if (FAILED(hr)) return hr; // 4. Compress to BC7 ScratchImage compressed; hr = Compress(mipChain.GetImages(), mipChain.GetImageCount(), mipChain.GetMetadata(), DXGI_FORMAT_BC7_UNORM, TEX_COMPRESS_DEFAULT, TEX_THRESHOLD_DEFAULT, compressed); if (FAILED(hr)) return hr; // 5. Save as DDS hr = SaveToDDSFile(compressed.GetImages(), compressed.GetImageCount(), compressed.GetMetadata(), DDS_FLAGS_NONE, L"diffuse.dds"); ``` -------------------------------- ### Create Volume Texture and Compress Source: https://github.com/microsoft/directxtex/blob/main/skills/texture-assembler/reference/examples.md Assemble a 3D volume texture from a series of PNG images, then compress it using BC4_UNORM format with texconv. ```plaintext texassemble volume -w 64 -h 64 -o noise3d_raw.dds noise*.png texconv -f BC4_UNORM -m 0 -y noise3d_raw.dds ``` -------------------------------- ### Get WIC Factory Source: https://github.com/microsoft/directxtex/wiki/WICFactory Retrieves the WIC imaging factory. The factory is created on-demand if it hasn't been set manually. ```APIDOC ## GetWICFactory ### Description Retrieves the WIC imaging factory. This object is normally created on-demand the first time this function is called. ### Method `IWICImagingFactory* GetWICFactory(bool& iswic2);` ### Remarks This function is not supported on Linux or Windows Subsystem for Linux. ### Returns `GetWICFactory` returns a pointer to the factory interface, or a nullptr on failure. ### Exceptions This function is marked `noexcept`, and does not throw C++ exceptions. ### Threading The `GetWICFactory` function is thread-safe. ### Cleanup To ensure the on-demand WIC factory object is released for a 'clean exit', you can use `SetWICFactory(nullptr);`. ``` -------------------------------- ### Get Metadata from WIC File Source: https://github.com/microsoft/directxtex/wiki/WIC-I-O-Functions Retrieves TexMetadata from a WIC-supported bitmap file. Supports C++17 with std::byte. ```cpp HRESULT GetMetadataFromWICFile( const wchar_t* szFile, WIC_FLAGS flags, TexMetadata& metadata, std::function getMQR = nullptr ); ``` -------------------------------- ### Get Metadata from WIC Memory Source: https://github.com/microsoft/directxtex/wiki/WIC-I-O-Functions Retrieves TexMetadata from a WIC-supported bitmap in memory. Supports C++17 with std::byte. ```cpp HRESULT GetMetadataFromWICMemory( const void* pSource, size_t size, WIC_FLAGS flags, TexMetadata& metadata, std::function getMQR = nullptr ); ``` ```cpp HRESULT GetMetadataFromWICMemory( const std::byte* pSource, size_t size, WIC_FLAGS flags, TexMetadata& metadata, std::function getMQR = nullptr ); ``` -------------------------------- ### Display built tools Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Prints a status message listing all the tools that are being built. ```cmake if(TOOL_EXES) message(STATUS "Building tools: ${TOOL_EXES}") endif() ``` -------------------------------- ### Texassemble GIF to Texture Array Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Converts an animated GIF into a texture array, ensuring frames are properly composed. ```bash texassemble gif -o MyAnimatedTexture.dds animation.gif ``` -------------------------------- ### Integration with Visual Studio Source: https://github.com/microsoft/directxtex/wiki/DirectXTexXbox Instructions on how to integrate the DirectXTex Xbox extension projects into a Visual Studio solution. ```APIDOC ## Integrating DirectXTex Xbox Extensions into Visual Studio ### Using Project-to-Project References 1. In your application's Visual Studio solution, right-click on the Solution. 2. Select "Add" -> "Existing Project..." 3. Navigate to and select the appropriate DirectXTex `.vcxproj` file for your project: * `DirectXTex_GDK_2022`: For Gaming.Xbox. extit{.x64 platforms. * `DirectXTex_GXDK_PC_2022`: For PC-hosted content tools in Microsoft GDKX development. ``` -------------------------------- ### Integration with vcpkg Source: https://github.com/microsoft/directxtex/wiki/DirectXTexXbox Instructions on how to use the vcpkg C++ library manager to include DirectXTex Xbox extensions. ```APIDOC ## Integrating DirectXTex Xbox Extensions with vcpkg When using the [vcpkg](https://github.com/microsoft/vcpkg) C++ Library Manager, the `xbox` feature can be enabled to include the DirectXTex Xbox extension functionality, provided the *Microsoft GDK with Xbox Extensions* is available. ``` -------------------------------- ### Enable libpng Support Source: https://github.com/microsoft/directxtex/blob/main/CMakeLists.txt Finds the PNG library and links it to the project if libpng support is enabled. ```cmake find_package(PNG REQUIRED) target_link_libraries(${PROJECT_NAME} PUBLIC PNG::PNG) ``` -------------------------------- ### Texassemble Horizontal Cross Command Example Source: https://github.com/microsoft/directxtex/wiki/Texassemble Creates a horizontal cross image layout from an input cubemap DDS file. ```bash texassemble h-cross -o MyHCross.bmp MyCube.dds ``` -------------------------------- ### Get TGA Metadata from File Source: https://github.com/microsoft/directxtex/wiki/TGA-I-O-Functions Retrieves TexMetadata from a TGA file on disk. Requires TGA_FLAGS for specific parsing behavior. ```cpp HRESULT GetMetadataFromTGAFile( const wchar_t* szFile, TGA_FLAGS flags, TexMetadata& metadata ); ``` -------------------------------- ### Example: Calculate Maximum Mip Levels for 3D Texture Source: https://github.com/microsoft/directxtex/wiki/CalculateMipLevels Demonstrates calculating the maximum mipmap levels for a 64x64x32 volume texture. The function returns true if successful, and mipLevels will contain the calculated count. ```cpp // Calculate maximum mipmap levels for a 64x64x32 volume texture mipLevels = 0; if (CalculateMipLevels3D(64, 64, 32, mipLevels)) { // mipLevels will be 7 (64×64×32->32×32×16->16×16×8->8×8×4->4×4×2->2×2×1->1×1×1) } ``` -------------------------------- ### Get TGA Metadata from Memory Source: https://github.com/microsoft/directxtex/wiki/TGA-I-O-Functions Retrieves TexMetadata from TGA data in memory. Requires TGA_FLAGS for specific parsing behavior. ```cpp HRESULT GetMetadataFromTGAMemory( const void* pSource, size_t size, TGA_FLAGS flags, TexMetadata& metadata ); ``` ```cpp HRESULT GetMetadataFromTGAMemory( const std::byte* pSource, size_t size, TGA_FLAGS flags, TexMetadata& metadata); ```