<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Robotic Pandas</title><subtitle type="html">On abstractions for building next generation user interfaces that utilize programmable GPUs to provide naturalistic and cinematic experiences.</subtitle><id>http://blogs.msdn.com/sean_mcdirmid/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/sean_mcdirmid/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-10-06T05:51:51Z</updated><entry><title>Designing a GPU-oriented geometry abstraction – Part Two.</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/11/21/designing-a-gpu-oriented-geometry-abstraction-part-two.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/11/21/designing-a-gpu-oriented-geometry-abstraction-part-two.aspx</id><published>2009-11-21T04:39:32Z</published><updated>2009-11-21T04:39:32Z</updated><content type="html">&lt;p&gt;My last post described the problem of crafting an appropriate geometry abstraction for Bling. Bling previously solved the code problem for vertex and pixel shading, but lacked decent geometry input abstractions as well as an abstraction that supported geometry shading. The last post proposes giving geometry its own abstraction, but I was a bit hesitant in including properties within the geometry, which leads to composition and addressing problems. I think its time to back up a bit and describe the abstractions that are relevant to a traditional programmable rendering pipeline:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;&lt;em&gt;Pixels &lt;/em&gt;&lt;/strong&gt;as children of geometric primitives. The position of a pixel is fixed and a pixel shader only specifies each pixel’s color and optional depth. It often makes sense to have other properties beyond color and depth at the pixel level for better shading computations; e.g., a normal that is computed on a per-pixel basis via a &lt;a href="http://en.wikipedia.org/wiki/Parametric_surface" target="_blank"&gt;parametric equation&lt;/a&gt; or via &lt;a href="http://en.wikipedia.org/wiki/Normal_mapping" target="_blank"&gt;normal mapping&lt;/a&gt;. Other properties can be accessed from previous vertex and geometry shading phases, where interpolation is used to determine the value of the property from the value of the property in the enclosing primitive’s vertices. Quality improves when properties are computed on a per-pixel basis rather than interpolated; e.g., consider &lt;a href="http://en.wikipedia.org/wiki/Phong_shading" target="_blank"&gt;Phong&lt;/a&gt; vs. &lt;a href="http://en.wikipedia.org/wiki/Gouraud_shading" target="_blank"&gt;Gouraud&lt;/a&gt; shading. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;em&gt;Vertices&lt;/em&gt;&lt;/strong&gt; as used to define primitives. A vertex shader specifies the position of each vertex, and will compute per-vertex properties that are later used in later geometry and pixel shading. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;em&gt;Primitive&lt;/em&gt;&lt;/strong&gt; geometries that are used to form complete geometries. A primitive is either of a point, line, or triangle topology, with triangle being the primitive that most commonly reaches the rendering stage. A primitive is defined by multiple vertices, and enclosing multiple pixels. Geometry shading works as a primitive translator, where one primitive can be translated into zero or more primitives of possibly a different topology. Primitives during geometry shading are defined as vertices. &lt;/li&gt;    &lt;li&gt;A &lt;strong&gt;&lt;em&gt;geometry&lt;/em&gt;&lt;/strong&gt; as a collection of primitives. Its initial definition is as a set of vertices, a topology for the vertices, and optionally an index buffer to allow for arbitrary vertex sharing between primitives, or an implicit adjacency relationship with explicit breaks (also used in geometry shading). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As described in the last post, we can define an abstraction for geometry that supports composition, duplication, and transformation. Ideally, rendering could then involve forming a geometry in a clean functional way through multiple compositions and transformations, and passing the resulting geometry into a render command. Transformations not only include modifying the layout of the geometry by rotating, scaling, and translating it, but also applying color and lighting to the geometry and the pixels contained within it, or whatever else is required for a complete rendering specification. Lighting could even be applied to the constituent parts of a geometry before they are composed. The various properties needed to make this possible include thinks like diffuse, specular, glass, and refractive materials as well as additional non-geometry constituents such as directional, ambient, and spot lights. Essentially, the geometry would then become a mini-&lt;a href="http://en.wikipedia.org/wiki/Scene_graph" target="_blank"&gt;scene graph&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Scene graphs are common in retained graphics APIs such as &lt;a href="http://blogs.msdn.com/wpf3d/" target="_blank"&gt;WPF 3D&lt;/a&gt; and &lt;a href="http://java.sun.com/javase/technologies/desktop/java3d/" target="_blank"&gt;Java 3D&lt;/a&gt;. Basically, a scene graph is a graph of the elements that affect scene rendering, and then becomes the basis for what is shown on the screen. Since we are only interested in what can be efficiently rendered in a shader pipeline, we have to keep the graph nodes mostly homogeneous: duplicating and transforming a geometry in the graph is fine, but composing completely different geometries with different lighting schemes is probably not going to work in the context of one rendering call (instead, render the geometries in separate rendering calls). &lt;/p&gt;  &lt;p&gt;The primary difference then with my previous geometry abstraction is that this new geometry embeds properties and transformations at every level of composition. A level in a geometric composition can omit or duplicate properties from a higher level, and transformations (e.g., lighting or layout) should input a property from the lowest level that it exists in the geometry that the transformation is attached to; e.g., a normal property at the pixel level is preferred from a normal property at the vertex level as it is more accurate for lighting computations. The problem with this approach is that we can lose static typing: a property might not exist at any level, then what? Right now I’m willing to live with dynamic checking since it will occur relatively early in Bling when code is generated at application startup. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9926650" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Designing a GPU-oriented geometry abstraction – Part One.</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/11/20/designing-a-gpu-oriented-geometry-abstraction-part-one.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/11/20/designing-a-gpu-oriented-geometry-abstraction-part-one.aspx</id><published>2009-11-20T04:46:16Z</published><updated>2009-11-20T04:46:16Z</updated><content type="html">&lt;p&gt;One the inputs of rendering via programmable shading on a modern graphics card is a collection of vertices associated with some per-vertex properties used in shader computations. When programming the GPU, this collection of vertices is commonly abstracted as a vertex buffer, which is essentially just a bag of bytes. The collection of vertices describes a primitive point, line, or triangle topology along with an optional index buffer (if triangle) that describes vertex sharing.&amp;#160; Again, the abstractions for describing these topologies are very weak and essentially amount to flags and arrays, although we can informally describe to the vertex buffer plus its primitive topology description as a &lt;em&gt;&lt;strong&gt;geometry&lt;/strong&gt;&lt;/em&gt;. Each vertex first individually undergoes processing in a vertex shader, and then (optionally) per-primitive (point, line, or triangle) in a &lt;em&gt;geometry shader&lt;/em&gt; where a new geometry can be specified with fewer or more vertices or even a completely different topology. After vertex and geometry shading, the position of each vertex must be specified. Finally, each pixel of the rasterized primitive is processed by a pixel shader to determine its color. This is a simplification, the GPU supports other features such as instancing, where we reuse the vertex buffer multiple times in a single rendering, and geometry shader stream out, where we save the result of vertex/geometry shading for later re-use.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://bling.codeplex.com/" target="_blank"&gt;Bling&lt;/a&gt; does a good job of abstracting vertices and pixels: in Bling shader code one value represents both the vertex and pixel, where the vertex position and pixel color are specified with respect to this value. Bling infers whether the code is referring to a vertex or pixel based on a simple dependency analysis: we are referring to a pixel whenever we refer to anything that is only available in the pixel shader, otherwise we are referring to a vertex and the computation result (if used in the pixel shader) will be interpolated based on the analogous results in the primitive’s other vertices. However, Bling’s vertex/pixel abstraction breaks down when dealing with geometry: the input of a a rendering is simply the vertex count and primitive data and Bling has no high-level geometry abstraction. Bling’s lack of a geometry abstraction prevents geometries from being manipulated, composed, and constructed at a high-level, and prevents geometry shaders from being expressed at all unless shader partitioning is expressed explicitly. And this is a pity because geometry shaders can do so many cool things such as dynamically forming fur, hair, feathers, sparkles, blobs, and so on. We definitely need to make manipulating geometries easier.&lt;/p&gt;  &lt;p&gt;So here is my new realization: give geometries their own abstraction in Bling, defined as one of the following:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The atomic case is a a collection of adjacent primitives. Actually, the only things that need to be specified for this primitive geometry is the number of vertices, vertex adjacency (if more than one primitive is defined), and primitive topology. Vertex properties such as positions and normals are not included as they will be inferred via dependency analysis within the shader. Examples: the face of a cube (four vertices to define two triangles) or the result of sampling a &lt;a href="http://en.wikipedia.org/wiki/Parametric_surface" target="_blank"&gt;parametric surface&lt;/a&gt; such as a sphere. &lt;/li&gt;    &lt;li&gt;A &lt;strong&gt;composition&lt;/strong&gt; of two different geometries (gn = g0 + g1). The geometries composed must be of the same primitive topology. Example: we could compose a face with a four simple triangles to form a pyramid. &lt;/li&gt;    &lt;li&gt;A &lt;strong&gt;duplication&lt;/strong&gt; of the same geometry N times (gn = N * g0). We define this separately from geometry composition as it could correspond (but might not depending on efficiency) to geometry instancing in the GPU API. For duplication to be meaningful, each duplicated geometry will always undergo a transformation based on the duplication index. Example: a face is duplicated four times to form a cube, or duplicating a cube N times to do some crude voxel rendering.&amp;#160; &lt;/li&gt;    &lt;li&gt;A &lt;strong&gt;transformation&lt;/strong&gt; of one geometry’s properties (gn = f(g0)). This is a bit difficult to define since there is no explicit common set of vertex properties, and indeed a vertex might not need a normal or even a position if it is not going to be rendered. However, the most common transformations will be on the position and normal, as the geometry is translated, scaled, and rotated. Example: rotating a face in 6 different ways to represent each face of a cube. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The benefit of this approach is immediately apparent: depending on our need we can define geometries mathematically or load them from a mesh. As properties aren’t included explicitly in the geometry, we can mix and match separate geometries as long as they share the same topology. We can then synthesize the index buffer (if needed) automatically, removing this burden from the user, and infer where geometry instancing occurs. &lt;/p&gt;  &lt;p&gt;Unfortunately, vertex properties are more difficult to specify. Before, each vertex had one vertex ID and an optional instance ID, which we would use to compute the vertex’s position and pixel’s color. These could then be used as indexes into a table (in the case of a mesh) to lookup initial values for properties like position and normal, or they could be used in more rich computations; e.g., computing a per-pixel normal from the derivative of a parametric surface. At any rate, we inferred what properties to include in the vertex buffer in a way that allowed an expressed computation to span the CPU and GPU, which is a very good feature from an optimization and reuse perspective.&amp;#160; Now if we allow geometries to be composed, duplicated, and transformed, vertex addressing obviously becomes much more difficult when inferring properties. There are few directions that we could go in to solve this problem:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;We could assign an ID to a vertex based on the order that it was defined in a composition, but often vertex properties are defined relative to their geometries, and such a scheme would basically ruin reuse of code of that computes properties for a certain composed geometry. &lt;/li&gt;    &lt;li&gt;We could come up with some hierarchical Id scheme. However, vertices are generally thrown all together (in the vertex shader at least), and as such it would be very difficult to reason about vertices whose IDs are not homogenous.&amp;#160; &lt;/li&gt;    &lt;li&gt;The other obvious option is to embed properties in geometries, which would solve the geometry reuse problem but then would tie geometries down to a common set of properties that they had to share in order to be combined. Yet this might be reasonable if the set of per-vertex properties are relatively fixed and it was easy to deal with exceptions easily. Whatever properties are declared do not necessarily correspond to what properties go in the vertex buffer; e.g., the position and normal properties of a sampled parametric surface can refer to dynamic CPU-only values (such as a slider’s position), where inference would synthesize the vertex buffer accordingly to include only static values. I’m still thinking about this problem but I guess I’m working in the direction of somehow re-introducing per-vertex properties, although I might have to throw away dynamic typing to make it work in C#’s type system (hopefully, generics will be enough). &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Any solution that I come with has to play friendly with geometry shaders, which have the ability transform geometries based on dynamic information. I’ll get into this with my next post. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9925991" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>What is a programmable abstraction?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/30/what-is-a-programmable-abstraction.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/30/what-is-a-programmable-abstraction.aspx</id><published>2009-07-30T07:50:43Z</published><updated>2009-07-30T07:50:43Z</updated><content type="html">&lt;p&gt;In a recent blog post I described the principles behind Bling’s design. The primary principle is preferring &lt;strong&gt;programmable&lt;/strong&gt; over &lt;strong&gt;fixed&lt;/strong&gt; abstractions. But I feel that my definition of a &lt;em&gt;programmable abstraction&lt;/em&gt; is so far unsatisfactory and undeveloped. To converge on a better definition, let’s first start at the beginning and discuss abstraction. &lt;/p&gt;   &lt;p&gt;The process of &lt;em&gt;&lt;a href="http://en.wikipedia.org/wiki/Abstraction_(computer_science)" target="_blank"&gt;abstraction&lt;/a&gt;&lt;/em&gt; (verb) is that of hiding details so that one can focus on fewer concepts at a time. An abstraction (noun) then is a hider of details. A &lt;a href="http://en.wikipedia.org/wiki/Programming_language" target="_blank"&gt;programming language&lt;/a&gt; provides various &lt;a href="http://en.wikipedia.org/wiki/Language_primitive" target="_blank"&gt;primitives&lt;/a&gt; for constructing custom abstractions, such as procedures, classes, functions, properties, or rules—let’s refer to these as &lt;em&gt;abstraction primitives&lt;/em&gt; to distinguish them from &lt;em&gt;computation primitives&lt;/em&gt;, which are used to build up computations that use or are hidden by abstractions. Using abstraction primitives, a &lt;a href="http://en.wikipedia.org/wiki/Library_(computer_science)" target="_blank"&gt;library&lt;/a&gt; then define and implement an &lt;a href="http://en.wikipedia.org/wiki/API" target="_blank"&gt;API&lt;/a&gt; as a set of abstractions that permit access to some kind of service. Of course, real life is often more complex: one or more languages can be involved in the definition of a library (e.g., WPF and C#, Visual Basic, XAML, and HLSL), while libraries will often be built from or to interoperate with abstractions from other libraries.&amp;#160; &lt;/p&gt;  &lt;p&gt;Libraries are increasingly defined as &lt;a href="http://en.wikipedia.org/wiki/Software_framework" target="_blank"&gt;frameworks&lt;/a&gt; that provide heavy large-grained abstractions for assembling systems in specific domains; for example consider a web framework (&lt;a href="http://rubyonrails.org/" target="_blank"&gt;Ruby on Rails&lt;/a&gt;) or an IDE framework (&lt;a href="http://www.eclipse.org" target="_blank"&gt;Eclipse&lt;/a&gt;). Programmers customize a framework’s abstractions from the top-down according to their needs and assemble them together according to a rigid &lt;a href="http://en.wikipedia.org/wiki/Software_architecture" target="_blank"&gt;architecture&lt;/a&gt;. The trade off of using a frameworks includes the learning curve of the framework’s abstractions and architecture, as well as a sacrifice in flexibility where the framework becomes difficult or impossible to program outside of its intended purpose. &lt;/p&gt;  &lt;p&gt;I am going to refer to framework abstractions as &lt;em&gt;fixed abstractions &lt;/em&gt;because they are analogous to abstractions used to program a GPU through a fixed-function graphics pipeline as opposed to the abstractions used to program a GPU through a programmable pipeline. In the past, GPUs encoded in hardware only various fixed functions to manipulate vertices, light pixels, and so on. To program the GPU, the programmer manipulated various abstractions that represent fixed GPU functions through an API such as DirectX or OpenGL. As with a framework, these fixed-function APIs allowed for some customization of abstractions from the top-down, come with rigid architectures, and sacrifice flexibility in what they can express. &lt;/p&gt;  &lt;p&gt;For the reason of flexibility alone, fixed-function pipelines are being rapidly replaced programmable pipelines that have the ability to express arbitrary custom effects through small programs known as &lt;a href="http://en.wikipedia.org/wiki/Shader" target="_blank"&gt;shaders&lt;/a&gt;, which run directly on the GPU. The programmable pipeline completely supersedes the fixed-function pipeline.&amp;#160; For example, whereas a specific kind of light (e.g., spotlight) and material (e.g., specular) are fixed abstractions that would be customized and installed in a fixed-function pipeline, now it is merely sufficient to take the position of the vertex or pixel being shaded and return a color! At first glance, such an API seems a lot harder to use in simple cases where well non-custom understood lighting is being used, but then such cases can rely on reusable procedures that package up the lighting math. Beyond dramatically increased flexibility, the programmable pipeline approach also has a better learning curve as their are only a few lightweight abstractions to learn with virtually no architecture. For an excellent write up what the move from fixed-function to programmable pipelines means, see &lt;a href="http://arstechnica.com/gaming/news/2008/09/gpu-sweeney-interview.ars" target="_blank"&gt;Tim Sweeney’s Ars interview on the subject&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;What do programmable pipelines and shaders have to do with software libraries and frameworks? They hint at an alternative to frameworks: rather design a library with many large-grained abstractions (fixed functions) that programmers customize down to what they want, instead we could design a library with a few small-grained versatile abstractions (shaders) that programmers then build up to what they want. We refer to the latter as &lt;strong&gt;programmable abstractions&lt;/strong&gt;, which are smaller and more generally used than fixed abstractions. Large-grained reuse then comes from assembling the programmable abstractions together and packaging them up through language-level abstraction primitives (e.g., procedures). Programmable abstractions are not a new concept: they underpin many libraries, especially those built in functional programming languages. Additionally, programmable abstractions can be promoted to language-level abstractions with &lt;a href="http://en.wikipedia.org/wiki/Domain-specific_language" target="_blank"&gt;domain-specific languages&lt;/a&gt; (e.g., &lt;a href="http://en.wikipedia.org/wiki/Anonymous_pipe" target="_blank"&gt;pipes&lt;/a&gt; in &lt;a href="http://en.wikipedia.org/wiki/Unix_shell" target="_blank"&gt;shell script&lt;/a&gt;), where the key to defining a good DSL is to base it on versatile programmable abstractions rather than fixed abstractions. I feel that a term for such abstractions is needed because of the confusion surrounding languages, frameworks, libraries, DSLs, and something in between (highly versatile libraries that are not quite DSLs). &lt;/p&gt;  &lt;p&gt;I’m not exactly sure what the threshold between a programmable abstraction and a fixed abstraction is, it might not be very clear cut. Also, the benefits of programmable abstractions are negated if excessive amounts of boilerplate are needed to use them, languages can go a long way in eliminating this boilerplate and making programmable abstractions more feasible (e.g., see express tree lifting in LINQ). To be continued, in the next part I’ll provide some concrete examples of differences between fixed and programmable abstractions. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9852914" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author><category term="Bling" scheme="http://blogs.msdn.com/sean_mcdirmid/archive/tags/Bling/default.aspx" /></entry><entry><title>Announcing Bling 3!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/27/announcing-bling-3.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/27/announcing-bling-3.aspx</id><published>2009-07-27T06:36:36Z</published><updated>2009-07-27T06:36:36Z</updated><content type="html">&lt;p&gt;I’d like to announce a newly rewritten &lt;a href="http://bling.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=30598"&gt;release&lt;/a&gt; of &lt;a href="http://bling.codeplex.com/"&gt;Bling&lt;/a&gt; with many improvements and exciting new features. Bling is a novel experiment in how WPF/UI programming can be enhanced via a lightweight domain-specific language hosted completely within C#. Bling basically changes the meaning of statements and expressions to do more that is otherwise possible with normal values in C#; e.g., a + b no longer means add the value of “a” to the value of “b,” but rather it creates tree that we can then transform into a databinding relationship, HLSL code, and so on. We exploit this to reduce or completely UI boilerplate code; e.g., no more value converters, no more HLSL code, no more C# shader classes, etc…Bling 3 goes farther with this style of programming through the addition of lifted functions (so f(a) also creates a tree!). For more details, see my &lt;a href="http://mcdirmid.wordpress.com/"&gt;blog&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;New features in Bling 3.0:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Improved support for multiple forms in lighting in pixel shaders reusing WPF3D abstractions when possible. Now a lighting effect can be added to a widget by specifying a WPF3D light, a material (similar to but not a WPF3D material), a normal map and/or a height map loaded from a texture (some kinds of lights require an eye position). Lighting effects can be combined via addition. WPF shader examples:&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image002_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image002_thumb.jpg" width="147" height="133" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image004_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image004_thumb.jpg" width="156" height="134" /&gt;&lt;/a&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image006_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image006_thumb.jpg" width="133" height="137" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image008_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image008" border="0" alt="clip_image008" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image008_thumb.jpg" width="135" height="135" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Normal mapped diffuse material + red specular material point light effect.&lt;/li&gt;    &lt;li&gt;Glass material + point light effect (also normal mapped).&lt;/li&gt;    &lt;li&gt;Diffraction material + point light effect (again normal mapped). &lt;/li&gt;    &lt;li&gt;Diffuse material + point light effect with parametric normals (computed vs. loaded from an image).&lt;/li&gt; &lt;/ol&gt;  &lt;ul&gt;   &lt;li&gt;Pixel-level transition abstractions that support composition. Example:&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image009_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image009" border="0" alt="clip_image009" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image009_thumb.png" width="244" height="163" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Intermediate progress of a ripple + sin transition composed via Min (take the min color from each transition’s result, where start and end are the same).&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Hacked prototype-quality WPF-friendly web browser that supports transformations and pixel shader effects. Browser will refresh every 100 milliseconds, so flash animations will work if a bit choppy. Link clicking is supported, but not keyboard input (need more interop code). Example:&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image011_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image011" border="0" alt="clip_image011" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image011_thumb.jpg" width="244" height="84" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Paper normal mapped browser navigated to Bling’s codeplex page.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Simplified physics engine (more examples in the future).&lt;/li&gt;    &lt;li&gt;Initial experimental support for DirectX 10, focusing right now on parametric surfaces. DirectX compositions look very much like Bling WPF pixel shaders and can refer directly to WPF dependency properties (just like a WPF pixel shader can). Example: &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image013_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image013" border="0" alt="clip_image013" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image013_thumb.jpg" width="176" height="147" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image015_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image015" border="0" alt="clip_image015" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image015_thumb.jpg" width="154" height="148" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image017_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image017" border="0" alt="clip_image017" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image017_thumb.jpg" width="154" height="149" /&gt;&lt;/a&gt; &lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image019_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image019" border="0" alt="clip_image019" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image019_thumb.jpg" width="146" height="146" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image021_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image021" border="0" alt="clip_image021" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnnouncingBling3_A30B/clip_image021_thumb.jpg" width="244" height="124" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;A sphere, no distortion&lt;/li&gt;    &lt;li&gt;A sphere + a egg crate distortion&lt;/li&gt;    &lt;li&gt;(2) with more magnitude&lt;/li&gt;    &lt;li&gt;(3) with more frequency&lt;/li&gt;    &lt;li&gt;(4) with less magnitude + the WPF sliders that are controlling it all.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Caveats:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Bling 3 is not backwards compatible with the previous version of Bling. Existing code requires porting.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;ToDo:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Work on DirectX abstractions (right now we just have a start).&lt;/li&gt;    &lt;li&gt;Would like to support Silverlight 3.&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9849600" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>A web browser suitable for Harry Potter in WPF!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/27/a-web-browser-suitable-for-harry-potter-in-wpf.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/27/a-web-browser-suitable-for-harry-potter-in-wpf.aspx</id><published>2009-07-27T05:01:50Z</published><updated>2009-07-27T05:01:50Z</updated><content type="html">&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Daily_Prophet#The_Daily_Prophet" target="_blank"&gt;Daily Prophet&lt;/a&gt; eat your heart out! Here is a prototype web browser we threw together in &lt;a href="http://bling.codeplex.com/" target="_blank"&gt;Bling&lt;/a&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnOldfashionedwebbrowserinBling_1273E/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/AnOldfashionedwebbrowserinBling_1273E/image_thumb.png" width="563" height="310" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;We (myself, my intern lighting-export Li SiYu, and a former intern Wang Chao who worked on the browser part) started with code from &lt;a href="http://chriscavanagh.wordpress.com/2008/09/04/youcube/"&gt;Chris Cavanagh's Blog&lt;/a&gt; to render a background IE instance into a WPF writeable bitmap, which we optimized using some sample code from &lt;a href="http://channel9.msdn.com/tags/David+Teitlebaum/" target="_blank"&gt;David Teitlebaum&lt;/a&gt;. The advantage of this approach is that a writeable bitmap is a first class entity in WPF and so can be transformed and distorted, whereas a standard WPF web browser doesn’t support these features due to airspace issues. The disadvantage of our approach is that its a big hack suitable only for playing around (e.g., in the above pic) and we can only update the browser about once every 100 milliseconds, so videos and Flash are extremely choppy (but still very lively!). &lt;/p&gt;  &lt;p&gt;The web browser content is projected onto a &lt;a href="http://en.wikipedia.org/wiki/Diffuse_reflection" target="_blank"&gt;diffuse&lt;/a&gt; material that is lit by a point light. We bind the point light’s position to the mouse so the lighting changes dynamically with user interaction. The height of the light is bound to the slider beneath the address bar. This code sets up the lighting:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ThumbBl &lt;/span&gt;LightXY = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ThumbBl&lt;/span&gt;();
&lt;span style="color: #2b91af"&gt;Action &lt;/span&gt;UpdateLightXY = &lt;span style="color: #2b91af"&gt;PointBl&lt;/span&gt;.Assign(LightXY, canvas.MousePosition);
&lt;span style="color: blue"&gt;this&lt;/span&gt;.MouseMove += (xx, yy) =&amp;gt; UpdateLightXY();
&lt;span style="color: #2b91af"&gt;Lighting &lt;/span&gt;lighting = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DiffuseMaterialCl&lt;/span&gt;().Apply(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PointLightBl&lt;/span&gt;() {
  Position = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;(LightXY, HeightOfLight),
  Color = &lt;span style="color: #2b91af"&gt;Colors&lt;/span&gt;.White,
});&lt;/pre&gt;

