ShadowDepthCube.bsl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #define USES_GS
  2. #include "$ENGINE$\ShadowDepthBase.bslinc"
  3. technique ShadowDepth
  4. {
  5. mixin ShadowDepthBase;
  6. code
  7. {
  8. struct GSToPS
  9. {
  10. float4 position : SV_Position;
  11. uint targetIdx : SV_RenderTargetArrayIndex;
  12. };
  13. cbuffer ShadowCubeMatrices
  14. {
  15. float4x4 gFaceVPMatrices[6];
  16. };
  17. cbuffer ShadowCubeMasks
  18. {
  19. uint gFaceMasks[6];
  20. };
  21. [maxvertexcount(18)]
  22. void gsmain(triangle ShadowVStoFS inputs[3], inout TriangleStream<GSToPS> outStream)
  23. {
  24. // Output a triangle to all relevant faces
  25. [unroll]
  26. for (int faceIdx = 0; faceIdx < 6; faceIdx++)
  27. {
  28. // Check the per-object masks that were determined based on CPU frustum culling
  29. [branch]
  30. if (gFaceMasks[faceIdx] > 0)
  31. {
  32. float4 clipPos[3];
  33. [unroll]
  34. for (int vertIdx = 0; vertIdx < 3; vertIdx++)
  35. clipPos[vertIdx] = mul(gFaceVPMatrices[faceIdx], inputs[vertIdx].worldPos);
  36. // Per-triangle frustum culling
  37. // Note: Test if this helps or hurts performance
  38. float4 testMask = saturate(clipPos[0].xyxy * float4(-1, -1, 1, 1) - clipPos[0].w);
  39. testMask *= saturate(clipPos[1].xyxy * float4(-1, -1, 1, 1) - clipPos[1].w);
  40. testMask *= saturate(clipPos[2].xyxy * float4(-1, -1, 1, 1) - clipPos[2].w);
  41. [branch]
  42. if (all(testMask == 0))
  43. {
  44. GSToPS output;
  45. output.targetIdx = faceIdx;
  46. // Note: I'm reversing the order here because otherwise the triangle order is reversed
  47. // (not sure why, but GS seems like it could be the only culprit). This doesn't happen in
  48. // OpenGL, because we flip the projection matrix Y axis due to upside-down UV coordinates
  49. [unroll]
  50. #ifdef OPENGL
  51. for (int vertIdx = 0; vertIdx < 3; vertIdx++)
  52. #else
  53. for (int vertIdx = 2; vertIdx >= 0; vertIdx--)
  54. #endif
  55. {
  56. output.position = clipPos[vertIdx];
  57. outStream.Append(output);
  58. }
  59. outStream.RestartStrip();
  60. }
  61. }
  62. }
  63. }
  64. };
  65. };