Refactor ImBuf buffer access #107609

Merged
Sergey Sharybin merged 8 commits from Sergey/blender:imbuf_buffer_data_refactor into main 2023-05-18 10:19:10 +02:00

The goal is to make it more explicit and centralized operation to
assign and steal buffer data, with proper ownership tracking.

The buffers and ownership flags are wrapped into their dedicated
structures now.

There should be no functional changes currently, it is a preparation
for allowing implicit sharing of the ImBuf buffers. Additionally, in
the future it is possible to more buffer-specific information (such
as color space) next to the buffer data itself. It is also possible
to clean up the allocation flags (IB_rect, ...) to give them more
clear naming and not have stored in the ImBuf->flags as they are only
needed for allocation.

The most dangerous part of this change is the change of byte buffer
data from int* to uint8_t*. In a lot of cases the byte buffer was
cast to uchar*, so those casts are now gone. But some code is
operating on int* so now there are casts in there. In practice this
should be fine, since we only support 64bit platforms, so allocations
are aligned. The real things to watch out for here is the fact that
allocation and offsetting from the byte buffer now need an explicit 4
channel multiplier.

Once everything is C++ it will be possible to simplify public
functions even further.

The goal is to make it more explicit and centralized operation to assign and steal buffer data, with proper ownership tracking. The buffers and ownership flags are wrapped into their dedicated structures now. There should be no functional changes currently, it is a preparation for allowing implicit sharing of the ImBuf buffers. Additionally, in the future it is possible to more buffer-specific information (such as color space) next to the buffer data itself. It is also possible to clean up the allocation flags (IB_rect, ...) to give them more clear naming and not have stored in the ImBuf->flags as they are only needed for allocation. The most dangerous part of this change is the change of byte buffer data from `int*` to `uint8_t*`. In a lot of cases the byte buffer was cast to `uchar*`, so those casts are now gone. But some code is operating on `int*` so now there are casts in there. In practice this should be fine, since we only support 64bit platforms, so allocations are aligned. The real things to watch out for here is the fact that allocation and offsetting from the byte buffer now need an explicit 4 channel multiplier. Once everything is C++ it will be possible to simplify public functions even further.
Sergey Sharybin added the
Interest
Images & Movies
Module
Core
labels 2023-05-04 10:43:47 +02:00
Sergey Sharybin added 1 commit 2023-05-04 10:43:52 +02:00
617f483e5b Refactor ImBuf buffer access
The goal is to make it more explicit and centralized operation to
assign and steal buffer data, with proper ownership tracking.

The buffers and ownership flags are wrapped into their dedicated
structures now.

There should be no functional changes currently, it is a preparation
for allowing implicit sharing of the ImBuf buffers. Additionally, in
the future it is possible to more buffer-specific information (such
as color space) next to the buffer data itself.

The most dangerous part of this change is the change of byte buffer
data from `int*` to `uint8_t*`. In a lot of cases the byte buffer was
cast to `uchar*`, so those casts are now gone. But some code is
operating on `int*` so now there are casts in there. In practice this
should be fine, since we only support 64bit platforms, so allocations
are aligned. The real things to watch out for here is the fact that
allocation and offsetting from the byte buffer now need an explicit 4
channel multiplier.

Once everything is C++ it will be possible to simplify public
functions even further.
Sergey Sharybin added this to the Core project 2023-05-04 10:43:59 +02:00
Sergey Sharybin added 1 commit 2023-05-05 18:47:23 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
5c3f684000
Merge branch 'main' into imbuf_buffer_data_refactor
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin added 1 commit 2023-05-05 18:52:00 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
3d274bcf30
Fix old API used on Windows GHOST
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin added 1 commit 2023-05-05 19:01:41 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
679cbe1c2a
Fix control reaches end of non-void function
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin requested review from Brecht Van Lommel 2023-05-05 19:09:17 +02:00
Sergey Sharybin requested review from Jeroen Bakker 2023-05-05 19:09:22 +02:00
Brecht Van Lommel approved these changes 2023-05-05 20:08:49 +02:00
Brecht Van Lommel left a comment
Owner

Mainly looked for potential issues in byte_buffer.data math but did not spot any.

Mainly looked for potential issues in `byte_buffer.data` math but did not spot any.
@ -197,3 +197,3 @@
/* Here we store the rect in the icon - same as before */
if (size == bbuf->x && size == bbuf->y && xofs == 0 && yofs == 0) {
memcpy(iimg->rect, bbuf->rect, size * size * sizeof(int));
memcpy(iimg->rect, bbuf->byte_buffer.data, size * size * sizeof(int));

sizeof(int) -> 4 * sizeof(uint8_t)

`sizeof(int)` -> `4 * sizeof(uint8_t)`
@ -206,1 +205,3 @@
&iimg->rect[y * size], &bbuf->rect[(y + yofs) * imgsize + xofs], size * sizeof(int));
memcpy(&iimg->rect[y * size],
&bbuf->byte_buffer.data[(y + yofs) * imgsize + xofs],
size * sizeof(int));

sizeof(int) -> 4 * sizeof(uint8_t)

