Exemple #1
0
        /// <summary>
        /// Creates a default D3D11 Texture with forced Shared-Flag
        /// </summary>
        /// <param name="device"></param>
        /// <param name="description"></param>
        /// <param name="D3D10Dev"> </param>
        /// <param name="D2DFactory"> </param>
        public SharedTexture(D2DInteropHandler handler, Texture2DDescription description)
        {
            As11Tex = new Texture2D(handler.D3DDevice11, new Texture2DDescription()
                {
                    ArraySize = description.ArraySize,
                    BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                    CpuAccessFlags = description.CpuAccessFlags,
                    Format = description.Format,
                    Height = description.Height,
                    MipLevels = description.MipLevels,
                    OptionFlags = ResourceOptionFlags.KeyedMutex,
                    SampleDescription = description.SampleDescription,
                    Usage = description.Usage,
                    Width = description.Width
                });

            Mutex11 = new KeyedMutex(As11Tex);
            AsResource = new SlimDX.DXGI.Resource(As11Tex);

            As10Tex = handler.D3DDevice10.OpenSharedResource<SlimDX.Direct3D10.Texture2D>(AsResource.SharedHandle);
            Mutex10 = new KeyedMutex(As10Tex);
            AsSurface = As10Tex.AsSurface();
            As2DTarget = SlimDX.Direct2D.RenderTarget.FromDXGI(handler.D2DFactory, AsSurface, new RenderTargetProperties()
                                                                                                            {
                                                                                                                MinimumFeatureLevel = FeatureLevel.Direct3D10,
                                                                                                                Usage = RenderTargetUsage.None,
                                                                                                                Type = RenderTargetType.Hardware,
                                                                                                                PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)
                                                                                                            });
        }
Exemple #2
0
        public void Render(Canvas canvas, Device2D device2D)
        {
            Contract.Requires(canvas != null);
            Contract.Requires(device2D != null);

            IntPtr sharedHandle = canvas.GetDeviceHandle(device2D);

            if(sharedHandle != _SharedHandle)
            {
                _SharedMutex.SafeDispose();
                _DependentView.SafeDispose();
                _SharedTexture.SafeDispose();

                _SharedMutex = null;
                _DependentView = null;
                _SharedTexture = null;

                if(sharedHandle != IntPtr.Zero)
                {
                    _SharedTexture = _Device3D.OpenSharedResource<Texture2D>(sharedHandle);

                    _SharedMutex = _SharedTexture.QueryInterface<KeyedMutex>();

                    _DependentView = new ShaderResourceView(_Device3D, _SharedTexture);

                    _SharedHandle = sharedHandle;
                }
            }

            if(_SharedMutex != null)
            {
                _SharedMutex.AcquireSync();

                try
                {
                    if(_SharedTexture != null)
                    {
                        var textureVariable = _Effect.GetVariableByName("tex2D");

                        Contract.Assert(textureVariable != null);

                        var shaderResource = textureVariable.AsShaderResource();

                        Contract.Assert(shaderResource != null);

                        shaderResource.SetResource(_DependentView);

                        _EffectPass.Apply();

                        _Device3D.Draw(_VertexCount, 0);
                    }
                }
                finally
                {
                    _SharedMutex.ReleaseSync();
                }
            }
        }
Exemple #3
0
        public void OnResize(int width, int height)
        {
            if (mRealTexture != null)
                mRealTexture.Dispose();
            if (mTmpTexture != null)
                mTmpTexture.Dispose();

            mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                Height = height,
                Width = width,
                MipLevels = 1,
                OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex,
                SampleDescription = new SampleDescription(1, 0),
                Usage = SharpDX.Direct3D11.ResourceUsage.Default
            });

            using (var resource = mRealTexture.QueryInterface<SharpDX.DXGI.Resource>())
                mTmpTexture = D2DDevice.OpenSharedResource<Texture2D>(resource.SharedHandle);

            if (NativeView != null)
                NativeView.Dispose();
            NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture,
                new SharpDX.Direct3D11.ShaderResourceViewDescription
                {
                    Format = Format.B8G8R8A8_UNorm,
                    Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                    Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource
                    {
                        MipLevels = 1,
                        MostDetailedMip = 0
                    }
                });

            if (RenderTarget != null)
                RenderTarget.Dispose();
            using (var surface = mTmpTexture.QueryInterface<Surface>())
                RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties()
                {
                    DpiX = 0.0f,
                    DpiY = 0.0f,
                    MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                    PixelFormat = new PixelFormat() { AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown },
                    Type = RenderTargetType.Hardware,
                    Usage = RenderTargetUsage.None
                });

            if (mMutex10 != null)
                mMutex10.Dispose();
            if (mMutex11 != null)
                mMutex11.Dispose();

            mMutex10 = mTmpTexture.QueryInterface<KeyedMutex>();
            mMutex11 = mRealTexture.QueryInterface<KeyedMutex>();

            Brushes.Initialize(RenderTarget);
            Fonts.Initialize(DirectWriteFactory);

            Button.Initialize();
            Frame.Initialize();

            // right now the texture is unowned and only a key of 0 will succeed.
            // after releasing it with a specific key said key then can be used for
            // further locking.
            mMutex10.Acquire(0, -1);
            mMutex10.Release(Key11);
        }
