Regression: 3D shader 3D_POLYLINE_UNIFORM_COLOR draw error (the need to set all shader uniforms need to be documented) #103176

Open
opened 2022-12-13 05:03:56 +01:00 by EMM · 22 comments

System Information
Operating system: Windows-10-10.0.22000-SP0 64 Bits
Graphics card: NVIDIA GeForce RTX 3070 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 526.98

Blender Version
Broken: version: 3.1.1, 3.5
Worked: 3.1.0

Behaviour changed in blender/blender@49fc4449e7

Short description of error
draw shader error

image.png
It's normal:image.png
Exact steps for others to reproduce the error
Run the following code block and use the move gizmo. Drag or directly draw errors

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords})


def draw():
    shader.uniform_float("color", (1, 1, 0, 1))
    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

**System Information** Operating system: Windows-10-10.0.22000-SP0 64 Bits Graphics card: NVIDIA GeForce RTX 3070 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 526.98 **Blender Version** Broken: version: 3.1.1, 3.5 Worked: 3.1.0 Behaviour changed in blender/blender@49fc4449e7 **Short description of error** draw shader error ![image.png](https://archive.blender.org/developer/F14054952/image.png) It's normal:![image.png](https://archive.blender.org/developer/F14054958/image.png) **Exact steps for others to reproduce the error** Run the following code block and use the move gizmo. Drag or directly draw errors ``` import bpy import gpu from gpu_extras.batch import batch_for_shader coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)] shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') batch = batch_for_shader(shader, 'LINES', {"pos": coords}) def draw(): shader.uniform_float("color", (1, 1, 0, 1)) batch.draw(shader) bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') ```
Author

Added subscriber: @Guai_Wo_Ge_EMM

Added subscriber: @Guai_Wo_Ge_EMM

blender/blender#87118 was marked as duplicate of this issue

blender/blender#87118 was marked as duplicate of this issue
Pratik Borhade was assigned by EMM 2022-12-14 11:27:29 +01:00
Member

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

Changed status from 'Needs Triage' to: 'Confirmed'
Pratik Borhade removed their assignment 2022-12-14 11:55:32 +01:00
Member

Added subscriber: @PratikPB2123

Added subscriber: @PratikPB2123
Pratik Borhade changed title from 3D shader 3D_POLYLINE_UNIFORM_COLOR draw error to Regression: 3D shader 3D_POLYLINE_UNIFORM_COLOR draw error 2022-12-14 11:55:41 +01:00
Member

Added subscribers: @ideasman42, @lichtwerk

Added subscribers: @ideasman42, @lichtwerk
Member

Behaviour changed in blender/blender@49fc4449e7

CC @ideasman42

Said commit removed the drawing with GPU_SHADER_3D_UNIFORM_COLOR on the select pass (and instead always draws with GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR now.
P3415 reverts that part (and it seems to work), but havent checked in depth...

Behaviour changed in blender/blender@49fc4449e7 CC @ideasman42 Said commit removed the drawing with `GPU_SHADER_3D_UNIFORM_COLOR` on the select pass (and instead always draws with `GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR` now. [P3415](https://archive.blender.org/developer/P3415.txt) reverts that part (and it seems to work), but havent checked in depth...

Changed status from 'Confirmed' to: 'Archived'

Changed status from 'Confirmed' to: 'Archived'

I don't think this is a bug: lineWidth and viewportSize uniforms need to be set.

This example works as expected:

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords})


def draw():
    region = bpy.context.region
    shader.uniform_float("lineWidth", 8)
    shader.uniform_float("viewportSize", (region.width, region.height))
    shader.uniform_float("color", (1, 1, 0, 1))
    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')
I don't think this is a bug: `lineWidth` and `viewportSize` uniforms need to be set. This example works as expected: ``` import bpy import gpu from gpu_extras.batch import batch_for_shader coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)] shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') batch = batch_for_shader(shader, 'LINES', {"pos": coords}) def draw(): region = bpy.context.region shader.uniform_float("lineWidth", 8) shader.uniform_float("viewportSize", (region.width, region.height)) shader.uniform_float("color", (1, 1, 0, 1)) batch.draw(shader) bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') ```
Member

@ideasman42 : OK, should this be mentioned in the documentation though ?

@ideasman42 : OK, should this be mentioned in the documentation though ?

Added subscriber: @fclem

Added subscriber: @fclem

@lichtwerk this seems more like a general issue with documentation.
As I understand it the docs should state prominently stated that all uniforms need to be set, failure to do so may use a value from the last user of the uniform (which could be Blender or another script).

@fclem is this the case? or are there exceptions to this rule?

@lichtwerk this seems more like a general issue with documentation. As I understand it the docs should state prominently stated that all uniforms need to be set, failure to do so may use a value from the last user of the uniform (which could be Blender or another script). @fclem is this the case? or are there exceptions to this rule?

@ideasman42 yes this is correct. There is no exceptions as far as I can remember. But you can use that to you advantage when drawing multiple things with the same shader, changing only the uniforms that need to be changed.

@ideasman42 yes this is correct. There is no exceptions as far as I can remember. But you can use that to you advantage when drawing multiple things with the same shader, changing only the uniforms that need to be changed.
Member

Changed status from 'Archived' to: 'Confirmed'

Changed status from 'Archived' to: 'Confirmed'
Member

To not forget about this, will repoen as a TODO for documentation.

To not forget about this, will repoen as a TODO for documentation.
Philipp Oeser changed title from Regression: 3D shader 3D_POLYLINE_UNIFORM_COLOR draw error to Regression: 3D shader 3D_POLYLINE_UNIFORM_COLOR draw error (the need to set all shader uniforms need to be documented) 2023-01-06 10:46:16 +01:00

Added subscriber: @Michael-Parkin-White-Apple

Added subscriber: @Michael-Parkin-White-Apple

As an additional note, since shaders have also been moved to GPUShaderCreateInfo, default uniform values are no longer valid in any case. Some shaders previously assigned default values directly in the uniform declaration. This feature removal was a requirement as this was not possible in Metal, when uniform values are always pulled directly from a GPU buffer or whole data block in the command stream.

Hence, if there are any other cases in Python add-ons which relied on default values being set, these may also no longer work as expected, especially if the default value is non-zero.

As an additional note, since shaders have also been moved to GPUShaderCreateInfo, default uniform values are no longer valid in any case. Some shaders previously assigned default values directly in the uniform declaration. This feature removal was a requirement as this was not possible in Metal, when uniform values are always pulled directly from a GPU buffer or whole data block in the command stream. Hence, if there are any other cases in Python add-ons which relied on default values being set, these may also no longer work as expected, especially if the default value is non-zero.
Member
Added subscribers: @MikhailRachinskiy, @Solstice245, @mano-wii, @Harley

@lichtwerk Thanks for directing me here. Campbell's short demonstration that they posted above was enough to figure out what I was doing wrong. Stick it in the docs somewhere maybe where people should expect to find that kinda thing ;p

@lichtwerk Thanks for directing me here. Campbell's short demonstration that they posted above was enough to figure out what I was doing wrong. Stick it in the docs somewhere maybe where people should expect to find that kinda thing ;p
Aaron Carlisle removed the
Module
Eevee & Viewport
Module
Python API
Module
User Interface
labels 2023-02-08 03:56:16 +01:00

I don't think this is a bug: lineWidth and viewportSize uniforms need to be set.

This example works as expected:

import bpy
import gpu
from gpu_extras.batch import batch_for_shader

coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)]
shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR')
batch = batch_for_shader(shader, 'LINES', {"pos": coords})


