README.md 2.67 KB
Newer Older
1
# DRTK – Differentiable Rendering Toolkit
facebook-github-bot's avatar
facebook-github-bot committed
2
3
4

This package is a PyTorch library that provides functionality for differentiable rasterization.

5
It consists of five main components:
facebook-github-bot's avatar
facebook-github-bot committed
6
7
8
9
10
11
12
13
14

* **transform**
* **rasterize**
* **render**
* **interpolate**
* **edge_grad**

There are also optional components such as **msi** and **mipmap_grid_sampler**. New components may be added in the future.

15
A typical flow looks like this:
facebook-github-bot's avatar
facebook-github-bot committed
16

17
**transform****rasterize****render****interpolate****CUSTOM SHADING****edge_grad**
facebook-github-bot's avatar
facebook-github-bot committed
18
19
20
21
22
23

where:
- **transform**: projects the vertex positions from camera space to image space
- **rasterize**: performs rasterization, where pixels in the output image are associated with triangles
- **render**: computes depth and baricentric image
- **interpolate**: interpolates arbitrary vertex attributes
24
25
- **CUSTOM SHADING**: user implemented shading
- **edge_grad**: special module that computes gradients for the **rasterize** step, which is not differentiable on its own. For details, please see [**Rasterized Edge Gradients: Handling Discontinuities Differentiably**](https://arxiv.org/abs/2405.02508)
facebook-github-bot's avatar
facebook-github-bot committed
26

Gabe Schwartz's avatar
Gabe Schwartz committed
27
28
## Hello Triangle
The "Hello Triangle" with DRTK would look like this:
facebook-github-bot's avatar
facebook-github-bot committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
```python
import drtk
import torch as th
from torchvision.utils import save_image  # to save images

# create vertex buffer of shape [1 x n_vertices x 3], here for triangle `n_vertices` == 3
v = th.as_tensor([[[0, 511, 1], [255, 0, 1], [511, 511, 1]]]).float().cuda()

# create index buffer
vi = th.as_tensor([[0, 1, 2]]).int().cuda()

# rasterize
index_img = drtk.rasterize(v, vi, height=512, width=512)

# compute baricentrics
_, bary = drtk.render(v, vi, index_img)

# we won't do shading, we'll just save the baricentrics and filter out the empty region
# which is marked with `-1` in `index_img`
img = bary * (index_img != -1)

save_image(img, "render.png")
```

Gabe Schwartz's avatar
Gabe Schwartz committed
53
![hello triangle](doc/hellow_triangle.png)
facebook-github-bot's avatar
facebook-github-bot committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

## Dependencies
* PyTorch >= 2.1.0

## Building

To build a wheel and install it:
```
pip install wheel
python setup.py  bdist_wheel
pip install dist/drtk-<wheel_name>.whl
```

To build inplace, which is useful for package development:
```
python setup.py build_ext --inplace -j 1
```

## Contributing

See the [CONTRIBUTING](CONTRIBUTING.md) file for how to help out.

## License
Stanislav Pidhorskyi's avatar
Stanislav Pidhorskyi committed
77
DRTK is MIT licensed, as found in the [LICENSE](LICENSE) file.
facebook-github-bot's avatar
facebook-github-bot committed
78
79

## Citation
80
81
82
83
84
85
86
```bibtex
@mish{pidhorskyi2024rasterized,
  title        = {Rasterized Edge Gradients: Handling Discontinuities Differentiably},
  author       = {Pidhorskyi, Stanislav and Simon, Tomas and Schwartz, Gabriel and Wen, He and Sheikh, Yaser and Saragih, Jason},
  howpublished = {arXiv preprint},
  year         = {2024},
  note         = {arXiv:2405.02508}
facebook-github-bot's avatar
facebook-github-bot committed
87
88
}
```