Quantcast
Channel: Games for Windows and the DirectX SDK
Viewing all 60 articles
Browse latest View live

DXGI Debug Device

$
0
0

In my original post on using the debug layer, I mentioned several tricks for getting helpful behavior out of the Direct3D SDK debug layer for your applications. This best practice is demonstrated in my Visual C++ Game templates as follows:

 #ifndef NDEBUG
Microsoft::ComPtr<ID3D11Debug> d3dDebug;
hr = m_d3dDevice.As(&d3dDebug);
if (SUCCEEDED(hr))
{
       ComPtr<ID3D11InfoQueue> d3dInfoQueue;
       hr = d3dDebug.As(&d3dInfoQueue);
       if (SUCCEEDED(hr))
       {
#ifdef _DEBUG
              d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
              d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
#endif
              D3D11_MESSAGE_ID hide [] =
              {
                     D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
              // TODO: Add more message IDs here as needed
              };
              D3D11_INFO_QUEUE_FILTER filter;
              memset(&filter, 0, sizeof(filter));
              filter.DenyList.NumIDs = _countof(hide);
              filter.DenyList.pIDList = hide;
              d3dInfoQueue->AddStorageFilterEntries(&filter);
       }
}
#endif

This snippet ensures that a common but harmless warning message is suppressed in non-Production builds, and enables ‘break on’ functionality in Debug builds if there are any serious corruption or error messages.

With the Direct3D 11.1 Runtime or later, you can also use a DXGI debug interface to track down additional leaks that are not known to your Direct3D 11 device. How this new interface is exposed, however, is a bit confusing.

For traditional Windows desktop apps, you are expected to use explicit linking. Since the DXGI debug layer is not present on end-user machines, this pattern encourages being able to handle the case of it not being present on the system. The confusing part is that the function you need, DXGIGetDebugInterface is not defined in any header and is not present in any import library.

