1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-21 20:10:05 +00:00

DDSView sample cleanup and added to CMake

This commit is contained in:
Chuck Walbourn 2022-03-27 17:29:48 -07:00
parent e6ef5ff6c2
commit 9e34fadda0
3 changed files with 323 additions and 276 deletions

View File

@ -13,6 +13,8 @@ project (DirectXTex
option(BUILD_TOOLS "Build tex command-line tools" ON)
option(BUILD_SAMPLE "Build DDSView sample" ON)
# Includes the functions for Direct3D 11 resources and DirectCompute compression
option(BUILD_DX11 "Build with DirectX11 Runtime support" ON)
@ -244,6 +246,17 @@ if(BUILD_TOOLS AND WIN32 AND (NOT WINDOWS_STORE))
endif()
endif()
#--- DDSView sample
if(BUILD_SAMPLE AND WIN32 AND (NOT WINDOWS_STORE))
list(APPEND TOOL_EXES ddsview)
add_executable(ddsview WIN32
DDSView/ddsview.cpp
DDSView/ddsview.rc)
target_link_libraries(ddsview ${PROJECT_NAME} d3d11.lib)
source_group(ddsview REGULAR_EXPRESSION DDSView/*.*)
endif()
if(MSVC)
foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME})
target_compile_options(${t} PRIVATE /fp:fast "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>")
@ -264,7 +277,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# OpenMP is not supported for clang for Windows by default
set(WarningsEXE ${WarningsLib} "-Wno-c++98-compat" "-Wno-c++98-compat-pedantic" "-Wno-switch" "-Wno-switch-enum" "-Wno-language-extension-token" "-Wno-missing-prototypes" "-Wno-global-constructors" "-Wno-double-promotion")
set(WarningsEXE ${WarningsLib} "-Wno-c++98-compat" "-Wno-c++98-compat-pedantic" "-Wno-switch" "-Wno-switch-enum" "-Wno-covered-switch-default" "-Wno-language-extension-token" "-Wno-missing-prototypes" "-Wno-global-constructors" "-Wno-double-promotion")
foreach(t IN LISTS TOOL_EXES)
target_compile_options(${t} PRIVATE ${WarningsEXE})
endforeach()

View File

@ -1,7 +1,7 @@
//--------------------------------------------------------------------------------------
// File: DDSView.cpp
//
// DirectX 11 DDS File Viewer
// DirectX 11 DDS File Viewer sample for DirectXTex
//
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
@ -13,8 +13,12 @@
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cwchar>
#include <iterator>
#include <tuple>
#include <dxgiformat.h>
#include <d3d11_1.h>
@ -47,6 +51,8 @@ struct CBArrayControl
//--------------------------------------------------------------------------------------
namespace
{
// fxc ddsview.fx /nologo /EVS /Tvs_4_1 /Fhshaders\vs.h
#include "shaders\vs.h"
@ -67,8 +73,11 @@ struct CBArrayControl
// fxc ddsview.fx /nologo /EPS_Cube /Tps_4_1 /Fhshaders\psCube.h
#include "shaders\psCube.h"
}
//--------------------------------------------------------------------------------------
namespace
{
HINSTANCE g_hInst = nullptr;
HWND g_hWnd = nullptr;
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
@ -94,6 +103,8 @@ UINT g_iMaxIndex = 1;
UINT g_iIndices = 0;
LPCWSTR g_szAppName = L"DDSView";
}
//--------------------------------------------------------------------------------------
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata);
@ -104,14 +115,15 @@ void Render();
//--------------------------------------------------------------------------------------
#pragma warning( suppress : 6262 )
int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow )
int WINAPI wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE /*hPrevInstance*/,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER( hPrevInstance );
UNREFERENCED_PARAMETER( lpCmdLine );
if (!*lpCmdLine)
{
MessageBoxW( nullptr, L"Usage: ddsview <filename>", L"DDSView", MB_OK | MB_ICONEXCLAMATION );
MessageBoxW(nullptr, L"Usage: ddsview <filename>", g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
@ -121,7 +133,7 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"Failed to open texture file\n\nFilename = %ls\nHRESULT %08X", lpCmdLine, static_cast<unsigned int>(hr));
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
@ -142,19 +154,42 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"Arrays of volume textures are not supported\n\nFilename = %ls\nArray size %zu", lpCmdLine, mdata.arraySize);
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
g_iMaxIndex = static_cast<UINT>(mdata.depth);
}
else
else if (mdata.arraySize > 1)
{
if (g_featureLevel < D3D_FEATURE_LEVEL_10_0)
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"Texture arrays require DirectX 10 hardware or later\n\nFilename = %ls\nArray size %zu", lpCmdLine, mdata.arraySize);
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
g_iMaxIndex = static_cast<UINT>(mdata.arraySize);
}
switch (mdata.format)
{
case DXGI_FORMAT_BC4_TYPELESS:
case DXGI_FORMAT_BC4_SNORM:
case DXGI_FORMAT_BC4_UNORM:
case DXGI_FORMAT_BC5_TYPELESS:
case DXGI_FORMAT_BC5_SNORM:
case DXGI_FORMAT_BC5_UNORM:
if (g_featureLevel < D3D_FEATURE_LEVEL_10_0)
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"BC4/BC5 requires DirectX 10 hardware or later\n\nFilename = %ls\nDXGI Format %d\nFeature Level %d", lpCmdLine, mdata.format, g_featureLevel);
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
break;
case DXGI_FORMAT_BC6H_TYPELESS:
case DXGI_FORMAT_BC6H_UF16:
case DXGI_FORMAT_BC6H_SF16:
@ -164,8 +199,8 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
if (g_featureLevel < D3D_FEATURE_LEVEL_11_0)
{
wchar_t buff[2048] = {};
swprintf_s( buff, L"BC6H/BC7 requires DirectX 11 hardware\n\nFilename = %ls\nDXGI Format %d\nFeature Level %d", lpCmdLine, mdata.format, g_featureLevel );
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
swprintf_s(buff, L"BC6H/BC7 requires DirectX 11 hardware or later\n\nFilename = %ls\nDXGI Format %d\nFeature Level %d", lpCmdLine, mdata.format, g_featureLevel);
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
break;
@ -174,11 +209,12 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
UINT flags = 0;
hr = g_pd3dDevice->CheckFormatSupport(mdata.format, &flags);
if ( FAILED(hr) || !(flags & (D3D11_FORMAT_SUPPORT_TEXTURE1D|D3D11_FORMAT_SUPPORT_TEXTURE2D|D3D11_FORMAT_SUPPORT_TEXTURE3D)) )
constexpr UINT required = D3D11_FORMAT_SUPPORT_TEXTURE1D | D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURE3D;
if (FAILED(hr) || !(flags & required))
{
wchar_t buff[2048] = {};
swprintf_s( buff, L"Format not supported by DirectX hardware\n\nFilename = %ls\nDXGI Format %d\nFeature Level %d\nHRESULT = %08X", lpCmdLine, mdata.format, g_featureLevel, static_cast<unsigned int>(hr) );
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
swprintf_s(buff, L"Format not supported by this DirectX hardware\n\nFilename = %ls\nDXGI Format %d\nFeature Level %d\nHRESULT = %08X", lpCmdLine, mdata.format, g_featureLevel, static_cast<unsigned int>(hr));
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
}
@ -191,7 +227,7 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"Failed to load texture file\n\nFilename = %ls\nHRESULT %08X", lpCmdLine, static_cast<unsigned int>(hr));
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
@ -203,7 +239,7 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
{
wchar_t buff[2048] = {};
swprintf_s(buff, L"Failed creating texture from file\n\nFilename = %ls\nHRESULT = %08X", lpCmdLine, static_cast<unsigned int>(hr));
MessageBoxW( nullptr, buff, L"DDSView", MB_OK | MB_ICONEXCLAMATION );
MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0;
}
@ -224,26 +260,26 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
CleanupDevice();
return ( int )msg.wParam;
return static_cast<int>(msg.wParam);
}
//--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata )
HRESULT InitWindow(
HINSTANCE hInstance,
int nCmdShow,
const TexMetadata& mdata)
{
// Register class
WNDCLASSEXW wcex;
WNDCLASSEXW wcex = {};
wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon( hInstance, ( LPCTSTR )IDI_MAIN_ICON );
wcex.hIcon = LoadIconW(hInstance, reinterpret_cast<LPCWSTR>(IDI_MAIN_ICON));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
wcex.lpszMenuName = nullptr;
wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcex.lpszClassName = L"DDSViewWindowClass";
wcex.hIconSm = LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_MAIN_ICON );
wcex.hIconSm = LoadIconW(wcex.hInstance, reinterpret_cast<LPCWSTR>(IDI_MAIN_ICON));
if (!RegisterClassExW(&wcex))
return E_FAIL;
@ -254,21 +290,21 @@ HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata
int cxborder = GetSystemMetrics(SM_CXBORDER);
int cxedge = GetSystemMetrics(SM_CXEDGE);
int screenX = GetSystemMetrics(SM_CXSCREEN) - std::max(cxborder, cxedge);
if( rc.right < (LONG)mdata.width )
rc.right = (LONG)mdata.width;
if (rc.right < static_cast<LONG>(mdata.width))
rc.right = static_cast<LONG>(mdata.width);
if (rc.right > screenX)
rc.right = screenX;
int cyborder = GetSystemMetrics(SM_CYBORDER);
int cyedge = GetSystemMetrics(SM_CYEDGE);
int screenY = GetSystemMetrics(SM_CYSCREEN) - std::max(cyborder, cyedge);
if ( rc.bottom < (LONG)mdata.height )
rc.bottom = (LONG)mdata.height;
if (rc.bottom < static_cast<LONG>(mdata.height))
rc.bottom = static_cast<LONG>(mdata.height);
if (rc.bottom > screenY)
rc.bottom = screenY;
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindowW( L"DDSViewWindowClass", L"DDS View", WS_OVERLAPPEDWINDOW,
g_hWnd = CreateWindowW(L"DDSViewWindowClass", g_szAppName, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr);
if (!g_hWnd)
@ -283,14 +319,14 @@ HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint( hWnd, &ps );
{
PAINTSTRUCT ps;
std::ignore = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
@ -312,7 +348,7 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
}
else if (wParam >= '0' && wParam <= '9')
{
UINT index = (wParam == '0') ? 10 : ((UINT) (wParam - '1'));
UINT index = (wParam == '0') ? 10u : static_cast<UINT>(wParam - '1');
if (index < g_iMaxIndex)
g_iCurrentIndex = index;
}
@ -334,8 +370,8 @@ HRESULT InitDevice( const TexMetadata& mdata )
RECT rc;
GetClientRect(g_hWnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
auto width = static_cast<UINT>(rc.right - rc.left);
auto height = static_cast<UINT>(rc.bottom - rc.top);
UINT createDeviceFlags = 0;
#if defined( DEBUG ) || defined( _DEBUG )
@ -368,14 +404,17 @@ HRESULT InitDevice( const TexMetadata& mdata )
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
g_driverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDeviceAndSwapChain( nullptr, g_driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext );
// See https://walbourn.github.io/anatomy-of-direct3d-11-create-device/
hr = D3D11CreateDeviceAndSwapChain(nullptr, g_driverType, nullptr,
createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &sd,
&g_pSwapChain, &g_pd3dDevice, &g_featureLevel, &g_pImmediateContext);
if (SUCCEEDED(hr))
break;
}
@ -404,11 +443,8 @@ HRESULT InitDevice( const TexMetadata& mdata )
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D(&descDepth, nullptr, &g_pDepthStencil);
if (FAILED(hr))
return hr;
@ -425,13 +461,12 @@ HRESULT InitDevice( const TexMetadata& mdata )
g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);
// Setup the viewport
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)width;
vp.Height = (FLOAT)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
D3D11_VIEWPORT vp = {};
vp.TopLeftX = vp.TopLeftY = 0.f;
vp.Width = static_cast<float>(width);
vp.Height = static_cast<float>(height);
vp.MinDepth = D3D11_MIN_DEPTH;
vp.MaxDepth = D3D11_MAX_DEPTH;
g_pImmediateContext->RSSetViewports(1, &vp);
// Create the vertex shader
@ -709,8 +744,8 @@ void Render()
float bf[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
g_pImmediateContext->OMSetBlendState(g_AlphaBlendState, bf, 0xffffffff);
CBArrayControl cb;
cb.Index = (float)g_iCurrentIndex;
CBArrayControl cb = {};
cb.Index = static_cast<float>(g_iCurrentIndex);
g_pImmediateContext->UpdateSubresource(g_pCBArrayControl, 0, nullptr, &cb, 0, 0);
g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
@ -745,4 +780,3 @@ void CleanupDevice()
if (g_pImmediateContext) g_pImmediateContext->Release();
if (g_pd3dDevice) g_pd3dDevice->Release();
}