&lt;p&gt;This code mostly involves databinding, except for tracking the mouse position, which we do manually. The “Apply” method of material exercises basic lighting equations according to the specified light (diffuse material + point light in our case) to create a lighting result. The lighting code for a diffuse material is encoded in Bling is something like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Point3DBl &lt;/span&gt;Dir = LightSource.DirectionToPosition(Position);
&lt;span style="color: #2b91af"&gt;DoubleBl &lt;/span&gt;Dis = Dir.Length;
Dir = Dir.Normalize;
LightingResult = ((LightSource.Color.ScRGB * Dir.Dot(Normal).Max(0) * 
  LightSource.UseAttenuation(Dir, Dis))) * Input.ScRGB;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Lights are distinguished by their direction (constant for a directional light, difference between position being shaded and light position for point light) and their &lt;a href="http://en.wikipedia.org/wiki/Attenuation" target="_blank"&gt;attenuation&lt;/a&gt; (a bit more math). &lt;/p&gt;

&lt;p&gt;We apply the lighting result to the canvas containing the browser via &lt;a href="http://en.wikipedia.org/wiki/Normal_mapping" target="_blank"&gt;normal mapping&lt;/a&gt;, which uses pre-baked texture to add depth and naturalness to the surface being lit. The textures used here make the browser look like it is printed on slightly folded paper! The texture scrolls as the scrollbar is used to scroll browser content, though not at the same magnitude. Code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Texture &lt;/span&gt;HeightField = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ImageBrushBl&lt;/span&gt;(HeightImage);
&lt;span style="color: #2b91af"&gt;Texture &lt;/span&gt;NormalMap = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ImageBrushBl&lt;/span&gt;(NormalImage);
HeightField = HeightField.Distort(uv =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PointBl&lt;/span&gt;(uv.X, (uv.Y + scrollBar.Value).Frac));
NormalMap =     NormalMap.Distort(uv =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PointBl&lt;/span&gt;(uv.X, (uv.Y + scrollBar.Value).Frac));
canvas.Effect.Custom = lighting.Apply((&lt;span style="color: #2b91af"&gt;HeightField&lt;/span&gt;)HeightField, (&lt;span style="color: #2b91af"&gt;NormalMap&lt;/span&gt;)NormalMap, 
                              canvas.Size.AddZ(0.015d), canvas.CenterSize.AddZ(100d));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Textures in WPF are brushes, so we load our normal mapping files (HeightImage and NormalImage) as image brushes. We distort the resulting textures by shifting them vertically by the scrollbar’s value, which are then converted into proper texture coordinates via Frac (an &lt;a href="http://msdn.microsoft.com/en-us/library/bb509611(VS.85).aspx" target="_blank"&gt;HLSL&lt;/a&gt; intrinsic defined as Math.Abs(Math.Round(n))). We then explicitly coerce the two textures into a height field and normal map respectively, for use in normal mapping. These explicit coercions convert the textures according to a popular normal map and height field texture representations (&lt;a href="http://www.crazybump.com/" target="_blank"&gt;Crazy Bump&lt;/a&gt; and various Photoshop normal mapping plugins). The other two arguments used for normal mapping are the 3D size of the canvas and the eye position we want to use. The code for normal mapping is as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PixelEffect &lt;/span&gt;Apply(&lt;span style="color: #2b91af"&gt;HeightField &lt;/span&gt;Heights, &lt;span style="color: #2b91af"&gt;NormalMap &lt;/span&gt;Normals, &lt;span style="color: #2b91af"&gt;Point3DBl &lt;/span&gt;Size, &lt;span style="color: #2b91af"&gt;Point3DBl &lt;/span&gt;Eye) {
  &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PixelEffect&lt;/span&gt;(input =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Texture&lt;/span&gt;(UV =&amp;gt; {
    &lt;span style="color: #2b91af"&gt;Point3DBl &lt;/span&gt;Position = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;(UV * Size.XY(), Heights[UV] * Size.Z);
    &lt;span style="color: blue"&gt;var &lt;/span&gt;eyeDir = (Eye - Position).Normalize;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;UV0 = UV + eyeDir.XY() * Heights[UV] * Size.Z;
    &lt;span style="color: blue"&gt;return this&lt;/span&gt;[Position, Normals[UV0], Eye][input, UV0];
  }));
}&lt;/pre&gt;

