gpt4 book ai didi

c++ - 为什么对设备和交换链使用相同的 IDXGIFactory

转载 作者:搜寻专家 更新时间:2023-10-31 01:39:19 26 4
gpt4 key购买 nike

IDXGIFactory interface 的引用告诉我,为了创建交换链,我可能会使用用于创建 Direct3D 设备的同一工厂:

Because you can create a Direct3D device without creating a swap chain, you might need to retrieve the factory that is used to create the device in order to create a swap chain.

它还给出了以下代码示例:

IDXGIDevice * pDXGIDevice;
hr = g_pd3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice);

IDXGIAdapter * pDXGIAdapter;
hr = pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter);

IDXGIFactory * pIDXGIFactory;
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&pIDXGIFactory);

这篇文章非常简洁,当试图完全理解它时,出现了以下两个问题,而第一个问题是主要问题(关于本帖的标题):

为什么我必须使用用于创建 Direct3D 设备的同一个工厂才能创建交换链?工厂实例是否维护一个重要的内部状态,或者只是为了避免创建另一个消耗资源的工厂实例?

此外,在代码示例中,我遇到了以下代码行:

hr = pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter);

对我来说,IDXGIAdapter 是 IDXGIDevice 的父级是不合逻辑的。否则我会期待 IDXGIAdapter有一个像 CreateDevice 这样的方法,它会使适配器成为设备的父级。但事实并非如此。为什么适配器是设备的父级?

最佳答案

问题的根源可以追溯到 DXGI 1.0 (Direct3D 10) 和 DXGI 1.1 (Direct3D 11)。如果您尝试使用 IDXGIFactory由两者创建 CreateDXGIFactoryCreateDXGIFactory1在相同的过程中,事情会很快变成梨形。

另一个问题是,当您将“默认设备”用于 Direct3D 10.x 或 11.x 设备创建时,会创建一个隐式工厂。上面的“魔术代码序列”让您可以隐式创建工厂。

文档中的第二步应该是 dxgiDevice->GetAdapter不是 dxgiDevice->GetParent .我会就此提交文档错误。

如果使用 Microsoft::WRL::ComPtr,这会更容易做到:

    // First, retrieve the underlying DXGI Device from the D3D Device
ComPtr<IDXGIDevice1> dxgiDevice;
DX::ThrowIfFailed(m_d3dDevice.As(&dxgiDevice));

// Identify the physical adapter (GPU or card) this device is running on.
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(dxgiDevice->GetAdapter(dxgiAdapter.GetAddressOf()));

// And obtain the factory object that created it.
ComPtr<IDXGIFactory1> dxgiFactory;
DX::ThrowIfFailed(dxgiAdapter->GetParent(__uuidof(IDXGIFactory1), &dxgiFactory));

Direct3D 12

请注意,这整个区域已被 Direct3D 12 清理干净,您无法使用 QI 获得 IDXGIDevice。来自ID3D12Device .

引用资料

DirectX Graphics Infrastructure (DXGI): Best Practices
Anatomy of Direct3D 11 Create Device

关于c++ - 为什么对设备和交换链使用相同的 IDXGIFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357744/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com