1
0
mirror of https://github.com/microsoft/DirectXTex synced 2024-11-22 04:20:07 +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_TOOLS "Build tex command-line tools" ON)
option(BUILD_SAMPLE "Build DDSView sample" ON)
# Includes the functions for Direct3D 11 resources and DirectCompute compression # Includes the functions for Direct3D 11 resources and DirectCompute compression
option(BUILD_DX11 "Build with DirectX11 Runtime support" ON) option(BUILD_DX11 "Build with DirectX11 Runtime support" ON)
@ -244,6 +246,17 @@ if(BUILD_TOOLS AND WIN32 AND (NOT WINDOWS_STORE))
endif() endif()
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) if(MSVC)
foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME}) foreach(t IN LISTS TOOL_EXES ITEMS ${PROJECT_NAME})
target_compile_options(${t} PRIVATE /fp:fast "$<$<NOT:$<CONFIG:DEBUG>>:/guard:cf>") 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 # 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) foreach(t IN LISTS TOOL_EXES)
target_compile_options(${t} PRIVATE ${WarningsEXE}) target_compile_options(${t} PRIVATE ${WarningsEXE})
endforeach() endforeach()

View File

@ -1,7 +1,7 @@
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// File: DDSView.cpp // File: DDSView.cpp
// //
// DirectX 11 DDS File Viewer // DirectX 11 DDS File Viewer sample for DirectXTex
// //
// Copyright (c) Microsoft Corporation. // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License. // Licensed under the MIT License.
@ -13,8 +13,12 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio> #include <cstdio>
#include <cwchar> #include <cwchar>
#include <iterator>
#include <tuple>
#include <dxgiformat.h> #include <dxgiformat.h>
#include <d3d11_1.h> #include <d3d11_1.h>
@ -47,6 +51,8 @@ struct CBArrayControl
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
namespace
{
// fxc ddsview.fx /nologo /EVS /Tvs_4_1 /Fhshaders\vs.h // fxc ddsview.fx /nologo /EVS /Tvs_4_1 /Fhshaders\vs.h
#include "shaders\vs.h" #include "shaders\vs.h"
@ -67,70 +73,76 @@ struct CBArrayControl
// fxc ddsview.fx /nologo /EPS_Cube /Tps_4_1 /Fhshaders\psCube.h // fxc ddsview.fx /nologo /EPS_Cube /Tps_4_1 /Fhshaders\psCube.h
#include "shaders\psCube.h" #include "shaders\psCube.h"
}
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
HINSTANCE g_hInst = nullptr; namespace
HWND g_hWnd = nullptr; {
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL; HINSTANCE g_hInst = nullptr;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0; HWND g_hWnd = nullptr;
ID3D11Device* g_pd3dDevice = nullptr; D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
ID3D11DeviceContext* g_pImmediateContext = nullptr; D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
IDXGISwapChain* g_pSwapChain = nullptr; ID3D11Device* g_pd3dDevice = nullptr;
ID3D11RenderTargetView* g_pRenderTargetView = nullptr; ID3D11DeviceContext* g_pImmediateContext = nullptr;
ID3D11Texture2D* g_pDepthStencil = nullptr; IDXGISwapChain* g_pSwapChain = nullptr;
ID3D11DepthStencilView* g_pDepthStencilView = nullptr; ID3D11RenderTargetView* g_pRenderTargetView = nullptr;
ID3D11VertexShader* g_pVertexShader = nullptr; ID3D11Texture2D* g_pDepthStencil = nullptr;
ID3D11PixelShader* g_pPixelShader = nullptr; ID3D11DepthStencilView* g_pDepthStencilView = nullptr;
ID3D11InputLayout* g_pVertexLayout = nullptr; ID3D11VertexShader* g_pVertexShader = nullptr;
ID3D11Buffer* g_pVertexBuffer = nullptr; ID3D11PixelShader* g_pPixelShader = nullptr;
ID3D11Buffer* g_pIndexBuffer = nullptr; ID3D11InputLayout* g_pVertexLayout = nullptr;
ID3D11Buffer* g_pCBArrayControl = nullptr; ID3D11Buffer* g_pVertexBuffer = nullptr;
ID3D11ShaderResourceView* g_pSRV = nullptr; ID3D11Buffer* g_pIndexBuffer = nullptr;
ID3D11BlendState* g_AlphaBlendState = nullptr; ID3D11Buffer* g_pCBArrayControl = nullptr;
ID3D11SamplerState* g_pSamplerLinear = nullptr; ID3D11ShaderResourceView* g_pSRV = nullptr;
ID3D11BlendState* g_AlphaBlendState = nullptr;
ID3D11SamplerState* g_pSamplerLinear = nullptr;
UINT g_iCurrentIndex = 0; UINT g_iCurrentIndex = 0;
UINT g_iMaxIndex = 1; UINT g_iMaxIndex = 1;
UINT g_iIndices = 0; UINT g_iIndices = 0;
LPCWSTR g_szAppName = L"DDSView";
}
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata ); HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow, const TexMetadata& mdata);
HRESULT InitDevice( const TexMetadata& mdata ); HRESULT InitDevice(const TexMetadata& mdata);
void CleanupDevice(); void CleanupDevice();
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void Render(); void Render();
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
#pragma warning( suppress : 6262 ) #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 ); if (!*lpCmdLine)
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; return 0;
} }
TexMetadata mdata; TexMetadata mdata;
HRESULT hr = GetMetadataFromDDSFile( lpCmdLine, DDS_FLAGS_NONE, mdata ); HRESULT hr = GetMetadataFromDDSFile(lpCmdLine, DDS_FLAGS_NONE, mdata);
if ( FAILED(hr) ) if (FAILED(hr))
{ {
wchar_t buff[2048] = {}; wchar_t buff[2048] = {};
swprintf_s( buff, L"Failed to open texture file\n\nFilename = %ls\nHRESULT %08X", lpCmdLine, static_cast<unsigned int>(hr) ); 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; return 0;
} }
if( FAILED( InitWindow( hInstance, nCmdShow, mdata ) ) ) if (FAILED(InitWindow(hInstance, nCmdShow, mdata)))
return 0; return 0;
SetWindowTextW( g_hWnd, lpCmdLine ); SetWindowTextW(g_hWnd, lpCmdLine);
if( FAILED( InitDevice( mdata ) ) ) if (FAILED(InitDevice(mdata)))
{ {
CleanupDevice(); CleanupDevice();
return 0; return 0;
@ -138,34 +150,57 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
if (mdata.dimension == TEX_DIMENSION_TEXTURE3D) if (mdata.dimension == TEX_DIMENSION_TEXTURE3D)
{ {
if ( mdata.arraySize > 1 ) if (mdata.arraySize > 1)
{ {
wchar_t buff[2048] = {}; wchar_t buff[2048] = {};
swprintf_s( buff, L"Arrays of volume textures are not supported\n\nFilename = %ls\nArray size %zu", lpCmdLine, mdata.arraySize ); 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; return 0;
} }
g_iMaxIndex = static_cast<UINT>( mdata.depth ); g_iMaxIndex = static_cast<UINT>(mdata.depth);
} }
else else if (mdata.arraySize > 1)
{ {
g_iMaxIndex = static_cast<UINT>( mdata.arraySize ); 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 ) 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_TYPELESS:
case DXGI_FORMAT_BC6H_UF16: case DXGI_FORMAT_BC6H_UF16:
case DXGI_FORMAT_BC6H_SF16: case DXGI_FORMAT_BC6H_SF16:
case DXGI_FORMAT_BC7_TYPELESS: case DXGI_FORMAT_BC7_TYPELESS:
case DXGI_FORMAT_BC7_UNORM: case DXGI_FORMAT_BC7_UNORM:
case DXGI_FORMAT_BC7_UNORM_SRGB: case DXGI_FORMAT_BC7_UNORM_SRGB:
if ( g_featureLevel < D3D_FEATURE_LEVEL_11_0 ) if (g_featureLevel < D3D_FEATURE_LEVEL_11_0)
{ {
wchar_t buff[2048] = {}; 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 ); 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, L"DDSView", MB_OK | MB_ICONEXCLAMATION ); MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0; return 0;
} }
break; break;
@ -173,12 +208,13 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
default: default:
{ {
UINT flags = 0; UINT flags = 0;
hr = g_pd3dDevice->CheckFormatSupport ( mdata.format, &flags ); 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] = {}; 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) ); 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, L"DDSView", MB_OK | MB_ICONEXCLAMATION ); MessageBoxW(nullptr, buff, g_szAppName, MB_OK | MB_ICONEXCLAMATION);
return 0; return 0;
} }
} }
@ -186,35 +222,35 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
} }
ScratchImage image; ScratchImage image;
hr = LoadFromDDSFile( lpCmdLine, DDS_FLAGS_NONE, &mdata, image ); hr = LoadFromDDSFile(lpCmdLine, DDS_FLAGS_NONE, &mdata, image);
if ( FAILED(hr) ) if (FAILED(hr))
{ {
wchar_t buff[2048] = {}; wchar_t buff[2048] = {};
swprintf_s( buff, L"Failed to load texture file\n\nFilename = %ls\nHRESULT %08X", lpCmdLine, static_cast<unsigned int>(hr) ); 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; return 0;
} }
// Special case to make sure Texture cubes remain arrays // Special case to make sure Texture cubes remain arrays
mdata.miscFlags &= ~TEX_MISC_TEXTURECUBE; mdata.miscFlags &= ~TEX_MISC_TEXTURECUBE;
hr = CreateShaderResourceView( g_pd3dDevice, image.GetImages(), image.GetImageCount(), mdata, &g_pSRV ); hr = CreateShaderResourceView(g_pd3dDevice, image.GetImages(), image.GetImageCount(), mdata, &g_pSRV);
if ( FAILED(hr) ) if (FAILED(hr))
{ {
wchar_t buff[2048] = {}; wchar_t buff[2048] = {};
swprintf_s( buff, L"Failed creating texture from file\n\nFilename = %ls\nHRESULT = %08X", lpCmdLine, static_cast<unsigned int>(hr) ); 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; return 0;
} }
// Main message loop // Main message loop
MSG msg = {}; MSG msg = {};
while( WM_QUIT != msg.message ) while (WM_QUIT != msg.message)
{ {
if( PeekMessage( &msg, nullptr, 0, 0, PM_REMOVE ) ) if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{ {
TranslateMessage( &msg ); TranslateMessage(&msg);
DispatchMessage( &msg ); DispatchMessage(&msg);
} }
else else
{ {
@ -224,103 +260,103 @@ int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
CleanupDevice(); 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 // Register class
WNDCLASSEXW wcex; WNDCLASSEXW wcex = {};
wcex.cbSize = sizeof( WNDCLASSEXW ); wcex.cbSize = sizeof(WNDCLASSEXW);
wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc; wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance; 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.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 ); wcex.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"DDSViewWindowClass"; 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 ) ) if (!RegisterClassExW(&wcex))
return E_FAIL; return E_FAIL;
// Create window // Create window
g_hInst = hInstance; g_hInst = hInstance;
RECT rc = { 0, 0, 640, 480 }; RECT rc = { 0, 0, 640, 480 };
int cxborder = GetSystemMetrics( SM_CXBORDER ); int cxborder = GetSystemMetrics(SM_CXBORDER);
int cxedge = GetSystemMetrics( SM_CXEDGE ); int cxedge = GetSystemMetrics(SM_CXEDGE);
int screenX = GetSystemMetrics( SM_CXSCREEN ) - std::max( cxborder, cxedge ); int screenX = GetSystemMetrics(SM_CXSCREEN) - std::max(cxborder, cxedge);
if( rc.right < (LONG)mdata.width ) if (rc.right < static_cast<LONG>(mdata.width))
rc.right = (LONG)mdata.width; rc.right = static_cast<LONG>(mdata.width);
if ( rc.right > screenX ) if (rc.right > screenX)
rc.right = screenX; rc.right = screenX;
int cyborder = GetSystemMetrics( SM_CYBORDER ); int cyborder = GetSystemMetrics(SM_CYBORDER);
int cyedge = GetSystemMetrics( SM_CYEDGE ); int cyedge = GetSystemMetrics(SM_CYEDGE);
int screenY = GetSystemMetrics( SM_CYSCREEN ) - std::max( cyborder, cyedge ); int screenY = GetSystemMetrics(SM_CYSCREEN) - std::max(cyborder, cyedge);
if ( rc.bottom < (LONG)mdata.height ) if (rc.bottom < static_cast<LONG>(mdata.height))
rc.bottom = (LONG)mdata.height; rc.bottom = static_cast<LONG>(mdata.height);
if ( rc.bottom > screenY ) if (rc.bottom > screenY)
rc.bottom = screenY; rc.bottom = screenY;
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE ); 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, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr ); nullptr);
if( !g_hWnd ) if (!g_hWnd)
return E_FAIL; return E_FAIL;
ShowWindow( g_hWnd, nCmdShow ); ShowWindow(g_hWnd, nCmdShow);
return S_OK; return S_OK;
} }
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{ {
PAINTSTRUCT ps; switch (message)
HDC hdc;
switch( message )
{ {
case WM_PAINT: case WM_PAINT:
hdc = BeginPaint( hWnd, &ps ); {
EndPaint( hWnd, &ps ); PAINTSTRUCT ps;
break; std::ignore = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage( 0 ); PostQuitMessage(0);
break; break;
case WM_KEYDOWN: case WM_KEYDOWN:
if ( wParam == VK_RIGHT ) if (wParam == VK_RIGHT)
{
if (g_iCurrentIndex < g_iMaxIndex - 1)
++g_iCurrentIndex;
}
else if (wParam == VK_LEFT)
{
if (g_iCurrentIndex > 0)
{ {
if ( g_iCurrentIndex < g_iMaxIndex-1 ) --g_iCurrentIndex;
++g_iCurrentIndex;
} }
else if ( wParam == VK_LEFT ) }
{ else if (wParam >= '0' && wParam <= '9')
if ( g_iCurrentIndex > 0 ) {
{ UINT index = (wParam == '0') ? 10u : static_cast<UINT>(wParam - '1');
--g_iCurrentIndex; if (index < g_iMaxIndex)
} g_iCurrentIndex = index;
} }
else if ( wParam >= '0' && wParam <= '9' ) InvalidateRect(hWnd, nullptr, FALSE);
{ break;
UINT index = (wParam == '0') ? 10 : ((UINT) (wParam - '1'));
if ( index < g_iMaxIndex )
g_iCurrentIndex = index;
}
InvalidateRect( hWnd, nullptr, FALSE );
break;
default: default:
return DefWindowProc( hWnd, message, wParam, lParam ); return DefWindowProc(hWnd, message, wParam, lParam);
} }
return 0; return 0;
@ -328,14 +364,14 @@ LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
HRESULT InitDevice( const TexMetadata& mdata ) HRESULT InitDevice(const TexMetadata& mdata)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
RECT rc; RECT rc;
GetClientRect( g_hWnd, &rc ); GetClientRect(g_hWnd, &rc);
UINT width = rc.right - rc.left; auto width = static_cast<UINT>(rc.right - rc.left);
UINT height = rc.bottom - rc.top; auto height = static_cast<UINT>(rc.bottom - rc.top);
UINT createDeviceFlags = 0; UINT createDeviceFlags = 0;
#if defined( DEBUG ) || defined( _DEBUG ) #if defined( DEBUG ) || defined( _DEBUG )
@ -368,32 +404,35 @@ HRESULT InitDevice( const TexMetadata& mdata )
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd; sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1; sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE; sd.Windowed = TRUE;
for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ ) for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{ {
g_driverType = driverTypes[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/
if( SUCCEEDED( hr ) ) 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; break;
} }
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Create a render target view // Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr; ID3D11Texture2D* pBackBuffer = nullptr;
hr = g_pSwapChain->GetBuffer( 0, IID_PPV_ARGS(&pBackBuffer) ); hr = g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
D3D11_RENDER_TARGET_VIEW_DESC vd = {}; D3D11_RENDER_TARGET_VIEW_DESC vd = {};
vd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; vd.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
vd.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; vd.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, &vd, &g_pRenderTargetView ); hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, &vd, &g_pRenderTargetView);
pBackBuffer->Release(); pBackBuffer->Release();
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Create depth stencil texture // Create depth stencil texture
@ -404,13 +443,10 @@ HRESULT InitDevice( const TexMetadata& mdata )
descDepth.ArraySize = 1; descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1; descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT; descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL; descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0; hr = g_pd3dDevice->CreateTexture2D(&descDepth, nullptr, &g_pDepthStencil);
descDepth.MiscFlags = 0; if (FAILED(hr))
hr = g_pd3dDevice->CreateTexture2D( &descDepth, nullptr, &g_pDepthStencil );
if( FAILED( hr ) )
return hr; return hr;
// Create the depth stencil view // Create the depth stencil view
@ -418,25 +454,24 @@ HRESULT InitDevice( const TexMetadata& mdata )
descDSV.Format = descDepth.Format; descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0; descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView ); hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView ); g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, g_pDepthStencilView);
// Setup the viewport // Setup the viewport
D3D11_VIEWPORT vp; D3D11_VIEWPORT vp = {};
vp.Width = (FLOAT)width; vp.TopLeftX = vp.TopLeftY = 0.f;
vp.Height = (FLOAT)height; vp.Width = static_cast<float>(width);
vp.MinDepth = 0.0f; vp.Height = static_cast<float>(height);
vp.MaxDepth = 1.0f; vp.MinDepth = D3D11_MIN_DEPTH;
vp.TopLeftX = 0; vp.MaxDepth = D3D11_MAX_DEPTH;
vp.TopLeftY = 0; g_pImmediateContext->RSSetViewports(1, &vp);
g_pImmediateContext->RSSetViewports( 1, &vp );
// Create the vertex shader // Create the vertex shader
hr = g_pd3dDevice->CreateVertexShader( g_VS, sizeof(g_VS), nullptr, &g_pVertexShader ); hr = g_pd3dDevice->CreateVertexShader(g_VS, sizeof(g_VS), nullptr, &g_pVertexShader);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Define the input layout // Define the input layout
@ -448,12 +483,12 @@ HRESULT InitDevice( const TexMetadata& mdata )
constexpr UINT numElements = static_cast<UINT>(std::size(layout)); constexpr UINT numElements = static_cast<UINT>(std::size(layout));
// Create the input layout // Create the input layout
hr = g_pd3dDevice->CreateInputLayout( layout, numElements, g_VS, sizeof(g_VS), &g_pVertexLayout ); hr = g_pd3dDevice->CreateInputLayout(layout, numElements, g_VS, sizeof(g_VS), &g_pVertexLayout);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Set the input layout // Set the input layout
g_pImmediateContext->IASetInputLayout( g_pVertexLayout ); g_pImmediateContext->IASetInputLayout(g_pVertexLayout);
// Select the pixel shader // Select the pixel shader
bool isCubeMap = false; bool isCubeMap = false;
@ -461,10 +496,10 @@ HRESULT InitDevice( const TexMetadata& mdata )
const BYTE* pshader = nullptr; const BYTE* pshader = nullptr;
size_t pshader_size = 0; size_t pshader_size = 0;
switch ( mdata.dimension ) switch (mdata.dimension)
{ {
case TEX_DIMENSION_TEXTURE1D: case TEX_DIMENSION_TEXTURE1D:
if ( mdata.arraySize > 1) if (mdata.arraySize > 1)
{ {
pshader = g_PS_1DArray; pshader = g_PS_1DArray;
pshader_size = sizeof(g_PS_1DArray); pshader_size = sizeof(g_PS_1DArray);
@ -478,13 +513,13 @@ HRESULT InitDevice( const TexMetadata& mdata )
break; break;
case TEX_DIMENSION_TEXTURE2D: case TEX_DIMENSION_TEXTURE2D:
if ( mdata.miscFlags & TEX_MISC_TEXTURECUBE ) if (mdata.miscFlags & TEX_MISC_TEXTURECUBE)
{ {
pshader = g_PS_Cube; pshader = g_PS_Cube;
pshader_size = sizeof(g_PS_Cube); pshader_size = sizeof(g_PS_Cube);
isCubeMap = true; isCubeMap = true;
} }
else if ( mdata.arraySize > 1 ) else if (mdata.arraySize > 1)
{ {
pshader = g_PS_2DArray; pshader = g_PS_2DArray;
pshader_size = sizeof(g_PS_2DArray); pshader_size = sizeof(g_PS_2DArray);
@ -505,11 +540,11 @@ HRESULT InitDevice( const TexMetadata& mdata )
return E_FAIL; return E_FAIL;
} }
assert( pshader && pshader_size > 0 ); assert(pshader && pshader_size > 0);
// Create the pixel shader // Create the pixel shader
hr = g_pd3dDevice->CreatePixelShader( pshader, pshader_size, nullptr, &g_pPixelShader ); hr = g_pd3dDevice->CreatePixelShader(pshader, pshader_size, nullptr, &g_pPixelShader);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Create vertex buffer // Create vertex buffer
@ -521,64 +556,64 @@ HRESULT InitDevice( const TexMetadata& mdata )
// Render cubemaps as horizontal cross // Render cubemaps as horizontal cross
// XPOS // XPOS
{ XMFLOAT4( .5f, .25f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(.5f, .25f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, .25f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, .25f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( .5f, -.25f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 0.f, 0.f ) }, { XMFLOAT4(.5f, -.25f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, -.25f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, -.25f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 0.f, 0.f) },
// XNEG // XNEG
{ XMFLOAT4( -.5f, .25f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 1.f, 0.f ) }, { XMFLOAT4(-.5f, .25f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 1.f, 0.f) },
{ XMFLOAT4( 0.f, .25f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 1.f, 0.f ) }, { XMFLOAT4(0.f, .25f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 1.f, 0.f) },
{ XMFLOAT4( -.5f, -.25f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 1.f, 0.f ) }, { XMFLOAT4(-.5f, -.25f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 1.f, 0.f) },
{ XMFLOAT4( 0.f, -.25f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 1.f, 0.f ) }, { XMFLOAT4(0.f, -.25f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 1.f, 0.f) },
// YPOS // YPOS
{ XMFLOAT4( -.5f, .75f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 2.f, 0.f ) }, { XMFLOAT4(-.5f, .75f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 2.f, 0.f) },
{ XMFLOAT4( 0.f, .75f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 2.f, 0.f ) }, { XMFLOAT4(0.f, .75f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 2.f, 0.f) },
{ XMFLOAT4( -.5f, .25f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 2.f, 0.f ) }, { XMFLOAT4(-.5f, .25f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 2.f, 0.f) },
{ XMFLOAT4( 0.f, .25f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 2.f, 0.f ) }, { XMFLOAT4(0.f, .25f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 2.f, 0.f) },
// YNEG // YNEG
{ XMFLOAT4( -.5f, -.25f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 3.f, 0.f ) }, { XMFLOAT4(-.5f, -.25f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 3.f, 0.f) },
{ XMFLOAT4( 0.f, -.25f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 3.f, 0.f ) }, { XMFLOAT4(0.f, -.25f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 3.f, 0.f) },
{ XMFLOAT4( -.5f, -.75f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 3.f, 0.f ) }, { XMFLOAT4(-.5f, -.75f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 3.f, 0.f) },
{ XMFLOAT4( 0.f, -.75f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 3.f, 0.f ) }, { XMFLOAT4(0.f, -.75f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 3.f, 0.f) },
// ZPOS // ZPOS
{ XMFLOAT4( 0.f, .25f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 4.f, 0.f ) }, { XMFLOAT4(0.f, .25f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 4.f, 0.f) },
{ XMFLOAT4( .5f, .25f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 4.f, 0.f ) }, { XMFLOAT4(.5f, .25f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 4.f, 0.f) },
{ XMFLOAT4( 0.f, -.25f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 4.f, 0.f ) }, { XMFLOAT4(0.f, -.25f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 4.f, 0.f) },
{ XMFLOAT4( .5f, -.25f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 4.f, 0.f ) }, { XMFLOAT4(.5f, -.25f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 4.f, 0.f) },
// ZNEG // ZNEG
{ XMFLOAT4( -1.f, .25f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 5.f, 0.f ) }, { XMFLOAT4(-1.f, .25f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 5.f, 0.f) },
{ XMFLOAT4( -.5f, .25f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 5.f, 0.f ) }, { XMFLOAT4(-.5f, .25f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 5.f, 0.f) },
{ XMFLOAT4( -1.f, -.25f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 5.f, 0.f ) }, { XMFLOAT4(-1.f, -.25f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 5.f, 0.f) },
{ XMFLOAT4( -.5f, -.25f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 5.f, 0.f ) }, { XMFLOAT4(-.5f, -.25f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 5.f, 0.f) },
}; };
static const SimpleVertex vertices[] = static const SimpleVertex vertices[] =
{ {
{ XMFLOAT4( -1.f, 1.f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(-1.f, 1.f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, 1.f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, 1.f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( -1.f, -1.f, 0.f, 1.f ), XMFLOAT4( 0.f, 1.f, 0.f, 0.f ) }, { XMFLOAT4(-1.f, -1.f, 0.f, 1.f), XMFLOAT4(0.f, 1.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, -1.f, 0.f, 1.f ), XMFLOAT4( 1.f, 1.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, -1.f, 0.f, 1.f), XMFLOAT4(1.f, 1.f, 0.f, 0.f) },
}; };
static const SimpleVertex vertices1D[] = static const SimpleVertex vertices1D[] =
{ {
{ XMFLOAT4( -1.f, .05f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(-1.f, .05f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, .05f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, .05f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( -1.f, -.05f, 0.f, 1.f ), XMFLOAT4( 0.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(-1.f, -.05f, 0.f, 1.f), XMFLOAT4(0.f, 0.f, 0.f, 0.f) },
{ XMFLOAT4( 1.f, -.05f, 0.f, 1.f ), XMFLOAT4( 1.f, 0.f, 0.f, 0.f ) }, { XMFLOAT4(1.f, -.05f, 0.f, 1.f), XMFLOAT4(1.f, 0.f, 0.f, 0.f) },
}; };
if ( isCubeMap ) if (isCubeMap)
{ {
nverts = static_cast<UINT>(std::size(verticesCube)); nverts = static_cast<UINT>(std::size(verticesCube));
InitData.pSysMem = verticesCube; InitData.pSysMem = verticesCube;
} }
else if ( is1D ) else if (is1D)
{ {
nverts = static_cast<UINT>(std::size(vertices1D)); nverts = static_cast<UINT>(std::size(vertices1D));
InitData.pSysMem = vertices1D; InitData.pSysMem = vertices1D;
@ -591,17 +626,17 @@ HRESULT InitDevice( const TexMetadata& mdata )
D3D11_BUFFER_DESC bd = {}; D3D11_BUFFER_DESC bd = {};
bd.Usage = D3D11_USAGE_DEFAULT; bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( SimpleVertex ) * nverts; bd.ByteWidth = sizeof(SimpleVertex) * nverts;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0; bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pVertexBuffer ); hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Set vertex buffer // Set vertex buffer
UINT stride = sizeof( SimpleVertex ); UINT stride = sizeof(SimpleVertex);
UINT offset = 0; UINT offset = 0;
g_pImmediateContext->IASetVertexBuffers( 0, 1, &g_pVertexBuffer, &stride, &offset ); g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
// Create index buffer // Create index buffer
static const WORD indicesCube[] = static const WORD indicesCube[] =
@ -626,7 +661,7 @@ HRESULT InitDevice( const TexMetadata& mdata )
2, 1, 3 2, 1, 3
}; };
if ( isCubeMap ) if (isCubeMap)
{ {
g_iIndices = static_cast<UINT>(std::size(indicesCube)); g_iIndices = static_cast<UINT>(std::size(indicesCube));
InitData.pSysMem = indicesCube; InitData.pSysMem = indicesCube;
@ -641,23 +676,23 @@ HRESULT InitDevice( const TexMetadata& mdata )
bd.ByteWidth = g_iIndices * sizeof(WORD); bd.ByteWidth = g_iIndices * sizeof(WORD);
bd.BindFlags = D3D11_BIND_INDEX_BUFFER; bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
bd.CPUAccessFlags = 0; bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer( &bd, &InitData, &g_pIndexBuffer ); hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pIndexBuffer);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Set index buffer // Set index buffer
g_pImmediateContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0 ); g_pImmediateContext->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0);
// Set primitive topology // Set primitive topology
g_pImmediateContext->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ); g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Create the constant buffers // Create the constant buffers
bd.Usage = D3D11_USAGE_DEFAULT; bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof(CBArrayControl); bd.ByteWidth = sizeof(CBArrayControl);
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = 0; bd.CPUAccessFlags = 0;
hr = g_pd3dDevice->CreateBuffer( &bd, nullptr, &g_pCBArrayControl ); hr = g_pd3dDevice->CreateBuffer(&bd, nullptr, &g_pCBArrayControl);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
// Create the state objects // Create the state objects
@ -669,8 +704,8 @@ HRESULT InitDevice( const TexMetadata& mdata )
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0; sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX; sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
hr = g_pd3dDevice->CreateSamplerState( &sampDesc, &g_pSamplerLinear ); hr = g_pd3dDevice->CreateSamplerState(&sampDesc, &g_pSamplerLinear);
if( FAILED( hr ) ) if (FAILED(hr))
return hr; return hr;
D3D11_BLEND_DESC dsc = D3D11_BLEND_DESC dsc =
@ -688,11 +723,11 @@ HRESULT InitDevice( const TexMetadata& mdata )
D3D11_BLEND_OP_ADD, D3D11_BLEND_OP_ADD,
D3D11_COLOR_WRITE_ENABLE_ALL D3D11_COLOR_WRITE_ENABLE_ALL
}, },
// ... // ...
} }
}; };
hr = g_pd3dDevice->CreateBlendState(&dsc, &g_AlphaBlendState ); hr = g_pd3dDevice->CreateBlendState(&dsc, &g_AlphaBlendState);
if( FAILED(hr) ) if (FAILED(hr))
return hr; return hr;
return S_OK; return S_OK;
@ -703,46 +738,45 @@ HRESULT InitDevice( const TexMetadata& mdata )
void Render() void Render()
{ {
float ClearColor[4] = { 0.f, 1.f, 1.f, 1.0f }; //red,green,blue,alpha float ClearColor[4] = { 0.f, 1.f, 1.f, 1.0f }; //red,green,blue,alpha
g_pImmediateContext->ClearRenderTargetView( g_pRenderTargetView, ClearColor ); g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, ClearColor);
g_pImmediateContext->ClearDepthStencilView( g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 ); g_pImmediateContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
float bf [4] = {1.0f, 1.0f, 1.0f, 1.0f}; float bf[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
g_pImmediateContext->OMSetBlendState( g_AlphaBlendState, bf, 0xffffffff ); g_pImmediateContext->OMSetBlendState(g_AlphaBlendState, bf, 0xffffffff);
CBArrayControl cb; CBArrayControl cb = {};
cb.Index = (float)g_iCurrentIndex; cb.Index = static_cast<float>(g_iCurrentIndex);
g_pImmediateContext->UpdateSubresource( g_pCBArrayControl, 0, nullptr, &cb, 0, 0 ); g_pImmediateContext->UpdateSubresource(g_pCBArrayControl, 0, nullptr, &cb, 0, 0);
g_pImmediateContext->VSSetShader( g_pVertexShader, nullptr, 0 ); g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0);
g_pImmediateContext->PSSetShader( g_pPixelShader, nullptr, 0 ); g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0);
g_pImmediateContext->PSSetConstantBuffers( 0, 1, &g_pCBArrayControl ); g_pImmediateContext->PSSetConstantBuffers(0, 1, &g_pCBArrayControl);
g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pSRV ); g_pImmediateContext->PSSetShaderResources(0, 1, &g_pSRV);
g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear ); g_pImmediateContext->PSSetSamplers(0, 1, &g_pSamplerLinear);
g_pImmediateContext->DrawIndexed( g_iIndices, 0, 0 ); g_pImmediateContext->DrawIndexed(g_iIndices, 0, 0);
g_pSwapChain->Present( 0, 0 ); g_pSwapChain->Present(0, 0);
} }
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
void CleanupDevice() void CleanupDevice()
{ {
if( g_pImmediateContext ) g_pImmediateContext->ClearState(); if (g_pImmediateContext) g_pImmediateContext->ClearState();
if( g_pSamplerLinear ) g_pSamplerLinear->Release(); if (g_pSamplerLinear) g_pSamplerLinear->Release();
if( g_AlphaBlendState ) g_AlphaBlendState->Release(); if (g_AlphaBlendState) g_AlphaBlendState->Release();
if( g_pSRV ) g_pSRV->Release(); if (g_pSRV) g_pSRV->Release();
if( g_pVertexBuffer ) g_pVertexBuffer->Release(); if (g_pVertexBuffer) g_pVertexBuffer->Release();
if( g_pIndexBuffer ) g_pIndexBuffer->Release(); if (g_pIndexBuffer) g_pIndexBuffer->Release();
if( g_pCBArrayControl ) g_pCBArrayControl->Release(); if (g_pCBArrayControl) g_pCBArrayControl->Release();
if( g_pVertexLayout ) g_pVertexLayout->Release(); if (g_pVertexLayout) g_pVertexLayout->Release();
if( g_pVertexShader ) g_pVertexShader->Release(); if (g_pVertexShader) g_pVertexShader->Release();
if( g_pPixelShader ) g_pPixelShader->Release(); if (g_pPixelShader) g_pPixelShader->Release();
if( g_pDepthStencil ) g_pDepthStencil->Release(); if (g_pDepthStencil) g_pDepthStencil->Release();
if( g_pDepthStencilView ) g_pDepthStencilView->Release(); if (g_pDepthStencilView) g_pDepthStencilView->Release();
if( g_pRenderTargetView ) g_pRenderTargetView->Release(); if (g_pRenderTargetView) g_pRenderTargetView->Release();
if( g_pSwapChain ) g_pSwapChain->Release(); if (g_pSwapChain) g_pSwapChain->Release();
if( g_pImmediateContext ) g_pImmediateContext->Release(); if (g_pImmediateContext) g_pImmediateContext->Release();
if( g_pd3dDevice ) g_pd3dDevice->Release(); if (g_pd3dDevice) g_pd3dDevice->Release();
} }

