explicit

package
v1.9.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package explicit is the renderer for the explicit GPU APIs, Vulkan and WebGPU (D19): the Device interface that both implement, the design limits they share (SPEC §9.3), and the WGSL shaders with the SPIR-V that glutter-gen makes from them (D20).

Flutter: Impeller's Vulkan and Metal backends (engine/src/flutter/impeller/renderer/backend).

Index

Constants

View Source
const FramesInFlight = 2

FramesInFlight is how many frames the GPU works on at once (SPEC §9.3). A device's frame slots alternate with every Submit, so the renderer keeps one set of dynamic buffers for each, and the frame numbered n uses set n % FramesInFlight.

Flutter: no counterpart

View Source
const GPUDevicePending = "GPUDevicePending"

GPUDevicePending is the tag of the error that a Renderer's open function returns when the backend has no device at the moment and is getting one without blocking: WebGPU asks the browser for a new device after the old one was lost, and the UI goroutine never waits for a promise (SPEC §9.5, §23.3). The renderer draws nothing for that frame and returns no error; the backend tells its owner when the device is there, and the next frame opens it.

Flutter: no counterpart

View Source
const SwapchainOutOfDate = "SwapchainOutOfDate"

SwapchainOutOfDate is the tag of the error that Swapchain.Acquire returns when the swapchain no longer matches its surface (the window was resized, or the platform asks for a new one): the caller configures it again and retries once (SPEC §9.3).

Flutter: no counterpart

View Source
const TagAtlasFull = gpu.TagAtlasFull

TagAtlasFull is the tag of the error of a coverage atlas that has no slot left in the frame. It is produced by the atlas of internal/gpu (and the renderers' backends); this package only compares with it.

Flutter: no counterpart

View Source
const TagGPUDeviceLost = "GPUDeviceLost"

TagGPUDeviceLost is the tag of the error of a lost device of another backend, such as WebGPU's. The backends produce it; this package only compares with it (device loss, SPEC §9.5).

Flutter: no counterpart

View Source
const TagGPUImageTooLarge = "GPUImageTooLarge"

TagGPUImageTooLarge is the tag of the error of an image larger than the textures of the device (made by the resources of this package and compared in the shape code).

Flutter: no counterpart

View Source
const TagVulkanDeviceLost = "VulkanDeviceLost"

TagVulkanDeviceLost is the tag of the error of a lost Vulkan device. It is produced by the Vulkan backend; this package only compares with it (device loss, SPEC §9.3).

Flutter: no counterpart

Variables

View Source
var ShaderBlit = &Shader{
	Name: "blit",
	WGSL: `// Copies the frame texture into the swapchain image, whatever its format
// (SPEC §9.3). Ported from the GLES blit shader (blit.vert.glsl, blit.frag.glsl).
//
// One triangle that covers the whole target, made from the vertex index, so it
// needs no vertex buffer: draw 3 vertices. Clip space points up and texture space
// points down, so the V coordinate is flipped and the picture stays upright.
//
// The Blit uniform is the pre-rotation of the swapchain (explicit.Rotation, SPEC §24.4): the
// image of the swapchain is the frame rotated clockwise by that many quarter turns, so that the
// presentation engine need not rotate it. The renderer picks the rotation with the dynamic offset
// of the binding; it is 0 on every window system but Android's. (It is a uniform and not the first
// instance of the draw, because Direct3D's SV_InstanceID does not include the first instance, SPEC
// §9.8.)

struct Blit {
	// rotation is the number of quarter turns; the other three components are padding.
	rotation: u32,
	pad0: u32,
	pad1: u32,
	pad2: u32,
}

@group(0) @binding(0) var frameTexture: texture_2d<f32>;
@group(0) @binding(1) var frameSampler: sampler;
@group(0) @binding(2) var<uniform> blit: Blit;

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) uv: vec2<f32>,
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32) -> VertexOutput {
	let rotation = blit.rotation;
	let p = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));
	// The position in the image, from its top left, as a fraction.
	let a = p.x;
	let b = 1.0 - p.y;
	// The point of the frame that lands there, when the frame is rotated clockwise by rotation
	// quarter turns: a point (u, v) of the frame goes to (1 - v, u) for one turn.
	var uv = vec2<f32>(a, b);
	if (rotation == 1u) {
		uv = vec2<f32>(b, 1.0 - a);
	} else if (rotation == 2u) {
		uv = vec2<f32>(1.0 - a, 1.0 - b);
	} else if (rotation == 3u) {
		uv = vec2<f32>(1.0 - b, a);
	}
	var out: VertexOutput;
	out.position = vec4<f32>(p * 2.0 - 1.0, 0.0, 1.0);
	out.uv = uv;
	return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	return textureSample(frameTexture, frameSampler, in.uv);
}
`,
	SPIRV: spirvBlit,
	HLSL: `struct Blit {
    uint rotation;
    uint pad0_;
    uint pad1_;
    uint pad2_;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 uv : LOC0;
};

Texture2D<float4> frameTexture : register(t0, space0);
SamplerState frameSampler : register(s1, space0);
cbuffer blit : register(b2, space0) { Blit blit; }

struct VertexOutput_vs_main {
    float2 uv_1 : LOC0;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 uv_2 : LOC0;
    float4 position_1 : SV_Position;
};

VertexOutput_vs_main vs_main(uint index : SV_VertexID)
{
    float2 uv = (float2)0;
    VertexOutput out_ = (VertexOutput)0;

    uint rotation = blit.rotation;
    float2 p = float2(float(((index << 1u) & 2u)), float((index & 2u)));
    float a = p.x;
    float b = (1.0 - p.y);
    uv = float2(a, b);
    if ((rotation == 1u)) {
        uv = float2(b, (1.0 - a));
    } else {
        if ((rotation == 2u)) {
            uv = float2((1.0 - a), (1.0 - b));
        } else {
            if ((rotation == 3u)) {
                uv = float2((1.0 - b), a);
            }
        }
    }
    out_.position = float4(((p * 2.0) - (1.0).xx), 0.0, 1.0);
    float2 _e47 = uv;
    out_.uv = _e47;
    VertexOutput _e48 = out_;
    VertexOutput vertexoutput = _e48;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.uv, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.uv_2 };
    float4 _e4 = frameTexture.Sample(frameSampler, in_.uv);
    return _e4;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "blit", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "frameTexture", Group: 0, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 0, Index: 0}},
			{Name: "frameSampler", Group: 0, Binding: 1, Kind: BindingSampler, Register: ShaderRegister{Class: "s", Space: 0, Index: 1}},
		}},
	},
}

ShaderBlit is shaders/blit.wgsl.

Flutter: no counterpart

View Source
var ShaderFilter = &Shader{
	Name: "filter",
	WGSL: `// One pass of an image filter over a layer: a copy, one axis of a Gaussian blur, one axis of a
// dilation or erosion, a transform of the pixels, a color filter, or the two resamplings that let
// a large blur work on a smaller picture (gpu.FilterPass). Ported from
// gles/shaders/filter.frag.glsl. All of them read the source with textureLoad and write
// premultiplied pixels, rows counted from the top as in the surface. Draw 3 vertices: one triangle
// covers the whole target.

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}

const PASS_COPY: i32 = 0;
const PASS_BLUR: i32 = 1;
const PASS_MORPH: i32 = 2;
const PASS_MATRIX: i32 = 3;
const PASS_COLOR: i32 = 4;
const PASS_DOWN: i32 = 5;
const PASS_UP: i32 = 6;

// What a pass sets.
struct Pass {
	// The pass kind, the axis (0 along x, 1 along y), the tile mode beyond the edges (0 clamp,
	// 1 repeated, 2 mirror, 3 decal), and 1 for a dilation or 0 for an erosion.
	kind: vec4<f32>,
	// The size of the source in pixels, and of the target.
	sizes: vec4<f32>,
	// The Gaussian's deviation, the taps on each side of the center, and 1 over the sum of the
	// weights.
	blur: vec4<f32>,
	// Resampling: the source pixels per output pixel in x and y; and the quality of a matrix pass
	// (0 bilinear, 1 nearest, 2 draws nothing).
	factor: vec4<f32>,
	// A matrix pass: from output to source pixels of the surface, a, b, c, d, then e, f and the
	// surface pixel of the layer's top left corner.
	mat0: vec4<f32>,
	mat1: vec4<f32>,
}

@group(0) @binding(1) var<uniform> params: Pass;
@group(1) @binding(0) var src: texture_2d<f32>;

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32) -> VertexOutput {
	let p = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));
	var out: VertexOutput;
	out.position = vec4<f32>(p * 2.0 - 1.0, 0.0, 1.0);
	return out;
}

// i modulo m in 0..m-1, also for a negative i.
fn floorMod(i: i32, m: i32) -> i32 {
	return i - m * i32(floor(f32(i) / f32(m)));
}

// The index in 0..n-1 that i stands for under the tile mode; the second component is 0 for
// transparent.
fn wrapIndex(i: i32, n: i32, mode: i32) -> vec2<i32> {
	if mode == 1 {
		return vec2<i32>(floorMod(i, n), 1);
	}
	if mode == 2 {
		let m = floorMod(i, 2 * n);
		return vec2<i32>(select(m, 2 * n - 1 - m, m >= n), 1);
	}
	if mode == 3 {
		return vec2<i32>(i, select(0, 1, i >= 0 && i < n));
	}
	return vec2<i32>(clamp(i, 0, n - 1), 1);
}

// The pixel at column x and row y from the top of a texture of the given size.
fn fetchTop(p: vec2<i32>, size: vec2<i32>, mode: i32) -> vec4<f32> {
	let x = wrapIndex(p.x, size.x, mode);
	let y = wrapIndex(p.y, size.y, mode);
	if x.y == 0 || y.y == 0 {
		return vec4<f32>(0.0);
	}
	return textureLoad(src, vec2<i32>(x.x, y.x), 0);
}

// The same without tiling: transparent outside.
fn fetchOrClear(p: vec2<i32>, size: vec2<i32>) -> vec4<f32> {
	return fetchTop(p, size, 3);
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	let srcSize = vec2<i32>(params.sizes.xy + vec2<f32>(0.5));
	let q = vec2<i32>(floor(in.position.xy));
	let kind = i32(params.kind.x + 0.5);
	let axis = select(vec2<i32>(0, 1), vec2<i32>(1, 0), params.kind.y < 0.5);
	let tile = i32(params.kind.z + 0.5);
	let radius = i32(params.blur.y + 0.5);
	var o = vec4<f32>(0.0);

	if kind == PASS_COPY {
		o = fetchOrClear(q, srcSize);
	} else if kind == PASS_BLUR {
		let k = -0.5 / (params.blur.x * params.blur.x);
		for (var i = -radius; i <= radius; i++) {
			o += fetchTop(q + axis * i, srcSize, tile) * exp(f32(i * i) * k);
		}
		o *= params.blur.z;
	} else if kind == PASS_MORPH {
		let dilate = params.kind.w > 0.5;
		o = select(vec4<f32>(1.0), vec4<f32>(0.0), dilate);
		for (var i = -radius; i <= radius; i++) {
			let c = fetchOrClear(q + axis * i, srcSize);
			o = select(min(o, c), max(o, c), dilate);
		}
	} else if kind == PASS_MATRIX {
		if params.factor.z < 1.5 {
			let origin = params.mat1.zw;
			let dev = origin + vec2<f32>(q) + vec2<f32>(0.5);
			let s = vec2<f32>(
				params.mat0.x * dev.x + params.mat0.z * dev.y + params.mat1.x,
				params.mat0.y * dev.x + params.mat0.w * dev.y + params.mat1.y) - origin;
			if params.factor.z > 0.5 {
				o = fetchOrClear(vec2<i32>(floor(s)), srcSize);
			} else {
				let f = s - vec2<f32>(0.5);
				let i0 = vec2<i32>(floor(f));
				let t = f - vec2<f32>(i0);
				let top = mix(fetchOrClear(i0, srcSize), fetchOrClear(i0 + vec2<i32>(1, 0), srcSize), t.x);
				let bot = mix(fetchOrClear(i0 + vec2<i32>(0, 1), srcSize), fetchOrClear(i0 + vec2<i32>(1, 1), srcSize), t.x);
				o = mix(top, bot, t.y);
			}
		}
	} else if kind == PASS_COLOR {
		o = clamp(applyFilters(fetchOrClear(q, srcSize)), vec4<f32>(0.0), vec4<f32>(1.0));
	} else if kind == PASS_DOWN {
		let f = vec2<i32>(params.factor.xy + vec2<f32>(0.5));
		for (var j = 0; j < f.y; j++) {
			for (var i = 0; i < f.x; i++) {
				o += fetchTop(q * f + vec2<i32>(i, j), srcSize, tile);
			}
		}
		o /= f32(f.x * f.y);
	} else {
		// Bilinear, from a picture with texels factor times as large.
		let f = (vec2<f32>(q) + vec2<f32>(0.5)) / params.factor.xy - vec2<f32>(0.5);
		let i0 = vec2<i32>(floor(f));
		let t = f - vec2<f32>(i0);
		let top = mix(fetchTop(i0, srcSize, 0), fetchTop(i0 + vec2<i32>(1, 0), srcSize, 0), t.x);
		let bot = mix(fetchTop(i0 + vec2<i32>(0, 1), srcSize, 0), fetchTop(i0 + vec2<i32>(1, 1), srcSize, 0), t.x);
		o = mix(top, bot, t.y);
	}
	return o;
}
`,
	SPIRV: spirvFilter,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Pass_ {
    float4 kind;
    float4 sizes;
    float4 blur;
    float4 factor;
    float4 mat0_;
    float4 mat1_;
};

struct VertexOutput {
    float4 position : SV_Position;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;
static const int PASS_COPY = int(0);
static const int PASS_BLUR = int(1);
static const int PASS_MORPH = int(2);
static const int PASS_MATRIX = int(3);
static const int PASS_COLOR = int(4);
static const int PASS_DOWN = int(5);
static const int PASS_UP = int(6);

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer params : register(b1, space0) { Pass_ params; }
Texture2D<float4> src_1 : register(t0, space1);

struct VertexOutput_vs_main {
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d * (1.0 - sa))) + ((2.0 * s) * d));
    }
    return ((((s * (1.0 - da)) + (d * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d)) * (sa - s)));
}

float softLightChannel(float s_1, float d_1, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_1 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_1 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_1) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_1 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_1 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_2, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_2) - max((s_2 * da_2), (d_2 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_2) - min((s_2 * da_2), (d_2 * sa_2)));
        }
        case 18: {
            if ((d_2 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_2 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_2 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_2 == da_2)) {
                return (d_2 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_2 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_2) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_2) - (2.0 * min((s_2 * da_2), (d_2 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_2) - ((2.0 * s_2) * d_2));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_2 * (1.0 - sa_2))) + (s_2 * d_2));
        }
        default: {
            return (s_2 + (d_2 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_3)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_3.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_3.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_3.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_3.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_3.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_3.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_3.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_3.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_4)
{
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_4;
        }
        case 3: {
            return (s_5 + (d_4 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_4 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_4 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_4 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_4 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_4 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_4 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_4), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_4);
        }
        case 14: {
            return ((s_5 + d_4) - (s_5 * d_4));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local = (t <= 1.0);
        } else {
            local = false;
        }
        bool _e33 = local;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i_3)
{
    float _e9 = paint.rampPos[(i_3 >> 2u)][(i_3 & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_4 = int(0);
    bool local_1 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n_1 = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_4;
        if ((_e10 < n_1)) {
            int _e14 = i_4;
            const float _e15 = rampPosAt(_e14);
            local_1 = (_e15 < t_1);
        } else {
            local_1 = false;
        }
        bool _e18 = local_1;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_4;
            i_4 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_4;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_4;
        if ((_e30 >= n_1)) {
            float4 _e37 = paint.rampColor[asint(asuint(n_1) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_4;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_4;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_4;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_4;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_4;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_2 = (dot(cd, cd) - (dr * dr));
    float2 p_5 = (q - c0_1);
    float b = (dot(p_5, cd) + (r0_ * dr));
    float c_10 = (dot(p_5, p_5) - (r0_ * r0_));
    if ((abs(a_2) < 1e-12)) {
        if ((b == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_10 / (2.0 * b));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b * b) - (a_2 * c_10));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b + sq) / a_2);
        t2_ = ((b - sq) / a_2);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_2 = (bool)0;
    bool local_3 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_2 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_2));
            } else {
                x = m_2;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_2 = (_e61 >= size.x);
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_3 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_3 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_3));
            } else {
                y = m_3;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_3 = (_e114 >= size.y);
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_1 = abs(x0_);
    if ((x_1 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_1) * x_1)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_1 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_1) * x_1)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_1)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j_1 = int(-1);
    int i_5 = (int)0;

    int2 base_1 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_1 = naga_f2i32((_e15 + 0.5));
    int2 size_3 = int2(NagaMipDimensions2D(paintImage, level_1));
    float2 uv = ((q_1 * float2(size_3)) / float2(base_1));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv)), size_3, level_1);
        return _e30;
    }
    float2 f = (uv - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j_1;
                j_1 = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j_1;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j_1;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_5 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_5;
                        i_5 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_5;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_5;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_5;
                        int _e65 = j_1;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_3, level_1);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_3).xxx), a_3);
    }
    const float4 _e89 = shaderTexel(i0_, size_3, level_1);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_3, level_1);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_3, level_1);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_3, level_1);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_2 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_2 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_2 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_2);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_5 = (q_2 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_5.y, d_5.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_2);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v)
{
    return ((v <= (0.0031308).xxx) ? (12.92 * v) : ((1.055 * pow(v, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_1)
{
    return ((v_1 <= (0.04045).xxx) ? (v_1 / (12.92).xxx) : pow(((v_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_11 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_2 = (((((_e32 * c_11.x) + (_e38 * c_11.y)) + (_e45 * c_11.z)) + (_e52 * c_11.w)) + _e59);
            const float4 _e69 = premul(clamp((o_2 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_6 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_6.y) * asuint(int(8)))) + asuint(p_6.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o_1 = (float4)0;
    bool local_4 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o_1 = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o_1 = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_4 = (src.w > 0.0);
    } else {
        local_4 = false;
    }
    bool _e35 = local_4;
    if (_e35) {
        float4 _e36 = o_1;
        const float _e37 = ditherAt(dev_2);
        o_1 = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o_1;
    return _e40;
}

int floorMod(int i_6, int m_1)
{
    return asint(asuint(i_6) - asuint(asint(asuint(m_1) * asuint(naga_f2i32(floor((float(i_6) / float(m_1))))))));
}

int2 wrapIndex(int i_7, int n, int mode_4)
{
    bool local_5 = (bool)0;

    if ((mode_4 == int(1))) {
        const int _e5 = floorMod(i_7, n);
        return int2(_e5, int(1));
    }
    if ((mode_4 == int(2))) {
        const int _e12 = floorMod(i_7, asint(asuint(int(2)) * asuint(n)));
        return int2(((_e12 >= n) ? asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(n))) - asuint(int(1)))) - asuint(_e12)) : _e12), int(1));
    }
    if ((mode_4 == int(3))) {
        if ((i_7 >= int(0))) {
            local_5 = (i_7 < n);
        } else {
            local_5 = false;
        }
        bool _e30 = local_5;
        return int2(i_7, (_e30 ? int(1) : int(0)));
    }
    return int2(clamp(i_7, int(0), asint(asuint(n) - asuint(int(1)))), int(1));
}

float4 fetchTop(int2 p_3, int2 size_1, int mode_5)
{
    bool local_6 = (bool)0;

    const int2 _e5 = wrapIndex(p_3.x, size_1.x, mode_5);
    const int2 _e8 = wrapIndex(p_3.y, size_1.y, mode_5);
    if (!((_e5.y == int(0)))) {
        local_6 = (_e8.y == int(0));
    } else {
        local_6 = true;
    }
    bool _e19 = local_6;
    if (_e19) {
        return (0.0).xxxx;
    }
    float4 _e27 = src_1.Load(int3(int2(_e5.x, _e8.x), int(0)));
    return _e27;
}

float4 fetchOrClear(int2 p_4, int2 size_2)
{
    const float4 _e3 = fetchTop(p_4, size_2, int(3));
    return _e3;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID)
{
    VertexOutput out_ = (VertexOutput)0;

    float2 p_7 = float2(float(((index << 1u) & 2u)), float((index & 2u)));
    out_.position = float4(((p_7 * 2.0) - (1.0).xx), 0.0, 1.0);
    VertexOutput _e20 = out_;
    VertexOutput vertexoutput = _e20;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.position };
    return vertexoutput_1;
}

int naga_neg(int val) {
    return asint(-asuint(val));
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1 };
    float4 o = (0.0).xxxx;
    int i = (int)0;
    int i_1 = (int)0;
    int j = int(0);
    int i_2 = (int)0;

    float4 _e3 = params.sizes;
    int2 srcSize = naga_f2i32((_e3.xy + (0.5).xx));
    int2 q_3 = naga_f2i32(floor(in_.position.xy));
    float _e16 = params.kind.x;
    int kind_2 = naga_f2i32((_e16 + 0.5));
    float _e29 = params.kind.y;
    int2 axis = ((_e29 < 0.5) ? int2(int(1), int(0)) : int2(int(0), int(1)));
    float _e36 = params.kind.z;
    int tile = naga_f2i32((_e36 + 0.5));
    float _e43 = params.blur.y;
    int radius = naga_f2i32((_e43 + 0.5));
    if ((kind_2 == PASS_COPY)) {
        const float4 _e52 = fetchOrClear(q_3, srcSize);
        o = _e52;
    } else {
        if ((kind_2 == PASS_BLUR)) {
            float _e58 = params.blur.x;
            float _e62 = params.blur.x;
            float k = (-0.5 / (_e58 * _e62));
            i = naga_neg(radius);
            uint2 loop_bound_3 = uint2(4294967295u, 4294967295u);
            bool loop_init_2 = true;
            while(true) {
                if (all(loop_bound_3 == uint2(0u, 0u))) { break; }
                loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
                if (!loop_init_2) {
                    int _e84 = i;
                    i = asint(asuint(_e84) + asuint(int(1)));
                }
                loop_init_2 = false;
                int _e68 = i;
                if ((_e68 <= radius)) {
                } else {
                    break;
                }
                {
                    float4 _e70 = o;
                    int _e71 = i;
                    const float4 _e74 = fetchTop(asint(asuint(q_3) + asuint(asint(asuint(axis) * asuint(_e71)))), srcSize, tile);
                    int _e75 = i;
                    int _e76 = i;
                    o = (_e70 + (_e74 * exp((float(asint(asuint(_e75) * asuint(_e76))) * k))));
                }
            }
            float4 _e86 = o;
            float _e90 = params.blur.z;
            o = (_e86 * _e90);
        } else {
            if ((kind_2 == PASS_MORPH)) {
                float _e97 = params.kind.w;
                bool dilate = (_e97 > 0.5);
                o = (dilate ? (0.0).xxxx : (1.0).xxxx);
                i_1 = naga_neg(radius);
                uint2 loop_bound_4 = uint2(4294967295u, 4294967295u);
                bool loop_init_3 = true;
                while(true) {
                    if (all(loop_bound_4 == uint2(0u, 0u))) { break; }
                    loop_bound_4 -= uint2(loop_bound_4.y == 0u, 1u);
                    if (!loop_init_3) {
                        int _e119 = i_1;
                        i_1 = asint(asuint(_e119) + asuint(int(1)));
                    }
                    loop_init_3 = false;
                    int _e107 = i_1;
                    if ((_e107 <= radius)) {
                    } else {
                        break;
                    }
                    {
                        int _e109 = i_1;
                        const float4 _e112 = fetchOrClear(asint(asuint(q_3) + asuint(asint(asuint(axis) * asuint(_e109)))), srcSize);
                        float4 _e113 = o;
                        float4 _e115 = o;
                        o = (dilate ? max(_e115, _e112) : min(_e113, _e112));
                    }
                }
            } else {
                if ((kind_2 == PASS_MATRIX)) {
                    float _e126 = params.factor.z;
                    if ((_e126 < 1.5)) {
                        float4 _e131 = params.mat1_;
                        float2 origin = _e131.zw;
                        float2 dev_3 = ((origin + float2(q_3)) + (0.5).xx);
                        float _e141 = params.mat0_.x;
                        float _e147 = params.mat0_.z;
                        float _e154 = params.mat1_.x;
                        float _e159 = params.mat0_.y;
                        float _e165 = params.mat0_.w;
                        float _e172 = params.mat1_.y;
                        float2 s_6 = (float2((((_e141 * dev_3.x) + (_e147 * dev_3.y)) + _e154), (((_e159 * dev_3.x) + (_e165 * dev_3.y)) + _e172)) - origin);
                        float _e179 = params.factor.z;
                        if ((_e179 > 0.5)) {
                            const float4 _e184 = fetchOrClear(naga_f2i32(floor(s_6)), srcSize);
                            o = _e184;
                        } else {
                            float2 f_1 = (s_6 - (0.5).xx);
                            int2 i0_1 = naga_f2i32(floor(f_1));
                            float2 t_4 = (f_1 - float2(i0_1));
                            const float4 _e192 = fetchOrClear(i0_1, srcSize);
                            const float4 _e197 = fetchOrClear(asint(asuint(i0_1) + asuint(int2(int(1), int(0)))), srcSize);
                            float4 top = lerp(_e192, _e197, t_4.x);
                            const float4 _e204 = fetchOrClear(asint(asuint(i0_1) + asuint(int2(int(0), int(1)))), srcSize);
                            const float4 _e209 = fetchOrClear(asint(asuint(i0_1) + asuint(int2(int(1), int(1)))), srcSize);
                            float4 bot = lerp(_e204, _e209, t_4.x);
                            o = lerp(top, bot, t_4.y);
                        }
                    }
                } else {
                    if ((kind_2 == PASS_COLOR)) {
                        const float4 _e216 = fetchOrClear(q_3, srcSize);
                        const float4 _e217 = applyFilters(_e216);
                        o = clamp(_e217, (0.0).xxxx, (1.0).xxxx);
                    } else {
                        if ((kind_2 == PASS_DOWN)) {
                            float4 _e227 = params.factor;
                            int2 f_2 = naga_f2i32((_e227.xy + (0.5).xx));
                            uint2 loop_bound_5 = uint2(4294967295u, 4294967295u);
                            bool loop_init_4 = true;
                            while(true) {
                                if (all(loop_bound_5 == uint2(0u, 0u))) { break; }
                                loop_bound_5 -= uint2(loop_bound_5.y == 0u, 1u);
                                if (!loop_init_4) {
                                    int _e255 = j;
                                    j = asint(asuint(_e255) + asuint(int(1)));
                                }
                                loop_init_4 = false;
                                int _e235 = j;
                                if ((_e235 < f_2.y)) {
                                } else {
                                    break;
                                }
                                {
                                    i_2 = int(0);
                                    uint2 loop_bound_6 = uint2(4294967295u, 4294967295u);
                                    bool loop_init_5 = true;
                                    while(true) {
                                        if (all(loop_bound_6 == uint2(0u, 0u))) { break; }
                                        loop_bound_6 -= uint2(loop_bound_6.y == 0u, 1u);
                                        if (!loop_init_5) {
                                            int _e252 = i_2;
                                            i_2 = asint(asuint(_e252) + asuint(int(1)));
                                        }
                                        loop_init_5 = false;
                                        int _e240 = i_2;
                                        if ((_e240 < f_2.x)) {
                                        } else {
                                            break;
                                        }
                                        {
                                            float4 _e243 = o;
                                            int _e245 = i_2;
                                            int _e246 = j;
                                            const float4 _e249 = fetchTop(asint(asuint(asint(asuint(q_3) * asuint(f_2))) + asuint(int2(_e245, _e246))), srcSize, tile);
                                            o = (_e243 + _e249);
                                        }
                                    }
                                }
                            }
                            float4 _e257 = o;
                            o = (_e257 / (float(asint(asuint(f_2.x) * asuint(f_2.y)))).xxxx);
                        } else {
                            float4 _e270 = params.factor;
                            float2 f_3 = (((float2(q_3) + (0.5).xx) / _e270.xy) - (0.5).xx);
                            int2 i0_2 = naga_f2i32(floor(f_3));
                            float2 t_5 = (f_3 - float2(i0_2));
                            const float4 _e281 = fetchTop(i0_2, srcSize, int(0));
                            const float4 _e287 = fetchTop(asint(asuint(i0_2) + asuint(int2(int(1), int(0)))), srcSize, int(0));
                            float4 top_1 = lerp(_e281, _e287, t_5.x);
                            const float4 _e295 = fetchTop(asint(asuint(i0_2) + asuint(int2(int(0), int(1)))), srcSize, int(0));
                            const float4 _e301 = fetchTop(asint(asuint(i0_2) + asuint(int2(int(1), int(1)))), srcSize, int(0));
                            float4 bot_1 = lerp(_e295, _e301, t_5.x);
                            o = lerp(top_1, bot_1, t_5.y);
                        }
                    }
                }
            }
        }
    }
    float4 _e306 = o;
    return _e306;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "params", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "src", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
		}},
	},
}

ShaderFilter is shaders/filter.wgsl.

Flutter: no counterpart

View Source
var ShaderImage = &Shader{
	Name: "image",
	WGSL: `// Images: one instance is one rectangle of an image, drawn through an affine transform. The
