<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-378902666221027785</id><updated>2011-11-28T01:43:27.187+01:00</updated><category term='Silverlight'/><title type='text'>Clueless</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://frodelights.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/378902666221027785/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://frodelights.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Frode Hus</name><uri>http://www.blogger.com/profile/12520489029474548273</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-378902666221027785.post-8558002315837025447</id><published>2009-04-03T12:55:00.002+02:00</published><updated>2009-04-03T13:46:36.260+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>NNUG presentation 03.04.2009</title><content type='html'>&lt;p&gt;This is the Silverlight presentation I showed at the NNUG (Norwegian .Net User Group) meeting 03.04.2009.&lt;/p&gt;&lt;p&gt;The text is mostly in norwegian, but there’s some nice demos of pixel shaders there to see. &lt;/p&gt;&lt;p&gt;&lt;strong&gt;This presentation requires Silverlight 3 beta to view and should be viewed at at least 1600x1200 resolution.&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;a href="http://silverlight.services.live.com/invoke/96295/NNUG%20presentation/iframe.html" target="_blank"&gt;View the presentation here&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/378902666221027785-8558002315837025447?l=frodelights.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://frodelights.blogspot.com/feeds/8558002315837025447/comments/default' title='Legg inn kommentarer'/><link rel='replies' type='text/html' href='http://frodelights.blogspot.com/2009/04/nnug-presentation-03042009.html#comment-form' title='0 Kommentarer'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/378902666221027785/posts/default/8558002315837025447'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/378902666221027785/posts/default/8558002315837025447'/><link rel='alternate' type='text/html' href='http://frodelights.blogspot.com/2009/04/nnug-presentation-03042009.html' title='NNUG presentation 03.04.2009'/><author><name>Frode Hus</name><uri>http://www.blogger.com/profile/12520489029474548273</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-378902666221027785.post-2788695355274936173</id><published>2009-04-03T12:19:00.004+02:00</published><updated>2009-04-03T20:18:51.383+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Silverlight'/><title type='text'>Pixel shaders in Silverlight 3 beta</title><content type='html'>&lt;p&gt;With Silverlight 3, we finally get pixel shader capability just like those in WPF. Pixel shaders are a very powerful tool for manipulating graphics and can be confusing to get into.&lt;/p&gt;  &lt;p&gt;With this in mind, I thought I’d write a (hopefully) simple tutorial.    &lt;br /&gt;Its a lengthy read, but hopefully manageable.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;So, how does a pixel shader work?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Basically, a pixel shader is a small program that gets executed per pixel on the graphical element which it is assigned to.    &lt;br /&gt;This means that if we attach a pixel shader to a simple image, the shader code will walk through every single pixel on that image and modify it. &lt;/p&gt;  &lt;p&gt;In Silverlight 3 beta, any brush can be used as input to a shader – movies, buttons, grids, etc – and they will retain their function even though the shader may completely alter their appearance.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;How is shader code written?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;This is where it all gets potentially very difficult or very easy.    &lt;br /&gt;Shader code for Silverlight is written in HLSL – High Level Shader Language which is more or less a variant of C and compiled using the DirectX SDK. The potential problem is not the language in itself, but how to perform the tasks that you want. Pixel shaders are very mathematically inclined and an effect can range from simple add/subtract to linear algebra and worse. This more or less means that pixel shaders are only limited by your creativity and mathematical knowledge (and some other limitations that I will explain later).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The anatomy of a shader&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The following code is an example of a pixel shader that does absolutely nothing:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family: system"&gt;sampler2D source : register(S0);        &lt;br /&gt;float4 main(float2 uv : TEXCOORD) : COLOR {         &lt;br /&gt;return tex2D(source,uv);         &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now, what exactly is happening here?    &lt;br /&gt;Our first line, &lt;span style="font-family: system"&gt;sampler2D source : register(S0)&lt;/span&gt;, assigns our image to the &lt;span style="font-family: system"&gt;source&lt;/span&gt; variable which is a two-dimensional sample.     &lt;br /&gt;The &lt;span style="font-family: system"&gt;main&lt;/span&gt; function is the shader’s entry point, this is where it starts executing. It takes one input parameter, an UV coordinate (which consists of X and Y coordinates described as a &lt;span style="font-family: system"&gt;float2&lt;/span&gt;) and returns a color describes as a &lt;span style="font-family: system"&gt;float4&lt;/span&gt; (red, green, blue and alpha channels). The UV coordinates range from &lt;span style="font-family: system"&gt;0&lt;/span&gt; to &lt;span style="font-family: system"&gt;1&lt;/span&gt; regardless of the resolution of the source image.     &lt;br /&gt;And all it does is returning the color at the current x,y coordinate by using the &lt;span style="font-family: system"&gt;tex2D()&lt;/span&gt; function (more information can be found &lt;a href="http://msdn.microsoft.com/en-us/library/bb509677(VS.85).aspx" target="_blank"&gt;here&lt;/a&gt;). As previously mentioned, a pixel shader iterates all the pixels in the source image and so the result on this particular shader is that we simply show the image unchanged.&lt;/p&gt;  &lt;p&gt;As you may have noticed, the way we assign the image to a variable is by using a register.    &lt;br /&gt;There are basically two registers that you will use, S# and C# where # is the register index. S registers are for samplers (textures) and C is for constants. These are the links we will use to connect the shader to Silverlight. &lt;/p&gt;  &lt;p&gt;&lt;u&gt;Important:&lt;/u&gt; The &lt;span style="font-family: system"&gt;S0&lt;/span&gt; register is implicitly registered to the control that this effect is attached to in Silverlight. I will explain this later.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Pixel shaders in Silverlight 3 beta are limited to Shader Model 2.0, which means the following (among others) limits apply:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;16 texture registers (the S registers) &lt;/li&gt;    &lt;li&gt;32 constant registers (the C registers) &lt;/li&gt;    &lt;li&gt;64 arithmetic instruction slots &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;em&gt;What does these limitations mean?&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;As an example, lets consider the Mandelbrot fractal. This fractal is ideal for pixel shaders as each point in the fractal is generated independently from the rest of the points.    &lt;br /&gt;This code snippet is the entire fractal generator and as the &lt;span style="font-family: system"&gt;iterations&lt;/span&gt; variable grows larger, the fractal becomes more clear and recognizable as a mandelbrot.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;float2 c = (uv - 0.5) * float2(3.8,3);&lt;/pre&gt;

  &lt;pre&gt;    float2 v = c;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    [unroll(7)]&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;int&lt;/span&gt; n = 0; n &amp;lt; iterations; n++){&lt;/pre&gt;

  &lt;pre class="alt"&gt;        v = float2(pow(v.x,2) - pow(v.y,2), v.x * v.y * 2) + c;&lt;/pre&gt;

  &lt;pre&gt;    }    &lt;/pre&gt;

  &lt;pre class="alt"&gt;    float4 color = tex2D(source,uv);&lt;/pre&gt;

  &lt;pre&gt;    float4 result = (dot(v, v) &amp;gt; 1) ? 1- color :color;   &lt;/pre&gt;

  &lt;pre class="alt"&gt;    result.a = 1;&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; result;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As you may notice, there’s a &lt;span style="font-family: system"&gt;[unroll(7)]&lt;/span&gt; instruction in there,this limits the iterations to 8 because beyond that, we hit the 64 arithmetic instructions limit. As a result, our mandelbrot does not look very much like what we’d expect.&lt;a href="http://lh3.ggpht.com/_UYdU3uq0-uk/SdXiuaNw_XI/AAAAAAAAABA/13pLsZflYGg/s1600-h/image%5B13%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://lh5.ggpht.com/_UYdU3uq0-uk/SdXiu-2si_I/AAAAAAAAABI/z4NslKl65to/image_thumb%5B5%5D.png?imgmax=800" width="142" height="244" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The image in blue is a proper mandelbrot and the white one is the result after 8 iterations. The shape is familiar but it lacks details.&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;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Silverlight 3 beta also has no GPU acceleration for pixel shaders thus far, hopefully this will change in the release as shaders without GPU gets very CPU intensive and thus becomes a performance issue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Writing a shader&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, lets get our hands dirty and dive in for a real example.&lt;/p&gt;

