示例#1
0
        public override void Render()
        {
            _deviceContext.ClearState();

            _deviceContext.ClearRenderTargetView(_renderTargetview, Color.CornflowerBlue);
            _deviceContext.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);

            _deviceContext.OutputMerger.SetTargets(_depthStencilView, _renderTargetview);

            var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height);

            _deviceContext.Rasterizer.SetViewport(viewport);

            _deviceContext.InputAssembler.SetVertexBuffers(0, _vertexBufferBinding);
            _deviceContext.InputAssembler.InputLayout       = _vertexLayout;
            _deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            _deviceContext.UpdateSubresource(ref _worldViewProj, _constantBuffer);
            _deviceContext.VertexShader.SetConstantBuffer(0, _constantBuffer);
            _deviceContext.VertexShader.Set(_vertexShader);

            _deviceContext.PixelShader.Set(_pixelShader);

            _deviceContext.Draw(36, 0);
        }
示例#2
0
        /// <summary>
        /// Shows windows at screen center
        /// </summary>
        /// <param name="screen"></param>
        /// <param name="viewport"></param>
        public void Show(MainScreen screen, SharpDX.ViewportF viewport)
        {
            Bounds.Location.X = (viewport.Width - Bounds.Size.X.Offset) / 2;
            Bounds.Location.Y = (viewport.Height - Bounds.Size.Y.Offset) / 2;

            Show(screen);
        }
示例#3
0
 private void engine_ScreenSize_Updated(SharpDX.ViewportF viewport, Texture2DDescription newBackBuffer)
 {
     CreateRenderTargets(newBackBuffer);
     if (_activatedEffect != null)
     {
         _activatedEffect.RefreshBackBuffer(_renderTextureView);
     }
 }
示例#4
0
        public virtual void CreateWindowSizeDependentResources()
        {
            Texture2DDescription renderTargetDesc = new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Width             = (int)_renderTargetSize.Width,
                Height            = (int)_renderTargetSize.Height,
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage             = ResourceUsage.Default,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
            };


            // Allocate a 2-D surface as the render target buffer.
            _renderTarget = ToDispose(new Texture2D(_device, renderTargetDesc));

            _renderTargetview = ToDispose(new RenderTargetView(_device, _renderTarget));

            Texture2DDescription depthStencilDesc = new Texture2DDescription()
            {
                Format            = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                Width             = (int)_renderTargetSize.Width,
                Height            = (int)_renderTargetSize.Height,
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.DepthStencil,
                Usage             = ResourceUsage.Default,
                CpuAccessFlags    = CpuAccessFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                OptionFlags       = ResourceOptionFlags.None
            };


            Texture2D depthStencil = ToDispose(new Texture2D(_device, depthStencilDesc));

            //DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
            //{
            //     Dimension = DepthStencilViewDimension.Texture2D,
            //};

            Utilities.Dispose(ref _depthStencilView);
            _depthStencilView = ToDispose(new DepthStencilView(_device, depthStencil)); //, depthStencilViewDesc));


            _windowBounds.Width  = _renderTargetSize.Width;
            _windowBounds.Height = _renderTargetSize.Height;

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height);

            _deviceContext.Rasterizer.SetViewport(viewport);
        }
示例#5
0
文件: Ray.cs 项目: conankzhang/fez
        public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
        {
            Vector3 vector1   = new Vector3((float)x, (float)y, 0.0f);
            Vector3 vector2   = new Vector3((float)x, (float)y, 1f);
            Vector3 position  = Vector3.Unproject(vector1, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth, viewport.MaxDepth, worldViewProjection);
            Vector3 direction = Vector3.Unproject(vector2, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth, viewport.MaxDepth, worldViewProjection) - position;

            direction.Normalize();
            return(new Ray(position, direction));
        }
