AcademySoftwareFoundation / OpenShadingLanguage

Advanced shading language for production GI renderers
BSD 3-Clause "New" or "Revised" License
2.05k stars 347 forks source link

Build error on MSVC with Partio #1755

Closed rasmusbonnedal closed 7 months ago

rasmusbonnedal commented 7 months ago

Problem

When OSL builds with Partio it gives an error on MSVC: https://gist.github.com/rasmusbonnedal/62c64d076a764b2f7c2ac07189501e49

This issue came when Pointcloud was marked OSLEXECPUBLIC because then MSVC needs all symbols of all members resolved. This makes it impossible to use unique_ptr in an STL container because the container has operations that access deleted members of the unique_ptr. (https://stackoverflow.com/questions/51033325/dll-exporting-causing-issues-with-unique-pointers). PointCloud has a member m_attributes of type std::unordered_map<ustringhash, std::unique_ptr<Partio::ParticleAttribute>> which causes the problem.

Could the unique_ptr be replaced with a shared_ptr?

Steps to Reproduce

  1. Build on Windows/MSVC with Partio enabled

Versions

AlexMWells commented 7 months ago

Thanks for the stackoverflow link, they make the point that we may not need the default copy/move constructor or assignment operator and explictly deleting them may resolve the need for those private methods to be accessed.

I'll see if I can mock a reproducer in godbolt to confirm.

AlexMWells commented 7 months ago

Reproducer with example of fix (delete default copy constructors and assignment operators): https://www.godbolt.org/z/1GT478hE8

So to fix this just add the following to pointcould.h

    PointCloud(const PointCloud&) = delete;
    PointCloud(const PointCloud&&) = delete;
    PointCloud & operator=(const PointCloud&) = delete;
    PointCloud & operator=(const PointCloud&&) = delete;
rasmusbonnedal commented 7 months ago

Oh, that's a nice solution!

rasmusbonnedal commented 7 months ago

BTW, I think SortedPointRecord on pointcloud.h:58 should be std::pair<float, Partio::ParticleIndex> which gets rid of a warning.

AlexMWells commented 7 months ago

I'll submit a PR for this.

AlexMWells commented 7 months ago

BTW, I think SortedPointRecord on pointcloud.h:58 should be std::pair<float, Partio::ParticleIndex> which gets rid of a warning.

I believe it might have been intentionally an int vs. the Partio::ParticleIndex which is size_t to improve sorting performance. So choose to fix with adding proper static cast when constucting the SortedPointRecord.

Of course if the particle index > MAX_INT that would be a problem. Do we expect point clouds > 2 billion entries? If so... then maybe should be Partio::ParticleIndex. @lgritz , thoughts?

lgritz commented 7 months ago

I think it should probably be ParticleIndex. I wonder if years ago, ParticleIndex was int and later changed to size_t?