Example #1
0
        private void ReleaseResources()
        {
            ReleaseD2DObjects();
            ReleaseRenderingObjects();

            void ReleaseD2DObjects()
            {
                renderTarget2d?.Dispose();
                surface?.Dispose();
                renderView?.Dispose();
                backBuffer?.Dispose();
                swapChain?.Dispose();
                device?.Dispose();
                factoryWrite?.Dispose();
                factory2d?.Dispose();
                factory?.Dispose();
            }

            void ReleaseRenderingObjects()
            {
                foreach (var cell in cells)
                {
                    cell.Dispose();
                }
                textFormat?.Dispose();
                sColorBrush?.Dispose();
            }
        }
Example #2
0
        public void InitRendering()
        {
            try
            {
                lock (_drawLock)
                {
                    m_Ready      = false;
                    ResizeRedraw = true;
                    var desc = new SwapChainDescription
                    {
                        BufferCount     = 2,
                        ModeDescription = new ModeDescription(ClientSize.Width, ClientSize.Height, new Rational(60, 1),
                                                              Format.R8G8B8A8_UNorm),
                        IsWindowed        = true,
                        OutputHandle      = Handle,
                        SampleDescription = new SampleDescription(1, 0),
                        SwapEffect        = SwapEffect.Discard,
                        Usage             = Usage.RenderTargetOutput | Usage.Shared
                    };

                    Device.CreateWithSwapChain(DriverType.Hardware,
                                               DeviceCreationFlags.BgraSupport,
                                               new[] { SharpDX.Direct3D.FeatureLevel.Level_9_3 },
                                               desc,
                                               out device,
                                               out swapChain);

                    var d2dFactory = new SharpDX.Direct2D1.Factory();

                    Factory factory = swapChain.GetParent <Factory>();
                    factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);

                    Texture2D backBuffer = Resource.FromSwapChain <Texture2D>(swapChain, 0);

                    surface = backBuffer.QueryInterface <Surface>();

                    d2dRenderTarget = new SharpDX.Direct2D1.RenderTarget(d2dFactory, surface,
                                                                         new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));


                    var bitmapProperties = new BitmapProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Ignore));

                    clientArea = new RawRectangleF
                    {
                        Left   = 0,
                        Top    = 0,
                        Bottom = ClientSize.Height,
                        Right  = ClientSize.Width
                    };

                    factory.Dispose();
                    backBuffer.Dispose();
                    m_Ready = true;
                }
            }
            catch (Exception ee)
            {
            }
        }
Example #3
0
 public void Dispose()
 {
     renderView.Dispose();
     backBuffer.Dispose();
     device.Dispose();
     swapChain.Dispose();
     factory.Dispose();
 }
Example #4
0
 public void TestDXGI()
 {
     // Force to load DXGI assembly
     var factory = new Factory();
     factory.Dispose();
     // Look for DXGI descriptor SharpDX.DXGI.ResultCode.DeviceRemoved
     var descriptor = ResultDescriptor.Find(0x887A0005);
     Assert.AreEqual(descriptor.NativeApiCode, "DXGI_ERROR_DEVICE_REMOVED");
 }
Example #5
0
 //overall end drawing
 public static void dispose()
 {
     rtv.Dispose();
     backBuffer.Dispose();
     dev.ImmediateContext.ClearState();
     dev.ImmediateContext.Flush();
     dev.Dispose();
     sc.Dispose();
     fact.Dispose();
 }
Example #6
0
 public void Destroy()
 {
     swapChain.Dispose();
     backBuffer.Dispose();
     device.ImmediateContext.ClearState();
     device.ImmediateContext.Flush();
     device.Dispose();
     renderView.Dispose();
     factory.Dispose();
 }
