explicit

package
v1.17.3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: BSD-3-Clause Imports: 14 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 MetalVertexBufferBase = 16

MetalVertexBufferBase is the index of the first vertex buffer in Metal's buffer argument table: the vertex buffer of slot s is bound at MetalVertexBufferBase+s, above the indexes of the buffers that a shader binds (a shader has at most MetalVertexBufferBase of them; SPEC §29.5).

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 TagGPUQueryUnsupported = "GPUQueryUnsupported"

TagGPUQueryUnsupported is the tag of the error of NewQuerySet on a device without timestamp queries (Features.TimestampQuery is false). The backends produce it; a caller that can do without timestamps compares with it (SPEC §27.1).

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct Blit {
    uint rotation;
    uint pad0_;
    uint pad1_;
    uint pad2_;
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 uv;
    char _pad2[8];
};

struct vs_mainInput {
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 uv [[user(loc0), center_perspective]];
};
vertex vs_mainOutput vs_main(
  uint index [[vertex_id]]
, constant Blit& blit [[buffer(0)]]
) {
    metal::float2 uv = {};
    VertexOutput out = {};
    uint rotation = blit.rotation;
    metal::float2 p = metal::float2(static_cast<float>((index << 1u) & 2u), static_cast<float>(index & 2u));
    float a = p.x;
    float b = 1.0 - p.y;
    uv = metal::float2(a, b);
    if (rotation == 1u) {
        uv = metal::float2(b, 1.0 - a);
    } else {
        if (rotation == 2u) {
            uv = metal::float2(1.0 - a, 1.0 - b);
        } else {
            if (rotation == 3u) {
                uv = metal::float2(1.0 - b, a);
            }
        }
    }
    out.position = metal::float4((p * 2.0) - metal::float2(1.0), 0.0, 1.0);
    metal::float2 _e47 = uv;
    out.uv = _e47;
    VertexOutput _e48 = out;
    const auto _tmp = _e48;
    return vs_mainOutput { _tmp.position, _tmp.uv };
}


struct fs_mainInput {
    metal::float2 uv_1 [[user(loc0), center_perspective]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, metal::texture2d<float, metal::access::sample> frameTexture [[texture(0)]]
, metal::sampler frameSampler [[sampler(0)]]
) {
    const VertexOutput in = { position, varyings_1.uv_1 };
    metal::float4 _e4 = frameTexture.sample(frameSampler, in.uv);
    return fs_mainOutput { _e4 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "blit", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "frameTexture", Group: 0, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 0, Index: 0}, MetalIndex: 0},
			{Name: "frameSampler", Group: 0, Binding: 1, Kind: BindingSampler, Register: ShaderRegister{Class: "s", Space: 0, Index: 1}, MetalIndex: 0},
		}},
	},
}

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Pass {
    metal::float4 kind;
    metal::float4 sizes;
    metal::float4 blur;
    metal::float4 factor;
    metal::float4 mat0_;
    metal::float4 mat1_;
};
struct VertexOutput {
    metal::float4 position;
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;
constant int PASS_COPY = 0;
constant int PASS_BLUR = 1;
constant int PASS_MORPH = 2;
constant int PASS_MATRIX = 3;
constant int PASS_COLOR = 4;
constant int PASS_DOWN = 5;
constant int PASS_UP = 6;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_2) - metal::max(s_2 * da_2, d_2 * sa_2);
        }
        case 17: {
            return (s_2 + d_2) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_2) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_4
) {
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_4, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_2 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_5 = q - c0_1;
    float b = metal::dot(p_5, cd) + (r0_ * dr);
    float c_10 = metal::dot(p_5, p_5) - (r0_ * r0_);
    if (metal::abs(a_2) < 0.000000000001) {
        if (b == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_10 / (2.0 * b);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b * b) - (a_2 * c_10);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_2 = {};
    bool local_3 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_2 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                x = m_2;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_2 = _e61 >= size.x;
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_3 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_3 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_3));
            } else {
                y = m_3;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_3 = _e114 >= size.y;
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_1 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j_1 = -1;
    int i_5 = {};
    metal::int2 base_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_3 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_1), paintImage.get_height(level_1)));
    metal::float2 uv = (q_1 * static_cast<metal::float2>(size_3)) / static_cast<metal::float2>(base_1);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv)), size_3, level_1, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j_1;
                j_1 = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j_1;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j_1;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_5 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_5;
                        i_5 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_5;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_5;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_5;
                        int _e65 = j_1;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_3, level_1, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_3)), a_3);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_3, level_1, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_3, level_1, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_3, level_1, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_3, level_1, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_2 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_2 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_2 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_2, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_5 = q_2 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_5.y, d_5.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_2, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_6 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_6.y) * as_type<uint>(8))) + as_type<uint>(p_6.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o_1 = {};
    bool local_4 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o_1 = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o_1;
        float _e37 = ditherAt(dev_2, bayer);
        o_1 = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o_1;
    return _e40;
}

int floorMod(
    int i_6,
    int m_1
) {
    return as_type<int>(as_type<uint>(i_6) - as_type<uint>(as_type<int>(as_type<uint>(m_1) * as_type<uint>(naga_f2i32(metal::floor(static_cast<float>(i_6) / static_cast<float>(m_1)))))));
}

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

metal::float4 fetchTop(
    metal::int2 p_3,
    metal::int2 size_1,
    int mode_5,
    metal::texture2d<float, metal::access::sample> src_1
) {
    bool local_6 = {};
    metal::int2 _e5 = wrapIndex(p_3.x, size_1.x, mode_5);
    metal::int2 _e8 = wrapIndex(p_3.y, size_1.y, mode_5);
    if (!((_e5.y == 0))) {
        local_6 = _e8.y == 0;
    } else {
        local_6 = true;
    }
    bool _e19 = local_6;
    if (_e19) {
        return metal::float4(0.0);
    }
    metal::float4 _e27 = src_1.read(metal::uint2(metal::int2(_e5.x, _e8.x)), 0);
    return _e27;
}

metal::float4 fetchOrClear(
    metal::int2 p_4,
    metal::int2 size_2,
    metal::texture2d<float, metal::access::sample> src_1
) {
    metal::float4 _e3 = fetchTop(p_4, size_2, 3, src_1);
    return _e3;
}

struct vs_mainInput {
};
struct vs_mainOutput {
    metal::float4 position [[position]];
};
vertex vs_mainOutput vs_main(
  uint index [[vertex_id]]
) {
    VertexOutput out = {};
    metal::float2 p_7 = metal::float2(static_cast<float>((index << 1u) & 2u), static_cast<float>(index & 2u));
    out.position = metal::float4((p_7 * 2.0) - metal::float2(1.0), 0.0, 1.0);
    VertexOutput _e20 = out;
    const auto _tmp = _e20;
    return vs_mainOutput { _tmp.position };
}

int naga_neg(int val) {
    return as_type<int>(-as_type<uint>(val));
}


struct fs_mainInput {
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  metal::float4 position [[position]]
, constant Paint& paint [[buffer(1)]]
, constant Pass& params [[buffer(0)]]
, metal::texture2d<float, metal::access::sample> src_1 [[texture(0)]]
) {
    const VertexOutput in = { position };
    metal::float4 o = metal::float4(0.0);
    int i = {};
    int i_1 = {};
    int j = 0;
    int i_2 = {};
    metal::float4 _e3 = params.sizes;
    metal::int2 srcSize = naga_f2i32(_e3.xy + metal::float2(0.5));
    metal::int2 q_3 = naga_f2i32(metal::floor(in.position.xy));
    float _e16 = params.kind.x;
    int kind_2 = naga_f2i32(_e16 + 0.5);
    float _e29 = params.kind.y;
    metal::int2 axis = (_e29 < 0.5) ? metal::int2(1, 0) : metal::int2(0, 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) {
        metal::float4 _e52 = fetchOrClear(q_3, srcSize, src_1);
        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);
            bool loop_init_2 = true;
            while(true) {
                if (metal::all(loop_bound_3 == uint2(0u))) { break; }
                loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
                if (!loop_init_2) {
                    int _e84 = i;
                    i = as_type<int>(as_type<uint>(_e84) + as_type<uint>(1));
                }
                loop_init_2 = false;
                int _e68 = i;
                if (_e68 <= radius) {
                } else {
                    break;
                }
                {
                    metal::float4 _e70 = o;
                    int _e71 = i;
                    metal::float4 _e74 = fetchTop(as_type<metal::int2>(as_type<metal::uint2>(q_3) + as_type<metal::uint2>(as_type<metal::int2>(as_type<metal::uint2>(axis) * as_type<uint>(_e71)))), srcSize, tile, src_1);
                    int _e75 = i;
                    int _e76 = i;
                    o = _e70 + (_e74 * metal::exp(static_cast<float>(as_type<int>(as_type<uint>(_e75) * as_type<uint>(_e76))) * k));
                }
            }
            metal::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 ? metal::float4(0.0) : metal::float4(1.0);
                i_1 = naga_neg(radius);
                uint2 loop_bound_4 = uint2(4294967295u);
                bool loop_init_3 = true;
                while(true) {
                    if (metal::all(loop_bound_4 == uint2(0u))) { break; }
                    loop_bound_4 -= uint2(loop_bound_4.y == 0u, 1u);
                    if (!loop_init_3) {
                        int _e119 = i_1;
                        i_1 = as_type<int>(as_type<uint>(_e119) + as_type<uint>(1));
                    }
                    loop_init_3 = false;
                    int _e107 = i_1;
                    if (_e107 <= radius) {
                    } else {
                        break;
                    }
                    {
                        int _e109 = i_1;
                        metal::float4 _e112 = fetchOrClear(as_type<metal::int2>(as_type<metal::uint2>(q_3) + as_type<metal::uint2>(as_type<metal::int2>(as_type<metal::uint2>(axis) * as_type<uint>(_e109)))), srcSize, src_1);
                        metal::float4 _e113 = o;
                        metal::float4 _e115 = o;
                        o = dilate ? metal::max(_e115, _e112) : metal::min(_e113, _e112);
                    }
                }
            } else {
                if (kind_2 == PASS_MATRIX) {
                    float _e126 = params.factor.z;
                    if (_e126 < 1.5) {
                        metal::float4 _e131 = params.mat1_;
                        metal::float2 origin = _e131.zw;
                        metal::float2 dev_3 = (origin + static_cast<metal::float2>(q_3)) + metal::float2(0.5);
                        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;
                        metal::float2 s_6 = metal::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) {
                            metal::float4 _e184 = fetchOrClear(naga_f2i32(metal::floor(s_6)), srcSize, src_1);
                            o = _e184;
                        } else {
                            metal::float2 f_1 = s_6 - metal::float2(0.5);
                            metal::int2 i0_1 = naga_f2i32(metal::floor(f_1));
                            metal::float2 t_4 = f_1 - static_cast<metal::float2>(i0_1);
                            metal::float4 _e192 = fetchOrClear(i0_1, srcSize, src_1);
                            metal::float4 _e197 = fetchOrClear(as_type<metal::int2>(as_type<metal::uint2>(i0_1) + as_type<metal::uint2>(metal::int2(1, 0))), srcSize, src_1);
                            metal::float4 top = metal::mix(_e192, _e197, t_4.x);
                            metal::float4 _e204 = fetchOrClear(as_type<metal::int2>(as_type<metal::uint2>(i0_1) + as_type<metal::uint2>(metal::int2(0, 1))), srcSize, src_1);
                            metal::float4 _e209 = fetchOrClear(as_type<metal::int2>(as_type<metal::uint2>(i0_1) + as_type<metal::uint2>(metal::int2(1, 1))), srcSize, src_1);
                            metal::float4 bot = metal::mix(_e204, _e209, t_4.x);
                            o = metal::mix(top, bot, t_4.y);
                        }
                    }
                } else {
                    if (kind_2 == PASS_COLOR) {
                        metal::float4 _e216 = fetchOrClear(q_3, srcSize, src_1);
                        metal::float4 _e217 = applyFilters(_e216, paint);
                        o = metal::clamp(_e217, metal::float4(0.0), metal::float4(1.0));
                    } else {
                        if (kind_2 == PASS_DOWN) {
                            metal::float4 _e227 = params.factor;
                            metal::int2 f_2 = naga_f2i32(_e227.xy + metal::float2(0.5));
                            uint2 loop_bound_5 = uint2(4294967295u);
                            bool loop_init_4 = true;
                            while(true) {
                                if (metal::all(loop_bound_5 == uint2(0u))) { break; }
                                loop_bound_5 -= uint2(loop_bound_5.y == 0u, 1u);
                                if (!loop_init_4) {
                                    int _e255 = j;
                                    j = as_type<int>(as_type<uint>(_e255) + as_type<uint>(1));
                                }
                                loop_init_4 = false;
                                int _e235 = j;
                                if (_e235 < f_2.y) {
                                } else {
                                    break;
                                }
                                {
                                    i_2 = 0;
                                    uint2 loop_bound_6 = uint2(4294967295u);
                                    bool loop_init_5 = true;
                                    while(true) {
                                        if (metal::all(loop_bound_6 == uint2(0u))) { break; }
                                        loop_bound_6 -= uint2(loop_bound_6.y == 0u, 1u);
                                        if (!loop_init_5) {
                                            int _e252 = i_2;
                                            i_2 = as_type<int>(as_type<uint>(_e252) + as_type<uint>(1));
                                        }
                                        loop_init_5 = false;
                                        int _e240 = i_2;
                                        if (_e240 < f_2.x) {
                                        } else {
                                            break;
                                        }
                                        {
                                            metal::float4 _e243 = o;
                                            int _e245 = i_2;
                                            int _e246 = j;
                                            metal::float4 _e249 = fetchTop(as_type<metal::int2>(as_type<metal::uint2>(as_type<metal::int2>(as_type<metal::uint2>(q_3) * as_type<metal::uint2>(f_2))) + as_type<metal::uint2>(metal::int2(_e245, _e246))), srcSize, tile, src_1);
                                            o = _e243 + _e249;
                                        }
                                    }
                                }
                            }
                            metal::float4 _e257 = o;
                            o = _e257 / metal::float4(static_cast<float>(as_type<int>(as_type<uint>(f_2.x) * as_type<uint>(f_2.y))));
                        } else {
                            metal::float4 _e270 = params.factor;
                            metal::float2 f_3 = ((static_cast<metal::float2>(q_3) + metal::float2(0.5)) / _e270.xy) - metal::float2(0.5);
                            metal::int2 i0_2 = naga_f2i32(metal::floor(f_3));
                            metal::float2 t_5 = f_3 - static_cast<metal::float2>(i0_2);
                            metal::float4 _e281 = fetchTop(i0_2, srcSize, 0, src_1);
                            metal::float4 _e287 = fetchTop(as_type<metal::int2>(as_type<metal::uint2>(i0_2) + as_type<metal::uint2>(metal::int2(1, 0))), srcSize, 0, src_1);
                            metal::float4 top_1 = metal::mix(_e281, _e287, t_5.x);
                            metal::float4 _e295 = fetchTop(as_type<metal::int2>(as_type<metal::uint2>(i0_2) + as_type<metal::uint2>(metal::int2(0, 1))), srcSize, 0, src_1);
                            metal::float4 _e301 = fetchTop(as_type<metal::int2>(as_type<metal::uint2>(i0_2) + as_type<metal::uint2>(metal::int2(1, 1))), srcSize, 0, src_1);
                            metal::float4 bot_1 = metal::mix(_e295, _e301, t_5.x);
                            o = metal::mix(top_1, bot_1, t_5.y);
                        }
                    }
                }
            }
        }
    }
    metal::float4 _e306 = o;
    return fs_mainOutput { _e306 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, 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}, MetalIndex: 0},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 1},
			{Name: "src", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 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, 1 when the texels are linear values (an RGBA16Float texture layer)
	@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);
	}
	if in.inv1.w > 0.5 {
		// Linear premultiplied texels (a texture layer of an RGBA16Float texture): the surface holds sRGB-encoded values.
		let c = unpremul(color);
		color = premul(vec4<f32>(linearToSrgb(c.rgb), c.a));
	}
	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;
        }
    }
    if ((in_.inv1_.w > 0.5)) {
        float4 _e143 = color;
        const float4 _e144 = unpremul(_e143);
        const float3 _e146 = linearToSrgb(_e144.xyz);
        const float4 _e149 = premul(float4(_e146, _e144.w));
        color = _e149;
    }
    float4 _e150 = color;
    color = (_e150 * in_.color);
    if ((in_.img.z >= 0.0)) {
        float4 _e162 = color;
        const float4 _e164 = blendPair(naga_f2i32((in_.img.z + 0.5)), _e162, in_.tint);
        color = _e164;
    }
    float4 _e165 = color;
    const float4 _e166 = applyFilters(_e165);
    color = _e166;
    float _e167 = coverage;
    if ((_e167 <= 0.0)) {
        discard;
    }
    float4 _e170 = color;
    float _e171 = coverage;
    const float4 _e172 = finish(_e170, _e171, dev_9);
    return _e172;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Draw {
    metal::float4 clipMask;
};
struct Instance {
    metal::float4 quad_;
    metal::float4 inv0_;
    metal::float4 inv1_;
    metal::float4 color;
    metal::float4 clip;
    metal::float4 box;
    metal::float4 img;
    metal::float4 tint;
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 dev;
    char _pad2[8];
    metal::float4 inv0_;
    metal::float4 inv1_;
    metal::float4 color;
    metal::float4 clip;
    metal::float4 box;
    metal::float4 img;
    metal::float4 tint;
};
struct type_14 {
    metal::float2 inner[6];
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;
constant int FLAG_BOX = 1;
constant int FLAG_ANTIALIAS = 2;
constant int QUALITY_NONE = 0;
constant int QUALITY_HIGH = 3;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_2) - metal::max(s_2 * da_2, d_2 * sa_2);
        }
        case 17: {
            return (s_2 + d_2) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_2) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_4
) {
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_4, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_2 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_4 = q - c0_1;
    float b = metal::dot(p_4, cd) + (r0_ * dr);
    float c_11 = metal::dot(p_4, p_4) - (r0_ * r0_);
    if (metal::abs(a_2) < 0.000000000001) {
        if (b == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_11 / (2.0 * b);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b * b) - (a_2 * c_11);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_2 = {};
    bool local_3 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_1 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_1 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_1));
            } else {
                x = m_1;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_2 = _e61 >= size.x;
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_2 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                y = m_2;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_3 = _e114 >= size.y;
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_1 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j = -1;
    int i_2 = {};
    metal::int2 base_2 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_2 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_3), paintImage.get_height(level_3)));
    metal::float2 uv_1 = (q_1 * static_cast<metal::float2>(size_2)) / static_cast<metal::float2>(base_2);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv_1)), size_2, level_3, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv_1 - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_2 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_2;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_2;
                        int _e65 = j;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_2, level_3, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_3)), a_3);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_2, level_3, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_2, level_3, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_2, level_3, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_2, level_3, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_2 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_2 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_2 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_2, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_5 = q_2 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_5.y, d_5.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_2, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_5 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_5.y) * as_type<uint>(8))) + as_type<uint>(p_5.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o = {};
    bool local_4 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o;
        float _e37 = ditherAt(dev_2, bayer);
        o = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o;
    return _e40;
}

