DeferredDirectionalLight.bsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if MSAA
  2. #define MSAA_COUNT 2
  3. #else
  4. #define MSAA_COUNT 1
  5. #endif
  6. #include "$ENGINE$\DeferredLightCommon.bslinc"
  7. technique DeferredDirectionalLight
  8. {
  9. mixin DeferredLightCommon;
  10. variations
  11. {
  12. MSAA = { true, false };
  13. MSAA_RESOLVE_0TH = { true, false };
  14. };
  15. depth
  16. {
  17. read = false;
  18. };
  19. #if MSAA
  20. stencil
  21. {
  22. enabled = true;
  23. front = { keep, keep, keep, eq };
  24. readmask = 0x80;
  25. #if MSAA_RESOLVE_0TH
  26. reference = 0;
  27. #else
  28. reference = 0x80;
  29. #endif
  30. };
  31. #endif
  32. code
  33. {
  34. struct VStoFS
  35. {
  36. float4 position : SV_POSITION;
  37. float2 uv0 : TEXCOORD0;
  38. float4 screenPos : TEXCOORD1;
  39. };
  40. struct VertexInput
  41. {
  42. float2 screenPos : POSITION;
  43. float2 uv0 : TEXCOORD0;
  44. };
  45. VStoFS vsmain(VertexInput input)
  46. {
  47. VStoFS output;
  48. output.position = float4(input.screenPos, 0, 1);
  49. output.screenPos = float4(input.screenPos, 0, 1);
  50. output.uv0 = input.uv0;
  51. return output;
  52. }
  53. float4 fsmain(VStoFS input
  54. #if MSAA_COUNT > 1 && !MSAA_RESOLVE_0TH
  55. , uint sampleIdx : SV_SampleIndex
  56. #endif
  57. ) : SV_Target0
  58. {
  59. uint2 pixelPos = UVToScreen(input.uv0);
  60. #if MSAA_COUNT > 1
  61. #if MSAA_RESOLVE_0TH
  62. SurfaceData surfaceData = getGBufferData(pixelPos, 0);
  63. #else
  64. SurfaceData surfaceData = getGBufferData(pixelPos, sampleIdx);
  65. #endif
  66. #else
  67. SurfaceData surfaceData = getGBufferData(pixelPos);
  68. #endif
  69. if(surfaceData.worldNormal.w > 0.0f)
  70. {
  71. float2 ndcPos = input.screenPos.xy / input.screenPos.w;
  72. float3 worldPosition = NDCToWorld(ndcPos, surfaceData.depth);
  73. float3 V = normalize(gViewOrigin - worldPosition);
  74. float3 N = surfaceData.worldNormal.xyz;
  75. float3 R = 2 * dot(V, N) * N - V;
  76. float3 specR = getSpecularDominantDir(N, R, surfaceData.roughness);
  77. LightData lightData = getLightData();
  78. #if MSAA_COUNT > 1
  79. #if MSAA_RESOLVE_0TH
  80. float occlusion = gLightOcclusionTex.Load(pixelPos, 0).r;
  81. #else
  82. float occlusion = gLightOcclusionTex.Load(pixelPos, sampleIdx).r;
  83. #endif
  84. #else
  85. float occlusion = gLightOcclusionTex.Load(int3(pixelPos, 0)).r;
  86. #endif
  87. // Reverse the sqrt we did when storing it
  88. occlusion *= occlusion;
  89. occlusion = 1.0f - occlusion;
  90. return float4(getLuminanceDirectional(lightData, worldPosition, V, specR, surfaceData) * occlusion, 1.0f);
  91. }
  92. else
  93. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  94. }
  95. };
  96. };