public HolographicGraphicsPresenter(GraphicsDevice device, PresentationParameters presentationParameters, HolographicSpace holographicSpace)
            : base(device, presentationParameters)
        {
            if (GraphicsDevice.RenderTargetViewAllocator.DescriptorHeap.Description.DescriptorCount != BufferCount)
            {
                GraphicsDevice.RenderTargetViewAllocator.Dispose();
                GraphicsDevice.RenderTargetViewAllocator = new DescriptorAllocator(GraphicsDevice, DescriptorHeapType.RenderTargetView, descriptorCount: BufferCount);
            }

            using (IDXGIDevice dxgiDevice = GraphicsDevice.NativeDirect3D11Device.QueryInterface <IDXGIDevice>())
            {
                IDirect3DDevice direct3DInteropDevice = Direct3DInterop.CreateDirect3DDevice(dxgiDevice);

                HolographicSpace = holographicSpace;
                HolographicSpace.SetDirect3D11Device(direct3DInteropDevice);
            }

            HolographicDisplay = HolographicDisplay.GetDefault();
            SpatialStationaryFrameOfReference = HolographicDisplay.SpatialLocator.CreateStationaryFrameOfReferenceAtCurrentLocation();

            HolographicFrame      = HolographicSpace.CreateNextFrame();
            HolographicSurface    = HolographicFrame.GetRenderingParameters(HolographicFrame.CurrentPrediction.CameraPoses[0]).Direct3D11BackBuffer;
            HolographicBackBuffer = GetHolographicBackBuffer();

            renderTarget           = CreateRenderTarget();
            direct3D11RenderTarget = CreateDirect3D11RenderTarget();
        }
Beispiel #2
0
 public void InitializeD3D11()
 {
     DXInteropHelper.InitializeD3D(D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE, out d3d11Device, D3D11_CREATE_DEVICE_FLAG.D3D11_CREATE_DEVICE_BGRA_SUPPORT);
     DXInteropHelper.InitializeD2D(d3d11Device, out d2dDeviceContext);
     dxgiDevice    = DXInteropHelper.GetDXGIDevice(d3d11Device);
     dxgiSwapChain = DXInteropHelper.CreateSwapChainForComposition(d3d11Device, (int)Window.Current.Bounds.Width, (int)Window.Current.Bounds.Height, true);
 }
Beispiel #3
0
        public unsafe Result SetDevice(IDXGIDevice device)
        {
            Result result = LocalInterop.CalliStdCallint(_nativePointer, device.NativePointer.ToPointer(), (*(void ***)_nativePointer)[3]);

            GC.KeepAlive(device);
            return(result);
        }
Beispiel #4
0
    public static T CreateDirect3D11DeviceFromDXGIDevice <T>(IDXGIDevice dxgiDevice)
    {
        CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice, out IntPtr graphicsDevicePtr).CheckError();
        T graphicsDevice = (T)Marshal.GetObjectForIUnknown(graphicsDevicePtr);

        Marshal.Release(graphicsDevicePtr);
        return(graphicsDevice);
    }
Beispiel #5
0
 public SurfaceImageSourceWrapper(SurfaceImageSourceInterop v_sis, Rect rect1arg, float[] colorargs1, Rect rectarg2, float[] colorargs2, IDXGIDevice device)
 {
     surfaceImageSource = v_sis;
     color1             = colorargs1;
     color2             = colorargs2;
     rect1       = rect1arg;
     rect2       = rectarg2;
     this.device = device;
 }
        public static IComObject <IDXGIAdapter> GetAdapter(this IDXGIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            device.GetAdapter(out var adapter).ThrowOnError();
            return(new ComObject <IDXGIAdapter>(adapter));
        }
Beispiel #7
0
 public void UpdateSurfaceImageSource(IDXGIDevice device)
 {
     if (device != null)
     {
         this.device = device;
     }
     surfaceImageSource.SetDevice(this.device);
     surfaceImageSource.UpdateSurface(rect1, color1);
     surfaceImageSource.UpdateSurface(rect2, color2);
 }
        public static int GetGPUThreadPriority(this IDXGIDevice device)
        {
            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            device.GetGPUThreadPriority(out var priority).ThrowOnError();
            return(priority);
        }
