Metadata-Version: 2.1
Name: fast_gauss
Version: 0.0.7
Summary: A geometry-shader-based, global CUDA sorted high-performance 3D Gaussian Splatting rasterizer. Can achieve a 5-10x speedup in rendering compared to the vanialla diff-gaussian-rasterization.
Author: Zhen Xu
Author-email: zhenx@zju.edu.cn
License: Copyright 2022-2024 3D Vision Group at the State Key Lab of CAD&CG,  
        Zhejiang University. All Rights Reserved. 
        
        For more information see <https://github.com/dendenxu/fast-gaussian-rasterization> 
        If you use this software, please cite the corresponding publications   
        listed on the above website. 
        
        Permission to use, copy, modify and distribute this software and its 
        documentation for educational, research and non-profit purposes only. 
        Any modification based on this work must be open-source and prohibited 
        for commercial use. 
        You must retain, in the source form of any derivative works that you  
        distribute, all copyright, patent, trademark, and attribution notices  
        from the source form of this work. 
         
        For commercial uses of this software, please send email to xwzhou@zju.edu.cn
        
Project-URL: homepage, https://github.com/dendenxu/fast-gaussian-rasterization
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Requires-Dist: torch
Requires-Dist: numpy
Requires-Dist: cuda-python
Requires-Dist: PyGLM
Requires-Dist: PyOpenGL>=3.1.7

# Fast Gaussian Rasterization

