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] Add wareframe mode for mesh & mesh_instance, add slider_int for Window.GUI. #5576

Merged
merged 29 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
7809e82
expose Polygon type and support for resetting graphics pipeline
Morcki Jul 29, 2022
e91a782
Support 3 kinds of display mode(0:Fill,1:Line,2:Point) for Scene.mesh…
Morcki Jul 29, 2022
46d24b1
Set default background color
Morcki Jul 29, 2022
e12517e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 29, 2022
9c7cfa3
Add slider_int to support int type value
Morcki Jul 31, 2022
9a74720
remove background color cause it would affect the some tests of verif…
Morcki Jul 31, 2022
01f8ac3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 31, 2022
a37bd6b
expose enum class of PolygonMode to python scope naming as DisplayMode
Morcki Aug 1, 2022
6ee70bb
change print-exit to raise exception
Morcki Aug 1, 2022
74f7ac1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2022
b9c4fb2
fix python format
Morcki Aug 1, 2022
f0d36c9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 1, 2022
498f1bc
fix when not support GGUI
Morcki Aug 1, 2022
036bfed
Update taichi/ui/backends/vulkan/renderable.cpp
Morcki Aug 2, 2022
0ed87b5
fix some small questions
Morcki Aug 2, 2022
a14a0cd
fix small questions
Morcki Aug 2, 2022
ea76f87
Add control the count of instances
Morcki Aug 2, 2022
766bf35
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
911c997
Update python/taichi/ui/scene.py
Morcki Aug 2, 2022
b5d4b3a
fix the implenmtation of indicating count of instances
Morcki Aug 2, 2022
1e9d4be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
8aac7e3
fix count of instances
Morcki Aug 2, 2022
e30b3be
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
a9d89e5
fix count of instances
Morcki Aug 2, 2022
e1f5e47
fix count of instances
Morcki Aug 2, 2022
b39681c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
ea81f81
drop display mode, change it to show_warefram
Morcki Aug 3, 2022
2b91e46
change display_mode to show_wareframe
Morcki Aug 3, 2022
266984f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 3, 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
14 changes: 14 additions & 0 deletions python/taichi/ui/imgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ def checkbox(self, text, old_value):
"""
return self.gui.checkbox(text, old_value)

def slider_int(self, text, old_value, minimum, maximum):
"""Declares a slider, and returns its newest value.

Args:
text (str): a line of text to be shown next to the slider
old_value (int) : the current value of the slider.
minimum (int): the minimum value of the slider.
maximum (int): the maximum value of the slider.

Returns:
int: the updated value of the slider.
"""
return self.gui.slider_int(text, old_value, minimum, maximum)

def slider_float(self, text, old_value, minimum, maximum):
"""Declares a slider, and returns its newest value.

Expand Down
30 changes: 22 additions & 8 deletions python/taichi/ui/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ def mesh(self,
vertex_offset: int = 0,
vertex_count: int = None,
index_offset: int = 0,
index_count: int = None):
index_count: int = None,
display_mode: int = 0):
"""Declare a mesh inside the scene.

if you indicate the index_offset and index_count, the normals will also
Expand Down Expand Up @@ -205,6 +206,9 @@ def mesh(self,
index_count (int, optional):
only available when `indices` is provided, which is the the number
of vertices to draw.
display_mode (int, optional):
Morcki marked this conversation as resolved.
Show resolved Hide resolved
there are 3 types of diplay mode, 0 equals Fill mode(Fill colors to all triagnles)
1 equals Line mode(WareFrame), 2 equals Point mode.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
Expand All @@ -220,13 +224,16 @@ def mesh(self,
index_count = vertex_count # FIXME : Need to confirm
else:
index_count = indices.shape[0]
if display_mode < 0 or display_mode >= 3:
print("Error! display_mode must be 0(Fill), 1(Line), 2(Point)")
exit()
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, index_count, index_offset, vertex_count,
vertex_offset)
vertex_offset, display_mode)

def mesh_instance(self,
vertices,
Expand All @@ -236,11 +243,12 @@ def mesh_instance(self,
per_vertex_color=None,
two_sided=False,
transforms=None,
draw_first_instance: int = 0,
instance_offset: int = 0,
vertex_offset: int = 0,
vertex_count: int = None,
index_offset: int = 0,
index_count: int = None):
index_count: int = None,
display_mode: int = 0):
"""Declare lots of mesh instances inside the scene.

