"""CPU-side draw layout and draw specifications for :class:`MeshData`.
These types describe *what portion of a mesh to draw*. They contain no OpenGL
objects. A VAO/VBG consumes a :class:`MeshDrawSpec` to issue ``glDraw*``.
"""
from __future__ import annotations
import ctypes
from dataclasses import dataclass
from typing import Any
import numpy as np
from picogl.backend.gl.enums import GLDrawMode
from picogl.backend.modern.core.vertex.array.draw_spec import DrawSpec
[docs]
_UINT32_BYTES = int(np.dtype(np.uint32).itemsize)
@dataclass(frozen=True)
[docs]
class MeshDrawInfo:
"""Persistent topology for a mesh (how items map onto vertices/indices).
:param mode: Primitive mode (``POINTS``, ``LINES``, ``TRIANGLES``, …).
:param indexed: Whether drawing uses an element buffer.
:param elements_per_item: Indices per logical item (atom, bond, …), or
``None`` when the mesh is not instanced.
:param vertices_per_item: Vertices per logical item, used when expanding
per-item attributes such as colors.
"""
[docs]
mode: GLDrawMode = GLDrawMode.TRIANGLES
[docs]
elements_per_item: int | None = None
[docs]
vertices_per_item: int | None = None
@dataclass(frozen=True)
[docs]
class MeshDrawSpec:
"""One GL-free draw: mode, element/vertex count, and EBO byte offset.
:param mode: Primitive mode for ``glDrawArrays`` / ``glDrawElements``.
:param count: Vertex count (non-indexed) or index count (indexed).
:param first: First vertex for non-indexed draws.
:param pointer: Byte offset into the element buffer for indexed draws.
"""
[docs]
def to_draw_spec(self) -> DrawSpec:
"""Convert to the GPU :class:`~picogl.backend.modern.core.vertex.array.draw_spec.DrawSpec`."""
return DrawSpec(
count=self.count,
mode=self.mode,
first=self.first,
pointer=ctypes.c_void_p(int(self.pointer)),
)
[docs]
def infer_draw_info(
*,
has_indices: bool,
index_count: int = 0,
) -> MeshDrawInfo:
"""Infer a conservative layout when the mesh has no explicit ``draw_info``.
Indexed meshes default to triangles; otherwise points. Line-stick bond
meshes must set :class:`MeshDrawInfo` explicitly (``LINES``).
"""
indexed = bool(has_indices and index_count > 0)
mode = GLDrawMode.TRIANGLES if indexed else GLDrawMode.POINTS
return MeshDrawInfo(mode=mode, indexed=indexed)
[docs]
def compute_draw_spec(
info: MeshDrawInfo,
*,
index_count: int = 0,
vertex_count: int = 0,
first_item: int = 0,
item_count: int | None = None,
) -> MeshDrawSpec:
"""Map a logical item range onto a :class:`MeshDrawSpec`.
:param info: Mesh topology.
:param index_count: Length of the element buffer (0 if none).
:param vertex_count: Number of vertices.
:param first_item: First logical item (atom, bond, …).
:param item_count: Number of items; ``None`` draws the remainder.
:return: Draw specification with counts clamped to the buffer.
"""
first_item = max(0, int(first_item))
epp = info.elements_per_item
if epp is not None and int(epp) > 0:
epp = int(epp)
if item_count is None:
if info.indexed and index_count > 0:
item_count = max(0, (index_count - first_item * epp) // epp)
elif info.vertices_per_item:
vpi = int(info.vertices_per_item)
item_count = max(0, (vertex_count - first_item * vpi) // vpi)
else:
item_count = 0
first_index = first_item * epp
count = int(item_count) * epp
if info.indexed and index_count > 0:
count = min(count, max(0, index_count - first_index))
if count <= 0:
return MeshDrawSpec(mode=info.mode, count=0, first=0, pointer=0)
return MeshDrawSpec(
mode=info.mode,
count=count,
first=0 if info.indexed else first_index,
pointer=first_index * _UINT32_BYTES if info.indexed else 0,
)
if info.indexed:
total = int(index_count)
first = first_item
if item_count is None:
count = max(0, total - first)
else:
count = int(item_count)
if total > 0:
count = min(count, max(0, total - first))
return MeshDrawSpec(
mode=info.mode,
count=count,
first=0,
pointer=first * _UINT32_BYTES,
)
total = int(vertex_count)
if item_count is None:
count = max(0, total - first_item)
else:
count = int(item_count)
if total > 0:
count = min(count, max(0, total - first_item))
return MeshDrawSpec(
mode=info.mode,
count=count,
first=first_item,
pointer=0,
)
[docs]
def execute_draw_spec(vao: Any, spec: MeshDrawSpec) -> None:
"""Issue ``VertexArrayObject.draw`` from a resolved :class:`MeshDrawSpec`.
:param vao: GPU vertex array that implements ``draw(...)``.
:param spec: Count/mode/offset for one ``glDraw*`` call.
"""
if spec.count <= 0:
return
vao.draw(spec.to_draw_spec())