// fragment shader maps each pixel back to the image's texels and samples them with the paint's
// filter quality: nearest, bilinear, bilinear on one mip level, or bicubic. Ported from the
// GLES image shaders (image.vert.glsl, image.frag.glsl).
// Draw 6 vertices per instance.

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is.
	origin: vec2<f32>,
}

// The clip mask's rectangle in the surface (x, y, width, height; a width of 0 means none).
struct Draw {
	clipMask: vec4<f32>,
}

@group(0) @binding(0) var<uniform> frame: Frame;
@group(0) @binding(1) var<uniform> draw: Draw;
@group(1) @binding(0) var clipMask: texture_2d<f32>;
// The image: premultiplied RGBA with mipmaps, and a linear sampler that picks a level.
@group(2) @binding(0) var image: texture_2d<f32>;
@group(2) @binding(1) var imageSampler: sampler;

struct Instance {
	@location(0) quad: vec4<f32>,  // left, top, right, bottom in pixels
	@location(1) inv0: vec4<f32>,  // pixels to texels: a, b, c, d
	@location(2) inv1: vec4<f32>,  // e, f, flags, unused
	@location(3) color: vec4<f32>, // premultiplied multiplier: the paint's alpha
	@location(4) clip: vec4<f32>,  // left, top, right, bottom in pixels
	@location(5) box: vec4<f32>,   // the rectangle drawn: texels, or pixels with the box flag
	@location(6) img: vec4<f32>,   // level of detail, quality, blend index (-1 none), unused
	@location(7) tint: vec4<f32>,  // premultiplied color blended with the image
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) dev: vec2<f32>,
	@location(1) @interpolate(flat) inv0: vec4<f32>,
	@location(2) @interpolate(flat) inv1: vec4<f32>,
	@location(3) @interpolate(flat) color: vec4<f32>,
	@location(4) @interpolate(flat) clip: vec4<f32>,
	@location(5) @interpolate(flat) box: vec4<f32>,
	@location(6) @interpolate(flat) img: vec4<f32>,
	@location(7) @interpolate(flat) tint: vec4<f32>,
}

const FLAG_BOX: i32 = 1;       // box is in pixels: exact coverage of an axis-aligned rectangle
const FLAG_ANTIALIAS: i32 = 2;

const QUALITY_NONE: i32 = 0;
const QUALITY_HIGH: i32 = 3;

fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32, instance: Instance) -> VertexOutput {
	var corners = array<vec2<f32>, 6>(
		vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
		vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0),
	);
	let dev = mix(instance.quad.xy, instance.quad.zw, corners[index]);
	var out: VertexOutput;
	out.position = toClip(dev);
	out.dev = dev;
	out.inv0 = instance.inv0;
	out.inv1 = instance.inv1;
	out.color = instance.color;
	out.clip = instance.clip;
	out.box = instance.box;
	out.img = instance.img;
	out.tint = instance.tint;
	return out;
}

// The part of the pixel centered at c that the interval [lo, hi] covers.
fn span(c: f32, lo: f32, hi: f32) -> f32 {
	return clamp(min(c + 0.5, hi) - max(c - 0.5, lo), 0.0, 1.0);
}

// The part of the pixel centered at dev that the rectangle r covers; without antialiasing, 1
// when the center is inside.
fn boxCoverage(dev: vec2<f32>, r: vec4<f32>, antialias: bool) -> f32 {
	if antialias {
		return span(dev.x, r.x, r.z) * span(dev.y, r.y, r.w);
	}
	if dev.x >= r.x && dev.x < r.z && dev.y >= r.y && dev.y < r.w {
		return 1.0;
	}
	return 0.0;
}

fn clipMaskCoverage(dev: vec2<f32>) -> f32 {
	if draw.clipMask.z <= 0.0 {
		return 1.0;
	}
	let p = vec2<i32>(floor(dev)) - vec2<i32>(draw.clipMask.xy);
	if p.x < 0 || p.y < 0 || p.x >= i32(draw.clipMask.z) || p.y >= i32(draw.clipMask.w) {
		return 0.0;
	}
	return textureLoad(clipMask, p, 0).r;
}

// The Mitchell-Netravali cubic with B = C = 1/3.
fn mitchell(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// A texel of a level, with the edge repeated outside.
fn texel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	return textureLoad(image, clamp(p, vec2<i32>(0), size - vec2<i32>(1)), level);
}

fn bicubic(uv: vec2<f32>, base: vec2<i32>, level: i32) -> vec4<f32> {
	let size = vec2<i32>(textureDimensions(image, level));
	let f = uv * vec2<f32>(size) / vec2<f32>(base) - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	var sum = vec4<f32>(0.0);
	for (var j = -1; j <= 2; j++) {
		let wy = mitchell(f32(j) - t.y);
		for (var i = -1; i <= 2; i++) {
			let w = wy * mitchell(f32(i) - t.x);
			sum += w * texel(vec2<i32>(f0) + vec2<i32>(i, j), size, level);
		}
	}
	// The cubic can overshoot: keep a valid premultiplied color.
	let a = clamp(sum.a, 0.0, 1.0);
	return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	let dev = floor(in.dev) + vec2<f32>(0.5);
	let flags = i32(in.inv1.z + 0.5);
	let aa = (flags & FLAG_ANTIALIAS) != 0;
	let uv = vec2<f32>(
		in.inv0.x * dev.x + in.inv0.z * dev.y + in.inv1.x,
		in.inv0.y * dev.x + in.inv0.w * dev.y + in.inv1.y);

	// The rectangle in texels: the distance outside it, in pixels by the screen derivatives.
	// The derivatives come before anything that returns or discards.
	let c = (in.box.xy + in.box.zw) * 0.5;
	let q = abs(uv - c) - (in.box.zw - in.box.xy) * 0.5;
	let d = max(q.x, q.y);
	let g = length(vec2<f32>(dpdx(d), dpdy(d)));

	var coverage: f32;
	if (flags & FLAG_BOX) != 0 {
		coverage = boxCoverage(dev, in.box, aa);
	} else if aa {
		var pixels = select(1.0, -1.0, d <= 0.0);
		if g > 1e-9 {
			pixels = d / g;
		}
		coverage = clamp(0.5 - pixels, 0.0, 1.0);
	} else {
		coverage = select(0.0, 1.0, d <= 0.0);
	}
	coverage = coverage * span(dev.x, in.clip.x, in.clip.z) * span(dev.y, in.clip.y, in.clip.w) * clipMaskCoverage(dev);

	let base = vec2<i32>(textureDimensions(image, 0));
	let quality = i32(in.img.y + 0.5);
	var color: vec4<f32>;
	if quality == QUALITY_NONE {
		color = texel(vec2<i32>(floor(uv)), base, 0);
	} else if quality == QUALITY_HIGH {
		color = bicubic(uv, base, i32(in.img.x + 0.5));
	} else {
		color = textureSampleLevel(image, imageSampler, uv / vec2<f32>(base), in.img.x);
	}
	color = color * in.color;
	if in.img.z >= 0.0 {
		color = blendPair(i32(in.img.z + 0.5), color, in.tint);
	}
	color = applyFilters(color);
	if coverage <= 0.0 {
		discard;
	}
	return finish(color, coverage, dev);
}
`,
	SPIRV: spirvImage,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Frame {
    float2 surface;
    float2 origin;
};

struct Draw {
    float4 clipMask;
};

struct Instance {
    float4 quad : LOC0;
    float4 inv0_ : LOC1;
    float4 inv1_ : LOC2;
    float4 color : LOC3;
    float4 clip_ : LOC4;
    float4 box : LOC5;
    float4 img : LOC6;
    float4 tint : LOC7;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 dev : LOC0;
    nointerpolation float4 inv0_ : LOC1;
    nointerpolation float4 inv1_ : LOC2;
    nointerpolation float4 color : LOC3;
    nointerpolation float4 clip_ : LOC4;
    nointerpolation float4 box : LOC5;
    nointerpolation float4 img : LOC6;
    nointerpolation float4 tint : LOC7;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;
static const int FLAG_BOX = int(1);
static const int FLAG_ANTIALIAS = int(2);
static const int QUALITY_NONE = int(0);
static const int QUALITY_HIGH = int(3);

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer frame : register(b0, space0) { Frame frame; }
cbuffer draw : register(b1, space0) { Draw draw; }
Texture2D<float4> clipMask : register(t0, space1);
Texture2D<float4> image : register(t0, space2);
SamplerState imageSampler : register(s1, space2);

struct VertexOutput_vs_main {
    float2 dev_6 : LOC0;
    nointerpolation float4 inv0_ : LOC1;
    nointerpolation float4 inv1_ : LOC2;
    nointerpolation float4 color_1 : LOC3;
    nointerpolation float4 clip_ : LOC4;
    nointerpolation float4 box : LOC5;
    nointerpolation float4 img : LOC6;
    nointerpolation float4 tint : LOC7;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 dev_7 : LOC0;
    nointerpolation float4 inv0_1 : LOC1;
    nointerpolation float4 inv1_1 : LOC2;
    nointerpolation float4 color_2 : LOC3;
    nointerpolation float4 clip_1 : LOC4;
    nointerpolation float4 box_1 : LOC5;
    nointerpolation float4 img_1 : LOC6;
    nointerpolation float4 tint_1 : LOC7;
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d * (1.0 - sa))) + ((2.0 * s) * d));
    }
    return ((((s * (1.0 - da)) + (d * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d)) * (sa - s)));
}

float softLightChannel(float s_1, float d_1, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_1 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_1 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_1) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_1 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_1 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_2, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_2) - max((s_2 * da_2), (d_2 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_2) - min((s_2 * da_2), (d_2 * sa_2)));
        }
        case 18: {
            if ((d_2 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_2 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_2 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_2 == da_2)) {
                return (d_2 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_2 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_2) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_2) - (2.0 * min((s_2 * da_2), (d_2 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_2) - ((2.0 * s_2) * d_2));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_2 * (1.0 - sa_2))) + (s_2 * d_2));
        }
        default: {
            return (s_2 + (d_2 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_3)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_3.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_3.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_3.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_3.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_3.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_3.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_3.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_3.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_4)
{
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_4;
        }
        case 3: {
            return (s_5 + (d_4 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_4 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_4 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_4 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_4 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_4 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_4 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_4), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_4);
        }
        case 14: {
            return ((s_5 + d_4) - (s_5 * d_4));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local = (t <= 1.0);
        } else {
            local = false;
        }
        bool _e33 = local;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i)
{
    float _e9 = paint.rampPos[(i >> 2u)][(i & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_1 = int(0);
    bool local_1 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_1;
        if ((_e10 < n)) {
            int _e14 = i_1;
            const float _e15 = rampPosAt(_e14);
            local_1 = (_e15 < t_1);
        } else {
            local_1 = false;
        }
        bool _e18 = local_1;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_1;
            i_1 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_1;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_1;
        if ((_e30 >= n)) {
            float4 _e37 = paint.rampColor[asint(asuint(n) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_1;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_1;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_1;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_1;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_1;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_2 = (dot(cd, cd) - (dr * dr));
    float2 p_4 = (q - c0_1);
    float b = (dot(p_4, cd) + (r0_ * dr));
    float c_11 = (dot(p_4, p_4) - (r0_ * r0_));
    if ((abs(a_2) < 1e-12)) {
        if ((b == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_11 / (2.0 * b));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b * b) - (a_2 * c_11));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b + sq) / a_2);
        t2_ = ((b - sq) / a_2);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_2 = (bool)0;
    bool local_3 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_1 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_1 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_1));
            } else {
                x = m_1;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_2 = (_e61 >= size.x);
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_2 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_2));
            } else {
                y = m_2;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_3 = (_e114 >= size.y);
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_1 = abs(x0_);
    if ((x_1 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_1) * x_1)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_1 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_1) * x_1)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_1)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j = int(-1);
    int i_2 = (int)0;

    int2 base_2 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_3 = naga_f2i32((_e15 + 0.5));
    int2 size_2 = int2(NagaMipDimensions2D(paintImage, level_3));
    float2 uv_1 = ((q_1 * float2(size_2)) / float2(base_2));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv_1)), size_2, level_3);
        return _e30;
    }
    float2 f = (uv_1 - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_2 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_2;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_2;
                        int _e65 = j;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_2, level_3);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_3).xxx), a_3);
    }
    const float4 _e89 = shaderTexel(i0_, size_2, level_3);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_2, level_3);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_2, level_3);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_2, level_3);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_2 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_2 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_2 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_2);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_5 = (q_2 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_5.y, d_5.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_2);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v)
{
    return ((v <= (0.0031308).xxx) ? (12.92 * v) : ((1.055 * pow(v, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_1)
{
    return ((v_1 <= (0.04045).xxx) ? (v_1 / (12.92).xxx) : pow(((v_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_12 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_1 = (((((_e32 * c_12.x) + (_e38 * c_12.y)) + (_e45 * c_12.z)) + (_e52 * c_12.w)) + _e59);
            const float4 _e69 = premul(clamp((o_1 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_5 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_5.y) * asuint(int(8)))) + asuint(p_5.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o = (float4)0;
    bool local_4 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_4 = (src.w > 0.0);
    } else {
        local_4 = false;
    }
    bool _e35 = local_4;
    if (_e35) {
        float4 _e36 = o;
        const float _e37 = ditherAt(dev_2);
        o = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o;
    return _e40;
}

float4 toClip(float2 dev_3)
{
    float2 _e3 = frame.origin;
    float2 p_6 = (dev_3 - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p_6.x / _e9) * 2.0) - 1.0), (1.0 - ((p_6.y / _e19) * 2.0)), 0.0, 1.0);
}

float span(float c_10, float lo, float hi)
{
    return clamp((min((c_10 + 0.5), hi) - max((c_10 - 0.5), lo)), 0.0, 1.0);
}

float boxCoverage(float2 dev_4, float4 r, bool antialias)
{
    bool local_5 = (bool)0;
    bool local_6 = (bool)0;
    bool local_7 = (bool)0;

    if (antialias) {
        const float _e6 = span(dev_4.x, r.x, r.z);
        const float _e10 = span(dev_4.y, r.y, r.w);
        return (_e6 * _e10);
    }
    if ((dev_4.x >= r.x)) {
        local_5 = (dev_4.x < r.z);
    } else {
        local_5 = false;
    }
    bool _e21 = local_5;
    if (_e21) {
        local_6 = (dev_4.y >= r.y);
    } else {
        local_6 = false;
    }
    bool _e28 = local_6;
    if (_e28) {
        local_7 = (dev_4.y < r.w);
    } else {
        local_7 = false;
    }
    bool _e35 = local_7;
    if (_e35) {
        return 1.0;
    }
    return 0.0;
}

float clipMaskCoverage(float2 dev_5)
{
    bool local_8 = (bool)0;
    bool local_9 = (bool)0;
    bool local_10 = (bool)0;

    float _e4 = draw.clipMask.z;
    if ((_e4 <= 0.0)) {
        return 1.0;
    }
    float4 _e12 = draw.clipMask;
    int2 p_7 = asint(asuint(naga_f2i32(floor(dev_5))) - asuint(naga_f2i32(_e12.xy)));
    if (!((p_7.x < int(0)))) {
        local_8 = (p_7.y < int(0));
    } else {
        local_8 = true;
    }
    bool _e26 = local_8;
    if (!(_e26)) {
        float _e34 = draw.clipMask.z;
        local_9 = (p_7.x >= naga_f2i32(_e34));
    } else {
        local_9 = true;
    }
    bool _e38 = local_9;
    if (!(_e38)) {
        float _e46 = draw.clipMask.w;
        local_10 = (p_7.y >= naga_f2i32(_e46));
    } else {
        local_10 = true;
    }
    bool _e50 = local_10;
    if (_e50) {
        return 0.0;
    }
    float4 _e54 = clipMask.Load(int3(p_7, int(0)));
    return _e54.x;
}

float mitchell(float x0_1)
{
    float x_2 = abs(x0_1);
    if ((x_2 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_2) * x_2)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_2 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_2) * x_2)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_2)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

float4 texel(int2 p_3, int2 size_1, int level_1)
{
    float4 _e10 = image.Load(int3(clamp(p_3, (int(0)).xx, asint(asuint(size_1) - asuint((int(1)).xx))), level_1));
    return _e10;
}

float4 bicubic(float2 uv, int2 base_1, int level_2)
{
    float4 sum_1 = (0.0).xxxx;
    int j_1 = int(-1);
    int i_3 = (int)0;

    int2 size_3 = int2(NagaMipDimensions2D(image, level_2));
    float2 f_1 = (((uv * float2(size_3)) / float2(base_1)) - (0.5).xx);
    float2 f0_1 = floor(f_1);
    float2 t_4 = (f_1 - f0_1);
    uint2 loop_bound_3 = uint2(4294967295u, 4294967295u);
    bool loop_init_2 = true;
    while(true) {
        if (all(loop_bound_3 == uint2(0u, 0u))) { break; }
        loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
        if (!loop_init_2) {
            int _e52 = j_1;
            j_1 = asint(asuint(_e52) + asuint(int(1)));
        }
        loop_init_2 = false;
        int _e20 = j_1;
        if ((_e20 <= int(2))) {
        } else {
            break;
        }
        {
            int _e23 = j_1;
            const float _e27 = mitchell((float(_e23) - t_4.y));
            i_3 = int(-1);
            uint2 loop_bound_4 = uint2(4294967295u, 4294967295u);
            bool loop_init_3 = true;
            while(true) {
                if (all(loop_bound_4 == uint2(0u, 0u))) { break; }
                loop_bound_4 -= uint2(loop_bound_4.y == 0u, 1u);
                if (!loop_init_3) {
                    int _e49 = i_3;
                    i_3 = asint(asuint(_e49) + asuint(int(1)));
                }
                loop_init_3 = false;
                int _e30 = i_3;
                if ((_e30 <= int(2))) {
                } else {
                    break;
                }
                {
                    int _e33 = i_3;
                    const float _e37 = mitchell((float(_e33) - t_4.x));
                    float w = (_e27 * _e37);
                    float4 _e39 = sum_1;
                    int _e41 = i_3;
                    int _e42 = j_1;
                    const float4 _e45 = texel(asint(asuint(naga_f2i32(f0_1)) + asuint(int2(_e41, _e42))), size_3, level_2);
                    sum_1 = (_e39 + (w * _e45));
                }
            }
        }
    }
    float _e55 = sum_1.w;
    float a_4 = clamp(_e55, 0.0, 1.0);
    float4 _e59 = sum_1;
    return float4(clamp(_e59.xyz, (0.0).xxx, (a_4).xxx), a_4);
}

typedef float2 ret_Constructarray6_float2_[6];
ret_Constructarray6_float2_ Constructarray6_float2_(float2 arg0, float2 arg1, float2 arg2, float2 arg3, float2 arg4, float2 arg5) {
    float2 ret[6] = { arg0, arg1, arg2, arg3, arg4, arg5 };
    return ret;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID, Instance instance)
{
    float2 corners[6] = Constructarray6_float2_(float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0));
    VertexOutput out_ = (VertexOutput)0;

    float2 _e27 = corners[min(uint(index), 5u)];
    float2 dev_8 = lerp(instance.quad.xy, instance.quad.zw, _e27);
    const float4 _e31 = toClip(dev_8);
    out_.position = _e31;
    out_.dev = dev_8;
    out_.inv0_ = instance.inv0_;
    out_.inv1_ = instance.inv1_;
    out_.color = instance.color;
    out_.clip_ = instance.clip_;
    out_.box = instance.box;
    out_.img = instance.img;
    out_.tint = instance.tint;
    VertexOutput _e47 = out_;
    VertexOutput vertexoutput = _e47;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.dev, vertexoutput.inv0_, vertexoutput.inv1_, vertexoutput.color, vertexoutput.clip_, vertexoutput.box, vertexoutput.img, vertexoutput.tint, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.dev_7, fragmentinput_fs_main.inv0_1, fragmentinput_fs_main.inv1_1, fragmentinput_fs_main.color_2, fragmentinput_fs_main.clip_1, fragmentinput_fs_main.box_1, fragmentinput_fs_main.img_1, fragmentinput_fs_main.tint_1 };
    float coverage = (float)0;
    float pixels = (float)0;
    float4 color = (float4)0;

    float2 dev_9 = (floor(in_.dev) + (0.5).xx);
    int flags = naga_f2i32((in_.inv1_.z + 0.5));
    bool aa = ((flags & FLAG_ANTIALIAS) != int(0));
    float2 uv_2 = float2((((in_.inv0_.x * dev_9.x) + (in_.inv0_.z * dev_9.y)) + in_.inv1_.x), (((in_.inv0_.y * dev_9.x) + (in_.inv0_.w * dev_9.y)) + in_.inv1_.y));
    float2 c_13 = ((in_.box.xy + in_.box.zw) * 0.5);
    float2 q_3 = (abs((uv_2 - c_13)) - ((in_.box.zw - in_.box.xy) * 0.5));
    float d_6 = max(q_3.x, q_3.y);
    float _e60 = ddx(d_6);
    float _e61 = ddy(d_6);
    float g = length(float2(_e60, _e61));
    if (((flags & FLAG_BOX) != int(0))) {
        const float _e70 = boxCoverage(dev_9, in_.box, aa);
        coverage = _e70;
    } else {
        if (aa) {
            pixels = ((d_6 <= 0.0) ? -1.0 : 1.0);
            if ((g > 1e-9)) {
                pixels = (d_6 / g);
            }
            float _e80 = pixels;
            coverage = clamp((0.5 - _e80), 0.0, 1.0);
        } else {
            coverage = ((d_6 <= 0.0) ? 1.0 : 0.0);
        }
    }
    float _e91 = coverage;
    const float _e97 = span(dev_9.x, in_.clip_.x, in_.clip_.z);
    const float _e104 = span(dev_9.y, in_.clip_.y, in_.clip_.w);
    const float _e106 = clipMaskCoverage(dev_9);
    coverage = (((_e91 * _e97) * _e104) * _e106);
    int2 base_3 = int2(NagaMipDimensions2D(image, int(0)));
    int quality_1 = naga_f2i32((in_.img.y + 0.5));
    if ((quality_1 == QUALITY_NONE)) {
        const float4 _e123 = texel(naga_f2i32(floor(uv_2)), base_3, int(0));
        color = _e123;
    } else {
        if ((quality_1 == QUALITY_HIGH)) {
            const float4 _e131 = bicubic(uv_2, base_3, naga_f2i32((in_.img.x + 0.5)));
            color = _e131;
        } else {
            float4 _e138 = image.SampleLevel(imageSampler, (uv_2 / float2(base_3)), in_.img.x);
            color = _e138;
        }
    }
    float4 _e139 = color;
    color = (_e139 * in_.color);
    if ((in_.img.z >= 0.0)) {
        float4 _e151 = color;
        const float4 _e153 = blendPair(naga_f2i32((in_.img.z + 0.5)), _e151, in_.tint);
        color = _e153;
    }
    float4 _e154 = color;
    const float4 _e155 = applyFilters(_e154);
    color = _e155;
    float _e156 = coverage;
    if ((_e156 <= 0.0)) {
        discard;
    }
    float4 _e159 = color;
    float _e160 = coverage;
    const float4 _e161 = finish(_e159, _e160, dev_9);
    return _e161;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
			{Name: "image", Group: 2, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}},
			{Name: "imageSampler", Group: 2, Binding: 1, Kind: BindingSampler, Register: ShaderRegister{Class: "s", Space: 2, Index: 1}},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}},
		}},
	},
}

ShaderImage is shaders/image.wgsl.

Flutter: no counterpart

View Source
var ShaderLayer = &Shader{
	Name: "layer",
	WGSL: `// A layer composited into its parent: one instance is the rectangle of the layer's pixels that
// the clip leaves. Ported from the GLES layer shaders (layer.vert.glsl, layer.frag.glsl). Draw 6
// vertices per instance.

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}
// The coverage of a rounded rectangle blurred by a Gaussian, in closed form (SPEC §9.1). Ported
// from gles/shaders/shadow.glsl. Shaders include this file with an include line.
//
// Across a row of the rectangle, the blur of an interval of columns is a difference of two
// normal distribution functions. Down the rows the Gaussian is integrated: exactly where the
// rows do not change (the straight part of the sides), and by a sum of samples over the rows of
// the rounded corners.

// The error function (Abramowitz and Stegun 7.1.26, error below 1.5e-7).
fn erfApprox(x0: f32) -> f32 {
	let s = select(1.0, -1.0, x0 < 0.0);
	let x = abs(x0);
	let t = 1.0 / (1.0 + 0.3275911 * x);
	let y = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * exp(-x * x);
	return s * y;
}

// The normal distribution function.
fn normalCdf(x: f32) -> f32 {
	return 0.5 + 0.5 * erfApprox(x * 0.7071067811865476);
}

// How far a corner of radii rad pulls the side in at the row y, with y measured from the center
// of the rectangle: top is true for the corners at the top.
fn cornerInset(rad: vec2<f32>, y: f32, hy: f32, top: bool) -> f32 {
	if rad.y <= 0.0 || rad.x <= 0.0 {
		return 0.0;
	}
	let cy = select(hy - rad.y, -hy + rad.y, top);
	let t = select((y - cy) / rad.y, (cy - y) / rad.y, top);
	if t <= 0.0 {
		return 0.0;
	}
	return rad.x * (1.0 - sqrt(max(1.0 - t * t, 0.0)));
}

// The weight of the rows of a corner zone, [y0, y1] from the center, as seen from the pixel at p:
// the sum over the rows of the Gaussian times the columns' share.
fn zoneMass(y0: f32, y1: f32, p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>, sigma: f32, top: bool) -> f32 {
	let a = max(y0, p.y - 4.0 * sigma);
	let b = min(y1, p.y + 4.0 * sigma);
	if b <= a {
		return 0.0;
	}
	let dv = (b - a) / 16.0;
	var sum = 0.0;
	for (var i = 0; i < 16; i++) {
		let y = a + (f32(i) + 0.5) * dv;
		let l = -h.x + cornerInset(select(r2.zw, r1.xy, top), y, h.y, top);
		let r = h.x - cornerInset(select(r2.xy, r1.zw, top), y, h.y, top);
		let w = exp(-0.5 * (y - p.y) * (y - p.y) / (sigma * sigma));
		sum += w * max(normalCdf((r - p.x) / sigma) - normalCdf((l - p.x) / sigma), 0.0);
	}
	return sum * dv / (sigma * 2.5066282746310002);
}

// The blurred coverage at the pixel whose position from the rectangle's center is p.
fn blurredRRect(p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>, sigma: f32) -> f32 {
	let topH = max(r1.y, r1.w);
	let botH = max(r2.y, r2.w);
	var mass = zoneMass(-h.y, -h.y + topH, p, h, r1, r2, sigma, true);
	mass += zoneMass(h.y - botH, h.y, p, h, r1, r2, sigma, false);
	let m0 = -h.y + topH;
	let m1 = h.y - botH;
	if m1 > m0 {
		let rows = normalCdf((m1 - p.y) / sigma) - normalCdf((m0 - p.y) / sigma);
		let cols = normalCdf((h.x - p.x) / sigma) - normalCdf((-h.x - p.x) / sigma);
		mass += rows * cols;
	}
	return clamp(mass, 0.0, 1.0);
}