`sizeof(int)` -> `4 * sizeof(uint8_t)`
@ -92,3 +172,2 @@
ibuf->rect_float = nullptr;
ibuf->mall &= ~IB_rectfloat;
ibuf->flags &= ~IB_rectfloat;

Doesn't have to be cleaned up as part of this refactor, but it seems this and various other flags are not used for anything. We should probably move them to a separate enum that is used only for arguments when allocating imbufs, and not stored in ibuf->flags.

Doesn't have to be cleaned up as part of this refactor, but it seems this and various other flags are not used for anything. We should probably move them to a separate enum that is used only for arguments when allocating imbufs, and not stored in `ibuf->flags`.
Author
Owner

I totally agree. I probably should have written about it in the description. That is something I was planning to do, but wanted to have a dedicated look into it.

I totally agree. I probably should have written about it in the description. That is something I was planning to do, but wanted to have a dedicated look into it.
@ -553,3 +687,3 @@
if (flags & IB_rect) {
memcpy(ibuf2->rect, ibuf1->rect, size_t(x) * y * sizeof(int));
memcpy(ibuf2->byte_buffer.data, ibuf1->byte_buffer.data, size_t(x) * y * sizeof(int));

sizeof(int) -> 4 * sizeof(uint8_t)

`sizeof(int)` -> `4 * sizeof(uint8_t)`
Sergey Sharybin added 1 commit 2023-05-05 21:32:10 +02:00
2d3a3d7f50 sizeof(int) -> 4 * sizeof(uint8_t)
In the cases spotted by Brecht.
Jeroen Bakker approved these changes 2023-05-08 09:14:29 +02:00
Jeroen Bakker left a comment
Member

I checked the core change and familiar areas and those changes seems fine.
Consider an other name for the struct or its attributes for more clarity.

I checked the core change and familiar areas and those changes seems fine. Consider an other name for the struct or its attributes for more clarity.
@ -154,0 +159,4 @@
/* Specialization of an ownership whenever a bare pointer is provided to the ImBuf buffers
* assignment API. */
typedef enum ImBufOwnership {
Member

Naming might be confusing as the enum name can be read as a state type, but the values represent an action/transition.

Naming might be confusing as the enum name can be read as a state type, but the values represent an action/transition.
@ -154,0 +160,4 @@
/* Specialization of an ownership whenever a bare pointer is provided to the ImBuf buffers
* assignment API. */
typedef enum ImBufOwnership {
/* The ImBug simply shares pointer with data owned by someone else, and will not perform any
Member

ImBuf

`ImBuf`
Sergey Sharybin added 2 commits 2023-05-08 11:16:41 +02:00
Author
Owner

I've fixed the typo.

For the enum I do see your concerns, but I can not think of a better naming. To me this is very the same than Ceres data ownership for residual blocks (so something I just got used to, so a bit hard to think out of the box).

If we want to change the naming before commit, I really wouldn't mind having some proposed changes, it would help a lot :)

I've fixed the typo. For the enum I do see your concerns, but I can not think of a better naming. To me this is very the same than Ceres data ownership for residual blocks (so something I just got used to, so a bit hard to think out of the box). If we want to change the naming before commit, I really wouldn't mind having some proposed changes, it would help a lot :)
Member

Fine by me!

Fine by me!
Member

We have GeometryOwnershipType currently (which is very similar in purpose actually), maybe just adding a Type to the end of the name would make it clearer? Not a big deal though IMO.

We have `GeometryOwnershipType` currently (which is very similar in purpose actually), maybe just adding a `Type` to the end of the name would make it clearer? Not a big deal though IMO.
Sergey Sharybin added 1 commit 2023-05-18 09:55:00 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
e1bfaf2ce8
Merge branch 'main' into imbuf_buffer_data_refactor
Author
Owner

@blender-bot build

@blender-bot build
Sergey Sharybin merged commit 406cfd214a into main 2023-05-18 10:19:10 +02:00
Sergey Sharybin deleted branch imbuf_buffer_data_refactor 2023-05-18 10:19:11 +02:00
Author
Owner

@HooglyBoogly It might be good to align to the GeometryOwnershipType. Although, after implementing the implicit sharing between imBuf and RenderResult it seems we can simplify the internal ImBuf storage to a simple boolean flag. So in this case it reads kinda neat IMB_assign_float_buffer(imbuf, data, IB_TAKE_OWNERSHIP).

In any case, to me it seemed that it is something we can easily re-iterate if needed, and decided to merge the current state, so that keeping such big-ish change up to the main branch is not becoming too much of a burden now when we allow all sort of refactors in the main :)

@HooglyBoogly It might be good to align to the `GeometryOwnershipType`. Although, after implementing the implicit sharing between imBuf and RenderResult it seems we can simplify the internal ImBuf storage to a simple boolean flag. So in this case it reads kinda neat `IMB_assign_float_buffer(imbuf, data, IB_TAKE_OWNERSHIP)`. In any case, to me it seemed that it is something we can easily re-iterate if needed, and decided to merge the current state, so that keeping such big-ish change up to the main branch is not becoming too much of a burden now when we allow all sort of refactors in the main :)
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:38 +02:00
Bastien Montagne removed this from the Core project 2023-07-03 12:46:02 +02: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
4 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#107609
No description provided.