DeferredPointLight.bsl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include "$ENGINE$\DeferredLightCommon.bslinc"
  2. Technique : inherits("DeferredLightCommon") =
  3. {
  4. Pass =
  5. {
  6. Common =
  7. {
  8. struct VStoFS
  9. {
  10. float4 position : SV_POSITION;
  11. float4 screenPos : TEXCOORD0;
  12. };
  13. };
  14. Vertex =
  15. {
  16. struct VertexInput
  17. {
  18. float3 position : POSITION;
  19. uint vertexIdx : SV_VERTEXID;
  20. };
  21. VStoFS main(VertexInput input)
  22. {
  23. VStoFS output;
  24. float3 worldPosition;
  25. uint numSides = gLightGeometry.x;
  26. if(numSides > 0) // Generate spot light geometry
  27. {
  28. uint numSlices = gLightGeometry.y;
  29. float radius = gLightGeometry.w;
  30. float angle = gLightSpotAnglesAndSqrdInvAttRadius.x;
  31. // Extra scale to ensure edges lie on the circle, not inside it
  32. // TODO - These can be precomputed
  33. float extraRadiusScale = 1.0f / cos(PI / (float)numSides);
  34. float angleTan = tan(angle);
  35. float height = radius / angleTan;
  36. uint sphereStartIdx = numSides * numSlices;
  37. // Cone vertices
  38. if (input.vertexIdx < sphereStartIdx)
  39. {
  40. uint sliceIdx = input.vertexIdx / numSides;
  41. uint sideIdx = input.vertexIdx % numSides;
  42. float curAngle = sideIdx * 2 * PI / (float)numSides;
  43. float sliceOffset = height * sliceIdx / (float)(numSlices - 1);
  44. float sliceRadius = sliceOffset * angleTan * extraRadiusScale;
  45. float4 localPos = float4(sliceRadius * cos(curAngle),
  46. sliceRadius * sin(curAngle), -sliceOffset, 1.0f);
  47. worldPosition = (mul(gMatConeTransform, localPos)).xyz;
  48. }
  49. else // Sphere cap vertices
  50. {
  51. uint sphereVertexIdx = input.vertexIdx - sphereStartIdx;
  52. uint sliceIdx = sphereVertexIdx / numSides;
  53. uint sideIdx = sphereVertexIdx % numSides;
  54. float curAngle = sideIdx * 2 * PI / (float)numSides;
  55. float sliceOffset = radius * sliceIdx / (float)(numSlices - 1);
  56. float sliceRadius = sqrt(max(0.0f, radius * radius - sliceOffset * sliceOffset)) * extraRadiusScale;
  57. float4 localPos = float4(sliceRadius * cos(curAngle),
  58. sliceRadius * sin(curAngle), -height - sliceOffset, 1.0f);
  59. worldPosition = (mul(gMatConeTransform, localPos)).xyz;
  60. }
  61. }
  62. else // Scale and position pre-generated sphere geometry
  63. {
  64. worldPosition = input.position * gLightGeometry.z + gLightPositionAndSrcRadius.xyz;
  65. }
  66. output.screenPos = mul(gMatViewProj, float4(worldPosition, 1));
  67. output.position = output.screenPos;
  68. return output;
  69. }
  70. };
  71. Fragment =
  72. {
  73. float4 main(VStoFS input, uint sampleIdx : SV_SampleIndex) : SV_Target0
  74. {
  75. float2 ndcPos = input.screenPos.xy / input.screenPos.w;
  76. float2 screenUV = ndcPos * gClipToUVScaleOffset.xy + gClipToUVScaleOffset.zw;
  77. uint2 pixelPos = (uint2)(screenUV * (float2)gViewportRectangle.zw - ((float2)gViewportRectangle.xy + 0.5f));
  78. #if MSAA_COUNT > 1
  79. SurfaceData surfaceData = getGBufferData(pixelPos, sampleIdx);
  80. #else
  81. SurfaceData surfaceData = getGBufferData(pixelPos);
  82. #endif
  83. if(surfaceData.worldNormal.w > 0.0f)
  84. {
  85. // x, y are now in clip space, z, w are in view space
  86. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  87. // z, w eliminated (since they are already in view space)
  88. // Note: Multiply by depth should be avoided if using ortographic projection
  89. float4 mixedSpacePos = float4(ndcPos.xy * -surfaceData.depth, surfaceData.depth, 1);
  90. float4 worldPosition4D = mul(gMatScreenToWorld, mixedSpacePos);
  91. float3 worldPosition = worldPosition4D.xyz / worldPosition4D.w;
  92. float3 V = normalize(gViewOrigin - worldPosition);
  93. float3 N = surfaceData.worldNormal.xyz;
  94. float3 R = 2 * dot(V, N) * N - V;
  95. float3 specR = getSpecularDominantDir(N, R, surfaceData.roughness);
  96. float roughness2 = max(surfaceData.roughness, 0.08f);
  97. roughness2 *= roughness2;
  98. LightData lightData = getLightData();
  99. bool isSpot = gShiftedLightPositionAndType.w > 0.5f;
  100. if(isSpot)
  101. return float4(getLuminanceSpot(lightData, worldPosition, V, R, roughness2, surfaceData), 1.0f);
  102. else // Radial
  103. return float4(getLuminanceRadial(lightData, worldPosition, V, R, roughness2, surfaceData), 1.0f);
  104. }
  105. else
  106. return float4(0.0f, 0.0f, 0.0f, 0.0f);
  107. }
  108. };
  109. };
  110. };