def draw():
    region = bpy.context.region
    shader.uniform_float("lineWidth", 8)
    shader.uniform_float("viewportSize", (region.width, region.height))
    shader.uniform_float("color", (1, 1, 0, 1))
    batch.draw(shader)


bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW')

Let me join your conversation.
So now to get anti-aliasing to work we need to complicate draw shader with additional calculations?
Since gpu.state.blend_set("ALPHA") works only with POLYLINE_UNIFORM_COLOR or POLYLINE_SMOOTH_COLOR as I know.
Is it heavier than old method?

Also, where we can get this 3D_POLYLINE_UNIFORM_COLOR? in the docs there is nothing https://docs.blender.org/api/current/gpu.shader.html

> I don't think this is a bug: `lineWidth` and `viewportSize` uniforms need to be set. > > This example works as expected: > > ``` > import bpy > import gpu > from gpu_extras.batch import batch_for_shader > > coords = [(1, 1, 1), (-2, 0, 0), (-2, -1, 3), (0, 1, 1)] > shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') > batch = batch_for_shader(shader, 'LINES', {"pos": coords}) > > > def draw(): > region = bpy.context.region > shader.uniform_float("lineWidth", 8) > shader.uniform_float("viewportSize", (region.width, region.height)) > shader.uniform_float("color", (1, 1, 0, 1)) > batch.draw(shader) > > > bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_VIEW') > ``` Let me join your conversation. So now to get anti-aliasing to work we need to complicate draw shader with additional calculations? Since `gpu.state.blend_set("ALPHA")` works only with `POLYLINE_UNIFORM_COLOR` or `POLYLINE_SMOOTH_COLOR` as I know. Is it heavier than old method? Also, where we can get this `3D_POLYLINE_UNIFORM_COLOR`? in the docs there is nothing https://docs.blender.org/api/current/gpu.shader.html
Contributor

