-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlt.py
189 lines (132 loc) · 5.34 KB
/
mlt.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import mitsuba as mi
import drjit as dr
import matplotlib.pyplot as plt
mi.set_variant("cuda_ad_rgb")
from pathrecord import Path, drjitstruct # noqa
@drjitstruct
class PVert:
wo: mi.Vector3f
f: mi.Spectrum
def __init__(self, wo=mi.Vector3f(), f=mi.Spectrum()):
self.wo = wo
self.f = f
class MltSampler:
pass
class Simple(mi.SamplingIntegrator):
def __init__(self, props=mi.Properties()):
super().__init__(props)
self.max_depth = props.get("max_depth")
self.rr_depth = props.get("rr_depth")
def render(self: mi.Integrator, scene: mi.Scene, sensor: mi.Sensor, seed: int = 0, spp: int = 0, develop: bool = True, evaluate: bool = True) -> dr.scalar.TensorXf:
film = sensor.film()
sampler = sensor.sampler()
spp = sampler.sample_count()
self.spp = spp
film_size = film.crop_size()
n_chanels = film.prepare(self.aov_names())
self.n_chanels = n_chanels
wavefront_size = film_size.x * film_size.y
# sampler.set_samples_per_wavefront()
sampler.seed(0, wavefront_size)
block: mi.ImageBlock = film.create_block()
block.set_offset(film.crop_offset())
idx = dr.arange(mi.UInt32, wavefront_size)
pos = mi.Vector2f()
pos.y = idx // film_size[0]
pos.x = idx % film_size[0]
pos += film.crop_offset()
aovs = [mi.Float(0)] * n_chanels
path = Path(wavefront_size, self.max_depth, dtype=PVert)
print(spp)
for i in range(spp):
self.render_sample(scene, sensor, sampler,
block, aovs, pos, path, idx)
# Trigger kernel launch
sampler.advance()
sampler.schedule_state()
dr.eval(path.vertices)
dr.eval(block.tensor())
film.put_block(block)
result = film.develop()
dr.schedule(result)
dr.eval()
return result
def render_sample(self, scene: mi.Scene, sensor: mi.Sensor, sampler: mi.Sampler, block: mi.ImageBlock, aovs, pos: mi.Vector2f, path: Path, idx: mi.UInt32, active=True):
film = sensor.film()
scale = 1. / mi.Vector2f(film.crop_size())
offset = - mi.Vector2f(film.crop_offset())
sample_pos = pos + offset + sampler.next_2d()
time = 1.
s1, s3 = sampler.next_1d(), sampler.next_2d()
ray, ray_weight = sensor.sample_ray(time, s1, sample_pos * scale, s3)
medium = sensor.medium()
active = mi.Bool(True)
(spec, mask, aov) = self.sample(
scene, sampler, ray, path, idx, medium, active)
spec = ray_weight * spec
rgb = mi.Color3f()
if mi.is_spectral:
rgb = mi.spectrum_list_to_srgb(spec, ray.wavelengths, active)
elif mi.is_monochromatic:
rgb = spec.x
else:
rgb = spec
# Debug:
aovs[0] = rgb.x
aovs[1] = rgb.y
aovs[2] = rgb.z
aovs[3] = 1.
block.put(sample_pos, aovs)
def sample(self, scene: mi.Scene, sampler: mi.Sampler, ray_: mi.RayDifferential3f, path: Path, idx: mi.UInt32, medium: mi.Medium = None, active: mi.Bool = True):
bsdf_ctx = mi.BSDFContext()
ray = mi.Ray3f(ray_)
depth = mi.UInt32(0)
f = mi.Spectrum(1.)
L = mi.Spectrum(0.)
active = mi.Bool(active)
prev_si = dr.zeros(mi.SurfaceInteraction3f)
loop = mi.Loop(name="Path Tracing", state=lambda: (
sampler, ray, depth, f, L, active, prev_si))
loop.set_max_iterations(self.max_depth)
while loop(active):
pvert: PVert = path[depth]
wo_new = dr.erfinv(mi.warp.square_to_uniform_sphere(
sampler.next_2d()))-pvert.wo
si: mi.SurfaceInteraction3f = scene.ray_intersect(
ray, ray_flags=mi.RayFlags.All, coherent=dr.eq(depth, 0))
bsdf: mi.BSDF = si.bsdf(ray)
# Direct emission
ds = mi.DirectionSample3f(scene, si=si, ref=prev_si)
Le = f * ds.emitter.eval(si)
active_next = (depth + 1 < self.max_depth) & si.is_valid()
# BSDF Sampling
bsdf_smaple, bsdf_val = bsdf.sample(
bsdf_ctx, si, sampler.next_1d(), sampler.next_2d(), active_next)
# Update loop variables
path[depth] = PVert(bsdf_smaple.wo, bsdf_val)
ray = si.spawn_ray(si.to_world(bsdf_smaple.wo))
L = (L + Le)
f *= bsdf_val
prev_si = dr.detach(si, True)
# Stopping criterion (russian roulettte)
active_next &= dr.neq(dr.max(f), 0)
rr_prop = dr.maximum(f.x, dr.maximum(f.y, f.z))
rr_prop[depth < self.rr_depth] = 1.
f *= dr.rcp(rr_prop)
active_next &= (sampler.next_1d() < rr_prop)
active = active_next
depth += 1
return (L, dr.neq(depth, 0), [])
mi.register_integrator("integrator", lambda props: Simple(props))
scene = mi.cornell_box()
scene['integrator']['type'] = 'integrator'
scene['integrator']['max_depth'] = 16
scene['integrator']['rr_depth'] = 2
scene['sensor']['sampler']['sample_count'] = 16
scene['sensor']['film']['width'] = 1024
scene['sensor']['film']['height'] = 1024
scene = mi.load_dict(scene)
img = mi.render(scene)
plt.imshow(img ** (1. / 2.2))
plt.axis("off")
plt.show()