forked from pierotofy/OpenSplat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasterize_gaussians.cpp
142 lines (118 loc) · 5.22 KB
/
rasterize_gaussians.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "rasterize_gaussians.hpp"
#include "vendor/gsplat/bindings.h"
#include "vendor/gsplat/config.h"
std::tuple<torch::Tensor,
torch::Tensor,
torch::Tensor,
torch::Tensor,
torch::Tensor> binAndSortGaussians(int numPoints, int numIntersects,
torch::Tensor xys,
torch::Tensor depths,
torch::Tensor radii,
torch::Tensor cumTilesHit,
TileBounds tileBounds){
auto t = map_gaussian_to_intersects_tensor(numPoints, numIntersects,
xys, depths, radii, cumTilesHit, tileBounds);
// unique IDs for each gaussian in the form (tile | depth id)
torch::Tensor isectIds = std::get<0>(t);
// Tensor that maps isect_ids back to cumHitTiles
torch::Tensor gaussianIds = std::get<1>(t);
auto sorted = torch::sort(isectIds);
// sorted unique IDs for each gaussian in the form (tile | depth id)
torch::Tensor isectIdsSorted = std::get<0>(sorted);
torch::Tensor sortedIndices = std::get<1>(sorted);
// sorted Tensor that maps isect_ids back to cumHitTiles
torch::Tensor gaussianIdsSorted = torch::gather(gaussianIds, 0, sortedIndices);
// range of gaussians hit per tile
torch::Tensor tileBins = get_tile_bin_edges_tensor(numIntersects, isectIdsSorted);
return std::make_tuple(isectIds, gaussianIds, isectIdsSorted, gaussianIdsSorted, tileBins);
}
torch::Tensor RasterizeGaussians::forward(AutogradContext *ctx,
torch::Tensor xys,
torch::Tensor depths,
torch::Tensor radii,
torch::Tensor conics,
torch::Tensor numTilesHit,
torch::Tensor colors,
torch::Tensor opacity,
int imgHeight,
int imgWidth,
torch::Tensor background
){
int numPoints = xys.size(0);
TileBounds tileBounds = std::make_tuple(
(imgWidth + BLOCK_X - 1) / BLOCK_X,
(imgHeight + BLOCK_Y - 1) / BLOCK_Y,
1
);
std::tuple<int, int, int> block = std::make_tuple(BLOCK_X, BLOCK_Y, 1);
std::tuple<int, int, int> imgSize = std::make_tuple(imgWidth, imgHeight, 1);
torch::Tensor cumTilesHit = torch::cumsum(numTilesHit, 0, torch::kInt32);
int numIntersects = cumTilesHit[cumTilesHit.size(0) - 1].item<int>();
auto b = binAndSortGaussians(numPoints, numIntersects, xys, depths, radii, cumTilesHit, tileBounds);
torch::Tensor gaussianIdsSorted = std::get<3>(b);
torch::Tensor tileBins = std::get<4>(b);
auto t = rasterize_forward_tensor(tileBounds, block, imgSize,
gaussianIdsSorted,
tileBins,
xys,
conics,
colors,
opacity,
background);
// Final image
torch::Tensor outImg = std::get<0>(t);
// Map of alpha-inverse (1 - finalTs = alpha)
torch::Tensor finalTs = std::get<1>(t);
// Map of tile bin IDs
torch::Tensor finalIdx = std::get<2>(t);
ctx->saved_data["imgWidth"] = imgWidth;
ctx->saved_data["imgHeight"] = imgHeight;
ctx->save_for_backward({ gaussianIdsSorted, tileBins, xys, conics, colors, opacity, background, finalTs, finalIdx });
return outImg;
}
tensor_list RasterizeGaussians::backward(AutogradContext *ctx, tensor_list grad_outputs) {
torch::Tensor v_outImg = grad_outputs[0];
int imgHeight = ctx->saved_data["imgHeight"].toInt();
int imgWidth = ctx->saved_data["imgWidth"].toInt();
variable_list saved = ctx->get_saved_variables();
torch::Tensor gaussianIdsSorted = saved[0];
torch::Tensor tileBins = saved[1];
torch::Tensor xys = saved[2];
torch::Tensor conics = saved[3];
torch::Tensor colors = saved[4];
torch::Tensor opacity = saved[5];
torch::Tensor background = saved[6];
torch::Tensor finalTs = saved[7];
torch::Tensor finalIdx = saved[8];
// torch::Tensor v_outAlpha = torch::zeros({imgHeight, imgWidth}, torch::TensorOptions().device(v_outImg.get_device());
torch::Tensor v_outAlpha = torch::zeros_like(v_outImg.index({"...", 0}));
auto t = rasterize_backward_tensor(imgHeight, imgWidth,
gaussianIdsSorted,
tileBins,
xys,
conics,
colors,
opacity,
background,
finalTs,
finalIdx,
v_outImg,
v_outAlpha);
torch::Tensor v_xy = std::get<0>(t);
torch::Tensor v_conic = std::get<1>(t);
torch::Tensor v_colors = std::get<2>(t);
torch::Tensor v_opacity = std::get<3>(t);
torch::Tensor none;
return { v_xy,
none, // depths
none, // radii
v_conic,
none, // numTilesHit
v_colors,
v_opacity,
none, // imgHeight
none, // imgWidth
none // background
};
}