Exemplo n.º 1
0
        private void CompositionTarget_Rendering(object sender, object e)
        {
            _context.OutputMerger.SetRenderTargets(_depthStencilView, _renderView);
            _context.ClearDepthStencilView(_depthStencilView, D3D.DepthStencilClearFlags.Depth, 1.0f, 0);
            _context.ClearRenderTargetView(_renderView, new RawColor4(0.0f, 0.0f, 0.0f, 0.0f));

            var view     = SharpDX.Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
            var proj     = SharpDX.Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, (float)(panel.RenderSize.Width / panel.RenderSize.Height), 0.1f, 100.0f);
            var viewProj = SharpDX.Matrix.Multiply(view, proj);
            var period   = 2000.0;
            var time     = (float)(_timer.ElapsedMilliseconds % period);

            var worldViewProj = SharpDX.Matrix.Scaling(1) * SharpDX.Matrix.RotationX(0.0f * time)
                                * SharpDX.Matrix.RotationY((float)(2 * Math.PI * time / period)) * SharpDX.Matrix.RotationZ(1 * time * 0.0f)
                                * viewProj;

            worldViewProj.Transpose();

            _context.InputAssembler.SetVertexBuffers(0, _vertexBinding);
            _context.InputAssembler.InputLayout       = _inputLayout;
            _context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            _context.VertexShader.SetConstantBuffer(0, _constantBuffer);
            _context.VertexShader.Set(_vertexShader);
            _context.PixelShader.Set(_pixelShader);
            _context.Draw(_vertices.Length, 0);

            // Update Constant Buffer
            _context.UpdateSubresource(ref worldViewProj, _constantBuffer, 0);

            _swapChain.Present(1, DXGI.PresentFlags.None);
        }
Exemplo n.º 2
0
        public override void Swap()
        {
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type  = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags = SharpDX.Direct3D12.ResourceBarrierFlags.None;

            if (_sampleDesc.Count > 1)
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffersAA[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget,
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveSource)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);

                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.Present,
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveDestination)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);

                _commands.ResolveSubresource(_backBuffers[_frameIndex], 0, _backBuffersAA[_frameIndex], 0, _format);

                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.ResolveDestination,
                                                                                      SharpDX.Direct3D12.ResourceStates.Present)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);
            }
            else
            {
                barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(_backBuffers[_frameIndex],
                                                                                      SharpDX.Direct3D12.ResourceStates.RenderTarget,
                                                                                      SharpDX.Direct3D12.ResourceStates.Present)
                {
                    Subresource = 0
                };

                _commands.ResourceBarrier(barrier);
            }

            // TODO: transition video textures

            _commands.Close();
            _queue.ExecuteCommandList(_commands);

            _fenceValue++;
            _queue.Signal(_fence, _fenceValue);
            _frameFenceValues[_frameIndex] = _fenceValue;

            _swapChain.Present(_syncInterval, _presentFlags);
            _frameIndex = _swapChain.CurrentBackBufferIndex;
        }