예제 #1
0
        private void CreateDXGI()
        {
            var res = CreateDXGIFactory1 <IDXGIFactory4>(out factory);

            device = CreateDevice(factory);
            if (device == null)
            {
                throw new InvalidOperationException("device cannot be null");
            }
            commandQueue = CreateCommandQueue(device);
            swapChain    = CreateSwapChain(factory, commandQueue, Format.R8G8B8A8_UNorm);

            rtvHeap.usedEntries = 0;
            rtvHeap.Heap        = CreateDescriptorHeap(device, rtvHeapSize, DescriptorHeapType.RenderTargetView, false);

            frameObjects = new FrameObject[DefaultSwapChainBuffers];
            for (int i = 0; i < DefaultSwapChainBuffers; i++)
            {
                frameObjects[i] = new FrameObject();

                frameObjects[i].cmdAllocator    = device.CreateCommandAllocator(CommandListType.Direct);
                frameObjects[i].swapChainBuffer = swapChain.GetBuffer <ID3D12Resource>(i);
                frameObjects[i].rtvHandle       = CreateRenderTargetView(device, frameObjects[i].swapChainBuffer, rtvHeap.Heap,
                                                                         ref rtvHeap.usedEntries, Format.R8G8B8A8_UNorm_SRgb);
            }

            var cmdList = device.CreateCommandList(0, CommandListType.Direct, frameObjects[0].cmdAllocator, null);

            commandList = cmdList.QueryInterface <ID3D12GraphicsCommandList4>();

            fence      = device.CreateFence(0, FenceFlags.None);
            fenceEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
        }
예제 #2
0
        private void InitDXR(IntPtr winHandle, int winWidth, int winHeight)
        {
            mHwnd = winHandle;
            this.mSwapChainRect = new Rect(0, 0, winWidth, winHeight);

            // Initialize the debug layer for debug builds
#if DEBUG
            if (D3D12.D3D12GetDebugInterface <ID3D12Debug>(out var pDx12Debug).Success)
            {
                pDx12Debug.EnableDebugLayer();
            }
#endif
            // Create the DXGI factory
            IDXGIFactory4 pDXGIFactory;
            DXGI.CreateDXGIFactory1 <IDXGIFactory4>(out pDXGIFactory);
            mpDevice    = this.context.CreateDevice(pDXGIFactory);
            mpCmdQueue  = this.context.CreateCommandQueue(mpDevice);
            mpSwapChain = this.context.CreateDXGISwapChain(pDXGIFactory, mHwnd, winWidth, winHeight, Format.R8G8B8A8_UNorm, mpCmdQueue);

            // Create a RTV descriptor heap
            mRtvHeap.Heap = this.context.CreateDescriptorHeap(mpDevice, kRtvHeapSize, DescriptorHeapType.RenderTargetView, false);

            // Create the per-frame objects
            this.mFrameObjects = new FrameObject[this.context.kDefaultSwapChainBuffers];
            for (int i = 0; i < this.context.kDefaultSwapChainBuffers; i++)
            {
                mFrameObjects[i].pCmdAllocator    = mpDevice.CreateCommandAllocator(CommandListType.Direct);
                mFrameObjects[i].pSwapChainBuffer = mpSwapChain.GetBuffer <ID3D12Resource>(i);
                mFrameObjects[i].rtvHandle        = context.CreateRTV(mpDevice, mFrameObjects[i].pSwapChainBuffer, mRtvHeap.Heap, ref mRtvHeap.usedEntries, Format.R8G8B8A8_UNorm_SRgb);
            }

            // Create the command-list
            var cmdList = mpDevice.CreateCommandList(0, CommandListType.Direct, mFrameObjects[0].pCmdAllocator, null);
            this.mpCmdList = cmdList.QueryInterface <ID3D12GraphicsCommandList4>();

            // Create a fence and the event
            this.mpFence     = mpDevice.CreateFence(0, FenceFlags.None);
            this.mFenceEvent = new EventWaitHandle(false, EventResetMode.AutoReset);
        }