Resize fail ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Resize fail ?

  1. #1
    Join Date
    Jul 2014
    Posts
    1

    Resize fail ?

    I work with C# and sharpDX and I a problem to resize a Form.

    Form1:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using SharpDX;
    using SharpDX.Direct2D1;
    using SharpDX.DXGI;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public DirectX_2D vue2D;
    
            public Form1()
            {
                InitializeComponent();
                vue2D = new DirectX_2D(this);
            }
    
            private void Form1_SizeChanged(object sender, EventArgs e)
            {
                panel1.Width = this.Width - 40;
                panel1.Height = this.Height - 60;
                // Redéfinie l'affichage
                vue2D.backBuffer.Dispose();
                vue2D.renderTargetView.Dispose();
                vue2D.depthBuffer.Dispose();
                vue2D.depthStencilView.Dispose();
      ->problem!          vue2D.SwapChain.ResizeBuffers(0, panel1.ClientSize.Width, panel1.ClientSize.Height, Format.Unknown, SwapChainFlags.None);
                vue2D.backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(vue2D.SwapChain, 0);
                vue2D.renderTargetView = new SharpDX.Direct3D11.RenderTargetView(vue2D.Device11, vue2D.backBuffer);
                vue2D.depthBuffer = new SharpDX.Direct3D11.Texture2D(vue2D.Device11, new SharpDX.Direct3D11.Texture2DDescription()
                {
                    Format = Format.R8G8B8A8_UNorm,
                    ArraySize = 1,
                    MipLevels = 1,
                    Width = panel1.ClientSize.Width,
                    Height = panel1.ClientSize.Height,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage = SharpDX.Direct3D11.ResourceUsage.Default,
                    BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
                    CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                    OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
                });
                vue2D.depthStencilView = new SharpDX.Direct3D11.DepthStencilView(vue2D.Device11, vue2D.depthBuffer);
                vue2D.Device11.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, panel1.ClientSize.Width, panel1.ClientSize.Height));
                vue2D.Device11.ImmediateContext.OutputMerger.SetTargets(vue2D.depthStencilView, vue2D.renderTargetView);
                vue2D.Render(this);
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                panel1.Width = this.Width - 40;
                panel1.Height = this.Height - 60;
                if (vue2D == null)
                    vue2D = new DirectX_2D(this);
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                vue2D.Render(this);
            }
        }
    }
    vue2D
    Code:
    using SharpDX;
    using SharpDX.Direct2D1;
    using SharpDX.DXGI;
    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public class DirectX_2D
        {
            public SharpDX.Direct3D11.Device Device11 = null;
            public SharpDX.Direct3D10.Device1 Device10 = null;
            public SwapChain SwapChain = null;
            public SharpDX.Direct2D1.Factory Factory = null;
            public SharpDX.Direct2D1.RenderTarget Graphic = null;
            public Surface Surface = null;
            public SharpDX.Direct3D11.Texture2D backBuffer = null;
            public SharpDX.Direct3D11.RenderTargetView renderTargetView = null;
            public SharpDX.Direct3D11.Texture2D depthBuffer = null;
            public SharpDX.Direct3D11.DepthStencilView depthStencilView = null;
    
            public DirectX_2D(Form1 form)
            {
                // Initialisation        
                try
                {
                    // SwapChain description
                    SwapChainDescription descriptionSwap = new SwapChainDescription()
                    {
                        BufferCount = 1,
                        ModeDescription =
                            new ModeDescription(form.panel1.ClientSize.Width, form.panel1.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                        IsWindowed = true,
                        OutputHandle = form.panel1.Handle,
                        SampleDescription = new SampleDescription(1, 0),
                        SwapEffect = SwapEffect.Discard,
                        Usage = Usage.RenderTargetOutput
                    };
                    // DirectX 11
                    SharpDX.Direct3D.FeatureLevel[] levels11 = {SharpDX.Direct3D.FeatureLevel.Level_11_0,SharpDX.Direct3D.FeatureLevel.Level_10_1,
                                                                SharpDX.Direct3D.FeatureLevel.Level_10_0,SharpDX.Direct3D.FeatureLevel.Level_9_3,
                                                                SharpDX.Direct3D.FeatureLevel.Level_9_2,SharpDX.Direct3D.FeatureLevel.Level_9_1};
                    try
                    {
                        SharpDX.Direct3D11.DeviceCreationFlags flag = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
                        SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, flag,
                                                                      levels11, descriptionSwap, out Device11, out SwapChain);
                        backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
                        renderTargetView = new SharpDX.Direct3D11.RenderTargetView(Device11, backBuffer);
                        depthBuffer = new SharpDX.Direct3D11.Texture2D(Device11, new SharpDX.Direct3D11.Texture2DDescription()
                        {
                            Format = Format.R8G8B8A8_UNorm,
                            ArraySize = 1,
                            MipLevels = 1,
                            Width = form.panel1.ClientSize.Width,
                            Height = form.panel1.ClientSize.Height,
                            SampleDescription = new SampleDescription(1, 0),
                            Usage = SharpDX.Direct3D11.ResourceUsage.Default,
                            BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
                            CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                            OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
                        });
                        depthStencilView = new SharpDX.Direct3D11.DepthStencilView(Device11, depthBuffer);
                        SharpDX.Direct3D11.RasterizerStateDescription rasterizerDescription = new SharpDX.Direct3D11.RasterizerStateDescription()
                        {
                            CullMode = SharpDX.Direct3D11.CullMode.None,
                            FillMode = SharpDX.Direct3D11.FillMode.Solid,
                            IsAntialiasedLineEnabled = true,
                            IsFrontCounterClockwise = true,
                            IsMultisampleEnabled = true,
                            IsDepthClipEnabled = true,
                            IsScissorEnabled = false
                        };
                        Device11.ImmediateContext.Rasterizer.State =new SharpDX.Direct3D11.RasterizerState(Device11, rasterizerDescription);
                        Device11.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, (int)form.panel1.ClientSize.Width, (int)form.panel1.ClientSize.Height));
                        Device11.ImmediateContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
                        goto OK1;
                    }
                    catch
                    {
                        goto Suite1;
                    }
                OK1:
                    Factory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded);
                    SharpDX.Direct3D11.Texture2D backBuffer11 = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
                    Surface = backBuffer11.QueryInterface<Surface>();
                    Graphic = new RenderTarget(Factory, Surface, new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)));
                    return;
                // DirectX 10
                Suite1:
                    SharpDX.Direct3D10.FeatureLevel[] levels10 ={SharpDX.Direct3D10.FeatureLevel.Level_10_1,SharpDX.Direct3D10.FeatureLevel.Level_10_0,
                                                               SharpDX.Direct3D10.FeatureLevel.Level_9_3,SharpDX.Direct3D10.FeatureLevel.Level_9_2,                   
                                                               SharpDX.Direct3D10.FeatureLevel.Level_9_1};
                    foreach (var level2 in levels10)
                    {
                        try
                        {
                            SharpDX.Direct3D10.Device1.CreateWithSwapChain(SharpDX.Direct3D10.DriverType.Hardware, SharpDX.Direct3D10.DeviceCreationFlags.BgraSupport,
                                                                            descriptionSwap, level2, out Device10, out SwapChain);
                            goto OK2;
                        }
                        catch (ArgumentException) // E_INVALIDARG
                        {
                            continue; // Try the next feature level
                        }
                        catch (OutOfMemoryException) // E_OUTOFMEMORY
                        {
                            continue; // Try the next feature level
                        }
                        catch (Exception) // SharpDX.Direct3D10.Direct3D10Exception D3DERR_INVALIDCALL or E_FAIL
                        {
                            continue; // Try the next feature level
                        }
                    }
                    throw new SystemException();
                OK2:
                    Factory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded);
                    SharpDX.Direct3D10.Texture2D backBuffer10 = SharpDX.Direct3D10.Texture2D.FromSwapChain<SharpDX.Direct3D10.Texture2D>(SwapChain, 0);
                    Surface = backBuffer10.QueryInterface<Surface>();
                    Graphic = new RenderTarget(Factory, Surface, new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)));
                    Graphic.DotsPerInch = new Size2F(96, 96);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message + "  -> Erreur vue2D: construction du device graphique impossible.");
                    Dispose();
                    Environment.Exit(0);
                }
            }
    
            public void Dispose()
            {
                if (Device10 != null)
                {
                    Device10.Dispose();
                    Device10 = null;
                }
                if (Device11 != null)
                {
                    Device11.Dispose();
                    Device11 = null;
                }
                if (SwapChain != null)
                {
                    SwapChain.Dispose();
                    SwapChain = null;
                }
                if (Graphic != null)
                {
                    Graphic.Dispose();
                    Graphic = null;
                }
                if (Factory != null)
                {
                    Factory.Dispose();
                    Factory = null;
                }
                if (backBuffer == null)
                {
                    backBuffer.Dispose();
                    backBuffer = null;
                }
                if (renderTargetView == null)
                {
                    renderTargetView.Dispose();
                    renderTargetView = null;
                }
                if (depthBuffer == null)
                {
                    depthBuffer.Dispose();
                    depthBuffer = null;
                }
                if (depthStencilView == null)
                {
                    depthStencilView.Dispose();
                    depthStencilView = null;
                }
            }
    
            public void Render(Form1 form)
            {
                if (Graphic == null)
                    return;
                Graphic.BeginDraw();
                Graphic.Clear(SharpDX.Color.Black);
                StrokeStyleProperties strokeProperties = new StrokeStyleProperties() { StartCap = CapStyle.Round, EndCap = CapStyle.Round };
                StrokeStyle stroke = new StrokeStyle(Factory, strokeProperties);
                SolidColorBrush brush = new SolidColorBrush(Graphic, new Color(255, 255, 255, 255));
    
                Graphic.DrawLine(new Vector2(0, 0), new Vector2(form.panel1.Width, form.panel1.Height), brush, 1f, stroke);
    
                brush.Dispose();
                stroke.Dispose();
                Graphic.EndDraw();
                SwapChain.Present(0, PresentFlags.None);
            }
    
        }
    }
    The code fail here in Form1:

    vue2D.SwapChain.ResizeBuffers(0, panel1.ClientSize.Width, panel1.ClientSize.Height, Format.Unknown, SwapChainFlags.None);

    The ResizeBuffers fail. I read perhaps, it was due because I don't release just before all the instance linked with my SwapChain.

    If somebody have an idea, thank you very much .

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,547

    Re: Resize fail ?

    You might receive better responses if you posted to a c# forum (http://forums.codeguru.com/forumdisp...rp-Programming) rather than to a c++ forum.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center