RayGen.hlsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "Common.hlsl"
  2. [shader("raygeneration")]
  3. void RayGen()
  4. {
  5. // Initialize the ray payload
  6. HitInfo rayPayload;
  7. rayPayload.hitCount = 0;
  8. float4 color = float4(0, 0, 0, 0);
  9. // Get the location within the dispatched 2D grid of work items
  10. // (often maps to pixels, so this could represent a pixel coordinate).
  11. float2 dispatchDimensions = float2(DispatchRaysDimensions().xy);
  12. uint2 dispatchIndex = DispatchRaysIndex().xy;
  13. float2 dispatchPercentage = (dispatchIndex + 0.5) / dispatchDimensions;
  14. if (useRays)
  15. {
  16. float2 d = (dispatchPercentage * 2.f - 1.f);
  17. RayDesc ray;
  18. ray.Origin = mul(inverseView, float4(0, 0, 0, 1)).xyz;
  19. float4 target = mul(inverseProjection, float4(d.x, -d.y, -1, 1));
  20. target.w = 1;
  21. ray.Direction = mul(inverseView, target).xyz - ray.Origin;
  22. ray.TMin = minDistance;
  23. ray.TMax = maxDistance;
  24. TraceRay(TLAS, /*RayFlags*/0, /*InstanceInclusionMask*/0xFF, /*RayContributionToHitGroupIndex*/0,
  25. /*MultiplierForGeometryContributionToHitGroupIndex*/0, /*MissShaderIndex*/0, ray, rayPayload);
  26. color = rayPayload.color[rayPayload.hitCount - 1];
  27. for (int i = rayPayload.hitCount - 2; i >= 0; i--)
  28. {
  29. color.rgb = rayPayload.color[i].rgb * rayPayload.color[i].a + color.rgb * (1 - rayPayload.color[i].a);
  30. }
  31. }
  32. uint outWidth, outHeight;
  33. gOutput.GetDimensions(outWidth, outHeight);
  34. uint2 outputIndex = uint2(dispatchPercentage * float2(outWidth, outHeight));
  35. if (renderGui)
  36. {
  37. uint guiWidth, guiHeight;
  38. guiTexture.GetDimensions(guiWidth, guiHeight);
  39. uint2 guiIndex = uint2(dispatchPercentage * float2(guiWidth, guiHeight));
  40. float4 guiColor = guiTexture[guiIndex];
  41. color.rgb = color.rgb * (1 - guiColor.a) + guiColor.rgb * guiColor.a;
  42. }
  43. gOutput[outputIndex] = float4(color.rgb, 1.f);
  44. }