本文主要是介绍DirectX11笔记(三)--Direct3D初始化代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这里先提出InitDevice方法, 讲解请参考前一篇文章: DirectX11笔记(三)–Direct3D初始化2
HRESULT InitDevice( )
{// 窗口信息RECT rc;GetClientRect( g_hWnd, &rc );UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 创建交换链描述DXGI_SWAP_CHAIN_DESC sd;sd.BufferDesc.Width = width;sd.BufferDesc.Height = height;sd.BufferDesc.RefreshRate.Numerator = 60;sd.BufferDesc.RefreshRate.Denominator = 1;sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.BufferCount = 1;sd.OutputWindow = g_hWnd;sd.Windowed = true;sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;sd.Flags = 0;sd.SampleDesc.Count = 1; // 不使用 MSAAsd.SampleDesc.Quality = 0;// 调试模式UINT createDeviceFlags = 0;#if defined( DEBUG ) || defined( _DEBUG )createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;#endif// 特征等级D3D_FEATURE_LEVEL featureLevels[] ={D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,};UINT numFeatureLevels = ARRAYSIZE( featureLevels );// 创建 ID3D11Device, ID3D11DeviceContext, IDXGISwapChainHRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,g_driverType,NULL,createDeviceFlags,featureLevels, numFeatureLevels,D3D11_SDK_VERSION,&sd,&g_pSwapChain,&g_pd3dDevice,&g_featureLevel,&g_pImmediateContext );if( FAILED(hr) ){MessageBox( 0, L"D3D11CreateDeviceAndSwapChain Failed.", 0, 0 );return false;}if( g_featureLevel != D3D_FEATURE_LEVEL_11_0 ){MessageBox( 0, L"Direct3D Feature Level 11 unsupported.", 0, 0 );return false;}// 创建 render target viewID3D11Texture2D* pBackBuffer = NULL;hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );if( FAILED( hr ) )return hr;hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );pBackBuffer->Release();if( FAILED( hr ) )return hr;// 创建深度模板缓冲D3D11_TEXTURE2D_DESC descDepth;ZeroMemory(&descDepth, sizeof(descDepth));descDepth.Width = width;descDepth.Height = height;descDepth.MipLevels = 1;descDepth.ArraySize = 1;descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;descDepth.SampleDesc.Count = 1;descDepth.SampleDesc.Quality = 0;descDepth.Usage = D3D11_USAGE_DEFAULT;descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;descDepth.CPUAccessFlags = 0;descDepth.MiscFlags = 0;hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencil);if (FAILED(hr))return hr;// 创建深度模板视图D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;ZeroMemory(&descDSV, sizeof(descDSV));descDSV.Format = descDepth.Format;descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;descDSV.Texture2D.MipSlice = 0;hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);if (FAILED(hr))return hr;g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );// 设置 viewportD3D11_VIEWPORT vp;vp.Width = (FLOAT)width;vp.Height = (FLOAT)height;vp.MinDepth = 0.0f;vp.MaxDepth = 1.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_pImmediateContext->RSSetViewports( 1, &vp );return true;
}
接下来是完整代码, 包括Win32程序的框架, 以后有时间的话补一个对Win32程序的简单说明
#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>HINSTANCE g_hInst = NULL;
HWND g_hWnd = NULL;
D3D_DRIVER_TYPE g_driverType = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL g_featureLevel = D3D_FEATURE_LEVEL_11_0;
ID3D11Device* g_pd3dDevice = NULL;
ID3D11DeviceContext* g_pImmediateContext = NULL;
IDXGISwapChain* g_pSwapChain = NULL;
ID3D11RenderTargetView* g_pRenderTargetView = NULL;
ID3D11Texture2D* g_pDepthStencil = NULL;
ID3D11DepthStencilView* g_pDepthStencilView = NULL;
ID3D11VertexShader* g_pVertexShader = NULL;
ID3D11PixelShader* g_pPixelShader = NULL;
ID3D11InputLayout* g_pVertexLayout = NULL;
ID3D11Buffer* g_pVertexBuffer = NULL;
ID3D11Buffer* g_pIndexBuffer = NULL;
ID3D11Buffer* g_pVSConstBuffer = NULL;
ID3D11Buffer* g_pPSConstBuffer = NULL;
UINT g_IndexNum = 0;HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
HRESULT InitDevice( );int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{UNREFERENCED_PARAMETER( hPrevInstance );UNREFERENCED_PARAMETER( lpCmdLine );if( FAILED( InitWindow( hInstance, nCmdShow ) ) )return 0;if( FAILED( InitDevice() ) ){//CleanupDevice();return 0;}// Main message loopMSG msg = {0};while( WM_QUIT != msg.message ){if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ){TranslateMessage( &msg );DispatchMessage( &msg );}else{//Render();}}//CleanupDevice();return ( int )msg.wParam;
}HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow )
{// Register classWNDCLASSEX wcex;wcex.cbSize = sizeof( WNDCLASSEX );wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = WndProc;wcex.cbClsExtra = 0;wcex.cbWndExtra = 0;wcex.hInstance = hInstance;wcex.hIcon = LoadIcon( NULL,IDC_ARROW );wcex.hCursor = LoadCursor( NULL, IDC_ARROW );wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );wcex.lpszMenuName = NULL;wcex.lpszClassName = L"Triangle3D";wcex.hIconSm = LoadIcon( NULL,IDC_ARROW );if( !RegisterClassEx( &wcex ) )return E_FAIL;// Create windowg_hInst = hInstance;RECT rc = { 0, 0, 640, 480 };AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE );g_hWnd = CreateWindow( L"Triangle3D", L"Triangle3D", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance,NULL );if( !g_hWnd )return E_FAIL;ShowWindow( g_hWnd, nCmdShow );return S_OK;
}LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{PAINTSTRUCT ps;HDC hdc;switch( message ){case WM_PAINT:hdc = BeginPaint( hWnd, &ps );EndPaint( hWnd, &ps );break;case WM_DESTROY:PostQuitMessage( 0 );break;default:return DefWindowProc( hWnd, message, wParam, lParam );}return 0;
}HRESULT InitDevice( )
{// 窗口信息RECT rc;GetClientRect( g_hWnd, &rc );UINT width = rc.right - rc.left;UINT height = rc.bottom - rc.top;// 创建交换链描述DXGI_SWAP_CHAIN_DESC sd;sd.BufferDesc.Width = width;sd.BufferDesc.Height = height;sd.BufferDesc.RefreshRate.Numerator = 60;sd.BufferDesc.RefreshRate.Denominator = 1;sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;sd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.BufferCount = 1;sd.OutputWindow = g_hWnd;sd.Windowed = true;sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;sd.Flags = 0;sd.SampleDesc.Count = 1; // 不使用 MSAAsd.SampleDesc.Quality = 0;// 调试模式UINT createDeviceFlags = 0;#if defined( DEBUG ) || defined( _DEBUG )createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;#endif// 特征等级D3D_FEATURE_LEVEL featureLevels[] ={D3D_FEATURE_LEVEL_11_0,D3D_FEATURE_LEVEL_10_1,D3D_FEATURE_LEVEL_10_0,};UINT numFeatureLevels = ARRAYSIZE( featureLevels );// 创建 ID3D11Device, ID3D11DeviceContext, IDXGISwapChainHRESULT hr = D3D11CreateDeviceAndSwapChain(NULL,g_driverType,NULL,createDeviceFlags,featureLevels, numFeatureLevels,D3D11_SDK_VERSION,&sd,&g_pSwapChain,&g_pd3dDevice,&g_featureLevel,&g_pImmediateContext );if( FAILED(hr) ){MessageBox( 0, L"D3D11CreateDeviceAndSwapChain Failed.", 0, 0 );return false;}if( g_featureLevel != D3D_FEATURE_LEVEL_11_0 ){MessageBox( 0, L"Direct3D Feature Level 11 unsupported.", 0, 0 );return false;}// 创建 render target viewID3D11Texture2D* pBackBuffer = NULL;hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );if( FAILED( hr ) )return hr;hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL, &g_pRenderTargetView );pBackBuffer->Release();if( FAILED( hr ) )return hr;// 创建深度模板缓冲D3D11_TEXTURE2D_DESC descDepth;ZeroMemory(&descDepth, sizeof(descDepth));descDepth.Width = width;descDepth.Height = height;descDepth.MipLevels = 1;descDepth.ArraySize = 1;descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;descDepth.SampleDesc.Count = 1;descDepth.SampleDesc.Quality = 0;descDepth.Usage = D3D11_USAGE_DEFAULT;descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;descDepth.CPUAccessFlags = 0;descDepth.MiscFlags = 0;hr = g_pd3dDevice->CreateTexture2D(&descDepth, NULL, &g_pDepthStencil);if (FAILED(hr))return hr;// 创建深度模板视图D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;ZeroMemory(&descDSV, sizeof(descDSV));descDSV.Format = descDepth.Format;descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;descDSV.Texture2D.MipSlice = 0;hr = g_pd3dDevice->CreateDepthStencilView(g_pDepthStencil, &descDSV, &g_pDepthStencilView);if (FAILED(hr))return hr;g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, g_pDepthStencilView );// 设置 viewportD3D11_VIEWPORT vp;vp.Width = (FLOAT)width;vp.Height = (FLOAT)height;vp.MinDepth = 0.0f;vp.MaxDepth = 1.0f;vp.TopLeftX = 0;vp.TopLeftY = 0;g_pImmediateContext->RSSetViewports( 1, &vp );return true;
}
这篇关于DirectX11笔记(三)--Direct3D初始化代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!