Exemple #1
0
        // Get's a handle to assosiate with the BackBuffer and Prepares Devices
        private void Initialize(IntPtr outputHandle)
        {
            // SwapChain Description
            var desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(0, 0, new Rational(0, 0), Format.B8G8R8A8_UNorm),   // RBGA | BGRA 32-bit
                IsWindowed        = true,
                OutputHandle      = outputHandle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            // Create Device, SwapChain & BackBuffer
            Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, desc, out _device, out _swapChain);
            _backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0);

            // Creates Association between outputHandle and BackBuffer
            var factory = _swapChain.GetParent <Factory>();

            factory.MakeWindowAssociation(outputHandle, WindowAssociationFlags.IgnoreAll);

            // Video Device | Video Context
            videoDevice1  = _device.QueryInterface <VideoDevice1>();
            videoContext1 = _device.ImmediateContext.QueryInterface <VideoContext1>();

            // Creates Video Processor Enumerator
            vpcd = new VideoProcessorContentDescription()
            {
                Usage            = VideoUsage.PlaybackNormal,
                InputFrameFormat = VideoFrameFormat.Progressive,

                InputFrameRate  = new Rational(1, 1),
                OutputFrameRate = new Rational(1, 1),

                // We Set those later
                InputWidth   = 1,
                OutputWidth  = 1,
                InputHeight  = 1,
                OutputHeight = 1
            };
            videoDevice1.CreateVideoProcessorEnumerator(ref vpcd, out vpe);
            videoDevice1.CreateVideoProcessor(vpe, 0, out videoProcessor);

            // Prepares Video Processor Input View Description for Video Processor Input View that we pass Shared NV12 Texture (nv12SharedResource) each time
            vpivd = new VideoProcessorInputViewDescription()
            {
                FourCC    = 0,
                Dimension = VpivDimension.Texture2D,
                Texture2D = new Texture2DVpiv()
                {
                    MipSlice = 0, ArraySlice = 0
                }
            };

            // Creates Video Processor Output to our BackBuffer
            vpovd = new VideoProcessorOutputViewDescription()
            {
                Dimension = VpovDimension.Texture2D
            };
            videoDevice1.CreateVideoProcessorOutputView((Resource)_backBuffer, vpe, vpovd, out vpov);

            // Prepares Streams Array
            vpsa = new VideoProcessorStream[1];
        }
