Quantcast
Channel: DirectXTex texture processing library
Viewing all 1174 articles
Browse latest View live

Updated Wiki: TGA I/O Functions

$
0
0
The Targa Truvision (.TGA) format is commonly used as a texture source file format in game development, but this format is not supported by any built-in WIC codec. These functions implement a simple reader and writer for this format.

http://en.wikipedia.org/wiki/Truevision_TGA

GetMetadataFromTGAMemory
GetMetadataFromTGAFile
Returns the TexMetadata from a .TGA file.

HRESULT GetMetadataFromTGAMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size,
    _Out_ TexMetadata& metadata );

HRESULT GetMetadataFromTGAFile( _In_z_ LPCWSTR szFile,
    _Out_ TexMetadata& metadata );

LoadFromTGAMemory
LoadFromTGAFile
Loads a .TGA file.

HRESULT LoadFromTGAMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

HRESULT LoadFromTGAFile( _In_z_ LPCWSTR szFile,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

SaveToTGAMemory
SaveToTGAFile
Saves an image to a .TGA file.
  • R8G8B8A8_UNORM, R8G8B8A8_UNORM_SRGB, B8G8R8A8_UNORM, and B8G8R8A8_UNORM_SRGB data are written as a 32-bit truecolor uncompressed .TGA file
  • B8G8R8X8_UNORM and B8G8R8X8_UNORM_SRGB data is written as a 24-bit truecolor uncompressed .TGA file
  • B5G5R5A1_UNORM data is written as a 16-bit truecolor uncompressed .TGA file
  • R8_UNORM and A8_UNORM data is written as an 8-bit uncompressed greyscale .TGA file
HRESULT SaveToTGAMemory( _In_ const Image& image, _Out_ Blob& blob );

HRESULT SaveToTGAFile( _In_ const Image& image, _In_z_ LPCWSTR szFile );

Examples

This is a simple loading example. The TGA format cannot contain complicated multi-image formats, so the TexMetadata info is redundant information.

unique_ptr<ScratchImage> image ( new ScratchImage );
HRESULT hr = LoadFromTGAFile( L"ROCKS.TGA", nullptr, *image );
if ( FAILED(hr) )
    // error

A TGA file can only store one 2D image.

const Image* img = image->GetImage(0,0,0);
assert( img );
HRESULT hr = SaveToTGAFile( *img, L"NEW_IMAGE.TGA" );
if ( FAILED(hr) )
    // error

You can also save data directly from memory without using the intermediate ScratchImage at all. This example assumes a single 2D image is being written out.

Image img;
img.width = /*<width of pixel data>*/;
img.height = /*<height of pixel data>*/;
img.format = /*<a DXGI format from the supported list above>*/;
img.rowPitch = /*<number of bytes in a scanline of the source data>*/;
img.slicePitch = /*<number of bytes in the entire 2D image>*/;
img.pixels = /*<pointer to pixel data>*/;
HRESULT hr = SaveToTGAFile( img, L"NEW_IMAGE.TGA" );
if ( FAILED(hr) )
    // error

Implementation Notes

  • The reader does not support .TGA files that contain color maps (which are rare in practice)
  • The reader does not support interleaved files (this feature was deprecated)
  • The reader only supports 8-bit greyscale, 16-bit truecolor, 24-bit truecolor, and 32-bit truecolor images
  • The writer always creates uncompressed files, although the reader can load RLE compressed files
  • The reader and writer do not support the TGA header extension metadata, which is ignored by the reader.
  • For 16-bit and 32-bit truecolor images, there is a special-case fixup if the entire alpha channel is 0 it is assumed to be fully opaque.

Windows Store apps

Load

If you wish to load an image from a file that is specified by the user from a WinRT picker, you will need to copy the file locally to a temporary location before you can use LoadFromTGAFile on it. This is because you either won't have file access rights to the user's file location, or the StorageFile is actually not a local file system path (i.e. it's a URL).

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 = LoadFromTGAFile( ..., tempFile->Path->Data(), ... );
                DX::ThrowIfFailed(hr);
            }
        });
    });

http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

Save

For SaveToTGAFile to succeed, the application must have write access to the destination path. For Windows Store apps, the file access permissions are rather restricted so you'll need to make sure you use a fully qualified path to a valid write folder. A good location to use is the app data folder:

auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
// use folder->Path->Data() as the path base

http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

If you are going to immediately copy it to another location via StorageFolder, then use the app's temporary folder:

auto folder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
// use folder->Path->Data() as the path base

Updated Wiki: DDS I/O Functions

$
0
0
These functions perform file I/O for .DDS files. These functions support many legacy Direct3D 9 .DDS files and all Direct3D 10.x/11.x era "DX10" extension .DDS files

GetMetadataFromDDSMemory
GetMetadataFromDDSFile
Returns the TexMetadata from a .DDS file.

HRESULT GetMetadataFromDDSMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size, _In_ DWORD flags,
    _Out_ TexMetadata& metadata );

HRESULT GetMetadataFromDDSFile( _In_z_ LPCWSTR szFile,
    _In_ DWORD flags, _Out_ TexMetadata& metadata );

LoadFromDDSMemory
LoadFromDDSFile
Loads a .DDS file.

HRESULT LoadFromDDSMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size, _In_ DWORD flags,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

