0

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?

KeyC0de
  • 4,728
  • 8
  • 44
  • 68
  • You get the backbuffer like this right? ID3D11Texture2D* texture; mSwapChain->GetBuffer(0,__uuidof(ID3D11Texture2D),(void**)(&texture)); – mrvux Sep 17 '13 at 02:18

2 Answers2

2

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);
    }
zdd
  • 8,258
  • 8
  • 46
  • 75
-1

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;
lancery
  • 658
  • 1
  • 6
  • 18
  • 1
    It is perfectly allowed to use NULL for the second parameter. If you do so your view will be allowed to access the whole resource. – mrvux Sep 17 '13 at 02:15