DeferredDirectionalLight.bsl 2.4 KB

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