DeferredDirectionalLight.bsl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. float3 screenDir : 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.uv0 = input.uv0;
  50. output.screenDir = mul(gMatInvProj, float4(input.screenPos, 1, 0)).xyz - gViewOrigin.xyz;
  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 = (uint2)(input.uv0 * (float2)gViewportRectangle.zw - ((float2)gViewportRectangle.xy + 0.5f));
  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. float3 cameraDir = normalize(input.screenDir);
  72. float3 worldPosition = input.screenDir * surfaceData.depth + gViewOrigin;
  73. float3 V = normalize(gViewOrigin - worldPosition);
  74. float3 N = surfaceData.worldNormal.xyz;
  75. float3 R = 2 * dot(V, N) * N - V;
  76. float roughness2 = max(surfaceData.roughness, 0.08f);
  77. roughness2 *= roughness2;
  78. LightData lightData = getLightData();
  79. #if MSAA_COUNT > 1
  80. #if MSAA_RESOLVE_0TH
  81. float occlusion = gLightOcclusionTex.Load(pixelPos, 0).r;
  82. #else
  83. float occlusion = gLightOcclusionTex.Load(pixelPos, sampleIdx).r;
  84. #endif
  85. #else
  86. float occlusion = gLightOcclusionTex.Load(int3(pixelPos, 0)).r;
  87. #endif
  88. // Reverse the sqrt we did when storing it
  89. occlusion *= occlusion;
  90. occlusion = 1.0f - occlusion;
  91. return float4(getLuminanceDirectional(lightData, worldPosition, V, R, surfaceData) * occlusion, 1.0f);
  92. }
  93. else
  94. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  95. }
  96. };
  97. };