ray-tracing-hello-world.azsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // AZSL version of the example raytracing shader found here:
  2. // https://github.com/microsoft/DirectX-Graphics-Samples/blob/master/Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingHelloWorld/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. ShaderResourceGroupSemantic RaySemantic
  29. {
  30. FrequencyId = 0u;
  31. };
  32. struct Viewport
  33. {
  34. float left;
  35. float top;
  36. float right;
  37. float bottom;
  38. };
  39. struct RayGenConstantBuffer
  40. {
  41. Viewport viewport;
  42. Viewport stencil;
  43. };
  44. ShaderResourceGroup RaySRG : RaySemantic
  45. {
  46. RaytracingAccelerationStructure Scene; // : register(t0, space0);
  47. RWTexture2D<float4> RenderTarget; // : register(u0);
  48. ConstantBuffer<RayGenConstantBuffer> g_rayGenCB; // : register(b0);
  49. };
  50. typedef BuiltInTriangleIntersectionAttributes MyAttributes;
  51. struct RayPayload
  52. {
  53. float4 color;
  54. };
  55. bool IsInsideViewport(float2 p, Viewport viewport)
  56. {
  57. return (p.x >= viewport.left && p.x <= viewport.right)
  58. && (p.y >= viewport.top && p.y <= viewport.bottom);
  59. }
  60. [shader("raygeneration")]
  61. void MyRaygenShader()
  62. {
  63. float2 lerpValues = (float2)DispatchRaysIndex() / (float2)DispatchRaysDimensions();
  64. // Orthographic projection since we're raytracing in screen space.
  65. float3 rayDir = float3(0, 0, 1);
  66. float3 origin = float3(
  67. lerp(RaySRG::g_rayGenCB.viewport.left, RaySRG::g_rayGenCB.viewport.right, lerpValues.x),
  68. lerp(RaySRG::g_rayGenCB.viewport.top, RaySRG::g_rayGenCB.viewport.bottom, lerpValues.y),
  69. 0.0f);
  70. if (IsInsideViewport(origin.xy, RaySRG::g_rayGenCB.stencil))
  71. {
  72. // Trace the ray.
  73. // Set the ray's extents.
  74. RayDesc ray;
  75. ray.Origin = origin;
  76. ray.Direction = rayDir;
  77. // Set TMin to a non-zero small value to avoid aliasing issues due to floating - point errors.
  78. // TMin should be kept small to prevent missing geometry at close contact areas.
  79. ray.TMin = 0.001;
  80. ray.TMax = 10000.0;
  81. RayPayload Payload = { float4(0, 0, 0, 0) };
  82. TraceRay(RaySRG::Scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, ~0, 0, 1, 0, ray, Payload);
  83. // Write the raytraced color to the output texture.
  84. RaySRG::RenderTarget[DispatchRaysIndex().xy] = Payload.color;
  85. }
  86. else
  87. {
  88. // Render interpolated DispatchRaysIndex outside the stencil window
  89. RaySRG::RenderTarget[DispatchRaysIndex().xy] = float4(lerpValues, 0, 1);
  90. }
  91. }
  92. [shader("closesthit")]
  93. void MyClosestHitShader(inout RayPayload a_payload, in MyAttributes attr)
  94. {
  95. float3 barycentrics = float3(1 - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
  96. a_payload.color = float4(barycentrics, 1);
  97. }
  98. [shader("miss")]
  99. void MyMissShader(inout RayPayload a_payload)
  100. {
  101. a_payload.color = float4(0, 0, 0, 1);
  102. }