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