metal::float4 toClip(
    metal::float2 dev_3,
    constant Frame& frame
) {
    metal::float2 _e3 = frame.origin;
    metal::float2 p_6 = dev_3 - _e3;
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return metal::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 metal::clamp(metal::min(c_10 + 0.5, hi) - metal::max(c_10 - 0.5, lo), 0.0, 1.0);
}

float boxCoverage(
    metal::float2 dev_4,
    metal::float4 r,
    bool antialias
) {
    bool local_5 = {};
    bool local_6 = {};
    bool local_7 = {};
    if (antialias) {
        float _e6 = span(dev_4.x, r.x, r.z);
        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(
    metal::float2 dev_5,
    constant Draw& draw,
    metal::texture2d<float, metal::access::sample> clipMask
) {
    bool local_8 = {};
    bool local_9 = {};
    bool local_10 = {};
    float _e4 = draw.clipMask.z;
    if (_e4 <= 0.0) {
        return 1.0;
    }
    metal::float4 _e12 = draw.clipMask;
    metal::int2 p_7 = as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_5))) - as_type<metal::uint2>(naga_f2i32(_e12.xy)));
    if (!((p_7.x < 0))) {
        local_8 = p_7.y < 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;
    }
    metal::float4 _e54 = clipMask.read(metal::uint2(p_7), 0);
    return _e54.x;
}

float mitchell(
    float x0_1
) {
    float x_2 = metal::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;
}

metal::float4 texel(
    metal::int2 p_3,
    metal::int2 size_1,
    int level_1,
    metal::texture2d<float, metal::access::sample> image
) {
    metal::float4 _e10 = image.read(metal::uint2(metal::clamp(p_3, metal::int2(0), as_type<metal::int2>(as_type<metal::uint2>(size_1) - as_type<metal::uint2>(metal::int2(1))))), level_1);
    return _e10;
}

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

struct vs_mainInput {
    metal::float4 quad_ [[attribute(0)]];
    metal::float4 inv0_ [[attribute(1)]];
    metal::float4 inv1_ [[attribute(2)]];
    metal::float4 color_1 [[attribute(3)]];
    metal::float4 clip [[attribute(4)]];
    metal::float4 box [[attribute(5)]];
    metal::float4 img [[attribute(6)]];
    metal::float4 tint [[attribute(7)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 dev [[user(loc0), center_perspective]];
    metal::float4 inv0_ [[user(loc1), flat]];
    metal::float4 inv1_ [[user(loc2), flat]];
    metal::float4 color [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
    metal::float4 box [[user(loc5), flat]];
    metal::float4 img [[user(loc6), flat]];
    metal::float4 tint [[user(loc7), flat]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, uint index [[vertex_id]]
, constant Frame& frame [[buffer(0)]]
) {
    const Instance instance = { varyings.quad_, varyings.inv0_, varyings.inv1_, varyings.color_1, varyings.clip, varyings.box, varyings.img, varyings.tint };
    type_14 corners = type_14 {{metal::float2(0.0, 0.0), metal::float2(1.0, 0.0), metal::float2(0.0, 1.0), metal::float2(1.0, 0.0), metal::float2(1.0, 1.0), metal::float2(0.0, 1.0)}};
    VertexOutput out = {};
    metal::float2 _e27 = corners.inner[index];
    metal::float2 dev_6 = metal::mix(instance.quad_.xy, instance.quad_.zw, _e27);
    metal::float4 _e31 = toClip(dev_6, frame);
    out.position = _e31;
    out.dev = dev_6;
    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;
    const auto _tmp = _e47;
    return vs_mainOutput { _tmp.position, _tmp.dev, _tmp.inv0_, _tmp.inv1_, _tmp.color, _tmp.clip, _tmp.box, _tmp.img, _tmp.tint };
}


struct fs_mainInput {
    metal::float2 dev_6 [[user(loc0), center_perspective]];
    metal::float4 inv0_ [[user(loc1), flat]];
    metal::float4 inv1_ [[user(loc2), flat]];
    metal::float4 color_1 [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
    metal::float4 box [[user(loc5), flat]];
    metal::float4 img [[user(loc6), flat]];
    metal::float4 tint [[user(loc7), flat]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, constant Paint& paint [[buffer(2)]]
, metal::texture2d<float, metal::access::sample> dst [[texture(3)]]
, constant Draw& draw [[buffer(1)]]
, metal::texture2d<float, metal::access::sample> clipMask [[texture(0)]]
, metal::texture2d<float, metal::access::sample> image [[texture(1)]]
, metal::sampler imageSampler [[sampler(0)]]
) {
    type_11 bayer = type_11 {{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}};
    const VertexOutput in = { position, varyings_1.dev_6, {}, varyings_1.inv0_, varyings_1.inv1_, varyings_1.color_1, varyings_1.clip, varyings_1.box, varyings_1.img, varyings_1.tint };
    float coverage = {};
    float pixels = {};
    metal::float4 color = {};
    metal::float2 dev_7 = metal::floor(in.dev) + metal::float2(0.5);
    int flags = naga_f2i32(in.inv1_.z + 0.5);
    bool aa = (flags & FLAG_ANTIALIAS) != 0;
    metal::float2 uv_2 = metal::float2(((in.inv0_.x * dev_7.x) + (in.inv0_.z * dev_7.y)) + in.inv1_.x, ((in.inv0_.y * dev_7.x) + (in.inv0_.w * dev_7.y)) + in.inv1_.y);
    metal::float2 c_13 = (in.box.xy + in.box.zw) * 0.5;
    metal::float2 q_3 = metal::abs(uv_2 - c_13) - ((in.box.zw - in.box.xy) * 0.5);
    float d_6 = metal::max(q_3.x, q_3.y);
    float _e60 = metal::dfdx(d_6);
    float _e61 = metal::dfdy(d_6);
    float g = metal::length(metal::float2(_e60, _e61));
    if ((flags & FLAG_BOX) != 0) {
        float _e70 = boxCoverage(dev_7, in.box, aa);
        coverage = _e70;
    } else {
        if (aa) {
            pixels = (d_6 <= 0.0) ? -1.0 : 1.0;
            if (g > 0.000000001) {
                pixels = d_6 / g;
            }
            float _e80 = pixels;
            coverage = metal::clamp(0.5 - _e80, 0.0, 1.0);
        } else {
            coverage = (d_6 <= 0.0) ? 1.0 : 0.0;
        }
    }
    float _e91 = coverage;
    float _e97 = span(dev_7.x, in.clip.x, in.clip.z);
    float _e104 = span(dev_7.y, in.clip.y, in.clip.w);
    float _e106 = clipMaskCoverage(dev_7, draw, clipMask);
    coverage = ((_e91 * _e97) * _e104) * _e106;
    metal::int2 base_3 = static_cast<metal::int2>(metal::uint2(image.get_width(0), image.get_height(0)));
    int quality_1 = naga_f2i32(in.img.y + 0.5);
    if (quality_1 == QUALITY_NONE) {
        metal::float4 _e123 = texel(naga_f2i32(metal::floor(uv_2)), base_3, 0, image);
        color = _e123;
    } else {
        if (quality_1 == QUALITY_HIGH) {
            metal::float4 _e131 = bicubic(uv_2, base_3, naga_f2i32(in.img.x + 0.5), image);
            color = _e131;
        } else {
            metal::float4 _e138 = image.sample(imageSampler, uv_2 / static_cast<metal::float2>(base_3), metal::level(in.img.x));
            color = _e138;
        }
    }
    if (in.inv1_.w > 0.5) {
        metal::float4 _e143 = color;
        metal::float4 _e144 = unpremul(_e143);
        metal::float3 _e146 = linearToSrgb(_e144.xyz);
        metal::float4 _e149 = premul(metal::float4(_e146, _e144.w));
        color = _e149;
    }
    metal::float4 _e150 = color;
    color = _e150 * in.color;
    if (in.img.z >= 0.0) {
        metal::float4 _e162 = color;
        metal::float4 _e164 = blendPair(naga_f2i32(in.img.z + 0.5), _e162, in.tint);
        color = _e164;
    }
    metal::float4 _e165 = color;
    metal::float4 _e166 = applyFilters(_e165, paint);
    color = _e166;
    float _e167 = coverage;
    if (_e167 <= 0.0) {
        metal::discard_fragment();
    }
    metal::float4 _e170 = color;
    float _e171 = coverage;
    metal::float4 _e172 = finish(_e170, _e171, dev_7, paint, dst, bayer);
    return fs_mainOutput { _e172 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}, MetalIndex: 1},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 2},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 0},
			{Name: "image", Group: 2, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}, MetalIndex: 1},
			{Name: "imageSampler", Group: 2, Binding: 1, Kind: BindingSampler, Register: ShaderRegister{Class: "s", Space: 2, Index: 1}, MetalIndex: 0},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}, MetalIndex: 3},
		}},
	},
}

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Draw {
    metal::float4 clipMask;
    metal::float4 layerRect;
    metal::float4 read;
};
struct Instance {
    metal::float4 quad_;
    metal::float4 color;
    metal::float4 clip;
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 dev;
    char _pad2[8];
    metal::float4 color;
    metal::float4 clip;
};
struct type_13 {
    metal::float2 inner[6];
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_2) - metal::max(s_2 * da_2, d_2 * sa_2);
        }
        case 17: {
            return (s_2 + d_2) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_2) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_4
) {
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_4, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_2 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_5 = q - c0_1;
    float b_1 = metal::dot(p_5, cd) + (r0_ * dr);
    float c_11 = metal::dot(p_5, p_5) - (r0_ * r0_);
    if (metal::abs(a_2) < 0.000000000001) {
        if (b_1 == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_11 / (2.0 * b_1);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b_1 * b_1) - (a_2 * c_11);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_3 = {};
    bool local_4 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_1 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_1 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_1));
            } else {
                x = m_1;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_3 = _e61 >= size.x;
                } else {
                    local_3 = true;
                }
                bool _e65 = local_3;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_2 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                y = m_2;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_4 = _e114 >= size.y;
                } else {
                    local_4 = true;
                }
                bool _e118 = local_4;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_2 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j = -1;
    int i_2 = {};
    metal::int2 base_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_1), paintImage.get_height(level_1)));
    metal::float2 uv = (q_1 * static_cast<metal::float2>(size_1)) / static_cast<metal::float2>(base_1);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv)), size_1, level_1, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_2 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_2;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_2;
                        int _e65 = j;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_1, level_1, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_3)), a_3);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_1, level_1, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_1, level_1, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_1, level_1, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_1, level_1, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_2 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_2 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_2 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_2, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_5 = q_2 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_5.y, d_5.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_2, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_6 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_6.y) * as_type<uint>(8))) + as_type<uint>(p_6.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o = {};
    bool local_5 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o;
        float _e37 = ditherAt(dev_2, bayer);
        o = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o;
    return _e40;
}

float erfApprox(
    float x0_1
) {
    float s_6 = (x0_1 < 0.0) ? -1.0 : 1.0;
    float x_3 = metal::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) * metal::exp(-(x_3) * x_3));
    return s_6 * y_2;
}

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

float cornerInset(
    metal::float2 rad,
    float y_1,
    float hy,
    bool top
) {
    bool local_6 = {};
    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 - metal::sqrt(metal::max(1.0 - (t_5 * t_5), 0.0)));
}

float zoneMass(
    float y0_,
    float y1_,
    metal::float2 p_3,
    metal::float2 h,
    metal::float4 r1_,
    metal::float4 r2_,
    float sigma,
    bool top_1
) {
    float sum_1 = 0.0;
    int i_3 = 0;
    float a_4 = metal::max(y0_, p_3.y - (4.0 * sigma));
    float b_2 = metal::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);
    bool loop_init_2 = true;
    while(true) {
        if (metal::all(loop_bound_3 == uint2(0u))) { break; }
        loop_bound_3 -= uint2(loop_bound_3.y == 0u, 1u);
        if (!loop_init_2) {
            int _e76 = i_3;
            i_3 = as_type<int>(as_type<uint>(_e76) + as_type<uint>(1));
        }
        loop_init_2 = false;
        int _e27 = i_3;
        if (_e27 < 16) {
        } else {
            break;
        }
        {
            int _e30 = i_3;
            float y_3 = a_4 + ((static_cast<float>(_e30) + 0.5) * dv);
            float _e42 = cornerInset(top_1 ? r1_.xy : r2_.zw, y_3, h.y, top_1);
            float l_1 = -(h.x) + _e42;
            float _e49 = cornerInset(top_1 ? r1_.zw : r2_.xy, y_3, h.y, top_1);
            float r = h.x - _e49;
            float w = metal::exp(((-0.5 * (y_3 - p_3.y)) * (y_3 - p_3.y)) / (sigma * sigma));
            float _e61 = sum_1;
            float _e65 = normalCdf((r - p_3.x) / sigma);
            float _e69 = normalCdf((l_1 - p_3.x) / sigma);
            sum_1 = _e61 + (w * metal::max(_e65 - _e69, 0.0));
        }
    }
    float _e78 = sum_1;
    return (_e78 * dv) / (sigma * 2.5066283);
}

float blurredRRect(
    metal::float2 p_4,
    metal::float2 h_1,
    metal::float4 r1_1,
    metal::float4 r2_1,
    float sigma_1
) {
    float mass = {};
    float topH = metal::max(r1_1.y, r1_1.w);
    float botH = metal::max(r2_1.y, r2_1.w);
    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;
    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_) {
        float _e35 = normalCdf((m1_ - p_4.y) / sigma_1);
        float _e39 = normalCdf((m0_ - p_4.y) / sigma_1);
        float rows = _e35 - _e39;
        float _e45 = normalCdf((h_1.x - p_4.x) / sigma_1);
        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 metal::clamp(_e56, 0.0, 1.0);
}

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

metal::float4 toClip(
    metal::float2 dev_3,
    constant Frame& frame
) {
    metal::float2 _e3 = frame.origin;
    metal::float2 p_7 = dev_3 - _e3;
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return metal::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 metal::clamp(metal::min(c_10 + 0.5, hi) - metal::max(c_10 - 0.5, lo), 0.0, 1.0);
}

float clipMaskCoverage(
    metal::float2 dev_4,
    constant Draw& draw,
    metal::texture2d<float, metal::access::sample> clipMask
) {
    bool local_7 = {};
    bool local_8 = {};
    bool local_9 = {};
    float _e4 = draw.clipMask.z;
    if (_e4 <= 0.0) {
        return 1.0;
    }
    metal::float4 _e12 = draw.clipMask;
    metal::int2 p_8 = as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_4))) - as_type<metal::uint2>(naga_f2i32(_e12.xy)));
    if (!((p_8.x < 0))) {
        local_7 = p_8.y < 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;
    }
    metal::float4 _e54 = clipMask.read(metal::uint2(p_8), 0);
    return _e54.x;
}

