Question

I managed to build a working base for which to run a DirectX 11 application. However, while encapulating code away, I noticed that some COM pointers didn't run as intuitively expected when they were placed as protected: members of a base class.

In Engine.h (header)

class   Infinity3D{
public:
    Infinity3D();
    ~Infinity3D();

    virtual void Render();
    virtual void Update();
    virtual void D3DProcess();
    virtual void ViewPort(UINT Height, UINT Width, float MaxDepth, float MinDepth, int     TopLeftX, int TopLeftY);
    virtual void StartSwapChain(ID3D11DepthStencilView *DepthStenView);
    virtual void InitializeD3D();
    virtual void TerminateD3D();
    virtual void InitializeShader();

protected:
    IDXGISwapChain*                  Swapchain;
    ID3D11Device*                    Device;
    ID3D11DeviceContext*             DeviceContext;
    ID3D11RenderTargetView*          RenderTargetView;
    IDXGIAdapter*                    DeviceAdapter;

    ID3D11InputLayout*               InputLayout;
    ID3D11VertexShader*              VertexShader;
    ID3D11PixelShader*               PixelShader;

    ID3D11Buffer*                    D3DBuffer;
    ID3D11Buffer*                    D3DIndexBuffer;

    ID3D11DepthStencilView*          DepthStenView;
    ID3D11Texture2D*                 DepthStenBuffer;

    ID3D11Buffer*                    MatrixBuffer;

    XMMATRIX                        mWorld;
    XMMATRIX                        mView;
    XMMATRIX                        mProjection;
    XMMATRIX                        mWVP;
    HWND                            hWnd;
    UINT cWidth;
    UINT cHeight;
};

As a test, I ran only the basic functions of DirectX initialization: SwapChain,Viewport, and Rendering. (I've a separate [header] class for window creation, which shows the window successfully.)

The instance to the class(es) has been created.

Infinity3D D3DSystem; 
InfinityEngine Engine(hInstance);

When calling D3DSystem.InitializeD3D(), or any Infinity3D member function that requires a DirectX COM object, the program produces a runtime error:

VS 2012 runtime error

The functions are called as:

int InfinityEngine::Process(){
    while(WM_QUIT != msg.message){
    if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE)){
                                TranslateMessage(&msg);
                                DispatchMessage(&msg);
                            }
                            else{
                                D3DSystem.Update();
                                D3DSystem.Render();
                            }
                        }
D3DSystem.TerminateD3D();
return msg.wParam;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nCmdShow){
    if(Engine.DisplayWindow()){D3DSystem.InitializeD3D();Engine.Process();}
}

For the sake of space, I'll show just the usage of COM pointers within the InitializeD3D class member.This is after DXGI_SWAP_CHAIN_DESC is instantiated and D3D11CreateDeviceAndSwapChain() is called.

Swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),(LPVOID*)&Texture2d);
Was it helpful?

Solution

Your Swapchain is nullptr, that's why your GetBuffer call fails.
This probably means that your initialization failed to create the swapchain.

All D3D "Create" functions return a value indicating success or failure, you should check those.
Also, you can turn on the debug layer, which will tell you what failed and likely why in VS' output window.
You can do this by opening the DirectX Control Panel (dxcpl.exe), adding the executable name of your program to the list, and setting it to "Force on".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top