示例#6
0
 public static void SetViewport(MyRenderContext RC, MyStereoRegion region)
 {
     SharpDX.ViewportF viewport = new SharpDX.ViewportF(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
     if (region == MyStereoRegion.LEFT)
     {
         viewport = new SharpDX.ViewportF(viewport.X, viewport.Y, viewport.Width / 2, viewport.Height);
     }
     else if (region == MyStereoRegion.RIGHT)
     {
         viewport = new SharpDX.ViewportF(viewport.X + viewport.Width / 2, viewport.Y, viewport.Width / 2, viewport.Height);
     }
     RC.DeviceContext.Rasterizer.SetViewport(viewport);
 }
示例#7
0
文件: Ray.cs 项目: oeoen/SharpDX
        /// <summary>
        /// Calculates a world space <see cref="SharpDX.Ray"/> from 2d screen coordinates.
        /// </summary>
        /// <param name="x">X coordinate on 2d screen.</param>
        /// <param name="y">Y coordinate on 2d screen.</param>
        /// <param name="viewport"><see cref="SharpDX.ViewportF"/>.</param>
        /// <param name="worldViewProjection">Transformation <see cref="SharpDX.Matrix"/>.</param>
        /// <returns>Resulting <see cref="SharpDX.Ray"/>.</returns>
        public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
        {
            var nearPoint = new Vector3(x, y, 0);
            var farPoint  = new Vector3(x, y, 1);

            nearPoint = Vector3.Unproject(nearPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
                                          viewport.MaxDepth, worldViewProjection);
            farPoint = Vector3.Unproject(farPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
                                         viewport.MaxDepth, worldViewProjection);

            Vector3 direction = farPoint - nearPoint;

            direction.Normalize();

            return(new Ray(nearPoint, direction));
        }
示例#8
0
        /// <summary>
        /// Function to resize the rendering region.
        /// </summary>
        /// <param name="size">The new size of the render region.</param>
        public void ResizeRenderRegion()
        {
            float newWidth       = (_swapChain.Width / 5.0f).Max(64).Min(640);
            float aspect         = (float)_swapChain.Height / _swapChain.Width;
            var   cubeRegionSize = new DX.Size2F(newWidth, newWidth * aspect);

            VolumeRegion = new DX.RectangleF(_swapChain.Width - cubeRegionSize.Width - 1, 1, cubeRegionSize.Width, cubeRegionSize.Height);
            DX.Matrix.PerspectiveFovLH(60.0f.ToRadians(), (float)_swapChain.Width / _swapChain.Height, 0.1f, 1000.0f, out _projection);
            _cubeView = new DX.ViewportF(VolumeRegion.Left, VolumeRegion.Top, VolumeRegion.Width, VolumeRegion.Height, 0, 1);

            if (_textureView == null)
            {
                return;
            }

            RebuildVolumeData();
        }
示例#9
0
        public virtual void CreateWindowSizeDependentResources(bool isNewDevice)
        {
            var resource = _renderTargetview.Resource;

            using (var texture2D = new Texture2D(resource.NativePointer))
            {
                var currentWidth  = (int)_renderTargetSize.Width;
                var currentHeight = (int)_renderTargetSize.Height;

                if ((currentWidth != texture2D.Description.Width &&
                     currentHeight != texture2D.Description.Height) || isNewDevice)
                {
                    _renderTargetSize.Width  = texture2D.Description.Width;
                    _renderTargetSize.Height = texture2D.Description.Height;

                    Utilities.Dispose(ref _depthStencilView);

                    using (var depthTexture = new Texture2D(
                               _device,
                               new Texture2DDescription()
                    {
                        Width = (int)_renderTargetSize.Width,
                        Height = (int)_renderTargetSize.Height,
                        ArraySize = 1,
                        BindFlags = BindFlags.DepthStencil,
                        CpuAccessFlags = CpuAccessFlags.None,
                        Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                        MipLevels = 1,
                        OptionFlags = ResourceOptionFlags.None,
                        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                        Usage = ResourceUsage.Default
                    }))
                        _depthStencilView = new DepthStencilView(_device, depthTexture);
                }
            }

            _windowBounds.Width  = _renderTargetSize.Width;
            _windowBounds.Height = _renderTargetSize.Height;

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height);

            _deviceContext.Rasterizer.SetViewport(viewport);
        }
