Пример #1
0
        public static bool Initialise(long adapterLuid)
        {
            // Find the adapter matching the luid from the parent process
            var factory = new DXGI.Factory1();

            DXGI.Adapter gameAdapter = null;
            foreach (var adapter in factory.Adapters)
            {
                if (adapter.Description.Luid == adapterLuid)
                {
                    gameAdapter = adapter;
                    break;
                }
            }
            if (gameAdapter == null)
            {
                var foundLuids = string.Join(",", factory.Adapters.Select(adapter => adapter.Description.Luid));
                Console.Error.WriteLine($"FATAL: Could not find adapter matching game adapter LUID {adapterLuid}. Found: {foundLuids}.");
                return(false);
            }

            // Use the adapter to build the device we'll use
            var flags = D3D11.DeviceCreationFlags.BgraSupport;

#if DEBUG
            flags |= D3D11.DeviceCreationFlags.Debug;
#endif

            Device = new D3D11.Device(gameAdapter, flags);

            return(true);
        }
Пример #2
0
        public Device(ref Descriptor desc, FeatureLevel featureLevel = FeatureLevel.Level_12_0) : base(ref desc)
        {
            if (desc.DebugDevice)
            {
                var debugInterface = SharpDX.Direct3D12.DebugInterface.Get();
                if (debugInterface != null)
                {
                    debugInterface.EnableDebugLayer();
                }
                else
                {
                    Log.Error("Failed to obtain DX12 debug layer.");
                }
            }

            // Use first adapter that is supported.
            using (SharpDX.DXGI.Factory dxgiFactory = new Factory4())
            {
                for (int adapterIndex = 0;; ++adapterIndex)
                {
                    var adapter = dxgiFactory.GetAdapter(adapterIndex);
                    if (adapter == null)
                    {
                        // TODO: Throw exception
                        return;
                    }

                    try
                    {
                        DeviceD3D12 = new SharpDX.Direct3D12.Device(adapter, (SharpDX.Direct3D.FeatureLevel)featureLevel);
                        Adapter     = adapter;
                        break;
                    }

                    catch (Exception)
                    {
                        DeviceD3D12 = null;
                        adapter.Dispose();
                    }
                }
            }

            // Get Resource handle increment sizes.
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.Sampler]        = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.Sampler);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.DepthStencil]   = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.DepthStencilView);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.RenderTarget]   = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
            descriptorHandleIncrementSize[(int)DescriptorHeap.Descriptor.ResourceDescriptorType.ShaderResource] = DeviceD3D12.GetDescriptorHandleIncrementSize(DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView);

            Memory.Enums.InitLookupTables();

            Log.Info("Successfully created a DX12 device with adapter \"{0}\"", Adapter.Description.Description);
        }
        public RenderController(IntPtr windowHandle)
        {
            writeFactory = new SharpDX.DirectWrite.Factory();

            SharpDX.Direct3D11.Device defaultDevice = new SharpDX.Direct3D11.Device
                                                      (
                DriverType.Hardware,
                DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport
                                                      );

            device = defaultDevice.QueryInterface <SharpDX.Direct3D11.Device1>();
            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            SwapChainDescription1 description = new SwapChainDescription1()
            {
                Width             = 0,
                Height            = 0,
                Format            = Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = Scaling.None,
                SwapEffect        = SwapEffect.FlipSequential,
            };

            swapChain  = new SwapChain1(dxgiFactory2, device, windowHandle, ref description);
            backBuffer = Surface.FromSwapChain(swapChain, 0);

            d2dDevice  = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
            properties = new BitmapProperties1
                         (
                new SharpDX.Direct2D1.PixelFormat
                (
                    SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                    SharpDX.Direct2D1.AlphaMode.Premultiplied
                ),
                0, 0, BitmapOptions.Target | BitmapOptions.CannotDraw
                         );
            d2dTarget         = new Bitmap1(d2dContext, backBuffer, properties);
            d2dContext.Target = d2dTarget;

            canDraw = true;
        }
