PerCameraData.bslinc 3.6 KB

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