Esempio n. 1
0
        /// <summary>
        /// Find the display mode that most closely matches the requested display mode.
        /// </summary>
        /// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
        /// <param name="mode">The mode.</param>
        /// <returns>Returns the closes display mode.</returns>
        /// <unmanaged>HRESULT IDXGIOutput::FindClosestMatchingMode([In] const DXGI_MODE_DESC* pModeToMatch,[Out] DXGI_MODE_DESC* pClosestMatch,[In, Optional] IUnknown* pConcernedDevice)</unmanaged>
        /// <remarks>Direct3D devices require UNORM formats. This method finds the closest matching available display mode to the mode specified in pModeToMatch. Similarly ranked fields (i.e. all specified, or all unspecified, etc) are resolved in the following order.  ScanlineOrdering Scaling Format Resolution RefreshRate  When determining the closest value for a particular field, previously matched fields are used to filter the display mode list choices, and  other fields are ignored. For example, when matching Resolution, the display mode list will have already been filtered by a certain ScanlineOrdering,  Scaling, and Format, while RefreshRate is ignored. This ordering doesn't define the absolute ordering for every usage scenario of FindClosestMatchingMode, because  the application can choose some values initially, effectively changing the order that fields are chosen. Fields of the display mode are matched one at a time, generally in a specified order. If a field is unspecified, FindClosestMatchingMode gravitates toward the values for the desktop related to this output.  If this output is not part of the desktop, then the default desktop output is used to find values. If an application uses a fully unspecified  display mode, FindClosestMatchingMode will typically return a display mode that matches the desktop settings for this output.   Unspecified fields are lower priority than specified fields and will be resolved later than specified fields.</remarks>
        public DisplayMode FindClosestMatchingDisplayMode(GraphicsProfile[] targetProfiles, DisplayMode mode)
        {
            if (targetProfiles == null)
            {
                throw new ArgumentNullException("targetProfiles");
            }

            ModeDescription closestDescription;

            SharpDX.Direct3D12.Device deviceTemp = null;
            for (int i = 0; i < targetProfiles.Length; i++)
            {
                // Create Device D3D12 with feature Level based on profile
                try
                {
                    deviceTemp = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, (FeatureLevel)targetProfiles[i]);
                    break;
                }
                catch (Exception)
                {
                }
            }

            if (deviceTemp == null)
            {
                throw new InvalidOperationException("Could not create D3D12 graphics device");
            }

            var description = new SharpDX.DXGI.ModeDescription()
            {
                Width            = mode.Width,
                Height           = mode.Height,
                RefreshRate      = mode.RefreshRate.ToSharpDX(),
                Format           = (SharpDX.DXGI.Format)mode.Format,
                Scaling          = DisplayModeScaling.Unspecified,
                ScanlineOrdering = DisplayModeScanlineOrder.Unspecified
            };

            using (var device = deviceTemp)
                output.GetClosestMatchingMode(device, description, out closestDescription);

            return(DisplayMode.FromDescription(closestDescription));
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes the presentable resources before resize.
        /// </summary>
        /// <param name="device"></param>
        public void InitializePresentable(GraphicsDevice device)
        {
            Device3D12 device3D12 = device.NativeDevice;

            deviceContext2D = new GraphicsDeviceContext2D(DeviceContext);

            Brush = new SolidColorBrush(DeviceContext, SharpDX.Color.White);

            var properties = new BitmapProperties1(
                new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied),
                DesktopDpi.Width,
                DesktopDpi.Height,
                BitmapOptions.Target | BitmapOptions.CannotDraw);

            FrameResources = new FrameResource[Presenter.BackBufferCount];
            for (int i = 0; i < Presenter.BackBufferCount; i++)
            {
                var frameResource = new FrameResource();
                FrameResources[i]          = frameResource;
                frameResource.RenderTarget = Presenter.BackBuffers[i].Resource;

                Device3D.CreateWrappedResource(
                    Presenter.BackBuffers[i].Resource,
                    new D3D11ResourceFlags()
                {
                    BindFlags = (int)BindFlags.RenderTarget
                },
                    (int)ResourceStates.RenderTarget,
                    (int)ResourceStates.Present,
                    Utilities.GetGuidFromType(typeof(Resource11)),
                    out frameResource.WrappedBackBuffer);

                frameResource.Surface = frameResource.WrappedBackBuffer.QueryInterface <Surface>();
                frameResource.Bitmap  = new Bitmap1(DeviceContext, frameResource.Surface, properties);
            }

            // https://msdn.microsoft.com/en-us/library/windows/desktop/dd370966(v=vs.85).aspx#resizing_a_dxgi_surface_render_target
            // https://msdn.microsoft.com/en-us/library/windows/desktop/bb205075(v=vs.85).aspx#Care_and_Feeding_of_the_Swap_Chain
            // https://msdn.microsoft.com/en-us/library/windows/desktop/bb205075(v=vs.85).aspx#Handling_Window_Resizing
        }