struct vs_mainInput {
    metal::float4 quad_ [[attribute(0)]];
    metal::float4 color [[attribute(1)]];
    metal::float4 clip [[attribute(2)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 dev [[user(loc0), center_perspective]];
    metal::float4 color [[user(loc1), flat]];
    metal::float4 clip [[user(loc2), flat]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, uint index [[vertex_id]]
, constant Frame& frame [[buffer(0)]]
) {
    const Instance instance = { varyings.quad_, varyings.color, varyings.clip };
    type_13 corners = type_13 {{metal::float2(0.0, 0.0), metal::float2(1.0, 0.0), metal::float2(0.0, 1.0), metal::float2(1.0, 0.0), metal::float2(1.0, 1.0), metal::float2(0.0, 1.0)}};
    VertexOutput out = {};
    metal::float2 _e27 = corners.inner[index];
    metal::float2 dev_5 = metal::mix(instance.quad_.xy, instance.quad_.zw, _e27);
    metal::float4 _e31 = toClip(dev_5, frame);
    out.position = _e31;
    out.dev = dev_5;
    out.color = instance.color;
    out.clip = instance.clip;
    VertexOutput _e37 = out;
    const auto _tmp = _e37;
    return vs_mainOutput { _tmp.position, _tmp.dev, _tmp.color, _tmp.clip };
}


struct fs_mainInput {
    metal::float2 dev_5 [[user(loc0), center_perspective]];
    metal::float4 color [[user(loc1), flat]];
    metal::float4 clip [[user(loc2), flat]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, constant Paint& paint [[buffer(2)]]
, metal::texture2d<float, metal::access::sample> paintImage [[texture(3)]]
, metal::texture2d<float, metal::access::sample> dst [[texture(4)]]
, constant Draw& draw [[buffer(1)]]
, metal::texture2d<float, metal::access::sample> clipMask [[texture(0)]]
, metal::texture2d<float, metal::access::sample> layer [[texture(1)]]
, metal::texture2d<float, metal::access::sample> layerOwn [[texture(2)]]
) {
    type_11 bayer = type_11 {{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}};
    const VertexOutput in = { position, varyings_1.dev_5, {}, varyings_1.color, varyings_1.clip };
    bool local = {};
    float own = 0.0;
    metal::float2 dev_6 = metal::floor(in.dev) + metal::float2(0.5);
    float _e11 = span(dev_6.x, in.clip.x, in.clip.z);
    float _e17 = span(dev_6.y, in.clip.y, in.clip.w);
    float _e19 = clipMaskCoverage(dev_6, draw, clipMask);
    float coverage = (_e11 * _e17) * _e19;
    if (coverage <= 0.0) {
        metal::discard_fragment();
    }
    metal::float4 _e25 = draw.layerRect;
    metal::int2 p_9 = naga_f2i32(metal::floor(dev_6 - _e25.xy));
    metal::float4 texel = layer.read(metal::uint2(p_9), 0);
    float _e36 = draw.read.y;
    if (_e36 != 0.0) {
        local = texel.w <= 0.0;
    } else {
        local = false;
    }
    bool _e45 = local;
    if (_e45) {
        metal::discard_fragment();
    }
    float _e49 = draw.read.x;
    int style_1 = naga_f2i32(_e49);
    if (style_1 >= 0) {
        if (style_1 != 0) {
            metal::float4 _e59 = layerOwn.read(metal::uint2(p_9), 0);
            own = _e59.w;
        }
        float _e62 = own;
        float _e63 = blurStyle(style_1, texel.w, _e62);
        float cov_1 = _e63 * coverage;
        if (cov_1 < 0.0009765625) {
            metal::discard_fragment();
        }
        metal::float4 _e68 = paintSource(in.color, dev_6, paint, paintImage);
        metal::float4 _e69 = finish(_e68, cov_1, dev_6, paint, dst, bayer);
        return fs_mainOutput { _e69 };
    }
    metal::float4 _e72 = applyFilters(texel * in.color, paint);
    metal::float4 _e73 = finish(_e72, coverage, dev_6, paint, dst, bayer);
    return fs_mainOutput { _e73 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}, MetalIndex: 1},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 2},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 0},
			{Name: "layer", Group: 2, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}, MetalIndex: 1},
			{Name: "layerOwn", Group: 2, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 2, Index: 1}, MetalIndex: 2},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}, MetalIndex: 3},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}, MetalIndex: 4},
		}},
	},
}

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Draw {
    metal::float4 clipMask;
};
struct Instance {
    metal::float4 quad_;
    metal::float4 tile;
    metal::float4 color;
    metal::float4 clip;
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 dev;
    char _pad2[8];
    metal::float4 quad_;
    metal::float4 tile;
    metal::float4 color;
    metal::float4 clip;
};
struct type_13 {
    metal::float2 inner[6];
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_2) - metal::max(s_2 * da_2, d_2 * sa_2);
        }
        case 17: {
            return (s_2 + d_2) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_2) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_4
) {
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_4, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_2 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_3 = q - c0_1;
    float b = metal::dot(p_3, cd) + (r0_ * dr);
    float c_11 = metal::dot(p_3, p_3) - (r0_ * r0_);
    if (metal::abs(a_2) < 0.000000000001) {
        if (b == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_11 / (2.0 * b);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b * b) - (a_2 * c_11);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_2 = {};
    bool local_3 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_1 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_1 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_1));
            } else {
                x = m_1;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_2 = _e61 >= size.x;
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_2 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                y = m_2;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_3 = _e114 >= size.y;
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_1 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j = -1;
    int i_2 = {};
    metal::int2 base_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_1), paintImage.get_height(level_1)));
    metal::float2 uv = (q_1 * static_cast<metal::float2>(size_1)) / static_cast<metal::float2>(base_1);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv)), size_1, level_1, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_2 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_2;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_2;
                        int _e65 = j;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_1, level_1, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_3)), a_3);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_1, level_1, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_1, level_1, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_1, level_1, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_1, level_1, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_2 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_2 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_2 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_2, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_5 = q_2 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_5.y, d_5.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_2, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_4 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_4.y) * as_type<uint>(8))) + as_type<uint>(p_4.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o = {};
    bool local_4 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o;
        float _e37 = ditherAt(dev_2, bayer);
        o = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o;
    return _e40;
}

metal::float4 toClip(
    metal::float2 dev_3,
    constant Frame& frame
) {
    metal::float2 _e3 = frame.origin;
    metal::float2 p_5 = dev_3 - _e3;
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return metal::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 metal::clamp(metal::min(c_10 + 0.5, hi) - metal::max(c_10 - 0.5, lo), 0.0, 1.0);
}

float clipMaskCoverage(
    metal::float2 dev_4,
    constant Draw& draw,
    metal::texture2d<float, metal::access::sample> clipMask
) {
    bool local_5 = {};
    bool local_6 = {};
    bool local_7 = {};
    float _e4 = draw.clipMask.z;
    if (_e4 <= 0.0) {
        return 1.0;
    }
    metal::float4 _e12 = draw.clipMask;
    metal::int2 p_6 = as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_4))) - as_type<metal::uint2>(naga_f2i32(_e12.xy)));
    if (!((p_6.x < 0))) {
        local_5 = p_6.y < 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;
    }
    metal::float4 _e54 = clipMask.read(metal::uint2(p_6), 0);
    return _e54.x;
}

struct vs_mainInput {
    metal::float4 quad_ [[attribute(0)]];
    metal::float4 tile [[attribute(1)]];
    metal::float4 color [[attribute(2)]];
    metal::float4 clip [[attribute(3)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 dev [[user(loc0), center_perspective]];
    metal::float4 quad_ [[user(loc1), flat]];
    metal::float4 tile [[user(loc2), flat]];
    metal::float4 color [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, uint index [[vertex_id]]
, constant Frame& frame [[buffer(0)]]
) {
    const Instance instance = { varyings.quad_, varyings.tile, varyings.color, varyings.clip };
    type_13 corners = type_13 {{metal::float2(0.0, 0.0), metal::float2(1.0, 0.0), metal::float2(0.0, 1.0), metal::float2(1.0, 0.0), metal::float2(1.0, 1.0), metal::float2(0.0, 1.0)}};
    VertexOutput out = {};
    metal::float2 _e27 = corners.inner[index];
    metal::float2 dev_5 = metal::mix(instance.quad_.xy, instance.quad_.zw, _e27);
    metal::float4 _e31 = toClip(dev_5, frame);
    out.position = _e31;
    out.dev = dev_5;
    out.quad_ = instance.quad_;
    out.tile = instance.tile;
    out.color = instance.color;
    out.clip = instance.clip;
    VertexOutput _e41 = out;
    const auto _tmp = _e41;
    return vs_mainOutput { _tmp.position, _tmp.dev, _tmp.quad_, _tmp.tile, _tmp.color, _tmp.clip };
}


struct fs_mainInput {
    metal::float2 dev_5 [[user(loc0), center_perspective]];
    metal::float4 quad_ [[user(loc1), flat]];
    metal::float4 tile [[user(loc2), flat]];
    metal::float4 color [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, constant Paint& paint [[buffer(2)]]
, metal::texture2d<float, metal::access::sample> paintImage [[texture(2)]]
, metal::texture2d<float, metal::access::sample> dst [[texture(3)]]
, constant Draw& draw [[buffer(1)]]
, metal::texture2d<float, metal::access::sample> clipMask [[texture(0)]]
, metal::texture2d<float, metal::access::sample> atlas [[texture(1)]]
) {
    type_11 bayer = type_11 {{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}};
    const VertexOutput in = { position, varyings_1.dev_5, {}, varyings_1.quad_, varyings_1.tile, varyings_1.color, varyings_1.clip };
    float coverage = {};
    metal::float2 dev_6 = metal::floor(in.dev) + metal::float2(0.5);
    metal::int2 texel = as_type<metal::int2>(as_type<metal::uint2>(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(in.tile.xy)) + as_type<metal::uint2>(naga_f2i32(metal::floor(dev_6))))) - as_type<metal::uint2>(naga_f2i32(in.quad_.xy)));
    metal::float4 _e18 = atlas.read(metal::uint2(texel), 0);
    coverage = _e18.x;
    float _e21 = coverage;
    float _e27 = span(dev_6.x, in.clip.x, in.clip.z);
    float _e34 = span(dev_6.y, in.clip.y, in.clip.w);
    float _e36 = clipMaskCoverage(dev_6, draw, clipMask);
    coverage = ((_e21 * _e27) * _e34) * _e36;
    float _e38 = coverage;
    if (_e38 <= 0.0) {
        metal::discard_fragment();
    }
    metal::float4 _e42 = paintSource(in.color, dev_6, paint, paintImage);
    float _e43 = coverage;
    metal::float4 _e44 = finish(_e42, _e43, dev_6, paint, dst, bayer);
    return fs_mainOutput { _e44 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}, MetalIndex: 1},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 2},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 0},
			{Name: "atlas", Group: 2, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 2, Index: 0}, MetalIndex: 1},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}, MetalIndex: 2},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}, MetalIndex: 3},
		}},
	},
}

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Draw {
    metal::float4 clipMask;
};
struct Instance {
    metal::float4 quad_;
    metal::float4 inv0_;
    metal::float4 inv1_;
    metal::float4 color;
    metal::float4 clip;
    metal::float4 a;
    metal::float4 arad1_;
    metal::float4 arad2_;
    metal::float4 b;
    metal::float4 brad1_;
    metal::float4 brad2_;
    metal::float4 param;
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 dev;
    char _pad2[8];
    metal::float4 inv0_;
    metal::float4 inv1_;
    metal::float4 color;
    metal::float4 clip;
    metal::float4 a;
    metal::float4 arad1_;
    metal::float4 arad2_;
    metal::float4 b;
    metal::float4 brad1_;
    metal::float4 brad2_;
    metal::float4 param;
};
struct type_13 {
    metal::float2 inner[6];
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;
constant int KIND_RRECT = 0;
constant int KIND_SUPER = 1;
constant int KIND_SEGMENT = 2;
constant int KIND_FILL = 3;
constant int KIND_BOX = 4;
constant int KIND_OUTLINE = 5;
constant int KIND_SHADOW = 6;
constant int FLAG_HAS_B = 1;
constant int FLAG_ANTIALIAS = 2;
constant int FLAG_SIMILARITY = 4;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_3, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_3) - metal::max(s_2 * da_2, d_3 * sa_2);
        }
        case 17: {
            return (s_2 + d_3) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_3, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_3, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_3) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_5
) {
    float sa_4 = s_5.w;
    float da_4 = d_5.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_5, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_5);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_5.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_5.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_5.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_3 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_9 = q - c0_1;
    float b_2 = metal::dot(p_9, cd) + (r0_ * dr);
    float c_11 = metal::dot(p_9, p_9) - (r0_ * r0_);
    if (metal::abs(a_3) < 0.000000000001) {
        if (b_2 == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_11 / (2.0 * b_2);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b_2 * b_2) - (a_3 * c_11);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::sqrt(disc);
        t1_ = (b_2 + sq) / a_3;
        t2_ = (b_2 - sq) / a_3;
        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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_5 = {};
    bool local_6 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_1 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_1 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_1));
            } else {
                x = m_1;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_5 = _e61 >= size.x;
                } else {
                    local_5 = true;
                }
                bool _e65 = local_5;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_2 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                y = m_2;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_6 = _e114 >= size.y;
                } else {
                    local_6 = true;
                }
                bool _e118 = local_6;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_2 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j = -1;
    int i_2 = {};
    metal::int2 base_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_1), paintImage.get_height(level_1)));
    metal::float2 uv = (q_1 * static_cast<metal::float2>(size_1)) / static_cast<metal::float2>(base_1);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv)), size_1, level_1, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_2 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_2;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_2;
                        int _e65 = j;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_1, level_1, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_4 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_4)), a_4);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_1, level_1, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_1, level_1, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_1, level_1, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_1, level_1, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_3 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_3 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_3 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_3, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_6 = q_3 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_6.y, d_6.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_3, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_10 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_10.y) * as_type<uint>(8))) + as_type<uint>(p_10.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o = {};
    bool local_7 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o;
        float _e37 = ditherAt(dev_2, bayer);
        o = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o;
    return _e40;
}

float erfApprox(
    float x0_1
) {
    float s_6 = (x0_1 < 0.0) ? -1.0 : 1.0;
    float x_3 = metal::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) * metal::exp(-(x_3) * x_3));
    return s_6 * y_2;
}

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

float cornerInset(
    metal::float2 rad,
    float y_1,
    float hy,
    bool top
) {
    bool local_8 = {};
    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 - metal::sqrt(metal::max(1.0 - (t_5 * t_5), 0.0)));
}

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

float blurredRRect(
    metal::float2 p_4,
    metal::float2 h_1,
    metal::float4 r1_1,
    metal::float4 r2_1,
    float sigma_1
) {
    float mass = {};
    float topH = metal::max(r1_1.y, r1_1.w);
    float botH = metal::max(r2_1.y, r2_1.w);
    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;
    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_) {
        float _e35 = normalCdf((m1_ - p_4.y) / sigma_1);
        float _e39 = normalCdf((m0_ - p_4.y) / sigma_1);
        float rows = _e35 - _e39;
        float _e45 = normalCdf((h_1.x - p_4.x) / sigma_1);
        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 metal::clamp(_e56, 0.0, 1.0);
}

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

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

float clipMaskCoverage(
    metal::float2 dev_4,
    constant Draw& draw,
    metal::texture2d<float, metal::access::sample> clipMask
) {
    bool local_9 = {};
    bool local_10 = {};
    bool local_11 = {};
    float _e4 = draw.clipMask.z;
    if (_e4 <= 0.0) {
        return 1.0;
    }
    metal::float4 _e12 = draw.clipMask;
    metal::int2 p_12 = as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_4))) - as_type<metal::uint2>(naga_f2i32(_e12.xy)));
    if (!((p_12.x < 0))) {
        local_9 = p_12.y < 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;
    }
    metal::float4 _e54 = clipMask.read(metal::uint2(p_12), 0);
    return _e54.x;
}

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

metal::float2 cornerRadii(
    metal::float2 p_5,
    metal::float4 r1_2,
    metal::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(
    metal::float2 p_6,
    metal::float2 h_2,
    metal::float4 r1_3,
    metal::float4 r2_3
) {
    bool local_12 = {};
    bool local_13 = {};
    metal::float2 _e4 = cornerRadii(p_6, r1_3, r2_3);
    metal::float2 q_4 = metal::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) {
            float _e28 = ellipseDistance(q_4, _e4);
            return _e28;
        }
        return metal::length(q_4);
    }
    return metal::max(q_4.x - _e4.x, q_4.y - _e4.y);
}

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

