DeferredLight.hlsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. float depth = tex2D(sDepthBuffer, iScreenPos).r;
  54. float3 worldPos = iFarRay * depth;
  55. #endif
  56. float4 albedoInput = tex2D(sAlbedoBuffer, iScreenPos);
  57. float4 normalInput = tex2D(sNormalBuffer, iScreenPos);
  58. #else
  59. #ifdef ORTHO
  60. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  61. float3 worldPos = lerp(iNearRay, iFarRay, depth) / iScreenPos.w;
  62. #else
  63. float depth = tex2Dproj(sDepthBuffer, iScreenPos).r;
  64. float3 worldPos = iFarRay * depth / iScreenPos.w;
  65. #endif
  66. float4 albedoInput = tex2Dproj(sAlbedoBuffer, iScreenPos);
  67. float4 normalInput = tex2Dproj(sNormalBuffer, iScreenPos);
  68. #endif
  69. // With a cubemasked shadowed point light and hardware depth reconstruction, SM2 runs out of instructions,
  70. // so skip normalization of normals in that case
  71. #if defined(SM3) || defined(HWSHADOW) || !defined(POINTLIGHT) || !defined(SHADOW) || !defined(CUBEMASK)
  72. float3 normal = normalize(normalInput.rgb * 2.0 - 1.0);
  73. #else
  74. float3 normal = normalInput.rgb * 2.0 - 1.0;
  75. #endif
  76. float4 projWorldPos = float4(worldPos, 1.0);
  77. float3 lightColor;
  78. float3 lightDir;
  79. float diff;
  80. #ifdef DIRLIGHT
  81. diff = GetDiffuse(normal, cLightDirPS, lightDir);
  82. #else
  83. float3 lightVec = (cLightPosPS.xyz - worldPos) * cLightPosPS.w;
  84. diff = GetDiffuse(normal, lightVec, lightDir);
  85. #endif
  86. #ifdef SHADOW
  87. diff *= GetShadowDeferred(projWorldPos, depth);
  88. #endif
  89. #if defined(SPOTLIGHT)
  90. float4 spotPos = mul(projWorldPos, cLightMatricesPS[0]);
  91. lightColor = spotPos.w > 0.0 ? tex2Dproj(sLightSpotMap, spotPos).rgb * cLightColor.rgb : 0.0;
  92. #elif defined(CUBEMASK)
  93. lightColor = texCUBE(sLightCubeMap, mul(lightVec, (float3x3)cLightMatricesPS[0])).rgb * cLightColor.rgb;
  94. #else
  95. lightColor = cLightColor.rgb;
  96. #endif
  97. #ifdef SPECULAR
  98. float spec = GetSpecular(normal, -worldPos, lightDir, normalInput.a * 255.0);
  99. oColor = diff * float4(lightColor * (albedoInput.rgb + spec * cLightColor.a * albedoInput.aaa), 0.0);
  100. #else
  101. oColor = diff * float4(lightColor * albedoInput.rgb, 0.0);
  102. #endif
  103. }