Exemple #2
0
        private void Initialize()
        {
            factory2d = new Factory2D(SharpDX.Direct2D1.FactoryType.MultiThreaded, DebugLevel.Information);

            HookControl         = Control.FromHandle(HookHandle);
            HookControl.Resize += HookResized;

            var desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(0, 0, new Rational(0, 0), Format.B8G8R8A8_UNorm),   // BGRA | Required for Direct2D/DirectWrite (<Win8)
                IsWindowed        = true,
                OutputHandle      = HookHandle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            /* [Enable Debug Layer]
             *
             * https://docs.microsoft.com/en-us/windows/win32/direct3d11/using-the-debug-layer-to-test-apps
             * To use this flag, you must have D3D11*SDKLayers.dll installed; otherwise, device creation fails. To get D3D11_1SDKLayers.dll, install the SDK for Windows 8.
             */

            // Enable on-demand to avoid "Failed to create device issue"
            //#if DEBUG
            //    Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.Debug | DeviceCreationFlags.BgraSupport, desc, out device, out swapChain);
            //#else
            Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, out device, out swapChain);
            //#endif

            var factory = swapChain.GetParent <FactoryDX>();

            factory.MakeWindowAssociation(HookHandle, WindowAssociationFlags.IgnoreAll);

            backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0);
            rtv        = new RenderTargetView(device, backBuffer);
            context    = device.ImmediateContext;

            factoryWrite = new FactoryDW();
            surface      = backBuffer.QueryInterface <Surface>();
            rtv2d        = new RenderTarget(factory2d, surface, new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
            brush2d      = new SolidColorBrush(rtv2d, Color.White);

            var VertexShaderByteCode = ShaderBytecode.Compile(Properties.Resources.VertexShader, "main", "vs_5_0", ShaderFlags.Debug);

            vertexLayout = new InputLayout(device, VertexShaderByteCode, new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 12, 0, InputClassification.PerVertexData, 0),
            });
            vertexShader = new VertexShader(device, VertexShaderByteCode);

            var PixelShaderByteCode = ShaderBytecode.Compile(Properties.Resources.PixelShader, "main", "ps_5_0", ShaderFlags.Debug);

            pixelShader = new PixelShader(device, PixelShaderByteCode);

            var PixelShaderByteCodeYUV = ShaderBytecode.Compile(Properties.Resources.PixelShader_YUV, "main", "ps_5_0", ShaderFlags.Debug);

            pixelShaderYUV = new PixelShader(device, PixelShaderByteCodeYUV);

            vertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
            {
                -1.0f, -1.0f, 0, 0.0f, 1.0f,
                -1.0f, 1.0f, 0, 0.0f, 0.0f,
                1.0f, -1.0f, 0, 1.0f, 1.0f,

                1.0f, -1.0f, 0, 1.0f, 1.0f,
                -1.0f, 1.0f, 0, 0.0f, 0.0f,
                1.0f, 1.0f, 0, 1.0f, 0.0f
            });

            SamplerState textureSampler = new SamplerState(device, new SamplerStateDescription()
            {
                AddressU           = TextureAddressMode.Clamp,
                AddressV           = TextureAddressMode.Clamp,
                AddressW           = TextureAddressMode.Clamp,
                ComparisonFunction = Comparison.Never,
                Filter             = Filter.MinMagMipLinear,
                MaximumAnisotropy  = 1,
                MaximumLod         = float.MaxValue,
                MinimumLod         = 0,
                MipLodBias         = 0.0f
            });

            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);

            textureRGB = new Texture2D(device, new Texture2DDescription()
            {
                Usage  = ResourceUsage.Default,
                Format = Format.R8G8B8A8_UNorm,

                Width  = HookControl.Width,
                Height = HookControl.Height,

                BindFlags      = BindFlags.ShaderResource | BindFlags.RenderTarget,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags    = ResourceOptionFlags.None,

                SampleDescription = new SampleDescription(1, 0),
                ArraySize         = 1,
                MipLevels         = 1
            });

            srvDescYUV           = new ShaderResourceViewDescription();
            srvDescYUV.Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D;
            srvDescYUV.Format    = Format.R8_UNorm;
            srvDescYUV.Texture2D.MostDetailedMip = 0;
            srvDescYUV.Texture2D.MipLevels       = 1;

            videoDevice1  = device.QueryInterface <VideoDevice1>();
            videoContext1 = device.ImmediateContext.QueryInterface <VideoContext1>();

            vpcd = new VideoProcessorContentDescription()
            {
                Usage            = VideoUsage.PlaybackNormal,
                InputFrameFormat = VideoFrameFormat.Progressive,
                InputFrameRate   = new Rational(1, 1),
                OutputFrameRate  = new Rational(1, 1),
                InputWidth       = 1,
                OutputWidth      = 1,
                InputHeight      = 1,
                OutputHeight     = 1
            };
            videoDevice1.CreateVideoProcessorEnumerator(ref vpcd, out vpe);
            videoDevice1.CreateVideoProcessor(vpe, 0, out videoProcessor);

            vpivd = new VideoProcessorInputViewDescription()
            {
                FourCC    = 0,
                Dimension = VpivDimension.Texture2D,
                Texture2D = new Texture2DVpiv()
                {
                    MipSlice = 0, ArraySlice = 0
                }
            };
            vpovd = new VideoProcessorOutputViewDescription()
            {
                Dimension = VpovDimension.Texture2D
            };
            vpsa = new VideoProcessorStream[1];

            SetViewport();

            //foreach (var osdsurf in osd)
            //osdsurf.Value.Init();
        }