Example #7
0
        public void Dispose()
        {
            // Release all resources

            figures.Dispose();

            if (ctx != null)
            {
                ctx.ClearState();
                ctx.Flush();
                ctx.Dispose();
            }

            if (il != null)
            {
                il.Dispose();
            }
            if (effect != null)
            {
                effect.Dispose();
            }

            if (hmd != null)
            {
                keyboard.Dispose();
                directInput.Dispose();

                mtex.Dispose();
                layers.Dispose();

                eye_texes[0].Dispose();
                eye_texes[1].Dispose();

                default_rasterizer_state.Dispose();
                default_depth_stencil_state.Dispose();
                default_blend_state.Dispose();

                ztex_view.Dispose();
                ztex.Dispose();

                buf0_view.Dispose();
                buf0.Dispose();

                swap_chain.Dispose();
                dxgi_factory.Dispose();

                // Disposing the device, before the hmd, will cause the hmd to fail when disposing.
                // Disposing the device, after the hmd, will cause the dispose of the device to fail.
                // It looks as if the hmd steals ownership of the device and destroys it, when it's shutting down.
                // device.Dispose();

                hmd.Dispose();
            }
            oculus.Dispose();
        }
Example #8
0
        public void TestDXGI()
        {
            // Force to load DXGI assembly
            var factory = new Factory();

            factory.Dispose();
            // Look for DXGI descriptor SharpDX.DXGI.ResultCode.DeviceRemoved
            var descriptor = ResultDescriptor.Find(0x887A0005);

            Assert.AreEqual(descriptor.NativeApiCode, "DXGI_ERROR_DEVICE_REMOVED");
        }
Example #9
0
 public void Dispose()
 {
     _isRunning = false;
     render2d.Dispose();
     device.Dispose();
     swapChain.Dispose();
     factory2d.Dispose();
     factoryText.Dispose();
     factoryDX.Dispose();
     brush.Dispose();
 }
Example #10
0
        private void InitializeForm()
        {
            ModeDescription modeDescription = new ModeDescription()
            {
                Width       = ClientSize.Width,
                Height      = ClientSize.Height,
                RefreshRate = new Rational(60, 1),
                Format      = Format.R8G8B8A8_UNorm
            };
            SwapChainDescription desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = modeDescription,
                IsWindowed        = true,
                OutputHandle      = Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            // Create Device and SwapChain
            SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport,
                                                          new[] { SharpDX.Direct3D.FeatureLevel.Level_11_1 }, desc, out SharpDX.Direct3D11.Device device, out SwapChain swapChain);

            SharpDX.Direct2D1.Factory d2dFactory = new SharpDX.Direct2D1.Factory();

            // Ignore all windows events
            SharpDX.DXGI.Factory factory = swapChain.GetParent <SharpDX.DXGI.Factory>();
            factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAll);

            Texture2D        backBuffer = SharpDX.Direct3D11.Resource.FromSwapChain <Texture2D>(swapChain, 0);
            RenderTargetView renderView = new RenderTargetView(device, backBuffer);

            Surface surface = backBuffer.QueryInterface <Surface>();

            RenderTarget d2dRenderTarget = new RenderTarget(d2dFactory, surface,
                                                            new RenderTargetProperties(new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));

            // Release all resources
            renderView.Dispose();
            backBuffer.Dispose();
            device.ImmediateContext.ClearState();
            device.ImmediateContext.Flush();
            device.Dispose();
            factory.Dispose();

            RulerRender thisForm = this;

            Manager = new Manager(ref thisForm, ref d2dRenderTarget, ref swapChain);
        }