Exemple #4
0
        public void OnResize(int width, int height)
        {
            if (mRealTexture != null)
            {
                mRealTexture.Dispose();
            }
            if (mTmpTexture != null)
            {
                mTmpTexture.Dispose();
            }

            mRealTexture = new SharpDX.Direct3D11.Texture2D(mDevice.Device, new SharpDX.Direct3D11.Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = SharpDX.Direct3D11.BindFlags.RenderTarget | SharpDX.Direct3D11.BindFlags.ShaderResource,
                CpuAccessFlags    = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format            = Format.B8G8R8A8_UNorm,
                Height            = height,
                Width             = width,
                MipLevels         = 1,
                OptionFlags       = SharpDX.Direct3D11.ResourceOptionFlags.SharedKeyedmutex,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = SharpDX.Direct3D11.ResourceUsage.Default
            });

            using (var resource = mRealTexture.QueryInterface <SharpDX.DXGI.Resource>())
                mTmpTexture = D2DDevice.OpenSharedResource <Texture2D>(resource.SharedHandle);

            if (NativeView != null)
            {
                NativeView.Dispose();
            }
            NativeView = new SharpDX.Direct3D11.ShaderResourceView(mDevice.Device, mRealTexture,
                                                                   new SharpDX.Direct3D11.ShaderResourceViewDescription
            {
                Format    = Format.B8G8R8A8_UNorm,
                Dimension = SharpDX.Direct3D.ShaderResourceViewDimension.Texture2D,
                Texture2D = new SharpDX.Direct3D11.ShaderResourceViewDescription.Texture2DResource
                {
                    MipLevels       = 1,
                    MostDetailedMip = 0
                }
            });

            if (RenderTarget != null)
            {
                RenderTarget.Dispose();
            }
            using (var surface = mTmpTexture.QueryInterface <Surface>())
                RenderTarget = new RenderTarget(Direct2DFactory, surface, new RenderTargetProperties()
                {
                    DpiX        = 0.0f,
                    DpiY        = 0.0f,
                    MinLevel    = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                    PixelFormat = new PixelFormat()
                    {
                        AlphaMode = AlphaMode.Premultiplied, Format = Format.Unknown
                    },
                    Type  = RenderTargetType.Hardware,
                    Usage = RenderTargetUsage.None
                });

            if (mMutex10 != null)
            {
                mMutex10.Dispose();
            }
            if (mMutex11 != null)
            {
                mMutex11.Dispose();
            }

            mMutex10 = mTmpTexture.QueryInterface <KeyedMutex>();
            mMutex11 = mRealTexture.QueryInterface <KeyedMutex>();

            Brushes.Initialize(RenderTarget);
            Fonts.Initialize(DirectWriteFactory);

            Button.Initialize();
            Frame.Initialize();

            // right now the texture is unowned and only a key of 0 will succeed.
            // after releasing it with a specific key said key then can be used for
            // further locking.
            mMutex10.Acquire(0, -1);
            mMutex10.Release(Key11);
        }
        private void OldSlowPresent()
        {
            /* Blit from our rendered layer to our first blitting layer */
            using (var mutex = new KeyedMutex(m_blitLayer.RenderTargetTexture.InternalTexture2D))
            {
                var result = mutex.Acquire(0, 30);
                m_blitLayer.Clear();
                m_blitLayer.BeginDraw();
                m_blitLayer.DrawLayer(Layer);
                m_blitLayer.EndDraw();
                mutex.Release(0);
            }

            /* Blit from our first blitting layer to our second */
            using (var mutex = new KeyedMutex(m_blitLayer.RenderTargetTexture.InternalTexture2D))
            {
                var result = mutex.Acquire(0, 30);
                m_blitLayer2.Clear();
                m_blitLayer2.BeginDraw();
                m_blitLayer2.DrawLayer(m_blitLayer);
                //m_blitLayer2.DrawLayer(Layer);
                m_blitLayer2.EndDraw();
                mutex.Release(0);
            }

            /* Always make sure this is invoked on the UI thread */
            //m_d3dImage.Dispatcher.Invoke((Action) delegate
            //{
            //    m_d3dImage.Lock();
            //    m_d3dImage.AddDirtyRect(new Int32Rect(0, 0, m_width, m_height));
            //    m_d3dImage.Unlock();
            //});
            InvalidateD3DSurface(IntPtr.Zero);

        }
Exemple #6
0
        public void Resize()
        {
            Viewport vp     = DeviceManager.Context.Rasterizer.GetViewports()[0];
            int      width  = (int)vp.Width;
            int      height = (int)vp.Height;

            if (height == 0 || width == 0)
            {
                return;
            }
            TextureSize = new Vector2(width, height);
            float       w = width / 2f, h = height / 2f;
            List <byte> vertexBytes = new List <byte>();

            CGHelper.AddListBuffer(new Vector3(-w, h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(0, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector3(w, h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(1, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector3(-w, -h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(0, 1), vertexBytes);
            CGHelper.AddListBuffer(new Vector3(w, h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(1, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector3(w, -h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(1, 1), vertexBytes);
            CGHelper.AddListBuffer(new Vector3(-w, -h, 0), vertexBytes);
            CGHelper.AddListBuffer(new Vector2(0, 1), vertexBytes);
            using (DataStream ds = new DataStream(vertexBytes.ToArray(), true, true))
            {
                BufferDescription bufDesc = new BufferDescription()
                {
                    BindFlags   = BindFlags.VertexBuffer,
                    SizeInBytes = (int)ds.Length
                };
                if (VertexBuffer != null && !VertexBuffer.Disposed)
                {
                    VertexBuffer.Dispose();
                }
                VertexBuffer = new SlimDX.Direct3D11.Buffer(DeviceManager.Device, ds, bufDesc);
            }
            SpriteProjectionMatrix = Matrix.OrthoLH(width, height, 0, 100);
            spriteViewport         = new Viewport()
            {
                Width  = width,
                Height = height,
                MaxZ   = 1
            };


            ViewMatrix = Matrix.LookAtLH(new Vector3(0, 0, -1), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

            //DirectX11用テクスチャの作成
            if (TextureD3D11 != null && !TextureD3D11.Disposed)
            {
                TextureD3D11.Dispose();
            }
            TextureD3D11 = new Texture2D(DeviceManager.Device, new Texture2DDescription()
            {
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.B8G8R8A8_UNorm,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.KeyedMutex
            });
            //DX11のテクスチャリソースをDX10とシェアする
            Resource sharedResource = new Resource(TextureD3D11);

            if (TextureD3D10 != null && !TextureD3D10.Disposed)
            {
                TextureD3D10.Dispose();
            }
            TextureD3D10 = DeviceManager.Device10.OpenSharedResource <SlimDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);
            if (MutexD3D10 != null && !MutexD3D10.Disposed)
            {
                MutexD3D10.Dispose();
            }
            if (MutexD3D11 != null && !MutexD3D11.Disposed)
            {
                MutexD3D11.Dispose();
            }
            MutexD3D10 = new KeyedMutex(TextureD3D10);
            MutexD3D11 = new KeyedMutex(TextureD3D11);
            sharedResource.Dispose();
            Surface surface            = TextureD3D10.AsSurface();
            RenderTargetProperties rtp = new RenderTargetProperties();

            rtp.MinimumFeatureLevel = FeatureLevel.Direct3D10;
            rtp.Type        = RenderTargetType.Hardware;
            rtp.Usage       = RenderTargetUsage.None;
            rtp.PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied);
            if (DWRenderTarget != null && !DWRenderTarget.Disposed)
            {
                DWRenderTarget.Dispose();
            }
            DWRenderTarget = RenderTarget.FromDXGI(context.D2DFactory, surface, rtp);
            surface.Dispose();

            if (RenderTargetRecreated != null)
            {
                RenderTargetRecreated(this, new EventArgs());
            }
        }
        public static void Main()
        {
            global::SlimDX.Direct3D11.Device device11;
            SwapChain swapChain;

            // DirectX DXGI 1.1 factory
            Factory1 factory1 = new Factory1();

            // The 1st graphics adapter
            Adapter1 adapter1 = factory1.GetAdapter1(0);

            var form = new RenderForm("Tutorial 4: Dx11 Triangle + Text");

            var description = new SwapChainDescription()
            {
                BufferCount       = 2,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = form.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            global::SlimDX.Direct3D11.Device.CreateWithSwapChain(adapter1, DeviceCreationFlags.Debug, description, out device11, out swapChain);

            // create a view of our render target, which is the backbuffer of the swap chain we just created
            RenderTargetView renderTarget;

            using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0))
                renderTarget = new RenderTargetView(device11, resource);

            // setting a viewport is required if you want to actually see anything
            var context  = device11.ImmediateContext;
            var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);

            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);

            // A DirectX 10.1 device is required because DirectWrite/Direct2D are unable
            // to access DirectX11.  BgraSupport is required for DXGI interaction between
            // DirectX10/Direct2D/DirectWrite.
            global::SlimDX.Direct3D10_1.Device1 device10_1 = new global::SlimDX.Direct3D10_1.Device1(
                adapter1,
                global::SlimDX.Direct3D10.DriverType.Hardware,
                global::SlimDX.Direct3D10.DeviceCreationFlags.BgraSupport | global::SlimDX.Direct3D10.DeviceCreationFlags.Debug,
                global::SlimDX.Direct3D10_1.FeatureLevel.Level_10_0
                );

            // Create the DirectX11 texture2D.  This texture will be shared with the DirectX10
            // device.  The DirectX10 device will be used to render text onto this texture.  DirectX11
            // will then draw this texture (blended) onto the screen.
            // The KeyedMutex flag is required in order to share this resource.
            global::SlimDX.Direct3D11.Texture2D textureD3D11 = new Texture2D(device11, new Texture2DDescription
            {
                Width             = form.Width,
                Height            = form.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.B8G8R8A8_UNorm,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                OptionFlags       = ResourceOptionFlags.KeyedMutex
            });

            // A DirectX10 Texture2D sharing the DirectX11 Texture2D
            global::SlimDX.DXGI.Resource        sharedResource = new global::SlimDX.DXGI.Resource(textureD3D11);
            global::SlimDX.Direct3D10.Texture2D textureD3D10   = device10_1.OpenSharedResource <global::SlimDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);

            // The KeyedMutex is used just prior to writing to textureD3D11 or textureD3D10.
            // This is how DirectX knows which DirectX (10 or 11) is supposed to be writing
            // to the shared texture.  The keyedMutex is just defined here, they will be used
            // a bit later.
            KeyedMutex mutexD3D10 = new KeyedMutex(textureD3D10);
            KeyedMutex mutexD3D11 = new KeyedMutex(textureD3D11);

            // Direct2D Factory
            global::SlimDX.Direct2D.Factory d2Factory = new global::SlimDX.Direct2D.Factory(
                global::SlimDX.Direct2D.FactoryType.SingleThreaded,
                global::SlimDX.Direct2D.DebugLevel.Information
                );

            // Direct Write factory
            global::SlimDX.DirectWrite.Factory dwFactory = new global::SlimDX.DirectWrite.Factory(
                global::SlimDX.DirectWrite.FactoryType.Isolated
                );

            // The textFormat we will use to draw text with
            global::SlimDX.DirectWrite.TextFormat textFormat = new global::SlimDX.DirectWrite.TextFormat(
                dwFactory,
                "Arial",
                global::SlimDX.DirectWrite.FontWeight.Normal,
                global::SlimDX.DirectWrite.FontStyle.Normal,
                global::SlimDX.DirectWrite.FontStretch.Normal,
                24,
                "en-US"
                );
            textFormat.TextAlignment      = global::SlimDX.DirectWrite.TextAlignment.Center;
            textFormat.ParagraphAlignment = global::SlimDX.DirectWrite.ParagraphAlignment.Center;

            // Query for a IDXGISurface.
            // DirectWrite and DirectX10 can interoperate thru DXGI.
            Surface surface = textureD3D10.AsSurface();

            global::SlimDX.Direct2D.RenderTargetProperties rtp = new global::SlimDX.Direct2D.RenderTargetProperties();
            rtp.MinimumFeatureLevel = global::SlimDX.Direct2D.FeatureLevel.Direct3D10;
            rtp.Type        = global::SlimDX.Direct2D.RenderTargetType.Hardware;
            rtp.Usage       = global::SlimDX.Direct2D.RenderTargetUsage.None;
            rtp.PixelFormat = new global::SlimDX.Direct2D.PixelFormat(Format.Unknown, global::SlimDX.Direct2D.AlphaMode.Premultiplied);
            global::SlimDX.Direct2D.RenderTarget dwRenderTarget = global::SlimDX.Direct2D.RenderTarget.FromDXGI(d2Factory, surface, rtp);

            // Brush used to DrawText
            global::SlimDX.Direct2D.SolidColorBrush brushSolidWhite = new global::SlimDX.Direct2D.SolidColorBrush(
                dwRenderTarget,
                new Color4(1, 1, 1, 1)
                );

            // Think of the shared textureD3D10 as an overlay.
            // The overlay needs to show the text but let the underlying triangle (or whatever)
            // show thru, which is accomplished by blending.
            BlendStateDescription bsd = new BlendStateDescription();

            bsd.RenderTargets[0].BlendEnable           = true;
            bsd.RenderTargets[0].SourceBlend           = BlendOption.SourceAlpha;
            bsd.RenderTargets[0].DestinationBlend      = BlendOption.InverseSourceAlpha;
            bsd.RenderTargets[0].BlendOperation        = BlendOperation.Add;
            bsd.RenderTargets[0].SourceBlendAlpha      = BlendOption.One;
            bsd.RenderTargets[0].DestinationBlendAlpha = BlendOption.Zero;
            bsd.RenderTargets[0].BlendOperationAlpha   = BlendOperation.Add;
            bsd.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            BlendState BlendState_Transparent = BlendState.FromDescription(device11, bsd);

            // Load Effect. This includes both the vertex and pixel shaders.
            // Also can include more than one technique.
            ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(
                "..\\..\\NewModules\\Rendering\\Text\\SharedResourceShader.fx",
                "fx_5_0",
                ShaderFlags.EnableStrictness,
                EffectFlags.None);

            Effect effect = new Effect(device11, shaderByteCode);

            // create triangle vertex data, making sure to rewind the stream afterward
            var verticesTriangle = new DataStream(VertexPositionColor.SizeInBytes * 3, true, true);

            verticesTriangle.Write(
                new VertexPositionColor(
                    new Vector3(0.0f, 0.5f, 0.5f),
                    new Color4(1.0f, 0.0f, 0.0f, 1.0f)
                    )
                );
            verticesTriangle.Write(
                new VertexPositionColor(
                    new Vector3(0.5f, -0.5f, 0.5f),
                    new Color4(0.0f, 1.0f, 0.0f, 1.0f)
                    )
                );
            verticesTriangle.Write(
                new VertexPositionColor(
                    new Vector3(-0.5f, -0.5f, 0.5f),
                    new Color4(0.0f, 0.0f, 1.0f, 1.0f)
                    )
                );

            verticesTriangle.Position = 0;

            // create the triangle vertex layout and buffer
            InputLayout layoutColor       = new InputLayout(device11, effect.GetTechniqueByName("Color").GetPassByIndex(0).Description.Signature, VertexPositionColor.inputElements);
            Buffer      vertexBufferColor = new Buffer(device11, verticesTriangle, (int)verticesTriangle.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            verticesTriangle.Close();

            // create text vertex data, making sure to rewind the stream afterward
            // Top Left of screen is -1, +1
            // Bottom Right of screen is +1, -1
            var verticesText = new DataStream(VertexPositionTexture.SizeInBytes * 4, true, true);

            verticesText.Write(
                new VertexPositionTexture(
                    new Vector3(-1, 1, 0),
                    new Vector2(0, 0f)
                    )
                );
            verticesText.Write(
                new VertexPositionTexture(
                    new Vector3(1, 1, 0),
                    new Vector2(1, 0)
                    )
                );
            verticesText.Write(
                new VertexPositionTexture(
                    new Vector3(-1, -1, 0),
                    new Vector2(0, 1)
                    )
                );
            verticesText.Write(
                new VertexPositionTexture(
                    new Vector3(1, -1, 0),
                    new Vector2(1, 1)
                    )
                );

            verticesText.Position = 0;

            // create the text vertex layout and buffer
            InputLayout layoutText       = new InputLayout(device11, effect.GetTechniqueByName("Text").GetPassByIndex(0).Description.Signature, VertexPositionTexture.inputElements);
            Buffer      vertexBufferText = new Buffer(device11, verticesText, (int)verticesText.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            verticesText.Close();

            // prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
            factory1.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);

            // handle alt+enter ourselves
            form.KeyDown += (o, e) =>
            {
                if (e.Alt && e.KeyCode == Keys.Enter)
                {
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
                }
            };

            // handle form size changes
            form.UserResized += (o, e) =>
            {
                renderTarget.Dispose();

                swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
                using (var resource = Resource.FromSwapChain <Texture2D>(swapChain, 0))
                    renderTarget = new RenderTargetView(device11, resource);

                context.OutputMerger.SetTargets(renderTarget);
            };

            MessagePump.Run(form, () =>
            {
                // clear the render target to black
                context.ClearRenderTargetView(renderTarget, new Color4(0, 0, 0));

                // Draw the triangle
                // configure the Input Assembler portion of the pipeline with the vertex data
                context.InputAssembler.InputLayout       = layoutColor;
                context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
                context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBufferColor, VertexPositionColor.SizeInBytes, 0));
                context.OutputMerger.BlendState  = null;
                EffectTechnique currentTechnique = effect.GetTechniqueByName("Color");

                /*for (int pass = 0; pass < currentTechnique.Description.PassCount; ++pass)
                 * {
                 *  EffectPass Pass = currentTechnique.GetPassByIndex(pass);
                 *  System.Diagnostics.Debug.Assert(Pass.IsValid, "Invalid EffectPass");
                 *  Pass.Apply(context);
                 *  context.Draw(3, 0);
                 * };*/

                // Draw Text on the shared Texture2D
                // Need to Acquire the shared texture for use with DirectX10
                mutexD3D10.Acquire(0, 100);
                dwRenderTarget.BeginDraw();
                dwRenderTarget.Clear(new Color4(0, 0, 0, 0));
                string text = "Hello Wizard";// adapter1.Description1.Description;
                dwRenderTarget.DrawText(text, textFormat, new System.Drawing.Rectangle(0, 0, form.Width, form.Height), brushSolidWhite);
                dwRenderTarget.EndDraw();
                mutexD3D10.Release(0);

                // Draw the shared texture2D onto the screen
                // Need to Aquire the shared texture for use with DirectX11
                mutexD3D11.Acquire(0, 100);

                ShaderResourceView srv = new ShaderResourceView(device11, textureD3D11);
                effect.GetVariableByName("g_textOverlay").AsResource().SetResource(srv);
                context.InputAssembler.InputLayout       = layoutText;
                context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
                context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBufferText, VertexPositionTexture.SizeInBytes, 0));
                context.OutputMerger.BlendState = BlendState_Transparent;
                currentTechnique = effect.GetTechniqueByName("Text");
                for (int pass = 0; pass < currentTechnique.Description.PassCount; ++pass)
                {
                    EffectPass Pass = currentTechnique.GetPassByIndex(pass);
                    System.Diagnostics.Debug.Assert(Pass.IsValid, "Invalid EffectPass");
                    Pass.Apply(context);
                    context.Draw(4, 0);
                }
                srv.Dispose();
                mutexD3D11.Release(0);

                swapChain.Present(0, PresentFlags.None);
            });

            // clean up all resources
            // anything we missed will show up in the debug output

            vertexBufferColor.Dispose();
            vertexBufferText.Dispose();
            layoutColor.Dispose();
            layoutText.Dispose();
            effect.Dispose();
            shaderByteCode.Dispose();
            renderTarget.Dispose();
            swapChain.Dispose();
            device11.Dispose();
            device10_1.Dispose();
            mutexD3D10.Dispose();
            mutexD3D11.Dispose();
            textureD3D10.Dispose();
            textureD3D11.Dispose();
            factory1.Dispose();
            adapter1.Dispose();
            sharedResource.Dispose();
            d2Factory.Dispose();
            dwFactory.Dispose();
            textFormat.Dispose();
            surface.Dispose();
            dwRenderTarget.Dispose();
            brushSolidWhite.Dispose();
            BlendState_Transparent.Dispose();
        }