DeferredDirectionalLight.bsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "$ENGINE$\DeferredLightCommon.bslinc"
  2. technique DeferredDirectionalLight
  3. {
  4. mixin DeferredLightCommon;
  5. depth
  6. {
  7. read = false;
  8. };
  9. code
  10. {
  11. struct VStoFS
  12. {
  13. float4 position : SV_POSITION;
  14. float2 uv0 : TEXCOORD0;
  15. float3 screenDir : TEXCOORD1;
  16. };
  17. struct VertexInput
  18. {
  19. float2 screenPos : POSITION;
  20. float2 uv0 : TEXCOORD0;
  21. };
  22. VStoFS vsmain(VertexInput input)
  23. {
  24. VStoFS output;
  25. output.position = float4(input.screenPos, 0, 1);
  26. output.uv0 = input.uv0;
  27. output.screenDir = mul(gMatInvProj, float4(input.screenPos, 1, 0)).xyz - gViewOrigin.xyz;
  28. return output;
  29. }
  30. float4 fsmain(VStoFS input, uint sampleIdx : SV_SampleIndex) : SV_Target0
  31. {
  32. uint2 pixelPos = (uint2)(input.uv0 * (float2)gViewportRectangle.zw - ((float2)gViewportRectangle.xy + 0.5f));
  33. #if MSAA_COUNT > 1
  34. SurfaceData surfaceData = getGBufferData(pixelPos, sampleIdx);
  35. #else
  36. SurfaceData surfaceData = getGBufferData(pixelPos);
  37. #endif
  38. if(surfaceData.worldNormal.w > 0.0f)
  39. {
  40. float3 cameraDir = normalize(input.screenDir);
  41. float3 worldPosition = input.screenDir * surfaceData.depth + gViewOrigin;
  42. float3 V = normalize(gViewOrigin - worldPosition);
  43. float3 N = surfaceData.worldNormal.xyz;
  44. float3 R = 2 * dot(V, N) * N - V;
  45. float roughness2 = max(surfaceData.roughness, 0.08f);
  46. roughness2 *= roughness2;
  47. LightData lightData = getLightData();
  48. #if MSAA_COUNT > 1
  49. float occlusion = gLightOcclusionTex.Load(pixelPos, sampleIdx).r;
  50. #else
  51. float occlusion = gLightOcclusionTex.Load(int3(pixelPos, 0)).r;
  52. #endif
  53. // Reverse the sqrt we did when storing it
  54. occlusion *= occlusion;
  55. occlusion = 1.0f - occlusion;
  56. return float4(getLuminanceDirectional(lightData, worldPosition, V, R, surfaceData) * occlusion, 1.0f);
  57. }
  58. else
  59. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  60. }
  61. };
  62. };