public void InitializeUsingHolographicSpace()
        {
            // The holographic space might need to determine which adapter supports
            // holograms, in which case it will specify a non-zero PrimaryAdapterId.
            int   shiftPos = sizeof(uint);
            ulong id       = (ulong)holographicSpace.PrimaryAdapterId.LowPart | (((ulong)holographicSpace.PrimaryAdapterId.HighPart) << shiftPos);

            // When a primary adapter ID is given to the app, the app should find
            // the corresponding DXGI adapter and use it to create Direct3D devices
            // and device contexts. Otherwise, there is no restriction on the DXGI
            // adapter the app can use.
            if (id != 0)
            {
                // Create the DXGI factory.
                using (var dxgiFactory4 = new SharpDX.DXGI.Factory4())
                {
                    // Retrieve the adapter specified by the holographic space.
                    IntPtr adapterPtr;
                    dxgiFactory4.EnumAdapterByLuid((long)id, InteropStatics.IDXGIAdapter3, out adapterPtr);

                    if (adapterPtr != IntPtr.Zero)
                    {
                        dxgiAdapter = new SharpDX.DXGI.Adapter3(adapterPtr);
                    }
                }
            }
            else
            {
                RemoveAndDispose(ref dxgiAdapter);
            }

            CreateDeviceResources();

            holographicSpace.SetDirect3D11Device(d3dInteropDevice);
        }
Exemplo n.º 2
0
 public static void EnumAdpter()
 {
     SharpDX.DXGI.Factory4 dxgiFactory = new SharpDX.DXGI.Factory4();
     foreach (var a in dxgiFactory.Adapters)
     {
         Console.WriteLine($"{a.Description.Description}");
     }
 }
Exemplo n.º 3
0
        public void Detect()
        {
            var factory = new SharpDX.DXGI.Factory4();

            var adapterCount = factory.GetAdapterCount1();

            this.logger.Info("Detected {0} video adapters.", adapterCount);

            for (int i = 0; i < adapterCount; i++)
            {
                var adapter = factory.GetAdapter1(i);
                this.logger.Info("Adapter {0}: {1}", i, adapter.Description1.Description);
                int n = 0;
                foreach (var output in adapter.Outputs)
                {
                    var desc = output.Description;
                    this.logger.Info("         Output {0}: {1} Active: {2} Bounds: {3}", n++, desc.DeviceName, desc.IsAttachedToDesktop, RawRectToString(desc.DesktopBounds));
                }
            }
        }
        public void InitializeUsingHolographicSpace()
        {
            // The holographic space might need to determine which adapter supports
            // holograms, in which case it will specify a non-zero PrimaryAdapterId.
            int  shiftPos = sizeof(uint);
            long id       = (long)holographicSpace.PrimaryAdapterId.LowPart | (((long)holographicSpace.PrimaryAdapterId.HighPart) << shiftPos);

            // When a primary adapter ID is given to the app, the app should find
            // the corresponding DXGI adapter and use it to create Direct3D devices
            // and device contexts. Otherwise, there is no restriction on the DXGI
            // adapter the app can use.
            if (id != 0)
            {
                // Create the DXGI factory.
                using (var dxgiFactory4 = new SharpDX.DXGI.Factory4())
                {
                    // Retrieve the adapter specified by the holographic space.
                    var adapters = dxgiFactory4.Adapters1;
                    foreach (var adapter in adapters)
                    {
                        if (adapter.Description1.Luid == id)
                        {
                            dxgiAdapter = adapter as SharpDX.DXGI.Adapter3;
                        }
                    }
                }
            }
            else
            {
                this.RemoveAndDispose(ref dxgiAdapter);
            }

            CreateDeviceResources();

            holographicSpace.SetDirect3D11Device(d3dInteropDevice);
        }
