Sign in to follow this  
RomanM5

SwapChains and MultiSample don't work together

Recommended Posts

I cannot force my second form (form2) which is rendered with SwapChain to use multisampling. Could anybody tell me what's wrong with the code below? I modified Tutorial2 sample from Manged DirectX SDK April 2006 Thank you Roman //----------------------------------------------------------------------------- // File: Vertices.cs // // Desc: In this tutorial, we are rendering some vertices. This introduces the // concept of the vertex buffer, a Direct3D object used to store // vertices. Vertices can be defined any way we want by defining a // custom structure and a custom FVF (flexible vertex format). In this // tutorial, we are using vertices that are transformed (meaning they // are already in 2D window coordinates) and lit (meaning we are not // using Direct3D lighting, but are supplying our own colors). // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Drawing; using System.Windows.Forms; using Microsoft.DirectX; using Microsoft.DirectX.Direct3D; namespace VerticesTutorial { public class Vertices : Form { Device device = null; SwapChain chain = null; Surface depthStencilSurface = null; Surface backBufferSurface = null; VertexBuffer vertexBuffer = null; Form form2 = new Form(); public Vertices() { this.ClientSize = new System.Drawing.Size(300,300); this.Text = "Direct3D Tutorial 2 - Vertices"; } public bool InitializeGraphics() { try { // Now let's setup our D3D stuff PresentParameters presentParams = new PresentParameters(); presentParams.Windowed=true; presentParams.SwapEffect = SwapEffect.Discard; presentParams.MultiSample = MultiSampleType.SixSamples; presentParams.MultiSampleQuality = 0; presentParams.DeviceWindow = this; presentParams.DeviceWindowHandle = this.Handle; //////////////////////////////////////////////////// presentParams.AutoDepthStencilFormat = DepthFormat.D16; presentParams.BackBufferFormat = Format.Unknown; presentParams.EnableAutoDepthStencil = true; presentParams.PresentFlag = PresentFlag.DiscardDepthStencil; // handle all events by yourself otherwise problems with Device.Reset after device lost Device.IsUsingEventHandlers = false; /////////////////////////////////////////////// device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); this.OnCreateDevice(device, null); // second window form2.Text = "BackBuffer Form"; form2.ClientSize = new Size(300, 300); PresentParameters pp = (PresentParameters)presentParams.Clone(); pp.BackBufferWidth = form2.ClientSize.Width; pp.BackBufferHeight = form2.ClientSize.Height; pp.DeviceWindow = form2; pp.DeviceWindowHandle = form2.Handle; pp.MultiSample = MultiSampleType.SixSamples; pp.MultiSampleQuality = 0; chain = new SwapChain(device, pp); backBufferSurface = chain.GetBackBuffer(0, BackBufferType.Mono); depthStencilSurface = device.CreateDepthStencilSurface(form2.ClientSize.Width, form2.ClientSize.Height, DepthFormat.D16, MultiSampleType.SixSamples, 0, false); form2.FormClosed += new FormClosedEventHandler(form2_FormClosed); form2.Paint += new PaintEventHandler(form2_Paint); form2.Show (); return true; } catch (DirectXException) { return false; } } void form2_Paint(object sender, PaintEventArgs e) { this.RenderBackBufferForm(); } void form2_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); } public void OnCreateDevice(object sender, EventArgs e) { Device dev = (Device)sender; vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored), 3, dev, 0, CustomVertex.TransformedColored.Format, Pool.Default); vertexBuffer.Created += new System.EventHandler(this.OnCreateVertexBuffer); this.OnCreateVertexBuffer(vertexBuffer, null); } public void OnCreateVertexBuffer(object sender, EventArgs e) { VertexBuffer vb = (VertexBuffer)sender; GraphicsStream stm = vb.Lock(0, 0, 0); CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3]; verts[0].X= 50; verts[0].Y = 0; verts[0].Z=0.5f; verts[0].Rhw=1; verts[0].Color = System.Drawing.Color.Aqua.ToArgb(); verts[1].X = 100; verts[1].Y = 300; verts[1].Z = 0.5f; verts[1].Rhw = 1; verts[1].Color = System.Drawing.Color.Aqua.ToArgb(); verts[2].X = 0; verts[2].Y = 300; verts[2].Z = 0.5f; verts[2].Rhw = 1; verts[2].Color = System.Drawing.Color.Aqua.ToArgb(); stm.Write(verts); vb.Unlock(); } private void Render() { if (device == null) return; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0); device.BeginScene(); device.SetStreamSource( 0, vertexBuffer, 0); device.VertexFormat = CustomVertex.TransformedColored.Format; device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); device.EndScene(); device.Present(); } private void RenderBackBufferForm() { if (device == null) return; device.RenderState.MultiSampleAntiAlias = true; device.RenderState.AntiAliasedLineEnable = true; device.SetRenderTarget(0, backBufferSurface); device.DepthStencilSurface = depthStencilSurface; device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Red, 1.0f, 0); device.BeginScene(); device.SetStreamSource(0, vertexBuffer, 0); device.VertexFormat = CustomVertex.TransformedColored.Format; device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); device.EndScene(); chain.Present (form2); } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { this.Render(); } protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e) { if ((int)(byte)e.KeyChar == (int)System.Windows.Forms.Keys.Escape) this.Close(); } /// <summary> /// The main entry point for the application. /// </summary> static void Main() { using (Vertices frm = new Vertices()) { if (!frm.InitializeGraphics()) // Initialize Direct3D { MessageBox.Show("Could not initialize Direct3D. This tutorial will exit."); return; } frm.Show(); // While the form is still valid, render and process messages while(frm.Created) { frm.Render(); frm.RenderBackBufferForm(); Application.DoEvents(); } } } } }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this