Page MenuHome

Mesh: Move material indices to a generic attribute
ClosedPublic

Authored by Hans Goudey (HooglyBoogly) on Aug 12 2022, 6:01 PM.

Details

Summary

This patch moves material indices from the mesh MPoly struct to a
generic integer attribute. The builtin material index was already
exposed in geometry nodes, but this makes it a "proper" attribute
accessible with Python and visible in the "Attributes" panel.

The goals of the refactor are code simplification and memory and
performance improvements, mainly because the attribute doesn't have
to be stored and processed if there are no materials. However, until
4.0, material indices will still be read and written in the old
format, meaning there may be a temporary increase in memory usage.

Further notes:

  • Completely removing the MPoly.mat_nr after 4.0 may require changes to DNA or introducing a new MPoly type.
  • Geometry nodes regression tests didn't look at material indices, so the change reveals a bug in the realize instances node that I fixed.
  • Access to material indices from the RNA MeshPolygon type is slower with this patch. The material_index attribute can be used instead.
  • Cycles is changed to read from the attribute instead.
  • BMesh isn't changed in this patch. Theoretically it could be though, to save 2 bytes per face when no materials are used.
  • Eventually we could use a 16 bit integer attribute type instead.

Ref T95967

Branch: refactor-mesh-material-index-generic

Diff Detail

Repository
rB Blender

Event Timeline

Hans Goudey (HooglyBoogly) requested review of this revision.Aug 12 2022, 6:01 PM
Hans Goudey (HooglyBoogly) created this revision.
Hans Goudey (HooglyBoogly) edited the summary of this revision. (Show Details)

Compiling, basics working

Hans Goudey (HooglyBoogly) retitled this revision from Mesh: Move material indices to a generic attribute (WIP) to Mesh: Move material indices to a generic attribute.
Hans Goudey (HooglyBoogly) edited the summary of this revision. (Show Details)
  • Add comment
  • Fix legacy conversion
  • Fix crash in bmesh to mesh conversion
  • Use attribute API for retrieving material indices in Cycles
  • Fix empty material mapping in the realize instances node
  • Cleanup
  • Fix realize instances node
  • Avoid retrieving material indices many times in draw code
  • Temporarily disable layer count mismatch test
  • Fix typo causing crash
  • Fix: Account for null material indices
  • Cleanup: Use attribute API
  • Cleanup: Add using namespaces
  • Cleanup: Remove unnecessary subsurf DerivedMesh material index handling
  • Fix old attribute collision warning
  • Fix missing BMesh table
  • Fix last use of deprecated material index struct member
  • Fix build error
  • Compiling, basics working

This patch is mostly just like the other similar refactors. But maybe @Brecht Van Lommel (brecht) wants to take a look at the Cycles change.
And maybe Jacques is interested in a the few geometry nodes changes. The realize instances node is the most complex change.

Brecht Van Lommel (brecht) requested changes to this revision.Aug 29 2022, 3:22 PM
Brecht Van Lommel (brecht) added inline comments.
source/blender/blenkernel/intern/mesh.cc
1422

Should this ensure the attribute is the on the face domain?

1448

Wrong domain

1461

Should this ensure the attribute is the on the face domain?

source/blender/blenkernel/intern/pbvh.c
151

This should be if (pbvh->material_indices[a] != pbvh->material_indices[b]) return false;.

Otherwise it skips the smooth test.

This revision now requires changes to proceed.Aug 29 2022, 3:22 PM
Hans Goudey (HooglyBoogly) marked 4 inline comments as done.
  • Fix face materials match PBVH check
  • Use correct domain
  • Add an assert that material indices are on the correct domain, don't crash
  • Merge branch 'master' into refactor-mesh-material-index-generic
source/blender/blenkernel/intern/mesh.cc
1422

The attribute system (create_attribute_providers_for_mesh) ensures that doesn't happen. But the conversion from BMesh to Mesh doesn't use that system, so I guess it doesn't hurt to be safe for now.

This revision is now accepted and ready to land.Aug 30 2022, 10:45 AM
Jacques Lucke (JacquesLucke) requested changes to this revision.Aug 30 2022, 1:14 PM

Tests fail currently, so that should be fixed.

This revision now requires changes to proceed.Aug 30 2022, 1:14 PM

Merge master

Tests are passing locally, so I'm not sure what's up with that. I'll try running them again with this patch.

https://builder.blender.org/admin/#/builders/57/builds/1475

  • Fix cycles retrieval of material indices (and tests)

I wasn't compiling with Freestyle, which disabled some tests.

This revision is now accepted and ready to land.Aug 31 2022, 3:26 PM