DefaultAnyHit.hlsl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "Common.hlsl"
  2. StructuredBuffer<int> textureIdBuffer : register(t1, space2);
  3. StructuredBuffer<int> indexBuffer : register(t1, space3);
  4. StructuredBuffer<VertexData> vertexData : register(t1, space4);
  5. StructuredBuffer<int> polygonSizeBuffer : register(t1, space5);
  6. [shader("anyhit")]
  7. void AnyHit(inout HitInfo rayPayload, SimpleBlocksAttributes attrib)
  8. {
  9. if (rayPayload.hitCount < 0)
  10. { // shadow ray
  11. rayPayload.hitCount = 0;
  12. // TODO: transparent objects should lead to half shadows
  13. AcceptHitAndEndSearch();
  14. return;
  15. }
  16. int instanceId = InstanceID();
  17. int currentTriangle = PrimitiveIndex();
  18. int textureId = 0;
  19. int i;
  20. for (i = 0; currentTriangle >= 0 && polygonSizeBuffer[i] > 0; i++)
  21. {
  22. currentTriangle -= polygonSizeBuffer[i];
  23. textureId = textureIdBuffer[i];
  24. }
  25. if (polygonSizeBuffer[i] <= 0)
  26. {
  27. // Could not find geometry index, ignore hit
  28. IgnoreHit();
  29. return;
  30. }
  31. Texture2D<float4> texture = textures[textureId];
  32. int index = PrimitiveIndex() * 3;
  33. // Bounds check for vertex indices
  34. if (indexBuffer[index] < 0 || indexBuffer[index + 1] < 0 || indexBuffer[index + 2] < 0)
  35. {
  36. IgnoreHit();
  37. return;
  38. }
  39. VertexData v0 = vertexData[indexBuffer[index]];
  40. VertexData v1 = vertexData[indexBuffer[index + 1]];
  41. VertexData v2 = vertexData[indexBuffer[index + 2]];
  42. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  43. float4 color = texture.SampleLevel(gSampler, texcoord, 0);
  44. if (color.w == 0.f)
  45. {
  46. IgnoreHit();
  47. return;
  48. }
  49. float distance = RayTCurrent();
  50. bool found = false;
  51. for (i = 0; i < rayPayload.hitCount; i++)
  52. {
  53. if (rayPayload.distance[i] > distance)
  54. {
  55. found = true;
  56. float4 tmpColor = rayPayload.color[i];
  57. rayPayload.color[i] = color;
  58. float tmpDistance = rayPayload.distance[i];
  59. rayPayload.distance[i] = distance;
  60. int tmpDayLight = rayPayload.dayLight[i];
  61. rayPayload.dayLight[i] = 0xFFFFFF;
  62. int tmpDynamicLight = rayPayload.dynamicLight[i];
  63. rayPayload.dynamicLight[i] = 0xFFFFFF;
  64. if (color.w == 1.f)
  65. {
  66. rayPayload.hitCount = i + 1;
  67. }
  68. else
  69. {
  70. for (int j = i + 1; j < rayPayload.hitCount + 1 && j < MAX_TRANSPACENT_HITS; j++)
  71. {
  72. float4 tmpColor2 = rayPayload.color[j];
  73. float tmpDistance2 = rayPayload.distance[j];
  74. int tmpDayLight2 = rayPayload.dayLight[j];
  75. int tmpDynamicLight2 = rayPayload.dynamicLight[j];
  76. rayPayload.color[j] = tmpColor;
  77. rayPayload.distance[j] = tmpDistance;
  78. rayPayload.dayLight[j] = tmpDayLight;
  79. rayPayload.dynamicLight[j] = tmpDynamicLight;
  80. tmpColor = tmpColor2;
  81. tmpDistance = tmpDistance2;
  82. tmpDayLight = tmpDayLight2;
  83. tmpDynamicLight = tmpDynamicLight2;
  84. }
  85. }
  86. break;
  87. }
  88. else
  89. {
  90. if (rayPayload.color[i].w == 1.f)
  91. {
  92. found = true;
  93. break;
  94. }
  95. }
  96. }
  97. if (!found && rayPayload.hitCount < MAX_TRANSPACENT_HITS)
  98. {
  99. rayPayload.color[rayPayload.hitCount] = color;
  100. rayPayload.distance[rayPayload.hitCount] = distance;
  101. rayPayload.dayLight[rayPayload.hitCount] = 0xFFFFFF;
  102. rayPayload.dynamicLight[rayPayload.hitCount] = 0xFFFFFF;
  103. rayPayload.hitCount++;
  104. }
  105. if (color.w < 1.f)
  106. {
  107. IgnoreHit();
  108. return;
  109. }
  110. }