If transforms is given, then according to the shape of transforms, we will
Expand All @@ -267,9 +275,9 @@ def mesh_instance(self,
transforms (ti.Matrix.field, optional):
The Matrix must be 4x4 size with N instances, and data type should
be ti.f32, ti.i32, ti.u32. If None, then it behaves like raw mesh (no copy).
draw_first_instance (int, optional):
instance_offset (int, optional):
Default value is 0 which means no offset to show mesh instances. Otherwise,
the mesh instances will show from the `draw_first_instance`.
the mesh instances will show from the `instance_offset`.
vertex_offset (int, optional):
if 'indices' is provided, this refers to the value added to the vertex
index before indexing into the vertex buffer, else this refers to the
Expand All @@ -283,6 +291,9 @@ def mesh_instance(self,
index_count (int, optional):
only available when `indices` is provided, which is the the number
of vertices to draw.
display_mode (int, optional):
there are 3 types of diplay mode, 0 equals Fill mode(Fill colors to all triagnles)
1 equals Line mode(WareFrame), 2 equals Point mode.
"""
vbo = get_vbo_field(vertices)
copy_vertices_to_vbo(vbo, vertices)
Expand All @@ -301,14 +312,17 @@ def mesh_instance(self,
if transforms and (transforms.m != 4 or transforms.n != 4):
print("Error! Transform matrix must be 4x4 shape")
exit()
if display_mode < 0 or display_mode >= 3:
print("Error! display_mode must be 0(Fill), 1(Line), 2(Point)")
exit()
Morcki marked this conversation as resolved.
Show resolved Hide resolved
copy_normals_to_vbo(vbo, normals)
vbo_info = get_field_info(vbo)
indices_info = get_field_info(indices)
transform_info = get_field_info(transforms)
self.scene.mesh_instance(vbo_info, has_per_vertex_color, indices_info,
color, two_sided, transform_info,
draw_first_instance, index_count,
index_offset, vertex_count, vertex_offset)
instance_offset, index_count, index_offset,
vertex_count, vertex_offset, display_mode)

def particles(self,
centers,
Expand Down
12 changes: 10 additions & 2 deletions taichi/python/export_ggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ struct PyGui {
bool checkbox(std::string name, bool old_value) {
return gui->checkbox(name, old_value);
}
int slider_int(std::string name, int old_value, int minimum, int maximum) {
return gui->slider_int(name, old_value, minimum, maximum);
}
float slider_float(std::string name,
float old_value,
float minimum,
Expand Down Expand Up @@ -172,7 +175,8 @@ struct PyScene {
float draw_index_count,
float draw_first_index,
float draw_vertex_count,
float draw_first_vertex) {
float draw_first_vertex,
int display_mode) {
RenderableInfo renderable_info;
renderable_info.vbo = vbo;
renderable_info.has_per_vertex_color = has_per_vertex_color;
Expand All @@ -182,6 +186,7 @@ struct PyScene {
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;
renderable_info.display_mode = taichi::lang::PolygonMode(display_mode);

MeshInfo info;
info.renderable_info = renderable_info;
Expand Down Expand Up @@ -222,7 +227,8 @@ struct PyScene {
float draw_index_count,
Morcki marked this conversation as resolved.
Show resolved Hide resolved
float draw_first_index,
float draw_vertex_count,
float draw_first_vertex) {
float draw_first_vertex,
int display_mode) {
RenderableInfo renderable_info;
renderable_info.vbo = vbo;
renderable_info.has_per_vertex_color = has_per_vertex_color;
Expand All @@ -232,6 +238,7 @@ struct PyScene {
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;
renderable_info.display_mode = taichi::lang::PolygonMode(display_mode);
Morcki marked this conversation as resolved.
Show resolved Hide resolved

MeshInfo info;
info.renderable_info = renderable_info;
Expand Down Expand Up @@ -494,6 +501,7 @@ void export_ggui(py::module &m) {
.def("end", &PyGui::end)
.def("text", &PyGui::text)
.def("checkbox", &PyGui::checkbox)
.def("slider_int", &PyGui::slider_int)
.def("slider_float", &PyGui::slider_float)
.def("color_edit_3", &PyGui::color_edit_3)
.def("button", &PyGui::button);
Expand Down
7 changes: 7 additions & 0 deletions taichi/ui/backends/vulkan/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ bool Gui::checkbox(std::string name, bool old_value) {
ImGui::Checkbox(name.c_str(), &old_value);
return old_value;
}
int Gui::slider_int(std::string name, int old_value, int minimum, int maximum) {
if (!initialized()) {
return old_value;
}
ImGui::SliderInt(name.c_str(), &old_value, minimum, maximum);
return old_value;
}
float Gui::slider_float(std::string name,
float old_value,
float minimum,
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/backends/vulkan/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class TI_DLL_EXPORT Gui final : public GuiBase {
virtual void end() override;
virtual void text(std::string text) override;
virtual bool checkbox(std::string name, bool old_value) override;
virtual int slider_int(std::string name,
int old_value,
int minimum,
int maximum) override;
virtual float slider_float(std::string name,
float old_value,
float minimum,
Expand Down
13 changes: 12 additions & 1 deletion taichi/ui/backends/vulkan/renderable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ void Renderable::update_data(const RenderableInfo &info) {
prog->flush();
}

bool is_pipeline_reset = false;
Morcki marked this conversation as resolved.
Show resolved Hide resolved

// Check if we need to update Graphics Pipeline
if (info.display_mode != config_.polygon_type) {
is_pipeline_reset = true;
config_.polygon_type = info.display_mode;
pipeline_.reset();
create_graphics_pipeline();
}

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;
Expand Down Expand Up @@ -88,7 +98,7 @@ void Renderable::update_data(const RenderableInfo &info) {
config_.draw_first_index = 0;
}

if (num_vertices > config_.max_vertices_count ||
if (is_pipeline_reset || num_vertices > config_.max_vertices_count ||
num_indices > config_.max_indices_count) {
free_buffers();
config_.max_vertices_count = num_vertices;
Expand Down Expand Up @@ -165,6 +175,7 @@ void Renderable::create_graphics_pipeline() {

RasterParams raster_params;
raster_params.prim_topology = config_.topology_type;
raster_params.polygon_mode = config_.polygon_type;
raster_params.depth_test = true;
raster_params.depth_write = true;

Expand Down
1 change: 1 addition & 0 deletions taichi/ui/backends/vulkan/renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct RenderableConfig {
std::string fragment_shader_path;
taichi::lang::TopologyType topology_type{
taichi::lang::TopologyType::Triangles};
taichi::lang::PolygonMode polygon_type{taichi::lang::PolygonMode::Fill};
Morcki marked this conversation as resolved.
Show resolved Hide resolved
VertexAttributes vbo_attrs{VboHelpers::all()};

size_t vbo_size() const {
Expand Down
1 change: 1 addition & 0 deletions taichi/ui/backends/vulkan/renderables/circles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void Circles::init_circles(AppContext *app_context,
app_context->config.package_path + "/shaders/Circles_vk_vert.spv",
app_context->config.package_path + "/shaders/Circles_vk_frag.spv",
TopologyType::Points,
PolygonMode::Fill,
vbo_attrs,
};

Expand Down
1 change: 1 addition & 0 deletions taichi/ui/backends/vulkan/renderables/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ void Mesh::init_mesh(AppContext *app_context,
app_context->config.package_path + "/shaders/Mesh_vk_vert.spv",
app_context->config.package_path + "/shaders/Mesh_vk_frag.spv",
TopologyType::Triangles,
PolygonMode::Fill,
vbo_attrs,
};

Expand Down
1 change: 1 addition & 0 deletions taichi/ui/backends/vulkan/renderables/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void Particles::init_particles(AppContext *app_context,
app_context->config.package_path + "/shaders/Particles_vk_vert.spv",
app_context->config.package_path + "/shaders/Particles_vk_frag.spv",
TopologyType::Points,
PolygonMode::Fill,
vbo_attrs,
};

Expand Down
2 changes: 1 addition & 1 deletion taichi/ui/backends/vulkan/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class TI_DLL_EXPORT Renderer {
taichi::lang::StreamSemaphore get_render_complete_semaphore();

private:
glm::vec3 background_color_ = glm::vec3(0.f, 0.f, 0.f);
glm::vec3 background_color_ = glm::vec3(0.f, 0.f, 0.0f);
Morcki marked this conversation as resolved.
Show resolved Hide resolved

std::vector<std::unique_ptr<Renderable>> renderables_;
int next_renderable_;
Expand Down
1 change: 1 addition & 0 deletions taichi/ui/common/app_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include "taichi/ui/utils/utils.h"
#include "taichi/rhi/arch.h"
#include "taichi/rhi/vulkan/vulkan_device.h"
Morcki marked this conversation as resolved.
Show resolved Hide resolved

namespace taichi {
namespace ui {
Expand Down
4 changes: 4 additions & 0 deletions taichi/ui/common/gui_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class GuiBase {
virtual void end() = 0;
virtual void text(std::string text) = 0;
virtual bool checkbox(std::string name, bool old_value) = 0;
virtual int slider_int(std::string name,
int old_value,
int minimum,
int maximum) = 0;
virtual float slider_float(std::string name,
float old_value,
float minimum,
Expand Down
1 change: 1 addition & 0 deletions taichi/ui/common/renderable_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct RenderableInfo {
int draw_first_vertex{0};
int draw_index_count{0};
int draw_first_index{0};
taichi::lang::PolygonMode display_mode{taichi::lang::PolygonMode::Fill};
};

TI_UI_NAMESPACE_END