&lt;p&gt;A pixel effect is simply a texture-to-texture function that is suitable for use as a pixel shader. A texture is defined as a point-to-color function that can be loaded from a file (as we did with HeightField and NormalMap) but can also be defined programmatically, which is what we are doing here. The math for the resulting texture is beyond the scope of this blog post but is not very difficult: basically we extract heights and normals from the height field and normal map according to the current texture coordinate UV, adjust them for the 3D size of our target, and bias UV by using an eye direction and the current height.&amp;#160; This is then plugged into our lighting result, which in turn is used to compute the resulting pixel from the input texture and the biased texture coordinate.&lt;/p&gt;

&lt;p&gt;This effect will work for any WPF application, not just our browser, notice the text field at the top, the slider below that, and the scrollbar tot he side, which are all WPF widgets!&lt;/p&gt;

&lt;p&gt;The code for this sample is available in the &lt;a href="http://bling.codeplex.com/" target="_blank"&gt;Bling 3 distribution&lt;/a&gt;, which will hopefully be released soon! &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9849555" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Fun(ctional) graphics in C#!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/20/fun-ctional-graphics-in-c.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/07/20/fun-ctional-graphics-in-c.aspx</id><published>2009-07-20T08:19:50Z</published><updated>2009-07-20T08:19:50Z</updated><content type="html">&lt;p&gt;Graphics programming often involves customizing and combining well known techniques as mathematic formulas and algorithms related to geometry, lighting, physics, and so on. For performance and architecture reasons, realizing these formulas in a real programming involves drastic transformations that results in code that is hard to read and write. For example, multiple graphics techniques are encoded into low-level pixel and vertex shader code in way that renders them unrecognizable. &lt;/p&gt;  &lt;p&gt;One solution is to encode graphic techniques in a high-level functional programming language, where functions allow us to effectively represent, combine, and manipulate graphic techniques. Take for example lighting computations that can be represented as a function &lt;strong&gt;Position3D –&amp;gt; Normal3D –&amp;gt; InputColor –&amp;gt; ResultColor&lt;/strong&gt;, which can then be combined via addition: take two lighting computations &lt;strong&gt;&lt;em&gt;l1&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;l2&lt;/em&gt;&lt;/strong&gt;, where a succinct &lt;strong&gt;&lt;em&gt;l1&lt;/em&gt;&lt;/strong&gt; + &lt;strong&gt;&lt;em&gt;l2&lt;/em&gt;&lt;/strong&gt; expands to p –&amp;gt; n –&amp;gt; c –&amp;gt; &lt;strong&gt;&lt;em&gt;l1&lt;/em&gt;&lt;/strong&gt;(p)(n)(c) + &lt;strong&gt;&lt;em&gt;l2&lt;/em&gt;&lt;/strong&gt;(p)(n)(c). As graphics is math-intensive, it benefits dramatically from function representations.&lt;/p&gt;  &lt;p&gt;Unfortunately, their are a couple of drawbacks to this approach. First, the execution of a functional program is drastically slower than imperative code. However, as most graphics computations are moving to GPUs via memory-restricted shader code, this problem can be solved by generating shader code from functional programs. Such is the approach taken by &lt;a href="http://conal.net/" target="_blank"&gt;Conal Elliott&lt;/a&gt;’s Haskell-hosted &lt;a href="http://conal.net/Vertigo/"&gt;Vertigo&lt;/a&gt; library done at MSR. In Vertigo, geometries can be expressed as &lt;a href="http://en.wikipedia.org/wiki/Parametric_surface" target="_blank"&gt;parametric surfaces&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Surface_normal" target="_blank"&gt;surface normals&lt;/a&gt; for lighting are computed via symbolic differentiation. The second drawback is more one of familiarity: the techniques seem to require abandoning our existing mainstream languages and tools for dramatically different and less familiar languages such as Haskell. However, while functional programming definitely benefits from functional languages, functional programming techniques can definitely be applied in more familiar languages. As a mainstream language, C# even provides a lambda construct to support programmers who want to dabble in functional programming; e.g., by using &lt;a href="http://en.wikipedia.org/wiki/Linq" target="_blank"&gt;LINQ&lt;/a&gt; to query databases. &lt;/p&gt;  &lt;p&gt;I’m implementing a functional graphics library in &lt;a href="http://bling.codeplex.com/" target="_blank"&gt;Bling&lt;/a&gt; for C#. Bling supports the creation and composition of expression trees as seemingly normal C# expressions, allowing us to easily compose code in a high level language and then ship the result to the GPU as a vertex or pixel shader. This allows us, among other things, to support a style of programming very similar to how Vertigo is used in Haskell. For example, consider the definition of a sphere parametric surface in Bling:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PSurface &lt;/span&gt;Sphere = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PSurface&lt;/span&gt;((&lt;span style="color: #2b91af"&gt;PointBl &lt;/span&gt;p) =&amp;gt; {
  &lt;span style="color: #2b91af"&gt;PointBl &lt;/span&gt;sin = p.SinU;
  &lt;span style="color: #2b91af"&gt;PointBl &lt;/span&gt;cos = p.CosU;
  &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;(sin[0] * cos[1], sin[0] * sin[1], cos[0]);
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;PSurface is a wrapper around a function from PointBl to Point3DBl, which are wrapper types around point (R2) and 3D point (R3) expression trees. SinU and CosU define scaled sine and cosine values over points between [0,0] and [1,1] by using the coordinates as percentage for 2PI angles. Rendering a sphere in Bling is simply a matter of creating some light and defining how many vertices we want to sample in the resulting vertex buffer: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;PSurface &lt;/span&gt;Surface = &lt;span style="color: #2b91af"&gt;Geometries&lt;/span&gt;.Sphere;
&lt;span style="color: #2b91af"&gt;DirectionalLightBl &lt;/span&gt;dirLight = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DirectionalLightBl&lt;/span&gt;() {
  Direction = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;(0, 0, 1),
  Color = &lt;span style="color: #2b91af"&gt;Colors&lt;/span&gt;.White,
};
&lt;span style="color: #2b91af"&gt;ParametricKeys &lt;/span&gt;SurfaceKeys = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ParametricKeys&lt;/span&gt;(200, 200);
Device.Render(SurfaceKeys, (&lt;span style="color: #2b91af"&gt;IntBl &lt;/span&gt;n, &lt;span style="color: #2b91af"&gt;PointBl &lt;/span&gt;uv, &lt;span style="color: #2b91af"&gt;IVertex &lt;/span&gt;vertex) =&amp;gt; {
  vertex.Position = (Surface * (world * view * project))[uv]; ;
  &lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;norm = (&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;)(Surface.Normals[uv] * world).Normalize;
  &lt;span style="color: #2b91af"&gt;Point3DBl &lt;/span&gt;usePosition = (&lt;span style="color: #2b91af"&gt;Point3DBl&lt;/span&gt;)((Surface[uv] * (world)));
  vertex.Color = dirLight.Diffuse()[usePosition, norm](n.SelectColor());
}); &lt;/pre&gt;