float boxCoverage(
    metal::float2 dev_5,
    metal::float4 r_1,
    bool antialias
) {
    bool local_14 = {};
    bool local_15 = {};
    bool local_16 = {};
    if (antialias) {
        float _e6 = span(dev_5.x, r_1.x, r_1.z);
        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(
    metal::float2 p_7,
    metal::float2 h_3,
    metal::float4 r1_4,
    metal::float4 r2_4,
    float n
) {
    bool local_17 = {};
    bool local_18 = {};
    metal::float2 _e5 = cornerRadii(p_7, r1_4, r2_4);
    metal::float2 q_5 = metal::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 metal::length(q_5);
        }
        metal::float2 u_1 = q_5 / _e5;
        float f_1 = (metal::pow(u_1.x, n) + metal::pow(u_1.y, n)) - 1.0;
        float g = n * metal::length(metal::pow(u_1, metal::float2(n - 1.0)) / _e5);
        if (g > 0.0) {
            return f_1 / g;
        }
        return 0.0;
    }
    return metal::max(q_5.x - _e5.x, q_5.y - _e5.y);
}

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

struct vs_mainInput {
    metal::float4 quad_ [[attribute(0)]];
    metal::float4 inv0_ [[attribute(1)]];
    metal::float4 inv1_ [[attribute(2)]];
    metal::float4 color [[attribute(3)]];
    metal::float4 clip [[attribute(4)]];
    metal::float4 a_3 [[attribute(5)]];
    metal::float4 arad1_ [[attribute(6)]];
    metal::float4 arad2_ [[attribute(7)]];
    metal::float4 b_2 [[attribute(8)]];
    metal::float4 brad1_ [[attribute(9)]];
    metal::float4 brad2_ [[attribute(10)]];
    metal::float4 param [[attribute(11)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 dev [[user(loc0), center_perspective]];
    metal::float4 inv0_ [[user(loc1), flat]];
    metal::float4 inv1_ [[user(loc2), flat]];
    metal::float4 color [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
    metal::float4 a [[user(loc5), flat]];
    metal::float4 arad1_ [[user(loc6), flat]];
    metal::float4 arad2_ [[user(loc7), flat]];
    metal::float4 b [[user(loc8), flat]];
    metal::float4 brad1_ [[user(loc9), flat]];
    metal::float4 brad2_ [[user(loc10), flat]];
    metal::float4 param [[user(loc11), flat]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, uint index [[vertex_id]]
, constant Frame& frame [[buffer(0)]]
) {
    const Instance instance = { varyings.quad_, varyings.inv0_, varyings.inv1_, varyings.color, varyings.clip, varyings.a_3, varyings.arad1_, varyings.arad2_, varyings.b_2, varyings.brad1_, varyings.brad2_, varyings.param };
    type_13 corners = type_13 {{metal::float2(0.0, 0.0), metal::float2(1.0, 0.0), metal::float2(0.0, 1.0), metal::float2(1.0, 0.0), metal::float2(1.0, 1.0), metal::float2(0.0, 1.0)}};
    VertexOutput out = {};
    metal::float2 _e27 = corners.inner[index];
    metal::float2 dev_6 = metal::mix(instance.quad_.xy, instance.quad_.zw, _e27);
    metal::float4 _e31 = toClip(dev_6, frame);
    out.position = _e31;
    out.dev = dev_6;
    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;
    const auto _tmp = _e55;
    return vs_mainOutput { _tmp.position, _tmp.dev, _tmp.inv0_, _tmp.inv1_, _tmp.color, _tmp.clip, _tmp.a, _tmp.arad1_, _tmp.arad2_, _tmp.b, _tmp.brad1_, _tmp.brad2_, _tmp.param };
}


struct fs_mainInput {
    metal::float2 dev_6 [[user(loc0), center_perspective]];
    metal::float4 inv0_ [[user(loc1), flat]];
    metal::float4 inv1_ [[user(loc2), flat]];
    metal::float4 color [[user(loc3), flat]];
    metal::float4 clip [[user(loc4), flat]];
    metal::float4 a_3 [[user(loc5), flat]];
    metal::float4 arad1_ [[user(loc6), flat]];
    metal::float4 arad2_ [[user(loc7), flat]];
    metal::float4 b_2 [[user(loc8), flat]];
    metal::float4 brad1_ [[user(loc9), flat]];
    metal::float4 brad2_ [[user(loc10), flat]];
    metal::float4 param [[user(loc11), flat]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, constant Paint& paint [[buffer(2)]]
, metal::texture2d<float, metal::access::sample> paintImage [[texture(1)]]
, metal::texture2d<float, metal::access::sample> dst [[texture(2)]]
, constant Draw& draw [[buffer(1)]]
, metal::texture2d<float, metal::access::sample> clipMask [[texture(0)]]
) {
    type_11 bayer = type_11 {{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}};
    const VertexOutput in = { position, varyings_1.dev_6, {}, varyings_1.inv0_, varyings_1.inv1_, varyings_1.color, varyings_1.clip, varyings_1.a_3, varyings_1.arad1_, varyings_1.arad2_, varyings_1.b_2, varyings_1.brad1_, varyings_1.brad2_, varyings_1.param };
    float d = -1.0;
    float boxCov = 0.0;
    float own = 0.0;
    float coverage = {};
    bool local = {};
    float pixels = {};
    bool local_1 = {};
    bool local_2 = {};
    metal::float2 dev_7 = metal::floor(in.dev) + metal::float2(0.5);
    float _e11 = span(dev_7.x, in.clip.x, in.clip.z);
    float _e17 = span(dev_7.y, in.clip.y, in.clip.w);
    float _e19 = clipMaskCoverage(dev_7, draw, clipMask);
    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) != 0;
    bool hasB = (flags & FLAG_HAS_B) != 0;
    metal::float2 p_13 = metal::float2(((in.inv0_.x * dev_7.x) + (in.inv0_.z * dev_7.y)) + in.inv1_.x, ((in.inv0_.y * dev_7.x) + (in.inv0_.w * dev_7.y)) + in.inv1_.y);
    if (kind_2 == KIND_BOX) {
        float _e71 = boxCoverage(dev_7, in.a, antialias_1);
        boxCov = _e71;
        if (hasB) {
            float _e72 = boxCov;
            float _e74 = boxCoverage(dev_7, in.b, antialias_1);
            boxCov = _e72 - _e74;
        }
    } else {
        if (kind_2 == KIND_SHADOW) {
            metal::float2 c_13 = dev_7 - in.a.xy;
            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 != 0) {
                float _e101 = rrectDistance(c_13, in.a.zw, in.arad1_, in.arad2_);
                own = metal::clamp(0.5 - _e101, 0.0, 1.0);
            }
            float _e107 = own;
            float _e108 = blurStyle(style_1, _e87, _e107);
            boxCov = _e108;
        } else {
            if (kind_2 == KIND_RRECT) {
                float _e118 = rrectDistance(p_13 - in.a.xy, in.a.zw, in.arad1_, in.arad2_);
                d = _e118;
                if (hasB) {
                    float _e119 = d;
                    float _e127 = rrectDistance(p_13 - in.b.xy, in.b.zw, in.brad1_, in.brad2_);
                    d = metal::max(_e119, -(_e127));
                }
            } else {
                if (kind_2 == KIND_OUTLINE) {
                    float _e139 = rrectDistance(p_13 - in.a.xy, in.a.zw, in.arad1_, in.arad2_);
                    d = metal::abs(_e139) - in.param.x;
                } else {
                    if (kind_2 == KIND_SUPER) {
                        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) {
                            float _e166 = segmentDistance(p_13, in.a.xy, in.a.zw, in.param.x, in.param.y);
                            d = _e166;
                        }
                    }
                }
            }
        }
    }
    float _e167 = d;
    float _e168 = metal::dfdx(_e167);
    float _e169 = d;
    float _e170 = metal::dfdy(_e169);
    float g_1 = metal::length(metal::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) != 0) {
                    float _e199 = d;
                    pixels = _e199 * in.param.w;
                } else {
                    if (g_1 > 0.000000001) {
                        float _e205 = d;
                        pixels = _e205 / g_1;
                    } else {
                        float _e207 = d;
                        pixels = (_e207 <= 0.0) ? -1.0 : 1.0;
                    }
                }
                float _e213 = pixels;
                coverage = metal::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) {
        metal::discard_fragment();
    }
    metal::float4 _e237 = paintSource(in.color, dev_7, paint, paintImage);
    metal::float4 _e238 = finish(_e237, total, dev_7, paint, dst, bayer);
    return fs_mainOutput { _e238 };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}, MetalIndex: 1},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 2},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 0},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}, MetalIndex: 1},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}, MetalIndex: 2},
		}},
	},
}

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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Instance {
    metal::float4 rect;
    metal::float4 color;
};
struct VertexOutput {
    metal::float4 position;
    metal::float4 color;
};
struct type_3 {
    metal::float2 inner[6];
};

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

