<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Manders vs. Machine</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/manders/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/manders/feed.xml</link>
    <description>A blog about PIX, XNA, and other game programming stuff</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:05:35 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>RoundLine Code Updated for XNA 4.0</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/roundline-code-updated-for-xna-4-0</link>
      <pubDate>Wed, 09 Feb 2011 08:23:52 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2011/02/09/roundline-code-updated-for-xna-4-0/</guid>
      <description><![CDATA[I've updated my RoundLine code to work with the latest version of XNA Game Studio (4.0).&nbsp; Check...]]></description>
      <content:encoded><![CDATA[<p>I've updated my <a href="http://blogs.msdn.com/b/manders/archive/2007/01/07/lines-2d-thick-rounded-line-segments-for-xna-programs.aspx">RoundLine</a> code to work with the latest version of <a href="http://create.msdn.com/en-us/home/getting_started">XNA Game Studio</a> (4.0).&nbsp; Check it out on <a href="http://roundline.codeplex.com/">CodePlex</a>.&nbsp; Enjoy!</p>
<p>-Mike</p>]]></content:encoded>
    </item>
    <item>
      <title>The February 2010 DirectX SDK is available!</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/the-february-2010-directx-sdk-is-available</link>
      <pubDate>Fri, 05 Feb 2010 16:50:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2010/02/05/the-february-2010-directx-sdk-is-available/</guid>
      <description><![CDATA[The February 2010 DirectX SDK was released just moments ago. In this release, PIX for Windows has...]]></description>
      <content:encoded><![CDATA[<SPAN lang="">
<P>The <A href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=2c7da5fb-ffbb-4af6-8c66-651cbd28ca15" mce_href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=2c7da5fb-ffbb-4af6-8c66-651cbd28ca15">February 2010 DirectX SDK</A> was released just moments ago. In this release, <A href="http://msdn.microsoft.com/en-us/library/ee417062(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/ee417062(VS.85).aspx">PIX for Windows</A> has been updated with much fuller support for Direct3D 11 analysis. If you're using Direct3D 11, you'll definitely want to pick up this version of the DXSDK in order to be able to use great PIX features such as shader debugging and the mesh viewer with your Direct3D 11 programs. There are also a few new features that help with analysis of programs using any version of Direct3D. </P>
<P>Here's the official "What's New" announcement:</P><B>
<P>What's New in the February 2010 DirectX SDK</P></B>
<P>This version of the DirectX SDK contains the following new features, tools, and documentation.</P><B>
<P>Improvements to PIX </P></B>
<P>PIX has much-improved support for analysis of programs that use Direct3D 11 features, including compute shaders, tessellation, and dynamic shader class linkage. PIX features such as pixel history, the shader debugger, the mesh viewer, frame counters, and screenshots now work for these Direct3D 11 programs.</P>
<P>The new "Playback Device" control in the Events pane enables the user to force PIX Direct3D playback to use the REF device.</P>
<P>Mesh viewer data can be exported to a CSV file by right-clicking the data tables displayed in the PreVS, PostVS, or PostGS views.</P>
<P>The texture viewer now supports the following additional formats:</P>
<UL>
<LI>DXGI_FORMAT_D24_UNORM_S8_UINT </LI>
<LI>DXGI_FORMAT_R24G8_TYPELESS </LI>
<LI>DXGI_FORMAT_BC1_UNORM_SRGB </LI>
<LI>DXGI_FORMAT_BC2_UNORM_SRGB </LI>
<LI>DXGI_FORMAT_BC3_UNORM_SRGB </LI></UL><B>
<P>XNAMath C++ SIMD Math Library </P></B>
<P>A minor revision to the library (version 2.02), includes a number of customer-reported fixes, optimizations, and improvements. Included are notable fixes to XMStoreColor(), XMQuaternionRotationMatrix(), XMVectorATan2(), and XMVectorATan2Est().</P><B>
<P>Direct3D 11 Deployment</P></B>
<P>The Direct3D 11 Deployment for Game Developers article and D3D11InstallHelper Sample have been updated to include guidance for corporate network users in a locally managed Windows Server Update Services (WSUS) environment. For these developers, KB 971644 is not applicable and they should refer to KB 971512 instead.</P><B>
<P>Enhancements to Voice Reuse Scenarios</P></B>
<P>Implementation changes to simplify voice reuse were made to IXAudio2SourceVoice::FlushSourceBuffers, IXAudio2SourceVoice::SetSourceSamplingRate, and IXAudio2SourceVoice::Stop. These changes enable titles to minimize the performance cost of creating and destroying multiple voices during the game.</P><B>
<P>Reverb and Mixing Performance Optimizations</P></B>
<P>We have optimized the codepaths for several of the less common channel mixing scenarios for mixing within the audio graph.</P>
<P>　</P>
<P>-Mike</P></SPAN>]]></content:encoded>
    </item>
    <item>
      <title>Importing and rendering XAML shape data in XNA Game Studio</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/importing-and-rendering-xaml-shape-data-in-xna-game-studio</link>
      <pubDate>Sun, 25 Oct 2009 00:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2009/10/25/importing-and-rendering-xaml-shape-data-in-xna-game-studio/</guid>
      <description><![CDATA[&nbsp; 
I've been wanting to find a better way to get 2D geometry content into my XNA Game Studio...]]></description>
      <content:encoded><![CDATA[&nbsp; 
<P>I've been wanting to find a better way to get 2D geometry content into my <A href="http://creators.xna.com/" mce_href="http://creators.xna.com">XNA Game Studio</A> projects. Laying out lines and triangles on graph paper, then entering coordinates by hand into code files gets old fast! I was looking for a 2D illustration package that I could use instead, then translate from its data files into something I could read from XNA.</P>
<P><A href="http://www.microsoft.com/expression/products/Design_Overview.aspx" mce_href="http://www.microsoft.com/expression/products/Design_Overview.aspx">Microsoft Expression Design</A> is a pretty nice tool for laying out lines and shapes, and I was interested to see that it can export its documents as WPF XAML, which looked fairly straightforward to parse. I decided to see if I could use the <A href="http://msdn.microsoft.com/en-us/library/bb203887.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb203887.aspx">XNA Content Pipeline</A> to translate the XAML into lines and triangles that I could render.</P>
<P><A href="http://msdn.microsoft.com/en-us/library/ms752059.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms752059.aspx">XAML</A> is based on XML, so I thought at first that I would parse the XML nodes directly with something like <A href="http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx">XmlReader</A>. Then I realized that it was pretty easy to let <A href="http://msdn.microsoft.com/en-us/library/ms754130.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms754130.aspx">Windows Presentation Foundation</A> take care of the parsing, and just traverse the resulting WPF&nbsp;object hierarchy&nbsp;to get the path information. The XAML files I generated in Expression Design had a top-level <A href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx">Canvas</A> object, which contained Canvas child objects for each layer. These inner Canvas objects had various <A href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.shape(VS.85).aspx">Shapes</A> -- the ones I was interested in were <A href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.path(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.shapes.path(VS.85).aspx">Paths</A>. A method called <A href="http://msdn.microsoft.com/en-us/library/system.windows.media.geometry.getflattenedpathgeometry.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.media.geometry.getflattenedpathgeometry.aspx">GetFlattenedPathGeometry</A> took care of converting any curves into line segments -- then I just had to iterate through the line segments and record the coordinates.</P>
<P>This was my first time attempting to extend the XNA Content Pipeline. It took a bit of background reading to get the hang of it, but it seems straightforward to me now that I understand the <A href="http://msdn.microsoft.com/en-us/library/bb447754.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb447754.aspx">concepts</A>. My Content Importer takes a XAML file as input and generates a "MyDataTypes.MyShape" object as output. I didn't need a Content Processor, since my app works with MyShape objects (there was no intermediate type needed). And thanks to XNA Game Studio 3.1's <A href="http://blogs.msdn.com/shawnhar/archive/2009/03/25/automatic-xnb-serialization-in-xna-game-studio-3-1.aspx" mce_href="http://blogs.msdn.com/shawnhar/archive/2009/03/25/automatic-xnb-serialization-in-xna-game-studio-3-1.aspx">automatic XNB serialization</A>, I didn't have to write a ContentTypeWriter or ContentTypeReader for MyShape.</P>
<P>One minor thing I had to deal with was a coordinate system change. Expression Designer and WPF treat the top-left corner of the document as the origin, with Y increasing as you move down the screen. I am more used to Y increasing as you go up the screen, so all my content was appearing upside-down at first (though I could modify my projection matrix to match WPF as one way to address this). I decided to have my Content Importer perform the Y inversion with a "Y = CanvasHeight - Y" adjustment.</P>
<P>Getting the line segments for a path was fairly easy. Getting the triangles for the interior of the path required a bit more work. Basically I needed a way to tessellate arbitrary convex polygons into triangles (this is sometimes called <A href="http://en.wikipedia.org/wiki/Polygon_triangulation" mce_href="http://en.wikipedia.org/wiki/Polygon_triangulation">triangulation</A>, since the term <A href="http://en.wikipedia.org/wiki/Tessellation" mce_href="http://en.wikipedia.org/wiki/Tessellation">tessellation</A> can mean <A href="http://blogs.msdn.com/manders/archive/2008/04/15/polygonal-tessellation.aspx" mce_href="http://blogs.msdn.com/manders/archive/2008/04/15/polygonal-tessellation.aspx">other things</A>).&nbsp; Algorithms exist to do triangulation, but I was having trouble finding some pre-existing code to use. Then I learned that <A href="http://msdn.microsoft.com/en-us/library/dd370990(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd370990(VS.85).aspx">Direct2D</A> has the ability to <A href="http://msdn.microsoft.com/en-us/library/dd742772(VS.85).aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd742772(VS.85).aspx">tessellate</A> geometry, and decided to try using that.</P>
<P>Direct2D is fairly new, and is built into <A href="http://www.microsoft.com/windows/windows-7/" mce_href="http://www.microsoft.com/windows/windows-7/">Windows 7</A>. It can be installed on Windows Vista SP2 if you've installed the "Platform Update for Windows Vista" -- see <A href="http://support.microsoft.com/kb/971644" mce_href="http://support.microsoft.com/kb/971644">KB 971644</A>. If you're still using Windows XP, I'm afraid you're out of luck -- you'll have to find another way to triangulate shapes. Headers/libs for Direct2D are available both in the latest <A href="http://msdn.microsoft.com/en-us/windows/bb980924.aspx" mce_href="http://msdn.microsoft.com/en-us/windows/bb980924.aspx">Windows SDK</A> and the August 2009 <A href="http://msdn.microsoft.com/en-us/directx/default.aspx" mce_href="http://msdn.microsoft.com/en-us/directx/default.aspx">DirectX SDK</A>.</P>
<P>I wrote a standalone tool called D2DTessellate.exe to handle this triangulation task while importing content. It's written in C++, not C#, so I've provided a executable as well as the source code, in case you don't have Visual C++ or the necessary headers/libs installed. The tool reads a text file containing a list of points specifying a polygon, calls into Direct2D to tessellate it, then writes the resulting triangles to another text file. It's short and it works! My content importer invokes D2DTessellate.exe as needed for each polygon.</P>
<P>Note that the Content Importer needs to use WPF and D2D, but everything is just "MyShape" by the time the game loads the content from the Content Pipeline. So games that use XAML-based content with this method can still run on the Xbox -- the game itself doesn't have any direct dependencies on those Windows-only technologies.</P>
<P>Back in my XNA Game Studio game app, the main new thing I needed to add was a SimpleMesh class for drawing these lists of triangles associated with MyShape. I used non-indexed triangle lists, since that matched the data coming out of D2DTessellate. Since a lot of vertices are shared by multiple triangles, it would be possible to save a little space and improve perf by using index buffers and re-using vertex data instead of replicating it. My shapes were small enough that it didn't really matter. I created a spiffy zig-zag pixel shader as an example of a more interesting way to render the mesh triangles than a solid color. I also added a wireframe option to the test app, so you can see the tessellation.</P>
<P><IMG style="WIDTH: 640px; HEIGHT: 373px" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/91/25/50/XamlDraw_2.jpg" original-url="http://blogs.msdn.com/photos/manders/images/9912550/640x373.aspx" width=640 height=373 mce_src="http://blogs.msdn.com/photos/manders/images/9912550/640x373.aspx"></P>
<P>I'm really happy with the result -- now I can draw shapes in Expression Designer, export the XAML, and easily import and draw it in my XNA Game Studio apps. Rock on!</P>
<P><IMG style="WIDTH: 640px; HEIGHT: 373px" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/91/25/49/XamlDraw_1.png" original-url="http://blogs.msdn.com/photos/manders/images/9912549/640x373.aspx" width=640 height=373 mce_src="http://blogs.msdn.com/photos/manders/images/9912549/640x373.aspx"></P>
<P>-Mike</P>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/91/25/51/XamlDraw_1_0.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-91-25-51/XamlDraw_5F00_1_5F00_0.zip">XamlDraw_1_0.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>XNA RoundLine Code Released on CodePlex</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/xna-roundline-code-released-on-codeplex</link>
      <pubDate>Sat, 14 Mar 2009 01:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2009/03/14/xna-roundline-code-released-on-codeplex/</guid>
      <description><![CDATA[Since there still seems to be a lot of interest in it, I've updated my RoundLine code and released...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>Since there still seems to be a lot of interest in it, I've updated my <A class="" href="http://blogs.msdn.com/manders/archive/2007/01/07/lines-2d-thick-rounded-line-segments-for-xna-programs.aspx" mce_href="http://blogs.msdn.com/manders/archive/2007/01/07/lines-2d-thick-rounded-line-segments-for-xna-programs.aspx">RoundLine</A> code and <A class="" href="http://roundline.codeplex.com/" mce_href="http://roundline.codeplex.com/">released it on CodePlex</A>. Here are the major changes from earlier versions:</P>
<UL>
<LI>Updated for <A class="" href="http://creators.xna.com/" mce_href="http://creators.xna.com">XNA Game Studio 3.0</A> </LI>
<LI>Uses <A class="" href="http://blogs.msdn.com/manders/archive/2008/04/06/it-s-full-of-stars-xna-2-d-shader-instancing.aspx" mce_href="http://blogs.msdn.com/manders/archive/2008/04/06/it-s-full-of-stars-xna-2-d-shader-instancing.aspx">shader instancing</A> for improved performance: over 20,000 lines per frame at 60fps on the Xbox 360</LI>
<LI>Drawing code is in a separate file from interaction code (using partial classes), so you can easily skip the latter if you don't need it</LI>
<LI>Miscellaneous cleanup</LI>
<LI>A small demo app is included</LI></UL>
<P>I hope you find it useful!</P>
<P><IMG style="WIDTH: 656px; HEIGHT: 396px" height=396 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/47/45/40/RoundLineDemo.png" original-url="http://blogs.msdn.com/photos/manders/images/9474540/original.aspx" width=656 mce_src="http://blogs.msdn.com/photos/manders/images/9474540/original.aspx"></P>
<P>-Mike</P></FONT>]]></content:encoded>
    </item>
    <item>
      <title>Check out the August 2008 version of PIX</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/check-out-the-august-2008-version-of-pix</link>
      <pubDate>Mon, 11 Aug 2008 03:12:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/08/11/check-out-the-august-2008-version-of-pix/</guid>
      <description><![CDATA[The August 2008 DirectX SDK has been released. You can get it here.
Here are the PIX updates, as...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>The August 2008 DirectX SDK has been released. You can get it <A class="" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=ea4894b5-e98d-44f6-842d-e32147237638&amp;DisplayLang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyID=ea4894b5-e98d-44f6-842d-e32147237638&amp;DisplayLang=en">here</A>.</P>
<P>Here are the PIX updates, as listed on the official "<A class="" href="http://msdn.microsoft.com/en-us/library/bb173043.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb173043.aspx">What's New</A>" summary for the Aug08 release.</P>
<P><B><FONT face="Times New Roman">PIX Improvements</B></FONT></P></FONT><FONT face="Times New Roman">
<DIR>
<P>This release of PIX has many significant improvements.</P>
<UL>
<LI>Draw call timing for single-frame captures—PIX now calculates GPU-side timing information for Direct3D 9 and Direct3D 10 draw calls when performing single-frame captures or during playback. </LI>
<LI>Improved 32/64-bit support—PIX has improved 64-bit support and cross-platform compatibility. Run files can be played back on either platform regardless of whether the original application was 32-bit or 64-bit. By using the 64-bit version of PIX to analyze your application, you can take advantage of the larger address-space available on 64-bit Windows and reduce the chance of running out of memory. </LI>
<LI>Improved application support—PIX has improved support for applications that call both Direct3D 9 and Direct3D 10. It is also able to safely capture data from multithreaded applications. </LI>
<LI>Output Messages tab—A new Output Messages tab is available that better exposes debug output from the Render, Mesh, and Shader views. You can filter messages by view and sort them by event ID, timestamp, and type, as well as copy them to the clipboard. </LI>
<LI>Render tab—As you move through the draw calls in a run file, the Render view continues to display the back buffer on Present calls, but for other calls it now displays the currently-bound render target. Also, the status bar now shows the surface format information. In addition, pencil icons appear on all the tabs of surfaces or textures that are currently bound to the device as render targets. </LI>
<LI>Buffer Data view—The performance of the Buffer Data is significantly improved and large buffers are no longer painfully slow to examine. </LI></FONT><FONT size=2></UL></DIR>
<P>And here's a more detailed <A class="" href="http://www.gamasutra.com/view/feature/3728/sponsored_feature_whats_new_in_.php" mce_href="http://www.gamasutra.com/view/feature/3728/sponsored_feature_whats_new_in_.php">article on Gamasutra</A> on new PIX functionality in the June 2008 and August 2008 versions of PIX.</P>
<P mce_keep="true">&nbsp;</P>
<P>-Mike</P></FONT>]]></content:encoded>
    </item>
    <item>
      <title>My First XNA Community Game</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/my-first-xna-community-game</link>
      <pubDate>Wed, 25 Jun 2008 03:04:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/06/25/my-first-xna-community-game/</guid>
      <description><![CDATA[I've been working on a little arcade game to submit to the XNA Creators Club Community Games Beta....]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>I've been working on a little arcade game to submit to the <A class="" href="http://creators.xna.com/" mce_href="http://creators.xna.com/">XNA Creators Club</A> Community Games Beta. I finally finished it last weekend and submitted it Sunday night. It passed Peer Review and went live a few hours ago -- woohoo! It's not the most amazing game of all time, but it was still a lot of work to get all the details finished. I have ideas for more ambitious games to tackle soon...</P>
<P>Check out "<A class="" href="http://catalog.xna.com/GameDetails.aspx?releaseId=173" mce_href="http://catalog.xna.com/GameDetails.aspx?releaseId=173">Gemini 08</A>" in the <A class="" href="http://catalog.xna.com/gamescatalog.aspx" mce_href="http://catalog.xna.com/gamescatalog.aspx">Games Catalog</A>. Anyone with an XNA Creators Club <A class="" href="http://creators.xna.com/en-us/membership" mce_href="http://creators.xna.com/en-us/membership">Premium Membership</A> can play it on the Xbox 360 for free! If you have feedback on the game, you can submit it <A class="" href="http://forums.xna.com/forums/t/13405.aspx" mce_href="http://forums.xna.com/forums/t/13405.aspx">here</A>.</P>
<P><IMG style="WIDTH: 640px; HEIGHT: 360px" height=360 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/65/14/17/Gemini_3.png" original-url="http://blogs.msdn.com/photos/manders/images/8651417/640x360.aspx" width=640 mce_src="http://blogs.msdn.com/photos/manders/images/8651417/640x360.aspx"></P>
<P>For more info about the XNA Creators Club and the Community Games Beta, check out <A class="" href="http://creators.xna.com/en-US/tour_detail" mce_href="http://creators.xna.com/en-US/tour_detail">this tour</A>.</P>
<P>-Mike</P></FONT>]]></content:encoded>
    </item>
    <item>
      <title>Polygonal Tessellation</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/polygonal-tessellation</link>
      <pubDate>Tue, 15 Apr 2008 03:21:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/04/15/polygonal-tessellation/</guid>
      <description><![CDATA[Now that I've got the hang of quickly drawing lots of shapes with shader instancing, what should I...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>Now that I've got the hang of quickly drawing lots of shapes with <A class="" href="http://blogs.msdn.com/manders/archive/2008/04/06/it-s-full-of-stars-xna-2-d-shader-instancing.aspx" mce_href="http://blogs.msdn.com/manders/archive/2008/04/06/it-s-full-of-stars-xna-2-d-shader-instancing.aspx">shader instancing</A>, what should I draw? All that geometric repetition got me thinking about the <A class="" href="http://www.mcescher.com/Gallery/gallery-symmetry.htm" mce_href="http://www.mcescher.com/Gallery/gallery-symmetry.htm">artwork</A> of <A class="" href="http://en.wikipedia.org/wiki/M._C._Escher" mce_href="http://en.wikipedia.org/wiki/M._C._Escher">M. C. Escher</A>, which led me to <A class="" href="http://euler.slu.edu/escher/index.php/Tessellations_by_Polygons" mce_href="http://euler.slu.edu/escher/index.php/Tessellations_by_Polygons">this page</A> about regular and semi-regular tessellation. The diagrams near the bottom looked nice, so I started playing with drawing similar images with <A class="" href="http://creators.xna.com/" mce_href="http://creators.xna.com/">XNA</A> code.</P>
<P>Regular tessellations are tessellations of the plane with a single regular polygon. This is only possible with triangles, squares, and hexagons. Things get more interesting when you allow multiple kinds of polygons. Semiregular tessellations have the same sequence of polygons around each vertex, such as (4, 6, 12) for a square, hexagon, and dodecagon. There are eight possible semiregular tessellations.</P>
<P>To draw these, I first needed to make meshes for various polygons: squares, hexagons, octagons, etc. And I needed to be able to move and rotate them around relative to each other. Rather than gobs of the usual sines and cosines, I thought I'd try using <A class="" href="http://en.wikipedia.org/wiki/Turtle_graphics" mce_href="http://en.wikipedia.org/wiki/Turtle_graphics">turtle graphics</A>, like the old Logo programming language. Here's my quick and dirty turtle class:</P></FONT><FONT face="Courier New" size=2>
<P></FONT><FONT face="Courier New" color=#0000ff size=2>public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>class</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Turtle<BR></FONT><FONT face="Courier New" size=2>{<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector2</FONT><FONT face="Courier New" size=2> pos = </FONT><FONT face="Courier New" color=#2b91af size=2>Vector2</FONT><FONT face="Courier New" size=2>.Zero;<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2> theta = 0;<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>void</FONT><FONT face="Courier New" size=2> Reset()<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>{<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>pos = </FONT><FONT face="Courier New" color=#2b91af size=2>Vector2</FONT><FONT face="Courier New" size=2>.Zero;<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>theta = 0;<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>}<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>void</FONT><FONT face="Courier New" size=2> Fwd(</FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2> amt)<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>{<BR></FONT><FONT face="Courier New" color=#2b91af size=2><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>Vector2</FONT><FONT face="Courier New" size=2> delta = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector2</FONT><FONT face="Courier New" size=2>(amt * (</FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2>)</FONT><FONT face="Courier New" color=#2b91af size=2>Math</FONT><FONT face="Courier New" size=2>.Cos(theta), amt * (</FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2>)</FONT><FONT face="Courier New" color=#2b91af size=2>Math</FONT><FONT face="Courier New" size=2>.Sin(theta));<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>pos += delta;<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>}<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>void</FONT><FONT face="Courier New" size=2> RotRadians(</FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2> radians)<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>{<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>theta += radians;<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>}<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>void</FONT><FONT face="Courier New" size=2> RotDegrees(</FONT><FONT face="Courier New" color=#0000ff size=2>float</FONT><FONT face="Courier New" size=2> degrees)<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>{<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>RotRadians(</FONT><FONT face="Courier New" color=#2b91af size=2>MathHelper</FONT><FONT face="Courier New" size=2>.ToRadians(degrees));<BR><FONT color=#0000ff>&nbsp;&nbsp;&nbsp; </FONT>}<BR></FONT><FONT face="Courier New" size=2>}</P></FONT><FONT size=2>
<P>So that was handy, both for creating polygons and for placing them relative to each other. Note that the origin of the polygon meshes is at one of the vertices, rather than in the center. That made it easy to place several polygons that share a vertex. (That is, they mathematically share a vertex in the tessellation...they don't share Direct3D vertices when they are rendered).</P>
<P>I separated my mesh code from the InstancedShapeManager, so I could pass various different polygons to a single shape manager for drawing. I still kept separate lists for all the squares, all the hexagons, etc., and draw all polygons of a given type at once.</P>
<P>The actual algorithm for generating the tessellations is a bit of a hack. I start at a vertex and rotate around, placing polygons as specified by the tessellation type. For example, for a (3, 4, 6, 4) tessellation, I would place a triangle, rotate 60 degrees, place a square, rotate 90 degrees, place a hexagon, rotate 120 degrees, and place a square. After placing each polygon, I'd enqueue a "task" to handle the vertex one unit away from the current vertex at the current orientation. When the current vertex was finished, I'd dequeue the next task and repeat the process. The neighboring vertices would have to start from a different orientation, which I called the "magic rotation" value and determined by trial and error. There's probably a simple way to derive it, but I didn't come up with it.</P>
<P>I had to add code to keep from re-adding vertices which were already processed (or in the task queue), and also to keep from placing polygons where one had already been placed.</P>
<P>I show square markers to represent unfinished vertex tasks. The green marker shows the current task, and red markers are enqueued tasks. When a task is complete, I remove the marker.</P>
<P>I use arbitrary hues to color each polygon -- again, there's probably a clever algorithm for coloring them such that adjacent polys are not the same color -- but I didn't go there.</P>
<P>The result is fun to watch, as the tessellation grows from the center point.</P>
<P mce_keep="true"><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/39/68/56/Tessellation_1.png" original-url="http://blogs.msdn.com/photos/manders/images/8396856/640x373.aspx" mce_src="http://blogs.msdn.com/photos/manders/images/8396856/640x373.aspx"></P>
<P><IMG style="WIDTH: 640px; HEIGHT: 373px" height=373 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/39/68/60/Tessellation_2.png" original-url="http://blogs.msdn.com/photos/manders/images/8396860/640x373.aspx" width=640 mce_src="http://blogs.msdn.com/photos/manders/images/8396860/640x373.aspx"></P>
<P>I didn't get some of the tessellations working, such as (4, 6, 12). With these, some vertices are (4, 6, 12) and others are (12, 6, 4). I was able to cope with that, but then I couldn't come up with magic rotation values to make the subsequent polygons appear in the right place. Oh well.</P>
<P>I'm probably not going to do anything further with this tessellation code (though it might be fun to try some <A class="" href="http://euler.slu.edu/escher/index.php/Hyperbolic_Geometry" mce_href="http://euler.slu.edu/escher/index.php/Hyperbolic_Geometry">hyperbolic tessellations</A>), but it was fun to make some pretty pictures.</P>
<P mce_keep="true">&nbsp;</P>
<P>-Mike</P></FONT>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/39/68/83/Tessellation_1_0.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-08-39-68-83/Tessellation_5F00_1_5F00_0.zip">Tessellation_1_0.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Debugging with PIX for Windows</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/debugging-with-pix-for-windows</link>
      <pubDate>Tue, 08 Apr 2008 23:29:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/04/08/debugging-with-pix-for-windows/</guid>
      <description><![CDATA[I haven't written anything about PIX in a while. But here's a great article written by Brian...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>I haven't written anything about PIX in a <A class="" href="http://blogs.msdn.com/manders/archive/2006/12/15/a-painless-introduction-to-pix-for-windows.aspx" mce_href="http://blogs.msdn.com/manders/archive/2006/12/15/a-painless-introduction-to-pix-for-windows.aspx">while</A>. But <A class="" href="http://www.garagegames.com/blogs/38050/14251" mce_href="http://www.garagegames.com/blogs/38050/14251">here's a great article</A> written by Brian Richardson at <A class="" href="http://www.garagegames.com/" mce_href="http://www.garagegames.com/">GarageGames</A> about using PIX to track down a rendering issue. Nice job, Brian!</P>
<P>You can find the latest version of PIX in the <A class="" href="http://www.microsoft.com/downloads/details.aspx?FamilyId=572BE8A6-263A-4424-A7FE-69CFF1A5B180&amp;displaylang=en" mce_href="http://www.microsoft.com/downloads/details.aspx?FamilyId=572BE8A6-263A-4424-A7FE-69CFF1A5B180&amp;displaylang=en">March 2008 DirectX SDK</A>.</P>
<P>&nbsp;-Mike</P></FONT>]]></content:encoded>
    </item>
    <item>
      <title>It's full of stars: XNA 2-D Shader Instancing</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/its-full-of-stars-xna-2-d-shader-instancing</link>
      <pubDate>Mon, 07 Apr 2008 01:37:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/04/07/its-full-of-stars-xna-2-d-shader-instancing/</guid>
      <description><![CDATA[A lot of my XNA-based programs draw many instances of the same mesh (such as RoundLines), with...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>A lot of my <A class="" href="http://creators.xna.com/" mce_href="http://creators.xna.com/">XNA</A>-based programs draw many instances of the same mesh (such as <A class="" href="http://blogs.msdn.com/manders/archive/2007/01/07/lines-2d-thick-rounded-line-segments-for-xna-programs.aspx" mce_href="http://blogs.msdn.com/manders/archive/2007/01/07/lines-2d-thick-rounded-line-segments-for-xna-programs.aspx">RoundLines</A>), with each instance having different position, rotation, etc. I had been submitting these meshes to Direct3D one instance at a time, which is not great for performance. Instanced drawing is a good way to get many copies of simple meshes on-screen faster, but it takes a bit of extra work. It's not too difficult, though.</P>
<P>The <A class="" href="http://creators.xna.com/Headlines/developmentaspx/archive/2007/01/01/Mesh-Instancing.aspx" mce_href="http://creators.xna.com/Headlines/developmentaspx/archive/2007/01/01/Mesh-Instancing.aspx">Instanced Model Sample</A> is a great place to start learning about instancing. It's a little intimidating, though, to learn that there are three ways to do instancing, and they all require slightly different code and have different hardware requirements.</P>
<P>&nbsp; 
<TABLE class="" cellSpacing=0 cellPadding=0 border=1>
<TBODY>
<TR>
<TD class="" vAlign=top width=160>
<P mce_keep="true">&nbsp;</P></TD>
<TD class="" vAlign=top width=160>
<P>Hardware Instancing</P></TD>
<TD class="" vAlign=top width=160>
<P>Shader Instancing</P></TD>
<TD class="" vAlign=top width=160>
<P>VFetch Instancing</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Works on Xbox?</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Works on Windows?</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes (if vs_3_0)</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Need multiple streams?</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Need modified vdecl?</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Need replicated VB?</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Need replicated IB?</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Need inline asm?</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>No</P></TD>
<TD class="" vAlign=top width=160>
<P>Yes</P></TD></TR>
<TR>
<TD class="" vAlign=top width=160>
<P>Max instances/draw?</P></TD>
<TD class="" vAlign=top width=160>
<P>Thousands</P></TD>
<TD class="" vAlign=top width=160>
<P>50-200</P></TD>
<TD class="" vAlign=top width=160>
<P>50-200</P></TD></TR></TBODY></TABLE></P>
<P>If you want maximum performance in every possible situation, you should probably implement all three methods. But, being a bit of a minimalist, I'd rather pick one method that <A class="" href="http://en.wikipedia.org/wiki/KISS_principle" mce_href="http://en.wikipedia.org/wiki/KISS_principle">works pretty well</A> in all situations. The "happy medium" method seems to be shader instancing. It works on both Windows and Xbox, it doesn't require multiple vertex streams or inline assembly, and gives good performance. Hardware instancing and VFetch instancing have some advantages on their respective platforms, but shader instancing is giving me the speed boost I need without a lot of extra code paths.</P>
<P>In the Instanced Model Sample, a full 4x4 transformation matrix is specified for each instance. Since there are only 256 float4 registers available to the vertex shader, this limits the number of instances per draw to about 60. You can often get away with much less per-instance data, especially in 2-D. In my test program, I pass just four float values per instance: x, y, rotation, and <A class="" href="http://en.wikipedia.org/wiki/HSV_color_space" mce_href="http://en.wikipedia.org/wiki/HSV_color_space">hue</A>. That all fits in a single float4, so I am submitting 200 instances per draw (I could have done closer to 256, but I want to leave room for some non-instance constant data). The per-instance data is going to vary depending on what functionality you want -- you might want a scaling factor or an alpha value, for example.</P>
<P>For shader instancing, the vertex declaration needs to have an element for the instance number, stored in a texture coordinate. The only other element I need for my program is position, so it is a pretty small vdecl:</P></FONT><FONT face="Courier New" size=2>
<P></FONT><FONT face="Courier New" color=#0000ff size=2>public</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#0000ff size=2>static</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElement</FONT><FONT face="Courier New" size=2>[] VertexElements = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElement</FONT><FONT face="Courier New" size=2>[] <BR>{<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElement</FONT><FONT face="Courier New" size=2>(0, 0, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementFormat</FONT><FONT face="Courier New" size=2>.Vector3, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementMethod</FONT><FONT face="Courier New" size=2>.Default, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementUsage</FONT><FONT face="Courier New" size=2>.Position, 0),<BR></FONT><FONT face="Courier New" color=#0000ff size=2>&nbsp;&nbsp;&nbsp; new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElement</FONT><FONT face="Courier New" size=2>(0, 12, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementFormat</FONT><FONT face="Courier New" size=2>.Single, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementMethod</FONT><FONT face="Courier New" size=2>.Default, </FONT><FONT face="Courier New" color=#2b91af size=2>VertexElementUsage</FONT><FONT face="Courier New" size=2>.TextureCoordinate, 0),<BR>};</P></FONT><FONT size=2>
<P>When creating a mesh, I need to repeat the vertex data for each instance, for a total of verticesPerInstance * numInstances vertices. Each instance's copy specifies its instance index. Here's the setup for quads:</P></FONT><FONT face="Courier New" size=2>
<P></FONT><FONT face="Courier New" color=#0000ff size=2>int</FONT><FONT face="Courier New" size=2> iVertex = 0;<BR></FONT><FONT face="Courier New" color=#0000ff size=2>for</FONT><FONT face="Courier New" size=2> (</FONT><FONT face="Courier New" color=#0000ff size=2>int</FONT><FONT face="Courier New" size=2> iInstance = 0; iInstance &lt; numInstances; iInstance++)<BR>{<BR>&nbsp;&nbsp;&nbsp; vertices[iVertex++] = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>MyVertexElement</FONT><FONT face="Courier New" size=2>(</FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector3</FONT><FONT face="Courier New" size=2>(-1, -1, 0), iInstance);<BR>&nbsp;&nbsp;&nbsp; vertices[iVertex++] = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>MyVertexElement</FONT><FONT face="Courier New" size=2>(</FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector3</FONT><FONT face="Courier New" size=2>(+1, -1, 0), iInstance);<BR>&nbsp;&nbsp;&nbsp; vertices[iVertex++] = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>MyVertexElement</FONT><FONT face="Courier New" size=2>(</FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector3</FONT><FONT face="Courier New" size=2>(+1, +1, 0), iInstance);<BR>&nbsp;&nbsp;&nbsp; vertices[iVertex++] = </FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>MyVertexElement</FONT><FONT face="Courier New" size=2>(</FONT><FONT face="Courier New" color=#0000ff size=2>new</FONT><FONT face="Courier New" size=2> </FONT><FONT face="Courier New" color=#2b91af size=2>Vector3</FONT><FONT face="Courier New" size=2>(-1, +1, 0), iInstance);<BR>}</P></FONT><FONT size=2>
<P>The index data needs to be repeated as well, though you don't need to store the instance index here:</P></FONT><FONT face="Courier New" size=2>
<P></FONT><FONT face="Courier New" color=#0000ff size=2>int</FONT><FONT face="Courier New" size=2> iIndex = 0;<BR></FONT><FONT face="Courier New" color=#0000ff size=2>for</FONT><FONT face="Courier New" size=2> (</FONT><FONT face="Courier New" color=#0000ff size=2>int</FONT><FONT face="Courier New" size=2> iInstance = 0; iInstance &lt; numInstances; iInstance++)<BR>{<BR></FONT><FONT face="Courier New" color=#0000ff size=2><FONT color=#000000>&nbsp;&nbsp;&nbsp; </FONT>int</FONT><FONT face="Courier New" size=2> iVertexBase = iInstance * 4;<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 1);<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 2);<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 3);<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 3);<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 0);<BR>&nbsp;&nbsp;&nbsp; indices[iIndex++] = (</FONT><FONT face="Courier New" color=#0000ff size=2>short</FONT><FONT face="Courier New" size=2>)(iVertexBase + 1);<BR>}</P></FONT><FONT size=2>
<P>The replication feels kind of wasteful, but for these tiny shapes you still end up with a very small VB and IB.</P>
<P>Before drawing, set the per-instance data into an array, then hand it to the instanceData effect parameter.</P>
<P>In the vertex shader, use the index to pull the per-instance data out of the instanceData array:</P></FONT><FONT face="Courier New" size=2>
<P>VS_OUTPUT MyVS(<BR>&nbsp;&nbsp;&nbsp; float4 Pos : POSITION,&nbsp;<BR>&nbsp;&nbsp;&nbsp; float Index : TEXCOORD0 )<BR>{<BR>&nbsp;&nbsp;&nbsp; VS_OUTPUT Out = (VS_OUTPUT)0;<BR>&nbsp;&nbsp;&nbsp; float tx = instanceData[Index].x;<BR>&nbsp;&nbsp;&nbsp; float ty = instanceData[Index].y;<BR>&nbsp;&nbsp;&nbsp; float rot = instanceData[Index].z;<BR>&nbsp;&nbsp;&nbsp; float hue = instanceData[Index].w;<BR>&nbsp;&nbsp;&nbsp; ...<BR></FONT><FONT size=2>}</P>
<P>That's basically it! You can see in my Draw() function how I break up the shapeList into batches of 200, since that's the maximum number I can draw at a time. This is still a huge speedup over drawing them one at a time.</P>
<P>In the demo, I draw <A class="" href="http://en.wikipedia.org/wiki/Hexagram" mce_href="http://en.wikipedia.org/wiki/Hexagram">hexagrams</A> just to show you can do something more interesting than quads. And I do some translation, rotation, and color cycling to exercise the per-instance information.</P>
<P><IMG style="WIDTH: 640px; HEIGHT: 373px" height=373 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/36/44/66/InstancedShapeDemo_Closeup.png" original-url="http://blogs.msdn.com/photos/manders/images/8364466/640x373.aspx" width=640 mce_src="http://blogs.msdn.com/photos/manders/images/8364466/640x373.aspx"></P>
<P>I'm using <A class="" href="http://msdn2.microsoft.com/en-us/library/microsoft.xna.framework.gametime.isrunningslowly.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/microsoft.xna.framework.gametime.isrunningslowly.aspx">GameTime.IsRunningSlowly</A> to tell if I'm doing so much work per frame that the system can't keep up with the target rate (60fps). On the Xbox I can maintain 60fps with up to around 15,000 instances. I suspect that there is more graphics power still going unused...perhaps I am causing too much garbage collection to render more. On the PC, I can do over 40,000 instances -- your mileage may vary depending on your hardware configuration, of course.</P>
<P>Instancing is great...I'll be using it a lot in my drawing code from now on.</P>
<P>-Mike</P></FONT>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/36/44/79/InstancedShapeDemo_1_0.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-08-36-44-79/InstancedShapeDemo_5F00_1_5F00_0.zip">InstancedShapeDemo_1_0.zip</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Wendybrot Revisited: A Better XNA Mandelbrot Set Explorer</title>
      <link>https://docs.microsoft.com/archive/blogs/manders/wendybrot-revisited-a-better-xna-mandelbrot-set-explorer</link>
      <pubDate>Thu, 20 Mar 2008 03:59:00 GMT</pubDate>
      <dc:creator><![CDATA[Michael Anderson]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/manders/2008/03/20/wendybrot-revisited-a-better-xna-mandelbrot-set-explorer/</guid>
      <description><![CDATA[(Insert standard gosh-I-haven't-blogged-in-a-while blurb here)
(UPDATE: If you get an...]]></description>
      <content:encoded><![CDATA[<FONT size=2>
<P>(Insert standard gosh-I-haven't-blogged-in-a-while <A class="" href="http://search.live.com/results.aspx?q=long+time+no+blog+update+sorry" mce_href="http://search.live.com/results.aspx?q=long+time+no+blog+update+sorry">blurb</A> here)</P>
<P><EM>(UPDATE: If you get an out-of-video-memory exception when starting the program, find the line of code that says "public const int numNodesToAllocate = 250;" and change 250 to a smaller number.)</EM></P>
<P>I decided to improve my old <A class="" href="http://creators.xna.com/" mce_href="http://creators.xna.com">XNA</A> <A class="" href="http://blogs.msdn.com/manders/archive/2006/11/28/wendybrot-1-1-faster-more-support-cleaner-code.aspx" mce_href="http://blogs.msdn.com/manders/archive/2006/11/28/wendybrot-1-1-faster-more-support-cleaner-code.aspx">Mandelbrot set explorer</A> to take advantage of the GPU horsepower available on PCs and the Xbox. It was a fair bit of work, but the end result is much more interactive and fun to use than the old version.</P>
<P><IMG style="WIDTH: 640px; HEIGHT: 373px" height=373 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/32/70/94/Wendybrot2.png" original-url="http://blogs.msdn.com/photos/manders/images/8327094/640x373.aspx" width=640 mce_src="http://blogs.msdn.com/photos/manders/images/8327094/640x373.aspx"></P>
<P>The core Mandelbrot set <A class="" href="http://en.wikipedia.org/wiki/Mandelbrot_set" mce_href="http://en.wikipedia.org/wiki/Mandelbrot_set">algorithm</A> is a natural fit for a pixel shader. The harder part is making it incremental. Once you're zoomed in a fair bit, it can take hundreds or thousands of iterations to determine if a point is in the set or not, and it's more fun if you can zoom and pan around while progressively improving the image. So you want a program that can iterate a little, then move and draw, then iterate a little more. That is a little off the beaten path of the standard rendering pipeline.</P>
<P>I decided to use render-to-texture and ping-pong between two 256x256 textures. So for the first frame, the shader reads from texture A, computes some iterations, and writes to texture B. Then texture B is rendered onscreen. Then in the next frame, the shader reads from texture B, computes some further iterations, and writes to texture A. Then texture A is rendered onscreen. Then we repeat the whole process. (Life would be easier if we could read to and write to the same texture, but Direct3D doesn't work that way).</P>
<P>But instead of there just being one texture A and one texture B, there are a whole lot of them. The two-dimensional space is subdivided with a quadtree, and each node in the quadtree has a "texture A" and a "texture B." The program both iterates on and draws all visible nodes, every frame.</P>
<P>Also, what I'm calling "texture A" for one node is actually more than one Direct3D texture. I need to maintain four pieces of state for each texel: the current calculated "x", the current calculated "y", the iteration count, and whether the point is "done." Ideally I'd use floats for "x" and "y", a uint for "iterations", and a boolean for "done." SurfaceFormat.Vector4 would have been sufficient (albeit a bit wasteful), but the Xbox <A class="" href="http://msdn2.microsoft.com/en-us/library/bb447675.aspx" mce_href="http://msdn2.microsoft.com/en-us/library/bb447675.aspx">doesn't support it</A>. I decided to use three SurfaceFormat.Single textures, and encode "done" into the iterations count by negating the value if the texel is done.</P>
<P>I had never tried using multiple render targets (MRT) before, but it worked fine. Instead of having the pixel shader output a float4, it needs to output a structure with values for each render target:</P></FONT><FONT face="Courier New" size=2>
<P>// MRT output. Even though each RT only has a red channel,<BR>// we have to write out a four-component vector to each RT.<BR>struct PS_OUTPUT<BR>{<BR>&nbsp;&nbsp;&nbsp; float4 x : COLOR0;<BR>&nbsp;&nbsp;&nbsp; float4 y : COLOR1;<BR>&nbsp;&nbsp;&nbsp; float4 iterations : COLOR2;<BR>};</P></FONT><FONT size=2>
<P>The shader also needs to read from three textures, but that's just a matter of using three samplers, with a different texture bound to each sampler:</P></FONT><FONT face="Courier New" size=2>
<P>&nbsp;&nbsp;&nbsp; float4 colorData = tex2D(dataSamp, pos);<BR>&nbsp;&nbsp;&nbsp; float4 colorData1 = tex2D(dataSamp1, pos);<BR>&nbsp;&nbsp;&nbsp; float4 colorData2 = tex2D(dataSamp2, pos);<BR>&nbsp;&nbsp;&nbsp; float x = colorData.r;<BR>&nbsp;&nbsp;&nbsp; float y = colorData1.r;<BR>&nbsp;&nbsp;&nbsp; float iterations = colorData2.r;<BR>&nbsp;&nbsp;&nbsp; bool done = false;<BR>&nbsp;&nbsp;&nbsp; if( iterations &lt; 0 )<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; done = true;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iterations = -iterations;<BR>&nbsp;&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp;&nbsp; (Not shown here: update x, y, iterations, done)<BR><BR>&nbsp;&nbsp;&nbsp; if( done )<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; iterations = -iterations;&nbsp;<BR>&nbsp;&nbsp;&nbsp; PS_OUTPUT output;<BR>&nbsp;&nbsp;&nbsp; output.x = float4(x,0,0,0);<BR>&nbsp;&nbsp;&nbsp; output.y = float4(y,0,0,0);<BR>&nbsp;&nbsp;&nbsp; output.iterations = float4(iterations,0,0,0);<BR>&nbsp;&nbsp;&nbsp; return output;<BR></FONT><FONT size=2></P>
<P>Okay, so each node consists of six 256x256 32 bpp textures, or 1.5MB of texture memory. How many of these nodes do we need around, to be able to view a frame at full screen resolution? At 1280x720, there are generally about 50 to 100 nodes recalculated per frame. Since we want to zoom and pan around, it's helpful to have a few extra nodes. So I allocate a pool of 250 nodes up front when the program starts, and purge least-recently-used nodes when I need fresh ones for newly-exposed areas of the screen. So, yeah, that's 375MB of textures altogether, and 75-150MB of that is read/written per frame. That's a lot of texture memory to eat up, especially on an Xbox, but it works.</P>
<P>There are actually three key pixel shaders. One is used to set the initial values for x, y, and iterations/done when a node is initialized. A second shader does the iterations. A third shader just reads the iterations/done info and translates it into a color for display.</P>
<P>You can use the "B" button to tell the "CalcCore" pixel shader to perform 1, 4, or 16 iterations per pixel per frame. More iterations makes a given node calculate faster, and is nice if you're not panning/zooming, but the frame rate suffers a bit. As with previous versions of Wendybrot, you can use the right stick to play around with the color palette.</P>
<P>The attached zip file contains all the source code, plus prebuilt ccgame packages for both Xbox and Windows. You can undefine "USE_RTT" in the source code to make Wendybrot use the old method of performing the iterations on the CPU instead of with the pixel shader -- but it's much slower! Also, you can try rendering at 1920x1080, but the frame rate struggles a bit here. Still, it's staggering to think about how much math and memory transfer is happening per frame.</P>
<P>I hope you enjoy playing with the program and learn a few things from the code.</P>
<P>-Mike</P></FONT>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/08/32/71/01/Wendybrot_2_0.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-08-32-71-01/Wendybrot_5F00_2_5F00_0.zip">Wendybrot_2_0.zip</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>