Пример #4
0
    private static Device CreateDevice()
    {
        SharpDX.DXGI.Adapter chosenAdapter = null;

        using (var dxgiFactory = new SharpDX.DXGI.Factory1()) {
            var adapters = dxgiFactory.Adapters;
            try {
                ulong adapterLuid = OpenVR.System.GetOutputDevice(ETextureType.DirectX, IntPtr.Zero);
                if (adapterLuid != 0)
                {
                    foreach (var adapter in adapters)
                    {
                        if ((ulong)adapter.Description.Luid == adapterLuid)
                        {
                            chosenAdapter = adapter;
                        }
                    }
                }

                if (chosenAdapter == null)
                {
                    //fallback to the default adapter
                    chosenAdapter = adapters[0];
                }

                var device = new Device(chosenAdapter, debugDevice ? DeviceCreationFlags.Debug : DeviceCreationFlags.None);
                return(device);
            }
            finally {
                foreach (var adapter in adapters)
                {
                    adapter.Dispose();
                }
            }
        }
    }
Пример #5
0
        void InitText(SwapChain3 tempSwapChain)
        {
            init       = true;
            device     = tempSwapChain.GetDevice <SharpDX.Direct3D11.Device1>();
            d3dContext = device.ImmediateContext.QueryInterface <SharpDX.Direct3D11.DeviceContext1>();
            var texture2d = tempSwapChain.GetBackBuffer <Texture2D>(0);

            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            SharpDX.Direct2D1.BitmapProperties1 properties = new SharpDX.Direct2D1.BitmapProperties1(
                new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm,
                                                  SharpDX.Direct2D1.AlphaMode.Premultiplied),
                96, 96, SharpDX.Direct2D1.BitmapOptions.Target | SharpDX.Direct2D1.BitmapOptions.CannotDraw);

            Surface backBuffer = tempSwapChain.GetBackBuffer <Surface>(0);

            d2dTarget = new SharpDX.Direct2D1.Bitmap1(d2dContext, new Size2(800, 600), properties);

            solidBrush = new SharpDX.Direct2D1.SolidColorBrush(d2dContext, Color.Coral);

            // Create a linear gradient brush.
            // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to,
            // NOT the geometry we will apply the brush.
            linearGradientBrush = new SharpDX.Direct2D1.LinearGradientBrush(d2dContext, new SharpDX.Direct2D1.LinearGradientBrushProperties()
            {
                StartPoint = new Vector2(50, 0),
                EndPoint   = new Vector2(450, 0),
            },
                                                                            new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[]
            {
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Blue,
                    Position = 0,
                },
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Green,
                    Position = 1,
                }
            }));

            SharpDX.Direct2D1.RadialGradientBrushProperties rgb = new SharpDX.Direct2D1.RadialGradientBrushProperties()
            {
                Center  = new Vector2(250, 525),
                RadiusX = 100,
                RadiusY = 100,
            };
            // Create a radial gradient brush.
            // The center is specified in absolute coordinates, too.
            radialGradientBrush = new SharpDX.Direct2D1.RadialGradientBrush(d2dContext, ref rgb
                                                                            ,
                                                                            new SharpDX.Direct2D1.GradientStopCollection(d2dContext, new SharpDX.Direct2D1.GradientStop[]
            {
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Yellow,
                    Position = 0,
                },
                new SharpDX.Direct2D1.GradientStop()
                {
                    Color    = Color.Red,
                    Position = 1,
                }
            }));
        }
