/// <summary>	
        /// Get the array of {{viewports}} bound  to the {{rasterizer stage}} 	
        /// </summary>	
        /// <returns>An array of viewports (see <see cref="SharpDX.Direct3D11.Viewport"/>).</returns>
        /// <unmanaged>void RSGetViewports([InOut] int* NumViewports,[Out, Buffer, Optional] D3D10_VIEWPORT* pViewports)</unmanaged>
        /// <msdn-id>ff476477</msdn-id>	
        /// <unmanaged>void ID3D11DeviceContext::RSGetViewports([InOut] unsigned int* pNumViewports,[Out, Buffer, Optional] D3D11_VIEWPORT* pViewports)</unmanaged>	
        /// <unmanaged-short>ID3D11DeviceContext::RSGetViewports</unmanaged-short>	
        public SharpDX.Direct3D11.Viewport[] GetViewports()
        {
            int numViewports = 0;

            GetViewports(ref numViewports, null);

            SharpDX.Direct3D11.Viewport[] viewports = new SharpDX.Direct3D11.Viewport[numViewports];
            GetViewports(ref numViewports, viewports);

            return viewports;
        }
Esempio n. 2
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

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

            // 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.
            using (var backBuffer = 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.Direct3D11.Viewport((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;
        }
Esempio n. 3
0
        protected virtual void CreateSizeDependentResources(TargetBase renderBase)
        {
            var d3dDevice  = DeviceManager.DeviceDirect3D;
            var d3dContext = DeviceManager.ContextDirect3D;
            var d2dContext = DeviceManager.ContextDirect2D;

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

            // 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.
            using (var backBuffer = 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.Direct3D11.Viewport((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;
        }
Esempio n. 4
0
        public void DrawPlaceMarks()
        {
            // todo11 port this Maybe instancing later?
            Matrix   projection = Earth3d.MainWindow.RenderContext11.Projection.Matrix11;
            Matrix   view       = Earth3d.MainWindow.RenderContext11.View.Matrix11;
            Matrix   world      = Earth3d.MainWindow.RenderContext11.World.Matrix11;
            Matrix3d worldD     = Earth3d.MainWindow.RenderContext11.World;
            Matrix   wvp        = (world * view * projection);

            try
            {
                Vector3 center = new Vector3(0f, 0f, 0);

                foreach (KmlPlacemark placemark in Placemarks)
                {
                    if (placemark.ShouldDisplay())
                    {
                        SharpDX.Direct3D11.Viewport vp = Earth3d.MainWindow.RenderContext11.ViewPort;
                        double   alt     = placemark.Point.altitude + EGM96Geoid.Height(placemark.Point.latitude, placemark.Point.longitude);
                        Vector3d point3d = Coordinates.GeoTo3dDouble(placemark.Point.latitude, placemark.Point.longitude, 1 + (alt / Earth3d.MainWindow.RenderContext11.NominalRadius));
                        Vector3  point   = Vector3.Project(point3d.Vector311, vp.TopLeftX, vp.TopLeftY, vp.Width, vp.Height, 0, 1, wvp);
                        // point.Z = 1;
                        KmlStyle  style   = placemark.Style.GetStyle(placemark.Selected);
                        Texture11 texture = style.IconStyle.Icon.Texture;

                        if (String.IsNullOrEmpty(style.IconStyle.Icon.Href))
                        {
                            texture = Star;
                        }

                        double sizeFactor = 1;

                        if (placemark.Selected)
                        {
                            double ticks          = HiResTimer.TickCount;
                            double elapsedSeconds = ((double)(ticks - TicksAtLastSelect)) / HiResTimer.Frequency;
                            sizeFactor = 1 + .3 * (Math.Sin(elapsedSeconds * 15) * Math.Max(0, (1 - elapsedSeconds)));
                        }

                        point3d.TransformCoordinate(worldD);
                        Vector3d dist     = Earth3d.MainWindow.RenderContext11.CameraPosition - point3d;
                        double   distance = dist.Length() * Earth3d.MainWindow.RenderContext11.NominalRadius;
                        dist.Normalize();
                        double dot = Vector3d.Dot(point3d, dist);
                        // if (dot > -.2)
                        {
                            double baseSize = Math.Min(40, 25 * ((2 * Math.Atan(.5 * (5884764 / distance))) / .7853)) * sizeFactor;
                            float  size     = (float)baseSize * style.IconStyle.Scale;
                            //todo fix this with real centers and offset by KML data
                            placemark.hitTestRect = new Rectangle((int)(point.X - (size / 2)), (int)(point.Y - (size / 2)), (int)(size + .5), (int)(size + .5));
                            if (texture != null)
                            {
                                center = new Vector3((float)texture.Width / 2f, (float)texture.Height / 2f, 0);

                                Sprite2d.Draw2D(Earth3d.MainWindow.RenderContext11, texture, new SizeF(size, size), new PointF(center.X, center.Y), (float)(style.IconStyle.Heading * Math.PI / 180f), new PointF(point.X, point.Y), Color.White);
                            }

                            if (style.LabelStyle.Color.A > 0 && style.LabelStyle.Scale > 0)
                            {
                                Rectangle recttext = new Rectangle((int)(point.X + (size / 2) + 10), (int)(point.Y - (size / 2)), 1000, 100);
                                //todo11 Earth3d.MainWindow.labelFont.DrawText(null, placemark.Name, recttext, DrawTextFormat.NoClip, style.LabelStyle.Color);
                            }
                        }
                    }
                }
            }
            finally
            {
            }
        }