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] [vulkan] Surpport for python users to control the start index and count number of particles & meshes data. #5388

Merged
merged 36 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
287656a
Add 2 new functions(get camera view/proj matrix) to Camera Class
Morcki Jul 6, 2022
3beaa94
expose 2 new functions(get camera view/proj matrix) to PyCamera
Morcki Jul 6, 2022
f930551
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2022
06bbc8b
[gui] Add 2 new functions(get camera view/projection matrix) to Camer…
Morcki Jul 6, 2022
7515d70
[gui] expose 2 new functions(get camera view/projection matrix) of Py…
Morcki Jul 6, 2022
0fef90f
add a test for getting the view/projection matrix of camera
Morcki Jul 6, 2022
18d6268
add 2 new functions to get view/projection matrix of camera
Morcki Jul 6, 2022
ea9a45b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2022
d1d866a
expose 2 new functions(get camera view/proj matrix) to PyCamera
Morcki Jul 6, 2022
7cd32d7
expose 2 new functions(get camera view/proj matrix) to PyCamera
Morcki Jul 6, 2022
b28bb81
Update taichi/python/export_ggui.cpp
Morcki Jul 6, 2022
c2eee88
Update taichi/python/export_ggui.cpp
Morcki Jul 6, 2022
74230fb
Update taichi/python/export_ggui.cpp
Morcki Jul 6, 2022
a2965ec
Update taichi/python/export_ggui.cpp
Morcki Jul 6, 2022
eea04db
Update taichi/python/export_ggui.cpp
Morcki Jul 6, 2022
702eddb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2022
ad3dc6f
add test for get the view/projection matrix of camera
Morcki Jul 6, 2022
b22640b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 6, 2022
f1f28a4
add a new function to get image buffer of current window content
Morcki Jul 7, 2022
1409b29
add a new function to get image buffer
Morcki Jul 7, 2022
4bba29b
add an interface for window to get image buffer
Morcki Jul 7, 2022
bc7d262
add a function to store image buffer for other use
Morcki Jul 7, 2022
4f79c65
create a new function for PyWindow to pass the image buffer data
Morcki Jul 7, 2022
fa5de86
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 7, 2022
537f147
Merge branch 'taichi-ggui_feedback_dev'
Morcki Jul 7, 2022
bb0c1ff
fix a bug that data format is not matched with color format when not …
Morcki Jul 7, 2022
586403f
update the mesh/particles api for the Scene class, surpport controlab…
Morcki Jul 11, 2022
d3f3cc5
update new api for renderable class and surpport for controlling the …
Morcki Jul 11, 2022
25c0d85
format conflict, merge from master
Morcki Jul 11, 2022
4cc1e01
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2022
c87af5f
update args name for particles function of Scene class
Morcki Jul 11, 2022
1e875a5
update args name for particles function of Scene class
Morcki Jul 11, 2022
af83b0b
update args name for particles function of Scene class
Morcki Jul 11, 2022
1ec9901
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2022
da15f93
update note about the usage of Scene.mesh
Morcki Jul 11, 2022
fe2d3c7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2022
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
45 changes: 38 additions & 7 deletions python/taichi/ui/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ def mesh(self,
normals=None,
color=(0.5, 0.5, 0.5),
per_vertex_color=None,
two_sided=False):
two_sided=False,
vertex_offset: int = 0,
vertex_count: int = None,
index_offset: int = 0,
index_count: int = None):
"""Declare a mesh inside the scene.

if you indicate the index_offset and index_count, the normals will also
be sliced by the args, and the shading resultes will not be affected.
(It is equal to make a part of the mesh visible)
Args:
vertices: a taichi 3D Vector field, where each element indicate the
3D location of a vertex.
Expand All @@ -112,6 +118,19 @@ def mesh(self,
element indicate the RGB color of a vertex.
two_sided (bool): whether or not the triangles should be able to be
seen from both sides.
vertex_offset: int type(ohterwise float type will be floored to int),
if 'indices' is provided, this means the value added to the vertex
index before indexing into the vertex buffer, else this means the
index of the first vertex to draw.
vertex_count: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is not provided, which is the number
of vertices to draw.
index_offset: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is provided, which is the base index
within the index buffer.
index_count: int type(ohterwise float type will be floored to int),
only avaliable when `indices` is provided, which is the the number
of vertices to draw.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
Expand All @@ -120,18 +139,25 @@ def mesh(self,
copy_colors_to_vbo(vbo, per_vertex_color)
if normals is None:
normals = gen_normals(vertices, indices)
if vertex_count is None:
vertex_count = vertices.shape[0]
if index_count is None:
index_count = indices.shape[0]
copy_normals_to_vbo(vbo, normals)
vbo_info = get_field_info(vbo)
indices_info = get_field_info(indices)

self.scene.mesh(vbo_info, has_per_vertex_color, indices_info, color,
two_sided)
two_sided, index_count, index_offset, vertex_count,
vertex_offset)

def particles(self,
centers,
radius,
color=(0.5, 0.5, 0.5),
per_vertex_color=None):
per_vertex_color=None,
index_offset: int = 0,
index_count: int = None):
"""Declare a set of particles within the scene.

