Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created March 9, 2023 20:16
Show Gist options
  • Save jonahwilliams/382dca36d9baee8b0b899499332393f4 to your computer and use it in GitHub Desktop.
Save jonahwilliams/382dca36d9baee8b0b899499332393f4 to your computer and use it in GitHub Desktop.
#include <metal_stdlib>
#include <simd/simd.h>
using namespace metal;
struct FragInfo
{
float2 start_point;
float2 end_point;
float tile_mode;
float texture_sampler_y_coord_scale;
float alpha;
float2 half_texel;
};
struct linear_gradient_fill_fragment_main_out
{
float4 frag_color [[color(0)]];
};
struct linear_gradient_fill_fragment_main_in
{
float2 v_position [[user(locn0)]];
};
fragment linear_gradient_fill_fragment_main_out linear_gradient_fill_fragment_main(linear_gradient_fill_fragment_main_in in [[stage_in]], constant FragInfo& frag_info [[buffer(0)]], texture2d<float> texture_sampler [[texture(0)]], sampler texture_samplerSmplr [[sampler(0)]])
{
linear_gradient_fill_fragment_main_out out = {};
float2 _234 = frag_info.end_point - frag_info.start_point;
float _249 = dot(in.v_position - frag_info.start_point, _234) / dot(_234, _234);
float4 _518;
do
{
bool _327 = frag_info.tile_mode == 3.0;
bool _340;
if (_327)
{
bool _331 = _249 < 0.0;
bool _338;
if (!_331)
{
_338 = _249 >= 1.0;
}
else
{
_338 = _331;
}
_340 = _338;
}
else
{
_340 = _327;
}
bool _359;
if (!_340)
{
_359 = _327 ? false : _327;
}
else
{
_359 = _340;
}
if (_359)
{
_518 = float4(0.0);
break;
}
bool _391 = frag_info.tile_mode == 0.0;
float _511;
if (_391)
{
_511 = fast::clamp(_249, 0.0, 1.0);
}
else
{
float _512;
if (frag_info.tile_mode == 1.0)
{
_512 = fract(_249);
}
else
{
float _513;
if (frag_info.tile_mode == 2.0)
{
float _406 = _249 - 1.0;
_513 = abs(fma(-2.0, floor(_406 * 0.5), _406) - 1.0);
}
else
{
_513 = _249;
}
_512 = _513;
}
_511 = _512;
}
float _514;
if (_391)
{
_514 = 0.5;
}
else
{
float _515;
if (frag_info.tile_mode == 1.0)
{
_515 = fract(0.5);
}
else
{
float _516;
if (frag_info.tile_mode == 2.0)
{
_516 = abs(fma(-2.0, floor(-0.25), -0.5) - 1.0);
}
else
{
_516 = 0.5;
}
_515 = _516;
}
_514 = _515;
}
float _474 = mix(frag_info.half_texel.y, 1.0 - frag_info.half_texel.y, _514);
float2 _525 = float2(mix(frag_info.half_texel.x, 1.0 - frag_info.half_texel.x, _511), _474);
float2 _517;
if (frag_info.texture_sampler_y_coord_scale < 0.0)
{
float2 _510 = _525;
_510.y = 1.0 - _474;
_517 = _510;
}
else
{
_517 = _525;
}
_518 = texture_sampler.sample(texture_samplerSmplr, _517);
break;
} while(false);
out.frag_color = _518;
out.frag_color = float4(out.frag_color.xyz * out.frag_color.w, out.frag_color.w) * frag_info.alpha;
return out;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment