DirectX 11 SDK文档
- 格式:doc
- 大小:377.00 KB
- 文档页数:64
dx学习(三、dx11初始化)进⼊正题啦1 . 创建设备(Device)和上下⽂(Context) ⾸先需要创建D3D11设备(ID3D11Device)和上下⽂(ID3D11DeviceContext)。
它们是是最重要的DD接⼝,可以被看成是物理图形设备硬件的软控制器;也就是说,我们可以通过该接⼝与硬件进⾏交互,命令硬件完成⼀些⼯作,如:在显存中分配资源、清空后台缓冲区、将资源绑定到各种管线阶段、绘制⼏何体。
a.ID3D11Device 接⼝⽤于检测显⽰适配器功能和分配资源。
b.ID3D11DeviceContext 接⼝⽤于设置管线状态、将资源绑定到图形管线和⽣成渲染命令。
HRESULT D3D11CreateDevice (IDXGIAdapter *pAdapter,D3D_DRIVER_TYPE DriverType,HMODULE Software ,UINT Flags ,CONST D3D_FEATURE_LEVEL *pFeatureLevels ,UINT FeatureLevels ,UINT SDKVersion,ID3D11Device **ppDevice ,D3D_FEATURE_LEVE L *pFeatureLevel,ID3D11DeviceContext **ppImmediateContext);参数分析:pAdapter 指定要为哪个物理显卡创建设备对象。
为NULL时,表⽰使⽤主显卡;DriverType 设置驱动类型,⼀般选择硬件加速D3D_DRIVER_TYPE_HARDWARE,此时下⼀个参数就是NULL;Flags 为可选参数,⼀般为NULL,可以设为 D3D11_CREATE_DEVICE_DEBUG 或 D3D11_CREATE_DEVICE_SINGLETHREADED,或两者⼀起,前者让要⽤于调试时收集信息,后者在确定程序只在单线程下运⾏时设置为它,可以提⾼性能;pFeatureLevels 为我们提供给程序的特征等级的⼀个数组,下⼀个参数为数组中元素个数;SDKVersion 恒定为D3D11_SDK_VERSION;ppDevice 为设备指针的地址,注意设备是指针类型,这⾥传递的是指针的地址(⼆维指针,d3d程序中所有的接⼝都声明为指针类型!);pFeatureLevel 为最后程序选中的特征等级,我们定义相应的变量,传递它的地址进来;ppImmediateContext 为设备上下⽂指针的地址,要求同设备指针。
VS2010DirectX11SDKWindowsSDK编程过程中的错误异常解决⽅案记录ID3DBlob 未声明的标记符 undefined signalID3DBlob 定义在 D3DCommon.h 中, 但是使⽤ vs2010 & DirectX11 联合开发时, D3DCommon.h 会有两份, 如我的环境是:C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\D3DCommon.hD:\Program Files\Microsoft DirectX SDK (June 2010)\Include\D3Dcommon.h这样, 即使在项⽬中有包含 d3dx11.h (包含 D3DCommon.h) 也不能正确进⾏编译, 需要将 DirectX11 sdk 包含⽬录在 Windonws sdk 的前⾯, 如:D3DCOMPILE_ENABLE_STRICTNESS 未声明标记符D3DX11CompileFromFile() 创建加载并编译 shader⽂件时需要⽤到的 shaderFlags , 定义在 D3DCompiler.h 中, 需要 includexnamath.h 报错: 在标识符“XMConvertToRadians”的前⾯如下报错:1>d:\program files\microsoft directx sdk (june 2010)\include\xnamath.h(159): error C2146: 语法错误: 缺少“;”(在标识符“XMConvertToRadians”的前⾯)1>d:\program files\microsoft directx sdk (june 2010)\include\xnamath.h(159): error C2433: “FLOAT”: 不允许在数据声明中使⽤“__forceinline”1>d:\program files\microsoft directx sdk (june 2010)\include\xnamath.h(159): error C4430: 缺少类型说明符 - 假定为 int。
directx中文手册1、什么是DirectX ?微软的DirectX软件开发工具包(SDK)提供了一套优秀的应用程序编程接口(APIs),这个编程接口可以提供给你开发高质量、实时的应用程序所需要的各种资源。
DirectX技术的出现将极大的有助于发展下一代多媒体应用程序和电脑游戏。
总的说来,使用DirectX的主要有两个好处:1、为软件开发者提供硬件无关性;2、为硬件开发提供策略。
1、为软件开发者提供硬件无关性微软开发DirectX,其最主要的目的之一是促进在Windows操作系统上的游戏和多媒体应用程序的发展。
在DirectX出现以前,主要的游戏开发平台是MS-DOS,游戏开发者们为了使他们的程序能够适应各种各样的硬件设备而绞尽脑汁。
自从有了DirectX,游戏开发者们便可以获益于Windows平台的设备无关性,而又不失去直接访问硬件的特性。
DirectX主要的目的就是提供象MS-DOS一样简洁的访问硬件的能力,来实现并且提高基于MS-DOS平台应用软件的运行效果,并且为个人电脑硬件的革新扫除障碍。
另一方面,微软公司开发DirectX是为了在当前或今后的计算机操作系统上提供给基于Windows平台的应用程序以高表现力、实时的访问硬件的能力。
DirectX在硬件设备和应用程序之间提供了一套完整一致的接口,以减小在安装和配置时的复杂程度,并且可以最大限度的利用硬件的优秀特性。
通过使用DirectX所提供的接口,软件开发者可以尽情的利用硬件所可能带来的高性能,而不用烦恼于那些复杂而又多变的硬件执行细节。
一个高表现力的基于Windows平台的游戏将得益于以下几种技术:专为提高图形运算及快速反应能力而设计的加速卡(Accelerator cards)即插即用以及其它Windows软硬件内建于Windows的通信服务, 包括DirectPlay2、为硬件开发提供策略DirectX的另外一个重要的目的是给硬件厂商提供开发策略,他们可以从高性能程序的开发者和独立的硬件供应商(independent hardware vendors IHVs)那里得到反馈。
DirectX11编程指南微软在2009年8⽉的DirectXSDK中发布了DirectX的正式版本。
基于对DirectX的⼀贯兴趣,我把DirectX Graphics的⽂档边看边译为中⽂。
也算是⼀份学习笔记吧。
,Direct3D是在Windows平台上开发实时3D应⽤的软件开发接⼝。
随着Windows操作系统的升级,Direct3D的版本也随着升级。
WindowsXP对应Direct3D9的各个版本,Vista对应D3D10,10.1,Windows7则对应D3D11。
所以在XP平台上是没法做D3D10及以上的开发了。
不过看起来因为Win7实际上是Vista的⼀个升级,所以微软也准备在Vista上⽀持D3D11。
D3D11是D3D10的⼀个超集,即它包含了D3D10的所有功能,并在其渲染管线上添加了⼀些新的特性。
根据官⽅的⽂档,D3D11的新特性包括:ComputerShader(计算着⾊器)计算着⾊器能把GPU当成⼀种通⽤功能的并⾏处理器来使⽤。
其实在D3D9时代就已经可以应⽤GPU来进⾏GP(General-Purpose)计算了,只不过那时候需要使⽤⼀些特殊的⼿段来处理(⽐如将数据包装成“纹理”)。
在D3D11中专门实现了这样⼀个着⾊器阶段来⽀持GP计算,应该使得开发这类应⽤更为⽅便⼀些了。
(参考NVIDIA的CUDA 技术以及OpenCL标准)虽然计算着⾊器也是通过D3DDevice进⾏访问的,并能和其他的图形着⾊器共享内存资源,但它并不直接连接到其他的着⾊器阶段上⾯。
这⼤概是因为ComputerShader并不直接与图形渲染功能相关吧。
Dynamic Shader Linkage(动态着⾊器链接)D3D11包含的SM5(Shader Model规范)⽀持⾯向对象语⾔的构建⽅式,并⽀持运⾏时的着⾊器链接。
多线程(Multithreading)随着CPU的多核化趋势渐渐普及,D3DAPI对多线程的⽀持。
Direct3D11学习:(⼀)开发环境配置转载请注明出处:从今天开始,开启⼀个新的系列:Direct3D11的学习教程。
因为⼀直对3D⽅⾯⽐较感兴趣,最近决定开始学习D3D知识。
我之前也没有接触过Direct3D9,在⽹络上咨询过⼀些⼤神,⼤家都推荐直接从D3D11开始学起。
我想了好久终于下决⼼买了⼀本D3D11的龙书《Introduction to 3D Game Programming: With Directx 11》,可惜这本书⽬前只有英⽂版。
⾃⼰可怜的英⽂当初四级都差点没过,只能逼着⾃⼰,⼀定要坚持下去啊。
写这个系列的博客有多⽅⾯的考虑,⼀⽅⾯⽤这种⽅式监督⾃⼰的学习,另⼀⽅⾯记录⼀下⾃⼰的学习历程,也跟⼤家交流⼀下学习过程中的问题和⼼得。
⾸先,在学习D3D之前,必须要做的就是开发环境的配置。
这篇⽂章主要讲解了D3D11开发环境的配置过程,创建并调试运⾏书中的⼀个例⼦BoxDemo。
⼀.⼯具准备1.DirectX11 SDK安装包安装包选择微软官⽹下载,我选择的SDK版本是DXSDK_Jun10.exe。
下载⽹址:按照安装提⽰安装完成。
注意DXSDK_Jun10.exe的安装过程中可能会出现安装错误,如下图:解决⽅法参考这⾥:2.Visual Studio 2010镜像龙书中教程使⽤的IDE是vs2010,我电脑中已经安装过了vs2010,所以就不贴镜像⽹址了。
⼆、创建⼀个win32项⽬1.运⾏vs2010,打开菜单⽂件>新建>项⽬,如下图所⽰:2.出现新建项⽬对话框后,选择Visual C++ > Win32选项,选择Win32项⽬,接下来输⼊项⽬名称并选择你想要创建的项⽬路径。
如果为解决⽅案创建⽬录选项默认勾选了,把它取消勾选。
点击确定按钮。
如下图:3.接下来弹出⼀个新的对话框窗⼝,如下图。
这个对话框左边有两个选项:概述和应⽤程序设置。
选择应⽤程序设置选项,对话框窗⼝出现应⽤程序设置页⾯。
Constant-Memory Order-Independent TransparencyTechniquesLouis Bavoil******************Eric Enderton********************March 2011Document Change HistoryNVIDIA Corporation2701 San Tomas ExpresswayOverviewOrder-Independent Transparency (OIT) algorithms are algorithms for which the output image is independent on the order of the input primitives. This SDK sample includes DirectX 11 implementations of Stochastic Transparency [Enderton et al. 2010] and Dual Depth Peeling [Bavoil and Myers 2008]. It uses the "independent blend ” hardware feature for Dual Depth Peeling and the output coverage mask feature for Stochastic Transparency.Figure 1. Full stochastic transparency algorithm [Enderton et al. 2010] with 3 geometry passes and 8x MSAA.IntroductionDepth peeling [Mammen 1989] [Everitt 2001] is a robust and simple OIT algorithm. Thealgorithm requires performing N geometry passes for peeling N color layers. Dual depthpeeling [Bavoil and Myers 2008] is an accelerated depth peeling algorithm, which can peel N layers in N/2+1 geometry passes. Both depth peeling and dual-depth peeling can beimplemented with a constant video-memory budget, by blending the layers on the fly as they get captured in front-to-back or back-to-front order. These algorithms require a view-dependent number of geometry passes to converge to the correct result.A different approach is to capture all the semi-transparent fragments first, before sorting andblending them. This is what the original A-Buffer algorithm does to handle transparency[Carpenter 1984]. Using DirectX 11 GPUs, it is now possible to capture all the transparentfragments using per-pixel linked lists in a single geometry pass [Yang et al. 2010]. Thefragments are then sorted and blended in a post-processing pass. The main issue with thisalgorithm is that it requires a dynamic amount of video memory to store all the visible semi-transparent fragments, which may be impractical for some applications where the number of semi-transparent fragments is unbounded.Bucket depth peeling [Liu et al. 2009] and Stochastic Transparency [Enderton et al. 2010] are approximate OIT algorithms that can produce plausible results with a constant video-memory budget. Another constant-memory OIT algorithm intended mostly for hair wasproposed by [Sintorn and Assarson 2009]. An advantage of the Stochastic Transparencyalgorithm is that it always converges to the correct result when increasing the effectivenumber of samples per pixel, without increasing the video-memory budget. Implementation NotesStochastic TransparencyFor Stochastic Transparency, each geometry pass uses 8 MSAA samples per pixel andmultiple passes can be performed to simulate more MSAA samples per pixel. Alphacorrection is enabled by default and can be disabled by settingUSE_ALPHA_CORRECTION=0 in StochasticTransparency11.hlsl.The implementation uses the RGBA16F format for the MSAA color buffer (seeSTOCHASTIC_COLOR_FORMAT in StochasticTransparency11.h). Using a RGBA8format introduces a significant bias in the results if USE_ALPHA_CORRECTION=1.Running the SampleThe model can be rotated by dragging the mouse on the window. The following renderingalgorithms are exposed in the GUI: stochastic transparency, dual depth peeling, and plainalpha blending (no OIT). The model is rendered with a uniform opacity, which is exposed in the GUI with the “Alpha” slider.ResultsFigure 2 compares Stochastic Transparency with Dual Depth Peeling for similar number of geometry passes. Stochastic Transparency has the advantage of being naturally anti-aliased.In this implementation, it has 8xMSAA quality because it uses 8 samples per pixel for itsMSAA buffers.With the motor scene (322,284 triangles) in 1920x1200 on GeForce GTX 580, the frametimes are 5.2 ms for Stochastic Transparency and 5.5 ms for Dual Depth Peeling, using 3geometry passes for Stochastic Transparency and 4 for Dual Depth Peeling.Figure 2. (left) Dual Depth Peeling with 4 geometry passes.(right) Stochastic Transparency with 3 geometry passes.References[Enderton et al. 2010] Eric Enderton, Erik Sintorn, Peter Shirley, David Luebke, “Stochastic Transparency”, Symposium on Interactive 3D Graphics and Games (I3D) 2010.[Yang et al. 2010] Yang, J. C., Hensley, J., Grün, H. and Thibieroz, N., “Real-Time Concurrent Linked List Construction on the GPU”, Computer Graphics Forum, 29, 2010.[Liu et al. 2009] Fang Liu, Meng-Cheng Huang, Xue-Hui Liu, En-Hua Wu, “Efficient Depth Peeling via Bucket Sort”, HPG '09 Proceedings of the Conference on High Performance Graphics 2009.[Sintorn and Assarson 2009] Erik Sintorn, Ulf Assarson, “Hair Self Shadowing and Transparency Depth Ordering Using Occupancy maps”, Proceedings of the Symposium on Interactive 3D Graphics and Games (I3D 2009).[Bavoil and Myers 2008] Louis Bavoil and Kevin Myers, "Order Independent Transparency with Dual Depth Peeling", NVIDIA, 2008.[Everitt 2001] Cass Everitt, “Interactive Order-Independent Transparency”, NVIDIA, 2001.[Mammen 1989] Abraham Mammen, “Transparency and Antialiasing Algorithms Implemented with the Virtual Pixel Maps Technique”, IEEE Computer Graphics and Applications, v.9 n.4, 1989.[Carpenter 1984] Loren Carpenter, “The A-Buffer, an Antialiased Hidden Surface Method”, ACM SIGGRAPH Computer Graphics, v.18 n.3, p.103-108, 1984.NoticeALL NVIDIA DESIGN SPECIFICATIONS, REFERENCE BOARDS, FILES, DRAWINGS, DIAGNOSTICS, LISTS, AND OTHER DOCUMENTS (TOGETHER AND SEPARATELY, “MATERIALS”) ARE BEING PROVIDED “AS IS.” NVIDIA MAKES NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.Information furnished is believed to be accurate and reliable. However, NVIDIA Corporation assumes no responsibility for the consequences of use of such information or for any infringement of patents or other rights of third parties that may result from its use. No license is granted by implication or otherwise under any patent or patent rights of NVIDIA Corporation. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. NVIDIA Corporation products are not authorized for use as critical components in life support devices or systems without express written approval of NVIDIA Corporation.TrademarksNVIDIA, the NVIDIA logo, GeForce, NVIDIA Quadro, and NVIDIA CUDA are trademarks or registered trademarks of NVIDIA Corporation in the United States and other countries. Other company and product names may be trademarks of the respective companies with which they are associated.Copyright© 2011 NVIDIA Corporation. All rights reserved.。
SDK使用说明范文一、SDK简介SDK(Software Development Kit,软件开发工具包)是一组开发工具和资源的集合,用于帮助开发者创建应用程序。
SDK提供了开发所需的应用程序接口(API)、示例代码、文档等。
SDK可以大大简化开发过程,提高开发效率。
二、SDK的安装和配置2.配置开发环境需要根据开发环境配置SDK。
例如,在Windows上,需要在系统环境变量中设置SDK的路径。
三、SDK的导入在开发工具中导入SDK,可以使用SDK提供的API进行开发。
1.创建新项目打开开发工具,创建一个新项目。
2.导入SDK在项目中添加SDK,可以通过添加依赖库或引入外部库的方式导入。
四、SDK的使用SDK提供了各种功能的API供开发者使用。
以下是一些常见的SDK使用方法。
1.初始化SDK在应用程序的入口处,调用SDK提供的初始化方法,完成SDK的初始化。
可以根据需要进行必要的配置。
2.登录与认证在用户登录模块中,调用SDK提供的登录与认证API,实现用户的身份认证与管理。
3.数据获取与处理使用SDK提供的API,获取所需的数据。
可以进行数据解析、处理与展示等。
4.事件与回调SDK通常提供事件与回调机制,用于处理异步操作或接收SDK返回的数据。
需要注册相应的事件监听器或回调函数。
5.功能扩展与定制SDK通常提供了一些扩展和定制功能的接口,开发者可以根据需要,自定义SDK的功能与行为。
五、SDK的调试与测试在开发过程中,调试和测试是不可或缺的环节。
1.调试开发者可以使用开发工具提供的调试功能,结合SDK的调试日志,来定位和解决开发中的问题。
2.测试开发者可以编写单元测试或集成测试,来验证SDK的使用是否符合预期,并检查各种边际情况下的功能表现。
六、SDK的更新与升级随着软件的发展和需求变化,SDK也需要进行更新和升级。
一般来说,开发者可以通过以下方式更新SDK:1.官方渠道更新开发者可以定期查看官方网站、论坛或邮件列表,了解最新版本的SDK发布信息。
实验准备安装与配置DirectX实验环境一、实验目的掌握DirectX程序设计的环境配置。
二、实验内容(一)Visual Studio的下载与安装(二)DX SDK的下载与安装三、实验步骤(一)安装Visual Studio 20121. VS 2012的下载在本学期的实验中,我们建议使用VS 2012或者更高的版本。
因为VS 2012已经集成了编译DirectX 11程序所需要的winSDK 8.0。
大家可以在微软的官方网站下载VS 2012的镜像文件,文件名为VS2012_ULT_chs.iso。
2. VS 2012的安装如果大家是Windows 8.1 操作系统,可以直接双击VS2012_ULT_chs.iso开始安装,否则需要安装一个虚拟光驱才可以安装。
开始安装后进入到安装界面,按照提示一步一步进行操作。
(1)选择“我同意许可条款和条件”(2)选择所有可安装功能(3)开始安装(4)输入产品密钥(5)选择默认环境设置为Visual C++开发环境后安装完毕(二)DX SDK的下载与安装1. DX SDK的下载如果大家安装VS2012或者VS2013,那么DX SDK的安装不是必须的。
但是有些实验可能会用到DX SDK中一些库文件,所以建议大家还是装上。
在本学期的实验中,我们使用的是微软发布的DXSDK_JUN10版本。
这是DXSDK最后一个版本,之后的版本都集合到了winSDK中。
大家可以在网上找到此版本的安装文件,文件名为DXSDK_Jun10.exe。
/download/A/E/7/AE743F1F-632B-4809-87A9-AA1BB345 8E31/DXSDK_Jun10.exe2. DX SDK的安装(1)双击DXSDK_Jun10.exe文件进入安装界面如图1所示。
图1:DirectX安装界面(2)点击下一步,当出现如图2所示界面时,点击否,表示不参加用户体验改进。
图2:DirectX安装界面(3)点击下一步,出现选择安装组件,如图3所示,选择默认安装的选项即可。
DirectX 11 SDK文档(一)分类:Winows API C DirectX2011-04-23 10:23 1373人阅读评论(0) 收藏举报文档applicationnulldirect3dcallback工作总结这是一个初步的教程。
我们将通过必要的步骤来创建一个Win32 Application。
我们创建一个空白的窗口为DirectX 11做准备。
创建窗口每一个窗口应用程序需要至少一个窗口对象。
甚至在开始获得DirectX 11的诸多细节之前,我们的Application必须要有一个工作着的窗口。
我们有三个事情要做:1.注册一个窗口类WNDCLASSEX 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(hInstance,(LPCTSTR)IDI_TUTORIAL1);//这个是资源图标,需要资源载入wcex.hCursor = LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName = NULL;wcex.lpszClassName = szWindowClass;wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_TUTORIAL1);if( !RegisterClassEx(&wcex) )return FALSE;2.创建一个窗口对象g_hInst = hInstance; // 用全局变量来保存应用程序实例句柄RECT rc = { 0, 0, 640, 480 };AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW,FALSE );//根据窗口风格,以rc为客户区调整整个窗口大小g_hWnd = CreateWindow( szWindowClass, L"Direct3D 11 Tutorial 0: Setting Up Window",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,rc.right - rc.left, rc.bottom - rc.top,NULL, NULL,hInstance, NULL);if( !g_hWnd )return FALSE;ShowWindow( g_hWnd, nCmdShow );3.检索和调度这个窗口的消息队列的消息MSG msg = {0};while( GetMessage( &msg, NULL, 0, 0 ) {TranslateMessage( &msg );DispatchMessage( &msg );}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;}对于每个窗口应用程序,这是最少的步骤来创建一个窗口对象。
如果我们编译运行这个代码,我们将看到一个空的白色背景的窗口。
DirectX 11 SDK文档(二)分类:Winows API C DirectX2011-04-23 11:43 1750人阅读评论(0) 收藏举报文档direct3dnullfloatoutputc总结这是第一个教程,我们将通过必要的元素来创建一个小型的DirectX 11应用程序。
每个DirectX 11应用程序必须拥有这些元素得以正常运转。
这些元素包括创建一个窗口对象和设备对象,并且能够显示一种颜色的窗口。
创建DirectX 11 设备创建窗口对象和消息循环请参见DirectX 11 SDK文档(一)。
现在我们有一个能够显示的窗口,接下来我们能够创建Direct3D 11 设备。
如果我们要渲染3D的场景,创建设备是必须的。
第一件事要做的就是创建以下三个对象:device,immediate context,swap chain。
immediate context在Direct3D 11 是一个新的对象。
在Direct3D 10中device对象被用来渲染场景和创建资源。
在Direct3D 11中immediate context被应用程序用来将场景渲染到缓冲区里,而device有很多创建资源的函数。
swap chain负责交换device渲染的场景所存放缓冲区,并且能够将要显示的内容呈现在屏幕上。
swap chain将包含两个或更多的缓冲区,主要包含前景缓冲区和背景缓冲区。
前景缓冲区就是用户当前在屏幕上所看到的。
前景缓冲区是只读,并且不能被修改。
背景缓冲区就是device所渲染场景的目标位置。
一旦完成了绘制操作,swap chain通过交换两个缓冲区将背景缓冲区的内容呈现在屏幕上。
这时背景缓冲区变成了前景缓冲区,而前景缓冲区变成了背景缓冲区。
为了创建swap chain,我们填充一个DXGI_SWAPCHAIN_DESC结构体,DXGI_SWAPCHAIN_DESC结构体描述了我们要创建swap chain的对象属性。
DXGI_SWAPCHAIN_DESC结构体的变量虽然多,但是我们只要关心一小部分就可以了。
BackBufferUsage标记应用程序将如何使用背景缓冲区。
在这个例子中,我们要渲染这个背景缓冲区,所以我们将BackBufferUsage赋值为DXGI_USAGE_RENDER_TARGET_OUTPUT。
OutputWindow代表窗口,该窗口是swap chain将图像呈现的区域。
SampleDesc用来支持多采样,既然这个例子备没有使用多采样,所以将SampleDesc.Count=1,SampleDesc.Quality=0。
一旦DXGI_SWAPCHAIN_DESC结构体被填好,我们就可以调用D3D11CreateDeviceAndSwapChain()函数为我们创建device 和swap chain。
代码如下:DXGI_SWAP_CHAIN_DESC sd;ZeroMemory( &sd, sizeof(sd) );sd.BufferCount = 1;sd.BufferDesc.Width = 640;sd.BufferDesc.Height = 480;sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;sd.BufferDesc.RefreshRate.Numerator = 60;sd.BufferDesc.RefreshRate.Denominator = 1;sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;sd.OutputWindow = g_hWnd;sd.SampleDesc.Count = 1;sd.SampleDesc.Quality = 0;sd.Windowed = TRUE;if( FAILED( D3D11CreateDeviceAndSwapChain( NULL,D3D_DRIVER_TYPE_HARDWARE,NULL, 0, featureLevels, numFeatureLevels,D3D11_SDK_VERSION, &sd, &g_pSwapChain,&g_pd3dDevice,NULL, &g_pImmediateContext ) ) ){return FALSE;}接下来我们需要创建一个render target view,它在Direct3D 11是resource view的一种类型。
在特定的阶段,resource view允许一个资源受graphics pipeline绑定。
resource views是C中声明的类型,是一个大块的raw memory 将应用于任何的数据类型。
我们能够在resource views中将大块的raw memory 用于定义一个整数数组,一个浮点数数组,一个结构体,一个结构体数组,等等。
如果我们不知道raw memory的数据类型,那么它对我们来讲是没有用处的。
Direct3D 11的resource views与之类似。
目前为止,一个2D的纹理保存方式与raw memory类似,也是一个潜在的raw resource。
一旦我们有了这个资源,我们就可以在graphics pipeline的不同阶段创建不同的resource views,数据格式类型如下:作为渲染目标,作为深度模板缓冲区来接受深度信息,或者作为纹理资源。
在C中的声明中允许一个内存块以不同的方式使用,resource views也是如此。
我们需要创建一个render target view,因为我们需要将它绑定到swap chain 后背缓冲区,作为渲染目标。
所以Direct3D 11能够将渲染的场景写入缓冲区。
我们可以调用GetBuffer()函数来获得背景缓冲区对象。
选择性的,我们可以选择填充一个描述render target view的D3D11_RENDERTARGETVIEW_DESC 结构体来创建render target view。
这个描述通常是CreateRenderTargetView()函数的第二个参数。
然而,在这个教程,创建一个默认的render target view就够了,所以第二个参数传NULL。
一旦我们创建了默认的render target view,我们就可以调用immediate context对象的OMSetRenderTargets()函数,就可以将render target view绑定到pipeline。