Ei kuvausta
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.

FetchGeometry.hlsl 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef _UNIFIEDRAYTRACING_FETCHGEOMETRY_HLSL_
  2. #define _UNIFIEDRAYTRACING_FETCHGEOMETRY_HLSL_
  3. #include "Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Common/GeometryPool/GeometryPoolDefs.cs.hlsl"
  4. #include "Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Common/GeometryPool/GeometryPool.hlsl"
  5. #include "Packages/com.unity.rendering.light-transport/Runtime/UnifiedRayTracing/Bindings.hlsl"
  6. #define INTERPOLATE_ATTRIBUTE(attr, barCoords) v.attr = v0.attr * (1.0 - barCoords.x - barCoords.y) + v1.attr * barCoords.x + v2.attr * barCoords.y
  7. StructuredBuffer<UnifiedRT::InstanceData> g_AccelStructInstanceList;
  8. StructuredBuffer<uint> g_globalIndexBuffer;
  9. StructuredBuffer<uint> g_globalVertexBuffer;
  10. int g_globalVertexBufferStride;
  11. StructuredBuffer<GeoPoolMeshChunk> g_MeshList;
  12. namespace UnifiedRT {
  13. namespace Internal {
  14. GeoPoolVertex InterpolateVertices(GeoPoolVertex v0, GeoPoolVertex v1, GeoPoolVertex v2, float2 barycentricCoords)
  15. {
  16. GeoPoolVertex v;
  17. INTERPOLATE_ATTRIBUTE(pos, barycentricCoords);
  18. INTERPOLATE_ATTRIBUTE(N, barycentricCoords);
  19. INTERPOLATE_ATTRIBUTE(uv0, barycentricCoords);
  20. INTERPOLATE_ATTRIBUTE(uv1, barycentricCoords);
  21. return v;
  22. }
  23. uint3 FetchTriangleIndices(GeoPoolMeshChunk meshInfo, uint triangleID)
  24. {
  25. return uint3(
  26. g_globalIndexBuffer[meshInfo.indexOffset + 3 * triangleID],
  27. g_globalIndexBuffer[meshInfo.indexOffset + 3 * triangleID + 1],
  28. g_globalIndexBuffer[meshInfo.indexOffset + 3 * triangleID + 2]);
  29. }
  30. GeoPoolVertex FetchVertex(GeoPoolMeshChunk meshInfo, uint vertexIndex)
  31. {
  32. GeoPoolVertex v;
  33. GeometryPool::LoadVertex(meshInfo.vertexOffset + (int)vertexIndex, 0, g_globalVertexBuffer, v);
  34. return v;
  35. }
  36. }
  37. static const uint kGeomAttribPosition = 1 << 0;
  38. static const uint kGeomAttribNormal = 1 << 1;
  39. static const uint kGeomAttribTexCoord0 = 1 << 4;
  40. static const uint kGeomAttribTexCoord1 = 1 << 8;
  41. static const uint kGeomAttribFaceNormal = 1 << 16;
  42. static const uint kGeomAttribAll = 0xFFFFFFFF;
  43. struct HitGeomAttributes
  44. {
  45. float3 position;
  46. float3 normal;
  47. float3 faceNormal;
  48. float4 uv0;
  49. float4 uv1;
  50. };
  51. HitGeomAttributes FetchHitGeomAttributes(int geometryIndex, int primitiveIndex, float2 uvBarycentrics, uint attributesToFetch = kGeomAttribAll)
  52. {
  53. HitGeomAttributes result = (HitGeomAttributes)0;
  54. GeoPoolMeshChunk meshInfo = g_MeshList[geometryIndex];
  55. uint3 triangleVertexIndices = Internal::FetchTriangleIndices(meshInfo, primitiveIndex);
  56. GeoPoolVertex v0, v1, v2;
  57. v0 = Internal::FetchVertex(meshInfo, triangleVertexIndices.x);
  58. v1 = Internal::FetchVertex(meshInfo, triangleVertexIndices.y);
  59. v2 = Internal::FetchVertex(meshInfo, triangleVertexIndices.z);
  60. GeoPoolVertex v = Internal::InterpolateVertices(v0, v1, v2, uvBarycentrics);
  61. if (attributesToFetch & kGeomAttribFaceNormal)
  62. result.faceNormal = cross(v1.pos - v0.pos, v2.pos - v0.pos);
  63. if (attributesToFetch & kGeomAttribPosition)
  64. result.position = v.pos;
  65. if (attributesToFetch & kGeomAttribNormal)
  66. result.normal = v.N;
  67. if (attributesToFetch & kGeomAttribTexCoord0)
  68. result.uv0 = v.uv0;
  69. if (attributesToFetch & kGeomAttribTexCoord1)
  70. result.uv1 = v.uv1;
  71. return result;
  72. }
  73. HitGeomAttributes FetchHitGeomAttributes(Hit hit, uint attributesToFetch = kGeomAttribAll)
  74. {
  75. int geometryIndex = g_AccelStructInstanceList[hit.instanceID].geometryIndex;
  76. return FetchHitGeomAttributes(geometryIndex, hit.primitiveIndex, hit.uvBarycentrics, attributesToFetch);
  77. }
  78. HitGeomAttributes FetchHitGeomAttributesInWorldSpace(UnifiedRT::InstanceData instanceInfo, UnifiedRT::Hit hit)
  79. {
  80. UnifiedRT::HitGeomAttributes res = UnifiedRT::FetchHitGeomAttributes(hit);
  81. HitGeomAttributes wsRes = res;
  82. wsRes.position = mul(instanceInfo.localToWorld, float4(res.position, 1)).xyz;
  83. wsRes.normal = normalize(mul((float3x3)instanceInfo.localToWorldNormals, res.normal));
  84. wsRes.faceNormal = normalize(mul((float3x3)instanceInfo.localToWorldNormals, res.faceNormal));
  85. return wsRes;
  86. }
  87. InstanceData GetInstance(uint instanceID)
  88. {
  89. return g_AccelStructInstanceList[instanceID];
  90. }
  91. } // namespace UnifiedRT
  92. #endif // UNIFIEDRAYTRACING_FETCH_GEOMETRY_HLSL