WIP: Sculpt: cleanup sculpt attribute API #106920

Closed
Joseph Eagar wants to merge 7 commits from temp-sculpt-attr-api into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This PR cleans up the sculpt attribute API a bit to make greater use of C++ templates.

  • BKE_sculpt_vertex_attr_get (and it's alias SCULPT_vertex_attr_get)
    is now blender::bke::paint::vertex_attr_ptr. Same for the face
    versions of those functions. The SCULPT_XXX aliases are removed.
  • Removed code duplication; [vertex/face]_attr_ptr are now implemented via
    a generic template (elem_attr_ptr).
  • Added [vertex/face]_attr_get and [vertex/face]_attr_set functions
    to get/set attribute data without pointer wrangling.
  • Given the unwieldly length of blender::bke::paint:: it's recommended
    to do a using blender::bke::paint; at the top of sculpt files.

Example: float w = vertex_attr_get<float>(vertex, ss->attrs.automasking_factor);

This PR cleans up the sculpt attribute API a bit to make greater use of C++ templates. * `BKE_sculpt_vertex_attr_get` (and it's alias `SCULPT_vertex_attr_get`) is now `blender::bke::paint::vertex_attr_ptr`. Same for the face versions of those functions. The SCULPT_XXX aliases are removed. * Removed code duplication; `[vertex/face]_attr_ptr` are now implemented via a generic template (`elem_attr_ptr`). * Added `[vertex/face]_attr_get` and `[vertex/face]_attr_set` functions to get/set attribute data without pointer wrangling. * Given the unwieldly length of `blender::bke::paint::` it's recommended to do a `using blender::bke::paint;` at the top of sculpt files. Example: `float w = vertex_attr_get<float>(vertex, ss->attrs.automasking_factor);`
Joseph Eagar added 1 commit 2023-04-13 21:20:49 +02:00
fbf091750c Sculpt: cleanup sculpt attribute API
* `BKE_sculpt_vertex_attr_get` (and it's alias `SCULPT_vertex_attr_get`)
  is now `blender::bke::paint::vertex_attr_ptr`.  Same for the face
  versions of those functions.  The SCULPT_XXX aliases are removed.
* Removed code duplication; `[vertex/face]_attr_ptr` now just calls
  a generic template (`elem_attr_ptr`).
* Added `[vertex/face]_attr_get` and `[vertex/face]_attr_set` functions
  to get/set attribute data without pointer wrangling.
* Given the unwieldly length of `blender::bke::paint::` it's recommended
  to do a `using blender::bke::paint;` at the top of sculpt files.

Example: `float w = vertex_attr_get<float>(vertex,
ss->attrs.automasking_factor);`
Joseph Eagar requested review from Hans Goudey 2023-04-13 21:22:42 +02:00
Hans Goudey requested changes 2023-04-19 15:52:25 +02:00
Hans Goudey left a comment
Member

In isolation, this is a nice cleanup, so I think with a few changes this PR makes sense.

However, I'm generally skeptical about making an API that processes a single element at a time.
For example, these functions take a single PBVHVertRef, abstract away the PBVH type, and return a value.
Given enough templating, some of these branches can be removed, but at the cost of templates being required everywhere and generating a lot of code. A different API might be one that processes spans of indices/bmesh pointers, and values, only abstracting away the PBVH type at the node level. That makes it easier to specialize the implementation for certain types, aligns more generally with methods of removing constant checks from hot loops, and avoids the downsides of one PBVH implementation leaking into another.

That's a fairly general comment about the design of C++ usage in the sculpt/paint area though, so maybe it's better to have that conversion elsewhere.

  • Given the unwieldly length of blender::bke::paint:: it's recommended
    to do a using blender::bke::paint; at the top of sculpt files.

Generally adding using namespace at the top of files is discouraged since it makes it more difficult to see where code is coming from and makes name conflict resolution harder. I think the best solution here is to move entire files to the proper namespace when that becomes possible. Then blender:: will be unnecessary everywhere, and bke:: and ::paint will often become unnecessary too.

In isolation, this is a nice cleanup, so I think with a few changes this PR makes sense. However, I'm generally skeptical about making an API that processes a single element at a time. For example, these functions take a single `PBVHVertRef`, abstract away the PBVH type, and return a value. Given enough templating, some of these branches can be removed, but at the cost of templates being required everywhere and generating a lot of code. A different API might be one that processes spans of indices/bmesh pointers, and values, only abstracting away the PBVH type at the node level. That makes it easier to specialize the implementation for certain types, aligns more generally with methods of removing constant checks from hot loops, and avoids the downsides of one PBVH implementation leaking into another. That's a fairly general comment about the design of C++ usage in the sculpt/paint area though, so maybe it's better to have that conversion elsewhere. > * Given the unwieldly length of `blender::bke::paint::` it's recommended > to do a `using blender::bke::paint;` at the top of sculpt files. Generally adding `using namespace` at the top of files is discouraged since it makes it more difficult to see where code is coming from and makes name conflict resolution harder. I think the best solution here is to move entire files to the proper namespace when that becomes possible. Then `blender::` will be unnecessary everywhere, and `bke::` and `::paint` will often become unnecessary too.
@ -946,0 +907,4 @@
/* Base implementation for vertex_attr_*** and face_attr_*** methods.
* Returns a pointer to the attribute data (as defined by attr) for elem.
*/
template<typename Tptr, typename ElemRef = PBVHVertRef>
Member

I think it might be clearer to avoid Tptr and just use T * in the code below.

I think it might be clearer to avoid `Tptr` and just use `T *` in the code below.
@ -946,0 +910,4 @@
template<typename Tptr, typename ElemRef = PBVHVertRef>
static Tptr elem_attr_ptr(const ElemRef elem, const SculptAttribute *attr)
{
void *ptr = nullptr;
Member

Could avoid the temporary variable here and return the pointer inside the if statement.

Could avoid the temporary variable here and return the pointer inside the if statement.
@ -946,0 +937,4 @@
* Example: float *persistent_co = vertex_attr_ptr<float*>(vertex, ss->attrs.persistent_co);
*/
template<typename Tptr>
static Tptr vertex_attr_ptr(const PBVHVertRef vertex, const SculptAttribute *attr)
Member

I think exposing the pointer where a value is stored might not work well with different attribute storage methods in the future, and exposes the internal of the system a bit more than necessary. (For example, virtual arrays don't provide that ability, not that we would use them here exactly). The alternative is using a get/set combo when that's actually necessary.

I think exposing the pointer where a value is stored might not work well with different attribute storage methods in the future, and exposes the internal of the system a bit more than necessary. (For example, virtual arrays don't provide that ability, not that we would use them here exactly). The alternative is using a `get`/`set` combo when that's actually necessary.
@ -268,3 +269,3 @@
if (stroke_id != automasking->current_stroke_id) {
f = *(char *)SCULPT_vertex_attr_get(
f = *vertex_attr_ptr<char *>(
Member

Can this use vertex_attr_get instead of *vertex_attr_ptr?

Same with a few places below.

Can this use `vertex_attr_get` instead of `*vertex_attr_ptr`? Same with a few places below.
Author
Member

In isolation, this is a nice cleanup, so I think with a few changes this PR makes sense.

However, I'm generally skeptical about making an API that processes a single element at a time.
For example, these functions take a single PBVHVertRef, abstract away the PBVH type, and return a value.
Given enough templating, some of these branches can be removed, but at the cost of templates being required everywhere and generating a lot of code. A different API might be one that processes spans of indices/bmesh pointers, and values, only abstracting away the PBVH type at the node level. That makes it easier to specialize the implementation for certain types, aligns more generally with methods of removing constant checks from hot loops, and avoids the downsides of one PBVH implementation leaking into another.

That's a fairly general comment about the design of C++ usage in the sculpt/paint area though, so maybe it's better to have that conversion elsewhere.

I do in fact plan to use templates pretty much everywhere in the sculpt code. The node span approach is basically how the PBVHVertIter works, but there are times when you really do need an element-wise API. I think it's hard to avoid templates if you want clearly written, polymorphic code that can be plugged into very different implementing back-ends.

If you think about it there's a trade off between code clarity, compiled code generation size and runtime performance. You can only optimize two of those three.

> In isolation, this is a nice cleanup, so I think with a few changes this PR makes sense. > > However, I'm generally skeptical about making an API that processes a single element at a time. > For example, these functions take a single `PBVHVertRef`, abstract away the PBVH type, and return a value. > Given enough templating, some of these branches can be removed, but at the cost of templates being required everywhere and generating a lot of code. A different API might be one that processes spans of indices/bmesh pointers, and values, only abstracting away the PBVH type at the node level. That makes it easier to specialize the implementation for certain types, aligns more generally with methods of removing constant checks from hot loops, and avoids the downsides of one PBVH implementation leaking into another. > > That's a fairly general comment about the design of C++ usage in the sculpt/paint area though, so maybe it's better to have that conversion elsewhere. I do in fact plan to use templates pretty much everywhere in the sculpt code. The node span approach is basically how the `PBVHVertIter` works, but there are times when you really do need an element-wise API. I think it's hard to avoid templates if you want clearly written, polymorphic code that can be plugged into very different implementing back-ends. If you think about it there's a trade off between code clarity, compiled code generation size and runtime performance. You can only optimize two of those three.
Joseph Eagar added 1 commit 2023-05-01 22:40:29 +02:00
Joseph Eagar added 2 commits 2023-05-03 08:37:50 +02:00
Joseph Eagar added 1 commit 2023-05-03 23:14:14 +02:00
Joseph Eagar added 2 commits 2023-05-09 21:59:00 +02:00
ef8777d4e8 temp-sculpt-attr-api: Tweak API to better support short-use attribute handles
Sometimes you want to clear a SculptAttribute handle without
destroying the attribute itself.

Also removed the using namespace blender::bke::paint lines.
Joseph Eagar closed this pull request 2023-10-09 21:15:21 +02:00

Pull request closed

Sign in to join this conversation.
No reviewers
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
2 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#106920
No description provided.