DeferredIBLProbe.bsl 3.0 KB

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