ray-tracing-procedural-geometry.azsl 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. // AZSL version of the example raytracing shader found here:
  2. // https://github.com/microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingProceduralGeometry/Raytracing.hlsl
  3. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. List of intrinsic declarations used in this shader:
  5. struct RayDesc
  6. {
  7. float3 Origin;
  8. float TMin;
  9. float3 Direction;
  10. float TMax;
  11. };
  12. uint3 DispatchRaysIndex();
  13. uint3 DispatchRaysDimensions();
  14. Template<payload_t>
  15. void TraceRay(RaytracingAccelerationStructure AccelerationStructure,
  16. uint RayFlags,
  17. uint InstanceInclusionMask,
  18. uint RayContributionToHitGroupIndex,
  19. uint MultiplierForGeometryContributionToHitGroupIndex,
  20. uint MissShaderIndex,
  21. RayDesc Ray,
  22. inout payload_t Payload);
  23. struct BuiltInTriangleIntersectionAttributes
  24. {
  25. float2 barycentrics;
  26. };
  27. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  28. struct ProceduralPrimitiveAttributes
  29. {
  30. float3 normal;
  31. };
  32. struct RayPayload
  33. {
  34. float4 color;
  35. uint recursionDepth;
  36. };
  37. struct ShadowRayPayload
  38. {
  39. bool hit;
  40. };
  41. struct SceneConstantBuffer
  42. {
  43. float4x4 projectionToWorld;
  44. float4 cameraPosition;
  45. float4 lightPosition;
  46. float4 lightAmbientColor;
  47. float4 lightDiffuseColor;
  48. float reflectance;
  49. float elapsedTime;
  50. };
  51. struct PrimitiveConstantBuffer
  52. {
  53. float4 albedo;
  54. float reflectanceCoef;
  55. float diffuseCoef;
  56. float specularCoef;
  57. float specularPower;
  58. float stepScale;
  59. float3 padding;
  60. };
  61. struct PrimitiveInstanceConstantBuffer
  62. {
  63. uint instanceIndex;
  64. uint primitiveType;
  65. };
  66. struct PrimitiveInstancePerFrameBuffer
  67. {
  68. float4x4 localSpaceToBottomLevelAS;
  69. float4x4 bottomLevelASToLocalSpace;
  70. };
  71. struct Vertex
  72. {
  73. float3 position;
  74. float3 normal;
  75. };
  76. enum class RayType_Enum {
  77. Radiance = 0,
  78. Shadow,
  79. Count
  80. };
  81. static const uint TraceRayParameters_InstanceMask = ~0;
  82. static const uint TraceRayParameters_HitGroup_Offset[RayType_Enum::Count] =
  83. {
  84. 0,
  85. 1
  86. };
  87. static const uint GeometryStride = RayType_Enum::Count;
  88. static const uint Offset[RayType_Enum::Count] =
  89. {
  90. 0,
  91. 1
  92. };
  93. static const float4 ChromiumReflectance = float4(0.549f, 0.556f, 0.554f, 1.0f);
  94. static const float4 BackgroundColor = float4(0.8f, 0.9f, 1.0f, 1.0f);
  95. static const float InShadowRadiance = 0.35f;
  96. enum class AnalyticPrimitive_Enum {
  97. AABB = 0,
  98. Spheres,
  99. Count
  100. };
  101. enum class VolumetricPrimitive_Enum {
  102. Metaballs = 0,
  103. Count
  104. };
  105. enum class SignedDistancePrimitive_Enum {
  106. MiniSpheres = 0,
  107. IntersectedRoundCube,
  108. SquareTorus,
  109. TwistedTorus,
  110. Cog,
  111. Cylinder,
  112. FractalPyramid,
  113. Count
  114. };
  115. struct Ray
  116. {
  117. float3 origin;
  118. float3 direction;
  119. };
  120. // [Atom-541] TODO Support argument overload
  121. //float length_toPow2(float2 p)
  122. //{
  123. // return dot(p, p);
  124. //}
  125. float length_toPow2(float3 p)
  126. {
  127. return dot(p, p);
  128. }
  129. float CalculateAnimationInterpolant(in float elapsedTime, in float cycleDuration)
  130. {
  131. float curLinearCycleTime = fmod(elapsedTime, cycleDuration) / cycleDuration;
  132. curLinearCycleTime = (curLinearCycleTime <= 0.5f) ? 2 * curLinearCycleTime : 1 - 2 * (curLinearCycleTime - 0.5f);
  133. return smoothstep(0, 1, curLinearCycleTime);
  134. }
  135. void swap(inout float a, inout float b)
  136. {
  137. float temp = a;
  138. a = b;
  139. b = temp;
  140. }
  141. bool IsInRange(in float val, in float min, in float max)
  142. {
  143. return (val >= min && val <= max);
  144. }
  145. static uint3 Load3x16BitIndices(uint offsetBytes, ByteAddressBuffer Indices)
  146. {
  147. uint3 indices_;
  148. const uint dwordAlignedOffset = offsetBytes & ~3;
  149. const uint2 four16BitIndices = Indices.Load2(dwordAlignedOffset);
  150. if (dwordAlignedOffset == offsetBytes)
  151. {
  152. indices_.x = four16BitIndices.x & 0xffff;
  153. indices_.y = (four16BitIndices.x >> 16) & 0xffff;
  154. indices_.z = four16BitIndices.y & 0xffff;
  155. }
  156. else
  157. {
  158. indices_.x = (four16BitIndices.x >> 16) & 0xffff;
  159. indices_.y = four16BitIndices.y & 0xffff;
  160. indices_.z = (four16BitIndices.y >> 16) & 0xffff;
  161. }
  162. return indices_;
  163. }
  164. float3 HitWorldPosition()
  165. {
  166. return WorldRayOrigin() + RayTCurrent() * WorldRayDirection();
  167. }
  168. float3 HitAttribute(float3 vertexAttribute[3], float2 barycentrics)
  169. {
  170. return vertexAttribute[0] +
  171. barycentrics.x * (vertexAttribute[1] - vertexAttribute[0]) +
  172. barycentrics.y * (vertexAttribute[2] - vertexAttribute[0]);
  173. }
  174. // TODO Support the inline keyword
  175. // Can functions *not* be inlined?
  176. // inline
  177. Ray GenerateCameraRay(uint2 index, in float3 cameraPosition, in float4x4 projectionToWorld)
  178. {
  179. float2 xy = index + 0.5f;
  180. float2 screenPos = xy / DispatchRaysDimensions().xy * 2.0 - 1.0;
  181. screenPos.y = -screenPos.y;
  182. float4 world = mul(float4(screenPos, 0, 1), projectionToWorld);
  183. world.xyz /= world.w;
  184. Ray ray;
  185. ray.origin = cameraPosition;
  186. ray.direction = normalize(world.xyz - ray.origin);
  187. return ray;
  188. }
  189. bool IsCulled(in Ray ray, in float3 hitSurfaceNormal)
  190. {
  191. float rayDirectionNormalDot = dot(ray.direction, hitSurfaceNormal);
  192. bool isCulled =
  193. ((RayFlags() & RAY_FLAG_CULL_BACK_FACING_TRIANGLES) && (rayDirectionNormalDot > 0))
  194. ||
  195. ((RayFlags() & RAY_FLAG_CULL_FRONT_FACING_TRIANGLES) && (rayDirectionNormalDot < 0));
  196. return isCulled;
  197. }
  198. bool IsAValidHit(in Ray ray, in float thit, in float3 hitSurfaceNormal)
  199. {
  200. return IsInRange(thit, RayTMin(), RayTCurrent()) && !IsCulled(ray, hitSurfaceNormal);
  201. }
  202. float2 TexCoords(in float3 position)
  203. {
  204. return position.xz;
  205. }
  206. void CalculateRayDifferentials(out float2 ddx_uv, out float2 ddy_uv, in float2 uv, in float3 hitPosition, in float3 surfaceNormal, in float3 cameraPosition, in float4x4 projectionToWorld)
  207. {
  208. Ray ddx = GenerateCameraRay(DispatchRaysIndex().xy + uint2(1, 0), cameraPosition, projectionToWorld);
  209. Ray ddy = GenerateCameraRay(DispatchRaysIndex().xy + uint2(0, 1), cameraPosition, projectionToWorld);
  210. float3 ddx_pos = ddx.origin - ddx.direction * dot(ddx.origin - hitPosition, surfaceNormal) / dot(ddx.direction, surfaceNormal);
  211. float3 ddy_pos = ddy.origin - ddy.direction * dot(ddy.origin - hitPosition, surfaceNormal) / dot(ddy.direction, surfaceNormal);
  212. ddx_uv = TexCoords(ddx_pos) - uv;
  213. ddy_uv = TexCoords(ddy_pos) - uv;
  214. }
  215. float CheckersTextureBoxFilter(in float2 uv, in float2 dpdx, in float2 dpdy, in uint ratio);
  216. float AnalyticalCheckersTexture(in float3 hitPosition, in float3 surfaceNormal, in float3 cameraPosition, in float4x4 projectionToWorld)
  217. {
  218. float2 ddx_uv;
  219. float2 ddy_uv;
  220. float2 uv = TexCoords(hitPosition);
  221. CalculateRayDifferentials(ddx_uv, ddy_uv, uv, hitPosition, surfaceNormal, cameraPosition, projectionToWorld);
  222. return CheckersTextureBoxFilter(uv, ddx_uv, ddy_uv, 50);
  223. }
  224. float3 FresnelReflectanceSchlick(in float3 I, in float3 N, in float3 f0)
  225. {
  226. float cosi = saturate(dot(-I, N));
  227. return f0 + (1 - f0)*pow(1 - cosi, 5);
  228. }
  229. bool SolveQuadraticEqn(float a, float b, float c, out float x0, out float x1)
  230. {
  231. float discr = b * b - 4 * a * c;
  232. if (discr < 0) return false;
  233. else if (discr == 0) x0 = x1 = -0.5 * b / a;
  234. else {
  235. float q = (b > 0) ?
  236. -0.5 * (b + sqrt(discr)) :
  237. -0.5 * (b - sqrt(discr));
  238. x0 = q / a;
  239. x1 = c / q;
  240. }
  241. if (x0 > x1) swap(x0, x1);
  242. return true;
  243. }
  244. float3 CalculateNormalForARaySphereHit(in Ray ray, in float thit, float3 a_center)
  245. {
  246. float3 hitPosition = ray.origin + thit * ray.direction;
  247. return normalize(hitPosition - a_center);
  248. }
  249. bool SolveRaySphereIntersectionEquation(in Ray ray, out float tmin, out float tmax, in float3 a_center, in float radius)
  250. {
  251. float3 L = ray.origin - a_center;
  252. float a = dot(ray.direction, ray.direction);
  253. float b = 2 * dot(ray.direction, L);
  254. float c = dot(L, L) - radius * radius;
  255. return SolveQuadraticEqn(a, b, c, tmin, tmax);
  256. }
  257. bool RaySphereIntersectionTest(in Ray ray, out float thit, out float tmax, in ProceduralPrimitiveAttributes attr, in float3 a_center = float3(0, 0, 0), in float radius = 1)
  258. {
  259. float t0, t1;
  260. if (!SolveRaySphereIntersectionEquation(ray, t0, t1, a_center, radius)) return false;
  261. tmax = t1;
  262. if (t0 < RayTMin())
  263. {
  264. if (t1 < RayTMin()) return false;
  265. attr.normal = CalculateNormalForARaySphereHit(ray, t1, a_center);
  266. if (IsAValidHit(ray, t1, attr.normal))
  267. {
  268. thit = t1;
  269. return true;
  270. }
  271. }
  272. else
  273. {
  274. attr.normal = CalculateNormalForARaySphereHit(ray, t0, a_center);
  275. if (IsAValidHit(ray, t0, attr.normal))
  276. {
  277. thit = t0;
  278. return true;
  279. }
  280. attr.normal = CalculateNormalForARaySphereHit(ray, t1, a_center);
  281. if (IsAValidHit(ray, t1, attr.normal))
  282. {
  283. thit = t1;
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. bool RaySolidSphereIntersectionTest(in Ray ray, out float thit, out float tmax, in float3 a_center = float3(0, 0, 0), in float radius = 1)
  290. {
  291. float t0, t1;
  292. if (!SolveRaySphereIntersectionEquation(ray, t0, t1, a_center, radius))
  293. return false;
  294. thit = max(t0, RayTMin());
  295. tmax = min(t1, RayTCurrent());
  296. return true;
  297. }
  298. bool RaySpheresIntersectionTest(in Ray ray, out float thit, out ProceduralPrimitiveAttributes attr)
  299. {
  300. const int N = 3;
  301. float3 centers[N] =
  302. {
  303. float3(-0.3, -0.3, -0.3),
  304. float3(0.1, 0.1, 0.4),
  305. float3(0.35,0.35, 0.0)
  306. };
  307. float radii[N] = { 0.6, 0.3, 0.15 };
  308. bool hitFound = false;
  309. thit = RayTCurrent();
  310. for (int i = 0; i < N; i++)
  311. {
  312. float _thit;
  313. float _tmax;
  314. ProceduralPrimitiveAttributes _attr;
  315. if (RaySphereIntersectionTest(ray, _thit, _tmax, _attr, centers[i], radii[i]))
  316. {
  317. if (_thit < thit)
  318. {
  319. thit = _thit;
  320. attr = _attr;
  321. hitFound = true;
  322. }
  323. }
  324. }
  325. return hitFound;
  326. }
  327. // [Atom-541] TODO Support function argument overload
  328. bool RayAABBIntersectionTestS(Ray ray, float3 aabb[2], out float tmin, out float tmax)
  329. {
  330. float3 tmin3, tmax3;
  331. int3 sign3 = ray.direction > 0;
  332. tmin3.x = (aabb[1 - sign3.x].x - ray.origin.x) / ray.direction.x;
  333. tmax3.x = (aabb[sign3.x].x - ray.origin.x) / ray.direction.x;
  334. tmin3.y = (aabb[1 - sign3.y].y - ray.origin.y) / ray.direction.y;
  335. tmax3.y = (aabb[sign3.y].y - ray.origin.y) / ray.direction.y;
  336. tmin3.z = (aabb[1 - sign3.z].z - ray.origin.z) / ray.direction.z;
  337. tmax3.z = (aabb[sign3.z].z - ray.origin.z) / ray.direction.z;
  338. tmin = max(max(tmin3.x, tmin3.y), tmin3.z);
  339. tmax = min(min(tmax3.x, tmax3.z), tmax3.z);
  340. return tmax > tmin && tmax >= RayTMin() && tmin <= RayTCurrent();
  341. }
  342. bool RayAABBIntersectionTest(Ray ray, float3 aabb[2], out float thit, out ProceduralPrimitiveAttributes attr)
  343. {
  344. float tmin, tmax;
  345. if (RayAABBIntersectionTestS(ray, aabb, tmin, tmax))
  346. {
  347. thit = tmin >= RayTMin() ? tmin : tmax;
  348. float3 hitPosition = ray.origin + thit * ray.direction;
  349. float3 distanceToBounds[2] = {
  350. abs(aabb[0] - hitPosition),
  351. abs(aabb[1] - hitPosition)
  352. };
  353. const float eps = 0.0001;
  354. if (distanceToBounds[0].x < eps) attr.normal = float3(-1, 0, 0);
  355. else if (distanceToBounds[0].y < eps) attr.normal = float3(0, -1, 0);
  356. else if (distanceToBounds[0].z < eps) attr.normal = float3(0, 0, -1);
  357. else if (distanceToBounds[1].x < eps) attr.normal = float3(1, 0, 0);
  358. else if (distanceToBounds[1].y < eps) attr.normal = float3(0, 1, 0);
  359. else if (distanceToBounds[1].z < eps) attr.normal = float3(0, 0, 1);
  360. return IsAValidHit(ray, thit, attr.normal);
  361. }
  362. return false;
  363. }
  364. struct Metaball
  365. {
  366. float3 m_center;
  367. float radius;
  368. };
  369. float CalculateMetaballPotential(in float3 position, in Metaball blob, out float distance)
  370. {
  371. distance = length(position - blob.m_center);
  372. if (distance <= blob.radius)
  373. {
  374. float d = distance;
  375. d = blob.radius - d;
  376. float r = blob.radius;
  377. return 6 * (d*d*d*d*d) / (r*r*r*r*r)
  378. - 15 * (d*d*d*d) / (r*r*r*r)
  379. + 10 * (d*d*d) / (r*r*r);
  380. }
  381. return 0;
  382. }
  383. float CalculateMetaballsPotential(in float3 position, in Metaball blobs[3], in uint nActiveMetaballs)
  384. {
  385. float sumFieldPotential = 0;
  386. for (uint j = 0; j < 3; j++)
  387. {
  388. float dummy;
  389. sumFieldPotential += CalculateMetaballPotential(position, blobs[j], dummy);
  390. }
  391. return sumFieldPotential;
  392. }
  393. float3 CalculateMetaballsNormal(in float3 position, in Metaball blobs[3], in uint nActiveMetaballs)
  394. {
  395. float e = 0.5773 * 0.00001;
  396. return normalize(float3(
  397. CalculateMetaballsPotential(position + float3(-e, 0, 0), blobs, nActiveMetaballs) -
  398. CalculateMetaballsPotential(position + float3(e, 0, 0), blobs, nActiveMetaballs),
  399. CalculateMetaballsPotential(position + float3(0, -e, 0), blobs, nActiveMetaballs) -
  400. CalculateMetaballsPotential(position + float3(0, e, 0), blobs, nActiveMetaballs),
  401. CalculateMetaballsPotential(position + float3(0, 0, -e), blobs, nActiveMetaballs) -
  402. CalculateMetaballsPotential(position + float3(0, 0, e), blobs, nActiveMetaballs)));
  403. }
  404. void InitializeAnimatedMetaballs(out Metaball blobs[3], in float elapsedTime, in float cycleDuration)
  405. {
  406. float3 keyFrameCenters[3][2] =
  407. {
  408. { float3(-0.3, -0.3, -0.4),float3(0.3,-0.3,-0.0) },
  409. { float3(0.0, -0.2, 0.5), float3(0.0, 0.4, 0.5) },
  410. { float3(0.4,0.4, 0.4), float3(-0.4, 0.2, -0.4) }
  411. };
  412. float radii[3] = { 0.45, 0.55, 0.45 };
  413. float tAnimate = CalculateAnimationInterpolant(elapsedTime, cycleDuration);
  414. for (uint j = 0; j < 3; j++)
  415. {
  416. blobs[j].m_center = lerp(keyFrameCenters[j][0], keyFrameCenters[j][1], tAnimate);
  417. blobs[j].radius = radii[j];
  418. }
  419. }
  420. void FindIntersectingMetaballs(in Ray ray, out float tmin, out float tmax, inout Metaball blobs[3], out uint nActiveMetaballs)
  421. {
  422. tmin = (1.0/0.0);
  423. tmax = -(1.0/0.0);
  424. nActiveMetaballs = 0;
  425. for (uint i = 0; i < 3; i++)
  426. {
  427. float _thit, _tmax;
  428. if (RaySolidSphereIntersectionTest(ray, _thit, _tmax, blobs[i].m_center, blobs[i].radius))
  429. {
  430. tmin = min(_thit, tmin);
  431. tmax = max(_tmax, tmax);
  432. nActiveMetaballs = 3;
  433. }
  434. }
  435. tmin = max(tmin, RayTMin());
  436. tmax = min(tmax, RayTCurrent());
  437. }
  438. bool RayMetaballsIntersectionTest(in Ray ray, out float thit, out ProceduralPrimitiveAttributes attr, in float elapsedTime)
  439. {
  440. Metaball blobs[3];
  441. InitializeAnimatedMetaballs(blobs, elapsedTime, 12.0f);
  442. float tmin, tmax;
  443. uint nActiveMetaballs = 0;
  444. FindIntersectingMetaballs(ray, tmin, tmax, blobs, nActiveMetaballs);
  445. uint MAX_STEPS = 128;
  446. float t = tmin;
  447. float minTStep = (tmax - tmin) / (MAX_STEPS / 1);
  448. uint iStep = 0;
  449. while (iStep++ < MAX_STEPS)
  450. {
  451. float3 position = ray.origin + t * ray.direction;
  452. float fieldPotentials[3];
  453. float sumFieldPotential = 0;
  454. for (uint j = 0; j < 3; j++)
  455. {
  456. float distance;
  457. fieldPotentials[j] = CalculateMetaballPotential(position, blobs[j], distance);
  458. sumFieldPotential += fieldPotentials[j];
  459. }
  460. const float Threshold = 0.25f;
  461. if (sumFieldPotential >= Threshold)
  462. {
  463. float3 normal = CalculateMetaballsNormal(position, blobs, nActiveMetaballs);
  464. if (IsAValidHit(ray, t, normal))
  465. {
  466. thit = t;
  467. attr.normal = normal;
  468. return true;
  469. }
  470. }
  471. t += minTStep;
  472. }
  473. return false;
  474. }
  475. float GetDistanceFromSignedDistancePrimitive(in float3 position, in SignedDistancePrimitive_Enum sdPrimitive);
  476. float opS(float d1, float d2)
  477. {
  478. return max(d1, -d2);
  479. }
  480. float opU(float d1, float d2)
  481. {
  482. return min(d1, d2);
  483. }
  484. float opI(float d1, float d2)
  485. {
  486. return max(d1, d2);
  487. }
  488. float3 opRep(float3 p, float3 c)
  489. {
  490. return fmod(p, c) - 0.5 * c;
  491. }
  492. float smin(float a, float b, float k)
  493. {
  494. float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
  495. return lerp(b, a, h) - k * h*(1.0 - h);
  496. }
  497. float smax(float a, float b, float k)
  498. {
  499. float h = clamp(0.5 + 0.5*(b - a) / k, 0.0, 1.0);
  500. return lerp(a, b, h) + k * h*(1.0 - h);
  501. }
  502. float opBlendU(float d1, float d2)
  503. {
  504. return smin(d1, d2, 0.1);
  505. }
  506. float opBlendI(float d1, float d2)
  507. {
  508. return smax(d1, d2, 0.1);
  509. }
  510. float3 opTwist(float3 p)
  511. {
  512. float c = cos(3.0 * p.y);
  513. float s = sin(3.0 * p.y);
  514. float2x2 m = float2x2(c, -s, s, c);
  515. return float3(mul(m, p.xz), p.y);
  516. }
  517. float sdPlane(float3 p)
  518. {
  519. return p.y;
  520. }
  521. float sdSphere(float3 p, float s)
  522. {
  523. return length(p) - s;
  524. }
  525. float sdBox(float3 p, float3 b)
  526. {
  527. float3 d = abs(p) - b;
  528. return min(max(d.x, max(d.y, d.z)), 0.0) + length(max(d, 0.0));
  529. }
  530. float sdEllipsoid(in float3 p, in float3 r)
  531. {
  532. return (length(p / r) - 1.0) * min(min(r.x, r.y), r.z);
  533. }
  534. float udRoundBox(float3 p, float3 b, float r)
  535. {
  536. return length(max(abs(p) - b, 0.0)) - r;
  537. }
  538. float sdTorus(float3 p, float2 t)
  539. {
  540. float2 q = float2(length(p.xz) - t.x, p.y);
  541. return length(q) - t.y;
  542. }
  543. float sdHexPrism(float3 p, float2 h)
  544. {
  545. float3 q = abs(p);
  546. float d1 = q.z - h.y;
  547. float d2 = max((q.x * 0.866025 + q.y * 0.5), q.y) - h.x;
  548. return length(max(float2(d1, d2), 0.0)) + min(max(d1, d2), 0.);
  549. }
  550. float sdCapsule(float3 p, float3 a, float3 b, float r)
  551. {
  552. float3 pa = p - a, ba = b - a;
  553. float h = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);
  554. return length(pa - ba * h) - r;
  555. }
  556. float sdEquilateralTriangle(in float2 p)
  557. {
  558. const float k = 1.73205;
  559. p.x = abs(p.x) - 1.0;
  560. p.y = p.y + 1.0 / k;
  561. if (p.x + k * p.y > 0.0) p = float2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
  562. p.x += 2.0 - 2.0 * clamp((p.x + 2.0) / 2.0, 0.0, 1.0);
  563. return -length(p) * sign(p.y);
  564. }
  565. float sdTriPrism(float3 p, float2 h)
  566. {
  567. float3 q = abs(p);
  568. float d1 = q.z - h.y;
  569. float d2 = max(q.x * 0.866025 + p.y * 0.5, -p.y) - h.x * 0.5;
  570. return length(max(float2(d1, d2), 0.0)) + min(max(d1, d2), 0.);
  571. }
  572. float sdCylinder(float3 p, float2 h)
  573. {
  574. float2 d = abs(float2(length(p.xz), p.y)) - h;
  575. return min(max(d.x, d.y), 0.0) + length(max(d, 0.0));
  576. }
  577. float sdCone(in float3 p, in float3 c)
  578. {
  579. float2 q = float2(length(p.xz), p.y);
  580. float d1 = -q.y - c.z;
  581. float d2 = max(dot(q, c.xy), q.y);
  582. return length(max(float2(d1, d2), 0.0)) + min(max(d1, d2), 0.);
  583. }
  584. float sdConeSection(in float3 p, in float h, in float r1, in float r2)
  585. {
  586. float d1 = -p.y - h;
  587. float q = p.y - h;
  588. float si = 0.5 * (r1 - r2) / h;
  589. float d2 = max(sqrt(dot(p.xz, p.xz) * (1.0 - si * si)) + q * si - r2, q);
  590. return length(max(float2(d1, d2), 0.0)) + min(max(d1, d2), 0.);
  591. }
  592. float sdOctahedron(float3 p, float3 h)
  593. {
  594. float d = 0.0;
  595. d = dot(float2(max(abs(p.x), abs(p.z)), abs(p.y)),
  596. float2(h.x, h.y));
  597. return d - h.y * h.z;
  598. }
  599. float sdPyramid(float3 p, float3 h)
  600. {
  601. float octa = sdOctahedron(p, h);
  602. return opS(octa, p.y);
  603. }
  604. float length_toPowNegative6(float2 p)
  605. {
  606. p = p * p * p;
  607. p = p * p;
  608. return pow(p.x + p.y, 1.0 / 6.0);
  609. }
  610. float length_toPowNegative8(float2 p)
  611. {
  612. p = p * p; p = p * p; p = p * p;
  613. return pow(p.x + p.y, 1.0 / 8.0);
  614. }
  615. float sdTorus82(float3 p, float2 t)
  616. {
  617. float2 q = float2(length(p.xz) - t.x, p.y);
  618. return length_toPowNegative8(q) - t.y;
  619. }
  620. float sdTorus88(float3 p, float2 t)
  621. {
  622. float2 q = float2(length_toPowNegative8(p.xz) - t.x, p.y);
  623. return length_toPowNegative8(q) - t.y;
  624. }
  625. float sdCylinder6(float3 p, float2 h)
  626. {
  627. return max(length_toPowNegative6(p.xz) - h.x, abs(p.y) - h.y);
  628. }
  629. float3 sdCalculateNormal(in float3 pos, in SignedDistancePrimitive_Enum sdPrimitive)
  630. {
  631. float2 e = float2(1.0, -1.0) * 0.5773 * 0.0001;
  632. return normalize(
  633. e.xyy * GetDistanceFromSignedDistancePrimitive(pos + e.xyy, sdPrimitive) +
  634. e.yyx * GetDistanceFromSignedDistancePrimitive(pos + e.yyx, sdPrimitive) +
  635. e.yxy * GetDistanceFromSignedDistancePrimitive(pos + e.yxy, sdPrimitive) +
  636. e.xxx * GetDistanceFromSignedDistancePrimitive(pos + e.xxx, sdPrimitive));
  637. }
  638. bool RaySignedDistancePrimitiveTest(in Ray ray, in SignedDistancePrimitive_Enum sdPrimitive, out float thit, out ProceduralPrimitiveAttributes attr, in float stepScale = 1.0f)
  639. {
  640. const float threshold = 0.0001;
  641. float t = RayTMin();
  642. const uint MaxSteps = 512;
  643. uint i = 0;
  644. while (i++ < MaxSteps && t <= RayTCurrent())
  645. {
  646. float3 position = ray.origin + t * ray.direction;
  647. float distance = GetDistanceFromSignedDistancePrimitive(position, sdPrimitive);
  648. if (distance <= threshold * t)
  649. {
  650. float3 hitSurfaceNormal = sdCalculateNormal(position, sdPrimitive);
  651. if (IsAValidHit(ray, t, hitSurfaceNormal))
  652. {
  653. thit = t;
  654. attr.normal = hitSurfaceNormal;
  655. return true;
  656. }
  657. }
  658. t += stepScale * distance;
  659. }
  660. return false;
  661. }
  662. float CheckersTextureBoxFilter(in float2 uv, in float2 dpdx, in float2 dpdy, in uint ratio)
  663. {
  664. float2 w = max(abs(dpdx), abs(dpdy));
  665. float2 a = uv + 0.5*w;
  666. float2 b = uv - 0.5*w;
  667. float2 i = (floor(a) + min(frac(a)*ratio, 1.0) -
  668. floor(b) - min(frac(b)*ratio, 1.0)) / (ratio*w);
  669. return (1.0 - i.x)*(1.0 - i.y);
  670. }
  671. float sdFractalPyramid(in float3 position, float3 h, in float Scale = 2.0f)
  672. {
  673. float a = h.z * h.y / h.x;
  674. float3 v1 = float3(0, h.z, 0);
  675. float3 v2 = float3(-a, 0, a);
  676. float3 v3 = float3(a, 0, -a);
  677. float3 v4 = float3(a, 0, a);
  678. float3 v5 = float3(-a, 0, -a);
  679. int n = 0;
  680. for (n = 0; n < 4; n++)
  681. {
  682. float dist, d;
  683. float3 v;
  684. v = v1; dist = length_toPow2(position - v1);
  685. d = length_toPow2(position - v2); if (d < dist) { v = v2; dist = d; }
  686. d = length_toPow2(position - v3); if (d < dist) { v = v3; dist = d; }
  687. d = length_toPow2(position - v4); if (d < dist) { v = v4; dist = d; }
  688. d = length_toPow2(position - v5); if (d < dist) { v = v5; dist = d; }
  689. position = Scale * position - v * (Scale - 1.0);
  690. }
  691. float distance = sdPyramid(position, h);
  692. return distance * pow(Scale, float(-n));
  693. }
  694. bool RayAnalyticGeometryIntersectionTest(in Ray ray, in AnalyticPrimitive_Enum analyticPrimitive, out float thit, out ProceduralPrimitiveAttributes attr)
  695. {
  696. float3 aabb[2] = {
  697. float3(-1,-1,-1),
  698. float3(1,1,1)
  699. };
  700. float tmax;
  701. switch (analyticPrimitive)
  702. {
  703. case AnalyticPrimitive::AABB: return RayAABBIntersectionTest(ray, aabb, thit, attr);
  704. case AnalyticPrimitive::Spheres: return RaySpheresIntersectionTest(ray, thit, attr);
  705. default: return false;
  706. }
  707. }
  708. bool RayVolumetricGeometryIntersectionTest(in Ray ray, in VolumetricPrimitive_Enum volumetricPrimitive, out float thit, out ProceduralPrimitiveAttributes attr, in float elapsedTime)
  709. {
  710. switch (volumetricPrimitive)
  711. {
  712. case VolumetricPrimitive_Metaballs: return RayMetaballsIntersectionTest(ray, thit, attr, elapsedTime);
  713. default: return false;
  714. }
  715. }
  716. float GetDistanceFromSignedDistancePrimitive(in float3 position, in SignedDistancePrimitive_Enum signedDistancePrimitive)
  717. {
  718. switch (signedDistancePrimitive)
  719. {
  720. case SignedDistancePrimitive::MiniSpheres:
  721. return opI(sdSphere(opRep(position + 1, (float3) 2/4), 0.65 / 4), sdBox(position, (float3)1));
  722. case SignedDistancePrimitive::IntersectedRoundCube:
  723. return opS(opS(udRoundBox(position, (float3) 0.75, 0.2), sdSphere(position, 1.20)), -sdSphere(position, 1.32));
  724. case SignedDistancePrimitive::SquareTorus:
  725. return sdTorus82(position, float2(0.75, 0.15));
  726. case SignedDistancePrimitive::TwistedTorus:
  727. return sdTorus(opTwist(position), float2(0.6, 0.2));
  728. case SignedDistancePrimitive::Cog:
  729. return opS( sdTorus82(position, float2(0.60, 0.3)),
  730. sdCylinder(opRep(float3(atan2(position.z, position.x) / 6.2831,
  731. 1,
  732. 0.015 + 0.25 * length(position)) + 1,
  733. float3(0.05, 1, 0.075)),
  734. float2(0.02, 0.8)));
  735. case SignedDistancePrimitive::Cylinder:
  736. return opI(sdCylinder(opRep(position + float3(1, 1, 1), float3(1, 2, 1)), float2(0.3, 2)),
  737. sdBox(position + float3(1, 1, 1), float3(2, 2, 2)));
  738. case SignedDistancePrimitive::FractalPyramid:
  739. return sdFractalPyramid(position + float3(0, 1, 0), float3(0.894, 0.447, 2.0), 2.0f);
  740. default: return 0;
  741. }
  742. }
  743. // ShaderResourceGroup
  744. ShaderResourceGroupSemantic RaySemantic
  745. {
  746. FrequencyId = 0u;
  747. };
  748. ShaderResourceGroup RaySRG : RaySemantic
  749. {
  750. RaytracingAccelerationStructure g_scene; // : register(t0, space0);
  751. RWTexture2D<float4> g_renderTarget; // : register(u0);
  752. ConstantBuffer<SceneConstantBuffer> g_sceneCB; // : register(b0);
  753. ByteAddressBuffer g_indices; // : register(t1, space0);
  754. StructuredBuffer<Vertex> g_vertices; // : register(t2, space0);
  755. StructuredBuffer<PrimitiveInstancePerFrameBuffer> g_AABBPrimitiveAttributes; // : register(t3, space0);
  756. ConstantBuffer<PrimitiveConstantBuffer> l_materialCB; // : register(b1);
  757. ConstantBuffer<PrimitiveInstanceConstantBuffer> l_aabbCB; // : register(b2);
  758. };
  759. float CalculateDiffuseCoefficient(in float3 hitPosition, in float3 incidentLightRay, in float3 normal)
  760. {
  761. float fNDotL = saturate(dot(-incidentLightRay, normal));
  762. return fNDotL;
  763. }
  764. float4 CalculateSpecularCoefficient(in float3 hitPosition, in float3 incidentLightRay, in float3 normal, in float specularPower)
  765. {
  766. float3 reflectedLightRay = normalize(reflect(incidentLightRay, normal));
  767. return pow(saturate(dot(reflectedLightRay, normalize (-WorldRayDirection()))), specularPower);
  768. }
  769. float4 CalculatePhongLighting(in float4 albedo, in float3 normal, in bool isInShadow, in float diffuseCoef = 1.0, in float specularCoef = 1.0, in float specularPower = 50)
  770. {
  771. float3 hitPosition = HitWorldPosition();
  772. float3 lightPosition = g_sceneCB.lightPosition.xyz;
  773. float shadowFactor = isInShadow ? InShadowRadiance : 1.0;
  774. float3 incidentLightRay = normalize(hitPosition - lightPosition);
  775. float4 lightDiffuseColor = g_sceneCB.lightDiffuseColor;
  776. float Kd = CalculateDiffuseCoefficient(hitPosition, incidentLightRay, normal);
  777. float4 diffuseColor = shadowFactor * diffuseCoef * Kd * lightDiffuseColor * albedo;
  778. float4 specularColor = float4(0, 0, 0, 0);
  779. if (!isInShadow)
  780. {
  781. float4 lightSpecularColor = float4(1, 1, 1, 1);
  782. float4 Ks = CalculateSpecularCoefficient(hitPosition, incidentLightRay, normal, specularPower);
  783. specularColor = specularCoef * Ks * lightSpecularColor;
  784. }
  785. float4 ambientColor = g_sceneCB.lightAmbientColor;
  786. float4 ambientColorMin = g_sceneCB.lightAmbientColor - 0.1;
  787. float4 ambientColorMax = g_sceneCB.lightAmbientColor;
  788. float a = 1 - saturate(dot(normal, float3(0, -1, 0)));
  789. ambientColor = albedo * lerp(ambientColorMin, ambientColorMax, a);
  790. return ambientColor + diffuseColor + specularColor;
  791. }
  792. float4 TraceRadianceRay(in Ray ray, in uint currentRayRecursionDepth)
  793. {
  794. if (currentRayRecursionDepth >= 3)
  795. {
  796. return float4(0, 0, 0, 0);
  797. }
  798. RayDesc rayDesc;
  799. rayDesc.Origin = ray.origin;
  800. rayDesc.Direction = ray.direction;
  801. rayDesc.TMin = 0;
  802. rayDesc.TMax = 10000;
  803. RayPayload rayPayload = { float4(0, 0, 0, 0), currentRayRecursionDepth + 1 };
  804. TraceRay(g_scene,
  805. RAY_FLAG_CULL_BACK_FACING_TRIANGLES,
  806. TraceRayParameters_InstanceMask,
  807. TraceRayParameters_HitGroup_Offset[RayType_Radiance],
  808. TraceRayParameters_HitGroup_GeometryStride,
  809. TraceRayParameters_MissShader_Offset[RayType_Radiance],
  810. rayDesc, rayPayload);
  811. return rayPayload.color;
  812. }
  813. bool TraceShadowRayAndReportIfHit(in Ray ray, in uint currentRayRecursionDepth)
  814. {
  815. if (currentRayRecursionDepth >= 3)
  816. {
  817. return false;
  818. }
  819. RayDesc rayDesc;
  820. rayDesc.Origin = ray.origin;
  821. rayDesc.Direction = ray.direction;
  822. rayDesc.TMin = 0;
  823. rayDesc.TMax = 10000;
  824. ShadowRayPayload shadowPayload = { true };
  825. TraceRay(g_scene,
  826. RAY_FLAG_CULL_BACK_FACING_TRIANGLES
  827. | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH
  828. | RAY_FLAG_FORCE_OPAQUE
  829. | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER,
  830. TraceRayParameters_InstanceMask,
  831. TraceRayParameters_HitGroup_Offset[RayType_Shadow],
  832. TraceRayParameters_HitGroup_GeometryStride,
  833. TraceRayParameters_MissShader_Offset[RayType_Shadow],
  834. rayDesc, shadowPayload);
  835. return shadowPayload.hit;
  836. }
  837. [shader("raygeneration")]
  838. void MyRaygenShader()
  839. {
  840. Ray ray = GenerateCameraRay(DispatchRaysIndex().xy, g_sceneCB.cameraPosition.xyz, g_sceneCB.projectionToWorld);
  841. uint currentRecursionDepth = 0;
  842. float4 color = TraceRadianceRay(ray, currentRecursionDepth);
  843. g_renderTarget[DispatchRaysIndex().xy] = color;
  844. }
  845. [shader("closesthit")]
  846. void MyClosestHitShader_Triangle(inout RayPayload rayPayload, in BuiltInTriangleIntersectionAttributes attr)
  847. {
  848. uint indexSizeInBytes = 2;
  849. uint indicesPerTriangle = 3;
  850. uint triangleIndexStride = indicesPerTriangle * indexSizeInBytes;
  851. uint baseIndex = PrimitiveIndex() * triangleIndexStride;
  852. const uint3 indices_ = Load3x16BitIndices(baseIndex, g_indices);
  853. float3 triangleNormal = g_vertices[indices_[0]].normal;
  854. float3 hitPosition = HitWorldPosition();
  855. Ray shadowRay = { hitPosition, normalize(g_sceneCB.lightPosition.xyz - hitPosition) };
  856. bool shadowRayHit = TraceShadowRayAndReportIfHit(shadowRay, rayPayload.recursionDepth);
  857. float checkers = AnalyticalCheckersTexture(HitWorldPosition(), triangleNormal, g_sceneCB.cameraPosition.xyz, g_sceneCB.projectionToWorld);
  858. float4 reflectedColor = float4(0, 0, 0, 0);
  859. if (l_materialCB.reflectanceCoef > 0.001 )
  860. {
  861. Ray reflectionRay = { HitWorldPosition(), reflect(WorldRayDirection(), triangleNormal) };
  862. float4 reflectionColor = TraceRadianceRay(reflectionRay, rayPayload.recursionDepth);
  863. float3 fresnelR = FresnelReflectanceSchlick(WorldRayDirection(), triangleNormal, l_materialCB.albedo.xyz);
  864. reflectedColor = l_materialCB.reflectanceCoef * float4(fresnelR, 1) * reflectionColor;
  865. }
  866. float4 phongColor = CalculatePhongLighting(l_materialCB.albedo, triangleNormal, shadowRayHit, l_materialCB.diffuseCoef, l_materialCB.specularCoef, l_materialCB.specularPower);
  867. float4 color = checkers * (phongColor + reflectedColor);
  868. float t = RayTCurrent();
  869. color = lerp(color, BackgroundColor, 1.0 - exp(-0.000002*t*t*t));
  870. rayPayload.color = color;
  871. }
  872. [shader("closesthit")]
  873. void MyClosestHitShader_AABB(inout RayPayload rayPayload, in ProceduralPrimitiveAttributes attr)
  874. {
  875. float3 hitPosition = HitWorldPosition();
  876. Ray shadowRay = { hitPosition, normalize(g_sceneCB.lightPosition.xyz - hitPosition) };
  877. bool shadowRayHit = TraceShadowRayAndReportIfHit(shadowRay, rayPayload.recursionDepth);
  878. float4 reflectedColor = float4(0, 0, 0, 0);
  879. if (l_materialCB.reflectanceCoef > 0.001)
  880. {
  881. Ray reflectionRay = { HitWorldPosition(), reflect(WorldRayDirection(), attr.normal) };
  882. float4 reflectionColor = TraceRadianceRay(reflectionRay, rayPayload.recursionDepth);
  883. float3 fresnelR = FresnelReflectanceSchlick(WorldRayDirection(), attr.normal, l_materialCB.albedo.xyz);
  884. reflectedColor = l_materialCB.reflectanceCoef * float4(fresnelR, 1) * reflectionColor;
  885. }
  886. float4 phongColor = CalculatePhongLighting(l_materialCB.albedo, attr.normal, shadowRayHit, l_materialCB.diffuseCoef, l_materialCB.specularCoef, l_materialCB.specularPower);
  887. float4 color = phongColor + reflectedColor;
  888. float t = RayTCurrent();
  889. color = lerp(color, BackgroundColor, 1.0 - exp(-0.000002*t*t*t));
  890. rayPayload.color = color;
  891. }
  892. [shader("miss")]
  893. void MyMissShader(inout RayPayload rayPayload)
  894. {
  895. float4 backgroundColor = float4(BackgroundColor);
  896. rayPayload.color = backgroundColor;
  897. }
  898. [shader("miss")]
  899. void MyMissShader_ShadowRay(inout ShadowRayPayload rayPayload)
  900. {
  901. rayPayload.hit = false;
  902. }
  903. Ray GetRayInAABBPrimitiveLocalSpace()
  904. {
  905. PrimitiveInstancePerFrameBuffer attr = g_AABBPrimitiveAttributes[l_aabbCB.instanceIndex];
  906. Ray ray;
  907. ray.origin = mul(float4(ObjectRayOrigin(), 1), attr.bottomLevelASToLocalSpace).xyz;
  908. ray.direction = mul(ObjectRayDirection(), (float3x3) attr.bottomLevelASToLocalSpace);
  909. return ray;
  910. }
  911. [shader("intersection")]
  912. void MyIntersectionShader_AnalyticPrimitive()
  913. {
  914. Ray localRay = GetRayInAABBPrimitiveLocalSpace();
  915. AnalyticPrimitive_Enum primitiveType = (AnalyticPrimitive_Enum) l_aabbCB.primitiveType;
  916. float thit;
  917. ProceduralPrimitiveAttributes attr;
  918. if (RayAnalyticGeometryIntersectionTest(localRay, primitiveType, thit, attr))
  919. {
  920. PrimitiveInstancePerFrameBuffer aabbAttribute = g_AABBPrimitiveAttributes[l_aabbCB.instanceIndex];
  921. attr.normal = mul(attr.normal, (float3x3) aabbAttribute.localSpaceToBottomLevelAS);
  922. attr.normal = normalize(mul((float3x3) ObjectToWorld3x4(), attr.normal));
  923. ReportHit(thit, 0, attr);
  924. }
  925. }
  926. [shader("intersection")]
  927. void MyIntersectionShader_VolumetricPrimitive()
  928. {
  929. Ray localRay = GetRayInAABBPrimitiveLocalSpace();
  930. VolumetricPrimitive_Enum primitiveType = (VolumetricPrimitive_Enum) l_aabbCB.primitiveType;
  931. float thit;
  932. ProceduralPrimitiveAttributes attr;
  933. if (RayVolumetricGeometryIntersectionTest(localRay, primitiveType, thit, attr, g_sceneCB.elapsedTime))
  934. {
  935. PrimitiveInstancePerFrameBuffer aabbAttribute = g_AABBPrimitiveAttributes[l_aabbCB.instanceIndex];
  936. attr.normal = mul(attr.normal, (float3x3) aabbAttribute.localSpaceToBottomLevelAS);
  937. attr.normal = normalize(mul((float3x3) ObjectToWorld3x4(), attr.normal));
  938. ReportHit(thit, 0, attr);
  939. }
  940. }
  941. [shader("intersection")]
  942. void MyIntersectionShader_SignedDistancePrimitive()
  943. {
  944. Ray localRay = GetRayInAABBPrimitiveLocalSpace();
  945. SignedDistancePrimitive_Enum primitiveType = (SignedDistancePrimitive_Enum) l_aabbCB.primitiveType;
  946. float thit;
  947. ProceduralPrimitiveAttributes attr;
  948. if (RaySignedDistancePrimitiveTest(localRay, primitiveType, thit, attr, l_materialCB.stepScale))
  949. {
  950. PrimitiveInstancePerFrameBuffer aabbAttribute = g_AABBPrimitiveAttributes[l_aabbCB.instanceIndex];
  951. attr.normal = mul(attr.normal, (float3x3) aabbAttribute.localSpaceToBottomLevelAS);
  952. attr.normal = normalize(mul((float3x3) ObjectToWorld3x4(), attr.normal));
  953. ReportHit(thit, 0, attr);
  954. }
  955. }