HRESULT LoadFromDDSFile( _In_z_ LPCWSTR szFile,
    _In_ DWORD flags,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

SaveToDDSMemory
SaveToDDSFile
Saves a single image or a set of images to a .DDS file.

HRESULT SaveToDDSMemory( _In_ const Image& image, _In_ DWORD flags,
    _Out_ Blob& blob );

HRESULT SaveToDDSMemory( _In_reads_(nimages) const Image* images,
    _In_ size_t nimages, _In_ const TexMetadata& metadata, _In_ DWORD flags,
    _Out_ Blob& blob );

HRESULT SaveToDDSFile( _In_ const Image& image, _In_ DWORD flags
     _In_z_ LPCWSTR szFile );

HRESULT SaveToDDSFile( _In_reads_(nimages) const Image* images, _In_ size_t nimages,
    _In_ const TexMetadata& metadata, _In_ DWORD flags, _In_z_ LPCWSTR szFile );

Examples

This is a simple loading example. A DDS file can potentially include any kind of Direct3D resource in any DXGI format, so the TexMetadata info is needed to understand the full content of the file.

TexMetadata info;
unique_ptr<ScratchImage> image ( new ScratchImage );
HRESULT hr = LoadFromDDSFile( L"TEXTURE.DDS", DDS_FLAGS_NONE, &info, *image );
if ( FAILED(hr) )
    // error

When saving a DDS file, it can contain one or more images (mipmaps, arrays, volumes, cubemaps, etc.).
Therefore, the writer needs the TexMetadata info to know how to interpret the image set.

const Image* img = image->GetImages();
assert( img );
size_t nimg = image->GetImageCount();
assert( nimg > 0 );
HRESULT hr = SaveToDDSFile( img, nimg, image->GetMetadata(),
     DDS_FLAGS_NONE, L"NEW_TEXTURE.DDS" );
if ( FAILED(hr) )
    // error

You can also save data directly from memory without using the intermediate ScratchImage at all. This example assumes a single 2D image is being written out.

Image img;
img.width = /*<width of pixel data>*/;
img.height = /*<height of pixel data>*/;
img.format = /*<any DXGI format>*/;
img.rowPitch = /*<number of bytes in a scanline of the source data>*/;
img.slicePitch = /*<number of bytes in the entire 2D image>*/;
img.pixels = /*<pointer to pixel data>*/;
HRESULT hr = SaveToDDSFile( img, DDS_FLAGS_NONE, L"NEW_TEXTURE.DDS" );
if ( FAILED(hr) )
    // error

Related Flags

  • DDS_FLAGS_NONE is the default
  • DDS_FLAGS_LEGACY_DWORD is used for loading some legacy Direct3D 8 era 24bpp .DDS files that use the non-standard DWORD alignment instead of BYTE. There's no realiable way to determine this from the file, so this requires trial-and-error.
  • DDS_FLAGS_NO_LEGACY_EXPANSION - By default the loader will expand many legacy Direct3D 9 .DDS files to supported formats. The use of this flag prevents expansions that increase the size of the pixels, and will return a failure instead.
  • DDS_FLAGS_NO_R10B10G10A2_FIXUP - By default, the loader uses a work-around for a long-standing issue with the D3DX DDS file format which reverses the RGB bit-masks for 10:10:10:2 formats. If this flag is used, then the loader instead assumes such data was written 'correctly'.
  • DDS_FLAGS_FORCE_RGB - By default we map many BGR formats directly to DXGI 1.1 formats. Use of this flag forces the use of DXGI 1.0 RGB formats instead for improved Direct3D 10.0/Windows Vista RTM/WDDM 1.0 driver support.
  • DDS_FLAGS_NO_16BPP - By default, 5:6:5, 5:5:5:1, and 4:4:4:4 formats are returned as DXGI 1.2 formats. If this flag is used, the loader will expand these to R8G8B8A8 instead.
  • DDS_FLAGS_EXPAND_LUMINANCE - By default, legacy luminance formats are mapped to the same size formats in DXGI (L8 -> R8_UNORM, L16 -> R16_UNORM, A8L8 -> R8G8_UNORM), but this requires some shader swizzling to replicate the original luminance greyscale behavior (.rrr or .rrrg)--this matches the implementation of DDSTextureLoader. Specifying this flag will instead expand these formats on load and replicate the colors to achieve the proper greyscale without any shader changes, but they will be significantly larger (8:8:8:8 or 16:16:16:16).
  • DDS_FLAGS_FORCE_DX10_EXT - When saving DDS files, the writer tries to use legacy Direct3D 9 .DDS file formats if possible rather than the 'DX10' header extension for better compatiblity with older tools. Using this flag, the writer will always generate 'DX10' extension header files which are much faster to parse at load-time. These files are compatible with the legacy D3DX10 or D3DX11 library.
  • DDS_FLAGS_FORCE_DX10_EXT_MISC2 - When saving DDS files, always use the 'DX10' header extension and write miscFlags2 data as needed, if even if the resulting file is not compatible with the legacy D3DX10 or D3DX11 libraries.

Remarks

Many older Direct3D 9 formats that do not directly map to DXGI formats are converted at load-time. For example, D3DFMT_R8G8B8 is always converted to DXGI_FORMAT_R8G8B8A8_UNORM since there is no 24bpp DXGI format.

The 'DX10' header is not supported by D3DX9, older Direct3D 9 era texture tools, or the legacy DirectX SDK DXTex.exe tool. Therefore, the default behavior is to prefer to use 'legacy' pixel formats when possible.

Note that for Direct3D 11.1 video formats, only DXGI_FORMAT_YUY2 is supported by Direct3D 9. Legacy DDS files containing FourCC "UYVY" are converted to YUY2 at load-time.

Windows Store apps

Load

If you wish to load a texture from a file that is specified by the user from a WinRT picker, you will need to copy the file locally to a temporary location before you can use LoadFromDDSFile on it. This is because you either won't have file access rights to the user's file location, or the StorageFile is actually not a local file system path (i.e. it's a URL).

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 = LoadFromDDSFile( ..., tempFile->Path->Data(), ... );
                DX::ThrowIfFailed(hr);
            }
        });
    });

http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

Save

For SaveToDDSFile to succeed, the application must have write access to the destination path. For Windows Store apps, the file access permissions are rather restricted so you'll need to make sure you use a fully qualified path to a valid write folder. A good location to use is the app data folder:

auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
// use folder->Path->Data() as the path base

If you are going to immediately copy it to another location via StorageFolder, then use the app's temporary folder:

auto folder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
// use folder->Path->Data() as the path base

http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

Updated Wiki: WIC I/O Functions

$
0
0
These functions use the Windows Imaging Componet (WIC) to read or write an image file. There are built-in WIC codecs in Windows for .BMP, .PNG, .GIF, .TIFF, .JPEG, and JPEG-XR / HD Photo images. Some containers (.GIF and .TIFF) can contain multi-frame bitmaps files.

GetMetadataFromWICMemory
GetMetadataFromWICFile
Returns the TexMetadata from a WIC-supported bitmap file.

HRESULT GetMetadataFromWICMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size, _In_ DWORD flags,
    _Out_ TexMetadata& metadata );

HRESULT GetMetadataFromWICFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
    _Out_ TexMetadata& metadata );

LoadFromWICMemory
LoadFromWICFile
Loads a WIC-supported bitmap file.

