public static void Fill(Renderer renderer, SharpDX.DXGI.Adapter adapter) { RendererInfo ri = new RendererInfo(); ri.AdapterDesc = adapter.Description.Description; ri.SystemMemory = (UInt64)((IntPtr)adapter.Description.DedicatedSystemMemory).ToPointer(); ri.VideoMemory = (UInt64)((IntPtr)adapter.Description.DedicatedVideoMemory).ToPointer(); ri.SharedMemory = (UInt64)((IntPtr)adapter.Description.SharedSystemMemory).ToPointer(); ri.Outputs = adapter.Outputs.Length; SharpDX.DXGI.Output output = adapter.Outputs[0]; var bounds = output.Description.DesktopBounds; ri.OutputName = output.Description.DeviceName; ri.ScreenBounds = new System.Drawing.Rectangle(new System.Drawing.Point(bounds.Top, bounds.Left), new System.Drawing.Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top)); ri.ScreenWidth = output.Description.DesktopBounds.Right - output.Description.DesktopBounds.Left; ri.ScreenHeight = output.Description.DesktopBounds.Bottom - output.Description.DesktopBounds.Top; renderer.Info = ri; }
//-------------------------------------------------------------- public MyGame() { Lock = new object(); // ----- Service Container // MyGame uses a ServiceContainer, which is a simple service locator and // Inversion of Control (IoC) container. (The ServiceContainer can be // replaced by any other container that implements System.IServiceProvider.) _serviceContainer = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _serviceContainer); _serviceContainer.Register(typeof(MyGame), null, this); // ----- Storage // Create a "virtual file system" for reading game assets. _titleStorage = new TitleStorage("Content"); _assetsStorage = new ZipStorage(_titleStorage, "Content.zip"); _digitalRuneStorage = new ZipStorage(_titleStorage, "DigitalRune.zip"); _vfsStorage = new VfsStorage(); _vfsStorage.MountInfos.Add(new VfsMountInfo(_assetsStorage, null)); _vfsStorage.MountInfos.Add(new VfsMountInfo(_digitalRuneStorage, null)); // ----- Content _contentManager = new StorageContentManager(ServiceLocator.Current, _vfsStorage); _serviceContainer.Register(typeof(ContentManager), null, _contentManager); // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { BackBufferWidth = 1, BackBufferHeight = 1, // Do not associate graphics device with any window. DeviceWindowHandle = IntPtr.Zero, }; var graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, presentationParameters); // An IGraphicsDeviceService is required by the MonoGame/XNA content manager. _serviceContainer.Register(typeof(IGraphicsDeviceService), null, new DummyGraphicsDeviceManager(graphicsDevice)); // Get DXGIOutput to call WaitForVerticalBlank() in the game loop. using (var dxgiFactory = new SharpDX.DXGI.Factory1()) using (var dxgiAdapter = dxgiFactory.GetAdapter1(0)) _dxgiOutput = dxgiAdapter.GetOutput(0); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, _contentManager); _serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); // ----- Timing // The game loop runs in a parallel thread to keep the UI thread responsive. // To measure the time that has passed, we use a HighPrecisionClock. _clock = new HighPrecisionClock(); _clock.Start(); _gameLoopTask = ThreadPool.RunAsync(GameLoopTaskAction, WorkItemPriority.High, WorkItemOptions.TimeSliced) .AsTask(); // The FixedStepTimer reads the clock and triggers the game loop at 60 Hz. //_timer = new FixedStepTimer(_clock) //{ // StepSize = new TimeSpan(166667), // ~60 Hz // AccumulateTimeSteps = false, //}; // The VariableStepTimer reads the clock and triggers the game loop as often as possible. _timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); CoreApplication.Suspending += OnCoreApplicationSuspending; // DirectX buffers only a limit amount of Present calls per frame which is controlled by // the MaximumFrameLatency property. The default value is usually 3. If the application // uses more SwapChainPresentationTargets we must increase this property. //var d3dDevice = (SharpDX.Direct3D11.Device)_graphicsManager.GraphicsDevice.Handle; //using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) // dxgiDevice2.MaximumFrameLatency = numberOfSwapChainPanels; }
private static GraphicsAdapter CreateAdapter(SharpDX.DXGI.Adapter1 device, SharpDX.DXGI.Output monitor) { var adapter = new GraphicsAdapter(); adapter._adapter = device; adapter.DeviceName = monitor.Description.DeviceName.TrimEnd(new char[] { '\0' }); adapter.Description = device.Description1.Description.TrimEnd(new char[] { '\0' }); adapter.DeviceId = device.Description1.DeviceId; adapter.Revision = device.Description1.Revision; adapter.VendorId = device.Description1.VendorId; adapter.SubSystemId = device.Description1.SubsystemId; adapter.MonitorHandle = monitor.Description.MonitorHandle; var desktopWidth = monitor.Description.DesktopBounds.Right - monitor.Description.DesktopBounds.Left; var desktopHeight = monitor.Description.DesktopBounds.Bottom - monitor.Description.DesktopBounds.Top; var modes = new List <DisplayMode>(); foreach (var formatTranslation in FormatTranslations) { SharpDX.DXGI.ModeDescription[] displayModes; // This can fail on headless machines, so just assume the desktop size // is a valid mode and return that... so at least our unit tests work. try { displayModes = monitor.GetDisplayModeList(formatTranslation.Key, 0); } catch (SharpDX.SharpDXException) { var mode = new DisplayMode(desktopWidth, desktopHeight, SurfaceFormat.Color); modes.Add(mode); adapter._currentDisplayMode = mode; break; } foreach (var displayMode in displayModes) { var mode = new DisplayMode(displayMode.Width, displayMode.Height, formatTranslation.Value); // Skip duplicate modes with the same width/height/formats. if (modes.Contains(mode)) { continue; } modes.Add(mode); if (adapter._currentDisplayMode == null) { if (mode.Width == desktopWidth && mode.Height == desktopHeight && mode.Format == SurfaceFormat.Color) { adapter._currentDisplayMode = mode; } } } } adapter._supportedDisplayModes = new DisplayModeCollection(modes); if (adapter._currentDisplayMode == null) //(i.e. desktop mode wasn't found in the available modes) { adapter._currentDisplayMode = new DisplayMode(desktopWidth, desktopHeight, SurfaceFormat.Color); } return(adapter); }
private void InitDx() { logger.Debug("GDICapture::InitDx()"); SharpDX.DXGI.Factory1 dxgiFactory = null; SharpDX.DXGI.Adapter1 adapter = null; SharpDX.DXGI.Output output = null; try { dxgiFactory = new SharpDX.DXGI.Factory1(); logger.Info(MediaFoundation.DxTool.LogDxAdapters(dxgiFactory.Adapters1)); //var hMonitor = NativeAPIs.User32.GetMonitorFromRect(this.srcRect); //if (hMonitor != IntPtr.Zero) //{ // foreach (var _adapter in dxgiFactory.Adapters1) // { // foreach (var _output in _adapter.Outputs) // { // var descr = _output.Description; // if (descr.MonitorHandle == hMonitor) // { // adapter = _adapter; // output = _output; // break; // } // } // } //} if (adapter == null) {// первым идет адаптер с которому подключен primary монитор adapter = dxgiFactory.GetAdapter1(0); } AdapterId = adapter.Description.Luid; //logger.Info("Screen source info: " + adapter.Description.Description + " " + output.Description.DeviceName); var deviceCreationFlags = //DeviceCreationFlags.Debug | // DeviceCreationFlags.VideoSupport | DeviceCreationFlags.BgraSupport; //var feature = FeatureLevel.Level_11_0; device = new Device(adapter, deviceCreationFlags); using (var multiThread = device.QueryInterface <SharpDX.Direct3D11.Multithread>()) { multiThread.SetMultithreadProtected(true); } SharedTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = DestSize.Width, Height = DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.Shared, }); gdiTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = SrcRect.Width, //DestSize.Width, Height = SrcRect.Height, //DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, OptionFlags = ResourceOptionFlags.GdiCompatible, }); renderTexture = new Texture2D(device, new Texture2DDescription { CpuAccessFlags = CpuAccessFlags.None, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm, Width = DestSize.Width, Height = DestSize.Height, MipLevels = 1, ArraySize = 1, SampleDescription = { Count = 1, Quality = 0 }, Usage = ResourceUsage.Default, //OptionFlags = ResourceOptionFlags.GdiCompatible//ResourceOptionFlags.None, OptionFlags = ResourceOptionFlags.Shared, }); if (DestSize.Width != SrcRect.Width || DestSize.Height != SrcRect.Height) { using (SharpDX.Direct2D1.Factory1 factory2D1 = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.MultiThreaded)) { using (var surf = renderTexture.QueryInterface <SharpDX.DXGI.Surface>()) { //var pixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Ignore); var pixelFormat = new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied); var renderTargetProps = new SharpDX.Direct2D1.RenderTargetProperties(pixelFormat); renderTarget = new SharpDX.Direct2D1.RenderTarget(factory2D1, surf, renderTargetProps); //var d2dContext = new SharpDX.Direct2D1.DeviceContext(surface); } } } } finally { if (adapter != null) { adapter.Dispose(); adapter = null; } if (output != null) { output.Dispose(); output = null; } if (dxgiFactory != null) { dxgiFactory.Dispose(); dxgiFactory = null; } } }
//-------------------------------------------------------------- #region Creation & Cleanup //-------------------------------------------------------------- public MyGame() { Lock = new object(); // ----- Service Container // MyGame uses a ServiceContainer, which is a simple service locator and // Inversion of Control (IoC) container. (The ServiceContainer can be // replaced by any other container that implements System.IServiceProvider.) _serviceContainer = new ServiceContainer(); ServiceLocator.SetLocatorProvider(() => _serviceContainer); _serviceContainer.Register(typeof(MyGame), null, this); // ----- Storage // Create a "virtual file system" for reading game assets. _titleStorage = new TitleStorage("Content"); _assetsStorage = new ZipStorage(_titleStorage, "Content.zip"); _digitalRuneStorage = new ZipStorage(_titleStorage, "DigitalRune.zip"); _vfsStorage = new VfsStorage(); _vfsStorage.MountInfos.Add(new VfsMountInfo(_assetsStorage, null)); _vfsStorage.MountInfos.Add(new VfsMountInfo(_digitalRuneStorage, null)); // ----- Content _contentManager = new StorageContentManager(ServiceLocator.Current, _vfsStorage); _serviceContainer.Register(typeof(ContentManager), null, _contentManager); // ----- Graphics // Create Direct3D 11 device. var presentationParameters = new PresentationParameters { BackBufferWidth = 1, BackBufferHeight = 1, // Do not associate graphics device with any window. DeviceWindowHandle = IntPtr.Zero, }; var graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.HiDef, presentationParameters); // An IGraphicsDeviceService is required by the MonoGame/XNA content manager. _serviceContainer.Register(typeof(IGraphicsDeviceService), null, new DummyGraphicsDeviceManager(graphicsDevice)); // Get DXGIOutput to call WaitForVerticalBlank() in the game loop. using (var dxgiFactory = new SharpDX.DXGI.Factory1()) using (var dxgiAdapter = dxgiFactory.GetAdapter1(0)) _dxgiOutput = dxgiAdapter.GetOutput(0); // Create and register the graphics manager. _graphicsManager = new GraphicsManager(graphicsDevice, _contentManager); _serviceContainer.Register(typeof(IGraphicsService), null, _graphicsManager); // ----- Timing // The game loop runs in a parallel thread to keep the UI thread responsive. // To measure the time that has passed, we use a HighPrecisionClock. _clock = new HighPrecisionClock(); _clock.Start(); _gameLoopTask = ThreadPool.RunAsync(GameLoopTaskAction, WorkItemPriority.High, WorkItemOptions.TimeSliced) .AsTask(); // The FixedStepTimer reads the clock and triggers the game loop at 60 Hz. //_timer = new FixedStepTimer(_clock) //{ // StepSize = new TimeSpan(166667), // ~60 Hz // AccumulateTimeSteps = false, //}; // The VariableStepTimer reads the clock and triggers the game loop as often as possible. _timer = new VariableStepTimer(_clock); _timer.TimeChanged += (s, e) => GameLoop(e.DeltaTime); _timer.Start(); CoreApplication.Suspending += OnCoreApplicationSuspending; // DirectX buffers only a limit amount of Present calls per frame which is controlled by // the MaximumFrameLatency property. The default value is usually 3. If the application // uses more SwapChainPresentationTargets we must increase this property. //var d3dDevice = (SharpDX.Direct3D11.Device)_graphicsManager.GraphicsDevice.Handle; //using (var dxgiDevice2 = d3dDevice.QueryInterface<SharpDX.DXGI.Device2>()) // dxgiDevice2.MaximumFrameLatency = numberOfSwapChainPanels; }