Image Access in Compositor Backdrop is not thread-safe #118337

Open
opened 2024-02-15 17:14:02 +01:00 by michael campbell · 11 comments

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 537.58

Blender Version
Broken: version: 4.2.0 Alpha, branch: main, commit date: 2024-02-13 07:22, hash: 9ad127780af9
Worked: (in tiled mode)

Short description of error
Full frame compositor backdrop disappears during playback due to performance issue not present in tiled or GPU execution modes

Exact steps for others to reproduce the error

  1. download below blend file and open with load ui enabled.
  2. making sure no other addons are installed that have frame change handlers,
  3. go to the script editor and run the script
  4. use one of the arrow keys to change to a frame between 1 and 10
  5. in the compositor, ensuring the backdrop is on, try playback in Tiled, Full Frame, and GPU compositors (you'll need to enable experimental compositor in the Blender preferences for those options to appear in the compositor n panel options tab)

Blend file link: https://www.dropbox.com/scl/fi/uqt6ghyw6aw16j32ospvz/full-frame-mode-image-update-performance-issue.zip?rlkey=3urj9sbde7pesmpupg5dk0bzm&dl=0

For me the backdrop in Tiled mode is buttery smooth, GPU is strangely very slow, and Full frame is so slow that it can't display anything before the playhead moves to the next frame.

If I call render.render() after updating the image filepath, this will force the compositor to wait until the tree finishes processing before moving to the next frame. Strangely Full Frame mode is the fastest in this case, which suggests the issue isn't full frame being slower, but perhaps it not interacting with the backdrop as quickly? If you try this you'll need to avoid recursion by setting a variable preventing the handler to execute multiple times per frame, because the frame change handler also fires after rendering.

**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 537.58 **Blender Version** Broken: version: 4.2.0 Alpha, branch: main, commit date: 2024-02-13 07:22, hash: `9ad127780af9` Worked: (in tiled mode) **Short description of error** Full frame compositor backdrop disappears during playback due to performance issue not present in tiled or GPU execution modes **Exact steps for others to reproduce the error** 1. download below blend file and open with load ui enabled. 2. making sure no other addons are installed that have frame change handlers, 3. go to the script editor and run the script 4. use one of the arrow keys to change to a frame between 1 and 10 5. in the compositor, ensuring the backdrop is on, try playback in Tiled, Full Frame, and GPU compositors (you'll need to enable experimental compositor in the Blender preferences for those options to appear in the compositor n panel options tab) Blend file link: https://www.dropbox.com/scl/fi/uqt6ghyw6aw16j32ospvz/full-frame-mode-image-update-performance-issue.zip?rlkey=3urj9sbde7pesmpupg5dk0bzm&dl=0 For me the backdrop in Tiled mode is buttery smooth, GPU is strangely very slow, and Full frame is so slow that it can't display anything before the playhead moves to the next frame. If I call render.render() after updating the image filepath, this will force the compositor to wait until the tree finishes processing before moving to the next frame. Strangely Full Frame mode is the fastest in this case, which suggests the issue isn't full frame being slower, but perhaps it not interacting with the backdrop as quickly? If you try this you'll need to avoid recursion by setting a variable preventing the handler to execute multiple times per frame, because the frame change handler also fires after rendering.
michael campbell added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2024-02-15 17:14:02 +01:00
Iliya Katushenock added the
Interest
Compositing
Interest
VFX & Video
labels 2024-02-15 17:30:10 +01:00

When trying to replicate the problem in the latest daily build, I found that neither Tiled nor Full Frame options are available. It appears that these modes were removed in a previous commit (a8a05ebba1). I'm not sure yet why this change was made (cc @Sergey).

Regarding the issue you described, it seems that the backdrop not being able to compute the composition at the same time as the playback may not be considered a bug. The backdrop is not intended to be used for video playback.

If your intention is to request an improvement in performance, I'm afraid that potential performance improvements are not handled as bug reports. However, you can be assured that the developers are continuously working on improving performance in general.

Did I misunderstand the issue?

When trying to replicate the problem in the [latest daily build](https://builder.blender.org/download/), I found that neither Tiled nor Full Frame options are available. It appears that these modes were removed in a previous commit (a8a05ebba170abf3047d83a610eaf6198d26a025). I'm not sure yet why this change was made (cc @Sergey). Regarding the issue you described, it seems that the backdrop not being able to compute the composition at the same time as the playback may not be considered a bug. The backdrop is not intended to be used for video playback. If your intention is to request an improvement in performance, I'm afraid that potential performance improvements are not handled as bug reports. However, you can be assured that the developers are continuously working on improving performance in general. Did I misunderstand the issue?
Germano Cavalcante added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2024-02-15 20:27:20 +01:00

The tiled was removed on Monday, so you'll need to compare your build with a pre-monday build.

It affects addons with caching systems.

I think maybe it's a bug because it works in the old tiled mode and current GPU mode without issue. I don't think it's a performance improvement issue because the full frame is actually faster than tiled mode to process the tree.

The tiled was removed on Monday, so you'll need to compare your build with a pre-monday build. It affects addons with caching systems. I think maybe it's a bug because it works in the old tiled mode and current GPU mode without issue. I don't think it's a performance improvement issue because the full frame is actually faster than tiled mode to process the tree.

oh sorry, I clicked the wrong button.

oh sorry, I clicked the wrong button.
Blender Bot added
Status
Archived
and removed
Status
Needs Information from User
labels 2024-02-15 20:45:54 +01:00
Iliya Katushenock added
Status
Needs Information from User
and removed
Status
Archived
labels 2024-02-15 20:58:17 +01:00

forgot to mention, it also affects the image editor.

forgot to mention, it also affects the image editor.

@mano-wii This was done as part of #106533, https://devtalk.blender.org/t/improved-render-compositor-feedback-and-discussion/33377

Compositor is not really meant to be executed during playback, as it will not be able to keep up with the frame-rate. This is how regular animation is handling it: if you have animated property playback does not trigger compositing. Updating image path forces compositor to refresh, but it is more of a side-effect when is done from a frame-update handler. However, ideally I think would be if the preview image is not cleared when compositing is cancelled.

I gave it a dig in the code, and it seems that the MultilayerBaseOperation::get_im_buf is not thread-safe: it does if (BKE_image_multilayer_index()) { /* acquire ibuf */ }. The BKE_image_multilayer_index is not thread-safe, so if the image path is changed from a different thread it the call might access stale or null-ified image_->rr. Or, the path might be changed in-between of the index check and ibuf acquisition.

I am not sure why do we even need the explicit check BKE_image_multilayer_index prior to the ibuf acquisition. It is safer to always call get_im_buf, which will do thread-safe ibuf acquisition. There is still unsafe call to BKE_image_is_multilayer in the get_im_buf, which potentially might leads to issues if frame update handler changes image type, but that's a separate story from this report.

@OmarEmaraDev Would be nice to have a second pair of eyes. Do you see anything potential issues if we do

ImBuf *MultilayerBaseOperation::get_im_buf()
{
  /* temporarily changes the view to get the right ImBuf */
  const int view = image_user_->view;

  image_user_->view = view_;
  image_user_->pass = pass_id_;

  ImBuf *ibuf = BaseImageOperation::get_im_buf();
  image_user_->view = view;
  return ibuf;
}

?

@mano-wii This was done as part of #106533, https://devtalk.blender.org/t/improved-render-compositor-feedback-and-discussion/33377 Compositor is not really meant to be executed during playback, as it will not be able to keep up with the frame-rate. This is how regular animation is handling it: if you have animated property playback does not trigger compositing. Updating image path forces compositor to refresh, but it is more of a side-effect when is done from a frame-update handler. However, ideally I think would be if the preview image is not cleared when compositing is cancelled. I gave it a dig in the code, and it seems that the `MultilayerBaseOperation::get_im_buf` is not thread-safe: it does `if (BKE_image_multilayer_index()) { /* acquire ibuf */ }`. The `BKE_image_multilayer_index` is not thread-safe, so if the image path is changed from a different thread it the call might access stale or null-ified `image_->rr`. Or, the path might be changed in-between of the index check and ibuf acquisition. I am not sure why do we even need the explicit check `BKE_image_multilayer_index` prior to the `ibuf` acquisition. It is safer to always call `get_im_buf`, which will do thread-safe `ibuf` acquisition. There is still unsafe call to `BKE_image_is_multilayer` in the `get_im_buf`, which potentially might leads to issues if frame update handler changes image type, but that's a separate story from this report. @OmarEmaraDev Would be nice to have a second pair of eyes. Do you see anything potential issues if we do ``` ImBuf *MultilayerBaseOperation::get_im_buf() { /* temporarily changes the view to get the right ImBuf */ const int view = image_user_->view; image_user_->view = view_; image_user_->pass = pass_id_; ImBuf *ibuf = BaseImageOperation::get_im_buf(); image_user_->view = view; return ibuf; } ``` ?
Member

@Sergey I will check myself. But immediately, just to clarify, BKE_image_multilayer_index setup the multi_index in the image user, which is necessary as far as I can remember. So removing it might not side-effect free. But maybe I don't remember exactly, so I will take another look.

@Sergey I will check myself. But immediately, just to clarify, `BKE_image_multilayer_index` setup the `multi_index` in the image user, which is necessary as far as I can remember. So removing it might not side-effect free. But maybe I don't remember exactly, so I will take another look.
Member

@Sergey I took another look, and I don't think we can remove that function call, since it initialized the multi_index necessary to acquire the buffer.

@Sergey I took another look, and I don't think we can remove that function call, since it initialized the `multi_index` necessary to acquire the buffer.

@OmarEmaraDev Ah, it looked like the get_im_buf sets the multi_index, but it only does it for non-multilayer images.
Ok, so we need to find a different way ensuring thread-safety of image access.

@OmarEmaraDev Ah, it looked like the `get_im_buf` sets the `multi_index`, but it only does it for non-multilayer images. Ok, so we need to find a different way ensuring thread-safety of image access.
Germano Cavalcante changed title from Full frame compositor backdrop disappears during playback. Issue not present in tiled or GPU execution modes to Image Access in Compositor Backdrop is not thread-safe 2024-02-27 14:12:12 +01:00

@Sergey could this issue also cause the below error when trying to write to the image datablock after rendering completes?

bpy.data.scenes[scene_name].node_tree.nodes[cache_node_name].image.filepath = new_exr_path

Error:

 'AttributeError: Writing to ID classes in this context is not allowed: cache for Scene-Camera-ViewLayer, Image datablock, error setting Image.filepath\n']

Before attempting to update the image datablock's filepath, I'm ensuring the compositor and render have fully finished, and I'm launching the function from queue.queue() to make sure it's back on the main thread. I'm not sure why it's saying can't write in this context:

#runs from queue.queue() after rendering completes

if bpy.app.version >= (3,3,0):
        if bpy.app.is_job_running('RENDER'):
            #print('render has still not completed')
            return 0.0001
        if bpy.app.is_job_running('COMPOSITE'):
            #print('compositing in progress')
            return 0.0001
bpy.data.scenes[scene_name].node_tree.nodes[cache_node_name].image.filepath = new_exr_path
@Sergey could this issue also cause the below error when trying to write to the image datablock after rendering completes? ```bpy.data.scenes[scene_name].node_tree.nodes[cache_node_name].image.filepath = new_exr_path``` Error: ``` 'AttributeError: Writing to ID classes in this context is not allowed: cache for Scene-Camera-ViewLayer, Image datablock, error setting Image.filepath\n'] ``` Before attempting to update the image datablock's filepath, I'm ensuring the compositor and render have fully finished, and I'm launching the function from queue.queue() to make sure it's back on the main thread. I'm not sure why it's saying can't write in this context: ``` #runs from queue.queue() after rendering completes if bpy.app.version >= (3,3,0): if bpy.app.is_job_running('RENDER'): #print('render has still not completed') return 0.0001 if bpy.app.is_job_running('COMPOSITE'): #print('compositing in progress') return 0.0001 bpy.data.scenes[scene_name].node_tree.nodes[cache_node_name].image.filepath = new_exr_path ```

@3di It looks to me a separate issue. It might be that you're modifying an image from a non-main thread, while main thread is in a state where modification of IDs is not allowed. I don't fully understand this check for whether ID is allowed to be modified or not, and it probably worth submitting a separate issue about it. It might or might not leat to the same root issue, but it needs a separate investigation.

It would also be nice if you try to reproduce the AttributeError with Compositor disabled in the Post-processing settings.

@3di It looks to me a separate issue. It might be that you're modifying an image from a non-main thread, while main thread is in a state where modification of IDs is not allowed. I don't fully understand this check for whether ID is allowed to be modified or not, and it probably worth submitting a separate issue about it. It might or might not leat to the same root issue, but it needs a separate investigation. It would also be nice if you try to reproduce the `AttributeError` with Compositor disabled in the Post-processing settings.

@Sergey thanks. I did create this report a while back #114455 (comment) , but at that time I thought it was because is_job_running was returning the incorrect result.

The python code doesn't run until back in the main thread, because it's executed from queue.queue() which doesn't run until back in the main thread as I understand it. Perhaps Blender is incorrectly raising the error because it thinks it's not back in the main thread when it is?

It must be a difficult one to solve, because even jacques lucke couldn't fix it when he ran into the problem several years ago with his animation nodes addon.

@Sergey thanks. I did create this report a while back https://projects.blender.org/blender/blender/issues/114455#issuecomment-1119612 , but at that time I thought it was because ```is_job_running``` was returning the incorrect result. The python code doesn't run until back in the main thread, because it's executed from queue.queue() which doesn't run until back in the main thread as I understand it. Perhaps Blender is incorrectly raising the error because it thinks it's not back in the main thread when it is? It must be a difficult one to solve, because even jacques lucke couldn't fix it when he ran into the problem several years ago with his animation nodes addon.
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#118337
No description provided.