#include <dxgidebug.h>
#if defined(_DEBUG)
    Microsoft::WRL::ComPtr<IDXGIInfoQueue> dxgiInfoQueue;
 
    typedef HRESULT (WINAPI * LPDXGIGETDEBUGINTERFACE)(REFIID, void ** );
 
    HMODULE dxgidebug = LoadLibraryEx( L"dxgidebug.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32 );
    if ( dxgidebug )
    {
        auto dxgiGetDebugInterface = reinterpret_cast<LPDXGIGETDEBUGINTERFACE>(
            reinterpret_cast<void*>( GetProcAddress( dxgidebug, "DXGIGetDebugInterface" ) ) );
 
        if ( SUCCEEDED( dxgiGetDebugInterface( IID_PPV_ARGS( dxgiInfoQueue.GetAddressOf() ) ) ) )
        {
            dxgiInfoQueue->SetBreakOnSeverity( DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, true );
            dxgiInfoQueue->SetBreakOnSeverity( DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, true );
        }
    }
#endif
 

One issue with this pattern is that you can’t use it for Windows Store or universal Windows apps since LoadPackagedLibrary cannot load a system DLL as a security measure. In the DirectX 11.2 Runtime (Windows 8.1 and Windows 10), there is now a DXGIGetDebugInterface1 defined in the dxgi1_3.h header and in the dxgi.lib import library. This implicit linking works fine for Windows Store apps and UWP, but for desktop apps you should stick with the explicit method particularly if you need Windows 7 support as this function is not present in the 11.1 or 11.0 runtime.

#if defined(_DEBUG)
Microsoft::WRL::ComPtr<IDXGIInfoQueue> dxgiInfoQueue;
if ( SUCCEEDED( DXGIGetDebugInterface1( 0, IID_PPV_ARGS( dxgiInfoQueue.GetAddressOf() ) ) ) )
{
       dxgiInfoQueue->SetBreakOnSeverity( DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, true );
       dxgiInfoQueue->SetBreakOnSeverity( DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, true );
}
#endif

As with the Direct3D debug layer, there is a method for reporting live DXGI objects as well for tracking down resource leaks. You obtain a IDXGIDevice instance the same was as you do a IDXGIInfoQueue interface above, and then call ReportLiveObjects.

DXGUID: One other issue of note is that the various DXGI debug control GUIDs (i.e. DXGI_DEBUG_ALL) are missing from DXGUID.LIB. You have to define it yourself using INITGUID. This is fixed for the Windows 10 SDK.

Related: Direct3D SDK Debug Layer Tricks, DirectX 11.1 and Windows 7 Update


Windows 10 SDK RTM

$
0
0

Last week saw the release of the final version of VS 2015, and yesterday was the release of the Windows 10 SDK (build 10240). The Windows 10 SDK is installed via VS 2015 Custom install options or as a standalone installer. This includes DirectXMath 3.07, Direct3D 11.3, Direct3D 12.0, DXGI 1.4, Direct2D/DirectWrite 1.3, and XAudio 2.9.

VS 2015 Users: Be sure to read this blog post about configuring your VS 2015 projects to use the 10240 build of the Windows 10 SDK–remember that Windows desktop applications with VS 2015 default to building with the Windows 8.1 SDK Spring 2015 release.

GitHub: All of my open source project have new releases to support VS 2015 and Windows 10 SDK RTM: DirectXTK, DirectXTex, DirectXMesh, UVAtlas, Effects11, and DXUT11.

Direct3D 12: The samples for Windows desktop development using Direct3D 12 are on GitHub. Note that WARP12 is part of the Graphics Tools optional feature as it’s currently intended only for use by developers.

XInput: For UWP, you can continue to use the XInput 1.4 API but need to change from linking to xinput.lib to xinputuap.lib. Alternatively, you can make use of the GamePad class in DirectX Tool Kit which uses the new Windows.Gaming.Input API. See XInput and Windows 8.

XAudio: With the Windows 10 SDK, if you are linking with xaudio2.lib you are linking against XAudio 2.9 and should build your application using _WIN32_WINNT=0x0A00. If you want to use XAudio 2.8 with the Windows 10 SDK, you need to set _WIN32_WINNT=0x0602 or _WIN32_WINNT=0x0603 and link against xaudio2_8.lib. See XAudio2 and Windows 8 for more details.

VS 2013 Users: As with the past few releases, the Windows 10 SDK only integrates with the latest Visual Studio, VS 2015. You can use the Windows 10 SDK with VS 2013 by using the props attached per this Visual C+ Team Blog.

Samples: Official Windows samples are hosted on GitHub: Windows-universal-samplesWindows-classic-samples, Windows-driver-samples. See also DirectX SDK Samples Catalog.

Related: Windows 10 SDK (November 2015)

Windows10SDKVS13.zip

Where is the DirectX SDK (2015 Edition)?

$
0
0

As noted on MSDN, the DirectX SDK is deprecated. The June 2010 release is the last release, and “DirectX” is now part of the Windows SDK. There are really only three scenarios where you should continue to use the old DirectX SDK:

  1. You have code (or perhaps an older book) that makes use of D3DX9, D3DX10, D3DX11, or XACT Engine.
  2. Your application uses use XAudio2 and supports Windows 7 systems.
  3. You are targeting Windows XP with the alternate v1x0_xp Platform Toolset.

Developer Runtime: The Windows 8.1 SDK or Windows 10 SDK is where you obtain the latest DirectX Developer Runtime that is compatible with Windows 7, Windows 8.0, and Windows 8.1. The DirectX Developer Runtime for Windows 10 is an optional Windows feature you enable in the operating system. You can also enable it on Windows 10 via the command-line using an admin prompt:

Dism /online /add-capability /capabilityname:Tools.Graphics.DirectX~~~~0.0.1.0

D3DX: All versions of D3DX are deprecated including D3DX9, D3DX10, and D3DX11. See Living without D3DX for replacements and recommendations including DirectX Tool Kit, DirectXTex, DirectXMesh, and UVAtlas on GitHub.

FX: The latest version of Effects for Direct3D 11 is available on GitHub and does not require the legacy DirectX SDK or any version of D3DX.

DXUT: The latest version of DXUT for Direct3D 11 is available on GitHub and does not require the legacy DirectX SDK or any version of D3DX.

Tools: Some of the developer tools are in the Windows SDK others are not. See DirectX SDK Tools Catalog for a complete inventory.

Samples: A number of samples from the DirectX SDK (June 2010) release have been updated and posted to GitHub. They do not require the legacy DirectX SDK or any version of D3DX. See DirectX SDK Samples Catalog.

The Windows 10 SDK includes the latest headers and link libraries for DirectX including Direct3D 12.0, Direct3D 11.3, DXGI 1.4, Direct2D/DirectWrite 1.3, and XAudio 2.9.

Both the Windows 8.1 SDK Spring 2015 update and the Windows 10 SDK include Direct3D 11.0/11.1/11.2, DXGI 1.0/1.1/1.2/1.3, DirectXMath 3.07, and Direct2D/DirectWrite 1.0/1.1/1.2. They also include the legacy Direct3D 10.0/10.1, Direct3D9Ex, Direct3D 9 headers, DirectSound8, DirectInput8, and DirectMusic “core” APIs. See DirectX SDKs of a certain age for a full catalog of older DirectX APIs and their locations.

XInput: The Windows 10 SDK includes the Windows.Gaming.Input WinRT API which is supported on Windows 10. Both the Windows 8.1 SDK and Windows 10 SDK include XInput 1.4 which is supported on Windows 8.x and Windows 10. You should use XInput 9.1.0 to support Windows 7. See XINPUT and Windows 8 and DirectX Tool Kit: Now with GamePads.

XAudio: The Windows 10 SDK includes XAudio 2.9 which is supported on Windows 10. The Windows 8.1 SDK and Windows 10 SDK include XAudio 2.8 which is supported on Windows 8.x and Windows 10. You have to use the legacy DirectX SDK and XAudio 2.7 to support Windows 7. See XAudio2 and Windows 8 and Known Issues: XAudio 2.7.

DirectX SDK: If you need to make use of legacy DirectX SDK components such as D3DX9, D3DX10, D3DX11, or XAudio 2.7 with VS 2012, VS 2013 or VS 2015, see MSDN for details on mixing the Windows 8 or Windows 10 SDK correctly with the legacy DirectX SDK. Be sure to The Zombie DirectX SDK as well. If you are targeting Windows XP which makes use of the Windows 7.1A SDK, see Visual Studio 2012 Update 1.

DirectX 12

There is something called the DirectX 12 SDK which was used as a beta vehicle for the development of DirectX 12 through the Early Access Program. Now that the Windows 10 SDK is final, you don’t need access to the DirectX 12 SDK at all. The DirectX 12 samples are on GitHub. Note that there is also a header file called d3dx12.h which is an all inline header with some utility code shipped in the GitHub samples.

Related: Where is the DirectX SDK (2013 Edition)?

See also: Where is DXERR.LIB?, GDF Tools, XDSP.H, SH Math

DirectX Tool Kit: Keyboard and Mouse support

$
0
0

The GamePad abstraction in DirectX Tool Kit was designed to simplify implementing game controller input across the spectrum of platforms supported by DirectX Tool Kit: Windows desktop, Xbox One, Windows 8 Store, and now universal Windows Apps for Windows 10. In a similar vein, the July 2015 release of DirectX Tool Kit includes two new classes for handling keyboard and mouse input across Windows desktop, Windows 8 Store, and universal Windows apps for Windows 10.

Keyboard

The Keyboard class is as usual based on the XNA Game Studio design, with the primary difference being that I needed to use a singleton rather than a static class. As with the XNA Game Studio class, this is intended for using the keyboard as a ‘game controller’ mapping keys to game input events. To support full text input for chat or text editing, you should make use of the underlying platform’s keyboard support to fully handle international input. Integration of the Keyboard class is slightly more complicated than GamePad in that after creating the class instance, you need to make the appropriate calls from either your Win32 message pump for keyboard messages in Windows desktop apps, or you need to provide your application’s CoreWindow so that Keyboard can register for the needed callbacks.

For Windows desktop applications, Keyboard takes input from WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, and WM_SYSKEYDOWN Win32 messages. For Windows 8 Store and universal Windows apps, it makes use of CoreDispatcher::AcceleratorKeyActivated. As with XNA Game Studio, the Keyboard::State object encodes the virtual keys (rather than scan codes). This works well for most key combinations, but this is a bit quirky when handling Left vs. Right Shift keys (details in the class documentation).

The abstraction makes writing keyboard-based controls quite simple and portable across the supported platforms:

void Game::Update(DX::StepTimer const& timer)
{

auto kb = m_keyboard->GetState();

if (kb.Up || kb.W)
move.y += 1.f;

if (kb.Down || kb.S)
move.y -= 1.f;

if (kb.Left || kb.A)
move.x += 1.f;

if (kb.Right || kb.D)
move.x -= 1.f;
}

See the documentation wiki page on the new class for details, and the related tutorial.

Mouse

The basics of the Mouse class are borrowed from XNA Game Studio, but is also singleton rather than a static class as well as having explicit support for a ‘relative’ or ‘mouse-look’ input mode. Integration of the Mouse class is slightly more complicated than GamePad in that you need to make the appropriate calls from either your Win32 message pump for mouse messages in Windows desktop apps, or you need to provide your application’s CoreWindow so that Mouse can register for the needed callbacks.

For Windows desktop applications, Mouse uses WM_MOUSEMOVE, WM_LBUTTONDOWN, WM_MOUSEWHEEL, etc. events for handling the default ‘absolute’ mouse position mode. When in the ‘relative’ mode, it uses WM_INPUT per the Taking Advantage of High-Definition Mouse Movement article.

For Windows 8 Store and universal Windows apps, Mouse makes use of the standard CoreWindow::PointerMoved, CoreWindow::PointerPressed, etc. events for handling the default ‘absolutely’ mouse position mode per Responding to touch input (DirectX and C++). For these platforms, the Mouse class also handles the conversion of DIPs to pixels, so be sure to call SetDPI appropriately. When in ‘relative’ mode, it uses CoreWindow::MouseMoved per Developing mouse controls (DirectX and C++).

The abstraction makes implementing mouse controls simple and reasonably portable between the supported platforms:

void Game::Update(DX::StepTimer const& timer)
{
auto state = g_mouse->GetState();
if (state.positionMode == Mouse::MODE_RELATIVE)
{
// state.x and state.y are relative values; system cursor is not visible
}
else
{
// state.x and state.y are absolute pixel values; system cursor is visible
}

tracker.Update(state);

if (tracker.leftButton == Mouse::ButtonStateTracker::ButtonState::PRESSED)
{
mouse->SetMode(Mouse::MODE_RELATIVE);
}
else if (tracker.leftButton == Mouse::ButtonStateTracker::ButtonState::RELEASED)
{
mouse->SetMode(Mouse::MODE_ABSOLUTE);
}

}

See the documentation wiki page on the new class for details, and the related tutorial.

DirectInput: Developers are strongly discouraged from using legacy DirectInput for handling keyboard and mouse processing as far back as Windows XP. DirectInput should really only be used for supporting legacy HID game controllers and joysticks.

Known Issues: XAudio 2.7

$
0
0

The XAudio2 library in the legacy DirectX SDK makes use of COM creation and reference counting for lifetime management, and a recent investigation has found a problem in this implementation. In short: in some situations the XAudio DLL itself is unloaded before the XAudio2 objects are completely destroyed, thus leading to an access violation. This normally happens on exit, although the exact details of when it might be evident depends on exactly which version of Windows you are using and the overall process layout for your application.

This issue does not affect XAudio 2.8 (Windows 8 SDK), XAudio 2.9 (Windows 10 SDK), XAudio2 on Xbox 360, or XAudio2 on Xbox One.

An application level workaround is very easy to implement, and is already implemented in DirectX Tool Kit for Audio and in the XAudio2 DirectX SDK refreshed samples on GitHub.

Before your first XAudio object creation, you should create an explicit reference to the DLL and hold on to it.

 #if ( _WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
HMODULE g_XAudioDLL = nullptr;
#endif

...

#if ( _WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
#ifdef _DEBUG
g_XAudioDLL = LoadLibraryExW( L"XAudioD2_7.DLL", nullptr, 0x00000800 /* LOAD_LIBRARY_SEARCH_SYSTEM32 */ );
#else
g_XAudioDLL = LoadLibraryExW( L"XAudio2_7.DLL", nullptr, 0x00000800 /* LOAD_LIBRARY_SEARCH_SYSTEM32 */ );
#endif
if ( !mXAudioDLL )
// error
#endif

...

DWORD creationFlags = 0;
#if (_WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/) && defined(_DEBUG)
creationFlags |= XAUDIO2_DEBUG_ENGINE;
#endif
IXAudio2* pXAudio2 = nullptr;
HRESULT hr = XAudio2Create( &pXAudio2, creationFlags, XAUDIO2_DEFAULT_PROCESSOR );
if ( FAILED(hr) )
// error

From here on, you work with XAudio2 as normal, delete objects, etc. Later after you do all cleanup and are fully done with working with XAudio2, you release the “extra” DLL reference:

 if (pXAudio2)
pXAudio2->Release();

...

#if ( _WIN32_WINNT < 0x0602 /*_WIN32_WINNT_WIN8*/)
if (g_XAudioDLL)
{
FreeLibrary(g_XAudioDLL);
g_XAudioDLL = nullptr;
}
#endif

This ensures that the DLL is not unloaded while XAudio2 objects are active.

See also: XAudio2 and Windows 8, Learning XAudio2, Windows 10 SDK RTM

Visual Studio 2015 Update 1

$
0
0

VS 2015 Update 1 is now available for download, including the updated Community edition. The VS 2015 Update 1 Redistribution packages are also available (x86, x64), as well as the Remote Debugging Tools (x86, x64, ARM). For more information, see Brian Harry’s blog, the Visual C++ Team blog, and the Visual Studio Team blog. Be sure to read the MSDN page as well.

Compiler and CRT

VS 2015 Update 1 includes a new version of the C/C++ compiler (19.00.23506). There is also a new version of the C/C++ Runtime (14.0.23506). Here are a list of STL fixes included in VS 2015 Update 1. The new compiler also includes support for the C++11 language feature Expression SFINAE.

Note that VS 2015 can target Windows 10, Windows 8.1, Windows 8.0, Windows 7 Service Pack 1, Windows Vista Service Pack 2, and optionally Windows XP Service Pack 3. The Visual C++ 2015 REDIST does not support Windows 7 RTM, Windows Vista RTM, Windows Vista Service Pack 1, Windows XP RTM, Windows XP Service Pack 1, or Windows XP Service Pack 2 as these platforms are all outside their support lifecycle. See KB2661358.

Windows 10 SDK: VS 2015 Update 1 includes an updated Windows Tools 1.2 with the Windows 10 SDK for Version 1511 (10.0.10586). As before, this is an optional install.

.NET: There is now a 4.6.1 release including a number of Windows Presentation Foundation (WPF) improvements including a D3DImage for Direct3D 11 available on GitHub.

Related: Visual Studio 2015 RTM

Windows 10 SDK (November 2015)

$
0
0

The Windows 10 SDK for the November 2015 update of Windows 10 (build 10586) is now available. It can be installed via an optional install with VS 2015 Update 1 or as a standalone installer. This includes DirectXMath 3.08, Direct3D 11.4, Direct3D 12.0, DXGI 1.5, Direct2D/DirectWrite 1.3, and XAudio 2.9.

GitHub: All of my open source project have new releases to support VS 2015 Update 1 and the Windows 10 SDK (10586): DirectXTK, DirectXTex, DirectXMesh, UVAtlas, Effects11, and DXUT11.

DirectX Developer Runtime: The DirectX Developer Runtime for Windows 10 is an optional feature called Graphics Tools as described in this blog post. When upgrading from 10240 to 10586, the optional feature can be disabled rather than updated, which can be fixed by re-enabling the optional feature. Note that WARP12 is part of the Graphics Tools optional feature as it’s currently intended only for use by developers.

XInput: For UWP, you can continue to use the XInput 1.4 API but need to change from linking to xinput.lib to xinputuap.lib. Alternatively, you can make use of the GamePad class in DirectX Tool Kit which uses the new Windows.Gaming.Input API. See XInput and Windows 8.

XAudio: With the Windows 10 SDK, if you are linking with xaudio2.lib you are linking against XAudio 2.9 and should build your application using _WIN32_WINNT=0x0A00. If you want to use XAudio 2.8 with the Windows 10 SDK, you need to set _WIN32_WINNT=0x0602 or _WIN32_WINNT=0x0603 and link against xaudio2_8.lib. See XAudio2 and Windows 8 for more details.

VS 2013 Users: As with the past few releases, the Windows 10 SDK only integrates with the latest Visual Studio, VS 2015. You can use the Windows 10 SDK with VS 2013 by using the props attached per this Visual C+ Team Blog.

Samples: As with the Windows 10 SDK RTM, official Windows samples are hosted on GitHub: Windows-universal-samplesWindows-classic-samples, Windows-driver-samples. Additional Direct3D 12 samples can be found in DirectX-Graphics-Samples. See also DirectX SDK Samples Catalog.

Related: Windows 10 SDK RTM

Windows10SDKVS13-10586.zip

DirectXMath 3.08

$
0
0

DirectXMath version 3.08 is included in the Windows 10 SDK November 2015 update (10586) that ships with VS 2015 Update 1 with the Windows Tools 1.2 for Windows 10.

This new version includes the following:

  • Added use of _mm_sfence for Stream methods
  • Fixed bug with non-uniform scaling transforms for BoundingOrientedBox
  • Added asserts for Near/FarZ in XMMatrix* methods
  • Added use of =default for PODs with VS 2013/2015
  • Additional SSE and ARM-NEON optimizations for PackedVector functions

It’s a fairly minor update compared to DirectXMath 3.07, but does have one interesting side-effect worth discussing further. Because of the use of the C++11 =default construct, existing DirectXMath code may generate new previously latent warnings when building with VS 2013 or VS 2015:

warning C4101: 'X': unreferenced local variable

warning C4701: potentially uninitialized local variable 'X' used

The warnings are easy to address, but may surprise developers when they pop up in existing code. Note that the =default construct is not merely syntactic fluff: In some use cases, it can make the compiler generate much better code by understanding the constructor does nothing at all and the type in question is in fact ‘trivial plain-old-data’. This mostly shows up in the cases of inheritance, so it may not be obviously different in simple codegen cases. It does, however, cause these compiler to notice when a variable is not actually used or initialized.

BoundingOrientedBox

The fix for non-uniform scaling transformations is trivial to apply to older versions of the library:

 inline void XM_CALLCONV BoundingOrientedBox::Transform( BoundingOrientedBox& Out, FXMMATRIX M ) const
{
// Load the box.
XMVECTOR vCenter = XMLoadFloat3( &Center );
XMVECTOR vExtents = XMLoadFloat3( &Extents );
XMVECTOR vOrientation = XMLoadFloat4( &Orientation );

assert( DirectX::Internal::XMQuaternionIsUnit( vOrientation ) );

// Composite the box rotation and the transform rotation.
XMMATRIX nM;
nM.r[0] = XMVector3Normalize( M.r[0] );
nM.r[1] = XMVector3Normalize( M.r[1] );
nM.r[2] = XMVector3Normalize( M.r[2] );
nM.r[3] = g_XMIdentityR3;
XMVECTOR Rotation = XMQuaternionRotationMatrix( nM );
vOrientation = XMQuaternionMultiply( vOrientation, Rotation );

// Transform the center.
vCenter = XMVector3Transform( vCenter, M );

// Scale the box extents.
XMVECTOR dX = XMVector3Length( M.r[0] );
XMVECTOR dY = XMVector3Length( M.r[1] );
XMVECTOR dZ = XMVector3Length( M.r[2] );

XMVECTOR VectorScale = XMVectorSelect( dY, dX, g_XMSelect1000 ); // !!swapped dX and dY
VectorScale = XMVectorSelect( dZ, VectorScale, g_XMSelect1100 ); // !!swapped dZ and VectorScale
vExtents = vExtents * VectorScale;

// Store the box.
XMStoreFloat3( &Out.Center, vCenter );
XMStoreFloat3( &Out.Extents, vExtents );
XMStoreFloat4( &Out.Orientation, vOrientation );
}

Xbox One: DirectXMath 3.08 shipped in the Xbox One XDK (July 2015 or later)

Related: Known Issues: DirectXMath 3.03, DirectXMath 3.06


Direct3D Game Visual Studio templates (Redux)

$
0
0

Back in January, I released a D3D11Win32Game Visual Studio 2013 template for Win32 desktop development primarily to support my DirectX Tool Kit tutorials. I modeled it after the basic template that we ship with the Xbox One XDK that consist of a Game class which sets up a device, swap chain, and timed rendering loop. I’ve since updated the templates on GitHub and now have versions for VS 2015, for the universal Windows platform, for Direct3D 12, and versions with the DeviceResources abstraction that is used in the official Windows Store and UWP templates.

VS Express users: I recommend taking a look at the VS Community edition which supports Windows desktop development if you don’t have the budget for purchasing a license for the Pro+ editions

Using the VSIX

To install: VS 2013 users should run Direct3DWin32Game.vsix and VS 2015 users should run Direct3DUWPGame.vsix. These packages install all the templates supported for that version of Visual Studio under the “Visual C++” node of the New Project dialog. If you have Visual Studio open, you should shut it down and restart it. If you have an older version installed of this VSIX installed, you should uninstall the old one first.

To remove: Go to Tools / Extensions and Updates… then uninstall “Direct3DWin32Game” or “Direct3DUWPGame”.

Template VS 2013 VS 2015 Description
Direct3D Win32 Game ü ü Win32 desktop Direct3D 11 Game template
Direct3D Win32 Game DR ü ü Win32 desktop Direct3D 11 Game template with DeviceResources abstraction
Direct3D UWP Game û ü Universal Windows app Direct3D 11 Game template
Direct3D UWP Game DR û ü Universal Windows app Direct3D 11 Game template with DeviceResources abstraction
Direct3D 12 Win32 Game û ü Win32 desktop Direct3D 12 Game template
Direct3D 12 Win32 Game DR û ü Win32 desktop Direct3D 12 Game template with DeviceResources abstraction
Direct3D 12 UWP Game û ü Universal Windows app Direct3D 12 Game template
Direct3D 12 UWP Game DR û ü Universal Windows app Direct3D 12 Game template with DeviceResources abstraction

DirectX 12: The Direct3D 12 versions of the template set _WIN32_WINNT to 0x0A00 (Windows 10), while the Direct3D 11 versions still use 0x0600 (Windows Vista). You need to have the Windows 10 SDK installed to build the Direct3D 12 templates, and Windows 10 to run it.

UWP: You have to be using Windows 8.1 or Windows 10 with the Windows 10 SDK installed to build the UWP templates. You need a Windows 10 device to run it.

DeviceResources

The basic template puts all the code for creating the device and swapchain into the main Game class. This makes it very simple to reference especially in the tutorial lessons. This does have the result, however, of putting a fair amount of ‘boiler plate’ code that clutters up the main Game class. The alternative for larger projects are the “DR” versions which add a helper class called DeviceResources. This class owns the Direct3D device, the DXGI swap chain, the depth/stencil buffer, and the render views needed for basic rendering. This does requires a few changes in the Game class.

 Game::Game()
{
m_deviceResources = std::make_unique<DX::DeviceResources>();
m_deviceResources->RegisterDeviceNotify(this);
}


void Game::Initialize(HWND window, int width, int height)
{
m_deviceResources->SetWindow(window, width, height);

m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();

m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
...
}

...

void Game::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}

Clear();

// TODO: Add your rendering code here.

m_deviceResources->Present();
}

// Helper method to clear the back buffers.
void Game::Clear()
{
// Clear the views
auto context = m_deviceResources->GetD3DDeviceContext();
auto renderTarget = m_deviceResources->GetBackBufferRenderTargetView();
auto depthStencil = m_deviceResources->GetDepthStencilView();

context->ClearRenderTargetView(renderTarget, Colors::CornflowerBlue);
context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH, 1.0f, 0);
context->OMSetRenderTargets(1, &renderTarget, depthStencil);

// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
}

...

void Game::CreateDeviceDependentResources()
{
// TODO: Initialize device dependent objects here (independent of window size).
}

// Allocate all memory resources that change on a window SizeChanged event.
void Game::CreateWindowSizeDependentResources()
{
// TODO: Initialize windows-size dependent objects here.
}

void Game::OnDeviceLost()
{
// TODO: Add Direct3D resource cleanup here.
}

void Game::OnDeviceRestored()
{
CreateDeviceDependentResources();

CreateWindowSizeDependentResources();
}

A few key things to note about this code compared with the non-DR version: 

  • The Game::CreateDevice method has been replaced with a call to DeviceResources::CreateDeviceResources and Game::CreateDeviceDependentResources.
  • The Game::CreateResources method has been replaced with a call to DeviceResources::CreateWindowSizeDependentResources and Game::CreateWindowSizeDependentResources.
  • The Game::OnDeviceLost method is now a callback from DeviceResources and only handles the cleanup. The Game::OnDeviceRestored call is made when the device has been re-recreated.
  • The usage difference can be seen in Game::Clear where instead of using local member variables, accessors on DeviceResources are used to obtain the device, context, etc.

