• DirectX 11的初始化


    总体来说可以概括为以下几个步骤:

    1. 创建Device和Context
    2. 创建SwapChain
    3. 为BackBuffer创建View
    4. 创建Depth/Stencil Buffer,并为之创建View
    5. 将View绑定到Context中
    6. 设置Viewport

    创建Device和Context

    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_LEVEL       *pFeatureLevel,
      ID3D11DeviceContext     **ppImmediateContext
    );
    

    主要调用D3D11CreateDevice创建ID3D11Device和ID3D11DeviceContext。接口说明可以查看API文档。这里注意第四个参数Flags。在Windows10系统下,如果要设置为D3D11_CREATE_DEVICE_DEBUG,则必须安装DirectX Graphics Tools。参见

    创建SwapChain

    HRESULT CreateSwapChain(
      IUnknown             *pDevice,
      DXGI_SWAP_CHAIN_DESC *pDesc,
      IDXGISwapChain       **ppSwapChain
    );
    

    主要调用IDXGIFactory::CreateSwapChain创建IDXGISwapChain。接口说明可以查看API文档。我们通过上一步创建的ID3D11Device来获得IDXGIFactory:参见

    IDXGIDevice * pDXGIDevice = nullptr;
    hr = g_pd3dDevice->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice);
    
    IDXGIAdapter * pDXGIAdapter = nullptr;
    hr = pDXGIDevice->GetAdapter( &pDXGIAdapter );
    
    IDXGIFactory * pIDXGIFactory = nullptr;
    pDXGIAdapter->GetParent(__uuidof(IDXGIFactory), (void **)&pIDXGIFactory);
    

    其实,我们也可以直接调用D3D11CreateDeviceAndSwapChain同时创建Device和SwapChain,但是DXGI_SWAP_CHAIN_DESC结构中包含Multisample相关字段,而在Device创建之前,是无法通过ID3D11Device::CheckMultisampleQualityLevels获取到正确值的。

    为BackBuffer创建View

    创建SwapChain的时候BackBuffer已经自动创建好了,我们只需做的是为它创建一个View。

    HRESULT CreateRenderTargetView(
      ID3D11Resource                      *pResource,
      const D3D11_RENDER_TARGET_VIEW_DESC *pDesc,
      ID3D11RenderTargetView              **ppRTView
    );
    

    View的用处其实就是用来指定的Buffer的用法,这样同一块Buffer可以多次复用。一个Buffer可以对应多个View。

    创建Depth/Stencil Buffer,并为之创建View

    HRESULT CreateTexture2D(
      const D3D11_TEXTURE2D_DESC   *pDesc,
      const D3D11_SUBRESOURCE_DATA *pInitialData,
      ID3D11Texture2D              **ppTexture2D
    );
    
    HRESULT CreateDepthStencilView(
      ID3D11Resource                      *pResource,
      const D3D11_DEPTH_STENCIL_VIEW_DESC *pDesc,
      ID3D11DepthStencilView              **ppDepthStencilView
    );
    

    首先通过ID3D11Device::CreateTexture2D创建出Buffer,然后调用ID3D11Device::CreateDepthStencilView创建View。值得注意的是,D3D11_TEXTURE2D_DESC结构中的Usage字段:参见

    Resource Usage Default Dynamic Immutable Staging
    GPU-Read yes yes yes yes
    GPU-Write yes yes
    CPU-Read yes
    CPU-Write yes yes

    将View绑定到Context中

    void OMSetRenderTargets(
      UINT                   NumViews,
      ID3D11RenderTargetView * const *ppRenderTargetViews,
      ID3D11DepthStencilView *pDepthStencilView
    );
    

    ID3D11DeviceContext负责绑定View。

    设置Viewport

    void RSSetViewports(
      UINT                 NumViewports,
      const D3D11_VIEWPORT *pViewports
    );
    

    ID3D11DeviceContext负责设置Viewport。

  • 相关阅读:
    [poj3974] Palindrome 解题报告 (hashmanacher)
    SQL Android
    SharedPreferences Android
    本地广播 localBroadcastManager Android
    RecyclerView Android
    自定义ListView Android
    布局 Android
    传值 Android
    活动的生命周期 Android
    CodeForces 907F Power Tower(扩展欧拉定理)
  • 原文地址:https://www.cnblogs.com/back-to-the-past/p/12008703.html
Copyright © 2020-2023  润新知