DirectX11笔记(三)--Direct3D初始化代码

2024-05-11 22:58

本文主要是介绍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初始化代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980896

相关文章

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时