Add texture loading

Add uv mapping
This commit is contained in:
2026-11-20 11:06:43 +01:00
parent b84f9c1052
commit 491367dfa8
10 changed files with 772 additions and 12 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ using namespace DirectX;
UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height) UINT Camera::init(IDirect3DDevice9* pD3DDevice, UINT width, UINT height)
{ {
// view matrix // view matrix
XMVECTOR position = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f); XMVECTOR position = XMVectorSet(0.0f, 0.0f, -1.0f, 0.0f);
XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f); XMVECTOR forward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
+2
View File
@@ -184,6 +184,7 @@
<ClCompile Include="Mesh.cpp" /> <ClCompile Include="Mesh.cpp" />
<ClCompile Include="Time.cpp" /> <ClCompile Include="Time.cpp" />
<ClCompile Include="Utilities.cpp" /> <ClCompile Include="Utilities.cpp" />
<ClCompile Include="WICTextureLoader9.cpp" />
<ClCompile Include="Window.cpp" /> <ClCompile Include="Window.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -195,6 +196,7 @@
<ClInclude Include="Time.h" /> <ClInclude Include="Time.h" />
<ClInclude Include="Utilities.h" /> <ClInclude Include="Utilities.h" />
<ClInclude Include="Vertex.h" /> <ClInclude Include="Vertex.h" />
<ClInclude Include="WICTextureLoader9.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+6
View File
@@ -42,6 +42,9 @@
<ClCompile Include="Window.cpp"> <ClCompile Include="Window.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="WICTextureLoader9.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Camera.h"> <ClInclude Include="Camera.h">
@@ -71,5 +74,8 @@
<ClInclude Include="Vertex.h"> <ClInclude Include="Vertex.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="WICTextureLoader9.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
+15
View File
@@ -1,13 +1,28 @@
#include "Material.h" #include "Material.h"
#include "Utilities.h" #include "Utilities.h"
#include "WICTextureLoader9.h"
UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCTSTR textureFileName) UINT Material::init(IDirect3DDevice9* pD3DDevice, LPCTSTR textureFileName)
{ {
HRESULT hr = DirectX::CreateWICTextureFromFile(pD3DDevice, textureFileName, &_pTexture, 0, DirectX::WIC_LOADER_MIP_AUTOGEN);
CheckFailed(hr, 600);
return 0; return 0;
} }
void Material::render(IDirect3DDevice9* pD3DDevice) void Material::render(IDirect3DDevice9* pD3DDevice)
{ {
// set texture
pD3DDevice->SetTexture(0, _pTexture);
// set sampler state
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
pD3DDevice->SetSamplerState(0, D3DSAMP_BORDERCOLOR, D3DCOLOR_XRGB(200, 0, 200));
pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
pD3DDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 0);
} }
void Material::deInit() void Material::deInit()
+1 -1
View File
@@ -9,6 +9,6 @@ public:
void deInit(); void deInit();
private: private:
IDirect3DTexture9* _texture = {}; IDirect3DTexture9* _pTexture = {};
}; };
+12 -6
View File
@@ -39,8 +39,8 @@ void Mesh::update(FLOAT dt)
if (GetAsyncKeyState(VK_RIGHT) & 0x8000) x += move; if (GetAsyncKeyState(VK_RIGHT) & 0x8000) x += move;
if (GetAsyncKeyState(VK_DOWN) & 0x8000) y -= move; if (GetAsyncKeyState(VK_DOWN) & 0x8000) y -= move;
if (GetAsyncKeyState(VK_UP) & 0x8000) y += move; if (GetAsyncKeyState(VK_UP) & 0x8000) y += move;
if (GetAsyncKeyState(VK_SUBTRACT) & 0x8000) z -= move; if ((GetAsyncKeyState(VK_SUBTRACT) & 0x8000) || (GetAsyncKeyState('Q') & 0x8000)) z -= move;
if (GetAsyncKeyState(VK_ADD) & 0x8000) z += move; if ((GetAsyncKeyState(VK_ADD) & 0x8000) || (GetAsyncKeyState('E') & 0x8000)) z += move;
XMMATRIX translation = XMMatrixTranslation(x, y, z); XMMATRIX translation = XMMatrixTranslation(x, y, z);
XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ); XMMATRIX rotation = XMMatrixRotationRollPitchYaw(0.0f, 0.0f, rotZ);
@@ -114,10 +114,16 @@ UINT Mesh::createVertexBuffer(IDirect3DDevice9* pD3DDevice)
//pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f); //pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f);
// quad with colors // quad with colors
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0); //pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 255, 0, 0);
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0); //pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 0, 255, 0);
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0, 0, 255); //pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 0, 0, 255);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0); //pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 255, 255, 0);
// quad with uvs
pVertices[0] = Vertex(-0.5f, 0.5f, 0.0f, 0.0f, 0.0f);
pVertices[1] = Vertex(0.5f, 0.5f, 0.0f, 1.0f, 0.0f);
pVertices[2] = Vertex(0.5f, -0.5f, 0.0f, 1.0f, 1.0f);
pVertices[3] = Vertex(-0.5f, -0.5f, 0.0f, 0.0f, 1.0f);
hr = _pVertexBuffer->Unlock(); hr = _pVertexBuffer->Unlock();
CheckFailed(hr, 320); CheckFailed(hr, 320);
+9 -4
View File
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <d3d9.h> #include <d3d9.h>
#define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE #define FVF D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1
struct Vertex struct Vertex
{ {
@@ -13,7 +13,12 @@ struct Vertex
// color // color
D3DCOLOR color; D3DCOLOR color;
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xffffffff) {} // texture coordinates
Vertex(float x, float y, float z) : x(x), y(y), z(z), color(0xffffffff) {} float u;
Vertex(float x, float y, float z, UINT r, UINT g, UINT b) : x(x), y(y), z(z), color(D3DCOLOR_XRGB(r, g, b)) {} float v;
Vertex() : x(0.0f), y(0.0f), z(0.0f), color(0xffffffff), u(0.0f), v(0.0f) {}
Vertex(float x, float y, float z) : x(x), y(y), z(z), color(0xffffffff), u(0.0f), v(0.0f) {}
Vertex(float x, float y, float z, UINT r, UINT g, UINT b) : x(x), y(y), z(z), color(D3DCOLOR_XRGB(r, g, b)), u(0.0f), v(0.0f) {}
Vertex(float x, float y, float z, float u, float v) : x(x), y(y), z(z), color(0xffffffff), u(u), v(v) {}
}; };
+637
View File
@@ -0,0 +1,637 @@
//--------------------------------------------------------------------------------------
// File: WICTextureLoader9.cpp
//
// Function for loading a WIC image and creating a Direct3D runtime texture for it
//
// Note: Assumes application has already called CoInitializeEx
//
// Note these functions are useful for images created as simple 2D textures. For
// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader.
// For a full-featured DDS file reader, writer, and texture processing pipeline see
// the 'Texconv' sample and the 'DirectXTex' library.
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// https://go.microsoft.com/fwlink/?LinkId=248926
// http://go.microsoft.com/fwlink/?LinkId=248929
//--------------------------------------------------------------------------------------
// We could load multi-frame images (TIFF/GIF) into a texture array.
// For now, we just load the first frame (note: DirectXTex supports multi-frame images)
#include "WICTextureLoader9.h"
#include <d3d9types.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <cstring>
#include <iterator>
#include <memory>
#include <wincodec.h>
#include <wrl/client.h>
#ifdef _MSC_VER
// Off by default warnings
#pragma warning(disable : 4619 4616 4061 4623 4626 5027)
// C4619/4616 #pragma warning warnings
// C4061 enumerator 'x' in switch of enum 'y' is not explicitly handled by a case label
// C4623 default constructor was implicitly defined as deleted
// C4626 assignment operator was implicitly defined as deleted
// C5027 move assignment operator was implicitly defined as deleted
#endif
#ifdef __clang__
#pragma clang diagnostic ignored "-Wcovered-switch-default"
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
using namespace DirectX;
using Microsoft::WRL::ComPtr;
namespace
{
//-------------------------------------------------------------------------------------
// WIC Pixel Format Translation Data
//-------------------------------------------------------------------------------------
struct WICTranslate
{
const GUID& wic;
D3DFORMAT format;
constexpr WICTranslate(const GUID& wg, D3DFORMAT fmt) noexcept
: wic(wg),
format(fmt)
{
}
};
constexpr WICTranslate g_WICFormats[] = {
// clang-format off
{ GUID_WICPixelFormat128bppRGBAFloat, D3DFMT_A32B32G32R32F },
{ GUID_WICPixelFormat64bppRGBAHalf, D3DFMT_A16B16G16R16F },
{ GUID_WICPixelFormat64bppRGBA, D3DFMT_A16B16G16R16 },
{ GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 },
{ GUID_WICPixelFormat32bppRGBA1010102, D3DFMT_A2B10G10R10 },
{ GUID_WICPixelFormat16bppBGRA5551, D3DFMT_A1R5G5B5 },
{ GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
{ GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
{ GUID_WICPixelFormat32bppGrayFloat, D3DFMT_R32F },
{ GUID_WICPixelFormat16bppGrayHalf, D3DFMT_R16F },
{ GUID_WICPixelFormat16bppGray, D3DFMT_L16 },
{ GUID_WICPixelFormat8bppGray, D3DFMT_L8 },
{ GUID_WICPixelFormat8bppAlpha, D3DFMT_A8 },
// clang-format on
};
//-------------------------------------------------------------------------------------
// WIC Pixel Format nearest conversion table
//-------------------------------------------------------------------------------------
struct WICConvert
{
const GUID& source;
const GUID& target;
constexpr WICConvert(const GUID& src, const GUID& tgt) noexcept
: source(src),
target(tgt)
{
}
};
constexpr WICConvert g_WICConvert[] = {
// clang-format off
// Note target GUID in this conversion table must be one of those directly supported formats (above).
{ GUID_WICPixelFormatBlackWhite, GUID_WICPixelFormat8bppGray }, // D3DFMT_L8
{ GUID_WICPixelFormat1bppIndexed, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat2bppIndexed, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat4bppIndexed, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat8bppIndexed, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat2bppGray, GUID_WICPixelFormat8bppGray }, // D3DFMT_L8
{ GUID_WICPixelFormat4bppGray, GUID_WICPixelFormat8bppGray }, // D3DFMT_L8
{ GUID_WICPixelFormat16bppGrayFixedPoint, GUID_WICPixelFormat16bppGrayHalf }, // D3DFMT_R16F
{ GUID_WICPixelFormat32bppGrayFixedPoint, GUID_WICPixelFormat32bppGrayFloat }, // D3DFMT_R32F
{ GUID_WICPixelFormat32bppBGR101010, GUID_WICPixelFormat32bppRGBA1010102 }, // D3DFMT_A2B10G10R10
{ GUID_WICPixelFormat24bppBGR, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat24bppRGB, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat32bppBGR, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat32bppRGBA, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat32bppPBGRA, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat32bppPRGBA, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat48bppRGB, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat48bppBGR, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat64bppBGRA, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat64bppPRGBA, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat64bppPBGRA, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat48bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat48bppBGRFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat64bppRGBAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat64bppBGRAFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat64bppRGBFixedPoint, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat64bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat48bppRGBHalf, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat128bppPRGBAFloat, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
{ GUID_WICPixelFormat128bppRGBFloat, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
{ GUID_WICPixelFormat128bppRGBAFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
{ GUID_WICPixelFormat128bppRGBFixedPoint, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
{ GUID_WICPixelFormat32bppRGBE, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
{ GUID_WICPixelFormat32bppCMYK, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat64bppCMYK, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat40bppCMYKAlpha, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat80bppCMYKAlpha, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat32bppRGB, GUID_WICPixelFormat32bppBGRA }, // D3DFMT_A8R8G8B8
{ GUID_WICPixelFormat64bppRGB, GUID_WICPixelFormat64bppRGBA }, // D3DFMT_A16B16G16R16
{ GUID_WICPixelFormat64bppPRGBAHalf, GUID_WICPixelFormat64bppRGBAHalf }, // D3DFMT_A16B16G16R16F
{ GUID_WICPixelFormat96bppRGBFloat, GUID_WICPixelFormat128bppRGBAFloat }, // D3DFMT_A32B32G32R32F
// We don't support n-channel formats
// clang-format on
};
//--------------------------------------------------------------------------------------
BOOL WINAPI InitializeWICFactory(PINIT_ONCE, PVOID, PVOID* ifactory) noexcept
{
HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory2, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory2), ifactory);
if (SUCCEEDED(hr))
{
// WIC2 is available on Windows 10, Windows 8.x, and Windows 7 SP1 with KB 2670838 installed
return TRUE;
}
else
{
hr = CoCreateInstance(CLSID_WICImagingFactory1, nullptr, CLSCTX_INPROC_SERVER, __uuidof(IWICImagingFactory), ifactory);
return SUCCEEDED(hr) ? TRUE : FALSE;
}
}
IWICImagingFactory* GetWIC() noexcept
{
static INIT_ONCE s_initOnce = INIT_ONCE_STATIC_INIT;
IWICImagingFactory* factory = nullptr;
if (!InitOnceExecuteOnce(&s_initOnce, InitializeWICFactory, nullptr, reinterpret_cast<LPVOID*>(&factory)))
{
return nullptr;
}
return factory;
}
//---------------------------------------------------------------------------------
D3DFORMAT WICToD3D9(const GUID& guid) noexcept
{
for (size_t i = 0; i < std::size(g_WICFormats); ++i)
{
if (memcmp(&g_WICFormats[i].wic, &guid, sizeof(GUID)) == 0)
return g_WICFormats[i].format;
}
return D3DFMT_UNKNOWN;
}
//---------------------------------------------------------------------------------
void FitPowerOf2(UINT origx, UINT origy, UINT& targetx, UINT& targety, size_t maxsize)
{
const 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)
{
const 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)
{
const float score = fabsf((float(x) / float(y)) - origAR);
if (score < bestScore)
{
bestScore = score;
targetx = UINT(x);
}
}
}
}
//---------------------------------------------------------------------------------
HRESULT CreateTextureFromWIC(_In_ LPDIRECT3DDEVICE9 device,
_In_ IWICBitmapFrameDecode* frame,
_In_ size_t maxsize,
_In_ DWORD usage,
_In_ D3DPOOL pool,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_ LPDIRECT3DTEXTURE9* texture) noexcept
{
UINT width, height;
HRESULT hr = frame->GetSize(&width, &height);
if (FAILED(hr))
return hr;
if (maxsize > UINT32_MAX)
return E_INVALIDARG;
assert(width > 0 && height > 0);
if (!maxsize)
{
maxsize = 4096u /*D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION*/;
}
assert(maxsize > 0);
UINT twidth = width;
UINT theight = height;
if (loadFlags & WIC_LOADER_FIT_POW2)
{
FitPowerOf2(width, height, twidth, theight, maxsize);
}
else if (width > maxsize || height > maxsize)
{
const float ar = static_cast<float>(height) / static_cast<float>(width);
if (width > height)
{
twidth = static_cast<UINT>(maxsize);
theight = std::max<UINT>(1, static_cast<UINT>(static_cast<float>(maxsize) * ar));
}
else
{
theight = static_cast<UINT>(maxsize);
twidth = std::max<UINT>(1, static_cast<UINT>(static_cast<float>(maxsize) / ar));
}
assert(twidth <= maxsize && theight <= maxsize);
}
if (loadFlags & WIC_LOADER_MAKE_SQUARE)
{
twidth = std::max<UINT>(twidth, theight);
theight = twidth;
}
// Determine format
WICPixelFormatGUID pixelFormat;
hr = frame->GetPixelFormat(&pixelFormat);
if (FAILED(hr))
return hr;
WICPixelFormatGUID convertGUID;
memcpy_s(&convertGUID, sizeof(WICPixelFormatGUID), &pixelFormat, sizeof(GUID));
D3DFORMAT format = WICToD3D9(pixelFormat);
if (format == D3DFMT_UNKNOWN)
{
for (size_t i = 0; i < std::size(g_WICConvert); ++i)
{
if (memcmp(&g_WICConvert[i].source, &pixelFormat, sizeof(WICPixelFormatGUID)) == 0)
{
memcpy_s(&convertGUID, sizeof(WICPixelFormatGUID), &g_WICConvert[i].target, sizeof(GUID));
format = WICToD3D9(g_WICConvert[i].target);
assert(format != D3DFMT_UNKNOWN);
break;
}
}
if (format == D3DFMT_UNKNOWN)
return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);
}
if (loadFlags & WIC_LOADER_FORCE_RGBA32)
{
memcpy_s(&convertGUID, sizeof(WICPixelFormatGUID), &GUID_WICPixelFormat32bppBGRA, sizeof(GUID));
format = D3DFMT_A8R8G8B8;
}
// Create texture
if (loadFlags & WIC_LOADER_MIP_AUTOGEN)
usage |= D3DUSAGE_AUTOGENMIPMAP;
ComPtr<IDirect3DTexture9> pTexture;
hr = device->CreateTexture(twidth, theight, 1u, usage, format, pool, pTexture.GetAddressOf(), nullptr);
if (FAILED(hr))
return hr;
// Create staging texture memory
ComPtr<IDirect3DTexture9> pStagingTexture;
if (pool == D3DPOOL_DEFAULT)
{
hr = device->CreateTexture(twidth, theight, 1u, 0u, format, D3DPOOL_SYSTEMMEM, pStagingTexture.GetAddressOf(), nullptr);
if (FAILED(hr))
return hr;
}
else
{
pStagingTexture = pTexture;
}
D3DLOCKED_RECT LockedRect = {};
hr = pStagingTexture->LockRect(0, &LockedRect, nullptr, 0);
if (FAILED(hr))
return hr;
const uint64_t numBytes = uint64_t(LockedRect.Pitch) * uint64_t(theight);
pStagingTexture->UnlockRect(0);
if (numBytes > UINT32_MAX)
{
return HRESULT_FROM_WIN32(ERROR_ARITHMETIC_OVERFLOW);
}
// Load image data
if (memcmp(&convertGUID, &pixelFormat, sizeof(GUID)) == 0 && twidth == width && theight == height)
{
// No format conversion or resize needed
hr = pStagingTexture->LockRect(0, &LockedRect, nullptr, 0);
if (FAILED(hr))
return hr;
hr = frame->CopyPixels(nullptr,
static_cast<UINT>(LockedRect.Pitch),
static_cast<UINT>(numBytes),
static_cast<BYTE*>(LockedRect.pBits));
pStagingTexture->UnlockRect(0);
if (FAILED(hr))
return hr;
}
else if (twidth != width || theight != height)
{
// Resize
auto pWIC = GetWIC();
if (!pWIC)
return E_NOINTERFACE;
ComPtr<IWICBitmapScaler> scaler;
hr = pWIC->CreateBitmapScaler(scaler.GetAddressOf());
if (FAILED(hr))
return hr;
hr = scaler->Initialize(frame, twidth, theight, WICBitmapInterpolationModeFant);
if (FAILED(hr))
return hr;
WICPixelFormatGUID pfScaler;
hr = scaler->GetPixelFormat(&pfScaler);
if (FAILED(hr))
return hr;
if (memcmp(&convertGUID, &pfScaler, sizeof(GUID)) == 0)
{
// No format conversion needed
hr = pStagingTexture->LockRect(0, &LockedRect, nullptr, 0);
if (FAILED(hr))
return hr;
hr = scaler->CopyPixels(nullptr,
static_cast<UINT>(LockedRect.Pitch),
static_cast<UINT>(numBytes),
static_cast<BYTE*>(LockedRect.pBits));
pStagingTexture->UnlockRect(0);
if (FAILED(hr))
return hr;
}
else
{
ComPtr<IWICFormatConverter> FC;
hr = pWIC->CreateFormatConverter(FC.GetAddressOf());
if (FAILED(hr))
return hr;
BOOL canConvert = FALSE;
hr = FC->CanConvert(pfScaler, convertGUID, &canConvert);
if (FAILED(hr) || !canConvert)
{
return E_UNEXPECTED;
}
hr = FC->Initialize(scaler.Get(),
convertGUID,
WICBitmapDitherTypeErrorDiffusion,
nullptr,
0,
WICBitmapPaletteTypeMedianCut);
if (FAILED(hr))
return hr;
hr = pStagingTexture->LockRect(0, &LockedRect, nullptr, 0);
if (FAILED(hr))
return hr;
hr = FC->CopyPixels(nullptr,
static_cast<UINT>(LockedRect.Pitch),
static_cast<UINT>(numBytes),
static_cast<BYTE*>(LockedRect.pBits));
pStagingTexture->UnlockRect(0);
if (FAILED(hr))
return hr;
}
}
else
{
// Format conversion but no resize
auto pWIC = GetWIC();
if (!pWIC)
return E_NOINTERFACE;
ComPtr<IWICFormatConverter> FC;
hr = pWIC->CreateFormatConverter(FC.GetAddressOf());
if (FAILED(hr))
return hr;
BOOL canConvert = FALSE;
hr = FC->CanConvert(pixelFormat, convertGUID, &canConvert);
if (FAILED(hr) || !canConvert)
{
return E_UNEXPECTED;
}
hr = FC->Initialize(frame, convertGUID, WICBitmapDitherTypeErrorDiffusion, nullptr, 0, WICBitmapPaletteTypeMedianCut);
if (FAILED(hr))
return hr;
hr = pStagingTexture->LockRect(0, &LockedRect, nullptr, 0);
if (FAILED(hr))
return hr;
hr = FC->CopyPixels(nullptr,
static_cast<UINT>(LockedRect.Pitch),
static_cast<UINT>(numBytes),
static_cast<BYTE*>(LockedRect.pBits));
pStagingTexture->UnlockRect(0);
if (FAILED(hr))
return hr;
}
if (pool == D3DPOOL_DEFAULT)
{
hr = device->UpdateTexture(pStagingTexture.Get(), pTexture.Get());
if (FAILED(hr))
return hr;
}
*texture = pTexture.Detach();
return S_OK;
}
} // anonymous namespace
//--------------------------------------------------------------------------------------
_Use_decl_annotations_ HRESULT DirectX::CreateWICTextureFromMemory(LPDIRECT3DDEVICE9 d3dDevice,
const uint8_t* wicData,
size_t wicDataSize,
LPDIRECT3DTEXTURE9* texture,
size_t maxsize,
WIC_LOADER_FLAGS loadFlags) noexcept
{
return CreateWICTextureFromMemoryEx(d3dDevice, wicData, wicDataSize, maxsize, 0u, D3DPOOL_DEFAULT, loadFlags, texture);
}
_Use_decl_annotations_ HRESULT DirectX::CreateWICTextureFromMemoryEx(LPDIRECT3DDEVICE9 d3dDevice,
const uint8_t* wicData,
size_t wicDataSize,
_In_ size_t maxsize,
_In_ DWORD usage,
_In_ D3DPOOL pool,
_In_ WIC_LOADER_FLAGS loadFlags,
LPDIRECT3DTEXTURE9* texture) noexcept
{
if (texture)
{
*texture = nullptr;
}
if (!d3dDevice || !wicData || !wicDataSize || !texture)
return E_INVALIDARG;
if (!wicDataSize)
return E_FAIL;
if (wicDataSize > UINT32_MAX)
return HRESULT_FROM_WIN32(ERROR_FILE_TOO_LARGE);
auto pWIC = GetWIC();
if (!pWIC)
return E_NOINTERFACE;
// Create input stream for memory
ComPtr<IWICStream> stream;
HRESULT hr = pWIC->CreateStream(stream.GetAddressOf());
if (FAILED(hr))
return hr;
hr = stream->InitializeFromMemory(const_cast<uint8_t*>(wicData), static_cast<DWORD>(wicDataSize));
if (FAILED(hr))
return hr;
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
hr = pWIC->CreateDecoderFromStream(stream.Get(), nullptr, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
if (FAILED(hr))
return hr;
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame(0, frame.GetAddressOf());
if (FAILED(hr))
return hr;
return CreateTextureFromWIC(d3dDevice, frame.Get(), maxsize, usage, pool, loadFlags, texture);
}
//--------------------------------------------------------------------------------------
_Use_decl_annotations_ HRESULT DirectX::CreateWICTextureFromFile(LPDIRECT3DDEVICE9 d3dDevice,
const wchar_t* fileName,
LPDIRECT3DTEXTURE9* texture,
size_t maxsize,
WIC_LOADER_FLAGS loadFlags) noexcept
{
return CreateWICTextureFromFileEx(d3dDevice, fileName, maxsize, 0u, D3DPOOL_DEFAULT, loadFlags, texture);
}
_Use_decl_annotations_ HRESULT DirectX::CreateWICTextureFromFileEx(LPDIRECT3DDEVICE9 d3dDevice,
const wchar_t* fileName,
size_t maxsize,
DWORD usage,
D3DPOOL pool,
WIC_LOADER_FLAGS loadFlags,
LPDIRECT3DTEXTURE9* texture) noexcept
{
if (texture)
{
*texture = nullptr;
}
if (!d3dDevice || !fileName || !texture)
return E_INVALIDARG;
auto pWIC = GetWIC();
if (!pWIC)
return E_NOINTERFACE;
// Initialize WIC
ComPtr<IWICBitmapDecoder> decoder;
HRESULT hr = pWIC->CreateDecoderFromFilename(fileName, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, decoder.GetAddressOf());
if (FAILED(hr))
return hr;
ComPtr<IWICBitmapFrameDecode> frame;
hr = decoder->GetFrame(0, frame.GetAddressOf());
if (FAILED(hr))
return hr;
return CreateTextureFromWIC(d3dDevice, frame.Get(), maxsize, usage, pool, loadFlags, texture);
}
+89
View File
@@ -0,0 +1,89 @@
//--------------------------------------------------------------------------------------
// File: WICTextureLoader9.h
//
// 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
//
// Note these functions are useful for images created as simple 2D textures. For
// more complex resources, DDSTextureLoader is an excellent light-weight runtime loader.
// For a full-featured DDS file reader, writer, and texture processing pipeline see
// the 'Texconv' sample and the 'DirectXTex' library.
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//
// https://go.microsoft.com/fwlink/?LinkId=248926
//--------------------------------------------------------------------------------------
#pragma once
#ifndef DIRECT3D_VERSION
#define DIRECT3D_VERSION 0x900
#endif
#include <d3d9.h>
#pragma comment(lib, "windowscodecs.lib")
#include <cstddef>
#include <cstdint>
namespace DirectX
{
#ifndef WIC_LOADER_FLAGS_DEFINED
#define WIC_LOADER_FLAGS_DEFINED
enum WIC_LOADER_FLAGS : uint32_t
{
WIC_LOADER_DEFAULT = 0,
WIC_LOADER_MIP_AUTOGEN = 0x8,
WIC_LOADER_FIT_POW2 = 0x20,
WIC_LOADER_MAKE_SQUARE = 0x40,
WIC_LOADER_FORCE_RGBA32 = 0x80,
};
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-dynamic-exception-spec"
#pragma clang diagnostic ignored "-Wextra-semi-stmt"
#endif
DEFINE_ENUM_FLAG_OPERATORS(WIC_LOADER_FLAGS);
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif
// Standard version
HRESULT CreateWICTextureFromMemory(_In_ LPDIRECT3DDEVICE9 d3dDevice,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_Outptr_ LPDIRECT3DTEXTURE9* texture,
_In_ size_t maxsize = 0,
_In_ WIC_LOADER_FLAGS loadFlags = WIC_LOADER_DEFAULT) noexcept;
HRESULT CreateWICTextureFromFile(_In_ LPDIRECT3DDEVICE9 d3dDevice,
_In_z_ const wchar_t* fileName,
_Outptr_ LPDIRECT3DTEXTURE9* texture,
_In_ size_t maxsize = 0,
_In_ WIC_LOADER_FLAGS loadFlags = WIC_LOADER_DEFAULT) noexcept;
// Extended version
HRESULT CreateWICTextureFromMemoryEx(_In_ LPDIRECT3DDEVICE9 d3dDevice,
_In_reads_bytes_(wicDataSize) const uint8_t* wicData,
_In_ size_t wicDataSize,
_In_ size_t maxsize,
_In_ DWORD usage,
_In_ D3DPOOL pool,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_ LPDIRECT3DTEXTURE9* texture) noexcept;
HRESULT CreateWICTextureFromFileEx(_In_ LPDIRECT3DDEVICE9 d3dDevice,
_In_z_ const wchar_t* fileName,
_In_ size_t maxsize,
_In_ DWORD usage,
_In_ D3DPOOL pool,
_In_ WIC_LOADER_FLAGS loadFlags,
_Outptr_ LPDIRECT3DTEXTURE9* texture) noexcept;
} // namespace DirectX
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 213 KiB