菜鸟学习DirctX12(2)--基础及初始化

                                           第一 基础知识

1、Direct3D 框架基于win32程序
Win32程序:在C语言的基础上,使用Win32API开发的程序。
菜鸟学习DirctX12(2)--基础及初始化_第1张图片


2、DirectX

菜鸟学习DirctX12(2)--基础及初始化_第2张图片



菜鸟学习DirctX12(2)--基础及初始化_第3张图片



3、初始化代码
添加D3DX12.h文件,D3DX12.h文件从官网可以下载



directx12.h   也可以去我的网盘下载 http://pan.baidu.com/s/1o7czULs
点击打开链接

main.cpp代码如下



#include
#include
#include "D3DX12.h"
#include
#include
#include
#include
#pragma comment(lib, "d3d12.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
using namespace DirectX;
using Microsoft::WRL::ComPtr;


HWND m_hwnd;
HINSTANCE m_hInstance;
int cNumSwapBufs = 2;
bool m_useWarpDevice;


static const UINT FrameCount = 2;


// Pipeline objects.
ComPtr m_swapChain;
ComPtr m_device;
ComPtr m_renderTargets[FrameCount];
ComPtr m_commandAllocator;
ComPtr m_commandQueue;
ComPtr m_rtvHeap;
ComPtr m_pipelineState;
ComPtr m_commandList;
UINT m_rtvDescriptorSize;
ComPtr m_rootSignature;


UINT m_frameIndex;
HANDLE m_fenceEvent;
ComPtr m_fence;
UINT  m_fenceValue;


void OnInit();
void LoadPipeline();
void LoadAssets();
void  OnDestroy();
void  OnRender();
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pScmdline, int iCmdshow)
{


WNDCLASSEX windowClass = { 0 };
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.hInstance = hInstance;
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.lpszClassName = L"Directx12";


if (!RegisterClassEx(&windowClass))
{
return E_FAIL;
}


RECT windowRect = { 0, 0, 600,480 };
AdjustWindowRect(&windowRect, WS_OVERLAPPEDWINDOW, FALSE);


m_hwnd = CreateWindow(
windowClass.lpszClassName,
L"2DirectX12",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
nullptr, // We have no parent window.
nullptr, // We aren't using menus.
m_hInstance,
NULL);


if (!m_hwnd)
{
return E_FAIL;
}


OnInit();




MSG msg = {};
while (msg.message != WM_QUIT)
{
// Process any messages in the queue.
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
OnRender();
}
}


OnDestroy();




return (int)msg.wParam;




}
void GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
{
ComPtr adapter;
*ppAdapter = nullptr;


for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex)
{
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);


if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
{
// Don't select the Basic Render Driver adapter.
// If you want a software adapter, pass in "/warp" on the command line.
continue;
}


// Check to see if the adapter supports Direct3D 12, but don't create the
// actual device yet.
if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
{
break;
}
}


*ppAdapter = adapter.Detach();
}


void OnInit()
{


LoadPipeline();
LoadAssets();
ShowWindow(m_hwnd, SW_SHOW);
}
void LoadPipeline()
{




#if defined (_DEBUG)
{
ComPtr debugController;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
{
debugController->EnableDebugLayer();
}
}
#endif // DEBUG




ComPtr factory;


if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))))
{
throw;
}
if (m_useWarpDevice)
{
ComPtrwarpAdapter;
if (FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter))))
{
throw;
}
if (FAILED(D3D12CreateDevice(warpAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_device))))
{
throw;
}
}
else
{
ComPtrhardwareAdapter;
GetHardwareAdapter(factory.Get(), &hardwareAdapter);


if (FAILED(D3D12CreateDevice(hardwareAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_device))))
{
throw;
}
}


D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
if (FAILED(m_device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue))))
{
throw;


}
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount = cNumSwapBufs;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
swapChainDesc.OutputWindow = m_hwnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.Windowed = true;
ComPtr swapChain;
if (FAILED(factory->CreateSwapChain(m_commandQueue.Get(), &swapChainDesc, &swapChain)))
{
throw;
}
if (FAILED(swapChain.As(&m_swapChain)))
{
throw;
}
if (FAILED(factory->MakeWindowAssociation(m_hwnd, DXGI_MWA_NO_ALT_ENTER)))
{
throw;
}
m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();


{
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
rtvHeapDesc.NumDescriptors = FrameCount;
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
if (FAILED(m_device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap))))
{
throw;
}
m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);


}
{
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
for (UINT n = 0; n < FrameCount; n++)
{
if (FAILED(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n]))))
{
throw;
}
m_device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
rtvHandle.Offset(1, m_rtvDescriptorSize);


}




}


if (FAILED(m_device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator))))
{
throw;
}




}




void LoadAssets()
{
if (FAILED(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(&m_commandList))))
{
throw;
}


// Command lists are created in the recording state, but there is nothing
// to record yet. The main loop expects it to be closed, so close it now.
if (FAILED(m_commandList->Close()))
{
throw;
}


// Create synchronization objects.
{
if (FAILED(m_device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_fence))))
{
throw;
}


m_fenceValue = 1;


// Create an event handle to use for frame synchronization.
m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (m_fenceEvent == nullptr)
{
if (FAILED(HRESULT_FROM_WIN32(GetLastError())))
{
throw;
}


}
}




}


void  OnUpdate()
{
}


// Render the scene.
void  OnRender()
{
if (FAILED(m_commandAllocator->Reset()))
{
throw;
}
if (FAILED(m_commandList->Reset(m_commandAllocator.Get(), m_pipelineState.Get())))
{
throw;
}
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));


CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);


const float clearColor[] = { 0.0f,2.0f,0.4f,1.0f };
m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
if (FAILED(m_commandList->Close()))
{
throw;
}


ID3D12CommandList *ppCommandLists[] = { m_commandList.Get() };
m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);


if (m_swapChain->Present(1, 0))
{
throw;
}
const UINT fence = m_fenceValue;
if (FAILED(m_commandQueue->Signal(m_fence.Get(), fence)))
{
throw;
}
if (m_fence->GetCompletedValue() < fence)
{
if (FAILED(m_fence->SetEventOnCompletion(fence, m_fenceEvent)))
{
throw;
}
WaitForSingleObject(m_fenceEvent, INFINITE);
}
m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();
}


void  OnDestroy()
{
const UINT64 fence = m_fenceValue;
if (FAILED(m_commandQueue->Signal(m_fence.Get(), fence)))
{
throw;
}
if (m_fence->GetCompletedValue() < fence)
{
if (FAILED(m_fence->SetEventOnCompletion(fence, m_fenceEvent)))
{
throw;
}
WaitForSingleObject(m_fenceEvent, INFINITE);
}
m_frameIndex = m_swapChain->GetCurrentBackBufferIndex();


CloseHandle(m_fenceEvent);
}


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{


switch (message)
{
case WM_PAINT:


OnUpdate();
OnRender();
return 0;


case WM_DESTROY:


PostQuitMessage(0);
return 0;
}


// Handle any messages the switch statement didn't.
return DefWindowProc(hWnd, message, wParam, lParam);
}





























你可能感兴趣的:(DirectX12)