&lt;p&gt;What we’ll do is write a pixel shader that gives us a spotlight effect and will look like this:&lt;a href="http://lh3.ggpht.com/_UYdU3uq0-uk/SdXivWBcy5I/AAAAAAAAABM/uIVrepH8CL4/s1600-h/image%5B17%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://lh4.ggpht.com/_UYdU3uq0-uk/SdXiwbWZ6iI/AAAAAAAAABQ/q_6AwTBbe1c/image_thumb%5B7%5D.png?imgmax=800" width="244" height="159" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;It will have an inner radius (this brightest spot) and a outer radius (the fall-off gradient). It will also have a center which we can move around so we can animate this or make it respond to mouse movements.&lt;/p&gt;

&lt;p&gt;The HLSL code here is a few leaps up from the simple example I showed above but we’ll see if we can’t make some sense out of it still.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;sampler2D source : register(S0);&lt;/pre&gt;

  &lt;pre&gt;float2 light_center : register(C0);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;float&lt;/span&gt; outer_radius : register(C1);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;float&lt;/span&gt; inner_radius : register(C2);&lt;/pre&gt;

  &lt;pre class="alt"&gt;float4 main(float2 uv : TEXCOORD) : COLOR{&lt;/pre&gt;

  &lt;pre&gt;    float4 sourceColor = tex2D(source,uv);    &lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;float&lt;/span&gt; d = distance(uv, light_center);&lt;/pre&gt;

  &lt;pre&gt;    float4 color;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt;(d &amp;lt; outer_radius){&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;if&lt;/span&gt;(d &amp;lt; inner_radius){&lt;/pre&gt;

  &lt;pre class="alt"&gt;            sourceColor += lerp(sourceColor, float4(0.8,0.8,0.8,1), distance(uv, light_center));&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; sourceColor;    &lt;/pre&gt;

  &lt;pre class="alt"&gt;        }        &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;float&lt;/span&gt; radius_diff = outer_radius - inner_radius;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;float&lt;/span&gt; ratio = (d - inner_radius) / radius_diff;&lt;/pre&gt;

  &lt;pre&gt;        ratio = ratio * 3.14159;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;float&lt;/span&gt; adjusted_ratio = cos(ratio);&lt;/pre&gt;

  &lt;pre&gt;        adjusted_ratio = adjusted_ratio + 1;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        adjusted_ratio = adjusted_ratio / 2; &lt;/pre&gt;

  &lt;pre&gt;        color = lerp(float4(0,0,0,1),sourceColor, distance(uv, uv * (1-adjusted_ratio)));&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; color;&lt;/pre&gt;

  &lt;pre&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;span class="kwrd"&gt;else&lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; float4(0,0,0,1);&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&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;There’s some math instructions in there that I won’t go much into, but essentially what happens there is that we adjust the brightness of the current pixel according to its distance from the center point – creating a nice gradient effect. If the pixel is inside the inner radius, we make it even brighter to simulate that there’s a spotlight shining directly at it. And of course if the pixel is outside the outer radius, we’ll just turn it off (set the color to black).&lt;/p&gt;

&lt;p&gt;Now we compile our shader using &lt;em&gt;fxc.exe &lt;/em&gt;(belongs to the DirectX SDK) using these parameters: &lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;/T ps_2_0 /E main /FoSpotlight.ps Spotlight.fx&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;/T specifies the shader model we compile against, ps_2_0 is Shader Model 2.0 and is required for the shader to work with Silverlight 3. &lt;/li&gt;

    &lt;li&gt;/E sets our entry point, this is the main function. &lt;/li&gt;

    &lt;li&gt;/Fo specifies our output &lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;&lt;u&gt;Silverlight&lt;/u&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, how do we make Silverlight speak to this shader?&lt;/p&gt;

&lt;p&gt;We will first create a new class file, call it Spotlight.cs and make it extend ShaderEffect.&lt;/p&gt;

&lt;p&gt;Then we will tell Silverlight where to find our shader. First we need to add our compiled shader to our project.&lt;a href="http://lh5.ggpht.com/_UYdU3uq0-uk/SdXiwth98JI/AAAAAAAAABU/B4RTeigYhd4/s1600-h/image%5B21%5D.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="right" src="http://lh5.ggpht.com/_UYdU3uq0-uk/SdXixJBrAgI/AAAAAAAAABY/LPO0Mb8S_nk/image_thumb%5B9%5D.png?imgmax=800" width="244" height="151" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Just add the .ps file to the project and set the Build Action to &lt;em&gt;Resource&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We will then refer to this file from our Spotlight.cs class.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; PixelShader pixelShader;&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;static&lt;/span&gt; Spotlight() {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            pixelShader = &lt;span class="kwrd"&gt;new&lt;/span&gt; PixelShader();&lt;/pre&gt;

  &lt;pre&gt;            pixelShader.UriSource = &lt;span class="kwrd"&gt;new&lt;/span&gt; Uri(&lt;span class="str"&gt;&amp;quot;OurApplication;component/Effects/Spotlight.ps&amp;quot;&lt;/span&gt;, UriKind.Relative);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&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;br /&gt;&lt;/p&gt;

&lt;p&gt;Now, remember our shader variables? We need to link them to our Spotlight class. To do this, we add DependencyProperties. &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty(&lt;span class="str"&gt;&amp;quot;Input&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Spotlight), 0);&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; DependencyProperty CenterProperty = DependencyProperty.Register(&lt;span class="str"&gt;&amp;quot;Center&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Point), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Spotlight), &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyMetadata(&lt;span class="kwrd"&gt;new&lt;/span&gt; Point(0.5,0.5), PixelShaderConstantCallback(0)));&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; DependencyProperty InnerRadiusProperty = DependencyProperty.Register(&lt;span class="str"&gt;&amp;quot;InnerRadius&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;double&lt;/span&gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Spotlight), &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyMetadata(0.2, PixelShaderConstantCallback(2)));&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; DependencyProperty OuterRadiusProperty = DependencyProperty.Register(&lt;span class="str"&gt;&amp;quot;OuterRadius&amp;quot;&lt;/span&gt;, &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(&lt;span class="kwrd"&gt;double&lt;/span&gt;), &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Spotlight), &lt;span class="kwrd"&gt;new&lt;/span&gt; PropertyMetadata(0.5, PixelShaderConstantCallback(1)));&lt;/pre&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;As you can see from the source above, we register the two kinds of properties – sampler and constant. We also specify the register index – for example our &lt;span style="font-family: system"&gt;C0&lt;/span&gt; constant register as &lt;span style="font-family: system"&gt;PixelShaderConstantCallback(0)&lt;/span&gt;. 

  &lt;br /&gt;

  &lt;br /&gt;We also define default values to these properties and what type of value the property accepts. 

  &lt;br /&gt;

  &lt;br /&gt;Silverlight can now talk to the shader code using these properties.&lt;/p&gt;

