DeferredIBLFinalize.bsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 DeferredIBLFinalize
  11. {
  12. mixin PPBase;
  13. mixin GBufferInput;
  14. mixin PerCameraData;
  15. mixin ImageBasedLighting;
  16. blend
  17. {
  18. target
  19. {
  20. enabled = true;
  21. color = { one, one, add };
  22. };
  23. };
  24. variations
  25. {
  26. MSAA = { true, false };
  27. MSAA_RESOLVE_0TH = { true, false };
  28. };
  29. #if MSAA
  30. stencil
  31. {
  32. enabled = true;
  33. readmask = 0x80;
  34. #if INSIDE_GEOMETRY
  35. back = { keep, keep, keep, eq };
  36. #else
  37. front = { keep, keep, keep, eq };
  38. #endif
  39. #if MSAA_RESOLVE_0TH
  40. reference = 0;
  41. #else
  42. reference = 0x80;
  43. #endif
  44. };
  45. #endif
  46. code
  47. {
  48. #if MSAA_COUNT > 1
  49. Texture2DMS<float4> gIBLRadianceTex;
  50. #else
  51. Texture2D gIBLRadianceTex;
  52. #endif
  53. float4 fsmain(VStoFS input, float4 pixelPos : SV_Position
  54. #if MSAA_COUNT > 1 && !MSAA_RESOLVE_0TH
  55. , uint sampleIdx : SV_SampleIndex
  56. #endif
  57. ) : SV_Target0
  58. {
  59. #if MSAA_COUNT > 1
  60. #if MSAA_RESOLVE_0TH
  61. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, 0);
  62. float3 radiance = gIBLRadianceTex.Load((uint2)pixelPos.xy, 0).rgb;
  63. #else
  64. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy, sampleIdx);
  65. float3 radiance = gIBLRadianceTex.Load((uint2)pixelPos.xy, sampleIdx).rgb;
  66. #endif
  67. #else
  68. SurfaceData surfaceData = getGBufferData((uint2)pixelPos.xy);
  69. float3 radiance = gIBLRadianceTex.Load(int3((int2)pixelPos.xy, 0)).rgb;
  70. #endif
  71. if(surfaceData.worldNormal.w > 0.0f)
  72. {
  73. // See C++ code for generation of gPreintegratedEnvBRDF to see why this code works as is
  74. float3 worldPosition = NDCToWorld(input.screenPos, surfaceData.depth);
  75. float3 V = normalize(gViewOrigin - worldPosition);
  76. float3 N = surfaceData.worldNormal.xyz;
  77. float NoV = saturate(dot(N, V));
  78. // Note: Using a fixed F0 value of 0.04 (plastic) for dielectrics, and using albedo as specular for conductors.
  79. // For more customizability allow the user to provide separate albedo/specular colors for both types.
  80. float3 specularColor = lerp(float3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, surfaceData.metalness);
  81. float2 envBRDF = gPreintegratedEnvBRDF.SampleLevel(gPreintegratedEnvBRDFSamp, float2(NoV, surfaceData.roughness), 0).rg;
  82. return float4(radiance * (specularColor * envBRDF.x + envBRDF.y), 0.0f);
  83. }
  84. else
  85. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  86. }
  87. };
  88. };