2016-08-22 21:26:36 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: TexConv.cpp
|
|
|
|
//
|
2016-09-11 06:43:33 +03:00
|
|
|
// DirectX Texture Converter
|
2016-08-22 21:26:36 +03:00
|
|
|
//
|
2021-02-27 09:59:42 +03:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-24 09:24:46 +03:00
|
|
|
// Licensed under the MIT License.
|
2017-02-26 10:57:13 +03:00
|
|
|
//
|
|
|
|
// http://go.microsoft.com/fwlink/?LinkId=248926
|
2016-08-22 21:26:36 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2023-12-30 23:45:09 +03:00
|
|
|
#ifdef _MSC_VER
|
2016-09-11 06:43:33 +03:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4005)
|
2023-12-30 23:45:09 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOMINMAX
|
2016-09-11 06:43:33 +03:00
|
|
|
#define NODRAWTEXT
|
|
|
|
#define NOMCX
|
|
|
|
#define NOSERVICE
|
|
|
|
#define NOHELP
|
2023-12-30 23:45:09 +03:00
|
|
|
#ifdef _MSC_VER
|
2016-09-11 06:43:33 +03:00
|
|
|
#pragma warning(pop)
|
2023-12-30 23:45:09 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-01-09 23:44:59 +03:00
|
|
|
#include <ShlObj.h>
|
|
|
|
|
2023-03-28 02:02:41 +03:00
|
|
|
#if __cplusplus < 201703L
|
|
|
|
#error Requires C++17 (and /Zc:__cplusplus with MSVC)
|
|
|
|
#endif
|
|
|
|
|
2020-12-30 05:04:51 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-05-01 10:44:49 +03:00
|
|
|
#include <cstring>
|
2021-01-07 11:43:18 +03:00
|
|
|
#include <cwchar>
|
2021-06-09 01:00:22 +03:00
|
|
|
#include <cwctype>
|
2023-03-28 02:02:41 +03:00
|
|
|
#include <filesystem>
|
2017-09-19 21:13:34 +03:00
|
|
|
#include <fstream>
|
2021-01-03 01:37:52 +03:00
|
|
|
#include <iterator>
|
2016-08-22 21:26:36 +03:00
|
|
|
#include <list>
|
2021-04-29 00:33:45 +03:00
|
|
|
#include <locale>
|
2021-01-17 22:59:46 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <new>
|
2021-06-09 01:00:22 +03:00
|
|
|
#include <set>
|
2021-01-09 23:44:59 +03:00
|
|
|
#include <string>
|
2021-10-17 21:57:07 +03:00
|
|
|
#include <tuple>
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
#include <wrl\client.h>
|
|
|
|
|
|
|
|
#include <d3d11.h>
|
|
|
|
#include <dxgi.h>
|
|
|
|
#include <dxgiformat.h>
|
|
|
|
|
|
|
|
#include <wincodec.h>
|
|
|
|
|
2023-12-30 23:45:09 +03:00
|
|
|
#ifdef _MSC_VER
|
2019-10-25 02:55:13 +03:00
|
|
|
#pragma warning(disable : 4619 4616 26812)
|
2023-12-30 23:45:09 +03:00
|
|
|
#endif
|
2019-10-25 02:55:13 +03:00
|
|
|
|
2019-04-17 03:38:37 +03:00
|
|
|
#include "DirectXTex.h"
|
2016-09-14 04:09:46 +03:00
|
|
|
#include "DirectXPackedVector.h"
|
|
|
|
|
2016-10-02 11:53:31 +03:00
|
|
|
#ifdef USE_OPENEXR
|
|
|
|
// See <https://github.com/Microsoft/DirectXTex/wiki/Adding-OpenEXR> for details
|
|
|
|
#include "DirectXTexEXR.h"
|
|
|
|
#endif
|
|
|
|
|
2024-01-20 22:02:19 +03:00
|
|
|
// See <https://github.com/Microsoft/DirectXTex/wiki/Using-JPEG-PNG-OSS> for details
|
|
|
|
#ifdef USE_LIBJPEG
|
|
|
|
#include "DirectXTexJPEG.h"
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBPNG
|
|
|
|
#include "DirectXTexPNG.h"
|
|
|
|
#endif
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
// See <https://github.com/microsoft/DirectXTex/wiki/DirectXTexXbox> for details
|
|
|
|
#include "DirectXTexXbox.h"
|
|
|
|
#endif
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
using namespace DirectX;
|
2016-09-14 04:09:46 +03:00
|
|
|
using namespace DirectX::PackedVector;
|
2016-08-22 21:26:36 +03:00
|
|
|
using Microsoft::WRL::ComPtr;
|
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
namespace
|
2018-02-08 04:14:11 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
enum OPTIONS : uint64_t
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
OPT_RECURSIVE = 1,
|
|
|
|
OPT_FILELIST,
|
|
|
|
OPT_WIDTH,
|
|
|
|
OPT_HEIGHT,
|
|
|
|
OPT_MIPLEVELS,
|
|
|
|
OPT_FORMAT,
|
|
|
|
OPT_FILTER,
|
|
|
|
OPT_SRGBI,
|
|
|
|
OPT_SRGBO,
|
|
|
|
OPT_SRGB,
|
|
|
|
OPT_PREFIX,
|
|
|
|
OPT_SUFFIX,
|
|
|
|
OPT_OUTPUTDIR,
|
|
|
|
OPT_TOLOWER,
|
|
|
|
OPT_OVERWRITE,
|
|
|
|
OPT_FILETYPE,
|
|
|
|
OPT_HFLIP,
|
|
|
|
OPT_VFLIP,
|
|
|
|
OPT_DDS_DWORD_ALIGN,
|
|
|
|
OPT_DDS_BAD_DXTN_TAILS,
|
2023-09-12 09:13:35 +03:00
|
|
|
OPT_DDS_PERMISSIVE,
|
2020-06-05 06:07:21 +03:00
|
|
|
OPT_USE_DX10,
|
|
|
|
OPT_USE_DX9,
|
|
|
|
OPT_TGA20,
|
2023-06-09 19:52:45 +03:00
|
|
|
OPT_TGAZEROALPHA,
|
2020-06-05 06:07:21 +03:00
|
|
|
OPT_WIC_QUALITY,
|
|
|
|
OPT_WIC_LOSSLESS,
|
|
|
|
OPT_WIC_MULTIFRAME,
|
|
|
|
OPT_NOLOGO,
|
|
|
|
OPT_TIMING,
|
|
|
|
OPT_SEPALPHA,
|
|
|
|
OPT_NO_WIC,
|
|
|
|
OPT_TYPELESS_UNORM,
|
|
|
|
OPT_TYPELESS_FLOAT,
|
|
|
|
OPT_PREMUL_ALPHA,
|
|
|
|
OPT_DEMUL_ALPHA,
|
|
|
|
OPT_EXPAND_LUMINANCE,
|
|
|
|
OPT_TA_WRAP,
|
|
|
|
OPT_TA_MIRROR,
|
|
|
|
OPT_FORCE_SINGLEPROC,
|
|
|
|
OPT_GPU,
|
|
|
|
OPT_NOGPU,
|
|
|
|
OPT_FEATURE_LEVEL,
|
|
|
|
OPT_FIT_POWEROF2,
|
|
|
|
OPT_ALPHA_THRESHOLD,
|
|
|
|
OPT_ALPHA_WEIGHT,
|
|
|
|
OPT_NORMAL_MAP,
|
|
|
|
OPT_NORMAL_MAP_AMPLITUDE,
|
|
|
|
OPT_BC_COMPRESS,
|
|
|
|
OPT_COLORKEY,
|
|
|
|
OPT_TONEMAP,
|
|
|
|
OPT_X2_BIAS,
|
|
|
|
OPT_PRESERVE_ALPHA_COVERAGE,
|
|
|
|
OPT_INVERT_Y,
|
2020-07-14 11:59:19 +03:00
|
|
|
OPT_RECONSTRUCT_Z,
|
2020-06-05 06:07:21 +03:00
|
|
|
OPT_ROTATE_COLOR,
|
|
|
|
OPT_PAPER_WHITE_NITS,
|
|
|
|
OPT_BCNONMULT4FIX,
|
2021-01-10 02:55:57 +03:00
|
|
|
OPT_SWIZZLE,
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
OPT_USE_XBOX,
|
|
|
|
OPT_XGMODE,
|
|
|
|
#endif
|
2020-06-05 06:07:21 +03:00
|
|
|
OPT_MAX
|
|
|
|
};
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
ROTATE_709_TO_HDR10 = 1,
|
|
|
|
ROTATE_HDR10_TO_709,
|
|
|
|
ROTATE_709_TO_2020,
|
|
|
|
ROTATE_2020_TO_709,
|
2021-07-27 21:42:30 +03:00
|
|
|
ROTATE_P3D65_TO_HDR10,
|
|
|
|
ROTATE_P3D65_TO_2020,
|
|
|
|
ROTATE_709_TO_P3D65,
|
|
|
|
ROTATE_P3D65_TO_709,
|
2020-06-05 06:07:21 +03:00
|
|
|
};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
FORMAT_DXT5_NM = 1,
|
|
|
|
FORMAT_DXT5_RXGB,
|
|
|
|
};
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
static_assert(OPT_MAX <= 64, "dwOptions is a unsigned int bitfield");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
struct SConversion
|
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
std::wstring szSrc;
|
|
|
|
std::wstring szFolder;
|
2020-06-05 06:07:21 +03:00
|
|
|
};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
template<typename T>
|
2020-06-05 06:07:21 +03:00
|
|
|
struct SValue
|
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
const wchar_t* name;
|
|
|
|
T value;
|
2020-06-05 06:07:21 +03:00
|
|
|
};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint64_t> g_pOptions[] =
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
{ L"r", OPT_RECURSIVE },
|
|
|
|
{ L"flist", OPT_FILELIST },
|
|
|
|
{ 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"l", OPT_TOLOWER },
|
|
|
|
{ L"y", OPT_OVERWRITE },
|
|
|
|
{ L"ft", OPT_FILETYPE },
|
|
|
|
{ L"hflip", OPT_HFLIP },
|
|
|
|
{ L"vflip", OPT_VFLIP },
|
|
|
|
{ L"dword", OPT_DDS_DWORD_ALIGN },
|
|
|
|
{ L"badtails", OPT_DDS_BAD_DXTN_TAILS },
|
2023-09-12 09:13:35 +03:00
|
|
|
{ L"permissive", OPT_DDS_PERMISSIVE },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"dx10", OPT_USE_DX10 },
|
|
|
|
{ L"dx9", OPT_USE_DX9 },
|
|
|
|
{ L"tga20", OPT_TGA20 },
|
2023-06-09 19:52:45 +03:00
|
|
|
{ L"tgazeroalpha", OPT_TGAZEROALPHA },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"wicq", OPT_WIC_QUALITY },
|
|
|
|
{ L"wiclossless", OPT_WIC_LOSSLESS },
|
|
|
|
{ L"wicmulti", OPT_WIC_MULTIFRAME },
|
|
|
|
{ L"nologo", OPT_NOLOGO },
|
|
|
|
{ L"timing", OPT_TIMING },
|
|
|
|
{ L"sepalpha", OPT_SEPALPHA },
|
|
|
|
{ L"keepcoverage", OPT_PRESERVE_ALPHA_COVERAGE },
|
|
|
|
{ L"nowic", OPT_NO_WIC },
|
|
|
|
{ L"tu", OPT_TYPELESS_UNORM },
|
|
|
|
{ L"tf", OPT_TYPELESS_FLOAT },
|
|
|
|
{ L"pmalpha", OPT_PREMUL_ALPHA },
|
|
|
|
{ L"alpha", OPT_DEMUL_ALPHA },
|
|
|
|
{ L"xlum", OPT_EXPAND_LUMINANCE },
|
|
|
|
{ L"wrap", OPT_TA_WRAP },
|
|
|
|
{ L"mirror", OPT_TA_MIRROR },
|
|
|
|
{ L"singleproc", OPT_FORCE_SINGLEPROC },
|
|
|
|
{ L"gpu", OPT_GPU },
|
|
|
|
{ L"nogpu", OPT_NOGPU },
|
|
|
|
{ L"fl", OPT_FEATURE_LEVEL },
|
|
|
|
{ L"pow2", OPT_FIT_POWEROF2 },
|
|
|
|
{ L"at", OPT_ALPHA_THRESHOLD },
|
|
|
|
{ L"aw", OPT_ALPHA_WEIGHT },
|
|
|
|
{ L"nmap", OPT_NORMAL_MAP },
|
|
|
|
{ L"nmapamp", OPT_NORMAL_MAP_AMPLITUDE },
|
|
|
|
{ L"bc", OPT_BC_COMPRESS },
|
|
|
|
{ L"c", OPT_COLORKEY },
|
|
|
|
{ L"tonemap", OPT_TONEMAP },
|
|
|
|
{ L"x2bias", OPT_X2_BIAS },
|
|
|
|
{ L"inverty", OPT_INVERT_Y },
|
2020-07-14 11:59:19 +03:00
|
|
|
{ L"reconstructz", OPT_RECONSTRUCT_Z },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"rotatecolor", OPT_ROTATE_COLOR },
|
|
|
|
{ L"nits", OPT_PAPER_WHITE_NITS },
|
2020-07-14 11:59:19 +03:00
|
|
|
{ L"fixbc4x4", OPT_BCNONMULT4FIX },
|
2021-01-10 02:55:57 +03:00
|
|
|
{ L"swizzle", OPT_SWIZZLE },
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
{ L"xbox", OPT_USE_XBOX },
|
|
|
|
{ L"xgmode", OPT_XGMODE },
|
|
|
|
#endif
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, 0 }
|
|
|
|
};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2020-05-21 04:18:24 +03:00
|
|
|
#define DEFFMT(fmt) { L## #fmt, DXGI_FORMAT_ ## fmt }
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pFormats[] =
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
DEFFMT(AYUV),
|
|
|
|
DEFFMT(Y410),
|
|
|
|
DEFFMT(Y416),
|
|
|
|
DEFFMT(YUY2),
|
|
|
|
DEFFMT(Y210),
|
|
|
|
DEFFMT(Y216),
|
|
|
|
// No support for legacy paletted video formats (AI44, IA44, P8, A8P8)
|
|
|
|
DEFFMT(B4G4R4A4_UNORM),
|
|
|
|
|
2023-05-11 19:58:28 +03:00
|
|
|
// D3D11on12 format
|
|
|
|
{ L"A4B4G4R4_UNORM", DXGI_FORMAT(191) },
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
// Xbox One extended formats
|
|
|
|
{ L"R10G10B10_7E3_A2_FLOAT", DXGI_FORMAT(116) },
|
|
|
|
{ L"R10G10B10_6E4_A2_FLOAT", DXGI_FORMAT(117) },
|
|
|
|
{ L"R10G10B10_SNORM_A2_UNORM", DXGI_FORMAT(189) },
|
|
|
|
{ L"R4G4_UNORM", DXGI_FORMAT(190) },
|
|
|
|
#endif
|
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, DXGI_FORMAT_UNKNOWN }
|
|
|
|
};
|
2018-02-22 11:17:49 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pFormatAliases[] =
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
{ L"DXT1", DXGI_FORMAT_BC1_UNORM },
|
|
|
|
{ L"DXT2", DXGI_FORMAT_BC2_UNORM },
|
|
|
|
{ L"DXT3", DXGI_FORMAT_BC2_UNORM },
|
|
|
|
{ L"DXT4", DXGI_FORMAT_BC3_UNORM },
|
|
|
|
{ L"DXT5", DXGI_FORMAT_BC3_UNORM },
|
2018-02-22 22:57:44 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"RGBA", DXGI_FORMAT_R8G8B8A8_UNORM },
|
|
|
|
{ L"BGRA", DXGI_FORMAT_B8G8R8A8_UNORM },
|
2021-06-03 04:12:59 +03:00
|
|
|
{ L"BGR", DXGI_FORMAT_B8G8R8X8_UNORM },
|
2018-02-22 11:17:49 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"FP16", DXGI_FORMAT_R16G16B16A16_FLOAT },
|
|
|
|
{ L"FP32", DXGI_FORMAT_R32G32B32A32_FLOAT },
|
2019-08-23 03:31:58 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ L"BPTC", DXGI_FORMAT_BC7_UNORM },
|
|
|
|
{ L"BPTC_FLOAT", DXGI_FORMAT_BC6H_UF16 },
|
2018-02-22 11:17:49 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, DXGI_FORMAT_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
const SValue<uint32_t> g_pSpecialFormats[] =
|
|
|
|
{
|
|
|
|
{ L"BC3n", FORMAT_DXT5_NM },
|
|
|
|
{ L"DXT5nm", FORMAT_DXT5_NM },
|
|
|
|
{ L"RXGB", FORMAT_DXT5_RXGB },
|
|
|
|
|
|
|
|
{ nullptr, 0 }
|
|
|
|
};
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pReadOnlyFormats[] =
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
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),
|
|
|
|
|
|
|
|
// DXGI 1.3 formats
|
|
|
|
{ L"P208", DXGI_FORMAT(130) },
|
|
|
|
{ L"V208", DXGI_FORMAT(131) },
|
|
|
|
{ L"V408", DXGI_FORMAT(132) },
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
// Xbox One extended formats
|
|
|
|
{ L"D16_UNORM_S8_UINT", DXGI_FORMAT(118) },
|
|
|
|
{ L"R16_UNORM_X8_TYPELESS", DXGI_FORMAT(119) },
|
|
|
|
{ L"X16_TYPELESS_G8_UINT", DXGI_FORMAT(120) },
|
|
|
|
#endif
|
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, DXGI_FORMAT_UNKNOWN }
|
|
|
|
};
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pFilters[] =
|
2020-06-05 06:07:21 +03: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 },
|
|
|
|
{ L"TRIANGLE", TEX_FILTER_TRIANGLE },
|
|
|
|
{ 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 },
|
|
|
|
{ L"TRIANGLE_DITHER", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER },
|
|
|
|
{ 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 },
|
|
|
|
{ L"TRIANGLE_DITHER_DIFFUSION", TEX_FILTER_TRIANGLE | TEX_FILTER_DITHER_DIFFUSION },
|
|
|
|
{ nullptr, TEX_FILTER_DEFAULT }
|
|
|
|
};
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pRotateColor[] =
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
2021-07-27 21:42:30 +03:00
|
|
|
{ L"709to2020", ROTATE_709_TO_2020 },
|
|
|
|
{ L"2020to709", ROTATE_2020_TO_709 },
|
|
|
|
{ L"709toHDR10", ROTATE_709_TO_HDR10 },
|
|
|
|
{ L"HDR10to709", ROTATE_HDR10_TO_709 },
|
|
|
|
{ L"P3D65to2020", ROTATE_P3D65_TO_2020 },
|
|
|
|
{ L"P3D65toHDR10", ROTATE_P3D65_TO_HDR10 },
|
|
|
|
{ L"709toP3D65", ROTATE_709_TO_P3D65 },
|
|
|
|
{ L"P3D65to709", ROTATE_P3D65_TO_709 },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
#define CODEC_DDS 0xFFFF0001
|
2016-08-22 21:26:36 +03:00
|
|
|
#define CODEC_TGA 0xFFFF0002
|
|
|
|
#define CODEC_HDP 0xFFFF0003
|
|
|
|
#define CODEC_JXR 0xFFFF0004
|
2016-09-11 06:43:33 +03:00
|
|
|
#define CODEC_HDR 0xFFFF0005
|
2020-06-05 06:07:21 +03:00
|
|
|
#define CODEC_PPM 0xFFFF0006
|
|
|
|
#define CODEC_PFM 0xFFFF0007
|
2019-07-06 09:48:39 +03:00
|
|
|
|
|
|
|
#ifdef USE_OPENEXR
|
2020-06-05 06:07:21 +03:00
|
|
|
#define CODEC_EXR 0xFFFF0008
|
2024-01-20 22:02:19 +03:00
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBJPEG
|
|
|
|
#define CODEC_JPEG 0xFFFF0009
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBPNG
|
|
|
|
#define CODEC_PNG 0xFFFF000A
|
2019-07-06 09:48:39 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pSaveFileTypes[] = // valid formats to write to
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"bmp", WIC_CODEC_BMP },
|
2024-01-20 22:02:19 +03:00
|
|
|
#ifdef USE_LIBJPEG
|
|
|
|
{ L"jpg", CODEC_JPEG },
|
|
|
|
{ L"jpeg", CODEC_JPEG },
|
|
|
|
#else
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"jpg", WIC_CODEC_JPEG },
|
|
|
|
{ L"jpeg", WIC_CODEC_JPEG },
|
2024-01-20 22:02:19 +03:00
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBPNG
|
|
|
|
{ L"png", CODEC_PNG },
|
|
|
|
#else
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"png", WIC_CODEC_PNG },
|
2024-01-20 22:02:19 +03:00
|
|
|
#endif
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"dds", CODEC_DDS },
|
2023-06-10 20:27:36 +03:00
|
|
|
{ L"ddx", CODEC_DDS },
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"tga", CODEC_TGA },
|
|
|
|
{ L"hdr", CODEC_HDR },
|
|
|
|
{ L"tif", WIC_CODEC_TIFF },
|
|
|
|
{ L"tiff", WIC_CODEC_TIFF },
|
|
|
|
{ L"wdp", WIC_CODEC_WMP },
|
|
|
|
{ L"hdp", CODEC_HDP },
|
|
|
|
{ L"jxr", CODEC_JXR },
|
|
|
|
{ L"ppm", CODEC_PPM },
|
|
|
|
{ L"pfm", CODEC_PFM },
|
2020-06-05 06:07:21 +03:00
|
|
|
#ifdef USE_OPENEXR
|
2021-08-01 03:03:54 +03:00
|
|
|
{ L"exr", CODEC_EXR },
|
2020-06-05 06:07:21 +03:00
|
|
|
#endif
|
2022-03-03 01:25:27 +03:00
|
|
|
{ L"heic", WIC_CODEC_HEIF },
|
|
|
|
{ L"heif", WIC_CODEC_HEIF },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, CODEC_DDS }
|
|
|
|
};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
const SValue<uint32_t> g_pFeatureLevels[] = // valid feature levels for -fl for maximimum size
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
|
|
|
{ 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 },
|
|
|
|
{ L"12.0", 16384 },
|
|
|
|
{ L"12.1", 16384 },
|
2021-10-30 01:46:20 +03:00
|
|
|
{ L"12.2", 16384 },
|
2020-06-05 06:07:21 +03:00
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
HRESULT __cdecl LoadFromBMPEx(
|
|
|
|
_In_z_ const wchar_t* szFile,
|
|
|
|
_In_ WIC_FLAGS flags,
|
|
|
|
_Out_opt_ TexMetadata* metadata,
|
|
|
|
_Out_ ScratchImage& image) noexcept;
|
|
|
|
|
|
|
|
HRESULT __cdecl LoadFromPortablePixMap(
|
|
|
|
_In_z_ const wchar_t* szFile,
|
|
|
|
_Out_opt_ TexMetadata* metadata,
|
|
|
|
_Out_ ScratchImage& image) noexcept;
|
|
|
|
|
|
|
|
HRESULT __cdecl SaveToPortablePixMap(
|
|
|
|
_In_ const Image& image,
|
|
|
|
_In_z_ const wchar_t* szFile) noexcept;
|
|
|
|
|
|
|
|
HRESULT __cdecl LoadFromPortablePixMapHDR(
|
|
|
|
_In_z_ const wchar_t* szFile,
|
|
|
|
_Out_opt_ TexMetadata* metadata,
|
|
|
|
_Out_ ScratchImage& image) noexcept;
|
|
|
|
|
|
|
|
HRESULT __cdecl SaveToPortablePixMapHDR(
|
|
|
|
_In_ const Image& image,
|
|
|
|
_In_z_ const wchar_t* szFile) noexcept;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2023-12-30 23:45:09 +03:00
|
|
|
#ifdef _MSC_VER
|
2016-08-22 21:26:36 +03:00
|
|
|
#pragma warning( disable : 4616 6211 )
|
2023-12-30 23:45:09 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
namespace
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2020-06-05 06:07:21 +03:00
|
|
|
inline HANDLE safe_handle(HANDLE h) noexcept { return (h == INVALID_HANDLE_VALUE) ? nullptr : h; }
|
2018-02-22 12:49:04 +03:00
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
struct find_closer { void operator()(HANDLE h) noexcept { assert(h != INVALID_HANDLE_VALUE); if (h) FindClose(h); } };
|
2016-09-13 07:54:08 +03:00
|
|
|
|
2020-01-19 12:36:52 +03:00
|
|
|
using ScopedFindHandle = std::unique_ptr<void, find_closer>;
|
2016-09-13 07:54:08 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
constexpr static bool ispow2(size_t x)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
|
|
|
return ((x != 0) && !(x & (x - 1)));
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2019-07-06 09:48:39 +03:00
|
|
|
#ifdef _PREFAST_
|
2016-08-22 21:26:36 +03:00
|
|
|
#pragma prefast(disable : 26018, "Only used with static internal arrays")
|
2019-07-06 09:48:39 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
template<typename T>
|
|
|
|
T LookupByName(const wchar_t *pName, const SValue<T> *pArray)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
while (pArray->name)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (!_wcsicmp(pName, pArray->name))
|
|
|
|
return pArray->value;
|
2016-09-13 02:39:26 +03:00
|
|
|
|
|
|
|
pArray++;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
return 0;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
template<typename T>
|
|
|
|
const wchar_t* LookupByValue(T value, const SValue<T> *pArray)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
while (pArray->name)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (value == pArray->value)
|
|
|
|
return pArray->name;
|
2016-09-13 02:39:26 +03:00
|
|
|
|
|
|
|
pArray++;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
return L"";
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
void SearchForFiles(const std::filesystem::path& path, std::list<SConversion>& files, bool recursive, const wchar_t* folder)
|
2016-09-13 07:54:08 +03:00
|
|
|
{
|
|
|
|
// Process files
|
2019-02-08 01:52:57 +03:00
|
|
|
WIN32_FIND_DATAW findData = {};
|
2023-06-14 01:40:45 +03:00
|
|
|
ScopedFindHandle hFile(safe_handle(FindFirstFileExW(path.c_str(),
|
2016-09-13 07:54:08 +03:00
|
|
|
FindExInfoBasic, &findData,
|
|
|
|
FindExSearchNameMatch, nullptr,
|
|
|
|
FIND_FIRST_EX_LARGE_FETCH)));
|
|
|
|
if (hFile)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!(findData.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_DIRECTORY)))
|
|
|
|
{
|
2021-01-09 23:44:59 +03:00
|
|
|
SConversion conv = {};
|
2023-06-14 01:40:45 +03:00
|
|
|
conv.szSrc = path.parent_path().append(findData.cFileName).native();
|
2021-01-09 23:44:59 +03:00
|
|
|
if (folder)
|
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
conv.szFolder = folder;
|
2021-01-09 23:44:59 +03:00
|
|
|
}
|
2016-09-13 07:54:08 +03:00
|
|
|
files.push_back(conv);
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:52:57 +03:00
|
|
|
if (!FindNextFileW(hFile.get(), &findData))
|
2016-09-13 07:54:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process directories
|
|
|
|
if (recursive)
|
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
auto searchDir = path.parent_path().append(L"*");
|
2016-09-13 07:54:08 +03:00
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
hFile.reset(safe_handle(FindFirstFileExW(searchDir.c_str(),
|
2016-09-13 07:54:08 +03:00
|
|
|
FindExInfoBasic, &findData,
|
|
|
|
FindExSearchLimitToDirectories, nullptr,
|
|
|
|
FIND_FIRST_EX_LARGE_FETCH)));
|
|
|
|
if (!hFile)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
{
|
|
|
|
if (findData.cFileName[0] != L'.')
|
|
|
|
{
|
2021-01-09 23:44:59 +03:00
|
|
|
auto subfolder = (folder)
|
2023-03-28 02:02:41 +03:00
|
|
|
? (std::wstring(folder) + std::wstring(findData.cFileName) + std::filesystem::path::preferred_separator)
|
|
|
|
: (std::wstring(findData.cFileName) + std::filesystem::path::preferred_separator);
|
2023-06-14 01:40:45 +03:00
|
|
|
|
|
|
|
auto subdir = path.parent_path().append(findData.cFileName).append(path.filename().c_str());
|
2016-09-13 07:54:08 +03:00
|
|
|
|
2021-01-09 23:44:59 +03:00
|
|
|
SearchForFiles(subdir, files, recursive, subfolder.c_str());
|
2016-09-13 07:54:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:52:57 +03:00
|
|
|
if (!FindNextFileW(hFile.get(), &findData))
|
2016-09-13 07:54:08 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:00:22 +03:00
|
|
|
void ProcessFileList(std::wifstream& inFile, std::list<SConversion>& files)
|
|
|
|
{
|
|
|
|
std::list<SConversion> flist;
|
|
|
|
std::set<std::wstring> excludes;
|
2023-06-14 01:40:45 +03:00
|
|
|
|
2021-06-09 01:00:22 +03:00
|
|
|
for (;;)
|
|
|
|
{
|
2023-10-07 00:18:53 +03:00
|
|
|
std::wstring fname;
|
|
|
|
std::getline(inFile, fname);
|
2021-06-09 01:00:22 +03:00
|
|
|
if (!inFile)
|
|
|
|
break;
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
if (fname[0] == L'#')
|
2021-06-09 01:00:22 +03:00
|
|
|
{
|
|
|
|
// Comment
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (fname[0] == L'-')
|
2021-06-09 01:00:22 +03:00
|
|
|
{
|
|
|
|
if (flist.empty())
|
|
|
|
{
|
2023-10-07 00:18:53 +03:00
|
|
|
wprintf(L"WARNING: Ignoring the line '%ls' in -flist\n", fname.c_str());
|
2021-06-09 01:00:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-10-07 00:18:53 +03:00
|
|
|
std::filesystem::path path(fname.c_str() + 1);
|
2023-03-28 02:02:41 +03:00
|
|
|
auto& npath = path.make_preferred();
|
2023-10-07 00:18:53 +03:00
|
|
|
if (wcspbrk(fname.c_str(), L"?*") != nullptr)
|
2021-06-09 01:00:22 +03:00
|
|
|
{
|
|
|
|
std::list<SConversion> removeFiles;
|
2023-06-14 01:40:45 +03:00
|
|
|
SearchForFiles(npath, removeFiles, false, nullptr);
|
2021-06-09 01:00:22 +03:00
|
|
|
|
2023-03-28 02:02:41 +03:00
|
|
|
for (auto& it : removeFiles)
|
2021-06-09 01:00:22 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
std::wstring name = it.szSrc;
|
|
|
|
std::transform(name.begin(), name.end(), name.begin(), towlower);
|
|
|
|
excludes.insert(name);
|
2021-06-09 01:00:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-28 02:02:41 +03:00
|
|
|
std::wstring name = npath.c_str();
|
2021-06-09 01:00:22 +03:00
|
|
|
std::transform(name.begin(), name.end(), name.begin(), towlower);
|
|
|
|
excludes.insert(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-10-07 00:18:53 +03:00
|
|
|
else if (wcspbrk(fname.c_str(), L"?*") != nullptr)
|
2021-06-09 01:00:22 +03:00
|
|
|
{
|
2023-10-07 00:18:53 +03:00
|
|
|
std::filesystem::path path(fname.c_str());
|
2023-06-14 01:40:45 +03:00
|
|
|
SearchForFiles(path.make_preferred(), flist, false, nullptr);
|
2021-06-09 01:00:22 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SConversion conv = {};
|
2023-10-07 00:18:53 +03:00
|
|
|
std::filesystem::path path(fname.c_str());
|
2023-06-14 01:40:45 +03:00
|
|
|
conv.szSrc = path.make_preferred().native();
|
2021-06-09 01:00:22 +03:00
|
|
|
flist.push_back(conv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inFile.close();
|
|
|
|
|
|
|
|
if (!excludes.empty())
|
|
|
|
{
|
|
|
|
// Remove any excluded files
|
|
|
|
for (auto it = flist.begin(); it != flist.end();)
|
|
|
|
{
|
|
|
|
std::wstring name = it->szSrc;
|
|
|
|
std::transform(name.begin(), name.end(), name.begin(), towlower);
|
|
|
|
auto item = it;
|
|
|
|
++it;
|
|
|
|
if (excludes.find(name) != excludes.end())
|
|
|
|
{
|
|
|
|
flist.erase(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flist.empty())
|
|
|
|
{
|
|
|
|
wprintf(L"WARNING: No file names found in -flist\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
files.splice(files.end(), flist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
void PrintFormat(DXGI_FORMAT Format)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
for (auto pFormat = g_pFormats; pFormat->name; pFormat++)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (static_cast<DXGI_FORMAT>(pFormat->value) == Format)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
wprintf(L"%ls", pFormat->name);
|
2016-09-13 02:39:26 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
for (auto pFormat = g_pReadOnlyFormats; pFormat->name; pFormat++)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (static_cast<DXGI_FORMAT>(pFormat->value) == Format)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
wprintf(L"%ls", pFormat->name);
|
2016-09-13 02:39:26 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"*UNKNOWN*");
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
void PrintInfo(const TexMetadata& info, bool isXbox)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2018-05-02 03:34:54 +03:00
|
|
|
wprintf(L" (%zux%zu", info.width, info.height);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (TEX_DIMENSION_TEXTURE3D == info.dimension)
|
2018-05-02 03:34:54 +03:00
|
|
|
wprintf(L"x%zu", info.depth);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.mipLevels > 1)
|
2018-05-02 03:34:54 +03:00
|
|
|
wprintf(L",%zu", info.mipLevels);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.arraySize > 1)
|
2018-05-02 03:34:54 +03:00
|
|
|
wprintf(L",%zu", info.arraySize);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L" ");
|
|
|
|
PrintFormat(info.format);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
switch (info.dimension)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
case TEX_DIMENSION_TEXTURE1D:
|
2019-11-15 11:13:05 +03:00
|
|
|
wprintf(L"%ls", (info.arraySize > 1) ? L" 1DArray" : L" 1D");
|
2016-09-13 02:39:26 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE2D:
|
|
|
|
if (info.IsCubemap())
|
|
|
|
{
|
2019-11-15 11:13:05 +03:00
|
|
|
wprintf(L"%ls", (info.arraySize > 6) ? L" CubeArray" : L" Cube");
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-15 11:13:05 +03:00
|
|
|
wprintf(L"%ls", (info.arraySize > 1) ? L" 2DArray" : L" 2D");
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEX_DIMENSION_TEXTURE3D:
|
|
|
|
wprintf(L" 3D");
|
|
|
|
break;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-14 10:03:22 +03:00
|
|
|
switch (info.GetAlphaMode())
|
|
|
|
{
|
|
|
|
case TEX_ALPHA_MODE_OPAQUE:
|
2022-11-21 02:22:30 +03:00
|
|
|
wprintf(L" \x03B1:Opaque");
|
2016-09-14 10:03:22 +03:00
|
|
|
break;
|
|
|
|
case TEX_ALPHA_MODE_PREMULTIPLIED:
|
2022-11-21 02:22:30 +03:00
|
|
|
wprintf(L" \x03B1:PM");
|
2016-09-14 10:03:22 +03:00
|
|
|
break;
|
|
|
|
case TEX_ALPHA_MODE_STRAIGHT:
|
2022-11-21 02:22:30 +03:00
|
|
|
wprintf(L" \x03B1:NonPM");
|
2016-09-14 10:03:22 +03:00
|
|
|
break;
|
2019-07-06 09:48:39 +03:00
|
|
|
case TEX_ALPHA_MODE_CUSTOM:
|
2022-11-21 02:22:30 +03:00
|
|
|
wprintf(L" \x03B1:Custom");
|
2019-07-06 09:48:39 +03:00
|
|
|
break;
|
|
|
|
case TEX_ALPHA_MODE_UNKNOWN:
|
|
|
|
break;
|
2016-09-14 10:03:22 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
if (isXbox)
|
|
|
|
{
|
|
|
|
wprintf(L" Xbox");
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L")");
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
void PrintList(size_t cch, const SValue<uint32_t> *pValue)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
while (pValue->name)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t cchName = wcslen(pValue->name);
|
2016-09-13 02:39:26 +03:00
|
|
|
|
|
|
|
if (cch + cchName + 2 >= 80)
|
|
|
|
{
|
|
|
|
wprintf(L"\n ");
|
|
|
|
cch = 6;
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
wprintf(L"%ls ", pValue->name);
|
2016-09-13 02:39:26 +03:00
|
|
|
cch += cchName + 2;
|
|
|
|
pValue++;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2023-03-28 02:02:41 +03:00
|
|
|
void PrintLogo(bool versionOnly)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2020-08-16 01:57:39 +03:00
|
|
|
wchar_t version[32] = {};
|
|
|
|
|
|
|
|
wchar_t appName[_MAX_PATH] = {};
|
2023-06-14 01:40:45 +03:00
|
|
|
if (GetModuleFileNameW(nullptr, appName, _MAX_PATH))
|
2020-08-16 01:57:39 +03:00
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const DWORD size = GetFileVersionInfoSizeW(appName, nullptr);
|
2020-08-16 01:57:39 +03:00
|
|
|
if (size > 0)
|
|
|
|
{
|
|
|
|
auto verInfo = std::make_unique<uint8_t[]>(size);
|
|
|
|
if (GetFileVersionInfoW(appName, 0, size, verInfo.get()))
|
|
|
|
{
|
|
|
|
LPVOID lpstr = nullptr;
|
|
|
|
UINT strLen = 0;
|
|
|
|
if (VerQueryValueW(verInfo.get(), L"\\StringFileInfo\\040904B0\\ProductVersion", &lpstr, &strLen))
|
|
|
|
{
|
|
|
|
wcsncpy_s(version, reinterpret_cast<const wchar_t*>(lpstr), strLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!*version || wcscmp(version, L"1.0.0.0") == 0)
|
|
|
|
{
|
|
|
|
swprintf_s(version, L"%03d (library)", DIRECTX_TEX_VERSION);
|
|
|
|
}
|
|
|
|
|
2023-03-28 02:02:41 +03:00
|
|
|
if (versionOnly)
|
|
|
|
{
|
|
|
|
wprintf(L"texconv version %ls\n", version);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-22 18:58:00 +03:00
|
|
|
#if defined(USE_XBOX_EXTS) && defined(_USE_SCARLETT)
|
|
|
|
wprintf(L"Microsoft (R) DirectX Texture Converter for Microsoft GDKX for Xbox Series X|S [Version %ls]\n", version);
|
|
|
|
#elif defined(USE_XBOX_EXTS)
|
|
|
|
wprintf(L"Microsoft (R) DirectX Texture Converter for Microsoft GDKX for Xbox One [Version %ls]\n", version);
|
|
|
|
#else
|
2023-03-28 02:02:41 +03:00
|
|
|
wprintf(L"Microsoft (R) DirectX Texture Converter [DirectXTex] Version %ls\n", version);
|
2024-01-22 18:58:00 +03:00
|
|
|
#endif
|
2023-03-28 02:02:41 +03:00
|
|
|
wprintf(L"Copyright (C) Microsoft Corp.\n");
|
|
|
|
#ifdef _DEBUG
|
|
|
|
wprintf(L"*** Debug build ***\n");
|
|
|
|
#endif
|
|
|
|
wprintf(L"\n");
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-01-07 03:45:01 +03:00
|
|
|
_Success_(return)
|
2016-09-13 02:39:26 +03:00
|
|
|
bool GetDXGIFactory(_Outptr_ IDXGIFactory1** pFactory)
|
|
|
|
{
|
|
|
|
if (!pFactory)
|
|
|
|
return false;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
*pFactory = nullptr;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
typedef HRESULT(WINAPI* pfn_CreateDXGIFactory1)(REFIID riid, _Out_ void **ppFactory);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
static pfn_CreateDXGIFactory1 s_CreateDXGIFactory1 = nullptr;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
if (!s_CreateDXGIFactory1)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2019-02-08 01:52:57 +03:00
|
|
|
HMODULE hModDXGI = LoadLibraryW(L"dxgi.dll");
|
2016-09-13 02:39:26 +03:00
|
|
|
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)));
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
void PrintUsage()
|
|
|
|
{
|
2023-03-28 02:02:41 +03:00
|
|
|
PrintLogo(false);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-10-25 23:18:09 +03:00
|
|
|
static const wchar_t* const s_usage =
|
2023-03-28 02:02:41 +03:00
|
|
|
L"Usage: texconv <options> [--] <files>\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L"\n"
|
|
|
|
L" -r wildcard filename search is recursive\n"
|
|
|
|
L" -r:flatten flatten the directory structure (default)\n"
|
|
|
|
L" -r:keep keep the directory structure\n"
|
|
|
|
L" -flist <filename> use text file with a list of input files (one per line)\n"
|
|
|
|
L"\n"
|
|
|
|
L" -w <n> width\n"
|
|
|
|
L" -h <n> height\n"
|
|
|
|
L" -m <n> miplevels\n"
|
|
|
|
L" -f <format> format\n"
|
|
|
|
L"\n"
|
|
|
|
L" -if <filter> image filtering\n"
|
|
|
|
L" -srgb{i|o} sRGB {input, output}\n"
|
|
|
|
L"\n"
|
|
|
|
L" -px <string> name prefix\n"
|
|
|
|
L" -sx <string> name suffix\n"
|
|
|
|
L" -o <directory> output directory\n"
|
|
|
|
L" -l force output filename to lower case\n"
|
|
|
|
L" -y overwrite existing output file (if any)\n"
|
|
|
|
L" -ft <filetype> output file type\n"
|
|
|
|
L"\n"
|
|
|
|
L" -hflip horizonal flip of source image\n"
|
|
|
|
L" -vflip vertical flip of source image\n"
|
|
|
|
L"\n"
|
|
|
|
L" -sepalpha resize/generate mips alpha channel separately\n"
|
|
|
|
L" from color channels\n"
|
|
|
|
L" -keepcoverage <ref> Preserve alpha coverage in mips for alpha test ref\n"
|
|
|
|
L"\n"
|
|
|
|
L" -nowic Force non-WIC filtering\n"
|
|
|
|
L" -wrap, -mirror texture addressing mode (wrap, mirror, or clamp)\n"
|
|
|
|
L" -pmalpha convert final texture to use premultiplied alpha\n"
|
|
|
|
L" -alpha convert premultiplied alpha to straight alpha\n"
|
2020-01-30 22:40:42 +03:00
|
|
|
L" -at <threshold> Alpha threshold used for BC1, RGBA5551, and WIC\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" (defaults to 0.5)\n"
|
|
|
|
L"\n"
|
|
|
|
L" -fl <feature-level> Set maximum feature level target (defaults to 11.0)\n"
|
|
|
|
L" -pow2 resize to fit a power-of-2, respecting aspect ratio\n"
|
|
|
|
L"\n"
|
|
|
|
L" -nmap <options> converts height-map to normal-map\n"
|
2016-09-13 02:39:26 +03:00
|
|
|
L" options must be one or more of\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" r, g, b, a, l, m, u, v, i, o\n"
|
|
|
|
L" -nmapamp <weight> normal map amplitude (defaults to 1.0)\n"
|
|
|
|
L"\n"
|
|
|
|
L" (DDS input only)\n"
|
|
|
|
L" -t{u|f} TYPELESS format is treated as UNORM or FLOAT\n"
|
|
|
|
L" -dword Use DWORD instead of BYTE alignment\n"
|
|
|
|
L" -badtails Fix for older DXTn with bad mipchain tails\n"
|
2023-10-31 08:04:49 +03:00
|
|
|
L" -permissive Allow some DX9 variants with unusual header values\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" -fixbc4x4 Fix for odd-sized BC files that Direct3D can't load\n"
|
|
|
|
L" -xlum expand legacy L8, L16, and A8P8 formats\n"
|
|
|
|
L"\n"
|
|
|
|
L" (DDS output only)\n"
|
|
|
|
L" -dx10 Force use of 'DX10' extended header\n"
|
|
|
|
L" -dx9 Force use of legacy DX9 header\n"
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
L" -xbox Tile/swizzle and use 'XBOX' variant of DDS\n"
|
|
|
|
L" -xgmode <mode> Tile/swizzle using provided memory layout mode\n"
|
|
|
|
#endif
|
2022-10-25 23:18:09 +03:00
|
|
|
L"\n"
|
2023-06-09 19:52:45 +03:00
|
|
|
L" (TGA input only)\n"
|
|
|
|
L" -tgazeroalpha Allow all zero alpha channel files to be loaded 'as is'\n"
|
|
|
|
L"\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" (TGA output only)\n"
|
|
|
|
L" -tga20 Write file including TGA 2.0 extension area\n"
|
|
|
|
L"\n"
|
|
|
|
L" (BMP, PNG, JPG, TIF, WDP output only)\n"
|
|
|
|
L" -wicq <quality> When writing images with WIC use quality (0.0 to 1.0)\n"
|
|
|
|
L" -wiclossless When writing images with WIC use lossless mode\n"
|
|
|
|
L" -wicmulti When writing images with WIC encode multiframe images\n"
|
|
|
|
L"\n"
|
|
|
|
L" -nologo suppress copyright message\n"
|
|
|
|
L" -timing Display elapsed processing time\n"
|
|
|
|
L"\n"
|
2023-03-28 02:02:41 +03:00
|
|
|
#ifdef _OPENMP
|
2022-10-25 23:18:09 +03:00
|
|
|
L" -singleproc Do not use multi-threaded compression\n"
|
2023-03-28 02:02:41 +03:00
|
|
|
#endif
|
2022-10-25 23:18:09 +03:00
|
|
|
L" -gpu <adapter> Select GPU for DirectCompute-based codecs (0 is default)\n"
|
|
|
|
L" -nogpu Do not use DirectCompute-based codecs\n"
|
|
|
|
L"\n"
|
|
|
|
L" -bc <options> Sets options for BC compression\n"
|
2020-05-10 11:26:32 +03:00
|
|
|
L" options must be one or more of\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" d, u, q, x\n"
|
2016-09-13 02:39:26 +03:00
|
|
|
L" -aw <weight> BC7 GPU compressor weighting for alpha error metric\n"
|
2022-10-25 23:18:09 +03:00
|
|
|
L" (defaults to 1.0)\n"
|
|
|
|
L"\n"
|
|
|
|
L" -c <hex-RGB> colorkey (a.k.a. chromakey) transparency\n"
|
|
|
|
L" -rotatecolor <rot> rotates color primaries and/or applies a curve\n"
|
|
|
|
L" -nits <value> paper-white value in nits to use for HDR10 (def: 200.0)\n"
|
|
|
|
L" -tonemap Apply a tonemap operator based on maximum luminance\n"
|
|
|
|
L" -x2bias Enable *2 - 1 conversion cases for unorm/pos-only-float\n"
|
|
|
|
L" -inverty Invert Y (i.e. green) channel values\n"
|
|
|
|
L" -reconstructz Rebuild Z (blue) channel assuming X/Y are normals\n"
|
2023-03-28 02:02:41 +03:00
|
|
|
L" -swizzle <rgba> Swizzle image channels using HLSL-style mask\n"
|
|
|
|
L"\n"
|
|
|
|
L" '-- ' is needed if any input filepath starts with the '-' or '/' character\n";
|
2022-10-25 23:18:09 +03:00
|
|
|
|
|
|
|
wprintf(L"%ls", s_usage);
|
2016-09-13 02:39:26 +03:00
|
|
|
|
2016-09-24 09:48:33 +03:00
|
|
|
wprintf(L"\n <format>: ");
|
2016-09-13 02:39:26 +03:00
|
|
|
PrintList(13, g_pFormats);
|
2018-02-22 11:17:49 +03:00
|
|
|
wprintf(L" ");
|
|
|
|
PrintList(13, g_pFormatAliases);
|
2023-06-13 00:34:32 +03:00
|
|
|
wprintf(L" ");
|
|
|
|
PrintList(13, g_pSpecialFormats);
|
2016-09-13 02:39:26 +03:00
|
|
|
|
2016-09-24 09:48:33 +03:00
|
|
|
wprintf(L"\n <filter>: ");
|
2016-09-13 02:39:26 +03:00
|
|
|
PrintList(13, g_pFilters);
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
wprintf(L"\n <rot>: ");
|
|
|
|
PrintList(13, g_pRotateColor);
|
|
|
|
|
2016-09-24 09:48:33 +03:00
|
|
|
wprintf(L"\n <filetype>: ");
|
2016-09-13 02:39:26 +03:00
|
|
|
PrintList(15, g_pSaveFileTypes);
|
|
|
|
|
2016-09-24 09:48:33 +03:00
|
|
|
wprintf(L"\n <feature-level>: ");
|
2016-09-13 02:39:26 +03:00
|
|
|
PrintList(13, g_pFeatureLevels);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
ComPtr<IDXGIFactory1> dxgiFactory;
|
|
|
|
if (GetDXGIFactory(dxgiFactory.GetAddressOf()))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-24 09:48:33 +03:00
|
|
|
wprintf(L"\n <adapter>:\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
2020-07-11 07:01:52 +03:00
|
|
|
for (UINT adapterIndex = 0;
|
|
|
|
SUCCEEDED(dxgiFactory->EnumAdapters(adapterIndex, adapter.ReleaseAndGetAddressOf()));
|
|
|
|
++adapterIndex)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
|
|
|
DXGI_ADAPTER_DESC desc;
|
2016-09-21 02:40:49 +03:00
|
|
|
if (SUCCEEDED(adapter->GetDesc(&desc)))
|
|
|
|
{
|
|
|
|
wprintf(L" %u: VID:%04X, PID:%04X - %ls\n", adapterIndex, desc.VendorId, desc.DeviceId, desc.Description);
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-01 10:59:57 +03:00
|
|
|
const wchar_t* GetErrorDesc(HRESULT hr)
|
|
|
|
{
|
|
|
|
static wchar_t desc[1024] = {};
|
|
|
|
|
|
|
|
LPWSTR errorText = nullptr;
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const DWORD result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
|
2021-06-03 00:36:31 +03:00
|
|
|
nullptr, static_cast<DWORD>(hr),
|
2021-04-01 10:59:57 +03:00
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>(&errorText), 0, nullptr);
|
|
|
|
|
|
|
|
*desc = 0;
|
|
|
|
|
|
|
|
if (result > 0 && errorText)
|
|
|
|
{
|
|
|
|
swprintf_s(desc, L": %ls", errorText);
|
|
|
|
|
|
|
|
size_t len = wcslen(desc);
|
2022-03-03 01:25:27 +03:00
|
|
|
if (len >= 1)
|
2021-04-01 10:59:57 +03:00
|
|
|
{
|
|
|
|
desc[len - 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorText)
|
|
|
|
LocalFree(errorText);
|
2023-05-17 21:38:08 +03:00
|
|
|
|
|
|
|
for (wchar_t* ptr = desc; *ptr != 0; ++ptr)
|
|
|
|
{
|
|
|
|
if (*ptr == L'\r' || *ptr == L'\n')
|
|
|
|
{
|
|
|
|
*ptr = L' ';
|
|
|
|
}
|
|
|
|
}
|
2021-04-01 10:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
2022-01-07 03:45:01 +03:00
|
|
|
_Success_(return)
|
2016-09-13 02:39:26 +03:00
|
|
|
bool CreateDevice(int adapter, _Outptr_ ID3D11Device** pDevice)
|
|
|
|
{
|
|
|
|
if (!pDevice)
|
|
|
|
return false;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
*pDevice = nullptr;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
static PFN_D3D11_CREATE_DEVICE s_DynamicD3D11CreateDevice = nullptr;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!s_DynamicD3D11CreateDevice)
|
|
|
|
{
|
2019-02-08 01:52:57 +03:00
|
|
|
HMODULE hModD3D11 = LoadLibraryW(L"d3d11.dll");
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!hModD3D11)
|
|
|
|
return false;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
s_DynamicD3D11CreateDevice = reinterpret_cast<PFN_D3D11_CREATE_DEVICE>(reinterpret_cast<void*>(GetProcAddress(hModD3D11, "D3D11CreateDevice")));
|
|
|
|
if (!s_DynamicD3D11CreateDevice)
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const D3D_FEATURE_LEVEL featureLevels[] =
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
|
|
|
D3D_FEATURE_LEVEL_11_0,
|
|
|
|
D3D_FEATURE_LEVEL_10_1,
|
|
|
|
D3D_FEATURE_LEVEL_10_0,
|
|
|
|
};
|
|
|
|
|
|
|
|
UINT createDeviceFlags = 0;
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifdef _DEBUG
|
2016-09-13 02:39:26 +03:00
|
|
|
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
ComPtr<IDXGIAdapter> pAdapter;
|
|
|
|
if (adapter >= 0)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
ComPtr<IDXGIFactory1> dxgiFactory;
|
|
|
|
if (GetDXGIFactory(dxgiFactory.GetAddressOf()))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2019-07-06 09:48:39 +03:00
|
|
|
if (FAILED(dxgiFactory->EnumAdapters(static_cast<UINT>(adapter), pAdapter.GetAddressOf())))
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Invalid GPU adapter index (%d)!\n", adapter);
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
D3D_FEATURE_LEVEL fl;
|
|
|
|
HRESULT hr = s_DynamicD3D11CreateDevice(pAdapter.Get(),
|
|
|
|
(pAdapter) ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
|
2021-01-02 23:44:35 +03:00
|
|
|
nullptr, createDeviceFlags, featureLevels, static_cast<UINT>(std::size(featureLevels)),
|
2016-09-13 02:39:26 +03:00
|
|
|
D3D11_SDK_VERSION, pDevice, &fl, nullptr);
|
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
if (fl < D3D_FEATURE_LEVEL_11_0)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
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)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
if (*pDevice)
|
|
|
|
{
|
|
|
|
(*pDevice)->Release();
|
|
|
|
*pDevice = nullptr;
|
|
|
|
}
|
|
|
|
hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
ComPtr<IDXGIDevice> dxgiDevice;
|
|
|
|
hr = (*pDevice)->QueryInterface(IID_PPV_ARGS(dxgiDevice.GetAddressOf()));
|
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = dxgiDevice->GetAdapter(pAdapter.ReleaseAndGetAddressOf());
|
|
|
|
if (SUCCEEDED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
DXGI_ADAPTER_DESC desc;
|
|
|
|
hr = pAdapter->GetDesc(&desc);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
2023-03-20 20:49:19 +03:00
|
|
|
wprintf(L"\n[Using DirectCompute %ls on \"%ls\"]\n",
|
|
|
|
(fl >= D3D_FEATURE_LEVEL_11_0) ? L"5.0" : L"4.0", desc.Description);
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 00:55:46 +03:00
|
|
|
void FitPowerOf2(size_t origx, size_t origy, _Inout_ size_t& targetx, _Inout_ size_t& targety, size_t maxsize)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const float origAR = float(origx) / float(origy);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (origx > origy)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
size_t x;
|
2019-07-25 08:36:14 +03:00
|
|
|
for (x = maxsize; x > 1; x >>= 1) { if (x <= targetx) break; }
|
2016-09-13 02:39:26 +03:00
|
|
|
targetx = x;
|
|
|
|
|
|
|
|
float bestScore = FLT_MAX;
|
|
|
|
for (size_t y = maxsize; y > 0; y >>= 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const float score = fabsf((float(x) / float(y)) - origAR);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (score < bestScore)
|
|
|
|
{
|
|
|
|
bestScore = score;
|
|
|
|
targety = y;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
else
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
size_t y;
|
2019-07-25 08:36:14 +03:00
|
|
|
for (y = maxsize; y > 1; y >>= 1) { if (y <= targety) break; }
|
2016-09-13 02:39:26 +03:00
|
|
|
targety = y;
|
|
|
|
|
|
|
|
float bestScore = FLT_MAX;
|
|
|
|
for (size_t x = maxsize; x > 0; x >>= 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const float score = fabsf((float(x) / float(y)) - origAR);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (score < bestScore)
|
|
|
|
{
|
|
|
|
bestScore = score;
|
|
|
|
targetx = x;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
constexpr size_t CountMips(_In_ size_t width, _In_ size_t height) noexcept
|
2021-04-28 23:42:45 +03:00
|
|
|
{
|
|
|
|
size_t mipLevels = 1;
|
|
|
|
|
|
|
|
while (height > 1 || width > 1)
|
|
|
|
{
|
|
|
|
if (height > 1)
|
|
|
|
height >>= 1;
|
|
|
|
|
|
|
|
if (width > 1)
|
|
|
|
width >>= 1;
|
|
|
|
|
|
|
|
++mipLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mipLevels;
|
|
|
|
}
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
constexpr size_t CountMips3D(_In_ size_t width, _In_ size_t height, _In_ size_t depth) noexcept
|
2021-04-28 23:42:45 +03:00
|
|
|
{
|
|
|
|
size_t mipLevels = 1;
|
|
|
|
|
|
|
|
while (height > 1 || width > 1 || depth > 1)
|
|
|
|
{
|
|
|
|
if (height > 1)
|
|
|
|
height >>= 1;
|
|
|
|
|
|
|
|
if (width > 1)
|
|
|
|
width >>= 1;
|
|
|
|
|
|
|
|
if (depth > 1)
|
|
|
|
depth >>= 1;
|
|
|
|
|
|
|
|
++mipLevels;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mipLevels;
|
|
|
|
}
|
|
|
|
|
2019-07-06 09:48:39 +03:00
|
|
|
const XMVECTORF32 c_MaxNitsFor2084 = { { { 10000.0f, 10000.0f, 10000.0f, 1.f } } };
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
// HDTV to UHDTV (Rec.709 color primaries into Rec.2020)
|
2018-02-08 04:14:11 +03:00
|
|
|
const XMMATRIX c_from709to2020 =
|
|
|
|
{
|
2020-11-11 10:26:09 +03:00
|
|
|
0.6274040f, 0.0690970f, 0.0163916f, 0.f,
|
|
|
|
0.3292820f, 0.9195400f, 0.0880132f, 0.f,
|
|
|
|
0.0433136f, 0.0113612f, 0.8955950f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
2018-02-08 04:14:11 +03:00
|
|
|
};
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
// UHDTV to HDTV
|
2018-02-08 04:14:11 +03:00
|
|
|
const XMMATRIX c_from2020to709 =
|
|
|
|
{
|
2020-11-11 10:26:09 +03:00
|
|
|
1.6604910f, -0.1245505f, -0.0181508f, 0.f,
|
|
|
|
-0.5876411f, 1.1328999f, -0.1005789f, 0.f,
|
|
|
|
-0.0728499f, -0.0083494f, 1.1187297f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
2018-02-08 04:14:11 +03:00
|
|
|
};
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
// DCI-P3-D65 https://en.wikipedia.org/wiki/DCI-P3 to UHDTV (DCI-P3-D65 color primaries into Rec.2020)
|
|
|
|
const XMMATRIX c_fromP3D65to2020 =
|
2018-02-08 04:14:11 +03:00
|
|
|
{
|
2020-11-11 10:26:09 +03:00
|
|
|
0.753845f, 0.0457456f, -0.00121055f, 0.f,
|
|
|
|
0.198593f, 0.941777f, 0.0176041f, 0.f,
|
|
|
|
0.047562f, 0.0124772f, 0.983607f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
2018-02-08 04:14:11 +03:00
|
|
|
};
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
// HDTV to DCI-P3-D65 (a.k.a. Display P3 or P3D65)
|
|
|
|
const XMMATRIX c_from709toP3D65 =
|
2021-03-30 01:40:06 +03:00
|
|
|
{
|
|
|
|
0.822461969f, 0.033194199f, 0.017082631f, 0.f,
|
|
|
|
0.1775380f, 0.9668058f, 0.0723974f, 0.f,
|
|
|
|
0.0000000f, 0.0000000f, 0.9105199f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
};
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
// DCI-P3-D65 to HDTV
|
|
|
|
const XMMATRIX c_fromP3D65to709 =
|
2021-03-30 01:40:06 +03:00
|
|
|
{
|
|
|
|
1.224940176f, -0.042056955f, -0.019637555f, 0.f,
|
|
|
|
-0.224940176f, 1.042056955f, -0.078636046f, 0.f,
|
|
|
|
0.0000000f, 0.0000000f, 1.098273600f, 0.f,
|
|
|
|
0.f, 0.f, 0.f, 1.f
|
|
|
|
};
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
inline float LinearToST2084(float normalizedLinearValue)
|
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const float ST2084 = pow((0.8359375f + 18.8515625f * pow(abs(normalizedLinearValue), 0.1593017578f)) / (1.0f + 18.6875f * pow(abs(normalizedLinearValue), 0.1593017578f)), 78.84375f);
|
2018-02-08 04:14:11 +03:00
|
|
|
return ST2084; // Don't clamp between [0..1], so we can still perform operations on scene values higher than 10,000 nits
|
|
|
|
}
|
|
|
|
|
|
|
|
inline float ST2084ToLinear(float ST2084)
|
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const float normalizedLinear = pow(std::max(pow(abs(ST2084), 1.0f / 78.84375f) - 0.8359375f, 0.0f) / (18.8515625f - 18.6875f * pow(abs(ST2084), 1.0f / 78.84375f)), 1.0f / 0.1593017578f);
|
2018-02-08 04:14:11 +03:00
|
|
|
return normalizedLinear;
|
|
|
|
}
|
2021-01-10 02:55:57 +03:00
|
|
|
|
2021-02-24 11:50:37 +03:00
|
|
|
bool ParseSwizzleMask(
|
|
|
|
_In_reads_(4) const wchar_t* mask,
|
|
|
|
_Out_writes_(4) uint32_t* swizzleElements,
|
|
|
|
_Out_writes_(4) uint32_t* zeroElements,
|
|
|
|
_Out_writes_(4) uint32_t* oneElements)
|
2021-01-10 02:55:57 +03:00
|
|
|
{
|
2021-02-24 11:50:37 +03:00
|
|
|
if (!mask || !swizzleElements || !zeroElements || !oneElements)
|
2021-01-10 02:55:57 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!mask[0])
|
|
|
|
return false;
|
|
|
|
|
2021-02-24 11:50:37 +03:00
|
|
|
for (uint32_t j = 0; j < 4; ++j)
|
2021-01-10 02:55:57 +03:00
|
|
|
{
|
|
|
|
if (!mask[j])
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (mask[j])
|
|
|
|
{
|
|
|
|
case L'R':
|
|
|
|
case L'X':
|
|
|
|
case L'r':
|
|
|
|
case L'x':
|
2021-02-24 11:50:37 +03:00
|
|
|
for (uint32_t k = j; k < 4; ++k)
|
|
|
|
{
|
2021-01-10 02:55:57 +03:00
|
|
|
swizzleElements[k] = 0;
|
2021-02-24 11:50:37 +03:00
|
|
|
zeroElements[k] = 0;
|
|
|
|
oneElements[k] = 0;
|
|
|
|
}
|
2021-01-10 02:55:57 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case L'G':
|
|
|
|
case L'Y':
|
|
|
|
case L'g':
|
|
|
|
case L'y':
|
2021-02-24 11:50:37 +03:00
|
|
|
for (uint32_t k = j; k < 4; ++k)
|
|
|
|
{
|
2021-01-10 02:55:57 +03:00
|
|
|
swizzleElements[k] = 1;
|
2021-02-24 11:50:37 +03:00
|
|
|
zeroElements[k] = 0;
|
|
|
|
oneElements[k] = 0;
|
|
|
|
}
|
2021-01-10 02:55:57 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case L'B':
|
|
|
|
case L'Z':
|
|
|
|
case L'b':
|
|
|
|
case L'z':
|
2021-02-24 11:50:37 +03:00
|
|
|
for (uint32_t k = j; k < 4; ++k)
|
|
|
|
{
|
2021-01-10 02:55:57 +03:00
|
|
|
swizzleElements[k] = 2;
|
2021-02-24 11:50:37 +03:00
|
|
|
zeroElements[k] = 0;
|
|
|
|
oneElements[k] = 0;
|
|
|
|
}
|
2021-01-10 02:55:57 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case L'A':
|
|
|
|
case L'W':
|
|
|
|
case L'a':
|
|
|
|
case L'w':
|
|
|
|
for (size_t k = j; k < 4; ++k)
|
2021-02-24 11:50:37 +03:00
|
|
|
{
|
2021-01-10 02:55:57 +03:00
|
|
|
swizzleElements[k] = 3;
|
2021-02-24 11:50:37 +03:00
|
|
|
zeroElements[k] = 0;
|
|
|
|
oneElements[k] = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case L'0':
|
|
|
|
for (uint32_t k = j; k < 4; ++k)
|
|
|
|
{
|
|
|
|
swizzleElements[k] = k;
|
|
|
|
zeroElements[k] = 1;
|
|
|
|
oneElements[k] = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case L'1':
|
|
|
|
for (uint32_t k = j; k < 4; ++k)
|
|
|
|
{
|
|
|
|
swizzleElements[k] = k;
|
|
|
|
zeroElements[k] = 0;
|
|
|
|
oneElements[k] = 1;
|
|
|
|
}
|
2021-01-10 02:55:57 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Entry-point
|
|
|
|
//--------------------------------------------------------------------------------------
|
2019-07-06 09:48:39 +03:00
|
|
|
#ifdef _PREFAST_
|
2016-08-22 21:26:36 +03:00
|
|
|
#pragma prefast(disable : 28198, "Command-line tool, frees all memory on exit")
|
2019-07-06 09:48:39 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
|
|
|
|
{
|
|
|
|
// Parameters and defaults
|
|
|
|
size_t width = 0;
|
2016-09-13 02:39:26 +03:00
|
|
|
size_t height = 0;
|
2016-08-22 21:26:36 +03:00
|
|
|
size_t mipLevels = 0;
|
|
|
|
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
|
2020-06-01 10:42:02 +03:00
|
|
|
TEX_FILTER_FLAGS dwFilter = TEX_FILTER_DEFAULT;
|
|
|
|
TEX_FILTER_FLAGS dwSRGB = TEX_FILTER_DEFAULT;
|
|
|
|
TEX_FILTER_FLAGS dwConvert = TEX_FILTER_DEFAULT;
|
|
|
|
TEX_COMPRESS_FLAGS dwCompress = TEX_COMPRESS_DEFAULT;
|
|
|
|
TEX_FILTER_FLAGS dwFilterOpts = TEX_FILTER_DEFAULT;
|
2021-04-25 05:57:56 +03:00
|
|
|
uint32_t FileType = CODEC_DDS;
|
|
|
|
uint32_t maxSize = 16384;
|
2016-08-22 21:26:36 +03:00
|
|
|
int adapter = -1;
|
2020-01-30 22:40:42 +03:00
|
|
|
float alphaThreshold = TEX_THRESHOLD_DEFAULT;
|
2016-08-22 21:26:36 +03:00
|
|
|
float alphaWeight = 1.f;
|
2020-06-01 10:42:02 +03:00
|
|
|
CNMAP_FLAGS dwNormalMap = CNMAP_DEFAULT;
|
2016-08-22 21:26:36 +03:00
|
|
|
float nmapAmplitude = 1.f;
|
|
|
|
float wicQuality = -1.f;
|
2021-04-25 05:57:56 +03:00
|
|
|
uint32_t colorKey = 0;
|
|
|
|
uint32_t dwRotateColor = 0;
|
2018-02-08 04:14:11 +03:00
|
|
|
float paperWhiteNits = 200.f;
|
2019-01-28 22:57:19 +03:00
|
|
|
float preserveAlphaCoverageRef = 0.0f;
|
2021-01-09 23:44:59 +03:00
|
|
|
bool keepRecursiveDirs = false;
|
2023-06-13 00:34:32 +03:00
|
|
|
bool dxt5nm = false;
|
|
|
|
bool dxt5rxgb = false;
|
2021-01-10 02:55:57 +03:00
|
|
|
uint32_t swizzleElements[4] = { 0, 1, 2, 3 };
|
2021-02-24 11:50:37 +03:00
|
|
|
uint32_t zeroElements[4] = {};
|
|
|
|
uint32_t oneElements[4] = {};
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2019-10-11 06:53:25 +03:00
|
|
|
wchar_t szPrefix[MAX_PATH] = {};
|
|
|
|
wchar_t szSuffix[MAX_PATH] = {};
|
2023-06-14 01:40:45 +03:00
|
|
|
std::filesystem::path outputDir;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-29 00:33:45 +03:00
|
|
|
// Set locale for output since GetErrorDesc can get localized strings.
|
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// Initialize COM (needed for WIC)
|
|
|
|
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L"Failed to initialize COM (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process command line
|
2021-04-25 05:57:56 +03:00
|
|
|
uint64_t dwOptions = 0;
|
2016-08-22 21:26:36 +03:00
|
|
|
std::list<SConversion> conversion;
|
2023-03-28 02:02:41 +03:00
|
|
|
bool allowOpts = true;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
for (int iArg = 1; iArg < argc; iArg++)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
PWSTR pArg = argv[iArg];
|
|
|
|
|
2023-03-28 02:02:41 +03:00
|
|
|
if (allowOpts
|
|
|
|
&& ('-' == pArg[0]) && ('-' == pArg[1]))
|
|
|
|
{
|
|
|
|
if (pArg[2] == 0)
|
|
|
|
{
|
|
|
|
// "-- " is the POSIX standard for "end of options" marking to escape the '-' and '/' characters at the start of filepaths.
|
|
|
|
allowOpts = false;
|
|
|
|
}
|
|
|
|
else if (!_wcsicmp(pArg,L"--version"))
|
|
|
|
{
|
|
|
|
PrintLogo(true);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (!_wcsicmp(pArg, L"--help"))
|
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf(L"Unknown option: %ls\n", pArg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (allowOpts
|
|
|
|
&& (('-' == pArg[0]) || ('/' == pArg[0])))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
pArg++;
|
|
|
|
PWSTR pValue;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
for (pValue = pArg; *pValue && (':' != *pValue); pValue++);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (*pValue)
|
2016-08-22 21:26:36 +03:00
|
|
|
*pValue++ = 0;
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const uint64_t dwOption = LookupByName(pArg, g_pOptions);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (!dwOption || (dwOptions & (uint64_t(1) << dwOption)))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
dwOptions |= (uint64_t(1) << dwOption);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// Handle options with additional value parameter
|
|
|
|
switch (dwOption)
|
|
|
|
{
|
|
|
|
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:
|
|
|
|
case OPT_GPU:
|
|
|
|
case OPT_FEATURE_LEVEL:
|
2020-01-30 22:40:42 +03:00
|
|
|
case OPT_ALPHA_THRESHOLD:
|
2016-08-22 21:26:36 +03:00
|
|
|
case OPT_ALPHA_WEIGHT:
|
|
|
|
case OPT_NORMAL_MAP:
|
|
|
|
case OPT_NORMAL_MAP_AMPLITUDE:
|
|
|
|
case OPT_WIC_QUALITY:
|
2020-05-10 11:26:32 +03:00
|
|
|
case OPT_BC_COMPRESS:
|
2016-09-14 04:09:46 +03:00
|
|
|
case OPT_COLORKEY:
|
2017-09-19 21:13:34 +03:00
|
|
|
case OPT_FILELIST:
|
2018-02-08 04:14:11 +03:00
|
|
|
case OPT_ROTATE_COLOR:
|
|
|
|
case OPT_PAPER_WHITE_NITS:
|
2019-01-28 22:57:19 +03:00
|
|
|
case OPT_PRESERVE_ALPHA_COVERAGE:
|
2021-01-10 02:55:57 +03:00
|
|
|
case OPT_SWIZZLE:
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
case OPT_XGMODE:
|
|
|
|
#endif
|
2021-01-09 23:44:59 +03:00
|
|
|
// These support either "-arg:value" or "-arg value"
|
2016-08-22 21:26:36 +03:00
|
|
|
if (!*pValue)
|
|
|
|
{
|
|
|
|
if ((iArg + 1 >= argc))
|
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iArg++;
|
|
|
|
pValue = argv[iArg];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
switch (dwOption)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
case OPT_WIDTH:
|
2018-05-02 03:34:54 +03:00
|
|
|
if (swscanf_s(pValue, L"%zu", &width) != 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -w (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_HEIGHT:
|
2018-05-02 03:34:54 +03:00
|
|
|
if (swscanf_s(pValue, L"%zu", &height) != 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -h (%ls)\n", pValue);
|
2016-08-22 21:26:36 +03:00
|
|
|
printf("\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_MIPLEVELS:
|
2018-05-02 03:34:54 +03:00
|
|
|
if (swscanf_s(pValue, L"%zu", &mipLevels) != 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -m (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FORMAT:
|
2018-02-22 11:17:49 +03:00
|
|
|
format = static_cast<DXGI_FORMAT>(LookupByName(pValue, g_pFormats));
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!format)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2018-02-22 11:17:49 +03:00
|
|
|
format = static_cast<DXGI_FORMAT>(LookupByName(pValue, g_pFormatAliases));
|
|
|
|
if (!format)
|
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
switch (LookupByName(pValue, g_pSpecialFormats))
|
|
|
|
{
|
|
|
|
case FORMAT_DXT5_NM:
|
|
|
|
format = DXGI_FORMAT_BC3_UNORM;
|
|
|
|
dxt5nm = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FORMAT_DXT5_RXGB:
|
|
|
|
format = DXGI_FORMAT_BC3_UNORM;
|
|
|
|
dxt5rxgb = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wprintf(L"Invalid value specified with -f (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-22 11:17:49 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FILTER:
|
2020-06-01 10:42:02 +03:00
|
|
|
dwFilter = static_cast<TEX_FILTER_FLAGS>(LookupByName(pValue, g_pFilters));
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!dwFilter)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -if (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
case OPT_ROTATE_COLOR:
|
|
|
|
dwRotateColor = LookupByName(pValue, g_pRotateColor);
|
|
|
|
if (!dwRotateColor)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -rotatecolor (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
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;
|
|
|
|
|
2018-08-02 22:31:20 +03:00
|
|
|
case OPT_NO_WIC:
|
|
|
|
dwFilterOpts |= TEX_FILTER_FORCE_NON_WIC;
|
|
|
|
break;
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
case OPT_PREFIX:
|
|
|
|
wcscpy_s(szPrefix, MAX_PATH, pValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_SUFFIX:
|
|
|
|
wcscpy_s(szSuffix, MAX_PATH, pValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_OUTPUTDIR:
|
2023-03-28 02:02:41 +03:00
|
|
|
{
|
|
|
|
std::filesystem::path path(pValue);
|
2023-06-14 01:40:45 +03:00
|
|
|
outputDir = path.make_preferred();
|
2023-03-28 02:02:41 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FILETYPE:
|
|
|
|
FileType = LookupByName(pValue, g_pSaveFileTypes);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!FileType)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -ft (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-09-14 10:03:22 +03:00
|
|
|
case OPT_PREMUL_ALPHA:
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_DEMUL_ALPHA))
|
2016-09-14 10:03:22 +03:00
|
|
|
{
|
|
|
|
wprintf(L"Can't use -pmalpha and -alpha at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_DEMUL_ALPHA:
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_PREMUL_ALPHA))
|
2016-09-14 10:03:22 +03:00
|
|
|
{
|
|
|
|
wprintf(L"Can't use -pmalpha and -alpha at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
case OPT_TA_WRAP:
|
2016-09-13 02:39:26 +03:00
|
|
|
if (dwFilterOpts & TEX_FILTER_MIRROR)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Can't use -wrap and -mirror at same time\n\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dwFilterOpts |= TEX_FILTER_WRAP;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_TA_MIRROR:
|
2016-09-13 02:39:26 +03:00
|
|
|
if (dwFilterOpts & TEX_FILTER_WRAP)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Can't use -wrap and -mirror at same time\n\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dwFilterOpts |= TEX_FILTER_MIRROR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_NORMAL_MAP:
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
dwNormalMap = CNMAP_DEFAULT;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if (wcschr(pValue, L'l'))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
dwNormalMap |= CNMAP_CHANNEL_LUMINANCE;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
else if (wcschr(pValue, L'r'))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
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);
|
|
|
|
return 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if (wcschr(pValue, L'i'))
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_INVERT_SIGN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wcschr(pValue, L'o'))
|
|
|
|
{
|
|
|
|
dwNormalMap |= CNMAP_COMPUTE_OCCLUSION;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
break;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
case OPT_NORMAL_MAP_AMPLITUDE:
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!dwNormalMap)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"-nmapamp requires -nmap\n\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (swscanf_s(pValue, L"%f", &nmapAmplitude) != 1)
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -nmapamp (%ls)\n\n", pValue);
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
else if (nmapAmplitude < 0.f)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Normal map amplitude must be positive (%ls)\n\n", pValue);
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2021-01-10 02:55:57 +03:00
|
|
|
wprintf(L"Invalid adapter index (%ls)\n\n", pValue);
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_FEATURE_LEVEL:
|
2016-09-13 02:39:26 +03:00
|
|
|
maxSize = LookupByName(pValue, g_pFeatureLevels);
|
|
|
|
if (!maxSize)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -fl (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-01-30 22:40:42 +03:00
|
|
|
case OPT_ALPHA_THRESHOLD:
|
|
|
|
if (swscanf_s(pValue, L"%f", &alphaThreshold) != 1)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -at (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (alphaThreshold < 0.f)
|
|
|
|
{
|
|
|
|
wprintf(L"-at (%ls) parameter must be positive\n", pValue);
|
|
|
|
wprintf(L"\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
case OPT_ALPHA_WEIGHT:
|
|
|
|
if (swscanf_s(pValue, L"%f", &alphaWeight) != 1)
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"Invalid value specified with -aw (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
else if (alphaWeight < 0.f)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"-aw (%ls) parameter must be positive\n", pValue);
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-05-10 11:26:32 +03:00
|
|
|
case OPT_BC_COMPRESS:
|
2016-09-21 02:59:15 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
dwCompress = TEX_COMPRESS_DEFAULT;
|
2020-05-10 11:26:32 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
bool found = false;
|
|
|
|
if (wcschr(pValue, L'u'))
|
|
|
|
{
|
|
|
|
dwCompress |= TEX_COMPRESS_UNIFORM;
|
|
|
|
found = true;
|
|
|
|
}
|
2020-05-10 11:26:32 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if (wcschr(pValue, L'd'))
|
|
|
|
{
|
|
|
|
dwCompress |= TEX_COMPRESS_DITHER;
|
|
|
|
found = true;
|
|
|
|
}
|
2020-05-10 11:26:32 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if (wcschr(pValue, L'q'))
|
|
|
|
{
|
|
|
|
dwCompress |= TEX_COMPRESS_BC7_QUICK;
|
|
|
|
found = true;
|
|
|
|
}
|
2020-05-10 11:26:32 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if (wcschr(pValue, L'x'))
|
|
|
|
{
|
|
|
|
dwCompress |= TEX_COMPRESS_BC7_USE_3SUBSETS;
|
|
|
|
found = true;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
if ((dwCompress & (TEX_COMPRESS_BC7_QUICK | TEX_COMPRESS_BC7_USE_3SUBSETS)) == (TEX_COMPRESS_BC7_QUICK | TEX_COMPRESS_BC7_USE_3SUBSETS))
|
|
|
|
{
|
|
|
|
wprintf(L"Can't use -bc x (max) and -bc q (quick) at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified for -bc (%ls), missing d, u, q, or x\n\n", pValue);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-21 02:59:15 +03:00
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
break;
|
2016-08-22 21:26:36 +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;
|
|
|
|
|
2016-09-14 04:09:46 +03:00
|
|
|
case OPT_COLORKEY:
|
2021-06-05 01:06:31 +03:00
|
|
|
if (swscanf_s(pValue, L"%x", &colorKey) != 1)
|
2016-09-14 04:09:46 +03:00
|
|
|
{
|
|
|
|
printf("Invalid value specified with -c (%ls)\n", pValue);
|
|
|
|
printf("\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
colorKey &= 0xFFFFFF;
|
|
|
|
break;
|
2016-09-30 10:16:56 +03:00
|
|
|
|
|
|
|
case OPT_X2_BIAS:
|
|
|
|
dwConvert |= TEX_FILTER_FLOAT_X2BIAS;
|
|
|
|
break;
|
2017-09-19 21:13:34 +03:00
|
|
|
|
2020-05-10 11:26:32 +03:00
|
|
|
case OPT_USE_DX10:
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_USE_DX9))
|
2020-05-10 11:26:32 +03:00
|
|
|
{
|
|
|
|
wprintf(L"Can't use -dx9 and -dx10 at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_USE_DX9:
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_USE_DX10))
|
2020-05-10 11:26:32 +03:00
|
|
|
{
|
|
|
|
wprintf(L"Can't use -dx9 and -dx10 at same time\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-09 23:44:59 +03:00
|
|
|
case OPT_RECURSIVE:
|
|
|
|
if (*pValue)
|
|
|
|
{
|
|
|
|
// This option takes 'flatten' or 'keep' with ':' syntax
|
|
|
|
if (!_wcsicmp(pValue, L"keep"))
|
|
|
|
{
|
|
|
|
keepRecursiveDirs = true;
|
|
|
|
}
|
|
|
|
else if (_wcsicmp(pValue, L"flatten") != 0)
|
|
|
|
{
|
|
|
|
wprintf(L"For recursive use -r, -r:flatten, or -r:keep\n\n");
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-19 21:13:34 +03:00
|
|
|
case OPT_FILELIST:
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
2023-03-28 02:02:41 +03:00
|
|
|
std::filesystem::path path(pValue);
|
|
|
|
std::wifstream inFile(path.make_preferred().c_str());
|
2022-04-05 01:03:02 +03:00
|
|
|
if (!inFile)
|
|
|
|
{
|
|
|
|
wprintf(L"Error opening -flist file %ls\n", pValue);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-09-14 03:31:09 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
inFile.imbue(std::locale::classic());
|
2021-10-28 21:12:16 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
ProcessFileList(inFile, conversion);
|
|
|
|
}
|
|
|
|
break;
|
2018-02-08 04:14:11 +03:00
|
|
|
|
|
|
|
case OPT_PAPER_WHITE_NITS:
|
|
|
|
if (swscanf_s(pValue, L"%f", &paperWhiteNits) != 1)
|
|
|
|
{
|
2019-01-28 22:57:19 +03:00
|
|
|
wprintf(L"Invalid value specified with -nits (%ls)\n\n", pValue);
|
2018-02-08 04:14:11 +03:00
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (paperWhiteNits > 10000.f || paperWhiteNits <= 0.f)
|
|
|
|
{
|
2019-01-28 22:57:19 +03:00
|
|
|
wprintf(L"-nits (%ls) parameter must be between 0 and 10000\n\n", pValue);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OPT_PRESERVE_ALPHA_COVERAGE:
|
|
|
|
if (swscanf_s(pValue, L"%f", &preserveAlphaCoverageRef) != 1)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -keepcoverage (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (preserveAlphaCoverageRef < 0.0f || preserveAlphaCoverageRef > 1.0f)
|
|
|
|
{
|
|
|
|
wprintf(L"-keepcoverage (%ls) parameter must be between 0.0 and 1.0\n\n", pValue);
|
2018-02-08 04:14:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2021-01-10 02:55:57 +03:00
|
|
|
|
|
|
|
case OPT_SWIZZLE:
|
|
|
|
if (!*pValue || wcslen(pValue) > 4)
|
|
|
|
{
|
|
|
|
wprintf(L"Invalid value specified with -swizzle (%ls)\n\n", pValue);
|
|
|
|
PrintUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
2021-02-24 11:50:37 +03:00
|
|
|
else if (!ParseSwizzleMask(pValue, swizzleElements, zeroElements, oneElements))
|
2021-01-10 02:55:57 +03:00
|
|
|
{
|
2021-02-24 11:50:37 +03:00
|
|
|
wprintf(L"-swizzle requires a 1 to 4 character mask composed of these letters: r, g, b, a, x, y, w, z, 0, 1\n");
|
2021-01-10 02:55:57 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2024-01-22 18:58:00 +03:00
|
|
|
|
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
case OPT_XGMODE:
|
|
|
|
{
|
|
|
|
#ifdef _USE_SCARLETT
|
|
|
|
static const SValue<uint32_t> s_pXGModes[] =
|
|
|
|
{
|
|
|
|
{ L"xboxseriess", XG_HARDWARE_VERSION_XBOX_SCARLETT_LOCKHART },
|
|
|
|
{ L"xboxseriesx", XG_HARDWARE_VERSION_XBOX_SCARLETT_ANACONDA },
|
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
static const SValue<uint32_t> s_pXGModes[] =
|
|
|
|
{
|
|
|
|
{ L"xboxone", XG_HARDWARE_VERSION_XBOX_ONE },
|
|
|
|
{ L"xboxonex", XG_HARDWARE_VERSION_XBOX_ONE_X },
|
|
|
|
{ L"scorpio", XG_HARDWARE_VERSION_SCORPIO },
|
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const uint32_t mode = LookupByName(pValue, s_pXGModes);
|
|
|
|
if (!mode)
|
|
|
|
{
|
|
|
|
printf("Invalid value specified with -xgmode (%ls)\n", pValue);
|
|
|
|
wprintf(L"\n <mode>: ");
|
|
|
|
PrintList(14, s_pXGModes);
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
XGSetHardwareVersion(static_cast<XG_HARDWARE_VERSION>(mode));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif // USE_XBOX_EXTS
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-13 07:54:08 +03:00
|
|
|
else if (wcspbrk(pArg, L"?*") != nullptr)
|
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t count = conversion.size();
|
2023-03-28 02:02:41 +03:00
|
|
|
std::filesystem::path path(pArg);
|
2023-06-14 01:40:45 +03:00
|
|
|
SearchForFiles(path.make_preferred(), conversion, (dwOptions & (uint64_t(1) << OPT_RECURSIVE)) != 0, nullptr);
|
2016-09-13 07:54:08 +03:00
|
|
|
if (conversion.size() <= count)
|
|
|
|
{
|
|
|
|
wprintf(L"No matching files found for %ls\n", pArg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
else
|
|
|
|
{
|
2021-01-09 23:44:59 +03:00
|
|
|
SConversion conv = {};
|
2023-03-28 02:02:41 +03:00
|
|
|
std::filesystem::path path(pArg);
|
2023-06-14 01:40:45 +03:00
|
|
|
conv.szSrc = path.make_preferred().native();
|
2016-08-22 21:26:36 +03:00
|
|
|
conversion.push_back(conv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (conversion.empty())
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
PrintUsage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (~dwOptions & (uint64_t(1) << OPT_NOLOGO))
|
2023-03-28 02:02:41 +03:00
|
|
|
PrintLogo(false);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2019-10-11 06:53:25 +03:00
|
|
|
auto fileTypeName = LookupByValue(FileType, g_pSaveFileTypes);
|
2016-08-22 21:26:36 +03: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;
|
|
|
|
}
|
|
|
|
|
2021-04-27 07:25:30 +03:00
|
|
|
LARGE_INTEGER qpcFreq = {};
|
2021-10-17 21:57:07 +03:00
|
|
|
std::ignore = QueryPerformanceFrequency(&qpcFreq);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-27 07:25:30 +03:00
|
|
|
LARGE_INTEGER qpcStart = {};
|
2021-10-17 21:57:07 +03:00
|
|
|
std::ignore = QueryPerformanceCounter(&qpcStart);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// Convert images
|
2019-10-11 06:53:25 +03:00
|
|
|
bool sizewarn = false;
|
2016-08-22 21:26:36 +03:00
|
|
|
bool nonpow2warn = false;
|
|
|
|
bool non4bc = false;
|
2019-01-28 22:57:19 +03:00
|
|
|
bool preserveAlphaCoverage = false;
|
2016-08-22 21:26:36 +03:00
|
|
|
ComPtr<ID3D11Device> pDevice;
|
|
|
|
|
2021-02-04 03:04:36 +03:00
|
|
|
int retVal = 0;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
for (auto pConv = conversion.begin(); pConv != conversion.end(); ++pConv)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
if (pConv != conversion.begin())
|
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2019-10-11 06:53:25 +03:00
|
|
|
// --- Load source image -------------------------------------------------------
|
2023-06-14 01:40:45 +03:00
|
|
|
wprintf(L"reading %ls", pConv->szSrc.c_str());
|
2016-08-22 21:26:36 +03:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
TexMetadata info;
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> image(new (std::nothrow) ScratchImage);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!image)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
std::filesystem::path curpath(pConv->szSrc);
|
|
|
|
auto const ext = curpath.extension();
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifndef USE_XBOX_EXTS
|
|
|
|
constexpr
|
|
|
|
#endif
|
|
|
|
bool isXbox = false;
|
2023-06-14 01:40:45 +03:00
|
|
|
if (_wcsicmp(ext.c_str(), L".dds") == 0 || _wcsicmp(ext.c_str(), L".ddx") == 0)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
hr = Xbox::GetMetadataFromDDSFile(curpath.c_str(), info, isXbox);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
retVal = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isXbox)
|
|
|
|
{
|
|
|
|
Xbox::XboxImage xbox;
|
|
|
|
|
|
|
|
hr = Xbox::LoadFromDDSFile(curpath.c_str(), &info, xbox);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = Xbox::Detile(xbox, *image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // USE_XBOX_EXTS
|
|
|
|
{
|
|
|
|
DDS_FLAGS ddsFlags = DDS_FLAGS_ALLOW_LARGE_FILES;
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_DDS_DWORD_ALIGN))
|
|
|
|
ddsFlags |= DDS_FLAGS_LEGACY_DWORD;
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_EXPAND_LUMINANCE))
|
|
|
|
ddsFlags |= DDS_FLAGS_EXPAND_LUMINANCE;
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_DDS_BAD_DXTN_TAILS))
|
|
|
|
ddsFlags |= DDS_FLAGS_BAD_DXTN_TAILS;
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_DDS_PERMISSIVE))
|
|
|
|
ddsFlags |= DDS_FLAGS_PERMISSIVE;
|
|
|
|
|
|
|
|
hr = LoadFromDDSFile(curpath.c_str(), ddsFlags, &info, *image);
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (IsTypeless(info.format))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_TYPELESS_UNORM))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
info.format = MakeTypelessUNORM(info.format);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2021-04-25 05:57:56 +03:00
|
|
|
else if (dwOptions & (uint64_t(1) << OPT_TYPELESS_FLOAT))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
info.format = MakeTypelessFLOAT(info.format);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (IsTypeless(info.format))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L" FAILED due to Typeless format %d\n", info.format);
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image->OverrideFormat(info.format);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".bmp") == 0)
|
2018-02-22 12:49:04 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromBMPEx(curpath.c_str(), WIC_FLAGS_NONE | dwFilter, &info, *image);
|
2018-02-22 12:49:04 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2018-02-22 12:49:04 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".tga") == 0)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2022-05-08 01:10:55 +03:00
|
|
|
TGA_FLAGS tgaFlags = (IsBGR(format)) ? TGA_FLAGS_BGR : TGA_FLAGS_NONE;
|
2023-06-09 19:52:45 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_TGAZEROALPHA))
|
|
|
|
{
|
|
|
|
tgaFlags |= TGA_FLAGS_ALLOW_ALL_ZERO_ALPHA;
|
|
|
|
}
|
2022-05-08 01:10:55 +03:00
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromTGAFile(curpath.c_str(), tgaFlags, &info, *image);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".hdr") == 0)
|
2016-09-11 06:43:33 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromHDRFile(curpath.c_str(), &info, *image);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-09-11 06:43:33 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-09-11 06:43:33 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".ppm") == 0)
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromPortablePixMap(curpath.c_str(), &info, *image);
|
2020-06-05 06:07:21 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2020-06-05 06:07:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".pfm") == 0)
|
2020-06-05 06:07:21 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromPortablePixMapHDR(curpath.c_str(), &info, *image);
|
2020-06-05 06:07:21 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2020-06-05 06:07:21 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifdef USE_OPENEXR
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".exr") == 0)
|
2016-10-02 11:53:31 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromEXRFile(curpath.c_str(), &info, *image);
|
2016-10-02 11:53:31 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-10-02 11:53:31 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2024-01-20 22:02:19 +03:00
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBJPEG
|
|
|
|
else if (_wcsicmp(ext.c_str(), L".jpg") == 0 || _wcsicmp(ext.c_str(), L".jpeg") == 0)
|
|
|
|
{
|
|
|
|
hr = LoadFromJPEGFile(curpath.c_str(), &info, *image);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
retVal = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBPNG
|
|
|
|
else if (_wcsicmp(ext.c_str(), L".png") == 0)
|
|
|
|
{
|
|
|
|
hr = LoadFromPNGFile(curpath.c_str(), &info, *image);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
retVal = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// WIC shares the same filter values for mode and dither
|
2019-07-06 09:48:39 +03:00
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_DITHER) == static_cast<int>(TEX_FILTER_DITHER), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_DITHER_DIFFUSION) == static_cast<int>(TEX_FILTER_DITHER_DIFFUSION), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_FILTER_POINT) == static_cast<int>(TEX_FILTER_POINT), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_FILTER_LINEAR) == static_cast<int>(TEX_FILTER_LINEAR), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_FILTER_CUBIC) == static_cast<int>(TEX_FILTER_CUBIC), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
|
|
|
static_assert(static_cast<int>(WIC_FLAGS_FILTER_FANT) == static_cast<int>(TEX_FILTER_FANT), "WIC_FLAGS_* & TEX_FILTER_* should match");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2020-06-01 10:42:02 +03:00
|
|
|
WIC_FLAGS wicFlags = WIC_FLAGS_NONE | dwFilter;
|
2016-08-22 21:26:36 +03:00
|
|
|
if (FileType == CODEC_DDS)
|
|
|
|
wicFlags |= WIC_FLAGS_ALL_FRAMES;
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = LoadFromWICFile(curpath.c_str(), wicFlags, &info, *image);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2022-03-03 01:25:27 +03:00
|
|
|
if (hr == static_cast<HRESULT>(0xc00d5212) /* MF_E_TOPO_CODEC_NOT_FOUND */)
|
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
if (_wcsicmp(ext.c_str(), L".heic") == 0 || _wcsicmp(ext.c_str(), L".heif") == 0)
|
2022-03-03 01:25:27 +03:00
|
|
|
{
|
|
|
|
wprintf(L"INFO: This format requires installing the HEIF Image Extensions - https://aka.ms/heif\n");
|
|
|
|
}
|
2023-06-14 01:40:45 +03:00
|
|
|
else if (_wcsicmp(ext.c_str(), L".webp") == 0)
|
2022-03-03 01:25:27 +03:00
|
|
|
{
|
|
|
|
wprintf(L"INFO: This format requires installing the WEBP Image Extensions - https://www.microsoft.com/p/webp-image-extensions/9pg2dk419drg\n");
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
PrintInfo(info, isXbox);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
size_t tMips = (!mipLevels && info.mipLevels > 1) ? info.mipLevels : mipLevels;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// Convert texture
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L" as");
|
2016-08-22 21:26:36 +03:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
// --- Planar ------------------------------------------------------------------
|
2016-09-13 02:39:26 +03:00
|
|
|
if (IsPlanar(info.format))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t nimg = image->GetImageCount();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = ConvertToSinglePlane(img, nimg, info, *timage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [converttosingleplane] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
|
|
|
info.format = tinfo.format;
|
2016-09-14 04:09:46 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
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.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const DXGI_FORMAT tformat = (format == DXGI_FORMAT_UNKNOWN) ? info.format : format;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// --- Decompress --------------------------------------------------------------
|
|
|
|
std::unique_ptr<ScratchImage> cimage;
|
2016-09-13 02:39:26 +03:00
|
|
|
if (IsCompressed(info.format))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2019-10-11 06:53:25 +03:00
|
|
|
// Direct3D can only create BC resources with multiple-of-4 top levels
|
|
|
|
if ((info.width % 4) != 0 || (info.height % 4) != 0)
|
|
|
|
{
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_BCNONMULT4FIX))
|
2019-10-11 06:53:25 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we started with < 4x4 then no need to generate mips
|
|
|
|
if (info.width < 4 && info.height < 4)
|
|
|
|
{
|
|
|
|
tMips = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix by changing size but also have to trim any mip-levels which can be invalid
|
|
|
|
TexMetadata mdata = image->GetMetadata();
|
|
|
|
mdata.width = (info.width + 3u) & ~0x3u;
|
|
|
|
mdata.height = (info.height + 3u) & ~0x3u;
|
|
|
|
mdata.mipLevels = 1;
|
|
|
|
hr = timage->Initialize(mdata);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [BC non-multiple-of-4 fixup] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2019-10-11 06:53:25 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdata.dimension == TEX_DIMENSION_TEXTURE3D)
|
|
|
|
{
|
|
|
|
for (size_t d = 0; d < mdata.depth; ++d)
|
|
|
|
{
|
|
|
|
auto simg = image->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 = image->GetImage(0, i, 0);
|
|
|
|
auto dimg = timage->GetImage(0, i, 0);
|
|
|
|
|
|
|
|
memcpy_s(dimg->pixels, dimg->slicePitch, simg->pixels, simg->slicePitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
info.width = mdata.width;
|
|
|
|
info.height = mdata.height;
|
|
|
|
info.mipLevels = mdata.mipLevels;
|
|
|
|
image.swap(timage);
|
|
|
|
}
|
|
|
|
else if (IsCompressed(tformat))
|
|
|
|
{
|
|
|
|
non4bc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t nimg = image->GetImageCount();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = Decompress(img, nimg, info, DXGI_FORMAT_UNKNOWN /* picks good default */, *timage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [decompress] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
|
|
|
info.format = tinfo.format;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
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.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FileType == CODEC_DDS)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
// Keep the original compressed image in case we can reuse it
|
2016-09-13 02:39:26 +03:00
|
|
|
cimage.reset(image.release());
|
|
|
|
image.reset(timage.release());
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:03:22 +03:00
|
|
|
// --- Undo Premultiplied Alpha (if requested) ---------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if ((dwOptions & (uint64_t(1) << OPT_DEMUL_ALPHA))
|
2016-09-14 10:03:22 +03:00
|
|
|
&& HasAlpha(info.format)
|
|
|
|
&& info.format != DXGI_FORMAT_A8_UNORM)
|
|
|
|
{
|
|
|
|
if (info.GetAlphaMode() == TEX_ALPHA_MODE_STRAIGHT)
|
|
|
|
{
|
|
|
|
printf("\nWARNING: Image is already using straight alpha\n");
|
|
|
|
}
|
|
|
|
else if (!info.IsPMAlpha())
|
|
|
|
{
|
|
|
|
printf("\nWARNING: Image is not using premultipled alpha\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t nimg = image->GetImageCount();
|
2016-09-14 10:03:22 +03:00
|
|
|
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:21:44 +03:00
|
|
|
hr = PremultiplyAlpha(img, nimg, info, TEX_PMALPHA_REVERSE | dwSRGB, *timage);
|
2016-09-14 10:03:22 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [demultiply alpha] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-09-14 10:03:22 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
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.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// --- Flip/Rotate -------------------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & ((uint64_t(1) << OPT_HFLIP) | (uint64_t(1) << OPT_VFLIP)))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:42:02 +03:00
|
|
|
TEX_FR_FLAGS dwFlags = TEX_FR_ROTATE0;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_HFLIP))
|
2016-08-22 21:26:36 +03:00
|
|
|
dwFlags |= TEX_FR_FLIP_HORIZONTAL;
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_VFLIP))
|
2016-08-22 21:26:36 +03:00
|
|
|
dwFlags |= TEX_FR_FLIP_VERTICAL;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(dwFlags != 0);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = FlipRotate(image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFlags, *timage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [fliprotate] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
|
|
|
info.width = tinfo.width;
|
|
|
|
info.height = tinfo.height;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(info.depth == tinfo.depth);
|
|
|
|
assert(info.arraySize == tinfo.arraySize);
|
|
|
|
assert(info.mipLevels == tinfo.mipLevels);
|
|
|
|
assert(info.miscFlags == tinfo.miscFlags);
|
|
|
|
assert(info.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Resize ------------------------------------------------------------------
|
2019-10-11 06:53:25 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_FIT_POWEROF2))
|
2019-10-11 06:53:25 +03:00
|
|
|
{
|
|
|
|
FitPowerOf2(info.width, info.height, twidth, theight, maxSize);
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.width != twidth || info.height != theight)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = Resize(image->GetImages(), image->GetImageCount(), image->GetMetadata(), twidth, theight, dwFilter | dwFilterOpts, *timage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [resize] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(tinfo.width == twidth && tinfo.height == theight && tinfo.mipLevels == 1);
|
2016-08-22 21:26:36 +03:00
|
|
|
info.width = tinfo.width;
|
|
|
|
info.height = tinfo.height;
|
|
|
|
info.mipLevels = 1;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(info.depth == tinfo.depth);
|
|
|
|
assert(info.arraySize == tinfo.arraySize);
|
|
|
|
assert(info.miscFlags == tinfo.miscFlags);
|
|
|
|
assert(info.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
cimage.reset();
|
2021-04-28 23:42:45 +03:00
|
|
|
|
|
|
|
if (tMips > 0)
|
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t maxMips = (info.depth > 1)
|
2021-04-28 23:42:45 +03:00
|
|
|
? CountMips3D(info.width, info.height, info.depth)
|
|
|
|
: CountMips(info.width, info.height);
|
|
|
|
|
|
|
|
if (tMips > maxMips)
|
|
|
|
{
|
|
|
|
tMips = maxMips;
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 02:55:57 +03:00
|
|
|
// --- Swizzle (if requested) --------------------------------------------------
|
2021-02-24 11:50:37 +03:00
|
|
|
if (swizzleElements[0] != 0 || swizzleElements[1] != 1 || swizzleElements[2] != 2 || swizzleElements[3] != 3
|
|
|
|
|| zeroElements[0] != 0 || zeroElements[1] != 0 || zeroElements[2] != 0 || zeroElements[3] != 0
|
|
|
|
|| oneElements[0] != 0 || oneElements[1] != 0 || oneElements[2] != 0 || oneElements[3] != 0)
|
2021-01-10 02:55:57 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR zc = XMVectorSelectControl(zeroElements[0], zeroElements[1], zeroElements[2], zeroElements[3]);
|
|
|
|
const XMVECTOR oc = XMVectorSelectControl(oneElements[0], oneElements[1], oneElements[2], oneElements[3]);
|
2021-02-24 11:50:37 +03:00
|
|
|
|
2021-01-10 02:55:57 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2021-02-24 11:50:37 +03:00
|
|
|
[&, zc, oc](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2021-01-10 02:55:57 +03:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
2021-06-16 10:02:58 +03:00
|
|
|
XMVECTOR pixel = XMVectorSwizzle(inPixels[j],
|
2021-01-10 02:55:57 +03:00
|
|
|
swizzleElements[0], swizzleElements[1], swizzleElements[2], swizzleElements[3]);
|
2021-06-16 10:02:58 +03:00
|
|
|
pixel = XMVectorSelect(pixel, g_XMZero, zc);
|
|
|
|
outPixels[j] = XMVectorSelect(pixel, g_XMOne, oc);
|
2021-01-10 02:55:57 +03:00
|
|
|
}
|
|
|
|
}, *timage);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [swizzle] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-01-10 02:55:57 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2021-01-10 02:55:57 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2021-01-10 02:55:57 +03:00
|
|
|
|
|
|
|
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.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
// --- Color rotation (if requested) -------------------------------------------
|
|
|
|
if (dwRotateColor)
|
|
|
|
{
|
2021-07-27 21:42:30 +03:00
|
|
|
if (dwRotateColor == ROTATE_HDR10_TO_709 || dwRotateColor == ROTATE_P3D65_TO_709)
|
2018-02-09 04:21:27 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = Convert(image->GetImages(), image->GetImageCount(), image->GetMetadata(), DXGI_FORMAT_R16G16B16A16_FLOAT,
|
2020-01-30 22:40:42 +03:00
|
|
|
dwFilter | dwFilterOpts | dwSRGB | dwConvert, alphaThreshold, *timage);
|
2018-02-09 04:21:27 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [convert] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2018-02-09 04:21:27 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2018-02-09 04:21:27 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2018-02-09 04:21:27 +03:00
|
|
|
|
|
|
|
assert(tinfo.format == DXGI_FORMAT_R16G16B16A16_FLOAT);
|
|
|
|
info.format = DXGI_FORMAT_R16G16B16A16_FLOAT;
|
|
|
|
|
|
|
|
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.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dwRotateColor)
|
|
|
|
{
|
|
|
|
case ROTATE_709_TO_HDR10:
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR paperWhite = XMVectorReplicate(paperWhiteNits);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
XMVECTOR nvalue = XMVector3Transform(value, c_from709to2020);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
// Convert to ST.2084
|
|
|
|
nvalue = XMVectorDivide(XMVectorMultiply(nvalue, paperWhite), c_MaxNitsFor2084);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
XMFLOAT4A tmp;
|
|
|
|
XMStoreFloat4A(&tmp, nvalue);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
tmp.x = LinearToST2084(tmp.x);
|
|
|
|
tmp.y = LinearToST2084(tmp.y);
|
|
|
|
tmp.z = LinearToST2084(tmp.z);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
nvalue = XMLoadFloat4A(&tmp);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ROTATE_709_TO_2020:
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2018-02-08 04:14:11 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVector3Transform(value, c_from709to2020);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ROTATE_HDR10_TO_709:
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR paperWhite = XMVectorReplicate(paperWhiteNits);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
// Convert from ST.2084
|
|
|
|
XMFLOAT4A tmp;
|
|
|
|
XMStoreFloat4A(&tmp, value);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
tmp.x = ST2084ToLinear(tmp.x);
|
|
|
|
tmp.y = ST2084ToLinear(tmp.y);
|
|
|
|
tmp.z = ST2084ToLinear(tmp.z);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
XMVECTOR nvalue = XMLoadFloat4A(&tmp);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
nvalue = XMVectorDivide(XMVectorMultiply(nvalue, c_MaxNitsFor2084), paperWhite);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
nvalue = XMVector3Transform(nvalue, c_from2020to709);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ROTATE_2020_TO_709:
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2018-02-08 04:14:11 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVector3Transform(value, c_from2020to709);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
case ROTATE_P3D65_TO_HDR10:
|
2018-02-08 04:14:11 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR paperWhite = XMVectorReplicate(paperWhiteNits);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
XMVECTOR nvalue = XMVector3Transform(value, c_fromP3D65to2020);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
// Convert to ST.2084
|
|
|
|
nvalue = XMVectorDivide(XMVectorMultiply(nvalue, paperWhite), c_MaxNitsFor2084);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
XMFLOAT4A tmp;
|
|
|
|
XMStoreFloat4A(&tmp, nvalue);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
tmp.x = LinearToST2084(tmp.x);
|
|
|
|
tmp.y = LinearToST2084(tmp.y);
|
|
|
|
tmp.z = LinearToST2084(tmp.z);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
nvalue = XMLoadFloat4A(&tmp);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
case ROTATE_P3D65_TO_2020:
|
2018-02-08 04:14:11 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2018-02-08 04:14:11 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVector3Transform(value, c_fromP3D65to2020);
|
2018-02-08 04:14:11 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2018-02-08 04:14:11 +03:00
|
|
|
break;
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
case ROTATE_709_TO_P3D65:
|
2021-03-30 01:40:06 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVector3Transform(value, c_from709toP3D65);
|
2021-03-30 01:40:06 +03:00
|
|
|
|
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
|
|
|
break;
|
|
|
|
|
2021-07-27 21:42:30 +03:00
|
|
|
case ROTATE_P3D65_TO_709:
|
2021-03-30 01:40:06 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVector3Transform(value, c_fromP3D65to709);
|
2021-03-30 01:40:06 +03:00
|
|
|
|
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
|
|
|
break;
|
|
|
|
|
2018-02-08 04:14:11 +03:00
|
|
|
default:
|
|
|
|
hr = E_NOTIMPL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [rotate color apply] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2018-02-08 04:14:11 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2018-02-08 04:14:11 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2018-02-08 04:14:11 +03:00
|
|
|
|
|
|
|
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.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2017-02-26 10:57:13 +03:00
|
|
|
// --- Tonemap (if requested) --------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & uint64_t(1) << OPT_TONEMAP)
|
2017-02-26 10:57:13 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute max luminosity across all images
|
|
|
|
XMVECTOR maxLum = XMVectorZero();
|
|
|
|
hr = EvaluateImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](const XMVECTOR* pixels, size_t w, size_t y)
|
2017-02-26 10:57:13 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
static const XMVECTORF32 s_luminance = { { { 0.3f, 0.59f, 0.11f, 0.f } } };
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
XMVECTOR v = *pixels++;
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
v = XMVector3Dot(v, s_luminance);
|
|
|
|
|
|
|
|
maxLum = XMVectorMax(v, maxLum);
|
|
|
|
}
|
|
|
|
});
|
2017-02-26 10:57:13 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [tonemap maxlum] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2017-02-26 10:57:13 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
// Reinhard et al, "Photographic Tone Reproduction for Digital Images"
|
2017-02-26 10:57:13 +03:00
|
|
|
// http://www.cs.utah.edu/~reinhard/cdrom/
|
|
|
|
maxLum = XMVectorMultiply(maxLum, maxLum);
|
|
|
|
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2017-02-26 10:57:13 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
XMVECTOR value = inPixels[j];
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR scale = XMVectorDivide(
|
2019-09-14 03:31:09 +03:00
|
|
|
XMVectorAdd(g_XMOne, XMVectorDivide(value, maxLum)),
|
|
|
|
XMVectorAdd(g_XMOne, value));
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR nvalue = XMVectorMultiply(value, scale);
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
value = XMVectorSelect(value, nvalue, g_XMSelect1110);
|
2017-02-26 10:57:13 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2017-02-26 10:57:13 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [tonemap apply] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2017-02-26 10:57:13 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2017-02-26 10:57:13 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2017-02-26 10:57:13 +03:00
|
|
|
|
|
|
|
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.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// --- Convert -----------------------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_NORMAL_MAP))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DXGI_FORMAT nmfmt = tformat;
|
2016-09-13 02:39:26 +03:00
|
|
|
if (IsCompressed(tformat))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2020-02-14 10:36:16 +03:00
|
|
|
switch (tformat)
|
|
|
|
{
|
|
|
|
case DXGI_FORMAT_BC4_SNORM:
|
|
|
|
case DXGI_FORMAT_BC5_SNORM:
|
2022-05-08 10:33:37 +03:00
|
|
|
nmfmt = (BitsPerColor(info.format) > 8) ? DXGI_FORMAT_R16G16B16A16_SNORM : DXGI_FORMAT_R8G8B8A8_SNORM;
|
2020-02-14 10:36:16 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DXGI_FORMAT_BC6H_SF16:
|
|
|
|
case DXGI_FORMAT_BC6H_UF16:
|
|
|
|
nmfmt = DXGI_FORMAT_R32G32B32_FLOAT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-05-08 10:33:37 +03:00
|
|
|
nmfmt = (BitsPerColor(info.format) > 8) ? DXGI_FORMAT_R16G16B16A16_UNORM : DXGI_FORMAT_R8G8B8A8_UNORM;
|
2020-02-14 10:36:16 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = ComputeNormalMap(image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwNormalMap, nmapAmplitude, nmfmt, *timage);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [normalmap] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
2016-09-13 02:39:26 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(tinfo.format == nmfmt);
|
2016-08-22 21:26:36 +03:00
|
|
|
info.format = tinfo.format;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
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.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
cimage.reset();
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
else if (info.format != tformat && !IsCompressed(tformat))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-14 20:56:38 +03:00
|
|
|
hr = Convert(image->GetImages(), image->GetImageCount(), image->GetMetadata(), tformat,
|
2020-01-30 22:40:42 +03:00
|
|
|
dwFilter | dwFilterOpts | dwSRGB | dwConvert, alphaThreshold, *timage);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [convert] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(tinfo.format == tformat);
|
2016-08-22 21:26:36 +03:00
|
|
|
info.format = tinfo.format;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
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.dimension == tinfo.dimension);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:09:46 +03:00
|
|
|
// --- ColorKey/ChromaKey ------------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if ((dwOptions & (uint64_t(1) << OPT_COLORKEY))
|
2017-02-26 11:32:11 +03:00
|
|
|
&& HasAlpha(info.format))
|
2016-09-14 04:09:46 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMVECTOR colorKeyValue = XMLoadColor(reinterpret_cast<const XMCOLOR*>(&colorKey));
|
|
|
|
|
2016-09-19 21:33:43 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2016-09-14 04:09:46 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
static const XMVECTORF32 s_tolerance = { { { 0.2f, 0.2f, 0.2f, 0.f } } };
|
2016-09-14 04:09:46 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
2016-09-14 04:09:46 +03:00
|
|
|
{
|
2019-09-14 03:31:09 +03:00
|
|
|
XMVECTOR value = inPixels[j];
|
2016-09-14 04:09:46 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
if (XMVector3NearEqual(value, colorKeyValue, s_tolerance))
|
|
|
|
{
|
|
|
|
value = g_XMZero;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = XMVectorSelect(g_XMOne, value, g_XMSelect1110);
|
|
|
|
}
|
|
|
|
|
|
|
|
outPixels[j] = value;
|
|
|
|
}
|
|
|
|
}, *timage);
|
2016-09-14 04:09:46 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [colorkey] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-09-14 04:09:46 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2016-09-14 04:09:46 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2016-09-14 04:09:46 +03:00
|
|
|
|
|
|
|
assert(info.width == tinfo.width);
|
2019-01-19 01:20:32 +03:00
|
|
|
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.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Invert Y Channel --------------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_INVERT_Y))
|
2019-01-19 01:20:32 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
2019-07-06 09:48:39 +03:00
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
|
|
|
static const XMVECTORU32 s_selecty = { { { XM_SELECT_0, XM_SELECT_1, XM_SELECT_0, XM_SELECT_0 } } };
|
2019-01-19 01:20:32 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
2019-01-19 01:20:32 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR value = inPixels[j];
|
2019-01-19 01:20:32 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const XMVECTOR inverty = XMVectorSubtract(g_XMOne, value);
|
2019-01-19 01:20:32 +03:00
|
|
|
|
2019-09-14 03:31:09 +03:00
|
|
|
outPixels[j] = XMVectorSelect(value, inverty, s_selecty);
|
|
|
|
}
|
|
|
|
}, *timage);
|
2019-01-19 01:20:32 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [inverty] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2019-01-19 01:20:32 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2020-07-14 11:59:19 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2020-07-14 11:59:19 +03:00
|
|
|
|
|
|
|
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.format == tinfo.format);
|
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Reconstruct Z Channel ---------------------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_RECONSTRUCT_Z))
|
2020-07-14 11:59:19 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:20:48 +03:00
|
|
|
bool isunorm = (FormatDataType(info.format) == FORMAT_TYPE_UNORM) != 0;
|
|
|
|
|
2020-07-14 11:59:19 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
|
|
|
[&](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
static const XMVECTORU32 s_selectz = { { { XM_SELECT_0, XM_SELECT_0, XM_SELECT_1, XM_SELECT_0 } } };
|
2020-07-14 11:59:19 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
2021-02-24 11:20:48 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
const XMVECTOR value = inPixels[j];
|
2020-07-14 11:59:19 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
XMVECTOR z;
|
|
|
|
if (isunorm)
|
|
|
|
{
|
|
|
|
XMVECTOR x2 = XMVectorMultiplyAdd(value, g_XMTwo, g_XMNegativeOne);
|
|
|
|
x2 = XMVectorSqrt(XMVectorSubtract(g_XMOne, XMVector2Dot(x2, x2)));
|
|
|
|
z = XMVectorMultiplyAdd(x2, g_XMOneHalf, g_XMOneHalf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
z = XMVectorSqrt(XMVectorSubtract(g_XMOne, XMVector2Dot(value, value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
outPixels[j] = XMVectorSelect(value, z, s_selectz);
|
|
|
|
}
|
|
|
|
}, *timage);
|
2020-07-14 11:59:19 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [reconstructz] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2020-07-14 11:59:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2019-01-19 01:20:32 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2019-01-19 01:20:32 +03:00
|
|
|
|
|
|
|
assert(info.width == tinfo.width);
|
2016-09-14 04:09:46 +03:00
|
|
|
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);
|
2017-02-26 10:57:13 +03:00
|
|
|
assert(info.format == tinfo.format);
|
2016-09-14 04:09:46 +03:00
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:57:19 +03:00
|
|
|
// --- Determine whether preserve alpha coverage is required (if requested) ----
|
|
|
|
if (preserveAlphaCoverageRef > 0.0f && HasAlpha(info.format) && !image->IsAlphaAllOpaque())
|
|
|
|
{
|
|
|
|
preserveAlphaCoverage = true;
|
|
|
|
}
|
2019-09-14 03:31:09 +03:00
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// --- Generate mips -----------------------------------------------------------
|
2020-06-01 10:42:02 +03:00
|
|
|
TEX_FILTER_FLAGS dwFilter3D = dwFilter;
|
2016-09-13 02:39:26 +03:00
|
|
|
if (!ispow2(info.width) || !ispow2(info.height) || !ispow2(info.depth))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2018-04-18 10:08:35 +03:00
|
|
|
if (!tMips || info.mipLevels != 1)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2018-04-18 10:08:35 +03:00
|
|
|
nonpow2warn = true;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2018-04-18 10:08:35 +03:00
|
|
|
|
|
|
|
if (info.dimension == TEX_DIMENSION_TEXTURE3D)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2018-04-18 10:08:35 +03:00
|
|
|
// Must force triangle filter for non-power-of-2 volume textures to get correct results
|
|
|
|
dwFilter3D = TEX_FILTER_TRIANGLE;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:57:19 +03:00
|
|
|
if ((!tMips || info.mipLevels != tMips || preserveAlphaCoverage) && (info.mipLevels != 1))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
// Mips generation only works on a single base image, so strip off existing mip levels
|
2019-01-28 22:57:19 +03:00
|
|
|
// Also required for preserve alpha coverage so that existing mips are regenerated
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
TexMetadata mdata = info;
|
|
|
|
mdata.mipLevels = 1;
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = timage->Initialize(mdata);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [copy to single level] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.dimension == TEX_DIMENSION_TEXTURE3D)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
for (size_t d = 0; d < info.depth; ++d)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
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))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [copy to single level] (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
for (size_t i = 0; i < info.arraySize; ++i)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
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))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [copy to single level] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
image.swap(timage);
|
2021-03-04 11:21:44 +03:00
|
|
|
info.mipLevels = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (cimage && (tMips == 1))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
// Special case for trimming mips off compressed images and keeping the original compressed highest level mip
|
|
|
|
mdata = cimage->GetMetadata();
|
|
|
|
mdata.mipLevels = 1;
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = timage->Initialize(mdata);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [copy compressed to single level] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (mdata.dimension == TEX_DIMENSION_TEXTURE3D)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
for (size_t d = 0; d < mdata.depth; ++d)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
auto simg = cimage->GetImage(0, 0, d);
|
|
|
|
auto dimg = timage->GetImage(0, 0, d);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
memcpy_s(dimg->pixels, dimg->slicePitch, simg->pixels, simg->slicePitch);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
for (size_t i = 0; i < mdata.arraySize; ++i)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
auto simg = cimage->GetImage(0, i, 0);
|
|
|
|
auto dimg = timage->GetImage(0, i, 0);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
memcpy_s(dimg->pixels, dimg->slicePitch, simg->pixels, simg->slicePitch);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
cimage.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if ((!tMips || info.mipLevels != tMips) && (info.width > 1 || info.height > 1 || info.depth > 1))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.dimension == TEX_DIMENSION_TEXTURE3D)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2018-04-18 10:08:35 +03:00
|
|
|
hr = GenerateMipMaps3D(image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFilter3D | dwFilterOpts, tMips, *timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
hr = GenerateMipMaps(image->GetImages(), image->GetImageCount(), image->GetMetadata(), dwFilter | dwFilterOpts, tMips, *timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [mipmaps] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
info.mipLevels = tinfo.mipLevels;
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(info.width == tinfo.width);
|
|
|
|
assert(info.height == tinfo.height);
|
|
|
|
assert(info.depth == tinfo.depth);
|
|
|
|
assert(info.arraySize == tinfo.arraySize);
|
|
|
|
assert(info.miscFlags == tinfo.miscFlags);
|
2017-02-26 10:57:13 +03:00
|
|
|
assert(info.format == tinfo.format);
|
2016-09-24 03:36:38 +03:00
|
|
|
assert(info.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2019-01-28 22:57:19 +03:00
|
|
|
// --- Preserve mipmap alpha coverage (if requested) ---------------------------
|
2019-01-29 02:58:59 +03:00
|
|
|
if (preserveAlphaCoverage && info.mipLevels != 1 && (info.dimension != TEX_DIMENSION_TEXTURE3D))
|
2019-01-28 22:57:19 +03:00
|
|
|
{
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = timage->Initialize(image->GetMetadata());
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [keepcoverage] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2019-01-28 22:57:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2019-09-14 03:31:09 +03:00
|
|
|
|
2019-01-28 22:57:19 +03:00
|
|
|
const size_t items = image->GetMetadata().arraySize;
|
|
|
|
for (size_t item = 0; item < items; ++item)
|
|
|
|
{
|
|
|
|
auto img = image->GetImage(0, item, 0);
|
|
|
|
assert(img);
|
|
|
|
|
2019-01-29 02:58:59 +03:00
|
|
|
hr = ScaleMipMapsAlphaForCoverage(img, info.mipLevels, info, item, preserveAlphaCoverageRef, *timage);
|
2019-01-28 22:57:19 +03:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [keepcoverage] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2019-01-28 22:57:19 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifndef NDEBUG
|
2019-01-28 22:57:19 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2019-01-28 22:57:19 +03:00
|
|
|
|
|
|
|
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.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// --- Premultiplied alpha (if requested) --------------------------------------
|
2021-04-25 05:57:56 +03:00
|
|
|
if ((dwOptions & (uint64_t(1) << OPT_PREMUL_ALPHA))
|
2016-09-13 02:39:26 +03:00
|
|
|
&& HasAlpha(info.format)
|
|
|
|
&& info.format != DXGI_FORMAT_A8_UNORM)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
if (info.IsPMAlpha())
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-14 10:03:22 +03:00
|
|
|
printf("\nWARNING: Image is already using premultiplied alpha\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t nimg = image->GetImageCount();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:42:02 +03:00
|
|
|
hr = PremultiplyAlpha(img, nimg, info, TEX_PMALPHA_DEFAULT | dwSRGB, *timage);
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED [premultiply alpha] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
info.miscFlags2 = tinfo.miscFlags2;
|
2016-09-13 02:39:26 +03:00
|
|
|
|
|
|
|
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.dimension == tinfo.dimension);
|
|
|
|
|
|
|
|
image.swap(timage);
|
2016-08-22 21:26:36 +03:00
|
|
|
cimage.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Compress ----------------------------------------------------------------
|
2023-06-13 00:34:32 +03:00
|
|
|
if (FileType == CODEC_DDS)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
if (dxt5nm || dxt5rxgb)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
// Prepare for DXT5nm/RXGB
|
|
|
|
assert(tformat == DXGI_FORMAT_BC3_UNORM);
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
if (dxt5nm)
|
2016-09-13 02:39:26 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
|
|
|
[=](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
outPixels[j] = XMVectorPermute<4, 1, 5, 0>(inPixels[j], g_XMIdentityR0);
|
|
|
|
}
|
|
|
|
}, *timage);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED [DXT5nm] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2023-06-13 00:34:32 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = TransformImage(image->GetImages(), image->GetImageCount(), image->GetMetadata(),
|
|
|
|
[=](XMVECTOR* outPixels, const XMVECTOR* inPixels, size_t w, size_t y)
|
|
|
|
{
|
|
|
|
UNREFERENCED_PARAMETER(y);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < w; ++j)
|
|
|
|
{
|
|
|
|
outPixels[j] = XMVectorSwizzle<3, 1, 2, 0>(inPixels[j]);
|
|
|
|
}
|
|
|
|
}, *timage);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED [DXT5 RXGB] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
auto& tinfo = timage->GetMetadata();
|
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
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);
|
2023-06-13 00:34:32 +03:00
|
|
|
assert(info.format == tinfo.format);
|
2016-09-13 02:39:26 +03:00
|
|
|
assert(info.dimension == tinfo.dimension);
|
2023-06-13 00:34:32 +03:00
|
|
|
|
|
|
|
image.swap(timage);
|
|
|
|
cimage.reset();
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
2023-06-13 00:34:32 +03:00
|
|
|
|
|
|
|
if (IsCompressed(tformat))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
if (cimage && (cimage->GetMetadata().format == tformat))
|
|
|
|
{
|
|
|
|
// We never changed the image and it was already compressed in our desired format, use original data
|
|
|
|
image.reset(cimage.release());
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
auto& tinfo = image->GetMetadata();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
if ((tinfo.width % 4) != 0 || (tinfo.height % 4) != 0)
|
|
|
|
{
|
|
|
|
non4bc = true;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03: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.dimension == tinfo.dimension);
|
|
|
|
}
|
|
|
|
else
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
cimage.reset();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
|
|
|
const size_t nimg = image->GetImageCount();
|
|
|
|
|
|
|
|
std::unique_ptr<ScratchImage> timage(new (std::nothrow) ScratchImage);
|
|
|
|
if (!timage)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
wprintf(L"\nERROR: Memory allocation failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bc6hbc7 = false;
|
|
|
|
switch (tformat)
|
|
|
|
{
|
|
|
|
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;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
static bool s_tryonce = false;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
if (!s_tryonce)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
s_tryonce = true;
|
|
|
|
|
|
|
|
if (!(dwOptions & (uint64_t(1) << OPT_NOGPU)))
|
|
|
|
{
|
|
|
|
if (!CreateDevice(adapter, pDevice.GetAddressOf()))
|
|
|
|
wprintf(L"\nWARNING: DirectCompute is not available, using BC6H / BC7 CPU codec\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wprintf(L"\nWARNING: using BC6H / BC7 CPU codec\n");
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 00:34:32 +03:00
|
|
|
break;
|
2019-07-06 09:48:39 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
TEX_COMPRESS_FLAGS cflags = dwCompress;
|
|
|
|
#ifdef _OPENMP
|
|
|
|
if (!(dwOptions & (uint64_t(1) << OPT_FORCE_SINGLEPROC)))
|
|
|
|
{
|
|
|
|
cflags |= TEX_COMPRESS_PARALLEL;
|
|
|
|
}
|
|
|
|
#endif
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
if ((img->width % 4) != 0 || (img->height % 4) != 0)
|
|
|
|
{
|
|
|
|
non4bc = true;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
if (bc6hbc7 && pDevice)
|
|
|
|
{
|
|
|
|
hr = Compress(pDevice.Get(), img, nimg, info, tformat, dwCompress | dwSRGB, alphaWeight, *timage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hr = Compress(img, nimg, info, tformat, cflags | dwSRGB, alphaThreshold, *timage);
|
|
|
|
}
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
wprintf(L" FAILED [compress] (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
|
|
|
retVal = 1;
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
auto& tinfo = timage->GetMetadata();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2023-06-13 00:34:32 +03: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.dimension == tinfo.dimension);
|
2016-09-13 02:39:26 +03:00
|
|
|
|
2023-06-13 00:34:32 +03:00
|
|
|
image.swap(timage);
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
2023-06-13 00:34:32 +03:00
|
|
|
|
|
|
|
cimage.reset();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// --- Set alpha mode ----------------------------------------------------------
|
2016-09-13 02:39:26 +03:00
|
|
|
if (HasAlpha(info.format)
|
|
|
|
&& info.format != DXGI_FORMAT_A8_UNORM)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
if (dxt5nm || dxt5rxgb)
|
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_CUSTOM);
|
|
|
|
}
|
|
|
|
else if (image->IsAlphaAllOpaque())
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_OPAQUE);
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
else if (info.IsPMAlpha())
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
// Aleady set TEX_ALPHA_MODE_PREMULTIPLIED
|
|
|
|
}
|
2021-04-25 05:57:56 +03:00
|
|
|
else if (dwOptions & (uint64_t(1) << OPT_SEPALPHA))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_CUSTOM);
|
|
|
|
}
|
2016-09-14 10:03:22 +03:00
|
|
|
else if (info.GetAlphaMode() == TEX_ALPHA_MODE_UNKNOWN)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_STRAIGHT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-14 10:03:22 +03:00
|
|
|
info.SetAlphaMode(TEX_ALPHA_MODE_UNKNOWN);
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- Save result -------------------------------------------------------------
|
|
|
|
{
|
2016-09-13 02:39:26 +03:00
|
|
|
auto img = image->GetImage(0, 0, 0);
|
|
|
|
assert(img);
|
2022-03-02 01:31:55 +03:00
|
|
|
const size_t nimg = image->GetImageCount();
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
const bool isXboxOut = ((FileType == CODEC_DDS) && (dwOptions & (uint64_t(1) << OPT_USE_XBOX))) != 0;
|
|
|
|
#else
|
|
|
|
constexpr bool isXboxOut = false;
|
|
|
|
#endif
|
|
|
|
PrintInfo(info, isXboxOut);
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
// Figure out dest filename
|
2023-06-14 01:40:45 +03:00
|
|
|
std::filesystem::path dest(outputDir);
|
2021-01-09 23:44:59 +03:00
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
if (keepRecursiveDirs && !pConv->szFolder.empty())
|
2021-01-09 23:44:59 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
dest.append(pConv->szFolder.c_str());
|
2021-01-09 23:44:59 +03:00
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
std::error_code ec;
|
|
|
|
auto apath = std::filesystem::absolute(dest, ec);
|
|
|
|
|
|
|
|
if (ec)
|
2021-01-09 23:44:59 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
wprintf(L" get full path FAILED (%hs)\n", ec.message().c_str());
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2021-01-09 23:44:59 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
auto const err = static_cast<DWORD>(SHCreateDirectoryExW(nullptr, apath.c_str(), nullptr));
|
2021-01-09 23:44:59 +03:00
|
|
|
if (err != ERROR_SUCCESS && err != ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" directory creation FAILED (%08X%ls)\n",
|
|
|
|
static_cast<unsigned int>(HRESULT_FROM_WIN32(err)), GetErrorDesc(HRESULT_FROM_WIN32(err)));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2021-01-09 23:44:59 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*szPrefix)
|
2023-06-14 01:40:45 +03:00
|
|
|
{
|
|
|
|
dest.append(szPrefix);
|
|
|
|
dest.concat(curpath.stem().c_str());
|
|
|
|
dest.concat(szSuffix);
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
else
|
2020-04-14 03:22:55 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
dest.append(curpath.stem().c_str());
|
|
|
|
dest.concat(szSuffix);
|
2021-01-09 23:44:59 +03:00
|
|
|
}
|
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
std::wstring destName = dest.c_str();
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_TOLOWER))
|
2021-01-09 23:44:59 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
std::transform(destName.begin(), destName.end(), destName.begin(), towlower);
|
2020-04-14 03:22:55 +03:00
|
|
|
}
|
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
// Write texture
|
2023-06-14 01:40:45 +03:00
|
|
|
wprintf(L"writing %ls", destName.c_str());
|
2016-08-22 21:26:36 +03:00
|
|
|
fflush(stdout);
|
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (~dwOptions & (uint64_t(1) << OPT_OVERWRITE))
|
2016-09-24 09:48:33 +03:00
|
|
|
{
|
2023-06-14 01:40:45 +03:00
|
|
|
if (GetFileAttributesW(destName.c_str()) != INVALID_FILE_ATTRIBUTES)
|
2016-09-24 09:48:33 +03:00
|
|
|
{
|
|
|
|
wprintf(L"\nERROR: Output file already exists, use -y to overwrite:\n");
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2016-09-24 09:48:33 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
switch (FileType)
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
|
|
|
case CODEC_DDS:
|
2024-01-22 18:58:00 +03:00
|
|
|
#ifdef USE_XBOX_EXTS
|
|
|
|
if (isXboxOut)
|
|
|
|
{
|
|
|
|
Xbox::XboxImage xbox;
|
|
|
|
|
|
|
|
hr = Xbox::Tile(img, nimg, info, xbox);
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = Xbox::SaveToDDSFile(xbox, destName.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // USE_XBOX_EXTS
|
2020-05-10 11:26:32 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
DDS_FLAGS ddsFlags = DDS_FLAGS_NONE;
|
|
|
|
if (dwOptions & (uint64_t(1) << OPT_USE_DX10))
|
|
|
|
{
|
|
|
|
ddsFlags |= DDS_FLAGS_FORCE_DX10_EXT | DDS_FLAGS_FORCE_DX10_EXT_MISC2;
|
|
|
|
}
|
|
|
|
else if (dwOptions & (uint64_t(1) << OPT_USE_DX9))
|
|
|
|
{
|
2023-06-13 00:34:32 +03:00
|
|
|
if (dxt5rxgb)
|
|
|
|
{
|
|
|
|
ddsFlags |= DDS_FLAGS_FORCE_DXT5_RXGB;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
ddsFlags |= DDS_FLAGS_FORCE_DX9_LEGACY;
|
|
|
|
}
|
2020-05-10 11:26:32 +03:00
|
|
|
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToDDSFile(img, nimg, info, ddsFlags, destName.c_str());
|
2022-04-05 01:03:02 +03:00
|
|
|
}
|
2024-01-22 18:58:00 +03:00
|
|
|
break;
|
2016-08-22 21:26:36 +03:00
|
|
|
|
|
|
|
case CODEC_TGA:
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToTGAFile(img[0], TGA_FLAGS_NONE, destName.c_str(), (dwOptions & (uint64_t(1) << OPT_TGA20)) ? &info : nullptr);
|
2016-08-22 21:26:36 +03:00
|
|
|
break;
|
|
|
|
|
2016-09-11 06:43:33 +03:00
|
|
|
case CODEC_HDR:
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToHDRFile(img[0], destName.c_str());
|
2016-09-11 06:43:33 +03:00
|
|
|
break;
|
|
|
|
|
2020-06-05 06:07:21 +03:00
|
|
|
case CODEC_PPM:
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToPortablePixMap(img[0], destName.c_str());
|
2020-06-05 06:07:21 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CODEC_PFM:
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToPortablePixMapHDR(img[0], destName.c_str());
|
2020-06-05 06:07:21 +03:00
|
|
|
break;
|
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
#ifdef USE_OPENEXR
|
2016-10-02 11:53:31 +03:00
|
|
|
case CODEC_EXR:
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToEXRFile(img[0], destName.c_str());
|
2016-10-02 11:53:31 +03:00
|
|
|
break;
|
2022-04-05 01:03:02 +03:00
|
|
|
#endif
|
2024-01-20 22:02:19 +03:00
|
|
|
#ifdef USE_LIBJPEG
|
|
|
|
case CODEC_JPEG:
|
|
|
|
hr = SaveToJPEGFile(img[0], destName.c_str());
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_LIBPNG
|
|
|
|
case CODEC_PNG:
|
|
|
|
hr = SaveToPNGFile(img[0], destName.c_str());
|
|
|
|
break;
|
|
|
|
#endif
|
2016-10-02 11:53:31 +03:00
|
|
|
|
2016-08-22 21:26:36 +03:00
|
|
|
default:
|
2022-04-05 01:03:02 +03:00
|
|
|
{
|
|
|
|
const WICCodecs codec = (FileType == CODEC_HDP || FileType == CODEC_JXR) ? WIC_CODEC_WMP : static_cast<WICCodecs>(FileType);
|
|
|
|
const size_t nimages = (dwOptions & (uint64_t(1) << OPT_WIC_MULTIFRAME)) ? nimg : 1;
|
2023-06-14 01:40:45 +03:00
|
|
|
hr = SaveToWICFile(img, nimages, WIC_FLAGS_NONE, GetWICCodec(codec), destName.c_str(), nullptr,
|
2022-04-05 01:03:02 +03:00
|
|
|
[&](IPropertyBag2* props)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
const bool wicLossless = (dwOptions & (uint64_t(1) << OPT_WIC_LOSSLESS)) != 0;
|
2019-09-14 03:31:09 +03:00
|
|
|
|
2022-04-05 01:03:02 +03:00
|
|
|
switch (FileType)
|
2019-09-14 03:31:09 +03:00
|
|
|
{
|
2022-04-05 01:03:02 +03:00
|
|
|
case WIC_CODEC_JPEG:
|
|
|
|
if (wicLossless || wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
options.pstrName = const_cast<wchar_t*>(L"ImageQuality");
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = (wicLossless) ? 1.f : wicQuality;
|
|
|
|
std::ignore = props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIC_CODEC_TIFF:
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
if (wicLossless)
|
|
|
|
{
|
|
|
|
options.pstrName = const_cast<wchar_t*>(L"TiffCompressionMethod");
|
|
|
|
varValues.vt = VT_UI1;
|
|
|
|
varValues.bVal = WICTiffCompressionNone;
|
|
|
|
}
|
|
|
|
else if (wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
options.pstrName = const_cast<wchar_t*>(L"CompressionQuality");
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = wicQuality;
|
|
|
|
}
|
|
|
|
std::ignore = props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WIC_CODEC_WMP:
|
|
|
|
case CODEC_HDP:
|
|
|
|
case CODEC_JXR:
|
|
|
|
{
|
|
|
|
PROPBAG2 options = {};
|
|
|
|
VARIANT varValues = {};
|
|
|
|
if (wicLossless)
|
|
|
|
{
|
|
|
|
options.pstrName = const_cast<wchar_t*>(L"Lossless");
|
|
|
|
varValues.vt = VT_BOOL;
|
|
|
|
varValues.bVal = TRUE;
|
|
|
|
}
|
|
|
|
else if (wicQuality >= 0.f)
|
|
|
|
{
|
|
|
|
options.pstrName = const_cast<wchar_t*>(L"ImageQuality");
|
|
|
|
varValues.vt = VT_R4;
|
|
|
|
varValues.fltVal = wicQuality;
|
|
|
|
}
|
|
|
|
std::ignore = props->Write(1, &options, &varValues);
|
|
|
|
}
|
|
|
|
break;
|
2019-09-14 03:31:09 +03:00
|
|
|
}
|
2022-04-05 01:03:02 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (FAILED(hr))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-01 10:59:57 +03:00
|
|
|
wprintf(L" FAILED (%08X%ls)\n", static_cast<unsigned int>(hr), GetErrorDesc(hr));
|
2021-02-04 03:04:36 +03:00
|
|
|
retVal = 1;
|
2022-03-03 01:25:27 +03:00
|
|
|
if ((hr == static_cast<HRESULT>(0xc00d5212) /* MF_E_TOPO_CODEC_NOT_FOUND */) && (FileType == WIC_CODEC_HEIF))
|
|
|
|
{
|
|
|
|
wprintf(L"INFO: This format requires installing the HEIF Image Extensions - https://aka.ms/heif\n");
|
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
continue;
|
|
|
|
}
|
2016-09-13 02:39:26 +03:00
|
|
|
wprintf(L"\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 06:53:25 +03:00
|
|
|
if (sizewarn)
|
|
|
|
{
|
2021-06-05 01:06:31 +03:00
|
|
|
wprintf(L"\nWARNING: Target size exceeds maximum size for feature level (%u)\n", maxSize);
|
2019-10-11 06:53:25 +03:00
|
|
|
}
|
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (nonpow2warn && maxSize <= 4096)
|
2016-09-12 22:48:27 +03:00
|
|
|
{
|
|
|
|
// Only emit this warning if ran with -fl set to a 9.x feature level
|
2016-09-14 10:03:22 +03:00
|
|
|
wprintf(L"\nWARNING: Not all feature levels support non-power-of-2 textures with mipmaps\n");
|
2016-09-12 22:48:27 +03:00
|
|
|
}
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2016-09-13 02:39:26 +03:00
|
|
|
if (non4bc)
|
2016-09-14 10:03:22 +03:00
|
|
|
wprintf(L"\nWARNING: Direct3D requires BC image to be multiple of 4 in width & height\n");
|
2016-08-22 21:26:36 +03:00
|
|
|
|
2021-04-25 05:57:56 +03:00
|
|
|
if (dwOptions & (uint64_t(1) << OPT_TIMING))
|
2016-08-22 21:26:36 +03:00
|
|
|
{
|
2021-04-27 07:25:30 +03:00
|
|
|
LARGE_INTEGER qpcEnd = {};
|
2021-10-17 21:57:07 +03:00
|
|
|
std::ignore = QueryPerformanceCounter(&qpcEnd);
|
2021-04-27 07:25:30 +03:00
|
|
|
|
2022-03-02 01:31:55 +03:00
|
|
|
const LONGLONG delta = qpcEnd.QuadPart - qpcStart.QuadPart;
|
2021-04-27 07:25:30 +03:00
|
|
|
wprintf(L"\n Processing time: %f seconds\n", double(delta) / double(qpcFreq.QuadPart));
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|
|
|
|
|
2021-02-04 03:04:36 +03:00
|
|
|
return retVal;
|
2016-08-22 21:26:36 +03:00
|
|
|
}
|