Common.hlsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Hit information, aka ray payload
  2. // This sample only carries a shading color and hit distance.
  3. // Note that the rayPayload should be kept as small as possible,
  4. // and that its size must be declared in the corresponding
  5. // D3D12_RAYTRACING_SHADER_CONFIG pipeline subobjet.
  6. #pragma pack_matrix(row_major)
  7. #define MAX_TRANSPACENT_HITS 5
  8. struct SingleHit
  9. {
  10. float4 color;
  11. float3 normal;
  12. float distance;
  13. int dayLight;
  14. int dynamicLight;
  15. };
  16. struct [raypayload] RayHits
  17. {
  18. SingleHit hits[MAX_TRANSPACENT_HITS] : write(caller, closesthit, anyhit, miss) : read(caller, anyhit, closesthit, miss);
  19. int hitCount : write(caller, anyhit, closesthit, miss) : read(caller, anyhit, closesthit, miss);
  20. };
  21. // Attributes output by the raytracing when hitting a surface,
  22. // here the barycentric coordinates
  23. struct ChunkHitAttributes
  24. {
  25. float2 texCoord;
  26. float3 normal;
  27. int textureId;
  28. int dayLight;
  29. int dynamicLight;
  30. };
  31. struct SimpleBlocksAttributes
  32. {
  33. float2 bary;
  34. };
  35. struct VertexData
  36. {
  37. float2 texcoord;
  38. float3 normal;
  39. };
  40. int packLight(float3 light)
  41. {
  42. uint r = (uint) (light.r * 255.0) & 0xFF;
  43. uint g = (uint) (light.g * 255.0) & 0xFF;
  44. uint b = (uint) (light.b * 255.0) & 0xFF;
  45. return (int) ((r << 16) | (g << 8) | b);
  46. }
  47. float3 unpackLight(int light)
  48. {
  49. float r = ((light >> 16) & 0xFF) / 255.0f;
  50. float g = ((light >> 8) & 0xFF) / 255.0f;
  51. float b = (light & 0xFF) / 255.0f;
  52. return float3(r, g, b);
  53. }
  54. void applyHit(inout RayHits rayPayload, in SingleHit hit)
  55. {
  56. for (int i = 0; i < rayPayload.hitCount; i++)
  57. {
  58. if (rayPayload.hits[i].distance > hit.distance)
  59. {
  60. SingleHit tmpHit = rayPayload.hits[i];
  61. rayPayload.hits[i] = hit;
  62. if (hit.color.w == 1.f)
  63. {
  64. rayPayload.hitCount = i + 1;
  65. }
  66. else
  67. {
  68. for (int j = i + 1; j < rayPayload.hitCount + 1 && j < MAX_TRANSPACENT_HITS; j++)
  69. {
  70. SingleHit tmpHit2 = rayPayload.hits[j];
  71. rayPayload.hits[j] = tmpHit;
  72. tmpHit = tmpHit2;
  73. }
  74. }
  75. return;
  76. }
  77. else
  78. {
  79. if (rayPayload.hits[i].color.w == 1.f)
  80. {
  81. return;
  82. }
  83. }
  84. }
  85. if (rayPayload.hitCount < MAX_TRANSPACENT_HITS)
  86. {
  87. rayPayload.hits[rayPayload.hitCount] = hit;
  88. rayPayload.hitCount++;
  89. }
  90. }
  91. // Global resources
  92. Texture2D<float4> textures[] : register(t0, space1);
  93. SamplerState gSampler : register(s0, space0);
  94. cbuffer DX12ShaderLightInfo : register(b0, space1)
  95. {
  96. float3 dayLightFactor;
  97. uint padding;
  98. float3 dayLightDirection;
  99. };
  100. // Raytracing acceleration structure, accessed as a SRV
  101. RaytracingAccelerationStructure TLAS : register(t0, space0);
  102. RWTexture2D<float4> gOutput : register(u0);
  103. RWTexture2D<float4> guiTexture : register(u1);
  104. cbuffer RayGenerationSettings : register(b0, space0)
  105. {
  106. int renderGui;
  107. int useRays;
  108. float minDistance;
  109. float maxDistance;
  110. float4x4 inverseView;
  111. float4x4 inverseProjection;
  112. };