Skip to content

Add support for fast tiled rendering. #204

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

Open
wants to merge 49 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
581708a
tiled rendering test
erwincoumans Jun 26, 2022
a3c5671
disable libuuid on Linux, it isn't always installed, use a simple C++…
erwincoumans Jul 4, 2022
9709b11
tiled rendering test
erwincoumans Jul 4, 2022
5f77863
explicit swap_buffers
erwincoumans Jul 25, 2022
9da7c8c
update stb_image_write.h
erwincoumans Jul 25, 2022
a002122
tweaks for rendering segmentation mask
erwincoumans Jul 26, 2022
30390a6
enable EGL for linux
erwincoumans Jul 27, 2022
13dac7c
update tiled example
erwincoumans Aug 5, 2022
f1cb8e5
turn URDF missing inertial from error to warning
erwincoumans Aug 10, 2022
5a9ea15
tweaks for tiled rendering
erwincoumans Aug 12, 2022
29ebf25
test nvtx profiling, improve tiling performance for many tiles (4k)
erwincoumans Aug 18, 2022
60524af
fix vector type
erwincoumans Aug 18, 2022
235d6f1
add requirements.txt
erwincoumans Aug 18, 2022
f802a4c
conditional nvtx profiling
erwincoumans Aug 18, 2022
bb897ca
Expose 'enable_render_to_texture" returning OpenGL texture id to allo…
erwincoumans Aug 25, 2022
f81c93f
add cuda interop for image output
erwincoumans Aug 27, 2022
eacaa8b
bump up to 0.5.0
erwincoumans Aug 30, 2022
1be4943
use link_mapping if graphics/physics have different number of links a…
erwincoumans Sep 15, 2022
5db41b1
bump up version
erwincoumans Sep 15, 2022
51fadd4
expose optional verbose for load_urdf command, avoid crash in synchro…
erwincoumans Sep 16, 2022
fae2fa5
Merge branch 'tiled' of github.com:erwincoumans/tiny-differentiable-s…
erwincoumans Sep 16, 2022
1247e52
expose get_column, compute_camera_view_matrix
erwincoumans Oct 11, 2022
28adf9e
Expose vbo to allow CUDA (and Warp in particular) to write object pos…
erwincoumans Oct 12, 2022
159cf31
Bump version
erwincoumans Oct 12, 2022
2075d09
fix in register_shape1, passing the right arguments
erwincoumans Dec 2, 2022
d60f5dd
Fix vertex count in register_shape1, allow double-sided meshes
eric-heiden Dec 5, 2022
16ea062
Merge pull request #206 from eric-heiden/tiled
erwincoumans Dec 5, 2022
cceb50d
expose keyboard, mouse, wheel, resize callbacks to Python
erwincoumans Dec 5, 2022
fe3ecc3
Python expose pump_messages for window
erwincoumans Dec 7, 2022
f6859ed
fix mac build
Dec 7, 2022
ec8fc98
Update tiny_egl_opengl_window.h
erwincoumans Dec 8, 2022
3e29829
bump up version to 0.9.0
erwincoumans Dec 13, 2022
0939f36
expose get/set of vertex and fragment shader programs in pytinyopengl3
erwincoumans Jan 13, 2023
a42dc40
explicitly pass device to arguments
erwincoumans Jan 17, 2023
0d9e739
expose get_shape_vertex_offsets, it can be used to update vertices us…
erwincoumans Jan 17, 2023
a10f9da
add example snippet how to read/write vertices in OpenGL VBO memory t…
erwincoumans Jan 18, 2023
52b9b50
bump version to 0.9.1
erwincoumans Jan 18, 2023
2e07dac
0.9.2 add write_transforms option to skip cpu write_transforms, to al…
erwincoumans Feb 16, 2023
7e418c4
Add and expose functions for getting/setting view and projection matrix
eric-heiden Feb 22, 2023
49e0a94
Read camera coords from view matrix in render_scene_internal
eric-heiden Feb 23, 2023
02009f6
Merge pull request #207 from eric-heiden/tiled
erwincoumans Feb 24, 2023
2205170
bump version to 0.9.3
erwincoumans Feb 24, 2023
33449ed
expose set_cuda_path and set_cudart_path
erwincoumans Mar 10, 2023
35b2a05
fix compile issue
erwincoumans Apr 8, 2023
19e3dc9
expose scene_render_tiled
erwincoumans Apr 14, 2023
c9a575d
expose render_scene2
erwincoumans Apr 25, 2023
2c6dced
add test file
erwincoumans May 12, 2023
3c0f750
disable broken example, use 1.0 scale factor in imgui example
erwincoumans Aug 29, 2024
a7d2a9f
fix compile issue(s)
erwincoumans Oct 19, 2024
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
5 changes: 0 additions & 5 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ target_include_directories(laikago_opengl_tiny_example PRIVATE ../third_party/ti
target_include_directories(laikago_opengl_tiny_example PRIVATE ../third_party ../src)
target_compile_definitions(laikago_opengl_tiny_example PRIVATE USE_TINY )

add_executable(inclined_plane_gui inclined_plane_gui.cpp ../third_party/stb_image/stb_image.cpp ../third_party/tinyobjloader/tiny_obj_loader.cc ${TDS_HDRS})
target_link_libraries(inclined_plane_gui Eigen3::Eigen tinyxml2 opengl_window ${CMAKE_DL_LIBS})
target_include_directories(inclined_plane_gui PRIVATE ../third_party/tinyobjloader)
target_include_directories(inclined_plane_gui PRIVATE ../third_party ../src)
target_compile_definitions(inclined_plane_gui PRIVATE USE_TINY )

endif(Eigen3_FOUND)

Expand Down
2 changes: 1 addition & 1 deletion examples/opengl_imgui_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ int main(int argc, char* argv[]) {
ImGui::StyleColorsDark();
if (1)
{
float SCALE = 2.0f;
float SCALE = 1.0f;
ImFontConfig cfg;
cfg.SizePixels = 13 * SCALE;
ImGui::GetIO().Fonts->AddFontDefault(&cfg)->DisplayOffset.y = SCALE;
Expand Down
210 changes: 184 additions & 26 deletions examples/opengl_urdf_visualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "visualizer/opengl/tiny_opengl3_app.h"
#include "visualizer/opengl/utils/tiny_mesh_utils.h"


// disabled #define USE_SDF_TO_MESH, since code crashes when radius == 0
// #define USE_SDF_TO_MESH
#ifdef USE_SDF_TO_MESH
Expand All @@ -39,6 +40,15 @@
struct UrdfInstancePair {
int m_link_index;
int m_visual_instance;
::TINY::TinyVector3f viz_origin_xyz;
::TINY::TinyVector3f viz_origin_rpy;

UrdfInstancePair()
: m_link_index(-1),
m_visual_instance(-1),
viz_origin_xyz(0.f, 0.f, 0.f),
viz_origin_rpy(0.f, 0.f, 0.f) {}

};

template <typename Algebra>
Expand Down Expand Up @@ -75,8 +85,12 @@ struct OpenGLUrdfVisualizer {
TinyOpenGL3App m_opengl_app;

OpenGLUrdfVisualizer(int width = 1024, int height = 768,
const char *title = "Tiny Differentiable Simulator")
: m_uid(1234), m_opengl_app(title, width, height) {
const char *title = "Tiny Differentiable Simulator",
bool allowRetina = true, int window_type = 0,
int render_device = -1, int max_num_object_capacity = 128 * 1024,
int max_shape_capacity_in_bytes = 128 * 1024 * 1024)
: m_uid(1234), m_opengl_app(title, width, height, allowRetina, window_type,
render_device, max_num_object_capacity, max_shape_capacity_in_bytes) {
m_opengl_app.m_renderer->init();
m_opengl_app.set_up_axis(2);
m_opengl_app.m_renderer->get_active_camera()->set_camera_distance(4);
Expand Down Expand Up @@ -272,7 +286,15 @@ struct OpenGLUrdfVisualizer {

b2v.visual_shape_uids.push_back(shape_id);
::TINY::TinyVector3f color(1, 1, 1);
b2v.shape_colors.push_back(color);
if (v.has_local_material) {
if (urdf.materials.count(v.material_name)) {
color = ::TINY::TinyVector3f (
Algebra::to_double(urdf.materials[v.material_name].material_rgb[0]),
Algebra::to_double(urdf.materials[v.material_name].material_rgb[1]),
Algebra::to_double(urdf.materials[v.material_name].material_rgb[2]));
}
}
b2v.shape_colors.push_back(::TINY::TinyVector3f (color[0],color[1],color[2]));
break;
}
case ::tds::TINY_CAPSULE_TYPE: {
Expand All @@ -298,7 +320,7 @@ struct OpenGLUrdfVisualizer {

b2v.visual_shape_uids.push_back(shape_id);
::TINY::TinyVector3f color(1, 1, 1);
b2v.shape_colors.push_back(color);
b2v.shape_colors.push_back(::TINY::TinyVector3f (color[0],color[1],color[2]));
break;
}
#ifdef USE_SDF_TO_MESH
Expand All @@ -316,6 +338,24 @@ struct OpenGLUrdfVisualizer {
b2v.shape_colors.push_back(color);
break;
}
#else
case ::tds::TINY_CYLINDER_TYPE: {
//::tds::Cylinder<Algebra> gen_cylinder(v.geometry.cylinder.radius,
// v.geometry.cylinder.length);

float radius = Algebra::to_double(v.geometry.cylinder.radius);
float half_height =
Algebra::to_double(v.geometry.cylinder.length) * 0.5;
int up_axis = 2;

int shape_id = m_opengl_app.register_graphics_cylinder_shape(
radius, half_height, up_axis, -1);

b2v.visual_shape_uids.push_back(shape_id);
::TINY::TinyVector3f color(1, 1, 1);
b2v.shape_colors.push_back(::TINY::TinyVector3f (color[0],color[1],color[2]));
break;
}
#endif
case ::tds::TINY_BOX_TYPE: {
float half_extentsx =
Expand All @@ -328,6 +368,14 @@ struct OpenGLUrdfVisualizer {
half_extentsx, half_extents_y, half_extents_z);
b2v.visual_shape_uids.push_back(shape_id);
::TINY::TinyVector3f color(1, 1, 1);
if (v.has_local_material) {
if (urdf.materials.count(v.material_name)) {
color = ::TINY::TinyVector3f(
Algebra::to_double(urdf.materials[v.material_name].material_rgb[0]),
Algebra::to_double(urdf.materials[v.material_name].material_rgb[1]),
Algebra::to_double(urdf.materials[v.material_name].material_rgb[2]));
}
}
b2v.shape_colors.push_back(color);

break;
Expand Down Expand Up @@ -412,12 +460,17 @@ struct OpenGLUrdfVisualizer {
for (int v = 0; v < vis_link.visual_shape_uids.size(); v++) {
int sphere_shape = vis_link.visual_shape_uids[v];
::TINY::TinyVector3f color(1, 1, 1);
if (v < vis_link.shape_colors.size()) {
color = vis_link.shape_colors[v];
}
// visualizer.m_b2vis
for (int ni = 0; ni < num_instances; ni++) {
instance = m_opengl_app.m_renderer->register_graphics_instance(
sphere_shape, pos, orn, color, scaling, 1.0, false);
pair.m_link_index = -1;
pair.m_visual_instance = instance;
pair.viz_origin_xyz = vis_link.origin_xyz;
pair.viz_origin_rpy = vis_link.origin_rpy;
all_instances[ni].push_back(pair);
}
}
Expand All @@ -435,12 +488,18 @@ struct OpenGLUrdfVisualizer {
for (int v = 0; v < vis_link.visual_shape_uids.size(); v++) {
int sphere_shape = vis_link.visual_shape_uids[v];
::TINY::TinyVector3f color(1, 1, 1);
if (v < vis_link.shape_colors.size()) {
color = vis_link.shape_colors[v];

}
// visualizer.m_b2vis
for (int ni = 0; ni < num_instances; ni++) {
instance = m_opengl_app.m_renderer->register_graphics_instance(
sphere_shape, pos, orn, color, scaling, 1.0, false);
pair.m_link_index = i;
pair.m_visual_instance = instance;
pair.viz_origin_xyz = vis_link.origin_xyz;
pair.viz_origin_rpy = vis_link.origin_rpy;
all_instances[ni].push_back(pair);
}
}
Expand Down Expand Up @@ -523,32 +582,81 @@ struct OpenGLUrdfVisualizer {
void sync_visual_transforms2(
const std::vector < std::vector<UrdfInstancePair>> & all_instances,
const std::vector < std::vector<float>> & visual_world_transforms_array,
int visual_offset, float sim_spacing) {
int visual_offset, float sim_spacing, bool apply_visual_offset,
const std::vector<int>& link_mapping) {
int batch_size = all_instances.size();
int viz_sz = visual_world_transforms_array.size();
for (int ni = 0; ni < batch_size; ni++) {
// visual_world_transforms.size();
const auto& pairs = all_instances[ni];
if (ni>=visual_world_transforms_array.size())
continue;

const auto &visual_world_transforms = visual_world_transforms_array[ni];
const int square_id = (int)std::sqrt((double)batch_size);
// for (int ni = 0; ni < batch_size; ni++) {
int index = visual_offset;
for (int i = 0; i < pairs.size(); i++) {
auto pair = pairs[i];
int link_index = pair.m_link_index;
bool skip=false;
if (link_mapping.size()==pairs.size())
{
int m = link_mapping[i];
if (m<0)
skip=true;
else
index = visual_offset+(link_mapping[i]*7);
}
if ((index+6)>visual_world_transforms.size())
skip=true;

if (!skip)
{
// Transform geom_X_world = body->base_X_world() * body->X_visuals()[v];
// visual_world_transforms
::TINY::TinyPosef pose_rbd;
pose_rbd.m_position.setValue(visual_world_transforms[index + 0],
visual_world_transforms[index + 1],
visual_world_transforms[index + 2]);

pose_rbd.m_orientation.setValue(visual_world_transforms[index + 3],
visual_world_transforms[index + 4],
visual_world_transforms[index + 5],
visual_world_transforms[index + 6]);

::TINY::TinyPosef pose_viz;
if (apply_visual_offset) {
pose_viz.m_position = pair.viz_origin_xyz;
pose_viz.m_orientation.set_euler_rpy(pair.viz_origin_rpy);
}else {
pose_viz.set_identity();
}

::TINY::TinyVector3f pos(
visual_world_transforms[index + 0] +
sim_spacing * (ni % square_id) - square_id * sim_spacing / 2,
visual_world_transforms[index + 1] +
sim_spacing * (ni / square_id) - square_id * sim_spacing / 2,
visual_world_transforms[index + 2]);

::TINY::TinyQuaternionf orn(visual_world_transforms[index + 3],
visual_world_transforms[index + 4],
visual_world_transforms[index + 5],
visual_world_transforms[index + 6]);
index += 7;
m_opengl_app.m_renderer->write_single_instance_transform_to_cpu(
pos, orn, pair.m_visual_instance);
::TINY::TinyPosef pose_w = pose_rbd * pose_viz;

::TINY::TinyVector3f pos = m_opengl_app.get_up_axis()==2?

::TINY::TinyVector3f(
pose_w.m_position.x() +
sim_spacing * (ni % square_id) - square_id * sim_spacing / 2,
pose_w.m_position.y() +
sim_spacing * (ni / square_id) - square_id * sim_spacing / 2,
pose_w.m_position.z()):
::TINY::TinyVector3f(
pose_w.m_position.x() +
sim_spacing * (ni % square_id) - square_id * sim_spacing / 2,
pose_w.m_position.y(),
pose_w.m_position.z() +
sim_spacing * (ni / square_id) - square_id * sim_spacing / 2
);

::TINY::TinyQuaternionf orn = pose_w.m_orientation;

index += 7;
m_opengl_app.m_renderer->write_single_instance_transform_to_cpu(
pos, orn, pair.m_visual_instance);
}
}
}
}
Expand Down Expand Up @@ -588,23 +696,73 @@ struct OpenGLUrdfVisualizer {
}
}
}
void render() {
int upAxis = 2;
m_opengl_app.m_renderer->write_transforms();

void render(bool do_swap_buffer = true, bool render_segmentation_mask=false, int upAxis=2, bool write_transforms = true) {
if (write_transforms)
m_opengl_app.m_renderer->write_transforms();
m_opengl_app.m_renderer->update_camera(upAxis);
double lightPos[3] = {-50, 30, 40};
m_opengl_app.m_renderer->set_light_position(lightPos);
float specular[3] = {1, 1, 1};
m_opengl_app.m_renderer->set_light_specular_intensity(specular);
DrawGridData data;
data.upAxis = 2;
data.upAxis = upAxis;
data.drawAxis = true;
m_opengl_app.draw_grid(data);
// const char* bla = "3d label";
// m_opengl_app.draw_text_3d(bla, 0, 0, 1, 1);
m_opengl_app.m_renderer->render_scene();
m_opengl_app.swap_buffer();
if (render_segmentation_mask)
{
std::vector<TinyViewportTile> tiles;
m_opengl_app.m_renderer->render_scene_internal(
tiles, B3_SEGMENTATION_MASK_RENDERMODE);
}
else
{
m_opengl_app.m_renderer->render_scene();
}
if (do_swap_buffer)
{
m_opengl_app.swap_buffer();
}
}
};

void render_tiled(std::vector<TinyViewportTile> &tiles,
bool do_swap_buffer = true,
bool render_segmentation_mask = false,
int upAxis =2,
bool write_transforms = true ) {
if (write_transforms)
m_opengl_app.m_renderer->write_transforms();
m_opengl_app.m_renderer->update_camera(upAxis);
double lightPos[3] = {-50, 30, 40};
m_opengl_app.m_renderer->set_light_position(lightPos);
float specular[3] = {1, 1, 1};
m_opengl_app.m_renderer->set_light_specular_intensity(specular);
// m_opengl_app.m_renderer->render_scene2(tiles);

if (render_segmentation_mask)
{
m_opengl_app.m_renderer->render_scene_internal(
tiles, B3_SEGMENTATION_MASK_RENDERMODE);
}
else
{
m_opengl_app.m_renderer->render_scene_internal(
tiles, B3_DEFAULT_RENDERMODE);

}
if (do_swap_buffer)
{
m_opengl_app.swap_buffer();
}
}


void swap_buffer() {
m_opengl_app.swap_buffer();
}

};

#endif // OPENGL_URDF_VISUALIZER_H
Loading