尝试在directx11中渲染三角形后,Windows冻结

时间:2016-06-04 12:15:50

标签: c++ visual-studio directx directx-11

我是directx编程的新手,所以我开始使用dx11在线学习一些教程。 我可以初始化win32窗口和directx11罚款,但当我尝试绘制三角形窗口出现然后它冻结我的电脑所以我必须重新启动它。我搜索了解决方案,但没有任何帮助。

这是我的WinMain代码:

#include "DXApp.h"
#include<DirectXMath.h>


class App : public DXApp {
public:
App(HINSTANCE hInstance);
~App();

bool Init() override;
void Update(float dt) override;
void Render(float dt) override;

};

int WINAPI WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in LPSTR lpCmdLine, __in int nShowCmd) {
App app(hInstance);

if (!app.Init()) return 1;

return app.Run();
}

App::App(HINSTANCE hInstance) : DXApp(hInstance)
{
}

App::~App()
{
}

bool App::Init()
{
return DXApp::Init();
}

void App::Update(float dt)
{
}

    void App::Render(float dt)
{
    immediateContext->ClearRenderTargetView(renderTargetView, DirectX::Colors::CornflowerBlue);
    immediateContext->Draw(3, 0);
    swapChain->Present(0, 0);
}

And my app header and cpp:

DXApp.h:

#pragma once
#include<Windows.h>
#include<string>
#include"DXUtil.h"
#include<d3dcompiler.h>
#pragma comment(lib, "d3dcompiler.lib")
#define WIN32_LEAN_AND_MEAN

class DXApp
{
public:
DXApp(HINSTANCE hInstance);
void cleanUp();
virtual ~DXApp(void);

//MAIN APPLICATION LOOP

int Run();

//FRAMEWORK METHODS

virtual bool Init();
virtual void Update(float dt) = 0;
virtual void Render(float dt) = 0;
virtual LRESULT MsgProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam);

protected:

HWND            hAppWnd;
HINSTANCE       hAppInstance;
unsigned int    ClientWidth;
unsigned int    ClientHeight;
std::string     AppTitle;
DWORD           WindStyle;

//DIRECTX ATTRIBUTES

ID3D11Device*               device;
ID3D11DeviceContext*        immediateContext;
IDXGISwapChain*             swapChain;
ID3D11RenderTargetView*     renderTargetView;
D3D_DRIVER_TYPE             driverType;
D3D_FEATURE_LEVEL           featureLevel;
D3D11_VIEWPORT              viewport;

ID3D11Buffer*               triangleVertBuffer;
ID3D11PixelShader*          pixelShader;
ID3D11VertexShader*         vertexShader;
ID3D10Blob*                 VSBuffer;
ID3D10Blob*                 PSBuffer;
ID3D11InputLayout*          vertLayout;

protected:

//INITIALZE WIN32 WINDOW

bool windowInit();

//INITIALIZE DIRECTX

bool direct3dInit();
};

DXApp.cpp:

#pragma once
#include<Windows.h>
#include<string>
#include"DXUtil.h"
#include<d3dcompiler.h>
#pragma comment(lib, "d3dcompiler.lib")
#define WIN32_LEAN_AND_MEAN

class DXApp
{
public:
DXApp(HINSTANCE hInstance);
void cleanUp();
virtual ~DXApp(void);

//MAIN APPLICATION LOOP

int Run();

//FRAMEWORK METHODS

virtual bool Init();
virtual void Update(float dt) = 0;
virtual void Render(float dt) = 0;
virtual LRESULT MsgProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam);

protected:

HWND            hAppWnd;
HINSTANCE       hAppInstance;
unsigned int    ClientWidth;
unsigned int    ClientHeight;
std::string     AppTitle;
DWORD           WindStyle;

//DIRECTX ATTRIBUTES

ID3D11Device*               device;
ID3D11DeviceContext*        immediateContext;
IDXGISwapChain*             swapChain;
ID3D11RenderTargetView*     renderTargetView;
D3D_DRIVER_TYPE             driverType;
D3D_FEATURE_LEVEL           featureLevel;
D3D11_VIEWPORT              viewport;

ID3D11Buffer*               triangleVertBuffer;
ID3D11PixelShader*          pixelShader;
ID3D11VertexShader*         vertexShader;
ID3D10Blob*                 VSBuffer;
ID3D10Blob*                 PSBuffer;
ID3D11InputLayout*          vertLayout;

protected:

//INITIALZE WIN32 WINDOW

bool windowInit();

//INITIALIZE DIRECTX

bool direct3dInit();
};

感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

调试Direct3D程序的第一步是:

(1)确保正确检查所有HRESULT值。您的代码中有许多地方无法检查结果。如果Direct3D函数返回void,则可以忽略错误检查。否则,您需要使用video.pause(); video.currentTime = 0; video.play(); SUCCEEDED或使用ThrowIfFailed之类的内容。这主要是因为继续失败将导致调试问题的真正原因变得非常困难。

(2)启用Direct3D调试设备并查找输出。您的调试版本中似乎有这个。你在调试窗口看到任何输出吗?

让一个程序“挂起”你的系统是非常不寻常的,但是如果你有一个坏的驱动程序或有故障的硬件,它是可能的。但是,如果没有更多详细信息,则很难诊断,因为您的程序开始时是错误的。