&lt;p&gt;where world, view, and project are standard 4x4 matrices used in translating position in 3D scenes. This code creates a directional light coming from the bottom (reused from WPF 3D no less!) and defines a set of surface keys over 200 by 200 vertices, meaning 40,000 vertices are sampled in the rendered sphere. The light is applied to the sphere in a render function using a world transformed position of each vertex along with a world transformed normal that is automatically computed via the derivative of the parametric surface. The color for each vertex is selected based on the vertex index, leading to a nice spiral pattern in the sphere that also gives us an idea of vertex topology. Here is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_thumb.png" width="244" height="220" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As described in the &lt;a href="http://conal.net/papers/Vertigo/" target="_blank"&gt;Vertigo paper&lt;/a&gt;, interesting geometries can also be formed from surfaces via displacement by height fields. Copying the examples in the vertigo paper, consider an eggcrate height field that whose definition in Bling has a simple structure similar to sphere:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HeightField &lt;/span&gt;EggCrate = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;HeightField&lt;/span&gt;((&lt;span style="color: #2b91af"&gt;PointBl &lt;/span&gt;p) =&amp;gt; {
  p = p.SinCosU;
  &lt;span style="color: blue"&gt;return &lt;/span&gt;p.X * p.Y;
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The displacement method on surfaces is then defined as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PSurface &lt;/span&gt;Displace(&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;PointBl&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;DoubleBl&lt;/span&gt;&amp;gt; HeightField) {
  &lt;span style="color: blue"&gt;return new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;PSurface&lt;/span&gt;((&lt;span style="color: #2b91af"&gt;PointBl&lt;/span&gt; p) =&amp;gt;
    &lt;span style="color: blue"&gt;this&lt;/span&gt;[p] + Normals[p] * HeightField(p));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As with lighting computations, surface normals are used to determine the proper direction in which to apply the height field. We can then apply the egg crate height field to a sphere as follows:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;PSurface &lt;/span&gt;Surface = &lt;span style="color: #2b91af"&gt;Geometries&lt;/span&gt;.Sphere.
  Displace((&lt;span style="color: #2b91af"&gt;Geometries&lt;/span&gt;.EggCrate.Frequency(20d).Magnitude(sliderZ.Value * .5)));&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The frequency and magnitude of a height field are adjusted via methods that have one line implementations (again, this is described in the Vertigo paper). One twist is that we control the magnitude by a slider (Z), which is a standard WPF slider with a value dependency property. Now, by moving the slider up, the sphere becomes deformed via the egg crate height field:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_thumb_1.png" width="208" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Moving the slider even farther up, the sphere becomes spiky:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/FunctionalgraphicsinC_702F/image_thumb_2.png" width="227" height="244" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;The change in deformation occurs in real time, so we see a smooth animation as the sphere becomes deformed. &lt;/p&gt;

&lt;p&gt;Bling will automatically determine what code executes outside of the shader in the form of either uniforms (per-shader external variables) or vertex buffers (per-vertex data). Bling will also determine what code runs in pixel shaders vs. vertex shaders, and will automatically generate vertex input and output structures to accommodate communication between the two kinds of shaders. Bling will also generate code to handle update uniforms and vertex buffers during rendering, requiring absolutely no boilerplate code on behalf of the programmer. Instead, the programmer can just focus on what graphics techniques they want to use, without needing to worry about adapting them to fit on the underlying graphics architecture. For the above example, here is the HLSL code that Bling generates, which is then rendered using Direct3D 10:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;div style="padding-bottom: 5px; padding-left: 5px; width: 750px; padding-right: 5px; display: block; float: none; margin-left: auto; margin-right: auto; padding-top: 5px" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:00d280bb-aaf4-4702-b383-eb9916c75cd7" class="wlWriterEditableSmartContent"&gt;
&lt;div style="border: #000080 1px solid; font-family: 'Courier New', Courier, Monospace; font-size: 10pt"&gt;
&lt;div style="background: #ddd; max-height: 300px; overflow: scroll; padding: 0"&gt;
&lt;ol style="background: #ffffff; margin: 0 0 0 35px; white-space: wrap"&gt;
&lt;li&gt;
float U0;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
float4x4 U1;&lt;/li&gt;&lt;li&gt;
float4x4 U2;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
float3 U3;&lt;/li&gt;&lt;li&gt;
float3 U4;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
struct VS_Input {&lt;/li&gt;&lt;li&gt;
  float P0 : POSITION;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 P1 : POSITION1;&lt;/li&gt;&lt;li&gt;
  float3 P2 : POSITION2;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float P3 : NORMAL;&lt;/li&gt;&lt;li&gt;
  float3 P4 : NORMAL1;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 P5 : NORMAL2;&lt;/li&gt;&lt;li&gt;
  float P6 : NORMAL3;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 P7 : NORMAL4;&lt;/li&gt;&lt;li&gt;
  float3 P8 : NORMAL5;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 P9 : COLOR;&lt;/li&gt;&lt;li&gt;
};&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
struct VS_Output {&lt;/li&gt;&lt;li&gt;
  float4 Pos : SV_Position;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 P0 : COLOR;&lt;/li&gt;&lt;li&gt;
};&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
VS_Output VS(VS_Input input) {&lt;/li&gt;&lt;li&gt;
  VS_Output output = (VS_Output) 0;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float t0_0 = (U0 * input.P0);&lt;/li&gt;&lt;li&gt;
  float3 t0_1 = t0_0.xxx;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_2 = (input.P1 * t0_1);&lt;/li&gt;&lt;li&gt;
  float3 t0_3 = (input.P2 + t0_2);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_4 = t0_3.xyz;&lt;/li&gt;&lt;li&gt;
  float4 t0_5 = float4(t0_4, 1);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_6 = t0_5;&lt;/li&gt;&lt;li&gt;
  float4 t0_7 = mul(t0_6, U1);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_8 = t0_7;&lt;/li&gt;&lt;li&gt;
  float3 t0_9 = t0_8.xyz;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_10 = t0_8.w.xxx;&lt;/li&gt;&lt;li&gt;
  float3 t0_11 = (t0_9 / t0_10);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_12 = float4(t0_11, 1);&lt;/li&gt;&lt;li&gt;
  float t0_13 = (U0 * input.P3);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_14 = t0_13.xxx;&lt;/li&gt;&lt;li&gt;
  float3 t0_15 = (input.P1 * t0_14);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_16 = (t0_1 * input.P4);&lt;/li&gt;&lt;li&gt;
  float3 t0_17 = (t0_15 + t0_16);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_18 = (input.P5 + t0_17);&lt;/li&gt;&lt;li&gt;
  float3x1 t0_19 = float3x1(t0_18.x, t0_18.y, t0_18.z);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_20 = t0_19;&lt;/li&gt;&lt;li&gt;
  float t0_21 = (U0 * input.P6);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_22 = t0_21.xxx;&lt;/li&gt;&lt;li&gt;
  float3 t0_23 = (input.P1 * t0_22);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_24 = (t0_1 * input.P7);&lt;/li&gt;&lt;li&gt;
  float3 t0_25 = (t0_23 + t0_24);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_26 = (input.P8 + t0_25);&lt;/li&gt;&lt;li&gt;
  float3x1 t0_27 = float3x1(t0_26.x, t0_26.y, t0_26.z);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_28 = t0_27;&lt;/li&gt;&lt;li&gt;
  float3 t0_29 = cross(t0_20, t0_28);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_30 = normalize(t0_29);&lt;/li&gt;&lt;li&gt;
  float3 t0_31 = t0_30.xyz;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_32 = float4(t0_31, 1);&lt;/li&gt;&lt;li&gt;
  float4 t0_33 = t0_32;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_34 = mul(t0_33, U2);&lt;/li&gt;&lt;li&gt;
  float4 t0_35 = t0_34;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_36 = normalize(t0_35);&lt;/li&gt;&lt;li&gt;
  float3 t0_37 = t0_36.xyz;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_38 = t0_36.w.xxx;&lt;/li&gt;&lt;li&gt;
  float3 t0_39 = (t0_37 / t0_38);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float t0_40 = dot(U3, t0_39);&lt;/li&gt;&lt;li&gt;
  float t0_41 = max(t0_40, 0);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float3 t0_42 = t0_41.xxx;&lt;/li&gt;&lt;li&gt;
  float3 t0_43 = (U4 * t0_42);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 t0_44 = float4(t0_43, 1);&lt;/li&gt;&lt;li&gt;
  float4 t0_45 = (t0_44 * input.P9);&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  output.Pos = t0_12;&lt;/li&gt;&lt;li&gt;
  output.P0 = t0_45;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  return output;&lt;/li&gt;&lt;li&gt;
}&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;&lt;li&gt;
float4 PS(VS_Output input) : SV_Target {&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  float4 retV;&lt;/li&gt;&lt;li&gt;
  retV = input.P0;&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  return retV;&lt;/li&gt;&lt;li&gt;
}&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;&lt;li&gt;
technique10 Render {&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  pass P0 {&lt;/li&gt;&lt;li&gt;
    SetVertexShader( CompileShader( vs_4_0, VS() ) );&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
    SetGeometryShader(NULL);&lt;/li&gt;&lt;li&gt;
    SetPixelShader(CompileShader(ps_4_0, PS()));&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
  }&lt;/li&gt;&lt;li&gt;
}&lt;/li&gt;&lt;li style="background: #f3f3f3"&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
Since the code is auto-generated, it is not very pretty, but it doesn’t have to be. I still need to work on the conversion process to minimize the number of vertex parameters passed in as input, as these are limited to 16 and can easily be exhausted when more external values (e.g., sliders) are added to parameterize geometry and lighting. 

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The code for this example is currently available in our codeplex SVN repository at &lt;a title="https://bling.svn.codeplex.com/svn/Bling3" href="https://bling.svn.codeplex.com/svn/Bling3"&gt;https://bling.svn.codeplex.com/svn/Bling3&lt;/a&gt;, and I’m currently working on cleaning things up for a new release of Bling (titled Bling 3) that will support rich UI construction in WPF and preliminary support for DirectX 10 (sorry XP users!) as described in this post. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9840882" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Bling WPF hits V1</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2009/02/20/bling-wpf-hits-v1.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2009/02/20/bling-wpf-hits-v1.aspx</id><published>2009-02-20T15:04:54Z</published><updated>2009-02-20T15:04:54Z</updated><content type="html">&lt;p&gt;I'd like to announce a new and improved version of &lt;a href="http://www.codeplex.com/bling"&gt;Bling WPF&lt;/a&gt;. In this version, we have redone the wrappers around WPF databinding and pixel shading for better usability, while a lot of documentation and examples have been added to the distribution and the Codeplex page. Finally, we've also added some experimental support for UI physics with an example! A release for Visual Studio 2008/.NET 3.5 SP1 is available at &lt;a href="http://www.codeplex.com/bling"&gt;http://www.codeplex.com/bling&lt;/a&gt;. For anyone unfamiliar with Bling, here are the primary features: &lt;ul&gt; &lt;li&gt;WPF Databinding without IValueConverters in C#!&amp;nbsp; For example, "button.CenterPosition.X = slider.Value * MainCanvas.Width" is valid C# code in Bling&amp;nbsp; that will setup a databinding relationship that binds button's LeftProperty to something that will move it with the slider. &lt;/li&gt; &lt;li&gt;WPF pixel shaders in C# without HLSL code or boilerplate! A pixel shader is simply a texture-to-pixel function, e.g., "canvas.CustomEffect = (input,uv) =&amp;gt; slider.Value.Lerp(input[uv], ColorBl.FromScRgb(new PointBl(1,1,1) - input[uv].ScRGB, input[uv].A));" is a one line pixel shader that will invert all the colors in canvas interpolated with respect to a slider's current value. No need to write HLSL code, no need to write a custom effect class.writing a pixel shader is boiled down to its core function. &lt;/li&gt; &lt;li&gt;Bling defines many WPF convenience properties; e.g., Size is defined as (Width, Height), Right is defined as Left + Width, CenterPosition is defined as LeftTop + Size / 2. Convenience properties are just like properties that are backed directly by dependency properties; i.e., they can undergo databinding, be used in pixel shaders, and so on.&lt;/li&gt; &lt;li&gt;Bling code is completely compatible with conventional WPF code. Bling wrappers are stateless so you can use Bling functionality anywhere in your program regardless of architecture. &lt;/li&gt; &lt;li&gt;UI Physics! Did you wonder what would happen if property bindings were solved via a physics engine rather than a databinding engine? Well, ok, probably not J, but the result is cool and could possibly be the future of UI toolkits. I'll write more about this later. &lt;/li&gt;&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9435900" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>New Bling WPF release with metaballs!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/12/29/new-bling-wpf-release-with-metaballs.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/12/29/new-bling-wpf-release-with-metaballs.aspx</id><published>2008-12-29T12:54:08Z</published><updated>2008-12-29T12:54:08Z</updated><content type="html">&lt;p&gt;No, not meatballs. I've done a lot of work on Bling this month, the first of which is writing a &lt;a href="http://cid-51c4267d41507773.skydrive.live.com/embedrowdetail.aspx/.Public/mcdirmid09ldsl.pdf"&gt;paper&lt;/a&gt; on the technique used to build Bling. I've also overhauled how pixel shader effects are expressed so that even less boilerplate is required than before.&amp;nbsp; In the new release, when you want to add a signal parameter to a shader, you can simply call "Sh" on the signal and it will automatically be added to the list of the shader's parameters.&lt;/p&gt; &lt;p&gt;As an example, consider code:&lt;/p&gt;&lt;pre class="code"&gt;Bling.Shaders.&lt;span style="color: #2b91af"&gt;Shaders&lt;/span&gt;.MakeDirect((txt, input, uv) =&amp;gt; {
  &lt;span style="color: #2b91af"&gt;FloatSh &lt;/span&gt;value = 0f;
  &lt;span style="color: #2b91af"&gt;Point3DSh &lt;/span&gt;rgb = &lt;span style="color: #2b91af"&gt;Point3DSh&lt;/span&gt;.New(0, 0, 0);
  &lt;span style="color: #2b91af"&gt;PointSg &lt;/span&gt;xyscaled = canvas.Size() / 
    (canvas.Size().X + canvas.Size().Y);

  uv = uv * xyscaled.Sh(txt);
  &lt;span style="color: blue"&gt;for &lt;/span&gt;(&lt;span style="color: blue"&gt;int &lt;/span&gt;i = 0; i &amp;lt; points.Length; i++) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;p = ((points[i] - canvas.LeftTop()) / canvas.Size());
    p = p * xyscaled;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;v = (uv - p.Sh(txt));
    v = v * v;
    &lt;span style="color: blue"&gt;var &lt;/span&gt;at = 1f / (v.X + v.Y);
    value += at;
    rgb += (colors[i % colors.Length].Sh().RGB * at);
  }
  &lt;span style="color: blue"&gt;var &lt;/span&gt;area = canvas.Width() * canvas.Height();
  &lt;span style="color: blue"&gt;var &lt;/span&gt;at0 = (area / 400).Sh(txt);

  &lt;span style="color: blue"&gt;return &lt;/span&gt;((value &amp;gt; at0)).Condition(
    &lt;span style="color: #2b91af"&gt;ColorSh&lt;/span&gt;.New(rgb / value, 1), 
    &lt;span style="color: #2b91af"&gt;Colors&lt;/span&gt;.White.Sh());
});
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;This code mixes signal code and shader code to create a nice meta-ball effect. The xyscaled variable is a point signal that scales X and Y coordinates according to the dimensions of the container. It is computed outside of the pixel shader but is multiplied the pixel coordinate (uv) by converting it to a shader parameter (xyscaled.Sh(txt)). For each point used to create the meta-ball effect (which is formed by 8 thumbs), the point is scaled according to the canvas then re-scaled according to x and y dimensions (so the ball generated is a circle) using xyscaled, all of these computations happen through data-binding and not in the pixel shader saving precious GPU instructions and not replicating shader operations on each pixel since they don't change. After these computations are performed outside of the shader, the point is brought into the shader (p.Sh(txt)) so it can be used in an operation with the pixel coordinate. Likewise, the area of the canvas is operated on outside of the GPU and brought into the shader using (area / 400).Sh(txt), where it is then used as the threshold for the metaball computation.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Check out the result (which is animated when you run it):&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/NewBlingWPFreleasewithmetaballs_FAB1/image_2.png"&gt;&lt;img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/NewBlingWPFreleasewithmetaballs_FAB1/image_thumb.png" width="198" border="0"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;The meta-ball example is the main example in the new source code/distribution, which you can get from &lt;a href="http://www.codeplex.com/Bling"&gt;Bling's Codeplex page&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9255521" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Shading Blobs with Bling WPF!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/28/shading-blobs-with-bling-wpf.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/28/shading-blobs-with-bling-wpf.aspx</id><published>2008-10-28T10:47:20Z</published><updated>2008-10-28T10:47:20Z</updated><content type="html">&lt;p&gt;I updated Bling WPF to version 0.6, get it at the normal place (&lt;a href="http://www.codeplex.com/bling"&gt;http://www.codeplex.com/bling&lt;/a&gt;). Mostly, I changed the DSL to get rid of more boilerplate code. Now you can create multiple input and parameter pixel shader effects with only a few lines of C# code (sorry, no XAML yet). Example of a blob shader:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3f4db6fa-5287-4692-b346-7af55afee0aa" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;        var effect &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; EightArgLiftedShader&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Point&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
        effect.ShaderFunction0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (input, uv, points) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; {
          FloatSh d &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
          &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.SegmentCount; i&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) 
            d &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; uv.Distance(points[i].LftSh());
          
          d &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; (d &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.SegmentCount);
          d &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; d &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
          var color &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; input[uv];
          &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; ColorSh.New(color.RGB &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; d, color.A);
        };
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.SegmentCount; i&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;)
          effect[i].Bind &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; polygons[j].RelativePoint(thumbs[i].CenterPosition());
        polygons[j].Effect &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; effect;
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;An &lt;em&gt;EightArgLiftedShader&lt;/em&gt; takes eight arguments of the same type (in this case Point). The parameters are then packaged up as an array of &lt;em&gt;ShaderValue&amp;lt;Point&amp;gt;&lt;/em&gt; objects (&lt;em&gt;points&lt;/em&gt;) where we then compute the average distance with the coordinate being processed (&lt;em&gt;uv&lt;/em&gt;). The distance is then inverted and doubled to come up with a value to multiple the current color by. Outside of the shader, each point parameter of the shader is bound to the relative center point of each thumb that forms the skin of the polygon being shaded (basically, take the AABB of the polygon and compute the percentage that the thumb is inside the AABB). Result on shading three blobs:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/ShadingBlobswithBlingWPF_DDF9/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/ShadingBlobswithBlingWPF_DDF9/image_thumb.png" width="463" height="380" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;A bit more 3D than a gradient brush! &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9019835" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Angles as doubles considered harmful</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/25/angles-as-doubles-considered-harmful.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/25/angles-as-doubles-considered-harmful.aspx</id><published>2008-10-25T08:57:44Z</published><updated>2008-10-25T08:57:44Z</updated><content type="html">&lt;p&gt;One problem with the .NET library is the inconsistent use of radians and degrees to represent angles: &lt;em&gt;System.Math&lt;/em&gt; angle functions assume angle are expressed in radian while WPF assumes angles are expressed in degrees. Trouble arises when you try to mix both APIs. While you can convert between degrees and radians using a couple simple equations (that I was forget and are not provided in &lt;em&gt;System.Math&lt;/em&gt; as they are in &lt;em&gt;java.lang.Math&lt;/em&gt;), a better way has to exist. &lt;/p&gt;  &lt;p&gt;In &lt;a href="http://www.codeplex.com/bling"&gt;Bling WPF&lt;/a&gt;, I’ve defined the struct &lt;em&gt;Bling.Signals.Angle&lt;/em&gt; that abstracts the underlying angle representation. Assuming “&lt;em&gt;using Bling.Signals,&lt;/em&gt;” you can go from a double or int to an Angle using three extensions methods: Degrees(), Radians(), or PI(). E.g., 90.Degrees() is an angle that is equivalent to .5.PI() and (.5 * Math.PI).Radians(). The Angle struct itself then has standard trigonometric functions on Angles that are otherwise in &lt;em&gt;Math&lt;/em&gt; (e.g., &lt;em&gt;Cos&lt;/em&gt;(), &lt;em&gt;Sin&lt;/em&gt;(), &lt;em&gt;Tan&lt;/em&gt;()) and some that Math left out that are useful in graphics programming; e.g., &lt;em&gt;SinCos() &lt;/em&gt;that returns a unit vector pointing in the direction of the angle. Since there is no point in an Angle struct if no APIs will ever use it, I’ve wrapped all the angle-like dependency properties to return Angle signals (&lt;em&gt;AngleSg&lt;/em&gt;) rather than double signals. Example of &lt;em&gt;Rotate&lt;/em&gt;:&lt;/p&gt;  &lt;p&gt;   &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:cb4539f5-36a5-400b-b5fa-62abbb703bad" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; AngleSg Rotate(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt; UIElement source, PointSg center) {
      RotateTransform rotate &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; source.Transform&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;RotateTransform&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
      rotate.Center().Bind &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; center;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; rotate.Angle();
    }
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;
  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b8922589-538d-4817-8df9-92f9c4ec67c5" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; AngleSg Angle(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt; RotateTransform source) {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; source.Signal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(RotateTransform.AngleProperty).Lft().Degrees();
    }
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;Basically, double signal (&lt;em&gt;DoubleSg&lt;/em&gt;) has a function &lt;em&gt;Degrees() &lt;/em&gt;that transforms it into a read/write angle signal. Rotation can now be expressed in angles or radians:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;rectangle.Rotate(rectangle.Center()).Value = 45.Degrees();&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;&lt;em&gt;rectangle.Rotate(rectangle.Center()).Value = .25.PI();&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These can also be combined with trig functions:&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;rectangle.Rotate(rectangle.Center()).Value = x.ASin();&lt;/em&gt;&lt;/li&gt;

  &lt;li&gt;&lt;em&gt;rectangle.Rotate(rectangle.Center()).Value = x.ACos();&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This becomes really convenient when you are doing lots of geometric graphics programming. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9015794" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>WPF Signal Library is now Bling WPF!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/21/wpf-signal-library-is-now-bling-wpf.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/21/wpf-signal-library-is-now-bling-wpf.aspx</id><published>2008-10-21T09:34:36Z</published><updated>2008-10-21T09:34:36Z</updated><content type="html">&lt;p&gt;I’m rebranding the signal library as &lt;a href="http://www.codeplex.com/bling"&gt;Bling WPF&lt;/a&gt; since it now contains support for expressing pixel shaders in C#. The release I’m putting out today polishes the pixel shader implementation that I released last week and fixes lots of outstanding issues:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;I’m not using Microsoft naming conventions; all names begin with a capital. This was a serious sticking point for many potential users.&lt;/li&gt;    &lt;li&gt;I renamed all the helper classes based on whether they were for signals or shaders. Example, &lt;em&gt;DoubleSg&lt;/em&gt; is a signal that lifts doubles while &lt;em&gt;FloatSh&lt;/em&gt; is a shader value that lifts floats. &lt;/li&gt;    &lt;li&gt;I added &lt;em&gt;Angle&lt;/em&gt; and &lt;em&gt;Percentage&lt;/em&gt; types to better represent angles and percentages that are encoded as dependency properties. This was getting on my nerves: why do I have to convert an angle to radians, call Cosine, and then convert it back to degrees so I can plop it into a WPF dependency property? Anyways, this should make WPF programming a bit higher level although it borders on unit types. To get a percent, call Percent on a value or double signal; e.g., 100.Percent() or 57.Percent(). To get an angle, there are three methods—90.Degrees(), .5.PI(), and (.5 * Math.PI).Radians() all evaluate to a 90 degree angle. &lt;/li&gt;    &lt;li&gt;Most HLSL 2.0 shader functions should be supported now on both scalars and vectors where appropriate. I also made it easier to define zero argument shaders (use &lt;em&gt;LiftedShaderEffect.Apply&lt;/em&gt;) and one argument shaders (call &lt;em&gt;Shader&lt;/em&gt; on the double/color/point signal that you want to be bound).&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9008803" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Writing WPF pixel shaders in C#!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/17/writing-wpf-pixel-shaders-in-c.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/17/writing-wpf-pixel-shaders-in-c.aspx</id><published>2008-10-17T10:44:10Z</published><updated>2008-10-17T10:44:10Z</updated><content type="html">&lt;p&gt;This week I saw a great project, &lt;a href="http://brahma.ananthonline.net/"&gt;Brahma&lt;/a&gt;, on expressing shader-based GPU computation in C#/LINQ. Using Brahma, one can write a simple LINQ query to transform a matrix without ever touching HLSL code or compiler, neat stuff! The advantage of this approach is that you don’t have to leave C# and the accompany tool support, and you don't have to setup your project to support pixel shader development. &lt;/p&gt;  &lt;p&gt;So I was thinking: can we do this to write WPF pixel shaders? Sure! I found a nice project that allows WPF pixel shaders to be compiled on the fly by &lt;a href="http://d.hatena.ne.jp/NyaRuRu/20080813/p1"&gt;NyaRuRu&lt;/a&gt; (in Japanese only), and the author gave me permission to use his compiler in my WPF signals project. I then wrote a lifted-style meta-programming library to generate HLSL code from lifted C# code—this is the same way I wrapped WPF databinding using signals. Basically, the body of a pixel shader uses special lifted types for floats, doubles, and the other data types that can go into a pixel shader. Example:&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3206f42c-22b2-4238-baed-11c088fc7f5c" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; ColorShader Emboss(ShaderCompiler&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;NoArgLiftedShader&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt, PointShader uv) {
      var input &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.ImplicitInput;
      var color &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; input[uv];
      var cA &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Cache(input[uv &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0.001&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;).Lft();
      var cB &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Cache(input[uv &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0.001&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;).Lft();
      var cC &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Cache(ColorShader.New(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.5f&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; cA &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; cB).Lft();
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; ColorShader.New(txt.Cache((cC.R &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; cC.G &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; cC.B) &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; 3f).Lft(), color.A);
    }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;This pixel shader method implements an emboss effect (I found the algorithm &lt;a href="http://blogs.microsoft.co.il/blogs/tamir/archive/2008/06/17/hlsl-pixel-shader-effects-tutorial.aspx"&gt;here&lt;/a&gt;). “uv” is the current point, implicit input is obtained from the shader compiler, use standard bracket syntax to access elements of the array. A special “&lt;strong&gt;&lt;em&gt;Cache&lt;/em&gt;&lt;/strong&gt;” method in the shader compiler is needed to create new local variables, where otherwise expressions will be repeated if they are used more than once. All standard arithmetic operations are available along with various data type constructors; e.g., &lt;strong&gt;&lt;em&gt;ColorShader&lt;/em&gt;&lt;/strong&gt;.&lt;strong&gt;&lt;em&gt;New&lt;/em&gt;&lt;/strong&gt;. To apply this shader, simply call &lt;strong&gt;&lt;em&gt;LiftedShaderEffect.Apply&lt;/em&gt;&lt;/strong&gt; and assign the result to the &lt;strong&gt;&lt;em&gt;Effect&lt;/em&gt;&lt;/strong&gt; property of your widget:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:10995fdd-53aa-42c4-9bcd-5778607ef5c7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;      canvas.Effect &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; LiftedShaderEffect.Apply(ShaderFunctions.Emboss);
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
Now, isn’t that easy? A more complicated lifted shader can have one or more “registers” that are represented as dependency properties in WPF. Here is an example of a wave shader that I copied from a random XNA sample project: 

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:bcb0cb4a-46f7-4696-9023-a79407490fc8" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; WaveShader : LiftedShaderEffect {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; LiftedShaderConfig Config0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; CreateConfig&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;WaveShader&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;();
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;protected&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; LiftedShaderConfig Config { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;get&lt;/span&gt;&lt;span style="color: #000000;"&gt; { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; Config0; } }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; DependencyProperty WaveProperty &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Config0.AddParameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;Wave&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, Math.PI &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0.75&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; DependencyProperty DistortionProperty &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Config0.AddParameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;Distortion&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, 1d);
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; DependencyProperty CenterProperty &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; Config0.AddParameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Point&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #800000;"&gt;Center&lt;/span&gt;&lt;span style="color: #800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Point(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.5&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #800080;"&gt;0.5&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Bling.Signals.DoubleSg Wave() { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Signal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(WaveProperty).Lft(); }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Bling.Signals.DoubleSg Distortion() { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Signal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(DistortionProperty).Lft(); }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; Bling.Signals.PointSg Center() { &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Signal&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Point&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(CenterProperty).Lft(); }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; WaveShader() { Config0.ShaderFunction &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ShaderFunction0; }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; ColorSh ShaderFunction0(ShaderCompiler txt, PointSh uv) {
      var wave &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Parameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(WaveProperty).LftSh();
      var distortion &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Parameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(DistortionProperty).LftSh();
      var center &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.Parameter&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;Point&lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt;(CenterProperty).LftSh();
      var distance &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (uv &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; center).Abs();
      var scalar &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; distance.Length;
      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; invert the scale so 1 is centerpoint&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      scalar &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; scalar).Abs();
      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; calculate how far to distort for this pixel    &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      var offset &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (wave &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; scalar).Sin();
      offset &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; offset.Clamp(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; calculate which direction to distort&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      var sign &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (wave &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; scalar).Cos();
      &lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt; reduce the distortion effect&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      offset &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; offset &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; distortion &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;32&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      var input &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; txt.ImplicitInput;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; input[uv &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (offset &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; sign)];
    }
  }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&lt;font color="#000000"&gt;A custom lifted shader effect must extend &lt;strong&gt;&lt;em&gt;LiftedShaderEffect &lt;/em&gt;&lt;/strong&gt;with itself as the type parameter (a bit of a cludge, I did some type hacking). It must also statically create a &lt;strong&gt;&lt;em&gt;LiftedShaderConfig&lt;/em&gt;&lt;/strong&gt; that manages transparently the registers and inputs of the pixel shader effect. Then, again statically, you can create your registers using the &lt;strong&gt;&lt;em&gt;AddParameter&lt;/em&gt;&lt;/strong&gt; of this config object by specifying the type of the parameter (passed in as a type parameter) along with its name and default value. Finally, you have to override the abstract &lt;strong&gt;&lt;em&gt;Config&lt;/em&gt;&lt;/strong&gt; property of &lt;strong&gt;&lt;em&gt;LiftedShaderEffect&lt;/em&gt;&lt;/strong&gt; with the static config object that you created. I’ve then wrapped all these dependency properties as signals for convenience. The pixel shader function is defined in &lt;strong&gt;&lt;em&gt;WaveShader’s&lt;/em&gt;&lt;/strong&gt; static initializer by assigning &lt;strong&gt;&lt;em&gt;ShaderFunctin0&lt;/em&gt;&lt;/strong&gt; to &lt;strong&gt;&lt;em&gt;ShaderFunction&lt;/em&gt;&lt;/strong&gt; of the config object. Inside the shader, each shader parameter from the shader compiler using the &lt;strong&gt;&lt;em&gt;Parameter&lt;/em&gt;&lt;/strong&gt; method with same property type that you used in AddParameter to create the dependency property (which only remember their type as a value). Finally, the &lt;strong&gt;&lt;em&gt;LftSh&lt;/em&gt;&lt;/strong&gt;() method has to be called on these parameters so that the appropriate operators are available. The rest of the code looks pretty standard, you can access methods like Abs (absolute value), Length (of a vector), Sin, Clamp, and Cos in the C# code as you would in HLSL code.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font color="#000000"&gt;The code and a release is available at &lt;a href="http://www.codeplex.com/wpfsignals"&gt;http://www.codeplex.com/wpfsignals&lt;/a&gt;. Download the latest release and build (should work ok if you have 3.5 SP1, although I’m not sure if the latest DirectX SDK is required or not…). Run the SignalTest project and you should see this picture:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/WritingWPFpixelshadersinC_DD35/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/WritingWPFpixelshadersinC_DD35/image_thumb.png" width="244" height="142" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;font color="#000000"&gt;The blue thumb in the middle as well as the two sliders are data bound to the wave shader parameters (via signals of course!). You can move these around to cause the pixel shader to refresh the screen. If you want to use this in your own code, just include Bling.Shaders in your project and add “using Bling.Shaders” to the set of used namespaces (add “using Bling.Signals” if you want to also use signals).&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9002730" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Twilight of the High-level UI Toolkit</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/08/twilight-of-the-high-level-ui-toolkit.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/08/twilight-of-the-high-level-ui-toolkit.aspx</id><published>2008-10-08T06:08:29Z</published><updated>2008-10-08T06:08:29Z</updated><content type="html">&lt;p&gt;I was reading Tim Sweeney’s &lt;a href="http://arstechnica.com/articles/paedia/gpu-sweeney-interview.ars"&gt;write up&lt;/a&gt; on how fixed function GPUs and the APIs that depend on them are quickly becoming obsolete. Basically, as GPUs move toward programmable “shaders,” graphic engine writers can easily whip up their own efficient graphic functions. We are already seeing this in new graphics APIs: mobile-oriented OpenGL ES is basically a shader-mostly version of OpenGL sans most of the fixed-function features.&lt;/p&gt;  &lt;p&gt;There is an analog with this move in how I program WPF: with retained graphics and convenient access to data binding functionality through &lt;a href="http://www.codeplex.com/wpfsignals"&gt;signals&lt;/a&gt;, I mostly find myself avoiding all the high-level widgets in WPF like StackPanels and Grids because using low-level Canvas is (a) easy and (b) incredibly flexible. For example, consider a grids: WPF has &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.grid.aspx"&gt;Grid&lt;/a&gt; class of course, but with data binding it is rather trivial to get cells to align with whatever row and column abstractions you want to define. This means you can have grids whose columns expand and collapse, scroll bars that move per-row and column, arbitrary highlights, and so on…you no longer have to be limited to Grid functionality. Example from a prototype I’m working on: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/TwilightoftheHighlevelUIToolkit_9CA3/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/TwilightoftheHighlevelUIToolkit_9CA3/image_thumb_1.png" width="492" height="207" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The little + signs represent rows that can be expanded:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/TwilightoftheHighlevelUIToolkit_9CA3/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/TwilightoftheHighlevelUIToolkit_9CA3/image_thumb_2.png" width="407" height="274" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We can also layout row headers vertically if we want. All of this is built on low-level widget, mostly Canvas, Border, Button and Scrollbar, connected together through data binding. Its not even much more code than configuring a Grid would have required.&lt;/p&gt;  &lt;p&gt;So I have a prediction to make: high-level UI toolkits (much of WPF, Swing) will become unpopular within 5 years. They will instead be replaced in popularity by low-level UI toolkits (Java2D/JavaFX, much of WPF) with high-level abstractions (data binding, signals). WPF can already be used like this, and it is very liberating. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8990004" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Reaction Diffusion using WriteableBitmap</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/07/reaction-diffusion-using-writeablebitmap.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/07/reaction-diffusion-using-writeablebitmap.aspx</id><published>2008-10-07T08:55:01Z</published><updated>2008-10-07T08:55:01Z</updated><content type="html">&lt;p&gt;&lt;/p&gt;  &lt;p&gt;For a while, I’ve been experimenting with reaction diffusion to provide for more “life like” surfaces. This reaction diffusion algorithm spreads colors out for a diffuse effect. To ensure responsiveness in the UI, the diffusion logic is run in a timer thread that notifies the UI thread to re-do the WriteableBitmap (texture) when it is done. As for performance, 300 by 300 is about all the algorithm can handle on my rather beefy machine before visible artifacts begin to show up. Multi-threading through ParallelFor doesn’t seem to help, but I’ll keep working on it to see if we can get a bigger buffer. Here is a sample of what the diffusion looks like:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/ReactionDiffusionusingWriteableBitmap_C121/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/ReactionDiffusionusingWriteableBitmap_C121/image_thumb.png" width="244" height="237" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I’ve done this using RenderTargetBitmap and a pixel shader before, however the behavior of diffusion was very difficult to control, using WriteableBitmap is more manageable. Code below, .NET 3.5 SP1 is required to run this program. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;em&gt;DiffusionBuffer.cs:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:0d7d81ab-91d5-4e2d-bf9a-a8365ff02170" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Threading;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Controls;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Media;