示例#10
0
        private void Resize(SharpDX.ViewportF viewport)
        {
            if (viewport.Height >= 620)
            {
                _headerHeight = (int)(viewport.Height * 0.3f);
            }
            else
            {
                _headerHeight = Math.Abs((int)viewport.Height - 434);
            }

            _cubes.Bounds   = new UniRectangle(0 + _borderOffset, 0 + _borderOffset, viewport.Width - (_borderOffset * 2), _headerHeight);
            _linen.Bounds   = new UniRectangle(0 + _borderOffset, _headerHeight, viewport.Width - (_borderOffset * 2), (viewport.Height - _headerHeight) - (_borderOffset * 2));
            _shadow.Bounds  = new UniRectangle(0, _headerHeight - 117, viewport.Width, 287);
            _logo.Bounds    = new UniRectangle((viewport.Width - 562) / 2, _headerHeight - 44, 562, 113);
            _version.Bounds = new UniRectangle((viewport.Width - 490) / 2 + 360, _headerHeight + 49, 89, 31);

            foreach (RotationCube cube in _rotatingCubes)
            {
                switch (cube.ID)
                {
                case 0:
                    cube.Scale          = _headerHeight * 0.4f;
                    cube.ScreenPosition = new Vector3((_engine.ViewPort.Width) * 0.23f, (_headerHeight) / 2, 0);
                    break;

                case 1:
                    cube.Scale          = 60;
                    cube.ScreenPosition = new Vector3(cube.Scale + 15, (_headerHeight), 0);
                    break;

                case 2:
                    cube.Scale          = 30;
                    cube.ScreenPosition = new Vector3(cube.Scale + 15, (_engine.ViewPort.Height - _headerHeight) / 4 + _headerHeight, 0);
                    break;

                case 3:
                    cube.Scale          = 40;
                    cube.ScreenPosition = new Vector3((_engine.ViewPort.Width) / 7, 15, 0);
                    break;
                }
            }
        }
        private void CreateWindowSizeDependentResources(Size2F size)
        {
            Texture2DDescription renderTargetDesc = new Texture2DDescription
            {
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Width = (int)size.Width,
                Height = (int)size.Height,
                ArraySize = 1,
                MipLevels = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage = ResourceUsage.Default,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
            };

            _renderTarget = ToDispose(new Texture2D(_device, renderTargetDesc));
            _renderTargetView = ToDispose(new RenderTargetView(_device, _renderTarget));

            var viewport = new SharpDX.ViewportF(0, 0, (float)size.Width, (float)size.Height);
            _deviceContext.Rasterizer.SetViewport(viewport);
        }
        private void CreateWindowSizeDependentResources(Size2F size)
        {
            Texture2DDescription renderTargetDesc = new Texture2DDescription
            {
                Format            = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Width             = (int)size.Width,
                Height            = (int)size.Height,
                ArraySize         = 1,
                MipLevels         = 1,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage             = ResourceUsage.Default,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
            };

            _renderTarget     = ToDispose(new Texture2D(_device, renderTargetDesc));
            _renderTargetView = ToDispose(new RenderTargetView(_device, _renderTarget));

            var viewport = new SharpDX.ViewportF(0, 0, (float)size.Width, (float)size.Height);

            _deviceContext.Rasterizer.SetViewport(viewport);
        }
示例#13
0
        /// <summary>
        /// Function to perform the actual rendering.
        /// </summary>
        public void Render()
        {
            UpdateCubeTransform();

            // Draw the window with our volume texture mapped to a cube.
            GorgonRenderTargetView currentRtv = _graphics.RenderTargets[0];

            DX.ViewportF oldViewport = _graphics.Viewports[0];

            // Draw the volume sections.
            _graphics.SetRenderTarget(_volumeRtSections[0]);
            _volumeRtSections[0].Clear(GorgonColor.BlackTransparent);
            _graphics.Submit(_cubePosDrawCull);
            _graphics.SetRenderTarget(_volumeRtSections[1]);
            _volumeRtSections[1].Clear(GorgonColor.BlackTransparent);
            _graphics.Submit(_cubePosDrawFrontCull);
            _graphics.SetRenderTarget(_volumeRtSections[2]);

            // Draw the actual cube.
            _graphics.SetRenderTarget(currentRtv);
            _graphics.SetViewport(ref _cubeView);
            _graphics.Submit(_cubeDirDrawCall);
            _graphics.SetViewport(ref oldViewport);
        }
