Direct3D 9 versions of texture loaders (#176)

This commit is contained in:
Chuck Walbourn
2020-05-09 17:22:35 -07:00
committed by GitHub
parent fdd7a70c44
commit 45fb8623d2
18 changed files with 3307 additions and 114 deletions

View File

@@ -1,7 +1,7 @@
//--------------------------------------------------------------------------------------
// File: WICTextureLoader12.cpp
//
// Function for loading a WIC image and creating a Direct3D 12 runtime texture for it
// Function for loading a WIC image and creating a Direct3D runtime texture for it
// (auto-generating mipmaps if possible)
//
// Note: Assumes application has already called CoInitializeEx
@@ -166,7 +166,8 @@ namespace
static INIT_ONCE s_initOnce = INIT_ONCE_STATIC_INIT;
IWICImagingFactory2* factory = nullptr;
if (!InitOnceExecuteOnce(&s_initOnce,
if (!InitOnceExecuteOnce(
&s_initOnce,
InitializeWICFactory,
nullptr,
reinterpret_cast<LPVOID*>(&factory)))
@@ -276,6 +277,47 @@ namespace
return bpp;
}
//---------------------------------------------------------------------------------
void FitPowerOf2(UINT origx, UINT origy, UINT& targetx, UINT& targety, size_t maxsize)
{
float origAR = float(origx) / float(origy);
if (origx > origy)
{
size_t x;
for (x = maxsize; x > 1; x >>= 1) { if (x <= targetx) break; }
targetx = UINT(x);
float bestScore = FLT_MAX;
for (size_t y = maxsize; y > 0; y >>= 1)
{
float score = fabsf((float(x) / float(y)) - origAR);
if (score < bestScore)
{
bestScore = score;
targety = UINT(y);
}
}
}
else
{
size_t y;
for (y = maxsize; y > 1; y >>= 1) { if (y <= targety) break; }
targety = UINT(y);
float bestScore = FLT_MAX;
for (size_t x = maxsize; x > 0; x >>= 1)
{
float score = fabsf((float(x) / float(y)) - origAR);
if (score < bestScore)
{
bestScore = score;
targetx = UINT(x);
}
}
}
}
//---------------------------------------------------------------------------------
HRESULT CreateTextureFromWIC(_In_ ID3D12Device* d3dDevice,
_In_ IWICBitmapFrameDecode *frame,
@@ -298,11 +340,16 @@ namespace
if (!maxsize)
{
maxsize = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
maxsize = size_t(D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION);
}
UINT twidth, theight;
if (width > maxsize || height > maxsize)
UINT twidth = width;
UINT theight = height;
if (loadFlags & WIC_LOADER_FIT_POW2)
{
FitPowerOf2(width, height, twidth, theight, maxsize);
}
else if (width > maxsize || height > maxsize)
{
float ar = static_cast<float>(height) / static_cast<float>(width);
if (width > height)
@@ -317,10 +364,11 @@ namespace
}
assert(twidth <= maxsize && theight <= maxsize);
}
else
if (loadFlags & WIC_LOADER_MAKE_SQUARE)
{
twidth = width;
theight = height;
twidth = std::max<UINT>(twidth, theight);
theight = twidth;
}
// Determine format
@@ -512,7 +560,8 @@ namespace
}
// Count the number of mips
uint32_t mipCount = (loadFlags & (WIC_LOADER_MIP_AUTOGEN|WIC_LOADER_MIP_RESERVE)) ? CountMips(twidth, theight) : 1;
uint32_t mipCount = (loadFlags & (WIC_LOADER_MIP_AUTOGEN | WIC_LOADER_MIP_RESERVE))
? CountMips(twidth, theight) : 1u;
// Create texture
D3D12_RESOURCE_DESC desc = {};
@@ -559,7 +608,7 @@ namespace
_In_ ID3D12Resource** texture) noexcept
{
#if !defined(NO_D3D12_DEBUG_NAME) && ( defined(_DEBUG) || defined(PROFILE) )
if (texture)
if (texture && *texture)
{
const wchar_t* pstrName = wcsrchr(fileName, '\\');
if (!pstrName)
@@ -571,10 +620,7 @@ namespace
pstrName++;
}
if (texture && *texture)
{
(*texture)->SetName(pstrName);
}
(*texture)->SetName(pstrName);
}
#else
UNREFERENCED_PARAMETER(fileName);
@@ -722,7 +768,11 @@ HRESULT DirectX::LoadWICTextureFromFileEx(
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName,
nullptr,
GENERIC_READ,
WICDecodeMetadataCacheOnDemand,
decoder.GetAddressOf());
if (FAILED(hr))
return hr;