DirectX 12设备在创建后立即暂停

时间:2016-02-13 12:49:20

标签: directx directx-12

创建DirectX 12命令队列时出现奇怪的错误。 其他DX12应用程序可以在同一台机器上成功启动。

我的计算机使用D3D_FEATURE_LEVEL_11_0 if块。 用于测试的显卡是NVIDIA GT 740,带有361.75驱动程序

这是使用中的代码(最小化):

#include <Windows.h>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <comdef.h>
#include <D3d12sdklayers.h>
#include <string>

#pragma comment(lib,"d3d12.lib")
#pragma comment(lib,"dxgi.lib")
#pragma comment(lib,"d3dcompiler.lib")
using namespace std;

LRESULT WINAPI WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine, int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(nCmdShow);
    UNREFERENCED_PARAMETER(lpCmdLine);

    wchar_t* WindowClass = L"Papergate";
    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hInstance;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
    wc.lpszClassName = WindowClass;

    if (!RegisterClassEx(&wc))
    {
        return 1;
    }

    HWND hwnd = CreateWindowEx(NULL, wc.lpszClassName, WindowClass,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL);

    if (!hwnd)
    {
        UnregisterClass(WindowClass, hInstance);
        return 1;
    }
    ShowWindow(hwnd, SW_SHOWDEFAULT);
    UpdateWindow(hwnd);

    ID3D12Device* device;
    HRESULT result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_12_1,
        __uuidof(ID3D12Device), (void**)&device);
    if (FAILED(result))
    {
        result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_12_0,
            __uuidof(ID3D12Device), (void**)&device);
        if (FAILED(result))
        {
            result = D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_11_0,
                __uuidof(ID3D12Device), (void**)&device);
            if (FAILED(result)) {
                _com_error error(result);
                MessageBox(hwnd, error.ErrorMessage(),
                    (wstring(L"Error: ") + to_wstring(__LINE__)).c_str(),
                    MB_OK);
                return 2;
            }
        }
    }

    ID3D12Debug* debugInterface;
    if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugInterface))))
    {
        debugInterface->EnableDebugLayer();
    }

    D3D12_COMMAND_QUEUE_DESC commandQueueDesc;
    commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
    commandQueueDesc.NodeMask = 0;
    commandQueueDesc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
    commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
    ID3D12CommandQueue* commandQueue;
    result = device->CreateCommandQueue(&commandQueueDesc, __uuidof(ID3D12CommandQueue), (void**)&commandQueue);
    if (FAILED(result)) {
        _com_error error(result);
        MessageBox(hwnd, error.ErrorMessage(),
            (wstring(L"Error: ") + to_wstring(__LINE__)).c_str(), MB_OK);
        result = device->GetDeviceRemovedReason();
        error = _com_error(result);
        MessageBox(hwnd, error.ErrorMessage(),
            (wstring(L"Error: ") + to_wstring(__LINE__)).c_str(), MB_OK);
        debugInterface->Release(); device->Release(); return 2;
    }

    MSG msg;
    ZeroMemory(&msg, sizeof(MSG));
    while (GetMessage(&msg, NULL, 0, 0) && msg.message != WM_QUIT)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    commandQueue->Release();
    device->Release();
    UnregisterClass(WindowClass, hInstance);
    return 0;
}

我在第97行和第102行分别遇到以下错误:

The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action.

第二个错误:

The GPU will not respond to more commands, most likely because some other application submitted invalid commands.
The calling application should re-create the device and continue.

1 个答案:

答案 0 :(得分:1)

这似乎很可能是某种驱动程序错误。检查是否有更新的硬件驱动程序。您应该尝试使用此VSIX中的Direct3D12游戏模板,看看它们是否遇到同样的问题(有关模板的详细信息,请参阅this blog post)。

对于各种功能级别调用D3D12CreateDevice的级联模式是不常见的,不是必需的。如果您的应用程序可以在Direct3D功能级别11.0或更高版本上运行,那么只需使用D3D_FEATURE_LEVEL_11_0一次。您应该将最小支持的功能级别传递给此功能。

如果Direct3D 12设备确实支持更高的功能级别,您可以通过检查各个功能或使用D3D12_FEATURE_FEATURE_LEVELS {/ 3>}来CheckFeatureSupport发现该功能。

// Create the DX12 API device object.
DX::ThrowIfFailed(D3D12CreateDevice(
    adapter.Get(),
    m_d3dMinFeatureLevel,
    IID_PPV_ARGS(&m_d3dDevice)
    ));

// Determine maximum supported feature level for this device
static const D3D_FEATURE_LEVEL s_featureLevels[] =
{
    D3D_FEATURE_LEVEL_12_1,
    D3D_FEATURE_LEVEL_12_0,
    D3D_FEATURE_LEVEL_11_1,
    D3D_FEATURE_LEVEL_11_0,
};

D3D12_FEATURE_DATA_FEATURE_LEVELS featLevels =
{
    _countof(s_featureLevels), s_featureLevels, D3D_FEATURE_LEVEL_11_0
};

HRESULT hr = m_d3dDevice->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS,
    &featLevels, sizeof(featLevels));
if (SUCCEEDED(hr))
{
    m_d3dFeatureLevel = featLevels.MaxSupportedFeatureLevel;
}
else
{
    m_d3dFeatureLevel = m_d3dMinFeatureLevel;
}

请注意,D3D_FEATURE_LEVEL_12_0D3D_FEATURE_LEVEL_12_1基本上只是D3D_FEATURE_LEVEL_11_1,并且必须使用一些可选功能。如果您的应用程序已经在11.x检查它们,则没有理由“要求”12.0或12.1。请参阅MSDN

绝大多数Direct3D 12游戏&amp;应用程序,D3D_FEATURE_LEVEL_11_0D3D_FEATURE_LEVEL_11_1是不错的选择。请记住,虽然AMD / ATI很早就支持功能级别11.1,但NVIDIA DirectX 11部件仅支持11.0,并且在一段时间内支持一些可选功能。