yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f02b08490
master
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=DX12):-slang -compute -dx12 -output-using-type -profile cs_6_5 -render-feature ray-query 2//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=VK):-vk -emit-spirv-directly -compute -shaderobj -output-using-type -render-feature ray-query 3 4//TEST_INPUT: set accelStruct = AccelerationStructure 5uniform RaytracingAccelerationStructure accelStruct; 6 7float GetRayT<let RAY_QUERY_FLAGS: uint>(uint rayInlineFlags) 8{ 9 RayDesc ray; 10 ray.Origin = float3(0.1f, 0.1f, 0.0f); 11 ray.Direction = float3(0.0f, 0.0f, 1.0f); 12 ray.TMin = 0.01f; 13 ray.TMax = 1e4f; 14 15 RayQuery<RAY_QUERY_FLAGS> rq; 16 rq.TraceRayInline(accelStruct, rayInlineFlags, 0xff, ray); 17 bool proceed = rq.Proceed(); 18 19 if( proceed ) 20 { 21 switch( rq.CandidateType() ) 22 { 23 case CANDIDATE_NON_OPAQUE_TRIANGLE: 24 rq.CommitNonOpaqueTriangleHit(); 25 rq.Abort(); 26 return rq.CommittedRayT(); 27 28 case CANDIDATE_PROCEDURAL_PRIMITIVE: 29 rq.CommitProceduralPrimitiveHit(0.5f); 30 rq.Abort(); 31 return rq.CommittedRayT(); 32 33 default: 34 rq.Abort(); 35 return 0.0f; 36 } 37 } 38 else 39 { 40 if( rq.CommittedStatus() == COMMITTED_TRIANGLE_HIT ) 41 { 42 rq.Abort(); 43 return rq.CommittedRayT(); 44 } 45 } 46 47 return 0.0f; 48} 49 50// Test the intrinsics that are available when RayQuery.Proceed() returns TRUE, as in the case of non-opaque triangles for example. 51float CheckRayIntrinsics() 52{ 53 RayDesc ray; 54 ray.Origin = float3(0.1f, 0.1f, 0.0f); 55 ray.Direction = float3(0.0f, 0.0f, 1.0f); 56 ray.TMin = 0.0f; 57 ray.TMax = 100.0f; 58 59 RayQuery<RAY_FLAG_FORCE_NON_OPAQUE> rq; 60 rq.TraceRayInline(accelStruct, RAY_FLAG_FORCE_NON_OPAQUE, 0xff, ray); 61 bool proceed = rq.Proceed(); 62 63 if( proceed ) 64 { 65 if( rq.CandidateType() == CANDIDATE_NON_OPAQUE_TRIANGLE ) 66 { 67 float score = 0.0f; 68 69 rq.CommitNonOpaqueTriangleHit(); 70 rq.Abort(); 71 72 if( rq.RayFlags() == RAY_FLAG_FORCE_NON_OPAQUE ) 73 score += 1.0f; 74 75 float3 wro = rq.WorldRayOrigin(); 76 if( wro.x == 0.1f && wro.y == 0.1f && wro.z == 0.0f ) 77 score += 1.0f; 78 79 float3 wrd = rq.WorldRayDirection(); 80 if( wrd.x == 0.0f && wrd.y == 0.0f && wrd.z == 1.0f ) 81 score += 1.0f; 82 83 if( rq.RayTMin() == 0.0f ) 84 score += 1.0f; 85 86 if( rq.CandidateTriangleRayT() == 0.5f ) 87 score += 1.0f; 88 89 if( rq.CommittedRayT() == 0.5f ) 90 score += 1.0f; 91 92 if( rq.CandidateInstanceIndex() == 0 ) 93 score += 1.0f; 94 95 if( rq.CandidateInstanceID() == 0 ) 96 score += 1.0f; 97 98 if( rq.CandidateInstanceContributionToHitGroupIndex() == 0 ) 99 score += 1.0f; 100 101 if( rq.CandidateGeometryIndex() == 0 ) 102 score += 1.0f; 103 104 if( rq.CandidatePrimitiveIndex() == 0 ) 105 score += 1.0f; 106 107 float3 oro = rq.CandidateObjectRayOrigin(); 108 if( oro.x == 0.1f && oro.y == 0.1f && oro.z == 0.0f ) 109 score += 1.0f; 110 111 float3 ord = rq.CandidateObjectRayDirection(); 112 if( ord.x == 0.0f && ord.y == 0.0f && ord.z == 1.0f ) 113 score += 1.0f; 114 115 float3x4 otwMat34 = rq.CandidateObjectToWorld3x4(); 116 score += otwMat34._11; score += otwMat34._12; score += otwMat34._13; score += otwMat34._14; 117 score += otwMat34._21; score += otwMat34._22; score += otwMat34._23; score += otwMat34._24; 118 score += otwMat34._31; score += otwMat34._32; score += otwMat34._33; score += otwMat34._34; 119 120 float4x3 otwMat43 = rq.CandidateObjectToWorld4x3(); 121 score += otwMat43._11; score += otwMat43._12; score += otwMat43._13; 122 score += otwMat43._21; score += otwMat43._22; score += otwMat43._23; 123 score += otwMat43._31; score += otwMat43._32; score += otwMat43._33; 124 score += otwMat43._41; score += otwMat43._42; score += otwMat43._43; 125 126 float3x4 wtoMat34 = rq.CandidateWorldToObject3x4(); 127 score += wtoMat34._11; score += wtoMat34._12; score += wtoMat34._13; score += wtoMat34._14; 128 score += wtoMat34._21; score += wtoMat34._22; score += wtoMat34._23; score += wtoMat34._24; 129 score += wtoMat34._31; score += wtoMat34._32; score += wtoMat34._33; score += wtoMat34._34; 130 131 float4x3 wtoMat43 = rq.CandidateWorldToObject4x3(); 132 score += wtoMat43._11; score += wtoMat43._12; score += wtoMat43._13; 133 score += wtoMat43._21; score += wtoMat43._22; score += wtoMat43._23; 134 score += wtoMat43._31; score += wtoMat43._32; score += wtoMat43._33; 135 score += wtoMat43._41; score += wtoMat43._42; score += wtoMat43._43; 136 137 if( rq.CommittedInstanceIndex() == 0 ) 138 score += 1.0f; 139 140 if( rq.CommittedInstanceID() == 0 ) 141 score += 1.0f; 142 143 if( rq.CommittedInstanceContributionToHitGroupIndex() == 0 ) 144 score += 1.0f; 145 146 if( rq.CommittedGeometryIndex() == 0 ) 147 score += 1.0f; 148 149 if( rq.CommittedPrimitiveIndex() == 0 ) 150 score += 1.0f; 151 152 oro = rq.CommittedObjectRayOrigin(); 153 if( oro.x == 0.1f && oro.y == 0.1f && oro.z == 0.0f ) 154 score += 1.0f; 155 156 ord = rq.CommittedObjectRayDirection(); 157 if( ord.x == 0.0f && ord.y == 0.0f && ord.z == 1.0f ) 158 score += 1.0f; 159 160 otwMat34 = rq.CommittedObjectToWorld3x4(); 161 score += otwMat34._11; score += otwMat34._12; score += otwMat34._13; score += otwMat34._14; 162 score += otwMat34._21; score += otwMat34._22; score += otwMat34._23; score += otwMat34._24; 163 score += otwMat34._31; score += otwMat34._32; score += otwMat34._33; score += otwMat34._34; 164 165 otwMat43 = rq.CommittedObjectToWorld4x3(); 166 score += otwMat43._11; score += otwMat43._12; score += otwMat43._13; 167 score += otwMat43._21; score += otwMat43._22; score += otwMat43._23; 168 score += otwMat43._31; score += otwMat43._32; score += otwMat43._33; 169 score += otwMat43._41; score += otwMat43._42; score += otwMat43._43; 170 171 wtoMat34 = rq.CommittedWorldToObject3x4(); 172 score += wtoMat34._11; score += wtoMat34._12; score += wtoMat34._13; score += wtoMat34._14; 173 score += wtoMat34._21; score += wtoMat34._22; score += wtoMat34._23; score += wtoMat34._24; 174 score += wtoMat34._31; score += wtoMat34._32; score += wtoMat34._33; score += wtoMat34._34; 175 176 wtoMat43 = rq.CommittedWorldToObject4x3(); 177 score += wtoMat43._11; score += wtoMat43._12; score += wtoMat43._13; 178 score += wtoMat43._21; score += wtoMat43._22; score += wtoMat43._23; 179 score += wtoMat43._31; score += wtoMat43._32; score += wtoMat43._33; 180 score += wtoMat43._41; score += wtoMat43._42; score += wtoMat43._43; 181 182 float2 bary = rq.CandidateTriangleBarycentrics(); 183 if( bary.x != 0.0f && bary.y != 0.0f ) 184 score += 1.0f; 185 186 if( rq.CandidateTriangleFrontFace() == true ) 187 score += 1.0f; 188 189 bary = rq.CommittedTriangleBarycentrics(); 190 if( bary.x != 0.0f && bary.y != 0.0f ) 191 score += 1.0f; 192 193 if( rq.CommittedTriangleFrontFace() == true ) 194 score += 1.0f; 195 196 return score; 197 } 198 } 199 200 return 0.0f; 201} 202 203 204//TEST_INPUT: ubuffer(data=[0], stride=4):out,name outputBuffer 205RWStructuredBuffer<float> outputBuffer; 206 207[shader("compute")] 208[numthreads(1, 1, 1)] 209void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) 210{ 211 int idx = dispatchThreadID.x; 212 float val = 0.0f; 213 214 const uint rayFlags[] = { 215 RAY_FLAG_NONE, 216 RAY_FLAG_FORCE_OPAQUE, 217 RAY_FLAG_FORCE_NON_OPAQUE, 218 RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH, 219 RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, 220 RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 221 RAY_FLAG_CULL_FRONT_FACING_TRIANGLES, 222 RAY_FLAG_CULL_OPAQUE, 223 RAY_FLAG_CULL_NON_OPAQUE, 224 RAY_FLAG_SKIP_TRIANGLES, 225 RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES 226 }; 227 const uint numRayFlags = sizeof(rayFlags) / sizeof(uint); 228 229 // RAY_FLAG_FORCE_NON_OPAQUE, RAY_FLAG_CULL_OPAQUE, RAY_FLAG_SKIP_TRIANGLES returns 0 instead of 0.5 230 for( uint i = 0; i < numRayFlags; ++i ) 231 { 232 val += GetRayT<RAY_FLAG_NONE>(rayFlags[i]); 233 val += GetRayT<RAY_FLAG_FORCE_OPAQUE>(rayFlags[i]); 234 val += GetRayT<RAY_FLAG_FORCE_NON_OPAQUE>(rayFlags[i]); // RAY_FLAG_FORCE_NON_OPAQUE returns 0 for all RayInlineFlags 235 val += GetRayT<RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH>(rayFlags[i]); 236 val += GetRayT<RAY_FLAG_SKIP_CLOSEST_HIT_SHADER>(rayFlags[i]); 237 val += GetRayT<RAY_FLAG_CULL_BACK_FACING_TRIANGLES>(rayFlags[i]); 238 val += GetRayT<RAY_FLAG_CULL_FRONT_FACING_TRIANGLES>(rayFlags[i]); 239 val += GetRayT<RAY_FLAG_CULL_OPAQUE>(rayFlags[i]); 240 val += GetRayT<RAY_FLAG_CULL_NON_OPAQUE>(rayFlags[i]); 241 val += GetRayT<RAY_FLAG_SKIP_TRIANGLES>(rayFlags[i]); 242 if( rayFlags[i] != RAY_FLAG_SKIP_TRIANGLES ) // Skip this combo that's causing differences in the score between DX12 and VK 243 val += GetRayT<RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES>(rayFlags[i]); 244 } 245 246 val += CheckRayIntrinsics(); 247 248 outputBuffer[idx] = val; 249} 250 251// DX12: 88.5 252// VK: 88.5