Skip to content

Commit

Permalink
[gui] Remove cached arrays (#8223)
Browse files Browse the repository at this point in the history
Issue: #

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at d18381f</samp>

Refactored `staging_buffer.py` to fix memory leaks and optimize array
access. Removed global caches and simplified array functions.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at d18381f</samp>

* Remove global caches for depth, vbo, indices, and transforms arrays to
avoid memory leaks and overhead
([link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L12-R27),
[link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L44),
[link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L52))
* Return arrays directly from `get_depth_ndarray`, `get_vbo_field`,
`get_indices_field`, and `get_transforms_field` functions in
`staging_buffer.py`
([link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L12-R27),
[link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L44),
[link](https://github.com/taichi-dev/taichi/pull/8223/files?diff=unified&w=0#diff-fd7dee80cc57fcef55c9bedc67a63a5aee4f20ac0375921bebec55beeb5bf928L52))

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
AntonioFerreras and pre-commit-ci[bot] authored Jun 28, 2023
1 parent 810653c commit a1c222d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
18 changes: 12 additions & 6 deletions python/taichi/ui/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from taichi.lang._texture import Texture
from .scene import SceneV2

from .staging_buffer import copy_all_to_vbo, copy_all_to_vbo_particle, get_indices_field, get_vbo_field, to_rgba8
from .staging_buffer import (
copy_all_to_vbo,
copy_all_to_vbo_particle,
get_indices_field_v2,
get_vbo_field_v2,
to_rgba8,
)
from .utils import get_field_info


Expand Down Expand Up @@ -97,13 +103,13 @@ def triangles(self, vertices, color=(0.5, 0.5, 0.5), indices=None, per_vertex_co
per_vertex_color (Tuple[float]): a taichi 3D vector field, \
where each element indicate the RGB color of a vertex.
"""
vbo = get_vbo_field(vertices)
vbo = get_vbo_field_v2(vertices)
has_per_vertex_color = per_vertex_color is not None
copy_all_to_vbo(vbo, vertices, 0, 0, per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
indices_ndarray = None
if indices:
indices_ndarray = get_indices_field(indices)
indices_ndarray = get_indices_field_v2(indices)
indices_info = get_field_info(indices_ndarray)
self.canvas.triangles(vbo_info, indices_info, has_per_vertex_color, color)

Expand All @@ -129,13 +135,13 @@ def lines(
per_vertex_color (tuple[float]): a taichi 3D vector field, where \
each element indicate the RGB color of a vertex.
"""
vbo = get_vbo_field(vertices)
vbo = get_vbo_field_v2(vertices)
has_per_vertex_color = per_vertex_color is not None
copy_all_to_vbo(vbo, vertices, 0, 0, per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
indices_ndarray = None
if indices:
indices_ndarray = get_indices_field(indices)
indices_ndarray = get_indices_field_v2(indices)
indices_info = get_field_info(indices_ndarray)
self.canvas.lines(vbo_info, indices_info, has_per_vertex_color, color, width)

Expand All @@ -153,7 +159,7 @@ def circles(self, centers, radius, color=(0.5, 0.5, 0.5), per_vertex_color=None,
per_vertex_radius: a taichi float field, where \
each element indicates the radius of a circle.
"""
vbo = get_vbo_field(centers)
vbo = get_vbo_field_v2(centers)
has_per_vertex_color = per_vertex_color is not None
has_per_vertex_radius = per_vertex_radius is not None
copy_all_to_vbo_particle(
Expand Down
19 changes: 11 additions & 8 deletions python/taichi/ui/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
copy_all_to_vbo,
copy_all_to_vbo_particle,
get_indices_field,
get_indices_field_v2,
get_transforms_field,
get_transforms_field_v2,
get_vbo_field,
get_vbo_field_v2,
)
from .utils import check_ggui_availability, get_field_info

Expand Down Expand Up @@ -157,10 +160,10 @@ def lines(
vertex_count = vertices.shape[0] - vertex_offset
vertex_count -= vertex_count % 2
has_per_vertex_color = per_vertex_color is not None
vbo = get_vbo_field(vertices)
vbo = get_vbo_field_v2(vertices)
copy_all_to_vbo(vbo, vertices, 0, 0, per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
indices_ndarray = get_indices_field(indices) if indices is not None else None
indices_ndarray = get_indices_field_v2(indices) if indices is not None else None
indices_info = get_field_info(indices_ndarray)
self.scene.lines(
vbo_info,
Expand Down Expand Up @@ -235,10 +238,10 @@ def mesh(
index_count = vertex_count # FIXME : Need to confirm
else:
index_count = indices.shape[0]
vbo = get_vbo_field(vertices)
vbo = get_vbo_field_v2(vertices)
copy_all_to_vbo(vbo, vertices, normals, 0, per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
indices_ndarray = get_indices_field(indices) if indices is not None else None
indices_ndarray = get_indices_field_v2(indices) if indices is not None else None
indices_info = get_field_info(indices_ndarray)

self.scene.mesh(
Expand Down Expand Up @@ -336,12 +339,12 @@ def mesh_instance(
else:
instance_count = 1

vbo = get_vbo_field(vertices)
vbo = get_vbo_field_v2(vertices)
copy_all_to_vbo(vbo, vertices, normals, 0, per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
indices_ndarray = get_indices_field(indices) if indices else None
indices_ndarray = get_indices_field_v2(indices) if indices else None
indices_info = get_field_info(indices_ndarray)
transforms_ndarray = get_transforms_field(transforms) if transforms else None
transforms_ndarray = get_transforms_field_v2(transforms) if transforms else None
transform_info = get_field_info(transforms_ndarray)
self.scene.mesh_instance(
vbo_info,
Expand Down Expand Up @@ -392,7 +395,7 @@ def particles(
if index_count is None:
index_count = centers.shape[0]
# per_vertex_radius_vec3[i] = Vector([per_vertex_radius[i], 0., 0.])
vbo = get_vbo_field(centers)
vbo = get_vbo_field_v2(centers)
copy_all_to_vbo_particle(
vbo,
centers,
Expand Down
25 changes: 25 additions & 0 deletions python/taichi/ui/staging_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ def get_vbo_field(vertices):
return vbo_field_cache[vertices]


def get_vbo_field_v2(vertices):
N = vertices.shape[0]
pos = 3
normal = 3
tex_coord = 2
color = 4
vertex_stride = pos + normal + tex_coord + color
vbo = np.ndarray((N, vertex_stride), dtype=np.float32)
return vbo


def get_indices_field(indices):
if isinstance(indices, np.ndarray):
return indices
Expand All @@ -45,6 +56,13 @@ def get_indices_field(indices):
return indices_arr


def get_indices_field_v2(indices):
if isinstance(indices, np.ndarray):
return indices
indices_arr = indices.to_numpy()
return indices_arr


def get_transforms_field(transforms):
if isinstance(transforms, np.ndarray):
return transforms
Expand All @@ -53,6 +71,13 @@ def get_transforms_field(transforms):
return transforms_arr


def get_transforms_field_v2(transforms):
if isinstance(transforms, np.ndarray):
return transforms
transforms_arr = transforms.to_numpy()
return transforms_arr


@kernel
def copy_all_to_vbo(
vbo: ndarray_type(element_dim=1),
Expand Down

0 comments on commit a1c222d

Please sign in to comment.