void resize() { if (renderView == null)//first show { var dxgiFactory = device.QueryInterface <IDXGIDevice>().GetParent <IDXGIAdapter>().GetParent <IDXGIFactory>(); var swapchainDesc = new SwapChainDescription() { BufferCount = 1, BufferDescription = new ModeDescription(Win32Window.Width, Win32Window.Height, format), IsWindowed = true, OutputWindow = Win32Window.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Vortice.DXGI.Usage.RenderTargetOutput }; swapChain = dxgiFactory.CreateSwapChain(device, swapchainDesc); dxgiFactory.MakeWindowAssociation(Win32Window.Handle, WindowAssociationFlags.IgnoreAll); backBuffer = swapChain.GetBuffer <ID3D11Texture2D>(0); renderView = device.CreateRenderTargetView(backBuffer); } else { renderView.Dispose(); backBuffer.Dispose(); swapChain.ResizeBuffers(1, Win32Window.Width, Win32Window.Height, format, SwapChainFlags.None); backBuffer = swapChain.GetBuffer <ID3D11Texture2D1>(0); renderView = device.CreateRenderTargetView(backBuffer); } }
public static ID2D1DeviceContext CreateRenderTarget( ID2D1Factory1 factory2d, ID3D11Device device3d) { var dxgiDevice = device3d.QueryInterface <IDXGIDevice>(); using (ID2D1Device device2d = factory2d.CreateDevice(dxgiDevice)) { return(device2d.CreateDeviceContext(DeviceContextOptions.None)); } }
public ID3D11Texture2D GetBackbuffer(ID3D11Device device, IntPtr hWnd) { if (!m_swapChain) { using (var dxgiDevice = new IDXGIDevice2()) { device.QueryInterface(ref IDXGIDevice2.IID, out dxgiDevice.PtrForNew).ThrowIfFailed(); dxgiDevice.GetAdapter(out IDXGIAdapter dxgiAdapter).ThrowIfFailed(); using (dxgiAdapter) { using (var dxgiFactory = new IDXGIFactory2()) { dxgiAdapter.GetParent(ref IDXGIFactory2.IID, out dxgiFactory.PtrForNew).ThrowIfFailed(); var desc = new DXGI_SWAP_CHAIN_DESC1 { Format = DXGI_FORMAT._B8G8R8A8_UNORM, AlphaMode = DXGI_ALPHA_MODE._UNSPECIFIED, BufferUsage = DXGI_USAGE._RENDER_TARGET_OUTPUT, Scaling = DXGI_SCALING._NONE, BufferCount = 2, SwapEffect = DXGI_SWAP_EFFECT._FLIP_SEQUENTIAL, SampleDesc = new DXGI_SAMPLE_DESC { Count = 1, Quality = 0, }, }; var fs_desc = new DXGI_SWAP_CHAIN_FULLSCREEN_DESC { Windowed = 1, }; var hr = dxgiFactory.CreateSwapChainForHwnd(device, hWnd, ref desc, ref fs_desc, null, out m_swapChain); hr.ThrowIfFailed(); } } } } var texture = new ID3D11Texture2D(); m_swapChain.GetBuffer(0, ref ID3D11Texture2D.IID, out texture.PtrForNew).ThrowIfFailed(); return(texture); }
public static IDXGISwapChain1 CreateSwapChainForHwnd( ID3D11Device device, IntPtr hwnd) { IDXGIDevice dxgiDevice = device.QueryInterface <IDXGIDevice>(); IDXGIFactory2 dxgiFactory = dxgiDevice.GetAdapter().GetParent <IDXGIFactory2>(); SwapChainDescription1 dxgiDesc = new() { Format = Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), BufferUsage = Usage.RenderTargetOutput, BufferCount = 2, }; return(dxgiFactory.CreateSwapChainForHwnd( device, hwnd, dxgiDesc)); }
public D3D11GraphicsDevice(bool validation, Window window) { Window = window; if (CreateDXGIFactory1(out Factory).Failure) { throw new InvalidOperationException("Cannot create IDXGIFactory1"); } var creationFlags = DeviceCreationFlags.BgraSupport; if (validation) { creationFlags |= DeviceCreationFlags.Debug; } if (D3D11CreateDevice( IntPtr.Zero, DriverType.Hardware, creationFlags, s_featureLevels, out Device, out FeatureLevel, out DeviceContext).Failure) { // Remove debug flag not being supported. creationFlags &= ~DeviceCreationFlags.Debug; var result = D3D11CreateDevice(null, DriverType.Hardware, creationFlags, s_featureLevels, out Device, out FeatureLevel, out DeviceContext); if (result.Failure) { // This will fail on Win 7 due to lack of 11.1, so re-try again without it D3D11CreateDevice( IntPtr.Zero, DriverType.Hardware, creationFlags, s_featureLevelsNoLevel11, out Device, out FeatureLevel, out DeviceContext).CheckError(); } } using (IDXGIDevice dxgiDevice = Device.QueryInterface <IDXGIDevice>()) { // Store a pointer to the DXGI adapter. // This is for the case of no preferred DXGI adapter, or fallback to WARP. dxgiDevice.GetAdapter(out Adapter).CheckError(); } IntPtr hwnd = (IntPtr)window.Handle; var swapChainDescription = new SwapChainDescription() { BufferCount = FrameCount, BufferDescription = new ModeDescription(window.Width, window.Height, Format.B8G8R8A8_UNorm), IsWindowed = true, OutputWindow = hwnd, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = DXGI.Usage.RenderTargetOutput }; SwapChain = Factory.CreateSwapChain(Device, swapChainDescription); Factory.MakeWindowAssociation(hwnd, WindowAssociationFlags.IgnoreAltEnter); BackBuffer = SwapChain.GetBuffer <ID3D11Texture2D>(0); RenderTargetView = Device.CreateRenderTargetView(BackBuffer); }
public D3D11GraphicsDevice(D3D11DeviceOptions options, SwapchainDescription?swapchainDesc) { var flags = (DeviceCreationFlags)options.DeviceCreationFlags; #if DEBUG flags |= DeviceCreationFlags.Debug; #endif // If debug flag set but SDK layers aren't available we can't enable debug. if (0 != (flags & DeviceCreationFlags.Debug) && !Vortice.Direct3D11.D3D11.SdkLayersAvailable()) { flags &= ~DeviceCreationFlags.Debug; } try { if (options.AdapterPtr != IntPtr.Zero) { _dxgiAdapter = new IDXGIAdapter(options.AdapterPtr); Vortice.Direct3D11.D3D11.D3D11CreateDevice(_dxgiAdapter, Vortice.Direct3D.DriverType.Hardware, flags, new[] { Vortice.Direct3D.FeatureLevel.Level_11_1, Vortice.Direct3D.FeatureLevel.Level_11_0, }, out _device).CheckError(); } else { Vortice.Direct3D11.D3D11.D3D11CreateDevice(null, Vortice.Direct3D.DriverType.Hardware, flags, new[] { Vortice.Direct3D.FeatureLevel.Level_11_1, Vortice.Direct3D.FeatureLevel.Level_11_0, }, out _device).CheckError(); } } catch { Vortice.Direct3D11.D3D11.D3D11CreateDevice(null, Vortice.Direct3D.DriverType.Hardware, flags, null, out _device).CheckError(); } using (var dxgiDevice = _device.QueryInterface <Vortice.DXGI.IDXGIDevice>()) { // Store a pointer to the DXGI adapter. // This is for the case of no preferred DXGI adapter, or fallback to WARP. _dxgiAdapter = dxgiDevice.Adapter; } if (swapchainDesc != null) { SwapchainDescription desc = swapchainDesc.Value; _mainSwapchain = new D3D11Swapchain(_device, ref desc); } _immediateContext = _device.ImmediateContext; _device.CheckThreadingSupport(out _supportsConcurrentResources, out _supportsCommandLists); Features = new GraphicsDeviceFeatures( computeShader: true, geometryShader: true, tessellationShaders: true, multipleViewports: true, samplerLodBias: true, drawBaseVertex: true, drawBaseInstance: true, drawIndirect: true, drawIndirectBaseInstance: true, fillModeWireframe: true, samplerAnisotropy: true, depthClipDisable: true, texture1D: true, independentBlend: true, structuredBuffer: true, subsetTextureView: true, commandListDebugMarkers: _device.FeatureLevel >= Vortice.Direct3D.FeatureLevel.Level_11_1, bufferRangeBinding: _device.FeatureLevel >= Vortice.Direct3D.FeatureLevel.Level_11_1, shaderFloat64: _device.CheckFeatureSupport <FeatureDataDoubles>(Vortice.Direct3D11.Feature.Doubles).DoublePrecisionFloatShaderOps); _d3d11ResourceFactory = new D3D11ResourceFactory(this); _d3d11Info = new BackendInfoD3D11(this); PostDeviceCreated(); }
void EnsureDevice(IntPtr hWnd) { if (m_device) { return; } // D3D Span <D3D_FEATURE_LEVEL> levels = stackalloc D3D_FEATURE_LEVEL[] { D3D_FEATURE_LEVEL._11_1, D3D_FEATURE_LEVEL._11_0, D3D_FEATURE_LEVEL._10_1, D3D_FEATURE_LEVEL._10_0, D3D_FEATURE_LEVEL._9_3, D3D_FEATURE_LEVEL._9_2, D3D_FEATURE_LEVEL._9_1 }; var flags = D3D11_CREATE_DEVICE_FLAG._DEBUG | D3D11_CREATE_DEVICE_FLAG._BGRA_SUPPORT; var level = default(D3D_FEATURE_LEVEL); d3d11.D3D11CreateDevice( null, D3D_DRIVER_TYPE._HARDWARE, IntPtr.Zero, (uint)flags, ref MemoryMarshal.GetReference(levels), (uint)levels.Length, Constants.D3D11_SDK_VERSION, out m_device, out level, out m_context).ThrowIfFailed(); // D2D using (var dxgiDevice = new IDXGIDevice()) { m_device.QueryInterface(ref IDXGIDevice.IID, out dxgiDevice.PtrForNew).ThrowIfFailed(); using (var d2dFactory = new ID2D1Factory1()) { var factory_opt = new D2D1_FACTORY_OPTIONS { }; d2d1.D2D1CreateFactory(D2D1_FACTORY_TYPE._SINGLE_THREADED, ref ID2D1Factory1.IID, ref factory_opt, out d2dFactory.PtrForNew).ThrowIfFailed(); d2dFactory.GetDesktopDpi(out float x, out float y); // using (var d2dDevice = new ()) { var prop = new D2D1_CREATION_PROPERTIES { }; d2dFactory.CreateDevice(dxgiDevice, out ID2D1Device d2dDevice).ThrowIfFailed(); using (d2dDevice) d2dDevice.CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS._NONE, out m_d2dContext).ThrowIfFailed(); } } // SWAPChain // using (var adapter = new ()) { dxgiDevice.GetAdapter(out IDXGIAdapter adapter).ThrowIfFailed(); using (adapter) using (var dxgiFactory = new IDXGIFactory2()) { adapter.GetParent(ref IDXGIFactory2.IID, out dxgiFactory.PtrForNew).ThrowIfFailed(); var swapChainDesc = new DXGI_SWAP_CHAIN_DESC1 { Width = 0, Height = 0, Format = DXGI_FORMAT._B8G8R8A8_UNORM, Stereo = 0 }; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.BufferUsage = DXGI_USAGE._RENDER_TARGET_OUTPUT; swapChainDesc.BufferCount = 2; //swapChainDesc.Scaling = DXGI_SCALING_NONE; swapChainDesc.Scaling = DXGI_SCALING._STRETCH; //swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT._DISCARD; swapChainDesc.AlphaMode = DXGI_ALPHA_MODE._UNSPECIFIED; var fs = new DXGI_SWAP_CHAIN_FULLSCREEN_DESC { Windowed = 1, }; dxgiFactory.CreateSwapChainForHwnd( dxgiDevice, hWnd, ref swapChainDesc, ref fs, null, out m_swapchain).ThrowIfFailed(); Console.Write("CreateSwapchain"); } } } // Dwrite dwrite.DWriteCreateFactory(DWRITE_FACTORY_TYPE._SHARED, ref IDWriteFactory.IID, out m_dwriteFactory.PtrForNew).ThrowIfFailed(); }
public bool Init(IntPtr WindowHandle) { var factory = DXGI.CreateDXGIFactory1 <IDXGIFactory1>(); var adapter = factory.GetAdapter1(0); var monitor = adapter.GetOutput(0); var modes = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced); var rational = new Rational(0, 1); var adapterDescription = adapter.Description; //VideoCardMemory = adapterDescription.DedicatedVideoMemory >> 10 >> 10; //VideoCardDescription = adapterDescription.Description.Trim('\0'); monitor.Dispose(); adapter.Dispose(); var swapChainDesc = new SwapChainDescription() { BufferCount = 2, BufferDescription = new ModeDescription(1920, 1080, rational, Format.R8G8B8A8_UNorm), Usage = Usage.RenderTargetOutput, OutputWindow = WindowHandle, SampleDescription = new SampleDescription(1, 0), IsWindowed = true, Flags = SwapChainFlags.None, SwapEffect = SwapEffect.Discard }; // Create Device and DeviceContext ID3D11Device TempDevice = null; ID3D11DeviceContext TempDeviceContext = null; D3D11.D3D11CreateDevice(adapter, DriverType.Hardware, DeviceCreationFlags.None, null, out TempDevice, out TempDeviceContext); Device = TempDevice.QueryInterface <ID3D11Device1>(); DeviceContext = TempDeviceContext.QueryInterface <ID3D11DeviceContext1>(); TempDevice.Dispose(); TempDeviceContext.Dispose(); // Create SwapChain SwapChain = factory.CreateSwapChain(Device, swapChainDesc); factory.MakeWindowAssociation(WindowHandle, WindowAssociationFlags.IgnoreAltEnter); var backBuffer = SwapChain.GetBuffer <ID3D11Texture2D>(0); m_RenderTargetView = Device.CreateRenderTargetView(backBuffer); backBuffer.Dispose(); // Create blend state BlendDescription bsd = new BlendDescription() { AlphaToCoverageEnable = false,//true, IndependentBlendEnable = false, }; bsd.RenderTarget[0].BlendOperationAlpha = BlendOperation.Add; bsd.RenderTarget[0].BlendOperation = BlendOperation.Add; bsd.RenderTarget[0].DestinationBlendAlpha = Blend.One; bsd.RenderTarget[0].DestinationBlend = Blend.InverseSourceAlpha; bsd.RenderTarget[0].IsBlendEnabled = true; bsd.RenderTarget[0].RenderTargetWriteMask = ColorWriteEnable.All; bsd.RenderTarget[0].SourceBlendAlpha = Blend.Zero; bsd.RenderTarget[0].SourceBlend = Blend.SourceAlpha; bsd.AlphaToCoverageEnable = true; ID3D11BlendState bsAlpha = Device.CreateBlendState(bsd); // Set Blend State DeviceContext.OMSetBlendState(bsAlpha); BuildDepthStencilView(1920, 1080); // Create rasterizers m_RSDesc = new RasterizerDescription() { AntialiasedLineEnable = false, CullMode = CullMode.Back, DepthBias = 0, DepthBiasClamp = .0f, DepthClipEnable = false, FillMode = FillMode.Solid, FrontCounterClockwise = true, MultisampleEnable = true, ScissorEnable = false, SlopeScaledDepthBias = .0f }; m_RSCullSolid = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.CullMode = CullMode.None; m_RSSolid = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.FillMode = FillMode.Wireframe; m_RSWireFrame = Device.CreateRasterizerState(m_RSDesc); m_RSDesc.CullMode = CullMode.Back; m_RSCullWireFrame = Device.CreateRasterizerState(m_RSDesc); UpdateRasterizer(); return(true); }