&lt;p&gt;We also add regular properties so we can set and read these values directly:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Brush Input {&lt;/pre&gt;

  &lt;pre&gt;            get {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (Brush)GetValue(InputProperty);&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            set {&lt;/pre&gt;

  &lt;pre&gt;                SetValue(InputProperty, &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Point Center {&lt;/pre&gt;

  &lt;pre&gt;            get {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (Point)GetValue(CenterProperty);&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            set {&lt;/pre&gt;

  &lt;pre&gt;                SetValue(CenterProperty, &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; InnerRadius {&lt;/pre&gt;

  &lt;pre&gt;            get {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;double&lt;/span&gt;)GetValue(InnerRadiusProperty);&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            set {&lt;/pre&gt;

  &lt;pre&gt;                SetValue(InnerRadiusProperty, &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;double&lt;/span&gt; OuterRadius {&lt;/pre&gt;

  &lt;pre&gt;            get {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; (&lt;span class="kwrd"&gt;double&lt;/span&gt;)GetValue(OuterRadiusProperty);&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            set {&lt;/pre&gt;

  &lt;pre&gt;                SetValue(OuterRadiusProperty, &lt;span class="kwrd"&gt;value&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&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;Now we’re only missing our effect constructor, all we’ll do here is update the shader with the default values we set in the dependencyproperties and den set the ShaderEffect.PixelShader property to use our pixel shader.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; Spotlight() {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.PixelShader = pixelShader;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            UpdateShaderValue(InputProperty);&lt;/pre&gt;

  &lt;pre&gt;            UpdateShaderValue(CenterProperty);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            UpdateShaderValue(InnerRadiusProperty);&lt;/pre&gt;

  &lt;pre&gt;            UpdateShaderValue(OuterRadiusProperty);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&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;All done!&lt;/p&gt;

&lt;p&gt;To use this effect, we add it to XAML like so:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Image&lt;/span&gt; &lt;span class="attr"&gt;Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;demoImage&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;Source&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;../Images/eye.jpg&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;           
   &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;Image.Effect&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt;&lt;span class="html"&gt;effects:Spotlight&lt;/span&gt; &lt;span class="attr"&gt;x:Name&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;flashlight&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;InnerRadius&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;0.5&amp;quot;&lt;/span&gt; &lt;span class="attr"&gt;OuterRadius&lt;/span&gt;&lt;span class="kwrd"&gt;=&amp;quot;0.2&amp;quot;&lt;/span&gt;&lt;span class="kwrd"&gt;/&amp;gt;&lt;/span&gt;                      &lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Image.Effect&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;            
&lt;span class="kwrd"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="html"&gt;Image&lt;/span&gt;&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&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;br /&gt;&lt;/p&gt;

&lt;p&gt;(remember to add the namespace ex. &lt;em&gt;xmlns:effects=&amp;quot;clr-namespace:OurApplication&amp;quot;&lt;/em&gt;) 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;We can now bind the effect properties to storyboards or mouse movement or whatever we want so we get a dynamic spotlight effect, but this is outside the scope of this tutorial.&lt;/p&gt;

&lt;p&gt;Hope this was understandable and if you have any questions, feel free to ask :)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I also recommend &lt;/em&gt;&lt;a href="http://shazzam-tool.com/" target="_blank"&gt;&lt;em&gt;Shazzam&lt;/em&gt;&lt;/a&gt;&lt;em&gt; – this is a nice tool for testing out HLSL code directly and see the effect without implementing it in Silverlight. It also autogenerates .NET class files for use with WPF (these can be used with Silverlight with only a few minor adjustments).&lt;/em&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/378902666221027785-2788695355274936173?l=frodelights.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://frodelights.blogspot.com/feeds/2788695355274936173/comments/default' title='Legg inn kommentarer'/><link rel='replies' type='text/html' href='http://frodelights.blogspot.com/2009/04/pixel-shaders-in-silverlight-3-beta.html#comment-form' title='0 Kommentarer'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/378902666221027785/posts/default/2788695355274936173'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/378902666221027785/posts/default/2788695355274936173'/><link rel='alternate' type='text/html' href='http://frodelights.blogspot.com/2009/04/pixel-shaders-in-silverlight-3-beta.html' title='Pixel shaders in Silverlight 3 beta'/><author><name>Frode Hus</name><uri>http://www.blogger.com/profile/12520489029474548273</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/_UYdU3uq0-uk/SdXiu-2si_I/AAAAAAAAABI/z4NslKl65to/s72-c/image_thumb%5B5%5D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry></feed>
