BachiLi / redner

Differentiable rendering without approximation.
https://people.csail.mit.edu/tzumao/diffrt/
MIT License
1.38k stars 139 forks source link

Quad Light GPU issue? #112

Closed MatisHudon closed 4 years ago

MatisHudon commented 4 years ago

I am trying to run the following code (taken from the example) I installed redner-gpu via pip. If I run the code selecting the cpu as device everything runs smoothly, whenever I run with cuda I have a segmentation fault. Also if I use an environment map everything runs well both with cuda and cpu. Whenever I try to use cuda and a quad_light there is a segmentation fault. Any idea of what is happening?

import torch
import pyredner
import matplotlib.pyplot as plt

device = torch.device('cpu')
pyredner.set_device(device)
# The steps arguments decide how many triangles are used to represent the sphere.
vertices, indices, uvs, normals = pyredner.generate_sphere(theta_steps = 64, phi_steps = 128)

m = pyredner.Material(diffuse_reflectance = torch.tensor((0.5, 0.5, 0.5), device = pyredner.get_device()))

obj = pyredner.Object(vertices = vertices, indices = indices, uvs = uvs, normals = normals, material = m)
cam = pyredner.automatic_camera_placement([obj], resolution = (480, 640))

# Put the light source behind the camera, so that it does not block the view.
light = pyredner.generate_quad_light(position = cam.position.to(pyredner.get_device()) + torch.tensor([0.0, 0.0, -1.0], device = pyredner.get_device()),
                                     look_at = torch.zeros(3, device = pyredner.get_device()),
                                     size = torch.tensor([0.1, 0.1], device = pyredner.get_device()),
                                     intensity = torch.tensor([5000.0, 5000.0, 5000.0], device = pyredner.get_device()))
scene = pyredner.Scene(objects = [obj, light], camera = cam)
img = pyredner.render_pathtracing(scene)
plt.imshow(torch.pow(img, 1.0/2.2).cpu())
plt.show()
BachiLi commented 4 years ago

Thanks for reporting! This is fixed by https://github.com/BachiLi/redner/commit/b5843bb9c7b2212a26b5bbd4805a0167d8f7273c

Redner assumes the intensity tensor being stored in CPU memory for faster access (since it is just a size 3 vector). I fixed it by always converting it to a CPU pytorch tensor before rendering. It would probably be slightly faster if you directly allocate it on CPU on your side.

MatisHudon commented 4 years ago

Brilliant, thank you!