2525#include < mutex>
2626#include < string>
2727#include < tuple>
28+ #include < unordered_map>
2829
2930#include " open3d/core/CUDAUtils.h"
3031#include " open3d/core/Device.h"
@@ -1945,8 +1946,7 @@ Image TriangleMesh::ComputeAmbientOcclusion(int tex_width,
19451946 }
19461947
19471948 // Bake positions and normals into textures.
1948- const float margin =
1949- (tex_width + 511 .f ) / 512 .f ; // = ceil(tex_width / 512)
1949+ const float margin = (tex_width + 511 ) / 512 ; // = ceil(tex_width / 512)
19501950 auto baked_textures =
19511951 BakeVertexAttrTextures (tex_width, {" positions" , " normals" }, margin,
19521952 /* fill=*/ 0.0 , /* update_material=*/ false );
@@ -1957,7 +1957,7 @@ Image TriangleMesh::ComputeAmbientOcclusion(int tex_width,
19571957
19581958 RaycastingScene rcs;
19591959 // TODO: Reuse RCS from BakeVertexAttrTextures() if possible.
1960- rcs.AddTriangles (*this );
1960+ rcs.AddTriangles (*this );
19611961
19621962 const int64_t n_pixels = tex_width * tex_width;
19631963
@@ -2234,12 +2234,15 @@ Image TriangleMesh::TransformNormalMap(const Image &normal_map,
22342234 " Use ComputeVertexNormals() and ComputeTangentSpace() to "
22352235 " compute them." );
22362236 }
2237-
22382237 if (normal_map.GetChannels () != 3 ) {
22392238 utility::LogError (" Normal map must have 3 channels, but has {}." ,
22402239 normal_map.GetChannels ());
22412240 }
2242-
2241+ const core::Device device = GetDevice ();
2242+ if (normal_map.GetDevice () != device) {
2243+ utility::LogError (" Normal map device {} does not match mesh device {}." ,
2244+ normal_map.GetDevice ().ToString (), device.ToString ());
2245+ }
22432246 int tex_width = normal_map.GetCols ();
22442247 int tex_height = normal_map.GetRows ();
22452248 if (tex_width != tex_height) {
@@ -2250,8 +2253,7 @@ Image TriangleMesh::TransformNormalMap(const Image &normal_map,
22502253 }
22512254
22522255 // Bake TBN vectors into textures.
2253- const float margin =
2254- (tex_width + 511 .f ) / 512 .f ; // = ceil(tex_width / 512)
2256+ const float margin = (tex_width + 511 ) / 512 ; // = ceil(tex_width / 512)
22552257 if (!HasMaterial ()) {
22562258 SetMaterial (visualization::rendering::Material ());
22572259 GetMaterial ().SetDefaultProperties (); // defaultLit
@@ -2260,18 +2262,19 @@ Image TriangleMesh::TransformNormalMap(const Image &normal_map,
22602262 std::unordered_map<std::string, core::Tensor> baked_textures;
22612263 if (!(GetMaterial ().HasTextureMap (" normals" ) &&
22622264 GetMaterial ().HasTextureMap (" tangents" ))) {
2265+ // BakeVertexAttrTextures requires CPU; move results back to the mesh
2266+ // device for all subsequent computation.
22632267 baked_textures = To (core::Device (" CPU:0" ))
22642268 .BakeVertexAttrTextures (
22652269 tex_width, {" normals" , " tangents" },
22662270 margin, /* fill=*/ 0.0 , false );
2271+ baked_textures[" normals" ] = baked_textures[" normals" ].To (device);
2272+ baked_textures[" tangents" ] = baked_textures[" tangents" ].To (device);
22672273 } else {
22682274 baked_textures[" normals" ] =
2269- GetMaterial ().GetTextureMap (" normals" ).AsTensor ().To (
2270- core::Device (" CPU:0" ));
2271- baked_textures[" tangents" ] = GetMaterial ()
2272- .GetTextureMap (" tangents" )
2273- .AsTensor ()
2274- .To (core::Device (" CPU:0" ));
2275+ GetMaterial ().GetTextureMap (" normals" ).AsTensor ().To (device);
2276+ baked_textures[" tangents" ] =
2277+ GetMaterial ().GetTextureMap (" tangents" ).AsTensor ().To (device);
22752278 }
22762279
22772280 // Baked (Interpolated) TBN vectors are NOT normalized.
@@ -2314,8 +2317,8 @@ Image TriangleMesh::TransformNormalMap(const Image &normal_map,
23142317 2 .0f -
23152318 1 .0f ;
23162319
2317- core::Tensor tangent_normals_t =
2318- core::Tensor::Empty ( {tex_height, tex_width, 3 }, core::Float32);
2320+ core::Tensor tangent_normals_t = core::Tensor::Empty (
2321+ {tex_height, tex_width, 3 }, core::Float32, device );
23192322 tangent_normals_t .Slice (2 , 0 , 1 ) =
23202323 fSign * (input_normals_t * row0).Sum ({2 }, true );
23212324 tangent_normals_t .Slice (2 , 1 , 2 ) =
0 commit comments