&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000;"&gt; PhysicsLib {
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; DiffusionBuffer : Canvas {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Media.Imaging.WriteableBitmap texture;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] bufferA;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] bufferB;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelWidth;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelHeight;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; Timer timer;
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; DiffusionBuffer(Vector size, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; updateTime) {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Width &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; size.X;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Height &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; size.Y;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.texture &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Media.Imaging.WriteableBitmap((&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)size.X, (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)size.Y, &lt;/span&gt;&lt;span style="color: #800080;"&gt;96&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;96&lt;/span&gt;&lt;span style="color: #000000;"&gt;, PixelFormats.Bgra32, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;null&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; bytesPerPixel_t &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (texture.PixelWidth) &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;8&lt;/span&gt;&lt;span style="color: #000000;"&gt;;&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;  bytesPerPixel=  25=200/8&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      stride_t &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelWidth &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; bytesPerPixel_t;&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;  stride=  5000=200*25 &lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; arraySize_t &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelHeight;&lt;/span&gt;&lt;span style="color: #008000;"&gt;//&lt;/span&gt;&lt;span style="color: #008000;"&gt;  arraysize=  1000000=5000*200&lt;/span&gt;&lt;span style="color: #008000;"&gt;
&lt;/span&gt;&lt;span style="color: #000000;"&gt;      bufferA &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;[arraySize_t];
      bufferB &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;[arraySize_t];

      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelWidth &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelHeight; ij&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; (texture.PixelWidth);
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelWidth;

        bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;000&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
        bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      }
      {
        var rect0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Int32Rect(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)texture.PixelWidth, (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)texture.PixelHeight);
        texture.WritePixels(rect0, bufferA, stride_t, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      }
      {
        var image &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Image() { Source &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture };
        Canvas.SetLeft(image, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        Canvas.SetTop(image, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Children.Add(image);
      }
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.PixelHeight &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelHeight;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.PixelWidth &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; texture.PixelWidth;


      timer &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Timer(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; TimerCallback((x) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; ((DiffusionBuffer)x).updateA()), &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;, updateTime, updateTime);
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; j, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; k, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; def) {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelWidth) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; def;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelHeight) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; def;

      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; k];
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx0(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; j, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; k) {
      var at &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; k];
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;long&lt;/span&gt;&lt;span style="color: #000000;"&gt; value &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;00&lt;/span&gt;&lt;span style="color: #000000;"&gt;, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;00&lt;/span&gt;&lt;span style="color: #000000;"&gt;, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;00&lt;/span&gt;&lt;span style="color: #000000;"&gt;, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;00&lt;/span&gt;&lt;span style="color: #000000;"&gt;, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp, k, at);

      var tp &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; sp &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, k, at);
      value &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, j &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; tp, k, at);
      var ret &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (value &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;7.97&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (ret &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt;) ret &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;)ret;
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; DummyDelegate(DiffusionBuffer buffer);

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; updateA() {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelWidth &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelHeight; ij&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; (PixelWidth);
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; ij &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; PixelWidth;

        bufferB[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bufferA[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;];
        bufferB[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx0(i, j, &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        bufferB[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx0(i, j, &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        bufferB[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; getPx0(i, j, &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      }
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;.Dispatcher.BeginInvoke(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; DummyDelegate((buffer) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; buffer.updateB()), &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;this&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; rectangle(Point p, Vector size, Color c) {
      var e &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; p &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; size;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)p.X; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)e.X; i&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
        &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)p.Y; j &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)e.Y; j&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
          add(bufferB, (i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, c.B);
          add(bufferB, (i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;, c.G);
          add(bufferB, (i &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (j &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; stride_t) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;, c.R);
        }
      }
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;private&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; add(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] buf, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; idx, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt; value) {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (idx &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;||&lt;/span&gt;&lt;span style="color: #000000;"&gt; idx &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; buf.Length) &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; buf[idx];
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (x &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;) x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; x;
      x &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; value;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt;&lt;span style="color: #000000;"&gt; (x &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt;) x &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;255&lt;/span&gt;&lt;span style="color: #000000;"&gt;;
      buf[idx] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;byte&lt;/span&gt;&lt;span style="color: #000000;"&gt;) x;
    }

    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;virtual&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; updateB() {
      var rect0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Int32Rect(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;, (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)texture.PixelWidth, (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt;)texture.PixelHeight);
      texture.WritePixels(rect0, bufferB, stride_t, &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
      var old &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bufferA;
      bufferA &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bufferB;
      bufferB &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; old;
    }
  }
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Program.cs&lt;/em&gt;&lt;/strong&gt;:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:de0068f9-ff2f-4f4a-8667-705ba6b92689" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Controls.Primitives;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Controls;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Media;
&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;using&lt;/span&gt;&lt;span style="color: #000000;"&gt; System.Windows.Shapes;

&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;namespace&lt;/span&gt;&lt;span style="color: #000000;"&gt; TestForWriteablebitmap {

  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; MyDiffusionBuffer : PhysicsLib.DiffusionBuffer {
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; Thumb[] thumbs &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Thumb[&lt;/span&gt;&lt;span style="color: #800080;"&gt;4&lt;/span&gt;&lt;span style="color: #000000;"&gt;];
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;readonly&lt;/span&gt;&lt;span style="color: #000000;"&gt; Color[] colors &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; { Colors.Red, Colors.Green, Colors.Blue, Colors.White };
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; MyDiffusionBuffer()
      : &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Vector(&lt;/span&gt;&lt;span style="color: #800080;"&gt;200&lt;/span&gt;&lt;span style="color: #000000;"&gt;,&lt;/span&gt;&lt;span style="color: #800080;"&gt;200&lt;/span&gt;&lt;span style="color: #000000;"&gt;), &lt;/span&gt;&lt;span style="color: #800080;"&gt;15&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {

      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; thumbs.Length; i&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
        thumbs[i] &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Thumb() { Background &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; SolidColorBrush() { Color &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; colors[i] }, Width &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10&lt;/span&gt;&lt;span style="color: #000000;"&gt;, Height &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;10&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
        Canvas.SetZIndex(thumbs[i], &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        thumbs[i].DragDelta &lt;/span&gt;&lt;span style="color: #000000;"&gt;+=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (x, y) &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&amp;gt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; {
          Canvas.SetLeft(((Thumb)x), Canvas.GetLeft((Thumb)x) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; y.HorizontalChange);
          Canvas.SetTop(((Thumb)x), Canvas.GetTop((Thumb)x) &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; y.VerticalChange);
        };
        Canvas.SetLeft(thumbs[i], &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        Canvas.SetTop(thumbs[i], &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;);
        Children.Add(thumbs[i]);
      }
    }
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;override&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; updateB() {
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;foreach&lt;/span&gt;&lt;span style="color: #000000;"&gt; (var t &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;in&lt;/span&gt;&lt;span style="color: #000000;"&gt; thumbs)
        rectangle(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Point(Canvas.GetLeft(t), Canvas.GetTop(t)), 
          &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Vector(t.Width, t.Height), ((SolidColorBrush)t.Background).Color);
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;base&lt;/span&gt;&lt;span style="color: #000000;"&gt;.updateB();
    }
  }
  &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt;&lt;span style="color: #000000;"&gt; Program {
    [STAThread]
    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;void&lt;/span&gt;&lt;span style="color: #000000;"&gt; Main(&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;string&lt;/span&gt;&lt;span style="color: #000000;"&gt;[] args) {
      var buf &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; MyDiffusionBuffer();
      buf.RenderTransform &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; ScaleTransform() { ScaleX &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;, ScaleY &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;, CenterX &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;150&lt;/span&gt;&lt;span style="color: #000000;"&gt;, CenterY &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;150&lt;/span&gt;&lt;span style="color: #000000;"&gt; };
      var win &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Window() { Content &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; buf };
      Application app &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; Application();
      app.Run(win);
    }
  }
}
&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8982441" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry><entry><title>Curved Polygons in WPF!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/06/curved-polygons-in-wpf.aspx" /><id>http://blogs.msdn.com/sean_mcdirmid/archive/2008/10/06/curved-polygons-in-wpf.aspx</id><published>2008-10-06T07:51:51Z</published><updated>2008-10-06T07:51:51Z</updated><content type="html">&lt;p&gt;I found something on the web &lt;a href="http://www.antigrain.com/research/bezier_interpolation/index.html"&gt;http://www.antigrain.com/research/bezier_interpolation/index.html&lt;/a&gt; about using Bezier curves to build polygons with smooth corners. Anyways, I ported this to WPF and it works very well! Basically, consider the last point (point0), current point (point1), next point (point2) and the point after that (point3), you can compute each Bezier segment as follows:&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:933632ef-b6ee-4661-ad58-eb95b6a8ed1d" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;    &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;public&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;static&lt;/span&gt;&lt;span style="color: #000000;"&gt; BezierSegment PolygonSegment(Vector point0, Vector point1, Vector point2, Vector point3, &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;double&lt;/span&gt;&lt;span style="color: #000000;"&gt; smoothValue) {
      var c1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; point1) &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; 2d;
      var c2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; point2) &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; 2d;
      var c3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; point3) &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; 2d;

      var len1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; point0).Length;
      var len2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; point1).Length;
      var len3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (point3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; point2).Length;

      var k1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; len1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; (len1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; len2);
      var k2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; len2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;/&lt;/span&gt;&lt;span style="color: #000000;"&gt; (len2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; len3);

      var m1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; c1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (c2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; c1) &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; k1;
      var m2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; c2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (c3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; c2) &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; k2;

      var ctrl1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; m1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (c2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; m1) &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; smoothValue &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; point1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; m1;
      var ctrl2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; m2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; (c2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; m2) &lt;/span&gt;&lt;span style="color: #000000;"&gt;*&lt;/span&gt;&lt;span style="color: #000000;"&gt; smoothValue &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; point2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;-&lt;/span&gt;&lt;span style="color: #000000;"&gt; m2;
      var curve &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; BezierSegment();
      curve.Point1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Point) ctrl1;
      curve.Point2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Point) ctrl2;
      curve.Point3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; (Point) point2;
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;return&lt;/span&gt;&lt;span style="color: #000000;"&gt; curve;
    }&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Simply loop through your points to create your polygon, here is some code in a physics engine I’m doing:&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:970b943e-90c4-43ae-ac2d-268b6582a3c7" class="wlWriterEditableSmartContent"&gt;&lt;pre style="background-color:White;;overflow: auto;"&gt;&lt;div&gt;&lt;!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt;&lt;span style="color: #000000;"&gt;      figure.StartPoint &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; points[(&lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count].centerPoint;
      var segments &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;new&lt;/span&gt;&lt;span style="color: #000000;"&gt; PathSegmentCollection();
      &lt;/span&gt;&lt;span style="color: #0000FF;"&gt;for&lt;/span&gt;&lt;span style="color: #000000;"&gt; (&lt;/span&gt;&lt;span style="color: #0000FF;"&gt;int&lt;/span&gt;&lt;span style="color: #000000;"&gt; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;; i &lt;/span&gt;&lt;span style="color: #000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count; i&lt;/span&gt;&lt;span style="color: #000000;"&gt;++&lt;/span&gt;&lt;span style="color: #000000;"&gt;) {
        var point0 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; points[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;0&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count].centerPoint;
        var point1 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; points[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;1&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count].centerPoint;
        var point2 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; points[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;2&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count].centerPoint;
        var point3 &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; points[(i &lt;/span&gt;&lt;span style="color: #000000;"&gt;+&lt;/span&gt;&lt;span style="color: #000000;"&gt; &lt;/span&gt;&lt;span style="color: #800080;"&gt;3&lt;/span&gt;&lt;span style="color: #000000;"&gt;) &lt;/span&gt;&lt;span style="color: #000000;"&gt;%&lt;/span&gt;&lt;span style="color: #000000;"&gt; points.Count].centerPoint;
        segments.Add(Extensions.PolygonSegment((Vector) point0, (Vector) point1, (Vector)point2, (Vector) point3, .&lt;/span&gt;&lt;span style="color: #800080;"&gt;7&lt;/span&gt;&lt;span style="color: #000000;"&gt;));
        var cp &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; point0;
        topLeft &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; topLeft.Min(cp);
        bottomRight &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; bottomRight.Max(cp);
      }
      figure.Segments &lt;/span&gt;&lt;span style="color: #000000;"&gt;=&lt;/span&gt;&lt;span style="color: #000000;"&gt; segments;&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --&gt;&lt;/div&gt;

&lt;p&gt;where figure is the only path figure of a PathGeometry. &lt;/p&gt;

&lt;p&gt;Check out the cool blob-like results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/CurvedPolygonsinWPF_B4DE/clip_image002_2.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="clip_image002" border="0" alt="clip_image002" src="https://blogs.msdn.com/blogfiles/sean_mcdirmid/WindowsLiveWriter/CurvedPolygonsinWPF_B4DE/clip_image002_thumb.jpg" width="244" height="166" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8977456" width="1" height="1"&gt;</content><author><name>Sean McDirmid</name><uri>http://blogs.msdn.com/members/Sean+McDirmid.aspx</uri></author></entry></feed>