示例#14
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            RemoveAndDispose(ref renderTargetView);
            RemoveAndDispose(ref depthStencilView);
            RemoveAndDispose(ref bitmapTarget);
            RemoveAndDispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain 
                using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>())
                using (var dxgiAdapter = dxgiDevice2.Adapter)
                using (var dxgiFactory2 = dxgiAdapter.GetParent<SharpDX.DXGI.Factory2>())
                {
                    swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc));

                    // Ensure that DXGI does not queue more than one frame at a time. This both reduces 
                    // latency and ensures that the application will only render after each VSync, minimizing 
                    // power consumption.
                    dxgiDevice2.MaximumFrameLatency = 1;
                }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = ToDispose(SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0));
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer));

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription() { Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D }));

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewports(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain. 
            // Whenever we render to this bitmap, it will be directly rendered to the 
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer<SharpDX.DXGI.Surface>(0))
                bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;            
        }
 public static void SetViewport(MyRenderContext RC, MyStereoRegion region)
 {
     SharpDX.ViewportF viewport = new SharpDX.ViewportF(0, 0, MyRender11.ViewportResolution.X, MyRender11.ViewportResolution.Y);
     if (region == MyStereoRegion.LEFT)
         viewport = new SharpDX.ViewportF(viewport.X, viewport.Y, viewport.Width / 2, viewport.Height);
     else if (region == MyStereoRegion.RIGHT)
         viewport = new SharpDX.ViewportF(viewport.X + viewport.Width / 2, viewport.Y, viewport.Width / 2, viewport.Height);
     RC.DeviceContext.Rasterizer.SetViewport(viewport);
 }
