-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrenderer.py
160 lines (143 loc) · 5.08 KB
/
renderer.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from typing import Any
import torch
import gaussian
from torch.autograd.function import once_differentiable
class _Drawer(torch.autograd.Function):
@staticmethod
def forward(
ctx,
gaussians_pos,
gaussians_rgb,
gaussians_opa,
gaussians_cov,
tile_n_point_accum,
padded_height,
padded_width,
focal_x,
focal_y,
render_weight_normalize=False,
sigmoid=False,
use_sh_coeff=False,
fast=False,
rays_o=None,
lefttop_pos=None,
vec_dx=None,
vec_dy=None
):
rendered_image = torch.zeros(padded_height, padded_width, 3, device=gaussians_pos.device, dtype=torch.float32)
gaussian.draw(
gaussians_pos,
gaussians_rgb,
gaussians_opa,
gaussians_cov,
tile_n_point_accum,
rendered_image,
focal_x,
focal_y,
render_weight_normalize,
sigmoid,
fast,
rays_o,
lefttop_pos,
vec_dx,
vec_dy,
use_sh_coeff,
)
ctx.save_for_backward(gaussians_pos, gaussians_rgb, gaussians_opa, gaussians_cov, tile_n_point_accum, rendered_image, rays_o, lefttop_pos, vec_dx, vec_dy)
ctx.focal_x = focal_x
ctx.focal_y = focal_y
ctx.render_weight_normalize = render_weight_normalize
ctx.sigmoid = sigmoid
ctx.fast = fast
ctx.use_sh_coeff = use_sh_coeff
return rendered_image
@staticmethod
def backward(ctx, grad_output):
# grad_output
gaussians_pos, gaussians_rgb, gaussians_opa, gaussians_cov, tile_n_point_accum, rendered_image, rays_o, lefttop_pos, vec_dx, vec_dy = ctx.saved_tensors
grad_pos = torch.zeros_like(gaussians_pos)
grad_rgb = torch.zeros_like(gaussians_rgb)
grad_opa = torch.zeros_like(gaussians_opa)
grad_cov = torch.zeros_like(gaussians_cov)
gaussian.draw_backward(
gaussians_pos,
gaussians_rgb,
gaussians_opa,
gaussians_cov,
tile_n_point_accum,
rendered_image,
grad_output,
grad_pos,
grad_rgb,
grad_opa,
grad_cov,
ctx.focal_x,
ctx.focal_y,
ctx.render_weight_normalize,
ctx.sigmoid,
ctx.fast,
rays_o,
lefttop_pos,
vec_dx,
vec_dy,
ctx.use_sh_coeff,
)
return grad_pos, grad_rgb, grad_opa, grad_cov, None, None, None, None, None, None, None, None, None, None, None, None, None
draw = _Drawer.apply
class _trunc_exp(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
return torch.exp(x)
@staticmethod
def backward(ctx, g):
x = ctx.saved_tensors[0]
return g * torch.exp(x.clamp(-1, 1))
trunc_exp = _trunc_exp.apply
class _world2camera(torch.autograd.Function):
@staticmethod
def forward(ctx, pos, rot, tran):
ctx.save_for_backward(rot)
res = torch.zeros_like(pos)
gaussian.world2camera(pos, rot, tran, res)
return res
@staticmethod
def backward(ctx, grad_out):
rot = ctx.saved_tensors[0]
grad_inp = torch.zeros_like(grad_out)
gaussian.world2camera_backward(grad_out, rot, grad_inp)
return grad_inp, None, None
world2camera_func = _world2camera.apply
class _GlobalCulling(torch.autograd.Function):
@staticmethod
def forward(ctx, pos, quat, scale, current_rot, current_tran, near, half_width, half_height):
res_pos = torch.zeros_like(pos)
res_cov = torch.zeros((pos.shape[0], 2, 2), device=pos.device)
culling_mask = torch.zeros(pos.shape[0], dtype=torch.long, device=pos.device)
gaussian.global_culling(
pos, quat, scale, current_rot, current_tran, res_pos, res_cov, culling_mask, near, half_width, half_height
)
ctx.save_for_backward(culling_mask, pos, quat, scale, current_rot, current_tran)
return res_pos, res_cov, culling_mask.detach()
@staticmethod
def backward(ctx, gradout_pos, gradout_cov, grad_culling_mask):
culling_mask = ctx.saved_tensors[0]
pos = ctx.saved_tensors[1]
quat = ctx.saved_tensors[2]
scale = ctx.saved_tensors[3]
current_rot = ctx.saved_tensors[4]
current_tran = ctx.saved_tensors[5]
gradinput_pos = torch.zeros_like(gradout_pos)
gradinput_quat = torch.zeros((gradout_pos.shape[0], 4), device=gradout_pos.device)
gradinput_scale = torch.zeros((gradout_pos.shape[0], 3), device=gradout_pos.device)
gaussian.global_culling_backward(
pos, quat, scale, current_rot, current_tran,
gradout_pos,
gradout_cov,
culling_mask,
gradinput_pos,
gradinput_quat,
gradinput_scale,
)
return gradinput_pos, gradinput_quat, gradinput_scale, None, None, None, None, None
global_culling = _GlobalCulling.apply