Added draw package as renderer focused on mixed use layout / 2D / 3D scene applications (#7)
Co-authored-by: Zachary Levy <zachary@sunforge.is> Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
296
draw/shaders/generated/base_2d.frag.metal
Normal file
296
draw/shaders/generated/base_2d.frag.metal
Normal file
@@ -0,0 +1,296 @@
|
||||
#pragma clang diagnostic ignored "-Wmissing-prototypes"
|
||||
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
|
||||
using namespace metal;
|
||||
|
||||
// Implementation of the GLSL mod() function, which is slightly different than Metal fmod()
|
||||
template<typename Tx, typename Ty>
|
||||
inline Tx mod(Tx x, Ty y)
|
||||
{
|
||||
return x - y * floor(x / y);
|
||||
}
|
||||
|
||||
struct main0_out
|
||||
{
|
||||
float4 out_color [[color(0)]];
|
||||
};
|
||||
|
||||
struct main0_in
|
||||
{
|
||||
float4 f_color [[user(locn0)]];
|
||||
float2 f_local_or_uv [[user(locn1)]];
|
||||
float4 f_params [[user(locn2)]];
|
||||
float4 f_params2 [[user(locn3)]];
|
||||
uint f_kind_flags [[user(locn4)]];
|
||||
float f_rotation [[user(locn5), flat]];
|
||||
};
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float2 apply_rotation(thread const float2& p, thread const float& angle)
|
||||
{
|
||||
float cr = cos(-angle);
|
||||
float sr = sin(-angle);
|
||||
return float2x2(float2(cr, sr), float2(-sr, cr)) * p;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdRoundedBox(thread const float2& p, thread const float2& b, thread float4& r)
|
||||
{
|
||||
float2 _61;
|
||||
if (p.x > 0.0)
|
||||
{
|
||||
_61 = r.xy;
|
||||
}
|
||||
else
|
||||
{
|
||||
_61 = r.zw;
|
||||
}
|
||||
r.x = _61.x;
|
||||
r.y = _61.y;
|
||||
float _78;
|
||||
if (p.y > 0.0)
|
||||
{
|
||||
_78 = r.x;
|
||||
}
|
||||
else
|
||||
{
|
||||
_78 = r.y;
|
||||
}
|
||||
r.x = _78;
|
||||
float2 q = (abs(p) - b) + float2(r.x);
|
||||
return (fast::min(fast::max(q.x, q.y), 0.0) + length(fast::max(q, float2(0.0)))) - r.x;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdf_stroke(thread const float& d, thread const float& stroke_width)
|
||||
{
|
||||
return abs(d) - (stroke_width * 0.5);
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdCircle(thread const float2& p, thread const float& r)
|
||||
{
|
||||
return length(p) - r;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdEllipse(thread float2& p, thread float2& ab)
|
||||
{
|
||||
p = abs(p);
|
||||
if (p.x > p.y)
|
||||
{
|
||||
p = p.yx;
|
||||
ab = ab.yx;
|
||||
}
|
||||
float l = (ab.y * ab.y) - (ab.x * ab.x);
|
||||
float m = (ab.x * p.x) / l;
|
||||
float m2 = m * m;
|
||||
float n = (ab.y * p.y) / l;
|
||||
float n2 = n * n;
|
||||
float c = ((m2 + n2) - 1.0) / 3.0;
|
||||
float c3 = (c * c) * c;
|
||||
float q = c3 + ((m2 * n2) * 2.0);
|
||||
float d = c3 + (m2 * n2);
|
||||
float g = m + (m * n2);
|
||||
float co;
|
||||
if (d < 0.0)
|
||||
{
|
||||
float h = acos(q / c3) / 3.0;
|
||||
float s = cos(h);
|
||||
float t = sin(h) * 1.73205077648162841796875;
|
||||
float rx = sqrt(((-c) * ((s + t) + 2.0)) + m2);
|
||||
float ry = sqrt(((-c) * ((s - t) + 2.0)) + m2);
|
||||
co = (((ry + (sign(l) * rx)) + (abs(g) / (rx * ry))) - m) / 2.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
float h_1 = ((2.0 * m) * n) * sqrt(d);
|
||||
float s_1 = sign(q + h_1) * powr(abs(q + h_1), 0.3333333432674407958984375);
|
||||
float u = sign(q - h_1) * powr(abs(q - h_1), 0.3333333432674407958984375);
|
||||
float rx_1 = (((-s_1) - u) - (c * 4.0)) + (2.0 * m2);
|
||||
float ry_1 = (s_1 - u) * 1.73205077648162841796875;
|
||||
float rm = sqrt((rx_1 * rx_1) + (ry_1 * ry_1));
|
||||
co = (((ry_1 / sqrt(rm - rx_1)) + ((2.0 * g) / rm)) - m) / 2.0;
|
||||
}
|
||||
float2 r = ab * float2(co, sqrt(1.0 - (co * co)));
|
||||
return length(r - p) * sign(p.y - r.y);
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdSegment(thread const float2& p, thread const float2& a, thread const float2& b)
|
||||
{
|
||||
float2 pa = p - a;
|
||||
float2 ba = b - a;
|
||||
float h = fast::clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
|
||||
return length(pa - (ba * h));
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline))
|
||||
float sdf_alpha(thread const float& d, thread const float& soft)
|
||||
{
|
||||
return 1.0 - smoothstep(-soft, soft, d);
|
||||
}
|
||||
|
||||
fragment main0_out main0(main0_in in [[stage_in]], texture2d<float> tex [[texture(0)]], sampler texSmplr [[sampler(0)]])
|
||||
{
|
||||
main0_out out = {};
|
||||
uint kind = in.f_kind_flags & 255u;
|
||||
uint flags = (in.f_kind_flags >> 8u) & 255u;
|
||||
if (kind == 0u)
|
||||
{
|
||||
out.out_color = in.f_color * tex.sample(texSmplr, in.f_local_or_uv);
|
||||
return out;
|
||||
}
|
||||
float d = 1000000015047466219876688855040.0;
|
||||
float soft = 1.0;
|
||||
if (kind == 1u)
|
||||
{
|
||||
float2 b = in.f_params.xy;
|
||||
float4 r = float4(in.f_params.zw, in.f_params2.xy);
|
||||
soft = fast::max(in.f_params2.z, 1.0);
|
||||
float stroke_px = in.f_params2.w;
|
||||
float2 p_local = in.f_local_or_uv;
|
||||
if (in.f_rotation != 0.0)
|
||||
{
|
||||
float2 param = p_local;
|
||||
float param_1 = in.f_rotation;
|
||||
p_local = apply_rotation(param, param_1);
|
||||
}
|
||||
float2 param_2 = p_local;
|
||||
float2 param_3 = b;
|
||||
float4 param_4 = r;
|
||||
float _491 = sdRoundedBox(param_2, param_3, param_4);
|
||||
d = _491;
|
||||
if ((flags & 1u) != 0u)
|
||||
{
|
||||
float param_5 = d;
|
||||
float param_6 = stroke_px;
|
||||
d = sdf_stroke(param_5, param_6);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kind == 2u)
|
||||
{
|
||||
float radius = in.f_params.x;
|
||||
soft = fast::max(in.f_params.y, 1.0);
|
||||
float stroke_px_1 = in.f_params.z;
|
||||
float2 param_7 = in.f_local_or_uv;
|
||||
float param_8 = radius;
|
||||
d = sdCircle(param_7, param_8);
|
||||
if ((flags & 1u) != 0u)
|
||||
{
|
||||
float param_9 = d;
|
||||
float param_10 = stroke_px_1;
|
||||
d = sdf_stroke(param_9, param_10);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kind == 3u)
|
||||
{
|
||||
float2 ab = in.f_params.xy;
|
||||
soft = fast::max(in.f_params.z, 1.0);
|
||||
float stroke_px_2 = in.f_params.w;
|
||||
float2 p_local_1 = in.f_local_or_uv;
|
||||
if (in.f_rotation != 0.0)
|
||||
{
|
||||
float2 param_11 = p_local_1;
|
||||
float param_12 = in.f_rotation;
|
||||
p_local_1 = apply_rotation(param_11, param_12);
|
||||
}
|
||||
float2 param_13 = p_local_1;
|
||||
float2 param_14 = ab;
|
||||
float _560 = sdEllipse(param_13, param_14);
|
||||
d = _560;
|
||||
if ((flags & 1u) != 0u)
|
||||
{
|
||||
float param_15 = d;
|
||||
float param_16 = stroke_px_2;
|
||||
d = sdf_stroke(param_15, param_16);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kind == 4u)
|
||||
{
|
||||
float2 a = in.f_params.xy;
|
||||
float2 b_1 = in.f_params.zw;
|
||||
float width = in.f_params2.x;
|
||||
soft = fast::max(in.f_params2.y, 1.0);
|
||||
float2 param_17 = in.f_local_or_uv;
|
||||
float2 param_18 = a;
|
||||
float2 param_19 = b_1;
|
||||
d = sdSegment(param_17, param_18, param_19) - (width * 0.5);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kind == 5u)
|
||||
{
|
||||
float inner = in.f_params.x;
|
||||
float outer = in.f_params.y;
|
||||
float start_rad = in.f_params.z;
|
||||
float end_rad = in.f_params.w;
|
||||
soft = fast::max(in.f_params2.x, 1.0);
|
||||
float r_1 = length(in.f_local_or_uv);
|
||||
float d_ring = fast::max(inner - r_1, r_1 - outer);
|
||||
float angle = precise::atan2(in.f_local_or_uv.y, in.f_local_or_uv.x);
|
||||
if (angle < 0.0)
|
||||
{
|
||||
angle += 6.283185482025146484375;
|
||||
}
|
||||
float ang_start = mod(start_rad, 6.283185482025146484375);
|
||||
float ang_end = mod(end_rad, 6.283185482025146484375);
|
||||
float _654;
|
||||
if (ang_end > ang_start)
|
||||
{
|
||||
_654 = float((angle >= ang_start) && (angle <= ang_end));
|
||||
}
|
||||
else
|
||||
{
|
||||
_654 = float((angle >= ang_start) || (angle <= ang_end));
|
||||
}
|
||||
float in_arc = _654;
|
||||
if (abs(ang_end - ang_start) >= 6.282185077667236328125)
|
||||
{
|
||||
in_arc = 1.0;
|
||||
}
|
||||
d = (in_arc > 0.5) ? d_ring : 1000000015047466219876688855040.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (kind == 6u)
|
||||
{
|
||||
float radius_1 = in.f_params.x;
|
||||
float rotation = in.f_params.y;
|
||||
float sides = in.f_params.z;
|
||||
soft = fast::max(in.f_params.w, 1.0);
|
||||
float stroke_px_3 = in.f_params2.x;
|
||||
float2 p = in.f_local_or_uv;
|
||||
float c = cos(rotation);
|
||||
float s = sin(rotation);
|
||||
p = float2x2(float2(c, -s), float2(s, c)) * p;
|
||||
float an = 3.1415927410125732421875 / sides;
|
||||
float bn = mod(precise::atan2(p.y, p.x), 2.0 * an) - an;
|
||||
d = (length(p) * cos(bn)) - radius_1;
|
||||
if ((flags & 1u) != 0u)
|
||||
{
|
||||
float param_20 = d;
|
||||
float param_21 = stroke_px_3;
|
||||
d = sdf_stroke(param_20, param_21);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
float param_22 = d;
|
||||
float param_23 = soft;
|
||||
float alpha = sdf_alpha(param_22, param_23);
|
||||
out.out_color = float4(in.f_color.xyz, in.f_color.w * alpha);
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user