初始化Driect3D类:程序员
#include "Common\d3dApp.h" #include <DirectXColors.h> using namespace DirectX; class InitDirect3DApp : public D3DApp { public: InitDirect3DApp(HINSTANCE hInstance); ~InitDirect3DApp(); virtual bool Initialize()override; private: virtual void OnResize()override; virtual void Update(const GameTimer& gt)override; virtual void Draw(const GameTimer& gt)override; }; InitDirect3DApp::InitDirect3DApp(HINSTANCE hInscance) :D3DApp(hInscance) { } InitDirect3DApp::~InitDirect3DApp() { } bool InitDirect3DApp::Initialize() { if (!D3DApp::Initialize()) { return false; } return true; } void InitDirect3DApp::OnResize() { D3DApp::OnResize(); } void InitDirect3DApp::Update(const GameTimer& gt) { } void InitDirect3DApp::Draw(const GameTimer& gt) { //重置命令分配器 ThrowIfFailed(mDirectCmdListAlloc->Reset()); //重置命令列表 ThrowIfFailed(mCommandList->Reset(mDirectCmdListAlloc.Get(), nullptr)); //对资源的状态进行转变,将资源从呈现状态转变到渲染目标状态 mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition( CurrentBackBuffer(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET )); //设置视口和裁剪矩形,他们须要跟随命令列表的重置而重置 mCommandList->RSSetViewports(1, &mScreenViewport); mCommandList->RSSetScissorRects(1, &mScissorRect); //清除后台缓冲区和深度缓冲区 mCommandList->ClearRenderTargetView(CurrentBackBufferView(), Colors::LightSteelBlue, 0, nullptr); mCommandList->ClearDepthStencilView(DepthStencilView(), D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr); //指定将要渲染的缓冲区 mCommandList->OMSetRenderTargets(1, &CurrentBackBufferView(), true, &DepthStencilView()); //再次对资源状态进行转变,将资源从渲染目标状态转变为呈现状态 mCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition( CurrentBackBuffer(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT )); //完成命令的记录 ThrowIfFailed(mCommandList->Close()); //将命令列表的命令加入到命令队列中 ID3D12CommandList* cmdsList[] = { mCommandList.Get() }; mCommandQueue->ExecuteCommandLists(_countof(cmdsList), cmdsList); //交换先后台缓冲区 ThrowIfFailed(mSwapChain->Present(0, 0));; mCurrBackBuffer = (mCurrBackBuffer + 1) % SwapChainBufferCount; //等待此帧的命令执行完毕 FlushCommandQueue(); }
主函数:数组
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd) { //为调试版本开启运行时内存检测,方便监督内存泄漏的状况 #if defined(DEBUG) | defined(_DEBUG) _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif try { InitDirect3DApp theApp(hInstance); if (!theApp.Initialize()) { return 0; } return theApp.Run(); } catch (DxException& e) { MessageBox(nullptr, e.ToString().c_str(), L"HR Failed", MB_OK); return 0; } }
运行结果ide
接下来咱们将介绍一些在前面没有提到的方法:函数
一、ClearRenderTargetView():将指定的渲染目标清理为给定的颜色工具
二、ClearDepthStencilView():清理指定的深度/模板缓冲区性能
三、OMSetRenderTargets():设置咱们但愿在渲染流水线上使用的渲染目标和深度/模板缓冲区动画
大多数的Direct3D函数会返回HRESULT错误码,咱们的示例程序则采用简单的错误处理机制检测返回的HRESULT值,若是检测失败。则抛出异常,显示调用出错的错误码,函数名,文件名以及发生错误的行号,这些操做都由d3dUtil.h中的代码实现:spa
class DxException { public: DxException() = default; DxException(HRESULT hr, const std::wstring& functionName, const std::wstring& filName, int lineNumber); std::wstring ToString()const; HRESULT ErrorCode = S_OK; std::wstring FunctionName; std::wstring FileName; int LineNumber = -1; }; #ifndef ThrowIfFailed #define ThrowIfFailed(x) \ { \ HRESULT hr__ = (x); \ std::wstring wfn = AnsiToWString(__FILE__); \ if(FAILED(hr__)) { throw DxException(hr__, L#x, wfn, __LINE__); } \ } #endif
由上面的代码能够看出来,ThrowifFailed是一个宏而不是一个函数,若是ThrowifFailed是一个函数,那么__FILE__和__LINE__将会定位到ThrowifFailed函数所在的文件和行,而不是出错函数的文件和行。3d
L#x会将ThrowifFailed的参数转换为Unicode字符串,这样一来,咱们就能够将函数调用的错误信息输出到消息框中。调试