yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f02b08490
master
1// hit-object-make-hit.slang 2 3//TEST:SIMPLE: -target dxil -entry rayGenerationMain -stage raygeneration -profile sm_6_5 -DNV_SHADER_EXTN_SLOT=u0 4//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry rayGenerationMain -stage raygeneration -profile sm_6_5 -line-directive-mode none -O0 5//TEST:SIMPLE(filecheck=SPIRV): -target spirv -entry rayGenerationMain -stage raygeneration -emit-spirv-directly 6//DISABLE_TEST(compute):COMPARE_COMPUTE:-d3d12 -output-using-type -profile sm_6_5 -render-feature ray-query 7//DISABLE_TEST(compute):COMPARE_COMPUTE:-vk -output-using-type -render-feature ray-query 8 9//TEST_INPUT: set scene = AccelerationStructure 10uniform RaytracingAccelerationStructure scene; 11 12//TEST_INPUT:set outputBuffer = out ubuffer(data=[0, 0, 0, 0], stride=4) 13RWStructuredBuffer<uint> outputBuffer; 14 15struct SomeValues 16{ 17 int a; 18 float b; 19}; 20 21 22uint calcValue(HitObject hit) 23{ 24 // SPIRV-DAG: OpHitObjectIsHitNV 25 // SPIRV-DAG: OpHitObjectGetInstanceCustomIndexNV 26 // SPIRV-DAG: OpHitObjectGetInstanceIdNV 27 // SPIRV-DAG: OpHitObjectGetGeometryIndexNV 28 // SPIRV-DAG: OpHitObjectGetPrimitiveIndexNV 29 // SPIRV-DAG: OpHitObjectGetHitKindNV 30 // SPIRV-DAG: OpHitObjectIsMissNV 31 uint r = 0; 32 33 if (hit.IsHit()) 34 { 35 uint instanceIndex = hit.GetInstanceIndex(); 36 uint instanceID = hit.GetInstanceID(); 37 uint geometryIndex = hit.GetGeometryIndex(); 38 uint primitiveIndex = hit.GetPrimitiveIndex(); 39 uint hitKind = hit.GetHitKind(); 40 41 r += hitKind; 42 r += instanceIndex; 43 r += instanceID; 44 r += geometryIndex; 45 r += primitiveIndex; 46 47 RayDesc ray = hit.GetRayDesc(); 48 49 r += uint(ray.TMin > 0); 50 r += uint(ray.TMax < ray.TMin); 51 52 SomeValues objSomeValues = hit.GetAttributes<SomeValues>(); 53 54 r += objSomeValues.a; 55 } 56 else if (hit.IsMiss()) 57 { 58 r += 1; 59 } 60 61 return r; 62} 63 64void rayGenerationMain() 65{ 66 int2 launchID = int2(DispatchRaysIndex().xy); 67 int2 launchSize = int2(DispatchRaysDimensions().xy); 68 69 int idx = launchID.x; 70 71 SomeValues someValues = { idx, idx * 2.0f }; 72 73 RayDesc ray; 74 ray.Origin = float3(idx, 0, 0); 75 ray.TMin = 0.01f; 76 ray.Direction = float3(0, 1, 0); 77 ray.TMax = 1e4f; 78 79 uint hitKind = 0; 80 81 uint r = 0; 82 { 83 // SPIRV-DAG: OpHitObjectRecordHitNV 84 HitObject hit = HitObject::MakeHit(0, scene, idx, idx * 2, idx * 3, hitKind, ray, someValues); 85 86 r = calcValue(hit); 87 } 88 89 { 90 int rayContributionToHitGroupIndex = 0; 91 int multiplierForGeometryContributionToHitGroupIndex = 4; 92 93 HitObject hit = HitObject::MakeHit(scene, 94 idx, 95 idx * 2, 96 idx * 3, 97 hitKind, 98 rayContributionToHitGroupIndex, 99 multiplierForGeometryContributionToHitGroupIndex, 100 ray, 101 someValues); 102 103 r += calcValue(hit); 104 } 105 106 outputBuffer[idx] = r; 107}