Пример #6
0
        /// <summary>
        /// Creates Direct3D11 Device, RenderTargetView, DepthStencilView, Viewport
        /// </summary>
        /// <param name="deviceDescription">The device description.</param>
        /// <exception cref="System.Exception"></exception>
        /// <exception cref="System.ComponentModel.InvalidEnumArgumentException">deviceDescription.MultiSampleCount</exception>
        private void Initialize(DeviceDescription deviceDescription)
        {
            FeatureLevel[] levels = new FeatureLevel[] {
                FeatureLevel.Level_10_0,
                FeatureLevel.Level_10_1,
                FeatureLevel.Level_11_0,
                FeatureLevel.Level_11_1,
            };

            d3dDevice = new D3D11.Device(DriverType.Hardware, D3D11.DeviceCreationFlags.Debug, levels);

            DXGI.ModeDescription backBufferDesc = new DXGI.ModeDescription()
            {
                Width            = width,
                Height           = height,
                Format           = BackBufferFormat,
                RefreshRate      = new DXGI.Rational(60, 1),
                Scaling          = DXGI.DisplayModeScaling.Unspecified,
                ScanlineOrdering = DXGI.DisplayModeScanlineOrder.Progressive,
            };

            DXGI.SwapChainDescription swapChainDesc = new DXGI.SwapChainDescription()
            {
                BufferCount     = 1,
                Flags           = DXGI.SwapChainFlags.None,
                IsWindowed      = !deviceDescription.Fullcreen,
                ModeDescription = backBufferDesc,
                OutputHandle    = renderControl.Handle,
                SwapEffect      = deviceDescription.SwapEffect,
                Usage           = DXGI.Usage.RenderTargetOutput,
            };

            switch (deviceDescription.MultiSampleCount)
            {
            case MultiSampleType.MSAA1:
                swapChainDesc.SampleDescription = new DXGI.SampleDescription(1, deviceDescription.MultiSampleQuality);
                break;

            case MultiSampleType.MSAA2:
                swapChainDesc.SampleDescription = new DXGI.SampleDescription(2, deviceDescription.MultiSampleQuality);
                break;

            case MultiSampleType.MSAA4:
                swapChainDesc.SampleDescription = new DXGI.SampleDescription(4, deviceDescription.MultiSampleQuality);
                break;

            case MultiSampleType.MSAA8:
                swapChainDesc.SampleDescription = new DXGI.SampleDescription(8, deviceDescription.MultiSampleQuality);
                break;

            case MultiSampleType.MSAA16:
                swapChainDesc.SampleDescription = new DXGI.SampleDescription(16, deviceDescription.MultiSampleQuality);
                break;

            case MultiSampleType.Unknown:
                var samples = SharpDXDevice.CheckMultiSample();
                if (samples == null)
                {
                    throw new Exception(MethodBase.GetCurrentMethod().Name +
                                        " Because the MultiSampleCount parameter is [Unknown], the device could not determine the parameter automatically");
                }
                swapChainDesc.SampleDescription = samples.Last();
                break;

            default:
                throw new System.ComponentModel.InvalidEnumArgumentException("deviceDescription.MultiSampleCount",
                                                                             (int)deviceDescription.MultiSampleCount, typeof(MultiSampleType));
            }

            DXGI.Device  device  = d3dDevice.QueryInterface <DXGI.Device>();
            DXGI.Adapter adapter = device.GetParent <DXGI.Adapter>();
            DXGI.Factory factory = adapter.GetParent <DXGI.Factory>();

            swapChain = new DXGI.SwapChain(factory, d3dDevice, swapChainDesc);

            d3dContext = d3dDevice.ImmediateContext;

            D3D11.RasterizerStateDescription rasterDesc = new D3D11.RasterizerStateDescription()
            {
                CullMode = D3D11.CullMode.Back,
                FillMode = D3D11.FillMode.Solid,
                IsAntialiasedLineEnabled = true,
                IsMultisampleEnabled     = true,
                IsDepthClipEnabled       = true,
            };

            viewport = new Viewport(0, 0, width, height);

            this.SetRasterizerState(rasterDesc);
            d3dRenderTarget = CreateRenderTarget();
            d3dDepthStencil = CreateDepthStencil();
        }