HRESULT LoadFromWICMemory( _In_reads_bytes_(size) LPCVOID pSource,
    _In_ size_t size, _In_ DWORD flags,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

HRESULT LoadFromWICFile( _In_z_ LPCWSTR szFile, _In_ DWORD flags,
    _Out_opt_ TexMetadata* metadata, _Out_ ScratchImage& image );

SaveToWICMemory
SaveToWICFile
Saves a single image or a set of images to a WIC-supported bitmap file. The caller provides the desired WIC container format to use via guidContainerFormat (see Utility functions GetWICCodec for a helper). There is an optional targetFormat to specify a desired WIC pixel format (which will result in an E_FAIL if not supported by the WIC codec)

HRESULT SaveToWICMemory( _In_ const Image& image, _In_ DWORD flags,
    _In_ REFGUID guidContainerFormat,
    _Out_ Blob& blob, _In_opt_ const GUID* targetFormat = nullptr,
    _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );

HRESULT SaveToWICMemory( _In_count_(nimages) const Image* images,
    _In_ size_t nimages, _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
    _Out_ Blob& blob, _In_opt_ const GUID* targetFormat = nullptr,
    _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );

HRESULT SaveToWICFile( _In_ const Image& image, _In_ DWORD flags,
    _In_ REFGUID guidContainerFormat,
    _In_z_ LPCWSTR szFile, _In_opt_ const GUID* targetFormat = nullptr,
    _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );

HRESULT SaveToWICFile( _In_count_(nimages) const Image* images, _In_ size_t nimages,
    _In_ DWORD flags, _In_ REFGUID guidContainerFormat,
    _In_z_ LPCWSTR szFile, _In_opt_ const GUID* targetFormat = nullptr,
    _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );

Examples

This is a simple loading example. Since it only returns a single 2D image, the TexMetadata info is redundant information.

unique_ptr<ScratchImage> image ( new ScratchImage );
HRESULT hr = LoadFromWICFile( L"WINLOGO.BMP", WIC_FLAGS_NONE, nullptr, *image );
if ( FAILED(hr) )
    // error

This is a multi-image loading example which can load an array of 2D images.

TexMetadata info;
unique_ptr<ScratchImage> image ( new ScratchImage );
HRESULT hr = LoadFromDDSFile( L"MULTIFRAME.TIF", WIC_FLAGS_ALL_FRAMES, &info, *image );
if ( FAILED(hr) )
    // error

This is saving a simple 2D image to a specific file container. You can either use the WIC GUID directly or make use of the GetWICCodec helper. Keep in mind that WIC may convert the pixel format in the final output image, so there is an optional additional parameter you can use to request a specific storage pixel format. In this case, we want the file's pixel format to be an 8-bit per channel RGB without an alpha channel.

const Image* img = image->GetImage(0,0,0);
assert( img );
HRESULT hr = SaveToWICFile( *img, WIC_FLAGS_NONE,
    GUID_ContainerFormatPng, L"NEW_IMAGE.PNG", &GUID_WICPixelFormat24bppBGR );
if ( FAILED(hr) )
    // error

You can also save data directly from memory without using the intermediate ScratchImage at all. This example assumes a single 2D image is being written out since a JPG file cannot contain an image array.

Image img;
img.width = /*<width of pixel data>*/;
img.height = /*<height of pixel data>*/;
img.format = /*<a DXGI format that maps directly to a WIC supported format>*/;
img.rowPitch = /*<number of bytes in a scanline of the source data>*/;
img.slicePitch = /*<number of bytes in the entire 2D image>*/;
img.pixels = /*<pointer to pixel data>*/;
HRESULT hr = SaveToWICFile( img, WIC_FLAGS_NONE, GetWICCodec(WIC_CODEC_JPEG), L"NEW_IMAGE.PNG" );
if ( FAILED(hr) )
    // error

When writing WIC files, you can also provide a callback for setting specific encoding options.

const Image* img = image->GetImage(0,0,0);
assert( img );
HRESULT hr = SaveToWICFile( *img, WIC_FLAGS_NONE,
    GUID_ContainerFormatTiff, L"NEW_IMAGE.TIF", nullptr, 
    [&](IPropertyBag2* props)
    {
        PROPBAG2 options[2] = { 0, 0 };
        options[0].pstrName = L"CompressionQuality";
        options[1].pstrName = L"TiffCompressionMethod";

        VARIANT varValues[2];
        varValues[0].vt = VT_R4;
        varValues[0].fltVal = 0.75f;

        varValues[1].vt = VT_UI1;
        varValues[1].bVal = WICTiffCompressionNone;

        (void)props->Write( 2, options, varValues ); 
    });
if ( FAILED(hr) )
    // error

Related Flags

  • WIC_FLAGS_NONE Default flags.
  • WIC_FLAGS_FORCE_RGB By default we map many BGR formats directly to DXGI 1.1 formats. Use of this flag forces the use of DXGI 1.0 RGB formats instead for improved Direct3D 10.0/Windows Vista RTM/WDDM 1.0 driver support.
  • WIC_FLAGS_NO_X2_BIAS By default GUID_WICPixelFormat32bppRGBA1010102XR is loaded as R10G10B10_XR_BIAS_A2_UNORM. Use of this flag will force it to convert to R10G10B10A2_UNORM instead.
  • WIC_FLAGS_NO_16BPP By default, 5:6:5 and 5:5:5:1 formats are returned as DXGI 1.2 formats. If this flag is used, the loader will expand these to R8G8B8A8 instead.
  • WIC_FLAGS_ALLOW_MONO By default, monochrome data is converted to greyscale. By using this flag, this data is loaded as R1_UNORM which is not supported for rendering by Direct3D.
  • WIC_FLAGS_ALL_FRAMES By default, only the first frame of a multi-frame file is loaded. If this flag is provided, all frames are loaded and resized to match the size of the first image to fit the DirectXTex requirements for a 2D array.
  • WIC_FLAGS_IGNORE_SRGB While there is no explicit 'sRGB' pixel format defined for WIC, the load function will check for known metadata tags and may return DXGI_FORMAT_*_SRGB formats if there are equivalents of the same size and channel configuration available. If this flag is specified, any 'sRGB' metadata ignored instead.

These flags control the use of dithering for image conversions. It defaults to 'no' dithering.
  • WIC_FLAGS_DITHER WIC will use 4x4 ordered dithering.
  • WIC_FLAGS_DITHER_DIFFUSION WIC will use error-diffusion (Floyd-Steinberg dithering).

These flags control the use of interpolation modes for image conversions/resizing. It defaults to "Fant"
  • WIC_FLAGS_FILTER_POINT Nearest-neighbor
  • WIC_FLAGS_FILTER_LINEAR - Bilinear interpolation
  • WIC_FLAGS_FILTER_CUBIC - Bicubic interpolation
  • WIC_FLAGS_FILTER_FANT - Fant which is equivalent to 'box' filteirng for down-scaling.

Release Notes

  • JPEG-XR / HD Photo supports nearly all WIC pixel formats including floating-point for both encoding and decoding.
  • TIFF can contain floating-point data (128bpp or 96bpp), but the WIC built-in codec can only decode such images. It always converts floating-point data to unorm when encoding. Windows 7 incorrectly handles decoding 96bpp TIFF files, which is corrected with WIC2 by returning the new format GUID_WICPixelFormat96bppRGBFloat
  • Windows WIC codec for .BMP files does not support alpha channels for 16-bit files. For 32-bit files, the alpha channel is ignored by Windows 7 or earlier. The WIC2 BMP codec can read 32-bit alpha channels if using the BITMAPV5HEADER header. DirectXTex opts into the WIC2 behavior for writing 32-bit alpha channels using the V5 header when available
  • GUID_WICPixelFormat32bppRGBE is an 8:8:8:8 format, which does not match DXGI_FORMAT_R9G9B9E5_SHAREDEXP. This WIC pixel format is therefore converted to GUID_WICPixelFormat128bppRGBAFloat and returns as DXGI_FORMAT_R32G32B32A32_FLOAT.
  • Paletted WIC formats are not supported for writing by the SaveToWIC functions.

WIC2

WIC2 is available on Windows 8 and on Windows 7 Service Pack 1 with KB 2670838 installed.
  • The WIC2 pixel format GUID_WICPixelFormat96bppRGBFloat loads as DXGI_FORMAT_R32G32B32_FLOAT. Otherwise it converts this to DXGI_FORMAT_R32G32B32A32_FLOAT.
  • Conversions cases for WIC2 pixel formats GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat64bppRGB, and GUID_WICPixelFormat64bppPRGBAHalf are included. The pixel format GUID_WICPixelFormat96bppRGBFixedPoint is converted to DXGI_FORMAT_R32G32B32_FLOAT rather than DXGI_FORMAT_R32G32B32A32_FLOAT
http://support.microsoft.com/kb/2670838

Windows Store apps

Load

If you wish to load an image from a file that is specified by the user from a WinRT picker, you will need to copy the file locally to a temporary location before you can use LoadFromWICFile on it. This is because you either won't have file access rights to the user's file location, or the StorageFile is actually not a local file system path (i.e. it's a URL).

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 = LoadFromWICFile( ..., tempFile->Path->Data(), ... );
                DX::ThrowIfFailed(hr);
            }
        });
    });

http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

Save