Beispiel #9
0
        /// <summary>
        /// Creates a new device object that can be used to create other Microsoft DirectComposition objects.
        /// </summary>
        /// <typeparam name="T">A generic type of <see cref="IDCompositionDevice"/>.</typeparam>
        /// <param name="dxgiDevice">The DXGI device <see cref="IDXGIDevice"/> to use to create DirectComposition surface objects.</param>
        /// <param name="compositionDevice">An instance of <see cref="IDCompositionDevice"/> interface or null if failure</param>
        /// <returns>The <see cref="Result"/> code.</returns>
        public static Result DCompositionCreateDevice <T>(IDXGIDevice dxgiDevice, out T?compositionDevice) where T : IDCompositionDevice
        {
            Result result = DCompositionCreateDevice(dxgiDevice, typeof(T).GUID, out IntPtr nativePtr);

            if (result.Failure)
            {
                compositionDevice = default;
                return(result);
            }

            compositionDevice = MarshallingHelpers.FromPointer <T>(nativePtr);
            return(result);
        }
Beispiel #10
0
    public static Result CreateDirect3D11DeviceFromDXGIDevice <T>(IDXGIDevice dxgiDevice, out T?graphicsDevice)
    {
        Result result = CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice, out IntPtr graphicsDevicePtr);

        if (result.Failure)
        {
            graphicsDevice = default;
            return(result);
        }

        graphicsDevice = (T)Marshal.GetObjectForIUnknown(graphicsDevicePtr);
        Marshal.Release(graphicsDevicePtr);
        return(result);
    }
        public static ComObject <T> CreateDevice <T>(this ID2D1Factory1 factory, IDXGIDevice device) where T : ID2D1Device
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            if (device == null)
            {
                throw new ArgumentNullException(nameof(device));
            }

            factory.CreateDevice(device, out var dev).ThrowOnError();
            return(new ComObject <T>((T)dev));
        }
Beispiel #12
0
        internal static IDirect3DDevice CreateDirect3DDevice(IDXGIDevice dxgiDevice)
        {
            Result result = CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.NativePointer, out IntPtr graphicsDevice);

            if (result.Failure)
            {
                throw new InvalidOperationException(result.Code.ToString());
            }

            IDirect3DDevice d3DInteropDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(graphicsDevice);

            Marshal.Release(graphicsDevice);

            return(d3DInteropDevice);
        }
Beispiel #13
0
        internal static IDirect3DDevice CreateDirect3DDevice(IDXGIDevice dxgiDevice)
        {
            Result result = CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.NativePointer, out IntPtr graphicsDevice);

            if (result.Failure)
            {
                throw new COMException("Device creation failed.", result.Code);
            }

            IDirect3DDevice d3DInteropDevice = (IDirect3DDevice)Marshal.GetObjectForIUnknown(graphicsDevice);

            Marshal.Release(graphicsDevice);

            return(d3DInteropDevice);
        }
Beispiel #14
0
        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));
        }
Beispiel #15
0
        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();
        }
Beispiel #16
0
        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)
                {
                    VorticeD3D11.D3D11CreateDevice(options.AdapterPtr,
                                                   Vortice.Direct3D.DriverType.Hardware,
                                                   flags,
                                                   new[]
                    {
                        Vortice.Direct3D.FeatureLevel.Level_11_1,
                        Vortice.Direct3D.FeatureLevel.Level_11_0,
                    },
                                                   out _device).CheckError();
                }
                else
                {
                    VorticeD3D11.D3D11CreateDevice(IntPtr.Zero,
                                                   Vortice.Direct3D.DriverType.Hardware,
                                                   flags,
                                                   new[]
                    {
                        Vortice.Direct3D.FeatureLevel.Level_11_1,
                        Vortice.Direct3D.FeatureLevel.Level_11_0,
                    },
                                                   out _device).CheckError();
                }
            }
            catch
            {
                VorticeD3D11.D3D11CreateDevice(IntPtr.Zero,
                                               Vortice.Direct3D.DriverType.Hardware,
                                               flags,
                                               null,
                                               out _device).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 _dxgiAdapter).CheckError();
                _deviceName = _dxgiAdapter.Description.Description;
            }

            if (swapchainDesc != null)
            {
                SwapchainDescription desc = swapchainDesc.Value;
                _mainSwapchain = new D3D11Swapchain(this, 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();
        }
Beispiel #17
0
 /// <summary>
 /// Creates a new device object that can be used to create other Microsoft DirectComposition objects.
 /// </summary>
 /// <typeparam name="T">A generic type of <see cref="IDCompositionDevice"/>.</typeparam>
 /// <param name="dxgiDevice">The DXGI device <see cref="IDXGIDevice"/> to use to create DirectComposition surface objects.</param>
 /// <returns>An instance of <see cref="IDCompositionDevice"/> interface .</returns>
 public static T DCompositionCreateDevice <T>(IDXGIDevice dxgiDevice) where T : IDCompositionDevice
 {
     DCompositionCreateDevice(dxgiDevice, typeof(T).GUID, out IntPtr nativePtr).CheckError();
     return(MarshallingHelpers.FromPointer <T>(nativePtr));
 }
        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);
        }