Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gui] Use templated bulk copy to simplify VBO preperation #7234

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions python/taichi/ui/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from taichi.lang import impl
from taichi.lang._texture import Texture

from .staging_buffer import (copy_colors_to_vbo, copy_vertices_to_vbo,
get_vbo_field, to_rgba8)
from .staging_buffer import copy_all_to_vbo, get_vbo_field, to_rgba8
from .utils import get_field_info


Expand Down Expand Up @@ -62,10 +61,9 @@ def triangles(self,
where each element indicate the RGB color of a vertex.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
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_info = get_field_info(indices)
self.canvas.triangles(vbo_info, indices_info, has_per_vertex_color,
Expand All @@ -92,10 +90,9 @@ def lines(self,
each element indicate the RGB color of a vertex.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
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_info = get_field_info(indices)
self.canvas.lines(vbo_info, indices_info, has_per_vertex_color, color,
Expand All @@ -118,10 +115,9 @@ def circles(self,
each element indicate the RGB color of a circle.
"""
vbo = get_vbo_field(centers)
copy_vertices_to_vbo(vbo, centers)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
copy_all_to_vbo(vbo, centers, 0, 0,
per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
self.canvas.circles(vbo_info, has_per_vertex_color, color, radius)

Expand Down
33 changes: 13 additions & 20 deletions python/taichi/ui/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from taichi.types.annotations import template
from taichi.types.primitive_types import f32

from .staging_buffer import (copy_colors_to_vbo, copy_normals_to_vbo,
copy_vertices_to_vbo, get_vbo_field)
from .staging_buffer import copy_all_to_vbo, get_vbo_field
from .utils import check_ggui_availability, get_field_info

normals_field_cache = {}
Expand Down Expand Up @@ -149,11 +148,10 @@ def lines(self,
print("Warning! Drawing count greater than shape will be cut")
vertex_count = vertices.shape[0] - vertex_offset
vertex_count -= vertex_count % 2
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
vbo = get_vbo_field(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_info = get_field_info(indices)
self.scene.lines(vbo_info, indices_info, has_per_vertex_color, color,
Expand Down Expand Up @@ -209,11 +207,7 @@ def mesh(self,
show_wireframe (bool, optional):
turn on/off WareFrame mode.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
if normals is None:
normals = gen_normals(vertices, indices)
if vertex_count is None:
Expand All @@ -223,7 +217,9 @@ def mesh(self,
index_count = vertex_count # FIXME : Need to confirm
else:
index_count = indices.shape[0]
copy_normals_to_vbo(vbo, normals)
vbo = get_vbo_field(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_info = get_field_info(indices)

Expand Down Expand Up @@ -293,11 +289,7 @@ def mesh_instance(self,
show_wireframe (bool, optional):
turn on/off WareFrame mode.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
if normals is None:
normals = gen_normals(vertices, indices)
if vertex_count is None:
Expand All @@ -315,7 +307,9 @@ def mesh_instance(self,
else:
instance_count = 1

copy_normals_to_vbo(vbo, normals)
vbo = get_vbo_field(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_info = get_field_info(indices)
transform_info = get_field_info(transforms)
Expand Down Expand Up @@ -346,13 +340,12 @@ def particles(self,
index_count (int, optional):
the number of vertices to draw.
"""
vbo = get_vbo_field(centers)
copy_vertices_to_vbo(vbo, centers)
has_per_vertex_color = per_vertex_color is not None
if has_per_vertex_color:
copy_colors_to_vbo(vbo, per_vertex_color)
if index_count is None:
index_count = centers.shape[0]
vbo = get_vbo_field(centers)
copy_all_to_vbo(vbo, centers, 0, 0,
per_vertex_color if has_per_vertex_color else 0)
vbo_info = get_field_info(vbo)
self.scene.particles(vbo_info, has_per_vertex_color, color, radius,
index_count, index_offset)
Expand Down
31 changes: 18 additions & 13 deletions python/taichi/ui/staging_buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ def get_depth_ndarray(window):


@kernel
def copy_to_vbo(vbo: template(), src: template(), offset: template(),
num_components: template()):
for i in src:
for c in ti.static(range(num_components)):
vbo[i][offset + c] = src[i][c]


@kernel
def fill_vbo(vbo: template(), value: f32, offset: template(),
num_components: template()):
for i in vbo:
for c in ti.static(range(num_components)):
vbo[i][offset + c] = value
def copy_all_to_vbo(vbo: ti.template(), vertex: template(), normal: template(),
bobcao3 marked this conversation as resolved.
Show resolved Hide resolved
texcoords: template(), color: template()):
for i in vertex:
if ti.static(vertex.n == 3):
vbo[i][0:3] = vertex[i]
else:
vbo[i][0:2] = vertex[i]
vbo[i][3] = 0.0
if ti.static(normal != 0):
vbo[i][3:6] = normal[i]
if ti.static(texcoords != 0):
vbo[i][6:8] = texcoords[i]
if ti.static(color != 0):
if ti.static(color.n == 3):
vbo[i][8:11] = color[i]
vbo[i][11] = 1.0
else:
vbo[i][8:12] = color[i]


def validate_input_field(f, name):
Expand Down