<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Windows Presentation Foundation-3D Musings : Shader</title><link>http://blogs.msdn.com/pantal/archive/tags/Shader/default.aspx</link><description>Tags: Shader</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Premiere Post! WPF w/ Direct3D Shader Interop and simple databinding</title><link>http://blogs.msdn.com/pantal/archive/2007/03/29/premiere-post-wpf-direct3d-interop.aspx</link><pubDate>Thu, 29 Mar 2007 06:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1983526</guid><dc:creator>pantal</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/pantal/comments/1983526.aspx</comments><wfw:commentRss>http://blogs.msdn.com/pantal/commentrss.aspx?PostID=1983526</wfw:commentRss><description>&lt;P&gt;Based on some questions I’ve seen on the forums, with regards to WPF on the issue of Shader use and technologies, I have decided to prepare a sample which illustrates one approach by which DirectX content can be embedded in a WPF app.&amp;nbsp;In particular, I render a single Managed DirectX surface containing a&amp;nbsp;simple mesh rendered with shaders,&amp;nbsp;&amp;nbsp;in a control embedded in a WPF app, and databound to a set of sliders to manipulate a color shift implemented via a pixel shader. This form can be helpful for teams that want to incorporate WPF as the overall UI technology(in place of GDI,&amp;nbsp;MFC, winforms, etc)&amp;nbsp;of their 3D heavy applications, for which a fair bit of DirectX code is often present.&lt;/P&gt;
&lt;P&gt;In this example, I used Managed DirectX API for primary language consistency&amp;nbsp;in this sample, but the HwndHost technology for interop can similarly be used to embed unmanaged DX 9/10. For this overview, I assume an working knowledge of Direct3D, in conjunction with some familiarity with WPF.&lt;/P&gt;
&lt;P&gt;&lt;A class=imagelink title=Screenshot.jpg href="http://www.recursim.com/wp-content/uploads/2007/03/Screenshot.jpg" mce_href="http://www.recursim.com/wp-content/uploads/2007/03/Screenshot.jpg"&gt;&lt;IMG id=image743 height=96 alt=Screenshot.jpg src="http://www.recursim.com/wp-content/uploads/2007/03/Screenshot.thumbnail.jpg" mce_src="http://www.recursim.com/wp-content/uploads/2007/03/Screenshot.thumbnail.jpg"&gt;&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1-The application references&amp;nbsp;a custom control containing the Manged DX Device in the XAML layout(For non XAML junkies, keep in mind, this can just as easily be done in code):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Custom Namespaces:&lt;BR&gt;&lt;FONT color=#ff0000 size=2&gt;&amp;nbsp; xmlns:local&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;clr-namespace:WPF_MDX_Interop_App&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;&amp;nbsp; xmlns:MDXControl&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;clr-namespace:WpfNuggets.ManagedDirectX;assembly=WpfNuggets.ManagedDirectX&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;…&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt; &lt;FONT color=#800000 size=2&gt;MDXControl:ManagedDirectXControl&lt;FONT color=#0000ff size=2&gt;&amp;gt;&lt;BR&gt;&amp;nbsp; &amp;lt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;MDXControl:ManagedDirectXControl.RenderContent&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;local:AdjustableShader&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;x:Name&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;AdjustableShader&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;TextureFilename&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;Content\gradient1.bmp&lt;/FONT&gt;&lt;FONT size=2&gt;“&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;/&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;MDXControl:ManagedDirectXControl.RenderContent&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#800000 size=2&gt;MDXControl:ManagedDirectXControl&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;2-Here’s the code for the custom control - this is a WindowsFormsHost which allows us to contain Winforms content&amp;nbsp;in WPF. The Xaml visible RenderContent field is used here for encapsulating the rendering behavior&amp;nbsp;we want to execute in DirectX:&lt;FONT color=#0000ff size=2&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;public&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;class&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ManagedDirectXControl&lt;/FONT&gt;&lt;FONT size=2&gt; : &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;WindowsFormsHost&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;{&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; public&lt;/FONT&gt;&lt;FONT size=2&gt; ManagedDirectXControl()&lt;BR&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Child = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;new&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ManagedDirectXDeviceWrapper&lt;/FONT&gt;&lt;FONT size=2&gt;();&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp; public&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;Renderable&lt;/FONT&gt;&lt;FONT size=2&gt; RenderContent&lt;BR&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;get&lt;/FONT&gt;&lt;FONT size=2&gt; { &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;return&lt;/FONT&gt;&lt;FONT size=2&gt; ((&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ManagedDirectXDeviceWrapper&lt;/FONT&gt;&lt;FONT size=2&gt;)Child).RenderContent; }&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set&lt;/FONT&gt;&lt;FONT size=2&gt; { ((&lt;/FONT&gt;&lt;FONT color=#008080 size=2&gt;ManagedDirectXDeviceWrapper&lt;/FONT&gt;&lt;FONT size=2&gt;)Child).RenderContent = &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;value&lt;/FONT&gt;&lt;FONT size=2&gt;; }&lt;BR&gt;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;3-The ManagedDirectXDeviceWrapper code which is contained in the project attached below, is pretty routine DirectX code for initialization and a rendering loop tied to repaint events on the container control. There are many fine tutorials on that subject, so I won't reinvent that&amp;nbsp;wheel of guidance.&amp;nbsp;The wrapper will&amp;nbsp;check&amp;nbsp;for HW PS &amp;amp; VS shaders V1.1, and will silently fall back to software mode. If you would like active notification of the fallback, look at the “&lt;/FONT&gt;&lt;FONT size=2&gt;_alreadyNagged” field in that class. This class contains hooks which depend on the&amp;nbsp;user supplied&amp;nbsp;Renderable object.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;4-Finally, the implementation of “ShaderRender” supplies a generic Quad mesh which gets textured and processed by user supplied shaders. The derived class “AdjustableShader” pre-defines a reference to a particular shader file, and exposes R,G,B properties for data binding use as targets, which provide parameters which are passed on to the pixel shader. Again, the mechanics of this is familiar territory in the DirectX world, so I'm not delving into that aspect here.&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt; &lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT size=2&gt;You can get&amp;nbsp;the&amp;nbsp;VS Solution with projects for the Managed DX wrapper project,&amp;nbsp;and a WPF&amp;nbsp;sample app containing simple data binding to drive the pixel shader inputs for shifting the texture color.&amp;nbsp;That code is available &lt;A class="" id=p744 title="WPF DX Interop" href="http://www.recursim.com/wp-content/uploads/2007/03/WPF%20DX%20Interop.zip" rel=attachment mce_href="http://www.recursim.com/wp-content/uploads/2007/03/WPF%20DX%20Interop.zip"&gt;from here&lt;/A&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;To work on the project you will need the following installed(Combined pre-req's for DX and WPF):&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT size=2&gt;Visual Studio 2005&amp;nbsp;&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT size=2&gt;DirectX SDK&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT size=2&gt;WPF Runtime or be running on Vista&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT size=2&gt;Orcas CTP for Visual Studio 2005(to support WPF VS project)&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;I would be glad to hear any questions, suggestions or other feedback...&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1983526" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/pantal/archive/tags/WPF/default.aspx">WPF</category><category domain="http://blogs.msdn.com/pantal/archive/tags/DirectX/default.aspx">DirectX</category><category domain="http://blogs.msdn.com/pantal/archive/tags/Shader/default.aspx">Shader</category></item></channel></rss>