Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using UnityEngine.Experimental.Rendering;
  2. using UnityEngine.Rendering.RenderGraphModule;
  3. namespace UnityEngine.Rendering.Universal
  4. {
  5. internal static class StpUtils
  6. {
  7. static void CalculateJitter(int frameIndex, out Vector2 jitter, out bool allowScaling)
  8. {
  9. // NOTE: STP's jitter must be negated in URP due to y-flip
  10. jitter = -STP.Jit16(frameIndex);
  11. allowScaling = false;
  12. }
  13. // Static allocation of JitterFunc delegate to avoid GC
  14. internal static TemporalAA.JitterFunc s_JitterFunc = CalculateJitter;
  15. static void PopulateStpConfig(UniversalCameraData cameraData, TextureHandle inputColor, TextureHandle inputDepth, TextureHandle inputMotion, int debugViewIndex, TextureHandle debugView, TextureHandle destination, Texture2D noiseTexture, out STP.Config config)
  16. {
  17. cameraData.camera.TryGetComponent<UniversalAdditionalCameraData>(out var additionalCameraData);
  18. Debug.Assert(additionalCameraData != null);
  19. var motionData = additionalCameraData.motionVectorsPersistentData;
  20. Debug.Assert(motionData != null);
  21. // Dynamic scaling isn't supported
  22. config.enableHwDrs = false;
  23. // URP only uses texture arrays when XR single pass mode is enabled
  24. config.enableTexArray = cameraData.xr.enabled && cameraData.xr.singlePassEnabled;
  25. // The motion scaling feature is only active outside of test environments.
  26. // If we allowed it to run during automated graphics tests, the results of each test run would be dependent on system performance.
  27. #if LWRP_DEBUG_STATIC_POSTFX
  28. config.enableMotionScaling = false;
  29. #else
  30. config.enableMotionScaling = true;
  31. #endif
  32. int frameIndex = Time.frameCount;
  33. config.noiseTexture = noiseTexture;
  34. config.inputColor = inputColor;
  35. config.inputDepth = inputDepth;
  36. config.inputMotion = inputMotion;
  37. // TODO: Support STP Stencil Responsive Feature in URP (JIRA: GFXRT-366)
  38. //
  39. // STP is capable of treating certain pixels in the image differently if they're known to cause
  40. // problems when run through TAA. Normally this is handled by using a single bit in the stencil
  41. // buffer to mask these pixels. This feature is typically used for transparent objects like sparks
  42. // and it can significantly improve visual quality on some scenes when enabled.
  43. config.inputStencil = TextureHandle.nullHandle;
  44. config.stencilMask = 0;
  45. config.debugView = debugView;
  46. config.destination = destination;
  47. var stpHistory = cameraData.stpHistory;
  48. Debug.Assert(stpHistory != null);
  49. int eyeIndex = (cameraData.xr.enabled && !cameraData.xr.singlePassEnabled) ? cameraData.xr.multipassId : 0;
  50. config.historyContext = stpHistory.GetHistoryContext(eyeIndex);
  51. config.nearPlane = cameraData.camera.nearClipPlane;
  52. config.farPlane = cameraData.camera.farClipPlane;
  53. config.frameIndex = frameIndex;
  54. config.hasValidHistory = !cameraData.resetHistory;
  55. config.debugViewIndex = debugViewIndex;
  56. config.deltaTime = motionData.deltaTime;
  57. config.lastDeltaTime = motionData.lastDeltaTime;
  58. // Note: The current and prior image sizes are always identical since DRS is not currently supported.
  59. config.currentImageSize = new Vector2Int(cameraData.cameraTargetDescriptor.width, cameraData.cameraTargetDescriptor.height);
  60. config.priorImageSize = config.currentImageSize;
  61. config.outputImageSize = new Vector2Int(cameraData.pixelWidth, cameraData.pixelHeight);
  62. // The number of active views may vary over time, but it must never be more than the number of supported view configs.
  63. int numActiveViews = cameraData.xr.enabled ? cameraData.xr.viewCount : 1;
  64. Debug.Assert(numActiveViews <= STP.perViewConfigs.Length);
  65. for (int viewIndex = 0; viewIndex < numActiveViews; ++viewIndex)
  66. {
  67. int targetIndex = viewIndex + eyeIndex;
  68. STP.PerViewConfig perViewConfig;
  69. // STP requires non-jittered versions of the current, previous, and "previous previous" projection matrix.
  70. perViewConfig.currentProj = motionData.projectionStereo[targetIndex];
  71. perViewConfig.lastProj = motionData.previousProjectionStereo[targetIndex];
  72. perViewConfig.lastLastProj = motionData.previousPreviousProjectionStereo[targetIndex];
  73. perViewConfig.currentView = motionData.viewStereo[targetIndex];
  74. perViewConfig.lastView = motionData.previousViewStereo[targetIndex];
  75. perViewConfig.lastLastView = motionData.previousPreviousViewStereo[targetIndex];
  76. // NOTE: STP assumes the view matrices also contain the world space camera position so we inject the camera position directly here.
  77. Vector3 currentPosition = motionData.worldSpaceCameraPos;
  78. Vector3 lastPosition = motionData.previousWorldSpaceCameraPos;
  79. Vector3 lastLastPosition = motionData.previousPreviousWorldSpaceCameraPos;
  80. perViewConfig.currentView.SetColumn(3, new Vector4(-currentPosition.x, -currentPosition.y, -currentPosition.z, 1.0f));
  81. perViewConfig.lastView.SetColumn(3, new Vector4(-lastPosition.x, -lastPosition.y, -lastPosition.z, 1.0f));
  82. perViewConfig.lastLastView.SetColumn(3, new Vector4(-lastLastPosition.x, -lastLastPosition.y, -lastLastPosition.z, 1.0f));
  83. STP.perViewConfigs[viewIndex] = perViewConfig;
  84. }
  85. config.numActiveViews = numActiveViews;
  86. config.perViewConfigs = STP.perViewConfigs;
  87. }
  88. static internal void Execute(RenderGraph renderGraph, UniversalResourceData resourceData, UniversalCameraData cameraData, TextureHandle inputColor, TextureHandle inputDepth, TextureHandle inputMotion, TextureHandle destination, Texture2D noiseTexture)
  89. {
  90. var debugView = TextureHandle.nullHandle;
  91. int debugViewIndex = 0;
  92. DebugHandler debugHandler = ScriptableRenderPass.GetActiveDebugHandler(cameraData);
  93. if ((debugHandler != null) && debugHandler.TryGetFullscreenDebugMode(out var fullscreenDebugMode))
  94. {
  95. if (fullscreenDebugMode == DebugFullScreenMode.STP)
  96. {
  97. debugView = renderGraph.CreateTexture(new TextureDesc(cameraData.pixelWidth, cameraData.pixelHeight, false, (cameraData.xr.enabled && cameraData.xr.singlePassEnabled))
  98. {
  99. name = "STP Debug View",
  100. colorFormat = GraphicsFormat.R8G8B8A8_UNorm,
  101. clearBuffer = true,
  102. enableRandomWrite = true
  103. });
  104. debugViewIndex = debugHandler.stpDebugViewIndex;
  105. // Save the debug view texture so it can be displayed later
  106. resourceData.stpDebugView = debugView;
  107. }
  108. }
  109. PopulateStpConfig(cameraData, inputColor, inputDepth, inputMotion, debugViewIndex, debugView, destination, noiseTexture, out var config);
  110. STP.Execute(renderGraph, ref config);
  111. }
  112. }
  113. }