Exemplo n.º 5
0
        public static bool ValidateDevice(Device device)
        {
            // The D3D Device is no longer valid if the default adapter changed since the device
            // was created or if the device has been removed.

            // First, get the LUID for the adapter from when the device was created.
            var previousAdapterLuid = device.AdapterLuid;

            // Next, get the information for the current default adapter.
            using (var factory = new Factory4())
            {
                var currentDefaultAdapter = factory.Adapters[0];
                var currentDesc           = currentDefaultAdapter.Description;

                // If the adapter LUIDs don't match, or if the device reports that it has been removed,
                // a new D3D device must be created.
                if (previousAdapterLuid != currentDesc.Luid ||
                    device.DeviceRemovedReason.Failure)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 6
0
        private void LoadPipeline()
        {
            viewport.Width    = this.width;
            viewport.Height   = this.height;
            viewport.MaxDepth = 1.0f;

            scissorRect.Right  = this.width;
            scissorRect.Bottom = this.height;

#if DEBUG
            // Enable the D3D12 debug layer.
            {
                DebugInterface.Get().EnableDebugLayer();
            }
#endif
            var fact = new SharpDX.DXGI.Factory1();
            SharpDX.DXGI.Adapter adapter = fact.GetAdapter(1);

            // create device
            using (var defaultDevice = new Device(adapter, SharpDX.Direct3D.FeatureLevel.Level_12_1))
                device = defaultDevice.QueryInterface <SharpDX.Direct3D12.Device2>();

            using (var factory = new SharpDX.DXGI.Factory4())
            {
                // Describe and create the command queue.
                var queueDesc = new CommandQueueDescription(CommandListType.Direct);
                commandQueue = device.CreateCommandQueue(queueDesc);


                // Describe and create the swap chain.
                var swapChainDesc = new SharpDX.DXGI.SwapChainDescription1()
                {
                    BufferCount       = FrameCount,
                    Format            = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    Height            = height,
                    Width             = width,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                    Scaling           = SharpDX.DXGI.Scaling.Stretch,
                    Stereo            = false,
                    SwapEffect        = SharpDX.DXGI.SwapEffect.FlipDiscard,
                    Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                };

                var tempSwapChain = new SharpDX.DXGI.SwapChain1(factory, commandQueue, ref swapChainDesc);
                swapChain = tempSwapChain.QueryInterface <SharpDX.DXGI.SwapChain3>();
                tempSwapChain.Dispose();
                frameIndex = swapChain.CurrentBackBufferIndex;

                using (SharpDX.DXGI.ISwapChainPanelNative nativeObject = ComObject.As <SharpDX.DXGI.ISwapChainPanelNative>(swapChainPanel))
                    nativeObject.SwapChain = swapChain;
            }

            // Create descriptor heaps.
            // Describe and create a render target view (RTV) descriptor heap.
            var rtvHeapDesc = new DescriptorHeapDescription()
            {
                DescriptorCount = FrameCount,
                Flags           = DescriptorHeapFlags.None,
                Type            = DescriptorHeapType.RenderTargetView
            };

            renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);

            rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);

            // Create frame resources.
            var rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            for (int n = 0; n < FrameCount; n++)
            {
                renderTargets[n] = swapChain.GetBackBuffer <Resource>(n);
                device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
                rtvHandle += rtvDescriptorSize;
            }

            commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
        }