Otherwise the DR version of the template is the same as the original D3DGame templates, including using StepTimer. See this page for a more detailed overview.

DirectX 12: The details of the Direct3D 12 versions of DeviceResources is different than the Direct3D 11 version since the APIs are quite different, but it’s the same design.

VS 2013 vs. 2015: Note there is one minor code difference between the VS 2013 and VS 2015 version of the templates because VS 2013 does not support C++11 uniform initialization. Otherwise the code is basically the same. VS 2015 is required for UWP development, and the Windows 10 SDK is required for Direct3D 12 development which only officially integrates with VS 2015–you can use a props solution to get it to work with VS 2013.

Visual Studio 2015 Update 2

$
0
0

VS 2015 Update 2 is now available for download, including the updated Community edition. The Visual C++ 2015 Update 2 Redistribution packages are also available (x86, x64), as well as the Remote Debugging Tools (x86, x64, ARM). For more information, see the Visual Studio Team blog. Be sure to read the MSDN page as well.

Compiler and CRT

VS 2015 Update 2 includes a new version of the C/C++ compiler (19.00.23918). There is also a new version of the C/C++ Runtime (14.0.23918). See these posts for details of the compiler and library improvements, along with this list of bug fixes.

Windows 10 SDK: VS 2015 Update 2 includes Windows Tools 1.3 with the Windows 10 SDK for Version 1511 (10.0.10586). As before, this is an optional install. The new Windows Tools 1.3 also adds a UI dialog when creating new UWP projects that prompts for which version of the Windows 10 SDK to use. Note that this same dialog is triggered when using my Direct3D 12 Win32 Game templates–the minimum version value is not used in Win32 projects.