Пример #7
0
        public SharpRender(RenderForm form)
        {
            this.form = form;
            var adapters = new DXGI.Factory1().Adapters;

            DXGI.Adapter myadapter = null;
            for (int i = 0; i < adapters.Length; ++i)
            {
                Logger.Log(string.Format("Adapter Found: [{0}] " +
                                         "{1}\tDeviceId={5}" +
                                         "{1}\tLuid={6}" +
                                         "{1}\tVendorId={10}" +
                                         "{1}\tSubsystemId={9}" +
                                         "{1}\tDescription={4}" +
                                         "{1}\tRevision={7}" +
                                         "{1}\tDedicatedSystemMemory={2}" +
                                         "{1}\tDedicatedVideoMemory={3}" +
                                         "{1}\tSharedSystemMemory={8}" +
                                         "",
                                         i, Environment.NewLine,
                                         adapters[i].Description.DedicatedSystemMemory,
                                         adapters[i].Description.DedicatedVideoMemory, adapters[i].Description.Description,
                                         adapters[i].Description.DeviceId, adapters[i].Description.Luid,
                                         adapters[i].Description.Revision, adapters[i].Description.SharedSystemMemory,
                                         adapters[i].Description.SubsystemId, adapters[i].Description.VendorId));
                var outputs = adapters[i].Outputs;
                for (int j = 0; j < outputs.Length; ++j)
                {
                    Logger.Log(string.Format("Output Found: [{0},{1}]" +
                                             "{2}\tDeviceName={4}" +
                                             "{2}\tIsAttachedToDesktop={5}" +
                                             "{2}\tMonitorHandle={6}" +
                                             "{2}\tDesktopBounds={3}" +
                                             "{2}\tRotation={7}" +
                                             "",
                                             i, j, Environment.NewLine,
                                             (Rectangle)outputs[j].Description.DesktopBounds,
                                             outputs[j].Description.DeviceName,
                                             outputs[j].Description.IsAttachedToDesktop,
                                             outputs[j].Description.MonitorHandle,
                                             outputs[j].Description.Rotation));
                }
                if (outputs.Length > 0 && myadapter == null)
                {
                    myadapter = adapters[i];
                }
            }
            d3device = new Direct3D11.Device(
                myadapter,
                Direct3D11.DeviceCreationFlags.BgraSupport);
            //SharpDX.Direct3D.DriverType.Hardware,
            //Direct3D11.DeviceCreationFlags.BgraSupport |
            //Direct3D11.DeviceCreationFlags.Debug);
            defDevice    = d3device.QueryInterface <Direct3D11.Device1>();
            dxgiDevice2  = defDevice.QueryInterface <DXGI.Device2>();
            dxgiAdapter  = dxgiDevice2.Adapter;
            dxgiFactory2 = dxgiAdapter.GetParent <DXGI.Factory2>();
            var scDescription = new DXGI.SwapChainDescription1()
            {
                Width             = 0,
                Height            = 0,
                Format            = DXGI.Format.B8G8R8A8_UNorm,
                Stereo            = false,
                SampleDescription = new DXGI.SampleDescription(1, 0),
                Usage             = DXGI.Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = DXGI.Scaling.None,
                SwapEffect        = DXGI.SwapEffect.FlipSequential
            };

            swapChain = new DXGI.SwapChain1(dxgiFactory2, defDevice, form.Handle,
                                            ref scDescription, null, null);
            d2dDevice  = new Direct2D1.Device(dxgiDevice2);
            d2dContext = new Direct2D1.DeviceContext(d2dDevice,
                                                     Direct2D1.DeviceContextOptions.None);
            fac = new Direct2D1.Factory(Direct2D1.FactoryType.SingleThreaded);
            var dpi          = fac.DesktopDpi;
            var bMProperties = new Direct2D1.BitmapProperties1(
                new Direct2D1.PixelFormat(DXGI.Format.B8G8R8A8_UNorm,
                                          Direct2D1.AlphaMode.Premultiplied),
                dpi.Width, dpi.Height,
                Direct2D1.BitmapOptions.CannotDraw | Direct2D1.BitmapOptions.Target);

            bb                = swapChain.GetBackBuffer <DXGI.Surface>(0);
            target            = new Direct2D1.Bitmap1(d2dContext, bb, bMProperties);
            d2dContext.Target = target;
            wrFactory         = new DirectWrite.Factory();

            brush = new Direct2D1.SolidColorBrush(d2dContext, c(Color.White));
        }