Exemplo n.º 7
0
        public Pipeline(int frameCount, System.Drawing.Size size, System.IntPtr windowHandle)
        {
            // Fields
            FrameCount = frameCount;
            Size       = size;

            // Pipeline
            var d3d12Device = new SharpDX.Direct3D12.Device(null, SharpDX.Direct3D.FeatureLevel.Level_12_1);

            var queueDescription     = new SharpDX.Direct3D12.CommandQueueDescription(SharpDX.Direct3D12.CommandListType.Direct);
            var commandQueue         = d3d12Device.CreateCommandQueue(queueDescription);
            var swapChainDescription = new SharpDX.DXGI.SwapChainDescription()
            {
                BufferCount       = frameCount,
                ModeDescription   = new SharpDX.DXGI.ModeDescription(Size.Width, Size.Height, new SharpDX.DXGI.Rational(60, 1), SharpDX.DXGI.Format.R8G8B8A8_UNorm),
                Usage             = SharpDX.DXGI.Usage.RenderTargetOutput,
                SwapEffect        = SharpDX.DXGI.SwapEffect.FlipDiscard,
                OutputHandle      = windowHandle,
                Flags             = SharpDX.DXGI.SwapChainFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
                IsWindowed        = true
            };
            var rtvHeapDescription = new SharpDX.Direct3D12.DescriptorHeapDescription()
            {
                DescriptorCount = frameCount,
                Flags           = SharpDX.Direct3D12.DescriptorHeapFlags.None,
                Type            = SharpDX.Direct3D12.DescriptorHeapType.RenderTargetView
            };
            var renderTargetViewHeap = d3d12Device.CreateDescriptorHeap(rtvHeapDescription);
            var rtvHandle            = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
            var dxgiFactory          = new SharpDX.DXGI.Factory4();
            var swapChain            = new SharpDX.DXGI.SwapChain(dxgiFactory, commandQueue, swapChainDescription);
            var swapChain3           = swapChain.QueryInterface <SharpDX.DXGI.SwapChain3>();
            var frameIndex           = swapChain3.CurrentBackBufferIndex;
            var renderTargets        = new SharpDX.Direct3D12.Resource[frameCount];
            var commandAllocators    = new SharpDX.Direct3D12.CommandAllocator[frameCount];
            var rtvDescriptorSize    = d3d12Device.GetDescriptorHandleIncrementSize(SharpDX.Direct3D12.DescriptorHeapType.RenderTargetView);

            var d2d1Factory        = new SharpDX.Direct2D1.Factory();
            var d3d11Device        = SharpDX.Direct3D11.Device.CreateFromDirect3D12(d3d12Device, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, null, null, commandQueue);
            var d3d11On12Device    = d3d11Device.QueryInterface <SharpDX.Direct3D11.Device11On12>();
            var wrappedBackBuffers = new SharpDX.Direct3D11.Resource[frameCount];
            var d2dRenderTargets   = new SharpDX.Direct2D1.RenderTarget[frameCount];

            for (int i = 0; i < frameCount; i++)
            {
                renderTargets[i]     = swapChain3.GetBackBuffer <SharpDX.Direct3D12.Resource>(i);
                commandAllocators[i] = d3d12Device.CreateCommandAllocator(SharpDX.Direct3D12.CommandListType.Direct);
                d3d12Device.CreateRenderTargetView(renderTargets[i], null, rtvHandle);
                rtvHandle += rtvDescriptorSize;

                var format = new SharpDX.Direct3D11.D3D11ResourceFlags()
                {
                    BindFlags      = (int)SharpDX.Direct3D11.BindFlags.RenderTarget,
                    CPUAccessFlags = (int)SharpDX.Direct3D11.CpuAccessFlags.None
                };
                d3d11On12Device.CreateWrappedResource(renderTargets[i], format, (int)SharpDX.Direct3D12.ResourceStates.Present, (int)SharpDX.Direct3D12.ResourceStates.RenderTarget, typeof(SharpDX.Direct3D11.Resource).GUID, out wrappedBackBuffers[i]);
                var surface = wrappedBackBuffers[i].QueryInterface <SharpDX.DXGI.Surface>();
                d2dRenderTargets[i] = new SharpDX.Direct2D1.RenderTarget(d2d1Factory, surface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
            }

            // Assets
            var fenceEvent  = new System.Threading.AutoResetEvent(false);
            var fence       = d3d12Device.CreateFence(0, SharpDX.Direct3D12.FenceFlags.None);
            var fenceValues = new int[frameCount];

            for (int i = 0; i < frameCount; i++)
            {
                fenceValues[i] = 1;
            }

            var commandList = d3d12Device.CreateCommandList(SharpDX.Direct3D12.CommandListType.Direct, commandAllocators[frameIndex], pipelineState);

            commandList.Close();

            D3D12Device          = d3d12Device;
            CommandAllocators    = commandAllocators;
            RenderTargetViewHeap = renderTargetViewHeap;
            RenderTargets        = renderTargets;
            CommandQueue         = commandQueue;
            SwapChain3           = swapChain3;
            Fence              = fence;
            FenceEvent         = fenceEvent;
            D3D11Device        = d3d11Device;
            D3D11On12Device    = d3d11On12Device;
            WrappedBackBuffers = wrappedBackBuffers;
            D2DRenderTargets   = d2dRenderTargets;
            CommandList        = commandList;
            FenceValues        = fenceValues;
            RtvDescriptorSize  = rtvDescriptorSize;
            FrameIndex         = frameIndex;
        }