gpu.types.GPUOffScreen together with gpu.state.depth_test_set causes blender ui to be unresponsive #98486

Open
opened 2022-05-30 01:05:16 +02:00 by Jakub Uhlik · 28 comments

System Information
(reporting from different os, those are copied from system-info.txt generated by blender)
Operating system: Windows-10-10.0.19043-SP0
Graphics card: GeForce GTX 760/PCIe/SSE2, 4.5.0 NVIDIA 461.92

Blender Version
Broken: version: 3.1.2, branch: master, commit date: 2022-03-31 17:40, hash: cc66d1020c, type: release
Worked: ?

Short description of error
gpu.types.GPUOffScreen together with gpu.state.depth_test_set causes blender ui to be unresponsive, nothing can be clicked, blender have to be killed. if gpu.state.depth_test_set is set to 'NONE' after drawing is finished, problem goes away. this happens on windows, on mac it works.

Exact steps for others to reproduce the error
example code is from: https://docs.blender.org/api/current/gpu.html#copy-offscreen-rendering-result-back-to-ram with marked modifications

  • Open text editor in default scene
  • Paste and run the attached code
import bpy
import gpu
import random
from mathutils import Matrix
from gpu_extras.presets import draw_circle_2d

IMAGE_NAME = "Generated Image"
WIDTH = 512
HEIGHT = 512
RING_AMOUNT = 10


offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT)

with offscreen.bind():
    fb = gpu.state.active_framebuffer_get()
    fb.clear(color=(0.0, 0.0, 0.0, 0.0))
    
    # one line added here ----------------------------------------------------
    gpu.state.depth_test_set('LESS_EQUAL')
    # ------------------------------------------------------------------------

    with gpu.matrix.push_pop():
        # reset matrices -> use normalized device coordinates [-1, 1]
        gpu.matrix.load_matrix(Matrix.Identity(4))
        gpu.matrix.load_projection_matrix(Matrix.Identity(4))

        for i in range(RING_AMOUNT):
            draw_circle_2d(
                (random.uniform(-1, 1), random.uniform(-1, 1)),
                (1, 1, 1, 1), random.uniform(0.1, 1),
                segments=20,
            )
    
    - and one line here ------------------------------------------------------
    - gpu.state.depth_test_set('NONE')
    # ------------------------------------------------------------------------

    buffer = fb.read_color(0, 0, WIDTH, HEIGHT, 4, 0, 'UBYTE')

offscreen.free()


if not IMAGE_NAME in bpy.data.images:
    bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT)
image = bpy.data.images[IMAGE_NAME]
image.scale(WIDTH, HEIGHT)

buffer.dimensions = WIDTH * HEIGHT * 4
image.pixels = [v / 255 for v in buffer]
**System Information** (reporting from different os, those are copied from system-info.txt generated by blender) Operating system: Windows-10-10.0.19043-SP0 Graphics card: GeForce GTX 760/PCIe/SSE2, 4.5.0 NVIDIA 461.92 **Blender Version** Broken: version: 3.1.2, branch: master, commit date: 2022-03-31 17:40, hash: cc66d1020c3b, type: release Worked: ? **Short description of error** `gpu.types.GPUOffScreen` together with `gpu.state.depth_test_set` causes blender ui to be unresponsive, nothing can be clicked, blender have to be killed. if `gpu.state.depth_test_set` is set to `'NONE'` after drawing is finished, problem goes away. this happens on windows, on mac it works. **Exact steps for others to reproduce the error** example code is from: https://docs.blender.org/api/current/gpu.html#copy-offscreen-rendering-result-back-to-ram with marked modifications - Open text editor in default scene - Paste and run the attached code ``` import bpy import gpu import random from mathutils import Matrix from gpu_extras.presets import draw_circle_2d IMAGE_NAME = "Generated Image" WIDTH = 512 HEIGHT = 512 RING_AMOUNT = 10 offscreen = gpu.types.GPUOffScreen(WIDTH, HEIGHT) with offscreen.bind(): fb = gpu.state.active_framebuffer_get() fb.clear(color=(0.0, 0.0, 0.0, 0.0)) # one line added here ---------------------------------------------------- gpu.state.depth_test_set('LESS_EQUAL') # ------------------------------------------------------------------------ with gpu.matrix.push_pop(): # reset matrices -> use normalized device coordinates [-1, 1] gpu.matrix.load_matrix(Matrix.Identity(4)) gpu.matrix.load_projection_matrix(Matrix.Identity(4)) for i in range(RING_AMOUNT): draw_circle_2d( (random.uniform(-1, 1), random.uniform(-1, 1)), (1, 1, 1, 1), random.uniform(0.1, 1), segments=20, ) - and one line here ------------------------------------------------------ - gpu.state.depth_test_set('NONE') # ------------------------------------------------------------------------ buffer = fb.read_color(0, 0, WIDTH, HEIGHT, 4, 0, 'UBYTE') offscreen.free() if not IMAGE_NAME in bpy.data.images: bpy.data.images.new(IMAGE_NAME, WIDTH, HEIGHT) image = bpy.data.images[IMAGE_NAME] image.scale(WIDTH, HEIGHT) buffer.dimensions = WIDTH * HEIGHT * 4 image.pixels = [v / 255 for v in buffer] ```
Author

