DeferredIBLSetup.bsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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$\PPBase.bslinc"
  8. #include "$ENGINE$\PerCameraData.bslinc"
  9. #include "$ENGINE$\ImageBasedLighting.bslinc"
  10. technique DeferredIBLSetup
  11. {
  12. mixin PPBase;
  13. mixin GBufferInput;
  14. mixin PerCameraData;
  15. mixin ImageBasedLighting;
  16. variations
  17. {
  18. MSAA = { true, false };
  19. MSAA_RESOLVE_0TH = { true, false };
  20. };
  21. #if MSAA
  22. stencil
  23. {
  24. enabled = true;
  25. readmask = 0x80;
  26. #if INSIDE_GEOMETRY
  27. back = { keep, keep, keep, eq };
  28. #else
  29. front = { keep, keep, keep, eq };
  30. #endif
  31. #if MSAA_RESOLVE_0TH
  32. reference = 0;
  33. #else
  34. reference = 0x80;
  35. #endif
  36. };
  37. #endif
  38. code
  39. {
  40. float4 fsmain(VStoFS input, float4 pixelPos : SV_Position
  41. #if MSAA_COUNT > 1 && !MSAA_RESOLVE_0TH
  42. , uint sampleIdx : SV_SampleIndex
  43. #endif
  44. ) : SV_Target0
  45. {
  46. #if MSAA_COUNT > 1
  47. #if MSAA_RESOLVE_0TH
  48. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, 0);
  49. #else
  50. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, sampleIdx);
  51. #endif
  52. #else
  53. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy);
  54. #endif
  55. if(surfaceData.worldNormal.w > 0.0f)
  56. {
  57. float3 worldPosition = NDCToWorld(input.screenPos, surfaceData.depth);
  58. float ao = gAmbientOcclusionTex.SampleLevel(gAmbientOcclusionSamp, input.uv0, 0.0f).r;
  59. float4 ssr = gSSRTex.SampleLevel(gSSRSamp, input.uv0, 0.0f);
  60. float3 V = normalize(gViewOrigin - worldPosition);
  61. float3 N = surfaceData.worldNormal.xyz;
  62. float NoV = saturate(dot(N, V));
  63. float3 radiance = ssr.rgb;
  64. float alpha = 1.0f - ssr.a; // Determines how much to blend in reflection probes & skybox
  65. // Generate an approximate spec. occlusion value from AO. This doesn't need to be applied to SSR since it accounts
  66. // for occlusion by tracing rays.
  67. float specOcclusion = getSpecularOcclusion(NoV, surfaceData.roughness * surfaceData.roughness, ao);
  68. alpha *= specOcclusion;
  69. if(gUseReflectionMaps == 0)
  70. {
  71. // Note: Using a fixed F0 value of 0.04 (plastic) for dielectrics, and using albedo as specular for conductors.
  72. // For more customizability allow the user to provide separate albedo/specular colors for both types.
  73. float3 specularColor = lerp(float3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, surfaceData.metalness);
  74. radiance += specularColor;
  75. }
  76. return float4(radiance, alpha);
  77. }
  78. else
  79. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  80. }
  81. };
  82. };