struct vs_mainInput {
    metal::float4 rect [[attribute(0)]];
    metal::float4 color [[attribute(1)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float4 color [[user(loc0), center_perspective]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, uint index [[vertex_id]]
, constant Frame& frame [[buffer(0)]]
) {
    const Instance instance = { varyings.rect, varyings.color };
    type_3 corners = type_3 {{metal::float2(0.0, 0.0), metal::float2(1.0, 0.0), metal::float2(0.0, 1.0), metal::float2(1.0, 0.0), metal::float2(1.0, 1.0), metal::float2(0.0, 1.0)}};
    VertexOutput out = {};
    metal::float2 corner = corners.inner[index];
    metal::float4 _e32 = toClip(instance.rect.xy + (corner * instance.rect.zw), frame);
    out.position = _e32;
    out.color = instance.color;
    VertexOutput _e35 = out;
    const auto _tmp = _e35;
    return vs_mainOutput { _tmp.position, _tmp.color };
}


struct fs_mainInput {
    metal::float4 color [[user(loc0), center_perspective]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
) {
    const VertexOutput in = { position, varyings_1.color };
    return fs_mainOutput { in.color };
}
`,
	Entries: []EntryPoint{
		{Name: "vs_main", Stage: ShaderStageVertex, ReadsVertexIndex: true, Bindings: []ShaderBinding{
			{Name: "frame", Group: 0, Binding: 0, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 0}, MetalIndex: 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;
}
`,
	MSL: `// language: metal2.4
#include <metal_stdlib>
#include <simd/simd.h>

using metal::uint;

struct type_4 {
    metal::float4 inner[5];
};
struct type_5 {
    metal::float4 inner[32];
};
struct type_6 {
    metal::float4 inner[8];
};
struct Paint {
    metal::float4 shader;
    metal::float4 xf0_;
    metal::float4 xf1_;
    metal::float4 p0_;
    metal::float4 p1_;
    metal::float4 imageInfo;
    metal::float4 constColor;
    metal::float4 flags;
    metal::float4 blend;
    metal::float4 filterColor;
    type_4 filterMatrix;
    type_5 rampColor;
    type_6 rampPos;
};
struct type_11 {
    float inner[64];
};
struct Frame {
    metal::float2 surface;
    metal::float2 origin;
};
struct Draw {
    metal::float4 clipMask;
    metal::float4 paint;
    metal::float4 clip;
    metal::float4 mode;
};
struct VertexInput {
    metal::float2 pos;
    char _pad1[8];
    metal::float4 color;
    metal::float2 tex;
    char _pad3[8];
};
struct VertexOutput {
    metal::float4 position;
    metal::float2 dev;
    char _pad2[8];
    metal::float4 color;
    metal::float2 tex;
    char _pad4[8];
};
constant int SHADER_NONE = 0;
constant int SHADER_LINEAR = 1;
constant int SHADER_RADIAL = 2;
constant int SHADER_CONICAL = 3;
constant int SHADER_SWEEP = 4;
constant int SHADER_IMAGE = 5;
constant int SHADER_CONST = 6;
constant int MAX_STOPS = 32;
constant float PI = 3.1415927;

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 body = {};
    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 = metal::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: {
            float _e5 = hardLightChannel(d_2, s_2, da_2, sa_2);
            return _e5;
        }
        case 16: {
            return (s_2 + d_2) - metal::max(s_2 * da_2, d_2 * sa_2);
        }
        case 17: {
            return (s_2 + d_2) - metal::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 * metal::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 - metal::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: {
            float _e63 = hardLightChannel(s_2, d_2, sa_2, da_2);
            return _e63;
        }
        case 21: {
            float _e64 = softLightChannel(s_2, d_2, sa_2, da_2);
            return _e64;
        }
        case 22: {
            return (s_2 + d_2) - (2.0 * metal::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(
    metal::float3 c
) {
    return ((0.3 * c.x) + (0.59 * c.y)) + (0.11 * c.z);
}

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

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

metal::float3 setLum(
    metal::float3 c_3,
    float l
) {
    float _e2 = lum(c_3);
    return c_3 + metal::float3(l - _e2);
}

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

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

metal::float4 blendPair(
    int mode_2,
    metal::float4 s_5,
    metal::float4 d_4
) {
    float sa_4 = s_5.w;
    float da_4 = d_4.w;
    switch(mode_2) {
        case 0: {
            return metal::float4(0.0);
        }
        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 metal::min(s_5 + d_4, metal::float4(1.0));
        }
        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: {
            metal::float4 _e48 = blendNonSeparable(mode_2, s_5, d_4);
            return _e48;
        }
        default: {
            float _e51 = blendChannel(mode_2, s_5.x, d_4.x, sa_4, da_4);
            float _e54 = blendChannel(mode_2, s_5.y, d_4.y, sa_4, da_4);
            float _e57 = blendChannel(mode_2, s_5.z, d_4.z, sa_4, da_4);
            return metal::float4(_e51, _e54, _e57, (sa_4 + da_4) - (sa_4 * da_4));
        }
    }
}

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

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

int naga_f2i32(float value) {
    return static_cast<int>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

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

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

metal::float4 conicalAt(
    metal::float2 q,
    constant Paint& paint
) {
    float t_2 = {};
    float t1_ = {};
    float t2_ = {};
    metal::float4 _e3 = paint.p0_;
    metal::float2 c0_1 = _e3.xy;
    float r0_ = paint.p0_.z;
    metal::float4 _e11 = paint.p1_;
    metal::float2 cd = _e11.xy - c0_1;
    float _e17 = paint.p1_.z;
    float dr = _e17 - r0_;
    float a_2 = metal::dot(cd, cd) - (dr * dr);
    metal::float2 p_3 = q - c0_1;
    float b = metal::dot(p_3, cd) + (r0_ * dr);
    float c_11 = metal::dot(p_3, p_3) - (r0_ * r0_);
    if (metal::abs(a_2) < 0.000000000001) {
        if (b == 0.0) {
            return metal::float4(0.0);
        }
        t_2 = c_11 / (2.0 * b);
        float _e40 = t_2;
        if ((r0_ + (_e40 * dr)) < 0.0) {
            return metal::float4(0.0);
        }
    } else {
        float disc = (b * b) - (a_2 * c_11);
        if (disc < 0.0) {
            return metal::float4(0.0);
        }
        float sq = metal::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 metal::float4(0.0);
            }
        }
    }
    float _e80 = t_2;
    metal::float4 _e81 = gradientAt(_e80, paint);
    return _e81;
}

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

metal::float4 shaderTexel(
    metal::int2 p,
    metal::int2 size,
    int level,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    int x = {};
    int y = {};
    bool local_2 = {};
    bool local_3 = {};
    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 == 1) {
        int _e23 = x;
        x = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e23, size.x)) + as_type<uint>(size.x)), size.x);
    } else {
        if (tileX == 2) {
            int _e32 = x;
            int m_1 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e32, as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.x)));
            if (m_1 >= size.x) {
                x = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.x))) - as_type<uint>(1))) - as_type<uint>(m_1));
            } else {
                x = m_1;
            }
        } else {
            if (tileX == 3) {
                int _e55 = x;
                if (!((_e55 < 0))) {
                    int _e61 = x;
                    local_2 = _e61 >= size.x;
                } else {
                    local_2 = true;
                }
                bool _e65 = local_2;
                if (_e65) {
                    return metal::float4(0.0);
                }
            } else {
                int _e68 = x;
                x = metal::clamp(_e68, 0, as_type<int>(as_type<uint>(size.x) - as_type<uint>(1)));
            }
        }
    }
    if (tileY == 1) {
        int _e76 = y;
        y = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e76, size.y)) + as_type<uint>(size.y)), size.y);
    } else {
        if (tileY == 2) {
            int _e85 = y;
            int m_2 = naga_mod(as_type<int>(as_type<uint>(naga_mod(_e85, as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))) + as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)))), as_type<int>(as_type<uint>(2) * as_type<uint>(size.y)));
            if (m_2 >= size.y) {
                y = as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(2) * as_type<uint>(size.y))) - as_type<uint>(1))) - as_type<uint>(m_2));
            } else {
                y = m_2;
            }
        } else {
            if (tileY == 3) {
                int _e108 = y;
                if (!((_e108 < 0))) {
                    int _e114 = y;
                    local_3 = _e114 >= size.y;
                } else {
                    local_3 = true;
                }
                bool _e118 = local_3;
                if (_e118) {
                    return metal::float4(0.0);
                }
            } else {
                int _e121 = y;
                y = metal::clamp(_e121, 0, as_type<int>(as_type<uint>(size.y) - as_type<uint>(1)));
            }
        }
    }
    int _e128 = x;
    int _e129 = y;
    metal::float4 _e131 = paintImage.read(metal::uint2(metal::int2(_e128, _e129)), level);
    return _e131;
}

float mitchellWeight(
    float x0_
) {
    float x_1 = metal::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;
}

metal::int2 naga_f2i32(metal::float2 value) {
    return static_cast<metal::int2>(metal::clamp(value, -2147483600.0, 2147483500.0));
}

metal::float4 imageShaderAt(
    metal::float2 q_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 sum = metal::float4(0.0);
    int j = -1;
    int i_2 = {};
    metal::int2 base_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(0), paintImage.get_height(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);
    metal::int2 size_1 = static_cast<metal::int2>(metal::uint2(paintImage.get_width(level_1), paintImage.get_height(level_1)));
    metal::float2 uv = (q_1 * static_cast<metal::float2>(size_1)) / static_cast<metal::float2>(base_1);
    if (quality == 0) {
        metal::float4 _e30 = shaderTexel(naga_f2i32(metal::floor(uv)), size_1, level_1, paint, paintImage);
        return _e30;
    }
    metal::float2 f = uv - metal::float2(0.5);
    metal::float2 f0_ = metal::floor(f);
    metal::float2 t_3 = f - f0_;
    metal::int2 i0_ = naga_f2i32(f0_);
    if (quality == 3) {
        uint2 loop_bound_1 = uint2(4294967295u);
        bool loop_init = true;
        while(true) {
            if (metal::all(loop_bound_1 == uint2(0u))) { break; }
            loop_bound_1 -= uint2(loop_bound_1.y == 0u, 1u);
            if (!loop_init) {
                int _e75 = j;
                j = as_type<int>(as_type<uint>(_e75) + as_type<uint>(1));
            }
            loop_init = false;
            int _e44 = j;
            if (_e44 <= 2) {
            } else {
                break;
            }
            {
                int _e47 = j;
                float _e51 = mitchellWeight(static_cast<float>(_e47) - t_3.y);
                i_2 = -1;
                uint2 loop_bound_2 = uint2(4294967295u);
                bool loop_init_1 = true;
                while(true) {
                    if (metal::all(loop_bound_2 == uint2(0u))) { break; }
                    loop_bound_2 -= uint2(loop_bound_2.y == 0u, 1u);
                    if (!loop_init_1) {
                        int _e72 = i_2;
                        i_2 = as_type<int>(as_type<uint>(_e72) + as_type<uint>(1));
                    }
                    loop_init_1 = false;
                    int _e54 = i_2;
                    if (_e54 <= 2) {
                    } else {
                        break;
                    }
                    {
                        metal::float4 _e57 = sum;
                        int _e58 = i_2;
                        float _e62 = mitchellWeight(static_cast<float>(_e58) - t_3.x);
                        int _e64 = i_2;
                        int _e65 = j;
                        metal::float4 _e68 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(_e64, _e65))), size_1, level_1, paint, paintImage);
                        sum = _e57 + ((_e51 * _e62) * _e68);
                    }
                }
            }
        }
        float _e78 = sum.w;
        float a_3 = metal::clamp(_e78, 0.0, 1.0);
        metal::float4 _e82 = sum;
        return metal::float4(metal::clamp(_e82.xyz, metal::float3(0.0), metal::float3(a_3)), a_3);
    }
    metal::float4 _e89 = shaderTexel(i0_, size_1, level_1, paint, paintImage);
    metal::float4 _e94 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 0))), size_1, level_1, paint, paintImage);
    metal::float4 _e99 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(0, 1))), size_1, level_1, paint, paintImage);
    metal::float4 _e104 = shaderTexel(as_type<metal::int2>(as_type<metal::uint2>(i0_) + as_type<metal::uint2>(metal::int2(1, 1))), size_1, level_1, paint, paintImage);
    return metal::mix(metal::mix(_e89, _e94, t_3.x), metal::mix(_e99, _e104, t_3.x), t_3.y);
}

metal::float4 shaderColor(
    metal::float2 p_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    float a_1 = {};
    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;
    metal::float2 q_2 = metal::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) {
        metal::float4 _e49 = paint.p0_;
        metal::float4 _e54 = paint.p0_;
        float _e60 = paint.p1_.x;
        metal::float4 _e62 = gradientAt(metal::dot(q_2 - _e49.xy, _e54.zw) * _e60, paint);
        return _e62;
    }
    if (kind == SHADER_RADIAL) {
        metal::float4 _e67 = paint.p0_;
        float _e74 = paint.p0_.z;
        metal::float4 _e76 = gradientAt(metal::length(q_2 - _e67.xy) / _e74, paint);
        return _e76;
    }
    if (kind == SHADER_CONICAL) {
        metal::float4 _e79 = conicalAt(q_2, paint);
        return _e79;
    }
    if (kind == SHADER_SWEEP) {
        metal::float4 _e84 = paint.p0_;
        metal::float2 d_5 = q_2 - _e84.xy;
        float _e93 = paint.p0_.z;
        a_1 = metal::atan2(d_5.y, d_5.x) - _e93;
        float _e96 = a_1;
        float _e98 = a_1;
        a_1 = _e96 - (6.2831855 * metal::floor(_e98 / 6.2831855));
        float _e104 = a_1;
        float _e108 = paint.p0_.w;
        metal::float4 _e110 = gradientAt(_e104 / _e108, paint);
        return _e110;
    }
    if (kind == SHADER_IMAGE) {
        metal::float4 _e113 = imageShaderAt(q_2, paint, paintImage);
        return _e113;
    }
    metal::float4 _e116 = paint.constColor;
    return _e116;
}

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

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

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

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

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

metal::float4 paintSource(
    metal::float4 base,
    metal::float2 p_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> paintImage
) {
    metal::float4 c_9 = {};
    c_9 = base;
    float _e6 = paint.shader.x;
    if (naga_f2i32(_e6 + 0.5) != SHADER_NONE) {
        metal::float4 _e12 = shaderColor(p_2, paint, paintImage);
        c_9 = _e12 * base;
    }
    metal::float4 _e14 = c_9;
    metal::float4 _e15 = applyFilters(_e14, paint);
    return _e15;
}

float ditherAt(
    metal::float2 dev,
    thread type_11& bayer
) {
    metal::int2 p_4 = naga_f2i32(metal::floor(dev)) & metal::int2(7);
    float _e13 = bayer.inner[as_type<int>(as_type<uint>(as_type<int>(as_type<uint>(p_4.y) * as_type<uint>(8))) + as_type<uint>(p_4.x))];
    return (((_e13 + 0.5) / 64.0) - 0.5) / 255.0;
}

metal::float4 destinationAt(
    metal::float2 dev_1,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst
) {
    metal::float4 _e6 = paint.blend;
    metal::float4 _e11 = dst.read(metal::uint2(as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_1))) - as_type<metal::uint2>(naga_f2i32(_e6.yz)))), 0);
    return _e11;
}

metal::float4 finish(
    metal::float4 src,
    float cov,
    metal::float2 dev_2,
    constant Paint& paint,
    metal::texture2d<float, metal::access::sample> dst,
    thread type_11& bayer
) {
    metal::float4 o = {};
    bool local_4 = {};
    float _e7 = paint.blend.x;
    if (_e7 < 0.0) {
        o = src * cov;
    } else {
        metal::float4 _e11 = destinationAt(dev_2, paint, dst);
        float _e15 = paint.blend.x;
        metal::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) {
        metal::float4 _e36 = o;
        float _e37 = ditherAt(dev_2, bayer);
        o = _e36 + metal::float4(_e37);
    }
    metal::float4 _e40 = o;
    return _e40;
}

metal::float4 toClip(
    metal::float2 dev_3,
    constant Frame& frame
) {
    metal::float2 _e3 = frame.origin;
    metal::float2 p_5 = dev_3 - _e3;
    float _e9 = frame.surface.x;
    float _e19 = frame.surface.y;
    return metal::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 metal::clamp(metal::min(c_10 + 0.5, hi) - metal::max(c_10 - 0.5, lo), 0.0, 1.0);
}

float clipMaskCoverage(
    metal::float2 dev_4,
    constant Draw& draw,
    metal::texture2d<float, metal::access::sample> clipMask
) {
    bool local_5 = {};
    bool local_6 = {};
    bool local_7 = {};
    float _e4 = draw.clipMask.z;
    if (_e4 <= 0.0) {
        return 1.0;
    }
    metal::float4 _e12 = draw.clipMask;
    metal::int2 p_6 = as_type<metal::int2>(as_type<metal::uint2>(naga_f2i32(metal::floor(dev_4))) - as_type<metal::uint2>(naga_f2i32(_e12.xy)));
    if (!((p_6.x < 0))) {
        local_5 = p_6.y < 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;
    }
    metal::float4 _e54 = clipMask.read(metal::uint2(p_6), 0);
    return _e54.x;
}

struct vs_mainInput {
    metal::float2 pos [[attribute(0)]];
    metal::float4 color_1 [[attribute(1)]];
    metal::float2 tex [[attribute(2)]];
};
struct vs_mainOutput {
    metal::float4 position [[position]];
    metal::float2 dev [[user(loc0), center_perspective]];
    metal::float4 color [[user(loc1), center_perspective]];
    metal::float2 tex [[user(loc2), center_perspective]];
};
vertex vs_mainOutput vs_main(
  vs_mainInput varyings [[stage_in]]
, constant Frame& frame [[buffer(0)]]
) {
    const VertexInput v = { varyings.pos, {}, varyings.color_1, varyings.tex };
    VertexOutput out = {};
    metal::float4 _e4 = toClip(v.pos, frame);
    out.position = _e4;
    out.dev = v.pos;
    out.color = v.color;
    out.tex = v.tex;
    VertexOutput _e11 = out;
    const auto _tmp = _e11;
    return vs_mainOutput { _tmp.position, _tmp.dev, _tmp.color, _tmp.tex };
}


struct fs_mainInput {
    metal::float2 dev_5 [[user(loc0), center_perspective]];
    metal::float4 color_1 [[user(loc1), center_perspective]];
    metal::float2 tex [[user(loc2), center_perspective]];
};
struct fs_mainOutput {
    metal::float4 member_1 [[color(0)]];
};
fragment fs_mainOutput fs_main(
  fs_mainInput varyings_1 [[stage_in]]
, metal::float4 position [[position]]
, constant Paint& paint [[buffer(2)]]
, metal::texture2d<float, metal::access::sample> paintImage [[texture(1)]]
, metal::texture2d<float, metal::access::sample> dst [[texture(2)]]
, constant Draw& draw [[buffer(1)]]
, metal::texture2d<float, metal::access::sample> clipMask [[texture(0)]]
) {
    type_11 bayer = type_11 {{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}};
    const VertexOutput in = { position, varyings_1.dev_5, {}, varyings_1.color_1, varyings_1.tex };
    metal::float4 color = {};
    metal::float2 at = {};
    metal::float2 dev_5 = metal::floor(in.dev) + metal::float2(0.5);
    metal::float4 _e8 = draw.paint;
    color = _e8;
    float _e13 = paint.shader.x;
    if (naga_f2i32(_e13 + 0.5) != SHADER_NONE) {
        at = dev_5;
        float _e23 = draw.mode.z;
        if (_e23 != 0.0) {
            at = in.tex;
        }
        metal::float2 _e27 = at;
        metal::float4 _e28 = shaderColor(_e27, paint, paintImage);
        metal::float4 _e31 = draw.paint;
        color = _e28 * _e31;
    }
    float _e36 = draw.mode.y;
    if (_e36 != 0.0) {
        float _e42 = draw.mode.x;
        metal::float4 _e46 = color;
        metal::float4 _e55 = blendPair(naga_f2i32(_e42 + 0.5), _e46, metal::float4(in.color.xyz * in.color.w, in.color.w));
        color = _e55;
    }
    metal::float4 _e56 = color;
    metal::float4 _e57 = applyFilters(_e56, paint);
    color = _e57;
    float _e62 = draw.clip.x;
    float _e66 = draw.clip.z;
    float _e67 = span(dev_5.x, _e62, _e66);
    float _e72 = draw.clip.y;
    float _e76 = draw.clip.w;
    float _e77 = span(dev_5.y, _e72, _e76);
    float _e79 = clipMaskCoverage(dev_5, draw, clipMask);
    float coverage = (_e67 * _e77) * _e79;
    metal::float4 _e81 = color;
    metal::float4 _e82 = finish(_e81, coverage, dev_5, paint, dst, bayer);
    return fs_mainOutput { _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}, MetalIndex: 0},
		}},
		{Name: "fs_main", Stage: ShaderStageFragment, Bindings: []ShaderBinding{
			{Name: "draw", Group: 0, Binding: 1, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 1}, MetalIndex: 1},
			{Name: "paint", Group: 0, Binding: 2, Kind: BindingUniformBuffer, Register: ShaderRegister{Class: "b", Space: 0, Index: 2}, MetalIndex: 2},
			{Name: "clipMask", Group: 1, Binding: 0, Kind: BindingSampledTexture, Register: ShaderRegister{Class: "t", Space: 1, Index: 0}, MetalIndex: 0},
			{Name: "paintImage", Group: 3, Binding: 0, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 0}, MetalIndex: 1},
			{Name: "dst", Group: 3, Binding: 1, Kind: BindingSampledTexture, Sample: SampleTypeUnfilterableFloat, Register: ShaderRegister{Class: "t", Space: 3, Index: 1}, MetalIndex: 2},
		}},
	},
}

ShaderVertices is shaders/vertices.wgsl.

Flutter: no counterpart

Functions

func CheckFirstIndices

func CheckFirstIndices(e EntryPoint, firstVertex, baseVertex, firstInstance int)

CheckFirstIndices panics with GPUFirstInstanceRead in debug builds when a draw has a first instance, a first vertex or a base vertex other than 0 and the vertex entry point reads instance_index or vertex_index: Direct3D 12's SV_InstanceID and SV_VertexID leave them out, so the rule holds on every backend, to show on Linux too (SPEC §27.1). Backends and the fake device call it from Draw and DrawIndexed.

Flutter: no counterpart

func Float16Bits

func Float16Bits(f float32) uint16

Float16Bits converts a float32 to the bits of an IEEE 754 half float, rounding to nearest even: the bytes that an RGBA16Float texture holds for a value. Values too large become infinity, and the smallest ones subnormal or zero.

Flutter: no counterpart

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 MipLevelsOf

func MipLevelsOf(width, height int) int

MipLevelsOf returns the number of levels of a full mip chain for a texture of the size: 1 plus the base-2 logarithm of the largest side, rounded down.

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 or a storage buffer; Size 0 means up to the end.
	// A uniform binding gets its offset from SetBindGroup too; a storage binding's Offset is a multiple of
	// Limits.StorageOffsetAlignment.
	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, or a storage texture (TextureStorage) for a storage binding.
	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>`, or a texture_2d_array, texture_cube or
	// texture_3d of f32 or u32 (ShaderBinding.Dimension and Sample).
	//
	// Flutter: no counterpart
	BindingSampledTexture
	// BindingSampler is a `sampler`.
	//
	// Flutter: no counterpart
	BindingSampler
	// BindingStorageBufferReadOnly is a `var<storage, read>`, bound at an offset that is a multiple
	// of Limits.StorageOffsetAlignment. Any stage may read it.
	//
	// Flutter: no counterpart
	BindingStorageBufferReadOnly
	// BindingStorageBuffer is a `var<storage, read_write>`: a compute entry point only.
	//
	// Flutter: no counterpart
	BindingStorageBuffer
	// BindingStorageTexture is a `texture_storage_2d<format, write>` (or _2d_array or _3d, with
	// ShaderBinding.StorageFormat): a compute entry point only.
	//
	// Flutter: no counterpart
	BindingStorageTexture
)

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

func (BindingKind) String

func (k BindingKind) String() string

String returns "uniform", "texture", "sampler", "storage-read", "storage" or "storage-texture".

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
	// Usage is what the buffer was made for.
	Usage() BufferUsage
	// Destroy releases the buffer. The GPU finishes the frames that use it first.
	Destroy()
}

Buffer is a GPU buffer.

Flutter: no counterpart

type BufferLayout

type BufferLayout struct {
	// Offset is where the first row starts, a multiple of 4 and of the bytes of a pixel.
	Offset int
	// BytesPerRow is the distance between two rows, a multiple of Limits.CopyRowPitchAlignment
	// (Limits.AlignedRowPitch gives one).
	BytesPerRow int
	// RowsPerImage is the number of rows of one layer or slice; the distance between two layers or
	// slices is BytesPerRow × RowsPerImage. It is at least the height of the box.
	RowsPerImage int
}

BufferLayout is how the rows of a texture box lie in a buffer, for the copies between a buffer and a texture.

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 or 32 bit indices (SetIndexBuffer, SetIndexBuffer32).
	//
	// Flutter: no counterpart
	BufferIndex
	// BufferUniform is a buffer that uniform bindings read, with dynamic offsets.
	//
	// Flutter: no counterpart
	BufferUniform
	// BufferStorage is a buffer that storage bindings read (var<storage, read>) or, in a compute
	// shader, write (var<storage, read_write>), bound at a Binding.Offset that is a multiple of
	// Limits.StorageOffsetAlignment.
	//
	// Flutter: no counterpart
	BufferStorage
	// BufferIndirect is a buffer of the arguments of DrawIndirect, DrawIndexedIndirect and
	// DispatchIndirect, which a shader may have written (with BufferStorage).
	//
	// Flutter: no counterpart
	BufferIndirect
	// BufferCopySource is a buffer that the copies and ReadBuffer read.
	//
	// Flutter: no counterpart
	BufferCopySource
	// BufferCopyDestination is a buffer that the copies write.
	//
	// Flutter: no counterpart
	BufferCopyDestination
	// BufferQueryResolve is a buffer that ResolveQuerySet writes.
	//
	// Flutter: no counterpart
	BufferQueryResolve
)

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

type ColorAttachment

type ColorAttachment struct {
	// Target is the texture drawn into: a 2D texture, or a view of one layer or level made by
	// Layer or Level. It has TextureRenderTarget.
	Target Texture
	// Resolve, when set, is a texture of one sample, of the size and format of Target, that
	// receives the average of the samples at the end of the pass; Target is multisampled then.
	Resolve Texture
	// Load is what happens to the contents of Target at the start.
	Load LoadOp
	// Store is what happens to the contents of Target at the end. With Resolve, StoreDiscard
	// keeps only the resolved pixels.
	Store StoreOp
	// Clear is the color of LoadClear, converted to the type of the format (the integers of a
	// Uint format are the whole numbers).
	Clear [4]float64
}

ColorAttachment is one color attachment of a render pass.

Flutter: no counterpart

type ColorTargetState

type ColorTargetState struct {
	// Format is the format of the attachment.
	Format TextureFormat
	// Blend is the blend state of the attachment.
	Blend BlendState
	// WriteMask says which channels are written.
	WriteMask ColorWriteMask
}

ColorTargetState is the format, blend state and write mask of one color attachment of a pipeline.

Flutter: no counterpart

type ColorWriteMask

type ColorWriteMask int

ColorWriteMask says which channels of a color attachment a pipeline writes; values are combined with |. The zero value is ColorWriteAll.

Flutter: no counterpart

const (
	// ColorWriteAll writes every channel; it is the zero value.
	//
	// Flutter: no counterpart
	ColorWriteAll ColorWriteMask = 0
	// ColorWriteRed writes the red channel.
	//
	// Flutter: no counterpart
	ColorWriteRed ColorWriteMask = 1 << (iota - 1)
	// ColorWriteGreen writes the green channel.
	//
	// Flutter: no counterpart
	ColorWriteGreen
	// ColorWriteBlue writes the blue channel.
	//
	// Flutter: no counterpart
	ColorWriteBlue
	// ColorWriteAlpha writes the alpha channel.
	//
	// Flutter: no counterpart
	ColorWriteAlpha
	// ColorWriteNone writes no channel.
	//
	// Flutter: no counterpart
	ColorWriteNone
)

The channels.

func (ColorWriteMask) Bits

func (m ColorWriteMask) Bits() uint32

Bits returns the channels as the bits 1 (red), 2 (green), 4 (blue) and 8 (alpha) that every backend uses: all of them for ColorWriteAll, none for ColorWriteNone.

Flutter: no counterpart

type CompareFunc

type CompareFunc int

CompareFunc is the comparison of a depth or stencil test: the new value against the stored one. The zero value is CompareUnspecified, which means CompareAlways.

Flutter: no counterpart

const (
	// CompareUnspecified is the zero value and means CompareAlways.
	//
	// Flutter: no counterpart
	CompareUnspecified CompareFunc = iota
	// CompareNever never passes.
	//
	// Flutter: no counterpart
	CompareNever
	// CompareLess passes when the new value is less.
	//
	// Flutter: no counterpart
	CompareLess
	// CompareEqual passes when the values are equal.
	//
	// Flutter: no counterpart
	CompareEqual
	// CompareLessEqual passes when the new value is less or equal.
	//
	// Flutter: no counterpart
	CompareLessEqual
	// CompareGreater passes when the new value is greater: reversed-Z.
	//
	// Flutter: no counterpart
	CompareGreater
	// CompareNotEqual passes when the values differ.
	//
	// Flutter: no counterpart
	CompareNotEqual
	// CompareGreaterEqual passes when the new value is greater or equal.
	//
	// Flutter: no counterpart
	CompareGreaterEqual
	// CompareAlways always passes.
	//
	// Flutter: no counterpart
	CompareAlways
)

The comparison functions.

type ComputePass

type ComputePass interface {
	// SetPipeline chooses the pipeline of the next dispatches.
	SetPipeline(ComputePipeline)
	// SetBindGroup binds the group number group, with one dynamic offset for each uniform buffer in
	// it, in binding order (a storage buffer has none: its offset is that of the bind group).
	SetBindGroup(group int, g BindGroup, dynamicOffsets ...uint32)
	// Dispatch runs x × y × z workgroups; each is at most Limits.MaxComputeWorkgroupsPerDimension.
	Dispatch(x, y, z int)
	// DispatchIndirect runs the workgroups whose counts are in b at offset: three 32 bit unsigned
	// integers. b has BufferIndirect; offset is a multiple of 4.
	DispatchIndirect(b Buffer, offset int)
	// End finishes the pass.
	End()
}

ComputePass records the dispatches of one compute pass.

Flutter: no counterpart

type ComputePassDesc

type ComputePassDesc struct {
	// Timestamps, when set, is where the pass writes its timestamps; it needs Features.TimestampQuery.
	Timestamps *PassTimestamps
}

ComputePassDesc describes a compute pass.

Flutter: no counterpart

type ComputePipeline

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

ComputePipeline is a GPU compute pipeline.

Flutter: no counterpart

type ComputePipelineDesc

type ComputePipelineDesc struct {
	// Shader is a generated shader.
	Shader *Shader
	// Entry is the name of its compute entry point.
	Entry string
}

ComputePipelineDesc describes a compute pipeline: the compute entry point of a generated shader.

Flutter: no counterpart

type CullMode

type CullMode int

CullMode says which triangles are not drawn. The zero value is CullNone.

Flutter: no counterpart

const (
	// CullNone draws every triangle.
	//
	// Flutter: no counterpart
	CullNone CullMode = iota
	// CullFront leaves out the triangles that face the viewer.
	//
	// Flutter: no counterpart
	CullFront
	// CullBack leaves out the triangles that face away from the viewer.
	//
	// Flutter: no counterpart
	CullBack
)

The cull modes.

type DepthAttachment

type DepthAttachment struct {
	// Target is the texture drawn into, of a depth format, with TextureRenderTarget.
	Target Texture
	// DepthLoad and DepthStore are what happens to the depth at the start and at the end.
	DepthLoad  LoadOp
	DepthStore StoreOp
	// ClearDepth is the depth of LoadClear, from 0 to 1.
	ClearDepth float64
	// StencilLoad, StencilStore and ClearStencil are the same for the stencil; they are ignored
	// by a format without one.
	StencilLoad  LoadOp
	StencilStore StoreOp
	ClearStencil uint32
}

DepthAttachment is the depth-stencil attachment of a render pass.

Flutter: no counterpart

type DepthStencilState

type DepthStencilState struct {
	// Format is the format of the depth-stencil attachment of the passes that use the pipeline;
	// TextureFormatUnspecified means the pipeline has none.
	Format TextureFormat
	// DepthWrite stores the depth of the fragments that pass.
	DepthWrite bool
	// DepthCompare is the depth test.
	DepthCompare CompareFunc
	// StencilFront and StencilBack are the stencil tests of the two kinds of faces. A format
	// without a stencil ignores them.
	StencilFront, StencilBack StencilFaceState
	// StencilReadMask and StencilWriteMask are the bits that the test reads and the operations
	// write; 0 means all the bits.
	StencilReadMask, StencilWriteMask uint32
	// DepthBias is added to the depth of each fragment, in units of the smallest difference of
	// the depth format; DepthBiasSlopeScale times the slope of the triangle is added too, and the
	// sum is at most DepthBiasClamp when that is not 0. Only triangles have a bias.
	DepthBias           int32
	DepthBiasSlopeScale float32
	DepthBiasClamp      float32
}

DepthStencilState is the depth and stencil state of a pipeline. The zero value is no depth-stencil attachment.

Flutter: no counterpart

type Device

type Device interface {
	// Limits are the design limits.
	Limits() Limits
	// Features are the optional features of the device.
	Features() Features
	// NewBuffer makes a buffer of size bytes.
	NewBuffer(size int, usage BufferUsage) (Buffer, error)
	// NewTexture makes a 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)
	// NewComputePipeline makes a compute pipeline from the compute entry point of a generated shader.
	NewComputePipeline(desc ComputePipelineDesc) (ComputePipeline, error)
	// NewBindGroup makes the resources of the bind group number group of the
	// pipeline.
	NewBindGroup(p Pipeline, group int, entries []Binding) (BindGroup, error)
	// NewComputeBindGroup makes the resources of the bind group number group of the compute
	// pipeline.
	NewComputeBindGroup(p ComputePipeline, group int, entries []Binding) (BindGroup, error)
	// NewQuerySet makes a set of count timestamps; it fails with GPUQueryUnsupported when
	// Features.TimestampQuery is false.
	NewQuerySet(count int) (QuerySet, 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)
	// WriteTextureRegion copies data into a box of a 2D array, cube or 3D texture (or a 2D texture,
	// with one layer): the rows of data are bytesPerRow apart and the images (the layers or slices)
	// rowsPerImage rows apart. The data is copied before the call returns.
	WriteTextureRegion(t Texture, region TextureRegion, data []byte, bytesPerRow, rowsPerImage int)
	// NewSwapchain makes the swapchain of a window.
	NewSwapchain(h gpu.SurfaceHandle) (Swapchain, error)
	// Begin starts an encoder: the commands of one submission. A frame may have several, one after
	// the other (each ends with Submit before the next Begin); the GPU runs them in order on the
	// device's one queue. The 2D renderer's frame is one encoder, submitted last.
	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 DeviceEvent

type DeviceEvent struct {
	// Kind says what happened.
	Kind DeviceEventKind
	// Device is the device that was opened, lost or closed.
	Device Device
	// Err is why the device was lost, for DeviceLost; nil otherwise.
	Err error
}

DeviceEvent is a change of the device of a renderer.

Flutter: no counterpart

type DeviceEventKind

type DeviceEventKind int

DeviceEventKind says what happened to the device of a renderer.

Flutter: no counterpart

const (
	// DeviceOpened is the first frame, or the first one after a loss: the renderer has a device.
	//
	// Flutter: no counterpart
	DeviceOpened DeviceEventKind = iota + 1
	// DeviceLost is the loss of the device (a driver reset, a destroyed WebGPU device): the renderer is
	// about to release it and makes a new one at its next frame (SPEC §9.4, §9.5). It is sent before the
	// device is closed, so that what holds resources made with it can let them go.
	//
	// Flutter: no counterpart
	DeviceLost
	// DeviceClosed is the end of the renderer (the app ends, or the engine goes on to another renderer):
	// the device is about to be closed and no other follows from this renderer.
	//
	// Flutter: no counterpart
	DeviceClosed
	// HostEnded is the end of the app: the host sends it after the renderer is closed, with no device. It
	// is the moment when what package gpu still holds is a leak.
	//
	// Flutter: no counterpart
	HostEnded
)

The events of the device of a renderer.

func (DeviceEventKind) String

func (k DeviceEventKind) String() string

String returns "opened", "lost", "closed" or "ended".

type DeviceHost

type DeviceHost interface {
	// GPUDevice returns the device of the renderer when state is HostReady.
	GPUDevice() (dev Device, state HostState)
	// WatchGPUDevice calls fn for each change of the device from now on, on the UI thread, inside the frame
	// that caused it. It returns the function that stops the calls.
	WatchGPUDevice(fn func(DeviceEvent)) (stop func())
	// GPULogger is where package gpu logs what it finds out (a resource that was not released).
	GPULogger() *slog.Logger
	// GPUTextures is the registry of the textures the windows show (SPEC §27.4); the renderer draws from it.
	GPUTextures() *TextureRegistry
}

DeviceHost is what the engine gives to package gpu (SPEC §27.2) through ui.PlatformDispatcher.HostAs: the device of the renderer it chose, and its changes. Every method runs on the UI thread.

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. It is BeginRenderPass with one color
	// attachment that is stored.
	BeginPass(target Texture, load LoadOp, clear ui.Color) Pass
	// BeginRenderPass starts a pass with the attachments of desc. The previous pass must have
	// ended.
	BeginRenderPass(desc PassDesc) 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))
	// ReadTextureBytes reads the rectangle r of a 2D texture (or a Level or Layer view) as the
	// bytes of its format: the rows of r.Dx() pixels, packed, so that rowBytes is r.Dx() times the
	// bytes of a pixel. It works for every format that CanBeCopied. done runs once the GPU has
	// finished, never inside the frame that asked.
	ReadTextureBytes(src Texture, r image.Rectangle, done func(data []byte, rowBytes int, err error))
	// BeginComputePass starts a compute pass. The previous pass must have ended.
	BeginComputePass(desc ComputePassDesc) ComputePass
	// CopyBuffer copies size bytes of src, from srcOffset, to dst at dstOffset. The buffers have
	// BufferCopySource and BufferCopyDestination; the offsets and the size are multiples of 4.
	CopyBuffer(src Buffer, srcOffset int, dst Buffer, dstOffset, size int)
	// CopyBufferToTexture copies the box region of a level of dst from the rows in src, laid out as
	// layout says. The box is inside the level. Every texture can be the destination but a depth
	// format and a multisampled texture.
	CopyBufferToTexture(src Buffer, layout BufferLayout, dst Texture, region TextureRegion)
	// CopyTextureToBuffer copies the box region of a level of src, a texture with TextureCopySource
	// (or TextureRenderTarget) of a format that CanBeCopied, into dst as layout says.
	CopyTextureToBuffer(src Texture, region TextureRegion, dst Buffer, layout BufferLayout)
	// ReadBuffer reads size bytes of src from offset: done runs once the GPU has finished, never
	// inside the frame that asked, with a copy of the bytes. src has BufferCopySource.
	ReadBuffer(src Buffer, offset, size int, done func(data []byte, err error))
	// ResolveQuerySet writes the count timestamps of set from first, as 64 bit unsigned integers
	// in ticks (Features.TimestampPeriod), to dst at dstOffset, a multiple of
	// Limits.StorageOffsetAlignment. dst has BufferQueryResolve.
	ResolveQuerySet(set QuerySet, first, count int, dst Buffer, dstOffset int)
	// OnComplete runs fn once the GPU has finished the work of this encoder, never inside the frame
	// that asked: Submit runs it before it returns on a device that waits for the GPU, and a device
	// that cannot (WebGPU) runs it later, on the UI thread. It must be called before Submit.
	OnComplete(fn func())
	// Submit sends the encoder's commands 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
	// WorkgroupSize is the @workgroup_size of a compute entry point (the missing dimensions are 1).
	WorkgroupSize [3]int
	// ReadsInstanceIndex and ReadsVertexIndex say that a vertex entry point reads
	// @builtin(instance_index) or @builtin(vertex_index): Direct3D 12's SV_InstanceID and
	// SV_VertexID leave out the first instance and the first vertex of a draw, so such a shader is
	// drawn with a first instance, a first vertex and a base vertex of 0 only (SPEC §27.1).
	ReadsInstanceIndex, ReadsVertexIndex bool
}

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

