DirectX creating swapchain

My book has code for creating swap chains.

IDXGIDevice * dxgiDevice = 0;
mD3dDevice->QueryInterface( __uuidof( IDXGIDevice ),( void ** ) & dxgiDevice );

IDXGIAdapter * dxgiAdapter = 0;
dxgiDevice->GetParent( __uuidof( IDXGIAdapter ),( void ** ) & dxgiAdapter );

IDXGIFactory * dxgiFactory = 0;
dxgiAdapter->GetParent( __uuidof( IDXGIFactory ),( void ** ) & dxgiFactory );

dxgiFactory->CreateSwapChain( mD3dDevice, & sd, & mSwapChain );

      

There is no explanation for this code, I check several books on drugs and there are too many explanations for this code.

Please help mi with this. I really don't understand what the GetParent method does. idxgiDevice extends idxgiObject. parent for idxgiDevice is idxgiObject, why are they using a pointer to idxgiFacory as a parameter.

And I don't know I understand the request Interface good

Please help mi with this

+3


source to share


1 answer


This code is for creating swap chains with DirectX 11 or later interfaces, and this pattern is specifically designed to make sure that the DXGI factory you are using is the one that was actually used when you created Direct3D 11.

Basically, when you first created a Direct3D 11 device, you had the opportunity to provide an instance IDXGIAdapter

for use. Most people post nullptr

or NULL

here and just let you create a device on the default adapter on the system. However, you need an instance of the DXGI factory to complete the swap setup. You could theoretically create one using DXGICreateFactory1

, but you can easily mess up and get the "wrong" one using DXGICreateFactory

or possibly DXGICreateFactory2

with the wrong flags.

Instead, the safest thing to do is retrieve IDXGIDevice

from yours ID3D11Device

using the standard COM IUnknown :: QueryInterface :

IDXGIDevice * dxgiDevice = 0;
HRESULT hr = mD3dDevice->QueryInterface( __uuidof( IDXGIDevice ),( void ** ) & dxgiDevice );
if ( SUCCEEDED(hr) )

      

Then get IDXGIAdapter

from IDXGIDevice

using IDXGIObject :: GetParent :

IDXGIAdapter * dxgiAdapter = 0;
hr = dxgiDevice->GetParent( __uuidof( IDXGIAdapter ),( void ** ) & dxgiAdapter );
if ( SUCCEEDED(hr) )

      

Then return IDXGIFactory

from IDXGIAdapter

using IDXGIObject :: GetParent :



IDXGIFactory * dxgiFactory = 0;
hr = dxgiAdapter->GetParent( __uuidof( IDXGIFactory ),( void ** ) & dxgiFactory );
if ( SUCCEEDED(hr) )

      

You now have IDXGIFactory

Direct3D 11 connected to your device, no matter how it was created. Remember that COM reference counting means you have references to all of these objects that you must clean up:

dxgiFactory->Release();
dxgiAdapter->Release();
dxgiDevice->Release();

      

Note that IDXGIFactory::CreateSwapChain

this is the DirectX 11.0 way of creating swap chains, and you will get basically the same results if you used D3D11CreateDeviceAndSwapChain and not D3D11CreateDevice in the first place. For DirectX 11.1 or later systems, you can use IDXGIFactory2::CreateSwapChainForHwnd

Win32 instead for desktop applications. For Windows Store apps, Windows Phone 8 and Xbox One, you always use IDXGIFactory2::CreateSwapChainForCoreWindow

.

For Win32 desktop, you have to follow the above code, for example:

IDXGIFactory2* dxgiFactory2 = 0;
hr = dxgiFactory->QueryInterface( __uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2) );
if ( SUCCEEDED(hr) )
{
    // This system has DirectX 11.1 or later installed, so we can use this interface
    dxgiFactory2->CreateSwapChainForHwnd( /* parameters */ );
    dxgiFactory2->Release();
}
else
{
    // This system only has DirectX 11.0 installed
     dxgiFactory->CreateSwapChain( /* parameters */ );
}

      

See Anatomy of Direct3D 11 Create a Device and Sample Tutorial Direct3D desktop version of Win32 application or Windows Store version of application .

+8


source







All Articles