Beispiel #1
0
        private void CreateAndBindTargets()
        {
            d3DSurface.SetRenderTarget(null);

            Disposer.SafeDispose(ref d2DRenderTarget);
            Disposer.SafeDispose(ref sharedTarget);
            Disposer.SafeDispose(ref dx11Target);

            var width  = Math.Max((int)ActualWidth, 100);
            var height = Math.Max((int)ActualHeight, 100);

            var frontDesc = new Texture2DDescription {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.Shared,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            var backDesc = new Texture2DDescription {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.None,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            sharedTarget = new Texture2D(device, frontDesc);
            dx11Target   = new Texture2D(device, backDesc);

            using (var surface = dx11Target.QueryInterface <Surface>())
            {
                d2DRenderTarget = new SharpDX.Direct2D1.DeviceContext(surface, new CreationProperties()
                {
                    Options = DeviceContextOptions.EnableMultithreadedOptimizations,
#if DEBUG
                    DebugLevel = DebugLevel.Information,
#endif
                    ThreadingMode = ThreadingMode.SingleThreaded
                });
            }

            resCache.RenderTarget = d2DRenderTarget;

            d3DSurface.SetRenderTarget(sharedTarget);

            device.ImmediateContext.Rasterizer.SetViewport(0, 0, width, height, 0.0f, 1.0f);
            TargetsCreated();
        }
        public void Dispose()
        {
            SetRenderTarget(null);

            Disposer.SafeDispose(ref renderTarget);

            EndD3D();
        }
Beispiel #3
0
        public void Dispose()
        {
            SetRenderTarget(null);

            Disposer.SafeDispose(ref renderTarget);

            ActiveClients--;
            EndD3D();
        }
 public void Clear()
 {
     foreach (var key in resources.Keys)
     {
         var res = resources[key];
         Disposer.SafeDispose(ref res);
     }
     generators.Clear();
     resources.Clear();
 }
Beispiel #5
0
        internal void SetRenderTarget(SharpDX.Direct3D11.Texture2D?target)
        {
            if (_renderTarget != null)
            {
                try
                {
                    Lock();
                    SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
                }
                finally
                {
                    Unlock();
                }

                Disposer.SafeDispose(ref _renderTarget);
            }

            if (target is null)
            {
                return;
            }

            var format = TranslateFormat(target);
            var handle = GetSharedHandle(target);

            if (IsShareable(target) == false)
            {
                throw new ArgumentException("Texture must be created with ResourceOptionFlags.Shared");
            }

            if (format == Format.Unknown)
            {
                throw new ArgumentException("Texture format is not compatible with OpenSharedResource");
            }

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("Invalid handle");
            }

            _renderTarget = new Texture(_d3DDevice, target.Description.Width, target.Description.Height, 1,
                                        Usage.RenderTarget, format, Pool.Default, ref handle);

            using var surface = _renderTarget.GetSurfaceLevel(0);

            try
            {
                Lock();
                SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer, D2dControl.IsSoftwareRenderingMode);
            }
            finally
            {
                Unlock();
            }
        }
Beispiel #6
0
        private void EndD3D()
        {
            d3DSurface.IsFrontBufferAvailableChanged -= OnIsFrontBufferAvailableChanged;
            base.Source = null;

            Disposer.SafeDispose(ref d2DRenderTarget);
            Disposer.SafeDispose(ref d2DFactory);
            Disposer.SafeDispose(ref d3DSurface);
            Disposer.SafeDispose(ref renderTarget);
            Disposer.SafeDispose(ref device);
        }
Beispiel #7
0
        public void Dispose()
        {
            SetRenderTarget(null);

            Disposer.SafeDispose(ref _renderTarget);

            EndD3D();

            Disposer.SafeDispose(ref _d3DDevice);
            Disposer.SafeDispose(ref _d3DContext);
        }