Esempio n. 3
0
 /// <summary>
 /// No documentation.
 /// </summary>
 /// <param name = "adapterRef">No documentation.</param>
 /// <param name = "minimumFeatureLevel">No documentation.</param>
 /// <param name = "riid">No documentation.</param>
 /// <param name = "deviceOut">No documentation.</param>
 /// <returns>No documentation.</returns>
 /// <unmanaged>HRESULT D3D12CreateDevice([In, Optional] IUnknown* pAdapter,[In] D3D_FEATURE_LEVEL MinimumFeatureLevel,[In] const GUID&amp; riid,[Out, Fast] void** ppDevice)</unmanaged>
 /// <unmanaged-short>D3D12CreateDevice</unmanaged-short>
 public static unsafe SharpDX.Result CreateDevice(SharpDX.IUnknown adapterRef, SharpDX.Direct3D.FeatureLevel minimumFeatureLevel, System.Guid riid, SharpDX.Direct3D12.Device deviceOut)
 {
     System.IntPtr  adapterRef_ = System.IntPtr.Zero;
     System.IntPtr  deviceOut_  = System.IntPtr.Zero;
     SharpDX.Result __result__;
     adapterRef_ = SharpDX.CppObject.ToCallbackPtr <SharpDX.IUnknown>(adapterRef);
     __result__  = D3D12CreateDevice_((void *)adapterRef_, unchecked ((System.Int32)minimumFeatureLevel), &riid, &deviceOut_);
     (deviceOut).NativePointer = deviceOut_;
     return(__result__);
 }
        /// <summary>
        /// Find the display mode that most closely matches the requested display mode.
        /// </summary>
        /// <param name="targetProfiles">The target profile, as available formats are different depending on the feature level..</param>
        /// <param name="mode">The mode.</param>
        /// <returns>Returns the closes display mode.</returns>
        /// <unmanaged>HRESULT IDXGIOutput::FindClosestMatchingMode([In] const DXGI_MODE_DESC* pModeToMatch,[Out] DXGI_MODE_DESC* pClosestMatch,[In, Optional] IUnknown* pConcernedDevice)</unmanaged>
        /// <remarks>Direct3D devices require UNORM formats. This method finds the closest matching available display mode to the mode specified in pModeToMatch. Similarly ranked fields (i.e. all specified, or all unspecified, etc) are resolved in the following order.  ScanlineOrdering Scaling Format Resolution RefreshRate  When determining the closest value for a particular field, previously matched fields are used to filter the display mode list choices, and  other fields are ignored. For example, when matching Resolution, the display mode list will have already been filtered by a certain ScanlineOrdering,  Scaling, and Format, while RefreshRate is ignored. This ordering doesn't define the absolute ordering for every usage scenario of FindClosestMatchingMode, because  the application can choose some values initially, effectively changing the order that fields are chosen. Fields of the display mode are matched one at a time, generally in a specified order. If a field is unspecified, FindClosestMatchingMode gravitates toward the values for the desktop related to this output.  If this output is not part of the desktop, then the default desktop output is used to find values. If an application uses a fully unspecified  display mode, FindClosestMatchingMode will typically return a display mode that matches the desktop settings for this output.   Unspecified fields are lower priority than specified fields and will be resolved later than specified fields.</remarks>
        public DisplayMode FindClosestMatchingDisplayMode(GraphicsProfile[] targetProfiles, DisplayMode mode)
        {
            if (targetProfiles == null) throw new ArgumentNullException("targetProfiles");

            ModeDescription closestDescription;
            SharpDX.Direct3D12.Device deviceTemp = null;
            for (int i = 0; i < targetProfiles.Length; i++)
            {
                // Create Device D3D12 with feature Level based on profile
                try
                {
                    deviceTemp = new SharpDX.Direct3D12.Device(Adapter.NativeAdapter, (FeatureLevel)targetProfiles[i]);
                    break;
                }
                catch (Exception)
                {
                }
            }

            if (deviceTemp == null)
                throw new InvalidOperationException("Could not create D3D12 graphics device");

            var description = new SharpDX.DXGI.ModeDescription()
            {
                Width = mode.Width,
                Height = mode.Height,
                RefreshRate = mode.RefreshRate.ToSharpDX(),
                Format = (SharpDX.DXGI.Format)mode.Format,
                Scaling = DisplayModeScaling.Unspecified,
                ScanlineOrdering = DisplayModeScanlineOrder.Unspecified
            };
            using (var device = deviceTemp)
                output.GetClosestMatchingMode(device, description, out closestDescription);

            return DisplayMode.FromDescription(closestDescription);
        }