Flutter: no counterpart

type Features

type Features struct {
	// Float32Filterable says that RGBA32Float and R32Float textures can be read with a filtering
	// sampler (WebGPU's float32-filterable).
	Float32Filterable bool
	// TimestampQuery says that query sets can be made and passes can write timestamps into them
	// (WebGPU's timestamp-query, Vulkan's timestampValidBits on the queue, Direct3D 12's timestamp
	// queries).
	TimestampQuery bool
	// TimestampPeriod is the time of one tick of a timestamp, in nanoseconds; 1 on WebGPU, whose
	// timestamps are nanoseconds. It is 0 when TimestampQuery is false.
	TimestampPeriod float64
}

Features are the optional features of a device (SPEC §27.1); each backend reports what it has.

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 FrontFace

type FrontFace int

FrontFace says which winding of a triangle on the screen faces the viewer, in the clip space of SPEC §27.1 (y up). The zero value is FrontFaceCCW.

Flutter: no counterpart

const (
	// FrontFaceCCW is a triangle whose vertices go counter-clockwise on the screen.
	//
	// Flutter: no counterpart
	FrontFaceCCW FrontFace = iota
	// FrontFaceCW is a triangle whose vertices go clockwise on the screen.
	//
	// Flutter: no counterpart
	FrontFaceCW
)