// The coverage of a blur style, from the blurred coverage b and the shape's own o.
fn blurStyle(style: i32, b: f32, o: f32) -> f32 {
	if style == 1 {
		return o + b * (1.0 - o); // solid
	}
	if style == 2 {
		return b * (1.0 - o); // outer
	}
	if style == 3 {
		return b * o; // inner
	}
	return b; // normal
}

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is.
	origin: vec2<f32>,
}

// What a draw call sets: the clip mask's rectangle in the surface (x, y, width, height; a width
// of 0 means none), the layer's pixels in the surface (x, y, width, height), and how the pixels
// are read: the mask style (-1: the layer's pixels are composited; else the layer's alpha is the
// coverage of a blurred shape, a blur style, that the paint's color fills) and drawnOnly (1:
// pixels with no alpha are left as they are).
struct Draw {
	clipMask: vec4<f32>,
	layerRect: vec4<f32>,
	read: vec4<f32>,
}

@group(0) @binding(0) var<uniform> frame: Frame;
@group(0) @binding(1) var<uniform> draw: Draw;
@group(1) @binding(0) var clipMask: texture_2d<f32>;
// The layer's pixels, premultiplied.
@group(2) @binding(0) var layer: texture_2d<f32>;
// The layer before it was blurred, for a mask style other than normal.
@group(2) @binding(1) var layerOwn: texture_2d<f32>;

struct Instance {
	@location(0) quad: vec4<f32>,  // left, top, right, bottom in pixels
	@location(1) color: vec4<f32>, // the layer's alpha in all four components
	@location(2) clip: vec4<f32>,  // left, top, right, bottom in pixels
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) dev: vec2<f32>,
	@location(1) @interpolate(flat) color: vec4<f32>,
	@location(2) @interpolate(flat) clip: vec4<f32>,
}

fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32, instance: Instance) -> VertexOutput {
	var corners = array<vec2<f32>, 6>(
		vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
		vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0),
	);
	let dev = mix(instance.quad.xy, instance.quad.zw, corners[index]);
	var out: VertexOutput;
	out.position = toClip(dev);
	out.dev = dev;
	out.color = instance.color;
	out.clip = instance.clip;
	return out;
}

// The part of the pixel centered at c that the interval [lo, hi] covers.
fn span(c: f32, lo: f32, hi: f32) -> f32 {
	return clamp(min(c + 0.5, hi) - max(c - 0.5, lo), 0.0, 1.0);
}

fn clipMaskCoverage(dev: vec2<f32>) -> f32 {
	if draw.clipMask.z <= 0.0 {
		return 1.0;
	}
	let p = vec2<i32>(floor(dev)) - vec2<i32>(draw.clipMask.xy);
	if p.x < 0 || p.y < 0 || p.x >= i32(draw.clipMask.z) || p.y >= i32(draw.clipMask.w) {
		return 0.0;
	}
	return textureLoad(clipMask, p, 0).r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	let dev = floor(in.dev) + vec2<f32>(0.5);
	let coverage = span(dev.x, in.clip.x, in.clip.z) * span(dev.y, in.clip.y, in.clip.w) * clipMaskCoverage(dev);
	if coverage <= 0.0 {
		discard;
	}
	let p = vec2<i32>(floor(dev - draw.layerRect.xy));
	let texel = textureLoad(layer, p, 0);
	if draw.read.y != 0.0 && texel.a <= 0.0 {
		discard;
	}
	let style = i32(draw.read.x);
	if style >= 0 {
		var own = 0.0;
		if style != 0 {
			own = textureLoad(layerOwn, p, 0).a;
		}
		let cov = blurStyle(style, texel.a, own) * coverage;
		if cov < 1.0 / 1024.0 {
			discard;
		}
		return finish(paintSource(in.color, dev), cov, dev);
	}
	let color = applyFilters(texel * in.color);
	return finish(color, coverage, dev);
}
`,
	SPIRV: spirvLayer,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Frame {
    float2 surface;
    float2 origin;
};

struct Draw {
    float4 clipMask;
    float4 layerRect;
    float4 read;
};

struct Instance {
    float4 quad : LOC0;
    float4 color : LOC1;
    float4 clip_ : LOC2;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 dev : LOC0;
    nointerpolation float4 color : LOC1;
    nointerpolation float4 clip_ : LOC2;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer frame : register(b0, space0) { Frame frame; }
cbuffer draw : register(b1, space0) { Draw draw; }
Texture2D<float4> clipMask : register(t0, space1);
Texture2D<float4> layer : register(t0, space2);
Texture2D<float4> layerOwn : register(t1, space2);

struct VertexOutput_vs_main {
    float2 dev_5 : LOC0;
    nointerpolation float4 color : LOC1;
    nointerpolation float4 clip_ : LOC2;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 dev_6 : LOC0;
    nointerpolation float4 color_1 : LOC1;
    nointerpolation float4 clip_1 : LOC2;
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d * (1.0 - sa))) + ((2.0 * s) * d));
    }
    return ((((s * (1.0 - da)) + (d * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d)) * (sa - s)));
}

float softLightChannel(float s_1, float d_1, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_1 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_1 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_1) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_1 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_1 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_2, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_2) - max((s_2 * da_2), (d_2 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_2) - min((s_2 * da_2), (d_2 * sa_2)));
        }
        case 18: {
            if ((d_2 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_2 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_2 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_2 == da_2)) {
                return (d_2 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_2 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_2) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_2) - (2.0 * min((s_2 * da_2), (d_2 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_2) - ((2.0 * s_2) * d_2));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_2 * (1.0 - sa_2))) + (s_2 * d_2));
        }
        default: {
            return (s_2 + (d_2 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_3)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_3.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_3.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_3.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_3.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_3.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_3.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_3.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_3.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_4)
{
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_4;
        }
        case 3: {
            return (s_5 + (d_4 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_4 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_4 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_4 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_4 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_4 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_4 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_4), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_4);
        }
        case 14: {
            return ((s_5 + d_4) - (s_5 * d_4));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local_1 = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local_1 = (t <= 1.0);
        } else {
            local_1 = false;
        }
        bool _e33 = local_1;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i)
{
    float _e9 = paint.rampPos[(i >> 2u)][(i & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_1 = int(0);
    bool local_2 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_1;
        if ((_e10 < n)) {
            int _e14 = i_1;
            const float _e15 = rampPosAt(_e14);
            local_2 = (_e15 < t_1);
        } else {
            local_2 = false;
        }
        bool _e18 = local_2;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_1;
            i_1 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_1;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_1;
        if ((_e30 >= n)) {
            float4 _e37 = paint.rampColor[asint(asuint(n) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_1;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_1;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_1;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_1;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_1;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_2 = (dot(cd, cd) - (dr * dr));
    float2 p_5 = (q - c0_1);
    float b_1 = (dot(p_5, cd) + (r0_ * dr));
    float c_11 = (dot(p_5, p_5) - (r0_ * r0_));
    if ((abs(a_2) < 1e-12)) {
        if ((b_1 == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_11 / (2.0 * b_1));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b_1 * b_1) - (a_2 * c_11));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b_1 + sq) / a_2);
        t2_ = ((b_1 - sq) / a_2);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_3 = (bool)0;
    bool local_4 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_1 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_1 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_1));
            } else {
                x = m_1;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_3 = (_e61 >= size.x);
                } else {
                    local_3 = true;
                }
                bool _e65 = local_3;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_2 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_2));
            } else {
                y = m_2;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_4 = (_e114 >= size.y);
                } else {
                    local_4 = true;
                }
                bool _e118 = local_4;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_2 = abs(x0_);
    if ((x_2 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_2) * x_2)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_2 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_2) * x_2)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_2)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j = int(-1);
    int i_2 = (int)0;

    int2 base_1 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_1 = naga_f2i32((_e15 + 0.5));
    int2 size_1 = int2(NagaMipDimensions2D(paintImage, level_1));
    float2 uv = ((q_1 * float2(size_1)) / float2(base_1));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv)), size_1, level_1);
        return _e30;
    }
    float2 f = (uv - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_2 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_2;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_2;
                        int _e65 = j;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_1, level_1);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_3).xxx), a_3);
    }
    const float4 _e89 = shaderTexel(i0_, size_1, level_1);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_1, level_1);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_1, level_1);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_1, level_1);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_2 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_2 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_2 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_2);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_5 = (q_2 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_5.y, d_5.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_2);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v)
{
    return ((v <= (0.0031308).xxx) ? (12.92 * v) : ((1.055 * pow(v, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_1)
{
    return ((v_1 <= (0.04045).xxx) ? (v_1 / (12.92).xxx) : pow(((v_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_12 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_2 = (((((_e32 * c_12.x) + (_e38 * c_12.y)) + (_e45 * c_12.z)) + (_e52 * c_12.w)) + _e59);
            const float4 _e69 = premul(clamp((o_2 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_6 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_6.y) * asuint(int(8)))) + asuint(p_6.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o = (float4)0;
    bool local_5 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_5 = (src.w > 0.0);
    } else {
        local_5 = false;
    }
    bool _e35 = local_5;
    if (_e35) {
        float4 _e36 = o;
        const float _e37 = ditherAt(dev_2);
        o = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o;
    return _e40;
}

float erfApprox(float x0_1)
{
    float s_6 = ((x0_1 < 0.0) ? -1.0 : 1.0);
    float x_3 = abs(x0_1);
    float t_4 = (1.0 / (1.0 + (0.3275911 * x_3)));
    float y_2 = (1.0 - ((((((((((1.0614054 * t_4) - 1.4531521) * t_4) + 1.4214138) * t_4) - 0.28449672) * t_4) + 0.2548296) * t_4) * exp((-(x_3) * x_3))));
    return (s_6 * y_2);
}

float normalCdf(float x_1)
{
    const float _e3 = erfApprox((x_1 * 0.70710677));
    return (0.5 + (0.5 * _e3));
}

float cornerInset(float2 rad, float y_1, float hy, bool top)
{
    bool local_6 = (bool)0;

    if (!((rad.y <= 0.0))) {
        local_6 = (rad.x <= 0.0);
    } else {
        local_6 = true;
    }
    bool _e14 = local_6;
    if (_e14) {
        return 0.0;
    }
    float cy = (top ? (-(hy) + rad.y) : (hy - rad.y));
    float t_5 = (top ? ((cy - y_1) / rad.y) : ((y_1 - cy) / rad.y));
    if ((t_5 <= 0.0)) {
        return 0.0;
    }
    return (rad.x * (1.0 - sqrt(max((1.0 - (t_5 * t_5)), 0.0))));
}

float zoneMass(float y0_, float y1_, float2 p_3, float2 h, float4 r1_, float4 r2_, float sigma, bool top_1)
{
    float sum_1 = 0.0;
    int i_3 = int(0);

    float a_4 = max(y0_, (p_3.y - (4.0 * sigma)));
    float b_2 = min(y1_, (p_3.y + (4.0 * sigma)));
    if ((b_2 <= a_4)) {
        return 0.0;
    }
    float dv = ((b_2 - a_4) / 16.0);
    uint2 loop_bound_3 = uint2(4294967295u, 4294967295u);
    bool loop_init_2 = true;
    while(true) {
        if (all(loop_bound_3 == uint2(0u, 0u))) { break; }
        loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
        if (!loop_init_2) {
            int _e76 = i_3;
            i_3 = asint(asuint(_e76) + asuint(int(1)));
        }
        loop_init_2 = false;
        int _e27 = i_3;
        if ((_e27 < int(16))) {
        } else {
            break;
        }
        {
            int _e30 = i_3;
            float y_3 = (a_4 + ((float(_e30) + 0.5) * dv));
            const float _e42 = cornerInset((top_1 ? r1_.xy : r2_.zw), y_3, h.y, top_1);
            float l_1 = (-(h.x) + _e42);
            const float _e49 = cornerInset((top_1 ? r1_.zw : r2_.xy), y_3, h.y, top_1);
            float r = (h.x - _e49);
            float w = exp((((-0.5 * (y_3 - p_3.y)) * (y_3 - p_3.y)) / (sigma * sigma)));
            float _e61 = sum_1;
            const float _e65 = normalCdf(((r - p_3.x) / sigma));
            const float _e69 = normalCdf(((l_1 - p_3.x) / sigma));
            sum_1 = (_e61 + (w * max((_e65 - _e69), 0.0)));
        }
    }
    float _e78 = sum_1;
    return ((_e78 * dv) / (sigma * 2.5066283));
}

float blurredRRect(float2 p_4, float2 h_1, float4 r1_1, float4 r2_1, float sigma_1)
{
    float mass = (float)0;

    float topH = max(r1_1.y, r1_1.w);
    float botH = max(r2_1.y, r2_1.w);
    const float _e17 = zoneMass(-(h_1.y), (-(h_1.y) + topH), p_4, h_1, r1_1, r2_1, sigma_1, true);
    mass = _e17;
    float _e19 = mass;
    const float _e24 = zoneMass((h_1.y - botH), h_1.y, p_4, h_1, r1_1, r2_1, sigma_1, false);
    mass = (_e19 + _e24);
    float m0_ = (-(h_1.y) + topH);
    float m1_ = (h_1.y - botH);
    if ((m1_ > m0_)) {
        const float _e35 = normalCdf(((m1_ - p_4.y) / sigma_1));
        const float _e39 = normalCdf(((m0_ - p_4.y) / sigma_1));
        float rows = (_e35 - _e39);
        const float _e45 = normalCdf(((h_1.x - p_4.x) / sigma_1));
        const float _e51 = normalCdf(((-(h_1.x) - p_4.x) / sigma_1));
        float cols = (_e45 - _e51);
        float _e53 = mass;
        mass = (_e53 + (rows * cols));
    }
    float _e56 = mass;
    return clamp(_e56, 0.0, 1.0);
}

float blurStyle(int style, float b, float o_1)
{
    if ((style == int(1))) {
        return (o_1 + (b * (1.0 - o_1)));
    }
    if ((style == int(2))) {
        return (b * (1.0 - o_1));
    }
    if ((style == int(3))) {
        return (b * o_1);
    }
    return b;
}

float4 toClip(float2 dev_3)
{
    float2 _e3 = frame.origin;
    float2 p_7 = (dev_3 - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p_7.x / _e9) * 2.0) - 1.0), (1.0 - ((p_7.y / _e19) * 2.0)), 0.0, 1.0);
}

float span(float c_10, float lo, float hi)
{
    return clamp((min((c_10 + 0.5), hi) - max((c_10 - 0.5), lo)), 0.0, 1.0);
}

float clipMaskCoverage(float2 dev_4)
{
    bool local_7 = (bool)0;
    bool local_8 = (bool)0;
    bool local_9 = (bool)0;

    float _e4 = draw.clipMask.z;
    if ((_e4 <= 0.0)) {
        return 1.0;
    }
    float4 _e12 = draw.clipMask;
    int2 p_8 = asint(asuint(naga_f2i32(floor(dev_4))) - asuint(naga_f2i32(_e12.xy)));
    if (!((p_8.x < int(0)))) {
        local_7 = (p_8.y < int(0));
    } else {
        local_7 = true;
    }
    bool _e26 = local_7;
    if (!(_e26)) {
        float _e34 = draw.clipMask.z;
        local_8 = (p_8.x >= naga_f2i32(_e34));
    } else {
        local_8 = true;
    }
    bool _e38 = local_8;
    if (!(_e38)) {
        float _e46 = draw.clipMask.w;
        local_9 = (p_8.y >= naga_f2i32(_e46));
    } else {
        local_9 = true;
    }
    bool _e50 = local_9;
    if (_e50) {
        return 0.0;
    }
    float4 _e54 = clipMask.Load(int3(p_8, int(0)));
    return _e54.x;
}

typedef float2 ret_Constructarray6_float2_[6];
ret_Constructarray6_float2_ Constructarray6_float2_(float2 arg0, float2 arg1, float2 arg2, float2 arg3, float2 arg4, float2 arg5) {
    float2 ret[6] = { arg0, arg1, arg2, arg3, arg4, arg5 };
    return ret;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID, Instance instance)
{
    float2 corners[6] = Constructarray6_float2_(float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0));
    VertexOutput out_ = (VertexOutput)0;

    float2 _e27 = corners[min(uint(index), 5u)];
    float2 dev_7 = lerp(instance.quad.xy, instance.quad.zw, _e27);
    const float4 _e31 = toClip(dev_7);
    out_.position = _e31;
    out_.dev = dev_7;
    out_.color = instance.color;
    out_.clip_ = instance.clip_;
    VertexOutput _e37 = out_;
    VertexOutput vertexoutput = _e37;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.dev, vertexoutput.color, vertexoutput.clip_, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.dev_6, fragmentinput_fs_main.color_1, fragmentinput_fs_main.clip_1 };
    bool local = (bool)0;
    float own = 0.0;

    float2 dev_8 = (floor(in_.dev) + (0.5).xx);
    const float _e11 = span(dev_8.x, in_.clip_.x, in_.clip_.z);
    const float _e17 = span(dev_8.y, in_.clip_.y, in_.clip_.w);
    const float _e19 = clipMaskCoverage(dev_8);
    float coverage = ((_e11 * _e17) * _e19);
    if ((coverage <= 0.0)) {
        discard;
    }
    float4 _e25 = draw.layerRect;
    int2 p_9 = naga_f2i32(floor((dev_8 - _e25.xy)));
    float4 texel = layer.Load(int3(p_9, int(0)));
    float _e36 = draw.read.y;
    if ((_e36 != 0.0)) {
        local = (texel.w <= 0.0);
    } else {
        local = false;
    }
    bool _e45 = local;
    if (_e45) {
        discard;
    }
    float _e49 = draw.read.x;
    int style_1 = naga_f2i32(_e49);
    if ((style_1 >= int(0))) {
        if ((style_1 != int(0))) {
            float4 _e59 = layerOwn.Load(int3(p_9, int(0)));
            own = _e59.w;
        }
        float _e62 = own;
        const float _e63 = blurStyle(style_1, texel.w, _e62);
        float cov_1 = (_e63 * coverage);
        if ((cov_1 < 0.0009765625)) {
            discard;
        }
        const float4 _e68 = paintSource(in_.color, dev_8);
        const float4 _e69 = finish(_e68, cov_1, dev_8);
        return _e69;
    }
    const float4 _e72 = applyFilters((texel * in_.color));
    const float4 _e73 = finish(_e72, coverage, dev_8);
    return _e73;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
			{Name: "layer", Group: 2, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}},
			{Name: "layerOwn", Group: 2, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 2, Index: 1}},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}},
		}},
	},
}

ShaderLayer is shaders/layer.wgsl.

Flutter: no counterpart

View Source
var ShaderMask = &Shader{
	Name: "mask",
	WGSL: `// Coverage masks (SPEC §9.1 "General paths"): one instance is one tile of the coverage atlas,
// drawn over the pixels it covers. Ported from the GLES mask shaders (mask.vert.glsl,
// mask.frag.glsl). Draw 6 vertices per instance.

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is.
	origin: vec2<f32>,
}

// The clip mask's rectangle in the surface (x, y, width, height; a width of 0 means none).
struct Draw {
	clipMask: vec4<f32>,
}

@group(0) @binding(0) var<uniform> frame: Frame;
@group(0) @binding(1) var<uniform> draw: Draw;
@group(1) @binding(0) var clipMask: texture_2d<f32>;
// An atlas page: 8-bit coverage.
@group(2) @binding(0) var atlas: texture_2d<f32>;

struct Instance {
	@location(0) quad: vec4<f32>,  // left, top, right, bottom in pixels
	@location(1) tile: vec4<f32>,  // the tile's top left texel in the atlas page
	@location(2) color: vec4<f32>, // premultiplied
	@location(3) clip: vec4<f32>,  // left, top, right, bottom in pixels
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) dev: vec2<f32>,
	@location(1) @interpolate(flat) quad: vec4<f32>,
	@location(2) @interpolate(flat) tile: vec4<f32>,
	@location(3) @interpolate(flat) color: vec4<f32>,
	@location(4) @interpolate(flat) clip: vec4<f32>,
}

fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32, instance: Instance) -> VertexOutput {
	var corners = array<vec2<f32>, 6>(
		vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
		vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0),
	);
	let dev = mix(instance.quad.xy, instance.quad.zw, corners[index]);
	var out: VertexOutput;
	out.position = toClip(dev);
	out.dev = dev;
	out.quad = instance.quad;
	out.tile = instance.tile;
	out.color = instance.color;
	out.clip = instance.clip;
	return out;
}

// The part of the pixel centered at c that the interval [lo, hi] covers.
fn span(c: f32, lo: f32, hi: f32) -> f32 {
	return clamp(min(c + 0.5, hi) - max(c - 0.5, lo), 0.0, 1.0);
}

fn clipMaskCoverage(dev: vec2<f32>) -> f32 {
	if draw.clipMask.z <= 0.0 {
		return 1.0;
	}
	let p = vec2<i32>(floor(dev)) - vec2<i32>(draw.clipMask.xy);
	if p.x < 0 || p.y < 0 || p.x >= i32(draw.clipMask.z) || p.y >= i32(draw.clipMask.w) {
		return 0.0;
	}
	return textureLoad(clipMask, p, 0).r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	let dev = floor(in.dev) + vec2<f32>(0.5);
	// The quad covers whole pixels, one texel each: the texel of this pixel is the tile's
	// corner plus its distance from the quad's corner.
	let texel = vec2<i32>(in.tile.xy) + vec2<i32>(floor(dev)) - vec2<i32>(in.quad.xy);
	var coverage = textureLoad(atlas, texel, 0).r;
	coverage = coverage * span(dev.x, in.clip.x, in.clip.z) * span(dev.y, in.clip.y, in.clip.w) * clipMaskCoverage(dev);
	if coverage <= 0.0 {
		discard;
	}
	return finish(paintSource(in.color, dev), coverage, dev);
}
`,
	SPIRV: spirvMask,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Frame {
    float2 surface;
    float2 origin;
};

struct Draw {
    float4 clipMask;
};

struct Instance {
    float4 quad : LOC0;
    float4 tile : LOC1;
    float4 color : LOC2;
    float4 clip_ : LOC3;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 dev : LOC0;
    nointerpolation float4 quad : LOC1;
    nointerpolation float4 tile : LOC2;
    nointerpolation float4 color : LOC3;
    nointerpolation float4 clip_ : LOC4;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer frame : register(b0, space0) { Frame frame; }
cbuffer draw : register(b1, space0) { Draw draw; }
Texture2D<float4> clipMask : register(t0, space1);
Texture2D<float4> atlas : register(t0, space2);

struct VertexOutput_vs_main {
    float2 dev_5 : LOC0;
    nointerpolation float4 quad : LOC1;
    nointerpolation float4 tile : LOC2;
    nointerpolation float4 color : LOC3;
    nointerpolation float4 clip_ : LOC4;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 dev_6 : LOC0;
    nointerpolation float4 quad_1 : LOC1;
    nointerpolation float4 tile_1 : LOC2;
    nointerpolation float4 color_1 : LOC3;
    nointerpolation float4 clip_1 : LOC4;
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d * (1.0 - sa))) + ((2.0 * s) * d));
    }
    return ((((s * (1.0 - da)) + (d * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d)) * (sa - s)));
}

float softLightChannel(float s_1, float d_1, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_1 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_1 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_1) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_1 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_1 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_2, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_2) - max((s_2 * da_2), (d_2 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_2) - min((s_2 * da_2), (d_2 * sa_2)));
        }
        case 18: {
            if ((d_2 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_2 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_2 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_2 == da_2)) {
                return (d_2 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_2 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_2) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_2) - (2.0 * min((s_2 * da_2), (d_2 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_2) - ((2.0 * s_2) * d_2));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_2 * (1.0 - sa_2))) + (s_2 * d_2));
        }
        default: {
            return (s_2 + (d_2 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_3)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_3.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_3.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_3.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_3.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_3.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_3.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_3.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_3.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_4)
{
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_4;
        }
        case 3: {
            return (s_5 + (d_4 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_4 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_4 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_4 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_4 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_4 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_4 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_4), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_4);
        }
        case 14: {
            return ((s_5 + d_4) - (s_5 * d_4));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local = (t <= 1.0);
        } else {
            local = false;
        }
        bool _e33 = local;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i)
{
    float _e9 = paint.rampPos[(i >> 2u)][(i & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_1 = int(0);
    bool local_1 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_1;
        if ((_e10 < n)) {
            int _e14 = i_1;
            const float _e15 = rampPosAt(_e14);
            local_1 = (_e15 < t_1);
        } else {
            local_1 = false;
        }
        bool _e18 = local_1;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_1;
            i_1 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_1;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_1;
        if ((_e30 >= n)) {
            float4 _e37 = paint.rampColor[asint(asuint(n) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_1;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_1;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_1;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_1;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_1;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_2 = (dot(cd, cd) - (dr * dr));
    float2 p_3 = (q - c0_1);
    float b = (dot(p_3, cd) + (r0_ * dr));
    float c_11 = (dot(p_3, p_3) - (r0_ * r0_));
    if ((abs(a_2) < 1e-12)) {
        if ((b == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_11 / (2.0 * b));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b * b) - (a_2 * c_11));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b + sq) / a_2);
        t2_ = ((b - sq) / a_2);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_2 = (bool)0;
    bool local_3 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_1 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_1 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_1));
            } else {
                x = m_1;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_2 = (_e61 >= size.x);
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_2 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_2));
            } else {
                y = m_2;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_3 = (_e114 >= size.y);
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_1 = abs(x0_);
    if ((x_1 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_1) * x_1)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_1 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_1) * x_1)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_1)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j = int(-1);
    int i_2 = (int)0;

    int2 base_1 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_1 = naga_f2i32((_e15 + 0.5));
    int2 size_1 = int2(NagaMipDimensions2D(paintImage, level_1));
    float2 uv = ((q_1 * float2(size_1)) / float2(base_1));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv)), size_1, level_1);
        return _e30;
    }
    float2 f = (uv - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_2 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_2;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_2;
                        int _e65 = j;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_1, level_1);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_3).xxx), a_3);
    }
    const float4 _e89 = shaderTexel(i0_, size_1, level_1);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_1, level_1);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_1, level_1);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_1, level_1);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_2 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_2 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_2 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_2);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_5 = (q_2 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_5.y, d_5.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_2);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v)
{
    return ((v <= (0.0031308).xxx) ? (12.92 * v) : ((1.055 * pow(v, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_1)
{
    return ((v_1 <= (0.04045).xxx) ? (v_1 / (12.92).xxx) : pow(((v_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_12 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_1 = (((((_e32 * c_12.x) + (_e38 * c_12.y)) + (_e45 * c_12.z)) + (_e52 * c_12.w)) + _e59);
            const float4 _e69 = premul(clamp((o_1 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_4 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_4.y) * asuint(int(8)))) + asuint(p_4.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o = (float4)0;
    bool local_4 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_4 = (src.w > 0.0);
    } else {
        local_4 = false;
    }
    bool _e35 = local_4;
    if (_e35) {
        float4 _e36 = o;
        const float _e37 = ditherAt(dev_2);
        o = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o;
    return _e40;
}

float4 toClip(float2 dev_3)
{
    float2 _e3 = frame.origin;
    float2 p_5 = (dev_3 - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p_5.x / _e9) * 2.0) - 1.0), (1.0 - ((p_5.y / _e19) * 2.0)), 0.0, 1.0);
}

float span(float c_10, float lo, float hi)
{
    return clamp((min((c_10 + 0.5), hi) - max((c_10 - 0.5), lo)), 0.0, 1.0);
}

float clipMaskCoverage(float2 dev_4)
{
    bool local_5 = (bool)0;
    bool local_6 = (bool)0;
    bool local_7 = (bool)0;

    float _e4 = draw.clipMask.z;
    if ((_e4 <= 0.0)) {
        return 1.0;
    }
    float4 _e12 = draw.clipMask;
    int2 p_6 = asint(asuint(naga_f2i32(floor(dev_4))) - asuint(naga_f2i32(_e12.xy)));
    if (!((p_6.x < int(0)))) {
        local_5 = (p_6.y < int(0));
    } else {
        local_5 = true;
    }
    bool _e26 = local_5;
    if (!(_e26)) {
        float _e34 = draw.clipMask.z;
        local_6 = (p_6.x >= naga_f2i32(_e34));
    } else {
        local_6 = true;
    }
    bool _e38 = local_6;
    if (!(_e38)) {
        float _e46 = draw.clipMask.w;
        local_7 = (p_6.y >= naga_f2i32(_e46));
    } else {
        local_7 = true;
    }
    bool _e50 = local_7;
    if (_e50) {
        return 0.0;
    }
    float4 _e54 = clipMask.Load(int3(p_6, int(0)));
    return _e54.x;
}

typedef float2 ret_Constructarray6_float2_[6];
ret_Constructarray6_float2_ Constructarray6_float2_(float2 arg0, float2 arg1, float2 arg2, float2 arg3, float2 arg4, float2 arg5) {
    float2 ret[6] = { arg0, arg1, arg2, arg3, arg4, arg5 };
    return ret;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID, Instance instance)
{
    float2 corners[6] = Constructarray6_float2_(float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0));
    VertexOutput out_ = (VertexOutput)0;

    float2 _e27 = corners[min(uint(index), 5u)];
    float2 dev_7 = lerp(instance.quad.xy, instance.quad.zw, _e27);
    const float4 _e31 = toClip(dev_7);
    out_.position = _e31;
    out_.dev = dev_7;
    out_.quad = instance.quad;
    out_.tile = instance.tile;
    out_.color = instance.color;
    out_.clip_ = instance.clip_;
    VertexOutput _e41 = out_;
    VertexOutput vertexoutput = _e41;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.dev, vertexoutput.quad, vertexoutput.tile, vertexoutput.color, vertexoutput.clip_, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.dev_6, fragmentinput_fs_main.quad_1, fragmentinput_fs_main.tile_1, fragmentinput_fs_main.color_1, fragmentinput_fs_main.clip_1 };
    float coverage = (float)0;

    float2 dev_8 = (floor(in_.dev) + (0.5).xx);
    int2 texel = asint(asuint(asint(asuint(naga_f2i32(in_.tile.xy)) + asuint(naga_f2i32(floor(dev_8))))) - asuint(naga_f2i32(in_.quad.xy)));
    float4 _e18 = atlas.Load(int3(texel, int(0)));
    coverage = _e18.x;
    float _e21 = coverage;
    const float _e27 = span(dev_8.x, in_.clip_.x, in_.clip_.z);
    const float _e34 = span(dev_8.y, in_.clip_.y, in_.clip_.w);
    const float _e36 = clipMaskCoverage(dev_8);
    coverage = (((_e21 * _e27) * _e34) * _e36);
    float _e38 = coverage;
    if ((_e38 <= 0.0)) {
        discard;
    }
    const float4 _e42 = paintSource(in_.color, dev_8);
    float _e43 = coverage;
    const float4 _e44 = finish(_e42, _e43, dev_8);
    return _e44;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
			{Name: "atlas", Group: 2, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}},
		}},
	},
}

ShaderMask is shaders/mask.wgsl.

Flutter: no counterpart

View Source
var ShaderShape = &Shader{
	Name: "shape",
	WGSL: `// Analytic shapes (SPEC §9.1, §9.3): one instance is one shape. The vertex shader only
