Guest User

Untitled

a guest
Nov 16th, 2026
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 85.01 KB | None | 0 0
  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2.  
  3. #define EYE_ADAPTATION_LOOSE_PARAMETERS 1
  4.  
  5. #include "Common.ush"
  6. #include "Random.ush"
  7. #include "EyeAdaptationCommon.ush"
  8. #include "TextureSampling.ush"
  9. #include "MonteCarlo.ush"
  10. #include "Quantization.ush"
  11.  
  12. //------------------------------------------------------- COMPILER CONFIG
  13.  
  14. // Generate vector truncation warnings to errors.
  15. #pragma warning(error: 3206)
  16.  
  17.  
  18. //------------------------------------------------------- ENUM VALUES
  19.  
  20. /** Payload of the history. History might still have addtional TAA internals. */
  21.     // Only have RGB.
  22.     #define HISTORY_PAYLOAD_RGB 0
  23.    
  24.     // Have RGB and translucency in alpha.
  25.     #define HISTORY_PAYLOAD_RGB_TRANSLUCENCY 1
  26.    
  27.     // Have RGB and opacity in alpha.
  28.     #define HISTORY_PAYLOAD_RGB_OPACITY (HISTORY_PAYLOAD_RGB_TRANSLUCENCY)
  29.    
  30.     // Have RGB and DOF's CocRadius in alpha.
  31.     #define HISTORY_PAYLOAD_RGB_COC 2
  32.    
  33.     // Have RGB, opacity in alpha and DOF's CocRadius in separate's Red.
  34.     #define HISTORY_PAYLOAD_RGB_OPACITY_COC 3
  35.    
  36.  
  37. /** Caching method for scene color. */
  38.     // Disable any in code cache.
  39.     #define AA_SAMPLE_CACHE_METHOD_DISABLE 0
  40.    
  41.     // Caches 3x3 Neighborhood into VGPR (although my have corner optimised away).
  42.     #define AA_SAMPLE_CACHE_METHOD_VGPR_3X3 1
  43.    
  44.     // Prefetches scene color into 10x10 LDS tile (8x8 when screen percentage < 71%).
  45.     #define AA_SAMPLE_CACHE_METHOD_LDS 2
  46.    
  47.  
  48. /** Clamping method for scene color. */
  49.     // Min max neighboorhing samples.
  50.     #define HISTORY_CLAMPING_BOX_MIN_MAX 0
  51.    
  52.     // Variance computed from neighboorhing samples.
  53.     #define HISTORY_CLAMPING_BOX_VARIANCE 1
  54.  
  55.     // Min max samples that are within distance from output pixel.
  56.     #define HISTORY_CLAMPING_BOX_SAMPLE_DISTANCE 2
  57.  
  58. /** Qualities */
  59.     #define TAA_QUALITY_LOW 0
  60.     #define TAA_QUALITY_MEDIUM 1
  61.     #define TAA_QUALITY_HIGH 2
  62.     #define TAA_QUALITY_MEDIUM_HIGH 3
  63.  
  64.  
  65. //-------------------------------------------------------
  66. // GHOSTFIX
  67. //-------------------------------------------------------
  68. // Tags: [AA+] higher = more AA, more ghosting.  [GHOST-] higher = less ghosting, less AA.  (Lower = the opposite.)
  69. // Rejection (0..1) = max(X speed ramp, Y speed ramp) * near factor. Speed = the pixel's OWN motion in pixels/frame.
  70. // Rejected pixels use less history (less AA). Still image = original shader.
  71.  
  72. #define GHOSTFIX_ENABLE          1      // 0 = original shader
  73. #define GHOSTFIX_SPEED_X_START   1.0    // Sideways speed where rejection starts. Higher = [AA+]
  74. #define GHOSTFIX_SPEED_X_FULL    2.0    // Sideways speed of full rejection. Higher = [AA+]. START = FULL = 999 turns X off
  75. #define GHOSTFIX_SPEED_Y_START   1.0    // Vertical speed where rejection starts. Higher = [AA+]
  76. #define GHOSTFIX_SPEED_Y_FULL    2.0    // Vertical speed of full rejection. Higher = [AA+]. START = FULL = 999 turns Y off
  77. #define GHOSTFIX_SPREAD          99.0   // Pixels. Each pixel also takes the fastest camera speed found this far around it, so the still centre of a drift (speed 0) gets rejected too. 0 = off. Higher = [GHOST-]
  78. #define GHOSTFIX_STRENGTH        1.0    // Current frame weight at full rejection, 1 = no AA. Higher = [GHOST-]
  79. #define GHOSTFIX_DEPTH_NEAR      480.0  // Depth (cm) at/below which rejection is full. Higher = [GHOST-]
  80. #define GHOSTFIX_DEPTH_FAR       800.0  // Depth (cm) at/above which rejection is 0, 0 = no depth limit. Higher = [GHOST-]
  81. #define GHOSTFIX_BLUR            1.0    // 0..1 blur of rejected pixels (hides jitter). Higher = [AA+]
  82. #define GHOSTFIX_PROTECT_MOVING  1      // 1 = moving objects (car) keep full AA. 0 = [GHOST-]
  83. #define GHOSTFIX_TRAIL           1      // 1 = drop history left behind moving objects. 1 = [GHOST-]
  84. #define GHOSTFIX_TRAIL_THRESHOLD 0.1    // History "moving object" flag (0..1) needed to drop it. Lower = [GHOST-], higher = [AA+]
  85. #define GHOSTFIX_DEBUG_REDMASK   0      // 0 = off, 1 = red where GhostFix removes AA (fades out as AA increases), 2 = own speed (red X, green Y, black = none), 3 = history moving-object flag (green)
  86.  
  87. #if !GHOSTFIX_ENABLE
  88.     #undef GHOSTFIX_TRAIL
  89.     #define GHOSTFIX_TRAIL 0
  90.     #undef GHOSTFIX_DEBUG_REDMASK
  91.     #define GHOSTFIX_DEBUG_REDMASK 0
  92. #endif
  93. //------------------------------------------------------- END OF GHOSTFIX CONFIG
  94.  
  95.  
  96. //------------------------------------------------------- CONFIGS
  97.  
  98. // Compute shaders always do responsive TAA in a single pass.
  99. #if COMPUTESHADER
  100.     #define AA_SINGLE_PASS_RESPONSIVE SHADING_PATH_DEFERRED //The StencilTexture is not available on the mobile platform
  101. #endif
  102.  
  103. #if TAA_PASS_CONFIG == 0 // Main
  104.     #if TAA_ALPHA_CHANNEL
  105.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_TRANSLUCENCY)
  106.     #else
  107.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB)
  108.     #endif
  109.     #define AA_BICUBIC 1
  110.     #define AA_CROSS 2
  111.     #define AA_DYNAMIC 1
  112.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  113.     #define AA_TONE 1
  114.     #define AA_YCOCG 1
  115.  
  116.     #if TAA_QUALITY == TAA_QUALITY_LOW
  117.         #define AA_FILTERED 0
  118.         #define AA_DYNAMIC_ANTIGHOST 0
  119.  
  120.     #elif TAA_QUALITY == TAA_QUALITY_MEDIUM
  121.         #define AA_FILTERED 1
  122.         #define AA_DYNAMIC_ANTIGHOST 0
  123.  
  124.     #elif TAA_QUALITY == TAA_QUALITY_HIGH
  125.         #define AA_FILTERED 1
  126.         #define AA_DYNAMIC_ANTIGHOST 1
  127.  
  128.     #elif TAA_QUALITY == TAA_QUALITY_MEDIUM_HIGH
  129.         #define AA_FILTERED 1
  130.         #define AA_DYNAMIC_ANTIGHOST 1
  131.  
  132.     #else
  133.         #error Unknown TAA quality
  134.     #endif
  135.  
  136.     #if COMPUTESHADER
  137.         #if AA_MOBILE_CONFIG
  138.             #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_DISABLE) // the shared memory is not as efficient as expected on mobile devices, try not to use it on mobile devices.
  139.         #else
  140.             #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_LDS)
  141.         #endif
  142.     #endif
  143. #elif TAA_PASS_CONFIG == 7 // Used for Hair
  144.     #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_OPACITY)
  145.     #define AA_DYNAMIC 1
  146.     #define AA_FILTERED 0
  147.     #define AA_LERP 3
  148.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  149.     #define AA_YCOCG 0
  150. #elif TAA_PASS_CONFIG == 3 // Used for SSR
  151.     #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_OPACITY)
  152.     #define AA_DYNAMIC 1
  153.     #define AA_FILTERED 1
  154.     #define AA_LERP 8
  155.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  156.     #define AA_YCOCG 1
  157.  
  158. #elif TAA_PASS_CONFIG == 4 // Used for LightShaft
  159.     #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_OPACITY)
  160.     #define AA_FILTERED 1
  161.     #define AA_LERP 64
  162.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  163.     #define AA_YCOCG 1
  164.     #define AA_LOWER_RESOLUTION 1
  165.  
  166. #elif TAA_PASS_CONFIG == 1 || TAA_PASS_CONFIG == 2 // MainUpsampling & MainSuperSampling
  167.     #if TAA_ALPHA_CHANNEL
  168.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_TRANSLUCENCY)
  169.     #else
  170.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB)
  171.     #endif
  172.     #define AA_BICUBIC 1
  173.     #define AA_CROSS 1
  174.     #define AA_DYNAMIC 1
  175.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  176.     #define AA_TONE 1
  177.     #if SWITCH_PROFILE || SWITCH_PROFILE_FORWARD
  178.         #define AA_YCOCG 0
  179.     #else
  180.         #define AA_YCOCG 1
  181.     #endif
  182.    
  183.     #define AA_UPSAMPLE 1
  184.     #define AA_UPSAMPLE_ADAPTIVE_FILTERING 1
  185.  
  186.     #if TAA_QUALITY == TAA_QUALITY_LOW
  187.         #define AA_FILTERED 0
  188.         #if AA_MOBILE_CONFIG
  189.             #define AA_SAMPLES 5
  190.         #else
  191.             #define AA_SAMPLES 6
  192.         #endif
  193.  
  194.     #elif TAA_QUALITY == TAA_QUALITY_MEDIUM
  195.         #define AA_FILTERED 1
  196.         #if AA_MOBILE_CONFIG
  197.             #define AA_SAMPLES 5
  198.         #else
  199.             #define AA_SAMPLES 6
  200.         #endif
  201.  
  202.     #elif TAA_QUALITY == TAA_QUALITY_HIGH
  203.         #define AA_HISTORY_CLAMPING_BOX (HISTORY_CLAMPING_BOX_SAMPLE_DISTANCE)
  204.         #define AA_FILTERED 1
  205.         #define AA_DYNAMIC_ANTIGHOST 1
  206.         #define AA_SAMPLES 9
  207.  
  208.     #elif TAA_QUALITY == TAA_QUALITY_MEDIUM_HIGH
  209.         #define AA_FILTERED 1
  210.         #if AA_MOBILE_CONFIG
  211.             #define AA_SAMPLES 5
  212.         #else
  213.             #define AA_SAMPLES 6
  214.         #endif
  215.         #define AA_DYNAMIC_ANTIGHOST 1
  216.  
  217.     #else
  218.         #error Unknown TAA quality
  219.     #endif
  220.  
  221.     #if COMPUTESHADER
  222.         // Do not use LDS caching for screen percentage > 100% or < 50%.
  223.         #if TAA_SCREEN_PERCENTAGE_RANGE == 2 || TAA_SCREEN_PERCENTAGE_RANGE == 3
  224.             #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_VGPR_3X3)
  225.  
  226.  
  227.         #else
  228.             #if AA_MOBILE_CONFIG
  229.                 #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_DISABLE) // the shared memory is not as efficient as expected on mobile devices, try not to use it on mobile devices.
  230.             #else
  231.                 #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_LDS)
  232.             #endif
  233.         #endif
  234.     #endif
  235.    
  236. #elif TAA_PASS_CONFIG == 5 || TAA_PASS_CONFIG == 6 // Used for diaphragm DOF pre-filtering.
  237.     #if TAA_ALPHA_CHANNEL
  238.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_OPACITY_COC)
  239.     #else
  240.         #define AA_HISTORY_PAYLOAD (HISTORY_PAYLOAD_RGB_COC)
  241.     #endif
  242.     #define AA_CROSS 4 // because running at half res.
  243.     #define AA_DYNAMIC 1
  244.     #define AA_FORCE_ALPHA_CLAMP 1
  245.     #define AA_MANUALLY_CLAMP_HISTORY_UV 1
  246.     #define AA_LOWER_RESOLUTION 1
  247.  
  248.     #if TAA_PASS_CONFIG == 6
  249.         #define AA_UPSAMPLE 1
  250.         #define AA_UPSAMPLE_ADAPTIVE_FILTERING 1
  251.     #else
  252.         #define AA_UPSAMPLE 0
  253.     #endif
  254.  
  255.     #if TAA_QUALITY == TAA_QUALITY_MEDIUM
  256.         #if AA_UPSAMPLE
  257.             #define AA_BICUBIC 1
  258.             #define AA_FILTERED 1
  259.         #endif
  260.  
  261.     #elif TAA_QUALITY == TAA_QUALITY_HIGH
  262.         #define AA_BICUBIC 1
  263.         #define AA_FILTERED 1
  264.         #define AA_YCOCG 1
  265.  
  266.     #elif TAA_QUALITY == TAA_QUALITY_MEDIUM_HIGH
  267.         #if AA_UPSAMPLE
  268.             #define AA_BICUBIC 1
  269.             #define AA_FILTERED 1
  270.         #endif
  271.  
  272.     #else
  273.         #error Unknown TAA quality
  274.     #endif
  275.  
  276.     #if TAA_SCREEN_PERCENTAGE_RANGE != 2 && AA_UPSAMPLE
  277.         //#define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_LDS) // TODO: doesn't work yet.
  278.     #endif
  279.    
  280. #else
  281.     #error Unknown TAA pass config. Have you changed ETAAPassConfig without updating me?
  282.  
  283. #endif
  284.  
  285. #if TAA_SCREEN_PERCENTAGE_RANGE == 2
  286.     #define AA_DOWNSAMPLE 1
  287. #else
  288.     #define AA_DOWNSAMPLE 0
  289. #endif
  290.  
  291.  
  292. //------------------------------------------------------- CONFIG DISABLED DEFAULTS
  293.  
  294. // Num samples of current frame
  295. #ifndef AA_SAMPLES
  296.     #define AA_SAMPLES 5
  297. #endif
  298.  
  299. // 1 = Use tighter AABB clamp for history.
  300. // 0 = Use simple min/max clamp.
  301. #ifndef AA_CLIP
  302.     #define AA_CLIP 0
  303. #endif
  304.  
  305. // Cross distance in pixels used in depth search X pattern.
  306. // 0 = Turn this feature off.
  307. // 2 = Is required for standard temporal AA pass.
  308. #ifndef AA_CROSS
  309.     #define AA_CROSS 0
  310. #endif
  311.  
  312. // 1 = Use dynamic motion.
  313. // 0 = Skip dynamic motion, currently required for half resolution passes.
  314. #ifndef AA_DYNAMIC
  315.     #define AA_DYNAMIC 0
  316. #endif
  317.  
  318. // 0 = Dynamic motion based lerp value (default).
  319. // non-zero = Use 1/LERP fixed lerp value (used for reflections).
  320. #ifndef AA_LERP
  321.     #define AA_LERP 0
  322. #endif
  323.  
  324. // 1 = Use higher quality round clamp.
  325. // 0 = Use lower quality but faster box clamp.
  326. #ifndef AA_ROUND
  327.     #define AA_ROUND 0
  328. #endif
  329.  
  330. // Force clamp on alpha.
  331. #ifndef AA_FORCE_ALPHA_CLAMP
  332.     #define AA_FORCE_ALPHA_CLAMP 0
  333. #endif
  334.  
  335. // Use YCoCg path.
  336. #ifndef AA_YCOCG
  337.     #define AA_YCOCG 0
  338. #endif
  339.  
  340. // Bicubic filter history
  341. #ifndef AA_BICUBIC
  342.     #define AA_BICUBIC 0
  343. #endif
  344.  
  345. // Tone map to kill fireflies
  346. #ifndef AA_TONE
  347.     #define AA_TONE 0
  348. #endif
  349.  
  350. // Antighosting using dynamic mask
  351. #ifndef AA_DYNAMIC_ANTIGHOST
  352.     #define AA_DYNAMIC_ANTIGHOST 0
  353. #endif
  354.  
  355. // Sample the stencil buffer inline rather than multiple masked passes.
  356. #ifndef AA_SINGLE_PASS_RESPONSIVE
  357.     #define AA_SINGLE_PASS_RESPONSIVE   0
  358. #endif
  359.  
  360. // Upsample the output.
  361. #ifndef AA_UPSAMPLE
  362.     #define AA_UPSAMPLE 0
  363. #endif
  364.  
  365. // Method used for generating the history clamping box.
  366. #ifndef AA_HISTORY_CLAMPING_BOX
  367.     #define AA_HISTORY_CLAMPING_BOX (HISTORY_CLAMPING_BOX_MIN_MAX)
  368. #endif
  369.  
  370. // Change the upsampling filter size when history is rejected that reduce blocky output pixels.
  371. #ifndef AA_UPSAMPLE_ADAPTIVE_FILTERING
  372.     #define AA_UPSAMPLE_ADAPTIVE_FILTERING 0
  373. #endif
  374.  
  375. // Whether this pass run at lower resolution than main view rectangle.
  376. #ifndef AA_LOWER_RESOLUTION
  377.     #define AA_LOWER_RESOLUTION 0
  378. #endif
  379.  
  380. // Whether the history buffer UV should be manually clamped.
  381. #ifndef AA_MANUALLY_CLAMP_HISTORY_UV
  382.     #define AA_MANUALLY_CLAMP_HISTORY_UV 0
  383. #endif
  384.  
  385.  
  386. //------------------------------------------------------- CONFIG ENABLED DEFAULTS
  387.  
  388. // Always enable scene color filtering
  389. // 1 = Use filtered sample.
  390. // 0 = Use center sample.
  391. #ifndef AA_FILTERED
  392.     #define AA_FILTERED 1
  393. #endif
  394.  
  395. // Always enable AA_NAN to avoid all NAN in all TAA pass that is more convenient considering the amount of / 0 we can have.
  396. // 0 = Don't use.
  397. // 1 = Use extra clamp to avoid NANs
  398. #ifndef AA_NAN
  399.     #define AA_NAN 1
  400. #endif
  401.  
  402. // Neighborhood clamping. Disable for testing reprojection. Always enabled, well because TAA is totally broken otherwise.
  403. #ifndef AA_CLAMP
  404.     #define AA_CLAMP 1
  405. #endif
  406.  
  407. // By default, always cache neighbooring samples into VGPR.
  408. #ifndef AA_SAMPLE_CACHE_METHOD
  409.     #if COMPUTESHADER
  410.         #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_VGPR_3X3)
  411.     #else
  412.         #define AA_SAMPLE_CACHE_METHOD (AA_SAMPLE_CACHE_METHOD_DISABLE)
  413.     #endif
  414. #endif
  415.  
  416. // By default, enable stocastic quantization of the output.
  417. #ifndef AA_ENABLE_STOCASTIC_QUANTIZATION
  418.     #define AA_ENABLE_STOCASTIC_QUANTIZATION 1
  419. #endif
  420.  
  421. // GHOSTFIX: force the dynamic history anti ghosting on for every quality that has dynamic motion (see GHOSTFIX config).
  422. #if GHOSTFIX_TRAIL && AA_DYNAMIC
  423.     #undef AA_DYNAMIC_ANTIGHOST
  424.     #define AA_DYNAMIC_ANTIGHOST 1
  425. #endif
  426.  
  427. // GHOSTFIX: whether the weighted neighborhood moments (mean / std deviation) are needed (only by the engine's variance clamping box).
  428. #if (AA_HISTORY_CLAMPING_BOX == HISTORY_CLAMPING_BOX_VARIANCE)
  429.     #define GHOSTFIX_NEEDS_MOMENTS 1
  430. #else
  431.     #define GHOSTFIX_NEEDS_MOMENTS 0
  432. #endif
  433.  
  434. // GHOSTFIX: the speed rejection only runs in the main color passes (0, 1, 2).
  435. #if GHOSTFIX_ENABLE && !(AA_LERP || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC || !(TAA_PASS_CONFIG == 0 || TAA_PASS_CONFIG == 1 || TAA_PASS_CONFIG == 2))
  436.     #define GHOSTFIX_ACTIVE 1
  437. #else
  438.     #define GHOSTFIX_ACTIVE 0
  439. #endif
  440.  
  441. // GHOSTFIX: whether the 3x3 gaussian blurred current color is needed.
  442. #if GHOSTFIX_ACTIVE
  443.     #define GHOSTFIX_NEEDS_BLUR 1
  444. #else
  445.     #define GHOSTFIX_NEEDS_BLUR 0
  446. #endif
  447.  
  448.  
  449. //------------------------------------------------------- MENDATORY CONFIG
  450.  
  451. #ifndef AA_HISTORY_PAYLOAD
  452.     #error You forgot to defines the history payload.
  453. #endif
  454.  
  455.  
  456. //------------------------------------------------------- DERIVES
  457.  
  458. // Defines number of component in history payload.
  459. #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB
  460.     #define HISTORY_PAYLOAD_COMPONENTS 3
  461. #elif AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  462.     #define HISTORY_PAYLOAD_COMPONENTS 5
  463. #else
  464.     #define HISTORY_PAYLOAD_COMPONENTS 4
  465. #endif
  466.  
  467. // Defines the number of render target to store TAA's history.
  468. #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  469.     #define HISTORY_RENDER_TARGETS 2
  470. #else
  471.     #define HISTORY_RENDER_TARGETS 1
  472. #endif
  473.  
  474.  
  475. //------------------------------------------------------- CONFIG CHECKS
  476.  
  477. #if AA_SAMPLES != 9 && AA_SAMPLES != 5 && AA_SAMPLES != 6
  478.     #error Samples must be 5, (6 for TAAU) or 9
  479. #endif
  480.  
  481. #if AA_SAMPLE_CACHE_METHOD >= 2 && !COMPUTESHADER
  482.     #error Group share only for compute shader.
  483. #endif
  484.  
  485.  
  486. //------------------------------------------------------- CONSTANTS
  487.  
  488. // K = Center of the nearest input pixel.
  489. // O = Center of the output pixel.
  490. //
  491. //          |           |
  492. //    0     |     1     |     2
  493. //          |           |
  494. //          |           |
  495. //  --------+-----------+--------
  496. //          |           |
  497. //          | O         |
  498. //    3     |     K     |     5
  499. //          |           |
  500. //          |           |
  501. //  --------+-----------+--------
  502. //          |           |
  503. //          |           |
  504. //    6     |     7     |     8
  505. //          |           |
  506. //
  507. static const int2 kOffsets3x3[9] =
  508. {
  509.     int2(-1, -1),
  510.     int2( 0, -1),
  511.     int2( 1, -1),
  512.     int2(-1,  0),
  513.     int2( 0,  0), // K
  514.     int2( 1,  0),
  515.     int2(-1,  1),
  516.     int2( 0,  1),
  517.     int2( 1,  1),
  518. };
  519.    
  520. // Indexes of the 3x3 square.
  521. static const uint kSquareIndexes3x3[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  522.  
  523. // Indexes of the offsets to have plus + shape.
  524. static const uint kPlusIndexes3x3[5] = { 1, 3, 4, 5, 7 };
  525.  
  526. // Number of neighbors.
  527. static const uint kNeighborsCount = 9;
  528.  
  529. // GHOSTFIX: 3x3 gaussian kernel (1 2 1 / 2 4 2 / 1 2 1) / 16, same order as kOffsets3x3.
  530. static const float kGhostFixGaussian3x3[9] = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
  531.  
  532.  
  533. #if AA_UPSAMPLE
  534.     // T = Center of the nearest top left pixel input pixel.
  535.     // O = Center of the output pixel.
  536.     //
  537.     //          |
  538.     //    T     |     .
  539.     //          |
  540.     //       O  |
  541.     //  --------+--------
  542.     //          |
  543.     //          |
  544.     //    .     |     .
  545.     //          |
  546.     static const int2 Offsets2x2[4] =
  547.     {
  548.         int2( 0,  0), // T
  549.         int2( 1,  0),
  550.         int2( 0,  1),
  551.         int2( 1,  1),
  552.     };
  553.        
  554.     // Indexes of the 2x2 square.
  555.     static const uint SquareIndexes2x2[4] = { 0, 1, 2, 3 };
  556.  
  557. #endif // AA_UPSAMPLE
  558.  
  559.  
  560. //------------------------------------------------------- PARAMETERS
  561.  
  562. float HistoryPreExposureCorrection;
  563. float CurrentFrameWeight;
  564. int bCameraCut;
  565.  
  566. DECLARE_SCALAR_ARRAY(float, SampleWeights, 9);
  567. DECLARE_SCALAR_ARRAY(float, PlusWeights, 5);
  568.  
  569. float4 ViewportUVToInputBufferUV;
  570. float4 MaxViewportUVAndSvPositionToViewportUV;
  571.  
  572. float2 ScreenPosAbsMax;
  573. float4 ScreenPosToHistoryBufferUV;
  574.  
  575. float4 InputSceneColorSize;
  576. int2 InputMinPixelCoord;
  577. int2 InputMaxPixelCoord;
  578. Texture2D InputSceneColor;
  579. SamplerState InputSceneColorSampler;
  580. Texture2D InputSceneMetadata;
  581. SamplerState InputSceneMetadataSampler;
  582.  
  583. Texture2D SceneDepthTexture;
  584. SamplerState SceneDepthTextureSampler;
  585.  
  586. #if COMPILER_GLSL_ES3_1
  587. Texture2D<uint4> GBufferVelocityTextureSRV;
  588. #else
  589. Texture2D GBufferVelocityTexture;
  590. SamplerState GBufferVelocityTextureSampler;
  591. #endif
  592.  
  593. Texture2D<uint2> StencilTexture;
  594.  
  595. Texture2D HistoryBuffer_0;
  596. Texture2D HistoryBuffer_1;
  597. SamplerState HistoryBufferSampler_0;
  598. SamplerState HistoryBufferSampler_1;
  599. float4 HistoryBufferSize;
  600. float4 HistoryBufferUVMinMax;
  601.  
  602. float CoCBilateralFilterStrength;
  603.  
  604. float4 OutputViewportSize;
  605. float4 OutputViewportRect;
  606. float3 OutputQuantizationError;
  607.  
  608. #if COMPUTESHADER
  609. RWTexture2D<float4> OutComputeTex_0;
  610.  
  611. #if TAA_DOWNSAMPLE
  612. // This shader permutation outputs half resolution image in addition to main full-res one.
  613. // It is more efficient than performing a separate downsampling pass afterwards.
  614. RWTexture2D<float4> OutComputeTexDownsampled;
  615. groupshared float4 GroupSharedDownsampleArray[THREADGROUP_SIZEX*THREADGROUP_SIZEY]; // TODO: share this with GroupSharedArrayF4 when possible
  616. #endif // TAA_DOWNSAMPLE
  617.  
  618. #if HISTORY_RENDER_TARGETS == 2
  619.     RWTexture2D<float4> OutComputeTex_1;
  620. #endif // HISTORY_RENDER_TARGETS == 2
  621. #endif
  622.  
  623. // Temporal upsample specific params.
  624. #if AA_UPSAMPLE
  625.  
  626. float2 InputViewMin;
  627. float4 InputViewSize;
  628.  
  629. // Temporal jitter at the pixel scale.
  630. float2 TemporalJitterPixels;
  631.  
  632. float ScreenPercentage;
  633. float UpscaleFactor; // = 1 / ScreenPercentage
  634.  
  635. #endif // AA_UPSAMPLE
  636.  
  637.  
  638. //------------------------------------------------------- FUNCTIONS
  639.  
  640. #if COMPILER_SUPPORTS_HLSL2021
  641.  
  642. template<typename T>
  643. void CorrectExposure(inout T X)
  644. {
  645.     X *= HistoryPreExposureCorrection;
  646. }
  647.  
  648. #endif
  649.  
  650. ENCODED_VELOCITY_TYPE SampleVelocityTexture(float2 BufferUV, int2 PixelOffset = int2(0, 0))
  651. {
  652. #if COMPILER_GLSL_ES3_1
  653.     int2 Coord = int2(BufferUV * InputSceneColorSize.xy) + PixelOffset;
  654.     return GBufferVelocityTextureSRV.Load(int3(Coord, 0));
  655. #else
  656.     return GBufferVelocityTexture.SampleLevel(GBufferVelocityTextureSampler, BufferUV, 0, PixelOffset);
  657. #endif
  658. }
  659.  
  660. float3 RGBToYCoCg( float3 RGB )
  661. {
  662.     float Y  = dot( RGB, float3(  1, 2,  1 ) );
  663.     float Co = dot( RGB, float3(  2, 0, -2 ) );
  664.     float Cg = dot( RGB, float3( -1, 2, -1 ) );
  665.    
  666.     float3 YCoCg = float3( Y, Co, Cg );
  667.     return YCoCg;
  668. }
  669.  
  670. float3 YCoCgToRGB( float3 YCoCg )
  671. {
  672.     float Y  = YCoCg.x * 0.25;
  673.     float Co = YCoCg.y * 0.25;
  674.     float Cg = YCoCg.z * 0.25;
  675.  
  676.     float R = Y + Co - Cg;
  677.     float G = Y + Cg;
  678.     float B = Y - Co - Cg;
  679.  
  680.     float3 RGB = float3( R, G, B );
  681.     return RGB;
  682. }
  683.  
  684. // Faster but less accurate luma computation.
  685. // Luma includes a scaling by 4.
  686. float Luma4(float3 Color)
  687. {
  688.     return (Color.g * 2.0) + (Color.r + Color.b);
  689. }
  690.  
  691. // Optimized HDR weighting function.
  692. float HdrWeight4(float3 Color, float Exposure)
  693. {
  694.     return rcp(Luma4(Color) * Exposure + 4.0);
  695. }
  696.  
  697. float HdrWeightY(float Color, float Exposure)
  698. {
  699.     return rcp(Color * Exposure + 4.0);
  700. }
  701.  
  702. // Intersect ray with AABB, knowing there is an intersection.
  703. //   Dir = Ray direction.
  704. //   Org = Start of the ray.
  705. //   Box = Box is at {0,0,0} with this size.
  706. // Returns distance on line segment.
  707. float IntersectAABB(float3 Dir, float3 Org, float3 Box)
  708. {
  709.     #if PS4_PROFILE
  710.         // This causes flicker, it should only be used on PS4 until proper fix is in.
  711.         if(min(min(abs(Dir.x), abs(Dir.y)), abs(Dir.z)) < (1.0/65536.0)) return 1.0;
  712.     #endif
  713.     float3 RcpDir = rcp(Dir);
  714.     float3 TNeg = (  Box  - Org) * RcpDir;
  715.     float3 TPos = ((-Box) - Org) * RcpDir;
  716.     return max(max(min(TNeg.x, TPos.x), min(TNeg.y, TPos.y)), min(TNeg.z, TPos.z));
  717. }
  718.  
  719. float HistoryClip(float3 History, float3 Filtered, float3 NeighborMin, float3 NeighborMax)
  720. {
  721. #if 0
  722.     float3 Min = min(Filtered, min(NeighborMin, NeighborMax));
  723.     float3 Max = max(Filtered, max(NeighborMin, NeighborMax)); 
  724.     float3 Avg2 = Max + Min;
  725.     float3 Dir = Filtered - History;
  726.     float3 Org = History - Avg2 * 0.5;
  727.     float3 Scale = Max - Avg2 * 0.5;
  728.     return saturate(IntersectAABB(Dir, Org, Scale));
  729. #else
  730.     float3 BoxMin = NeighborMin;
  731.     float3 BoxMax = NeighborMax;
  732.     //float3 BoxMin = min( Filtered, NeighborMin );
  733.     //float3 BoxMax = max( Filtered, NeighborMax );
  734.  
  735.     float3 RayOrigin = History;
  736.     float3 RayDir = Filtered - History;
  737.     RayDir = select(abs( RayDir ) < (1.0/65536.0), (1.0/65536.0), RayDir);
  738.     float3 InvRayDir = rcp( RayDir );
  739.  
  740.     float3 MinIntersect = (BoxMin - RayOrigin) * InvRayDir;
  741.     float3 MaxIntersect = (BoxMax - RayOrigin) * InvRayDir;
  742.     float3 EnterIntersect = min( MinIntersect, MaxIntersect );
  743.     return max3( EnterIntersect.x, EnterIntersect.y, EnterIntersect.z );
  744. #endif
  745. }
  746.  
  747. float2 WeightedLerpFactors(float WeightA, float WeightB, float Blend)
  748. {
  749.     float BlendA = (1.0 - Blend) * WeightA;
  750.     float BlendB =        Blend  * WeightB;
  751.     float RcpBlend = rcp(BlendA + BlendB);
  752.     BlendA *= RcpBlend;
  753.     BlendB *= RcpBlend;
  754.     return float2(BlendA, BlendB);
  755. }
  756.  
  757. #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  758.  
  759. // Computes the bilateral weight according to two Coc radii.
  760. float ComputeBilateralWeight(float RefCocRadius, float SampleCocRadius)
  761. {
  762.     float Factor = (abs(RefCocRadius) > 1 ? rcp(abs(RefCocRadius)) : 1.0);
  763.     return saturate(1 - abs(RefCocRadius - SampleCocRadius) * Factor);
  764. }
  765.  
  766. float ComputeNeightborSampleBilateralWeight(float CenterCocRadius, float SampleCocRadius)
  767. {
  768.     float Factor = (abs(CenterCocRadius) > 1 ? rcp(abs(CenterCocRadius)) : 1.0);
  769.     return saturate(1 - (CenterCocRadius - SampleCocRadius) * Factor);
  770. }
  771.  
  772.  
  773. #endif
  774.  
  775.  
  776. //------------------------------------------------------- HISTORY's PAYLOAD
  777.  
  778. // Payload of the TAA's history.
  779. struct FTAAHistoryPayload
  780. {
  781.     // Transformed scene color and alpha channel.
  782.     float4 Color;
  783.  
  784.     // Radius of the circle of confusion for DOF.
  785.     float CocRadius;
  786. };
  787.  
  788. FTAAHistoryPayload MulPayload(in FTAAHistoryPayload Payload, in float x)
  789. {
  790.     Payload.Color *= x;
  791.     Payload.CocRadius *= x;
  792.     return Payload;
  793. }
  794.  
  795. FTAAHistoryPayload AddPayload(in FTAAHistoryPayload Payload0, in FTAAHistoryPayload Payload1)
  796. {
  797.     Payload0.Color += Payload1.Color;
  798.     Payload0.CocRadius += Payload1.CocRadius;
  799.     return Payload0;
  800. }
  801.  
  802. FTAAHistoryPayload MinPayload(in FTAAHistoryPayload Payload0, in FTAAHistoryPayload Payload1)
  803. {
  804.     Payload0.Color = min(Payload0.Color, Payload1.Color);
  805.     Payload0.CocRadius = min(Payload0.CocRadius, Payload1.CocRadius);
  806.     return Payload0;
  807. }
  808.  
  809. FTAAHistoryPayload MaxPayload(in FTAAHistoryPayload Payload0, in FTAAHistoryPayload Payload1)
  810. {
  811.     Payload0.Color = max(Payload0.Color, Payload1.Color);
  812.     Payload0.CocRadius = max(Payload0.CocRadius, Payload1.CocRadius);
  813.     return Payload0;
  814. }
  815.  
  816. FTAAHistoryPayload MinPayload3(in FTAAHistoryPayload Payload0, in FTAAHistoryPayload Payload1, in FTAAHistoryPayload Payload2)
  817. {
  818.     Payload0.Color = min3(Payload0.Color, Payload1.Color, Payload2.Color);
  819.     Payload0.CocRadius = min3(Payload0.CocRadius, Payload1.CocRadius, Payload2.CocRadius);
  820.     return Payload0;
  821. }
  822.  
  823. FTAAHistoryPayload MaxPayload3(in FTAAHistoryPayload Payload0, in FTAAHistoryPayload Payload1, in FTAAHistoryPayload Payload2)
  824. {
  825.     Payload0.Color = max3(Payload0.Color, Payload1.Color, Payload2.Color);
  826.     Payload0.CocRadius = max3(Payload0.CocRadius, Payload1.CocRadius, Payload2.CocRadius);
  827.     return Payload0;
  828. }
  829.  
  830.  
  831. //------------------------------------------------------- TAA INTERMEDIARY STRUCTURES
  832.  
  833. // Output pixel parameters. Should not be modified once setup.
  834. struct FTAAInputParameters
  835. {
  836.     // Compute shader dispatch params, set to 0 in pixel shader.
  837.     uint2 GroupId;
  838.     uint2 GroupThreadId;
  839.     uint GroupThreadIndex;
  840.  
  841.     // Viewport UV of the output pixel.
  842.     float2 ViewportUV;
  843.  
  844.     // Position of the output pixel on screen.
  845.     float2 ScreenPos;
  846.  
  847.     // Buffer UV of the nearest input pixel.
  848.     float2 NearestBufferUV;
  849.  
  850.     #if AA_UPSAMPLE
  851.         // Buffer UV of the nearest top left input pixel.
  852.         float2 NearestTopLeftBufferUV;
  853.     #endif
  854.    
  855.     // Whether this pixel should be responsive.
  856.     float bIsResponsiveAAPixel;
  857.  
  858.     // Frame exposure's scale.
  859.     float FrameExposureScale;
  860.    
  861.     // Cache of neightbors' transformed scene color.
  862.     #if AA_SAMPLE_CACHE_METHOD == AA_SAMPLE_CACHE_METHOD_VGPR_3X3
  863.         float4 CachedNeighbors0[kNeighborsCount];
  864.  
  865.         #if HISTORY_RENDER_TARGETS == 2
  866.             float CachedNeighbors1[kNeighborsCount];
  867.         #endif
  868.     #endif
  869. };
  870.  
  871.  
  872. // Intermediary results for major function to conveniently share values between them.
  873. //
  874. // it is allowed to passdown this to major function with still unitialized variables.
  875. struct FTAAIntermediaryResult
  876. {
  877.     // The filtered input.
  878.     FTAAHistoryPayload Filtered;
  879.  
  880.     // Temporal weight of the filtered input.
  881.     float FilteredTemporalWeight;
  882.  
  883.     // 1 / filtering kernel scale factor for AA_UPSAMPLE_ADAPTIVE_FILTERING.
  884.     float InvFilterScaleFactor;
  885.  
  886.     // GHOSTFIX: HDR weighted mean and standard deviation of the neighborhood (working color space RGB).
  887.     // Only valid when GHOSTFIX_NEEDS_MOMENTS.
  888.     float3 NeighborMean;
  889.     float3 NeighborStdDev;
  890.  
  891.     // GHOSTFIX: 3x3 HDR weighted gaussian blurred current color (working color space). Only valid when GHOSTFIX_NEEDS_BLUR.
  892.     float4 GhostFixBlurred;
  893. };
  894.  
  895.  
  896. // Create intermediary result.
  897. FTAAIntermediaryResult CreateIntermediaryResult()
  898. {
  899.     // Disable warning X4008: floating point division by zero
  900.     #pragma warning(disable:4008)
  901.     FTAAIntermediaryResult IntermediaryResult = (FTAAIntermediaryResult) (1.0 / 0.0);
  902.     #pragma warning(default:4008)
  903.  
  904.     IntermediaryResult.FilteredTemporalWeight = 1;
  905.     IntermediaryResult.InvFilterScaleFactor = 1;
  906.     // GHOSTFIX: do not leave INF in the moments.
  907.     IntermediaryResult.NeighborMean = 0;
  908.     IntermediaryResult.NeighborStdDev = 0;
  909.     IntermediaryResult.GhostFixBlurred = 0;
  910.     return IntermediaryResult;
  911. }
  912.  
  913.  
  914. // Transformed scene color's data for a sample.
  915. struct FTAASceneColorSample
  916. {
  917.     // Transformed scene color and alpha channel.
  918.     float4 Color;
  919.  
  920.     // Radius of the circle of confusion for DOF.
  921.     float CocRadius;
  922.  
  923.     // HDR weight of the scene color sample.
  924.     float HdrWeight;
  925. };
  926.  
  927.  
  928. //------------------------------------------------------- SCENE COLOR SPACE MANAGMENT
  929.  
  930. // Transform RAW linear scene color RGB to TAA's working color space.
  931. float4 TransformSceneColor(float4 RawLinearSceneColorRGBA)
  932. {
  933.     #if AA_YCOCG
  934.         return float4(RGBToYCoCg(RawLinearSceneColorRGBA.rgb), RawLinearSceneColorRGBA.a);
  935.     #else
  936.         return RawLinearSceneColorRGBA;
  937.     #endif
  938. }
  939.  
  940. // Reciprocal of TransformSceneColor().
  941. float4 TransformBackToRawLinearSceneColor(float4 SceneColor)
  942. {
  943.     #if AA_YCOCG
  944.         return float4(YCoCgToRGB(SceneColor.xyz), SceneColor.a);
  945.     #else
  946.         return SceneColor;
  947.     #endif
  948. }
  949.  
  950. // Transform current frame's RAW scene color RGB to TAA's working color space.
  951. float4 TransformCurrentFrameSceneColor(float4 RawSceneColorRGBA)
  952. {
  953.     return TransformSceneColor(RawSceneColorRGBA);
  954. }
  955.  
  956. // Get the Luma4 of the sceneColor
  957. float GetSceneColorLuma4(float4 SceneColor)
  958. {
  959.     #if AA_YCOCG
  960.         return SceneColor.x;
  961.     #else
  962.         return Luma4(SceneColor.rgb);
  963.     #endif
  964. }
  965.  
  966. // Get the HDR weight of the transform scene color.
  967. float GetSceneColorHdrWeight(
  968.     in FTAAInputParameters InputParams,
  969.     float4 SceneColor)
  970. {
  971.     #if AA_YCOCG
  972.         return HdrWeightY(SceneColor.x, InputParams.FrameExposureScale);
  973.     #else
  974.         return HdrWeight4(SceneColor.rgb, InputParams.FrameExposureScale);
  975.     #endif
  976. }
  977.  
  978.  
  979. //------------------------------------------------------- INPUT SAMPLE CACHING.
  980. // API to sample input scene color and depth through caching system.
  981. //
  982. // Precache scene color or depth:
  983. //      PrecacheInputSceneColor(InputParams);
  984. //      PrecacheInputSceneDepth(InputParams);
  985. //
  986. // Then sample scene color or depth:
  987. //      SampleCachedSceneColorTexture(InputParams, /* Offset = */ int2(-1, -1));
  988. //      SampleCachedSceneDepthTexture(InputParams, /* Offset = */ int2(-1, -1));
  989. //
  990. //      <Offset> parameter is meant to be compile time constant of the pixel offset from nearest input sample.
  991.  
  992. #if AA_SAMPLE_CACHE_METHOD == AA_SAMPLE_CACHE_METHOD_VGPR_3X3
  993. //------------------------------------------------------- 3x3 NEIGHTBORS CACHING
  994.  
  995.     #define AA_PRECACHE_SCENE_COLOR 1
  996.    
  997.     void PrecacheInputSceneColor(inout FTAAInputParameters InputParams)
  998.     {
  999.         // Precache 3x3 input scene color into FTAAInputParameters::CachedNeighbors.
  1000.         UNROLL
  1001.         for (uint i = 0; i < kNeighborsCount; i++)
  1002.         {
  1003.             int2 Coord = int2(InputParams.NearestBufferUV * InputSceneColorSize.xy) + kOffsets3x3[i];
  1004.             Coord = clamp(Coord, InputMinPixelCoord, InputMaxPixelCoord);
  1005.  
  1006.             InputParams.CachedNeighbors0[i] = TransformCurrentFrameSceneColor(InputSceneColor[Coord]);
  1007.    
  1008.             #if HISTORY_RENDER_TARGETS == 2
  1009.                 InputParams.CachedNeighbors1[i] = InputSceneMetadata[Coord].r;
  1010.             #endif
  1011.         }
  1012.     }
  1013.  
  1014.     FTAASceneColorSample SampleCachedSceneColorTexture(
  1015.         inout FTAAInputParameters InputParams,
  1016.         int2 PixelOffset)
  1017.     {
  1018.         // PixelOffset is const at compile time. Therefore all this computaton is actually free.
  1019.         uint NeighborsId = uint(4 + PixelOffset.x + PixelOffset.y * 3);
  1020.         FTAASceneColorSample Sample;
  1021.  
  1022.         Sample.Color = InputParams.CachedNeighbors0[NeighborsId];
  1023.         Sample.CocRadius = 0;
  1024.  
  1025.         #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC
  1026.             Sample.CocRadius = Sample.Color.a;
  1027.         #elif AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  1028.             Sample.CocRadius = InputParams.CachedNeighbors1[NeighborsId];
  1029.         #endif
  1030.  
  1031.         Sample.HdrWeight = GetSceneColorHdrWeight(InputParams, Sample.Color);
  1032.         return Sample;
  1033.     }
  1034.  
  1035.  
  1036. #elif AA_SAMPLE_CACHE_METHOD == AA_SAMPLE_CACHE_METHOD_LDS
  1037. //-------------------------------- Generic LDS functions.
  1038.  
  1039. // Returns SV_GroupIndex.
  1040. uint GetGroupIndex(in FTAAInputParameters InputParams)
  1041. {
  1042.     if (0)
  1043.     {
  1044.         // Group thread index doesn't actually suppress ALU instruction on consoles.
  1045.         return InputParams.GroupThreadIndex;
  1046.     }
  1047.     return InputParams.GroupThreadId.x + InputParams.GroupThreadId.y * THREADGROUP_SIZEX;
  1048. }
  1049.  
  1050.  
  1051. //------------------------------------------------------- COMPUTE SHADER GROUP SHARE OPTIMIZATION
  1052.  
  1053. #if THREADGROUP_SIZEX != THREADGROUP_SIZEY
  1054.     #error AA_SAMPLE_CACHE_METHOD >= 2 assume square shaped tiles.
  1055. #endif
  1056.  
  1057. #ifndef TAA_SCREEN_PERCENTAGE_RANGE
  1058.     #error LDS cache needs to know the screen percentage range.
  1059. #endif
  1060.  
  1061. #define AA_PRECACHE_IMPLEMENTATIONS 1
  1062.  
  1063. // Total number of thread per group.
  1064. #define THREADGROUP_TOTAL (THREADGROUP_SIZEX * THREADGROUP_SIZEY)
  1065.  
  1066. // Configure base width of the LDS tile according to screen percentage range.
  1067. // If screen percentage is lower than 75%, we can set the base tile width to 6, which will subsequently create a scene color
  1068. // tile width of 8 that can be cache with only one iteration per lane in PrecacheInputSceneColorToLDS(), and reduce size(LDS) = 1k.
  1069. #if TAA_SCREEN_PERCENTAGE_RANGE == 1 // screen percentage < 75%
  1070.     #define LDS_BASE_TILE_WIDTH 6
  1071. #elif TAA_SCREEN_PERCENTAGE_RANGE == 0 // screen percentage in [75%; 100%]
  1072.     #define LDS_BASE_TILE_WIDTH THREADGROUP_SIZEX
  1073. #elif TAA_SCREEN_PERCENTAGE_RANGE == 2 // screen percentage > 100%
  1074.     #error Should not use LDS caching.
  1075. #else
  1076.     #error Unknown screen percentage range.
  1077. #endif
  1078.  
  1079.  
  1080. //-------------------------------- Configuration.
  1081.  
  1082. // Configuration of what should be prefetched.
  1083. // 1: use Load; 2: use gather4.
  1084.  
  1085. #if !AA_UPSAMPLE
  1086. // Disables scene depth caching for TAA upsample because the extra screen percentage ALU is making things worst.
  1087. #define AA_PRECACHE_SCENE_DEPTH 2
  1088. #endif
  1089.  
  1090. // 1: use load.
  1091. #define AA_PRECACHE_SCENE_COLOR 1
  1092.  
  1093. // Precache GetSceneColorHdrWeight() into scene color's alpha channel.
  1094. #define AA_PRECACHE_SCENE_HDR_WEIGHT (AA_TONE && HISTORY_PAYLOAD_COMPONENTS == 3)
  1095.  
  1096. // Layout of scene color in LDS.
  1097. // 0: AoS
  1098. // 1: SoA
  1099. // 2: AoSoA (SoA scene color, AoS GetSceneColorHdrWeight())
  1100. #define LDS_COLOR_LAYOUT 0
  1101.  
  1102.  
  1103. //-------------------------------- Depth tile constants.
  1104.  
  1105. // Number of texels arround the group tile for depth.
  1106. #define LDS_DEPTH_TILE_BORDER_SIZE (AA_CROSS)
  1107.  
  1108. // Width in texels of the depth tile cached into LDS.
  1109. #define LDS_DEPTH_TILE_WIDTH (LDS_BASE_TILE_WIDTH + 2 * LDS_DEPTH_TILE_BORDER_SIZE)
  1110.  
  1111. // Total number of texels cached in the depth tile.
  1112. #define LDS_DEPTH_ARRAY_SIZE (LDS_DEPTH_TILE_WIDTH * LDS_DEPTH_TILE_WIDTH)
  1113.  
  1114.  
  1115. //-------------------------------- Scene color tile constants.
  1116.  
  1117. // TODO: shader permutation for screen percentage <= 75% with AA_UPSAMPLE to fit in 1k LDS.
  1118.  
  1119. // Number of scene color component that gets cached.
  1120. #if HISTORY_PAYLOAD_COMPONENTS == 4 || AA_PRECACHE_SCENE_HDR_WEIGHT
  1121. #define LDS_COLOR_COMPONENT_COUNT 4
  1122. #else
  1123. #define LDS_COLOR_COMPONENT_COUNT 3
  1124. #endif
  1125.  
  1126. // Number of texels arround the group tile for scene color.
  1127. #define LDS_COLOR_TILE_BORDER_SIZE (1)
  1128.  
  1129. // Width in texels of the depth tile cached into LDS.
  1130. #define LDS_COLOR_TILE_WIDTH (LDS_BASE_TILE_WIDTH + 2 * LDS_COLOR_TILE_BORDER_SIZE)
  1131.  
  1132. // Total number of texels cached in the scene color tile.
  1133. #define LDS_COLOR_ARRAY_SIZE (LDS_COLOR_TILE_WIDTH * LDS_COLOR_TILE_WIDTH)
  1134.  
  1135.  
  1136. //-------------------------------- Group shared global.
  1137.  
  1138. // Size of the LDS to be allocated.
  1139. #define LDS_ARRAY_SIZE (LDS_COLOR_ARRAY_SIZE * LDS_COLOR_COMPONENT_COUNT)
  1140. #if LDS_ARRAY_SIZE < LDS_DEPTH_ARRAY_SIZE
  1141.     #error LDS_ARRAY_SIZE assumed scene color caching is bigger than scene depth caching.
  1142. #endif
  1143.  
  1144. // Some compilers may have issues optimising LDS store instructions, therefore we give the compiler a hint by using a float4 LDS.
  1145. #if defined(AA_PRECACHE_SCENE_DEPTH)
  1146. #define LDS_USE_FLOAT4_ARRAY 0
  1147. #else
  1148. #define LDS_USE_FLOAT4_ARRAY (LDS_COLOR_COMPONENT_COUNT == 4 && LDS_COLOR_LAYOUT == 0)
  1149. #endif
  1150.  
  1151. #if LDS_USE_FLOAT4_ARRAY
  1152. groupshared float4 GroupSharedArrayF4[LDS_ARRAY_SIZE/4];
  1153. #else
  1154. groupshared float GroupSharedArray[LDS_ARRAY_SIZE];
  1155. #endif
  1156.  
  1157.  
  1158. //-------------------------------- Generic LDS tile functions.
  1159.  
  1160. #if AA_UPSAMPLE
  1161.  
  1162. // Get the pixel coordinate of the nearest input pixel K for group's thread 0.
  1163. float2 GetGroupThread0InputPixelCoord(in FTAAInputParameters InputParams)
  1164. {
  1165.     // Output pixel center position of the group thread index 0, relative to top left corner of the viewport.
  1166.     float2 Thread0SvPosition = InputParams.GroupId * uint2(THREADGROUP_SIZEX, THREADGROUP_SIZEY) + 0.5;
  1167.        
  1168.     // Output pixel's viewport UV group thread index 0.
  1169.     float2 Thread0ViewportUV = Thread0SvPosition * OutputViewportSize.zw;
  1170.  
  1171.     // Pixel coordinate of the center of output pixel O in the input viewport.
  1172.     float2 Thread0PPCo = Thread0ViewportUV * InputViewSize.xy + TemporalJitterPixels;
  1173.    
  1174.     // Pixel coordinate of the center of the nearest input pixel K.
  1175.     float2 Thread0PPCk = floor(Thread0PPCo) + 0.5;
  1176.        
  1177.     return InputViewMin.xy + Thread0PPCk;
  1178. }
  1179.  
  1180. #endif
  1181.  
  1182. // Get the texel offset of a LDS tile's top left corner.
  1183. uint2 GetGroupTileTexelOffset(in FTAAInputParameters InputParams, uint TileBorderSize)
  1184. {
  1185.     #if AA_UPSAMPLE
  1186.     {
  1187.         // Pixel coordinate of the center of the nearest input pixel K.
  1188.         float2 Thread0PPCk = GetGroupThread0InputPixelCoord(InputParams);
  1189.        
  1190.         return uint2(floor(Thread0PPCk) - TileBorderSize);
  1191.     }
  1192.     #else // !AA_UPSAMPLE
  1193.     {
  1194.         return OutputViewportRect.xy + InputParams.GroupId * uint2(THREADGROUP_SIZEX, THREADGROUP_SIZEY) - TileBorderSize;
  1195.     }
  1196.     #endif
  1197. }
  1198.  
  1199. // Get the index within the LDS array.
  1200. uint GetTileArrayIndexFromPixelOffset(in FTAAInputParameters InputParams, int2 PixelOffset, uint TileBorderSize)
  1201. {
  1202.     #if AA_UPSAMPLE
  1203.     {
  1204.         const float2 RowMultiplier = float2(1, TileBorderSize * 2 + LDS_BASE_TILE_WIDTH);
  1205.  
  1206.         float2 Thread0PPCk = GetGroupThread0InputPixelCoord(InputParams);
  1207.         float2 PPCk = InputParams.NearestBufferUV * InputSceneColorSize.xy;
  1208.  
  1209.         float2 TilePos = floor(PPCk) - floor(Thread0PPCk);
  1210.         return uint(dot(TilePos, RowMultiplier) + dot(float2(PixelOffset) + float(TileBorderSize), RowMultiplier));
  1211.     }
  1212.     #else
  1213.     {
  1214.         uint2 TilePos = InputParams.GroupThreadId + uint2(PixelOffset + TileBorderSize);
  1215.         return TilePos.x + TilePos.y * (TileBorderSize * 2 + LDS_BASE_TILE_WIDTH);
  1216.     }
  1217.     #endif
  1218. }
  1219.  
  1220.  
  1221. //-------------------------------- Share depth texture fetches.
  1222. #if defined(AA_PRECACHE_SCENE_DEPTH)
  1223.  
  1224. // Precache input scene depth into LDS.
  1225. void PrecacheInputSceneDepthToLDS(in FTAAInputParameters InputParams)
  1226. {
  1227.     uint2 GroupTexelOffset = GetGroupTileTexelOffset(InputParams, LDS_DEPTH_TILE_BORDER_SIZE);
  1228.    
  1229.     #if AA_PRECACHE_SCENE_DEPTH == 1
  1230.     // Prefetch depth buffer using Load.
  1231.     {
  1232.         const uint LoadCount = (LDS_DEPTH_ARRAY_SIZE + THREADGROUP_TOTAL - 1) / THREADGROUP_TOTAL;
  1233.        
  1234.         uint LinearGroupThreadId = GetGroupIndex(InputParams);
  1235.  
  1236.         UNROLL
  1237.         for (uint i = 0; i < LoadCount; i++)
  1238.         {
  1239.             uint2 TexelLocation =  GroupTexelOffset + uint2(
  1240.                 LinearGroupThreadId % LDS_DEPTH_TILE_WIDTH,
  1241.                 LinearGroupThreadId / LDS_DEPTH_TILE_WIDTH);
  1242.  
  1243.             if ((LinearGroupThreadId < LDS_DEPTH_ARRAY_SIZE) ||
  1244.                 (i != LoadCount - 1) ||
  1245.                 (LDS_DEPTH_ARRAY_SIZE % THREADGROUP_TOTAL) == 0)
  1246.             {
  1247.                 GroupSharedArray[LinearGroupThreadId] = SceneDepthTexture.Load(uint3(TexelLocation, 0)).x;
  1248.             }
  1249.             LinearGroupThreadId += THREADGROUP_TOTAL;
  1250.         }
  1251.  
  1252.     }
  1253.  
  1254.     #elif AA_PRECACHE_SCENE_DEPTH == 2
  1255.     // Prefetch depth buffer using Gather.
  1256.     {
  1257.         const uint LoadCount = (LDS_DEPTH_ARRAY_SIZE / 4 + THREADGROUP_TOTAL - 1) / THREADGROUP_TOTAL;
  1258.        
  1259.         uint LinearGroupThreadId = GetGroupIndex(InputParams);
  1260.    
  1261.         UNROLL
  1262.         for (uint i = 0; i < LoadCount; i++)
  1263.         {
  1264.             uint2 TileDest = uint2(
  1265.                 (2 * LinearGroupThreadId) % LDS_DEPTH_TILE_WIDTH,
  1266.                 2 * ((2 * LinearGroupThreadId) / LDS_DEPTH_TILE_WIDTH));
  1267.  
  1268.             uint2 TexelLocation =  GroupTexelOffset + TileDest;
  1269.  
  1270.             uint DestI = TileDest.x + TileDest.y * LDS_DEPTH_TILE_WIDTH;
  1271.  
  1272.             if ((DestI < LDS_DEPTH_ARRAY_SIZE) ||
  1273.                 (i != LoadCount - 1) ||
  1274.                 ((LDS_DEPTH_ARRAY_SIZE / 4) % THREADGROUP_TOTAL) == 0)
  1275.             {
  1276.                 float2 UV = float2(TexelLocation + 0.5) * InputSceneColorSize.zw;
  1277.                 float4 Depth = SceneDepthTexture.Gather(SceneDepthTextureSampler, UV);
  1278.                 GroupSharedArray[DestI + 1 * LDS_DEPTH_TILE_WIDTH + 0] = Depth.x;
  1279.                 GroupSharedArray[DestI + 1 * LDS_DEPTH_TILE_WIDTH + 1] = Depth.y;
  1280.                 GroupSharedArray[DestI + 0 * LDS_DEPTH_TILE_WIDTH + 1] = Depth.z;
  1281.                 GroupSharedArray[DestI + 0 * LDS_DEPTH_TILE_WIDTH + 0] = Depth.w;
  1282.             }
  1283.             LinearGroupThreadId += THREADGROUP_TOTAL;
  1284.         }
  1285.     }
  1286.  
  1287.     #else
  1288.         #error Wrong AA_PRECACHE_SCENE_DEPTH
  1289.  
  1290.     #endif
  1291. }
  1292.  
  1293. float SampleCachedSceneDepthTexture(in FTAAInputParameters InputParams, int2 PixelOffset)
  1294. {
  1295.     return GroupSharedArray[GetTileArrayIndexFromPixelOffset(InputParams, PixelOffset, LDS_DEPTH_TILE_BORDER_SIZE)];
  1296. }
  1297.  
  1298.  
  1299. #endif // define(AA_PRECACHE_SCENE_DEPTH)
  1300.  
  1301.  
  1302. //-------------------------------- Share color texture fetches.
  1303.  
  1304. #if defined(AA_PRECACHE_SCENE_COLOR)
  1305.  
  1306. // Return the index GroupSharedArray from a given ArrayIndex and ComponentId.
  1307. uint GetSceneColorLDSIndex(uint ArrayIndex, uint ComponentId)
  1308. {
  1309.     #if LDS_COLOR_LAYOUT == 0 // AoS
  1310.         return ArrayIndex * LDS_COLOR_COMPONENT_COUNT + ComponentId;
  1311.     #elif LDS_COLOR_LAYOUT == 1 // SoA
  1312.         return ArrayIndex + ComponentId * LDS_COLOR_ARRAY_SIZE;
  1313.     #else
  1314.         #error Unknown color layout.
  1315.     #endif
  1316. }
  1317.  
  1318. // Precache input scene color into LDS.
  1319. void PrecacheInputSceneColorToLDS(in FTAAInputParameters InputParams)
  1320. {
  1321.     const uint LoadCount = (LDS_COLOR_ARRAY_SIZE + THREADGROUP_TOTAL - 1) / THREADGROUP_TOTAL;
  1322.    
  1323.     #define LDS_FLOAT_UV AA_UPSAMPLE
  1324.  
  1325.     #if LDS_FLOAT_UV
  1326.         float LinearGroupThreadId = float(GetGroupIndex(InputParams));
  1327.         float2 Thread0PPCk = GetGroupThread0InputPixelCoord(InputParams);
  1328.         float2 GroupTexelOffset = Thread0PPCk - LDS_COLOR_TILE_BORDER_SIZE;
  1329.     #else
  1330.         uint LinearGroupThreadId = GetGroupIndex(InputParams);
  1331.         uint2 GroupTexelOffset = GetGroupTileTexelOffset(InputParams, LDS_COLOR_TILE_BORDER_SIZE);
  1332.     #endif
  1333.    
  1334.     UNROLL
  1335.     for (uint i = 0; i < LoadCount; i++)
  1336.     {
  1337.         #if LDS_FLOAT_UV
  1338.             float Y = floor(LinearGroupThreadId * (1.0 / LDS_COLOR_TILE_WIDTH));
  1339.             float X = LinearGroupThreadId - LDS_COLOR_TILE_WIDTH * Y;
  1340.  
  1341.             float2 TexelLocation = GroupTexelOffset + float2(X, Y);
  1342.         #else
  1343.             uint2 TexelLocation =  GroupTexelOffset + uint2(
  1344.                 LinearGroupThreadId % LDS_COLOR_TILE_WIDTH,
  1345.                 LinearGroupThreadId / LDS_COLOR_TILE_WIDTH);
  1346.         #endif
  1347.  
  1348.         if ((LinearGroupThreadId < LDS_COLOR_ARRAY_SIZE) ||
  1349.             (i != LoadCount - 1) ||
  1350.             (LDS_COLOR_ARRAY_SIZE % THREADGROUP_TOTAL) == 0)
  1351.         {
  1352.             #if LDS_FLOAT_UV
  1353.                 int2 Coord = TexelLocation;
  1354.                 Coord = clamp(Coord, InputMinPixelCoord, InputMaxPixelCoord);
  1355.  
  1356.                 float4 RawColor = InputSceneColor[Coord];
  1357.             #else
  1358.                 int2 Coord = int2(TexelLocation);
  1359.                 Coord = clamp(Coord, InputMinPixelCoord, InputMaxPixelCoord);
  1360.                 float4 RawColor = InputSceneColor.Load(uint3(Coord, 0));
  1361.             #endif
  1362.  
  1363.             float4 Color = TransformCurrentFrameSceneColor(RawColor);
  1364.  
  1365.             // Precache scene color's HDR weight into alpha channel to reduce rcp() instructions in innerloops.
  1366.             #if AA_PRECACHE_SCENE_HDR_WEIGHT
  1367.                 Color.a = GetSceneColorHdrWeight(InputParams, Color);
  1368.             #endif
  1369.  
  1370.             #if LDS_USE_FLOAT4_ARRAY
  1371.                 GroupSharedArrayF4[uint(LinearGroupThreadId)] = Color;
  1372.  
  1373.             #else
  1374.                 GroupSharedArray[GetSceneColorLDSIndex(uint(LinearGroupThreadId), 0)] = Color.r;
  1375.                 GroupSharedArray[GetSceneColorLDSIndex(uint(LinearGroupThreadId), 1)] = Color.g;
  1376.                 GroupSharedArray[GetSceneColorLDSIndex(uint(LinearGroupThreadId), 2)] = Color.b;
  1377.                 #if LDS_COLOR_COMPONENT_COUNT == 4
  1378.                     GroupSharedArray[GetSceneColorLDSIndex(uint(LinearGroupThreadId), 3)] = Color.a;
  1379.                 #endif
  1380.            
  1381.             #endif
  1382.         }
  1383.         LinearGroupThreadId += THREADGROUP_TOTAL;
  1384.     }
  1385. }
  1386.  
  1387. FTAASceneColorSample SampleCachedSceneColorTexture(
  1388.     in FTAAInputParameters InputParams,
  1389.     int2 PixelOffset)
  1390. {
  1391.     uint ArrayPos = GetTileArrayIndexFromPixelOffset(InputParams, PixelOffset, LDS_COLOR_TILE_BORDER_SIZE);
  1392.    
  1393.     FTAASceneColorSample Sample;
  1394.     Sample.CocRadius = 0;
  1395.    
  1396.     #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  1397.         #error Unsupported history payload with LDS scene color caching.Test
  1398.     #endif
  1399.  
  1400.     #if LDS_USE_FLOAT4_ARRAY
  1401.         Sample.Color = GroupSharedArrayF4[ArrayPos];
  1402.  
  1403.         #if AA_PRECACHE_SCENE_HDR_WEIGHT
  1404.             Sample.HdrWeight = Sample.Color.a;
  1405.             Sample.Color.a = 0;
  1406.         #elif HISTORY_PAYLOAD_COMPONENTS != 4
  1407.             #error LDS_USE_FLOAT4_ARRAY assumes 4 components.
  1408.         #endif
  1409.  
  1410.     #else
  1411.         Sample.Color.r = GroupSharedArray[GetSceneColorLDSIndex(ArrayPos, 0)];
  1412.         Sample.Color.g = GroupSharedArray[GetSceneColorLDSIndex(ArrayPos, 1)];
  1413.         Sample.Color.b = GroupSharedArray[GetSceneColorLDSIndex(ArrayPos, 2)];
  1414.         Sample.Color.a = 0;
  1415.    
  1416.         #if HISTORY_PAYLOAD_COMPONENTS == 4
  1417.             Sample.Color.a = GroupSharedArray[GetSceneColorLDSIndex(ArrayPos, 3)];
  1418.         #elif AA_PRECACHE_SCENE_HDR_WEIGHT
  1419.             Sample.HdrWeight = GroupSharedArray[GetSceneColorLDSIndex(ArrayPos, 3)];
  1420.         #endif
  1421.  
  1422.     #endif
  1423.    
  1424.     // if scene color weight was not precached in LDS, compute it.
  1425.     #if !AA_PRECACHE_SCENE_HDR_WEIGHT
  1426.         Sample.HdrWeight = GetSceneColorHdrWeight(InputParams, Sample.Color);
  1427.     #endif
  1428.    
  1429.     // Color has already been transformed in PrecacheInputSceneColor.
  1430.     return Sample;
  1431. }
  1432.  
  1433.  
  1434. #endif // defined(AA_PRECACHE_SCENE_COLOR)
  1435.  
  1436.  
  1437. void PrecacheInputSceneDepth(in FTAAInputParameters InputParams)
  1438. {
  1439.     #if defined(AA_PRECACHE_SCENE_DEPTH)
  1440.         PrecacheInputSceneDepthToLDS(InputParams);
  1441.         GroupMemoryBarrierWithGroupSync();
  1442.     #endif
  1443. }
  1444.  
  1445. void PrecacheInputSceneColor(in FTAAInputParameters InputParams)
  1446. {
  1447.     #if defined(AA_PRECACHE_SCENE_DEPTH) && defined(AA_PRECACHE_SCENE_COLOR)
  1448.         GroupMemoryBarrierWithGroupSync();
  1449.     #endif
  1450.     #if defined(AA_PRECACHE_SCENE_COLOR)
  1451.         PrecacheInputSceneColorToLDS(InputParams);
  1452.         GroupMemoryBarrierWithGroupSync();
  1453.     #endif
  1454. }
  1455.  
  1456.  
  1457. #endif // AA_SAMPLE_CACHE_METHOD == AA_SAMPLE_CACHE_METHOD_LDS
  1458.  
  1459. //------------------------------------------------------- FALLBACK TO NO CACHING IMPLEMENTATIONS.
  1460.  
  1461. #if !defined(AA_PRECACHE_SCENE_DEPTH)
  1462.  
  1463. #if !defined(AA_PRECACHE_IMPLEMENTATIONS)
  1464.  
  1465. // Silently do no scene depth precaching.
  1466. void PrecacheInputSceneDepth(in FTAAInputParameters InputParams)
  1467. { }
  1468.  
  1469. #endif
  1470.  
  1471.  
  1472. // Sample scene color.
  1473. float SampleCachedSceneDepthTexture(in FTAAInputParameters InputParams, int2 PixelOffset)
  1474. {
  1475.     return SceneDepthTexture.SampleLevel(SceneDepthTextureSampler, InputParams.NearestBufferUV, 0, PixelOffset).r;
  1476. }
  1477.  
  1478.  
  1479. #endif
  1480.  
  1481. #if !defined(AA_PRECACHE_SCENE_COLOR)
  1482.  
  1483. #if !defined(AA_PRECACHE_IMPLEMENTATIONS)
  1484.  
  1485. // Silently do no scene color precaching.
  1486. void PrecacheInputSceneColor(in FTAAInputParameters InputParams)
  1487. { }
  1488.  
  1489. #endif
  1490.  
  1491.  
  1492. // Sample and transform scene color.
  1493. FTAASceneColorSample SampleCachedSceneColorTexture(
  1494.     in FTAAInputParameters InputParams,
  1495.     int2 PixelOffset)
  1496. {
  1497.     FTAASceneColorSample Sample;
  1498.  
  1499.  
  1500.     int2 Coord = int2(InputParams.NearestBufferUV * InputSceneColorSize.xy) + PixelOffset;
  1501.     Coord = clamp(Coord, InputMinPixelCoord, InputMaxPixelCoord);
  1502.  
  1503.     Sample.Color = TransformCurrentFrameSceneColor(InputSceneColor[Coord]);
  1504.  
  1505.     Sample.CocRadius = 0;
  1506.  
  1507.     #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC
  1508.         Sample.CocRadius = Sample.Color.a;
  1509.     #elif AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  1510.         Sample.CocRadius = InputSceneMetadata[Coord].r;
  1511.     #endif
  1512.  
  1513.     Sample.HdrWeight = GetSceneColorHdrWeight(InputParams, Sample.Color);
  1514.     return Sample;
  1515. }
  1516.  
  1517.  
  1518. #endif
  1519.  
  1520.  
  1521. //------------------------------------------------------- TEMPORAL UPSAMPLING
  1522.  
  1523. #if AA_UPSAMPLE
  1524.  
  1525. // Returns the weight of a pixels at a coordinate <PixelDelta> from the PDF highest point.
  1526. float ComputeSampleWeigth(in FTAAIntermediaryResult IntermediaryResult, float2 PixelDelta)
  1527. {
  1528.     float u2 = UpscaleFactor * UpscaleFactor;
  1529.  
  1530.     // The point of InvFilterScaleFactor is to blur current frame scene color when upscaling.
  1531.     // Therefore there is no need to do it when downscaling.
  1532.     if (!AA_DOWNSAMPLE)
  1533.     {
  1534.         u2 *= (IntermediaryResult.InvFilterScaleFactor * IntermediaryResult.InvFilterScaleFactor);
  1535.     }
  1536.  
  1537.     #if 1
  1538.         // 1 - 1.9 * x^2 + 0.9 * x^4
  1539.         float x2 = saturate(u2 * dot(PixelDelta, PixelDelta));
  1540.         return (0.905 * x2 - 1.9) * x2 + 1;
  1541.  
  1542.     #else
  1543.         // original e ^ (- x^2 / (2 * s^2))
  1544.         const float Sigma = 0.47;
  1545.         const float ExponentInputFactor = (-0.5 / (Sigma * Sigma));
  1546.        
  1547.         float x2 = dot(PixelDelta, PixelDelta) * u2;
  1548.         return exp(ExponentInputFactor * x2);
  1549.  
  1550.     #endif
  1551. }
  1552.  
  1553.  
  1554. // Returns the weight of a pixels at a coordinate <PixelDelta> from the PDF highest point.
  1555. float ComputePixelWeigth(in FTAAIntermediaryResult IntermediaryResult, float2 PixelDelta)
  1556. {
  1557.     float u2 = UpscaleFactor * UpscaleFactor;
  1558.  
  1559.     // The point of InvFilterScaleFactor is to blur current frame scene color when upscaling.
  1560.     // Therefore there is no need to do it when downscaling.
  1561.     if (!AA_DOWNSAMPLE)
  1562.     {
  1563.         u2 *= (IntermediaryResult.InvFilterScaleFactor * IntermediaryResult.InvFilterScaleFactor);
  1564.     }
  1565.  
  1566.     #if 1
  1567.         // 1 - 1.9 * x^2 + 0.9 * x^4
  1568.         float x2 = saturate(u2 * dot(PixelDelta, PixelDelta));
  1569.         float r = (0.905 * x2 - 1.9) * x2 + 1;
  1570.  
  1571.     #else
  1572.         // original e ^ (- x^2 / (2 * s^2))
  1573.         const float Sigma = 0.47;
  1574.         const float ExponentInputFactor = (-0.5 / (Sigma * Sigma));
  1575.        
  1576.         float x2 = dot(PixelDelta, PixelDelta);
  1577.         float r = exp(ExponentInputFactor * x2);
  1578.  
  1579.     #endif
  1580.  
  1581.     if (!AA_DOWNSAMPLE)
  1582.     {
  1583.         // Multiply pixel weight ^ 2 by upscale factor because have only a probability = screen percentage ^ 2 to return 1.
  1584.         // Only do it for upsampling to not converge slower than if doing screen percentage 100%.
  1585.         return u2 * r;
  1586.     }
  1587.     return r;
  1588. }
  1589.  
  1590. #endif // AA_UPSAMPLE
  1591.  
  1592.  
  1593. //------------------------------------------------------- TAA MAJOR FUNCTIONS
  1594.  
  1595. // Filter input pixels.
  1596. void FilterCurrentFrameInputSamples(
  1597.     in FTAAInputParameters InputParams,
  1598.     inout FTAAIntermediaryResult IntermediaryResult)
  1599. {
  1600.     #if !AA_FILTERED
  1601.     {
  1602.         IntermediaryResult.Filtered.Color = SampleCachedSceneColorTexture(InputParams, int2(0, 0)).Color;
  1603.         IntermediaryResult.Filtered.CocRadius = SampleCachedSceneColorTexture(InputParams, int2(0, 0)).CocRadius;
  1604.         return;
  1605.     }
  1606.     #endif
  1607.  
  1608.     FTAAHistoryPayload Filtered;
  1609.  
  1610.     {
  1611.         #if AA_UPSAMPLE
  1612.             // Pixel coordinate of the center of output pixel O in the input viewport.
  1613.             float2 PPCo = InputParams.ViewportUV * InputViewSize.xy + TemporalJitterPixels;
  1614.  
  1615.             // Pixel coordinate of the center of the nearest input pixel K.
  1616.             float2 PPCk = floor(PPCo) + 0.5;
  1617.        
  1618.             // Vector in pixel between pixel K -> O.
  1619.             float2 dKO = PPCo - PPCk;
  1620.         #endif
  1621.        
  1622.         #if AA_SAMPLES == 9
  1623.             const uint SampleIndexes[9] = kSquareIndexes3x3;
  1624.         #elif AA_SAMPLES == 5 || AA_SAMPLES == 6
  1625.             const uint SampleIndexes[5] = kPlusIndexes3x3;
  1626.         #endif
  1627.  
  1628.         #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  1629.             // Fetches center pixel's Coc for the bilateral filtering.
  1630.             float CenterCocRadius = SampleCachedSceneColorTexture(InputParams, int2(0, 0)).CocRadius;
  1631.         #endif
  1632.  
  1633.         float NeighborsHdrWeight = 0;
  1634.         float NeighborsFinalWeight = 0;
  1635.         float4 NeighborsColor = 0;
  1636.  
  1637.         UNROLL
  1638.         for (uint i = 0; i < AA_SAMPLES; i++)
  1639.         {
  1640.             // Get the sample offset from the nearest input pixel.
  1641.             int2 SampleOffset;
  1642.            
  1643.             #if AA_UPSAMPLE && AA_SAMPLES == 6
  1644.                 if (i == 5)
  1645.                 {
  1646.                     SampleOffset = SignFastInt(dKO);
  1647.                 }
  1648.                 else
  1649.             #endif
  1650.             {
  1651.                 const uint SampleIndex = SampleIndexes[i];
  1652.                 SampleOffset = kOffsets3x3[SampleIndex];
  1653.             }
  1654.             float2 fSampleOffset = float2(SampleOffset);
  1655.            
  1656.             // When doing Coc bilateral, the center sample is accumulated last.
  1657.             #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC && 0
  1658.                 if (all(SampleOffset == 0) && (AA_SAMPLES != 6 || i != 5))
  1659.                 {
  1660.                     continue;
  1661.                 }
  1662.             #endif
  1663.  
  1664.             // Finds out the spatial weight of this input sample.
  1665.             #if AA_UPSAMPLE
  1666.                 // Compute the pixel delta between output pixels and input pixel I.
  1667.                 //  Note: abs() is unecessary because of the dot(dPP, dPP) latter on.
  1668.                 float2 dPP = fSampleOffset - dKO;
  1669.  
  1670.                 float SampleSpatialWeight = ComputeSampleWeigth(IntermediaryResult, dPP);
  1671.  
  1672.             #elif AA_SAMPLES == 9
  1673.                 float SampleSpatialWeight = GET_SCALAR_ARRAY_ELEMENT(SampleWeights, i);
  1674.  
  1675.             #elif AA_SAMPLES == 5
  1676.                 float SampleSpatialWeight = GET_SCALAR_ARRAY_ELEMENT(PlusWeights, i);
  1677.  
  1678.             #else
  1679.                 #error Do not know how to compute filtering sample weight.
  1680.  
  1681.             #endif
  1682.  
  1683.             // Fetch sample.
  1684.             FTAASceneColorSample Sample = SampleCachedSceneColorTexture(InputParams, SampleOffset);
  1685.                
  1686.             // Finds out the sample's HDR weight.
  1687.             #if AA_TONE
  1688.                 float SampleHdrWeight = Sample.HdrWeight;
  1689.             #else
  1690.                 float SampleHdrWeight = 1;
  1691.             #endif
  1692.  
  1693.             // Finds out the sample's bilateral weight according to the payload.
  1694.             #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC
  1695.                 float BilateralWeight = ComputeNeightborSampleBilateralWeight(CenterCocRadius, Sample.CocRadius);
  1696.  
  1697.             #else
  1698.                 float BilateralWeight = 1;
  1699.  
  1700.             #endif
  1701.  
  1702.             float SampleFinalWeight = SampleSpatialWeight * SampleHdrWeight * BilateralWeight;
  1703.  
  1704.             // Apply pixel.
  1705.             NeighborsColor       += SampleFinalWeight * Sample.Color;
  1706.             NeighborsFinalWeight += SampleFinalWeight;
  1707.  
  1708.             NeighborsHdrWeight   += SampleSpatialWeight * SampleHdrWeight;
  1709.         }
  1710.        
  1711.         #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  1712.         {
  1713.             FTAASceneColorSample Sample = SampleCachedSceneColorTexture(InputParams, 0);
  1714.            
  1715.             #if AA_UPSAMPLE
  1716.                 float SampleWeight = ComputeSampleWeigth(IntermediaryResult, -dKO);
  1717.  
  1718.             #elif AA_SAMPLES == 9
  1719.                 float SampleWeight = GET_SCALAR_ARRAY_ELEMENT(SampleWeights, 4);
  1720.  
  1721.             #else
  1722.                 float SampleWeight = GET_SCALAR_ARRAY_ELEMENT(PlusWeights, 2);
  1723.  
  1724.             #endif
  1725.            
  1726.             if (AA_TONE)
  1727.             {
  1728.                 SampleWeight *= Sample.HdrWeight;
  1729.             }
  1730.  
  1731.             // TODO: it feels wrong...
  1732.             //Filtered = NeighborsColor * (NeighborsHdrWeight * rcp(NeighborsFinalWeight)) + Sample.Color * SampleWeight;
  1733.             Filtered.Color = NeighborsColor * rcp(NeighborsFinalWeight);
  1734.             Filtered.CocRadius = CenterCocRadius;
  1735.         }
  1736.         #elif AA_TONE || AA_UPSAMPLE
  1737.         {
  1738.             // Reweight because SampleFinalWeight does not that have total sum = 1.
  1739.             Filtered.Color = NeighborsColor * rcp(NeighborsFinalWeight);
  1740.             Filtered.CocRadius = 0;
  1741.         }
  1742.         #else
  1743.         {
  1744.             Filtered.Color = NeighborsColor;
  1745.             Filtered.CocRadius = 0;
  1746.         }
  1747.         #endif
  1748.        
  1749.         #if AA_UPSAMPLE
  1750.             // Compute the temporal weight of the output pixel.
  1751.             IntermediaryResult.FilteredTemporalWeight = ComputePixelWeigth(IntermediaryResult, dKO);
  1752.         #endif
  1753.     }
  1754.  
  1755.     IntermediaryResult.Filtered = Filtered;
  1756. }
  1757.  
  1758.  
  1759. #if GHOSTFIX_NEEDS_MOMENTS
  1760.  
  1761. // GHOSTFIX: number of samples that take part in the variance computation (same set as the current frame filter).
  1762. #if AA_SAMPLES == 9
  1763.     #define GHOSTFIX_MOMENT_COUNT 9
  1764. #else
  1765.     #define GHOSTFIX_MOMENT_COUNT 5
  1766. #endif
  1767.  
  1768. // GHOSTFIX: HDR (luma) weighted mean and standard deviation of the neighborhood, in the working color space.
  1769. // Weighting with the same function as the temporal filter makes bright fireflies count less, so one hot pixel
  1770. // does not open the clipping box (which would reintroduce trails) and does not cause flicker.
  1771. void GhostFixComputeMoments(
  1772.     in FTAAInputParameters InputParams,
  1773.     in FTAAHistoryPayload Neighbors[kNeighborsCount],
  1774.     out float3 OutMean,
  1775.     out float3 OutStdDev)
  1776. {
  1777.     #if AA_SAMPLES == 9
  1778.         const uint MomentIndexes[9] = kSquareIndexes3x3;
  1779.     #else
  1780.         const uint MomentIndexes[5] = kPlusIndexes3x3;
  1781.     #endif
  1782.  
  1783.     float WeightSum = 0;
  1784.     float3 M1 = 0;
  1785.     float3 M2 = 0;
  1786.  
  1787.     UNROLL
  1788.     for (uint i = 0; i < GHOSTFIX_MOMENT_COUNT; i++)
  1789.     {
  1790.         float3 C = Neighbors[MomentIndexes[i]].Color.rgb;
  1791.         float W = GetSceneColorHdrWeight(InputParams, float4(C, 0.0));
  1792.  
  1793.         WeightSum += W;
  1794.         M1 += W * C;
  1795.         M2 += W * C * C;
  1796.     }
  1797.  
  1798.     float RcpWeightSum = rcp(WeightSum);
  1799.     M1 *= RcpWeightSum;
  1800.     M2 *= RcpWeightSum;
  1801.  
  1802.     OutMean = M1;
  1803.     OutStdDev = sqrt(max(M2 - M1 * M1, 0.0));
  1804. }
  1805.  
  1806. #endif // GHOSTFIX_NEEDS_MOMENTS
  1807.  
  1808.  
  1809. // Compute the neighborhood bounding box used to reject history.
  1810. void ComputeNeighborhoodBoundingbox(
  1811.     in FTAAInputParameters InputParams,
  1812.     inout FTAAIntermediaryResult IntermediaryResult, // GHOSTFIX: was 'in', now writes the neighborhood moments.
  1813.     out FTAAHistoryPayload OutNeighborMin,
  1814.     out FTAAHistoryPayload OutNeighborMax)
  1815. {
  1816.     // TODO: clean this up.
  1817.     FTAAHistoryPayload Neighbors[kNeighborsCount];
  1818.     UNROLL
  1819.     for (uint i = 0; i < kNeighborsCount; i++)
  1820.     {
  1821.         Neighbors[i].Color = SampleCachedSceneColorTexture(InputParams, kOffsets3x3[i]).Color;
  1822.         Neighbors[i].CocRadius = SampleCachedSceneColorTexture(InputParams, kOffsets3x3[i]).CocRadius;
  1823.     }
  1824.  
  1825.     FTAAHistoryPayload NeighborMin;
  1826.     FTAAHistoryPayload NeighborMax;
  1827.  
  1828.     #if GHOSTFIX_NEEDS_BLUR
  1829.     {
  1830.         // GHOSTFIX: 3x3 gaussian blur of the current frame, HDR weighted so a firefly does not smear over its neighbours.
  1831.         float4 BlurSum = 0;
  1832.         float BlurWeightSum = 0;
  1833.  
  1834.         UNROLL
  1835.         for (uint b = 0; b < kNeighborsCount; b++)
  1836.         {
  1837.             float W = kGhostFixGaussian3x3[b] * GetSceneColorHdrWeight(InputParams, Neighbors[b].Color);
  1838.             BlurSum += W * Neighbors[b].Color;
  1839.             BlurWeightSum += W;
  1840.         }
  1841.         IntermediaryResult.GhostFixBlurred = BlurSum * rcp(BlurWeightSum);
  1842.     }
  1843.     #endif
  1844.  
  1845.     #if GHOSTFIX_NEEDS_MOMENTS
  1846.         // GHOSTFIX: moments used by the variance clamping box.
  1847.         GhostFixComputeMoments(InputParams, Neighbors, IntermediaryResult.NeighborMean, IntermediaryResult.NeighborStdDev);
  1848.  
  1849.     #endif
  1850.  
  1851.     #if AA_HISTORY_CLAMPING_BOX == HISTORY_CLAMPING_BOX_VARIANCE
  1852.     {
  1853.         // GHOSTFIX: the original code of this branch could not compile (assigned a struct to a float4, used an
  1854.         // uninitialised Filtered and rejected AA_SAMPLES == 6). Rewritten on top of GhostFixComputeMoments().
  1855.         const float VarianceGamma = 1.0;
  1856.         float3 VarMin = IntermediaryResult.NeighborMean - VarianceGamma * IntermediaryResult.NeighborStdDev;
  1857.         float3 VarMax = IntermediaryResult.NeighborMean + VarianceGamma * IntermediaryResult.NeighborStdDev;
  1858.  
  1859.         #if AA_UPSAMPLE_ADAPTIVE_FILTERING == 0
  1860.             // Filtered is not available yet with adaptive filtering (it is computed after clamping).
  1861.             VarMin = min(VarMin, IntermediaryResult.Filtered.Color.rgb);
  1862.             VarMax = max(VarMax, IntermediaryResult.Filtered.Color.rgb);
  1863.         #endif
  1864.  
  1865.         NeighborMin = Neighbors[4];
  1866.         NeighborMax = Neighbors[4];
  1867.         NeighborMin.Color.rgb = VarMin;
  1868.         NeighborMax.Color.rgb = VarMax;
  1869.  
  1870.         // Alpha / Coc keep the plain min/max of the plus shape.
  1871.         FTAAHistoryPayload PlusMin = MinPayload3( Neighbors[1], Neighbors[3], Neighbors[4] );
  1872.         PlusMin = MinPayload3( PlusMin, Neighbors[5], Neighbors[7] );
  1873.         FTAAHistoryPayload PlusMax = MaxPayload3( Neighbors[1], Neighbors[3], Neighbors[4] );
  1874.         PlusMax = MaxPayload3( PlusMax, Neighbors[5], Neighbors[7] );
  1875.         NeighborMin.Color.a = PlusMin.Color.a;
  1876.         NeighborMax.Color.a = PlusMax.Color.a;
  1877.         NeighborMin.CocRadius = PlusMin.CocRadius;
  1878.         NeighborMax.CocRadius = PlusMax.CocRadius;
  1879.     }
  1880.     #elif AA_HISTORY_CLAMPING_BOX == HISTORY_CLAMPING_BOX_SAMPLE_DISTANCE
  1881.     // Do color clamping only within a radius.
  1882.     {
  1883.         float2 PPCo = InputParams.ViewportUV * InputViewSize.xy + TemporalJitterPixels;
  1884.         float2 PPCk = floor(PPCo) + 0.5;
  1885.         float2 dKO = PPCo - PPCk;
  1886.        
  1887.         // Sample 4 is is always going to be considered anyway.
  1888.         NeighborMin = Neighbors[4];
  1889.         NeighborMax = Neighbors[4];
  1890.        
  1891.         // Reduce distance threshold as upsacale factor increase to reduce ghosting.
  1892.         float DistthresholdLerp = UpscaleFactor - 1;
  1893.         float DistThreshold = lerp(1.51, 1.3, DistthresholdLerp);
  1894.  
  1895.         #if AA_SAMPLES == 9
  1896.             const uint Indexes[9] = kSquareIndexes3x3;
  1897.         #else
  1898.             const uint Indexes[5] = kPlusIndexes3x3;
  1899.         #endif
  1900.  
  1901.         UNROLL
  1902.         for( uint i = 0; i < AA_SAMPLES; i++ )
  1903.         {
  1904.             uint NeightborId = Indexes[i];
  1905.             if (NeightborId != 4)
  1906.             {
  1907.                 float2 dPP = float2(kOffsets3x3[NeightborId]) - dKO;
  1908.  
  1909.                 FLATTEN
  1910.                 if (dot(dPP, dPP) < (DistThreshold * DistThreshold))
  1911.                 {
  1912.                     NeighborMin = MinPayload(NeighborMin, Neighbors[NeightborId]);
  1913.                     NeighborMax = MaxPayload(NeighborMax, Neighbors[NeightborId]);
  1914.                 }
  1915.             }
  1916.         }
  1917.     }
  1918.     #elif AA_HISTORY_CLAMPING_BOX == HISTORY_CLAMPING_BOX_MIN_MAX
  1919.     {
  1920.         NeighborMin = MinPayload3( Neighbors[1], Neighbors[3], Neighbors[4] );
  1921.         NeighborMin = MinPayload3( NeighborMin,  Neighbors[5], Neighbors[7] );
  1922.  
  1923.         NeighborMax = MaxPayload3( Neighbors[1], Neighbors[3], Neighbors[4] );
  1924.         NeighborMax = MaxPayload3( NeighborMax,  Neighbors[5], Neighbors[7] );
  1925.        
  1926.         #if AA_SAMPLES == 6
  1927.         {
  1928.             float2 PPCo = InputParams.ViewportUV * InputViewSize.xy + TemporalJitterPixels;
  1929.             float2 PPCk = floor(PPCo) + 0.5;
  1930.             float2 dKO = PPCo - PPCk;
  1931.            
  1932.             int2 FifthNeighborOffset = SignFastInt(dKO);
  1933.  
  1934.             FTAAHistoryPayload FifthNeighbor;
  1935.             FifthNeighbor.Color = SampleCachedSceneColorTexture(InputParams, FifthNeighborOffset).Color;
  1936.             FifthNeighbor.CocRadius = SampleCachedSceneColorTexture(InputParams, FifthNeighborOffset).CocRadius;
  1937.            
  1938.             NeighborMin = MinPayload(NeighborMin, FifthNeighbor);
  1939.             NeighborMax = MaxPayload(NeighborMax, FifthNeighbor);
  1940.         }
  1941.         #elif AA_SAMPLES == 9
  1942.         {
  1943.             FTAAHistoryPayload NeighborMinPlus = NeighborMin;
  1944.             FTAAHistoryPayload NeighborMaxPlus = NeighborMax;
  1945.  
  1946.             NeighborMin = MinPayload3( NeighborMin, Neighbors[0], Neighbors[2] );
  1947.             NeighborMin = MinPayload3( NeighborMin, Neighbors[6], Neighbors[8] );
  1948.  
  1949.             NeighborMax = MaxPayload3( NeighborMax, Neighbors[0], Neighbors[2] );
  1950.             NeighborMax = MaxPayload3( NeighborMax, Neighbors[6], Neighbors[8] );
  1951.  
  1952.             if( AA_ROUND )
  1953.             {
  1954.                 NeighborMin = AddPayload(MulPayload(NeighborMin, 0.5), MulPayload(NeighborMinPlus, 0.5));
  1955.                 NeighborMax = AddPayload(MulPayload(NeighborMax, 0.5), MulPayload(NeighborMaxPlus, 0.5));
  1956.             }
  1957.         }
  1958.         #endif
  1959.     }
  1960.     #else
  1961.         #error Unknown history clamping box.
  1962.     #endif
  1963.  
  1964.     OutNeighborMin = NeighborMin;
  1965.     OutNeighborMax = NeighborMax;
  1966. }
  1967.  
  1968.  
  1969. // Sample history.
  1970. FTAAHistoryPayload SampleHistory(in float2 HistoryScreenPosition)
  1971. {
  1972.     float4 RawHistory0 = 0;
  1973.     float4 RawHistory1 = 0;
  1974.  
  1975.     // Sample the history using Catmull-Rom to reduce blur on motion.
  1976.     #if AA_BICUBIC
  1977.     {
  1978.         float2 HistoryBufferUV = HistoryScreenPosition * ScreenPosToHistoryBufferUV.xy + ScreenPosToHistoryBufferUV.zw;
  1979.  
  1980.         // Clamp HistoryBufferUV so that we don't have to do it entirely for each samples.
  1981.         #if AA_MANUALLY_CLAMP_HISTORY_UV
  1982.             HistoryBufferUV = clamp(HistoryBufferUV, HistoryBufferUVMinMax.xy, HistoryBufferUVMinMax.zw);
  1983.         #endif
  1984.  
  1985.         FCatmullRomSamples Samples = GetBicubic2DCatmullRomSamples(HistoryBufferUV, HistoryBufferSize.xy, HistoryBufferSize.zw);
  1986.         UNROLL
  1987.         for (uint i = 0; i < Samples.Count; i++)
  1988.         {
  1989.             float2 SampleUV = Samples.UV[i];
  1990.  
  1991.             // Clamp SampleUV within HistoryBufferUVMinMax to avoid sampling potential NaN outside view rect.
  1992.             // This may look expensive, but Samples.UVDir is actually compile time constant to give a hint on what and how each component can be optimally clamped.
  1993.             if (AA_MANUALLY_CLAMP_HISTORY_UV)
  1994.             {
  1995.                 if (Samples.UVDir[i].x < 0)
  1996.                 {
  1997.                     SampleUV.x = max(SampleUV.x, HistoryBufferUVMinMax.x);
  1998.                 }
  1999.                 else if (Samples.UVDir[i].x > 0)
  2000.                 {
  2001.                     SampleUV.x = min(SampleUV.x, HistoryBufferUVMinMax.z);
  2002.                 }
  2003.  
  2004.                 if (Samples.UVDir[i].y < 0)
  2005.                 {
  2006.                     SampleUV.y = max(SampleUV.y, HistoryBufferUVMinMax.y);
  2007.                 }
  2008.                 else if (Samples.UVDir[i].y > 0)
  2009.                 {
  2010.                     SampleUV.y = min(SampleUV.y, HistoryBufferUVMinMax.w);
  2011.                 }
  2012.             }
  2013.  
  2014.             RawHistory0 += HistoryBuffer_0.SampleLevel(HistoryBufferSampler_0, SampleUV, 0) * Samples.Weight[i];
  2015.         }
  2016.         RawHistory0 *= Samples.FinalMultiplier;
  2017.     }
  2018.  
  2019.     // Sample the history using bilinear sampler.
  2020.     #else
  2021.     {
  2022.         // Clamp HistoryScreenPosition to be within viewport.
  2023.         if (AA_MANUALLY_CLAMP_HISTORY_UV)
  2024.         {
  2025.             HistoryScreenPosition = clamp(HistoryScreenPosition, -ScreenPosAbsMax, ScreenPosAbsMax);
  2026.         }
  2027.  
  2028.         float2 HistoryBufferUV = HistoryScreenPosition * ScreenPosToHistoryBufferUV.xy + ScreenPosToHistoryBufferUV.zw;
  2029.  
  2030.         RawHistory0 = HistoryBuffer_0.SampleLevel(HistoryBufferSampler_0, HistoryBufferUV, 0);
  2031.     }
  2032.     #endif
  2033.  
  2034.     #if HISTORY_RENDER_TARGETS == 2
  2035.     {
  2036.         if (AA_MANUALLY_CLAMP_HISTORY_UV)
  2037.         {
  2038.             HistoryScreenPosition = clamp(HistoryScreenPosition, -ScreenPosAbsMax, ScreenPosAbsMax);
  2039.         }
  2040.  
  2041.         float2 HistoryBufferUV = HistoryScreenPosition * ScreenPosToHistoryBufferUV.xy + ScreenPosToHistoryBufferUV.zw;
  2042.  
  2043.         RawHistory1 = HistoryBuffer_1.SampleLevel(HistoryBufferSampler_1, HistoryBufferUV, 0);
  2044.     }
  2045.     #endif
  2046.    
  2047.     // GHOSTFIX: Debug_RedMask stores its red amount as NEGATIVE alpha, remove the red here so it never accumulates in the history.
  2048.     #if GHOSTFIX_DEBUG_REDMASK == 1 && HISTORY_PAYLOAD_COMPONENTS == 3
  2049.     {
  2050.         float GhostFixTintHistory = saturate(-RawHistory0.a);
  2051.         RawHistory0.rgb = (RawHistory0.rgb - GhostFixTintHistory * float3(1.0, 0.0, 0.0)) * rcp(max(1.0 - GhostFixTintHistory, 0.1));
  2052.         RawHistory0.a = max(RawHistory0.a, 0.0);
  2053.     }
  2054.     #endif
  2055.  
  2056.     FTAAHistoryPayload HistoryPayload;
  2057.     HistoryPayload.Color = RawHistory0;
  2058.  
  2059.     #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  2060.         HistoryPayload.CocRadius = RawHistory1.r;
  2061.     #else
  2062.         HistoryPayload.CocRadius = RawHistory0.a;
  2063.     #endif
  2064.  
  2065.     #if COMPILER_SUPPORTS_HLSL2021
  2066.         CorrectExposure(HistoryPayload.Color.rgb);
  2067.     #else
  2068.         HistoryPayload.Color.rgb *= HistoryPreExposureCorrection;
  2069.     #endif
  2070.  
  2071.     HistoryPayload.Color = TransformSceneColor(HistoryPayload.Color);
  2072.  
  2073.     return HistoryPayload;
  2074. }
  2075.  
  2076.  
  2077. // Clamp history.
  2078. FTAAHistoryPayload ClampHistory(inout FTAAIntermediaryResult IntermediaryResult, FTAAHistoryPayload History, FTAAHistoryPayload NeighborMin, FTAAHistoryPayload NeighborMax)
  2079. {
  2080.     #if !AA_CLAMP
  2081.         return History;
  2082.  
  2083.     #else
  2084.         // GHOSTFIX: plain per channel clamp (the original AA_CLIP branch could not compile and was only used by AA_CLIP == 1).
  2085.         History.Color = clamp(History.Color, NeighborMin.Color, NeighborMax.Color);
  2086.         History.CocRadius = clamp(History.CocRadius, NeighborMin.CocRadius, NeighborMax.CocRadius);
  2087.         return History;
  2088.     #endif
  2089. }
  2090.  
  2091.  
  2092. #if GHOSTFIX_ACTIVE
  2093. // GHOSTFIX: camera-only motion (abs pixels/frame, x and y) of a screen position at a given depth.
  2094. float2 GhostFixCameraSpeed(float2 ScreenPos, float DeviceZ)
  2095. {
  2096.     float4 PrevClip = mul(float4(ScreenPos, DeviceZ, 1), View.ClipToPrevClip);
  2097.     return abs((ScreenPos - PrevClip.xy / PrevClip.w) * OutputViewportSize.xy * 0.5);
  2098. }
  2099. #endif
  2100.  
  2101. //------------------------------------------------------- TAA MAIN FUNCTION
  2102.  
  2103. FTAAHistoryPayload TemporalAASample(uint2 GroupId, uint2 GroupThreadId, uint GroupThreadIndex, float2 ViewportUV, float FrameExposureScale)
  2104. {
  2105.     // SETUP
  2106.     // -----
  2107.     FTAAInputParameters InputParams;
  2108.  
  2109.     // Per frame setup.
  2110.     InputParams.FrameExposureScale = ToScalarMemory(FrameExposureScale * View.OneOverPreExposure);
  2111.  
  2112.  
  2113.     // Per pixel setup.
  2114.     {
  2115.         InputParams.GroupId = GroupId;
  2116.         InputParams.GroupThreadId = GroupThreadId;
  2117.         InputParams.GroupThreadIndex = GroupThreadIndex;
  2118.         InputParams.ViewportUV = ViewportUV;
  2119.         InputParams.ScreenPos = ViewportUVToScreenPos(ViewportUV);
  2120.         InputParams.NearestBufferUV = ViewportUV * ViewportUVToInputBufferUV.xy + ViewportUVToInputBufferUV.zw;
  2121.  
  2122.  
  2123.         // Handle single or multi-pass responsive AA
  2124.         #if AA_SINGLE_PASS_RESPONSIVE
  2125.         {
  2126.             const uint kResponsiveStencilMask = 1 << 3;
  2127.            
  2128.             int2 SceneStencilUV = (int2)trunc(InputParams.NearestBufferUV * InputSceneColorSize.xy);
  2129.             uint SceneStencilRef = StencilTexture.Load(int3(SceneStencilUV, 0)) STENCIL_COMPONENT_SWIZZLE;
  2130.  
  2131.             InputParams.bIsResponsiveAAPixel = (SceneStencilRef & kResponsiveStencilMask) ? 1.f : 0.f;
  2132.  
  2133.         }
  2134.         #elif TAA_RESPONSIVE
  2135.             InputParams.bIsResponsiveAAPixel = 1.f;
  2136.         #else
  2137.             InputParams.bIsResponsiveAAPixel = 0.f;
  2138.         #endif
  2139.    
  2140.         #if AA_UPSAMPLE
  2141.         {
  2142.             // Pixel coordinate of the center of output pixel O in the input viewport.
  2143.             float2 PPCo = ViewportUV * InputViewSize.xy + TemporalJitterPixels;
  2144.        
  2145.             // Pixel coordinate of the center of the nearest input pixel K.
  2146.             float2 PPCk = floor(PPCo) + 0.5;
  2147.        
  2148.             // Pixel coordinate of the center of the nearest top left input pixel T.
  2149.             float2 PPCt = floor(PPCo - 0.5) + 0.5;
  2150.        
  2151.             InputParams.NearestBufferUV = InputSceneColorSize.zw * (InputViewMin + PPCk);
  2152.             InputParams.NearestTopLeftBufferUV = InputSceneColorSize.zw * (InputViewMin + PPCt);
  2153.  
  2154.             // TODO: because use nearest sampler, can be faster in this computation.
  2155.         }
  2156.         #endif
  2157.     }
  2158.  
  2159.     // Setup intermediary results.
  2160.     FTAAIntermediaryResult IntermediaryResult = CreateIntermediaryResult();
  2161.  
  2162.     // GHOSTFIX: own pixel motion (abs pixels/frame, x and y) and the speed rejection 0..1.
  2163.     float2 GhostFixOwnMovementPixels = 0.0;
  2164.     float GhostFixReject = 0.0;
  2165.     float2 GhostFixSpeed = 0.0;         // own speed + fastest speed around (GHOSTFIX_SPREAD)
  2166.     float GhostFixBlendBase = 0.0;      // blend before the "minimum contribution" term, used by the debug view
  2167.     bool GhostFixTrail = false;         // history dropped as trail of a moving object
  2168.     float GhostFixHistDyn = 0.0;        // history "moving object" flag, debug view 3
  2169.     float GhostFixTint = 0.0;           // debug red amount
  2170.  
  2171.     // FIND MOTION OF PIXEL AND NEAREST IN NEIGHBORHOOD
  2172.     // ------------------------------------------------
  2173.     float3 PosN; // Position of this pixel, possibly later nearest pixel in neighborhood.
  2174.     PosN.xy = InputParams.ScreenPos;
  2175.  
  2176.     PrecacheInputSceneDepth(InputParams);
  2177.     PosN.z = SampleCachedSceneDepthTexture(InputParams, int2(0, 0));
  2178.  
  2179.     // GHOSTFIX: the pixel's own depth, before the neighbourhood depth dilation below overwrites PosN.z.
  2180.     const float GhostFixOwnDeviceZ = PosN.z;
  2181.  
  2182.     // Screen position of minimum depth.
  2183.     float2 VelocityOffset = float2(0.0, 0.0);
  2184.     #if AA_CROSS // TODO: 2x2.
  2185.     {
  2186.         // For motion vector, use camera/dynamic motion from min depth pixel in pattern around pixel.
  2187.         // This enables better quality outline on foreground against different motion background.
  2188.         // Larger 2 pixel distance "x" works best (because AA dilates surface).
  2189.         float4 Depths;
  2190.         Depths.x = SampleCachedSceneDepthTexture(InputParams, int2(-AA_CROSS, -AA_CROSS));
  2191.         Depths.y = SampleCachedSceneDepthTexture(InputParams, int2( AA_CROSS, -AA_CROSS));
  2192.         Depths.z = SampleCachedSceneDepthTexture(InputParams, int2(-AA_CROSS,  AA_CROSS));
  2193.         Depths.w = SampleCachedSceneDepthTexture(InputParams, int2( AA_CROSS,  AA_CROSS));
  2194.  
  2195.         float2 DepthOffset = float2(AA_CROSS, AA_CROSS);
  2196.         float DepthOffsetXx = float(AA_CROSS);
  2197.         #if HAS_INVERTED_Z_BUFFER
  2198.             // Nearest depth is the largest depth (depth surface 0=far, 1=near).
  2199.             if(Depths.x > Depths.y)
  2200.             {
  2201.                 DepthOffsetXx = -AA_CROSS;
  2202.             }
  2203.             if(Depths.z > Depths.w)
  2204.             {
  2205.                 DepthOffset.x = -AA_CROSS;
  2206.             }
  2207.             float DepthsXY = max(Depths.x, Depths.y);
  2208.             float DepthsZW = max(Depths.z, Depths.w);
  2209.             if(DepthsXY > DepthsZW)
  2210.             {
  2211.                 DepthOffset.y = -AA_CROSS;
  2212.                 DepthOffset.x = DepthOffsetXx;
  2213.             }
  2214.             float DepthsXYZW = max(DepthsXY, DepthsZW);
  2215.             if(DepthsXYZW > PosN.z)
  2216.             {
  2217.                 // This is offset for reading from velocity texture.
  2218.                 // This supports half or fractional resolution velocity textures.
  2219.                 // With the assumption that UV position scales between velocity and color.
  2220.                 VelocityOffset = DepthOffset * InputSceneColorSize.zw;
  2221.                 // This is [0 to 1] flipped in Y.
  2222.                 //PosN.xy = ScreenPos + DepthOffset * OutputViewportSize.zw * 2.0;
  2223.                 PosN.z = DepthsXYZW;
  2224.             }
  2225.         #else // !HAS_INVERTED_Z_BUFFER
  2226.             #error Fix me!
  2227.         #endif // !HAS_INVERTED_Z_BUFFER
  2228.     }
  2229.     #endif  // AA_CROSS
  2230.  
  2231.     // Camera motion for pixel or nearest pixel (in ScreenPos space).
  2232.     bool OffScreen = false;
  2233.     float Velocity = 0;
  2234.     float HistoryBlur = 0;
  2235.     float2 HistoryScreenPosition = InputParams.ScreenPos;
  2236.  
  2237.     #if 1
  2238.     {
  2239.         float4 ThisClip = float4( PosN.xy, PosN.z, 1 );
  2240.         float4 PrevClip = mul( ThisClip, View.ClipToPrevClip );
  2241.         float2 PrevScreen = PrevClip.xy / PrevClip.w;
  2242.         float2 BackN = PosN.xy - PrevScreen;
  2243.  
  2244.         float2 BackTemp = BackN * OutputViewportSize.xy;
  2245.  
  2246.         #if AA_DYNAMIC
  2247.         {
  2248.             ENCODED_VELOCITY_TYPE EncodedVelocity = SampleVelocityTexture(InputParams.NearestBufferUV + VelocityOffset);
  2249.             bool DynamicN = EncodedVelocity.x > 0.0;
  2250.             if(DynamicN)
  2251.             {
  2252.                 BackN = DecodeVelocityFromTexture(EncodedVelocity).xy;
  2253.             }
  2254.  
  2255.  
  2256.             BackTemp = BackN * OutputViewportSize.xy;
  2257.         }
  2258.         #endif
  2259.  
  2260.         Velocity = sqrt(dot(BackTemp, BackTemp));
  2261.  
  2262.         // GHOSTFIX: OWN pixel motion (not dilated), and whether the pixel itself is a moving object.
  2263.         float GhostFixOwnDynamic = 0.0;
  2264.         {
  2265.             float4 OwnPrevClip = mul(float4(InputParams.ScreenPos, GhostFixOwnDeviceZ, 1), View.ClipToPrevClip);
  2266.             float2 OwnBack = InputParams.ScreenPos - OwnPrevClip.xy / OwnPrevClip.w;
  2267.             #if AA_DYNAMIC
  2268.                 ENCODED_VELOCITY_TYPE OwnEncodedVelocity = SampleVelocityTexture(InputParams.NearestBufferUV);
  2269.                 if (OwnEncodedVelocity.x > 0.0)
  2270.                 {
  2271.                     OwnBack = DecodeVelocityFromTexture(OwnEncodedVelocity).xy;
  2272.                     GhostFixOwnDynamic = 1.0;
  2273.                 }
  2274.             #endif
  2275.             GhostFixOwnMovementPixels = abs(OwnBack * OutputViewportSize.xy * 0.5);
  2276.         }
  2277.  
  2278.         // GHOSTFIX: speed rejection = max(X ramp, Y ramp) * near factor, moving objects excluded.
  2279.         #if GHOSTFIX_ACTIVE
  2280.         {
  2281.             // Fill the still hole (drift centre) with the fastest camera speed found around the pixel (2 rings of 8 taps).
  2282.             GhostFixSpeed = GhostFixOwnMovementPixels;
  2283.             #if 1
  2284.             if (GHOSTFIX_SPREAD > 0.0)
  2285.             {
  2286.                 const float2 SpreadDirs[8] = { float2(1, 0), float2(0.7071, 0.7071), float2(0, 1), float2(-0.7071, 0.7071), float2(-1, 0), float2(-0.7071, -0.7071), float2(0, -1), float2(0.7071, -0.7071) };
  2287.                 [unroll]
  2288.                 for (int SpreadIndex = 0; SpreadIndex < 8; SpreadIndex++)
  2289.                 {
  2290.                     float2 SpreadOffset = SpreadDirs[SpreadIndex] * float2(2.0, -2.0) * OutputViewportSize.zw * GHOSTFIX_SPREAD;
  2291.                     GhostFixSpeed = max(GhostFixSpeed, GhostFixCameraSpeed(InputParams.ScreenPos + SpreadOffset, GhostFixOwnDeviceZ));
  2292.                     GhostFixSpeed = max(GhostFixSpeed, GhostFixCameraSpeed(InputParams.ScreenPos + SpreadOffset * 0.5, GhostFixOwnDeviceZ));
  2293.                 }
  2294.             }
  2295.             #endif
  2296.  
  2297.             float RejectX = saturate((GhostFixSpeed.x - GHOSTFIX_SPEED_X_START) * rcp(max(GHOSTFIX_SPEED_X_FULL - GHOSTFIX_SPEED_X_START, 1e-3)));
  2298.             float RejectY = saturate((GhostFixSpeed.y - GHOSTFIX_SPEED_Y_START) * rcp(max(GHOSTFIX_SPEED_Y_FULL - GHOSTFIX_SPEED_Y_START, 1e-3)));
  2299.             float Near = (GHOSTFIX_DEPTH_FAR > 0.0) ? 1.0 - saturate((ConvertFromDeviceZ(GhostFixOwnDeviceZ) - GHOSTFIX_DEPTH_NEAR) * rcp(max(GHOSTFIX_DEPTH_FAR - GHOSTFIX_DEPTH_NEAR, 1e-3))) : 1.0;
  2300.             float Protect = GHOSTFIX_PROTECT_MOVING ? GhostFixOwnDynamic : 0.0;
  2301.  
  2302.             GhostFixReject = max(RejectX, RejectY) * Near * (1.0 - Protect);
  2303.         }
  2304.         #endif
  2305.         #if !AA_BICUBIC
  2306.             // Save the amount of pixel offset of just camera motion, used later as the amount of blur introduced by history.
  2307.             float HistoryBlurAmp = 2.0;
  2308.             HistoryBlur = saturate(abs(BackTemp.x) * HistoryBlurAmp + abs(BackTemp.y) * HistoryBlurAmp);
  2309.         #endif
  2310.         // Easier to do off screen check before conversion.
  2311.         // BackN is in units of 2pixels/viewportWidthInPixels
  2312.         // This converts back projection vector to [-1 to 1] offset in viewport.
  2313.         HistoryScreenPosition = InputParams.ScreenPos - BackN;
  2314.  
  2315.         // Detect if HistoryBufferUV would be outside of the viewport.
  2316.         OffScreen = max(abs(HistoryScreenPosition.x), abs(HistoryScreenPosition.y)) >= 1.0;
  2317.     }
  2318.     #endif
  2319.  
  2320.     // Precache input scene color.
  2321.     PrecacheInputSceneColor(/* inout = */ InputParams);
  2322.  
  2323.     // Filter input.
  2324.     #if AA_UPSAMPLE_ADAPTIVE_FILTERING == 0
  2325.         FilterCurrentFrameInputSamples(
  2326.             InputParams,
  2327.             /* inout = */ IntermediaryResult);
  2328.     #endif
  2329.    
  2330.     // Compute neighborhood bounding box.
  2331.     FTAAHistoryPayload NeighborMin;
  2332.     FTAAHistoryPayload NeighborMax;
  2333.  
  2334.     ComputeNeighborhoodBoundingbox(
  2335.         InputParams,
  2336.         /* inout = */ IntermediaryResult,
  2337.         NeighborMin, NeighborMax);
  2338.  
  2339.     // Sample history.
  2340.     FTAAHistoryPayload History = SampleHistory(HistoryScreenPosition);
  2341.  
  2342.     // Whether the feedback needs to be reset.
  2343.     bool IgnoreHistory = OffScreen || bCameraCut;
  2344.  
  2345.     // DYNAMIC ANTI GHOSTING
  2346.     // ---------------------
  2347.     // GHOSTFIX_TRAIL: history alpha = "this pixel was dynamic last frame". Dynamic in history but nothing dynamic now = trail.
  2348.     #if AA_DYNAMIC_ANTIGHOST && AA_DYNAMIC && HISTORY_PAYLOAD_COMPONENTS == 3
  2349.     bool Dynamic4;
  2350.     {
  2351.         // TODO: try a 2x2 for AA_UPSAMPLE
  2352.         bool Dynamic1 = SampleVelocityTexture(InputParams.NearestBufferUV, int2( 0, -1)).x > 0;
  2353.         bool Dynamic3 = SampleVelocityTexture(InputParams.NearestBufferUV, int2(-1,  0)).x > 0;
  2354.         Dynamic4 = SampleVelocityTexture(InputParams.NearestBufferUV).x > 0;
  2355.         bool Dynamic5 = SampleVelocityTexture(InputParams.NearestBufferUV, int2( 1,  0)).x > 0;
  2356.         bool Dynamic7 = SampleVelocityTexture(InputParams.NearestBufferUV, int2( 0,  1)).x > 0;
  2357.  
  2358.         bool Dynamic = Dynamic1 || Dynamic3 || Dynamic4 || Dynamic5 || Dynamic7;
  2359.         GhostFixHistDyn = saturate(History.Color.a);
  2360.         GhostFixTrail = !Dynamic && History.Color.a > (GHOSTFIX_TRAIL ? GHOSTFIX_TRAIL_THRESHOLD : 0.0);
  2361.         IgnoreHistory = IgnoreHistory || GhostFixTrail;
  2362.     }
  2363.     #endif
  2364.    
  2365.     // Save off luma of history before the clamp.
  2366.     float LumaMin     = GetSceneColorLuma4(NeighborMin.Color);
  2367.     float LumaMax     = GetSceneColorLuma4(NeighborMax.Color);
  2368.     float LumaHistory = GetSceneColorLuma4(History.Color);
  2369.    
  2370.     // Clamp history.
  2371.     FTAAHistoryPayload PreClampingHistoryColor = History;
  2372.     History = ClampHistory(IntermediaryResult, History, NeighborMin, NeighborMax);
  2373.  
  2374.    
  2375.     // Filter input after color clamping.
  2376.     #if AA_UPSAMPLE_ADAPTIVE_FILTERING == 1
  2377.     {
  2378.         #if AA_VARIANCE
  2379.             #error AA_VARIANCE and AA_UPSAMPLE_ADAPTIVE_FILTERING are not compatible because of circular code dependency.
  2380.         #endif
  2381.  
  2382.         if (IgnoreHistory) // || any(HistoryColor != PreClampingHistoryColor))
  2383.         {
  2384.             // Set the input filter infinitely large when we know need to rely on it.
  2385.             IntermediaryResult.InvFilterScaleFactor = 0;
  2386.         }
  2387.  
  2388.         // Blur input according to input pixel velocity to reduce blocky filtering cause by narrow filter on low screen percentage.
  2389.         // Multiplied by upscale factor because Velocity is in output resolution based pixel velocity.
  2390.         IntermediaryResult.InvFilterScaleFactor -= (Velocity * UpscaleFactor) * 0.1;
  2391.  
  2392.         // Set a minimal filtering scale to screen percentage to not unecessarily blur the input more than the screen percentage.
  2393.         IntermediaryResult.InvFilterScaleFactor = max(IntermediaryResult.InvFilterScaleFactor, ScreenPercentage);
  2394.  
  2395.         FilterCurrentFrameInputSamples(
  2396.             InputParams,
  2397.             /* inout = */ IntermediaryResult);
  2398.     }
  2399.     #endif
  2400.  
  2401.     // ADD BACK IN ALIASING TO SHARPEN
  2402.     // -------------------------------
  2403.     #if AA_FILTERED && !AA_BICUBIC
  2404.     {
  2405.         #if AA_UPSAMPLE
  2406.             #error Temporal upsample does not support sharpen.
  2407.         #endif
  2408.        
  2409.         // Blend in non-filtered based on the amount of sub-pixel motion.
  2410.         float AddAliasing = saturate(HistoryBlur) * 0.5;
  2411.         float LumaContrastFactor = 32.0;
  2412.         #if AA_YCOCG // TODO: Probably a bug arround here because using Luma4() even with YCOCG=0.
  2413.             // 1/4 as bright.
  2414.             LumaContrastFactor *= 4.0;
  2415.         #endif
  2416.         float LumaContrast = LumaMax - LumaMin;
  2417.         AddAliasing = saturate(AddAliasing + rcp(1.0 + LumaContrast * LumaContrastFactor));
  2418.         IntermediaryResult.Filtered.Color = lerp(IntermediaryResult.Filtered.Color, SampleCachedSceneColorTexture(InputParams, int2(0, 0)).Color, AddAliasing);
  2419.     }
  2420.     #endif
  2421.    
  2422.     // GHOSTFIX: rejected pixels use a 3x3 blurred current frame (they would look sharper than their surroundings).
  2423.     #if GHOSTFIX_NEEDS_BLUR
  2424.     {
  2425.         float GhostFixBlurAmount = GhostFixReject * GHOSTFIX_BLUR;
  2426.         IntermediaryResult.Filtered.Color = lerp(IntermediaryResult.Filtered.Color, IntermediaryResult.GhostFixBlurred, GhostFixBlurAmount);
  2427.     }
  2428.     #endif
  2429.  
  2430.     // COMPUTE BLEND AMOUNT
  2431.     // --------------------
  2432.     float BlendFinal;
  2433.     {
  2434.         float LumaFiltered = GetSceneColorLuma4(IntermediaryResult.Filtered.Color);
  2435.  
  2436.         BlendFinal = IntermediaryResult.FilteredTemporalWeight * CurrentFrameWeight;
  2437.  
  2438.         BlendFinal = lerp(BlendFinal, 0.2, saturate(Velocity / 40));
  2439.         GhostFixBlendBase = BlendFinal;
  2440.  
  2441.         #if 0
  2442.         {
  2443.             // Anti-flicker
  2444.             float DistToClamp = 2 * abs(min(LumaHistory - LumaMin, LumaMax - LumaHistory) / (LumaMax - LumaMin));
  2445.             //BlendFinal *= lerp( 0, 1, saturate(4 * DistToClamp) );
  2446.             BlendFinal += 0.8 * saturate(0.02 * LumaHistory / abs(Filtered.x - LumaHistory));
  2447.             BlendFinal *= (LumaMin * InExposureScale + 0.5) / (LumaMax * InExposureScale + 0.5);
  2448.         }
  2449.         #endif
  2450.  
  2451.         // Make sure to have at least some small contribution
  2452.         BlendFinal = max( BlendFinal, saturate( 0.01 * LumaHistory / abs( LumaFiltered - LumaHistory ) ) );
  2453.  
  2454.         #if AA_NAN && (COMPILER_GLSL || COMPILER_METAL)
  2455.             // The current Metal & GLSL compilers don't handle saturate(NaN) -> 0, instead they return NaN/INF.
  2456.             BlendFinal = -min(-BlendFinal, 0.0);
  2457.         #endif
  2458.  
  2459.         // Responsive forces 1/4 of new frame.
  2460.         BlendFinal = InputParams.bIsResponsiveAAPixel ? (1.0/4.0) : BlendFinal;
  2461.  
  2462.         // GHOSTFIX: speed rejection, more current frame / less history.
  2463.         BlendFinal = lerp(BlendFinal, max(BlendFinal, GHOSTFIX_STRENGTH), GhostFixReject);
  2464.  
  2465.         // Debug views 2 and 3 show raw data, no history so nothing accumulates.
  2466.         #if GHOSTFIX_DEBUG_REDMASK >= 2
  2467.             BlendFinal = 1.0;
  2468.         #endif
  2469.  
  2470.         #if AA_LERP
  2471.             BlendFinal = 1.0/float(AA_LERP);
  2472.         #endif
  2473.    
  2474.         #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC || AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  2475.         {
  2476.             float BilateralWeight = ComputeBilateralWeight(IntermediaryResult.Filtered.CocRadius, History.CocRadius);
  2477.  
  2478.             BlendFinal = lerp(BlendFinal, 1, (1-BilateralWeight)*CoCBilateralFilterStrength);
  2479.         }
  2480.         #endif
  2481.        
  2482.         if (bCameraCut)
  2483.         {
  2484.             BlendFinal = 1.0;
  2485.         }
  2486.     }
  2487.  
  2488.     // Offscreen feedback resets.
  2489.     if (IgnoreHistory)
  2490.     {
  2491.         History = IntermediaryResult.Filtered;
  2492.         #if HISTORY_PAYLOAD_COMPONENTS == 3
  2493.             History.Color.a = 0.0;
  2494.         #endif
  2495.     }
  2496.    
  2497.     // DO FINAL BLEND BETWEEN HISTORY AND FILTERED COLOR
  2498.     // -------------------------------------------------
  2499.     // Luma weighted blend
  2500.     float FilterWeight = GetSceneColorHdrWeight(InputParams, IntermediaryResult.Filtered.Color.x);
  2501.     float HistoryWeight = GetSceneColorHdrWeight(InputParams, History.Color.x);
  2502.  
  2503.     FTAAHistoryPayload OutputPayload;
  2504.     {
  2505.         float2 Weights = WeightedLerpFactors(HistoryWeight, FilterWeight, BlendFinal);
  2506.         OutputPayload = AddPayload(MulPayload(History, Weights.x), MulPayload(IntermediaryResult.Filtered, Weights.y));
  2507.     }
  2508.  
  2509.     // Ensure that alpha values that are expected to be opaque (but are only close to opaque) are forced to be opaque.
  2510.     // (0.995 chosen to accomodate handling of 254/255)
  2511.     if (OutputPayload.Color.a > 0.995)
  2512.     {
  2513.         OutputPayload.Color.a = 1;
  2514.     }
  2515.  
  2516.     OutputPayload.Color = TransformBackToRawLinearSceneColor(OutputPayload.Color);
  2517.    
  2518.     #if AA_NAN
  2519.         // Transform NaNs to black, transform negative colors to black.
  2520.         OutputPayload.Color = -min(-OutputPayload.Color, 0.0);
  2521.  
  2522.         OutputPayload.CocRadius = isnan(OutputPayload.CocRadius) ? 0.0 : OutputPayload.CocRadius;
  2523.     #endif
  2524.  
  2525.     // GHOSTFIX: Debug_RedMask. Red = AA removed by GhostFix (speed rejection or trail). Full red = no AA, fades out as more history is used.
  2526.     // Measured on the blend BEFORE the engine's "minimum contribution" term (that one reaches 1 on flat areas like sky or car paint).
  2527.     // Red is removed again when the history is read (see SampleHistory), so it does not accumulate.
  2528.     #if GHOSTFIX_DEBUG_REDMASK == 1
  2529.     {
  2530.         float GhostFixCurrentWeight = GhostFixTrail ? 1.0 : lerp(GhostFixBlendBase, max(GhostFixBlendBase, GHOSTFIX_STRENGTH), GhostFixReject);
  2531.         float GhostFixNoAA = saturate((GhostFixCurrentWeight - 0.2) * rcp(0.8));
  2532.         GhostFixTint = min(GhostFixNoAA, 0.9); // < 1 so the color can be recovered from the history
  2533.         OutputPayload.Color.rgb = lerp(OutputPayload.Color.rgb, float3(1.0, 0.0, 0.0), GhostFixTint);
  2534.     }
  2535.     #elif GHOSTFIX_DEBUG_REDMASK == 2
  2536.         // Speed used by the rejection (own + spread) relative to the X / Y full-rejection speeds: red = X, green = Y, yellow = both, black = still.
  2537.         OutputPayload.Color.rgb = float3(saturate(GhostFixSpeed.x / max(GHOSTFIX_SPEED_X_FULL, 1e-3)), saturate(GhostFixSpeed.y / max(GHOSTFIX_SPEED_Y_FULL, 1e-3)), 0.0);
  2538.     #elif GHOSTFIX_DEBUG_REDMASK == 3
  2539.         // History "a moving object was here" flag (what GHOSTFIX_TRAIL reacts to): green = flagged.
  2540.         OutputPayload.Color.rgb = float3(0.0, GhostFixHistDyn, 0.0);
  2541.     #endif
  2542.  
  2543.     #if HISTORY_PAYLOAD_COMPONENTS == 3
  2544.         #if  AA_DYNAMIC_ANTIGHOST && AA_DYNAMIC
  2545.             OutputPayload.Color.a = (Dynamic4 ? 1 : 0) - GhostFixTint;
  2546.         #else
  2547.             // Zero out to remove any prior computation of alpha
  2548.             OutputPayload.Color.a = -GhostFixTint;
  2549.         #endif
  2550.     #endif
  2551.  
  2552.     return OutputPayload;
  2553. }
  2554.  
  2555. //------------------------------------------------------- ENTRY POINTS
  2556.  
  2557. #if COMPUTESHADER
  2558.  
  2559. [numthreads(THREADGROUP_SIZEX, THREADGROUP_SIZEY, 1)]
  2560. void MainCS(
  2561.     uint2 DispatchThreadId : SV_DispatchThreadID,
  2562.     uint2 GroupId : SV_GroupID,
  2563.     uint2 GroupThreadId : SV_GroupThreadID,
  2564.     uint GroupThreadIndex : SV_GroupIndex)
  2565. {
  2566.     float2 ViewportUV = (float2(DispatchThreadId) + 0.5f) * OutputViewportSize.zw;
  2567.    
  2568.     #if AA_LOWER_RESOLUTION
  2569.     {
  2570.         ViewportUV = (float2(DispatchThreadId) + 0.5f) * MaxViewportUVAndSvPositionToViewportUV.zw;
  2571.         ViewportUV = min(ViewportUV, MaxViewportUVAndSvPositionToViewportUV.xy);
  2572.     }
  2573.     #endif
  2574.  
  2575.     float FrameExposureScale = EyeAdaptationLookup();
  2576.     FTAAHistoryPayload OutputPayload = TemporalAASample(GroupId, GroupThreadId, GroupThreadIndex, ViewportUV, FrameExposureScale);
  2577.  
  2578.     float4 OutColor0 = 0;
  2579.     float4 OutColor1 = 0;
  2580.  
  2581.     #if AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_COC
  2582.     {
  2583.         OutColor0.rgb = OutputPayload.Color.rgb;
  2584.         OutColor0.a = OutputPayload.CocRadius;
  2585.     }
  2586.     #elif AA_HISTORY_PAYLOAD == HISTORY_PAYLOAD_RGB_OPACITY_COC
  2587.     {
  2588.         OutColor0 = OutputPayload.Color;
  2589.         OutColor1.r = OutputPayload.CocRadius;
  2590.     }
  2591.     #else
  2592.     {
  2593.         OutColor0 = OutputPayload.Color;
  2594.     }
  2595.     #endif
  2596.  
  2597.     uint2 PixelPos = DispatchThreadId + OutputViewportRect.xy;
  2598.     if (all(PixelPos < OutputViewportRect.zw))
  2599.     {
  2600.         float4 FinalOutput0 = min(MaxHalfFloat.xxxx, OutColor0);
  2601.         #if AA_ENABLE_STOCASTIC_QUANTIZATION
  2602.         {
  2603.             FinalOutput0.rgb = QuantizeFloatColor(FinalOutput0.rgb, OutputQuantizationError, PixelPos, QUANTIZE_NOISE_HAMMERSLEY);
  2604.         }
  2605.         #endif
  2606.  
  2607.         OutComputeTex_0[PixelPos] = FinalOutput0;
  2608.  
  2609.         #if HISTORY_RENDER_TARGETS == 2
  2610.             OutComputeTex_1[PixelPos] = OutColor1;
  2611.         #endif
  2612.     }
  2613.  
  2614.     #if TAA_DOWNSAMPLE
  2615.     {
  2616.         // This shader permutation outputs half resolution image in addition to main full-res one.
  2617.         // It is more efficient than performing a separate downsampling pass afterwards.
  2618.         // Assumes output resolution is always even.
  2619.  
  2620.         uint P0 = GroupThreadId.x + GroupThreadId.y * THREADGROUP_SIZEX;
  2621.         uint P1 = P0 + 1;
  2622.         uint P2 = P0 + THREADGROUP_SIZEX;
  2623.         uint P3 = P2 + 1;
  2624.  
  2625.         GroupSharedDownsampleArray[P0] = OutColor0;
  2626.  
  2627.         GroupMemoryBarrierWithGroupSync();
  2628.  
  2629.         if (((GroupThreadId.x | GroupThreadId.y) & 1) == 0)
  2630.         {
  2631.             OutComputeTexDownsampled[PixelPos / 2] =
  2632.                 (OutColor0 + GroupSharedDownsampleArray[P1] + GroupSharedDownsampleArray[P2] + GroupSharedDownsampleArray[P3]) * 0.25;
  2633.         }
  2634.     }
  2635.     #endif //TAA_DOWNSAMPLE
  2636. }
  2637.  
  2638. #elif PIXELSHADER // Mobile Only, supports Main and MainUpsampling
  2639.  
  2640. void MainPS(
  2641.     float4 SvPosition : SV_POSITION,
  2642.     out float4 OutColor0 : SV_Target0
  2643.     )
  2644. {
  2645.     float2 ViewportUV = (SvPosition.xy - OutputViewportRect.xy) * OutputViewportSize.zw;
  2646.  
  2647.     uint2 GroupId = int2(0, 0);
  2648.     uint2 GroupThreadId = int2(0, 0);
  2649.     uint GroupThreadIndex = 0;
  2650.     float FrameExposureScale = EyeAdaptationLookup();
  2651.    
  2652.     FTAAHistoryPayload OutputPayload = TemporalAASample(GroupId, GroupThreadId, GroupThreadIndex, ViewportUV, FrameExposureScale);
  2653.  
  2654.     OutColor0 = OutputPayload.Color;
  2655. }
  2656.  
  2657. #endif
  2658.  
Advertisement
Add Comment
Please, Sign In to add comment