Example #11
0
        static void D2d()
        {
            var plugfire = new Plugfire();

            InitializeXAudio2();
            //var fileStream = new NativeFileStream(@"D:\plug17.mp3", NativeFileMode.Open, NativeFileAccess.Read);
            var embeddedStream = Assembly.GetEntryAssembly().GetManifestResourceStream(typeof(Program), "music.mp3");
            var audioPlayer    = new AudioPlayer(xaudio2, embeddedStream);

            audioPlayer.Play();

            var form = new RenderForm("SharpDX - MiniCube Direct3D11 Sample");

            form.Width  = Plugfire.WIDTH * 4;
            form.Height = Plugfire.HEIGHT * 4;
            Device    device;
            SwapChain swapChain;
            var       d2dFactory = new SharpDX.Direct2D1.Factory();

            var desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = false,
                OutputHandle      = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain);

            // Ignore all windows events
            Factory factory = swapChain.GetParent <Factory>();

            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            form.KeyUp += (sender, args) =>
            {
                if (args.KeyCode == Keys.F5)
                {
                    swapChain.SetFullscreenState(true, null);
                }
                else if (args.KeyCode == Keys.F4)
                {
                    swapChain.SetFullscreenState(false, null);
                }
                else if (args.KeyCode == Keys.Escape)
                {
                    form.Close();
                }
            };

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Texture2D        backBuffer = null;
            Bitmap           bitmap     = null;
            RenderTarget     render     = null;
            RenderTargetView renderView = null;

            backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            //var textBuffer = new Texture2D(device, new Texture2DDescription {Height = 100, Width = 100, Format = Format.R8G8B8A8_UNorm});
            renderView = new RenderTargetView(device, backBuffer);
            //var textView = new RenderTargetView(device, textBuffer);
            Surface surface = backBuffer.QueryInterface <Surface>();

            //Surface textSurface = textBuffer.QueryInterface<Surface>();
            render = new RenderTarget(d2dFactory, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            //var textRender = new RenderTarget(d2dFactory, textSurface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            bitmap = new Bitmap(render, new Size2(Plugfire.WIDTH, Plugfire.HEIGHT), new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Ignore)));

            SharpDX.DirectWrite.Factory FactoryDWrite = new SharpDX.DirectWrite.Factory();

            var whiteTextBrush     = new SolidColorBrush(render, new RawColor4(1, 1, 1, 1));
            var debugTextFormat    = new TextFormat(FactoryDWrite, "Arial", 10);
            var titleTextBrush     = new SolidColorBrush(render, new RawColor4(1, 1, 0.3f, 1));
            var foamTextFormat     = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 100);
            var presentsTextFormat = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 60);
            var titleTextFormat    = new TextFormat(FactoryDWrite, "Arial", FontWeight.Bold, FontStyle.Normal, 80);

            // Main loop
            RenderLoop.Run(form, () =>
            {
                plugfire.Frame(stopwatch.ElapsedMilliseconds, audioPlayer.Position.TotalMilliseconds);
                bitmap.CopyFromMemory(plugfire.GetRgbImage(), Plugfire.WIDTH * 4);

                render.BeginDraw();
                //render.Clear(Color.Wheat);
                //solidColorBrush.Color = new Color4(1, 1, 1, (float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001)));
                //d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
                render.DrawBitmap(bitmap, new RawRectangleF(0, 0, render.Size.Width, render.Size.Height), 1.0f, BitmapInterpolationMode.Linear);
                //render.DrawText(audioPlayer.Position.ToString(), debugTextFormat, new RawRectangleF(10,10,100,100), whiteTextBrush);

                //if (plugfire.Mode == 0)
                //{
                //    var fade = (float)Math.Min((stopwatch.Elapsed.TotalMilliseconds - 500) / 2000, 1);
                //    titleTextBrush.Color = new RawColor4(fade, fade, fade * 0.3f, 1);
                //    render.DrawText("FOAM", foamTextFormat, new RawRectangleF(500, 100, 2000, 2000), titleTextBrush);
                //    render.DrawText("presents", presentsTextFormat, new RawRectangleF(520, 200, 2000, 2000), titleTextBrush);
                //    render.DrawText("Plugfire Regurgitated", titleTextFormat, new RawRectangleF(250, 400, 2000, 2000), titleTextBrush);
                //}

                render.EndDraw();

                swapChain.Present(0, PresentFlags.None);
            });

            // Release all resources
            renderView.Dispose();
            backBuffer.Dispose();
            device.ImmediateContext.ClearState();
            device.ImmediateContext.Flush();
            device.Dispose();
            device.Dispose();
            swapChain.Dispose();
            factory.Dispose();
        }