View File

@ -8,17 +8,17 @@
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Constant Buffer Variables // Constant Buffer Variables
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
Texture1D tx1D : register( t0 ); Texture1D tx1D : register(t0);
Texture1DArray tx1DArray : register( t0 ); Texture1DArray tx1DArray : register(t0);
Texture2D tx2D : register( t0 ); Texture2D tx2D : register(t0);
Texture2DArray tx2DArray : register( t0 ); Texture2DArray tx2DArray : register(t0);
Texture3D tx3D : register( t0 ); Texture3D tx3D : register(t0);
SamplerState samLinear : register( s0 ); SamplerState samLinear : register(s0);
cbuffer cbArrayControl : register( b0 ) cbuffer cbArrayControl : register(b0)
{ {
float Index; float Index;
}; };
@ -40,7 +40,7 @@ struct PS_INPUT
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Vertex Shader // Vertex Shader
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input ) PS_INPUT VS(VS_INPUT input)
{ {
PS_INPUT output = (PS_INPUT)0; PS_INPUT output = (PS_INPUT)0;
output.Pos = input.Pos; output.Pos = input.Pos;
@ -52,35 +52,35 @@ PS_INPUT VS( VS_INPUT input )
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Pixel Shader // Pixel Shader
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
float4 PS_1D( PS_INPUT input) : SV_Target float4 PS_1D(PS_INPUT input) : SV_Target
{ {
return tx1D.Sample( samLinear, input.Tex.x ); return tx1D.Sample(samLinear, input.Tex.x);
} }
float4 PS_1DArray( PS_INPUT input) : SV_Target float4 PS_1DArray(PS_INPUT input) : SV_Target
{ {
return tx1DArray.Sample( samLinear, float2(input.Tex.x, Index) ); return tx1DArray.Sample(samLinear, float2(input.Tex.x, Index));
} }
float4 PS_2D( PS_INPUT input) : SV_Target float4 PS_2D(PS_INPUT input) : SV_Target
{ {
return tx2D.Sample( samLinear, input.Tex.xy ); return tx2D.Sample(samLinear, input.Tex.xy);
} }
float4 PS_2DArray( PS_INPUT input) : SV_Target float4 PS_2DArray(PS_INPUT input) : SV_Target
{ {
return tx2DArray.Sample( samLinear, float3(input.Tex.xy, Index) ); return tx2DArray.Sample(samLinear, float3(input.Tex.xy, Index));
} }
float4 PS_3D( PS_INPUT input) : SV_Target float4 PS_3D(PS_INPUT input) : SV_Target
{ {
int Width, Height, Depth; int Width, Height, Depth;
tx3D.GetDimensions( Width, Height, Depth); tx3D.GetDimensions(Width, Height, Depth);
return tx3D.Sample( samLinear, float3(input.Tex.xy, Index / Depth) ); return tx3D.Sample(samLinear, float3(input.Tex.xy, Index / Depth));
} }
float4 PS_Cube( PS_INPUT input) : SV_Target float4 PS_Cube(PS_INPUT input) : SV_Target
{ {
return tx2DArray.Sample( samLinear, float3(input.Tex.xy, input.Tex.z + (6*Index)) ); return tx2DArray.Sample(samLinear, float3(input.Tex.xy, input.Tex.z + (6*Index)));
} }