Metal: Assert is triggered when drawing a Batch with an attribute format different from the type used in the Shader #111370
Operating system: macOS-13.3.1-arm64-arm-64bit 64 Bits
Graphics card: Metal API Apple M1 1.2
Broken: version: 4.0.0 Alpha, branch: main, commit date: 2023-08-21 22:52, hash:
Worked: [couldn't check yet]
Short description of error
I'm not sure how serious the problem is.
Even with the assert, the drawing seems to work.
Below is a small code that demonstrates the problem:
import bpy import gpu from gpu.types import ( GPUBatch, GPUIndexBuf, GPUVertBuf, GPUVertFormat, ) def draw(): x1=0 y1=0 x2=bpy.context.region.width y2=bpy.context.region.height # The size of each color in `vertex_colors` needs to be 4 and # the size of each vector in `vertices` needs to be 3 # otherwise a assert will be triggered the Metal backend. vertex_colors = ((1.0, 0.0, 0.0, 1.0), (1.0, 1.0, 1.0, 1.0)) vertices = ((x1, y1), (x2, y2)) shader = gpu.shader.from_builtin('POLYLINE_SMOOTH_COLOR') shader.uniform_float("viewportSize", gpu.state.viewport_get()[2:]) shader.uniform_float("lineWidth", 5) vbo_format = GPUVertFormat() vbo_format.attr_add(id='color', comp_type='F32', len=len(vertex_colors), fetch_mode='FLOAT') vbo_format.attr_add(id='pos', comp_type='F32', len=len(vertices), fetch_mode='FLOAT') vbo = GPUVertBuf(vbo_format, len(vertices)) vbo.attr_fill('color', vertex_colors) vbo.attr_fill('pos', vertices) batch = GPUBatch(type='LINE_STRIP', buf=vbo) batch.draw(shader) del batch bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
Note that the dimension of the vertices is 2D.
If converted to 3D (by adding a z value), the assert is no longer triggered.
Exact steps for others to reproduce the error
- Open attached .blend file.
- Run the script in the text editor
- Update the 3D view (by zooming for example)
I noticed this assert while investigating blender/blender-addons#104744
It was added in
Note that if it is not possible to resolve in the backend, I can make edits in the python
batch_for_shader utility, and avoid this.
@mano-wii Thanks for the report. The asserts in these cases were generally added to ensure that a given format conversion is supported. Some conversions are supported implicitly (generally type truncations or expansions e.g. vec3->vec4), or normalizations between UNorm and float.
However, some need to be implemented explicitly. The asserts are generally in places where conversions are not yet accounted for.
In some cases, the conversion may still be implemented, but the flag still exists as it's not an ideal compatibility path which could lose precision etc;
I'll look at this particular case. It may be a situation where a new conversion can be added to the 3D poly line shader, or if it is already supported, the assertion could be tightened to avoid the case, if it otherwise appears to render correctly.
No due date set.
No dependencies set.
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?