DirectX CreateRenderTargetView not properly initialized

1.9k Views Asked by At

For some reason I seem to be unable to initialize my RenderTargetView (it stays NULL) which causes an access violation.

Here is the line that should initialize the RenderTargetView:

hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);

pBackBuffer is the Back buffer and it gets a value, it isn't NULL. However, the rendertagetview stays NULL throughout the process. Any idea why?

2

There are 2 best solutions below

2
On

In order to trace the DirectX11 errors, you'd better to create the D3D11 device with the debug layer, it will print the error message to output window in Visual Studio when you launch your app.

    // 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
On

I think you are failing to create the render target view because the second parameter is 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
);

You can initialize it to something like this:

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