The windings.

type HostState

type HostState int

HostState is whether a host has a device.

Flutter: no counterpart

const (
	// HostPending means that the renderer is one that has a device and has not opened it yet (it does so at
	// its first frame, with the handle of the first window), or has lost it and opens another at its next frame.
	//
	// Flutter: no counterpart
	HostPending HostState = iota
	// HostReady means that GPUDevice returns the device.
	//
	// Flutter: no counterpart
	HostReady
	// HostUnavailable means that the renderer has no device and will not have one: the GLES, WebGL and software
	// renderers.
	//
	// Flutter: no counterpart
	HostUnavailable
)

The states of a DeviceHost.

type IdleWaiter

type IdleWaiter interface {
	// WaitIdle returns when the GPU has nothing left to do for the app.
	WaitIdle()
}

IdleWaiter is implemented by a Device that can wait for the GPU to finish the work that was committed (Metal's, which an iOS app needs before it enters the background).

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
	// MaxTextureSize3D is the largest width, height or depth of a 3D texture, in pixels.
	MaxTextureSize3D int
	// MaxTextureArrayLayers is the largest number of layers of a 2D array texture.
	MaxTextureArrayLayers int
	// MaxColorAttachments is the number of color attachments of a pass and of a pipeline.
	MaxColorAttachments int
	// MaxColorAttachmentBytesPerSample is the budget of the color attachments of a pass, in the
	// bytes of TextureFormat.RenderTargetPixelBytes added up.
	MaxColorAttachmentBytesPerSample int
	// MaxStorageBuffersPerStage and MaxStorageTexturesPerStage are how many storage resources of each
	// kind one shader stage can read or write.
	MaxStorageBuffersPerStage, MaxStorageTexturesPerStage int
	// MaxStorageBindingSize is the largest storage buffer binding, in bytes.
	MaxStorageBindingSize int
	// StorageOffsetAlignment is what the offset of a storage binding is a multiple of, and the offset
	// of ResolveQuerySet.
	StorageOffsetAlignment int
	// MaxBufferSize is the largest buffer, in bytes.
	MaxBufferSize int
	// MaxComputeInvocationsPerWorkgroup is the most threads of a workgroup (the product of the
	// three sizes of @workgroup_size), and MaxComputeWorkgroupSize the most in each dimension.
	MaxComputeInvocationsPerWorkgroup int
	MaxComputeWorkgroupSize           [3]int
	// MaxComputeWorkgroupsPerDimension is the most workgroups a dispatch runs in a dimension.
	MaxComputeWorkgroupsPerDimension 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) CheckBinding

func (l Limits) CheckBinding(sb ShaderBinding, e Binding)

CheckBinding panics with GPUDescriptorInvalid in debug builds when an entry of a bind group does not fit the binding of the shader it is for: a uniform binding needs a buffer with BufferUniform, a storage binding one with BufferStorage at an offset that is a multiple of StorageOffsetAlignment and of a size that MaxStorageBindingSize allows, a texture binding a texture and a sampler binding a sampler, and a storage texture binding a texture with TextureStorage. Backends and the fake device call it from NewBindGroup and NewComputeBindGroup, for each entry whose binding they found.

Flutter: no counterpart

func (Limits) CheckBuffer

func (l Limits) CheckBuffer(size int, usage BufferUsage)

CheckBuffer panics with GPULimitExceeded in debug builds when the buffer is larger than MaxBufferSize, and with GPUDescriptorInvalid when it has no usage. Backends and the fake device call it from NewBuffer.

Flutter: no counterpart

func (Limits) CheckBufferTexture

func (l Limits) CheckBufferTexture(buf Buffer, layout BufferLayout, tex TextureDesc, region TextureRegion, toTexture bool)

CheckBufferTexture panics with GPUDescriptorInvalid in debug builds when a copy between a buffer and a texture breaks a rule: the box is not inside the level, the format cannot be copied (toTexture: cannot be written), the texture is multisampled, the layout is not aligned or does not fit the box, or the rows do not fit the buffer. Backends and the fake device call it from CopyBufferToTexture and CopyTextureToBuffer.

Flutter: no counterpart

func (Limits) CheckComputePipeline

func (l Limits) CheckComputePipeline(desc ComputePipelineDesc)

CheckComputePipeline panics with GPULimitExceeded in debug builds when the compute entry point has a workgroup larger than the limits, or reads more storage resources than a stage may, and with GPUDescriptorInvalid when the shader has no compute entry point of that name. Backends and the fake device call it from NewComputePipeline.

Flutter: no counterpart

func (Limits) CheckCopyBuffer

func (l Limits) CheckCopyBuffer(src Buffer, srcOffset int, dst Buffer, dstOffset, size int)

CheckCopyBuffer panics with GPUDescriptorInvalid in debug builds when the buffers of CopyBuffer lack BufferCopySource and BufferCopyDestination, the offsets or the size are not multiples of 4, or the ranges are not inside the buffers. Backends and the fake device call it from CopyBuffer.

Flutter: no counterpart

func (Limits) CheckDispatch

func (l Limits) CheckDispatch(x, y, z int)

CheckDispatch panics with GPULimitExceeded in debug builds when a dimension of a dispatch is more than MaxComputeWorkgroupsPerDimension, and with GPUDescriptorInvalid when one is negative. Backends and the fake device call it from Dispatch.

Flutter: no counterpart

func (Limits) CheckIndirect

func (l Limits) CheckIndirect(b Buffer, offset, size int)

CheckIndirect panics with GPUDescriptorInvalid in debug builds when the buffer of an indirect draw or dispatch has no BufferIndirect, the offset is not a multiple of 4, or the arguments (size bytes) are not inside the buffer. Backends and the fake device call it from DrawIndirect, DrawIndexedIndirect and DispatchIndirect.

Flutter: no counterpart

func (Limits) CheckLayer

func (l Limits) CheckLayer(desc TextureDesc, level, layer int)

CheckLayer panics with GPUDescriptorInvalid in debug builds when Texture.Layer is asked for a level or layer the texture does not have, or for a 3D texture. Backends and the fake device call it from Layer.

Flutter: no counterpart

func (Limits) CheckPass

func (l Limits) CheckPass(desc PassDesc)

CheckPass panics with GPULimitExceeded in debug builds when the pass has more color attachments, or color attachments of more bytes for each sample, than the limits allow, and with GPUDescriptorInvalid when it has no attachment, an attachment that is not a render target or has the wrong format, attachments of different sizes or sample counts, or a resolve target that is not a single-sample texture of the size and format of its attachment. Backends and the fake device call it from BeginRenderPass.

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) CheckRead

func (l Limits) CheckRead(desc TextureDesc, bytes bool)

CheckRead panics with GPUDescriptorInvalid in debug builds when a texture is read that has no TextureCopySource or TextureRenderTarget, is of a format that cannot be copied, is not a 2D texture (read a Layer), or, for ReadTexture (bytes false), is not one of the 8 bit formats that it converts to RGBA. Backends and the fake device call it from ReadTexture and ReadTextureBytes.

Flutter: no counterpart

func (Limits) CheckReadBuffer

func (l Limits) CheckReadBuffer(b Buffer, offset, size int)

CheckReadBuffer panics with GPUDescriptorInvalid in debug builds when the buffer has no BufferCopySource or the range is not inside it. Backends and the fake device call it from ReadBuffer.

Flutter: no counterpart

func (Limits) CheckResolveQuerySet

func (l Limits) CheckResolveQuerySet(set QuerySet, first, count int, dst Buffer, dstOffset int)

CheckResolveQuerySet panics with GPUDescriptorInvalid in debug builds when the queries are not in the set, the buffer has no BufferQueryResolve, or the 8 bytes of each do not fit it at an offset that is a multiple of StorageOffsetAlignment. Backends and the fake device call it from ResolveQuerySet.

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 the limits (MaxTextureSize, MaxTextureSize3D, MaxTextureArrayLayers), and with GPUDescriptorInvalid when the description cannot be made: a usage the format does not allow, a cube that is not square or has other than 6 layers, more mip levels than the size has, or a multisampled texture that is not a single level of a 2D texture that a pass draws into. Backends and the fake device call it from NewTexture.

Flutter: no counterpart

func (Limits) CheckTimestamps

func (l Limits) CheckTimestamps(f Features, ts *PassTimestamps)

CheckTimestamps panics with GPUDescriptorInvalid in debug builds when a pass writes timestamps that the device cannot (Features.TimestampQuery is false) or into queries the set lacks. Backends and the fake device call it from BeginRenderPass and BeginComputePass.

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 of 16 bit indices, starting at offset bytes.
	SetIndexBuffer(b Buffer, offset int)
	// SetIndexBuffer32 binds the index buffer of 32 bit indices, starting at offset bytes.
	SetIndexBuffer32(b Buffer, offset int)
	// SetScissor limits the draws to the rectangle, in pixels of the target.
	SetScissor(r image.Rectangle)
	// SetStencilReference sets the reference value of the stencil tests and of StencilReplace; it
	// is 0 at the start of a pass.
	SetStencilReference(ref uint32)
	// Draw draws vertices, instances times.
	Draw(vertices, instances, firstVertex, firstInstance int)
	// DrawIndexed draws indices, instances times.
	DrawIndexed(indices, instances, firstIndex, baseVertex, firstInstance int)
	// DrawIndirect draws with the arguments in b at offset: four 32 bit unsigned integers, the
	// vertex count, the instance count, the first vertex and the first instance. b has BufferIndirect;
	// offset is a multiple of 4. The first vertex and the first instance must be 0 for a shader that
	// reads vertex_index or instance_index (the arguments cannot be checked by the CPU).
	DrawIndirect(b Buffer, offset int)
	// DrawIndexedIndirect draws with the arguments in b at offset: five 32 bit integers, the index
	// count, the instance count, the first index, the base vertex (signed) and the first instance.
	DrawIndexedIndirect(b Buffer, offset int)
	// End finishes the pass.
	End()
}

Pass records the draws of one render pass.

Flutter: no counterpart

type PassDesc

type PassDesc struct {
	// Color are the color attachments.
	Color []ColorAttachment
	// Depth is the depth-stencil attachment; nil for none.
	Depth *DepthAttachment
	// Timestamps, when set, is where the pass writes its timestamps; it needs Features.TimestampQuery.
	Timestamps *PassTimestamps
}

PassDesc describes a render pass: 1 to Limits.MaxColorAttachments color attachments and at most one depth-stencil attachment. They all have the same size and sample count.

Flutter: no counterpart

type PassTimestamps

type PassTimestamps struct {
	// Set is the query set.
	Set QuerySet
	// Begin and End are the indices of its queries.
	Begin, End int
}

PassTimestamps says where a pass writes its timestamps: the time at which the pass began into the query Begin of Set, and the time at which it ended into the query End. A negative index writes nothing.

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 of the one color target of the 2D renderer; it is the state of
	// target 0 when ColorTargets is empty.
	Blend BlendState
	// Target is the format of the one color target of the 2D renderer; it is the format of
	// target 0 when ColorTargets is empty.
	Target TextureFormat
	// ColorTargets are the color attachments of the passes that use the pipeline, 1 to
	// Limits.MaxColorAttachments of them; when empty, the pipeline has the one target of Target and
	// Blend. A pipeline made with ColorTargets, a Topology other than the triangle list, a
	// culling, a depth-stencil state or SampleCount is a pipeline of the 3D API; the clip space is
	// the same for every pipeline (SPEC §27.1).
	ColorTargets []ColorTargetState
	// Topology says how the vertices make primitives.
	Topology PrimitiveTopology
	// CullMode says which triangles are left out.
	CullMode CullMode
	// FrontFace says which winding faces the viewer.
	FrontFace FrontFace
	// DepthStencil is the depth and stencil state; the zero value has no depth-stencil
	// attachment.
	DepthStencil DepthStencilState
	// SampleCount is the number of samples of the attachments: 1 or 4; 0 means 1.
	SampleCount int
}

PipelineDesc describes a render pipeline: a generated shader, its entry points, the vertex layout, the blend state, the format of the targets and the state of the primitives, the depth and stencil tests and the multisampling.

Flutter: no counterpart

func (PipelineDesc) Samples

func (d PipelineDesc) Samples() int

Samples returns SampleCount, with 0 meaning 1.

Flutter: no counterpart

func (PipelineDesc) Targets

func (d PipelineDesc) Targets() []ColorTargetState

Targets returns the color targets of the pipeline: ColorTargets, or the one target of Target and Blend.

Flutter: no counterpart

type PrimitiveTopology

type PrimitiveTopology int

PrimitiveTopology says how the vertices of a draw make primitives. The zero value is TopologyTriangleList. A strip is not restarted by an index: the primitive restart is off.

Flutter: no counterpart

const (
	// TopologyTriangleList makes a triangle of each three vertices.
	//
	// Flutter: no counterpart
	TopologyTriangleList PrimitiveTopology = iota
	// TopologyTriangleStrip makes a triangle of each vertex and the two before it.
	//
	// Flutter: no counterpart
	TopologyTriangleStrip
	// TopologyLineList makes a line of each two vertices.
	//
	// Flutter: no counterpart
	TopologyLineList
	// TopologyLineStrip makes a line of each vertex and the one before it.
	//
	// Flutter: no counterpart
	TopologyLineStrip
	// TopologyPointList makes a point of each vertex.
	//
	// Flutter: no counterpart
	TopologyPointList
)

The topologies.

type QuerySet

type QuerySet interface {
	// Count is the number of timestamps.
	Count() int
	// Destroy releases the query set.
	Destroy()
}

QuerySet is a set of timestamps that passes write and Encoder.ResolveQuerySet reads.

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

func (*Renderer) Suspend

func (r *Renderer) Suspend() bool

Suspend waits for the GPU to finish what was committed, when the device can say so (IdleWaiter), so that the app can go to the background with no GPU work left, and says whether it waited. The next frame goes on as before.

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
	// OnDevice is told when the renderer opens its device, loses it and closes it (DeviceEvent), on the
	// thread that calls Draw and Close. The engine gives it to package gpu (SPEC §27.2).
	OnDevice func(DeviceEvent)
	// Textures is the registry of the textures that TextureLayers show (SPEC §27.4); nil when the app has none,
	// and a texture layer then draws nothing.
	Textures *TextureRegistry
}

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 SampleType

type SampleType int

SampleType is how a shader reads the texels of a texture (WebGPU's GPUTextureSampleType). The zero value is SampleTypeFloat.

Flutter: no counterpart

const (
	// SampleTypeFloat is a floating point texture that a filtering sampler can read.
	//
	// Flutter: no counterpart
	SampleTypeFloat SampleType = iota
	// SampleTypeUnfilterableFloat is a floating point texture that only textureLoad and a
	// non-filtering sampler can read.
	//
	// Flutter: no counterpart
	SampleTypeUnfilterableFloat
	// SampleTypeUint is a texture of unsigned integers, read with textureLoad.
	//
	// Flutter: no counterpart
	SampleTypeUint
)

The sample types.

func (SampleType) Fits

func (s SampleType) Fits(want SampleType) bool

Fits reports whether a texture that shaders read as s can be bound where a shader declares want: a filterable float texture fits a float or an unfilterable float binding, a float32 texture without filtering only the unfilterable one, a uint texture only a uint binding.

Flutter: no counterpart

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
	// AddressW is the mode of the third direction of a 3D texture.
	AddressW 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
	// MSL is the Metal Shading Language 2.4 that naga writes for the module, with the resources bound at
	// the indexes of ShaderBinding.MetalIndex (Metal only).
	MSL 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
	// Dimension is the dimension of a texture binding; the zero value is a 2D texture.
	Dimension TextureDimension
	// Sample is the sample type of a texture binding. A f32 texture that the shader reads only with
	// textureLoad, textureDimensions and the like is SampleTypeUnfilterableFloat: any float
	// texture can be bound to it, a float32 texture without float32-filterable too. A u32 texture
	// is SampleTypeUint.
	Sample SampleType
	// StorageFormat is the texel format of a storage texture binding.
	StorageFormat TextureFormat
	// Register is where the HLSL binds the resource (Direct3D 12 only).
	Register ShaderRegister
	// MetalIndex is where the MSL binds the resource (Metal only): the index in the argument table of its
	// class, buffer for uniform and storage buffers, texture for sampled and storage textures, sampler for
	// samplers. It is the place of the binding among the bindings of the module of its class, in the order
	// of group and binding (SPEC §29.5).
	MetalIndex int
}

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 or a
	// read-only storage buffer, "s" for a sampler and "u" for a writable storage buffer or a storage
	// texture.
	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
	// ShaderStageCompute is an entry point marked @compute.
	//
	// Flutter: no counterpart
	ShaderStageCompute
)

The stages of the shaders of the explicit renderer. The 2D renderer's shaders have no compute entry point; those of glutter-gen shaders -gpu may (SPEC §27.1).

func (ShaderStage) String

func (s ShaderStage) String() string

String returns "vertex" or "fragment".