Visual C++ Build Tools 2015: There is a new edition of Visual Studio available without the IDE for those just looking for the compiler toolset.

Related: Visual Studio 2015 RTM, Visual Studio 2015 Update 1

Visual Studio 2015 Update 3

$
0
0

VS 2015 Update 3 is now available for download, including the updated Community edition. The Visual C++ 2015 Update 3 Redistribution packages are also available (x86, x64), as well as the Remote Debugging Tools (x86, x64, ARM). For more information see the Visual Studio Team Blog. Be sure to read the release notes.

Compiler and CRT

VS 2015 Update 3 includes a new version of the C/C++ compiler (19.00.24210.0). There is also a new version of the C/C++ Runtime (14.0.24212). See these posts for details about the compiler, optimizer, and standards compliance including expression SFINAE.

Windows 10 SDK: VS 2015 Update 3 includes new Windows Tools 1.4 with the Windows 10 SDK for Version 1511 (10.0.10586). As before, this is an optional install.

Visual C++ Build Tools 2015: There is an edition of Visual Studio available without the IDE for those just looking for the compiler toolset.

Related: Visual Studio 2015 RTM, Visual Studio 2015 Update 1, Visual Studio Update 2

DirectX Tool Kit for DirectX 12

$
0
0

Since the release of DirectX Tool Kit four years ago, it has proven to be a very useful library for samples, indie and hobbyist projects, people moving from XNA Game Studio to C++, learning Direct3D 11, and for developers looking for supported replacements for the legacy D3DX library and the retiring of the legacy DirectX SDK.

Today I’m announcing the DirectX Tool Kit for DirectX 12, which is hosted as it’s own GitHub site. After much discussion and debate, we realized that both tool kits are really independent projects. While they share a lot of code (all the non-graphics stuff is 100% identical), the Direct3D 12 API is significantly different than Direct3D 11 so the graphics components ‘feel’ the same but in practice are used very differently.

DirectXTK for DirectX 12 includes the following components:

  • CommonStates – Factory for common combinations of rendering states (C++ versions modelled after the ‘public fields’ of BlendState, DepthStencilState, RasterizerState, and SampleState classes used for XNA Game Studio)
  • DDSTextureLoader – light-weight DDS file texture loader
  • DirectXHelpers – misc C++ helpers for D3D programming
  • Effects – a collection of ready-to-use common shaders (C++ versions of the BasicEffect, AlphaTestEffect, DualTextureEffect, EnvironmentMapEffect, and SkinnedEffect used for XNA Game Studio)
  • GeometricPrimitives – geometry helpers for common shapes (Cube, Sphere, GeoSphere, Cylinder, Torus, Teapot, Tetrahedron, Octahedron, Dodecahedron, and Isosahedron)
  • GraphicsMemory – helper for managing graphics memory allocation
  • Model – draws simple meshes loaded from .SDKMESH or .VBO files
  • PrimitiveBatch – simple and efficient way to draw user primitives which replicates the simplicity of the Direct3D 9 era DrawUP APIs
  • ScreenGrab – light-weight screen shot saver
  • SpriteBatch – a 2D sprite rendering class (a C++ version of the SpriteBatch used for XNA Game Studio)
  • SpriteFont – a bitmap based text rendering (a C++ version of the SpriteFont used for XNA Game Studio)
  • VertexTypes –  collection of commonly used vertex buffer data structures with a input layout descriptions (C++ versions of VertexPositionColor, VertexPositionTexture, VertexPositionColorTexture, and VertexPositionNormalTexture structures used for XNA Game Studio)
  • WICTextureLoader – WIC-based image file texture loader
DirectX Tool Kit for DirectX 12 also includes DirectX Tool Kit for Audio, GamePad, Keyboard, Mouse, and SimpleMath.  The MakeSpriteFont and XWBTool tools are identical so they are hosted on the DirectX Tool Kit for DirectX 11 GitHub. A few key differences compared to the DirectX 11 version:
  • No support for loading .CMO models or DGSL effect shaders (i.e. DGSLEffect)
  • VertexTypes does not include VertexPositionNormalTangentColorTexture or
    VertexPositionNormalTangentColorTextureSkinning
  • DirectX Tool Kit for DirectX 11 supports Feature Level 9.3, while DirectX 12 requires Direct3D Feature Level 11.0. There are no expected DirectX 12 drivers for  any lower feature level devices.
  • The library assumes it is building for Windows 10 (aka _WIN32_WINNT=0x0A00) so it makes use of XAudio 2.9 and WIC2 as well as DirectX 12.

A word of caution: DirectX 12 is an API designed for graphics experts. If you are not already an expert in using Direct3D 11, I’d recommend sticking with the Direct3D 11 API until you find yourself in need of the additional control. Direct3D 12 provides a great deal of control over memory allocation, synchronization, state management which can result in reduced CPU overhead for rendering compared to older versions of Direct3D, but that control means the API is quite unforgiving. If you are new to Direct3D entirely, definitely start with Direct3D 11 first.

DirectX Tool Kit for DirectX 12 is documented on the GitHub wiki and there are basic tutorials on it’s usage. It’s also host on NuGet for Windows Universal Platform (UWP) apps and Win32 desktop apps on Windows 10.

Both version so the DirectX Tool Kit now include support for NormalMapEffect and per-pixel lighting support for EnvironmentMapEffect.

Windows 10 Anniversary Update SDK

$
0
0

Windows 10 Anniversary Update (build 14393, aka Version 1607) is now available along with a new Windows 10 SDK release. The Windows 10 Anniversary Update SDK (10.0.14393) can be installed via an optional install with VS 2015 Update 3 or as a standalone installer. This includes DirectXMath 3.09 and updated versions of Direct3D 12, Direct3D 11.4, DXGI 1.5, Direct2D/DirectWrite 1.3. Note XAudio 2.9 is unchanged.

GibHub: All of my open source projects have new releases to support the Windows 10 Anniversary Update SDK: DirectXTK, DirectXTK12, DirectXTex, DirectXMesh, UVAtlas, Effects 11, DXUT11.

DirectX Developer Runtime: The DirectX Developer Runtime for Windows 10 is an optional feature called Graphics Tools as described in this blog post. When upgrading from 10586 to 14393, the optional feature can be disabled rather than updated, which can be fixed by re-enabling the optional feature. Note that WARP12 is part of the Graphics Tools optional feature as it’s currently intended only for use by developers.

VS 2013 Users: As with the past few releases, the Windows 10 SDK only integrates with VS 2015. You can use the Windows 10 SDK with VS 2013 via the props attached per this Visual C+ Team Blog.

VS 2015 Users: Use the custom installer option (or modifying an existing install) with VS 2015 Update 3 to add the Windows Tools 1.4.1 and Windows 10 SDK (10.0.14393) feature.

Samples: As with the previous releases of the Windows 10 SDK, official Windows samples are hosted on GitHub: Windows-universal-samplesWindows-classic-samples, Windows-driver-samples. Additional Direct3D 12 samples can be found in DirectX-Graphics-Samples. See also DirectX SDK Samples Catalog.

Related: Windows 10 SDK RTM, Windows 10 SDK (November 2015)

Windows10SDKVS13-14393

DirectXMath 3.09

$
0
0

