暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TraceRay.hlsl 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #ifndef _UNIFIEDRAYTRACING_TRACERAY_HLSL_
  2. #define _UNIFIEDRAYTRACING_TRACERAY_HLSL_
  3. #include "Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Bindings.hlsl"
  4. namespace UnifiedRT {
  5. static const uint kRayFlagNone = 0x0;
  6. static const uint kRayFlagCullBackFacingTriangles = 0x10;
  7. static const uint kRayFlagCullFrontFacingTriangles = 0x20;
  8. #if defined(UNIFIED_RT_BACKEND_HARDWARE)
  9. Hit TraceRayClosestHit(DispatchInfo dispatchInfo, RayTracingAccelStruct accelStruct, uint instanceMask, Ray ray, uint rayFlags)
  10. {
  11. RayDesc rayDesc;
  12. rayDesc.Origin = ray.origin;
  13. rayDesc.TMin = ray.tMin;
  14. rayDesc.Direction = ray.direction;
  15. rayDesc.TMax = ray.tMax;
  16. Hit payload;
  17. TraceRay(accelStruct.accelStruct, RAY_FLAG_FORCE_OPAQUE | rayFlags, instanceMask, 0, 1, 0, rayDesc, payload);
  18. return payload;
  19. }
  20. bool TraceRayAnyHit(DispatchInfo dispatchInfo, RayTracingAccelStruct accelStruct, uint instanceMask, Ray ray, uint rayFlags)
  21. {
  22. RayDesc rayDesc;
  23. rayDesc.Origin = ray.origin;
  24. rayDesc.TMin = ray.tMin;
  25. rayDesc.Direction = ray.direction;
  26. rayDesc.TMax = ray.tMax;
  27. Hit payLoadShadow = (Hit)0;
  28. TraceRay(accelStruct.accelStruct, RAY_FLAG_SKIP_CLOSEST_HIT_SHADER | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | rayFlags, instanceMask, 0, 1, 0, rayDesc, payLoadShadow);
  29. return payLoadShadow.IsValid();
  30. }
  31. #elif defined(UNIFIED_RT_BACKEND_COMPUTE)
  32. int GetCullMode(uint rayFlags)
  33. {
  34. int cullMode = CULL_MODE_NONE;
  35. if (rayFlags & kRayFlagCullFrontFacingTriangles)
  36. cullMode = CULL_MODE_FRONTFACE;
  37. if (rayFlags & kRayFlagCullBackFacingTriangles)
  38. cullMode = CULL_MODE_BACKFACE;
  39. return cullMode;
  40. }
  41. Hit TraceRayClosestHit(DispatchInfo dispatchInfo, RayTracingAccelStruct accelStruct, uint instanceMask, Ray ray, uint rayFlags)
  42. {
  43. TraceParams traceParams;
  44. traceParams.bvh = accelStruct.bvh;
  45. traceParams.bottom_bvhs = accelStruct.bottom_bvhs;
  46. traceParams.stack = g_stack;
  47. traceParams.instance_infos = accelStruct.instance_infos;
  48. traceParams.globalThreadIndex = dispatchInfo.globalThreadIndex;
  49. traceParams.localThreadIndex = dispatchInfo.localThreadIndex;
  50. VertexPoolDesc vertex_pool_desc;
  51. vertex_pool_desc.index_buffer = accelStruct.indexBuffer;
  52. vertex_pool_desc.vertex_buffer = accelStruct.vertexBuffer;
  53. vertex_pool_desc.vertex_stride = accelStruct.vertexStride;
  54. int cull_mode = GetCullMode(rayFlags);
  55. TraceHitResult hitData = TraceRay(traceParams, vertex_pool_desc, ray.origin, ray.tMin, ray.direction, ray.tMax, instanceMask, cull_mode, true);
  56. Hit res;
  57. res.instanceID = hitData.inst_id != -1 ? GetUserInstanceID(traceParams, hitData.inst_id) : -1;
  58. res.primitiveIndex = hitData.prim_id;
  59. res.uvBarycentrics = hitData.uv;
  60. res.hitDistance = hitData.hit_distance;
  61. res.isFrontFace = hitData.front_face;
  62. return res;
  63. }
  64. bool TraceRayAnyHit(DispatchInfo dispatchInfo, RayTracingAccelStruct accelStruct, uint instanceMask, Ray ray, uint rayFlags)
  65. {
  66. TraceParams traceParams;
  67. traceParams.bvh = accelStruct.bvh;
  68. traceParams.bottom_bvhs = accelStruct.bottom_bvhs;
  69. traceParams.stack = g_stack;
  70. traceParams.instance_infos = accelStruct.instance_infos;
  71. traceParams.globalThreadIndex = dispatchInfo.globalThreadIndex;
  72. traceParams.localThreadIndex = dispatchInfo.localThreadIndex;
  73. VertexPoolDesc vertex_pool_desc;
  74. vertex_pool_desc.index_buffer = accelStruct.indexBuffer;
  75. vertex_pool_desc.vertex_buffer = accelStruct.vertexBuffer;
  76. vertex_pool_desc.vertex_stride = accelStruct.vertexStride;
  77. int cull_mode = GetCullMode(rayFlags);
  78. TraceHitResult hit = TraceRay(traceParams, vertex_pool_desc, ray.origin, ray.tMin, ray.direction, ray.tMax, instanceMask, cull_mode, false);
  79. return hit.inst_id != INVALID_NODE;
  80. }
  81. #endif
  82. } // namespace UnifiedRT
  83. #endif // UNIFIEDRAYTRACING_TRACERAY_HLSL