Class SkiaShader
- Namespace
- DrawnUi.Infrastructure
- Assembly
- DrawnUi.Maui.dll
Lightweight reusable shader wrapper for applying SKSL shaders directly to an SKPaint. Used standalone for direct rendering, or as the engine inside SkiaShaderEffect. Subclass and override CreateUniforms/CreateChildren for custom uniforms.
public class SkiaShader : IDisposable
- Inheritance
-
SkiaShader
- Implements
- Inherited Members
- Extension Methods
Fields
_bufImageResolution
protected readonly float[] _bufImageResolution
Field Value
- float[]
_bufMouse
protected readonly float[] _bufMouse
Field Value
- float[]
_bufOffset
protected readonly float[] _bufOffset
Field Value
- float[]
_bufResolution
protected readonly float[] _bufResolution
Field Value
- float[]
_compiled
protected SKRuntimeEffect _compiled
Field Value
Properties
BlendMode
Blend mode used when drawing. Default is SrcOver.
public SKBlendMode BlendMode { get; set; }
Property Value
Code
Gets the shader code used for last compilation
public string Code { get; }
Property Value
Compiled
The compiled SKRuntimeEffect, if available.
public SKRuntimeEffect Compiled { get; }
Property Value
FilterMode
Filter mode for the primary input texture. Default is Linear.
public SKFilterMode FilterMode { get; set; }
Property Value
IsCompiled
Whether the shader is compiled and ready to use.
public bool IsCompiled { get; }
Property Value
MipmapMode
Mipmap mode for the primary input texture. Default is None.
public SKMipmapMode MipmapMode { get; set; }
Property Value
Mouse
Current mouse/touch position (Shadertoy convention).
public SKPoint Mouse { get; set; }
Property Value
MouseInitial
Where click/drag started. If zero, means not dragging.
public SKPoint MouseInitial { get; set; }
Property Value
Offset
Top-left offset of the drawing rect. Default (0,0).
public SKPoint Offset { get; set; }
Property Value
TileMode
Tile mode for input textures. Default is Clamp.
public SKShaderTileMode TileMode { get; set; }
Property Value
Time
Elapsed time in seconds, typically fed from a frame timer.
public float Time { get; set; }
Property Value
Methods
ApplyTo(SKPaint, SKImage, float, float)
Apply the shader to a paint using an image as the primary input texture (iImage1). Returns the created SKShader so caller can dispose it, or null if not compiled. The uniforms, children, and texture shader objects are cached on the instance and reused across frames — callers must NOT dispose them.
public SKShader ApplyTo(SKPaint paint, SKImage source, float width, float height)
Parameters
Returns
ApplyTo(SKPaint, float, float)
Apply the shader without an input texture (generative/output-only shaders).
public SKShader ApplyTo(SKPaint paint, float width, float height)
Parameters
Returns
CompileFromCode(string, string, bool, Action<string>)
Compile shader from an SKSL code string.
public void CompileFromCode(string skslCode, string cacheKey = null, bool useCache = false, Action<string> onError = null)
Parameters
CompileFromResource(string, bool, Action<string>)
Compile shader from an embedded resource file.
public void CompileFromResource(string resourcePath, bool useCache = true, Action<string> onError = null)
Parameters
CreateChildren(SKShader)
Creates texture children with the primary texture as iImage1. Override to add additional textures (iImage2, etc.) by setting extra keys on the returned instance.
IMPORTANT: the returned SKRuntimeEffectChildren is OWNED by this SkiaShader and reused every frame. Do not dispose it and do not return a different instance from an override. It is invalidated automatically when the compiled effect changes.
public virtual SKRuntimeEffectChildren CreateChildren(SKShader primaryTexture)
Parameters
primaryTextureSKShader
Returns
CreateTextureShader(SKImage)
Creates (or reuses) a shader from an SKImage for use as a texture uniform. The returned SKShader is cached on the instance and reused while the source image handle and sampling options are unchanged — callers must NOT dispose it. It is disposed automatically when the source, sampling options, or compiled effect change, and in Dispose().
public SKShader CreateTextureShader(SKImage source)
Parameters
sourceSKImage
Returns
CreateUniforms(float, float, float, float)
Creates standard uniforms: iResolution, iImageResolution, iTime, iOffset, iMouse.
Override to add custom uniforms for specialized shaders — the typical pattern is
to call base.CreateUniforms(...) and then set extra keys on the returned
instance.
IMPORTANT: the returned SKRuntimeEffectUniforms is OWNED by this SkiaShader and reused every frame. Do not dispose it and do not return a different instance from an override — mutate and return the same one. It is invalidated automatically when the compiled effect changes.
public virtual SKRuntimeEffectUniforms CreateUniforms(float viewportWidth, float viewportHeight, float imageWidth, float imageHeight)
Parameters
Returns
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
DisposeCompiled()
public void DisposeCompiled()
DrawImage(SKCanvas, SKImage, float, float)
Full render: apply the shader and draw the result on the canvas. The source image is passed to the shader as iImage1 texture; actual drawing uses DrawRect because the shader generates all pixels itself.
public void DrawImage(SKCanvas canvas, SKImage source, float x, float y)
Parameters
DrawRect(SKCanvas, SKImage, SKRect)
Full render: apply the shader and draw a rect on the canvas.
public void DrawRect(SKCanvas canvas, SKImage source, SKRect destination)
Parameters
DrawRect(SKCanvas, SKRect)
Full render without input texture (generative shaders).
public void DrawRect(SKCanvas canvas, SKRect destination)
Parameters
FromCode(string, Action<string>)
Create and compile from inline SKSL code.
public static SkiaShader FromCode(string skslCode, Action<string> onError = null)
Parameters
Returns
FromResource(string, bool, Action<string>)
Create and compile from a resource file path (e.g. "Shaders/myeffect.sksl").
public static SkiaShader FromResource(string resourcePath, bool useCache = true, Action<string> onError = null)
Parameters
Returns
GetPaint()
public SKPaint GetPaint()
Returns
NormalizeLineEndings(string)
public static string NormalizeLineEndings(string text)
Parameters
textstring