1#include <metal_stdlib> 2#include <simd/simd.h> 3using namespace metal; 4struct Inputs { 5 uint3 sk_GlobalInvocationID; 6}; 7struct Globals { 8 texture2d<half, access::write> dest; 9}; 10kernel void computeMain(uint3 sk_GlobalInvocationID [[thread_position_in_grid]], texture2d<half, access::write> dest [[texture(0)]]) { 11 Globals _globals{dest}; 12 (void)_globals; 13 Inputs _in = { sk_GlobalInvocationID }; 14 half4 pixel = half4(0.0h, 0.0h, 0.0h, 1.0h); 15 float max_x = 5.0; 16 float max_y = 5.0; 17 float x = float(_in.sk_GlobalInvocationID.x * 2u - _globals.dest.get_width()) / float(_globals.dest.get_width()); 18 float y = float(_in.sk_GlobalInvocationID.y * 2u - _globals.dest.get_height()) / float(_globals.dest.get_height()); 19 float3 ray_origin = float3(0.0, 0.0, -1.0); 20 float3 ray_target = float3(x * max_x, y * max_y, 0.0); 21 float3 sphere_center = float3(0.0, 0.0, -10.0); 22 float sphere_radius = 1.0; 23 float3 t_minus_c = ray_target - sphere_center; 24 float b = dot(ray_origin, t_minus_c); 25 float c = dot(t_minus_c, t_minus_c) - sphere_radius * sphere_radius; 26 float bsqmc = b * b - c; 27 if (bsqmc >= 0.0) { 28 pixel = half4(0.4h, 0.4h, 1.0h, 1.0h); 29 } 30 _globals.dest.write(pixel, _in.sk_GlobalInvocationID.xy); 31 return; 32} 33