// places a quad that covers the shape's bounds in pixels; the fragment shader works out
// the coverage of each pixel from a signed distance field. Ported from the GLES shape
// shaders (shape.vert.glsl, shape.frag.glsl) with sdf.glsl and common.glsl's toClip.
//
// Draw 6 vertices per instance: two triangles made from the vertex index. The instance
// is twelve vec4 attributes (gpu.ShapeInstance), the same for the corners, so each is
// passed on as a flat varying.
//
// The color is the paint's: a solid color, a gradient or an image shader, with its color filter
// (paint.wgsl.inc); the blend unit scales it by the coverage, or the shader blends it with a
// copy of the destination. The clip masks of shape clips multiply the coverage (clipmask.glsl).

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}
// The coverage of a rounded rectangle blurred by a Gaussian, in closed form (SPEC §9.1). Ported
// from gles/shaders/shadow.glsl. Shaders include this file with an include line.
//
// Across a row of the rectangle, the blur of an interval of columns is a difference of two
// normal distribution functions. Down the rows the Gaussian is integrated: exactly where the
// rows do not change (the straight part of the sides), and by a sum of samples over the rows of
// the rounded corners.

// The error function (Abramowitz and Stegun 7.1.26, error below 1.5e-7).
fn erfApprox(x0: f32) -> f32 {
	let s = select(1.0, -1.0, x0 < 0.0);
	let x = abs(x0);
	let t = 1.0 / (1.0 + 0.3275911 * x);
	let y = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t - 0.284496736) * t + 0.254829592) * t * exp(-x * x);
	return s * y;
}

// The normal distribution function.
fn normalCdf(x: f32) -> f32 {
	return 0.5 + 0.5 * erfApprox(x * 0.7071067811865476);
}

// How far a corner of radii rad pulls the side in at the row y, with y measured from the center
// of the rectangle: top is true for the corners at the top.
fn cornerInset(rad: vec2<f32>, y: f32, hy: f32, top: bool) -> f32 {
	if rad.y <= 0.0 || rad.x <= 0.0 {
		return 0.0;
	}
	let cy = select(hy - rad.y, -hy + rad.y, top);
	let t = select((y - cy) / rad.y, (cy - y) / rad.y, top);
	if t <= 0.0 {
		return 0.0;
	}
	return rad.x * (1.0 - sqrt(max(1.0 - t * t, 0.0)));
}

// The weight of the rows of a corner zone, [y0, y1] from the center, as seen from the pixel at p:
// the sum over the rows of the Gaussian times the columns' share.
fn zoneMass(y0: f32, y1: f32, p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>, sigma: f32, top: bool) -> f32 {
	let a = max(y0, p.y - 4.0 * sigma);
	let b = min(y1, p.y + 4.0 * sigma);
	if b <= a {
		return 0.0;
	}
	let dv = (b - a) / 16.0;
	var sum = 0.0;
	for (var i = 0; i < 16; i++) {
		let y = a + (f32(i) + 0.5) * dv;
		let l = -h.x + cornerInset(select(r2.zw, r1.xy, top), y, h.y, top);
		let r = h.x - cornerInset(select(r2.xy, r1.zw, top), y, h.y, top);
		let w = exp(-0.5 * (y - p.y) * (y - p.y) / (sigma * sigma));
		sum += w * max(normalCdf((r - p.x) / sigma) - normalCdf((l - p.x) / sigma), 0.0);
	}
	return sum * dv / (sigma * 2.5066282746310002);
}

// The blurred coverage at the pixel whose position from the rectangle's center is p.
fn blurredRRect(p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>, sigma: f32) -> f32 {
	let topH = max(r1.y, r1.w);
	let botH = max(r2.y, r2.w);
	var mass = zoneMass(-h.y, -h.y + topH, p, h, r1, r2, sigma, true);
	mass += zoneMass(h.y - botH, h.y, p, h, r1, r2, sigma, false);
	let m0 = -h.y + topH;
	let m1 = h.y - botH;
	if m1 > m0 {
		let rows = normalCdf((m1 - p.y) / sigma) - normalCdf((m0 - p.y) / sigma);
		let cols = normalCdf((h.x - p.x) / sigma) - normalCdf((-h.x - p.x) / sigma);
		mass += rows * cols;
	}
	return clamp(mass, 0.0, 1.0);
}

// The coverage of a blur style, from the blurred coverage b and the shape's own o.
fn blurStyle(style: i32, b: f32, o: f32) -> f32 {
	if style == 1 {
		return o + b * (1.0 - o); // solid
	}
	if style == 2 {
		return b * (1.0 - o); // outer
	}
	if style == 3 {
		return b * o; // inner
	}
	return b; // normal
}

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is: a layer draws
	// into a texture that covers a rectangle of the surface.
	origin: vec2<f32>,
}

// What a draw call sets: the clip mask's rectangle in the surface (x, y, width, height; a
// width of 0 means no mask).
struct Draw {
	clipMask: vec4<f32>,
}

// The uniform buffers are bound with dynamic offsets.
@group(0) @binding(0) var<uniform> frame: Frame;
@group(0) @binding(1) var<uniform> draw: Draw;

// The coverage of the clip shapes that are not rectangles: one 8-bit coverage per pixel of a
// rectangle of the surface; a 1x1 texture when there is no mask.
@group(1) @binding(0) var clipMask: texture_2d<f32>;

struct Instance {
	@location(0) quad: vec4<f32>,   // left, top, right, bottom in pixels
	@location(1) inv0: vec4<f32>,   // the inverse transform: a, b, c, d
	@location(2) inv1: vec4<f32>,   // e, f, kind, flags
	@location(3) color: vec4<f32>,  // premultiplied
	@location(4) clip: vec4<f32>,   // left, top, right, bottom in pixels
	@location(5) a: vec4<f32>,      // shape A: center x, y, half width, half height
	@location(6) arad1: vec4<f32>,  // top left x, y; top right x, y
	@location(7) arad2: vec4<f32>,  // bottom right x, y; bottom left x, y
	@location(8) b: vec4<f32>,      // shape B, which is cut out of A
	@location(9) brad1: vec4<f32>,
	@location(10) brad2: vec4<f32>,
	@location(11) param: vec4<f32>, // half stroke width, cap, exponent, pixels per local unit
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) dev: vec2<f32>,
	@location(1) @interpolate(flat) inv0: vec4<f32>,
	@location(2) @interpolate(flat) inv1: vec4<f32>,
	@location(3) @interpolate(flat) color: vec4<f32>,
	@location(4) @interpolate(flat) clip: vec4<f32>,
	@location(5) @interpolate(flat) a: vec4<f32>,
	@location(6) @interpolate(flat) arad1: vec4<f32>,
	@location(7) @interpolate(flat) arad2: vec4<f32>,
	@location(8) @interpolate(flat) b: vec4<f32>,
	@location(9) @interpolate(flat) brad1: vec4<f32>,
	@location(10) @interpolate(flat) brad2: vec4<f32>,
	@location(11) @interpolate(flat) param: vec4<f32>,
}

// The kinds of shape (gpu.ShapeKind*).
const KIND_RRECT: i32 = 0;   // a rounded rectangle, minus shape B when the flags say so
const KIND_SUPER: i32 = 1;   // a rounded superellipse
const KIND_SEGMENT: i32 = 2; // a line segment with caps, or a dot
const KIND_FILL: i32 = 3;    // every pixel
const KIND_BOX: i32 = 4;     // an axis-aligned rectangle in pixels, minus shape B, by exact area
const KIND_OUTLINE: i32 = 5; // every point within a half width of a rounded rectangle's outline
const KIND_SHADOW: i32 = 6;  // a rounded rectangle in pixels blurred by a Gaussian, see shadow.wgsl.inc

// The flags (gpu.ShapeFlag*).
const FLAG_HAS_B: i32 = 1;
const FLAG_ANTIALIAS: i32 = 2;
const FLAG_SIMILARITY: i32 = 4;

// The clip space position of the point dev, in pixels from the top left of the surface.
fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32, instance: Instance) -> VertexOutput {
	// The corners of the two triangles: (0,0) (1,0) (0,1) and (1,0) (1,1) (0,1).
	var corners = array<vec2<f32>, 6>(
		vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
		vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0),
	);
	let dev = mix(instance.quad.xy, instance.quad.zw, corners[index]);
	var out: VertexOutput;
	out.position = toClip(dev);
	out.dev = dev;
	out.inv0 = instance.inv0;
	out.inv1 = instance.inv1;
	out.color = instance.color;
	out.clip = instance.clip;
	out.a = instance.a;
	out.arad1 = instance.arad1;
	out.arad2 = instance.arad2;
	out.b = instance.b;
	out.brad1 = instance.brad1;
	out.brad2 = instance.brad2;
	out.param = instance.param;
	return out;
}

// The coverage of the pixel whose center is dev (in pixels from the top left).
fn clipMaskCoverage(dev: vec2<f32>) -> f32 {
	if draw.clipMask.z <= 0.0 {
		return 1.0;
	}
	let p = vec2<i32>(floor(dev)) - vec2<i32>(draw.clipMask.xy);
	if p.x < 0 || p.y < 0 || p.x >= i32(draw.clipMask.z) || p.y >= i32(draw.clipMask.w) {
		return 0.0;
	}
	return textureLoad(clipMask, p, 0).r;
}

// ---- signed distance fields (sdf.glsl)

// Distance from the corner q of an ellipse with radii r, with q measured from the
// ellipse's center: a first-order estimate that is exact for a circle.
fn ellipseDistance(q: vec2<f32>, r: vec2<f32>) -> f32 {
	let k0 = length(q / r);
	let k1 = length(q / (r * r));
	if k1 > 0.0 {
		return k0 * (k0 - 1.0) / k1;
	}
	return -min(r.x, r.y);
}

// The corner radii of the quadrant that p is in (y points down).
fn cornerRadii(p: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>) -> vec2<f32> {
	if p.x < 0.0 {
		if p.y < 0.0 {
			return r1.xy;
		}
		return r2.zw;
	}
	if p.y < 0.0 {
		return r1.zw;
	}
	return r2.xy;
}

// A rounded rectangle centered on the origin with half extents h.
fn rrectDistance(p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>) -> f32 {
	let r = cornerRadii(p, r1, r2);
	let q = abs(p) - (h - r);
	if q.x > 0.0 && q.y > 0.0 {
		if r.x > 0.0 && r.y > 0.0 {
			return ellipseDistance(q, r);
		}
		return length(q);
	}
	return max(q.x - r.x, q.y - r.y);
}

// The part of the pixel centered at c that the interval [lo, hi] covers.
fn span(c: f32, lo: f32, hi: f32) -> f32 {
	return clamp(min(c + 0.5, hi) - max(c - 0.5, lo), 0.0, 1.0);
}

// The part of the pixel centered at dev that the rectangle r (left, top, right, bottom)
// covers; without antialiasing, 1 when the center is inside.
fn boxCoverage(dev: vec2<f32>, r: vec4<f32>, antialias: bool) -> f32 {
	if antialias {
		return span(dev.x, r.x, r.z) * span(dev.y, r.y, r.w);
	}
	if dev.x >= r.x && dev.x < r.z && dev.y >= r.y && dev.y < r.w {
		return 1.0;
	}
	return 0.0;
}

// A rounded superellipse: the corner boxes in r1 and r2 are filled by the curve
// |x|^n + |y|^n = 1.
fn superDistance(p: vec2<f32>, h: vec2<f32>, r1: vec4<f32>, r2: vec4<f32>, n: f32) -> f32 {
	let r = cornerRadii(p, r1, r2);
	let q = abs(p) - (h - r);
	if q.x > 0.0 && q.y > 0.0 {
		if r.x <= 0.0 || r.y <= 0.0 {
			return length(q);
		}
		let u = q / r;
		let f = pow(u.x, n) + pow(u.y, n) - 1.0;
		let g = n * length(pow(u, vec2<f32>(n - 1.0)) / r);
		if g > 0.0 {
			return f / g;
		}
		return 0.0;
	}
	return max(q.x - r.x, q.y - r.y);
}