Пример #8
0
        /// <summary>
        /// Now that we have a CoreWindow object, the DirectX device/context can be created.
        /// </summary>
        /// <param name="entryPoint"></param>
        public async void Load(string entryPoint)
        {
            // Get the default hardware device and enable debugging. Don't care about the available feature level.
            // 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.
            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            // Description for our swap chain settings.
            SwapChainDescription1 description = new SwapChainDescription1()
            {
                // 0 means to use automatic buffer sizing.
                Width  = 0,
                Height = 0,
                // 32 bit RGBA color.
                Format = Format.B8G8R8A8_UNorm,
                // No stereo (3D) display.
                Stereo = false,
                // No multisampling.
                SampleDescription = new SampleDescription(1, 0),
                // Use the swap chain as a render target.
                Usage = Usage.RenderTargetOutput,
                // Enable double buffering to prevent flickering.
                BufferCount = 2,
                // No scaling.
                Scaling = Scaling.None,
                // Flip between both buffers.
                SwapEffect = SwapEffect.FlipSequential,
            };

            // Generate a swap chain for our window based on the specified description.
            swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(device, new ComObject(window), ref description, null);

            // Get the default Direct2D device and create a context.
            SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            // Specify the properties for the bitmap that we will use as the target of our Direct2D operations.
            // We want a 32-bit BGRA surface with premultiplied alpha.
            BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                                                                 DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw);

            // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target.
            Surface backBuffer = swapChain.GetBackBuffer <Surface>(0);

            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);

            // Create the DirectWrite factory objet.
            SharpDX.DirectWrite.Factory fontFactory = new SharpDX.DirectWrite.Factory();

            // Create a TextFormat object that will use the Segoe UI font with a size of 24 DIPs.
            textFormat = new TextFormat(fontFactory, "Segoe UI", 24.0f);

            // Create two TextLayout objects for rendering the moving text.
            textLayout1 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with snapped pixel boundaries.", textFormat, 400.0f, 200.0f);
            textLayout2 = new TextLayout(fontFactory, "This is an example of a moving TextLayout object with no snapped pixel boundaries.", textFormat, 400.0f, 200.0f);

            // Vertical offset for the moving text.
            layoutY = 0.0f;

            // Create the brushes for the text background and text color.
            backgroundBrush = new SolidColorBrush(d2dContext, Color.White);
            textBrush       = new SolidColorBrush(d2dContext, Color.Black);
        }
Пример #9
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) && !SdkLayersAvailable)
            {
                flags &= ~DeviceCreationFlags.Debug;
            }

            try
            {
                if (options.AdapterPtr != IntPtr.Zero)
                {
                    _dxgiAdapter = new Adapter(options.AdapterPtr);
                    _device      = new SharpDX.Direct3D11.Device(_dxgiAdapter,
                                                                 flags,
                                                                 SharpDX.Direct3D.FeatureLevel.Level_11_1);
                }
                else
                {
                    _device = new SharpDX.Direct3D11.Device(
                        SharpDX.Direct3D.DriverType.Hardware,
                        flags,
                        SharpDX.Direct3D.FeatureLevel.Level_11_1);
                }
            }
            catch (SharpDXException)
            {
                _device = new SharpDX.Direct3D11.Device(SharpDX.Direct3D.DriverType.Hardware, flags);
            }

            using (var dxgiDevice = _device.QueryInterface <SharpDX.DXGI.Device>())
            {
                // 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 >= SharpDX.Direct3D.FeatureLevel.Level_11_1,
                bufferRangeBinding: _device.FeatureLevel >= SharpDX.Direct3D.FeatureLevel.Level_11_1);

            _d3d11ResourceFactory = new D3D11ResourceFactory(this);
            _d3d11Info            = new BackendInfoD3D11(this);

            PostDeviceCreated();
        }
Пример #10
0
        /// <summary>
        /// Now that we have a CoreWindow object, the DirectX device/context can be created.
        /// </summary>
        /// <param name="entryPoint"></param>
        public void Load(string entryPoint)
        {
            // Get the default hardware device and enable debugging. Don't care about the available feature level.
            // 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.
            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            // Description for our swap chain settings.
            SwapChainDescription1 description = new SwapChainDescription1()
            {
                // 0 means to use automatic buffer sizing.
                Width  = 0,
                Height = 0,
                // 32 bit RGBA color.
                Format = Format.B8G8R8A8_UNorm,
                // No stereo (3D) display.
                Stereo = false,
                // No multisampling.
                SampleDescription = new SampleDescription(1, 0),
                // Use the swap chain as a render target.
                Usage = Usage.RenderTargetOutput,
                // Enable double buffering to prevent flickering.
                BufferCount = 2,
                // No scaling.
                Scaling = Scaling.None,
                // Flip between both buffers.
                SwapEffect = SwapEffect.FlipSequential,
            };

            // Generate a swap chain for our window based on the specified description.
            swapChain = dxgiFactory2.CreateSwapChainForCoreWindow(device, new ComObject(window), ref description, null);

            // Get the default Direct2D device and create a context.
            SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            // Specify the properties for the bitmap that we will use as the target of our Direct2D operations.
            // We want a 32-bit BGRA surface with premultiplied alpha.
            BitmapProperties1 properties = new BitmapProperties1(new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                                                                 DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw);

            // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target.
            Surface backBuffer = swapChain.GetBackBuffer <Surface>(0);

            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);

            // Create a solid color brush.
            solidBrush = new SolidColorBrush(d2dContext, Color.Coral);

            // Create a linear gradient brush.
            // Note that the StartPoint and EndPoint values are set as absolute coordinates of the surface you are drawing to,
            // NOT the geometry we will apply the brush.
            linearGradientBrush = new LinearGradientBrush(d2dContext, new LinearGradientBrushProperties()
            {
                StartPoint = new Vector2(50, 0),
                EndPoint   = new Vector2(450, 0),
            },
                                                          new GradientStopCollection(d2dContext, new GradientStop[]
            {
                new GradientStop()
                {
                    Color    = Color.Blue,
                    Position = 0,
                },
                new GradientStop()
                {
                    Color    = Color.Green,
                    Position = 1,
                }
            }));

            // Create a radial gradient brush.
            // The center is specified in absolute coordinates, too.
            radialGradientBrush = new RadialGradientBrush(d2dContext, new RadialGradientBrushProperties()
            {
                Center  = new Vector2(250, 525),
                RadiusX = 100,
                RadiusY = 100,
            },
                                                          new GradientStopCollection(d2dContext, new GradientStop[]
            {
                new GradientStop()
                {
                    Color    = Color.Yellow,
                    Position = 0,
                },
                new GradientStop()
                {
                    Color    = Color.Red,
                    Position = 1,
                }
            }));
        }