Added subscriber: @JakubUhlik

Added subscriber: @JakubUhlik
Member

Added subscriber: @PratikPB2123

Added subscriber: @PratikPB2123
Member

causes blender ui to be unresponsive, nothing can be clicked, blender have to be killed

Thanks for the report. I'm not able to confirm this in both 3.1 and current master (3c0d7152c89d6983db193783113387e4681e0c4d)

Operating system: Windows-10-10.0.19043-SP0 64 Bits
Graphics card: Intel(R) UHD Graphics 620 Intel 4.5.0 - Build 30.0.101.1660
Graphics card : AMD Radeon(TM) 535 ATI Technologies .```
> causes blender ui to be unresponsive, nothing can be clicked, blender have to be killed Thanks for the report. I'm not able to confirm this in both 3.1 and current master (`3c0d7152c89d6983db193783113387e4681e0c4d`) ```Blender version: 3.3, 3c0d7152c89d6983db193783113387e4681e0c4d Operating system: Windows-10-10.0.19043-SP0 64 Bits Graphics card: Intel(R) UHD Graphics 620 Intel 4.5.0 - Build 30.0.101.1660 Graphics card : AMD Radeon(TM) 535 ATI Technologies .```
Member

Can you check again in latest development build?: https://builder.blender.org/download/daily

Can you check again in latest development build?: https://builder.blender.org/download/daily

Added subscriber: @dfelinto

Added subscriber: @dfelinto

I cannot reproduce it either on:

Operating system: Linux-5.13.0-41-generic-x86_64-with-glibc2.31 64 Bits
Graphics card: NVIDIA GeForce GTX 1080/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 510.73.05
I cannot reproduce it either on: ``` Operating system: Linux-5.13.0-41-generic-x86_64-with-glibc2.31 64 Bits Graphics card: NVIDIA GeForce GTX 1080/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 510.73.05 ```

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'

@JakubUhlik you can reproduce this simply by running this code in the Text Editor (after uncommenting "gpu.state.depth_test_set('NONE'))?

@JakubUhlik you can reproduce this simply by running this code in the Text Editor (after uncommenting "gpu.state.depth_test_set('NONE'))?
Author

@dfelinto to reproduce, be on windows machine, paste code to text editor as is, run. uncommenting depth_test_set('NONE') line only fixes issue

@dfelinto to reproduce, be on windows machine, paste code to text editor as is, run. uncommenting `depth_test_set('NONE')` line only fixes issue
Author

@PratikPB2123 i just tried latest blender-3.2.0-beta+v32.32bf6455a019-windows.amd64-release, same thing.

@PratikPB2123 i just tried latest `blender-3.2.0-beta+v32.32bf6455a019-windows.amd64-release`, same thing.
Author

also, after runnning script, try to do something in all areas, looks like properties works, 3d viewport nothing can be clicked, even close/open subpanel buttons.

also, after runnning script, try to do something in all areas, looks like properties works, 3d viewport nothing can be clicked, even close/open subpanel buttons.
Member

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs User Info' to: 'Needs Triage'
Contributor

Added subscriber: @IyadAhmed

Added subscriber: @IyadAhmed
Contributor

I can't reproduce on Windows, is this the whole code that is running?

I can't reproduce on Windows, is this the whole code that is running?
Author

yes, that's whole code. here is video demonstration. i am clicking vigorously on everything

v.mp4

sometimes (not sure when) some artifacts in lower left corner appear, they constantly change

a.jpg

yes, that's whole code. here is video demonstration. i am clicking vigorously on everything [v.mp4](https://archive.blender.org/developer/F13123608/v.mp4) sometimes (not sure when) some artifacts in lower left corner appear, they constantly change ![a.jpg](https://archive.blender.org/developer/F13123615/a.jpg)
Contributor

Can you try on dev build maybe it is fixed there?

Can you try on dev build maybe it is fixed there?
Author

video is from 3.1, 3.2 is the same, 3.3 as well with minor change that buttons in header with pop up menus highlights when clicked

video is from 3.1, 3.2 is the same, 3.3 as well with minor change that buttons in header with pop up menus highlights when clicked

Added subscriber: @mano-wii

Added subscriber: @mano-wii

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'

Does this problem happen if you just call gpu.state.depth_test_set('LESS_EQUAL') and nothing else?

import gpu
gpu.state.depth_test_set('LESS_EQUAL')

Note that you are changing a status that can affect the entire drawing in Blender. That's why in some cases it is advisable to restore the previous state.

Does this problem happen if you just call `gpu.state.depth_test_set('LESS_EQUAL')` and nothing else? ``` import gpu gpu.state.depth_test_set('LESS_EQUAL') ``` Note that you are changing a status that can affect the entire drawing in Blender. That's why in some cases it is advisable to restore the previous state.
Author

@mano-wii yes, on windows. on mac nothing bad happens.
and yes, i am always reverting state, but in one case i forgot and it lead to painful debugging, and because this happens on windows and not on mac, it puzzled me even more. either it should be python user responsibility to always revert state back and that highlighted in documentation or should be done on blender side in consistent way on all platforms..

@mano-wii yes, on windows. on mac nothing bad happens. and yes, i am always reverting state, but in one case i forgot and it lead to painful debugging, and because this happens on windows and not on mac, it puzzled me even more. either it should be python user responsibility to always revert state back and that highlighted in documentation or should be done on blender side in consistent way on all platforms..
Member

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'

In #98486#1366968, @mano-wii wrote:
Does this problem happen if you just call gpu.state.depth_test_set('LESS_EQUAL') and nothing else?

import gpu
gpu.state.depth_test_set('LESS_EQUAL')

@JakubUhlik, this would still be useful information to delimit where the bug is hiding ^.
Can you try run the snippet code?

> In #98486#1366968, @mano-wii wrote: > Does this problem happen if you just call `gpu.state.depth_test_set('LESS_EQUAL')` and nothing else? > ``` > import gpu > gpu.state.depth_test_set('LESS_EQUAL') > ``` @JakubUhlik, this would still be useful information to delimit where the bug is hiding ^. Can you try run the snippet code?
Author

@mano-wii yes, it happens even with just this. sorry, i thought it was clear from my last message

@mano-wii yes, it happens even with just this. sorry, i thought it was clear from my last message
Member

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs User Info' to: 'Needs Triage'

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

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

Since this is a real issue on some GPUs, I believe it deserves to be confirmed at least as a Known Issue.
The depth test state should have no effect when the framebuffer has no depth texture.

It would be nice to send an error message in Python to users to avoid such invalid state. But since the state remains even after the framebuffer it supports is used, it is not easy to find a place for such an error message.

We could change the C++ code to skip the state change if the framebuffer doesn't support it. But it doesn't seem worth it for something that can be avoided before.

diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc
index 46422124112..b076f39135f 100644
--- a/source/blender/gpu/opengl/gl_state.cc
+++ b/source/blender/gpu/opengl/gl_state.cc
@@ -77,7 +77,7 @@ void GLStateManager::force_state()
   this->set_mutable_state(this->mutable_state);
 };
 
-void GLStateManager::set_state(const GPUState &state)
+void GLStateManager::set_state(GPUState &state)
 {
   GPUState changed = state ^ current_;
 
@@ -88,7 +88,13 @@ void GLStateManager::set_state(const GPUState &state)
     set_write_mask((eGPUWriteMask)state.write_mask);
   }
   if (changed.depth_test != 0) {
-    set_depth_test((eGPUDepthTest)state.depth_test);
+    if (!active_fb || active_fb->depth_tex() == nullptr) {
+      /* Don't change the depth state if the framebuffer doesn't support it. */
+      state.depth_test = current_.depth_test;
+    }
+    else {
+      set_depth_test((eGPUDepthTest)state.depth_test);
+    }
   }
   if (changed.stencil_test != 0 || changed.stencil_op != 0) {
     set_stencil_test((eGPUStencilTest)state.stencil_test, (eGPUStencilOp)state.stencil_op);
diff --git a/source/blender/gpu/opengl/gl_state.hh b/source/blender/gpu/opengl/gl_state.hh
index 74c68e51755..75071d753f0 100644
--- a/source/blender/gpu/opengl/gl_state.hh
+++ b/source/blender/gpu/opengl/gl_state.hh
@@ -94,7 +94,7 @@ class GLStateManager : public StateManager {
   static void set_shadow_bias(bool enable);
   static void set_blend(eGPUBlend value);
 
-  void set_state(const GPUState &state);
+  void set_state(GPUState &state);
   void set_mutable_state(const GPUStateMutable &state);
 
   void texture_bind_apply();


(note: seems related to D15859: Draw manager: Validate the draw state matches framebuffer. in some way).

Since this is a real issue on some GPUs, I believe it deserves to be confirmed at least as a `Known Issue`. The depth test state should have no effect when the framebuffer has no depth texture. It would be nice to send an error message in Python to users to avoid such invalid state. But since the state remains even after the framebuffer it supports is used, it is not easy to find a place for such an error message. We could change the C++ code to skip the state change if the framebuffer doesn't support it. But it doesn't seem worth it for something that can be avoided before. ```lines=18 diff --git a/source/blender/gpu/opengl/gl_state.cc b/source/blender/gpu/opengl/gl_state.cc index 46422124112..b076f39135f 100644 --- a/source/blender/gpu/opengl/gl_state.cc +++ b/source/blender/gpu/opengl/gl_state.cc @@ -77,7 +77,7 @@ void GLStateManager::force_state() this->set_mutable_state(this->mutable_state); }; -void GLStateManager::set_state(const GPUState &state) +void GLStateManager::set_state(GPUState &state) { GPUState changed = state ^ current_; @@ -88,7 +88,13 @@ void GLStateManager::set_state(const GPUState &state) set_write_mask((eGPUWriteMask)state.write_mask); } if (changed.depth_test != 0) { - set_depth_test((eGPUDepthTest)state.depth_test); + if (!active_fb || active_fb->depth_tex() == nullptr) { + /* Don't change the depth state if the framebuffer doesn't support it. */ + state.depth_test = current_.depth_test; + } + else { + set_depth_test((eGPUDepthTest)state.depth_test); + } } if (changed.stencil_test != 0 || changed.stencil_op != 0) { set_stencil_test((eGPUStencilTest)state.stencil_test, (eGPUStencilOp)state.stencil_op); diff --git a/source/blender/gpu/opengl/gl_state.hh b/source/blender/gpu/opengl/gl_state.hh index 74c68e51755..75071d753f0 100644 --- a/source/blender/gpu/opengl/gl_state.hh +++ b/source/blender/gpu/opengl/gl_state.hh @@ -94,7 +94,7 @@ class GLStateManager : public StateManager { static void set_shadow_bias(bool enable); static void set_blend(eGPUBlend value); - void set_state(const GPUState &state); + void set_state(GPUState &state); void set_mutable_state(const GPUStateMutable &state); void texture_bind_apply(); ``` --- (note: seems related to [D15859: Draw manager: Validate the draw state matches framebuffer.](https://archive.blender.org/developer/D15859) in some way).
Philipp Oeser removed the
Interest
EEVEE & Viewport
label 2023-02-09 15:12:35 +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
5 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#98486
No description provided.