2012-05-02 00:00:24 +04:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: TexConv.cpp
|
|
|
|
//
|
2014-10-23 04:50:21 +04:00
|
|
|
// DirectX 11 Texture Converter
|
2012-05-02 00:00:24 +04:00
|
|
|
//
|
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2016-04-25 06:28:04 +03:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOMINMAX
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
#include <memory>
|
2014-10-07 01:08:06 +04:00
|
|
|
#include <list>
|
2015-01-31 04:31:49 +03:00
|
|
|
|
|
|
|
#include <wrl\client.h>
|
2014-07-15 04:22:30 +04:00
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
#include <d3d11.h>
|
|
|
|
#include <dxgi.h>
|
2012-05-02 00:00:24 +04:00
|
|
|
#include <dxgiformat.h>
|
|
|
|
|
2016-06-22 02:58:18 +03:00
|
|
|
#include <wincodec.h>
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
#include "directxtex.h"
|
|
|
|
|
|
|
|
using namespace DirectX;
|
2014-10-07 01:08:06 +04:00
|
|
|
using Microsoft::WRL::ComPtr;
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
enum OPTIONS // Note: dwOptions below assumes 64 or less options.
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
OPT_WIDTH = 1,
|
|
|
|
OPT_HEIGHT,
|
|
|
|
OPT_MIPLEVELS,
|
|
|
|
OPT_FORMAT,
|
|
|
|
OPT_FILTER,
|
|
|
|
OPT_SRGBI,
|
|
|
|
OPT_SRGBO,
|
|
|
|
OPT_SRGB,
|
|
|
|
OPT_PREFIX,
|
|
|
|
OPT_SUFFIX,
|
|
|
|
OPT_OUTPUTDIR,
|
|
|
|
OPT_FILETYPE,
|
|
|
|
OPT_HFLIP,
|
|
|
|
OPT_VFLIP,
|
|
|
|
OPT_DDS_DWORD_ALIGN,
|
|
|
|
OPT_USE_DX10,
|
|
|
|
OPT_NOLOGO,
|
2015-04-22 23:31:37 +03:00
|
|
|
OPT_TIMING,
|
2012-07-03 00:31:12 +04:00
|
|
|
OPT_SEPALPHA,
|
|
|
|
OPT_TYPELESS_UNORM,
|
|
|
|
OPT_TYPELESS_FLOAT,
|
2012-12-14 00:12:35 +04:00
|
|
|
OPT_PREMUL_ALPHA,
|
2013-06-14 09:46:42 +04:00
|
|
|
OPT_EXPAND_LUMINANCE,
|
|
|
|
OPT_TA_WRAP,
|
|
|
|
OPT_TA_MIRROR,
|
2013-07-13 02:12:03 +04:00
|
|
|
OPT_FORCE_SINGLEPROC,
|
2016-08-19 02:49:24 +03:00
|
|
|
OPT_GPU,
|
2013-08-05 21:58:54 +04:00
|
|
|
OPT_NOGPU,
|
2013-11-16 04:21:00 +04:00
|
|
|
OPT_FEATURE_LEVEL,
|
|
|
|
OPT_FIT_POWEROF2,
|
2014-02-20 05:31:35 +04:00
|
|
|
OPT_ALPHA_WEIGHT,
|
2015-03-11 01:22:08 +03:00
|
|
|
OPT_NORMAL_MAP,
|
|
|
|
OPT_NORMAL_MAP_AMPLITUDE,
|
2015-04-22 23:31:37 +03:00
|
|
|
OPT_COMPRESS_UNIFORM,
|
|
|
|
OPT_COMPRESS_MAX,
|
|
|
|
OPT_COMPRESS_DITHER,
|
2016-06-22 02:58:18 +03:00
|
|
|
OPT_WIC_QUALITY,
|
|
|
|
OPT_WIC_LOSSLESS,
|
2013-08-05 21:58:54 +04:00
|
|
|
OPT_MAX
|
2012-05-02 00:00:24 +04:00
|
|
|
};
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
static_assert( OPT_MAX <= 64, "dwOptions is a DWORD64 bitfield" );
|
2013-08-05 21:58:54 +04:00
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
struct SConversion
|
|
|
|
{
|
2016-05-11 21:36:27 +03:00
|
|
|
wchar_t szSrc [MAX_PATH];
|
|
|
|
wchar_t szDest[MAX_PATH];
|
2012-05-02 00:00:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SValue
|
|
|
|
{
|
|
|
|
LPCWSTR pName;
|
|
|
|
DWORD dwValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
SValue g_pOptions[] =
|
|
|
|
{
|
|
|
|
{ L"w", OPT_WIDTH },
|
|
|
|
{ L"h", OPT_HEIGHT },
|
|
|
|
{ L"m", OPT_MIPLEVELS },
|
|
|
|
{ L"f", OPT_FORMAT },
|
|
|
|
{ L"if", OPT_FILTER },
|
|
|
|
{ L"srgbi", OPT_SRGBI },
|
|
|
|
{ L"srgbo", OPT_SRGBO },
|
|
|
|
{ L"srgb", OPT_SRGB },
|
|
|
|
{ L"px", OPT_PREFIX },
|
|
|
|
{ L"sx", OPT_SUFFIX },
|
|
|
|
{ L"o", OPT_OUTPUTDIR },
|
|
|
|
{ L"ft", OPT_FILETYPE },
|
|
|
|
{ L"hflip", OPT_HFLIP },
|
|
|
|
{ L"vflip", OPT_VFLIP },
|
|
|
|
{ L"dword", OPT_DDS_DWORD_ALIGN },
|
|
|
|
{ L"dx10", OPT_USE_DX10 },
|
|
|
|
{ L"nologo", OPT_NOLOGO },
|
2015-04-22 23:31:37 +03:00
|
|
|
{ L"timing", OPT_TIMING },
|
2012-05-02 00:00:24 +04:00
|
|
|
{ L"sepalpha", OPT_SEPALPHA },
|
2012-07-03 00:31:12 +04:00
|
|
|
{ L"tu", OPT_TYPELESS_UNORM },
|
|
|
|
{ L"tf", OPT_TYPELESS_FLOAT },
|
2012-12-14 00:12:35 +04:00
|
|
|
{ L"pmalpha", OPT_PREMUL_ALPHA },
|
2013-06-12 00:34:48 +04:00
|
|
|
{ L"xlum", OPT_EXPAND_LUMINANCE },
|
2016-08-19 02:49:24 +03:00
|
|
|
{ L"wrap", OPT_TA_WRAP },
|
2013-06-14 09:46:42 +04:00
|
|
|
{ L"mirror", OPT_TA_MIRROR },
|
2013-07-13 02:12:03 +04:00
|
|
|
{ L"singleproc", OPT_FORCE_SINGLEPROC },
|
2016-08-19 02:49:24 +03:00
|
|
|
{ L"gpu", OPT_GPU },
|
2013-08-05 21:58:54 +04:00
|
|
|
{ L"nogpu", OPT_NOGPU },
|
2013-11-16 04:21:00 +04:00
|
|
|
{ L"fl", OPT_FEATURE_LEVEL },
|
|
|
|
{ L"pow2", OPT_FIT_POWEROF2 },
|
2014-02-20 05:31:35 +04:00
|
|
|
{ L"aw", OPT_ALPHA_WEIGHT },
|
2015-03-11 01:22:08 +03:00
|
|
|
{ L"nmap", OPT_NORMAL_MAP },
|
|
|
|
{ L"nmapamp", OPT_NORMAL_MAP_AMPLITUDE },
|
2015-04-22 23:31:37 +03:00
|
|
|
{ L"bcuniform", OPT_COMPRESS_UNIFORM },
|
|
|
|
{ L"bcmax", OPT_COMPRESS_MAX },
|
|
|
|
{ L"bcdither", OPT_COMPRESS_DITHER },
|
2016-06-22 02:58:18 +03:00
|
|
|
{ L"wicq", OPT_WIC_QUALITY },
|
|
|
|
{ L"wiclossless", OPT_WIC_LOSSLESS },
|
2012-05-02 00:00:24 +04:00
|
|
|
{ nullptr, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEFFMT(fmt) { L#fmt, DXGI_FORMAT_ ## fmt }
|
|
|
|
|
|
|
|
SValue g_pFormats[] =
|
|
|
|
{
|
|
|
|
// List does not include _TYPELESS or depth/stencil formats
|
|
|
|
DEFFMT(R32G32B32A32_FLOAT),
|
|
|
|
DEFFMT(R32G32B32A32_UINT),
|
|
|
|
DEFFMT(R32G32B32A32_SINT),
|
|
|
|
DEFFMT(R32G32B32_FLOAT),
|
|
|
|
DEFFMT(R32G32B32_UINT),
|
|
|
|
DEFFMT(R32G32B32_SINT),
|
|
|
|
DEFFMT(R16G16B16A16_FLOAT),
|
|
|
|
DEFFMT(R16G16B16A16_UNORM),
|
|
|
|
DEFFMT(R16G16B16A16_UINT),
|
|
|
|
DEFFMT(R16G16B16A16_SNORM),
|
|
|
|
DEFFMT(R16G16B16A16_SINT),
|
|
|
|
DEFFMT(R32G32_FLOAT),
|
|
|
|
DEFFMT(R32G32_UINT),
|
|
|
|
DEFFMT(R32G32_SINT),
|
|
|
|
DEFFMT(R10G10B10A2_UNORM),
|
|
|
|
DEFFMT(R10G10B10A2_UINT),
|
|
|
|
DEFFMT(R11G11B10_FLOAT),
|
|
|
|
DEFFMT(R8G8B8A8_UNORM),
|
|
|
|
DEFFMT(R8G8B8A8_UNORM_SRGB),
|
|
|
|
DEFFMT(R8G8B8A8_UINT),
|
|
|
|
DEFFMT(R8G8B8A8_SNORM),
|
|
|
|
DEFFMT(R8G8B8A8_SINT),
|
|
|
|
DEFFMT(R16G16_FLOAT),
|
|
|
|
DEFFMT(R16G16_UNORM),
|
|
|
|
DEFFMT(R16G16_UINT),
|
|
|
|
DEFFMT(R16G16_SNORM),
|
|
|
|
DEFFMT(R16G16_SINT),
|
|
|
|
DEFFMT(R32_FLOAT),
|
|
|
|
DEFFMT(R32_UINT),
|
|
|
|
DEFFMT(R32_SINT),
|
|
|
|
DEFFMT(R8G8_UNORM),
|
|
|
|
DEFFMT(R8G8_UINT),
|
|
|
|
DEFFMT(R8G8_SNORM),
|
|
|
|
DEFFMT(R8G8_SINT),
|
|
|
|
DEFFMT(R16_FLOAT),
|
|
|
|
DEFFMT(R16_UNORM),
|
|
|
|
DEFFMT(R16_UINT),
|
|
|
|
DEFFMT(R16_SNORM),
|
|
|
|
DEFFMT(R16_SINT),
|
|
|
|
DEFFMT(R8_UNORM),
|
|
|
|
DEFFMT(R8_UINT),
|
|
|
|
DEFFMT(R8_SNORM),
|
|
|
|
DEFFMT(R8_SINT),
|
|
|
|
DEFFMT(A8_UNORM),
|
|
|
|
DEFFMT(R9G9B9E5_SHAREDEXP),
|
|
|
|
DEFFMT(R8G8_B8G8_UNORM),
|
|
|
|
DEFFMT(G8R8_G8B8_UNORM),
|
|
|
|
DEFFMT(BC1_UNORM),
|
|
|
|
DEFFMT(BC1_UNORM_SRGB),
|
|
|
|
DEFFMT(BC2_UNORM),
|
|
|
|
DEFFMT(BC2_UNORM_SRGB),
|
|
|
|
DEFFMT(BC3_UNORM),
|
|
|
|
DEFFMT(BC3_UNORM_SRGB),
|
|
|
|
DEFFMT(BC4_UNORM),
|
|
|
|
DEFFMT(BC4_SNORM),
|
|
|
|
DEFFMT(BC5_UNORM),
|
|
|
|
DEFFMT(BC5_SNORM),
|
|
|
|
DEFFMT(B5G6R5_UNORM),
|
|
|
|
DEFFMT(B5G5R5A1_UNORM),
|
|
|
|
|
|
|
|
// DXGI 1.1 formats
|
|
|
|
DEFFMT(B8G8R8A8_UNORM),
|
|
|
|
DEFFMT(B8G8R8X8_UNORM),
|
|
|
|
DEFFMT(R10G10B10_XR_BIAS_A2_UNORM),
|
|
|
|
DEFFMT(B8G8R8A8_UNORM_SRGB),
|
|
|
|
DEFFMT(B8G8R8X8_UNORM_SRGB),
|
|
|
|
DEFFMT(BC6H_UF16),
|
|
|
|
DEFFMT(BC6H_SF16),
|
|
|
|
DEFFMT(BC7_UNORM),
|
|
|
|
DEFFMT(BC7_UNORM_SRGB),
|
|
|
|
|
|
|
|
// DXGI 1.2 formats
|
2014-02-15 01:33:01 +04:00
|
|
|
DEFFMT(AYUV),
|
|
|
|
DEFFMT(Y410),
|
|
|
|
DEFFMT(Y416),
|
|
|
|
DEFFMT(YUY2),
|
|
|
|
DEFFMT(Y210),
|
|
|
|
DEFFMT(Y216),
|
|
|
|
// No support for legacy paletted video formats (AI44, IA44, P8, A8P8)
|
2012-05-02 00:00:24 +04:00
|
|
|
DEFFMT(B4G4R4A4_UNORM),
|
|
|
|
|
|
|
|
{ nullptr, DXGI_FORMAT_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
SValue g_pReadOnlyFormats[] =
|
|
|
|
{
|
|
|
|
DEFFMT(R32G32B32A32_TYPELESS),
|
|
|
|
DEFFMT(R32G32B32_TYPELESS),
|
|
|
|
DEFFMT(R16G16B16A16_TYPELESS),
|
|
|
|
DEFFMT(R32G32_TYPELESS),
|
|
|
|
DEFFMT(R32G8X24_TYPELESS),
|
|
|
|
DEFFMT(D32_FLOAT_S8X24_UINT),
|
|
|
|
DEFFMT(R32_FLOAT_X8X24_TYPELESS),
|
|
|
|
DEFFMT(X32_TYPELESS_G8X24_UINT),
|
|
|
|
DEFFMT(R10G10B10A2_TYPELESS),
|
|
|
|
DEFFMT(R8G8B8A8_TYPELESS),
|
|
|
|
DEFFMT(R16G16_TYPELESS),
|
|
|
|
DEFFMT(R32_TYPELESS),
|
|
|
|
DEFFMT(D32_FLOAT),
|
|
|
|
DEFFMT(R24G8_TYPELESS),
|
|
|
|
DEFFMT(D24_UNORM_S8_UINT),
|
|
|
|
DEFFMT(R24_UNORM_X8_TYPELESS),
|
|
|
|
DEFFMT(X24_TYPELESS_G8_UINT),
|
|
|
|
DEFFMT(R8G8_TYPELESS),
|
|
|
|
DEFFMT(R16_TYPELESS),
|
|
|
|
DEFFMT(R8_TYPELESS),
|
|
|
|
DEFFMT(BC1_TYPELESS),
|
|
|
|
DEFFMT(BC2_TYPELESS),
|
|
|
|
DEFFMT(BC3_TYPELESS),
|
|
|
|
DEFFMT(BC4_TYPELESS),
|
|
|
|
DEFFMT(BC5_TYPELESS),
|
|
|
|
|
|
|
|
// DXGI 1.1 formats
|
|
|
|
DEFFMT(B8G8R8A8_TYPELESS),
|
|
|
|
DEFFMT(B8G8R8X8_TYPELESS),
|
|
|
|
DEFFMT(BC6H_TYPELESS),
|
|
|
|
DEFFMT(BC7_TYPELESS),
|
|
|
|
|
|
|
|
// DXGI 1.2 formats
|
|
|
|
DEFFMT(NV12),
|
|
|
|
DEFFMT(P010),
|
|
|
|
DEFFMT(P016),
|
|
|
|
DEFFMT(420_OPAQUE),
|
|
|
|
DEFFMT(NV11),
|
|
|
|
|
|
|
|
{ nullptr, DXGI_FORMAT_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
SValue g_pFilters[] =
|
|
|
|
{
|
2013-06-12 00:18:23 +04:00
|
|
|
{ L"POINT", TEX_FILTER_POINT },
|
|
|
|
{ L"LINEAR", TEX_FILTER_LINEAR },
|
|
|
|
{ L"CUBIC", TEX_FILTER_CUBIC },
|
|
|
|
{ L"FANT", TEX_FILTER_FANT },
|
|
|
|
{ L"BOX", TEX_FILTER_BOX },
|
2013-06-15 03:17:16 +04:00
|
|
|
{ L"TRIANGLE", TEX_FILTER_TRIANGLE },
|
2013-06-12 00:18:23 +04:00
|
|
|
{ L"POINT_DITHER", TEX_FILTER_POINT | TEX_FILTER_DITHER },
|
|
|
|
{ L"LINEAR_DITHER", TEX_FILTER_LINEAR | TEX_FILTER_DITHER },
|
|
|
|
{ L"CUBIC_DITHER", TEX_FILTER_CUBIC | TEX_FILTER_DITHER },
|
|
|
|
{ L"FANT_DITHER", TEX_FILTER_FANT | TEX_FILTER_DITHER },
|
|
|
|
{ L"BOX_DITHER", TEX_FILTER_BOX | TEX_FILTER_DITHER },
|
2013-06-15 03:17:16 +04:00
|
|
|
{ L"TRIANGLE_DITHER", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER },
|
2013-06-12 00:18:23 +04:00
|
|
|
{ L"POINT_DITHER_DIFFUSION", TEX_FILTER_POINT | TEX_FILTER_DITHER_DIFFUSION },
|
|
|
|
{ L"LINEAR_DITHER_DIFFUSION", TEX_FILTER_LINEAR | TEX_FILTER_DITHER_DIFFUSION },
|
|
|
|
{ L"CUBIC_DITHER_DIFFUSION", TEX_FILTER_CUBIC | TEX_FILTER_DITHER_DIFFUSION },
|
|
|
|
{ L"FANT_DITHER_DIFFUSION", TEX_FILTER_FANT | TEX_FILTER_DITHER_DIFFUSION },
|
|
|
|
{ L"BOX_DITHER_DIFFUSION", TEX_FILTER_BOX | TEX_FILTER_DITHER_DIFFUSION },
|
2013-06-15 03:17:16 +04:00
|
|
|
{ L"TRIANGLE_DITHER_DIFFUSION", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER_DIFFUSION },
|
2013-06-12 00:18:23 +04:00
|
|
|
{ nullptr, TEX_FILTER_DEFAULT }
|
2012-05-02 00:00:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define CODEC_DDS 0xFFFF0001
|
|
|
|
#define CODEC_TGA 0xFFFF0002
|
2016-06-22 02:58:18 +03:00
|
|
|
#define CODEC_HDP 0xFFFF0003
|
|
|
|
#define CODEC_JXR 0xFFFF0004
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
SValue g_pSaveFileTypes[] = // valid formats to write to
|
|
|
|
{
|
|
|
|
{ L"BMP", WIC_CODEC_BMP },
|
|
|
|
{ L"JPG", WIC_CODEC_JPEG },
|
|
|
|
{ L"JPEG", WIC_CODEC_JPEG },
|
|
|
|
{ L"PNG", WIC_CODEC_PNG },
|
|
|
|
{ L"DDS", CODEC_DDS },
|
|
|
|
{ L"TGA", CODEC_TGA },
|
|
|
|
{ L"TIF", WIC_CODEC_TIFF },
|
|
|
|
{ L"TIFF", WIC_CODEC_TIFF },
|
|
|
|
{ L"WDP", WIC_CODEC_WMP },
|
2016-06-22 02:58:18 +03:00
|
|
|
{ L"HDP", CODEC_HDP },
|
|
|
|
{ L"JXR", CODEC_JXR },
|
2012-05-02 00:00:24 +04:00
|
|
|
{ nullptr, CODEC_DDS }
|
|
|
|
};
|
|
|
|
|
2013-11-16 04:21:00 +04:00
|
|
|
SValue g_pFeatureLevels[] = // valid feature levels for -fl for maximimum size
|
|
|
|
{
|
|
|
|
{ L"9.1", 2048 },
|
|
|
|
{ L"9.2", 2048 },
|
|
|
|
{ L"9.3", 4096 },
|
|
|
|
{ L"10.0", 8192 },
|
|
|
|
{ L"10.1", 8192 },
|
|
|
|
{ L"11.0", 16384 },
|
|
|
|
{ L"11.1", 16384 },
|
2015-11-03 04:03:41 +03:00
|
|
|
{ L"12.0", 16384 },
|
|
|
|
{ L"12.1", 16384 },
|
2013-11-16 04:21:00 +04:00
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#pragma warning( disable : 4616 6211 )
|
|
|
|
|
|
|
|
inline static bool ispow2(size_t x)
|
|
|
|
{
|
|
|
|
return ((x != 0) && !(x & (x - 1)));
|
|
|
|
}
|
|
|
|
|
2013-03-28 11:14:06 +04:00
|
|
|
#pragma prefast(disable : 26018, "Only used with static internal arrays")
|
|
|
|
|
2016-05-11 21:36:27 +03:00
|
|
|
DWORD LookupByName(const wchar_t *pName, const SValue *pArray)
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
while(pArray->pName)
|
|
|
|
{
|
|
|
|
if(!_wcsicmp(pName, pArray->pName))
|
|
|
|
return pArray->dwValue;
|
|
|
|
|
|
|
|
pArray++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-11 21:36:27 +03:00
|
|
|
const wchar_t* LookupByValue(DWORD pValue, const SValue *pArray)
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
while(pArray->pName)
|
|
|
|
{
|
|
|
|
if(pValue == pArray->dwValue)
|
|
|
|
return pArray->pName;
|
|
|
|
|
|
|
|
pArray++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return L"";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintFormat(DXGI_FORMAT Format)
|
|
|
|
{
|
|
|
|
for(SValue *pFormat = g_pFormats; pFormat->pName; pFormat++)
|
|
|
|
{
|
|
|
|
if((DXGI_FORMAT) pFormat->dwValue == Format)
|
|
|
|
{
|
|
|
|
wprintf( pFormat->pName );
|
2014-07-15 04:22:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(SValue *pFormat = g_pReadOnlyFormats; pFormat->pName; pFormat++)
|
|
|
|
{
|
|
|
|
if((DXGI_FORMAT) pFormat->dwValue == Format)
|
|
|
|
{
|
|
|
|
wprintf( pFormat->pName );
|
|
|
|
return;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 04:22:30 +04:00
|
|
|
|
|
|
|
wprintf( L"*UNKNOWN*" );
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintInfo( const TexMetadata& info )
|
|
|
|
{
|
|
|
|
wprintf( L" (%Iux%Iu", info.width, info.height);
|
|
|
|
|
|
|
|
if ( TEX_DIMENSION_TEXTURE3D == info.dimension )
|
|
|
|
wprintf( L"x%Iu", info.depth);
|
|
|
|
|
|
|
|
if ( info.mipLevels > 1 )
|
|
|
|
wprintf( L",%Iu", info.mipLevels);
|
|
|
|
|
2015-03-18 04:07:21 +03:00
|
|
|
if ( info.arraySize > 1 )
|
|
|
|
wprintf( L",%Iu", info.arraySize);
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L" ");
|
|
|
|
PrintFormat( info.format );
|
|
|
|
|
|
|
|
switch ( info.dimension )
|
|
|
|
{
|
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
|
|
|
wprintf( (info.arraySize > 1) ? L" 1DArray" : L" 1D" );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
2013-03-23 04:52:43 +04:00
|
|
|
if ( info.IsCubemap() )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2013-06-15 10:59:00 +04:00
|
|
|
wprintf( (info.arraySize > 6) ? L" CubeArray" : L" Cube" );
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf( (info.arraySize > 1) ? L" 2DArray" : L" 2D" );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
wprintf( L" 3D");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintf( L")");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrintList(size_t cch, SValue *pValue)
|
|
|
|
{
|
|
|
|
while(pValue->pName)
|
|
|
|
{
|
|
|
|
size_t cchName = wcslen(pValue->pName);
|
|
|
|
|
|
|
|
if(cch + cchName + 2>= 80)
|
|
|
|
{
|
|
|
|
wprintf( L"\n ");
|
|
|
|
cch = 6;
|
|
|
|
}
|
|
|
|
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"%ls ", pValue->pName );
|
2012-05-02 00:00:24 +04:00
|
|
|
cch += cchName + 2;
|
|
|
|
pValue++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wprintf( L"\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrintLogo()
|
|
|
|
{
|
|
|
|
wprintf( L"Microsoft (R) DirectX 11 Texture Converter (DirectXTex version)\n");
|
|
|
|
wprintf( L"Copyright (C) Microsoft Corp. All rights reserved.\n");
|
2015-07-08 00:20:14 +03:00
|
|
|
#ifdef _DEBUG
|
|
|
|
wprintf( L"*** Debug build ***\n");
|
|
|
|
#endif
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
_Success_(return != false)
|
|
|
|
bool GetDXGIFactory(_Outptr_ IDXGIFactory1** pFactory)
|
|
|
|
{
|
|
|
|
if (!pFactory)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*pFactory = nullptr;
|
|
|
|
|
|
|
|
typedef HRESULT(WINAPI* pfn_CreateDXGIFactory1)(REFIID riid, _Out_ void **ppFactory);
|
|
|
|
|
|
|
|
static pfn_CreateDXGIFactory1 s_CreateDXGIFactory1 = nullptr;
|
|
|
|
|
|
|
|
if (!s_CreateDXGIFactory1)
|
|
|
|
{
|
|
|
|
HMODULE hModDXGI = LoadLibrary(L"dxgi.dll");
|
|
|
|
if (!hModDXGI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
s_CreateDXGIFactory1 = reinterpret_cast<pfn_CreateDXGIFactory1>(reinterpret_cast<void*>(GetProcAddress(hModDXGI, "CreateDXGIFactory1")));
|
|
|
|
if (!s_CreateDXGIFactory1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCEEDED(s_CreateDXGIFactory1(IID_PPV_ARGS(pFactory)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
void PrintUsage()
|
|
|
|
{
|
|
|
|
PrintLogo();
|
|
|
|
|
|
|
|
wprintf( L"Usage: texconv <options> <files>\n");
|
|
|
|
wprintf( L"\n");
|
|
|
|
wprintf( L" -w <n> width\n");
|
|
|
|
wprintf( L" -h <n> height\n");
|
|
|
|
wprintf( L" -m <n> miplevels\n");
|
|
|
|
wprintf( L" -f <format> format\n");
|
|
|
|
wprintf( L" -if <filter> image filtering\n");
|
|
|
|
wprintf( L" -srgb{i|o} sRGB {input, output}\n");
|
|
|
|
wprintf( L" -px <string> name prefix\n");
|
|
|
|
wprintf( L" -sx <string> name suffix\n");
|
|
|
|
wprintf( L" -o <directory> output directory\n");
|
|
|
|
wprintf( L" -ft <filetype> output file type\n");
|
|
|
|
wprintf( L" -hflip horizonal flip of source image\n");
|
|
|
|
wprintf( L" -vflip vertical flip of source image\n");
|
2013-06-12 00:41:39 +04:00
|
|
|
wprintf( L" -sepalpha resize/generate mips alpha channel separately\n");
|
|
|
|
wprintf( L" from color channels\n");
|
2013-06-14 09:46:42 +04:00
|
|
|
wprintf( L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n");
|
2013-01-09 01:15:48 +04:00
|
|
|
wprintf( L" -pmalpha convert final texture to use premultiplied alpha\n");
|
2013-11-16 04:21:00 +04:00
|
|
|
wprintf( L" -pow2 resize to fit a power-of-2, respecting aspect ratio\n" );
|
2015-03-11 01:22:08 +03:00
|
|
|
wprintf (L" -nmap <options> converts height-map to normal-map\n"
|
|
|
|
L" options must be one or more of\n"
|
|
|
|
L" r, g, b, a, l, m, u, v, i, o\n" );
|
|
|
|
wprintf (L" -nmapamp <weight> normal map amplitude (defaults to 1.0)\n" );
|
2013-11-16 04:21:00 +04:00
|
|
|
wprintf( L" -fl <feature-level> Set maximum feature level target (defaults to 11.0)\n");
|
2013-06-12 00:41:39 +04:00
|
|
|
wprintf( L"\n (DDS input only)\n");
|
|
|
|
wprintf( L" -t{u|f} TYPELESS format is treated as UNORM or FLOAT\n");
|
|
|
|
wprintf( L" -dword Use DWORD instead of BYTE alignment\n");
|
|
|
|
wprintf( L" -xlum expand legacy L8, L16, and A8P8 formats\n");
|
|
|
|
wprintf( L"\n (DDS output only)\n");
|
|
|
|
wprintf( L" -dx10 Force use of 'DX10' extended header\n");
|
|
|
|
wprintf( L"\n -nologo suppress copyright message\n");
|
2015-04-22 23:31:37 +03:00
|
|
|
wprintf( L" -timing Display elapsed processing time\n\n");
|
2013-07-13 02:12:03 +04:00
|
|
|
#ifdef _OPENMP
|
|
|
|
wprintf( L" -singleproc Do not use multi-threaded compression\n");
|
|
|
|
#endif
|
2016-08-19 02:49:24 +03:00
|
|
|
wprintf( L" -gpu <adapter> Select GPU for DirectCompute-based codecs (0 is default)\n");
|
2013-08-05 21:58:54 +04:00
|
|
|
wprintf( L" -nogpu Do not use DirectCompute-based codecs\n");
|
2015-04-22 23:31:37 +03:00
|
|
|
wprintf( L" -bcuniform Use uniform rather than perceptual weighting for BC1-3\n");
|
|
|
|
wprintf( L" -bcdither Use dithering for BC1-3\n");
|
|
|
|
wprintf( L" -bcmax Use exchaustive compression (BC7 only)\n");
|
2016-06-22 02:58:18 +03:00
|
|
|
wprintf( L" -wicq <quality> When writing images with WIC use quality (0.0 to 1.0)\n");
|
|
|
|
wprintf( L" -wiclossless When writing images with WIC use lossless mode\n");
|
2015-04-22 23:31:37 +03:00
|
|
|
wprintf( L" -aw <weight> BC7 GPU compressor weighting for alpha error metric\n"
|
|
|
|
L" (defaults to 1.0)\n" );
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
wprintf( L"\n");
|
|
|
|
wprintf( L" <format>: ");
|
|
|
|
PrintList(13, g_pFormats);
|
|
|
|
|
|
|
|
wprintf( L"\n");
|
|
|
|
wprintf( L" <filter>: ");
|
|
|
|
PrintList(13, g_pFilters);
|
|
|
|
|
|
|
|
wprintf( L"\n");
|
|
|
|
wprintf( L" <filetype>: ");
|
|
|
|
PrintList(15, g_pSaveFileTypes);
|
2013-11-16 04:21:00 +04:00
|
|
|
|
|
|
|
wprintf( L"\n");
|
|
|
|
wprintf( L" <feature-level>: ");
|
|
|
|
PrintList(13, g_pFeatureLevels);
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
ComPtr<IDXGIFactory1> dxgiFactory;
|
|
|
|
if ( GetDXGIFactory( dxgiFactory.GetAddressOf() ) )
|
|
|
|
{
|
|
|
|
wprintf( L"\n" );
|
|
|
|
wprintf( L" <adapter>:\n" );
|
|
|
|
|
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
|
|
|
for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != dxgiFactory->EnumAdapters(adapterIndex, adapter.ReleaseAndGetAddressOf()); ++adapterIndex)
|
|
|
|
{
|
|
|
|
DXGI_ADAPTER_DESC desc;
|
|
|
|
adapter->GetDesc(&desc);
|
|
|
|
|
|
|
|
wprintf( L" %u: VID:%04X, PID:%04X - %ls\n", adapterIndex, desc.VendorId, desc.DeviceId, desc.Description);
|
|
|
|
}
|
|
|
|
}
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
|
2013-08-05 21:58:54 +04:00
|
|
|
_Success_(return != false)
|
2016-08-19 02:49:24 +03:00
|
|
|
bool CreateDevice( int adapter, _Outptr_ ID3D11Device** pDevice )
|
2013-08-05 21:58:54 +04:00
|
|
|
{
|
|
|
|
if ( !pDevice )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*pDevice = nullptr;
|
|
|
|
|
2014-01-31 04:04:08 +04:00
|
|
|
static PFN_D3D11_CREATE_DEVICE s_DynamicD3D11CreateDevice = nullptr;
|
2013-08-05 21:58:54 +04:00
|
|
|
|
|
|
|
if ( !s_DynamicD3D11CreateDevice )
|
|
|
|
{
|
|
|
|
HMODULE hModD3D11 = LoadLibrary( L"d3d11.dll" );
|
|
|
|
if ( !hModD3D11 )
|
|
|
|
return false;
|
|
|
|
|
2015-01-31 04:31:49 +03:00
|
|
|
s_DynamicD3D11CreateDevice = reinterpret_cast<PFN_D3D11_CREATE_DEVICE>( reinterpret_cast<void*>( GetProcAddress( hModD3D11, "D3D11CreateDevice" ) ) );
|
2013-08-05 21:58:54 +04:00
|
|
|
if ( !s_DynamicD3D11CreateDevice )
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D_FEATURE_LEVEL featureLevels[] =
|
|
|
|
{
|
|
|
|
D3D_FEATURE_LEVEL_11_0,
|
|
|
|
D3D_FEATURE_LEVEL_10_1,
|
|
|
|
D3D_FEATURE_LEVEL_10_0,
|
|
|
|
};
|
|
|
|
|
|
|
|
UINT createDeviceFlags = 0;
|
|
|
|
#ifdef _DEBUG
|
|
|
|
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
|
|
|
|
#endif
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
ComPtr<IDXGIAdapter> pAdapter;
|
|
|
|
if ( adapter >= 0 )
|
|
|
|
{
|
|
|
|
ComPtr<IDXGIFactory1> dxgiFactory;
|
|
|
|
if ( GetDXGIFactory( dxgiFactory.GetAddressOf() ) )
|
|
|
|
{
|
|
|
|
if ( FAILED(dxgiFactory->EnumAdapters( adapter, pAdapter.GetAddressOf() )) )
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Invalid GPU adapter index (%d)!\n", adapter);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:58:54 +04:00
|
|
|
D3D_FEATURE_LEVEL fl;
|
2016-08-19 02:57:03 +03:00
|
|
|
HRESULT hr = s_DynamicD3D11CreateDevice( pAdapter.Get(),
|
|
|
|
(pAdapter) ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
|
|
|
|
nullptr, createDeviceFlags, featureLevels, _countof(featureLevels),
|
2013-08-05 21:58:54 +04:00
|
|
|
D3D11_SDK_VERSION, pDevice, &fl, nullptr );
|
|
|
|
if ( SUCCEEDED(hr) )
|
|
|
|
{
|
|
|
|
if ( fl < D3D_FEATURE_LEVEL_11_0 )
|
|
|
|
{
|
|
|
|
D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS hwopts;
|
|
|
|
hr = (*pDevice)->CheckFeatureSupport( D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS, &hwopts, sizeof(hwopts) );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
memset( &hwopts, 0, sizeof(hwopts) );
|
|
|
|
|
|
|
|
if ( !hwopts.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x )
|
|
|
|
{
|
2013-10-09 03:26:29 +04:00
|
|
|
if ( *pDevice )
|
|
|
|
{
|
|
|
|
(*pDevice)->Release();
|
|
|
|
*pDevice = nullptr;
|
|
|
|
}
|
2013-08-05 21:58:54 +04:00
|
|
|
hr = HRESULT_FROM_WIN32( ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 03:26:29 +04:00
|
|
|
if ( SUCCEEDED(hr) )
|
2013-08-05 21:58:54 +04:00
|
|
|
{
|
2014-10-07 01:08:06 +04:00
|
|
|
ComPtr<IDXGIDevice> dxgiDevice;
|
2016-04-19 00:45:56 +03:00
|
|
|
hr = (*pDevice)->QueryInterface( IID_PPV_ARGS( dxgiDevice.GetAddressOf() ) );
|
2013-10-09 03:26:29 +04:00
|
|
|
if ( SUCCEEDED(hr) )
|
|
|
|
{
|
2016-08-19 02:49:24 +03:00
|
|
|
hr = dxgiDevice->GetAdapter( pAdapter.ReleaseAndGetAddressOf() );
|
2013-10-09 03:26:29 +04:00
|
|
|
if ( SUCCEEDED(hr) )
|
|
|
|
{
|
|
|
|
DXGI_ADAPTER_DESC desc;
|
|
|
|
hr = pAdapter->GetDesc( &desc );
|
|
|
|
if ( SUCCEEDED(hr) )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"\n[Using DirectCompute on \"%ls\"]\n", desc.Description );
|
2013-10-09 03:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2013-08-05 21:58:54 +04:00
|
|
|
}
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
|
2013-11-16 04:21:00 +04:00
|
|
|
void FitPowerOf2( size_t origx, size_t origy, size_t& targetx, size_t& targety, size_t maxsize )
|
|
|
|
{
|
|
|
|
float origAR = float(origx) / float(origy);
|
|
|
|
|
|
|
|
if ( origx > origy )
|
|
|
|
{
|
|
|
|
size_t x;
|
|
|
|
for( x = maxsize; x > 1; x >>= 1 ) { if ( x <= targetx ) break; };
|
|
|
|
targetx = x;
|
|
|
|
|
|
|
|
float bestScore = FLT_MAX;
|
|
|
|
for( size_t y = maxsize; y > 0; y >>= 1 )
|
|
|
|
{
|
|
|
|
float score = fabs( (float(x) / float(y)) - origAR );
|
|
|
|
if ( score < bestScore )
|
|
|
|
{
|
|
|
|
bestScore = score;
|
|
|
|
targety = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t y;
|
|
|
|
for( y = maxsize; y > 1; y >>= 1 ) { if ( y <= targety ) break; };
|
|
|
|
targety = y;
|
|
|
|
|
|
|
|
float bestScore = FLT_MAX;
|
|
|
|
for( size_t x = maxsize; x > 0; x >>= 1 )
|
|
|
|
{
|
|
|
|
float score = fabs( (float(x) / float(y)) - origAR );
|
|
|
|
if ( score < bestScore )
|
|
|
|
{
|
|
|
|
bestScore = score;
|
|
|
|
targetx = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Entry-point
|
|
|
|
//--------------------------------------------------------------------------------------
|
2013-03-28 11:14:06 +04:00
|
|
|
#pragma prefast(disable : 28198, "Command-line tool, frees all memory on exit")
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
|
|
|
|
{
|
|
|
|
// Parameters and defaults
|
|
|
|
size_t width = 0;
|
|
|
|
size_t height = 0;
|
|
|
|
size_t mipLevels = 0;
|
|
|
|
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
|
|
|
|
DWORD dwFilter = TEX_FILTER_DEFAULT;
|
|
|
|
DWORD dwSRGB = 0;
|
2015-04-22 23:31:37 +03:00
|
|
|
DWORD dwCompress = TEX_COMPRESS_DEFAULT;
|
2012-05-02 00:00:24 +04:00
|
|
|
DWORD dwFilterOpts = 0;
|
|
|
|
DWORD FileType = CODEC_DDS;
|
2013-11-16 04:21:00 +04:00
|
|
|
DWORD maxSize = 16384;
|
2016-08-19 02:49:24 +03:00
|
|
|
int adapter = -1;
|
2014-02-20 05:31:35 +04:00
|
|
|
float alphaWeight = 1.f;
|
2015-03-11 01:22:08 +03:00
|
|
|
DWORD dwNormalMap = 0;
|
|
|
|
float nmapAmplitude = 1.f;
|
2016-06-22 02:58:18 +03:00
|
|
|
float wicQuality = -1.f;
|
|
|
|
bool wicLossless = false;
|
2015-07-27 22:46:54 +03:00
|
|
|
|
2016-05-11 21:36:27 +03:00
|
|
|
wchar_t szPrefix [MAX_PATH];
|
|
|
|
wchar_t szSuffix [MAX_PATH];
|
|
|
|
wchar_t szOutputDir[MAX_PATH];
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
szPrefix[0] = 0;
|
|
|
|
szSuffix[0] = 0;
|
|
|
|
szOutputDir[0] = 0;
|
|
|
|
|
|
|
|
// Initialize COM (needed for WIC)
|
2014-10-07 01:08:06 +04:00
|
|
|
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
|
|
|
if( FAILED(hr) )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
wprintf( L"Failed to initialize COM (%08X)\n", hr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process command line
|
2015-04-22 23:31:37 +03:00
|
|
|
DWORD64 dwOptions = 0;
|
2014-10-07 01:08:06 +04:00
|
|
|
std::list<SConversion> conversion;
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
for(int iArg = 1; iArg < argc; iArg++)
|
|
|
|
{
|
|
|
|
PWSTR pArg = argv[iArg];
|
|
|
|
|
|
|
|
if(('-' == pArg[0]) || ('/' == pArg[0]))
|
|
|
|
{
|
|
|
|
pArg++;
|
|
|
|
PWSTR pValue;
|
|
|
|
|
|
|
|
for(pValue = pArg; *pValue && (':' != *pValue); pValue++);
|
|
|
|
|
|
|
|
if(*pValue)
|
|
|
|
*pValue++ = 0;
|
|
|
|
|
|
|
|
DWORD dwOption = LookupByName(pArg, g_pOptions);
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if(!dwOption || (dwOptions & (DWORD64(1) << dwOption)))
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
dwOptions |= (DWORD64(1) << dwOption);
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2016-06-22 02:58:18 +03:00
|
|
|
// Handle options with additional value parameter
|
|
|
|
switch (dwOption)
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2016-06-22 02:58:18 +03:00
|
|
|
case OPT_WIDTH:
|
|
|
|
case OPT_HEIGHT:
|
|
|
|
case OPT_MIPLEVELS:
|
|
|
|
case OPT_FORMAT:
|
|
|
|
case OPT_FILTER:
|
|
|
|
case OPT_PREFIX:
|
|
|
|
case OPT_SUFFIX:
|
|
|
|
case OPT_OUTPUTDIR:
|
|
|
|
case OPT_FILETYPE:
|
2016-08-19 02:49:24 +03:00
|
|
|
case OPT_GPU:
|
2016-06-22 02:58:18 +03:00
|
|
|
case OPT_FEATURE_LEVEL:
|
|
|
|
case OPT_ALPHA_WEIGHT:
|
|
|
|
case OPT_NORMAL_MAP:
|
|
|
|
case OPT_NORMAL_MAP_AMPLITUDE:
|
|
|
|
case OPT_WIC_QUALITY:
|
|
|
|
if (!*pValue)
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2016-06-22 02:58:18 +03:00
|
|
|
if ((iArg + 1 >= argc))
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iArg++;
|
|
|
|
pValue = argv[iArg];
|
|
|
|
}
|
2016-06-22 02:58:18 +03:00
|
|
|
break;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(dwOption)
|
|
|
|
{
|
|
|
|
case OPT_WIDTH:
|
|
|
|
if (swscanf_s(pValue, L"%Iu", &width) != 1)
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -w (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_HEIGHT:
|
|
|
|
if (swscanf_s(pValue, L"%Iu", &height) != 1)
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -h (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
printf("\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_MIPLEVELS:
|
|
|
|
if (swscanf_s(pValue, L"%Iu", &mipLevels) != 1)
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -m (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FORMAT:
|
|
|
|
format = (DXGI_FORMAT) LookupByName(pValue, g_pFormats);
|
|
|
|
if ( !format )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -f (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FILTER:
|
|
|
|
dwFilter = LookupByName(pValue, g_pFilters);
|
|
|
|
if ( !dwFilter )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -if (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SRGBI:
|
|
|
|
dwSRGB |= TEX_FILTER_SRGB_IN;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SRGBO:
|
|
|
|
dwSRGB |= TEX_FILTER_SRGB_OUT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SRGB:
|
|
|
|
dwSRGB |= TEX_FILTER_SRGB;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SEPALPHA:
|
|
|
|
dwFilterOpts |= TEX_FILTER_SEPARATE_ALPHA;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_PREFIX:
|
|
|
|
wcscpy_s(szPrefix, MAX_PATH, pValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SUFFIX:
|
|
|
|
wcscpy_s(szSuffix, MAX_PATH, pValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_OUTPUTDIR:
|
|
|
|
wcscpy_s(szOutputDir, MAX_PATH, pValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FILETYPE:
|
|
|
|
FileType = LookupByName(pValue, g_pSaveFileTypes);
|
|
|
|
if ( !FileType )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -ft (%ls)\n", pValue);
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-14 09:46:42 +04:00
|
|
|
|
|
|
|
case OPT_TA_WRAP:
|
|
|
|
if ( dwFilterOpts & TEX_FILTER_MIRROR )
|
|
|
|
{
|
|
|
|
wprintf( L"Can't use -wrap and -mirror at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dwFilterOpts |= TEX_FILTER_WRAP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_TA_MIRROR:
|
|
|
|
if ( dwFilterOpts & TEX_FILTER_WRAP )
|
|
|
|
{
|
|
|
|
wprintf( L"Can't use -wrap and -mirror at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dwFilterOpts |= TEX_FILTER_MIRROR;
|
|
|
|
break;
|
2013-11-16 04:21:00 +04:00
|
|
|
|
2015-03-11 01:22:08 +03:00
|
|
|
case OPT_NORMAL_MAP:
|
|
|
|
{
|
|
|
|
dwNormalMap = 0;
|
|
|
|
|
|
|
|
if ( wcschr( pValue, L'l' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_CHANNEL_LUMINANCE;
|
|
|
|
}
|
|
|
|
else if ( wcschr( pValue, L'r' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_CHANNEL_RED;
|
|
|
|
}
|
|
|
|
else if ( wcschr( pValue, L'g' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_CHANNEL_GREEN;
|
|
|
|
}
|
|
|
|
else if ( wcschr( pValue, L'b' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_CHANNEL_BLUE;
|
|
|
|
}
|
|
|
|
else if ( wcschr( pValue, L'a' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_CHANNEL_ALPHA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf( L"Invalid value specified for -nmap (%ls), missing l, r, g, b, or a\n\n", pValue );
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wcschr( pValue, L'm' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_MIRROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( wcschr( pValue, L'u' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_MIRROR_U;
|
|
|
|
}
|
|
|
|
if ( wcschr( pValue, L'v' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_MIRROR_V;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wcschr( pValue, L'i' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_INVERT_SIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( wcschr( pValue, L'o' ) )
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_COMPUTE_OCCLUSION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_NORMAL_MAP_AMPLITUDE:
|
|
|
|
if ( !dwNormalMap )
|
|
|
|
{
|
|
|
|
wprintf( L"-nmapamp requires -nmap\n\n" );
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (swscanf_s(pValue, L"%f", &nmapAmplitude) != 1)
|
|
|
|
{
|
|
|
|
wprintf( L"Invalid value specified with -nmapamp (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( nmapAmplitude < 0.f )
|
|
|
|
{
|
|
|
|
wprintf( L"Normal map amplitude must be positive (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-19 02:49:24 +03:00
|
|
|
case OPT_GPU:
|
|
|
|
if (swscanf_s(pValue, L"%d", &adapter) != 1)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -gpu (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (adapter < 0)
|
|
|
|
{
|
|
|
|
wprintf(L"Adapter index (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-16 04:21:00 +04:00
|
|
|
case OPT_FEATURE_LEVEL:
|
|
|
|
maxSize = LookupByName( pValue, g_pFeatureLevels );
|
|
|
|
if ( !maxSize )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -fl (%ls)\n", pValue);
|
2013-11-16 04:21:00 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2014-02-20 05:31:35 +04:00
|
|
|
|
|
|
|
case OPT_ALPHA_WEIGHT:
|
|
|
|
if (swscanf_s(pValue, L"%f", &alphaWeight) != 1)
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"Invalid value specified with -aw (%ls)\n", pValue);
|
2014-02-21 04:13:36 +04:00
|
|
|
wprintf( L"\n");
|
2014-02-20 05:31:35 +04:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( alphaWeight < 0.f )
|
|
|
|
{
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"-aw (%ls) parameter must be positive\n", pValue);
|
2014-02-21 04:13:36 +04:00
|
|
|
wprintf( L"\n");
|
2014-02-20 05:31:35 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2015-04-22 23:31:37 +03:00
|
|
|
|
|
|
|
case OPT_COMPRESS_UNIFORM:
|
|
|
|
dwCompress |= TEX_COMPRESS_UNIFORM;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_COMPRESS_MAX:
|
|
|
|
dwCompress |= TEX_COMPRESS_BC7_USE_3SUBSETS;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_COMPRESS_DITHER:
|
|
|
|
dwCompress |= TEX_COMPRESS_DITHER;
|
|
|
|
break;
|
2016-06-22 02:58:18 +03:00
|
|
|
|
|
|
|
case OPT_WIC_QUALITY:
|
|
|
|
if (swscanf_s(pValue, L"%f", &wicQuality) != 1
|
|
|
|
|| (wicQuality < 0.f)
|
|
|
|
|| (wicQuality > 1.f))
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -wicq (%ls)\n", pValue);
|
|
|
|
printf("\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_WIC_LOSSLESS:
|
|
|
|
wicLossless = true;
|
|
|
|
break;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-07-27 22:46:54 +03:00
|
|
|
{
|
2014-10-07 01:08:06 +04:00
|
|
|
SConversion conv;
|
|
|
|
wcscpy_s(conv.szSrc, MAX_PATH, pArg);
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
conv.szDest[0] = 0;
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
conversion.push_back(conv);
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
if(conversion.empty())
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if(~dwOptions & (DWORD64(1) << OPT_NOLOGO))
|
2012-05-02 00:00:24 +04:00
|
|
|
PrintLogo();
|
|
|
|
|
|
|
|
// Work out out filename prefix and suffix
|
|
|
|
if(szOutputDir[0] && (L'\\' != szOutputDir[wcslen(szOutputDir) - 1]))
|
|
|
|
wcscat_s( szOutputDir, MAX_PATH, L"\\" );
|
|
|
|
|
|
|
|
if(szPrefix[0])
|
|
|
|
wcscat_s(szOutputDir, MAX_PATH, szPrefix);
|
|
|
|
|
|
|
|
wcscpy_s(szPrefix, MAX_PATH, szOutputDir);
|
|
|
|
|
2016-05-11 21:36:27 +03:00
|
|
|
const wchar_t* fileTypeName = LookupByValue(FileType, g_pSaveFileTypes);
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
if (fileTypeName)
|
|
|
|
{
|
|
|
|
wcscat_s(szSuffix, MAX_PATH, L".");
|
|
|
|
wcscat_s(szSuffix, MAX_PATH, fileTypeName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wcscat_s(szSuffix, MAX_PATH, L".unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FileType != CODEC_DDS)
|
|
|
|
{
|
|
|
|
mipLevels = 1;
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
LARGE_INTEGER qpcFreq;
|
|
|
|
if ( !QueryPerformanceFrequency( &qpcFreq ) )
|
|
|
|
{
|
|
|
|
qpcFreq.QuadPart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LARGE_INTEGER qpcStart;
|
|
|
|
if ( !QueryPerformanceCounter( &qpcStart ) )
|
|
|
|
{
|
|
|
|
qpcStart.QuadPart = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
// Convert images
|
|
|
|
bool nonpow2warn = false;
|
2013-07-31 04:32:45 +04:00
|
|
|
bool non4bc = false;
|
2014-10-07 01:08:06 +04:00
|
|
|
ComPtr<ID3D11Device> pDevice;
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
for( auto pConv = conversion.begin(); pConv != conversion.end(); ++pConv )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2014-10-07 01:08:06 +04:00
|
|
|
if ( pConv != conversion.begin() )
|
2012-05-02 00:00:24 +04:00
|
|
|
wprintf( L"\n");
|
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
// Load source image
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"reading %ls", pConv->szSrc );
|
2012-05-02 00:00:24 +04:00
|
|
|
fflush(stdout);
|
|
|
|
|
2016-05-11 21:36:27 +03:00
|
|
|
wchar_t ext[_MAX_EXT];
|
|
|
|
wchar_t fname[_MAX_FNAME];
|
2012-05-02 00:00:24 +04:00
|
|
|
_wsplitpath_s( pConv->szSrc, nullptr, 0, nullptr, 0, fname, _MAX_FNAME, ext, _MAX_EXT );
|
|
|
|
|
|
|
|
TexMetadata info;
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> image( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
if ( !image )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( _wcsicmp( ext, L".dds" ) == 0 )
|
|
|
|
{
|
2013-06-12 00:34:48 +04:00
|
|
|
DWORD ddsFlags = DDS_FLAGS_NONE;
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_DDS_DWORD_ALIGN) )
|
2013-06-12 00:34:48 +04:00
|
|
|
ddsFlags |= DDS_FLAGS_LEGACY_DWORD;
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_EXPAND_LUMINANCE) )
|
2013-06-12 00:34:48 +04:00
|
|
|
ddsFlags |= DDS_FLAGS_EXPAND_LUMINANCE;
|
|
|
|
|
|
|
|
hr = LoadFromDDSFile( pConv->szSrc, ddsFlags, &info, *image );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-03 00:31:12 +04:00
|
|
|
|
|
|
|
if ( IsTypeless( info.format ) )
|
|
|
|
{
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_TYPELESS_UNORM) )
|
2012-07-03 00:31:12 +04:00
|
|
|
{
|
|
|
|
info.format = MakeTypelessUNORM( info.format );
|
|
|
|
}
|
2015-04-22 23:31:37 +03:00
|
|
|
else if ( dwOptions & (DWORD64(1) << OPT_TYPELESS_FLOAT) )
|
2012-07-03 00:31:12 +04:00
|
|
|
{
|
|
|
|
info.format = MakeTypelessFLOAT( info.format );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( IsTypeless( info.format ) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED due to Typeless format %d\n", info.format );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->OverrideFormat( info.format );
|
|
|
|
}
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
else if ( _wcsicmp( ext, L".tga" ) == 0 )
|
|
|
|
{
|
|
|
|
hr = LoadFromTGAFile( pConv->szSrc, &info, *image );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// WIC shares the same filter values for mode and dither
|
|
|
|
static_assert( WIC_FLAGS_DITHER == TEX_FILTER_DITHER, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
static_assert( WIC_FLAGS_DITHER_DIFFUSION == TEX_FILTER_DITHER_DIFFUSION, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
static_assert( WIC_FLAGS_FILTER_POINT == TEX_FILTER_POINT, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
static_assert( WIC_FLAGS_FILTER_LINEAR == TEX_FILTER_LINEAR, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
static_assert( WIC_FLAGS_FILTER_CUBIC == TEX_FILTER_CUBIC, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
static_assert( WIC_FLAGS_FILTER_FANT == TEX_FILTER_FANT, "WIC_FLAGS_* & TEX_FILTER_* should match" );
|
|
|
|
|
2015-03-18 04:07:21 +03:00
|
|
|
DWORD wicFlags = dwFilter;
|
|
|
|
if (FileType == CODEC_DDS)
|
|
|
|
wicFlags |= WIC_FLAGS_ALL_FRAMES;
|
2015-07-27 22:46:54 +03:00
|
|
|
|
2015-03-18 04:07:21 +03:00
|
|
|
hr = LoadFromWICFile( pConv->szSrc, wicFlags, &info, *image );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintInfo( info );
|
|
|
|
|
|
|
|
size_t tMips = ( !mipLevels && info.mipLevels > 1 ) ? info.mipLevels : mipLevels;
|
|
|
|
|
2013-11-16 04:21:00 +04:00
|
|
|
bool sizewarn = false;
|
|
|
|
|
|
|
|
size_t twidth = ( !width ) ? info.width : width;
|
|
|
|
if ( twidth > maxSize )
|
|
|
|
{
|
|
|
|
if ( !width )
|
|
|
|
twidth = maxSize;
|
|
|
|
else
|
|
|
|
sizewarn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t theight = ( !height ) ? info.height : height;
|
|
|
|
if ( theight > maxSize )
|
|
|
|
{
|
|
|
|
if ( !height )
|
|
|
|
theight = maxSize;
|
|
|
|
else
|
|
|
|
sizewarn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( sizewarn )
|
|
|
|
{
|
|
|
|
wprintf( L"\nWARNING: Target size exceeds maximum size for feature level (%u)\n", maxSize );
|
|
|
|
}
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if (dwOptions & (DWORD64(1) << OPT_FIT_POWEROF2))
|
2013-11-16 04:21:00 +04:00
|
|
|
{
|
|
|
|
FitPowerOf2( info.width, info.height, twidth, theight, maxSize );
|
|
|
|
}
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
// Convert texture
|
|
|
|
wprintf( L" as");
|
|
|
|
fflush(stdout);
|
|
|
|
|
2014-02-15 01:33:01 +04:00
|
|
|
// --- Planar ------------------------------------------------------------------
|
|
|
|
if ( IsPlanar( info.format ) )
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
auto img = image->GetImage(0,0,0);
|
2014-02-15 01:33:01 +04:00
|
|
|
assert( img );
|
|
|
|
size_t nimg = image->GetImageCount();
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2014-02-15 01:33:01 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2014-02-15 01:33:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = ConvertToSinglePlane( img, nimg, info, *timage );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [converttosingeplane] (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2014-02-15 01:33:01 +04:00
|
|
|
|
|
|
|
info.format = tinfo.format;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
2014-02-15 01:33:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DXGI_FORMAT tformat = ( format == DXGI_FORMAT_UNKNOWN ) ? info.format : format;
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
// --- Decompress --------------------------------------------------------------
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> cimage;
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( IsCompressed( info.format ) )
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
auto img = image->GetImage(0,0,0);
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( img );
|
|
|
|
size_t nimg = image->GetImageCount();
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = Decompress( img, nimg, info, DXGI_FORMAT_UNKNOWN /* picks good default */, *timage );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [decompress] (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
info.format = tinfo.format;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( FileType == CODEC_DDS )
|
|
|
|
{
|
|
|
|
// Keep the original compressed image in case we can reuse it
|
|
|
|
cimage.reset( image.release() );
|
|
|
|
image.reset( timage.release() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
image.swap( timage );
|
|
|
|
}
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Flip/Rotate -------------------------------------------------------------
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & ( (DWORD64(1) << OPT_HFLIP) | (DWORD64(1) << OPT_VFLIP) ) )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
DWORD dwFlags = 0;
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_HFLIP) )
|
2012-05-02 00:00:24 +04:00
|
|
|
dwFlags |= TEX_FR_FLIP_HORIZONTAL;
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_VFLIP) )
|
2012-05-02 00:00:24 +04:00
|
|
|
dwFlags |= TEX_FR_FLIP_VERTICAL;
|
|
|
|
|
|
|
|
assert( dwFlags != 0 );
|
|
|
|
|
|
|
|
hr = FlipRotate( image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFlags, *timage );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [fliprotate] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
assert( tinfo.width == twidth && tinfo.height == theight );
|
|
|
|
|
|
|
|
info.width = tinfo.width;
|
|
|
|
info.height = tinfo.height;
|
|
|
|
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( info.format == tinfo.format );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Resize ------------------------------------------------------------------
|
|
|
|
if ( info.width != twidth || info.height != theight )
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
hr = Resize( image->GetImages(), image->GetImageCount(), image->GetMetadata(), twidth, theight, dwFilter | dwFilterOpts, *timage );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [resize] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
assert( tinfo.width == twidth && tinfo.height == theight && tinfo.mipLevels == 1 );
|
|
|
|
info.width = tinfo.width;
|
|
|
|
info.height = tinfo.height;
|
|
|
|
info.mipLevels = 1;
|
|
|
|
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( info.format == tinfo.format );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Convert -----------------------------------------------------------------
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( dwOptions & (DWORD64(1) << OPT_NORMAL_MAP) )
|
2015-03-11 01:22:08 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DXGI_FORMAT nmfmt = tformat;
|
|
|
|
if ( IsCompressed( tformat ) )
|
|
|
|
{
|
|
|
|
nmfmt = (dwNormalMap & CNMAP_COMPUTE_OCCLUSION) ? DXGI_FORMAT_R32G32B32A32_FLOAT : DXGI_FORMAT_R32G32B32_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = ComputeNormalMap( image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwNormalMap, nmapAmplitude, nmfmt, *timage );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [normalmap] (%x)\n", hr);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
|
|
|
assert( tinfo.format == nmfmt );
|
|
|
|
info.format = tinfo.format;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
else if ( info.format != tformat && !IsCompressed( tformat ) )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2013-06-08 05:07:08 +04:00
|
|
|
hr = Convert( image->GetImages(), image->GetImageCount(), image->GetMetadata(), tformat, dwFilter | dwFilterOpts | dwSRGB, 0.5f, *timage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [convert] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
assert( tinfo.format == tformat );
|
|
|
|
info.format = tinfo.format;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Generate mips -----------------------------------------------------------
|
|
|
|
if ( !ispow2(info.width) || !ispow2(info.height) || !ispow2(info.depth) )
|
|
|
|
{
|
|
|
|
if ( info.dimension == TEX_DIMENSION_TEXTURE3D )
|
|
|
|
{
|
|
|
|
if ( !tMips )
|
|
|
|
{
|
|
|
|
tMips = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Cannot generate mips for non-power-of-2 volume textures\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( !tMips || info.mipLevels != 1 )
|
|
|
|
{
|
|
|
|
nonpow2warn = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 23:27:41 +04:00
|
|
|
if ( (!tMips || info.mipLevels != tMips) && ( info.mipLevels != 1 ) )
|
|
|
|
{
|
|
|
|
// Mips generation only works on a single base image, so strip off existing mip levels
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-09-06 23:27:41 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-09-06 23:27:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
TexMetadata mdata = info;
|
|
|
|
mdata.mipLevels = 1;
|
|
|
|
hr = timage->Initialize( mdata );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [copy to single level] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-09-06 23:27:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( info.dimension == TEX_DIMENSION_TEXTURE3D )
|
|
|
|
{
|
|
|
|
for( size_t d = 0; d < info.depth; ++d )
|
|
|
|
{
|
|
|
|
hr = CopyRectangle( *image->GetImage( 0, 0, d ), Rect( 0, 0, info.width, info.height ),
|
|
|
|
*timage->GetImage( 0, 0, d ), TEX_FILTER_DEFAULT, 0, 0 );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [copy to single level] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-09-06 23:27:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < info.arraySize; ++i )
|
|
|
|
{
|
|
|
|
hr = CopyRectangle( *image->GetImage( 0, i, 0 ), Rect( 0, 0, info.width, info.height ),
|
|
|
|
*timage->GetImage( 0, i, 0 ), TEX_FILTER_DEFAULT, 0, 0 );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [copy to single level] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-09-06 23:27:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
info.mipLevels = image->GetMetadata().mipLevels;
|
2012-09-06 23:27:41 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( cimage && ( tMips == 1 ) )
|
|
|
|
{
|
|
|
|
// Special case for trimming mips off compressed images and keeping the original compressed highest level mip
|
|
|
|
mdata = cimage->GetMetadata();
|
|
|
|
mdata.mipLevels = 1;
|
|
|
|
hr = timage->Initialize( mdata );
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [copy compressed to single level] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2014-07-15 04:22:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( mdata.dimension == TEX_DIMENSION_TEXTURE3D )
|
|
|
|
{
|
|
|
|
for( size_t d = 0; d < mdata.depth; ++d )
|
|
|
|
{
|
|
|
|
auto simg = cimage->GetImage( 0, 0, d );
|
|
|
|
auto dimg = timage->GetImage( 0, 0, d );
|
|
|
|
|
|
|
|
memcpy_s( dimg->pixels, dimg->slicePitch, simg->pixels, simg->slicePitch );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( size_t i = 0; i < mdata.arraySize; ++i )
|
|
|
|
{
|
|
|
|
auto simg = cimage->GetImage( 0, i, 0 );
|
|
|
|
auto dimg = timage->GetImage( 0, i, 0 );
|
|
|
|
|
|
|
|
memcpy_s( dimg->pixels, dimg->slicePitch, simg->pixels, simg->slicePitch );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cimage.swap( timage );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cimage.reset();
|
|
|
|
}
|
2012-09-06 23:27:41 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( ( !tMips || info.mipLevels != tMips ) && ( info.width > 1 || info.height > 1 || info.depth > 1 ) )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2012-05-02 00:00:24 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( info.dimension == TEX_DIMENSION_TEXTURE3D )
|
|
|
|
{
|
|
|
|
hr = GenerateMipMaps3D( image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFilter | dwFilterOpts, tMips, *timage );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = GenerateMipMaps( image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFilter | dwFilterOpts, tMips, *timage );
|
|
|
|
}
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [mipmaps] (%x)\n", hr);
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
info.mipLevels = tinfo.mipLevels;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-14 00:12:35 +04:00
|
|
|
// --- Premultiplied alpha (if requested) --------------------------------------
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( ( dwOptions & (DWORD64(1) << OPT_PREMUL_ALPHA) )
|
2012-12-14 00:12:35 +04:00
|
|
|
&& HasAlpha( info.format )
|
|
|
|
&& info.format != DXGI_FORMAT_A8_UNORM )
|
|
|
|
{
|
2013-03-23 04:52:43 +04:00
|
|
|
if ( info.IsPMAlpha() )
|
2012-12-14 00:12:35 +04:00
|
|
|
{
|
2013-03-23 04:52:43 +04:00
|
|
|
printf("WARNING: Image is already using premultiplied alpha\n");
|
2012-12-14 00:12:35 +04:00
|
|
|
}
|
2013-03-23 04:52:43 +04:00
|
|
|
else
|
2012-12-14 00:12:35 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
auto img = image->GetImage(0,0,0);
|
2013-03-23 04:52:43 +04:00
|
|
|
assert( img );
|
|
|
|
size_t nimg = image->GetImageCount();
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
2013-03-23 04:52:43 +04:00
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2013-03-23 04:52:43 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 04:28:56 +04:00
|
|
|
hr = PremultiplyAlpha( img, nimg, info, dwSRGB, *timage );
|
2013-03-23 04:52:43 +04:00
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [premultiply alpha] (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2013-03-23 04:52:43 +04:00
|
|
|
info.miscFlags2 = tinfo.miscFlags2;
|
|
|
|
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
cimage.reset();
|
2012-12-14 00:12:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
// --- Compress ----------------------------------------------------------------
|
|
|
|
if ( IsCompressed( tformat ) && (FileType == CODEC_DDS) )
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( cimage && ( cimage->GetMetadata().format == tformat ) )
|
2012-05-02 00:00:24 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
// We never changed the image and it was already compressed in our desired format, use original data
|
|
|
|
image.reset( cimage.release() );
|
|
|
|
|
|
|
|
auto& tinfo = image->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( (tinfo.width % 4) != 0 || (tinfo.height % 4) != 0 )
|
|
|
|
{
|
|
|
|
non4bc = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.format = tinfo.format;
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
|
|
|
}
|
|
|
|
else
|
2013-07-13 02:12:03 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
cimage.reset();
|
|
|
|
|
|
|
|
auto img = image->GetImage(0,0,0);
|
|
|
|
assert( img );
|
|
|
|
size_t nimg = image->GetImageCount();
|
|
|
|
|
|
|
|
std::unique_ptr<ScratchImage> timage( new (std::nothrow) ScratchImage );
|
|
|
|
if ( !timage )
|
|
|
|
{
|
|
|
|
wprintf( L" ERROR: Memory allocation failed\n" );
|
2014-10-07 01:08:06 +04:00
|
|
|
return 1;
|
2014-07-15 04:22:30 +04:00
|
|
|
}
|
2013-08-05 21:58:54 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
bool bc6hbc7=false;
|
|
|
|
switch( tformat )
|
2013-07-13 02:12:03 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
case DXGI_FORMAT_BC6H_TYPELESS:
|
|
|
|
case DXGI_FORMAT_BC6H_UF16:
|
|
|
|
case DXGI_FORMAT_BC6H_SF16:
|
|
|
|
case DXGI_FORMAT_BC7_TYPELESS:
|
|
|
|
case DXGI_FORMAT_BC7_UNORM:
|
|
|
|
case DXGI_FORMAT_BC7_UNORM_SRGB:
|
|
|
|
bc6hbc7=true;
|
2013-08-05 21:58:54 +04:00
|
|
|
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
static bool s_tryonce = false;
|
2013-08-05 21:58:54 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( !s_tryonce )
|
2013-08-05 21:58:54 +04:00
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
s_tryonce = true;
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if ( !(dwOptions & (DWORD64(1) << OPT_NOGPU) ) )
|
2014-07-15 04:22:30 +04:00
|
|
|
{
|
2016-08-19 02:49:24 +03:00
|
|
|
if ( !CreateDevice( adapter, pDevice.GetAddressOf() ) )
|
2014-07-15 04:22:30 +04:00
|
|
|
wprintf( L"\nWARNING: DirectCompute is not available, using BC6H / BC7 CPU codec\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf( L"\nWARNING: using BC6H / BC7 CPU codec\n" );
|
|
|
|
}
|
2013-08-05 21:58:54 +04:00
|
|
|
}
|
|
|
|
}
|
2014-07-15 04:22:30 +04:00
|
|
|
break;
|
2013-07-13 02:12:03 +04:00
|
|
|
}
|
2013-08-05 21:58:54 +04:00
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
DWORD cflags = dwCompress;
|
2013-08-05 21:58:54 +04:00
|
|
|
#ifdef _OPENMP
|
2015-07-08 00:20:14 +03:00
|
|
|
if ( !(dwOptions & (DWORD64(1) << OPT_FORCE_SINGLEPROC) ) )
|
2014-07-15 04:22:30 +04:00
|
|
|
{
|
|
|
|
cflags |= TEX_COMPRESS_PARALLEL;
|
|
|
|
}
|
2013-07-13 02:12:03 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( (img->width % 4) != 0 || (img->height % 4) != 0 )
|
|
|
|
{
|
|
|
|
non4bc = true;
|
|
|
|
}
|
2013-07-31 04:32:45 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
if ( bc6hbc7 && pDevice )
|
|
|
|
{
|
2015-04-22 23:31:37 +03:00
|
|
|
hr = Compress( pDevice.Get(), img, nimg, info, tformat, dwCompress | dwSRGB, alphaWeight, *timage );
|
2014-07-15 04:22:30 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = Compress( img, nimg, info, tformat, cflags | dwSRGB, 0.5f, *timage );
|
|
|
|
}
|
|
|
|
if ( FAILED(hr) )
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED [compress] (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
info.format = tinfo.format;
|
|
|
|
assert( info.width == tinfo.width );
|
|
|
|
assert( info.height == tinfo.height );
|
|
|
|
assert( info.depth == tinfo.depth );
|
|
|
|
assert( info.arraySize == tinfo.arraySize );
|
|
|
|
assert( info.mipLevels == tinfo.mipLevels );
|
|
|
|
assert( info.miscFlags == tinfo.miscFlags );
|
|
|
|
assert( info.miscFlags2 == tinfo.miscFlags2 );
|
|
|
|
assert( info.dimension == tinfo.dimension );
|
2012-05-02 00:00:24 +04:00
|
|
|
|
2014-07-15 04:22:30 +04:00
|
|
|
image.swap( timage );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cimage.reset();
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|
|
|
|
|
2013-03-23 04:52:43 +04:00
|
|
|
// --- Set alpha mode ----------------------------------------------------------
|
|
|
|
if ( HasAlpha( info.format )
|
|
|
|
&& info.format != DXGI_FORMAT_A8_UNORM )
|
|
|
|
{
|
|
|
|
if ( image->IsAlphaAllOpaque() )
|
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_OPAQUE);
|
|
|
|
}
|
|
|
|
else if ( info.IsPMAlpha() )
|
|
|
|
{
|
|
|
|
// Aleady set TEX_ALPHA_MODE_PREMULTIPLIED
|
|
|
|
}
|
2015-04-22 23:31:37 +03:00
|
|
|
else if ( dwOptions & (DWORD64(1) << OPT_SEPALPHA) )
|
2013-03-23 04:52:43 +04:00
|
|
|
{
|
2013-04-12 02:23:54 +04:00
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_CUSTOM);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_STRAIGHT);
|
2013-03-23 04:52:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
info.miscFlags2 &= ~TEX_MISC2_ALPHA_MODE_MASK;
|
|
|
|
}
|
|
|
|
|
2012-05-02 00:00:24 +04:00
|
|
|
// --- Save result -------------------------------------------------------------
|
|
|
|
{
|
2014-07-15 04:22:30 +04:00
|
|
|
auto img = image->GetImage(0,0,0);
|
2012-05-02 00:00:24 +04:00
|
|
|
assert( img );
|
|
|
|
size_t nimg = image->GetImageCount();
|
|
|
|
|
|
|
|
PrintInfo( info );
|
|
|
|
wprintf( L"\n");
|
|
|
|
|
|
|
|
// Figure out dest filename
|
2016-05-11 21:36:27 +03:00
|
|
|
wchar_t *pchSlash, *pchDot;
|
2012-05-02 00:00:24 +04:00
|
|
|
|
|
|
|
wcscpy_s(pConv->szDest, MAX_PATH, szPrefix);
|
|
|
|
|
|
|
|
pchSlash = wcsrchr(pConv->szSrc, L'\\');
|
|
|
|
if(pchSlash != 0)
|
|
|
|
wcscat_s(pConv->szDest, MAX_PATH, pchSlash + 1);
|
|
|
|
else
|
|
|
|
wcscat_s(pConv->szDest, MAX_PATH, pConv->szSrc);
|
|
|
|
|
|
|
|
pchSlash = wcsrchr(pConv->szDest, '\\');
|
|
|
|
pchDot = wcsrchr(pConv->szDest, '.');
|
|
|
|
|
|
|
|
if(pchDot > pchSlash)
|
|
|
|
*pchDot = 0;
|
|
|
|
|
|
|
|
wcscat_s(pConv->szDest, MAX_PATH, szSuffix);
|
|
|
|
|
|
|
|
// Write texture
|
2015-01-24 12:17:54 +03:00
|
|
|
wprintf( L"writing %ls", pConv->szDest);
|
2012-05-02 00:00:24 +04:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
switch( FileType )
|
|
|
|
{
|
|
|
|
case CODEC_DDS:
|
|
|
|
hr = SaveToDDSFile( img, nimg, info,
|
2015-04-22 23:31:37 +03:00
|
|
|
(dwOptions & (DWORD64(1) << OPT_USE_DX10) ) ? (DDS_FLAGS_FORCE_DX10_EXT|DDS_FLAGS_FORCE_DX10_EXT_MISC2) : DDS_FLAGS_NONE,
|
2012-05-02 00:00:24 +04:00
|
|
|
pConv->szDest );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CODEC_TGA:
|
|
|
|
hr = SaveToTGAFile( img[0], pConv->szDest );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-06-22 02:58:18 +03:00
|
|
|
{
|
|
|
|
WICCodecs codec = (FileType == CODEC_HDP || FileType == CODEC_JXR) ? WIC_CODEC_WMP : static_cast<WICCodecs>(FileType);
|
|
|
|
hr = SaveToWICFile( img, nimg, WIC_FLAGS_ALL_FRAMES, GetWICCodec(codec), pConv->szDest, nullptr,
|
|
|
|
[&](IPropertyBag2* props)
|
|
|
|
{
|
|
|
|
switch (FileType)
|
|
|
|
{
|
|
|
|
case WIC_CODEC_JPEG:
|
|
|
|
if (wicLossless || wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
options.pstrName = L"ImageQuality";
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = (wicLossless) ? 1.f : wicQuality;
|
|
|
|
(void)props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIC_CODEC_TIFF:
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
if (wicLossless)
|
|
|
|
{
|
|
|
|
options.pstrName = L"TiffCompressionMethod";
|
|
|
|
varValues.vt = VT_UI1;
|
|
|
|
varValues.bVal = WICTiffCompressionNone;
|
|
|
|
}
|
|
|
|
else if (wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
options.pstrName = L"CompressionQuality";
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = wicQuality;
|
|
|
|
}
|
|
|
|
(void)props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIC_CODEC_WMP:
|
|
|
|
case CODEC_HDP:
|
|
|
|
case CODEC_JXR:
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
if (wicLossless)
|
|
|
|
{
|
|
|
|
options.pstrName = L"Lossless";
|
|
|
|
varValues.vt = VT_BOOL;
|
|
|
|
varValues.bVal = TRUE;
|
|
|
|
}
|
|
|
|
else if (wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
options.pstrName = L"ImageQuality";
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = wicQuality;
|
|
|
|
}
|
|
|
|
(void)props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-05-02 00:00:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf( L" FAILED (%x)\n", hr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wprintf( L"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( nonpow2warn )
|
|
|
|
wprintf( L"\n WARNING: Not all feature levels support non-power-of-2 textures with mipmaps\n" );
|
|
|
|
|
2013-07-31 04:32:45 +04:00
|
|
|
if ( non4bc )
|
|
|
|
wprintf( L"\n WARNING: Direct3D requires BC image to be multiple of 4 in width & height\n" );
|
|
|
|
|
2015-04-22 23:31:37 +03:00
|
|
|
if(dwOptions & (DWORD64(1) << OPT_TIMING))
|
|
|
|
{
|
|
|
|
LARGE_INTEGER qpcEnd;
|
|
|
|
if ( QueryPerformanceCounter( &qpcEnd ) )
|
|
|
|
{
|
|
|
|
LONGLONG delta = qpcEnd.QuadPart - qpcStart.QuadPart;
|
|
|
|
wprintf( L"\n Processing time: %f seconds\n", double(delta) / double(qpcFreq.QuadPart) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-07 01:08:06 +04:00
|
|
|
return 0;
|
2012-05-02 00:00:24 +04:00
|
|
|
}
|