Dx11DemoBase 基类(二) 初始化 DirectX11 的 4个基本步骤
上文說的 要用現實現象來模擬解釋 初始化 Direct3D11的過程, 呃... 暫時想不出了, 呵呵.
一般初始化 Direct3D11的過程, 分為四步:
?
bool Dx11DemoBase::Initialize( HINSTANCE hInstance, HWND hwnd) {//store members View Code// steop 1. Description of driver types and feature level View Code D3D_DRIVER_TYPE driverTypes[] = {D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP, D3D_DRIVER_TYPE_SOFTWARE};unsigned int totalDriverTypes = ARRAYSIZE( driverTypes);D3D_FEATURE_LEVEL featureLevels[] = {D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0};unsigned int toalFeatureLevels = ARRAYSIZE( featureLevels);
?
// step 2. create swap-chain, device and context View Code bool result = false;result = CreateDeviceAndSwapChain( totalDriverTypes, driverTypes, toalFeatureLevels, featureLevels);assert( result == true);?
// step 3. set render target view View Code result = SetRenderTargetView();assert( result == true);?
// step 4. set viewport View Code result = SetViewport();assert( result == true);?
return LoadContent(); }開始接觸Direct3D時, 覺得很煩, 需要很多的初始化對象, 還不是最終直接需要的東西, 如 step 1 中定義的一些 的結構數據; 對這種初始化創建方式的不熟悉, 應該是我自己 接觸的代碼量還是相當匱乏, 了解的編程編碼方式不了解; 還好 前段時間在進行 linux的網絡庫開發時, 用到了 pthread_mtex_t 鎖, 創建方式如:
//聲明并定義 mutex結構體 pthread_mutex_t mutex;//聲明定義 mutex使用的 屬性設置 結構體 attr pthread_mutexattr_t attr; attr.xxx = yyy; attr.... = ...;//使用 屬性結構體attr 初始化 mutex pthread_mutex_init( &mutex, &attr);創建線程也有類似的方式:
//聲明 線程tid pthread_t tid;//聲明 線程屬性 pthread_attr_t attr; //接著是 該屬性的設置 pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);//最后使用 屬性結構體 attr 創建線程: pthread_create(&tid, &attr, callback_func, NULL);這種使用屬性結構體來初始化/或創建 最終目的對象的方式 也可轉換到 directx11 編程中; 如 swap-chain( 只不過這里不適用 attr類的 標記單詞, 而使用 Desc后綴):
// step 2. create swap-chain(其實不僅僅是 swap-chain 啦, 還包括 device, context等, 這里是為了說明 attr/desc - creation的 編碼方式)bool Dx11DemoBase::CreateDeviceAndSwapChain( unsigned int totalDriverTypes, D3D_DRIVER_TYPE driverTypes[], unsigned int totalFeatureLevels, D3D_FEATURE_LEVEL featureLevels[]) {RECT dimensions;GetClientRect( hwnd_, &dimensions );unsigned int width = dimensions.right - dimensions.left;unsigned int height = dimensions.bottom - dimensions.top;//第一步聲明并定義 swap-chain 描述: DXGI_SWAP_CHAIN_DESC swapChainDesc;ZeroMemory( &swapChainDesc, sizeof( swapChainDesc));swapChainDesc.BufferCount = 1;swapChainDesc.BufferDesc.Width = width;swapChainDesc.BufferDesc.Height = height;swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;swapChainDesc.OutputWindow = hwnd_;swapChainDesc.Windowed = true;swapChainDesc.SampleDesc.Count = 1;swapChainDesc.SampleDesc.Quality = 0;//第二步才是 創建 swap-chain, 不過unsigned int creationFlags = 0; #ifdef _DEBUGcreationFlags |= D3D11_CREATE_DEVICE_DEBUG; #endifHRESULT result;unsigned int driver = 0;for( driver = 0; driver < totalDriverTypes; ++ driver){//匹配當前的 driver 和 feature-levelresult = D3D11CreateDeviceAndSwapChain(0, driverTypes[ driver], 0, creationFlags, featureLevels, totalFeatureLevels,D3D11_SDK_VERSION,&swapChainDesc, //就是這里啦&swapChain_, //主體討論的對象&d3dDevice_, //同時被創建了&featureLevel_, //同時被創建了&d3dContext_ //同時被創建了 );if( SUCCEEDED( result)){driverType_ = driverTypes[ driver];break;}}if( FAILED( result)){DXTRACE_MSG( "Failed to create the Direct3D device!");return false;}return true; }
?
step 1. 進行 driver , feature-level 的確定;step 2. 產生 device, context 和 swap-chain . 這三者的關系( 通過書本的解釋)如下圖: Direct3D device : 與硬件設備通信Direct3D context : 告訴 Direct3D device 如何進行 draw
swap-chain : Direct3D device 和 Direct3D context 繪制的 目的地
?
step 3. Set render target view View Code其實 第3步 還是沒有超出上圖的 描述, 這一步主要是針對 swap-chain 和 device 的設置( 按照我剛開始學習directx11 而言), 因為 swap-chain分為 多層, 需要設置一層為 候補層 backBufferTarget, 而 其中一層 用來實際直接渲染 , that are the primary and secondary rendering buffers of a swapchain. 如下圖:
?
// step 4. set viewportView Code
最后的 step 4. 也沒有超出上述范圍, 不下圖所示的 ④ 標記, 用以設置 "how to draw", 主要是坐標 /大小:
?
?整個新的 Dx11DemoBase 基類為:
#ifndef _DEMO_BASE_H_ #define _DEMO_BASE_H_#include <d3d11.h> #include <d3dx11.h> #include <DxErr.h>class Dx11DemoBase{ public:Dx11DemoBase();virtual ~Dx11DemoBase();bool Initialize( HINSTANCE hInstance, HWND hwnd);void Shutdown();virtual bool LoadContent();virtual void UnloadContent();protected:HINSTANCE hInstance_;HWND hwnd_;D3D_DRIVER_TYPE driverType_;D3D_FEATURE_LEVEL featureLevel_;ID3D11Device *d3dDevice_;ID3D11DeviceContext *d3dContext_;IDXGISwapChain *swapChain_;ID3D11RenderTargetView *backBufferTarget_;unsigned int width_ ;unsigned int height_ ;private:bool CreateDeviceAndSwapChain( unsigned int totalDriverTypes, D3D_DRIVER_TYPE driverTypes[], unsigned int totalFeatureLevels, D3D_FEATURE_LEVEL featureLevels[]);bool SetRenderTargetView();bool SetViewport(); };#endif?最新文件:?Dx11DemoBase.tar
?
?
?這篇寫到這里, 發現跟原文敘述的過程也差不多, 但通過自己的一層層刨釋, 即使是僅僅 抄寫代碼, 但考慮到 本隨筆還是可能會被其他 觀客 看到, 不能馬虎, 不能行文前后矛盾, 一深入思考, 反而提升了自己對 DirectX11 的認識.
轉載于:https://www.cnblogs.com/Wilson-Loo/archive/2012/11/28/2791857.html
總結
以上是生活随笔為你收集整理的Dx11DemoBase 基类(二) 初始化 DirectX11 的 4个基本步骤的全部內容,希望文章能夠幫你解決所遇到的問題。