DirectXMath version 3.09 is included in the Windows 10 Anniversary Update SDK (14393) that ships with VS 2015 Update 3 when you install the Windows Tools 1.4.1 and select the 10.0.14393 Target Platform Version (see this blog post).

The new version includes the following:

  • Support for additional optimizations when built with /arch:AVX or /arch:AVX2
  • Added use of constexpr for type constructors, XMConvertToRadians, and XMConvertToDegrees
  • Marked __vector4i, XMXDEC4XMDECN4XMDEC4, and associated Load & Store functions as deprecated. These are vestiges of Xbox 360 support and will be removed in a future release.
  • Renamed parameter in XMMatrixPerspectiveFov* to reduce user confusion when relying on IntelliSense
  • XMU565XMUNIBBLE4 constructors take uint8_t instead of int8_t

Arch Switch

The DirectXMath library assumes on x86 and x64 that both SSE and SSE2 are always supported. Later instruction sets are not always supported on PCs, and to avoid the penalty of additional guarded code paths or runtime selection the library avoids using them. As I’ve covered in the past, you can use specific versions in your own guarded code paths as the developer can pick a high-enough level to do the runtime selection to amortize the penalties involved.

For fixed-platforms like the Xbox One, however, the library can rely on additional instructions being present. These optimizations are now also available in the Windows version of DirectXMath when built using the /arch:AVX or /arch:AVX2 switches. Keep in mind that the resulting EXE or DLL only works correctly on systems with AVX and/or AVX2 support, so you should ensure that these binaries are only used on such systems (XMVerifyCPUSupport will perform the additional tests as appropriate when built with these switches).

GitHub

In addition to shipping with the Windows SDK and the Xbox One XDK, DirectXMath is now also available on GitHub under the MIT license. This repo also includes all the extension libraries such as Spherical Harmonics math, XDSP, etc.

The GitHub’s master branch is already host to a new revision in progress for a future DirectXMath 3.10 release.

Related: Known Issues: DirectXMath 3.03, DirectXMath 3.06, DirectXMath 3.07, DirectXMath 3.08

Anatomy of Direct3D 12 Create Device

$
0
0

Based on some questions I’ve been getting lately, it seems like now’s a good time to revisit my classic post Anatomy of Direct3D 11 Create Device updated for Direct3D 12!

The first thing to note is that while you can pass a nullptr for the ‘default’ device with Direct3D 12 to D3D12CreateDevice, that’s probably not the best solution. At this point, every driver on Windows 7 or later supports Direct3D 11, so you can pretty safely assume the default device is going to support Direct3D 11 at some Direct3D hardware feature level. While a lot of existing (as well as new) GPUs support Direct3D 12, this doesn’t apply to all GPUs. Specifically, a new WDDM 2 driver is required to support Direct3D 12, and there are no devices below Direct3D Feature Level 11.0 that are expected to get such updated drivers.

Another difference is that the debug device is not enabled through a creation flag like it is in Direct3D 11. Therefore, our first step is to enable debugging if available (the debug device is only present if the Graphics Tools Windows 10 optional feature is enabled). I’m making use of Microsoft::WRL::ComPtr which is recommended for both Direct3D 11 and Direct3D 12 (see this page for more information on this useful smart-pointer for COM programming).

#if defined(_DEBUG)
    // Enable the debug layer.
    {
        ComPtr<ID3D12Debug> debugController;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
        {
            debugController->EnableDebugLayer();
        }
    }
#endif

Next, we create a DXGI factory:

ComPtr<IDXGIFactory1> dxgiFactory;
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory));
if (FAILED(hr))
    // Error!

Then we scan DXGI adapters looking for one that supports Direct3D 12:

ComPtr<IDXGIAdapter1> adapter;
for (UINT adapterIndex = 0;
     DXGI_ERROR_NOT_FOUND !=
         dxgiFactory->EnumAdapters1(adapterIndex, &adapter);
     ++adapterIndex)
{
    DXGI_ADAPTER_DESC1 desc;
    hr = adapter->GetDesc1(&desc);
    if (FAILED(hr))
        continue;

    if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
    {
        // Don't select the Basic Render Driver adapter.
        continue;
    }

    // Check to see if the adapter supports Direct3D 12,
    // but don't create the actual device yet.
    if (SUCCEEDED(
        D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0,
            _uuidof(ID3D12Device), nullptr)))
    {
        break;
    }
}

Note: We are excluding the Microsoft Basic Render adapter here (aka WARP+VGA driver) since games typically don’t play well using WARP, but keep in mind that WARP12 is not present on standard Windows 10 systems; it’s only installed as part of the Graphics Tools optional feature.

If there’s no Direct3D 12-capable hardware, then for development builds it is useful to fallback to the WARP software device for Direct3D 12. Here is another difference compared to Direct3D 11: WARP12 is a specific adapter you obtain from the DXGI factory:

#if !defined(NDEBUG)
    if (!adapter)
    {
        ComPtr<IDXGIFactory4> dxgiFactory4;
        if (SUCCEEDED(dxgiFactory.As(&dxgiFactory4)))
        {
            if (FAILED(dxgiFactory4->EnumWarpAdapter(IID_PPV_ARGS(&adapter))))
            {
                adapter.Reset();
            }
        }
     }
#endif

If at this point, we still don’t have a valid adapter, then either a fatal error should be displayed, -or- if the application supports it, you should fall back to using Direct3D 11.

Otherwise, it’s time to create the device. Here’s another Direct3D 11 difference: Instead of providing an input array of every possible Direct3D feature level your application supports, you simply provide the minimum feature level you can use. Because as I noted above there’s no expected drivers for anything below Feature Level 11.0, that’s the minimum I’m using in this code and you’ll find the same in the Visual Studio DirectX 12 templates.

ComPtr<ID3D12Device> device;
hr = D3D12CreateDevice(adapter.Get(),
    D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device));
if (FAILED(hr))
    // Error!

Great, so we have a device, but how do you know if you managed to get a higher feature level than your minimum? Here we use CheckFeatureSupport to find that out:

static const D3D_FEATURE_LEVEL s_featureLevels[] =
{
    D3D_FEATURE_LEVEL_12_1,
    D3D_FEATURE_LEVEL_12_0,
    D3D_FEATURE_LEVEL_11_1,
    D3D_FEATURE_LEVEL_11_0,
};

D3D12_FEATURE_DATA_FEATURE_LEVELS featLevels =
{
    _countof(s_featureLevels), s_featureLevels, D3D_FEATURE_LEVEL_11_0
};

D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
hr = device->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS,
     &featLevels, sizeof(featLevels));
if (SUCCEEDED(hr))
{
    featureLevel = featLevels.MaxSupportedFeatureLevel;
}

Swap Chain

At this point, you are ready to create the swap chain. For Win32 classic desktop apps you use CreateSwapChainForHwnd, and for Universal Windows Platform (UWP) apps you use CreateSwapChainForCoreWindow or CreateSwapChainForComposition, all of which require IDXGIFactory2 or later:

ComPtr<IDXGIFactory2> dxgiFactory2;
if (FAILED(dxgiFactory.As(&dxgiFactory2)))
    // Fatal error (shouldn't happen in practice at this point)

The key thing to note about swap chain creation is that you must use either DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL or DXGI_SWAP_EFFECT_FLIP_DISCARD for the DXGI_SWAP_CHAIN_DESC1.SwapEffect because the older swap effects are not supported for Direct3D 12.

For Universal Windows Platform (UWP) apps, you should also consider using DXGI_SCALING_ASPECT_RATIO_STRETCH for the DXGI_SWAP_CHAIN_DESC1.Scaling, but for Win32 classic desktop swap chains you need to stick with DXGI_SCALING_NONE or DXGI_SCALING_STRETCH.

