IntPtr GetSharedHandle(SharpDX.Direct3D10.Texture2D Texture)
        {
            SharpDX.DXGI.Resource resource = Texture.QueryInterface <SharpDX.DXGI.Resource>();
            IntPtr result = resource.SharedHandle;

            resource.Dispose();
            return(result);
        }
Example #2
0
        public static IDraw GetDrawer()
        {
            var dv  = new SharpDX.Direct3D10.Device1(DriverType.Hardware);
            var df  = new SharpDX.Direct2D1.Factory();
            var t2d = new SharpDX.Direct3D10.Texture2D(dv, new Texture2DDescription()
            {
                Height = 100, Width = 100, MipLevels = 1, Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm
            });
            var rt = new RenderTarget(df, t2d.QueryInterface <SharpDX.DXGI.Surface1>(), new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)));

            return(new GetSDXContext(new Renderers.SharpDX.SharpDX_RenderElements(rt)));
        }
Example #3
0
        protected void CreateSizeDependentComponents()
        {
            backBuffer     = Direct3D.Resource.FromSwapChain <Direct3D.Texture2D>(swapChain, 0);
            backBufferView = new Direct3D.RenderTargetView(device, backBuffer);

            factory        = new Direct2D1.Factory();
            imagingFactory = new ImagingFactory();

            using (var surface = backBuffer.QueryInterface <DXGI.Surface>()) {
                renderTarget = new Direct2D1.RenderTarget(factory, surface,
                                                          new Direct2D1.RenderTargetProperties(new Direct2D1.PixelFormat(DXGI.Format.Unknown, Direct2D1.AlphaMode.Premultiplied)));
            }

            renderTarget.AntialiasMode     = Direct2D1.AntialiasMode.PerPrimitive;
            renderTarget.TextAntialiasMode = Direct2D1.TextAntialiasMode.Cleartype;

            var imageLoader = ImageLoader as ImageLoader;

            if (imageLoader != null)
            {
                imageLoader.ReloadAll();
            }
        }
Example #4
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();
                }
            }
        }
Example #5
0
        private void _initializeGraphics()
        {
            Console.Write("Initializing graphic device... ");

            var desc = new SwapChainDescription() {
                BufferCount = 1,
                ModeDescription = new ModeDescription(
                    _appConfiguration.Width,
                    _appConfiguration.Height,
                    new Rational(60, 1),
                    Format.R8G8B8A8_UNorm),
                IsWindowed = !_appConfiguration.FullScreen,
                OutputHandle = DisplayHandle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };

            Device1.CreateWithSwapChain(
                DriverType.Hardware,
            #if DEBUG
             DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug | DeviceCreationFlags.SingleThreaded,
            #else
                DeviceCreationFlags.BgraSupport,
            #endif
             desc,
                out _device,
                out _swapChain);

            if (_device == null)
                throw new SharpDXException("Failed to initialize graphics device.");

            if (_swapChain == null)
                throw new SharpDXException("Failed to initialize swap chain.");

            ToDispose<Device1>(_device);
            ToDispose<SwapChain>(_swapChain);
            Factory2D = ToDispose<Factory2D>(new Factory2D());

            _factoryDXGI = ToDispose<FactoryDXGI>(_swapChain.GetParent<FactoryDXGI>());
            _factoryDXGI.MakeWindowAssociation(DisplayHandle, WindowAssociationFlags.IgnoreAll);

            _backBuffer = ToDispose<Texture2D>(Texture2D.FromSwapChain<Texture2D>(_swapChain, 0));
            _backBufferRenderTargetView = ToDispose<RenderTargetView>(new RenderTargetView(_device, _backBuffer));

            Viewport = new Viewport(0, 0, _appConfiguration.Width, _appConfiguration.Height);
            using (var surface = _backBuffer.QueryInterface<Surface>()) {
                RenderTarget2D = ToDispose<RenderTarget>(
                    new RenderTarget(Factory2D,
                        surface,
                        new RenderTargetProperties(
                            new PixelFormat(
                                Format.Unknown,
                                AlphaMode.Premultiplied))));
            }
            RenderTarget2D.AntialiasMode = AntialiasMode.PerPrimitive;

            _vsync = Config.VSync;

            ScreenSize = new DrawingSizeF(Viewport.Width, Viewport.Height);

            Console.WriteLine("done.");
        }
Example #6
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 Initialize()
        {
            // Create the form
            form = CreateForm();

            // SwapChain description
            var desc = new SwapChainDescription()
            {
                BufferCount = 1,
                ModeDescription =
                    new ModeDescription((int)Config.SCREEN_WIDTH, (int)Config.SCREEN_HEIGHT,
                                        new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed = true,
                OutputHandle = form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput
            };
            //Create the device and swapchain
            try
            {
                Device1.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, out device, out swapChain);
            }
            catch (Exception e)
            {
                Device1.CreateWithSwapChain(DriverType.Warp, DeviceCreationFlags.BgraSupport, desc, out device, out swapChain);
                Console.WriteLine("Could not create Hardware drivertype, using Warp instead. \n" + e.ToString());
            }

            // Ignore all Windows events
            SharpDX.DXGI.Factory factory = swapChain.GetParent<SharpDX.DXGI.Factory>();
            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            // New RenderTargetView from the backbuffer
            backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
            backBufferView = new RenderTargetView(device, backBuffer);

            // Create the rendertarget for the form
            factory2D = new SharpDX.Direct2D1.Factory();
            Surface surface = backBuffer.QueryInterface<Surface>();
            renderTarget = new RenderTarget(factory2D, surface, new RenderTargetProperties(new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)));
            renderTarget.AntialiasMode = AntialiasMode.Aliased;
            // Initialize the global resources used for drawing and writing.
            Resources.Initialize(renderTarget);
            runnableComponent.InitBase();
        }