Example #1
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/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".
             *
             */

            // 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 PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
            brush2d        = new SolidColorBrush(rtv2d, Color.White);
            brush2dOutline = new SolidColorBrush(rtv2d, Color.Gray);

            outlineRenderer.renderer = this;

            string vertexProfile = "vs_5_0";
            string pixelProfile  = "ps_5_0";

            if (device.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_9_1 ||
                device.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_9_2 ||
                device.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_9_3 ||
                device.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_10_0 ||
                device.FeatureLevel == SharpDX.Direct3D.FeatureLevel.Level_10_1)
            {
                vertexProfile = "vs_4_0_level_9_1";
                pixelProfile  = "ps_4_0_level_9_1";
            }

            var VertexShaderByteCode = ShaderBytecode.Compile(Properties.Resources.VertexShader, "main", vertexProfile, 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", pixelProfile, ShaderFlags.Debug);

            pixelShader = new PixelShader(device, PixelShaderByteCode);

            var PixelShaderByteCodeYUV = ShaderBytecode.Compile(Properties.Resources.PixelShader_YUV, "main", pixelProfile, 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;

            // Falling back from videoDevice1/videoContext1 to videoDevice/videoContext to ensure backwards compatibility
            videoDevice1  = device.QueryInterfaceOrNull <VideoDevice>();
            videoContext1 = device.ImmediateContext.QueryInterfaceOrNull <VideoContext>();

            if (videoDevice1 == null || videoContext1 == null)
            {
                SetViewport(); return;
            }

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