// A segment from a to b of half width hw. cap is 0 for butt, 1 for round, 2 for square.
// A segment of no length is a dot: round or square, nothing for butt.
fn segmentDistance(p: vec2<f32>, a: vec2<f32>, b: vec2<f32>, hw: f32, cap: f32) -> f32 {
	let ba = b - a;
	let len = length(ba);
	if len < 1e-5 {
		let pa = p - a;
		if cap == 1.0 {
			return length(pa) - hw;
		}
		if cap == 2.0 {
			return max(abs(pa.x), abs(pa.y)) - hw;
		}
		return 1e9;
	}
	let dir = ba / len;
	let pa = p - (a + b) * 0.5;
	let along = abs(dot(pa, dir));
	let across = abs(dot(pa, vec2<f32>(-dir.y, dir.x)));
	if cap == 1.0 {
		return length(vec2<f32>(max(along - len * 0.5, 0.0), across)) - hw;
	}
	if cap == 2.0 {
		return max(along - len * 0.5 - hw, across - hw);
	}
	return max(along - len * 0.5, across - hw);
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	// The interpolation is off by a rounding error, which would decide a pixel whose
	// center is on an edge; the center is where the position is the nearest half pixel.
	let dev = floor(in.dev) + vec2<f32>(0.5);

	// The clip rectangle: exact coverage, so anti-aliased clip edges are smooth.
	let clipCoverage = span(dev.x, in.clip.x, in.clip.z) * span(dev.y, in.clip.y, in.clip.w) * clipMaskCoverage(dev);

	let kind = i32(in.inv1.z + 0.5);
	let flags = i32(in.inv1.w + 0.5);
	let antialias = (flags & FLAG_ANTIALIAS) != 0;
	let hasB = (flags & FLAG_HAS_B) != 0;
	let p = vec2<f32>(
		in.inv0.x * dev.x + in.inv0.z * dev.y + in.inv1.x,
		in.inv0.y * dev.x + in.inv0.w * dev.y + in.inv1.y);

	// The distance d of the pixel from the shape, negative inside, in the shape's units.
	// A box has its coverage from the exact area instead.
	var d = -1.0;
	var boxCov = 0.0;
	if kind == KIND_BOX {
		boxCov = boxCoverage(dev, in.a, antialias);
		if hasB {
			boxCov -= boxCoverage(dev, in.b, antialias);
		}
	} else if kind == KIND_SHADOW {
		// Shape A is in pixels: center, half extents and radii. param holds the deviation of
		// the blur in pixels and the blur style.
		let c = dev - in.a.xy;
		let blurred = blurredRRect(c, in.a.zw, in.arad1, in.arad2, in.param.x);
		let style = i32(in.param.y + 0.5);
		var own = 0.0;
		if style != 0 {
			own = clamp(0.5 - rrectDistance(c, in.a.zw, in.arad1, in.arad2), 0.0, 1.0);
		}
		boxCov = blurStyle(style, blurred, own);
	} else if kind == KIND_RRECT {
		d = rrectDistance(p - in.a.xy, in.a.zw, in.arad1, in.arad2);
		if hasB {
			d = max(d, -rrectDistance(p - in.b.xy, in.b.zw, in.brad1, in.brad2));
		}
	} else if kind == KIND_OUTLINE {
		d = abs(rrectDistance(p - in.a.xy, in.a.zw, in.arad1, in.arad2)) - in.param.x;
	} else if kind == KIND_SUPER {
		d = superDistance(p - in.a.xy, in.a.zw, in.arad1, in.arad2, in.param.z);
	} else if kind == KIND_SEGMENT {
		d = segmentDistance(p, in.a.xy, in.a.zw, in.param.x, in.param.y);
	}

	// The screen derivatives need every pixel of the quad: they come before anything
	// that returns or discards.
	let g = length(vec2<f32>(dpdx(d), dpdy(d)));

	var coverage: f32;
	if kind == KIND_BOX || kind == KIND_SHADOW {
		coverage = boxCov;
	} else if !antialias {
		coverage = select(0.0, 1.0, d <= 0.0);
	} else if kind == KIND_FILL {
		coverage = 1.0;
	} else {
		// The distance in pixels: the same everywhere for a transform that only rotates
		// and scales evenly, found from the screen derivatives otherwise.
		var pixels: f32;
		if (flags & FLAG_SIMILARITY) != 0 {
			pixels = d * in.param.w;
		} else if g > 1e-9 {
			pixels = d / g;
		} else {
			pixels = select(1.0, -1.0, d <= 0.0);
		}
		coverage = clamp(0.5 - pixels, 0.0, 1.0);
	}
	let total = coverage * clipCoverage;
	if total <= 0.0 || (kind == KIND_SHADOW && total < 1.0 / 1024.0) {
		discard;
	}
	return finish(paintSource(in.color, dev), total, dev);
}
`,
	SPIRV: spirvShape,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Frame {
    float2 surface;
    float2 origin;
};

struct Draw {
    float4 clipMask;
};

struct Instance {
    float4 quad : LOC0;
    float4 inv0_ : LOC1;
    float4 inv1_ : LOC2;
    float4 color : LOC3;
    float4 clip_ : LOC4;
    float4 a : LOC5;
    float4 arad1_ : LOC6;
    float4 arad2_ : LOC7;
    float4 b : LOC8;
    float4 brad1_ : LOC9;
    float4 brad2_ : LOC10;
    float4 param : LOC11;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 dev : LOC0;
    nointerpolation float4 inv0_ : LOC1;
    nointerpolation float4 inv1_ : LOC2;
    nointerpolation float4 color : LOC3;
    nointerpolation float4 clip_ : LOC4;
    nointerpolation float4 a : LOC5;
    nointerpolation float4 arad1_ : LOC6;
    nointerpolation float4 arad2_ : LOC7;
    nointerpolation float4 b : LOC8;
    nointerpolation float4 brad1_ : LOC9;
    nointerpolation float4 brad2_ : LOC10;
    nointerpolation float4 param : LOC11;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;
static const int KIND_RRECT = int(0);
static const int KIND_SUPER = int(1);
static const int KIND_SEGMENT = int(2);
static const int KIND_FILL = int(3);
static const int KIND_BOX = int(4);
static const int KIND_OUTLINE = int(5);
static const int KIND_SHADOW = int(6);
static const int FLAG_HAS_B = int(1);
static const int FLAG_ANTIALIAS = int(2);
static const int FLAG_SIMILARITY = int(4);

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer frame : register(b0, space0) { Frame frame; }
cbuffer draw : register(b1, space0) { Draw draw; }
Texture2D<float4> clipMask : register(t0, space1);

struct VertexOutput_vs_main {
    float2 dev_6 : LOC0;
    nointerpolation float4 inv0_ : LOC1;
    nointerpolation float4 inv1_ : LOC2;
    nointerpolation float4 color : LOC3;
    nointerpolation float4 clip_ : LOC4;
    nointerpolation float4 a_3 : LOC5;
    nointerpolation float4 arad1_ : LOC6;
    nointerpolation float4 arad2_ : LOC7;
    nointerpolation float4 b_2 : LOC8;
    nointerpolation float4 brad1_ : LOC9;
    nointerpolation float4 brad2_ : LOC10;
    nointerpolation float4 param : LOC11;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 dev_7 : LOC0;
    nointerpolation float4 inv0_1 : LOC1;
    nointerpolation float4 inv1_1 : LOC2;
    nointerpolation float4 color_1 : LOC3;
    nointerpolation float4 clip_1 : LOC4;
    nointerpolation float4 a_4 : LOC5;
    nointerpolation float4 arad1_1 : LOC6;
    nointerpolation float4 arad2_1 : LOC7;
    nointerpolation float4 b_3 : LOC8;
    nointerpolation float4 brad1_1 : LOC9;
    nointerpolation float4 brad2_1 : LOC10;
    nointerpolation float4 param_1 : LOC11;
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d_1, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d_1 * (1.0 - sa))) + ((2.0 * s) * d_1));
    }
    return ((((s * (1.0 - da)) + (d_1 * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d_1)) * (sa - s)));
}

float softLightChannel(float s_1, float d_2, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_2 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_2 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_2) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_2 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_2 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_3, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_3, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_3) - max((s_2 * da_2), (d_3 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_3) - min((s_2 * da_2), (d_3 * sa_2)));
        }
        case 18: {
            if ((d_3 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_3 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_3 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_3 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_3 == da_2)) {
                return (d_3 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_3 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_3) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_3 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_3, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_3, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_3) - (2.0 * min((s_2 * da_2), (d_3 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_3) - ((2.0 * s_2) * d_3));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_3 * (1.0 - sa_2))) + (s_2 * d_3));
        }
        default: {
            return (s_2 + (d_3 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_4)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_4.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_4.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_4.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_4.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_4.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_4.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_4.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_4.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_5)
{
    float sa_4 = s_5.w;
    float da_4 = d_5.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_5;
        }
        case 3: {
            return (s_5 + (d_5 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_5 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_5 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_5 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_5 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_5 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_5 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_5), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_5);
        }
        case 14: {
            return ((s_5 + d_5) - (s_5 * d_5));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_5);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_5.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_5.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_5.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local_3 = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local_3 = (t <= 1.0);
        } else {
            local_3 = false;
        }
        bool _e33 = local_3;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i)
{
    float _e9 = paint.rampPos[(i >> 2u)][(i & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_1 = int(0);
    bool local_4 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n_1 = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_1;
        if ((_e10 < n_1)) {
            int _e14 = i_1;
            const float _e15 = rampPosAt(_e14);
            local_4 = (_e15 < t_1);
        } else {
            local_4 = false;
        }
        bool _e18 = local_4;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_1;
            i_1 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_1;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_1;
        if ((_e30 >= n_1)) {
            float4 _e37 = paint.rampColor[asint(asuint(n_1) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_1;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_1;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_1;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_1;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_1;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_5 = (dot(cd, cd) - (dr * dr));
    float2 p_9 = (q - c0_1);
    float b_4 = (dot(p_9, cd) + (r0_ * dr));
    float c_11 = (dot(p_9, p_9) - (r0_ * r0_));
    if ((abs(a_5) < 1e-12)) {
        if ((b_4 == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_11 / (2.0 * b_4));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b_4 * b_4) - (a_5 * c_11));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b_4 + sq) / a_5);
        t2_ = ((b_4 - sq) / a_5);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_5 = (bool)0;
    bool local_6 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_1 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_1 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_1));
            } else {
                x = m_1;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_5 = (_e61 >= size.x);
                } else {
                    local_5 = true;
                }
                bool _e65 = local_5;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_2 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_2));
            } else {
                y = m_2;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_6 = (_e114 >= size.y);
                } else {
                    local_6 = true;
                }
                bool _e118 = local_6;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_2 = abs(x0_);
    if ((x_2 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_2) * x_2)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_2 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_2) * x_2) * x_2) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_2) * x_2)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_2)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j = int(-1);
    int i_2 = (int)0;

    int2 base_1 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_1 = naga_f2i32((_e15 + 0.5));
    int2 size_1 = int2(NagaMipDimensions2D(paintImage, level_1));
    float2 uv = ((q_1 * float2(size_1)) / float2(base_1));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv)), size_1, level_1);
        return _e30;
    }
    float2 f = (uv - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_2 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_2;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_2;
                        int _e65 = j;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_1, level_1);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_6 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_6).xxx), a_6);
    }
    const float4 _e89 = shaderTexel(i0_, size_1, level_1);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_1, level_1);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_1, level_1);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_1, level_1);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_3 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_3 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_3 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_3);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_6 = (q_3 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_6.y, d_6.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_3);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v)
{
    return ((v <= (0.0031308).xxx) ? (12.92 * v) : ((1.055 * pow(v, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_1)
{
    return ((v_1 <= (0.04045).xxx) ? (v_1 / (12.92).xxx) : pow(((v_1 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_12 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_2 = (((((_e32 * c_12.x) + (_e38 * c_12.y)) + (_e45 * c_12.z)) + (_e52 * c_12.w)) + _e59);
            const float4 _e69 = premul(clamp((o_2 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_10 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_10.y) * asuint(int(8)))) + asuint(p_10.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o = (float4)0;
    bool local_7 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_7 = (src.w > 0.0);
    } else {
        local_7 = false;
    }
    bool _e35 = local_7;
    if (_e35) {
        float4 _e36 = o;
        const float _e37 = ditherAt(dev_2);
        o = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o;
    return _e40;
}

float erfApprox(float x0_1)
{
    float s_6 = ((x0_1 < 0.0) ? -1.0 : 1.0);
    float x_3 = abs(x0_1);
    float t_4 = (1.0 / (1.0 + (0.3275911 * x_3)));
    float y_2 = (1.0 - ((((((((((1.0614054 * t_4) - 1.4531521) * t_4) + 1.4214138) * t_4) - 0.28449672) * t_4) + 0.2548296) * t_4) * exp((-(x_3) * x_3))));
    return (s_6 * y_2);
}

float normalCdf(float x_1)
{
    const float _e3 = erfApprox((x_1 * 0.70710677));
    return (0.5 + (0.5 * _e3));
}

float cornerInset(float2 rad, float y_1, float hy, bool top)
{
    bool local_8 = (bool)0;

    if (!((rad.y <= 0.0))) {
        local_8 = (rad.x <= 0.0);
    } else {
        local_8 = true;
    }
    bool _e14 = local_8;
    if (_e14) {
        return 0.0;
    }
    float cy = (top ? (-(hy) + rad.y) : (hy - rad.y));
    float t_5 = (top ? ((cy - y_1) / rad.y) : ((y_1 - cy) / rad.y));
    if ((t_5 <= 0.0)) {
        return 0.0;
    }
    return (rad.x * (1.0 - sqrt(max((1.0 - (t_5 * t_5)), 0.0))));
}

float zoneMass(float y0_, float y1_, float2 p_3, float2 h, float4 r1_, float4 r2_, float sigma, bool top_1)
{
    float sum_1 = 0.0;
    int i_3 = int(0);

    float a_7 = max(y0_, (p_3.y - (4.0 * sigma)));
    float b_5 = min(y1_, (p_3.y + (4.0 * sigma)));
    if ((b_5 <= a_7)) {
        return 0.0;
    }
    float dv = ((b_5 - a_7) / 16.0);
    uint2 loop_bound_3 = uint2(4294967295u, 4294967295u);
    bool loop_init_2 = true;
    while(true) {
        if (all(loop_bound_3 == uint2(0u, 0u))) { break; }
        loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
        if (!loop_init_2) {
            int _e76 = i_3;
            i_3 = asint(asuint(_e76) + asuint(int(1)));
        }
        loop_init_2 = false;
        int _e27 = i_3;
        if ((_e27 < int(16))) {
        } else {
            break;
        }
        {
            int _e30 = i_3;
            float y_3 = (a_7 + ((float(_e30) + 0.5) * dv));
            const float _e42 = cornerInset((top_1 ? r1_.xy : r2_.zw), y_3, h.y, top_1);
            float l_1 = (-(h.x) + _e42);
            const float _e49 = cornerInset((top_1 ? r1_.zw : r2_.xy), y_3, h.y, top_1);
            float r_2 = (h.x - _e49);
            float w = exp((((-0.5 * (y_3 - p_3.y)) * (y_3 - p_3.y)) / (sigma * sigma)));
            float _e61 = sum_1;
            const float _e65 = normalCdf(((r_2 - p_3.x) / sigma));
            const float _e69 = normalCdf(((l_1 - p_3.x) / sigma));
            sum_1 = (_e61 + (w * max((_e65 - _e69), 0.0)));
        }
    }
    float _e78 = sum_1;
    return ((_e78 * dv) / (sigma * 2.5066283));
}

float blurredRRect(float2 p_4, float2 h_1, float4 r1_1, float4 r2_1, float sigma_1)
{
    float mass = (float)0;

    float topH = max(r1_1.y, r1_1.w);
    float botH = max(r2_1.y, r2_1.w);
    const float _e17 = zoneMass(-(h_1.y), (-(h_1.y) + topH), p_4, h_1, r1_1, r2_1, sigma_1, true);
    mass = _e17;
    float _e19 = mass;
    const float _e24 = zoneMass((h_1.y - botH), h_1.y, p_4, h_1, r1_1, r2_1, sigma_1, false);
    mass = (_e19 + _e24);
    float m0_ = (-(h_1.y) + topH);
    float m1_ = (h_1.y - botH);
    if ((m1_ > m0_)) {
        const float _e35 = normalCdf(((m1_ - p_4.y) / sigma_1));
        const float _e39 = normalCdf(((m0_ - p_4.y) / sigma_1));
        float rows = (_e35 - _e39);
        const float _e45 = normalCdf(((h_1.x - p_4.x) / sigma_1));
        const float _e51 = normalCdf(((-(h_1.x) - p_4.x) / sigma_1));
        float cols = (_e45 - _e51);
        float _e53 = mass;
        mass = (_e53 + (rows * cols));
    }
    float _e56 = mass;
    return clamp(_e56, 0.0, 1.0);
}

float blurStyle(int style, float b, float o_1)
{
    if ((style == int(1))) {
        return (o_1 + (b * (1.0 - o_1)));
    }
    if ((style == int(2))) {
        return (b * (1.0 - o_1));
    }
    if ((style == int(3))) {
        return (b * o_1);
    }
    return b;
}

float4 toClip(float2 dev_3)
{
    float2 _e3 = frame.origin;
    float2 p_11 = (dev_3 - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p_11.x / _e9) * 2.0) - 1.0), (1.0 - ((p_11.y / _e19) * 2.0)), 0.0, 1.0);
}

float clipMaskCoverage(float2 dev_4)
{
    bool local_9 = (bool)0;
    bool local_10 = (bool)0;
    bool local_11 = (bool)0;

    float _e4 = draw.clipMask.z;
    if ((_e4 <= 0.0)) {
        return 1.0;
    }
    float4 _e12 = draw.clipMask;
    int2 p_12 = asint(asuint(naga_f2i32(floor(dev_4))) - asuint(naga_f2i32(_e12.xy)));
    if (!((p_12.x < int(0)))) {
        local_9 = (p_12.y < int(0));
    } else {
        local_9 = true;
    }
    bool _e26 = local_9;
    if (!(_e26)) {
        float _e34 = draw.clipMask.z;
        local_10 = (p_12.x >= naga_f2i32(_e34));
    } else {
        local_10 = true;
    }
    bool _e38 = local_10;
    if (!(_e38)) {
        float _e46 = draw.clipMask.w;
        local_11 = (p_12.y >= naga_f2i32(_e46));
    } else {
        local_11 = true;
    }
    bool _e50 = local_11;
    if (_e50) {
        return 0.0;
    }
    float4 _e54 = clipMask.Load(int3(p_12, int(0)));
    return _e54.x;
}

float ellipseDistance(float2 q_2, float2 r)
{
    float k0_ = length((q_2 / r));
    float k1_ = length((q_2 / (r * r)));
    if ((k1_ > 0.0)) {
        return ((k0_ * (k0_ - 1.0)) / k1_);
    }
    return -(min(r.x, r.y));
}

float2 cornerRadii(float2 p_5, float4 r1_2, float4 r2_2)
{
    if ((p_5.x < 0.0)) {
        if ((p_5.y < 0.0)) {
            return r1_2.xy;
        }
        return r2_2.zw;
    }
    if ((p_5.y < 0.0)) {
        return r1_2.zw;
    }
    return r2_2.xy;
}

float rrectDistance(float2 p_6, float2 h_2, float4 r1_3, float4 r2_3)
{
    bool local_12 = (bool)0;
    bool local_13 = (bool)0;

    const float2 _e4 = cornerRadii(p_6, r1_3, r2_3);
    float2 q_4 = (abs(p_6) - (h_2 - _e4));
    if ((q_4.x > 0.0)) {
        local_12 = (q_4.y > 0.0);
    } else {
        local_12 = false;
    }
    bool _e17 = local_12;
    if (_e17) {
        if ((_e4.x > 0.0)) {
            local_13 = (_e4.y > 0.0);
        } else {
            local_13 = false;
        }
        bool _e27 = local_13;
        if (_e27) {
            const float _e28 = ellipseDistance(q_4, _e4);
            return _e28;
        }
        return length(q_4);
    }
    return max((q_4.x - _e4.x), (q_4.y - _e4.y));
}

float span(float c_10, float lo, float hi)
{
    return clamp((min((c_10 + 0.5), hi) - max((c_10 - 0.5), lo)), 0.0, 1.0);
}

float boxCoverage(float2 dev_5, float4 r_1, bool antialias)
{
    bool local_14 = (bool)0;
    bool local_15 = (bool)0;
    bool local_16 = (bool)0;

    if (antialias) {
        const float _e6 = span(dev_5.x, r_1.x, r_1.z);
        const float _e10 = span(dev_5.y, r_1.y, r_1.w);
        return (_e6 * _e10);
    }
    if ((dev_5.x >= r_1.x)) {
        local_14 = (dev_5.x < r_1.z);
    } else {
        local_14 = false;
    }
    bool _e21 = local_14;
    if (_e21) {
        local_15 = (dev_5.y >= r_1.y);
    } else {
        local_15 = false;
    }
    bool _e28 = local_15;
    if (_e28) {
        local_16 = (dev_5.y < r_1.w);
    } else {
        local_16 = false;
    }
    bool _e35 = local_16;
    if (_e35) {
        return 1.0;
    }
    return 0.0;
}

float superDistance(float2 p_7, float2 h_3, float4 r1_4, float4 r2_4, float n)
{
    bool local_17 = (bool)0;
    bool local_18 = (bool)0;

    const float2 _e5 = cornerRadii(p_7, r1_4, r2_4);
    float2 q_5 = (abs(p_7) - (h_3 - _e5));
    if ((q_5.x > 0.0)) {
        local_17 = (q_5.y > 0.0);
    } else {
        local_17 = false;
    }
    bool _e18 = local_17;
    if (_e18) {
        if (!((_e5.x <= 0.0))) {
            local_18 = (_e5.y <= 0.0);
        } else {
            local_18 = true;
        }
        bool _e29 = local_18;
        if (_e29) {
            return length(q_5);
        }
        float2 u_1 = (q_5 / _e5);
        float f_1 = ((pow(u_1.x, n) + pow(u_1.y, n)) - 1.0);
        float g = (n * length((pow(u_1, ((n - 1.0)).xx) / _e5)));
        if ((g > 0.0)) {
            return (f_1 / g);
        }
        return 0.0;
    }
    return max((q_5.x - _e5.x), (q_5.y - _e5.y));
}

float segmentDistance(float2 p_8, float2 a_2, float2 b_1, float hw, float cap)
{
    float2 ba = (b_1 - a_2);
    float len = length(ba);
    if ((len < 1e-5)) {
        float2 pa = (p_8 - a_2);
        if ((cap == 1.0)) {
            return (length(pa) - hw);
        }
        if ((cap == 2.0)) {
            return (max(abs(pa.x), abs(pa.y)) - hw);
        }
        return 1000000000.0;
    }
    float2 dir = (ba / (len).xx);
    float2 pa_1 = (p_8 - ((a_2 + b_1) * 0.5));
    float along = abs(dot(pa_1, dir));
    float across = abs(dot(pa_1, float2(-(dir.y), dir.x)));
    if ((cap == 1.0)) {
        return (length(float2(max((along - (len * 0.5)), 0.0), across)) - hw);
    }
    if ((cap == 2.0)) {
        return max(((along - (len * 0.5)) - hw), (across - hw));
    }
    return max((along - (len * 0.5)), (across - hw));
}

typedef float2 ret_Constructarray6_float2_[6];
ret_Constructarray6_float2_ Constructarray6_float2_(float2 arg0, float2 arg1, float2 arg2, float2 arg3, float2 arg4, float2 arg5) {
    float2 ret[6] = { arg0, arg1, arg2, arg3, arg4, arg5 };
    return ret;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID, Instance instance)
{
    float2 corners[6] = Constructarray6_float2_(float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0));
    VertexOutput out_ = (VertexOutput)0;

    float2 _e27 = corners[min(uint(index), 5u)];
    float2 dev_8 = lerp(instance.quad.xy, instance.quad.zw, _e27);
    const float4 _e31 = toClip(dev_8);
    out_.position = _e31;
    out_.dev = dev_8;
    out_.inv0_ = instance.inv0_;
    out_.inv1_ = instance.inv1_;
    out_.color = instance.color;
    out_.clip_ = instance.clip_;
    out_.a = instance.a;
    out_.arad1_ = instance.arad1_;
    out_.arad2_ = instance.arad2_;
    out_.b = instance.b;
    out_.brad1_ = instance.brad1_;
    out_.brad2_ = instance.brad2_;
    out_.param = instance.param;
    VertexOutput _e55 = out_;
    VertexOutput vertexoutput = _e55;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.dev, vertexoutput.inv0_, vertexoutput.inv1_, vertexoutput.color, vertexoutput.clip_, vertexoutput.a, vertexoutput.arad1_, vertexoutput.arad2_, vertexoutput.b, vertexoutput.brad1_, vertexoutput.brad2_, vertexoutput.param, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.dev_7, fragmentinput_fs_main.inv0_1, fragmentinput_fs_main.inv1_1, fragmentinput_fs_main.color_1, fragmentinput_fs_main.clip_1, fragmentinput_fs_main.a_4, fragmentinput_fs_main.arad1_1, fragmentinput_fs_main.arad2_1, fragmentinput_fs_main.b_3, fragmentinput_fs_main.brad1_1, fragmentinput_fs_main.brad2_1, fragmentinput_fs_main.param_1 };
    float d = -1.0;
    float boxCov = 0.0;
    float own = 0.0;
    float coverage = (float)0;
    bool local = (bool)0;
    float pixels = (float)0;
    bool local_1 = (bool)0;
    bool local_2 = (bool)0;

    float2 dev_9 = (floor(in_.dev) + (0.5).xx);
    const float _e11 = span(dev_9.x, in_.clip_.x, in_.clip_.z);
    const float _e17 = span(dev_9.y, in_.clip_.y, in_.clip_.w);
    const float _e19 = clipMaskCoverage(dev_9);
    float clipCoverage = ((_e11 * _e17) * _e19);
    int kind_2 = naga_f2i32((in_.inv1_.z + 0.5));
    int flags = naga_f2i32((in_.inv1_.w + 0.5));
    bool antialias_1 = ((flags & FLAG_ANTIALIAS) != int(0));
    bool hasB = ((flags & FLAG_HAS_B) != int(0));
    float2 p_13 = float2((((in_.inv0_.x * dev_9.x) + (in_.inv0_.z * dev_9.y)) + in_.inv1_.x), (((in_.inv0_.y * dev_9.x) + (in_.inv0_.w * dev_9.y)) + in_.inv1_.y));
    if ((kind_2 == KIND_BOX)) {
        const float _e71 = boxCoverage(dev_9, in_.a, antialias_1);
        boxCov = _e71;
        if (hasB) {
            float _e72 = boxCov;
            const float _e74 = boxCoverage(dev_9, in_.b, antialias_1);
            boxCov = (_e72 - _e74);
        }
    } else {
        if ((kind_2 == KIND_SHADOW)) {
            float2 c_13 = (dev_9 - in_.a.xy);
            const float _e87 = blurredRRect(c_13, in_.a.zw, in_.arad1_, in_.arad2_, in_.param.x);
            int style_1 = naga_f2i32((in_.param.y + 0.5));
            if ((style_1 != int(0))) {
                const float _e101 = rrectDistance(c_13, in_.a.zw, in_.arad1_, in_.arad2_);
                own = clamp((0.5 - _e101), 0.0, 1.0);
            }
            float _e107 = own;
            const float _e108 = blurStyle(style_1, _e87, _e107);
            boxCov = _e108;
        } else {
            if ((kind_2 == KIND_RRECT)) {
                const float _e118 = rrectDistance((p_13 - in_.a.xy), in_.a.zw, in_.arad1_, in_.arad2_);
                d = _e118;
                if (hasB) {
                    float _e119 = d;
                    const float _e127 = rrectDistance((p_13 - in_.b.xy), in_.b.zw, in_.brad1_, in_.brad2_);
                    d = max(_e119, -(_e127));
                }
            } else {
                if ((kind_2 == KIND_OUTLINE)) {
                    const float _e139 = rrectDistance((p_13 - in_.a.xy), in_.a.zw, in_.arad1_, in_.arad2_);
                    d = (abs(_e139) - in_.param.x);
                } else {
                    if ((kind_2 == KIND_SUPER)) {
                        const float _e155 = superDistance((p_13 - in_.a.xy), in_.a.zw, in_.arad1_, in_.arad2_, in_.param.z);
                        d = _e155;
                    } else {
                        if ((kind_2 == KIND_SEGMENT)) {
                            const float _e166 = segmentDistance(p_13, in_.a.xy, in_.a.zw, in_.param.x, in_.param.y);
                            d = _e166;
                        }
                    }
                }
            }
        }
    }
    float _e167 = d;
    float _e168 = ddx(_e167);
    float _e169 = d;
    float _e170 = ddy(_e169);
    float g_1 = length(float2(_e168, _e170));
    if (!((kind_2 == KIND_BOX))) {
        local = (kind_2 == KIND_SHADOW);
    } else {
        local = true;
    }
    bool _e182 = local;
    if (_e182) {
        float _e183 = boxCov;
        coverage = _e183;
    } else {
        if (!(antialias_1)) {
            float _e185 = d;
            coverage = ((_e185 <= 0.0) ? 1.0 : 0.0);
        } else {
            if ((kind_2 == KIND_FILL)) {
                coverage = 1.0;
            } else {
                if (((flags & FLAG_SIMILARITY) != int(0))) {
                    float _e199 = d;
                    pixels = (_e199 * in_.param.w);
                } else {
                    if ((g_1 > 1e-9)) {
                        float _e205 = d;
                        pixels = (_e205 / g_1);
                    } else {
                        float _e207 = d;
                        pixels = ((_e207 <= 0.0) ? -1.0 : 1.0);
                    }
                }
                float _e213 = pixels;
                coverage = clamp((0.5 - _e213), 0.0, 1.0);
            }
        }
    }
    float _e219 = coverage;
    float total = (_e219 * clipCoverage);
    if (!((total <= 0.0))) {
        if ((kind_2 == KIND_SHADOW)) {
            local_2 = (total < 0.0009765625);
        } else {
            local_2 = false;
        }
        bool _e233 = local_2;
        local_1 = _e233;
    } else {
        local_1 = true;
    }
    bool _e235 = local_1;
    if (_e235) {
        discard;
    }
    const float4 _e237 = paintSource(in_.color, dev_9);
    const float4 _e238 = finish(_e237, total, dev_9);
    return _e238;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}},
		}},
	},
}

ShaderShape is shaders/shape.wgsl.

Flutter: no counterpart

View Source
var ShaderSolid = &Shader{
	Name: "solid",
	WGSL: `// Solid rectangles, one instance each (SPEC §9.3). Ported from the GLES solid
// shader (solid.vert.glsl, solid.frag.glsl) and common.glsl's toClip, with the
// rectangle and the color per instance, as the GLES renderer's instanced quads.
//
// Draw 6 vertices per instance: two triangles made from the vertex index.

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is: a layer draws
	// into a texture that covers a rectangle of the surface.
	origin: vec2<f32>,
}

// The uniform buffer is bound with a dynamic offset.
@group(0) @binding(0) var<uniform> frame: Frame;

struct Instance {
	// x, y, width and height in pixels from the top left of the surface.
	@location(0) rect: vec4<f32>,
	// The color to fill with, premultiplied.
	@location(1) color: vec4<f32>,
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) color: vec4<f32>,
}

// The clip space position of the point dev, in pixels from the top left of the surface.
fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(@builtin(vertex_index) index: u32, instance: Instance) -> VertexOutput {
	// The corners of the two triangles: (0,0) (1,0) (0,1) and (1,0) (1,1) (0,1).
	var corners = array<vec2<f32>, 6>(
		vec2<f32>(0.0, 0.0), vec2<f32>(1.0, 0.0), vec2<f32>(0.0, 1.0),
		vec2<f32>(1.0, 0.0), vec2<f32>(1.0, 1.0), vec2<f32>(0.0, 1.0),
	);
	let corner = corners[index];
	var out: VertexOutput;
	out.position = toClip(instance.rect.xy + corner * instance.rect.zw);
	out.color = instance.color;
	return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	return in.color;
}
`,
	SPIRV: spirvSolid,
	HLSL: `struct Frame {
    float2 surface;
    float2 origin;
};

struct Instance {
    float4 rect : LOC0;
    float4 color : LOC1;
};

struct VertexOutput {
    float4 position : SV_Position;
    float4 color : LOC0;
};

cbuffer frame : register(b0, space0) { Frame frame; }

struct VertexOutput_vs_main {
    float4 color : LOC0;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float4 color_1 : LOC0;
    float4 position_1 : SV_Position;
};

float4 toClip(float2 dev)
{
    float2 _e3 = frame.origin;
    float2 p = (dev - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p.x / _e9) * 2.0) - 1.0), (1.0 - ((p.y / _e19) * 2.0)), 0.0, 1.0);
}

typedef float2 ret_Constructarray6_float2_[6];
ret_Constructarray6_float2_ Constructarray6_float2_(float2 arg0, float2 arg1, float2 arg2, float2 arg3, float2 arg4, float2 arg5) {
    float2 ret[6] = { arg0, arg1, arg2, arg3, arg4, arg5 };
    return ret;
}

VertexOutput_vs_main vs_main(uint index : SV_VertexID, Instance instance)
{
    float2 corners[6] = Constructarray6_float2_(float2(0.0, 0.0), float2(1.0, 0.0), float2(0.0, 1.0), float2(1.0, 0.0), float2(1.0, 1.0), float2(0.0, 1.0));
    VertexOutput out_ = (VertexOutput)0;

    float2 corner = corners[min(uint(index), 5u)];
    const float4 _e32 = toClip((instance.rect.xy + (corner * instance.rect.zw)));
    out_.position = _e32;
    out_.color = instance.color;
    VertexOutput _e35 = out_;
    VertexOutput vertexoutput = _e35;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.color, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.color_1 };
    return in_.color;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{}},
	},
}

ShaderSolid is shaders/solid.wgsl.

Flutter: no counterpart

