Repository: godot
Issue: Unexpectedly low performance of temporal antialiasing due to high GPU cost
Labels: discussion,topic:rendering,confirmed,topic:3d,performance

### Godot version

4.0.alpha (b9375ea7fc135f5468dcfb8c80b51a945ac14155)

### System information

Fedora 36, GeForce GTX 1080 (NVIDIA 510.68.02)

### Issue description

In Godot, enabling [TAA](https://github.com/godotengine/godot/pull/61319) on a GTX 1080 and a 2560×1440 viewport takes up more than 1.2 ms of GPU time, even in an empty scene with just a Camera3D. The performance impact seems fairly constant regardless of the scene's contents:

### Empty scene

| TAA disabled | TAA enabled |
|-|-|
| ![2022-06-10_17 25 36](https://user-images.githubusercontent.com/180032/173099012-bfa7bb91-3fca-45a0-966f-25eb7504c040.png) | ![2022-06-10_17 25 44](https://user-images.githubusercontent.com/180032/173099031-ece37cf2-30db-4ab6-972d-90b2f94e0730.png) |

### 1 BoxMesh

| TAA disabled | TAA enabled |
|-|-|
| ![2022-06-10_17 24 39](https://user-images.githubusercontent.com/180032/173099143-c095fde2-ee47-4c60-9df9-9fe50faf8e2d.png) | ![2022-06-10_17 24 44](https://user-images.githubusercontent.com/180032/173099159-059a2a7c-1984-4f48-a7ca-8d8b2b7cd289.png) |

### 1 BoxMesh + 1 DirectionalLight3D with shadows + Default Environment

| TAA disabled | TAA enabled |
|-|-|
| ![2022-06-10_17 25 09](https://user-images.githubusercontent.com/180032/173099188-141153d7-883e-44ac-8465-09429e4d1474.png) | ![2022-06-10_17 25 15](https://user-images.githubusercontent.com/180032/173099213-750a9d0c-718a-489d-9ca7-a6984640df3e.png) |

According to the View Frame Time panel, nearly all of the rendering cost is on the GPU. CPU frame time barely changes when TAA is enabled, at least in simple scenes. Therefore, motion vector generation isn't the bottleneck. The actual TAA shader is more likely to be a bottleneck.

[Replacing the `main()` function's contents with just `return;` in the `taa_resolve.glsl` shader](https://github.com/godotengine/godot/blob/8df8fff54ba2263d53448b0a7f4e33fa76fdea50/servers/rendering/renderer_rd/shaders/taa_resolve.glsl#L371-L393) results in a black image, but still increases GPU time by 0.7 mspf compared to TAA disabled.

Renderdoc confirms that TAA is indeed taking around 1.2 ms of GPU time. (I don't have access to actual Vulkan profiling tools on this GPU, as it's too old to use Vulkan profiling on NSight.)

At lower resolutions, the performance impact of TAA is *much* less noticeable:

### 575×310

| TAA disabled | TAA enabled |
|-|-|
| ![2022-06-10_17 33 47](https://user-images.githubusercontent.com/180032/173100745-8560cf18-d9ea-4218-bd4c-b015c3215082.png) | ![2022-06-10_17 33 54](https://user-images.githubusercontent.com/180032/173100756-e9a4a630-d989-4d71-9693-228b4e0c75c0.png) |

### 905×550

| TAA disabled | TAA enabled |
|-|-|
| ![2022-06-10_17 34 32](https://user-images.githubusercontent.com/180032/173100719-08a92709-a446-486e-8c49-c15bac573b3d.png) | ![2022-06-10_17 34 38](https://user-images.githubusercontent.com/180032/173100735-447c451a-48fa-456b-8c9c-50af5b31a195.png) |

This doesn't compare favorably to the TAA implementation in other open source rendering engines. For example, in [Tesseract](http://tesseract.gg/), in a semi-complex scene with many objects and lights, the frame time difference between TAA disabled and enabled is only ~0.2 ms (`(1.0/333 - 1.0/357) * 1000`):

| TAA disabled in Tesseract | TAA enabled in Tesseract |
|-|-|
| ![2022-05-24_22 49 49_complex_edit](https://user-images.githubusercontent.com/180032/173097152-a0c25174-9f82-4fc0-92e4-dc169c701492.png) | ![2022-05-24_22 49 45_complex_edit](https://user-images.githubusercontent.com/180032/173097127-b8e2594b-7542-458b-a58f-7fcd577a7e3b.png) |

There are many technical differences between Godot 4 and Tesseract's rendering engines:

- Tesseract uses traditional deferred rendering, not clustered forward rendering. It has a non-PBR renderer and favors performance over flexibility.
- Tesseract uses a seemingly simpler (yet effective) form of TAA called TQAA (temporal quincunx antialiasing).
- Tesseract uses OpenGL 4.3 on supported hardware (with 3.3 as a minimum), not Vulkan. This is less efficient than Godot on paper, even though OpenGL can be pretty efficient if used well.

Still, I feel TAA should not be this expensive on the GPU in Godot.

### Steps to reproduce

- Enable **Perspective > View Frame Time** in the top-left corner of the 3D viewport.
- Enable **Use Taa** in the Project Settings.

### Minimal reproduction project

[test_taa_performance.zip](https://github.com/godotengine/godot/files/8880193/test_taa_performance.zip)