Beispiel #8
0
        private void EndD3D()
        {
            if (ActiveClients != 0)
            {
                return;
            }

            Disposer.SafeDispose(ref renderTarget);
            Disposer.SafeDispose(ref D3DDevice);
            Disposer.SafeDispose(ref D3DContext);
        }
        public bool Remove(string key)
        {
            object res;

            if (resources.TryGetValue(key, out res))
            {
                Disposer.SafeDispose(ref res);
                generators.Remove(key);
                resources.Remove(key);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #10
0
        private void CreateAndBindTargets()
        {
            if (d3DSurface == null)
            {
                return;
            }

            d3DSurface.SetRenderTarget(null);

            Disposer.SafeDispose(ref d2DRenderTarget);
            Disposer.SafeDispose(ref d2DFactory);
            Disposer.SafeDispose(ref renderTarget);

            var width  = Math.Max((int)ActualWidth, 100);
            var height = Math.Max((int)ActualHeight, 100);

            var renderDesc = new Texture2DDescription {
                BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format            = Format.B8G8R8A8_UNorm,
                Width             = width,
                Height            = height,
                MipLevels         = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Default,
                OptionFlags       = ResourceOptionFlags.Shared,
                CpuAccessFlags    = CpuAccessFlags.None,
                ArraySize         = 1
            };

            renderTarget = new Texture2D(device, renderDesc);

            var surface = renderTarget.QueryInterface <Surface>();

            d2DFactory = new SharpDX.Direct2D1.Factory();
            var rtp = new RenderTargetProperties(new PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied));

            d2DRenderTarget       = new RenderTarget(d2DFactory, surface, rtp);
            resCache.RenderTarget = d2DRenderTarget;

            d3DSurface.SetRenderTarget(renderTarget);

            device.ImmediateContext.Rasterizer.SetViewport(0, 0, width, height, 0.0f, 1.0f);
        }
Beispiel #11
0
        private void EndD3D()
        {
            d3DSurface.IsFrontBufferAvailableChanged -= OnIsFrontBufferAvailableChanged;
            Source = null;

            Disposer.SafeDispose(ref d2DRenderTarget);
            Disposer.SafeDispose(ref D2dDeviceContext);
            Disposer.SafeDispose(ref D2dFactory);
            Disposer.SafeDispose(ref d3DSurface);
            Disposer.SafeDispose(ref renderTarget);
            Disposer.SafeDispose(ref device);

            ResourceCache.Clear();

            Disposer.SafeDispose(ref DirectWriteFactory);
            Disposer.SafeDispose(ref ImagingFactory);

            Dispose();
        }
        public void SetRenderTarget(SharpDX.Direct3D11.Texture2D target)
        {
            if (renderTarget != null)
            {
                base.Lock();
                base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, IntPtr.Zero);
                base.Unlock();

                Disposer.SafeDispose(ref renderTarget);
            }

            if (target == null)
            {
                return;
            }

            var format = Dx11ImageSource.TranslateFormat(target);
            var handle = GetSharedHandle(target);

            if (!IsShareable(target))
            {
                throw new ArgumentException("Texture must be created with ResouceOptionFlags.Shared");
            }

            if (format == Format.Unknown)
            {
                throw new ArgumentException("Texture format is not compatible with OpenSharedResouce");
            }

            if (handle == IntPtr.Zero)
            {
                throw new ArgumentException("Invalid handle");
            }

            renderTarget = new Texture(D3DDevice, target.Description.Width, target.Description.Height, 1, Usage.RenderTarget, format, Pool.Default, ref handle);

            using (var surface = renderTarget.GetSurfaceLevel(0)) {
                base.Lock();
                base.SetBackBuffer(D3DResourceType.IDirect3DSurface9, surface.NativePointer);
                base.Unlock();
            }
        }
Beispiel #13
0
        // - public methods --------------------------------------------------------------

        public void Add(string key, Func <RenderTarget, object> gen)
        {
            object resOld;

            if (resources.TryGetValue(key, out resOld))
            {
                Disposer.SafeDispose(ref resOld);
                generators.Remove(key);
                resources.Remove(key);
            }

            if (renderTarget == null)
            {
                generators.Add(key, gen);
                resources.Add(key, null);
            }
            else
            {
                var res = gen(renderTarget);
                generators.Add(key, gen);
                resources.Add(key, res);
            }
        }
Beispiel #14
0
        // - private methods -------------------------------------------------------------

        private void UpdateResources()
        {
            if (renderTarget == null)
            {
                return;
            }

            foreach (var g in generators)
            {
                var key = g.Key;
                var gen = g.Value;
                var res = gen(renderTarget);

                object resOld;
                if (resources.TryGetValue(key, out resOld))
                {
                    Disposer.SafeDispose(ref resOld);
                    resources.Remove(key);
                }

                resources.Add(key, res);
            }
        }
Beispiel #15
0
 private void EndD3D()
 {
     Disposer.SafeDispose(ref _renderTarget);
 }
 private void EndD3D()
 {
     Disposer.SafeDispose(ref renderTarget);
     Disposer.SafeDispose(ref D3DDevice);
     Disposer.SafeDispose(ref D3DContext);
 }