View Source
var ShaderVertices = &Shader{
	Name: "vertices",
	WGSL: `// Triangles from vertices (Canvas.drawVertices): positions are already in pixels. Ported from
// the GLES vertices shaders (vertices.vert.glsl, vertices.frag.glsl). Draw the vertices as they
// are: three for each triangle.

// The source color of a paint: a solid color, a gradient or an image shader, then the paint's
// color filter and inverted colors, and the blend with what is already drawn. Ported from
// gles/shaders/paint.glsl. Fragment shaders include this file, after blend.wgsl.inc.
//
// The 29 blend modes of Flutter's BlendMode, on premultiplied colors, as the software renderer
// computes them (internal/gpu/soft/blend.go). mode is the index of the mode in BlendMode's
// order: 0 clear, 1 src, 2 dst, 3 srcOver, 4 dstOver, 5 srcIn, 6 dstIn, 7 srcOut, 8 dstOut,
// 9 srcATop, 10 dstATop, 11 xor, 12 plus, 13 modulate, 14 screen, 15 overlay, 16 darken,
// 17 lighten, 18 colorDodge, 19 colorBurn, 20 hardLight, 21 softLight, 22 difference,
// 23 exclusion, 24 multiply, 25 hue, 26 saturation, 27 color, 28 luminosity. Ported from
// gles/shaders/blend.glsl. Shaders include this file with an include line.

fn hardLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	if 2.0 * s <= sa {
		return s * (1.0 - da) + d * (1.0 - sa) + 2.0 * s * d;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + sa * da - 2.0 * (da - d) * (sa - s);
}

fn softLightChannel(s: f32, d: f32, sa: f32, da: f32) -> f32 {
	var m = 0.0;
	if da > 0.0 {
		m = d / da;
	}
	let s2 = 2.0 * s;
	let m4 = 4.0 * m;
	let darkSrc = d * (sa + (s2 - sa) * (1.0 - m));
	let darkDst = (m4 * m4 + m4) * (m - 1.0) + 7.0 * m;
	let liteDst = sqrt(m) - m;
	var tail = liteDst;
	if 4.0 * d <= da {
		tail = darkDst;
	}
	let liteSrc = d * sa + da * (s2 - sa) * tail;
	var body = liteSrc;
	if s2 <= sa {
		body = darkSrc;
	}
	return s * (1.0 - da) + d * (1.0 - sa) + body;
}

fn blendChannel(mode: i32, s: f32, d: f32, sa: f32, da: f32) -> f32 {
	switch mode {
	case 15: {
		return hardLightChannel(d, s, da, sa); // overlay
	}
	case 16: {
		return s + d - max(s * da, d * sa); // darken
	}
	case 17: {
		return s + d - min(s * da, d * sa); // lighten
	}
	case 18: { // colorDodge
		if d == 0.0 {
			return s * (1.0 - da);
		}
		if s == sa {
			return s + d * (1.0 - sa);
		}
		return sa * min(da, d * sa / (sa - s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 19: { // colorBurn
		if d == da {
			return d + s * (1.0 - da);
		}
		if s == 0.0 {
			return d * (1.0 - sa);
		}
		return sa * (da - min(da, (da - d) * sa / s)) + s * (1.0 - da) + d * (1.0 - sa);
	}
	case 20: {
		return hardLightChannel(s, d, sa, da); // hardLight
	}
	case 21: {
		return softLightChannel(s, d, sa, da); // softLight
	}
	case 22: {
		return s + d - 2.0 * min(s * da, d * sa); // difference
	}
	case 23: {
		return s + d - 2.0 * s * d; // exclusion
	}
	case 24: {
		return s * (1.0 - da) + d * (1.0 - sa) + s * d; // multiply
	}
	default: {
		return s + d * (1.0 - sa);
	}
	}
}

fn lum(c: vec3<f32>) -> f32 {
	return 0.30 * c.r + 0.59 * c.g + 0.11 * c.b;
}

fn sat(c: vec3<f32>) -> f32 {
	return max(c.r, max(c.g, c.b)) - min(c.r, min(c.g, c.b));
}

fn setSat(c: vec3<f32>, s: f32) -> vec3<f32> {
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let range = mx - mn;
	if range == 0.0 {
		return vec3<f32>(0.0);
	}
	return (c - vec3<f32>(mn)) * s / range;
}

fn setLum(c: vec3<f32>, l: f32) -> vec3<f32> {
	return c + vec3<f32>(l - lum(c));
}

fn clipColor(c0: vec3<f32>, a: f32) -> vec3<f32> {
	var c = c0;
	let mn = min(c.r, min(c.g, c.b));
	let mx = max(c.r, max(c.g, c.b));
	let l = lum(c);
	if mn < 0.0 {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * l / (l - mn);
	}
	if mx > a {
		c = vec3<f32>(l) + (c - vec3<f32>(l)) * (a - l) / (mx - l);
	}
	return max(c, vec3<f32>(0.0));
}

fn blendNonSeparable(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	var c: vec3<f32>;
	if mode == 25 { // hue
		c = s.rgb * sa;
		c = setSat(c, sat(d.rgb) * sa);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 26 { // saturation
		c = d.rgb * sa;
		c = setSat(c, sat(s.rgb) * da);
		c = setLum(c, lum(d.rgb) * sa);
	} else if mode == 27 { // color
		c = s.rgb * da;
		c = setLum(c, lum(d.rgb) * sa);
	} else { // luminosity
		c = d.rgb * sa;
		c = setLum(c, lum(s.rgb) * da);
	}
	c = clipColor(c, sa * da);
	return vec4<f32>(s.rgb * (1.0 - da) + d.rgb * (1.0 - sa) + c, sa + da - sa * da);
}

// blendPair blends the source s over the destination d with the mode.
fn blendPair(mode: i32, s: vec4<f32>, d: vec4<f32>) -> vec4<f32> {
	let sa = s.a;
	let da = d.a;
	switch mode {
	case 0: {
		return vec4<f32>(0.0);
	}
	case 1: {
		return s;
	}
	case 2: {
		return d;
	}
	case 3: {
		return s + d * (1.0 - sa);
	}
	case 4: {
		return d + s * (1.0 - da);
	}
	case 5: {
		return s * da;
	}
	case 6: {
		return d * sa;
	}
	case 7: {
		return s * (1.0 - da);
	}
	case 8: {
		return d * (1.0 - sa);
	}
	case 9: {
		return s * da + d * (1.0 - sa);
	}
	case 10: {
		return d * sa + s * (1.0 - da);
	}
	case 11: {
		return s * (1.0 - da) + d * (1.0 - sa);
	}
	case 12: {
		return min(s + d, vec4<f32>(1.0));
	}
	case 13: {
		return s * d;
	}
	case 14: {
		return s + d - s * d;
	}
	case 25, 26, 27, 28: {
		return blendNonSeparable(mode, s, d);
	}
	default: {
		// The separable modes defined per channel, whose alpha is that of source-over.
		return vec4<f32>(
			blendChannel(mode, s.r, d.r, sa, da),
			blendChannel(mode, s.g, d.g, sa, da),
			blendChannel(mode, s.b, d.b, sa, da),
			sa + da - sa * da);
	}
	}
}

const SHADER_NONE: i32 = 0;
const SHADER_LINEAR: i32 = 1;
const SHADER_RADIAL: i32 = 2;
const SHADER_CONICAL: i32 = 3;
const SHADER_SWEEP: i32 = 4;
const SHADER_IMAGE: i32 = 5;
const SHADER_CONST: i32 = 6;

const MAX_STOPS: i32 = 32;
const PI: f32 = 3.14159265358979;

// What a draw call's paint sets, the layout of gpu.PaintSpec as float4s (see paintUniform).
struct Paint {
	// The shader kind, the tile modes in x and y (0 clamp, 1 repeated, 2 mirror, 3 decal) and
	// the number of color stops.
	shader: vec4<f32>,
	// The matrix from pixels to the shader's space: a, b, c, d, then e, f.
	xf0: vec4<f32>,
	xf1: vec4<f32>,
	// linear: from x, y, dx, dy; radial: center x, y, radius; conical: focal x, y, focal radius;
	// sweep: center x, y, start, span.
	p0: vec4<f32>,
	// linear: 1 / (dx*dx + dy*dy); conical: end center x, y, end radius.
	p1: vec4<f32>,
	// The image shader's quality (0 none, 1 low, 2 medium, 3 high) and level.
	imageInfo: vec4<f32>,
	// A degenerate gradient: premultiplied color.
	constColor: vec4<f32>,
	// Dither (0 or 1), the color filter (0 none, 1 mode, 2 matrix, 3 linear to sRGB, 4 sRGB to
	// linear), the blend index of a mode filter, and inverted colors (0 or 1).
	flags: vec4<f32>,
	// The blend index of a blend that reads the destination (-1: the blend unit does it), and the
	// pixel of the surface that the copy of the destination starts at.
	blend: vec4<f32>,
	// A mode filter's premultiplied color.
	filterColor: vec4<f32>,
	// A matrix filter: the columns of the 4x5 matrix in 0 to 255 units.
	filterMatrix: array<vec4<f32>, 5>,
	// The gradient's colors (not premultiplied) and positions, four to a float4.
	rampColor: array<vec4<f32>, 32>,
	rampPos: array<vec4<f32>, 8>,
}

@group(0) @binding(2) var<uniform> paint: Paint;
// The image of an image shader, and a copy of the pixels being drawn over.
@group(3) @binding(0) var paintImage: texture_2d<f32>;
@group(3) @binding(1) var dst: texture_2d<f32>;

// ---- gradients

// Applies the tile mode to a position along a gradient; the second component is 0 where the
// shader draws nothing.
fn tileT(t: f32, mode: i32) -> vec2<f32> {
	if mode == 1 {
		return vec2<f32>(t - floor(t), 1.0);
	}
	if mode == 2 {
		let u = t - 2.0 * floor(t / 2.0);
		if u > 1.0 {
			return vec2<f32>(2.0 - u, 1.0);
		}
		return vec2<f32>(u, 1.0);
	}
	if mode == 3 {
		if t >= 0.0 && t <= 1.0 {
			return vec2<f32>(t, 1.0);
		}
		return vec2<f32>(t, 0.0);
	}
	return vec2<f32>(clamp(t, 0.0, 1.0), 1.0);
}

fn rampPosAt(i: i32) -> f32 {
	return paint.rampPos[i >> 2u][i & 3];
}

// The premultiplied color at t, interpolated without premultiplied alpha.
fn rampAt(t: f32) -> vec4<f32> {
	let n = i32(paint.shader.w + 0.5);
	var i = 0;
	while i < n && rampPosAt(i) < t {
		i++;
	}
	var c: vec4<f32>;
	if i == 0 {
		c = paint.rampColor[0];
	} else if i >= n {
		c = paint.rampColor[n - 1];
	} else {
		let p0 = rampPosAt(i - 1);
		let p1 = rampPosAt(i);
		if p1 <= p0 {
			c = paint.rampColor[i];
		} else {
			c = mix(paint.rampColor[i - 1], paint.rampColor[i], (t - p0) / (p1 - p0));
		}
	}
	c = clamp(c, vec4<f32>(0.0), vec4<f32>(1.0));
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn gradientAt(t0: f32) -> vec4<f32> {
	if t0 != t0 {
		return vec4<f32>(0.0);
	}
	let tt = tileT(t0, i32(paint.shader.y + 0.5));
	if tt.y == 0.0 {
		return vec4<f32>(0.0);
	}
	return rampAt(tt.x);
}

// The two-point conical gradient from the focal circle to the end circle: the color at a point
// is that of the largest t whose circle, which moves and grows from one to the other, goes
// through the point.
fn conicalAt(q: vec2<f32>) -> vec4<f32> {
	let c0 = paint.p0.xy;
	let r0 = paint.p0.z;
	let cd = paint.p1.xy - c0;
	let dr = paint.p1.z - r0;
	let a = dot(cd, cd) - dr * dr;
	let p = q - c0;
	let b = dot(p, cd) + r0 * dr;
	let c = dot(p, p) - r0 * r0;
	var t: f32;
	if abs(a) < 1e-12 {
		if b == 0.0 {
			return vec4<f32>(0.0);
		}
		t = c / (2.0 * b);
		if r0 + t * dr < 0.0 {
			return vec4<f32>(0.0);
		}
	} else {
		let disc = b * b - a * c;
		if disc < 0.0 {
			return vec4<f32>(0.0);
		}
		let sq = sqrt(disc);
		var t1 = (b + sq) / a;
		var t2 = (b - sq) / a;
		if t1 < t2 {
			let tmp = t1;
			t1 = t2;
			t2 = tmp;
		}
		if r0 + t1 * dr >= 0.0 {
			t = t1;
		} else if r0 + t2 * dr >= 0.0 {
			t = t2;
		} else {
			return vec4<f32>(0.0);
		}
	}
	return gradientAt(t);
}

// ---- image shaders

// A texel of the shader's image at a level, with the tile modes applied to the pixel's index;
// transparent where decal draws nothing.
fn shaderTexel(p: vec2<i32>, size: vec2<i32>, level: i32) -> vec4<f32> {
	var x = p.x;
	var y = p.y;
	let tileX = i32(paint.shader.y + 0.5);
	let tileY = i32(paint.shader.z + 0.5);
	if tileX == 1 {
		x = ((x % size.x) + size.x) % size.x;
	} else if tileX == 2 {
		let m = ((x % (2 * size.x)) + 2 * size.x) % (2 * size.x);
		if m >= size.x {
			x = 2 * size.x - 1 - m;
		} else {
			x = m;
		}
	} else if tileX == 3 {
		if x < 0 || x >= size.x {
			return vec4<f32>(0.0);
		}
	} else {
		x = clamp(x, 0, size.x - 1);
	}
	if tileY == 1 {
		y = ((y % size.y) + size.y) % size.y;
	} else if tileY == 2 {
		let m = ((y % (2 * size.y)) + 2 * size.y) % (2 * size.y);
		if m >= size.y {
			y = 2 * size.y - 1 - m;
		} else {
			y = m;
		}
	} else if tileY == 3 {
		if y < 0 || y >= size.y {
			return vec4<f32>(0.0);
		}
	} else {
		y = clamp(y, 0, size.y - 1);
	}
	return textureLoad(paintImage, vec2<i32>(x, y), level);
}

fn mitchellWeight(x0: f32) -> f32 {
	let b = 1.0 / 3.0;
	let c = 1.0 / 3.0;
	let x = abs(x0);
	if x < 1.0 {
		return ((12.0 - 9.0 * b - 6.0 * c) * x * x * x + (-18.0 + 12.0 * b + 6.0 * c) * x * x + (6.0 - 2.0 * b)) / 6.0;
	}
	if x < 2.0 {
		return ((-b - 6.0 * c) * x * x * x + (6.0 * b + 30.0 * c) * x * x + (-12.0 * b - 48.0 * c) * x + (8.0 * b + 24.0 * c)) / 6.0;
	}
	return 0.0;
}

// The shader's image at the point q of its pixels, with the filter quality.
fn imageShaderAt(q: vec2<f32>) -> vec4<f32> {
	let base = vec2<i32>(textureDimensions(paintImage, 0));
	let quality = i32(paint.imageInfo.x + 0.5);
	let level = i32(paint.imageInfo.y + 0.5);
	let size = vec2<i32>(textureDimensions(paintImage, level));
	let uv = q * vec2<f32>(size) / vec2<f32>(base);
	if quality == 0 {
		return shaderTexel(vec2<i32>(floor(uv)), size, level);
	}
	let f = uv - vec2<f32>(0.5);
	let f0 = floor(f);
	let t = f - f0;
	let i0 = vec2<i32>(f0);
	if quality == 3 {
		var sum = vec4<f32>(0.0);
		for (var j = -1; j <= 2; j++) {
			let wy = mitchellWeight(f32(j) - t.y);
			for (var i = -1; i <= 2; i++) {
				sum += wy * mitchellWeight(f32(i) - t.x) * shaderTexel(i0 + vec2<i32>(i, j), size, level);
			}
		}
		let a = clamp(sum.a, 0.0, 1.0);
		return vec4<f32>(clamp(sum.rgb, vec3<f32>(0.0), vec3<f32>(a)), a);
	}
	let c00 = shaderTexel(i0, size, level);
	let c10 = shaderTexel(i0 + vec2<i32>(1, 0), size, level);
	let c01 = shaderTexel(i0 + vec2<i32>(0, 1), size, level);
	let c11 = shaderTexel(i0 + vec2<i32>(1, 1), size, level);
	return mix(mix(c00, c10, t.x), mix(c01, c11, t.x), t.y);
}

// The premultiplied color of the shader at the pixel at p (or, for vertices, at the texture
// coordinates p).
fn shaderColor(p: vec2<f32>) -> vec4<f32> {
	let q = vec2<f32>(
		paint.xf0.x * p.x + paint.xf0.z * p.y + paint.xf1.x,
		paint.xf0.y * p.x + paint.xf0.w * p.y + paint.xf1.y);
	let kind = i32(paint.shader.x + 0.5);
	if kind == SHADER_LINEAR {
		return gradientAt(dot(q - paint.p0.xy, paint.p0.zw) * paint.p1.x);
	}
	if kind == SHADER_RADIAL {
		return gradientAt(length(q - paint.p0.xy) / paint.p0.z);
	}
	if kind == SHADER_CONICAL {
		return conicalAt(q);
	}
	if kind == SHADER_SWEEP {
		let d = q - paint.p0.xy;
		var a = atan2(d.y, d.x) - paint.p0.z;
		a = a - 2.0 * PI * floor(a / (2.0 * PI));
		return gradientAt(a / paint.p0.w);
	}
	if kind == SHADER_IMAGE {
		return imageShaderAt(q);
	}
	return paint.constColor;
}

// ---- color filters

fn unpremul(c: vec4<f32>) -> vec4<f32> {
	if c.a <= 0.0 {
		return vec4<f32>(0.0);
	}
	return vec4<f32>(clamp(c.rgb / c.a, vec3<f32>(0.0), vec3<f32>(1.0)), c.a);
}

fn premul(c: vec4<f32>) -> vec4<f32> {
	return vec4<f32>(c.rgb * c.a, c.a);
}

fn linearToSrgb(v: vec3<f32>) -> vec3<f32> {
	return select(1.055 * pow(v, vec3<f32>(1.0 / 2.4)) - vec3<f32>(0.055), 12.92 * v, v <= vec3<f32>(0.0031308));
}

fn srgbToLinear(v: vec3<f32>) -> vec3<f32> {
	return select(pow((v + vec3<f32>(0.055)) / 1.055, vec3<f32>(2.4)), v / 12.92, v <= vec3<f32>(0.04045));
}

// The paint's color filter, then its inverted colors.
fn applyFilters(px0: vec4<f32>) -> vec4<f32> {
	var px = px0;
	let kind = i32(paint.flags.y + 0.5);
	if kind == 1 {
		px = blendPair(i32(paint.flags.z + 0.5), paint.filterColor, px);
	} else if kind == 2 {
		let c = unpremul(px) * 255.0;
		let o = paint.filterMatrix[0] * c.r + paint.filterMatrix[1] * c.g + paint.filterMatrix[2] * c.b + paint.filterMatrix[3] * c.a + paint.filterMatrix[4];
		px = premul(clamp(o / 255.0, vec4<f32>(0.0), vec4<f32>(1.0)));
	} else if kind == 3 {
		let c = unpremul(px);
		px = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	} else if kind == 4 {
		let c = unpremul(px);
		px = premul(vec4<f32>(srgbToLinear(c.rgb), c.a));
	}
	if paint.flags.w != 0.0 {
		let c = unpremul(px);
		px = premul(vec4<f32>(vec3<f32>(1.0) - c.rgb, c.a));
	}
	return px;
}

// The source color of a paint at the pixel dev: base is the paint's color, premultiplied, or for
// a shader its alpha in all four components; p is the point the shader is evaluated at.
fn paintSource(base: vec4<f32>, p: vec2<f32>) -> vec4<f32> {
	var c = base;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		c = shaderColor(p) * base;
	}
	return applyFilters(c);
}

// ---- output

var<private> bayer: array<f32, 64> = array<f32, 64>(
	0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
	48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
	12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
	60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
	3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
	51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
	15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
	63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);

// The ordered dither of gradients: up to half a level of 8 bits either way.
fn ditherAt(dev: vec2<f32>) -> f32 {
	let p = vec2<i32>(floor(dev)) & vec2<i32>(7);
	return ((bayer[p.y * 8 + p.x] + 0.5) / 64.0 - 0.5) / 255.0;
}

// The pixel of the copy of what is being drawn over at the pixel dev.
fn destinationAt(dev: vec2<f32>) -> vec4<f32> {
	return textureLoad(dst, vec2<i32>(floor(dev)) - vec2<i32>(paint.blend.yz), 0);
}

// The color to write for the source src that covers the pixel by cov: with a fixed-function
// blend, the source scaled by the coverage; with any other mode, the destination mixed toward
// the blended color by the coverage.
fn finish(src: vec4<f32>, cov: f32, dev: vec2<f32>) -> vec4<f32> {
	var o: vec4<f32>;
	if paint.blend.x < 0.0 {
		o = src * cov;
	} else {
		let d = destinationAt(dev);
		o = d + (blendPair(i32(paint.blend.x + 0.5), src, d) - d) * cov;
	}
	if paint.flags.x != 0.0 && src.a > 0.0 {
		o = o + vec4<f32>(ditherAt(dev));
	}
	return o;
}

struct Frame {
	// The size of the texture being drawn into, in pixels.
	surface: vec2<f32>,
	// The pixel of the surface that the texture's top left corner is.
	origin: vec2<f32>,
}

// What a draw call sets.
struct Draw {
	// The clip mask's rectangle in the surface (x, y, width, height; a width of 0 means none).
	clipMask: vec4<f32>,
	// The paint's color, premultiplied.
	paint: vec4<f32>,
	// The clip rectangle: left, top, right, bottom in pixels.
	clip: vec4<f32>,
	// The blend index of the paint (source) with the vertex colors (destination), whether the
	// vertices have colors (0 or 1), whether the shader is evaluated at the texture coordinates
	// (0 or 1), and one unused.
	mode: vec4<f32>,
}

@group(0) @binding(0) var<uniform> frame: Frame;
@group(0) @binding(1) var<uniform> draw: Draw;
@group(1) @binding(0) var clipMask: texture_2d<f32>;

struct VertexInput {
	@location(0) pos: vec2<f32>,
	@location(1) color: vec4<f32>, // not premultiplied
	@location(2) tex: vec2<f32>,   // texture coordinates, for a shader
}

struct VertexOutput {
	@builtin(position) position: vec4<f32>,
	@location(0) dev: vec2<f32>,
	@location(1) color: vec4<f32>,
	@location(2) tex: vec2<f32>,
}

fn toClip(dev: vec2<f32>) -> vec4<f32> {
	let p = dev - frame.origin;
	return vec4<f32>(p.x / frame.surface.x * 2.0 - 1.0, 1.0 - p.y / frame.surface.y * 2.0, 0.0, 1.0);
}

@vertex
fn vs_main(v: VertexInput) -> VertexOutput {
	var out: VertexOutput;
	out.position = toClip(v.pos);
	out.dev = v.pos;
	out.color = v.color;
	out.tex = v.tex;
	return out;
}

// The part of the pixel centered at c that the interval [lo, hi] covers.
fn span(c: f32, lo: f32, hi: f32) -> f32 {
	return clamp(min(c + 0.5, hi) - max(c - 0.5, lo), 0.0, 1.0);
}

fn clipMaskCoverage(dev: vec2<f32>) -> f32 {
	if draw.clipMask.z <= 0.0 {
		return 1.0;
	}
	let p = vec2<i32>(floor(dev)) - vec2<i32>(draw.clipMask.xy);
	if p.x < 0 || p.y < 0 || p.x >= i32(draw.clipMask.z) || p.y >= i32(draw.clipMask.w) {
		return 0.0;
	}
	return textureLoad(clipMask, p, 0).r;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
	let dev = floor(in.dev) + vec2<f32>(0.5);
	var color = draw.paint;
	if i32(paint.shader.x + 0.5) != SHADER_NONE {
		var at = dev;
		if draw.mode.z != 0.0 {
			at = in.tex;
		}
		color = shaderColor(at) * draw.paint;
	}
	if draw.mode.y != 0.0 {
		color = blendPair(i32(draw.mode.x + 0.5), color, vec4<f32>(in.color.rgb * in.color.a, in.color.a));
	}
	color = applyFilters(color);
	let coverage = span(dev.x, draw.clip.x, draw.clip.z) * span(dev.y, draw.clip.y, draw.clip.w) * clipMaskCoverage(dev);
	return finish(color, coverage, dev);
}
`,
	SPIRV: spirvVertices,
	HLSL: `struct Paint {
    float4 shader;
    float4 xf0_;
    float4 xf1_;
    float4 p0_;
    float4 p1_;
    float4 imageInfo;
    float4 constColor;
    float4 flags;
    float4 blend;
    float4 filterColor;
    float4 filterMatrix[5];
    float4 rampColor[32];
    float4 rampPos[8];
};

struct Frame {
    float2 surface;
    float2 origin;
};

struct Draw {
    float4 clipMask;
    float4 paint;
    float4 clip_;
    float4 mode;
};

struct VertexInput {
    float2 pos : LOC0;
    float4 color : LOC1;
    float2 tex : LOC2;
};

struct VertexOutput {
    float4 position : SV_Position;
    float2 dev : LOC0;
    float4 color : LOC1;
    float2 tex : LOC2;
};

typedef float ret_Constructarray64_float_[64];
ret_Constructarray64_float_ Constructarray64_float_(float arg0, float arg1, float arg2, float arg3, float arg4, float arg5, float arg6, float arg7, float arg8, float arg9, float arg10, float arg11, float arg12, float arg13, float arg14, float arg15, float arg16, float arg17, float arg18, float arg19, float arg20, float arg21, float arg22, float arg23, float arg24, float arg25, float arg26, float arg27, float arg28, float arg29, float arg30, float arg31, float arg32, float arg33, float arg34, float arg35, float arg36, float arg37, float arg38, float arg39, float arg40, float arg41, float arg42, float arg43, float arg44, float arg45, float arg46, float arg47, float arg48, float arg49, float arg50, float arg51, float arg52, float arg53, float arg54, float arg55, float arg56, float arg57, float arg58, float arg59, float arg60, float arg61, float arg62, float arg63) {
    float ret[64] = { arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25, arg26, arg27, arg28, arg29, arg30, arg31, arg32, arg33, arg34, arg35, arg36, arg37, arg38, arg39, arg40, arg41, arg42, arg43, arg44, arg45, arg46, arg47, arg48, arg49, arg50, arg51, arg52, arg53, arg54, arg55, arg56, arg57, arg58, arg59, arg60, arg61, arg62, arg63 };
    return ret;
}

static const int SHADER_NONE = int(0);
static const int SHADER_LINEAR = int(1);
static const int SHADER_RADIAL = int(2);
static const int SHADER_CONICAL = int(3);
static const int SHADER_SWEEP = int(4);
static const int SHADER_IMAGE = int(5);
static const int SHADER_CONST = int(6);
static const int MAX_STOPS = int(32);
static const float PI = 3.1415927;

cbuffer paint : register(b2, space0) { Paint paint; }
Texture2D<float4> paintImage : register(t0, space3);
Texture2D<float4> dst_ : register(t1, space3);
static float bayer[64] = Constructarray64_float_(0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0, 48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0, 12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0, 60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0, 3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0, 51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0, 15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0, 63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0);
cbuffer frame : register(b0, space0) { Frame frame; }
cbuffer draw : register(b1, space0) { Draw draw; }
Texture2D<float4> clipMask : register(t0, space1);

struct VertexOutput_vs_main {
    float2 dev_5 : LOC0;
    float4 color_1 : LOC1;
    float2 tex : LOC2;
    float4 position : SV_Position;
};

struct FragmentInput_fs_main {
    float2 dev_6 : LOC0;
    float4 color_2 : LOC1;
    float2 tex_1 : LOC2;
    float4 position_1 : SV_Position;
};

float hardLightChannel(float s, float d, float sa, float da)
{
    if (((2.0 * s) <= sa)) {
        return (((s * (1.0 - da)) + (d * (1.0 - sa))) + ((2.0 * s) * d));
    }
    return ((((s * (1.0 - da)) + (d * (1.0 - sa))) + (sa * da)) - ((2.0 * (da - d)) * (sa - s)));
}

float softLightChannel(float s_1, float d_1, float sa_1, float da_1)
{
    float m = 0.0;
    float tail = (float)0;
    float body = (float)0;

    if ((da_1 > 0.0)) {
        m = (d_1 / da_1);
    }
    float s2_ = (2.0 * s_1);
    float _e11 = m;
    float m4_ = (4.0 * _e11);
    float _e15 = m;
    float darkSrc = (d_1 * (sa_1 + ((s2_ - sa_1) * (1.0 - _e15))));
    float _e23 = m;
    float _e27 = m;
    float darkDst = ((((m4_ * m4_) + m4_) * (_e23 - 1.0)) + (7.0 * _e27));
    float _e31 = m;
    float _e33 = m;
    float liteDst = (sqrt(_e31) - _e33);
    tail = liteDst;
    if (((4.0 * d_1) <= da_1)) {
        tail = darkDst;
    }
    float _e42 = tail;
    float liteSrc = ((d_1 * sa_1) + ((da_1 * (s2_ - sa_1)) * _e42));
    body = liteSrc;
    if ((s2_ <= sa_1)) {
        body = darkSrc;
    }
    float _e54 = body;
    return (((s_1 * (1.0 - da_1)) + (d_1 * (1.0 - sa_1))) + _e54);
}

float blendChannel(int mode, float s_2, float d_2, float sa_2, float da_2)
{
    switch(mode) {
        case 15: {
            const float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return ((s_2 + d_2) - max((s_2 * da_2), (d_2 * sa_2)));
        }
        case 17: {
            return ((s_2 + d_2) - min((s_2 * da_2), (d_2 * sa_2)));
        }
        case 18: {
            if ((d_2 == 0.0)) {
                return (s_2 * (1.0 - da_2));
            }
            if ((s_2 == sa_2)) {
                return (s_2 + (d_2 * (1.0 - sa_2)));
            }
            return (((sa_2 * min(da_2, ((d_2 * sa_2) / (sa_2 - s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 19: {
            if ((d_2 == da_2)) {
                return (d_2 + (s_2 * (1.0 - da_2)));
            }
            if ((s_2 == 0.0)) {
                return (d_2 * (1.0 - sa_2));
            }
            return (((sa_2 * (da_2 - min(da_2, (((da_2 - d_2) * sa_2) / s_2)))) + (s_2 * (1.0 - da_2))) + (d_2 * (1.0 - sa_2)));
        }
        case 20: {
            const float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            const float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return ((s_2 + d_2) - (2.0 * min((s_2 * da_2), (d_2 * sa_2))));
        }
        case 23: {
            return ((s_2 + d_2) - ((2.0 * s_2) * d_2));
        }
        case 24: {
            return (((s_2 * (1.0 - da_2)) + (d_2 * (1.0 - sa_2))) + (s_2 * d_2));
        }
        default: {
            return (s_2 + (d_2 * (1.0 - sa_2)));
        }
    }
}

float lum(float3 c)
{
    return (((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z));
}

float sat(float3 c_1)
{
    return (max(c_1.x, max(c_1.y, c_1.z)) - min(c_1.x, min(c_1.y, c_1.z)));
}

float3 setSat(float3 c_2, float s_3)
{
    float mn = min(c_2.x, min(c_2.y, c_2.z));
    float mx = max(c_2.x, max(c_2.y, c_2.z));
    float range = (mx - mn);
    if ((range == 0.0)) {
        return (0.0).xxx;
    }
    return (((c_2 - (mn).xxx) * s_3) / (range).xxx);
}

float3 setLum(float3 c_3, float l)
{
    const float _e2 = lum(c_3);
    return (c_3 + ((l - _e2)).xxx);
}

float3 clipColor(float3 c0_, float a)
{
    float3 c_4 = (float3)0;

    c_4 = c0_;
    float _e4 = c_4.x;
    float _e6 = c_4.y;
    float _e8 = c_4.z;
    float mn_1 = min(_e4, min(_e6, _e8));
    float _e12 = c_4.x;
    float _e14 = c_4.y;
    float _e16 = c_4.z;
    float mx_1 = max(_e12, max(_e14, _e16));
    float3 _e19 = c_4;
    const float _e20 = lum(_e19);
    if ((mn_1 < 0.0)) {
        float3 _e24 = c_4;
        c_4 = ((_e20).xxx + (((_e24 - (_e20).xxx) * _e20) / ((_e20 - mn_1)).xxx));
    }
    if ((mx_1 > a)) {
        float3 _e34 = c_4;
        c_4 = ((_e20).xxx + (((_e34 - (_e20).xxx) * (a - _e20)) / ((mx_1 - _e20)).xxx));
    }
    float3 _e43 = c_4;
    return max(_e43, (0.0).xxx);
}

float4 blendNonSeparable(int mode_1, float4 s_4, float4 d_3)
{
    float3 c_5 = (float3)0;

    float sa_3 = s_4.w;
    float da_3 = d_3.w;
    if ((mode_1 == int(25))) {
        c_5 = (s_4.xyz * sa_3);
        float3 _e10 = c_5;
        const float _e12 = sat(d_3.xyz);
        const float3 _e14 = setSat(_e10, (_e12 * sa_3));
        c_5 = _e14;
        float3 _e15 = c_5;
        const float _e17 = lum(d_3.xyz);
        const float3 _e19 = setLum(_e15, (_e17 * sa_3));
        c_5 = _e19;
    } else {
        if ((mode_1 == int(26))) {
            c_5 = (d_3.xyz * sa_3);
            float3 _e24 = c_5;
            const float _e26 = sat(s_4.xyz);
            const float3 _e28 = setSat(_e24, (_e26 * da_3));
            c_5 = _e28;
            float3 _e29 = c_5;
            const float _e31 = lum(d_3.xyz);
            const float3 _e33 = setLum(_e29, (_e31 * sa_3));
            c_5 = _e33;
        } else {
            if ((mode_1 == int(27))) {
                c_5 = (s_4.xyz * da_3);
                float3 _e38 = c_5;
                const float _e40 = lum(d_3.xyz);
                const float3 _e42 = setLum(_e38, (_e40 * sa_3));
                c_5 = _e42;
            } else {
                c_5 = (d_3.xyz * sa_3);
                float3 _e45 = c_5;
                const float _e47 = lum(s_4.xyz);
                const float3 _e49 = setLum(_e45, (_e47 * da_3));
                c_5 = _e49;
            }
        }
    }
    float3 _e50 = c_5;
    const float3 _e52 = clipColor(_e50, (sa_3 * da_3));
    c_5 = _e52;
    float3 _e62 = c_5;
    return float4((((s_4.xyz * (1.0 - da_3)) + (d_3.xyz * (1.0 - sa_3))) + _e62), ((sa_3 + da_3) - (sa_3 * da_3)));
}

float4 blendPair(int mode_2, float4 s_5, float4 d_4)
{
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return (0.0).xxxx;
        }
        case 1: {
            return s_5;
        }
        case 2: {
            return d_4;
        }
        case 3: {
            return (s_5 + (d_4 * (1.0 - sa_4)));
        }
        case 4: {
            return (d_4 + (s_5 * (1.0 - da_4)));
        }
        case 5: {
            return (s_5 * da_4);
        }
        case 6: {
            return (d_4 * sa_4);
        }
        case 7: {
            return (s_5 * (1.0 - da_4));
        }
        case 8: {
            return (d_4 * (1.0 - sa_4));
        }
        case 9: {
            return ((s_5 * da_4) + (d_4 * (1.0 - sa_4)));
        }
        case 10: {
            return ((d_4 * sa_4) + (s_5 * (1.0 - da_4)));
        }
        case 11: {
            return ((s_5 * (1.0 - da_4)) + (d_4 * (1.0 - sa_4)));
        }
        case 12: {
            return min((s_5 + d_4), (1.0).xxxx);
        }
        case 13: {
            return (s_5 * d_4);
        }
        case 14: {
            return ((s_5 + d_4) - (s_5 * d_4));
        }
        case 25:
        case 26:
        case 27:
        case 28: {
            const float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            const float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            const float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            const float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return float4(_e51, _e54, _e57, ((sa_4 + da_4) - (sa_4 * da_4)));
        }
    }
}

float2 tileT(float t, int mode_3)
{
    bool local = (bool)0;

    if ((mode_3 == int(1))) {
        return float2((t - floor(t)), 1.0);
    }
    if ((mode_3 == int(2))) {
        float u = (t - (2.0 * floor((t / 2.0))));
        if ((u > 1.0)) {
            return float2((2.0 - u), 1.0);
        }
        return float2(u, 1.0);
    }
    if ((mode_3 == int(3))) {
        if ((t >= 0.0)) {
            local = (t <= 1.0);
        } else {
            local = false;
        }
        bool _e33 = local;
        if (_e33) {
            return float2(t, 1.0);
        }
        return float2(t, 0.0);
    }
    return float2(clamp(t, 0.0, 1.0), 1.0);
}

float rampPosAt(int i)
{
    float _e9 = paint.rampPos[(i >> 2u)][(i & int(3))];
    return _e9;
}

int naga_f2i32(float value) {
    return int(clamp(value, -2147483600.0, 2147483500.0));
}

float4 rampAt(float t_1)
{
    int i_1 = int(0);
    bool local_1 = (bool)0;
    float4 c_6 = (float4)0;

    float _e4 = paint.shader.w;
    int n = naga_f2i32((_e4 + 0.5));
    uint2 loop_bound = uint2(4294967295u, 4294967295u);
    while(true) {
        if (all(loop_bound == uint2(0u, 0u))) { break; }
        loop_bound -= uint2(loop_bound.y == 0u, 1u);
        int _e10 = i_1;
        if ((_e10 < n)) {
            int _e14 = i_1;
            const float _e15 = rampPosAt(_e14);
            local_1 = (_e15 < t_1);
        } else {
            local_1 = false;
        }
        bool _e18 = local_1;
        if (_e18) {
        } else {
            break;
        }
        {
            int _e20 = i_1;
            i_1 = asint(asuint(_e20) + asuint(int(1)));
        }
    }
    int _e23 = i_1;
    if ((_e23 == int(0))) {
        float4 _e29 = paint.rampColor[0];
        c_6 = _e29;
    } else {
        int _e30 = i_1;
        if ((_e30 >= n)) {
            float4 _e37 = paint.rampColor[asint(asuint(n) - asuint(int(1)))];
            c_6 = _e37;
        } else {
            int _e38 = i_1;
            const float _e41 = rampPosAt(asint(asuint(_e38) - asuint(int(1))));
            int _e42 = i_1;
            const float _e43 = rampPosAt(_e42);
            if ((_e43 <= _e41)) {
                int _e47 = i_1;
                float4 _e49 = paint.rampColor[_e47];
                c_6 = _e49;
            } else {
                int _e52 = i_1;
                float4 _e56 = paint.rampColor[asint(asuint(_e52) - asuint(int(1)))];
                int _e59 = i_1;
                float4 _e61 = paint.rampColor[_e59];
                c_6 = lerp(_e56, _e61, ((t_1 - _e41) / (_e43 - _e41)));
            }
        }
    }
    float4 _e66 = c_6;
    c_6 = clamp(_e66, (0.0).xxxx, (1.0).xxxx);
    float4 _e72 = c_6;
    float _e75 = c_6.w;
    float _e78 = c_6.w;
    return float4((_e72.xyz * _e75), _e78);
}

float4 gradientAt(float t0_)
{
    if ((t0_ != t0_)) {
        return (0.0).xxxx;
    }
    float _e7 = paint.shader.y;
    const float2 _e11 = tileT(t0_, naga_f2i32((_e7 + 0.5)));
    if ((_e11.y == 0.0)) {
        return (0.0).xxxx;
    }
    const float4 _e18 = rampAt(_e11.x);
    return _e18;
}

float4 conicalAt(float2 q)
{
    float t_2 = (float)0;
    float t1_ = (float)0;
    float t2_ = (float)0;

    float4 _e3 = paint.p0_;
    float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    float4 _e11 = paint.p1_;
    float2 cd = (_e11.xy - c0_1);
    float _e17 = paint.p1_.z;
    float dr = (_e17 - r0_);
    float a_2 = (dot(cd, cd) - (dr * dr));
    float2 p_3 = (q - c0_1);
    float b = (dot(p_3, cd) + (r0_ * dr));
    float c_11 = (dot(p_3, p_3) - (r0_ * r0_));
    if ((abs(a_2) < 1e-12)) {
        if ((b == 0.0)) {
            return (0.0).xxxx;
        }
        t_2 = (c_11 / (2.0 * b));
        float _e40 = t_2;
        if (((r0_ + (_e40 * dr)) < 0.0)) {
            return (0.0).xxxx;
        }
    } else {
        float disc = ((b * b) - (a_2 * c_11));
        if ((disc < 0.0)) {
            return (0.0).xxxx;
        }
        float sq = sqrt(disc);
        t1_ = ((b + sq) / a_2);
        t2_ = ((b - sq) / a_2);
        float _e61 = t1_;
        float _e62 = t2_;
        if ((_e61 < _e62)) {
            float tmp = t1_;
            float _e65 = t2_;
            t1_ = _e65;
            t2_ = tmp;
        }
        float _e66 = t1_;
        if (((r0_ + (_e66 * dr)) >= 0.0)) {
            float _e71 = t1_;
            t_2 = _e71;
        } else {
            float _e72 = t2_;
            if (((r0_ + (_e72 * dr)) >= 0.0)) {
                float _e77 = t2_;
                t_2 = _e77;
            } else {
                return (0.0).xxxx;
            }
        }
    }
    float _e80 = t_2;
    const float4 _e81 = gradientAt(_e80);
    return _e81;
}

int naga_mod(int lhs, int rhs) {
    int divisor = ((lhs == int(-2147483647 - 1) & rhs == -1) | (rhs == 0)) ? 1 : rhs;
    return lhs - (lhs / divisor) * divisor;
}

float4 shaderTexel(int2 p, int2 size, int level)
{
    int x = (int)0;
    int y = (int)0;
    bool local_2 = (bool)0;
    bool local_3 = (bool)0;

    x = p.x;
    y = p.y;
    float _e10 = paint.shader.y;
    int tileX = naga_f2i32((_e10 + 0.5));
    float _e17 = paint.shader.z;
    int tileY = naga_f2i32((_e17 + 0.5));
    if ((tileX == int(1))) {
        int _e23 = x;
        x = naga_mod(asint(asuint(naga_mod(_e23, size.x)) + asuint(size.x)), size.x);
    } else {
        if ((tileX == int(2))) {
            int _e32 = x;
            int m_1 = naga_mod(asint(asuint(naga_mod(_e32, asint(asuint(int(2)) * asuint(size.x)))) + asuint(asint(asuint(int(2)) * asuint(size.x)))), asint(asuint(int(2)) * asuint(size.x)));
            if ((m_1 >= size.x)) {
                x = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.x))) - asuint(int(1)))) - asuint(m_1));
            } else {
                x = m_1;
            }
        } else {
            if ((tileX == int(3))) {
                int _e55 = x;
                if (!((_e55 < int(0)))) {
                    int _e61 = x;
                    local_2 = (_e61 >= size.x);
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return (0.0).xxxx;
                }
            } else {
                int _e68 = x;
                x = clamp(_e68, int(0), asint(asuint(size.x) - asuint(int(1))));
            }
        }
    }
    if ((tileY == int(1))) {
        int _e76 = y;
        y = naga_mod(asint(asuint(naga_mod(_e76, size.y)) + asuint(size.y)), size.y);
    } else {
        if ((tileY == int(2))) {
            int _e85 = y;
            int m_2 = naga_mod(asint(asuint(naga_mod(_e85, asint(asuint(int(2)) * asuint(size.y)))) + asuint(asint(asuint(int(2)) * asuint(size.y)))), asint(asuint(int(2)) * asuint(size.y)));
            if ((m_2 >= size.y)) {
                y = asint(asuint(asint(asuint(asint(asuint(int(2)) * asuint(size.y))) - asuint(int(1)))) - asuint(m_2));
            } else {
                y = m_2;
            }
        } else {
            if ((tileY == int(3))) {
                int _e108 = y;
                if (!((_e108 < int(0)))) {
                    int _e114 = y;
                    local_3 = (_e114 >= size.y);
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return (0.0).xxxx;
                }
            } else {
                int _e121 = y;
                y = clamp(_e121, int(0), asint(asuint(size.y) - asuint(int(1))));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    float4 _e131 = paintImage.Load(int3(int2(_e128, _e129), level));
    return _e131;
}

float mitchellWeight(float x0_)
{
    float x_1 = abs(x0_);
    if ((x_1 < 1.0)) {
        return ((((((((12.0 - (9.0 * 0.33333334)) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((-18.0 + (12.0 * 0.33333334)) + (6.0 * 0.33333334)) * x_1) * x_1)) + (6.0 - (2.0 * 0.33333334))) / 6.0);
    }
    if ((x_1 < 2.0)) {
        return ((((((((-(0.33333334) - (6.0 * 0.33333334)) * x_1) * x_1) * x_1) + ((((6.0 * 0.33333334) + (30.0 * 0.33333334)) * x_1) * x_1)) + (((-12.0 * 0.33333334) - (48.0 * 0.33333334)) * x_1)) + ((8.0 * 0.33333334) + (24.0 * 0.33333334))) / 6.0);
    }
    return 0.0;
}

int2 naga_f2i32(float2 value) {
    return int2(clamp(value, -2147483600.0, 2147483500.0));
}

uint2 NagaMipDimensions2D(Texture2D<float4> tex, uint mip_level)
{
    uint4 ret;
    tex.GetDimensions(mip_level, ret.x, ret.y, ret.z);
    return ret.xy;
}

float4 imageShaderAt(float2 q_1)
{
    float4 sum = (0.0).xxxx;
    int j = int(-1);
    int i_2 = (int)0;

    int2 base_1 = int2(NagaMipDimensions2D(paintImage, int(0)));
    float _e8 = paint.imageInfo.x;
    int quality = naga_f2i32((_e8 + 0.5));
    float _e15 = paint.imageInfo.y;
    int level_1 = naga_f2i32((_e15 + 0.5));
    int2 size_1 = int2(NagaMipDimensions2D(paintImage, level_1));
    float2 uv = ((q_1 * float2(size_1)) / float2(base_1));
    if ((quality == int(0))) {
        const float4 _e30 = shaderTexel(naga_f2i32(floor(uv)), size_1, level_1);
        return _e30;
    }
    float2 f = (uv - (0.5).xx);
    float2 f0_ = floor(f);
    float2 t_3 = (f - f0_);
    int2 i0_ = naga_f2i32(f0_);
    if ((quality == int(3))) {
        uint2 loop_bound_1 = uint2(4294967295u, 4294967295u);
        bool loop_init = true;
        while(true) {
            if (all(loop_bound_1 == uint2(0u, 0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = asint(asuint(_e75) + asuint(int(1)));
            }
            loop_init = false;
            int _e44 = j;
            if ((_e44 <= int(2))) {
            } else {
                break;
            }
            {
                int _e47 = j;
                const float _e51 = mitchellWeight((float(_e47) - t_3.y));
                i_2 = int(-1);
                uint2 loop_bound_2 = uint2(4294967295u, 4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (all(loop_bound_2 == uint2(0u, 0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = asint(asuint(_e72) + asuint(int(1)));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if ((_e54 <= int(2))) {
                    } else {
                        break;
                    }
                    {
                        float4 _e57 = sum;
                        int _e58 = i_2;
                        const float _e62 = mitchellWeight((float(_e58) - t_3.x));
                        int _e64 = i_2;
                        int _e65 = j;
                        const float4 _e68 = shaderTexel(asint(asuint(i0_) + asuint(int2(_e64, _e65))), size_1, level_1);
                        sum = (_e57 + ((_e51 * _e62) * _e68));
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = clamp(_e78, 0.0, 1.0);
        float4 _e82 = sum;
        return float4(clamp(_e82.xyz, (0.0).xxx, (a_3).xxx), a_3);
    }
    const float4 _e89 = shaderTexel(i0_, size_1, level_1);
    const float4 _e94 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(0)))), size_1, level_1);
    const float4 _e99 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(0), int(1)))), size_1, level_1);
    const float4 _e104 = shaderTexel(asint(asuint(i0_) + asuint(int2(int(1), int(1)))), size_1, level_1);
    return lerp(lerp(_e89, _e94, t_3.x), lerp(_e99, _e104, t_3.x), t_3.y);
}

float4 shaderColor(float2 p_1)
{
    float a_1 = (float)0;

    float _e4 = paint.xf0_.x;
    float _e10 = paint.xf0_.z;
    float _e17 = paint.xf1_.x;
    float _e22 = paint.xf0_.y;
    float _e28 = paint.xf0_.w;
    float _e35 = paint.xf1_.y;
    float2 q_2 = float2((((_e4 * p_1.x) + (_e10 * p_1.y)) + _e17), (((_e22 * p_1.x) + (_e28 * p_1.y)) + _e35));
    float _e41 = paint.shader.x;
    int kind = naga_f2i32((_e41 + 0.5));
    if ((kind == SHADER_LINEAR)) {
        float4 _e49 = paint.p0_;
        float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        const float4 _e62 = gradientAt((dot((q_2 - _e49.xy), _e54.zw) * _e60));
        return _e62;
    }
    if ((kind == SHADER_RADIAL)) {
        float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        const float4 _e76 = gradientAt((length((q_2 - _e67.xy)) / _e74));
        return _e76;
    }
    if ((kind == SHADER_CONICAL)) {
        const float4 _e79 = conicalAt(q_2);
        return _e79;
    }
    if ((kind == SHADER_SWEEP)) {
        float4 _e84 = paint.p0_;
        float2 d_5 = (q_2 - _e84.xy);
        float _e93 = paint.p0_.z;
        a_1 = (atan2(d_5.y, d_5.x) - _e93);
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = (_e96 - (6.2831855 * floor((_e98 / 6.2831855))));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        const float4 _e110 = gradientAt((_e104 / _e108));
        return _e110;
    }
    if ((kind == SHADER_IMAGE)) {
        const float4 _e113 = imageShaderAt(q_2);
        return _e113;
    }
    float4 _e116 = paint.constColor;
    return _e116;
}

float4 unpremul(float4 c_7)
{
    if ((c_7.w <= 0.0)) {
        return (0.0).xxxx;
    }
    return float4(clamp((c_7.xyz / (c_7.w).xxx), (0.0).xxx, (1.0).xxx), c_7.w);
}

float4 premul(float4 c_8)
{
    return float4((c_8.xyz * c_8.w), c_8.w);
}

float3 linearToSrgb(float3 v_1)
{
    return ((v_1 <= (0.0031308).xxx) ? (12.92 * v_1) : ((1.055 * pow(v_1, (0.41666666).xxx)) - (0.055).xxx));
}

float3 srgbToLinear(float3 v_2)
{
    return ((v_2 <= (0.04045).xxx) ? (v_2 / (12.92).xxx) : pow(((v_2 + (0.055).xxx) / (1.055).xxx), (2.4).xxx));
}

float4 applyFilters(float4 px0_)
{
    float4 px = (float4)0;

    px = px0_;
    float _e5 = paint.flags.y;
    int kind_1 = naga_f2i32((_e5 + 0.5));
    if ((kind_1 == int(1))) {
        float _e14 = paint.flags.z;
        float4 _e20 = paint.filterColor;
        float4 _e21 = px;
        const float4 _e22 = blendPair(naga_f2i32((_e14 + 0.5)), _e20, _e21);
        px = _e22;
    } else {
        if ((kind_1 == int(2))) {
            float4 _e25 = px;
            const float4 _e26 = unpremul(_e25);
            float4 c_12 = (_e26 * 255.0);
            float4 _e32 = paint.filterMatrix[0];
            float4 _e38 = paint.filterMatrix[1];
            float4 _e45 = paint.filterMatrix[2];
            float4 _e52 = paint.filterMatrix[3];
            float4 _e59 = paint.filterMatrix[4];
            float4 o_1 = (((((_e32 * c_12.x) + (_e38 * c_12.y)) + (_e45 * c_12.z)) + (_e52 * c_12.w)) + _e59);
            const float4 _e69 = premul(clamp((o_1 / (255.0).xxxx), (0.0).xxxx, (1.0).xxxx));
            px = _e69;
        } else {
            if ((kind_1 == int(3))) {
                float4 _e72 = px;
                const float4 _e73 = unpremul(_e72);
                const float3 _e75 = linearToSrgb(_e73.xyz);
                const float4 _e78 = premul(float4(_e75, _e73.w));
                px = _e78;
            } else {
                if ((kind_1 == int(4))) {
                    float4 _e81 = px;
                    const float4 _e82 = unpremul(_e81);
                    const float3 _e84 = srgbToLinear(_e82.xyz);
                    const float4 _e87 = premul(float4(_e84, _e82.w));
                    px = _e87;
                }
            }
        }
    }
    float _e91 = paint.flags.w;
    if ((_e91 != 0.0)) {
        float4 _e94 = px;
        const float4 _e95 = unpremul(_e94);
        const float4 _e102 = premul(float4(((1.0).xxx - _e95.xyz), _e95.w));
        px = _e102;
    }
    float4 _e103 = px;
    return _e103;
}

float4 paintSource(float4 base, float2 p_2)
{
    float4 c_9 = (float4)0;

    c_9 = base;
    float _e6 = paint.shader.x;
    if ((naga_f2i32((_e6 + 0.5)) != SHADER_NONE)) {
        const float4 _e12 = shaderColor(p_2);
        c_9 = (_e12 * base);
    }
    float4 _e14 = c_9;
    const float4 _e15 = applyFilters(_e14);
    return _e15;
}

float ditherAt(float2 dev)
{
    int2 p_4 = (naga_f2i32(floor(dev)) & (int(7)).xx);
    float _e13 = bayer[min(uint(asint(asuint(asint(asuint(p_4.y) * asuint(int(8)))) + asuint(p_4.x))), 63u)];
    return ((((_e13 + 0.5) / 64.0) - 0.5) / 255.0);
}

float4 destinationAt(float2 dev_1)
{
    float4 _e6 = paint.blend;
    float4 _e11 = dst_.Load(int3(asint(asuint(naga_f2i32(floor(dev_1))) - asuint(naga_f2i32(_e6.yz))), int(0)));
    return _e11;
}

float4 finish(float4 src, float cov, float2 dev_2)
{
    float4 o = (float4)0;
    bool local_4 = (bool)0;

    float _e7 = paint.blend.x;
    if ((_e7 < 0.0)) {
        o = (src * cov);
    } else {
        const float4 _e11 = destinationAt(dev_2);
        float _e15 = paint.blend.x;
        const float4 _e19 = blendPair(naga_f2i32((_e15 + 0.5)), src, _e11);
        o = (_e11 + ((_e19 - _e11) * cov));
    }
    float _e26 = paint.flags.x;
    if ((_e26 != 0.0)) {
        local_4 = (src.w > 0.0);
    } else {
        local_4 = false;
    }
    bool _e35 = local_4;
    if (_e35) {
        float4 _e36 = o;
        const float _e37 = ditherAt(dev_2);
        o = (_e36 + (_e37).xxxx);
    }
    float4 _e40 = o;
    return _e40;
}

float4 toClip(float2 dev_3)
{
    float2 _e3 = frame.origin;
    float2 p_5 = (dev_3 - _e3);
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return float4((((p_5.x / _e9) * 2.0) - 1.0), (1.0 - ((p_5.y / _e19) * 2.0)), 0.0, 1.0);
}

float span(float c_10, float lo, float hi)
{
    return clamp((min((c_10 + 0.5), hi) - max((c_10 - 0.5), lo)), 0.0, 1.0);
}

float clipMaskCoverage(float2 dev_4)
{
    bool local_5 = (bool)0;
    bool local_6 = (bool)0;
    bool local_7 = (bool)0;

    float _e4 = draw.clipMask.z;
    if ((_e4 <= 0.0)) {
        return 1.0;
    }
    float4 _e12 = draw.clipMask;
    int2 p_6 = asint(asuint(naga_f2i32(floor(dev_4))) - asuint(naga_f2i32(_e12.xy)));
    if (!((p_6.x < int(0)))) {
        local_5 = (p_6.y < int(0));
    } else {
        local_5 = true;
    }
    bool _e26 = local_5;
    if (!(_e26)) {
        float _e34 = draw.clipMask.z;
        local_6 = (p_6.x >= naga_f2i32(_e34));
    } else {
        local_6 = true;
    }
    bool _e38 = local_6;
    if (!(_e38)) {
        float _e46 = draw.clipMask.w;
        local_7 = (p_6.y >= naga_f2i32(_e46));
    } else {
        local_7 = true;
    }
    bool _e50 = local_7;
    if (_e50) {
        return 0.0;
    }
    float4 _e54 = clipMask.Load(int3(p_6, int(0)));
    return _e54.x;
}

VertexOutput_vs_main vs_main(VertexInput v)
{
    VertexOutput out_ = (VertexOutput)0;

    const float4 _e4 = toClip(v.pos);
    out_.position = _e4;
    out_.dev = v.pos;
    out_.color = v.color;
    out_.tex = v.tex;
    VertexOutput _e11 = out_;
    VertexOutput vertexoutput = _e11;
    VertexOutput_vs_main vertexoutput_1 = { vertexoutput.dev, vertexoutput.color, vertexoutput.tex, vertexoutput.position };
    return vertexoutput_1;
}

float4 fs_main(FragmentInput_fs_main fragmentinput_fs_main) : SV_Target0
{
    VertexOutput in_ = { fragmentinput_fs_main.position_1, fragmentinput_fs_main.dev_6, fragmentinput_fs_main.color_2, fragmentinput_fs_main.tex_1 };
    float4 color = (float4)0;
    float2 at = (float2)0;

    float2 dev_7 = (floor(in_.dev) + (0.5).xx);
    float4 _e8 = draw.paint;
    color = _e8;
    float _e13 = paint.shader.x;
    if ((naga_f2i32((_e13 + 0.5)) != SHADER_NONE)) {
        at = dev_7;
        float _e23 = draw.mode.z;
        if ((_e23 != 0.0)) {
            at = in_.tex;
        }
        float2 _e27 = at;
        const float4 _e28 = shaderColor(_e27);
        float4 _e31 = draw.paint;
        color = (_e28 * _e31);
    }
    float _e36 = draw.mode.y;
    if ((_e36 != 0.0)) {
        float _e42 = draw.mode.x;
        float4 _e46 = color;
        const float4 _e55 = blendPair(naga_f2i32((_e42 + 0.5)), _e46, float4((in_.color.xyz * in_.color.w), in_.color.w));
        color = _e55;
    }
    float4 _e56 = color;
    const float4 _e57 = applyFilters(_e56);
    color = _e57;
    float _e62 = draw.clip_.x;
    float _e66 = draw.clip_.z;
    const float _e67 = span(dev_7.x, _e62, _e66);
    float _e72 = draw.clip_.y;
    float _e76 = draw.clip_.w;
    const float _e77 = span(dev_7.y, _e72, _e76);
    const float _e79 = clipMaskCoverage(dev_7);
    float coverage = ((_e67 * _e77) * _e79);
    float4 _e81 = color;
    const float4 _e82 = finish(_e81, coverage, dev_7);
    return _e82;
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}},
		}},
	},
}

ShaderVertices is shaders/vertices.wgsl.

Flutter: no counterpart

Functions

func IsDevicePending

func IsDevicePending(err error) bool

IsDevicePending reports whether err is the error of a device that is on its way.

Flutter: no counterpart

func IsSwapchainOutOfDate

func IsSwapchainOutOfDate(err error) bool

IsSwapchainOutOfDate reports whether err is the error of an out-of-date swapchain.

Flutter: no counterpart

func RotateRects

func RotateRects(rects []image.Rectangle, width, height int, rotation Rotation) []image.Rectangle

RotateRects turns rectangles of a width x height window, from its top left, into the rectangles of the image that is drawn with the pre-rotation: the image has the frame rotated clockwise by it (SPEC §24.4). nil stays nil (the whole window).

Flutter: no counterpart

Types

type AddressMode

type AddressMode int

AddressMode is what a sampler does outside the texture. The zero value is AddressUnspecified, which means AddressClamp.

Flutter: no counterpart

const (
	// AddressUnspecified is the zero value and means AddressClamp.
	//
	// Flutter: no counterpart
	AddressUnspecified AddressMode = iota
	// AddressClamp repeats the edge texel.
	//
	// Flutter: no counterpart
	AddressClamp
	// AddressRepeat tiles the texture.
	//
	// Flutter: no counterpart
	AddressRepeat
	// AddressMirror tiles the texture, flipping every other tile.
	//
	// Flutter: no counterpart
	AddressMirror
)

The address modes.

type BindGroup

type BindGroup interface {
	// Destroy releases the bind group.
	Destroy()
}

BindGroup is a set of resources for one bind group of a pipeline.

Flutter: no counterpart

type Binding

type Binding struct {
	// Binding is the number of the shader's @binding.
	Binding int
	// Buffer, with Offset and Size, is a uniform buffer; Size 0 means up to the end.
	Buffer Buffer
	// Offset is where the binding starts in Buffer, in bytes.
	Offset int
	// Size is the size of the binding in bytes.
	Size int
	// Texture is a sampled texture.
	Texture Texture
	// Sampler is a sampler.
	Sampler Sampler
}

Binding is one resource of a bind group: the entry for @binding(Binding).

Flutter: no counterpart

type BindingKind

type BindingKind int

BindingKind is what a shader binding is.

Flutter: no counterpart

const (
	// BindingUnspecified is the zero value: no binding.
	//
	// Flutter: no counterpart
	BindingUnspecified BindingKind = iota
	// BindingUniformBuffer is a `var<uniform>`. It is always bound with a dynamic
	// offset: the uniforms of a frame go into a ring buffer (SPEC §9.3).
	//
	// Flutter: no counterpart
	BindingUniformBuffer
	// BindingSampledTexture is a `texture_2d<f32>`.
	//
	// Flutter: no counterpart
	BindingSampledTexture
	// BindingSampler is a `sampler`.
	//
	// Flutter: no counterpart
	BindingSampler
)

The kinds of binding the design limits allow (SPEC §9.3).

func (BindingKind) String

func (k BindingKind) String() string

String returns "uniform", "texture" or "sampler".

type BlendFactor

type BlendFactor int

BlendFactor is a factor of the blend equation. The zero value is BlendFactorUnspecified, which means BlendOne.

Flutter: no counterpart

const (
	// BlendFactorUnspecified is the zero value and means BlendOne.
	//
	// Flutter: no counterpart
	BlendFactorUnspecified BlendFactor = iota
	// BlendZero is 0.
	//
	// Flutter: no counterpart
	BlendZero
	// BlendOne is 1.
	//
	// Flutter: no counterpart
	BlendOne
	// BlendSrcAlpha is the alpha of the source.
	//
	// Flutter: no counterpart
	BlendSrcAlpha
	// BlendOneMinusSrcAlpha is 1 minus the alpha of the source.
	//
	// Flutter: no counterpart
	BlendOneMinusSrcAlpha
	// BlendDstAlpha is the alpha of the destination.
	//
	// Flutter: no counterpart
	BlendDstAlpha
	// BlendOneMinusDstAlpha is 1 minus the alpha of the destination.
	//
	// Flutter: no counterpart
	BlendOneMinusDstAlpha
)

The blend factors.

type BlendState

type BlendState struct {
	// Enabled turns blending on; off, the source replaces the destination.
	Enabled bool
	// SrcFactor and DstFactor are the factors for the color and the alpha.
	SrcFactor, DstFactor BlendFactor
}

BlendState is the blend equation of a pipeline: source * SrcFactor + destination * DstFactor, for color and alpha. The zero value is no blending: the source replaces the destination.

Flutter: no counterpart

type Buffer

type Buffer interface {
	// Size is the size in bytes.
	Size() int
	// Destroy releases the buffer. The GPU finishes the frames that use it first.
	Destroy()
}

Buffer is a GPU buffer.

Flutter: no counterpart

type BufferUsage

type BufferUsage int

BufferUsage says what a buffer is used for; values are combined with |.

Flutter: no counterpart

const (
	// BufferVertex is a buffer of vertex or instance data.
	//
	// Flutter: no counterpart
	BufferVertex BufferUsage = 1 << iota
	// BufferIndex is a buffer of 16 bit indices.
	//
	// Flutter: no counterpart
	BufferIndex
	// BufferUniform is a buffer that uniform bindings read, with dynamic offsets.
	//
	// Flutter: no counterpart
	BufferUniform
)

The uses of a buffer. Every buffer can be written with Device.WriteBuffer.

type Device

type Device interface {
	// Limits are the design limits.
	Limits() Limits
	// NewBuffer makes a buffer of size bytes.
	NewBuffer(size int, usage BufferUsage) (Buffer, error)
	// NewTexture makes a 2D texture.
	NewTexture(desc TextureDesc) (Texture, error)
	// NewSampler makes a sampler.
	NewSampler(desc SamplerDesc) (Sampler, error)
	// NewPipeline makes a pipeline from a generated shader.
	NewPipeline(desc PipelineDesc) (Pipeline, error)
	// NewBindGroup makes the resources of the bind group number group of the
	// pipeline.
	NewBindGroup(p Pipeline, group int, entries []Binding) (BindGroup, error)
	// WriteBuffer copies data into the buffer at offset. The data is copied before
	// the call returns.
	WriteBuffer(b Buffer, offset int, data []byte)
	// WriteTexture copies the rows of data, stride bytes apart, into the rectangle r
	// of the level of the texture.
	WriteTexture(t Texture, level int, r image.Rectangle, data []byte, stride int)
	// NewSwapchain makes the swapchain of a window.
	NewSwapchain(h gpu.SurfaceHandle) (Swapchain, error)
	// Begin starts the commands of a frame; there is one encoder per frame.
	Begin() (Encoder, error)
	// Close releases the device and everything made with it.
	Close() error
}

Device is a GPU, as the explicit renderer sees it. Vulkan and WebGPU implement it within the design limits (SPEC §9.3). Every method runs on the UI thread. The methods that make resources fail with GPUOutOfMemory or GPUDeviceLost; a call that breaks a design limit panics with GPULimitExceeded in debug builds.

Flutter: no counterpart

type Encoder

type Encoder interface {
	// BeginPass starts a pass that draws into the target, which must have
	// TextureRenderTarget. The previous pass must have ended.
	BeginPass(target Texture, load LoadOp, clear ui.Color) Pass
	// CopyTexture copies the rectangle r of src to dst at the point at.
	CopyTexture(src Texture, r image.Rectangle, dst Texture, at image.Point)
	// ReadTexture reads the rectangle r of src. done runs once the GPU has finished,
	// never inside the frame that asked.
	ReadTexture(src Texture, r image.Rectangle, done func(*image.RGBA, error))
	// Submit sends the frame to the GPU.
	Submit() error
}

Encoder records the commands of one frame.

Flutter: no counterpart

type EntryPoint

type EntryPoint struct {
	// Name is the name of the function.
	Name string
	// Stage is the stage the function runs in.
	Stage ShaderStage
	// Bindings are the resources the function reads, directly or through the
	// functions it calls, sorted by group and binding.
	Bindings []ShaderBinding
}

EntryPoint is a function of a shader that a pipeline stage runs.

Flutter: no counterpart

type FilterMode

type FilterMode int

FilterMode is how a sampler reads between texels. The zero value is FilterUnspecified, which means FilterNearest.

Flutter: no counterpart

const (
	// FilterUnspecified is the zero value and means FilterNearest.
	//
	// Flutter: no counterpart
	FilterUnspecified FilterMode = iota
	// FilterNearest takes the nearest texel.
	//
	// Flutter: no counterpart
	FilterNearest
	// FilterLinear blends the two nearest texels in each direction.
	//
	// Flutter: no counterpart
	FilterLinear
)

The filter modes.

type FrameStats

type FrameStats struct {
	// Commands is the number of drawing commands the frame walked. The commands inside a layer
	// whose pixels the raster cache has are not walked (gpu.FlattenOpts.Retained), and the lines of a
	// paragraph that lie outside the clip are not expanded into commands, so neither is counted.
	Commands int
	// Drawn is the number of commands drawn.
	Drawn int
	// Culled is the number of commands that were left out, counted in Drawn, because they lie
	// outside the part of the surface their clip reaches (SPEC §9.3, "Culling").
	Culled int
	// Skipped is the number of commands left out because the renderer cannot draw them
	// yet; only a renderer with SkipUnsupported skips.
	Skipped int
	// Instances is the number of instanced primitives sent to the GPU (shapes, coverage
	// tiles and image rectangles), Vertices the number of vertices, and Batches the number
	// of draw calls that drew them.
	Instances, Vertices, Batches int
	// TilesRasterized, ClipMasksBuilt and ImagesUploaded count the work the frame did that an
	// earlier frame's cache could have saved: coverage tiles rasterized, clip masks made and
	// images uploaded.
	TilesRasterized, ClipMasksBuilt, ImagesUploaded int
	// AtlasRequests is the number of times the frame asked the coverage atlas for the tiles of a
	// shape; a shape that culling leaves out asks for none.
	AtlasRequests int
	// CachedLayersMade is the number of layers drawn into a texture that the raster cache keeps,
	// and CachedLayersDrawn the number drawn from such a texture instead of from their contents
	// (SPEC §9.1 "Raster cache").
	CachedLayersMade, CachedLayersDrawn int
	// DamagedPixels is the number of pixels the frame told the compositor to update, and
	// DamageRects the number of rectangles they were in: the whole window and 0 when the frame
	// was presented whole (SPEC §9.1 "Damage").
	DamagedPixels, DamageRects int
	// Unsupported lists the features that caused skipped commands, once each.
	Unsupported []string
}

FrameStats says what the last frame did.

Flutter: no counterpart

type Limits

type Limits struct {
	// MaxBindGroups is the number of bind groups a pipeline can use.
	MaxBindGroups int
	// MaxSampledTexturesPerStage, MaxSamplersPerStage and MaxUniformBuffersPerStage
	// are how many resources of each kind one shader stage can read.
	MaxSampledTexturesPerStage, MaxSamplersPerStage, MaxUniformBuffersPerStage int
	// UniformOffsetAlignment is what dynamic uniform offsets are a multiple of.
	UniformOffsetAlignment int
	// MaxUniformBindingSize is the largest uniform binding, in bytes.
	MaxUniformBindingSize int
	// MaxTextureSize is the largest width or height of a texture, in pixels.
	MaxTextureSize int
	// MaxVertexBuffers and MaxVertexAttributes are what a pipeline's vertex layout
	// can have.
	MaxVertexBuffers, MaxVertexAttributes int
	// CopyRowPitchAlignment is what the row pitch of a copy between a buffer and a
	// texture is a multiple of, in bytes.
	CopyRowPitchAlignment int
}

Limits are what a Device guarantees and what the renderer may use. They are WebGPU's default limits, so neither Vulkan nor WebGPU requests more (SPEC §9.3).

Flutter: no counterpart

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the design limits of SPEC §9.3.

Flutter: no counterpart

func (Limits) AlignedRowPitch

func (l Limits) AlignedRowPitch(width, bytesPerPixel int) int

AlignedRowPitch returns the row pitch, in bytes, of a copy between a buffer and a texture of the width and the bytes per pixel: the row rounded up to a multiple of CopyRowPitchAlignment.

Flutter: no counterpart

func (Limits) CheckBindGroup

func (l Limits) CheckBindGroup(group int, entries []Binding)

CheckBindGroup panics with GPULimitExceeded in debug builds when the group index is not below MaxBindGroups, or a uniform binding is larger than MaxUniformBindingSize. Backends and the fake device call it from NewBindGroup.

Flutter: no counterpart

func (Limits) CheckPipeline

func (l Limits) CheckPipeline(desc PipelineDesc)

CheckPipeline panics with GPULimitExceeded in debug builds when the pipeline has more vertex buffers or attributes, or its entry points read more resources of a kind or from more bind groups, than the limits allow. Backends and the fake device call it from NewPipeline.

Flutter: no counterpart

func (Limits) CheckSetBindGroup

func (l Limits) CheckSetBindGroup(group int, dynamicOffsets []uint32)

CheckSetBindGroup panics with GPULimitExceeded in debug builds when the group index is not below MaxBindGroups or a dynamic offset is not a multiple of UniformOffsetAlignment. Backends and the fake device call it from SetBindGroup.

Flutter: no counterpart

func (Limits) CheckSetVertexBuffer

func (l Limits) CheckSetVertexBuffer(slot int)

CheckSetVertexBuffer panics with GPULimitExceeded in debug builds when the slot is not below MaxVertexBuffers. Backends and the fake device call it from SetVertexBuffer.

Flutter: no counterpart

func (Limits) CheckTexture

func (l Limits) CheckTexture(desc TextureDesc)

CheckTexture panics with GPULimitExceeded in debug builds when the texture is larger than MaxTextureSize. Backends and the fake device call it from NewTexture.

Flutter: no counterpart

type LoadOp

type LoadOp int

LoadOp is what a pass does with the target's contents before it draws. The zero value is LoadClear.

Flutter: no counterpart

const (
	// LoadClear fills the target with the clear color.
	//
	// Flutter: no counterpart
	LoadClear LoadOp = iota
	// LoadLoad keeps the target's contents.
	//
	// Flutter: no counterpart
	LoadLoad
	// LoadDontCare leaves the target's contents undefined, for a pass that draws every pixel of
	// it: a tile-based GPU (Adreno, Mali, PowerVR) then neither clears the tile nor reads it
	// from memory (SPEC §24.4). A backend without the operation clears.
	//
	// Flutter: no counterpart
	LoadDontCare
)

The load operations.

type Pass

type Pass interface {
	// SetPipeline chooses the pipeline of the next draws.
	SetPipeline(Pipeline)
	// SetBindGroup binds the group number group, with one dynamic offset for each
	// uniform buffer in it, in binding order, each a multiple of
	// Limits.UniformOffsetAlignment.
	SetBindGroup(group int, g BindGroup, dynamicOffsets ...uint32)
	// SetVertexBuffer binds a vertex buffer to a slot, starting at offset bytes.
	SetVertexBuffer(slot int, b Buffer, offset int)
	// SetIndexBuffer binds the index buffer, starting at offset bytes.
	SetIndexBuffer(b Buffer, offset int)
	// SetScissor limits the draws to the rectangle, in pixels of the target.
	SetScissor(r image.Rectangle)
	// Draw draws vertices, instances times.
	Draw(vertices, instances, firstVertex, firstInstance int)
	// DrawIndexed draws indices, instances times.
	DrawIndexed(indices, instances, firstIndex, baseVertex, firstInstance int)
	// End finishes the pass.
	End()
}

Pass records the draws of one render pass.

Flutter: no counterpart

type Pipeline

type Pipeline interface {
	// Desc is what the pipeline was made with.
	Desc() PipelineDesc
	// Destroy releases the pipeline.
	Destroy()
}

Pipeline is a GPU render pipeline.

Flutter: no counterpart

type PipelineDesc

type PipelineDesc struct {
	// Shader is a generated shader.
	Shader *Shader
	// VertexEntry and FragmentEntry are the names of its entry points.
	VertexEntry, FragmentEntry string
	// VertexBuffers are the layouts of the vertex buffers, by slot.
	VertexBuffers []VertexBufferLayout
	// Blend is the blend state.
	Blend BlendState
	// Target is the format of the texture the pipeline draws into.
	Target TextureFormat
}

PipelineDesc describes a render pipeline: a generated shader, its entry points, the vertex layout, the blend state and the format of the target. Triangles are the only topology the renderer uses.

Flutter: no counterpart

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer is the renderer for the explicit APIs (SPEC §9.3): the engine's renderer on Vulkan and WebGPU. Each window has a swapchain and an offscreen frame texture; a frame is drawn into the frame texture, then blitted into the swapchain image and presented. It draws everything the GLES renderer draws (SPEC §9.1): shapes, paths, clips, images, text, gradients, color filters, blend modes, layers, blurs, shadows and image filters, with the raster cache and damage tracking, and snapshots without a window (step 20.6).

The device is opened at the first frame, with the handle of the first window, because the choice of a Vulkan device depends on the window.

Flutter: no counterpart (flutter::Rasterizer)

func NewRenderer

func NewRenderer(open func(gpu.SurfaceHandle) (Device, error), opts ...RendererOpts) *Renderer

NewRenderer returns a renderer that has no device yet: open makes it at the first frame, from the handle of the first window.

Flutter: no counterpart

func (*Renderer) Close

func (r *Renderer) Close()

Close destroys the windows' state and the device. It can be called twice.

Flutter: no counterpart

func (*Renderer) Describe

func (r *Renderer) Describe() string

Describe names the renderer and its device, as the device does, or "" before the first frame.

Flutter: no counterpart

func (*Renderer) Draw

func (r *Renderer) Draw(h gpu.SurfaceHandle, width, height int, scene *ui.Scene, capture bool) (*image.RGBA, error)

Draw draws one frame into the window of the handle, whose size is width x height pixels, and presents it; with capture it also reads the frame back, top row first. The first call opens the device and the swapchain (their errors are returned unchanged, such as VulkanLoadFailed or VulkanNoDevice). A swapchain that is out of date is configured again and the frame retried once.

Flutter: no counterpart

func (*Renderer) Invalidate

func (r *Renderer) Invalidate(h gpu.SurfaceHandle)

Invalidate says that the window lost its content, so that its next frame is presented whole; a handle that was never drawn into is ignored.

Flutter: no counterpart

func (*Renderer) LastFrame

func (r *Renderer) LastFrame() FrameStats

LastFrame says what the last call of Draw drew and left out.

Flutter: no counterpart

func (*Renderer) PipelineLibrary

func (r *Renderer) PipelineLibrary() (present, known bool)

PipelineLibrary says whether the device keeps a library of serialized pipelines (Direct3D 12's ID3D12PipelineLibrary, which vkd3d does not have, SPEC §9.8), when the device has the concept: its method HasPipelineLibrary. known is false for a device that does not.

Flutter: no counterpart

func (*Renderer) RasterizeScene

func (r *Renderer) RasterizeScene(scene *ui.Scene, width, height int) (*ui.Image, error)

RasterizeScene draws the scene into a new image of width x height pixels, as Snapshot does: the ui.Rasterizer of the GPU renderer, for Picture.ToImage and Scene.ToImage.

Flutter: no counterpart

func (*Renderer) Release

func (r *Renderer) Release(h gpu.SurfaceHandle)

Release destroys what the renderer keeps for the window of the handle, which must still exist; a handle that was never drawn into is ignored.

Flutter: no counterpart

func (*Renderer) Snapshot

func (r *Renderer) Snapshot(scene *ui.Scene, size ui.Size) (*image.RGBA, error)

Snapshot draws the scene into a new image of the size, without a window: the work of Picture.ToImage and Scene.ToImage. The scene is drawn over transparent pixels into a pooled texture, which is read back; the pixels are premultiplied, top row first. The device must exist, so a window has to have been drawn into at some point; the raster cache is neither used nor fed. It fails with RendererClosed, RendererNotAttached, RendererSizeInvalid, GPUNotImplemented as Draw does, and the errors of the device.

Flutter: no counterpart

func (*Renderer) StartupReport

func (r *Renderer) StartupReport() gpu.StartupReport

StartupReport says how the start went, when the device can tell: how long it took to open and how its pipelines were made.

Flutter: no counterpart

type RendererOpts

type RendererOpts struct {
	// Background is the color each frame is cleared to when HasBackground is set;
	// otherwise opaque white.
	Background ui.Color
	// HasBackground says that Background is set.
	HasBackground bool
	// Damage tells the compositor which part of a window changed in each frame, when
	// the device can (SPEC §9.1 "Damage", §9.4).
	Damage bool
	// RasterCacheAfter turns on the raster cache: a layer that a scene retains (it is in this
	// many consecutive scenes) is drawn into a texture that later frames draw instead of its
	// contents (SPEC §9.1 "Raster cache"). Zero, the default, keeps nothing. The engine sets 3:
	// the layer of a RepaintBoundary that the framework did not repaint for three frames.
	RasterCacheAfter int
	// Now is the clock of the device-loss rule (a second loss within 10 seconds of the first
	// ends the app); time.Now when nil. Tests set it.
	Now func() time.Time
	// Log is where a lost device is reported, at Info level; nothing is logged when nil.
	Log *slog.Logger
	// OnCapture receives a capture whose read-back finishes after Draw returned, on a device
	// whose reads are asynchronous (WebGPU, SPEC §9.5): Draw then returns a nil image and a nil
	// error for the frame that asked, and OnCapture is called later, on the thread that
	// calls Draw, with the image or the error of the read. A device that finishes reads inside
	// Submit (Vulkan) returns the image from Draw as before. Without OnCapture a capture that is
	// late fails with RendererCaptureFailed.
	OnCapture func(img *image.RGBA, err error)
	// SkipUnsupported makes Draw leave out what the renderer cannot draw yet, and count
	// it in LastFrame, instead of failing with GPUNotImplemented. The development
	// examples use it to show what works.
	SkipUnsupported bool
}

RendererOpts are the optional parameters of NewRenderer.

Flutter: no counterpart

type Rotation

type Rotation int

Rotation is a rotation by quarter turns, clockwise: a surface's pre-transform (SPEC §24.4).

Flutter: no counterpart

const (
	Rotation0 Rotation = iota
	Rotation90
	Rotation180
	Rotation270
)

The rotations of a pre-transform.

type Sampler

type Sampler interface {
	// Destroy releases the sampler.
	Destroy()
}

Sampler is a GPU sampler.

Flutter: no counterpart

type SamplerDesc

type SamplerDesc struct {
	// MinFilter and MagFilter are used when the texture is minified and magnified.
	MinFilter, MagFilter FilterMode
	// MipmapFilter chooses between mip levels; FilterUnspecified uses level 0 only.
	MipmapFilter FilterMode
	// AddressU and AddressV are the modes of the two directions.
	AddressU, AddressV AddressMode
}

SamplerDesc describes a sampler.

Flutter: no counterpart

type Shader

type Shader struct {
	// Name is the name of the .wgsl file without its extension.
	Name string
	// WGSL is the source text.
	WGSL string
	// SPIRV is what naga made from WGSL: one module with every entry point.
	SPIRV []byte
	// HLSL is what naga made from WGSL for shader model 5.1, after the generator put every
	// binding at `register(<class>B, spaceG)` (ShaderBinding.Register): one file with every entry
	// point, under the names they have in the WGSL. Direct3D 12 compiles it with D3DCompile.
	HLSL string
	// Entries are the entry points of the module.
	Entries []EntryPoint
}

Shader is one WGSL module with its SPIR-V. glutter-gen shaders makes the values of this type in shaders_gen.go (D20): WebGPU takes WGSL, Vulkan takes SPIR-V.

Flutter: no counterpart

func Shaders

func Shaders() []*Shader

Shaders returns every generated shader, in the order of their names: the modules a backend that checks them at startup (WebGPU) compiles.

Flutter: no counterpart

func (*Shader) Entry

func (s *Shader) Entry(name string) (e EntryPoint, ok bool)

Entry returns the entry point with the name; ok is false when there is none.

Flutter: no counterpart

type ShaderBinding

type ShaderBinding struct {
	// Name is the name of the variable in the WGSL.
	Name string
	// Group and Binding are the numbers of the declaration.
	Group, Binding int
	// Kind says what the resource is.
	Kind BindingKind
	// Register is where the HLSL binds the resource (Direct3D 12 only).
	Register ShaderRegister
}

ShaderBinding is one resource a shader reads, from its declaration in the WGSL: `@group(g) @binding(b) var ...`.

Flutter: no counterpart

type ShaderRegister

type ShaderRegister struct {
	// Class is the register class: "b" for a constant buffer (a uniform), "t" for a texture and "s"
	// for a sampler.
	Class string
	// Space is the register space, the group of the binding.
	Space int
	// Index is the register number, the binding number.
	Index int
}

ShaderRegister is where the HLSL of a shader binds a resource (D25, SPEC §9.8): for the binding B of the group G, `register(<Class>B, spaceG)`. The generator makes every register of shaders_gen.go like that and checks the HLSL against it.

Flutter: no counterpart

func (ShaderRegister) String

func (r ShaderRegister) String() string

String returns the register as HLSL writes it, such as "t3, space1".

type ShaderStage

type ShaderStage int

ShaderStage is the stage of a shader entry point.

Flutter: no counterpart

const (
	// ShaderStageUnspecified is the zero value: no stage.
	//
	// Flutter: no counterpart
	ShaderStageUnspecified ShaderStage = iota
	// ShaderStageVertex is an entry point marked @vertex.
	//
	// Flutter: no counterpart
	ShaderStageVertex
	// ShaderStageFragment is an entry point marked @fragment.
	//
	// Flutter: no counterpart
	ShaderStageFragment
)

The stages of the shaders of the explicit renderer. There is no compute in 1.1.

func (ShaderStage) String

func (s ShaderStage) String() string

String returns "vertex" or "fragment".

type Swapchain

type Swapchain interface {
	// Configure sets the size of the images, in device pixels of the window as it is seen: when
	// the swapchain has a pre-transform of a quarter turn, its images have the size turned.
	Configure(width, height int) error
	// PreRotation is the rotation that the window system expects of the images (Android's
	// currentTransform): the frame is drawn into the image rotated clockwise by it, and the
	// presentation engine then does not rotate it again. It is valid after Configure, and
	// Rotation0 on every window system but Android's.
	PreRotation() Rotation
	// Stale says that the window system turned the surface (Android's currentTransform changed,
	// which it reports once the surface is on screen, or when the device is turned without a
	// change of size): the image that was just presented was drawn for the old pre-transform, and the
	// renderer draws it again, whole and without building the scene again, into a swapchain that
	// Configure makes. It is cleared by Configure.
	Stale() bool
	// Acquire returns the image to draw into next. When it is out of date, Configure
	// again and retry once.
	Acquire() (Texture, error)
	// Present shows the image, telling the compositor which rectangles changed (nil
	// means all).
	Present(damage []image.Rectangle) error
	// Destroy releases the swapchain.
	Destroy()
}

Swapchain is the images of a window.

Flutter: no counterpart

type Texture

type Texture interface {
	// Desc is what the texture was made with, with MipLevels at least 1.
	Desc() TextureDesc
	// Level returns a texture that is the mip level i of this one: it has the size of that
	// level and one level, and a pass can draw into it and a bind group can sample it, which
	// is how the downsampling pass makes the mipmaps (SPEC §9.3). The texture it returns
	// belongs to this one and goes away with it. Level(0) of a texture with one level is the
	// texture itself.
	Level(i int) Texture
	// Destroy releases the texture. The GPU finishes the frames that use it first.
	Destroy()
}

Texture is a GPU 2D texture.

Flutter: no counterpart

type TextureDesc

type TextureDesc struct {
	// Width and Height are in pixels, at most Limits.MaxTextureSize.
	Width, Height int
	// Format is the pixel format.
	Format TextureFormat
	// MipLevels is the number of levels; 0 means 1.
	MipLevels int
	// Usage says how the texture is used.
	Usage TextureUsage
}

TextureDesc describes a 2D texture.

Flutter: no counterpart

type TextureFormat

type TextureFormat int

TextureFormat is the pixel format of a texture. There are no sRGB formats: blending is done in non-linear sRGB (SPEC §9.1, §9.3). The zero value is TextureFormatUnspecified.

Flutter: no counterpart

const (
	// TextureFormatUnspecified is the zero value: no format.
	//
	// Flutter: no counterpart
	TextureFormatUnspecified TextureFormat = iota
	// TextureFormatRGBA8Unorm is 4 bytes per pixel, red first.
	//
	// Flutter: no counterpart
	TextureFormatRGBA8Unorm
	// TextureFormatBGRA8Unorm is 4 bytes per pixel, blue first: the usual swapchain
	// format.
	//
	// Flutter: no counterpart
	TextureFormatBGRA8Unorm
	// TextureFormatR8Unorm is 1 byte per pixel: coverage masks and glyph atlases.
	//
	// Flutter: no counterpart
	TextureFormatR8Unorm
)

The formats of a texture.

func (TextureFormat) BytesPerPixel

func (f TextureFormat) BytesPerPixel() int

BytesPerPixel returns the size of a pixel in bytes; 0 for an unspecified format.

Flutter: no counterpart

type TextureUsage

type TextureUsage int

TextureUsage says what a texture is used for; values are combined with |. Every texture can be written with Device.WriteTexture.

Flutter: no counterpart

const (
	// TextureSampled is a texture that shaders sample.
	//
	// Flutter: no counterpart
	TextureSampled TextureUsage = 1 << iota
	// TextureRenderTarget is a texture that a pass draws into.
	//
	// Flutter: no counterpart
	TextureRenderTarget
	// TextureCopySource is a texture that CopyTexture or ReadTexture reads.
	//
	// Flutter: no counterpart
	TextureCopySource
)

The uses of a texture.

type VertexAttribute

type VertexAttribute struct {
	// Location is the @location of the shader's input.
	Location int
	// Format is the type of the data.
	Format VertexFormat
	// Offset is where the data starts in an element of the buffer, in bytes.
	Offset int
}

VertexAttribute is one input of the vertex shader, in a buffer.

Flutter: no counterpart

type VertexBufferLayout

type VertexBufferLayout struct {
	// Stride is the size of an element in bytes.
	Stride int
	// PerInstance makes the buffer advance once for each instance instead of each
	// vertex.
	PerInstance bool
	// Attributes are the inputs that come from this buffer.
	Attributes []VertexAttribute
}

VertexBufferLayout is how a vertex buffer is read.

Flutter: no counterpart

type VertexFormat

type VertexFormat int

VertexFormat is the type of a vertex attribute. The zero value is VertexFormatUnspecified.

Flutter: no counterpart

const (
	// VertexFormatUnspecified is the zero value: no format.
	//
	// Flutter: no counterpart
	VertexFormatUnspecified VertexFormat = iota
	// VertexFloat32 is one float32.
	//
	// Flutter: no counterpart
	VertexFloat32
	// VertexFloat32x2 is two float32.
	//
	// Flutter: no counterpart
	VertexFloat32x2
	// VertexFloat32x3 is three float32.
	//
	// Flutter: no counterpart
	VertexFloat32x3
	// VertexFloat32x4 is four float32.
	//
	// Flutter: no counterpart
	VertexFloat32x4
	// VertexUnorm8x4 is four bytes read as numbers from 0 to 1.
	//
	// Flutter: no counterpart
	VertexUnorm8x4
)

The vertex formats.

Directories

Path Synopsis
Package fakedevice is an explicit.Device that records every call, for the unit tests of the explicit renderer without a GPU.
Package fakedevice is an explicit.Device that records every call, for the unit tests of the explicit renderer without a GPU.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL