Question

I have a DirectX10 + C++ problem.

Basically we're at the early stages of rendering, and for some reason our depth stencil seems to be failing to understand our model. Basically, here is everything we are doing:

  1. Load shader, model and texture
  2. Initialize DirectX
  3. Draw

The model, shader and texture all load and work correctly, however (as shown in the screenshot below), the depth stencil is clearly not doing its job and the shader is being used in the wrong places. I have also included our initialization method in case you need it to figure it out. We believe we have tried almost everything but knowing our luck we have probably missed out 1 line of important code ^.^

We also saw that someone else had the same problem, however their fix didn't work (their problem was that they had set the near clipping plane to 0.0, however ours is not 0.0 so that is not the problem)

Thanks in advance!

Problem screenshot

void GraphicsDeviceDirectX::InitGraphicsDevice(HWND hWnd)
{
    DXGI_SWAP_CHAIN_DESC scd;    // create a struct to hold various swap chain information

    ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));    // clear out the struct for use

    scd.BufferCount = 2;    // create two buffers, one for the front, one for the back
    scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
    scd.BufferDesc.Height = 600;
    scd.BufferDesc.Width = 600;
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // tell how the chain is to be used
    scd.OutputWindow = hWnd;    // set the window to be used by Direct3D
    scd.SampleDesc.Count = 1;    // set the level of multi-sampling
    scd.SampleDesc.Quality = 0;    // set the quality of multi-sampling
    scd.Windowed = true;    // set to windowed or full-screen mode

    //set scan line ordering and scaling
    scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    //discard back buffer dontents
    scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    //dont set advanced flags
    scd.Flags = 0;

    // create a device class and swap chain class using the information in the scd struct
    if(FAILED(D3D10CreateDeviceAndSwapChain(NULL,
                                  D3D10_DRIVER_TYPE_HARDWARE,
                                  NULL,
                                  D3D10_CREATE_DEVICE_DEBUG,
                                  D3D10_SDK_VERSION,
                                  &scd,
                                  &swapchain,
                                  &device)))
    {
        throw EngineException("Error creating graphics device");
    }

    //Push graphics device to Persistant Object Manager
    //PerObjMan::Push(device);
    //Push swapchain to Peristant Object Manager
    PerObjMan::Push(swapchain);

    // get the address of the back buffer and use it to create the render target
    ID3D10Texture2D* pBackBuffer;
    swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
    device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);

    /*D3D10_TEXTURE2D_DESC descBack;
    pBackBuffer->GetDesc(&descBack);*/
    pBackBuffer->Release();
    pBackBuffer = NULL;

    //Push graphics device to Persistant Object Manager
    PerObjMan::Push(rtv);

    ID3D10Texture2D* pDepthStencil = NULL;
    D3D10_TEXTURE2D_DESC descDepth;

    ZeroMemory(&descDepth, sizeof(descDepth));

    descDepth.Width = 600;
    descDepth.Height = 600;
    descDepth.MipLevels = 1;
    descDepth.ArraySize = 1;
    descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    descDepth.SampleDesc.Count = 1;
    descDepth.SampleDesc.Quality = 0;
    descDepth.Usage = D3D10_USAGE_DEFAULT;
    descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
    descDepth.CPUAccessFlags = 0;
    descDepth.MiscFlags = 0;
    HRESULT hr;
    hr = GetGraphicsDevice()->CreateTexture2D( &descDepth, NULL, &pDepthStencil );
    if(FAILED(hr))
        throw EngineException("FAIL");

    PerObjMan::Push(pDepthStencil);

    D3D10_DEPTH_STENCIL_DESC dsDesc;

    ZeroMemory(&dsDesc, sizeof(dsDesc));
    // Depth test parameters
    dsDesc.DepthEnable = true;
    dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK::D3D10_DEPTH_WRITE_MASK_ALL;
    dsDesc.DepthFunc = D3D10_COMPARISON_FUNC::D3D10_COMPARISON_LESS;

    // Stencil test parameters
    dsDesc.StencilEnable = false;
    dsDesc.StencilReadMask = 0xFF;
    dsDesc.StencilWriteMask = 0xFF;

    // Stencil operations if pixel is front-facing.
    dsDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
    dsDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
    dsDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
    dsDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

    // Stencil operations if pixel is back-facing.
    dsDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
    dsDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
    dsDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
    dsDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;      

    // Create depth stencil state
    hr = device->CreateDepthStencilState(&dsDesc, &dss);
    if(FAILED(hr))
        throw EngineException("FAIL");

    // Bind depth stencil state
    device->OMSetDepthStencilState(dss, 1);


    PerObjMan::Push(dss);

    D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;

    ZeroMemory(&descDSV, sizeof(descDSV));

    descDSV.Format = descDepth.Format;
    descDSV.ViewDimension = D3D10_DSV_DIMENSION::D3D10_DSV_DIMENSION_TEXTURE2D;
    descDSV.Texture2D.MipSlice = 0;

    // Create the depth stencil view
    hr = device->CreateDepthStencilView( pDepthStencil, // Depth stencil texture
                                         &descDSV, // Depth stencil desc
                                         &dsv );  // [out] Depth stencil view

    if(FAILED(hr))
        throw EngineException("FAIL");

    PerObjMan::Push(dsv);

    // Bind the depth stencil view
    device->OMSetRenderTargets( 1,          // One rendertarget view
                                &rtv,      // Render target view, created earlier
                                dsv);     // Depth stencil view for the render target


    D3D10_VIEWPORT viewport;    // create a struct to hold the viewport data

    ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));    // clear out the struct for use

    GameToImplement::GameInfo::Info info = GameToImplement::GameInfo::GetGameInfo();

    RECT rect;
    int width = 0;
    int height = 0;
    if(GetClientRect(hWnd, &rect))
    {
        width = rect.right - rect.left;
        height = rect.bottom - rect.top;
    }
    else
    {
        throw EngineException("");
    }

    viewport.TopLeftX = 0;    // set the left to 0
    viewport.TopLeftY = 0;    // set the top to 0
    viewport.Width = 600;    // set the width to the window's width
    viewport.Height = 600;    // set the height to the window's height
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;

    device->RSSetViewports(1, &viewport);    // set the viewport

}
Was it helpful?

Solution

I fixed it, thanks to catflier's nod in the right direction. Turns out I was actually releasing the rasterizer state too early for the depth stencil to be used.

I'll leave this answer here for anyone who has the same problem.

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