示例#16
0
        public override void Render()
        {
            _deviceContext.ClearState();

            _deviceContext.ClearRenderTargetView(_renderTargetview, Color.CornflowerBlue);
            _deviceContext.ClearDepthStencilView(_depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);

            _deviceContext.OutputMerger.SetTargets(_depthStencilView, _renderTargetview);

            var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height);
            _deviceContext.Rasterizer.SetViewport(viewport);

            _deviceContext.InputAssembler.SetVertexBuffers(0, _vertexBufferBinding);
            _deviceContext.InputAssembler.InputLayout = _vertexLayout;
            _deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            _deviceContext.UpdateSubresource(ref _worldViewProj, _constantBuffer);
            _deviceContext.VertexShader.SetConstantBuffer(0, _constantBuffer);
            _deviceContext.VertexShader.Set(_vertexShader);

            _deviceContext.PixelShader.Set(_pixelShader);

            _deviceContext.Draw(36, 0);
        }
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice  = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

            d2dContext.Target = null;
            RemoveAndDispose(ref renderTargetView);
            RemoveAndDispose(ref depthStencilView);
            RemoveAndDispose(ref bitmapTarget);
            RemoveAndDispose(ref backBuffer);

            // If the swap chain already exists, resize it.
            if (swapChain != null)
            {
                swapChain.ResizeBuffers(2, Width, Height, SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.DXGI.SwapChainFlags.None);
            }
            // Otherwise, create a new one.
            else
            {
                // SwapChain description
                var desc = CreateSwapChainDescription();

                // Once the desired swap chain description is configured, it must be created on the same adapter as our D3D Device

                // First, retrieve the underlying DXGI Device from the D3D Device.
                // Creates the swap chain
                using (var dxgiDevice2 = d3dDevice.QueryInterface <SharpDX.DXGI.Device2>())
                    using (var dxgiAdapter = dxgiDevice2.Adapter)
                        using (var dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>())
                        {
                            swapChain = ToDispose(CreateSwapChain(dxgiFactory2, d3dDevice, desc));

                            // Ensure that DXGI does not queue more than one frame at a time. This both reduces
                            // latency and ensures that the application will only render after each VSync, minimizing
                            // power consumption.
                            dxgiDevice2.MaximumFrameLatency = 1;
                        }
            }

            // Obtain the backbuffer for this window which will be the final 3D rendertarget.
            backBuffer = ToDispose(SharpDX.Direct3D11.Texture2D.FromSwapChain <SharpDX.Direct3D11.Texture2D>(swapChain, 0));
            {
                // Create a view interface on the rendertarget to use on bind.
                renderTargetView = ToDispose(new SharpDX.Direct3D11.RenderTargetView(d3dDevice, BackBuffer));

                // Cache the rendertarget dimensions in our helper class for convenient use.
                var backBufferDesc = BackBuffer.Description;
                RenderTargetBounds = new Windows.Foundation.Rect(0, 0, backBufferDesc.Width, backBufferDesc.Height);
            }

            // Create a descriptor for the depth/stencil buffer.
            // Allocate a 2-D surface as the depth/stencil buffer.
            // Create a DepthStencil view on this surface to use on bind.
            using (var depthBuffer = new SharpDX.Direct3D11.Texture2D(d3dDevice, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                ArraySize = 1,
                MipLevels = 1,
                Width = (int)RenderTargetSize.Width,
                Height = (int)RenderTargetSize.Height,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
            }))
                depthStencilView = ToDispose(new SharpDX.Direct3D11.DepthStencilView(d3dDevice, depthBuffer, new SharpDX.Direct3D11.DepthStencilViewDescription()
                {
                    Dimension = SharpDX.Direct3D11.DepthStencilViewDimension.Texture2D
                }));

            // Create a viewport descriptor of the full window size.
            var viewport = new SharpDX.ViewportF((float)RenderTargetBounds.X, (float)RenderTargetBounds.Y, (float)RenderTargetBounds.Width, (float)RenderTargetBounds.Height, 0.0f, 1.0f);

            // Set the current viewport using the descriptor.
            d3dContext.Rasterizer.SetViewport(viewport);

            // Now we set up the Direct2D render target bitmap linked to the swapchain.
            // Whenever we render to this bitmap, it will be directly rendered to the
            // swapchain associated with the window.
            var bitmapProperties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                DeviceManager.Dpi,
                DeviceManager.Dpi,
                SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            // Direct2D needs the dxgi version of the backbuffer surface pointer.
            // Get a D2D surface from the DXGI back buffer to use as the D2D render target.
            using (var dxgiBackBuffer = swapChain.GetBackBuffer <SharpDX.DXGI.Surface>(0))
                bitmapTarget = ToDispose(new SharpDX.Direct2D1.Bitmap1(d2dContext, dxgiBackBuffer, bitmapProperties));

            // So now we can set the Direct2D render target.
            d2dContext.Target = BitmapTarget2D;

            // Set D2D text anti-alias mode to Grayscale to ensure proper rendering of text on intermediate surfaces.
            d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Grayscale;
        }
示例#18
0
 /// <summary>
 /// Calculates a world space <see cref="Ray"/> from 2d screen coordinates.
 /// Integer version converts values to float
 /// </summary>
 /// <param name="x">X coordinate on 2d screen.</param>
 /// <param name="y">Y coordinate on 2d screen.</param>
 /// <param name="viewport"><see cref="ViewportF"/>.</param>
 /// <param name="worldViewProjection">Transformation <see cref="Matrix"/>.</param>
 /// <returns>Resulting <see cref="Ray"/>.</returns>
 public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
 {
     return(GetPickRay((float)x, (float)y, viewport, worldViewProjection));
 }