Args:
Expand All @@ -141,16 +167,21 @@ def particles(self,
values. If `per_vertex_color` is provided, this is ignored.
per_vertex_color (Tuple[float]): a taichi 3D vector field, where each
element indicate the RGB color of a particle.
two_sided (bool): whether or not the triangles should be able to be
seen from both sides.
index_offset: int type(ohterwise float type will be floored to int),
the index of the first vertex to draw.
index_count: int type(ohterwise float type will be floored to int),
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_info = get_field_info(vbo)
self.scene.particles(vbo_info, has_per_vertex_color, color, radius)
self.scene.particles(vbo_info, has_per_vertex_color, color, radius,
index_count, index_offset)

def point_light(self, pos, color): # pylint: disable=W0235
"""Set a point light in this scene.
Expand Down
18 changes: 16 additions & 2 deletions taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,20 @@ struct PyScene {
bool has_per_vertex_color,
FieldInfo indices,
py::tuple color,
bool two_sided) {
bool two_sided,
float draw_index_count,
float draw_first_index,
float draw_vertex_count,
float draw_first_vertex) {
RenderableInfo renderable_info;
renderable_info.vbo = vbo;
renderable_info.has_per_vertex_color = has_per_vertex_color;
renderable_info.indices = indices;
renderable_info.has_user_customized_draw = true;
renderable_info.draw_index_count = (int)draw_index_count;
renderable_info.draw_first_index = (int)draw_first_index;
renderable_info.draw_vertex_count = (int)draw_vertex_count;
renderable_info.draw_first_vertex = (int)draw_first_vertex;

MeshInfo info;
info.renderable_info = renderable_info;
Expand All @@ -156,10 +165,15 @@ struct PyScene {
void particles(FieldInfo vbo,
bool has_per_vertex_color,
py::tuple color_,
float radius) {
float radius,
float draw_vertex_count,
float draw_first_vertex) {
RenderableInfo renderable_info;
renderable_info.vbo = vbo;
renderable_info.has_user_customized_draw = true;
renderable_info.has_per_vertex_color = has_per_vertex_color;
renderable_info.draw_vertex_count = (int)draw_vertex_count;
renderable_info.draw_first_vertex = (int)draw_first_vertex;

ParticlesInfo info;
info.renderable_info = renderable_info;
Expand Down
29 changes: 27 additions & 2 deletions taichi/ui/backends/vulkan/renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,45 @@ void Renderable::update_data(const RenderableInfo &info) {
}

int num_vertices = info.vbo.shape[0];
int draw_num_vertices = info.draw_vertex_count;
int draw_first_vertices = info.draw_first_vertex % num_vertices;

int num_indices;
int draw_num_indices;
int draw_first_indices;

if (info.indices.valid) {
TI_ERROR_IF(info.indices.matrix_cols != 1,
"indices must either be a ti.field or a 2D/3D ti.Vector.field");
num_indices = info.indices.shape[0] * info.indices.matrix_rows;
draw_num_indices = info.draw_index_count * info.indices.matrix_rows;
draw_first_indices =
(info.draw_first_index * info.indices.matrix_rows) % num_indices;
if (info.indices.dtype != PrimitiveType::i32 &&
info.indices.dtype != PrimitiveType::u32) {
throw std::runtime_error("dtype needs to be 32-bit ints for indices");
}
} else {
num_indices = 1;
draw_num_indices = 1;
draw_first_indices = 0;
}

config_.vertices_count = num_vertices;
config_.indices_count = num_indices;

if (info.has_user_customized_draw) {
config_.draw_vertex_count = draw_num_vertices;
config_.draw_first_vertex = draw_first_vertices;
config_.draw_index_count = draw_num_indices;
config_.draw_first_index = draw_first_indices;
} else {
config_.draw_vertex_count = num_vertices;
config_.draw_first_vertex = 0;
config_.draw_index_count = num_indices;
config_.draw_first_index = 0;
}

if (num_vertices > config_.max_vertices_count ||
num_indices > config_.max_indices_count) {
free_buffers();
Expand Down Expand Up @@ -252,9 +275,11 @@ void Renderable::record_this_frame_commands(CommandList *command_list) {
command_list->bind_resources(pipeline_->resource_binder());

if (indexed_) {
command_list->draw_indexed(config_.indices_count, 0, 0);
command_list->draw_indexed(config_.draw_index_count,
config_.draw_first_vertex,
config_.draw_first_index);
} else {
command_list->draw(config_.vertices_count, 0);
command_list->draw(config_.draw_vertex_count, config_.draw_first_vertex);
}
}

Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct RenderableConfig {
int max_indices_count{0};
int vertices_count{0};
int indices_count{0};
int draw_vertex_count{0};
int draw_first_vertex{0};
int draw_index_count{0};
int draw_first_index{0};
size_t ubo_size{0};
size_t ssbo_size{0};
bool blending{false};
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/circles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ void Circles::init_circles(AppContext *app_context,
1,
vertices_count,
1,
vertices_count,
0,
1,
0,
sizeof(UniformBufferObject),
0,
true,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/lines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ void Lines::init_lines(AppContext *app_context,
indices_count,
vertices_count,
indices_count,
vertices_count,
0,
indices_count,
0,
sizeof(UniformBufferObject),
0,
true,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ void Mesh::init_mesh(AppContext *app_context,
indices_count,
vertices_count,
indices_count,
vertices_count,
0,
indices_count,
0,
sizeof(UniformBufferObject),
1,
true,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ void Particles::init_particles(AppContext *app_context,
1,
vertices_count,
1,
vertices_count,
0,
1,
0,
sizeof(UniformBufferObject),
1,
true,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/set_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void SetImage::init_set_image(AppContext *app_context,
6,
6,
6,
6,
0,
6,
0,
sizeof(UniformBufferObject),
0,
false,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/renderables/triangles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ void Triangles::init_triangles(AppContext *app_context,
indices_count,
vertices_count,
indices_count,
vertices_count,
0,
indices_count,
0,
sizeof(UniformBufferObject),
0,
true,
Expand Down
5 changes: 5 additions & 0 deletions taichi/ui/common/renderable_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct RenderableInfo {
FieldInfo indices;
bool has_per_vertex_color{false};
VertexAttributes vbo_attrs{VboHelpers::all()};
bool has_user_customized_draw{false};
int draw_vertex_count{0};
int draw_first_vertex{0};
int draw_index_count{0};
int draw_first_index{0};
};

TI_UI_NAMESPACE_END