DirectX CreateRenderTargetView未正确初始化

时间:2013-08-30 11:38:59

标签: directx direct3d directx-11 d3dx

出于某种原因,我似乎无法初始化我的RenderTargetView(它保持为NULL),这会导致访问冲突。 这是应该初始化RenderTargetView的行:     hr = g_pd3dDevice-> CreateRenderTargetView(pBackBuffer,NULL,& g_pRenderTargetView); pBackBuffer是Back缓冲区,它获取一个值,它不是NULL。但是,rendertagetview在整个过程中保持为NULL。知道为什么吗?

2 个答案:

答案 0 :(得分:2)

为了跟踪DirectX11错误,您最好使用debug layer创建D3D11设备,它会在您启动应用程序时将错误消息打印到Visual Studio中的输出窗口。

    // Create device and swap chain
    HRESULT hr;
    UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined( DEBUG ) || defined( _DEBUG )
    flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif 

    // Create device and swap chain
    D3D_FEATURE_LEVEL FeatureLevelsRequested = D3D_FEATURE_LEVEL_11_0; // Use d3d11
    UINT              numLevelsRequested = 1; // Number of levels 
    D3D_FEATURE_LEVEL FeatureLevelsSupported;

    if (FAILED (hr = D3D11CreateDeviceAndSwapChain( NULL, 
        D3D_DRIVER_TYPE_HARDWARE,
        NULL,
        0,
        &FeatureLevelsRequested,
        numLevelsRequested,
        D3D11_SDK_VERSION,
        &sd_, 
        &swap_chain_,
        &d3ddevice_,
        &FeatureLevelsSupported,
        &immediate_context_)))
    {
        MessageBox(hWnd, L"Create device and swap chain failed!", L"Error", 0);
    }

答案 1 :(得分:0)

我认为您无法创建渲染目标视图,因为第二个参数是NULL:

HRESULT CreateRenderTargetView
(
  [in]   ID3D11Resource *pResource,
  [in]   const D3D11_RENDER_TARGET_VIEW_DESC *pDesc, <== You need to pass in a valid description
  [out]  ID3D11RenderTargetView **ppRTView
);

您可以将其初始化为以下内容:

D3D11_RENDER_TARGET_VIEW_DESC desc = {0};
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;