Esempio n. 5
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;
        }
Esempio n. 6
0
 /// <summary>
 /// <p>Creates a device that represents the display adapter.</p>
 /// </summary>
 /// <param name="adapterRef"><dd>  <p> A reference to the video adapter to use when creating a device. Pass <strong><c>null</c></strong> to use the default adapter, which is the first adapter that is enumerated by <strong>IDXGIFactory1::EnumAdapters</strong>. </p> <strong>Note</strong>?? Do not mix the use of DXGI 1.0 (<strong><see cref="SharpDX.DXGI.Factory"/></strong>) and DXGI 1.1 (<strong><see cref="SharpDX.DXGI.Factory1"/></strong>) in an application. Use <strong><see cref="SharpDX.DXGI.Factory"/></strong> or <strong><see cref="SharpDX.DXGI.Factory1"/></strong>, but not both in an application.  ? </dd></param>
 /// <param name="minimumFeatureLevel"><dd>  <p> The <strong><see cref="SharpDX.Direct3D.DriverType"/></strong>, which represents the driver type to create. </p> </dd></param>
 /// <param name="riid"><dd>  <p> A handle to a DLL that implements a software rasterizer. If <em>DriverType</em> is <em><see cref="SharpDX.Direct3D.DriverType.Software"/></em>, <em>Software</em> must not be <strong><c>null</c></strong>. Get the handle by calling LoadLibrary, LoadLibraryEx , or GetModuleHandle. </p> </dd></param>
 /// <param name="deviceOut"><dd>  <p> Returns the address of a reference to an <strong><see cref="SharpDX.Direct3D11.Device"/></strong> object that represents the device created. If this parameter is <strong><c>null</c></strong>, no <see cref="SharpDX.Direct3D11.Device"/> will be returned. </p> </dd></param>
 /// <returns><p> This method can return one of the Direct3D 11 Return Codes. </p><p> This method returns E_INVALIDARG if you set the <em>pAdapter</em> parameter to a non-<strong><c>null</c></strong> value and the <em>DriverType</em> parameter to the <see cref="SharpDX.Direct3D.DriverType.Hardware"/> value. </p><p> This method returns <strong><see cref="SharpDX.DXGI.ResultCode.SdkComponentMissing"/></strong> if you specify <strong><see cref="SharpDX.Direct3D11.DeviceCreationFlags.Debug"/></strong> in <em>Flags</em> and the incorrect version of the debug layer is installed on your computer. Install the latest Windows SDK to get the correct version. </p></returns>
 /// <remarks>
 /// <p> This entry-point is supported by the Direct3D 11 runtime, which is available on Windows 7, Windows Server 2008 R2, and as an update to Windows Vista (KB971644). </p><p> To create a Direct3D 11.1 device (<strong><see cref="SharpDX.Direct3D11.Device1"/></strong>), which is available on Windows?8, Windows Server?2012, and Windows?7 and Windows Server?2008?R2 with the Platform Update for Windows 7 installed, you first create a <strong><see cref="SharpDX.Direct3D11.Device"/></strong> with this function, and then call the <strong>QueryInterface</strong> method on the <strong><see cref="SharpDX.Direct3D11.Device"/></strong> object to obtain the <strong><see cref="SharpDX.Direct3D11.Device1"/></strong> interface. </p><p> To create a Direct3D 11.2 device (<strong><see cref="SharpDX.Direct3D11.Device2"/></strong>), which is available on Windows?8.1 and Windows Server?2012?R2, you first create a <strong><see cref="SharpDX.Direct3D11.Device"/></strong> with this function, and then call the <strong>QueryInterface</strong> method on the <strong><see cref="SharpDX.Direct3D11.Device"/></strong> object to obtain the <strong><see cref="SharpDX.Direct3D11.Device2"/></strong> interface. </p><p> Set <em>ppDevice</em> and <em>ppImmediateContext</em> to <strong><c>null</c></strong> to determine which feature level is supported by looking at <em>pFeatureLevel</em> without creating a device. </p><p> For an example, see How To: Create a Device and Immediate Context; to create a device and a swap chain at the same time, use <strong>D3D11CreateDeviceAndSwapChain</strong>. </p><p> If you set the <em>pAdapter</em> parameter to a non-<strong><c>null</c></strong> value, you must also set the <em>DriverType</em> parameter to the <see cref="SharpDX.Direct3D.DriverType.Unknown"/> value. If you set the <em>pAdapter</em> parameter to a non-<strong><c>null</c></strong> value and the <em>DriverType</em> parameter to the <see cref="SharpDX.Direct3D.DriverType.Hardware"/> value, <strong><see cref="SharpDX.Direct3D11.D3D11.CreateDevice"/></strong> returns an <strong><see cref="SharpDX.Result"/></strong> of E_INVALIDARG. </p><table> <tr><td> <p>Differences between Direct3D 10 and Direct3D 11:</p> <p> In Direct3D 10, the presence of <em>pAdapter</em> dictated which adapter to use and the <em>DriverType</em> could mismatch what the adapter was. </p> <p> In Direct3D 11, if you are trying to create a hardware or a software device, set <em>pAdapter</em> != <strong><c>null</c></strong> which constrains the other inputs to be: </p> <ul> <li><em>DriverType</em> must be <see cref="SharpDX.Direct3D.DriverType.Unknown"/> </li> <li><em>Software</em> must be <strong><c>null</c></strong>. </li> </ul> <p> On the other hand, if <em>pAdapter</em> == <strong><c>null</c></strong>, the <em>DriverType</em> cannot be set to <see cref="SharpDX.Direct3D.DriverType.Unknown"/>; it can be set to either: </p> <ul> <li> If <em>DriverType</em> == <see cref="SharpDX.Direct3D.DriverType.Software"/>,  <em>Software</em> cannot be <strong><c>null</c></strong>. </li> <li> If <em>DriverType</em> == <see cref="SharpDX.Direct3D.DriverType.Hardware"/>, the adapter used will be the default adapter, which is the first adapter that is enumerated by <strong>IDXGIFactory1::EnumAdapters</strong> </li> </ul> </td></tr> </table><p>?</p><p> The function signature PFN_D3D11_CREATE_DEVICE is provided as a typedef, so that you can use dynamic linking techniques (GetProcAddress) instead of statically linking. </p><p><strong>Windows?Phone?8: </strong> This API is supported. </p><p><strong>Windows Phone 8.1: </strong> This API is supported. </p>
 /// </remarks>
 /// <include file='.\..\Documentation\CodeComments.xml' path="/comments/comment[@id='D3D12CreateDevice']/*"/>
 /// <msdn-id>ff476082</msdn-id>
 /// <unmanaged>HRESULT D3D12CreateDevice([In, Optional] IUnknown* pAdapter,[In] D3D_FEATURE_LEVEL MinimumFeatureLevel,[In] const GUID&amp; riid,[Out, Fast] ID3D12Device** ppDevice)</unmanaged>
 /// <unmanaged-short>D3D12CreateDevice</unmanaged-short>
 public static SharpDX.Result CreateDevice(SharpDX.ComObject adapterRef, SharpDX.Direct3D.FeatureLevel minimumFeatureLevel, System.Guid riid, SharpDX.Direct3D12.Device deviceOut)
 {
     unsafe {
         IntPtr         deviceOut_ = IntPtr.Zero;
         SharpDX.Result __result__;
         __result__ =
             D3D12CreateDevice_((void *)((adapterRef == null)?IntPtr.Zero:adapterRef.NativePointer), unchecked ((int)minimumFeatureLevel), &riid, &deviceOut_);
         ((SharpDX.Direct3D12.Device)deviceOut).NativePointer = deviceOut_;
         return(__result__);
     }
 }