type StencilFaceState

type StencilFaceState struct {
	// Compare is the test of the reference against the stored value.
	Compare CompareFunc
	// FailOp is what happens when the stencil test fails.
	FailOp StencilOp
	// DepthFailOp is what happens when the stencil test passes and the depth test fails.
	DepthFailOp StencilOp
	// PassOp is what happens when both pass.
	PassOp StencilOp
}

StencilFaceState is the stencil test of the front or the back faces of triangles. The zero value always passes and keeps the value.

Flutter: no counterpart

type StencilOp

type StencilOp int

StencilOp is what a stencil test does to the stencil value. The zero value is StencilKeep.

Flutter: no counterpart

const (
	// StencilKeep keeps the value.
	//
	// Flutter: no counterpart
	StencilKeep StencilOp = iota
	// StencilZero sets the value to 0.
	//
	// Flutter: no counterpart
	StencilZero
	// StencilReplace sets the value to the reference of the pass (Pass.SetStencilReference).
	//
	// Flutter: no counterpart
	StencilReplace
	// StencilInvert inverts the bits of the value.
	//
	// Flutter: no counterpart
	StencilInvert
	// StencilIncrementClamp adds 1 up to the largest value.
	//
	// Flutter: no counterpart
	StencilIncrementClamp
	// StencilDecrementClamp subtracts 1 down to 0.
	//
	// Flutter: no counterpart
	StencilDecrementClamp
	// StencilIncrementWrap adds 1 and wraps to 0.
	//
	// Flutter: no counterpart
	StencilIncrementWrap
	// StencilDecrementWrap subtracts 1 and wraps to the largest value.
	//
	// Flutter: no counterpart
	StencilDecrementWrap
)

The stencil operations.

type StoreOp

type StoreOp int

StoreOp is what a pass does with the pixels it drew when it ends. The zero value is StoreStore.

Flutter: no counterpart

const (
	// StoreStore keeps what the pass drew.
	//
	// Flutter: no counterpart
	StoreStore StoreOp = iota
	// StoreDiscard leaves the attachment undefined, for a target that is only resolved or only
	// read inside the pass (a depth buffer that nothing reads later): a tile-based GPU then does
	// not write it to memory.
	//
	// Flutter: no counterpart
	StoreDiscard
)

The store operations.

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, normalized (Normalized).
	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. A 2D array, cube or 3D texture keeps its kind
	// and its layers in the view of a level, and a pass can draw into it only through Layer. Level(0)
	// of a texture with one level is the texture itself.
	Level(i int) Texture
	// Layer returns a 2D texture over one layer (the face of a cube, in the order of
	// TextureKindCube) of the mip level of a 2D array or cube texture: a pass can draw into it, a
	// bind group can sample it as a 2D texture, and ReadTextureBytes can read it. It belongs to
	// this texture and goes away with it. A 2D texture has the one layer 0, which is Level(level).
	// A 3D texture has no layers: the call panics.
	Layer(level, layer int) Texture
	// Destroy releases the texture. The GPU finishes the frames that use it first.
	Destroy()
}

Texture is a GPU texture.

Flutter: no counterpart

type TextureDesc

type TextureDesc struct {
	// Width and Height are in pixels, at most Limits.MaxTextureSize (Limits.MaxTextureSize3D for
	// a 3D texture).
	Width, Height int
	// Kind is the shape; the zero value is a 2D texture.
	Kind TextureKind
	// DepthOrLayers is the number of layers of a 2D array, the depth of a 3D texture, and 6 for a
	// cube (0 means 6); for a 2D texture it is 1 (0 means 1).
	DepthOrLayers int
	// Format is the pixel format.
	Format TextureFormat
	// MipLevels is the number of levels; 0 means 1.
	MipLevels int
	// SampleCount is 1 or 4 samples for each pixel; 0 means 1. A multisampled texture is a 2D
	// texture with one level that only a pass draws into, and a resolve target gets its pixels.
	SampleCount int
	// Usage says how the texture is used.
	Usage TextureUsage
}

TextureDesc describes a texture.

Flutter: no counterpart

func (TextureDesc) Depth

func (d TextureDesc) Depth() int

Depth returns the depth of the normalized description: the slices of a 3D texture and 1 for the others.

Flutter: no counterpart

func (TextureDesc) Layers

func (d TextureDesc) Layers() int

Layers returns the number of array layers of the normalized description: the layers of an array, 6 for a cube and 1 for the others.

Flutter: no counterpart

func (TextureDesc) Normalized

func (d TextureDesc) Normalized() TextureDesc

Normalized returns the description with the defaults filled in: kind 2D, the layers or depth of the kind (1, or 6 for a cube), one mip level and one sample.

Flutter: no counterpart

type TextureDimension

type TextureDimension int

TextureDimension is the dimension of a texture binding of a shader: the WGSL type texture_2d, texture_2d_array, texture_cube or texture_3d. The zero value is TextureDim2D.

Flutter: no counterpart

const (
	// TextureDim2D is a texture_2d.
	//
	// Flutter: no counterpart
	TextureDim2D TextureDimension = iota
	// TextureDim2DArray is a texture_2d_array.
	//
	// Flutter: no counterpart
	TextureDim2DArray
	// TextureDimCube is a texture_cube.
	//
	// Flutter: no counterpart
	TextureDimCube
	// TextureDim3D is a texture_3d.
	//
	// Flutter: no counterpart
	TextureDim3D
)

The dimensions of a texture binding.

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
	// TextureFormatRGBA16Float is 8 bytes per pixel, four half floats: linear color for
	// high dynamic range.
	//
	// Flutter: no counterpart
	TextureFormatRGBA16Float
	// TextureFormatRGBA32Float is 16 bytes per pixel, four floats. Shaders sample it with
	// textureLoad, or with a filter when Features.Float32Filterable.
	//
	// Flutter: no counterpart
	TextureFormatRGBA32Float
	// TextureFormatR32Float is 4 bytes per pixel, one float: heights, distances.
	//
	// Flutter: no counterpart
	TextureFormatR32Float
	// TextureFormatR32Uint is 4 bytes per pixel, one unsigned integer: object ids.
	//
	// Flutter: no counterpart
	TextureFormatR32Uint
	// TextureFormatRG32Uint is 8 bytes per pixel, two unsigned integers.
	//
	// Flutter: no counterpart
	TextureFormatRG32Uint
	// TextureFormatDepth32Float is a depth buffer of one float per pixel.
	//
	// Flutter: no counterpart
	TextureFormatDepth32Float
	// TextureFormatDepth24PlusStencil8 is a depth buffer of at least 24 bits with an 8 bit
	// stencil. It cannot be copied.
	//
	// Flutter: no counterpart
	TextureFormatDepth24PlusStencil8
)

The formats of a texture (SPEC §27.1).

func (TextureFormat) AllowedUsage

func (f TextureFormat) AllowedUsage() TextureUsage

AllowedUsage returns the uses the format allows. A format that no use allows is unspecified. The depth formats are drawn into (and Depth32Float copied from), and are not sampled in 1.10.

Flutter: no counterpart

func (TextureFormat) BytesPerPixel

func (f TextureFormat) BytesPerPixel() int

BytesPerPixel returns the size of a pixel in bytes; 0 for an unspecified format. For TextureFormatDepth24PlusStencil8 it is the 4 bytes that a pixel nominally takes: the format cannot be copied, so the number is only used to count memory.

Flutter: no counterpart

func (TextureFormat) CanBeCopied

func (f TextureFormat) CanBeCopied() bool

CanBeCopied reports whether CopyTexture and ReadTextureBytes can read the format (when the texture has TextureCopySource): every format but Depth24PlusStencil8.

Flutter: no counterpart

func (TextureFormat) CanBeWritten

func (f TextureFormat) CanBeWritten() bool

CanBeWritten reports whether Device.WriteTexture can fill the format: the depth formats cannot be written (a pass fills them).

Flutter: no counterpart

func (TextureFormat) HasStencil

func (f TextureFormat) HasStencil() bool

HasStencil reports whether the format has a stencil aspect.

Flutter: no counterpart

func (TextureFormat) IsDepthStencil

func (f TextureFormat) IsDepthStencil() bool

IsDepthStencil reports whether the format is a depth or depth-stencil format.

Flutter: no counterpart

func (TextureFormat) RenderTargetPixelBytes

func (f TextureFormat) RenderTargetPixelBytes() int

RenderTargetPixelBytes is the number of bytes that the format takes in the budget of Limits.MaxColorAttachmentBytesPerSample (WebGPU's render target pixel byte cost); 0 for a format that is not a color attachment.

Flutter: no counterpart

func (TextureFormat) SampleType

func (f TextureFormat) SampleType(features Features) SampleType

SampleType returns how shaders read the format, for a device with the features; the formats that are not sampled (the depth formats) report SampleTypeFloat and have no TextureSampled usage.

Flutter: no counterpart

type TextureKind

type TextureKind int

TextureKind is the shape of a texture. The zero value is TextureKindUnspecified, which means TextureKind2D.

Flutter: no counterpart

const (
	// TextureKindUnspecified is the zero value and means TextureKind2D.
	//
	// Flutter: no counterpart
	TextureKindUnspecified TextureKind = iota
	// TextureKind2D is a flat image.
	//
	// Flutter: no counterpart
	TextureKind2D
	// TextureKind2DArray is TextureDesc.DepthOrLayers images of one size, indexed by layer.
	//
	// Flutter: no counterpart
	TextureKind2DArray
	// TextureKindCube is six square images, the faces +X, -X, +Y, -Y, +Z and -Z in that order
	// (layers 0 to 5), sampled by a direction.
	//
	// Flutter: no counterpart
	TextureKindCube
	// TextureKind3D is a volume of TextureDesc.DepthOrLayers slices.
	//
	// Flutter: no counterpart
	TextureKind3D
)

The kinds of texture.

func (TextureKind) Dimension

func (k TextureKind) Dimension() TextureDimension

Dimension returns the dimension of the views of a texture of this kind that a shader samples.

Flutter: no counterpart

type TextureRegion

type TextureRegion struct {
	// Level is the mip level.
	Level int
	// Origin is the first pixel (x, y) and the first layer or slice (z).
	Origin [3]int
	// Size is the width and height in pixels and the number of layers or slices.
	Size [3]int
}

TextureRegion is a box of one mip level of a texture: the pixels from Origin, of the size Size. The third coordinate is the layer of an array or a cube, or the slice of a 3D texture.

Flutter: no counterpart

type TextureRegistry

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

TextureRegistry is the table of the textures that an app shows in its windows with a TextureLayer (SPEC §27.4): package gpu registers a texture of the renderer's device and gets an id, the scene names the id, and the renderer looks the texture up when it draws the layer. A texture has a version that MarkFrameAvailable raises; the damage tracker hashes it, so that a frame in which the app wrote the texture damages the layer's rectangle. It is used on the UI thread (the lock is for the tests that read it from another goroutine).

Flutter: no counterpart (the embedder's texture registry, FlutterEngineRegisterExternalTexture)

func NewTextureRegistry

func NewTextureRegistry() *TextureRegistry

NewTextureRegistry returns an empty registry.

Flutter: no counterpart

func (*TextureRegistry) DropDevice

func (r *TextureRegistry) DropDevice(dev Device)

DropDevice forgets the textures of the device, which is gone.

Flutter: no counterpart

func (*TextureRegistry) Len

func (r *TextureRegistry) Len() int

Len is the number of registered textures.

Flutter: no counterpart

func (*TextureRegistry) Lookup

func (r *TextureRegistry) Lookup(dev Device, id int64) (Texture, bool)

Lookup returns the texture with the id if it is registered and belongs to the device: a texture of a device that was lost is not drawn by the one that replaced it.

Flutter: no counterpart

func (*TextureRegistry) MarkFrameAvailable

func (r *TextureRegistry) MarkFrameAvailable(id int64) bool

MarkFrameAvailable says that the texture has a new frame, and reports whether the id is registered.

Flutter: no counterpart (FlutterEngineMarkExternalTextureFrameAvailable)

func (*TextureRegistry) Register

func (r *TextureRegistry) Register(dev Device, tex Texture) int64

Register adds a texture of the device and returns its id, from 1 up. The id is not used again.

Flutter: no counterpart (FlutterEngineRegisterExternalTexture)

func (*TextureRegistry) Unregister

func (r *TextureRegistry) Unregister(id int64) bool

Unregister removes the texture and reports whether the id was registered.

Flutter: no counterpart (FlutterEngineUnregisterExternalTexture)

func (*TextureRegistry) Version

func (r *TextureRegistry) Version(id int64) uint64

Version is a number that changes when the texture has a new frame; 0 for an id that is not registered.

Flutter: no counterpart

type TextureUsage

type TextureUsage int

TextureUsage says what a texture is used for; values are combined with |. Every texture whose format CanBeWritten 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
	// TextureStorage is a texture that compute shaders write (SPEC §27.1). Only RGBA8Unorm,
	// RGBA16Float, R32Float and R32Uint allow it.
	//
	// Flutter: no counterpart
	TextureStorage
)

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: WebGPU's vertex formats (SPEC §27.1). 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
	// VertexUint32 is one uint32.
	//
	// Flutter: no counterpart
	VertexUint32
	// VertexUint32x2 is two uint32.
	//
	// Flutter: no counterpart
	VertexUint32x2
	// VertexUint32x3 is three uint32.
	//
	// Flutter: no counterpart
	VertexUint32x3
	// VertexUint32x4 is four uint32.
	//
	// Flutter: no counterpart
	VertexUint32x4
	// VertexSint32 is one int32.
	//
	// Flutter: no counterpart
	VertexSint32
	// VertexSint32x2 is two int32.
	//
	// Flutter: no counterpart
	VertexSint32x2
	// VertexSint32x3 is three int32.
	//
	// Flutter: no counterpart
	VertexSint32x3
	// VertexSint32x4 is four int32.
	//
	// Flutter: no counterpart
	VertexSint32x4
	// VertexUint8x2 is two bytes read as integers.
	//
	// Flutter: no counterpart
	VertexUint8x2
	// VertexUint8x4 is four bytes read as integers.
	//
	// Flutter: no counterpart
	VertexUint8x4
	// VertexSint8x2 is two signed bytes read as integers.
	//
	// Flutter: no counterpart
	VertexSint8x2
	// VertexSint8x4 is four signed bytes read as integers.
	//
	// Flutter: no counterpart
	VertexSint8x4
	// VertexUnorm8x2 is two bytes read as numbers from 0 to 1.
	//
	// Flutter: no counterpart
	VertexUnorm8x2
	// VertexSnorm8x2 is two signed bytes read as numbers from -1 to 1.
	//
	// Flutter: no counterpart
	VertexSnorm8x2
	// VertexSnorm8x4 is four signed bytes read as numbers from -1 to 1.
	//
	// Flutter: no counterpart
	VertexSnorm8x4
	// VertexUint16x2 is two uint16 read as integers.
	//
	// Flutter: no counterpart
	VertexUint16x2
	// VertexUint16x4 is four uint16 read as integers.
	//
	// Flutter: no counterpart
	VertexUint16x4
	// VertexSint16x2 is two int16 read as integers.
	//
	// Flutter: no counterpart
	VertexSint16x2
	// VertexSint16x4 is four int16 read as integers.
	//
	// Flutter: no counterpart
	VertexSint16x4
	// VertexUnorm16x2 is two uint16 read as numbers from 0 to 1.
	//
	// Flutter: no counterpart
	VertexUnorm16x2
	// VertexUnorm16x4 is four uint16 read as numbers from 0 to 1.
	//
	// Flutter: no counterpart
	VertexUnorm16x4
	// VertexSnorm16x2 is two int16 read as numbers from -1 to 1.
	//
	// Flutter: no counterpart
	VertexSnorm16x2
	// VertexSnorm16x4 is four int16 read as numbers from -1 to 1.
	//
	// Flutter: no counterpart
	VertexSnorm16x4
	// VertexFloat16x2 is two half floats.
	//
	// Flutter: no counterpart
	VertexFloat16x2
	// VertexFloat16x4 is four half floats.
	//
	// Flutter: no counterpart
	VertexFloat16x4
)

The vertex formats. A format with unorm or snorm gives the shader floats from 0 to 1 or from -1 to 1; with uint or sint, integers.

func (VertexFormat) Size

func (f VertexFormat) Size() int

Size returns the size of the attribute in bytes; 0 for an unspecified format.

Flutter: no counterpart

Directories

Path Synopsis
Package devicetest holds the scenes that every backend of the explicit device runs (SPEC §27.12): Vulkan on lavapipe, Direct3D 12 under Wine, WebGPU in Chromium and the fake device.
Package devicetest holds the scenes that every backend of the explicit device runs (SPEC §27.12): Vulkan on lavapipe, Direct3D 12 under Wine, WebGPU in Chromium and the fake device.
gotest
Package gotest adapts *testing.T to devicetest.T. It imports testing, so only tests import it.
Package gotest adapts *testing.T to devicetest.T. It imports testing, so only tests import it.
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