示例#19
0
        public static void Run()
        {
            var featureLevels = new[]
            {
                FeatureLevel.Level_11_1,
                FeatureLevel.Level_11_0
            };
            const DeviceCreationFlags creationFlags = DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug;

            const int width  = 640;
            const int height = 480;

            using (var form = new RenderForm
            {
                Width = width,
                Height = height
            })
            {
                var swapChainDescription = new SwapChainDescription1
                {
                    Width             = form.ClientSize.Width,
                    Height            = form.ClientSize.Height,
                    Format            = Format.B8G8R8A8_UNorm,
                    Stereo            = false,
                    SampleDescription = new SampleDescription(4, 4),
                    Usage             = Usage.BackBuffer | Usage.RenderTargetOutput,
                    BufferCount       = 1,
                    Scaling           = Scaling.Stretch,
                    SwapEffect        = SwapEffect.Discard,
                    Flags             = SwapChainFlags.AllowModeSwitch
                };
                var swapChainFullScreenDescription = new SwapChainFullScreenDescription
                {
                    RefreshRate = new Rational(60, 1),
                    Scaling     = DisplayModeScaling.Centered,
                    Windowed    = true
                };

                var samplerStateDescription = new SamplerStateDescription
                {
                    AddressU = TextureAddressMode.Wrap,
                    AddressV = TextureAddressMode.Wrap,
                    AddressW = TextureAddressMode.Wrap,
                    Filter   = Filter.MinMagMipLinear
                };
                var rasterizerStateDescription = RasterizerStateDescription.Default();
                rasterizerStateDescription.IsFrontCounterClockwise = true;

                // Set up the graphics devices
                using (var device0 = new SharpDX.Direct3D11.Device(DriverType.Hardware, creationFlags, featureLevels))
                    using (var device1 = device0.QueryInterface <SharpDX.Direct3D11.Device1>())
                        using (var context = device0.ImmediateContext.QueryInterface <DeviceContext1>())


                            // Create shaders and related resources
                            using (var vertexShaderBytecode = ShaderBytecode.CompileFromFile("shaders.hlsl", "VSMain", "vs_5_0", ShaderFlags.Debug))
                                using (var vertexShader = new VertexShader(device1, vertexShaderBytecode))
                                    using (var pixelShaderBytecode = ShaderBytecode.CompileFromFile("shaders.hlsl", "PSMain", "ps_5_0", ShaderFlags.Debug))
                                        using (var pixelShader = new PixelShader(device1, pixelShaderBytecode))
                                            using (var inputLayout = new InputLayout(device1, ShaderSignature.GetInputSignature(vertexShaderBytecode), new[]
                                            {
                                                new InputElement("SV_Position", 0, Format.R32G32B32A32_Float, 0, 0),
                                                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                                                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0),
                                            }))
                                                using (var worldViewProjectionBuffer = new SharpDX.Direct3D11.Buffer(device1, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0))
                                                    using (var texture = TextureLoader.CreateTexture2DFromBitmap(device1, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "text.png")))
                                                        using (ShaderResourceView textureView = new ShaderResourceView(device1, texture))
                                                            using (var samplerState = new SamplerState(device1, samplerStateDescription))

                                                                // Prepare rendering targets and related resources
                                                                using (var dxgiDevice2 = device1.QueryInterface <SharpDX.DXGI.Device2>())
                                                                    using (var dxgiFactory2 = dxgiDevice2.Adapter.GetParent <Factory2>())
                                                                        using (var swapChain = new SwapChain1(dxgiFactory2, device1, form.Handle, ref swapChainDescription, swapChainFullScreenDescription))
                                                                            using (var backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0))
                                                                                using (var rasterizerState = new RasterizerState(device1, rasterizerStateDescription))
                                                                                    using (var renderTargetView = new RenderTargetView(device1, backBuffer))
                                                                                    {
                                                                                        var viewport = new ViewportF(0, 0, backBuffer.Description.Width, backBuffer.Description.Height);
                                                                                        context.Rasterizer.SetViewport(viewport);
                                                                                        context.Rasterizer.State = rasterizerState;

                                                                                        var depthBufferDescription = new Texture2DDescription
                                                                                        {
                                                                                            Format            = Format.D32_Float_S8X24_UInt,
                                                                                            ArraySize         = 1,
                                                                                            MipLevels         = 1,
                                                                                            Width             = backBuffer.Description.Width,
                                                                                            Height            = backBuffer.Description.Height,
                                                                                            SampleDescription = swapChain.Description.SampleDescription,
                                                                                            BindFlags         = BindFlags.DepthStencil,
                                                                                        };
                                                                                        var depthStencilViewDescription = new DepthStencilViewDescription
                                                                                        {
                                                                                            Dimension = swapChain.Description.SampleDescription.Count > 1 || swapChain.Description.SampleDescription.Quality > 0
                            ? DepthStencilViewDimension.Texture2DMultisampled
                            : DepthStencilViewDimension.Texture2D
                                                                                        };
                                                                                        var depthStencilStateDescription = new DepthStencilStateDescription
                                                                                        {
                                                                                            IsDepthEnabled   = true,
                                                                                            DepthComparison  = Comparison.Less,
                                                                                            DepthWriteMask   = DepthWriteMask.All,
                                                                                            IsStencilEnabled = false,
                                                                                            StencilReadMask  = 0xff,
                                                                                            StencilWriteMask = 0xff,
                                                                                            FrontFace        = new DepthStencilOperationDescription
                                                                                            {
                                                                                                Comparison         = Comparison.Always,
                                                                                                PassOperation      = StencilOperation.Keep,
                                                                                                FailOperation      = StencilOperation.Keep,
                                                                                                DepthFailOperation = StencilOperation.Increment
                                                                                            },
                                                                                            BackFace = new DepthStencilOperationDescription
                                                                                            {
                                                                                                Comparison         = Comparison.Always,
                                                                                                PassOperation      = StencilOperation.Keep,
                                                                                                FailOperation      = StencilOperation.Keep,
                                                                                                DepthFailOperation = StencilOperation.Decrement
                                                                                            }
                                                                                        };

                                                                                        using (var depthBuffer = new Texture2D(device1, depthBufferDescription))
                                                                                            using (var depthStencilView = new DepthStencilView(device1, depthBuffer, depthStencilViewDescription))
                                                                                                using (var depthStencilState = new DepthStencilState(device1, depthStencilStateDescription))
                                                                                                {
                                                                                                    context.OutputMerger.SetRenderTargets(depthStencilView, renderTargetView);
                                                                                                    context.OutputMerger.DepthStencilState = depthStencilState;
                                                                                                    context.InputAssembler.InputLayout     = inputLayout;
                                                                                                    context.VertexShader.SetConstantBuffer(0, worldViewProjectionBuffer);
                                                                                                    context.VertexShader.Set(vertexShader);
                                                                                                    context.PixelShader.Set(pixelShader);
                                                                                                    context.PixelShader.SetShaderResource(0, textureView);
                                                                                                    context.PixelShader.SetSampler(0, samplerState);

                                                                                                    form.Show();

                                                                                                    var cameraPosition      = new Vector3(1.5f, 1.8f, -3);
                                                                                                    var cameraTarget        = Vector3.Zero;
                                                                                                    var cameraUp            = Vector3.UnitY;
                                                                                                    var worldMatrix         = Matrix.Identity;
                                                                                                    var viewMatrix          = Matrix.LookAtLH(cameraPosition, cameraTarget, cameraUp);                                                        // reorient everything to camera space
                                                                                                    var projectionMatrix    = Matrix.PerspectiveFovLH((float)Math.PI / 3f, form.ClientSize.Width / (float)form.ClientSize.Height, .5f, 100f); // create a generic perspective projection matrix
                                                                                                    var viewProjection      = Matrix.Multiply(viewMatrix, projectionMatrix);                                                                  // apply the perspective projection to the view matrix so that we're performing both operations
                                                                                                    var worldViewProjection = worldMatrix * viewProjection;                                                                                   // include world translation with the view projection matrix
                                                                                                    worldViewProjection.Transpose();

                                                                                                    var model = GetCubeModel();
                                                                                                    using (var vertexBuffer = CreateBuffer(device1, BindFlags.VertexBuffer, model.Vertices))
                                                                                                        using (var indexBuffer = CreateBuffer(device1, BindFlags.IndexBuffer, model.Triangles))
                                                                                                        {
                                                                                                            var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vertex>(), 0);
                                                                                                            context.InputAssembler.SetVertexBuffers(0, vertexBufferBinding);
                                                                                                            context.InputAssembler.SetIndexBuffer(indexBuffer, Format.R32_UInt, 0);
                                                                                                            context.UpdateSubresource(ref worldViewProjection, worldViewProjectionBuffer);

                                                                                                            RenderLoop.Run(form, () =>
                                                                                                            {
                                                                                                                context.ClearRenderTargetView(renderTargetView, Color.CornflowerBlue);
                                                                                                                context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

                                                                                                                context.DrawIndexed(model.Triangles.Length * 3, 0, 0);

                                                                                                                swapChain.Present(1, PresentFlags.None);
                                                                                                            });
                                                                                                        }
                                                                                                }
                                                                                    }
            }
        }
