Direct3D 12 Early Adopters: Basic presentation

Source: Internet
Author: User
Tags clear screen windows insider

(reproduced please specify the source)

Please call me dig the Pit Maniac _ (: 3"∠) _

Microsoft released the WIN10 development tool a few days ago, hoping to use the children's shoes to join the Windows Insider program for download.

Here's my environment:
    • Windows Ten Technical Preview Build 10041
    • Visual Studio CTP 6
    • Visual Studio Tools for Windows 10

Of course, a virtual machine is used.

D3D12 documents can be viewed in official documents, with programming wizards and API documentation. However, this document is preliminary , and the link may be invalidated.

Initialization

Initializing the COM component and creating the window is no longer a matter of exhaustion, and directly into the topic:
Similar to D3d11, using D3d12createdevice to create a d3d12 device, the function is now declared as follows:

HRESULT WINAPI D3D12CreateDevice(    IDXGIAdapter* pAdapter,    D3D_DRIVER_TYPE DriverType,    D3D12_CREATE_DEVICE_FLAG Flags,    D3D_FEATURE_LEVEL MinimumFeatureLevel,    UINT SDKVersion,    REFIID riid,    void** ppDevice );

The first one is the explicit SFF adapter, which can be enumerated, can be nullptr,
The second one is the drive type, I am here the virtual machine, so choose warp
The third is to create a flag (no RGBA support, i.e. cannot link d2d?)
The fourth one is the feature level, and there is no 12 rating, so choose 11.1
The fifth one is the SDK version, using the macro d3d12_sdk_version
The fifth sixth one is also familiar, if only with the MSC compiled, you can use the macro
Iid_ppv_args, but for a compiler like GCC, write it by hand:

        D3D12_CREATE_DEVICE_FLAG flags = D3D12_CREATE_DEVICE_NONE;#ifdef _DEBUG        flags |= D3D12_CREATE_DEVICE_DEBUG;#endif        hr = ::D3D12CreateDevice(            nullptr,            D3D_DRIVER_TYPE_WARP,            flags,            D3D_FEATURE_LEVEL_11_1,            D3D12_SDK_VERSION,            IID_ID3D12Device,            reinterpret_cast<void**>(&m_pd3dDevice)            );

Do not know whether it is a bug or not implemented or what the reason, not as d3d11 as the use of D3D devices to obtain Dxgi devices , and then Balabala create a swap chain. So here we use CreateDXGIFactory2 to create the Dxgi plant.

        hr = ::CreateDXGIFactory2(            0,            IID_IDXGIFactory2,             reinterpret_cast<void**>(&m_pDxgiFactory)            );

Then use Idxgifactory2::createswapchainforhwnd to create a swap chain for the window, it is important to note that the first parameter, used D3D11 children's shoes may habitually pass a D3D12 device pointer, but this is wrong , The call may not be a problem, but the rendering will be error, the first parameter should pass a id3d12commandqueue pointer, so we should also create a D3D12 command queue, the Official wizard can get a default queue, but found that the interface is now removed, It can only be created directly:

    //create command queue    if(SUCCEEDED (HR)) {D3d12_command_queue_desc DESC = {D3d12_command_list_type_direct,0, D3d12_command_queue_none,0}; hr = M_pd3ddevice->createcommandqueue (&desc, Iid_id3d12commandqueue,reinterpret_cast<void**> (&m_pcmdqueue)); }//Create swap chain    if(SUCCEEDED (HR)) {Rect rect = {0}; :: GetClientRect (M_hwnd, &rect);//Exchange chain informationDxgi_swap_chain_desc1 Swapchaindesc = {0};        M_ubufferwidth = Swapchaindesc.width = Rect.right-rect.left;        M_ubufferheight = Swapchaindesc.height = Rect.bottom-rect.top;        Swapchaindesc.format = Dxgi_format_b8g8r8a8_unorm;        Swapchaindesc.stereo = FALSE; SwapChainDesc.SampleDesc.Count =1; SwapChainDesc.SampleDesc.Quality =0;        Swapchaindesc.bufferusage = Dxgi_usage_render_target_output; Swapchaindesc.buffercount =2;        swapchaindesc.scaling = Dxgi_scaling_stretch; Swapchaindesc.flags =0;//General desktop ApplicationsSwapchaindesc.alphamode = dxgi_alpha_mode_unspecified; Swapchaindesc.swapeffect = Dxgi_swap_effect_discard;//using the window handle to create a swap chainhr = M_pdxgifactory->createswapchainforhwnd (M_pcmdqueue, M_hwnd, &swapchaindesc,nullptr,nullptr, &m_pswapchain); }

D3D12 is clearly the command list id3d12commandlist, where one implementation is id3d12graphicscommandlist, similar to the command list in direct2d, but can be reset.
The feature of this image command list is to record Drawcall, close it, and then reproduce it efficiently.

To create a list of image commands, we need to create a command allocator, because you can specify the type of allocator, or you can create a command list directly from the device context to D2D, using Id3d12device::createcommandqueue:

    // 创建命令队列    if (SUCCEEDED(hr)) {        D3D12_COMMAND_QUEUE_DESC desc = {            D3D12_COMMAND_LIST_TYPE_DIRECT,            0,            D3D12_COMMAND_QUEUE_NONE,            0        };        hr = m_pd3dDevice->CreateCommandQueue(            &desc,            IID_ID3D12CommandQueue,            reinterpret_cast<void**>(&m_pCmdQueue)            );    }

This creates a d3d12_command_list_type_direct type allocator, which is a list of commands that can be created by the GPU executable.

Now you can finally create an image command list id3d12device::createcommandlist:

    // 创建图像命令列表    if (SUCCEEDED(hr)) {        hr = m_pd3dDevice->CreateCommandList(            0, D3D12_COMMAND_LIST_TYPE_DIRECT,            m_pCmdAllocator,            nullptr,            IID_ID3D12GraphicsCommandList,            reinterpret_cast<void**>(&m_pGfxCmdList)            );    }

This image command list is the same as the D3D11 device context where the (JI) row (LU)-Specific rendering commands are available:

In D3d11, we can get a 2D texture from the swap chain, but there is no 2D texture in the d3d12, instead of the Id3d12resource interface that can represent the resource, just get it right:

    // 获取缓冲区    if (SUCCEEDED(hr)) {        hr = m_pSwapChain->GetBuffer(            0, IID_ID3D12Resource,            reinterpret_cast<void**>(&m_pTargetBuffer)        );    }

Similarly, in D3d11, you can use the device to create an RTV
(Id3d11device::createrendertargetview),

In D3d12, all the resources are bound to the "descriptor" identifier, as well as descriptor tables, descriptor heaps, root signature, and more, see resource bindings. Here is mainly the descriptor and descriptor heap, the main difference, the former is the single latter is continuous.
Because (currently) there is no id3d12device::createdescriptor, so use Id3d12device::createdescriptorheap instead, create a good, and later have the same resources need to bind, You can create a descriptor heap to use together.

 //Create RTV Descriptor  if  (SUCCEEDED (HR)) {D3d12_descriptor_heap_desc DESC = {D3d12_rtv_descripto R_heap, 1 , D3d12_descriptor_heap_none, 0 }; hr = M_pd3ddevice->createdescriptorheap (&desc, iid_id3d12descriptorheap, reinterpret_cast  <void  **> (&m_prtvdescriptor)); } //create RTV  if  (SUCCEEDED (HR)) {M_pd3ddev            Ice->createrendertargetview (M_ptargetbuffer, nullptr ,    M_prtvdescriptor->getcpudescriptorhandleforheapstart ()); }

D3d11 Similar, create RTV, but no dedicated interface, with this descriptor is good, from here and above can be seen, d3d12 without a lot of interfaces, generalization.

In D3d11, the clear screen is simple:
    1. Set RTV for the current device context in the OM phase
    2. Cleaning RTV
In d3d12:
    1. Set RTV for an image command list in the RS phase
    2. For this list of commands for the resource (render cache) setting barrier indicates from "for rendering" to "for render object"
    3. Clear RTV
    4. Set barrier, change back
    5. Close this command list
    6. Join the command queue and execute

Can see a few more steps, in fact, two more steps: Set the resources barrier past, set barrier back. (Because D3d12 has done a lot of multithreaded rendering?) A resource is barrier to handle multiple accesses to a resource.

For a list of commands, you can reset it if you no longer use it .
This part of the code is not put up, you can see the accompanying instance code :

Here's the result map.

Known Issues

Errors can be seen from the Output window:

This error has a chance to say it again _ (: 3"∠) _

The code 's on top, don't miss it.

Direct3D 12 Early Adopters: Basic presentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.