Table of Contents

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

SKRuntimeEffect

Properties

BlendMode

Blend mode used when drawing. Default is SrcOver.

public SKBlendMode BlendMode { get; set; }

Property Value

SKBlendMode

Code

Gets the shader code used for last compilation

public string Code { get; }

Property Value

string

Compiled

The compiled SKRuntimeEffect, if available.

public SKRuntimeEffect Compiled { get; }

Property Value

SKRuntimeEffect

FilterMode

Filter mode for the primary input texture. Default is Linear.

public SKFilterMode FilterMode { get; set; }

Property Value

SKFilterMode

IsCompiled

Whether the shader is compiled and ready to use.

public bool IsCompiled { get; }

Property Value

bool

MipmapMode

Mipmap mode for the primary input texture. Default is None.

public SKMipmapMode MipmapMode { get; set; }

Property Value

SKMipmapMode

Mouse

Current mouse/touch position (Shadertoy convention).

public SKPoint Mouse { get; set; }

Property Value

SKPoint

MouseInitial

Where click/drag started. If zero, means not dragging.

public SKPoint MouseInitial { get; set; }

Property Value

SKPoint

Offset

Top-left offset of the drawing rect. Default (0,0).

public SKPoint Offset { get; set; }

Property Value

SKPoint

TileMode

Tile mode for input textures. Default is Clamp.

public SKShaderTileMode TileMode { get; set; }

Property Value

SKShaderTileMode

Time

Elapsed time in seconds, typically fed from a frame timer.

public float Time { get; set; }

Property Value

float

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

paint SKPaint
source SKImage
width float
height float

Returns

SKShader

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

paint SKPaint
width float
height float

Returns

SKShader

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

skslCode string
cacheKey string
useCache bool
onError Action<string>

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

resourcePath string
useCache bool
onError Action<string>

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

primaryTexture SKShader

Returns

SKRuntimeEffectChildren

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

source SKImage

Returns

SKShader

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

viewportWidth float
viewportHeight float
imageWidth float
imageHeight float

Returns

SKRuntimeEffectUniforms

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

canvas SKCanvas
source SKImage
x float
y float

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

canvas SKCanvas
source SKImage
destination SKRect

DrawRect(SKCanvas, SKRect)

Full render without input texture (generative shaders).

public void DrawRect(SKCanvas canvas, SKRect destination)

Parameters

canvas SKCanvas
destination SKRect

FromCode(string, Action<string>)

Create and compile from inline SKSL code.

public static SkiaShader FromCode(string skslCode, Action<string> onError = null)

Parameters

skslCode string
onError Action<string>

Returns

SkiaShader

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

resourcePath string
useCache bool
onError Action<string>

Returns

SkiaShader

GetPaint()

public SKPaint GetPaint()

Returns

SKPaint

NormalizeLineEndings(string)

public static string NormalizeLineEndings(string text)

Parameters

text string

Returns

string