730 likes | 1.13k Views
Deep Dive into Silverlight Graphics. Seema Ramchandani. Deep Dive into Silverlight Graphics. Seema Ramchandani. Agenda. How we draw Effects Projection Hardware. Threads. UI Thread User Code Control Code Animations Layout Non-UI Thread Frame Rasterization Media Decoding GPU Work.
E N D
Deep Dive into Silverlight Graphics Seema Ramchandani
Deep Dive into Silverlight Graphics Seema Ramchandani
Agenda • How we draw • Effects • Projection • Hardware
Threads • UI Thread • User Code • Control Code • Animations • Layout • Non-UI Thread • Frame Rasterization • Media Decoding • GPU Work
<DoubleAnimation Storyboard.TargetName="myprojection" Storyboard.TargetProperty="RotationY" From="0" To="90" Duration="0:0:1" />
time BUSY Skip 3 turns 33 msec Frame Based Model 132msec Silverlight’s Real-Time Animation
Animate& Layout • Poll for dirty flags • 2 Passes: Measure & Arrange • Designed for Nesting
Layout’s impact on Render 1. internal Clip LayoutClip • Desired sizeUAvailable Size 2. internal Transform LocalTransform • Offset from parent + RenderTransform Both are consumed in: • Measure • Arrange • Render • Hit-testing passes offset Clip
How we draw • ParentNode • Layout Offset (internal LayoutTransform) • RenderTransform • Clip • Opacity • OpacityMask • Children
How we draw • ParentNode • Layout Offset • RenderTransform • Clip • Opacity • OpacityMask • Children
How we draw • ParentNode • Layout Offset • RenderTransform • Projection • Clip • Effect • Opacity • OpacityMask • Hardware Cache • Children
Intermediate surface Apply Effect Apply 2nd effect. Top image is input
RenderToIntermediate • Effects • WriteableBitmap • Projection • GPU Caching
Effects, an overview • Built-in, Multi-pass • Blur Effect • DropShadow Effect • Custom Effect • Compiled HLSL pixel shader • .NET class that invokes the shader • Effects can pull content from any brush
MyCustomShader.cs • SL’s shader engine • Consumes MyShader.ps • Registers available: • 16 Textures • 32 Constants • Pixel Shader 2.0 • DX SDK http://bit.ly/DXsdk • fxc.exe myshader.fx /Tps_2_0 • WPF build task: http://bit.ly/buildtask http://blogs.msdn.com/greg_schechter
demo Default shader class
MyCustomShader.fx // Inputs (Brushes, including ImplicitInput) sampler2DimplicitInputSampler : register(S0);
MyCustomShader.fx // Inputs (Brushes, including ImplicitInput) sampler2D implicitInputSampler : register(S0); // Pixel Shader float4 main(float2 uv : TEXCOORD) : COLOR { float4 color = tex2D(implicitInputSampler, uv);
Brushes • A brush maps a screen position to a color. • SolidColorBrush <SolidColorBrush Color="Green"/>
Brushes StartPoint • A brush maps a screen position to a color. • LinearGradientBrush EndPoint <LinearGradientBrush> <LinearGradientBrush.GradientStops> <GradientStop Color="#FF0000" /> <GradientStop Color="#00FF00" Offset=".5" /> <GradientStop Color="#0000FF" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush>
Brushes • A brush maps a screen position to a color. • RadialGradientBrush <RadialGradientBrushGradientOrigin="0.25 0.25"> <RadialGradientBrush.GradientStops> <GradientStop Color="White" Offset="0"/> <GradientStop Color="Blue" Offset="1"/> </RadialGradientBrush.GradientStops> </RadialGradientBrush> StartPoint EndPoint
Brushes • Maps a screen position to a color. • Integrate the different graphics stacks • Image & Text
MyCustomShader.fx // Inputs (Brushes, including ImplicitInput) sampler2D implicitInputSampler : register(S0); // Pixel Shader float4 main(float2 uv : TEXCOORD) : COLOR { float4 color = tex2D(implicitInputSampler, uv);
MyCustomShader.fx // Inputs (Brushes, including ImplicitInput) sampler2D implicitInputSampler : register(S0); // Pixel Shader float4 main(float2 uv : TEXCOORD) : COLOR { float4 color = tex2D(implicitInputSampler, uv); color.rgb = 1 - color.rgb; return color; }
demo Simple ShaderComplex Shader
Effects, an overview • Blur & DropShadow • Built-in, compiled to assembly • Multi-Pass shaders • Custom Effect • Compiled HLSL pixel shader • .NET class that invokes the shader • Effects can pull content from any brush
How we draw • ParentNode • Layout Offset • RenderTransform • Projection • Clip • Effect • Opacity • OpacityMask • Hardware Cache • Children
Effects, Perf overview • Blur & DropShadow • When done, nullify UIElement.Effect • Sample all pixels • BlurRadius impacts how wide we sample. • Custom Effect • Math is easy. • Pushing around memory is time-costly. • Effects can pull content from any brush • Effects do not cache output • Change a pixel, run again.
WriteableBitmap Pixel Access:To save output of our rendering RTM
RenderIntoIntermediate • Effect • WriteableBitmap • Projection • GPU Caching