Ejemplo n.º 1
0
        public static void Fill(Renderer renderer, SharpDX.DXGI.Adapter adapter)
        {
            RendererInfo ri = new RendererInfo();

            ri.AdapterDesc  = adapter.Description.Description;
            ri.SystemMemory = (UInt64)((IntPtr)adapter.Description.DedicatedSystemMemory).ToPointer();
            ri.VideoMemory  = (UInt64)((IntPtr)adapter.Description.DedicatedVideoMemory).ToPointer();
            ri.SharedMemory = (UInt64)((IntPtr)adapter.Description.SharedSystemMemory).ToPointer();
            ri.Outputs      = adapter.Outputs.Length;

            SharpDX.DXGI.Output output = adapter.Outputs[0];
            var bounds = output.Description.DesktopBounds;

            ri.OutputName   = output.Description.DeviceName;
            ri.ScreenBounds = new System.Drawing.Rectangle(new System.Drawing.Point(bounds.Top, bounds.Left), new System.Drawing.Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top));
            ri.ScreenWidth  = output.Description.DesktopBounds.Right - output.Description.DesktopBounds.Left;
            ri.ScreenHeight = output.Description.DesktopBounds.Bottom - output.Description.DesktopBounds.Top;

            renderer.Info = ri;
        }
Ejemplo n.º 2
0
        public Renderer(Player player)
        {
            this.player = player;
            this.player.Control.Resize += ResizeBuffers;

            /* [Enable Debug Layer]
             *
             * 1) Enable native code debugging in your main project properties
             * 2) Requires SDK
             *
             * https://docs.microsoft.com/en-us/windows/win32/direct3d11/overviews-direct3d-11-devices-layers
             * https://docs.microsoft.com/en-us/windows/win32/direct3d11/using-the-debug-layer-to-test-apps
             *
             * For Windows 7 with Platform Update for Windows 7 (KB2670838) or Windows 8.x, to create a device that supports the debug layer, install the Windows Software Development Kit (SDK) for Windows 8.x to get D3D11_1SDKLayers.dll
             * For Windows 10, to create a device that supports the debug layer, enable the "Graphics Tools" optional feature. Go to the Settings panel, under System, Apps & features, Manage optional Features, Add a feature, and then look for "Graphics Tools".
             */

            //device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.VideoSupport | DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug, ((SharpDX.Direct3D.FeatureLevel[]) Enum.GetValues(typeof(SharpDX.Direct3D.FeatureLevel))).Reverse().ToArray() );
            //deviceDbg = new DeviceDebug(device); // To Report Live Objects if required
            device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.VideoSupport | DeviceCreationFlags.BgraSupport, ((SharpDX.Direct3D.FeatureLevel[])Enum.GetValues(typeof(SharpDX.Direct3D.FeatureLevel))).Reverse().ToArray());
            using (var mthread = device.QueryInterface <Multithread>()) mthread.SetMultithreadProtected(true);

            using (var device2 = device.QueryInterface <SharpDX.DXGI.Device2>())
                using (var adapter = device2.Adapter)
                    using (var factory = adapter.GetParent <Factory2>())
                    {
                        device2.MaximumFrameLatency = 1; // Dont queue more than 1 frame

                        RendererInfo.Fill(this, adapter);
                        Log("\r\n" + Info.ToString());

                        // Swap Chain (TODO: Backwards compatibility)
                        var desc1 = new SwapChainDescription1()
                        {
                            BufferCount = device.FeatureLevel >= SharpDX.Direct3D.FeatureLevel.Level_11_0 ? 6 : 1, // Should be 1 for Win < 8 | HDR 60 fps requires 6 for non drops
                            SwapEffect  = device.FeatureLevel >= SharpDX.Direct3D.FeatureLevel.Level_12_0 ? SwapEffect.FlipSequential : SwapEffect.FlipDiscard,
                            //Format      = HDREnabled ? Format.R10G10B10A2_UNorm : Format.B8G8R8A8_UNorm, // Create always 10 bit and fallback to 8?
                            Format = Format.B8G8R8A8_UNorm,
                            //Format      = Format.R16G16B16A16_Float,
                            //Format      = Format.R10G10B10A2_UNorm,
                            Width     = player.Control.Width,
                            Height    = player.Control.Height,
                            AlphaMode = AlphaMode.Ignore,
                            Usage     = Usage.RenderTargetOutput,
                            Scaling   = Scaling.None,
                            //Flags = SwapChainFlags.AllowModeSwitch,
                            //Flags = 0 (or if already in fullscreen while recreating -> SwapChainFlags.AllowModeSwitch)
                            SampleDescription = new SampleDescription()
                            {
                                Count   = 1,
                                Quality = 0
                            }
                        };

                        swapChain = new SwapChain1(factory, device, this.player.Control.Handle, ref desc1);
                    }

            backBuffer   = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            rtv          = new RenderTargetView(device, backBuffer);
            context      = device.ImmediateContext;
            vertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, vertexBufferData);

            SamplerState textureSampler = new SamplerState(device, new SamplerStateDescription()
            {
                AddressU           = TextureAddressMode.Clamp,
                AddressV           = TextureAddressMode.Clamp,
                AddressW           = TextureAddressMode.Clamp,
                ComparisonFunction = Comparison.Never,
                Filter             = Filter.MinMagMipLinear,
                MaximumLod         = float.MaxValue
            });

            // Load Shaders Byte Code
            Dictionary <string, byte[]> Shaders = Shaders_v5.Shaders;

            if (device.FeatureLevel < SharpDX.Direct3D.FeatureLevel.Level_11_0)
            {
                Shaders = Shaders_v4.Shaders;
            }

            pixelShaders = new Dictionary <string, PixelShader>();
            foreach (var entry in Shaders)
            {
                if (entry.Key.ToString() == "VertexShader")
                {
                    vertexLayout = new InputLayout(device, entry.Value, inputElements);
                    vertexShader = new VertexShader(device, entry.Value);
                }
                else
                {
                    pixelShaders.Add(entry.Key.ToString(), new PixelShader(device, entry.Value));
                }
            }

            context.InputAssembler.InputLayout       = vertexLayout;
            context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <float>() * 5, 0));

            context.VertexShader.Set(vertexShader);
            context.PixelShader.SetSampler(0, textureSampler);

            if (player.Status == Status.Stopped)
            {
                GetViewport = new Viewport(0, 0, player.Control.Width, player.Control.Height);
                context.Rasterizer.SetViewport(0, 0, player.Control.Width, player.Control.Height);
            }
            else
            {
                SetViewport();
            }
        }