So now to get anti-aliasing to work we need to complicate draw shader with additional calculations?

Yes, now you need to provide lineWidth and viewportSize uniforms and if you're using custom shader you also need to implement something similar to polylines yourself.

I'm not sure if it's heavier after all but now it does create 2 vertex for each line vertex to define line thickness and add some blending for anti-aliasing.

Code for builtins polyline shaders is here - https://projects.blender.org/blender/blender/src/branch/main/source/blender/gpu/shaders

Also, where we can get this 3D_POLYLINE_UNIFORM_COLOR? in the docs there is nothing

The same way as there is nothing about 3D_UNIFORM_COLOR.

>So now to get anti-aliasing to work we need to complicate draw shader with additional calculations? Yes, now you need to provide `lineWidth` and `viewportSize` uniforms and if you're using custom shader you also need to implement something similar to polylines yourself. I'm not sure if it's heavier after all but now it does create 2 vertex for each line vertex to define line thickness and add some blending for anti-aliasing. Code for builtins polyline shaders is here - https://projects.blender.org/blender/blender/src/branch/main/source/blender/gpu/shaders >Also, where we can get this 3D_POLYLINE_UNIFORM_COLOR? in the docs there is nothing The same way as there is nothing about `3D_UNIFORM_COLOR`.

I don't think this should be considered a documentation error.
All builtins shader uniforms are indicated for example in: https://docs.blender.org/api/current/gpu.shader.html#module-gpu.shader

As with any parameter, a coder should already know that not setting a required value can result in undefined behavior.

It is not clear from this report how the documentation should be improved.

I don't think this should be considered a documentation error. All builtins shader uniforms are indicated for example in: https://docs.blender.org/api/current/gpu.shader.html#module-gpu.shader As with any parameter, a coder should already know that not setting a required value can result in undefined behavior. It is not clear from this report how the documentation should be improved.

A statement at the top of https://docs.blender.org/api/current/gpu.shader.html noting that uniforms are expected to be initialized by the scripts that use them would help.

A statement at the top of https://docs.blender.org/api/current/gpu.shader.html noting that uniforms are expected to be initialized by the scripts that use them would help.
Aaron Carlisle added the
Module
User Interface
label 2023-08-13 14:38:13 +02:00
Aaron Carlisle added
Module
Eevee & Viewport
and removed
Module
User Interface
labels 2023-08-13 15:15:50 +02:00
Aaron Carlisle added
Module
Python API
and removed
Module
Eevee & Viewport
labels 2023-08-13 15:30:48 +02:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
11 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-manual#103176
No description provided.