D3D11-OBS初始化代码
2024-05-11
21
0
#include <Windows.h>
#include <d3d9.h>
#include <d3dkmthk.h>
#include <dxgi.h>
#include <d3d11.h>
#include <stdio.h>
#include <string>
#pragma comment(lib,"DXGI.lib")
#pragma comment(lib,"d3d11.lib")
class gs_device
{
IDXGIFactory1* factory;
IDXGIAdapter1* adapter;
ID3D11Device* device;
ID3D11DeviceContext* context;
DWORD adpIdx = 0;
private:
bool nv12Supported;
bool p010Supported;
public:
void InitD3D11();
void InitFactory(DWORD adapterIdx);
void InitAdapter(DWORD adapterIdx);
void InitDevice(DWORD adapterIdx);
};
void gs_device::InitD3D11()
{
InitFactory(adpIdx);
InitAdapter(adpIdx);
InitDevice(adpIdx);
}
void gs_device::InitFactory(DWORD adapterIdx)
{
//创建可用于生成其他 DXGI 对象的 DXGI 1.1 工厂
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory));
if (FAILED(hr))
{
printf("CreateDXGIFactory1 err\n");
}
}
void gs_device::InitAdapter(DWORD adapterIdx)
{
//当 EnumAdapters1 方法成功并用指向适配器接口的指针的地址填充 ppAdapter 参
HRESULT hr = factory->EnumAdapters1(adapterIdx, &adapter);
if (FAILED(hr))
{
printf("InitAdapter err\n");
}
}
static bool CheckFormat(ID3D11Device* device, DXGI_FORMAT format)
{
constexpr UINT required = D3D11_FORMAT_SUPPORT_TEXTURE2D |
D3D11_FORMAT_SUPPORT_RENDER_TARGET;
UINT support = 0;
return SUCCEEDED(device->CheckFormatSupport(format, &support)) &&
((support & required) == required);
}
void gs_device::InitDevice(DWORD adapterIdx)
{
HRESULT hr;
DXGI_ADAPTER_DESC desc;
adapter->GetDesc(&desc);
LARGE_INTEGER umd;
hr = adapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &umd);
if (SUCCEEDED(hr))
LONGLONG driverVersion = umd.QuadPart;
//Creates a device that represents the display adapter.
//创建表示显示适配器的设备。
DWORD createFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
const static D3D_FEATURE_LEVEL featureLevels[] = {
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,};
D3D_FEATURE_LEVEL levelUsed = D3D_FEATURE_LEVEL_10_0;
hr = D3D11CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, NULL,
D3D11_CREATE_DEVICE_BGRA_SUPPORT, featureLevels,
sizeof(featureLevels) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION, &device, &levelUsed,
&context);
if (FAILED(hr))
{
printf("D3D11CreateDevice err\n");
}
/* needs to support extended resource sharing */
D3D11_FEATURE_DATA_D3D11_OPTIONS opts = {};
hr = device->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &opts, sizeof(opts));
if (FAILED(hr) || !opts.ExtendedResourceSharing)
{
return;
}
nv12Supported = CheckFormat(device, DXGI_FORMAT_NV12);
p010Supported = nv12Supported && CheckFormat(device, DXGI_FORMAT_P010);
}