CustomRayGen.hlsl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "Common.hlsl"
  2. // Raytracing output texture, accessed as a UAV
  3. RWTexture2D<float4> gOutput : register(u0);
  4. RWTexture2D<float4> guiTexture : register(u1);
  5. // Raytracing acceleration structure, accessed as a SRV
  6. RaytracingAccelerationStructure TLAS : register(t0);
  7. cbuffer RayGenerationSettings : register(b0)
  8. {
  9. int renderGui;
  10. int useRays;
  11. float minDistance;
  12. float maxDistance;
  13. float4x4 inverseView;
  14. float4x4 inverseProjection;
  15. }
  16. [shader("raygeneration")]
  17. void RayGen()
  18. {
  19. // Initialize the ray payload
  20. HitInfo rayPayload;
  21. rayPayload.hitCount = 0;
  22. float3 color = float3(0, 0, 0);
  23. // Get the location within the dispatched 2D grid of work items
  24. // (often maps to pixels, so this could represent a pixel coordinate).
  25. float2 dispatchDimensions = float2(DispatchRaysDimensions().xy);
  26. uint2 dispatchIndex = DispatchRaysIndex().xy;
  27. float2 dispatchPercentage = (dispatchIndex + 0.5) / dispatchDimensions;
  28. if (useRays)
  29. {
  30. float2 d = (dispatchPercentage * 2.f - 1.f);
  31. RayDesc ray;
  32. ray.Origin = mul(inverseView, float4(0, 0, 0, 1)).xyz;
  33. float4 target = mul(inverseProjection, float4(d.x, -d.y, -1, 1));
  34. target.w = 1;
  35. ray.Direction = mul(inverseView, target).xyz - ray.Origin;
  36. ray.TMin = minDistance;
  37. ray.TMax = maxDistance;
  38. TraceRay(TLAS, /*RayFlags*/0, /*InstanceInclusionMask*/0xFF, /*RayContributionToHitGroupIndex*/0,
  39. /*MultiplierForGeometryContributionToHitGroupIndex*/0, /*MissShaderIndex*/0, ray, rayPayload);
  40. float dayLightFactor = 1.f; // TODO: set this based on time
  41. float3 minLight = float3(0.1f, 0.1f, 0.1f);
  42. float3 dayLight = unpackLight(rayPayload.dayLight[rayPayload.hitCount - 1]);
  43. float3 dynamicLight = unpackLight(rayPayload.dynamicLight[rayPayload.hitCount - 1]);
  44. float3 light = max(minLight, max(dynamicLight, dayLight * dayLightFactor));
  45. color = rayPayload.color[rayPayload.hitCount - 1].rgb * light;
  46. for (int i = rayPayload.hitCount - 2; i >= 0; i--)
  47. {
  48. dayLight = unpackLight(rayPayload.dayLight[i]);
  49. dynamicLight = unpackLight(rayPayload.dynamicLight[i]);
  50. light = max(minLight, max(dynamicLight, dayLight * dayLightFactor));
  51. color = rayPayload.color[i].rgb * light * rayPayload.color[i].a + color * (1 - rayPayload.color[i].a);
  52. }
  53. }
  54. uint outWidth, outHeight;
  55. gOutput.GetDimensions(outWidth, outHeight);
  56. uint2 outputIndex = uint2(dispatchPercentage * float2(outWidth, outHeight));
  57. if (renderGui)
  58. {
  59. uint guiWidth, guiHeight;
  60. guiTexture.GetDimensions(guiWidth, guiHeight);
  61. uint2 guiIndex = uint2(dispatchPercentage * float2(guiWidth, guiHeight));
  62. float4 guiColor = guiTexture[guiIndex];
  63. color = color * (1 - guiColor.a) + guiColor.rgb * guiColor.a;
  64. }
  65. gOutput[outputIndex] = float4(color, 1.f);
  66. }