4.1.3 纹理数据格式
2D纹理即一个数据矩阵,每个元素可以是color值,也可是float3向量值
格式例子: DXGI_FORMAT_R32G32B32_FLOAT(每个纹素为3个32位的float型,可存储3D vector)
DXGI_FORMAT_R8G8B8A8_UNORM (4个8bit unsigned数据,[0,1]之间)
DXGI_FORMAT_R8G8B8A8_SNORM (8bit signed [-1,1]之间)
DXGI_FORMAT_R8G8B8A8_SINT (signed [-128,128])
DXGI_FORMAT_R8G8B8A8_UINT (unsigned [0,255])
DXGI_FORMAT_R8G8B8A8_TYPELESS(仅用来存储数据,在给渲染管线使用前,reinterpret_cast指出数据类型)
4.1.4 SwapChain是啥
two texture buffers are maintained by the hardware, one called the front buffer and a second called the back buffer.The front buffer stores the image data currently being displayed on the monitor, while the next frame of animation is being drawn to the back buffer.
底层硬件维持两个texture,前buffer和后buffer,前buffer的图像数据是准备好的,只管显示到屏幕上即可,后buffer中进行下一帧的数据绘制。
Swapping the roles of the back and front buffers is called presenting. Presenting is an efficient operation, as the pointer to the current front buffer and the pointer to the current back buffer just need to be swapped.
前后不停交换。只是swap下指针。如下图:
上图中, 首先render bufferB,当此帧结束后,指针切换, bufferB为frontBuffer来显示。
4.1.5 Depth Buffering
1.There is a one-to-one correspondence between each element in the depth buffer and each pixel in the back buffer (i.e., the ijth element in the back buffer corresponds to the ijth element in the depth buffer). So if the back buffer had a resolution of 1280 × 1024, there would be 1280 × 1024 depth entries.
就是depthBuffer和backBuffer的纹理存储要一一对应
2. 同一pixel点上,有不同depth的数据需要绘制:首先backBuffer和depthBuffer进行clear成默认颜色和深度值1.0f,
然后在某pixel点p1绘制了数据old,这时候在p1点有另外新的数据new
检测新数据的z值是否小于old?如果大于,则抛弃new数据
DXGI_FORMAT_D24_UNORM_S8_UINT
uses 24-bits for the depth buffer and 8-bits for the stencil buffer. For this reason, the depth buffer is better called the depth/stencil buffer.
4.2 初始化D3D
步骤:
1.创建deviice和context
2.检测反锯齿的支持level
3.描述swapChain,并创建之
4.创建 renderTargetView关联backBuffer,创建depth/stencil view关联depth_stencil buffer
5.设置viewPort
1.device和context
HRESULT hr = D3D11CreateDevice( 0, // default adapter md3dDriverType, //设备类型,使用硬件 还是软件渲染? 0, // no software device createDeviceFlags, 0, 0, // default feature level array D3D11_SDK_VERSION, &md3dDevice, &featureLevel, &md3dImmediateContext);
md3dDriverType 中,D3D_DRIVER_TYPE_REFERENCE:算是一种参考标准,用来检测正确性。检测硬件渲染是否正确?(如果reference模式下无bug的话)
SOFTWARE:一种软件渲染方式,来渲染东东,
ID3D11DeviceContext* md3dImmediateContext;
There is also such a thing called a deferred context (ID3D11Device::CreateDeferredContext). This is part of the
multithreading support in Direct3D 11. We consider multithreading an advance topic and do not cover
it in this book, but the basic idea is as follows:
1. Have the immediate context on the main rendering thread.
2. Have any additional deferred contexts on separate worker threads.
(a) Each worker thread can record graphics commands into a command list (ID3D11CommandList).
(b) The command list from each worker thread can then be executed on the main rendering thread.
If the command lists take time to assemble, which they can for complicated rendering graphs, being able to assemble
the command lists in parallel on multi-core systems is advantageous
swapChain:
// Fill out a DXGI_SWAP_CHAIN_DESC to describe our swap chain. DXGI_SWAP_CHAIN_DESC sd; sd.BufferDesc.Width = mClientWidth; //此下几行为backBuffer的信息 sd.BufferDesc.Height = mClientHeight; 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; // Use 4X MSAA? if( mEnable4xMsaa ) { sd.SampleDesc.Count = 4; sd.SampleDesc.Quality = m4xMsaaQuality-1; } // No MSAA else { sd.SampleDesc.Count = 1; sd.SampleDesc.Quality = 0; } sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; sd.BufferCount = 1; //backBuffer数 sd.OutputWindow = mhMainWnd; sd.Windowed = true; sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; sd.Flags = 0; // To correctly create the swap chain, we must use the IDXGIFactory that was // used to create the device. If we tried to use a different IDXGIFactory instance // (by calling CreateDXGIFactory), we get an error: "IDXGIFactory::CreateSwapChain: // This function is being called with a device from a different IDXGIFactory." IDXGIDevice* dxgiDevice = 0; HR(md3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice)); IDXGIAdapter* dxgiAdapter = 0; HR(dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter)); IDXGIFactory* dxgiFactory = 0; HR(dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory)); HR(dxgiFactory->CreateSwapChain(md3dDevice, &sd, &mSwapChain)); ReleaseCOM(dxgiDevice); ReleaseCOM(dxgiAdapter); ReleaseCOM(dxgiFactory);
If you wanted to change the multisampling settings at runtime, you would have to destroy and recreate the swap chain.
Create the Render Target View
Create the Depth/Stencil Buffer and View
The depth buffer is just a 2D texture that stores the depth information (and stencil information if using stenciling).
D3D11_USAGE Usage;比较重要。
Usage: A member of the D3D11_USAGE enumerated type specifying how the texture will be used. The four
usage values are:
(a) D3D11_USAGE_DEFAULT: Specify this usage if the GPU (graphics processing unit) will be reading and
writing to the resource. The CPU cannot read or write to a resource with this usage. For the depth/stencil
buffer, we specify D3D11_USAGE_DEFAULT because the GPU will be doing all the reading and writing to
the depth/stencil buffer.
default模式,GPU读写,CPU不能读写。depth/stencil buffer设置成default,因为他只需要GPU进行操作。
(b) D3D11_USAGE_IMMUTABLE: Specify this usage if the contents of a resource does not ever change after
creation. This allows for some potential optimizations, as the resource will be read-only by the GPU. The
CPU and GPU cannot write to an immutable resource, except at creation time to initialize the resource. The
CPU cannot read from an immutable resource.
(c) D3D11_USAGE_DYNAMIC: Specify this usage if the application (CPU) needs to update the data contents of
the resource frequently (e.g., on a per frame basis). A resource with this usage can be read by the GPU and
written to by the CPU. Updating a GPU resource dynamically from the CPU incurs a performance hit, as the
new data must be transferred over from CPU memory (i.e., system RAM) to GPU memory (i.e., video RAM);
therefore, dynamic usage should be avoided unless necessary.
(d) D3D11_USAGE_STAGING: Specify this usage if the application (CPU) needs to be able to read a copy of the
resource (i.e., the resource supports copying data from video memory to system