Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[solved][SlimDX]Multiple Swapchains - Problem with Resizing?

Started by Daimon Sep 6, 2010 at 3:33 PM 1 replies 2.9k views
Original Post
Daimon
Daimon
Hello!

I am currently learning how to use SlimDX along with D3DImage in WPF, and was making a small editor like app with 4 different viewports using multiple swapchains.
I ran into a problem though, which meant weird errors with cutting off parts of my rendered scene ( a fancy triangle ), when I resize the window.
As you can see in the code listing of a minimalistic app I made to focus on the problem, I'm resizing the backbuffer, and even reset the device with the new present parameters, but still can't get it to work properly.

using System;using System.Windows;using SlimDX.Direct3D9;using System.Windows.Interop;using SlimDX;using System.Windows.Media;using System.Runtime.InteropServices;namespace minimalistic{    class CustomVertex    {        [StructLayout(LayoutKind.Sequential)]        public struct TransformedColored        {            public Vector4 Position;            public int Color;            public static readonly VertexFormat Format = VertexFormat.Diffuse | VertexFormat.PositionRhw;        }        [StructLayout(LayoutKind.Sequential)]        public struct PositionColored        {            public PositionColored(float x, float y, float z, int color)            {                Position = new Vector3(x, y, z);                Color = color;            }            public Vector3 Position;            public int Color;            public static readonly VertexFormat Format = VertexFormat.Diffuse | VertexFormat.Position;        }    }        public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            InitializeD3D();            this.Loaded += new RoutedEventHandler(Window_Loaded);        }        Device device;        Direct3D Direct3D;        SwapChain swapchain;        CustomVertex.PositionColored[] vertices;        PresentParameters pp;        public void InitializeD3D()        {            HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "test", IntPtr.Zero);            Direct3D = new SlimDX.Direct3D9.Direct3D();            pp = new PresentParameters();            pp.SwapEffect = SwapEffect.Discard;            pp.DeviceWindowHandle = hwnd.Handle;            pp.Windowed = true;            pp.BackBufferWidth = 400;            pp.BackBufferHeight = 400;            pp.BackBufferFormat = Format.X8R8G8B8;            device = new Device(                Direct3D,                0,                DeviceType.Hardware,                hwnd.Handle,                CreateFlags.HardwareVertexProcessing,                pp                );            swapchain = new SwapChain(device, pp);        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            CompositionTarget.Rendering += new EventHandler(OnRendering);            d3dimage.Lock();            d3dimage.SetBackBuffer(              D3DResourceType.IDirect3DSurface9,              swapchain.GetBackBuffer(0).ComPointer              );            d3dimage.Unlock();            vertices = new CustomVertex.PositionColored[3];            vertices[0].Position = new Vector3(-1f, -1f, 0f);            vertices[0].Color = System.Drawing.Color.Red.ToArgb();            vertices[1].Position = new Vector3(1f, -1f, 0f);            vertices[1].Color = System.Drawing.Color.Green.ToArgb();            vertices[2].Position = new Vector3(0f, 1, 0f);            vertices[2].Color = System.Drawing.Color.Yellow.ToArgb();            device.SetRenderState(RenderState.Lighting, false);            device.SetRenderState(RenderState.CullMode, Cull.None);            device.VertexFormat = CustomVertex.PositionColored.Format;            device.SetTransform(TransformState.Projection, SlimDX.Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)output.ActualWidth / (float)output.ActualHeight, 1f, 50f));            device.SetTransform(TransformState.View, SlimDX.Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new Vector3(0, 1, 0)));        }        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)        {                                          pp.BackBufferWidth = (int)output.ActualWidth;                pp.BackBufferHeight = (int)output.ActualHeight;                                      if (swapchain != null)                {                 swapchain.Dispose();                                swapchain = new SwapChain(device, pp);                                    }                d3dimage.Lock();                d3dimage.SetBackBuffer(D3DResourceType.IDirect3DSurface9, swapchain.GetBackBuffer(0).ComPointer);                d3dimage.Unlock();                                device.SetTransform(TransformState.Projection, SlimDX.Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)output.ActualWidth / (float)output.ActualHeight, 1f, 50f));                device.SetTransform(TransformState.View, SlimDX.Matrix.LookAtLH(new Vector3(0, 0, 10), new Vector3(0, 0, 0), new Vector3(0, 1, 0)));                device.Viewport = new Viewport(0, 0, (int)output.Width, (int)output.Height);                                device.SetRenderTarget(0, swapchain.GetBackBuffer(0));                    }        public void Render()        {                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Pink, 1.0f, 0);                device.BeginScene();                device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, vertices);                device.EndScene();        }        private void OnRendering(object sender, EventArgs e)        {            d3dimage.Lock();            Render();            d3dimage.AddDirtyRect(new Int32Rect(0, 0, (int)d3dimage.Width, (int)d3dimage.Height));            d3dimage.Unlock();        }    }}



any Ideas?

I also include a screenshot that shows the weirdness.

HERP DERP

[Edited by - Daimon on September 17, 2010 5:14:12 AM]
Daimon
Daimon
Okay, I did the same in winforms, without any problems. Must be D3DImage related, but I can't see what is wrong there... Any Ideas?
Daimon
Daimon
Without Problems? Nope. If I used SwapChains, I had the same problem.
I could finally solve it though, by creating a DepthStencil Surface with the size of the biggest active window and assign it to the device. Thanks anyway.

Here's a bit of code out of the device initialisation stuff of my renderwindow.


            PresentParameters pp = new PresentParameters();            pp.BackBufferWidth = this.Width;            pp.BackBufferHeight = this.Height;            pp.DeviceWindowHandle = this.Handle;            D3D9Device device = GraphicsDevice.Device;            if (device.Device == null) //Device was not created yet, so create it            {                // points to my device class, simply creates a Device or DeviceEx depending on which one is supported                device.CreateDevice(                    0,                    DeviceType.Hardware,                    this.Handle,                    CreateFlags.HardwareVertexProcessing | CreateFlags.Multithreaded,                    pp);            }            else //there is already a device, so create an additional swapchain for the next window            {                                depthStencilSurface = Surface.CreateDepthStencil(device.Device,                   maxWidth, // greatest width of all active windows                    maxHeight, // greatest height of all active windows                    pp.AutoDepthStencilFormat,                   MultisampleType.None,                   0,                   true);                device.Device.DepthStencilSurface = depthStencilSurface;                //create the new swapchain                SwapChain = new SwapChain(device.Device, pp);            }


Suggestions / constructive criticism is very welcome (I'm still a noob with Direct3D / SlimDX).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.