Пример #11
0
        /// <summary>
        /// Now that we have a CoreWindow object, the DirectX device/context can be created.
        /// </summary>
        /// <param name="entryPoint"></param>
        public void Load(string entryPoint)
        {
            // Get the default hardware device and enable debugging. Don't care about the available feature level.
            // 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.
            SharpDX.DXGI.Device2  dxgiDevice2  = device.QueryInterface <SharpDX.DXGI.Device2>();
            SharpDX.DXGI.Adapter  dxgiAdapter  = dxgiDevice2.Adapter;
            SharpDX.DXGI.Factory2 dxgiFactory2 = dxgiAdapter.GetParent <SharpDX.DXGI.Factory2>();

            // Description for our swap chain settings.
            SwapChainDescription1 description = new SwapChainDescription1()
            {
                // 0 means to use automatic buffer sizing.
                Width  = 0,
                Height = 0,
                // 32 bit RGBA color.
                Format = Format.B8G8R8A8_UNorm,
                // No stereo (3D) display.
                Stereo = false,
                // No multisampling.
                SampleDescription = new SampleDescription(1, 0),
                // Use the swap chain as a render target.
                Usage = Usage.RenderTargetOutput,
                // Enable double buffering to prevent flickering.
                BufferCount = 2,
                // No scaling.
                Scaling = Scaling.None,
                // Flip between both buffers.
                SwapEffect = SwapEffect.FlipSequential,
            };

            // Generate a swap chain for our window based on the specified description.
            swapChain = new SwapChain1(dxgiFactory2, device, new ComObject(window), ref description);

            // Get the default Direct2D device and create a context.
            SharpDX.Direct2D1.Device d2dDevice = new SharpDX.Direct2D1.Device(dxgiDevice2);
            d2dContext = new SharpDX.Direct2D1.DeviceContext(d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);

            // Specify the properties for the bitmap that we will use as the target of our Direct2D operations.
            // We want a 32-bit BGRA surface with premultiplied alpha.
            BitmapProperties1 properties = new BitmapProperties1(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                                                                 DisplayProperties.LogicalDpi, DisplayProperties.LogicalDpi, BitmapOptions.Target | BitmapOptions.CannotDraw);

            // Get the default surface as a backbuffer and create the Bitmap1 that will hold the Direct2D drawing target.
            Surface backBuffer = swapChain.GetBackBuffer <Surface>(0);

            d2dTarget = new Bitmap1(d2dContext, backBuffer, properties);

            playerBitmap  = this.LoadBitmapFromContentFile("/Assets/Bitmaps/player.png");
            terrainBitmap = this.LoadBitmapFromContentFile("/Assets/Bitmaps/terrain.png");
            terrainBrush  = new BitmapBrush1(d2dContext, terrainBitmap, new BitmapBrushProperties1()
            {
                ExtendModeX = ExtendMode.Wrap,
                ExtendModeY = ExtendMode.Wrap,
            });
        }