Example #12
0
 public void Dispose()
 {
     d2dFactory.Dispose();
     dxgiFactory.Dispose();
     device.Dispose();
 }
Example #13
0
        public static void Run(WorldBase world, int wndWidth = 1280, int wndHeight = 720, string windowTitle = "Ants", int targetFamerate = 60)
        {
            var form = new RenderForm(windowTitle)
            {
                ClientSize = new System.Drawing.Size(wndWidth, wndHeight)
            };
            // SwapChain description
            var desc = new SwapChainDescription()
            {
                BufferCount     = 1,
                ModeDescription =
                    new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
                                        new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            // Create Device and SwapChain
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport,
                                       new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 },
                                       desc, out var device, out var swapChain);

            var d2dFactory = new SharpDX.Direct2D1.Factory();

            int width  = form.ClientSize.Width;
            int height = form.ClientSize.Height;

            var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory,
                                                                 new RoundedRectangle()
            {
                RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2)
            });

            // Ignore all windows events
            Factory factory = swapChain.GetParent <Factory>();

            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            var       renderView = new RenderTargetView(device, backBuffer);

            Surface surface = backBuffer.QueryInterface <Surface>();


            var d2dRenderTarget = new RenderTarget(d2dFactory, surface,
                                                   new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));


            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Main loop
            RenderLoop.Run(form, () =>
            {
                d2dRenderTarget.BeginDraw();
                d2dRenderTarget.Clear(Color.Aqua);
                world.RenderLoop(stopwatch, d2dRenderTarget);
                d2dRenderTarget.EndDraw();
                swapChain.Present(0, PresentFlags.None);
            });

            // Release all resources
            renderView.Dispose();
            backBuffer.Dispose();
            device.ImmediateContext.ClearState();
            device.ImmediateContext.Flush();
            device.Dispose();
            device.Dispose();
            swapChain.Dispose();
            factory.Dispose();
        }
Example #14
0
        private static void Main()
        {
            var form = new RenderForm("SharpDX - MiniTri Direct2D - Direct3D 10 Sample");

            // SwapChain description
            var desc = new SwapChainDescription()
            {
                BufferCount     = 1,
                ModeDescription =
                    new ModeDescription(form.ClientSize.Width, form.ClientSize.Height,
                                        new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            // Create Device and SwapChain
            Device    device;
            SwapChain swapChain;

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 }, desc, out device, out swapChain);

            var d2dFactory = new SharpDX.Direct2D1.Factory();

            int width  = form.ClientSize.Width;
            int height = form.ClientSize.Height;

            var rectangleGeometry = new RoundedRectangleGeometry(d2dFactory, new RoundedRectangle()
            {
                RadiusX = 32, RadiusY = 32, Rect = new RectangleF(128, 128, width - 128 * 2, height - 128 * 2)
            });

            // Ignore all windows events
            Factory factory = swapChain.GetParent <Factory>();

            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            Texture2D backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            var       renderView = new RenderTargetView(device, backBuffer);

            Surface surface = backBuffer.QueryInterface <Surface>();


            var d2dRenderTarget = new RenderTarget(d2dFactory, surface,
                                                   new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));

            var solidColorBrush = new SolidColorBrush(d2dRenderTarget, Color.White);

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Main loop
            RenderLoop.Run(form, () =>
            {
                d2dRenderTarget.BeginDraw();
                d2dRenderTarget.Clear(Color.Black);
                solidColorBrush.Color = new Color4(1, 1, 1, (float)Math.Abs(Math.Cos(stopwatch.ElapsedMilliseconds * .001)));
                d2dRenderTarget.FillGeometry(rectangleGeometry, solidColorBrush, null);
                d2dRenderTarget.EndDraw();

                swapChain.Present(0, PresentFlags.None);
            });

            // Release all resources
            renderView.Dispose();
            backBuffer.Dispose();
            device.ImmediateContext.ClearState();
            device.ImmediateContext.Flush();
            device.Dispose();
            device.Dispose();
            swapChain.Dispose();
            factory.Dispose();
        }