One more consideration: For gamma-correct rendering to standard 8-bit per channel UNORM formats, you’ll want to create the Render Target using an sRGB format. The new flip modes, however, do not allow you to create a swap chain back buffer using an sRGB format. In this case, you create one using the non-sRGB format (i.e. DXGI_SWAP_CHAIN_DESC1.Format = DXGI_FORMAT_B8G8R8A8_UNORM) and use sRGB for the Render Target View (i.e. D3D12_RENDER_TARGET_VIEW_DESC.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB).

Note: With Direct3D 12, you cannot use the device.As(&dxgiDevice) sequence to obtain the DXGI factory from the Direct3D 12 device for cases where you use nullptr to create the D3D12CreateDevice instance. You must always explicitly create the DXGI factory using CreateDXGIFactory1 or CreateDXGIFactory2.

Windows SDK

To build an application using Direct3D 12 APIs, you must make use of the Windows 10 SDK. The latest version is the Windows 10 Anniversary Update SDK (14393). With Visual Studio 2015, you install it via a custom install option and for Win32 classic desktop projects you’ll need to explicitly change the project property to use it rather than the default Windows 8.1 SDK (Spring 2015).

Direct3D 12.1

With the Windows 10 Anniversary Update, newer drivers and devices can support some additional features for Direct3D 12. You can obtain the 12.1 interface from your 12.0 device by using QueryInterface or ComPtr::As–this will fail on older versions of Windows 10.

ComPtr<ID3D12Device1> device1;
if (SUCCEEDED(device.As(&device1)))
{
    // Direct3D 12.1 Runtime is available
}

Win32 desktop application notes

If your application supports Windows 8.1 or earlier, then you need to make use of explicit rather than implicit linking to the Direct3D 12 functions since they are not available before Windows 10. Implicit linking to dxgi.lib (and d3d11.lib if needed) is not a problem unless you are trying to support Windows XP as well. The code above is careful to try to use DXGI 1.1 for the initial detection to support Windows 7 systems.

HMODULE dx12 = LoadLibraryEx(L"d3d12.dll",
    nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!dx12)
    // Fallback to using Direct3D 11

auto pD3D12CreateDevice = reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(
    GetProcAddress(dx12, "D3D12CreateDevice"));
if (!pD3D12CreateDevice)
    // Fallback to using Direct3D 11

...

#if defined(_DEBUG)
    // Enable the debug layer.
    auto pD3D12GetDebugInterface =
        reinterpret_cast<PFN_D3D12_GET_DEBUG_INTERFACE>(
        GetProcAddress(dx12, "D3D12GetDebugInterface"));
    if (pD3D12GetDebugInterface)
    {
        ComPtr<ID3D12Debug> debugController;
        if (SUCCEEDED(pD3D12GetDebugInterface(
            IID_PPV_ARGS(&debugController))))
        {
            debugController->EnableDebugLayer();
        }
    }
#endif

...
// Change both cases where we use D3D12CreateDevice above to
// pD3D12CreateDevice. If you fail to find an adapter, use
// Direct3D 11 instead

Universal Windows Platform (UWP) notes

Because the minimum OS version is enforced for the platform, you can count on IDXGIFactory4 always being available. Therefore, you can simplify the code above by starting out with that version–which is exactly what you’ll find in the Visual Studio DirectX 12 UWP templates:

ComPtr<IDXGIFactory4> dxgiFactory;
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory));
if (FAILED(hr))
    // Error!

Related: Direct3D Game Visual Studio templates (Redux), DirectX Tool Kit for DirectX 12


Getting Started with Direct3D 12

$
0
0

The first thing to do is get up to speed on Direct3D 11 (see Getting Started with Direct3D 11), especially if you are coming from a background of knowing Direct3D 9. Jumping feet-first into Direct3D 12 without a solid grounding in what a Direct3D feature level means, DXGI device-and-swapchain creation, the modern HLSL compiler story, the fate of the legacy DirectX SDK, and the Direct3D 10/Direct3D 11 state model and graphics pipeline design is a recipe for confusion and frustration.

DirectX 12 is an expert API which builds on knowing the ins & outs of DirectX 11. DirectX 12 is an extremely low-level API designed for graphic experts who have a solid understanding of the architecture of modern GPU hardware, and can essentially write the DirectX 11 Runtime from scratch. Both DirectX 11 and DirectX 12 provide access to the same hardware features on Windows 10, but drive the hardware in different ways which can allow a well-optimized DirectX 12 engine to achieve much lower CPU overhead than in DirectX 11.

With that preamble out of the way, you should familiarize yourself with the documentation on MSDN and the official samples on GitHub. Then install Visual Studio 2015 and be sure to install the Windows Tools and Windows 10 SDK component. See this blog post for details.

For introductory samples, take a look at D3D12HelloWorld (PC desktop and UWP), as well as the Xbox ATG Graphics UWP samples. DirectX Tool Kit for DirectX 12 includes some tutorials as well.

Presentations: There’ve been a number of public presentations on Direct3D 12.

DirectX: Evolving Microsoft’s Graphics Platform (GDC 2014): link

Direct3D 12 API Preview (BUILD 2014): link

Better Power, Better Performance: Your Game on DirectX12 (GDC 2015): link

Advanced DirectX12 Graphics and Performance (GDC 2015/BUILD 2015): link

Direct3D Update: (GDC 2016): link

There is also a series of YouTube videos on various aspects of Direct3D 12 you should take a look at as well.

AMD, Intel, and NVidia also have additional materials available online.

Debugging: The debug device (aka the Developer Runtime) on Windows 10 is not installed by any SDK. It is enabled as a Windows Optional Feature called “Graphics Tools”. See this post for details. You should also familiarize yourself with DXGI debugging features.

Utilities: The various Direct3D 12 templates make use of a simple header-only helper D3DX12. This does not have nearly the scope of the deprecated D3DX libraries, but is useful in taking care of many of the more mundane aspects of creating the required structures. For support in loading textures, rendering fonts & sprites, loading models, etc. see DirectX Tool Kit for DirectX 12. For graphics math, see DirectXMath. You should use DirectXTex, DirectXMesh, and UVAtlas for content processing as well.

Multi-GPU: See this blog post.

Related: Anatomy of Direct3D 12 Create Device, Direct3D Game Visual Studio templates (Redux), Windows 10 Anniversary Update SDK

DirectXTex and DirectXMesh now support Direct3D 12

$
0
0

As part of my multi-year personal project of providing open source replacements for the deprecated D3DX library once found in the legacy DirectX SDK, two libraries are focused on content creation tools and build pipelines. DirectXTex handles loading image files, texture processing including format conversion, mipmap generation, block-compression, and writing out ‘fully cooked’ textures into DDS files. DirectXMesh provides geometry support such as computing normals and tangent-frames, transparent vertex cache optimization, and provides utilities for extracting/inserting vertex data in vertex buffers.

These libraries were originally written for DirectX 11, and it seems likely that most tools should continue to use DirectX 11 for the simplicity and ease of developer productivity. There are, however, cases where you want to use some of this functionality ‘in-engine’, so the January 2017 releases include DirectX 12 API support as well.

DirectXTex January 2017 release on GitHub

DirectXMesh January 2017 release on GitHub

To simplify supporting all the various platforms and Windows SDK combinations, the library continues to default to using DirectX 11. If you want to use DirectX 12, you need to explicitly include the required headers before including the library header:

#include <d3d12.h>
#include "DirectXTex.h"

You also need to link with the DirectXTex_Desktop_2015_Win10.vcxproj, DirectXTex_Windows10.vcxproj, or DirectXTex_XboxOneXDK_2015.vcxproj projects which build with both DirectX 11 and DirectX 12 support.

If you want to use both DirectX 11 and DirectX 12 in the same compilation module, then you need to explicitly include both:

#include <d3d11_1.h>
#include <d3d12.h>
#include "DirectXTex.h"

