private void CreateDevice() { SharpDX.Direct3D.FeatureLevel[] levels = new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_11_0, SharpDX.Direct3D.FeatureLevel.Level_10_1, SharpDX.Direct3D.FeatureLevel.Level_10_0, SharpDX.Direct3D.FeatureLevel.Level_9_3, SharpDX.Direct3D.FeatureLevel.Level_9_2, SharpDX.Direct3D.FeatureLevel.Level_9_1 }; foreach (var level in levels) { try { this.device = new D3D11.Device(SharpDX.Direct3D.DriverType.Hardware, D3D11.DeviceCreationFlags.BgraSupport, level); break; } catch { continue; } } if (this.device == null) { throw new PlatformNotSupportedException("DirectXデバイスの作成に失敗しました"); } device_dxgi = this.device.QueryInterface <DXGI.Device>(); factory_dxgi = device_dxgi.Adapter.GetParent <DXGI.Factory2>(); }
protected override SharpDX.DXGI.SwapChain2 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates a SwapChain from a CoreWindow pointer using (var comWindow = new ComObject(window)) using (var swapChain1 = new SwapChain1(factory, device, comWindow, ref desc)) return(swapChain1.QueryInterface <SwapChain2>()); }
public VideoRenderer(VideoTrack videoTrack, RendererOptions options) { VideoTrack = videoTrack; VideoFrameWidth = options.VideoFrameWidth; VideoFrameHeight = options.VideoFrameHeight; VideoFrameQueueSize = options.VideoFrameQueueSize; videoTrack.LocalVideoFrameEncoded += OnLocalVideoFrameEncoded; // _onMissedFrame = options.OnMissedFrame ?? OnMissedFrame; bool debug = options.CreationFlags.HasFlag(D3D11.DeviceCreationFlags.Debug); FactoryDXGI = new DXGI.Factory2(debug); // Find the requested adapter. using (var adapters = FactoryDXGI.Adapters.ToDisposableList()) { var adapter = adapters.First(a => a.Description.VendorId == options.AdapterVendorId); Device3D = new D3D11.Device(adapter, options.CreationFlags, options.FeatureLevels); DeviceDXGI = Device3D.QueryInterface <DXGI.Device>(); // We need to access D3D11 on multiple threads, so enable multi-threading ThreadLock3D = Device3D.ImmediateContext.QueryInterface <D3D11.Multithread>(); ThreadLock3D.SetMultithreadProtected(true); } }
void CreateDeviceResources() { var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); var device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); var dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); var dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); var desc = new SwapChainDescription1(); desc.Width = 480; desc.Height = 640; desc.Format = Format.B8G8R8A8_UNorm; desc.Stereo = false; desc.SampleDescription = new SampleDescription(1, 0); desc.Usage = Usage.RenderTargetOutput; desc.BufferCount = 2; desc.Scaling = Scaling.AspectRatioStretch; desc.SwapEffect = SwapEffect.FlipSequential; desc.Flags = SwapChainFlags.AllowModeSwitch; this.swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(mWindow), ref desc); var d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); this.d2dDeviceContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None); var backBuffer = this.swapChain.GetBackBuffer <Surface>(0); var displayInfo = DisplayInformation.GetForCurrentView(); this.d2dTarget = new Bitmap1(this.d2dDeviceContext, backBuffer, new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), displayInfo.LogicalDpi, displayInfo.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw)); this.updateList = new List <IUpdatable>(); // 10.5改変 this.playerShotManager = new PlayerShotManager(this.d2dDeviceContext); this.updateList.Add(this.playerShotManager); this.fighterDisplay = new Fighter(this.d2dDeviceContext, playerShotManager); this.fighterDisplay.SetPosition(540, 240); this.displayList = new List <IDrawable>(); this.displayList.Add(this.fighterDisplay); this.displayList.Add(this.playerShotManager); this.targetManager = new RectTargetManager(this.d2dDeviceContext, this.playerShotManager); this.displayList.Add(this.targetManager); this.updateList.Add(this.targetManager); this.enemyShotManager = new EnemyShotManager(this.d2dDeviceContext, this.targetManager, this.fighterDisplay); this.displayList.Add(this.enemyShotManager); this.updateList.Add(this.enemyShotManager); }
/// <summary> /// Unloads all resources. /// </summary> internal void UnloadResources() { m_factory = CommonTools.DisposeObject(m_factory); m_adapter = CommonTools.DisposeObject(m_adapter); #if UNIVERSAL m_device = CommonTools.DisposeObject(m_device); #endif }
/// <summary> /// Initializes a new instance of the <see cref="DeviceHandlerDXGI"/> class. /// </summary> internal DeviceHandlerDXGI(DXGI.Adapter1 adapter, D3D11.Device device) { #if UNIVERSAL m_device = device.QueryInterface <DXGI.Device3>(); #endif m_adapter = adapter; m_factory = m_adapter.GetParent <DXGI.Factory2>(); }
void Initialize(ComObject coreWindow, AntiAliasingMode antiAliasingMode) { InitializeInner(antiAliasingMode); var swapChainDesc = device.CreateSwapChainDescription(); DXGI.Factory2 factory = device.QueryInterface <DXGI.Device1>().Adapter.GetParent <DXGI.Factory2>(); swapChain = new DXGI.SwapChain1(factory, device, coreWindow, ref swapChainDesc); }
protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Create the swap chain for XAML composition var swapChain = new SwapChain1(factory, device, ref desc); // Attach swap chain to SwapChainPanel nativePanel.SwapChain = swapChain; return(swapChain); }
protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates the swap chain for XAML composition var swapChain = new SwapChain1(factory, device, ref desc); // Associate the SwapChainBackgroundPanel with the swap chain nativeBackgrounPanel.SwapChain = swapChain; return(swapChain); }
protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates the swap chain for XAML composition var swapChain = new SwapChain1(factory, device, ref desc); //factory.CreateSwapChainForComposition(device, ref desc, null); // Associate the SwapChainPanel with the swap chain nativePanel.SwapChain = swapChain; // Returns the new swap chain return(swapChain); }
protected override SharpDX.DXGI.SwapChain2 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates the swap chain for XAML composition using (var swapChain1 = new SwapChain1(factory, device, ref desc)) { var swapChain2 = swapChain1.QueryInterface<SwapChain2>(); // Associate the SwapChainPanel with the swap chain nativePanel.SwapChain = swapChain2; // Returns the new swap chain return swapChain2; } }
public BasicCapture(IDirect3DDevice d, GraphicsCaptureItem i) { item = i; device = d; d3dDevice = Direct3D11Helper.CreateSharpDXDevice(device); var size = item.Size; if (size.Height == 0 || size.Width == 0) { size = new SizeInt32() { Height = 1, Width = 1 } } ; var dxgiFactory = new SharpDX.DXGI.Factory2(); var description = new SharpDX.DXGI.SwapChainDescription1() { Width = size.Width, Height = size.Height, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SharpDX.DXGI.SampleDescription() { Count = 1, Quality = 0 }, Usage = SharpDX.DXGI.Usage.RenderTargetOutput, BufferCount = 2, Scaling = SharpDX.DXGI.Scaling.Stretch, SwapEffect = SharpDX.DXGI.SwapEffect.FlipSequential, AlphaMode = SharpDX.DXGI.AlphaMode.Premultiplied, Flags = SharpDX.DXGI.SwapChainFlags.None }; swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, d3dDevice, ref description); framePool = Direct3D11CaptureFramePool.Create( device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 2, size); session = framePool.CreateCaptureSession(i); lastSize = size; framePool.FrameArrived += OnFrameArrived; }
protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates a SwapChain from a CoreWindow pointer SwapChainFullScreenDescription scFullScreenDesc = new SwapChainFullScreenDescription() { Windowed = deviceManager.Settings.IsWindowed, RefreshRate = new Rational(120, 1) }; #if DIRECTX11_1 return(new SwapChain1(factory, device, form.Handle, ref desc)); #else return(factory.CreateSwapChainForHwnd(device, form.Handle, ref desc, scFullScreenDesc, null)); #endif }
public RenderController(IntPtr windowHandle) { writeFactory = new SharpDX.DirectWrite.Factory(); SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device ( DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport ); device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); SwapChainDescription1 description = new SwapChainDescription1() { Width = 0, Height = 0, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(1, 0), Usage = Usage.RenderTargetOutput, BufferCount = 2, Scaling = Scaling.None, SwapEffect = SwapEffect.FlipSequential, }; swapChain = new SwapChain1(dxgiFactory2, device, windowHandle, ref description); backBuffer = Surface.FromSwapChain(swapChain, 0); d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); properties = new BitmapProperties1 ( new SharpDX.Direct2D1.PixelFormat ( SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied ), 0, 0, BitmapOptions.Target | BitmapOptions.CannotDraw ); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); d2dContext.Target = d2dTarget; canDraw = true; }
void InitializeDirect2D() { d3dDevice = new D3D.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport); dxgiDevice = d3dDevice.QueryInterface <DXGI.Device1>(); var desc = new SwapChainDescription1() { Width = 0, Height = 0, Format = Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new SampleDescription(1, 0), Usage = Usage.RenderTargetOutput, BufferCount = 3, Scaling = Scaling.None, SwapEffect = SwapEffect.FlipSequential, Flags = SwapChainFlags.None }; DXGI.Factory2 dxgiFactory = dxgiDevice.Adapter.GetParent <DXGI.Factory2>(); swapChain = new SwapChain1(dxgiFactory, d3dDevice, Child.Handle, ref desc); swapChain.BackgroundColor = Color4.White; dxgiFactory.Dispose(); d2dFactory = new D2D.Factory1(FactoryType.SingleThreaded); d2dDevice = new D2D.Device(d2dFactory, dxgiDevice); d2dDeviceContext = new D2D.DeviceContext(d2dDevice, DeviceContextOptions.None); d2dDeviceContext.TextAntialiasMode = TextAntialiasMode.Cleartype; //d2dDeviceContext.DotsPerInch = new Size2F(96, 96); var props = new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, D2D.AlphaMode.Ignore), d2dDeviceContext.DotsPerInch.Width, d2dDeviceContext.DotsPerInch.Height, BitmapOptions.Target | BitmapOptions.CannotDraw); Surface1 dxgiSurface = swapChain.GetBackBuffer <Surface1>(0); d2dSurface = new Bitmap1(d2dDeviceContext, dxgiSurface, props); dxgiSurface.Dispose(); d2dDeviceContext.Target = d2dSurface; VertexFillBrush = new SolidColorBrush(d2dDeviceContext, new Color4(1, 0.5f, 0, 1)); VertexDrawBrush = new SolidColorBrush(d2dDeviceContext, new Color4(0.2f, 0.2f, 0.2f, 1)); EdgeDrawBrush = new SolidColorBrush(d2dDeviceContext, Color4.Black); RasterDrawBrush = new SolidColorBrush(d2dDeviceContext, new Color4(0.5f, 0.5f, 0.5f, 1)); }
/// <summary> /// Creates a swapchain for DirectComposition API or WinRT XAML framework. This is applicable only for WinRT platform. /// </summary> /// <param name="factory">The DXGI Factory used to create the swapchain.</param> /// <param name="device">The associated device instance.</param> /// <param name="description">The swap chain description.</param> /// <param name="restrictToOutput">The output to which this swap chain should be restricted. Default is null, meaning that there is no restriction.</param> public SwapChain1(Factory2 factory, ComObject device, ref SwapChainDescription1 description, Output restrictToOutput = null) : base(IntPtr.Zero) { factory.CreateSwapChainForComposition(device, ref description, restrictToOutput, this); }
/// <summary> /// Creates a swapchain associated to the specified HWND. This is applicable only for Desktop platform. /// </summary> /// <param name="factory">The DXGI Factory used to create the swapchain.</param> /// <param name="device">The associated device instance.</param> /// <param name="hwnd">The HWND of the window to which this swapchain is associated.</param> /// <param name="description">The swap chain description.</param> /// <param name="fullScreenDescription">The fullscreen description of the swap chain. Default is null.</param> /// <param name="restrictToOutput">The output to which this swap chain should be restricted. Default is null, meaning that there is no restriction.</param> public SwapChain1(Factory2 factory, ComObject device, IntPtr hwnd, ref SwapChainDescription1 description, SwapChainFullScreenDescription?fullScreenDescription = null, Output restrictToOutput = null) : base(IntPtr.Zero) { factory.CreateSwapChainForHwnd(device, hwnd, ref description, fullScreenDescription, restrictToOutput, this); }
public VideoRenderer(VideoTrack videoTrack, RendererOptions options) { VideoTrack = videoTrack; VideoFrameWidth = options.VideoFrameWidth; VideoFrameHeight = options.VideoFrameHeight; VideoFrameQueueSize = options.VideoFrameQueueSize; videoTrack.LocalVideoFrameProcessed += OnLocalVideoFrameProcessed; // _onMissedFrame = options.OnMissedFrame ?? OnMissedFrame; bool debug = options.CreationFlags.HasFlag(D3D11.DeviceCreationFlags.Debug); FactoryDXGI = new DXGI.Factory2(debug); // Find the requested adapter. using (var adapters = FactoryDXGI.Adapters.ToDisposableList()) { var adapter = adapters.First(a => a.Description.VendorId == options.AdapterVendorId); Device3D = new D3D11.Device(adapter, options.CreationFlags, options.FeatureLevels); DeviceDXGI = Device3D.QueryInterface <DXGI.Device>(); // We need to access D3D11 on multiple threads, so enable multi-threading ThreadLock3D = Device3D.ImmediateContext.QueryInterface <D3D11.Multithread>(); ThreadLock3D.SetMultithreadProtected(true); if (options.PreviewWindowOptions != null) { var width = options.PreviewWindowOptions.Width ?? VideoFrameWidth; var height = options.PreviewWindowOptions.Height ?? width * VideoFrameHeight / VideoFrameWidth; _sdlWindow = new SdlWindow("WebRTC server preview", width, height); // SwapChain description var desc = new DXGI.SwapChainDescription1() { BufferCount = 2, AlphaMode = DXGI.AlphaMode.Unspecified, Format = DXGI.Format.B8G8R8A8_UNorm, Width = VideoFrameWidth, Height = VideoFrameHeight, Scaling = DXGI.Scaling.Stretch, Stereo = false, Flags = DXGI.SwapChainFlags.AllowTearing | DXGI.SwapChainFlags.FrameLatencyWaitAbleObject, Usage = DXGI.Usage.RenderTargetOutput, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.FlipDiscard, }; SwapChain = new DXGI.SwapChain1(FactoryDXGI, Device3D, _sdlWindow.NativeHandle, ref desc); using (var swapChain2 = SwapChain.QueryInterface <DXGI.SwapChain2>()) { var value = swapChain2.MaximumFrameLatency; swapChain2.MaximumFrameLatency = 1; } } } }
/// <summary> /// Initializes the SwapChain for use with LibVLC /// </summary> void CreateSwapChain() { SharpDX.DXGI.Factory2 dxgiFactory = null; try { var deviceCreationFlags = DeviceCreationFlags.BgraSupport | DeviceCreationFlags.VideoSupport; #if DEBUG if (Windows.System.Profile.AnalyticsInfo.VersionInfo.DeviceFamily != Mobile) { deviceCreationFlags |= DeviceCreationFlags.Debug; } try { dxgiFactory = new SharpDX.DXGI.Factory2(true); } catch (SharpDXException) { dxgiFactory = new SharpDX.DXGI.Factory2(false); } #else dxgiFactory = new SharpDX.DXGI.Factory2(false); #endif _d3D11Device = null; foreach (var adapter in dxgiFactory.Adapters) { try { _d3D11Device = new SharpDX.Direct3D11.Device(adapter, deviceCreationFlags); break; } catch (SharpDXException) { } } if (_d3D11Device is null) { throw new VLCException("Could not create Direct3D11 device : No compatible adapter found."); } _device = _d3D11Device.QueryInterface <SharpDX.DXGI.Device1>(); //Create the swapchain var swapChainDescription = new SharpDX.DXGI.SwapChainDescription1 { Width = (int)(_panel.ActualWidth * _panel.CompositionScaleX), Height = (int)(_panel.ActualHeight * _panel.CompositionScaleY), Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = { Count = 1, Quality = 0 }, Usage = Usage.RenderTargetOutput, BufferCount = 2, SwapEffect = SwapEffect.FlipSequential, Flags = SwapChainFlags.None, AlphaMode = AlphaMode.Unspecified }; _swapChain = new SharpDX.DXGI.SwapChain1(dxgiFactory, _d3D11Device, ref swapChainDescription); _device.MaximumFrameLatency = 1; using (var panelNative = ComObject.As <ISwapChainPanelNative>(_panel)) { panelNative.SwapChain = _swapChain; } // This is necessary so we can call Trim() on suspend _device3 = _device.QueryInterface <SharpDX.DXGI.Device3>(); if (_device3 == null) { throw new VLCException("Failed to query interface \"Device3\""); } _swapChain2 = _swapChain.QueryInterface <SharpDX.DXGI.SwapChain2>(); if (_swapChain2 == null) { throw new VLCException("Failed to query interface \"SwapChain2\""); } UpdateScale(); UpdateSize(); _loaded = true; Initialized?.Invoke(this, new InitializedEventArgs(SwapChainOptions)); } catch (Exception ex) { DestroySwapChain(); if (ex is SharpDXException) { throw new VLCException("SharpDX operation failed, see InnerException for details", ex); } throw; } finally { dxgiFactory?.Dispose(); } }
/// <summary> /// Now that we have a CoreWindow object, the DirectX device/context can be created. /// </summary> /// <param name="entryPoint"></param> public void Load(string entryPoint) { // Get the default hardware device and enable debugging. Don't care about the available feature level. // DeviceCreationFlags.BgraSupport must be enabled to allow Direct2D interop. SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>(); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); // Description for our swap chain settings. SwapChainDescription1 description = new SwapChainDescription1() { // 0 means to use automatic buffer sizing. Width = 0, Height = 0, // 32 bit RGBA color. Format = Format.B8G8R8A8_UNorm, // No stereo (3D) display. Stereo = false, // No multisampling. SampleDescription = new SampleDescription(1, 0), // Use the swap chain as a render target. Usage = Usage.RenderTargetOutput, // Enable double buffering to prevent flickering. BufferCount = 2, // No scaling. Scaling = Scaling.None, // Flip between both buffers. SwapEffect = SwapEffect.FlipSequential, }; // Generate a swap chain for our window based on the specified description. swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(device, new ComObject(window), ref description, null); // Get the default Direct2D device and create a context. SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); // Specify the properties for the bitmap that we will use as the target of our Direct2D operations. // We want a 32-bit BGRA surface with premultiplied alpha. BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw); // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target. Surface backBuffer = swapChain.GetBackBuffer <Surface>(0); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); // Create a solid color brush. solidBrush = new SolidColorBrush(d2dContext, Color.Coral); // Create a linear gradient brush. // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to, // NOT the geometry we will apply the brush. linearGradientBrush = new LinearGradientBrush(d2dContext, new LinearGradientBrushProperties() { StartPoint = new Vector2(50, 0), EndPoint = new Vector2(450, 0), }, new GradientStopCollection(d2dContext, new GradientStop[] { new GradientStop() { Color = Color.Blue, Position = 0, }, new GradientStop() { Color = Color.Green, Position = 1, } })); // Create a radial gradient brush. // The center is specified in absolute coordinates, too. radialGradientBrush = new RadialGradientBrush(d2dContext, new RadialGradientBrushProperties() { Center = new Vector2(250, 525), RadiusX = 100, RadiusY = 100, }, new GradientStopCollection(d2dContext, new GradientStop[] { new GradientStop() { Color = Color.Yellow, Position = 0, }, new GradientStop() { Color = Color.Red, Position = 1, } })); }
public SharpRender(RenderForm form) { this.form = form; var adapters = new DXGI.Factory1().Adapters; DXGI.Adapter myadapter = null; for (int i = 0; i < adapters.Length; ++i) { Logger.Log(string.Format("Adapter Found: [{0}] " + "{1}\tDeviceId={5}" + "{1}\tLuid={6}" + "{1}\tVendorId={10}" + "{1}\tSubsystemId={9}" + "{1}\tDescription={4}" + "{1}\tRevision={7}" + "{1}\tDedicatedSystemMemory={2}" + "{1}\tDedicatedVideoMemory={3}" + "{1}\tSharedSystemMemory={8}" + "", i, Environment.NewLine, adapters[i].Description.DedicatedSystemMemory, adapters[i].Description.DedicatedVideoMemory, adapters[i].Description.Description, adapters[i].Description.DeviceId, adapters[i].Description.Luid, adapters[i].Description.Revision, adapters[i].Description.SharedSystemMemory, adapters[i].Description.SubsystemId, adapters[i].Description.VendorId)); var outputs = adapters[i].Outputs; for (int j = 0; j < outputs.Length; ++j) { Logger.Log(string.Format("Output Found: [{0},{1}]" + "{2}\tDeviceName={4}" + "{2}\tIsAttachedToDesktop={5}" + "{2}\tMonitorHandle={6}" + "{2}\tDesktopBounds={3}" + "{2}\tRotation={7}" + "", i, j, Environment.NewLine, (Rectangle)outputs[j].Description.DesktopBounds, outputs[j].Description.DeviceName, outputs[j].Description.IsAttachedToDesktop, outputs[j].Description.MonitorHandle, outputs[j].Description.Rotation)); } if (outputs.Length > 0 && myadapter == null) { myadapter = adapters[i]; } } d3device = new Direct3D11.Device( myadapter, Direct3D11.DeviceCreationFlags.BgraSupport); //SharpDX.Direct3D.DriverType.Hardware, //Direct3D11.DeviceCreationFlags.BgraSupport | //Direct3D11.DeviceCreationFlags.Debug); defDevice = d3device.QueryInterface <Direct3D11.Device1>(); dxgiDevice2 = defDevice.QueryInterface <DXGI.Device2>(); dxgiAdapter = dxgiDevice2.Adapter; dxgiFactory2 = dxgiAdapter.GetParent <DXGI.Factory2>(); var scDescription = new DXGI.SwapChainDescription1() { Width = 0, Height = 0, Format = DXGI.Format.B8G8R8A8_UNorm, Stereo = false, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, BufferCount = 2, Scaling = DXGI.Scaling.None, SwapEffect = DXGI.SwapEffect.FlipSequential }; swapChain = new DXGI.SwapChain1(dxgiFactory2, defDevice, form.Handle, ref scDescription, null, null); d2dDevice = new Direct2D1.Device(dxgiDevice2); d2dContext = new Direct2D1.DeviceContext(d2dDevice, Direct2D1.DeviceContextOptions.None); fac = new Direct2D1.Factory(Direct2D1.FactoryType.SingleThreaded); var dpi = fac.DesktopDpi; var bMProperties = new Direct2D1.BitmapProperties1( new Direct2D1.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, Direct2D1.AlphaMode.Premultiplied), dpi.Width, dpi.Height, Direct2D1.BitmapOptions.CannotDraw | Direct2D1.BitmapOptions.Target); bb = swapChain.GetBackBuffer <DXGI.Surface>(0); target = new Direct2D1.Bitmap1(d2dContext, bb, bMProperties); d2dContext.Target = target; wrFactory = new DirectWrite.Factory(); brush = new Direct2D1.SolidColorBrush(d2dContext, c(Color.White)); }
/// <summary> /// Initializes a new instance of the <see cref="FormRenderer"/> class. /// </summary> /// <param name="form">The form.</param> public FormRenderer(Form1 form) { this.form = form; #if DEBUG var creationFlags = D3D11.DeviceCreationFlags.BgraSupport | D3D11.DeviceCreationFlags.Debug; var debugFactory = true; #else var creationFlags = D3D11.DeviceCreationFlags.BgraSupport; var debugFactory = false; #endif this.device = new D3D11.Device(D3D.DriverType.Hardware, creationFlags); #if DEBUG this.deviceDebug = new D3D11.DeviceDebug(this.device); #endif using (var dxgiDevice = this.device.QueryInterface <DXGI.Device>()) { using (var dxgiFactory = new DXGI.Factory2(debugFactory)) { var desc = new DXGI.SwapChainDescription1() { BufferCount = 2, AlphaMode = DXGI.AlphaMode.Premultiplied, SampleDescription = new DXGI.SampleDescription(1, 0), Usage = DXGI.Usage.RenderTargetOutput, SwapEffect = DXGI.SwapEffect.FlipDiscard, Format = DXGI.Format.B8G8R8A8_UNorm, Width = form.Width, Height = form.Height, }; this.swapChain = new DXGI.SwapChain1(dxgiFactory, dxgiDevice, ref desc, null); this.deviceComp = new DComp.Device(dxgiDevice); this.compositionTarget = DComp.Target.FromHwnd(this.deviceComp, form.Handle, true); using (var visual = new DComp.Visual(this.deviceComp)) { visual.Content = this.swapChain; this.compositionTarget.Root = visual; } this.deviceComp.Commit(); } } using (var device = this.device.QueryInterface <DXGI.Device>()) { this.device2d = new D2D.Device(this.factory2d, device); } this.deviceContext2D = new D2D.DeviceContext(this.device2d, D2D.DeviceContextOptions.None) { DotsPerInch = this.factory2d.DesktopDpi, AntialiasMode = D2D.AntialiasMode.PerPrimitive, }; this.CreateResources(); }
/// <summary> /// Now that we have a CoreWindow object, the DirectX device/context can be created. /// </summary> /// <param name="entryPoint"></param> public async void Load(string entryPoint) { // Get the default hardware device and enable debugging. Don't care about the available feature level. // DeviceCreationFlags.BgraSupport must be enabled to allow Direct2D interop. SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>(); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); // Description for our swap chain settings. SwapChainDescription1 description = new SwapChainDescription1() { // 0 means to use automatic buffer sizing. Width = 0, Height = 0, // 32 bit RGBA color. Format = Format.B8G8R8A8_UNorm, // No stereo (3D) display. Stereo = false, // No multisampling. SampleDescription = new SampleDescription(1, 0), // Use the swap chain as a render target. Usage = Usage.RenderTargetOutput, // Enable double buffering to prevent flickering. BufferCount = 2, // No scaling. Scaling = Scaling.None, // Flip between both buffers. SwapEffect = SwapEffect.FlipSequential, }; // Generate a swap chain for our window based on the specified description. swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(device, new ComObject(window), ref description, null); // Get the default Direct2D device and create a context. SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); // Specify the properties for the bitmap that we will use as the target of our Direct2D operations. // We want a 32-bit BGRA surface with premultiplied alpha. BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw); // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target. Surface backBuffer = swapChain.GetBackBuffer <Surface>(0); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); // Create the DirectWrite factory objet. SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory(); // Create a TextFormat object that will use the Segoe UI font with a size of 24 DIPs. textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f); // Create two TextLayout objects for rendering the moving text. textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f); textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f); // Vertical offset for the moving text. layoutY = 0.0f; // Create the brushes for the text background and text color. backgroundBrush = new SolidColorBrush(d2dContext, Color.White); textBrush = new SolidColorBrush(d2dContext, Color.Black); }
private void CreateDeviceResources() { /// デフォルトDirect3Dデバイスの作成(取得) var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); /// サポートされているデバイスの取得 var device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); /// COMを使って、D3Dデバイスオブジェクトに内在するのDXGIデバイスオブジェクトを取得する var dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); /// アダプターの取得 var dxgiAdapter = dxgiDevice2.Adapter; /// COMを使って、DXGIデバイスのファクトリーオブジェクトを取得 SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); var desc = new SwapChainDescription1(); desc.Width = 480; desc.Height = 640; desc.Format = Format.B8G8R8A8_UNorm; desc.Stereo = false; desc.SampleDescription = new SampleDescription(1, 0); desc.Usage = Usage.RenderTargetOutput; desc.BufferCount = 2; desc.Scaling = Scaling.AspectRatioStretch; desc.SwapEffect = SwapEffect.FlipSequential; desc.Flags = SwapChainFlags.AllowModeSwitch; /// スワップチェインの作成 this.swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(mWindow), ref desc); /// Direct2Dデバイスの取得。 var d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); /// Direct2Dデバイスのコンテキストを取得。 this.d2dDeviceContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, DeviceContextOptions.None); /// スワップチェインからBuffer0のサーフェイスを取得。 var backBuffer = this.swapChain.GetBackBuffer <Surface>(0); var displayInfo = DisplayInformation.GetForCurrentView(); this.d2dTarget = new Bitmap1(this.d2dDeviceContext, backBuffer, new BitmapProperties1(new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), displayInfo.LogicalDpi , displayInfo.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw)); this.PlayerScorer = new Scorer(this.d2dDeviceContext); this.updateList = new List <IUpdatable>(); /// 自機の作成 this.playerShotManager = new PlayerShotManager(this.d2dDeviceContext); this.updateList.Add(this.playerShotManager); this.fighterDisplay = new Fighter(this.d2dDeviceContext, playerShotManager); this.fighterDisplay.SetPosition(540, 240); this.displayList = new List <IDrawable>(); this.displayList.Add(this.fighterDisplay); this.displayList.Add(this.playerShotManager); this.targetManager = new RectTargetManager(this.d2dDeviceContext, this.playerShotManager, this.PlayerScorer); this.displayList.Add(this.targetManager); this.updateList.Add(this.targetManager); this.displayList.Add(this.PlayerScorer); /* 様々な初期化処理を以下に書く */ var fighterPath = new PathGeometry(d2dDevice.Factory); var sink = fighterPath.Open(); sink.BeginFigure(new Vector2(50f, 0f), FigureBegin.Filled); sink.AddLines(new SharpDX.Mathematics.Interop.RawVector2[] { new Vector2(0f, 0f), new Vector2(0f, 50f), new Vector2(50f, 50f) }); sink.EndFigure(FigureEnd.Closed); sink.Close(); this.tFighterPath = new TransformedGeometry(d2dDevice.Factory, fighterPath, Matrix3x2.Identity); this.fighterBrush = new SolidColorBrush(d2dDeviceContext, Color.OrangeRed); this.fpsController = new FramePerSec(this.d2dDeviceContext); this.enemyShotManager = new EnemyShotManager(this.d2dDeviceContext, this.targetManager, this.fighterDisplay); this.displayList.Add(this.enemyShotManager); this.updateList.Add(this.enemyShotManager); }
protected abstract SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc);
protected abstract SwapChain1 CreateSwapChain(Factory2 dxgiFactory, SwapChainDescription1 swapChainDesc);
/// <summary> /// Now that we have a CoreWindow object, the DirectX device/context can be created. /// </summary> /// <param name="entryPoint"></param> public void Load(string entryPoint) { // Get the default hardware device and enable debugging. Don't care about the available feature level. // DeviceCreationFlags.BgraSupport must be enabled to allow Direct2D interop. SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport); // Query the default device for the supported device and context interfaces. device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>(); // Query for the adapter and more advanced DXGI objects. SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); // Description for our swap chain settings. SwapChainDescription1 description = new SwapChainDescription1() { // 0 means to use automatic buffer sizing. Width = 0, Height = 0, // 32 bit RGBA color. Format = Format.B8G8R8A8_UNorm, // No stereo (3D) display. Stereo = false, // No multisampling. SampleDescription = new SampleDescription(1, 0), // Use the swap chain as a render target. Usage = Usage.RenderTargetOutput, // Enable double buffering to prevent flickering. BufferCount = 2, // No scaling. Scaling = Scaling.None, // Flip between both buffers. SwapEffect = SwapEffect.FlipSequential, }; // Generate a swap chain for our window based on the specified description. swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(window), ref description); // Get the default Direct2D device and create a context. SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); // Specify the properties for the bitmap that we will use as the target of our Direct2D operations. // We want a 32-bit BGRA surface with premultiplied alpha. BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw); // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target. Surface backBuffer = swapChain.GetBackBuffer <Surface>(0); d2dTarget = new Bitmap1(d2dContext, backBuffer, properties); playerBitmap = this.LoadBitmapFromContentFile("/Assets/Bitmaps/player.png"); terrainBitmap = this.LoadBitmapFromContentFile("/Assets/Bitmaps/terrain.png"); terrainBrush = new BitmapBrush1(d2dContext, terrainBitmap, new BitmapBrushProperties1() { ExtendModeX = ExtendMode.Wrap, ExtendModeY = ExtendMode.Wrap, }); }
void InitText(SwapChain3 tempSwapChain) { init = true; device = tempSwapChain.GetDevice <SharpDX.Direct3D11.Device1>(); d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>(); var texture2d = tempSwapChain.GetBackBuffer <Texture2D>(0); SharpDX.DXGI.Device2 dxgiDevice2 = device.QueryInterface <SharpDX.DXGI.Device2>(); SharpDX.DXGI.Adapter dxgiAdapter = dxgiDevice2.Adapter; SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>(); SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2); d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); SharpDX.Direct2D1.BitmapProperties1 properties = new SharpDX.Direct2D1.BitmapProperties1( new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied), 96, 96, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw); Surface backBuffer = tempSwapChain.GetBackBuffer <Surface>(0); d2dTarget = new SharpDX.Direct2D1.Bitmap1(d2dContext, new Size2(800, 600), properties); solidBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Coral); // Create a linear gradient brush. // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to, // NOT the geometry we will apply the brush. linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(d2dContext, new SharpDX.Direct2D1.LinearGradientBrushProperties() { StartPoint = new Vector2(50, 0), EndPoint = new Vector2(450, 0), }, new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[] { new SharpDX.Direct2D1.GradientStop() { Color = Color.Blue, Position = 0, }, new SharpDX.Direct2D1.GradientStop() { Color = Color.Green, Position = 1, } })); SharpDX.Direct2D1.RadialGradientBrushProperties rgb = new SharpDX.Direct2D1.RadialGradientBrushProperties() { Center = new Vector2(250, 525), RadiusX = 100, RadiusY = 100, }; // Create a radial gradient brush. // The center is specified in absolute coordinates, too. radialGradientBrush = new SharpDX.Direct2D1.RadialGradientBrush(d2dContext, ref rgb , new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[] { new SharpDX.Direct2D1.GradientStop() { Color = Color.Yellow, Position = 0, }, new SharpDX.Direct2D1.GradientStop() { Color = Color.Red, Position = 1, } })); }
/// <summary> /// Creates a swapchain associated to the specified HWND. This is applicable only for Desktop platform. /// </summary> /// <param name="factory">The DXGI Factory used to create the swapchain.</param> /// <param name="device">The associated device instance.</param> /// <param name="hwnd">The HWND of the window to which this swapchain is associated.</param> /// <param name="description">The swap chain description.</param> /// <param name="fullScreenDescription">The fullscreen description of the swap chain. Default is null.</param> /// <param name="restrictToOutput">The output to which this swap chain should be restricted. Default is null, meaning that there is no restriction.</param> public SwapChain1(Factory2 factory, ComObject device, IntPtr hwnd, ref SwapChainDescription1 description, SwapChainFullScreenDescription? fullScreenDescription = null, Output restrictToOutput = null) : base(IntPtr.Zero) { factory.CreateSwapChainForHwnd(device, hwnd, ref description, fullScreenDescription, restrictToOutput, this); }
/// <summary> /// Creates the swap chain. /// </summary> /// <param name="factory">The DXGI factory</param> /// <param name="device">The D3D11 device</param> /// <param name="desc">The swap chain description</param> /// <returns>An instance of swap chain</returns> protected abstract SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc);
protected override SharpDX.DXGI.SwapChain1 CreateSwapChain(SharpDX.DXGI.Factory2 factory, SharpDX.Direct3D11.Device1 device, SharpDX.DXGI.SwapChainDescription1 desc) { // Creates the swap chain for the Window's Hwnd return(new SwapChain1(factory, device, Window.Handle, ref desc, CreateFullScreenDescription(), null)); }