public void ConstructRenderAndResource(double width, double height) { float dpiX, dpiY; this.GetDpi(out dpiX, out dpiY); var desc = new DXGI.SwapChainDescription() { BufferCount = 1, ModeDescription = new DXGI.ModeDescription((int)width, (int)height, new DXGI.Rational(60, 1), DXGI.Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = TextBox.Handle, SampleDescription = new DXGI.SampleDescription(1, 0), SwapEffect = DXGI.SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; this.swapchain = new DXGI.SwapChain(factory_dxgi, device, desc); this.device2d = new D2D.Device1(this._factory.D2DFactory, device_dxgi); this.render = new D2D.DeviceContext1(this.device2d, D2D.DeviceContextOptions.None); D2D.BitmapProperties bmpProp = new D2D.BitmapProperties(); bmpProp.DpiX = dpiX; bmpProp.DpiY = dpiY; bmpProp.PixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied); this.bmp_d2d = new D2D.Bitmap(this.render, DXGI.Surface.FromSwapChain(swapchain, 0), bmpProp); this.cachedBitMap = new D2D.Bitmap(this.render, new SharpDX.Size2((int)width, (int)height), bmpProp); this.hasCache = false; this.render.Target = this.bmp_d2d; this.textRender = new CustomTextRenderer(this._factory, this.Foreground); this.renderSize = new Size(width, height); //デフォルト値を反映させる this.Foreground = ToColor4(this.TextBox.Foreground); this.Background = ToColor4(this.TextBox.Background); this.ControlChar = ToColor4(this.TextBox.ControlChar); this.Url = ToColor4(this.TextBox.Url); this.Keyword1 = ToColor4(this.TextBox.Keyword1); this.Keyword2 = ToColor4(this.TextBox.Keyword2); this.Literal = ToColor4(this.TextBox.Literal); this.Comment = ToColor4(this.TextBox.Comment); this.Hilight = ToColor4(this.TextBox.Hilight); this.LineMarker = ToColor4(this.TextBox.LineMarker); this.InsertCaret = ToColor4(this.TextBox.InsertCaret); this.OverwriteCaret = ToColor4(this.TextBox.OverwriteCaret); this.UpdateArea = ToColor4(this.TextBox.UpdateArea); this.HilightForeground = ToColor4(this.TextBox.HilightForeground); }
void CreateRenderingResources(SharpDX.Direct3D11.Device device3d, SharpDX.Direct2D1.Factory2 d2dFactory) { device = device3d; Factory2D = d2dFactory; dxgiDevice = device.QueryInterface <SharpDX.DXGI.Device1>(); d2dDevice = new SharpDX.Direct2D1.Device1(Factory2D, dxgiDevice); d2dResourceCreationDeviceContext = new SharpDX.Direct2D1.DeviceContext1(d2dDevice, DeviceContextOptions.None); }
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("DirectX10デバイスの作成に失敗しました"); } var dxgiDevice = this.device.QueryInterface <DXGI.Device>(); this.device2d = new D2D.Device1(this._factory.D2DFactory, dxgiDevice); dxgiDevice.Dispose(); this.render = new D2D.DeviceContext1(this.device2d, D2D.DeviceContextOptions.None); IntPtr DesktopWnd = NativeMethods.GetDesktopWindow(); D3D9.Direct3DEx d3dex = new D3D9.Direct3DEx(); D3D9.PresentParameters param = new D3D9.PresentParameters(); param.Windowed = true; param.SwapEffect = D3D9.SwapEffect.Discard; param.DeviceWindowHandle = DesktopWnd; param.PresentationInterval = D3D9.PresentInterval.Default; try { this.device9 = new D3D9.DeviceEx( d3dex, 0, D3D9.DeviceType.Hardware, DesktopWnd, D3D9.CreateFlags.HardwareVertexProcessing | D3D9.CreateFlags.Multithreaded | D3D9.CreateFlags.FpuPreserve, param); } catch { try { this.device9 = new D3D9.DeviceEx( d3dex, 0, D3D9.DeviceType.Hardware, DesktopWnd, D3D9.CreateFlags.SoftwareVertexProcessing | D3D9.CreateFlags.Multithreaded | D3D9.CreateFlags.FpuPreserve, param); } catch { throw new PlatformNotSupportedException("DirectX9デバイスの作成に失敗しました"); } } finally { d3dex.Dispose(); } }
/// <summary> /// Initializes a new instance of the <see cref="DeviceContext1"/> class using an existing <see cref="Device1"/>. /// </summary> /// <param name="device">The device.</param> /// <param name="options">The options to be applied to the created device context.</param> /// <remarks> /// The new device context will not have a selected target bitmap. The caller must create and select a bitmap as the target surface of the context. /// </remarks> /// <unmanaged>HRESULT ID2D1Device1::CreateDeviceContext([In] D2D1_DEVICE_CONTEXT_OPTIONS options,[Out] ID2D1DeviceContext1** deviceContext1)</unmanaged> public DeviceContext1(Device1 device, DeviceContextOptions options) : base(IntPtr.Zero) { device.CreateDeviceContext(options, this); }
void CreateRenderingResources (SharpDX.Direct3D11.Device device3d, SharpDX.Direct2D1.Factory2 d2dFactory) { device = device3d; Factory2D = d2dFactory; dxgiDevice = device.QueryInterface<SharpDX.DXGI.Device1>(); d2dDevice = new SharpDX.Direct2D1.Device1(Factory2D, dxgiDevice); d2dResourceCreationDeviceContext = new SharpDX.Direct2D1.DeviceContext1(d2dDevice, DeviceContextOptions.None); }
private static void CreateDevices(int graphicsAdapter) { var deviceCreationFlags = DeviceCreationFlags.BgraSupport; #if DEBUG deviceCreationFlags |= DeviceCreationFlags.Debug; #endif Logger.Info("Attempting to create device."); var adapter = Factory.GetAdapter(graphicsAdapter); var levels = new[] { FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0, FeatureLevel.Level_9_3 }; var device = new SharpDX.Direct3D11.Device(adapter, deviceCreationFlags, levels) { DebugName = adapter.Description.Description }; Logger.Info( $"GPU{graphicsAdapter}: {device.DebugName} ({((long)adapter.Description.DedicatedVideoMemory).ToPrettySize()} VRAM)"); Device = device.QueryInterface <DeviceD3D>(); Device.DebugName = device.DebugName; Logger.Info("D3D Device created."); if (deviceCreationFlags.HasFlag(DeviceCreationFlags.Debug)) { DeviceDebug = new DeviceDebug(Device); Logger.Info("Debug device created."); } DxgiDevice = Device.QueryInterface <Device>(); Context = Device.ImmediateContext; Device2D = new DeviceD2D(Factory2D, DxgiDevice); Context2D = new ContextD2D(Device2D, DeviceContextOptions.EnableMultithreadedOptimizations) { TextAntialiasMode = RenderSettings.UseClearTypeRendering ? TextAntialiasMode.Cleartype : (RenderSettings.GuiAntiAliasing ? TextAntialiasMode.Grayscale : TextAntialiasMode.Aliased), AntialiasMode = RenderSettings.GuiAntiAliasing ? AntialiasMode.PerPrimitive : AntialiasMode.Aliased, UnitMode = UnitMode.Pixels }; Logger.Info("D2D Device created."); Logger.Info("Filling out DebugSettings GPU info."); DebugSettings.FillGpuInfo(adapter); #if DEBUG try { DeviceDebug = new DeviceDebug(Device); Logger.Info("Debug device created."); } catch (SharpDXException) { Logger.Warn("DeviceDebug not supported."); } #endif Logger.Info("Renderer initialized."); }
/// <summary> /// Initializes a new instance of the <see cref="Renderer" /> class. /// </summary> /// <param name="gameEngine">The game engine.</param> /// <param name="form">The form.</param> /// <param name="width">The primary width of the view in pixels.</param> /// <param name="height">The primary height of the view in pixels.</param> public Renderer(IGameEngine gameEngine, GameForm form, int width, int height) { if (gameEngine == null) { throw new ArgumentNullException(nameof(gameEngine)); } if (form == null) { throw new ArgumentNullException(nameof(form)); } _form = form; _resize = false; _scale = 1.0f; _width = width; _height = height; _dwFactory = new SharpDX.DirectWrite.Factory(); InitFonts(); // 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. using (var dxgiDevice2 = _device.QueryInterface <SharpDX.DXGI.Device2>()) { _d2dFactory = new SharpDX.Direct2D1.Factory2(SharpDX.Direct2D1.FactoryType.SingleThreaded); // Get the default Direct2D device and create a context. using (var d2dDevice = new SharpDX.Direct2D1.Device1(_d2dFactory, dxgiDevice2)) { _d2dContext = new SharpDX.Direct2D1.DeviceContext1(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None); } } CreateSizeDependentResources(); _d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Aliased; _d2dContext.AntialiasMode = AntialiasMode.Aliased; _d2dContext.UnitMode = UnitMode.Pixels; _hudYellow = new SolidColorBrush(_d2dContext, new Color4(1.0f, 1.0f, 0.0f, 1.0f)); _hudWhite = new SolidColorBrush(_d2dContext, new Color4(1.0f, 1.0f, 1.0f, 1.0f)); _hudTextFormat = new TextFormat(_dwFactory, "Sonic Genesis/Mega Drive Font", _fontCollection, FontWeight.Normal, FontStyle.Normal, FontStretch.Normal, 14); // init game stuff _game = gameEngine; _texResMan = new TextureResourceManager(gameEngine, _d2dContext); _spriteResMan = new SpriteResourceManager(gameEngine, _d2dContext); // get the services _mapService = _game.GetService <IMapService>(); _varService = _game.GetService <IVariableService>(); _entityService = _game.GetService <IEntityService>(); _renderService = _game.GetService <IRenderService>(); _collisionService = _game.GetService <ICollisionService>(); _varShowCollisionMaps = _varService.GetVar <bool>("r_showcollisionmaps"); _varShowTileFrames = _varService.GetVar <bool>("r_showtileframes"); _varShowEntityOrigins = _varService.GetVar <bool>("r_showentityorigins"); _varShowTraceLines = _varService.GetVar <bool>("r_showtracelines"); _varShowCollisionBoxes = _varService.GetVar <bool>("r_showcollisionboxes"); _rc = new RenderContext(); _spriteRenderList = new List <Animatable>(25); }