0

I am new to the rendering API and I have been doing okay until now. I have been debugging a while to figure this one out and I just can't figure out why this isn't working. So I need some help.

I believe the source of the problem is here.

m_VertexShader->Release();

Because it doesn't return a HRESULT there isn't much I can't do.

Here is the code.

    #include "shader.h"

    ShaderProgram::ShaderProgram(Renderer& renderer, const char* vertShader, const char* pixShader)
    : m_Renderer(renderer), m_VertexShaderSource(vertShader), m_PixelShaderSource(pixShader)
    {
    Load();
    }

    ShaderProgram::~ShaderProgram()
    {
    m_VertexShader->Release();
    m_PixelShader->Release();
    m_InputLayout->Release();
    }

    void ShaderProgram::Load()
    {
    vector<char> vertSource = FileReader::read_file(m_VertexShaderSource);
    vector<char> fragSource = FileReader::read_file(m_PixelShaderSource);

    auto resultVert = m_Renderer.getDevice()->CreateVertexShader(vertSource.data(), vertSource.size(), nullptr, &m_VertexShader);
    auto resultFrag = m_Renderer.getDevice()->CreatePixelShader(fragSource.data(), fragSource.size(), nullptr, &m_PixelShader);

    if (resultVert != S_OK || resultFrag != S_OK)
    {
        MessageBox(nullptr, "Failed to create shader!", "Error", MB_OK);
        exit(0);
    }

    D3D11_INPUT_ELEMENT_DESC layout[]{
        { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
        { "COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }
    };

    auto result = m_Renderer.getDevice()->CreateInputLayout(layout, 2, vertSource.data(), vertSource.size(), &m_InputLayout);
    if (result != S_OK)
    {
        MessageBox(nullptr, "Could not create the input layout!", "Error", MB_OK);
        exit(0);
    }

Here is my rendering class. BTW I have just added stencil and depth testing just now and that could have to do something with the problem.

    #include "renderer.h"

    Renderer::Renderer(Window& window)
    {
    createDevice(window);
    createRenderTarget();
    createDepthStencil();
    }

    Renderer::~Renderer()
    {
    m_SwapChain->Release();
    m_Device->Release();
    m_DeviceContex->Release();
    m_RenderTargetView->Release();
    m_DepthStencilView->Release();
    }

    void Renderer::createDevice(Window& window)
    {
    DXGI_SWAP_CHAIN_DESC swapChain = { 0 };
    swapChain.BufferCount = 1;
    swapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChain.OutputWindow = window.getHandle();
    swapChain.SampleDesc.Count = 1;
    swapChain.Windowed = true;

    auto result = D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, nullptr,
        0, D3D11_SDK_VERSION, &swapChain, &m_SwapChain, &m_Device, nullptr, &m_DeviceContex);

    if (result != S_OK)
    {
        MessageBox(nullptr, "Problem with creating DX11!", "Error", MB_OK);
        exit(0);
    }
}

    void Renderer::createDepthStencil()
    {
    D3D11_TEXTURE2D_DESC depthStencilDesc;

    depthStencilDesc.Width = m_BackBuffer.Width;
    depthStencilDesc.Height = m_BackBuffer.Height;
    depthStencilDesc.MipLevels = 1;
    depthStencilDesc.ArraySize = 1;
    depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthStencilDesc.SampleDesc.Count = 1;
    depthStencilDesc.SampleDesc.Quality = 0;
    depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
    depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    depthStencilDesc.CPUAccessFlags = 0;
    depthStencilDesc.MiscFlags = 0;

    m_Device->CreateTexture2D(&depthStencilDesc, nullptr, &m_DepthStencilBuffer);
    m_Device->CreateDepthStencilView(m_DepthStencilBuffer, nullptr, &m_DepthStencilView);
    m_DepthStencilBuffer->Release();
}

    void Renderer::createRenderTarget()
    {
    ID3D11Texture2D* backBuffer;
    auto result = m_SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
    if (result != S_OK)
    {
        MessageBox(nullptr, "Failed to get the swap chain buffer!", "Error", MB_OK);
        exit(0);
    }
    m_Device->CreateRenderTargetView(backBuffer, nullptr, &m_RenderTargetView);

    backBuffer->GetDesc(&m_BackBuffer);
    backBuffer->Release();
   }

    void Renderer::beginFrame()
    {
    m_DeviceContex->OMSetRenderTargets(1, &m_RenderTargetView, m_DepthStencilView);

    auto viewport = CD3D11_VIEWPORT(0.0f, 0.0f, (float) m_BackBuffer.Width, (float) m_BackBuffer.Height);
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;
    m_DeviceContex->RSSetViewports(1, &viewport);

    float clearColor[] = { 0.25f, 0.75f, 0.8f, 1.0f };
    m_DeviceContex->ClearRenderTargetView(m_RenderTargetView, clearColor);
    m_DeviceContex->ClearDepthStencilView(m_DepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    }

//Swaps the buffer!

    void Renderer::endFrame()   
    {
       m_SwapChain->Present(1, 0);
    }

}

2
  • Are you by chance, trying to debug in release mode?
    – Brandon
    Feb 24, 2016 at 2:14
  • resultVert != S_OK is not robust. Use FAILED(resultVert) instead. You have a number of functions here which return HRESULT values you are ignoring (CreateTexture2D, CreateDepthStencilView, CreateRenderTargetView, etc.). You need to check them all for failure. Feb 25, 2016 at 7:42

2 Answers 2

0

Do you get the same error when you run it in non-debug mode? With the limited info you've given it looks like your debug symbol table is not being found. If its all fine and dandy in non-debug mode then its off to sift through the docs to find out how to load the symbol table. Not much of an answer but hope it might help a little

0

You really should move to using Microsoft::WRL::ComPtr. With raw pointers, you are likely to call Release too few or too many times, which is what happened here. See this page.

It would remove silly things like the fact that you keep a dangling pointer to m_DepthStencilView even after you release it. You don't need to make it a member variable at all since you only use it to create m_DepthStencilView.

#include <wrl/client.h>

using Microsoft::WRL::ComPtr;

...

ComPtr<ID3D11Texture2D> depthStencil;
m_Device->CreateTexture2D(&depthStencilDesc, nullptr, &depthStencil);
m_Device->CreateDepthStencilView(depthStencil.Get(), nullptr, &m_DepthStencilView);

Take a look at the Direct3D 11 Game Visual Studio templates, in particular the implementation of DeviceResources.h / DeviceResources.cpp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.