blender/scripts/modules/gpu_extras/batch.py
Testfiya a1de44dd54 iOS: Bump version to 5.1.2
This commit bumps the `ios` branch from 5.0.0 alpha to 5.1.2 release.

I merged v5.0-release first then v5.1-release. So it was a sequential
merge.

I'm just waiting to the approval of continuing development.

The stuff that are missing (those are so close to implement):
- #145484 iOS: Keyboard Input Support: https://projects.blender.org/blender/blender/pulls/145484
- #145702 iOS: Add clipboard Image: https://projects.blender.org/blender/blender/pulls/145702
- Fix QOS threading
- Expose Downloads
- UIScene Migration

`make deps` needs to run two times. (still hacky....) OSL occurs racing
conditions.

Disclaimer: I used AI for searching in Documentation of Deps. So the
implementation is by hand. **SACRED PINKY FINGER PROMISE**

Pull Request: https://projects.blender.org/blender/blender/pulls/161073
2026-07-07 16:03:12 +02:00

80 lines
2.6 KiB
Python

# SPDX-FileCopyrightText: 2018-2023 Blender Authors
#
# SPDX-License-Identifier: GPL-2.0-or-later
__all__ = (
"batch_for_shader",
)
def batch_for_shader(shader, type, content, *, indices=None):
"""
Return a batch already configured and compatible with the shader.
:param shader: shader for which a compatible format will be computed.
:type shader: :class:`gpu.types.GPUShader`
:param type: The primitive type of batch geometry.
:type type: Literal['POINTS', 'LINES', 'TRIS', 'LINE_STRIP', 'TRI_STRIP', 'LINES_ADJ', 'TRIS_ADJ', 'LINE_STRIP_ADJ']
:param content: Maps the name of the shader attribute with the data to fill the vertex buffer.
For the dictionary values see documentation for :class:`gpu.types.GPUVertBuf.attr_fill` data argument.
:type content: dict[str, Buffer | Sequence[float] | Sequence[int] | \
Sequence[Sequence[float]] | Sequence[Sequence[int]]]
:return: compatible batch
:rtype: :class:`gpu.types.GPUBatch`
"""
from gpu.types import (
GPUBatch,
GPUIndexBuf,
GPUVertBuf,
GPUVertFormat,
)
def recommended_comp_type(attr_type):
if attr_type in {'FLOAT', 'VEC2', 'VEC3', 'VEC4', 'MAT3', 'MAT4'}:
return 'F32'
if attr_type in {'UINT', 'UVEC2', 'UVEC3', 'UVEC4'}:
return 'U32'
# `attr_type` in {'INT', 'IVEC2', 'IVEC3', 'IVEC4', 'BOOL'}.
return 'I32'
def recommended_attr_len(attr_name):
attr_len = 1
try:
item = content[attr_name][0]
while True:
attr_len *= len(item)
item = item[0]
except (TypeError, IndexError):
pass
return attr_len
def recommended_fetch_mode(comp_type):
if comp_type == 'F32':
return 'FLOAT'
return 'INT'
for data in content.values():
vbo_len = len(data)
break
else:
raise ValueError("Empty 'content'")
vbo_format = GPUVertFormat()
attrs_info = shader.attrs_info_get()
for name, attr_type in attrs_info:
comp_type = recommended_comp_type(attr_type)
attr_len = recommended_attr_len(name)
vbo_format.attr_add(id=name, comp_type=comp_type, len=attr_len, fetch_mode=recommended_fetch_mode(comp_type))
vbo = GPUVertBuf(vbo_format, vbo_len)
for id, data in content.items():
if len(data) != vbo_len:
raise ValueError("Length mismatch for 'content' values")
vbo.attr_fill(id, data)
if indices is None:
return GPUBatch(type=type, buf=vbo)
else:
ibo = GPUIndexBuf(type=type, seq=indices)
return GPUBatch(type=type, buf=vbo, elem=ibo)