internal void Initialise() { RemoveAndDispose(ref _d2dFactory); RemoveAndDispose(ref _dwFactory); _d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory1(FactoryType.SingleThreaded)); _dwFactory = ToDispose(new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared)); RemoveAndDispose(ref _d3dDevice); RemoveAndDispose(ref _d3dContext); RemoveAndDispose(ref _d2dDevice); RemoveAndDispose(ref _d2dContext); //var desc = CreateSwapChainDescription(); using (var device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport)) // | DeviceCreationFlags.Debug, { _d3dDevice = ToDispose(device.QueryInterface <SharpDX.Direct3D11.Device1>()); } _d3dContext = ToDispose(_d3dDevice.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>()); using (var dxgiDevice = _d3dDevice.QueryInterface <SharpDX.DXGI.Device>()) { _d2dDevice = ToDispose(new SharpDX.Direct2D1.Device(_d2dFactory, dxgiDevice)); } _d2dContext = ToDispose(new DeviceContext(_d2dDevice, DeviceContextOptions.None)); /* * D2Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new [] { FeatureLevel.Level_10_0 }, desc, out _device, out _swapChain); * * ToDispose(_device); * ToDispose(_swapChain); * * var d2dFactory = ToDispose(new SharpDX.Direct2D1.Factory()); * * Factory factory = ToDispose(_swapChain.GetParent<Factory>()); * factory.MakeWindowAssociation(_outputHandle, WindowAssociationFlags.IgnoreAll); * * _backBuffer = ToDispose(Texture2D.FromSwapChain<Texture2D>(_swapChain, 0)); * _renderTargetView = ToDispose(new RenderTargetView(_device, _backBuffer)); * _surface = ToDispose(_backBuffer.QueryInterface<Surface>()); * * _target = ToDispose(new RenderTarget(d2dFactory, _surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)))); */ InitialiseResources(); RemoveAndDispose(ref _fpsCounter); _fpsCounter = new FpsCounter(); _fpsCounter.InitialiseGraphics(this); OnInitialize?.Invoke(this); }
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(); }
public bool Initialize(GameForm pForm, bool pFullScreen) { _form = pForm; try { #if DEBUG Device defaultDevice = new Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); _debug = defaultDevice.QueryInterface <DeviceDebug>(); #else Device defaultDevice = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport); #endif Device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); var dxgiDevice2 = Device.QueryInterface <SharpDX.DXGI.Device2>(); var dxgiFactory2 = dxgiDevice2.Adapter.GetParent <SharpDX.DXGI.Factory2>(); // SwapChain description var desc = new SwapChainDescription1() { Width = 0, Height = 0, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(1, 0), BufferCount = BUFFER_COUNT, Scaling = Scaling.None, SwapEffect = SwapEffect.FlipSequential, Usage = Usage.RenderTargetOutput }; _swapChain = new SwapChain1(dxgiFactory2, Device, pForm.Handle, ref desc, null); _2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); Context = new SharpDX.Direct2D1.DeviceContext(_2dDevice, DeviceContextOptions.EnableMultithreadedOptimizations); SecondaryContext = new SharpDX.Direct2D1.DeviceContext(_2dDevice, DeviceContextOptions.None); #if DEBUG Factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded, DebugLevel.Information); #else Factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded); #endif var dpi = Factory2D.DesktopDpi; var prop = new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), dpi.Height, dpi.Width, BitmapOptions.CannotDraw | BitmapOptions.Target); _backBuffer = _swapChain.GetBackBuffer <Surface>(0); _renderTarget = new Bitmap1(Context, _backBuffer, prop); Context.Target = _renderTarget; // Ignore all windows events using (Factory factory = _swapChain.GetParent <Factory>()) { factory.MakeWindowAssociation(pForm.Handle, WindowAssociationFlags.IgnoreAll); } FactoryDWrite = new SharpDX.DirectWrite.Factory(); return(true); } catch (System.Exception e) { System.Diagnostics.Debug.WriteLine("Error creating render target: " + e.Message); return(false); } }