Example #1
0
        /// <summary>
        /// Render scene
        /// </summary>
        public void Render()
        {
            // record all the commands we need to render the scene into the command list
            PopulateCommandLists();

            // execute the command list
            commandQueue.ExecuteCommandList(commandList);

            // swap the back and front buffers
            swapChain.Present(1, 0);

            // wait and reset EVERYTHING
            WaitForPrevFrame();

            RemoveAndDispose(ref renderTarget);
            if (width != newWidth || height != newHeight)
            {
                width  = newWidth;
                height = newHeight;
                swapChain.ResizeBuffers(SwapBufferCount, width, height, Format.Unknown, SwapChainFlags.None);
#if USE_DEPTH
                RemoveAndDispose(ref depthBuffer);
                depthBuffer = Collect(device.CreateCommittedResource(
                                          new HeapProperties(HeapType.Default),
                                          HeapFlags.None,
                                          new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 1, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil),
                                          ResourceStates.Common,
                                          new ClearValue
                {
                    Format       = Format.D32_Float,
                    DepthStencil = new DepthStencilValue
                    {
                        Depth   = 1,
                        Stencil = 0,
                    }
                }));
                device.CreateDepthStencilView(depthBuffer, null, descriptorHeapDS.CPUDescriptorHandleForHeapStart);
#endif
                // Create the viewport
                viewPort = new ViewportF(0, 0, width, height);

                // Create the scissor
                scissorRectangle = new Rectangle(0, 0, width, height);
            }
            renderTarget = Collect(swapChain.GetBackBuffer <Resource>(swapChain.CurrentBackBufferIndex));
            device.CreateRenderTargetView(renderTarget, null, descriptorHeapRT.CPUDescriptorHandleForHeapStart);
        }
Example #2
0
        public void Resize(System.Drawing.Size size)
        {
            Size = size;
            foreach (var renderTarget in RenderTargets)
            {
                renderTarget.Dispose();
            }
            SwapChain3.ResizeBuffers(FrameCount, size.Width, size.Height, SharpDX.DXGI.Format.R8G8B8A8_UNorm, SharpDX.DXGI.SwapChainFlags.AllowModeSwitch);
            FrameIndex = SwapChain3.CurrentBackBufferIndex;
            var rtvHandle = RenderTargetViewHeap.CPUDescriptorHandleForHeapStart;

            for (int i = 0; i < FrameCount; i++)
            {
                RenderTargets[i] = SwapChain3.GetBackBuffer <SharpDX.Direct3D12.Resource>(i);
                D3D12Device.CreateRenderTargetView(RenderTargets[i], null, rtvHandle);
                rtvHandle     += RtvDescriptorSize;
                FenceValues[i] = FenceValues[FrameIndex];
            }
        }
        private void CreateWindowResources()
        {
            // Wait until all previous GPU work is complete.
            WaitForGPU();

            // Clear the previous window size specific content.
            for (int i = 0; i < FrameCount; i++)
            {
                renderTargets[i] = null;
            }

            // Calculate the necessary render target size in pixels.
            var outputSize = new Size2();

            outputSize.Width  = window.ClientSize.Width;
            outputSize.Height = window.ClientSize.Height;

            // Prevent zero size DirectX content from being created.
            outputSize.Width  = Math.Max(outputSize.Width, 640);
            outputSize.Height = Math.Max(outputSize.Width, 480);

            if (swapChain != null)
            {
                // If the swap chain already exists, resize it.
                swapChain.ResizeBuffers(FrameCount, outputSize.Width, outputSize.Height, Format.B8G8R8A8_UNorm, SwapChainFlags.None);

                if (!DXDebug.ValidateDevice(device))
                {
                    throw new ArgumentNullException(nameof(device));
                }
            }
            else
            {
                using (var factory = new Factory4())
                {
                    // Otherwise, create a new one using the same adapter as the existing Direct3D device.
                    SwapChainDescription swapChainDesc = new SwapChainDescription()
                    {
                        BufferCount       = FrameCount,
                        ModeDescription   = new ModeDescription(outputSize.Width, outputSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                        Usage             = Usage.RenderTargetOutput,
                        SwapEffect        = SwapEffect.FlipDiscard,
                        OutputHandle      = window.Handle,
                        SampleDescription = new SampleDescription(1, 0),
                        IsWindowed        = true,
                    };

                    var tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
                    swapChain           = tempSwapChain.QueryInterface <SwapChain3>();
                    swapChain.DebugName = "SwapChain";
                    tempSwapChain.Dispose();
                }
            }

            // Create a render target view of the swap chain back buffer.
            var descriptorHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = FrameCount,
                Type            = DescriptorHeapType.RenderTargetView,
                Flags           = DescriptorHeapFlags.None
            };

            rtvHeap      = device.CreateDescriptorHeap(descriptorHeapDesc);
            rtvHeap.Name = "Render Target View Descriptor Heap";

            // All pending GPU work was already finished. Update the tracked fence values
            // to the last value signaled.
            for (int i = 0; i < FrameCount; i++)
            {
                fenceValues[i] = fenceValues[currentFrame];
            }

            currentFrame = 0;
            var rtvDescriptor = rtvHeap.CPUDescriptorHandleForHeapStart;

            rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            for (int i = 0; i < FrameCount; i++)
            {
                renderTargets[i] = swapChain.GetBackBuffer <Resource>(i);
                device.CreateRenderTargetView(renderTargets[i], null, rtvDescriptor + (rtvDescriptorSize * i));

                renderTargets[i].Name = $"Render Target {i}";
            }

            viewport          = new ViewportF();
            viewport.Width    = outputSize.Width;
            viewport.Height   = outputSize.Height;
            viewport.MaxDepth = 1.0f;

            scissorRect        = new Rectangle();
            scissorRect.Right  = outputSize.Width;
            scissorRect.Bottom = outputSize.Height;
        }