Prepare custom data API for attribute sharing. #95842

Closed
opened 2022-02-17 17:17:18 +01:00 by Jacques Lucke · 11 comments
Member

The custom data api in BKE_customdata.h has multiple methods to get a specific layer. For example CustomData_get_layer. The main issue is that these functions are not const correct. They always return a non-const void * and have no idea whether the caller wants to modify the data or not.

That is problematic when we want to use attribute sharing, because we have to make a copy of shared attributes when the caller wants to modify the attribute.

The solution is to split every CustomData_get_* method in two:

  • A *_for_read method that returns a const pointer, that the caller must never change.
  • A *_for_write method that returns a non-const pointer and internally makes a copy of the data if necessary.

With this refactor, the CustomData_duplicate_referenced_layer_* functions become redundant, because the *_for_write methods will make the copy already.

Unfortunately, the "convenience pointers" like Mesh.mvert make this system quite annoying and error prone:

  • The convenience pointers are not const. On a const-correctness level that implies that everyone with a non-const pointer to a mesh can also modify the vertex positions of that mesh. This is not true, because the array might be shared.
  • A first step in the right direction would be to make these pointers const. However, even with that, there is the issue that one would have to pass the mesh into *_for_write methods, so that the pointer can be updated if necessary.
  • A possibly better alternative would be to remove these convenience pointers completely and to provide a simple convenience api instead. E.g. there could be a MVert *BKE_mesh_mverts_for_write(Mesh *) and a const MVert *BKE_mesh_mvert_for_read(const Mesh *) method. This refactor is a bit ugly because the direct data access through convenience pointers is very common, but it might be worth it regardless.
The custom data api in `BKE_customdata.h` has multiple methods to get a specific layer. For example `CustomData_get_layer`. The main issue is that these functions are not const correct. They always return a non-const `void *` and have no idea whether the caller wants to modify the data or not. That is problematic when we want to use attribute sharing, because we have to make a copy of shared attributes when the caller wants to modify the attribute. The solution is to split every `CustomData_get_*` method in two: * A `*_for_read` method that returns a const pointer, that the caller must never change. * A `*_for_write` method that returns a non-const pointer and internally makes a copy of the data if necessary. With this refactor, the `CustomData_duplicate_referenced_layer_*` functions become redundant, because the `*_for_write` methods will make the copy already. Unfortunately, the "convenience pointers" like `Mesh.mvert` make this system quite annoying and error prone: * The convenience pointers are not const. On a const-correctness level that implies that everyone with a non-const pointer to a mesh can also modify the vertex positions of that mesh. This is not true, because the array might be shared. * A first step in the right direction would be to make these pointers const. However, even with that, there is the issue that one would have to pass the mesh into `*_for_write` methods, so that the pointer can be updated if necessary. * A possibly better alternative would be to remove these convenience pointers completely and to provide a simple convenience api instead. E.g. there could be a `MVert *BKE_mesh_mverts_for_write(Mesh *)` and a `const MVert *BKE_mesh_mvert_for_read(const Mesh *)` method. This refactor is a bit ugly because the direct data access through convenience pointers is very common, but it might be worth it regardless.
Author
Member

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Added subscriber: @HooglyBoogly

Added subscriber: @HooglyBoogly

Added subscriber: @JacobMerrill-1

Added subscriber: @JacobMerrill-1

Added subscriber: @GeorgiaPacific

Added subscriber: @GeorgiaPacific

Added subscriber: @hzuika

Added subscriber: @hzuika

This issue was referenced by cf69652618

This issue was referenced by cf69652618fefcd22b2cde9a2e0338b63f9a003e

This issue was referenced by e9f82d3dc7

This issue was referenced by e9f82d3dc7eebadcc52fdc43858d060c3a8214b2

This issue was referenced by 410a6efb74

This issue was referenced by 410a6efb747f188da30c75074d6bf318b862d5d5

This issue was referenced by 05952aa94d

This issue was referenced by 05952aa94d33eeb504fa63618ba35c2bcc8bd19b

This issue was referenced by 3a3d9488a1

This issue was referenced by 3a3d9488a1633bfefabbab422dd22283bca02626
Bastien Montagne added this to the Core project 2023-02-09 15:42:54 +01:00
Bastien Montagne removed this from the Core project 2023-02-09 18:20:37 +01:00
Blender Bot added
Status
Archived
and removed
Status
Confirmed
labels 2023-12-27 18:49:14 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#95842
No description provided.