DeferredIBLProbe.bsl 3.1 KB

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