Metal: Assert is triggered when drawing a Batch with an attribute format different from the type used in the Shader #111370

Open
opened 2023-08-22 05:02:30 +02:00 by Germano Cavalcante · 2 comments

System Information
Operating system: macOS-13.3.1-arm64-arm-64bit 64 Bits
Graphics card: Metal API Apple M1 1.2

Blender Version
Broken: version: 4.0.0 Alpha, branch: main, commit date: 2023-08-21 22:52, hash: d45f47a809dd
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[0]), fetch_mode='FLOAT')
    vbo_format.attr_add(id='pos', comp_type='F32', len=len(vertices[0]), 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)

gpu_metal_assert__bug.blend

**System Information** Operating system: macOS-13.3.1-arm64-arm-64bit 64 Bits Graphics card: Metal API Apple M1 1.2 **Blender Version** Broken: version: 4.0.0 Alpha, branch: main, commit date: 2023-08-21 22:52, hash: `d45f47a809dd` 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: ```python 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[0]), fetch_mode='FLOAT') vbo_format.attr_add(id='pos', comp_type='F32', len=len(vertices[0]), 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) [gpu_metal_assert__bug.blend](/attachments/40ab81e4-5bfd-41cd-a536-5ecb90bcdbdc)
Germano Cavalcante added the
Type
Report
Severity
Normal
Status
Needs Triage
labels 2023-08-22 05:02:31 +02:00
Author
Member

I noticed this assert while investigating blender/blender-addons#104744

It was added in aed301704a

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.

I noticed this assert while investigating blender/blender-addons#104744 It was added in aed301704a 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.

@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.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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
2 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#111370
No description provided.