Page MenuHome

Attributes: Improve custom data initialization options
ClosedPublic

Authored by Hans Goudey (HooglyBoogly) on Aug 4 2022, 4:54 AM.

Details

Summary

When allocating new CustomData layers, often we do redundant
initialization of arrays. For example, it's common that values are
allocated, set to their default value, and then set to some other
value. This is wasteful, and it negates the benefits of optimizations
to the allocator like D15082. There are two reasons for this. The
first is array-of-structs storage that makes it annoying to initialize
values manually, and the second is confusing options in the Custom Data
API. This patch addresses the latter.

The CustomData "alloc type" options are rearranged. Besides
the options that use existing layers, there are two remaining:

  • CD_SET_DEFAULT sets the default value.
    • Usually zeroes, but for colors this is white (how it was before).
    • Should be used when you add the layer but don't set all values.
  • CD_CONSTRUCT refers to the "default construct" C++ term.
    • Only necessary or defined for non-trivial types like vertex groups.
    • Doesn't do anything for trivial types like int or float3.
    • Should be used every other time, when all values will be set.

The attribute API's AttributeInit types are updated as well.

The patch doesn't name any functional changes yet. Follow-up commits
will change code from CD_SET_DEFAULT to CD_CONSTRUCT where the
correctness is clear.

Diff Detail

Repository
rB Blender

Event Timeline

Hans Goudey (HooglyBoogly) requested review of this revision.Aug 4 2022, 4:54 AM
Hans Goudey (HooglyBoogly) created this revision.
Hans Goudey (HooglyBoogly) edited the summary of this revision. (Show Details)
source/blender/blenkernel/BKE_attribute.hh
92–93

Looks like this uses CD_CONSTRUCT internally, making the comment kind of wrong. Better not use "none" here.

source/blender/blenkernel/BKE_customdata.h
64

This could have a more specific comment that explains what happens with trivial types.

65

Should also have an integer value for consistency.

source/blender/blenkernel/intern/customdata.cc
157–165

Should specify that data is expected to be uninitialized, same below.

Hans Goudey (HooglyBoogly) marked 4 inline comments as done.
Hans Goudey (HooglyBoogly) edited the summary of this revision. (Show Details)

Improve naming and comments

Hans Goudey (HooglyBoogly) retitled this revision from Attributes: Clarify and improve custom data initialization to Attributes: Improve custom data initialization options.

Fix diff (forgot to save file)

Jacques Lucke (JacquesLucke) requested changes to this revision.Aug 30 2022, 1:40 PM
Jacques Lucke (JacquesLucke) added inline comments.
source/blender/blenkernel/BKE_customdata.h
57–58

Mention that the default is zero for most types.

source/blender/io/wavefront_obj/tests/obj_importer_tests.cc
108

This looks unrelated and wrong.

This revision now requires changes to proceed.Aug 30 2022, 1:40 PM
  • Fix stray code from another branch
  • Improve comment
This revision is now accepted and ready to land.Aug 30 2022, 9:31 PM