- **Can be 5-10x faster than the original software CUDA rasterizer ([diff-gaussian-rasterization](https://github.com/graphdeco-inria/diff-gaussian-rasterization)).**
- **Can be 2-3x faster if using offline rendering. (Bottleneck: copying rendered images around, thinking about improvements.)**
- **Speedup most visible with high pixel-to-point ratio (large Gaussians, small point count, high-res rendering).**

https://github.com/dendenxu/fast-gaussian-splatting/assets/43734697/f50afd6f-bbd5-4e18-aca6-a7356a5d3f75

No backward pass is supported yet. 
Will think of ways to add a backward. 
Depth-peeling ([4K4D](https://zju3dv.github.io/4k4d)) is too slow.
Discussion welcomed.

## Installation

Install the latest release from PyPI:

```shell
pip install fast_gauss
```

Or the latest commit from GitHub:

```shell
pip install git+https://github.com/dendenxu/fast-gaussian-rasterization
```

No CUDA compilation is required to build `fast_gauss` since we're only shader-based for now.

## Usage

Replace the original import of `diff_gaussian_rasterization` with `fast_gauss`.

For example, replace this:

```python
from diff_gaussian_rasterization import GaussianRasterizationSettings, GaussianRasterizer
```

with this:

```python
from fast_gauss import GaussianRasterizationSettings, GaussianRasterizer
```

And you're good to go.

## Tips

**Note: for the ultimate 5-10x performance increase, you'll need to let `fast_gauss`'s shader directly write to your desired framebuffer.**

Currently, we are trying to automatically detect whether you're managing your own OpenGL context (i.e. opening up a GUI) by checking for the module `OpenGL` during the import of `fast_gauss`.
If detected, all rendering commands will return `None`s and we will directly write to the bound framebuffer at the time of the draw call.
Thus if you're running in a GUI (OpenGL-based) environment, the output of our rasterizer will be `None`s and does not require further processing.

- [ ] TODO: Improve offline rendering performance.
- [ ] TODO: Add a warning to the user if they're performing further processing on the returned values.

**Note: the speedup is the most visible when the pixel-to-point ratio is high.**

That is, when there are large Gaussians and very high-resolution rendering, the speedup is more visible.
The CUDA-based software implementation is more resolution sensitive and for some extremely dense point clouds (> 1 million points), the CUDA implementation might be faster.
This is because the typical rasterization-based pipeline on modern graphics hardware is [not well-optimized for small triangles](https://www.youtube.com/watch?v=hf27qsQPRLQ&list=WL).

**Note: for best performance, cache the persistent results (for example, the 6 elements of the covariance matrix).**

This is more of a general tip and not directly related to `fast_gauss`.
However, the impact is more observable here since we haven't implemented a fast 3D covariance computation (from scales and rotations) in the shader yet.
Only PyTorch implementation is available for now.

When the point count increases, even the smallest `precomputation` can help.
An example is the concatenation of the base 0-degree SH parameter and the rest, that small maneuver might cost us 10ms on a 3060 with 5 million points.
Thus, store the concatenated tensors instead and avoid concatenating them in every frame.

- [ ] TODO: Implement SH eval in the vertex shader.
- [ ] TODO: Warn users if they're not properly precomputing the covariance matrix.
- [ ] TODO: Implement a more optimized `OptimizedGaussians` for precomputing things and apply a cache. Similar to that of the vertex shader (see [Invokation frequency](https://www.khronos.org/opengl/wiki/Vertex_Shader)).

**Note: it's recommended to pass in a CPU tensor in the `GaussianRasterizationSettings` to avoid explicit synchronizations for even better performance.**

- [ ] TODO: Add a warning to the user if GPU tensors are detected.

**Note: the second output of the `GaussianRasterizer` is not radii anymore (since we're not gonna use it for the backward pass), but the alpha values of the rendered image instead.**

And the alpha channel content seems to be bugged currently, will debug.

- [ ] TODO: Debug alpha channel values

## TODOs

- [ ] TODO: Apply more of the optimization techniques used by similar shaders, including packing the data into a texture and bit reduction during computation.
- [ ] TODO: Thinks of ways for a backward pass. Welcome to discuss!
- [ ] TODO: Compute covariance from scaling and rotation in the shader, currently it's on the CUDA (PyTorch) side.
- [ ] TODO: Compute SH in the shader, currently it's on the CUDA (PyTorch) side.
- [ ] TODO: Try to align the rendering results at the pixel level, small deviation exists currently.
- [ ] TODO: Use indexed draw calls to minimize data passing and shuffling.
- [ ] TODO: Do incremental sorting based on viewport change, currently it's a full resort on with CUDA (PyTorch).

## Implementation

**Guidelines**

- Let the professionals do the work.
  - Let GPU do the large-scale sorting.
  - Let the graphics pipeline do the rasterization for us, not the other way around.
  - Let OpenGL directly write to your framebuffer.
- Minimize repeated work.
  - Compute the 3D to 2D covariance projection only once for each Gaussian, instead of 4 times for the quad, enabled by the geometry shader.
- Minimize stalls (minimize explicit synchronizations between GPU and CPU).
  - Enabled by using `non_blocking=True` data passing and moving sync points to as early as possible.
  - Boosted by the fact that we're sorting on the GPU, thus no need to perform synchronized host-to-device copies.

**Why does a global sort work?**

The OpenGL specification is somewhat vague but there's this reference:
(in the 4th paragraph of section 2.1 of chapter 2 of this specification: https://registry.khronos.org/OpenGL/specs/gl/glspec44.core.pdf)

> Commands are always processed in the order in which they are received, although there may be an indeterminate delay before the effects of a command are realized. This means, for example, that one primitive must be drawn completely before any subsequent one can affect the framebuffer.

Thus if the order of the data in the vertex buffer (or as specified by an index buffer) is back-to-front, and alpha blending is enabled, you can count on OpenGL to correctly update the framebuffer in the correct back to front order.

- [ ] TODO: Expand implementation details.

## Environment

This project requires you to have an NVIDIA GPU with the ability to interop between CUDA and OpenGL.
Thus, WSL is [not supported](https://docs.nvidia.com/cuda/wsl-user-guide/index.html#features-not-yet-supported) and OSX (MacOS) is not supported.
Tested on Linux and Windows.

For offline rendering (the drop-in replacement of the original CUDA rasterizer), we also need a valid EGL environment.
It can sometimes be hard to set up for virtualized machines. [Potential fix](https://github.com/zju3dv/4K4D/issues/27#issuecomment-2026747401).

## Credits

Inspired by those insanely fast WebGL-based 3DGS viewers:

- [GaussianSplats3D](https://github.com/mkkellogg/GaussianSplats3D) for inspiring our vertex-geometry-fragment shader pipeline.
- [gsplat.tech](https://gsplat.tech/).
- [splat](https://github.com/antimatter15/splat).

Using the algorithm and improvements from:

- [diff-gaussian-rasterization](https://github.com/graphdeco-inria/diff-gaussian-rasterization) for the main Gaussian Splatting algorithm.
- [diff_gauss](https://github.com/dendenxu/diff-gaussian-rasterization) for the fixed culling.

CUDA-GL interop & EGL environment inspired by:

- [4K4D](https://zju3dv.github.io/4k4d) where they(I) used the interop for depth-peeling.
- [EasyVolcap](https://github.com/zju3dv/EasyVolcap) for the collection of utilities, including EGL setup.
- [nvdiffrast](https://nvlabs.github.io/nvdiffrast) for their EGL context setup and CUDA-GL interop setup.

## Citation

```bibtex
@misc{fast_gauss,  
    title = {Fast Gaussian Rasterization},
    howpublished = {GitHub},  
    year = {2024},
    url = {https://github.com/dendenxu/fast-gaussian-rasterization}
}
```
