RtMaterialFetchDbg.ankiprog 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma anki technique RtMaterialFetch rgen
  6. #include <AnKi/Shaders/RtMaterialFetch.hlsl>
  7. #include <AnKi/Shaders/Include/GpuSceneTypes.h>
  8. #define CHEAP 0
  9. [shader("raygeneration")] void main()
  10. {
  11. Vec2 outSize;
  12. g_colorAndPdfTex.GetDimensions(outSize.x, outSize.y);
  13. const Vec2 uv = Vec2(DispatchRaysIndex().xy) / outSize;
  14. const Vec4 v4 = mul(g_globalRendererConstants.m_matrices.m_invertedViewProjectionJitter, Vec4(uvToNdc(uv), 1.0, 1.0));
  15. const Vec3 worldPos = v4.xyz / v4.w;
  16. const Vec3 rayOrigin = g_globalRendererConstants.m_matrices.m_cameraTransform.getTranslationPart().xyz;
  17. const Vec3 rayDir = normalize(worldPos - rayOrigin);
  18. const F32 tMax = 1000.0;
  19. const F32 tMin = 0.1;
  20. constexpr U32 traceFlags = RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_SKIP_PROCEDURAL_PRIMITIVES;
  21. Vec3 col;
  22. #if !CHEAP
  23. const F32 texLod = 0.0;
  24. GBufferLight<F16> gbuffer = (GBufferLight<F16>)0;
  25. F32 rayT = 0.0;
  26. Bool backfacing = false;
  27. const Bool hit = materialRayTrace<F16>(rayOrigin, rayDir, tMin, tMax, texLod, gbuffer, rayT, backfacing, traceFlags);
  28. if(!hit)
  29. {
  30. col = Vec3(0.0, 0.0, 1.0);
  31. }
  32. else if(backfacing)
  33. {
  34. col = Vec3(1.0, 0.0, 1.0);
  35. }
  36. else
  37. {
  38. // col = gbuffer.m_diffuse * 1.0 + (gbuffer.m_worldNormal / 2.0 + 0.5) * 0.0 + rayT * 0.0 + gbuffer.m_emission * 0.0;
  39. col = directLighting(gbuffer, rayOrigin + rayDir * rayT, false, true, 100.0, true);
  40. col += gbuffer.m_diffuse * 0.3;
  41. // col = gbuffer.m_worldNormal / 2.0 + 0.5;
  42. }
  43. #else
  44. RayQuery<RAY_FLAG_FORCE_OPAQUE> q;
  45. const U32 cullMask = 0xFFu;
  46. RayDesc ray;
  47. ray.Origin = rayOrigin;
  48. ray.TMin = tMin;
  49. ray.Direction = rayDir;
  50. ray.TMax = tMax;
  51. q.TraceRayInline(g_tlas, RAY_FLAG_FORCE_OPAQUE, cullMask, ray);
  52. while(q.Proceed())
  53. {
  54. }
  55. const Bool hit = q.CommittedStatus() == COMMITTED_TRIANGLE_HIT;
  56. Bool backfacing = false;
  57. if(hit)
  58. {
  59. backfacing = q.CommittedTriangleFrontFace();
  60. }
  61. if(!hit)
  62. {
  63. col = Vec3(0.0, 0.0, 1.0);
  64. }
  65. else if(backfacing)
  66. {
  67. col = Vec3(1.0, 0.0, 1.0);
  68. }
  69. else
  70. {
  71. UVec3 coloru = q.CommittedInstanceID();
  72. coloru >>= UVec3(16, 8, 0);
  73. coloru &= 0xFF;
  74. const Vec3 color = Vec3(coloru) / 255.0;
  75. const Vec3 positions[3] = spvRayQueryGetIntersectionTriangleVertexPositionsKHR(q, SpvRayQueryCommittedIntersectionKHR);
  76. const Vec3 vertNormal = normalize(cross(positions[1] - positions[0], positions[2] - positions[1]));
  77. const Vec3 worldNormal = normalize(mul(q.CommittedObjectToWorld3x4(), Vec4(vertNormal, 0.0)));
  78. GBufferLight<F16> gbuffer = (GBufferLight<F16>)0;
  79. gbuffer.m_diffuse = color;
  80. gbuffer.m_worldNormal = worldNormal;
  81. const F32 rayT = q.CommittedRayT();
  82. col = directLighting(gbuffer, rayOrigin + rayDir * rayT, false, true, 100.0, true);
  83. col += gbuffer.m_diffuse * 0.3;
  84. // col = worldNormal / 2.0 + 0.5;
  85. // col = frac(positions[0]);
  86. }
  87. #endif
  88. g_colorAndPdfTex[DispatchRaysIndex().xy] = Vec4(col, 0.0);
  89. }