SimpleBlocksAnyHit.hlsl 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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> indexOffsetBuffer : register(t1, space5);
  6. ByteAddressBuffer lightBuffer : register(t1, space6);
  7. [shader("anyhit")]
  8. void SimpleBlocksAnyHit(inout RayHits rayPayload, SimpleBlocksAttributes attrib)
  9. {
  10. int currentTriangle = PrimitiveIndex();
  11. int geometryIndex = GeometryIndex();
  12. // Bounds check for geometryIndex
  13. if (geometryIndex < 0)
  14. {
  15. IgnoreHit();
  16. return;
  17. }
  18. int textureId = textureIdBuffer[geometryIndex];
  19. Texture2D<float4> texture = textures[textureId];
  20. int indexOffset = indexOffsetBuffer[geometryIndex];
  21. int index = indexOffset + currentTriangle * 3;
  22. // Bounds check for indices
  23. if (indexBuffer[index] < 0 || indexBuffer[index + 1] < 0 || indexBuffer[index + 2] < 0)
  24. {
  25. IgnoreHit();
  26. return;
  27. }
  28. VertexData v0 = vertexData[indexBuffer[index]];
  29. VertexData v1 = vertexData[indexBuffer[index + 1]];
  30. VertexData v2 = vertexData[indexBuffer[index + 2]];
  31. float3 normal = v0.normal * (1 - attrib.bary.x - attrib.bary.y) + v1.normal * attrib.bary.x + v2.normal * attrib.bary.y;
  32. float2 texcoord = v0.texcoord * (1 - attrib.bary.x - attrib.bary.y) + v1.texcoord * attrib.bary.x + v2.texcoord * attrib.bary.y;
  33. float4 color = texture.SampleLevel(gSampler, texcoord, 0);
  34. if (color.w == 0.f)
  35. {
  36. IgnoreHit();
  37. return;
  38. }
  39. if (rayPayload.hitCount < 0)
  40. { // shadow ray
  41. rayPayload.hitCount = 0;
  42. // TODO: transparent objects should lead to half shadows
  43. AcceptHitAndEndSearch();
  44. return;
  45. }
  46. SingleHit hit;
  47. hit.color = color;
  48. hit.normal = normal;
  49. hit.distance = RayTCurrent();
  50. hit.dayLight = getLight(lightBuffer, geometryIndex * 6);
  51. hit.dynamicLight = getLight(lightBuffer, geometryIndex * 6 + 3);
  52. applyHit(rayPayload, hit);
  53. if (color.w < 1.f)
  54. {
  55. IgnoreHit();
  56. return;
  57. }
  58. }