DeferredIBLProbe.bsl 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #if MSAA
  2. #define MSAA_COUNT 2
  3. #else
  4. #define MSAA_COUNT 1
  5. #endif
  6. #include "$ENGINE$\GBufferInput.bslinc"
  7. #include "$ENGINE$\PerCameraData.bslinc"
  8. #include "$ENGINE$\ImageBasedLighting.bslinc"
  9. technique DeferredIBLProbe
  10. {
  11. mixin GBufferInput;
  12. mixin PerCameraData;
  13. mixin ImageBasedLighting;
  14. depth
  15. {
  16. write = false;
  17. #ifdef INSIDE_GEOMETRY
  18. read = false;
  19. #else
  20. read = true;
  21. #endif
  22. };
  23. blend
  24. {
  25. target
  26. {
  27. enabled = true;
  28. color = { dstA, one, add };
  29. alpha = { dstA, zero, add };
  30. };
  31. };
  32. raster
  33. {
  34. #ifdef INSIDE_GEOMETRY
  35. cull = cw;
  36. #else
  37. cull = ccw;
  38. #endif
  39. };
  40. variations
  41. {
  42. MSAA = { true, false };
  43. INSIDE_GEOMETRY = { true, false };
  44. MSAA_RESOLVE_0TH = { true, false };
  45. };
  46. #if MSAA
  47. stencil
  48. {
  49. enabled = true;
  50. readmask = 0x80;
  51. #if INSIDE_GEOMETRY
  52. back = { keep, keep, keep, eq };
  53. #else
  54. front = { keep, keep, keep, eq };
  55. #endif
  56. #if MSAA_RESOLVE_0TH
  57. reference = 0;
  58. #else
  59. reference = 0x80;
  60. #endif
  61. };
  62. #endif
  63. code
  64. {
  65. struct VStoFS
  66. {
  67. float4 position : SV_POSITION;
  68. float4 screenPos : TEXCOORD0;
  69. };
  70. struct VertexInput
  71. {
  72. float3 position : POSITION;
  73. uint vertexIdx : SV_VERTEXID;
  74. };
  75. cbuffer PerProbe
  76. {
  77. float3 gPosition;
  78. float3 gExtents;
  79. float gTransitionDistance;
  80. float4x4 gInvBoxTransform;
  81. uint gCubemapIdx;
  82. uint gType; // 0 - Sphere, 1 - Box
  83. }
  84. VStoFS vsmain(VertexInput input)
  85. {
  86. VStoFS output;
  87. // Position & scale geometry
  88. float3 worldPosition = input.position * gExtents + gPosition;
  89. output.screenPos = mul(gMatViewProj, float4(worldPosition, 1));
  90. output.position = output.screenPos;
  91. return output;
  92. }
  93. float4 fsmain(VStoFS input, float4 pixelPos : SV_Position
  94. #if MSAA_COUNT > 1 && !MSAA_RESOLVE_0TH
  95. , uint sampleIdx : SV_SampleIndex
  96. #endif
  97. ) : SV_Target0
  98. {
  99. #if MSAA_COUNT > 1
  100. #if MSAA_RESOLVE_0TH
  101. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, 0);
  102. #else
  103. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, sampleIdx);
  104. #endif
  105. #else
  106. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy);
  107. #endif
  108. if(surfaceData.worldNormal.w > 0.0f)
  109. {
  110. ReflProbeData probeData;
  111. probeData.position = gPosition;
  112. probeData.radius = gExtents.x;
  113. probeData.boxExtents = gExtents;
  114. probeData.transitionDistance = gTransitionDistance;
  115. probeData.invBoxTransform = gInvBoxTransform;
  116. probeData.cubemapIdx = gCubemapIdx;
  117. probeData.type = gType;
  118. probeData.padding = float2(0, 0);
  119. float2 ndcPos = input.screenPos.xy / input.screenPos.w;
  120. float3 worldPosition = NDCToWorld(ndcPos, surfaceData.depth);
  121. float3 V = normalize(gViewOrigin - worldPosition);
  122. float3 N = surfaceData.worldNormal.xyz;
  123. float3 R = 2 * dot(V, N) * N - V;
  124. float mipLevel = mapRoughnessToMipLevel(surfaceData.roughness, gReflCubemapNumMips);
  125. return evaluateProbe(worldPosition, R, mipLevel, probeData);
  126. }
  127. else
  128. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  129. }
  130. };
  131. };