PerCameraData.bslinc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. mixin PerCameraData
  2. {
  3. code
  4. {
  5. [internal]
  6. cbuffer PerCamera
  7. {
  8. float3 gViewDir;
  9. float3 gViewOrigin;
  10. float4x4 gMatViewProj;
  11. float4x4 gMatView;
  12. float4x4 gMatProj;
  13. float4x4 gMatInvProj;
  14. float4x4 gMatInvViewProj;
  15. // Special inverse view-projection matrix that had projection entries that affect z and w eliminated.
  16. // Used to transform a vector(clip_x, clip_y, view_z, view_w), where clip_x/clip_y are in clip space,
  17. // and view_z/view_w in view space, into world space
  18. float4x4 gMatScreenToWorld;
  19. // Transforms a location in NDC, to the location of the same pixel on the previous frame. Used for
  20. // determining camera movement for temporal filtering
  21. float4x4 gNDCToPrevNDC;
  22. // Converts device Z to world Z using this formula: worldZ = (1 / (deviceZ + y)) * x
  23. float2 gDeviceZToWorldZ;
  24. float2 gNDCZToWorldZ;
  25. float2 gNDCZToDeviceZ;
  26. // x - near plane distance, y - far plane distance
  27. float2 gNearFar;
  28. // xy - Viewport offset in pixels
  29. // zw - Viewport width & height in pixels
  30. int4 gViewportRectangle;
  31. // xy - (Viewport size in pixels / 2) / Target size in pixels
  32. // zw - (Viewport offset in pixels + (Viewport size in pixels / 2) + Optional pixel center offset) / Target size in pixels
  33. float4 gClipToUVScaleOffset;
  34. float gAmbientFactor;
  35. }
  36. /** Converts Z value in range [0,1] into Z value in view space. */
  37. float convertFromDeviceZ(float deviceZ)
  38. {
  39. // Note: Convert to MAD form
  40. return gDeviceZToWorldZ.x / (deviceZ + gDeviceZToWorldZ.y);
  41. }
  42. /** Converts Z value in range [0,1] into Z value in view space. */
  43. float4 convertFromDeviceZ(float4 deviceZ)
  44. {
  45. // Note: Convert to MAD form
  46. return gDeviceZToWorldZ.x / (deviceZ + gDeviceZToWorldZ.y);
  47. }
  48. /** Converts Z value from view space to NDC space. */
  49. float convertToNDCZ(float viewZ)
  50. {
  51. return -gNDCZToWorldZ.y + (gNDCZToWorldZ.x / viewZ);
  52. }
  53. /** Converts Z value from NDC space to device Z value in range [0, 1]. */
  54. float NDCZToDeviceZ(float ndcZ)
  55. {
  56. return (ndcZ + gNDCZToDeviceZ.y) * gNDCZToDeviceZ.x;
  57. }
  58. /** Converts Z value from device range ([0, 1]) to NDC space. */
  59. float DeviceZToNDCZ(float deviceZ)
  60. {
  61. return deviceZ / gNDCZToDeviceZ.x - gNDCZToDeviceZ.y;
  62. }
  63. /** Converts position in NDC to UV coordinates mapped to the screen rectangle. */
  64. float2 NDCToUV(float2 ndcPos)
  65. {
  66. return ndcPos.xy * gClipToUVScaleOffset.xy + gClipToUVScaleOffset.zw;
  67. }
  68. /** Converts position in UV coordinates mapped to screen rectangle to NDC coordinates. */
  69. float2 UVToNDC(float2 uvPos)
  70. {
  71. return (uvPos - gClipToUVScaleOffset.zw) / gClipToUVScaleOffset.xy;
  72. }
  73. /** Converts position in UV coordinates mapped to the screen, to screen coordinates in pixels. */
  74. uint2 UVToScreen(float2 uv)
  75. {
  76. return (uint2)(uv * (float2)gViewportRectangle.zw - ((float2)gViewportRectangle.xy));
  77. }
  78. /** Converts position in NDC to screen coordinates in pixels. */
  79. uint2 NDCToScreen(float2 ndcPos)
  80. {
  81. float2 uv = NDCToUV(ndcPos);
  82. return UVToScreen(uv);
  83. }
  84. /** Converts position in NDC to world space. */
  85. float3 NDCToWorld(float2 ndcPos, float depth)
  86. {
  87. // x, y are now in clip space, z, w are in view space
  88. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  89. // z, w eliminated (since they are already in view space)
  90. // Note: Multiply by depth should be avoided if using ortographic projection
  91. float4 mixedSpacePos = float4(ndcPos.xy * -depth, depth, 1);
  92. float4 worldPosition4D = mul(gMatScreenToWorld, mixedSpacePos);
  93. return worldPosition4D.xyz / worldPosition4D.w;
  94. }
  95. };
  96. };