示例#20
0
        public virtual void CreateWindowSizeDependentResources()
        {
            var resource = _renderTargetview.Resource;
            using (var texture2D = new Texture2D(resource.NativePointer))
            {

                var currentWidth = (int) _renderTargetSize.Width;
                var currentHeight = (int) _renderTargetSize.Height;

                if (currentWidth != texture2D.Description.Width &&
                    currentHeight != texture2D.Description.Height)
                {
                    _renderTargetSize.Width = texture2D.Description.Width;
                    _renderTargetSize.Height = texture2D.Description.Height;

                    Utilities.Dispose(ref _depthStencilView);

                    using (var depthTexture = new Texture2D(
                        _device,
                        new Texture2DDescription()
                            {
                                Width = (int) _renderTargetSize.Width,
                                Height = (int) _renderTargetSize.Height,
                                ArraySize = 1,
                                BindFlags = BindFlags.DepthStencil,
                                CpuAccessFlags = CpuAccessFlags.None,
                                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                                MipLevels = 1,
                                OptionFlags = ResourceOptionFlags.None,
                                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                                Usage = ResourceUsage.Default
                            }))
                        _depthStencilView = new DepthStencilView(_device, depthTexture);
                }
            }

            _windowBounds.Width = _renderTargetSize.Width;
            _windowBounds.Height = _renderTargetSize.Height;

            // Create a viewport descriptor of the full window size.
             var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height );

            _deviceContext.Rasterizer.SetViewport(viewport);
        }