The story is similar for DirectXMesh, although in this case it’s just to let you use D3D12_ input layouts enums and structures instead of D3D11_–the data itself is identical.

In case you missed it, DirectXTex was updated to support the HDR (RGBE) file format as a source for floating-point HDR texture data, as well as having ‘opt-in’ support for OpenEXR. For more details on how to enable OpenEXR, see this page.

Related: DirectX Tool Kit for DirectX 12

DirectX Tool Kit and C++/WinRT

$
0
0

The February 2017 releases of DirectX Tool Kit for DirectX 11 and DirectX 12 are now available on GitHub. In addition to various bug-fixes and a few minor improvements to the input classes (Mouse, Keyboard, and GamePad), the libraries now also support C++/WinRT applications for UWP and Xbox One. C++/WinRT language projections allow you to use Windows Runtime APIs without using the C++/CX language extensions (i.e. the libraries will work with applications built with or without /ZW).

For more on C++/WinRT, see:

https://moderncpp.com/

C++ – Introducing C++/WinRT (MSDN Magazine)

cppwinrt on GitHub

Migrating C++/CX source code to C++/WinRT

Embracing Standard C++ for the Windows Runtime” (CppCon 2016)

Putting Coroutines to Work with the Windows Runtime” (CppCon 2016)

VS Templates: I’ve added C++/WinRT variants of my Direct3D UWP templates for DirectX 11 and DirectX 12 to directx-vs-templates and the VS 2015 VSIX.

Samples: In addition to the samples on the cppwinrt GitHub, there’s a C++/WinRT version of some samples on Xbox-ATG-Samples.

NuGet: You can use  C++/WinRT on NuGet as an easy way to add the C++/WinRT headers for the Windows 10 Anniversary Update (14393) to the templates above or your own project.

Compiler: To use C++/WinRT, you must be use using Visual Studio 2015 Update 3 or Visual Studio 2017 and build with the /std:c++latest switch. The use of /await is also recommended.

DirectXMath, DirectXTex, DirectXMesh: These libraries are also compatible with both C++/WinRT and C++/CX applications.

Related: DirectX Tool Kit for DirectX 12, Direct3D Game Visual Studio templates (Redux)

Visual Studio 2017

$
0
0

Visual Studio 2017 RTM is now available for download, including the updated Community edition. The VS 2017 RTM Redistribution packages are also available (x86, x64), as well as the Remote Debugging Tools (x86, x64). For more information see the Visual C++ Team Blog and Visual Studio Team Blog.

This version of Visual Studio includes a new lightweight installer. Be sure to read this post for an overview. Note that most C++ workloads include the Windows 10 Anniversary Update SDK (14393) by default, but older versions including Windows 8.1 SDK are available as optional components.

The latest docs are located here rather than their traditional location on MSDN.  In particular, see the Visual C++ Porting and Upgrading Guide.

UWP C++ Developers:  When you select the Universal Windows Platform (UWP) workload, be sure to add the optional component C++ Universal Windows Platform tools.

Automated Installs: See this page for details on installing VS 2017 from the command-line. For example, this installs C++ toolsets for game development for Win32 classic desktop and UWP apps:

vs_community.exe --lang en-us --add Microsoft.VisualStudio.Workload.NativeGame
--add Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Win81
--add Microsoft.VisualStudio.Workload.Universal
--add Microsoft.VisualStudio.ComponentGroup.UWP.VC
--add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended -p --wait

Compiler and CRT

VS 2017 includes a new version of C/C++ compiler (19.10.25017). See this blog post and this post for details on the new compiler and standards conformance (including more work on Expression SFINAE and additional C++14 conformance). See this post for details on /analyze updates, and this post for details on the C++ Core Guidelines Checker.

The C/C++ Runtime (4.10.25008) is binary compatible with VS 2015, which means you can safely link code built with VS 2015 with VS 2017 applications. See this post and this post for details. For details on library fixes in the latest version, see this post.

VS 2017 can target Windows 10, Windows 8.1, Windows 7 Service Pack 1, Windows Vista Service Pack 2, and optionally Windows XP Service Pack 3. Note that the Visual C++ 2017 REDIST does not support Windows 8.0, Windows 7 RTM, Windows Vista RTM, Windows Vista Service Pack 1, Windows XP RTM, Windows XP Service Pack 1, or Windows XP Service Pack 2 as these platforms are all outside their support lifecycle. See Visual Studio 2017 Product Family System Requirements.

Visual ++ Build Tools 2017: There’s an edition of Visual Studio available without the IDE for those looking for just the compiler toolset or setting up a build server.

Known issues

  • Note that there are errors generated when using the new VS 2017 conformance switch with the platform headers in Windows 10 SDK (14393) or earlier. These are addressed in the upcoming Windows 10 Creators Update SDK. Microsoft Developer Insiders can try out /permissive- with the preview SDK.
  • When you upgrade a project to the v141 toolset, you are given a UI prompt to select which version of the Windows SDK to use based on all the side-by-side installed SDKs.  This is ignored for Win32 desktop projects. To use a newer Windows SDK, you should edit the project properties after the upgrade–otherwise it’s likely the 8.1 SDK will be required, which is not installed by default.

Windows XP: When building using the “v141_xp” Platform Toolset for Windows XP Service Pack 3 target support, remember this uses the Windows 7.1A SDK. The older SDK will generate some warnings in system headers with the new toolset that have to be externally suppressed. See VS 2012 Update 1 for some additional implications for DirectX development.

DirectX SDK: If you need to continue to make use of legacy DirectX SDK components such as D3DX9, D3DX10, D3DX11, or XAudio 2.7 with Visual Studio 2017, see MSDN for details on mixing the paths correctly. See also DirectX SDKs of a certain age, The Zombie DirectX SDK, Living without D3DX, DirectX SDK Tools Catalog, DirectX SDK Samples Catalog, and Where’s DXERR.LIB?

GitHub: There are VS 2017 projects in the latest releases of DirectX Tool Kit for DirectX 11, DirectX Tool Kit for DirectX 12, DirectXTex, DirectXMesh, UVAtlas, DXUT, and Effects 11. These are set up to use the Windows 10 SDK (14393) because that’s the default version for VS 2017. Because of the fact that VS 2015 and VS 2017 are binary compatible w.r.t. to the C/C++ runtime, you can use the 2015 version of the NuGet packages with VS 2017. The Direct3D Game templates have been updated to support VS 2017 as well, and the Direct3DUWPGame.vsix now supports both VS 2015 aad VS 2017.

Windows 10 Creators Update SDK

$
0
0

The Windows 10 Creators Update (build 15063, aka Version 1703) is now available along with a new Windows 10 SDK release. The Windows 10 Creators Update SDK (10.0.15063) can be installed via VS 2017 or as a standalone installer. This includes DirectXMath 3.10 and updated versions of Direct3D 12, DXGI 1.6, Direct3D 11.4 Direct2D, and DirectWrite.

VS 2015 Users: Note that the Windows 10 SDK (15063) is officially only supported for VS 2017.

VS 2017 Users: The Windows 10 Creators Update SDK resolves a number of conformance errors in Windows system headers enabling the use of the /permissive- switch. You can install the new SDK by installing the latest VS 2017 update (15.1 or later). Note that the new Windows 10 SDK  (15063) is actually three distinct components: Microsoft.VisualStudio.Component.Windows10SDK.15063.Desktop, Microsoft.VisualStudio.Component.Windows10SDK.15063.UWP, and Microsoft.VisualStudio.Component.Windows10SDK.15063.UWP.Native.

Related: Windows 10 SDK RTM, Windows 10 SDK (November 2015), Windows 10 Anniversary Update SDK

Viewing all 60 articles
Browse latest View live