directx11简单的应用程序崩溃

时间:2014-07-11 17:36:22

标签: c++ directx-11

我正在开发一个directx11应用程序,出于某种原因,我的简单应用程序因访问冲突错误而崩溃。某些东西导致它崩溃,然后我的图形驱动程序导致重置。

这是我的初始化:

DirectXRendererImpl::DirectXRendererImpl(const EngineSettings& settings, Logger& logger) : mLogger(logger), mWindowHandle(GetActiveWindow()), mSwapchain(nullptr), mBackbuffer(nullptr), mDevice(nullptr), mContext(nullptr),
    mForwardVertexShader(nullptr), mForwardPixelShader(nullptr), mVertexBuffer(nullptr), mInputLayout(nullptr)
{
    // create swapchain, device and devicecontext
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
    swapChainDesc.BufferCount = 2;
    swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.OutputWindow = mWindowHandle;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.Windowed = true;

    const D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
    const UINT numFeatureLevels = 1;

    HRESULT result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, &featureLevel, numFeatureLevels, D3D11_SDK_VERSION, &swapChainDesc, &mSwapchain, &mDevice, NULL, &mContext);
    if (result != S_OK)
    {
        JONS_LOG_ERROR(mLogger, "DirectXRenderer::DirectXRenderer(): D3D11CreateDeviceAndSwapChain failed: code " + result);
        throw std::runtime_error("DirectXRenderer::DirectXRenderer(): D3D11CreateDeviceAndSwapChain failed: code " + result);
    }

    // backbuffer rendertarget setup
    ID3D11Texture2D* backbuffer = nullptr;
    mSwapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backbuffer);

    mDevice->CreateRenderTargetView(backbuffer, NULL, &mBackbuffer);
    backbuffer->Release();

    mContext->OMSetRenderTargets(1, &mBackbuffer, NULL);

    // setup viewport
    // query width/height from d3d
    ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
    mSwapchain->GetDesc(&swapChainDesc);

    D3D11_VIEWPORT viewport;
    ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = static_cast<float>(swapChainDesc.BufferDesc.Width);
    viewport.Height = static_cast<float>(swapChainDesc.BufferDesc.Height);
    viewport.MinDepth = 0.0f;
    viewport.MaxDepth = 1.0f;

    mContext->RSSetViewports(1, &viewport);

    // create shader objects
    mDevice->CreateVertexShader(gForwardVertexShader, sizeof(gForwardVertexShader), NULL, &mForwardVertexShader);
    mDevice->CreatePixelShader(gForwardPixelShader, sizeof(gForwardPixelShader), NULL, &mForwardPixelShader);

    mContext->VSSetShader(mForwardVertexShader, NULL, NULL);
    mContext->PSSetShader(mForwardPixelShader, NULL, NULL);

    // fill vertex buffer
    VERTEX OurVertices[] =
    {
        { 0.0f, 0.5f, 0.0f },
        { 0.45f, -0.5, 0.0f},
        { -0.45f, -0.5f, 0.0f }
    };

    D3D11_BUFFER_DESC bufferDescription;
    ZeroMemory(&bufferDescription, sizeof(D3D11_BUFFER_DESC));
    bufferDescription.Usage = D3D11_USAGE_DEFAULT;
    bufferDescription.ByteWidth = sizeof(OurVertices);
    bufferDescription.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    bufferDescription.CPUAccessFlags = 0;
    bufferDescription.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA initData;
    ZeroMemory(&initData, sizeof(D3D11_SUBRESOURCE_DATA));
    initData.pSysMem = OurVertices;

    mDevice->CreateBuffer(&bufferDescription, &initData, &mVertexBuffer);

    D3D11_INPUT_ELEMENT_DESC inputDescription;
    ZeroMemory(&inputDescription, sizeof(D3D11_INPUT_ELEMENT_DESC));
    inputDescription.SemanticName = "POSITION";
    inputDescription.SemanticIndex = 0;
    inputDescription.Format = DXGI_FORMAT_R32G32B32_FLOAT;
    inputDescription.InputSlot = 0;
    inputDescription.AlignedByteOffset = 0;
    inputDescription.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
    inputDescription.InstanceDataStepRate = 0;

    mDevice->CreateInputLayout(&inputDescription, 1, gForwardVertexShader, sizeof(gForwardVertexShader), &mInputLayout);

    mContext->IASetInputLayout(mInputLayout);

    // register as window subclass to listen for WM_SIZE events. etc
    if (!SetWindowSubclass(mWindowHandle, WndProc, gSubClassID, 0))
    {
        JONS_LOG_ERROR(mLogger, "DirectXRenderer::DirectXRenderer(): SetWindowSubclass() failed");
        throw std::runtime_error("DirectXRenderer::DirectXRenderer(): SetWindowSubclass() failed");
    }

    gDirectXRendererImpl = this;
}

简单的顶点着色器:

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut main(float4 position : POSITION)
{
    VOut output;

    output.position = position;
    output.color = float4(1.0, 0.0, 0.0, 0.0);

    return output;
}

简单像素着色器:

float4 main(float4 position : POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}

渲染功能就是这样:

void DirectXRendererImpl::Render(const RenderQueue& renderQueue, const RenderableLighting& lighting, const DebugOptions::RenderingMode debugMode, const DebugOptions::RenderingFlags debugExtra)
{
    const FLOAT clearColor[4] = { 1.0f, 1.0f, 0.0f, 1.0f };
    mContext->ClearRenderTargetView(mBackbuffer, clearColor);

    uint32_t vertexSize = sizeof(VERTEX);
    uint32_t offset = 0;
    mContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &vertexSize, &offset);
    mContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    mContext->Draw(3, 0);

    mSwapchain->Present(0, 0);
}

该应用程序运行几秒钟,然后重置完整的视频驱动程序。我的应用程序是否泄漏内存或导致此问题?

编辑:更具体地说,这是错误:

First-chance exception at 0x0F69974F (nvwgf2um.dll) in ExampleGame.exe: 0xC0000005: Access violation writing location 0x00193000.
Unhandled exception at 0x0F69974F (nvwgf2um.dll) in ExampleGame.exe: 0xC0000005: Access violation writing location 0x00193000.

1 个答案:

答案 0 :(得分:0)

你必须使用这个->

bufferDescription.ByteWidth = sizeof(VERTEX) * sizeof(OurVertices); 否则程序会崩溃。

相关问题