DeferredIBLFinalize.bsl 2.5 KB

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