yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f75bf474e
master
1//TEST:SIMPLE(filecheck=CHECK): -target cuda 2//TEST:SIMPLE(filecheck=CHECK-PTX): -target ptx 3 4// Test that we emit a valid raygeneration kernel. 5// This test will fail if either: (1) `OptixTraversableHandle` is not emitted 6// as a local var for `RaytracingAccelerationStructure` or (2)`RaytracingAccelerationStructure` 7// is not hoisted into use-site. 8 9// CHECK: void{{.*}}raygenMain 10// CHECK-PTX: .visible .entry{{.*}}raygenMain 11 12struct Ray { 13 float3 origin; 14 float t_min; 15 float3 dir; 16 float t_max; 17 18 __init(float3 origin, float3 dir, float t_min = 0.f, float t_max = 1000.0) 19 { 20 this.origin = origin; 21 this.dir = dir; 22 this.t_min = t_min; 23 this.t_max = t_max; 24 } 25 26 RayDesc to_ray_desc() { return { origin, t_min, dir, t_max }; } 27}; 28 29 30struct Camera { 31 float3 position; 32 float3 image_u; 33 float3 image_v; 34 float3 image_w; 35 36 Ray get_ray(float2 uv) 37 { 38 uv = uv * 2 - 1; 39 float3 dir = normalize(uv.x * image_u + uv.y * image_v + image_w); 40 return Ray(position, dir); 41 } 42}; 43 44struct Scene { 45 RaytracingAccelerationStructure tlas; 46 47 Camera camera; 48}; 49 50struct Path { 51 uint2 pixel; 52 uint vertex_index; 53 Ray ray; 54 float3 thp; 55 float3 L; 56 int rng; 57 58 __init(uint2 pixel, Ray ray, int rng) 59 { 60 this.pixel = pixel; 61 this.vertex_index = 0; 62 this.ray = ray; 63 this.thp = float3(1); 64 this.L = float3(0); 65 this.rng = rng; 66 } 67}; 68ParameterBlock<Scene> g_scene; 69RWTexture2D<float4> g_output; 70 71[require(sm_6_8, cuda)] 72[shader("raygeneration")] 73void raygenMain() 74{ 75 uint2 pixel = DispatchRaysIndex().xy; 76 float3 L = float3(0); 77 Ray ray = g_scene.camera.get_ray(pixel.xy); 78 Path path = Path(pixel, ray, 1); 79 TraceRay( 80 g_scene.tlas, 81 0, 82 0xff, 83 0, 84 0, 85 0, 86 path.ray.to_ray_desc(), 87 path 88 ); 89 L += path.L; 90 g_output[pixel] = float4(L, 1); 91}