示例#21
0
        public virtual void CreateWindowSizeDependentResources()
        {
            Texture2DDescription renderTargetDesc = new Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                Width = (int) _renderTargetSize.Width,
                Height = (int) _renderTargetSize.Height,
                ArraySize = 1,
                MipLevels = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Usage = ResourceUsage.Default,
                CpuAccessFlags =  CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.SharedKeyedmutex | ResourceOptionFlags.SharedNthandle,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0)
            };

            
            // Allocate a 2-D surface as the render target buffer.
            _renderTarget = ToDispose(new Texture2D(_device, renderTargetDesc));

            _renderTargetview = ToDispose(new RenderTargetView(_device, _renderTarget));

            Texture2DDescription depthStencilDesc = new Texture2DDescription()
            {
                Format = SharpDX.DXGI.Format.D24_UNorm_S8_UInt,
                Width = (int)_renderTargetSize.Width,
                Height = (int)_renderTargetSize.Height,
                ArraySize = 1,
                MipLevels = 1,
                BindFlags = BindFlags.DepthStencil,
                Usage = ResourceUsage.Default,
                CpuAccessFlags = CpuAccessFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0), 
                OptionFlags = ResourceOptionFlags.None

            };


            Texture2D depthStencil = ToDispose(new Texture2D(_device, depthStencilDesc));
            //DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
            //{
            //     Dimension = DepthStencilViewDimension.Texture2D,
            //};
            
            Utilities.Dispose(ref _depthStencilView);
            _depthStencilView = ToDispose(new DepthStencilView(_device, depthStencil)); //, depthStencilViewDesc));
            

            _windowBounds.Width = _renderTargetSize.Width;
            _windowBounds.Height = _renderTargetSize.Height;

            // Create a viewport descriptor of the full window size.
             var viewport = new SharpDX.ViewportF(0, 0, (float)_renderTargetSize.Width, (float)_renderTargetSize.Height );

            _deviceContext.Rasterizer.SetViewport(viewport);
        }