DeferredLight.hlsl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "Uniforms.hlsl"
  2. #include "Samplers.hlsl"
  3. #include "Transform.hlsl"
  4. #include "ScreenPos.hlsl"
  5. #include "Lighting.hlsl"
  6. void VS(float4 iPos : POSITION,
  7. #ifdef DIRLIGHT
  8. out float2 oScreenPos : TEXCOORD0,
  9. #else
  10. out float4 oScreenPos : TEXCOORD0,
  11. #endif
  12. out float3 oFarRay : TEXCOORD1,
  13. #ifdef ORTHO
  14. out float3 oNearRay : TEXCOORD2,
  15. #endif
  16. out float4 oPos : POSITION)
  17. {
  18. float4x3 modelMatrix = iModelMatrix;
  19. float3 worldPos = GetWorldPos(modelMatrix);
  20. oPos = GetClipPos(worldPos);
  21. #ifdef DIRLIGHT
  22. oScreenPos = GetScreenPosPreDiv(oPos);
  23. oFarRay = GetFarRay(oPos);
  24. #ifdef ORTHO
  25. oNearRay = GetNearRay(oPos);
  26. #endif
  27. #else
  28. oScreenPos = GetScreenPos(oPos);
  29. oFarRay = GetFarRay(oPos) * oPos.w;
  30. #ifdef ORTHO
  31. oNearRay = GetNearRay(oPos) * oPos.w;
  32. #endif
  33. #endif
  34. }
  35. void PS(
  36. #ifdef DIRLIGHT
  37. float2 iScreenPos : TEXCOORD0,
  38. #else
  39. float4 iScreenPos : TEXCOORD0,
  40. #endif
  41. float3 iFarRay : TEXCOORD1,
  42. #ifdef ORTHO
  43. float3 iNearRay : TEXCOORD2,
  44. #endif
  45. out float4 oColor : COLOR0)
  46. {
  47. // If rendering a directional light quad, optimize out the w divide
  48. #ifdef DIRLIGHT
  49. #ifdef ORTHO
  50. float depth = tex2D(sDepthBuffer, iScreenPos).r;
  51. float3 worldPos = lerp(iNearRay, iFarRay, depth);
  52. #else
  53. #ifdef LINEAR
  54. float depth = tex2D(sDepthBuffer, iScreenPos).r;
  55. #else
  56. float depth = ReconstructDepth(tex2D(sDepthBuffer, iScreenPos).r);
  57. #endif
  58. float3 worldPos = iFarRay * depth;
  59. #endif
  60. float4 albedoInput = tex2D(sAlbedoBuffer, iScreenPos);
  61. float4 normalInput = tex2D(sNormalBuffer, iScreenPos);
  62. #else
  63. #ifdef ORTHO
  64. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  65. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  66. #else
  67. #ifdef LINEAR
  68. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  69. #else
  70. float depth = ReconstructDepth(tex2Dproj(sDepthBuffer, iScreenPos).r);
  71. #endif
  72. float3 worldPos = iFarRay * depth / iScreenPos.w;
  73. #endif
  74. float4 albedoInput = tex2Dproj(sAlbedoBuffer, iScreenPos);
  75. float4 normalInput = tex2Dproj(sNormalBuffer, iScreenPos);
  76. #endif
  77. // With a cubemasked shadowed point light and hardware depth reconstruction, SM2 runs out of instructions,
  78. // so skip normalization of normals in that case
  79. #if defined(SM3) || defined(HWSHADOW) || !defined(POINTLIGHT) || !defined(SHADOW) || !defined(CUBEMASK)
  80. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  81. #else
  82. float3 normal = normalInput.rgb * 2.0 - 1.0;
  83. #endif
  84. float4 projWorldPos = float4(worldPos, 1.0);
  85. float3 lightColor;
  86. float3 lightDir;
  87. float diff;
  88. #ifdef DIRLIGHT
  89. lightDir = cLightDirPS;
  90. diff = GetDiffuseDir(normal, lightDir);
  91. #else
  92. float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  93. diff = GetDiffusePointOrSpot(normal, lightVec, lightDir);
  94. #endif
  95. #ifdef SHADOW
  96. #if defined(DIRLIGHT)
  97. float4 shadowPos = GetDirShadowPosDeferred(cLightMatricesPS, projWorldPos, depth);
  98. diff *= saturate(GetShadow(shadowPos) + GetShadowFade(depth));
  99. #elif defined(SPOTLIGHT)
  100. float4 shadowPos = mul(projWorldPos, cLightMatricesPS[1]);
  101. diff *= GetShadow(shadowPos);
  102. #else
  103. float3 shadowPos = worldPos - cLightPosPS.xyz;
  104. diff *= GetCubeShadow(shadowPos);
  105. #endif
  106. #endif
  107. #if defined(SPOTLIGHT)
  108. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  109. lightColor = spotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  110. #elif defined(CUBEMASK)
  111. lightColor = texCUBE(sLightCubeMap, mul(lightVec, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  112. #else
  113. lightColor = cLightColor.rgb;
  114. #endif
  115. #ifdef SPECULAR
  116. float spec = GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  117. oColor = diff * float4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  118. #else
  119. oColor = diff * float4(lightColor * albedoInput.rgb, 0.0);
  120. #endif
  121. }