DirectX应用程序导致计算机崩溃

时间:2012-11-13 10:07:35

标签: crash directx direct3d directx-11 d3dx

我已经编写了一台DirectX11应用程序,该应用程序在带有GeForce GT 555m显卡的Alienware笔记本电脑上运行,因此我知道计算机支持它。

我遇到过这种奇怪的事情,如果笔记本电脑没有插电,我的笔记本电脑会没有任何警告,但是如果插上电源,应用程序运行正常。

我试图将其与其他DirectX11应用程序一起复制,包括DirectX附带的Samples,但是我的应用程序是唯一一个这样做的应用程序,但我使用的代码非常相似。我必须遗漏一些东西,但我不知道是什么。

这是我设置SwapChain和RenderTargetView

的代码
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

if(vsync) {
    swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
    swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
}
else {
    swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
    swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
}

swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;

if(fullscreen) {
    swapChainDesc.Windowed = false;
}
else {
    swapChainDesc.Windowed = true;
}

swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = 0;

D3D_FEATURE_LEVEL featureLevels[4] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_3, };
if (FAILED(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, featureLevels, 4, D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, &featureLevel, &deviceContext))) {
    if (!device) {
        printf("Insanity Error: Failed To Create Direct3D Device\n");
    }
    if (!swapChain) {
        printf("Insanity Error: Failed To Create Swap Chain\n");
    }
    return false;
}
else {
    switch (featureLevel) {
    case D3D_FEATURE_LEVEL_11_0:
        printf("Insanity Info: Currently Using DirectX 11.0\n");
        break;
    case D3D_FEATURE_LEVEL_10_1:
        printf("Insanity Info: Currently Using DirectX 10.1\n");
        break;
    case D3D_FEATURE_LEVEL_10_0:
        printf("Insanity Info: Currently Using DirectX 10.0\n");
        break;
    case D3D_FEATURE_LEVEL_9_3:
        printf("Insanity Info: Currently Using DirectX 9.3\n");
        break;
    default:
        printf("Insanity Info: Graphics System Unknown\n");
        break;
    }
}
if (FAILED(swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr))) {
    printf("Insanity Error: Faield To Find Back Buffer\n");
    return false;
}
if (FAILED(device->CreateRenderTargetView(backBufferPtr, 0, &renderTargetView))) {
    printf("Insanity Error: Failed To Create Render View Target\n");
    return false;
}
backBufferPtr->Release();
backBufferPtr = 0;

然后这是我设置DepthStencilState和RasterState的代码

    ZeroMemory(&depthStencilDesc, sizeof(D3D11_DEPTH_STENCIL_DESC));
depthStencilDesc.DepthEnable = false;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
if (FAILED(device->CreateDepthStencilState(&depthStencilDesc, &depthDisableStencilState))) {
    printf("Insanity Error: Failed To Create Depth State\n");
    return false;
}

deviceContext->OMSetDepthStencilState(depthEnableStencilState, 1);
this->enableDepthTesting = true;

ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = depthBufferDesc.Format;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
if (FAILED(device->CreateDepthStencilView(depthStencilBuffer, &depthStencilViewDesc, &depthStencilView))) {
    printf("Insanity Error: Failed To Create Depth View\n");
    return false;
}

rasterDesc.AntialiasedLineEnable = false;
rasterDesc.CullMode = D3D11_CULL_NONE;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D11_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = true;
rasterDesc.SlopeScaledDepthBias = 0.0f;

if (FAILED(device->CreateRasterizerState(&rasterDesc, &rasterState))) {
    printf("Insanity Error: Failed To Create Rasterizer State\n");
    return false;
}

deviceContext->RSSetState(rasterState);

如果有任何人对可能发生的事情有任何想法请帮助!

1 个答案:

答案 0 :(得分:0)

尝试使用参考设备而不是硬件设备,看看是否再次出现问题。这应该有助于将驱动程序与代码隔离开来。在您创建设备时,请使用D3D_DRIVER_TYPE_REFERENCE