For SaveToWICFile to succeed, the application must have write access to the destination path. For Windows Store apps, the file access permissions are rather restricted so you'll need to make sure you use a fully qualified path to a valid write folder. A good location to use is the app data folder:

auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
// use folder->Path->Data() as the path base

If you are going to immediately copy it to another location via StorageFolder, then use the app's temporary folder:

auto folder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
// use folder->Path->Data() as the path base

http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

Updated Wiki: CreateTexture

$
0
0
Creates a Direct3D 11 resource from a set of images.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using DDSTextureLoader, and/or WICTextureLoader instead of DirectXTex.

HRESULT CreateTexture( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _Outptr_ ID3D11Resource** ppResource );

HRESULT CreateTextureEx( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_ ID3D11Resource** ppResource );

Parameters

  • usage: The non-Ex version of this function assumes this is D3D11_USAGE_DEFAULT.
  • bindFlags: The non-Ex version of this function assumes this is D3D11_BIND_SHADER_RESOURCE.
  • cpuAccessFlags: The non-Ex version of this function assumes this is 0.
  • miscFlags: The non-Ex version of this function assumes this is 0.
  • forceSRGB: This is for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format.

Example

WCHAR 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 );
}
elseif ( _wcsicmp( ext, L".tga" ) == 0 )
{
    hr = LoadFromTGAFile( filename, nullptr, image );
}
else
{
    hr = LoadFromWICFile( filename, WIC_FLAGS_NONE, nullptr, image );
}
if ( SUCCEEDED(hr) )
{
    ID3D11Resource* pResource = nullptr;
    hr = CreateTexture( device,
        image.GetImages(), image.GetImageCount(),
        image.GetMetadata(), &pResource );

Remark

This function does not provide support for auto-gen mipmaps (the runtime/engine loaders can support this) because the assumption is if you need mipmaps with DirectTex you will call GenerateMipMaps or GenerateMipMaps3D

Updated Wiki: CreateShaderResourceView

$
0
0
Creates a Direct3D 11 resource and shader resource view from a set of images.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using DDSTextureLoader, and/or WICTextureLoader instead of DirectXTex.

HRESULT CreateShaderResourceView( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _Outptr_ ID3D11ShaderResourceView** ppSRV );

HRESULT CreateShaderResourceViewEx( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_ ID3D11ShaderResourceView** ppSRV );

Parameters

  • usage: The non-Ex version of this function assumes this is D3D11_USAGE_DEFAULT.
  • bindFlags: The non-Ex version of this function assumes this is D3D11_BIND_SHADER_RESOURCE.
  • cpuAccessFlags: The non-Ex version of this function assumes this is 0.
  • miscFlags: The non-Ex version of this function assumes this is 0.
  • forceSRGB: This is for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format.

Example

WCHAR 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 );
}
elseif ( _wcsicmp( ext, L".tga" ) == 0 )
{
    hr = LoadFromTGAFile( 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(),
        metadata.GetMetadata(), &pSRV );

Remark

This function does not provide support for auto-gen mipmaps (the runtime/engine loaders can support this) because the assumption is if you need mipmaps with DirectTex you will call GenerateMipMaps or GenerateMipMaps3D

Updated Wiki: CaptureTexture

$
0
0
Captures a Direct3D 11 render target and returns an image.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using ScreenGrab instead of DirectXTex.

HRESULT CaptureTexture( _In_ ID3D11Device* pDevice,
    _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _Out_ ScratchImage& result );

Example

ScratchImage image;

HRESULT hr = CaptureTexture( device, context, pResource, image );
if ( SUCCEEDED(hr) )
{
    hr = SaveToDDSFile( image.GetImages(), image.GetImageCount(), image.GetMetadata(), 
        DDS_FLAGS_NONE, filename );
    if ( FAILED(hr) )
    {
        // ...

Updated Wiki: CreateTexture

$
0
0
Creates a Direct3D 11 resource from a set of images.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using DDSTextureLoader, and/or WICTextureLoader instead of DirectXTex.

HRESULT CreateTexture( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _Outptr_ ID3D11Resource** ppResource );

HRESULT CreateTextureEx( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_ ID3D11Resource** ppResource );

Parameters

  • usage: The non-Ex version of this function assumes this is D3D11_USAGE_DEFAULT.
  • bindFlags: The non-Ex version of this function assumes this is D3D11_BIND_SHADER_RESOURCE.
  • cpuAccessFlags: The non-Ex version of this function assumes this is 0.
  • miscFlags: The non-Ex version of this function assumes this is 0.
  • forceSRGB: This is for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format.

Example

WCHAR 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 );
}
elseif ( _wcsicmp( ext, L".tga" ) == 0 )
{
    hr = LoadFromTGAFile( filename, nullptr, image );
}
else
{
    hr = LoadFromWICFile( filename, WIC_FLAGS_NONE, nullptr, image );
}
if ( SUCCEEDED(hr) )
{
    ID3D11Resource* pResource = nullptr;
    hr = CreateTexture( device,
        image.GetImages(), image.GetImageCount(),
        image.GetMetadata(), &pResource );
    if ( FAILED(hr) )
    {
        ...

Remark

This function does not provide support for auto-gen mipmaps (the runtime/engine loaders can support this) because the assumption is if you need mipmaps with DirectTex you will call GenerateMipMaps or GenerateMipMaps3D

Updated Wiki: CreateShaderResourceView

$
0
0
Creates a Direct3D 11 resource and shader resource view from a set of images.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using DDSTextureLoader, and/or WICTextureLoader instead of DirectXTex.

HRESULT CreateShaderResourceView( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _Outptr_ ID3D11ShaderResourceView** ppSRV );

HRESULT CreateShaderResourceViewEx( _In_ ID3D11Device* pDevice,
    _In_reads_(nimages) const Image* srcImages, _In_ size_t nimages,
    _In_ const TexMetadata& metadata,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_ ID3D11ShaderResourceView** ppSRV );

Parameters

  • usage: The non-Ex version of this function assumes this is D3D11_USAGE_DEFAULT.
  • bindFlags: The non-Ex version of this function assumes this is D3D11_BIND_SHADER_RESOURCE.
  • cpuAccessFlags: The non-Ex version of this function assumes this is 0.
  • miscFlags: The non-Ex version of this function assumes this is 0.
  • forceSRGB: This is for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format.

Example

WCHAR 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 );
}
elseif ( _wcsicmp( ext, L".tga" ) == 0 )
{
    hr = LoadFromTGAFile( 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(),
        metadata.GetMetadata(), &pSRV );
    if ( FAILED(hr) )
    {
        ...

Remark

This function does not provide support for auto-gen mipmaps (the runtime/engine loaders can support this) because the assumption is if you need mipmaps with DirectTex you will call GenerateMipMaps or GenerateMipMaps3D

Updated Wiki: CaptureTexture

$
0
0
Captures a Direct3D 11 render target and returns an image.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using ScreenGrab instead of DirectXTex.

HRESULT CaptureTexture( _In_ ID3D11Device* pDevice,
    _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _Out_ ScratchImage& result );

Example

ScratchImage image;

HRESULT hr = CaptureTexture( device, context, pResource, image );
if ( SUCCEEDED(hr) )
{
    hr = SaveToDDSFile( image.GetImages(), image.GetImageCount(), image.GetMetadata(), 
        DDS_FLAGS_NONE, filename );
    if ( FAILED(hr) )
    {
        ...

Updated Wiki: CaptureTexture

$
0
0
Captures a Direct3D 11 render target and returns an image.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using ScreenGrab instead of DirectXTex.

HRESULT CaptureTexture( _In_ ID3D11Device* pDevice,
    _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _Out_ ScratchImage& result );

Example

ScratchImage image;
HRESULT hr = CaptureTexture( device, context, pResource, image );
if ( SUCCEEDED(hr) )
{
    hr = SaveToDDSFile( image.GetImages(), image.GetImageCount(), image.GetMetadata(), 
        DDS_FLAGS_NONE, filename );
    if ( FAILED(hr) )
    {
        ...

Updated Wiki: CaptureTexture

$
0
0
Captures a Direct3D 11 render target and returns an image.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using ScreenGrab instead of DirectXTex.

HRESULT CaptureTexture( _In_ ID3D11Device* pDevice,
    _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _Out_ ScratchImage& result );

Example

ScratchImage image;
HRESULT hr = CaptureTexture( device, context, pResource, image );
if ( SUCCEEDED(hr) )
{
    hr = SaveToDDSFile( image.GetImages(), image.GetImageCount(), image.GetMetadata(), 
        DDS_FLAGS_NONE, filename );
    if ( FAILED(hr) )
    {
        ...

Remarks

This function can support capturing 1D, 1D array, 2D, 2D array, and 3D textures in the full range of DXGI formats.

Updated Wiki: CaptureTexture

$
0
0
Captures a Direct3D 11 render target and returns an image.

This function is intended for use with tools or editor programs. For runtime/engine use, we strongly recommend using ScreenGrab instead of DirectXTex.

HRESULT CaptureTexture( _In_ ID3D11Device* pDevice,
    _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _Out_ ScratchImage& result );

Example

ScratchImage image;
HRESULT hr = CaptureTexture( device, context, pResource, image );
if ( SUCCEEDED(hr) )
{
    hr = SaveToDDSFile( image.GetImages(), image.GetImageCount(), image.GetMetadata(), 
        DDS_FLAGS_NONE, filename );
    if ( FAILED(hr) )
    {
        ...

Remarks

This function can support capturing 1D, 1D array, 2D, 2D array, cubemap, cubemap array, and 3D textures in the full range of DXGI formats.

Updated Wiki: ScreenGrab

$
0
0
A Direct3D 11 2D texture save routine for generating a "screenshot" from a render target texture. There is a function that will dump the 2D texture to a .DDS file, and another that will write using WIC (BMP, JPEG, PNG, TIFF, GIF, JPEG-XR / HD Photo, or other WIC supported file container).

These writers do not support array textures, 1D textures, 3D volume textures, or cubemaps. Mipmaps are also ignored. For those scenarios, use the full DirectXTex library functionality (CaptureTexture). The ScreenGrab module is really designed to make a quick and light-weight solution for capturing screenshots at runtime.

MSAA textures are resolved before being written.

Also part of DirectXTK http://go.microsoft.com/fwlink/?LinkId=248929

Functions

SaveDDSTextureToFile
Saves a texture to a DDS file on disk. It performs no format conversions, but will try to favor writing legacy .DDS files when possible for improved tool support.

HRESULT SaveDDSTextureToFile( _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _In_z_ LPCWSTR fileName );

SaveWICTextureToFile
Saves a texture to a WIC-supported bitmap file on disk. The caller provides the desired WIC container format via guidContainerFormat and can optionally specify a desired WIC pixel format via targetFormat (which will result in E_FAIL if the requested pixel format is not supported by the WIC codec). If no WIC pixel format GUID is provided as the targetFormat parameter, it will default to a non-alpha format since 'screenshots' usually ignore the alpha channel in render targets.

HRESULT SaveWICTextureToFile( _In_ ID3D11DeviceContext* pContext,
    _In_ ID3D11Resource* pSource,
    _In_ REFGUID guidContainerFormat, 
    _In_z_ LPCWSTR fileName,
    _In_opt_ const GUID* targetFormat = nullptr,
    _In_opt_ std::function<void(IPropertyBag2*)> setCustomProps = nullptr );

NOTE: SaveWICTextureToFile is not supported on Windows Phone 8, because WIC is not available on that platform.

Examples

This example saves a JPEG screenshot given a ID3D11DeviceContext immContext and IDXGISwapChain swapChain.

ID3D11Texture2D* backBuffer = nullptr;
HRESULT hr = swapChain->GetBuffer( 0, __uuidof( *backBuffer ), ( LPVOID* )&backBuffer );
if ( SUCCEEDED(hr) )
{
    hr = SaveWICTextureToFile( immContext, backBuffer,
                GUID_ContainerFormatJpeg, L"SCREENSHOT.JPG" );
    backBuffer->Release();
}
if ( FAILED(hr) )
    // error

Here is an example of explicitly writing a screenshot as a 16-bit (5:6:5) BMP.

ID3D11Texture2D* backBuffer = nullptr;
HRESULT hr = swapChain->GetBuffer( 0, __uuidof( *backBuffer ), ( LPVOID* )&backBuffer );
if ( SUCCEEDED(hr) )
{
    hr = SaveWICTextureToFile( immContext, backBuffer,
                GUID_ContainerFormatBmp, L"SCREENSHOT.BMP",
                &GUID_WICPixelFormat16bppBGR565 );
    backBuffer->Release();
}
if ( FAILED(hr) )
    // error

When writing WIC files, you can also provide a callback for setting specific encoding options.

ID3D11Texture2D* backBuffer = nullptr;
HRESULT hr = swapChain->GetBuffer( 0, __uuidof( *backBuffer ), ( LPVOID* )&backBuffer );
if ( SUCCEEDED(hr) )
{
    hr = SaveWICTextureToFile( immContext, backBuffer,
                GUID_ContainerFormatTiff, L"SCREENSHOT.TIF", nullptr,
                [&](IPropertyBag2* props)
                {
                    PROPBAG2 options[2] = { 0, 0 };
                    options[0].pstrName = L"CompressionQuality";
                    options[1].pstrName = L"TiffCompressionMethod";

                    VARIANT varValues[2];
                    varValues[0].vt = VT_R4;
                    varValues[0].fltVal = 0.75f;

                    varValues[1].vt = VT_UI1;
                    varValues[1].bVal = WICTiffCompressionNone;

                    (void)props->Write( 2, options, varValues ); 
                });
    backBuffer->Release();
}
if ( FAILED(hr) )
    // error

Release Notes

  • If built with #define DXGI_1_2_FORMATS the DDS writer supports BGRA 4:4:4:4 files.
  • JPEG-XR / HD Photo supports nearly all WIC pixel formats including floating-point for both encoding and decoding.
  • TIFF can contain floating-point (128bpp or 96bpp) data, but the WIC built-in codec can only decode such images. It always converts floating-point data to unorm when encoding.
  • Paletted WIC formats are not supported for writing by the SaveWICTextureToFile function.

WIC2

WIC2 is available on Windows 8 and on Windows 7 Service Pack 1 with KB 2670838 installed.
  • If WIC2 is supported, then this function can make use of the new WIC pixel format GUID_WICPixelFormat96bppRGBFloat.
http://support.microsoft.com/kb/2670838

Windows Store apps

For Save*TextureToFile to succeed, the application must have write access to the destination path. For Windows Store apps, the file access permissions are rather restricted so you'll need to make sure you use a fully qualified path to a valid write folder. A good location to use is the app data folder:

auto folder = Windows::Storage::ApplicationData::Current->LocalFolder;
// use folder->Path->Data() as the path base

If you are going to immediately copy it to another location via StorageFolder, then use the app's temporary folder:

auto folder = Windows::Storage::ApplicationData::Current->TemporaryFolder;
// use folder->Path->Data() as the path base

http://msdn.microsoft.com/en-us/library/windows/apps/hh967755.aspx

Updated Wiki: DDSTextureLoader

$
0
0
A streamlined version of the DirectX SDK sample DDSWithoutD3DX11 texture loading code for a simple light-weight runtime .DDS file loader. This version only supports Direct3D 11 and performs no runtime pixel data conversions (see Release Notes for more details). This is ideal for runtime usage, and supports the full complement of Direct3D 11 texture resources (1D, 2D, volume maps, cubemaps, mipmap levels, texture arrays, BC formats, etc.). It supports both legacy and 'DX10' extension header format .dds files.

Also part of DirectXTK http://go.microsoft.com/fwlink/?LinkId=248929

Functions

CreateDDSTextureFromMemory
Loads a .DDS file assuming the image of the file is located in a memory buffer. Creates a Direct3D 11 resource and optionally a Direct3D 11 shader resource view.

HRESULT CreateDDSTextureFromMemory( _In_ ID3D11Device* d3dDevice, 
    _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData, _In_ size_t ddsDataSize,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _In_ size_t maxsize = 0, _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

HRESULT CreateDDSTextureFromMemory( _In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext,
    _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData, _In_ size_t ddsDataSize,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _In_ size_t maxsize = 0, _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

CreateDDSTextureFromFile
Loads a .DDS file from disk and creates a Direct3D 11 resource and optionally a Direct3D 11 shader resource view.

HRESULT CreateDDSTextureFromFile( _In_ ID3D11Device* d3dDevice, 
    _In_z_ constwchar_t* szFileName,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
     _In_ size_t maxsize = 0, _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

HRESULT CreateDDSTextureFromFile( _In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext,
    _In_z_ constwchar_t* szFileName,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
     _In_ size_t maxsize = 0, _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

CreateDDSTextureFromMemoryEx
CreateDDSTextureFromFileEx
These versions provide explicit control over the created resource's usage, binding flags, CPU access flags, and miscellaneous flags for advanced / expert scenarios. The standard routines default to D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE, 0, and 0 respectively. For cubemaps, the miscellaneous flags default to D3D11_RESOURCE_MISC_TEXTURECUBE. For auto-gen mipmaps, the default binding flags are D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET and miscellaneous flags is set to D3D11_RESOURCE_MISC_GENERATE_MIPS. There is also a 'forceSRGB' option for working around gamma issues with content that is in the sRGB or similar color space but is not encoded explicitly as an SRGB format. Note that the 'maxsize' parameter is not at the end of the parameter list like it is in the non-Ex version.

HRESULT CreateDDSTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice,
    _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData, _In_ size_t ddsDataSize,
    _In_ size_t maxsize,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

HRESULT CreateDDSTextureFromMemoryEx( _In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext,
    _In_reads_bytes_(ddsDataSize) const uint8_t* ddsData, _In_ size_t ddsDataSize,
    _In_ size_t maxsize,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );


HRESULT CreateDDSTextureFromFileEx( _In_ ID3D11Device* d3dDevice,
    _In_z_ constwchar_t* szFileName,
    _In_ size_t maxsize,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

HRESULT CreateDDSTextureFromFileEx( _In_ ID3D11Device* d3dDevice, _In_opt_ ID3D11DeviceContext* d3dContext,
    _In_z_ constwchar_t* szFileName,
    _In_ size_t maxsize,
    _In_ D3D11_USAGE usage, _In_ unsignedint bindFlags,
    _In_ unsignedint cpuAccessFlags, _In_ unsignedint miscFlags,
    _In_ bool forceSRGB,
    _Outptr_opt_ ID3D11Resource** texture, _Outptr_opt_ ID3D11ShaderResourceView** textureView,
    _Out_opt_ DDS_ALPHA_MODE* alphaMode = nullptr );

Parameters

If maxsize parameter non-zero, then all mipmap levels larger than the maxsize are ignored before creating the Direct3D 11 resource. This allows for load-time scaling. If '0', then if the attempt to create the Direct3D 11 resource fails and there are mipmaps present, it will retry assuming a maxsize based on the device's current feature level.

If a d3dContext is given to these functions, they will attempt to use the auto-generation of mipmaps features in the Direct3D 11 API if supported for the pixel format. Note the quality of auto-gen mipmaps is up to the driver, so can vary widely. Also if a context is passed, the function is not thread safe.

The last optional parameter alphaMode is a pointer to return the alpha mode of the DDS file. This can be one of the following values to return information about the alpha channel if present in the file:
  • DDS_ALPHA_MODE_UNKNOWN (0) - This is the default for most .DDS files if the specific metadata isn't present, and it's up to the application to know if it's really something else. Viewers should assume the alpha channel is intended for 'normal' alpha blending.
  • DDS_ALPHA_MODE_STRAIGHT (1) - This indicates that the alpha channel if present is assumed to be using 'straight' alpha. Viewers should use the alpha channel with 'normal' alpha blending.
  • DDS_ALPHA_MODE_PREMULTIPLIED (2) - This indicates the alpha channel if present is premultiplied alpha. This information is only present if the file is written using the latest version of the "DX10" extended header, or if the file is BC2/BC3 with the "DXT2"/"DXT4" FourCC which are explicitly stored as premultiplied alpha. Viewers should use the alpha channel with premultiplied alpha blending.
  • DDS_ALPHA_MODE_OPAQUE (3) - This indicates that the alpha channel if present is fully opaque for all pixels. Viewers can assume there is no alpha blending.
  • DDS_ALPHA_MODE_CUSTOM (4) - This indicates the alpha channel if present does not contain transparency (neither straight or premultiplied alpha) and instead is encoding some other channel of information. Viewers should not use the alpha channel for blending, and should instead view it as a distinct image channel.

Example

This example creates a shader resource view on the ID3D11Device d3dDevice which can be used for rendering.

ID3D11ShaderResourceView* pSRV = nullptr;
HRESULT hr = CreateDDSTextureFromFile( d3dDevice, L"SEAFLOOR.DDS", nullptr, &pSRV );
if (FAILED(hr))
   // error

Feature Level Notes

In order to support all feature levels, you should make sure your DDS textures are mip-mapped so that they contain a suitably sized image. Non-mipmapped textures will either need explicit feature level association, or be sized less than or equal to 2048 for 1D, 2048 x 2048 for 2D, 512 x 512 for cubemaps, and 256 x 256 x 256 for volume maps.

Texture arrays require Feature Level 10.0 or later. Cubemap arrays requires Feature Level 10.1 or later.

Be sure to review the various format limitations for the different feature levels. To support all feature levels, stick with textures in the following formats:
  • DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB
  • DXGI_FORMAT_R8G8B8A8_SNORM
  • DXGI_FORMAT_B8G8R8A8_UNORM
  • DXGI_FORMAT_R16G16_SNORM
  • DXGI_FORMAT_R8G8_SNORM
  • DXGI_FORMAT_R8_UNORM
  • DXGI_FORMAT_BC1_UNORM, DXGI_FORMAT_BC1_UNORM_SRGB
  • DXGI_FORMAT_BC2_UNORM, DXGI_FORMAT_BC2_UNORM_SRGB
  • DXGI_FORMAT_BC3_UNORM, DXGI_FORMAT_BC3_UNORM_SRGB
On Windows 8 with WDDM 1.2 drivers, all feature levels support 16bpp formats as well DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B5G5R5A1_UNORM, and DXGI_FORMAT_B4G4R4A4_UNORM.

Release Notes

  • DDSTextureLoader performs no run-time conversions. If there is not a direct mapping to a DXGI supported format, the function fails. You can make use of the DirectXTex library or texconv tool to convert legacy Direct3D9 DDS files to a supported format. Legacy formats which require conversion include:
    • D3DFMT_R8G8B8 (24bpp RGB) - Use a 32bpp format
    • D3DFMT_X8B8G8R8 (32bpp RGBX) - Use BGRX, BGRA, or RGBA
    • D3DFMT_A2R10G10B10 (BGRA 10:10:10:2) - Use RGBA 10:10:10:2
    • D3DFMT_X1R5G5B5 (BGR 5:5:5) - Use BGRA 5:5:5:1 or BGR 5:6:5
    • D3DFMT_A8R3G3B2, D3DFMT_R3G3B2 (BGR 3:3:2) - Expand to a supported format
    • D3DFMT_P8, D3DFMT_A8P8 (8-bit palette) - Expand to a supported format
    • D3DFMT_A4L4 (Luminance 4:4) - Expand to a supported format
    • D3DFMT_UYVY (YUV 4:2:2 16bpp) - Swizzle to YUY2
http://go.microsoft.com/fwlink/?LinkId=248926.
  • If built with #define DXGI_1_2_FORMATS the code supports loading BGRA 4:4:4:4 files. Otherwise, it fails.
  • On a system with the DirectX 11.0 Runtime or lacking WDDM 1.2 drivers, attempts to load 16bpp format files (BGR 5:6:5, BGRA 5:5:5:1, and BGRA 4:4:4:4) will fail.
  • Partial cubemaps (i.e. DDS files without all six faces defined) are not supported by Direct3D 11.

Windows Store apps

The texture loader function is typically used to load texture files from the application's install folder as they were included with the AppX package. If you wish to create a texture from a file that is specified by the user from a WinRT picker, you will need to copy the file locally to a temporary location before you can use DDSTextureLoader on it. This is because you either won't have file access rights to the user's file location, or the StorageFile is actually not a local file system path (i.e. it's a URL).

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 = CreateDDSTextureFromFile( ..., tempFile->Path->Data(), ... );
                DX::ThrowIfFailed(hr);
            }
        });
    });

http://msdn.microsoft.com/en-us/library/windows/apps/hh758319.aspx

DDS Files

This function loads both traditional and FourCC "DX10" variant DDS files.

http://msdn.microsoft.com/en-us/library/windows/desktop/bb943990.aspx

Further Reading

http://blogs.msdn.com/b/chuckw/archive/2010/02/05/the-dds-file-format-lives.aspx

http://blogs.msdn.com/b/chuckw/archive/2012/06/20/direct3d-feature-levels.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/bb219822.aspx

Source code checked in, #35306

$
0
0
DirectXTex: Minor code review fix

Updated Wiki: DirectXTex

$
0
0
The DirectXTex library includes a full-featured DDS reader and writer including legacy format conversions, a TGA reader and writer, a WIC-based bitmap reader and writer (BMP, JPEG, PNG, TIFF, and HD Photo), and various texture processing functions. This is intended primarily for tool usage.

The library assumes that the client code will have already called CoInitialize or CoInitializeEx as needed by the application before calling any DirectXTex routines

NOTE: DirectXTex is not supported on Windows Phone 8, because WIC is not available on that platform. The .DDS files it generates are suitable for use on Windows Phone 8 assuming the pixel format is supported by the device (currently Feature Level 9.3).

Headers

The majority of the header files here are intended for internal implementation of the library only (BC.h, DDS.h, DirectXTexP.h, and scoped.h). Only DirectXTex.h is meant as a 'public' header for the library.

Namespace

All the functions in the library are in the "DirectX" C++ namespace.

Functions

DDS I/O Functions

TGA I/O Functions

WIC I/O Functions

Texture Functions

Direct3D 11 Helper Functions

Utility Functions

Structures

TexMetadata contains metadata information about the texture resource and organization such as width, height, depth, format, dimension, etc.
  • TEX_DIMENSION_TEXTURE1D, TEX_DIMENSION_TEXTURE2D, and TEX_DIMENSION_TEXTURE3D are alises for D3D10_RESOURCE_DIMEMSION and D3D11_RESOURCE_DIMENSION.
  • TEX_MISC_TEXTURECUBE is an alias for the same D3D10_RESOURCE_MISC_FLAG and D3D11_RESOURCE_MISC_FLAG.
  • TEX_MISC2_ALPHA_MODE_MASK is a mask for miscFlags2 to obtain the TEX_ALPHA_MODE.
Image contains information about the surface including width, height, format, rowPitch, slicePitch, and a pointer to pixel data. Note that for 1D and 2D images, slicePitch should be set to the full size of the image.

ScratchImage is a helper class that manages memory for functions that return a Image or set of Images.

Blob is a helper class that manages for functions that return a binary blob of data.

Rect contains a simple pixel-based rectangle used by the CopyRectangle function.

Adding to a VS Project

In your application's solution, right-click on the Solution and use "Add \ Existing Project..." to add the appropriate .vcxproj file to your solution.
  • DirectXTex_Desktop_2013 is for Win32 desktop applications building with VS 2013 Express for Desktop, VS 2013 Professional or higher.
  • DirectXTex_Desktop_2012 is for Win32 desktop applications building with VS 2012 Express for Desktop, VS 2012 Professional or higher
  • DirectXTex_Desktop_2010 is for Win32 desktop applications building with VS 2010 using the Windows 8.1 SDK
  • DirectXTex_Windows81 is for Windows Store apps building with VS 2013 targeting Windows 8.1
  • DirectXTex_Windows8 is for Windows Store apps building with VS 2012 targeting Windows 8
  • DirectXTex_XboxOneXDK is for Xbox One exclusive apps building with VS 2012 using the Xbox One XDK
  • DirectXTex_XboxOneADK is for Xbox One hub apps building with VS 2012 using the Xbox One ADK
In your application's project, right-click on the Project and use "References...", then "Add New Reference...", and then check the DirectXTex project name and click OK. For a Windows Store app, you need to set Reference Assembly Output to false since DirectXTex is a static C++ library and not a WinRT component.

In your application's project settings, on the "C++ / General" page set Configuration to "All Configurations", set Platform to "All Platforms", and then add the relative path to DirectXTex; to the Additional Include Directories properties. Click Apply.

When using VS 2010 with the Windows 8.x SDK http://go.microsoft.com/fwlink/?LinkID=323507, or when using VS 2012 with the Windows 8.1 SDK, you'll need to apply the correct .props files to your projects as well as use the correct DirectXTex project. http://blogs.msdn.com/b/vcblog/archive/2012/11/23/using-the-windows-8-sdk-with-visual-studio-2010-configuring-multiple-projects.aspx

http://blogs.msdn.com/b/vcblog/archive/2010/05/03/flexible-project-to-project-references.aspx

Release Notes

  • The alpha mode specification for DDS files was updated between the March 2013 and April 2013 releases. Any DDS files created using the DDS_FLAGS_FORCE_DX10_EXT_MISC2 flag or the texconv -dx10 switch using the March 2013 release should be refreshed.
  • Due to the underlying Windows BMP WIC codec, alpha channels are not supported for 16bpp or 32bpp BMP pixel format files. The Windows 8 version of the Windows BMP WIC codec does support 32bpp pixel formats with alpha when using the BITMAPV5HEADER file header.
  • While DXGI 1.0 and DXGI 1.1 include 5:6:5 (DXGI_FORMAT_B5G6R5_UNORM) and 5:5:5:1 (DXGI_FORMAT_B5G5R5A1_UNORM) pixel format enumerations, the DirectX 10.x and 11.0 Runtimes do not support these formats for use with Direct3D. The DirectX 11.1 runtime, DXGI 1.2, and the WDDM 1.2 driver model fully support 16bpp formats (5:6:5, 5:5:5:1, and 4:4:4:4).
  • Loading of 96bpp floating-point TIFF files results in a corrupted image prior to Windows 8. This fix is available on Windows 7 SP1 with KB 2670838 installed.

Source code checked in, #35327

$
0
0
ScreenGrab: Minor code-review fix

Source code checked in, #35337

$
0
0
Sync with latest DirectXTK versions of DDSTextureLoader, ScreenGrab, and WICTextureLoader

Source code checked in, #35339

$
0
0
Projects and support for the Windows phone 8.1 platform

Updated Wiki: DirectXTex

$
0
0
The DirectXTex library includes a full-featured DDS reader and writer including legacy format conversions, a TGA reader and writer, a WIC-based bitmap reader and writer (BMP, JPEG, PNG, TIFF, and HD Photo), and various texture processing functions. This is intended primarily for tool usage.

The library assumes that the client code will have already called CoInitialize or CoInitializeEx as needed by the application before calling any DirectXTex routines

NOTE: DirectXTex is not supported on Windows Phone 8.0, because WIC is not available on that platform. The .DDS files it generates are suitable for use on Windows Phone 8.x assuming the pixel format is supported by the device (currently Feature Level 9.3).

Headers

The majority of the header files here are intended for internal implementation of the library only (BC.h, DDS.h, DirectXTexP.h, and scoped.h). Only DirectXTex.h is meant as a 'public' header for the library.

Namespace

All the functions in the library are in the "DirectX" C++ namespace.

Functions

DDS I/O Functions

TGA I/O Functions

WIC I/O Functions

Texture Functions

Direct3D 11 Helper Functions

Utility Functions

Structures

TexMetadata contains metadata information about the texture resource and organization such as width, height, depth, format, dimension, etc.
  • TEX_DIMENSION_TEXTURE1D, TEX_DIMENSION_TEXTURE2D, and TEX_DIMENSION_TEXTURE3D are alises for D3D10_RESOURCE_DIMEMSION and D3D11_RESOURCE_DIMENSION.
  • TEX_MISC_TEXTURECUBE is an alias for the same D3D10_RESOURCE_MISC_FLAG and D3D11_RESOURCE_MISC_FLAG.
  • TEX_MISC2_ALPHA_MODE_MASK is a mask for miscFlags2 to obtain the TEX_ALPHA_MODE.
Image contains information about the surface including width, height, format, rowPitch, slicePitch, and a pointer to pixel data. Note that for 1D and 2D images, slicePitch should be set to the full size of the image.

ScratchImage is a helper class that manages memory for functions that return a Image or set of Images.

Blob is a helper class that manages for functions that return a binary blob of data.

Rect contains a simple pixel-based rectangle used by the CopyRectangle function.

Adding to a VS Project

In your application's solution, right-click on the Solution and use "Add \ Existing Project..." to add the appropriate .vcxproj file to your solution.
  • DirectXTex_Desktop_2013 is for Win32 desktop applications building with VS 2013 Express for Desktop, VS 2013 Professional or higher.
  • DirectXTex_Desktop_2012 is for Win32 desktop applications building with VS 2012 Express for Desktop, VS 2012 Professional or higher
  • DirectXTex_Desktop_2010 is for Win32 desktop applications building with VS 2010 using the Windows 8.1 SDK
  • DirectXTex_Windows81 is for Windows Store apps building with VS 2013 targeting Windows 8.1
  • DirectXTex_Windows8 is for Windows Store apps building with VS 2012 targeting Windows 8
  • DirectXTex_WindowsPhone81 is for Windows phone 8.1 apps building with VS 2013 Update 2 or later.
  • DirectXTex_XboxOneXDK is for Xbox One exclusive apps building with VS 2012 using the Xbox One XDK
  • DirectXTex_XboxOneADK is for Xbox One hub apps building with VS 2012 using the Xbox One ADK
In your application's project, right-click on the Project and use "References...", then "Add New Reference...", and then check the DirectXTex project name and click OK. For a Windows Store app, you need to set Reference Assembly Output to false since DirectXTex is a static C++ library and not a WinRT component.

In your application's project settings, on the "C++ / General" page set Configuration to "All Configurations", set Platform to "All Platforms", and then add the relative path to DirectXTex; to the Additional Include Directories properties. Click Apply.

When using VS 2010 with the Windows 8.x SDK http://go.microsoft.com/fwlink/?LinkID=323507, or when using VS 2012 with the Windows 8.1 SDK, you'll need to apply the correct .props files to your projects as well as use the correct DirectXTex project. http://blogs.msdn.com/b/vcblog/archive/2012/11/23/using-the-windows-8-sdk-with-visual-studio-2010-configuring-multiple-projects.aspx

http://blogs.msdn.com/b/vcblog/archive/2010/05/03/flexible-project-to-project-references.aspx

Release Notes

  • The alpha mode specification for DDS files was updated between the March 2013 and April 2013 releases. Any DDS files created using the DDS_FLAGS_FORCE_DX10_EXT_MISC2 flag or the texconv -dx10 switch using the March 2013 release should be refreshed.
  • Due to the underlying Windows BMP WIC codec, alpha channels are not supported for 16bpp or 32bpp BMP pixel format files. The Windows 8 version of the Windows BMP WIC codec does support 32bpp pixel formats with alpha when using the BITMAPV5HEADER file header.
  • While DXGI 1.0 and DXGI 1.1 include 5:6:5 (DXGI_FORMAT_B5G6R5_UNORM) and 5:5:5:1 (DXGI_FORMAT_B5G5R5A1_UNORM) pixel format enumerations, the DirectX 10.x and 11.0 Runtimes do not support these formats for use with Direct3D. The DirectX 11.1 runtime, DXGI 1.2, and the WDDM 1.2 driver model fully support 16bpp formats (5:6:5, 5:5:5:1, and 4:4:4:4).
  • Loading of 96bpp floating-point TIFF files results in a corrupted image prior to Windows 8. This fix is available on Windows 7 SP1 with KB 2670838 installed.
Viewing all 1174 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>