Skip to content

Commit 2ee903c

Browse files
committed
update to nanobind 0.3.0
1 parent f5020e2 commit 2ee903c

File tree

8 files changed

+161
-157
lines changed

8 files changed

+161
-157
lines changed

ext/nanobind

Submodule nanobind updated 121 files

include/nanogui/python.h

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,65 +27,70 @@ extern "C" {
2727
};
2828

2929
/// Provides a ``NB_OVERRIDE`` for any relevant Widget items that need to be bound.
30-
#define NANOGUI_WIDGET_OVERLOADS(Parent) \
31-
NB_TRAMPOLINE(Parent, 20) \
32-
bool mouse_button_event(const ::nanogui::Vector2i &p, int button, bool down, int modifiers) override { \
33-
NB_OVERRIDE(bool, Parent, mouse_button_event, p, button, down, modifiers); \
34-
} \
35-
bool mouse_motion_event(const ::nanogui::Vector2i &p, const ::nanogui::Vector2i &rel, int button, int modifiers) override { \
36-
NB_OVERRIDE(bool, Parent, mouse_motion_event, p, rel, button, modifiers); \
37-
} \
38-
bool mouse_drag_event(const ::nanogui::Vector2i &p, const ::nanogui::Vector2i &rel, int button, int modifiers) override { \
39-
NB_OVERRIDE(bool, Parent, mouse_drag_event, p, rel, button, modifiers); \
40-
} \
41-
bool mouse_enter_event(const ::nanogui::Vector2i &p, bool enter) override { \
42-
NB_OVERRIDE(bool, Parent, mouse_enter_event, p, enter); \
43-
} \
44-
bool scroll_event(const ::nanogui::Vector2i &p, const ::nanogui::Vector2f &rel) override { \
45-
NB_OVERRIDE(bool, Parent, scroll_event, p, rel); \
46-
} \
47-
bool focus_event(bool focused) override { \
48-
NB_OVERRIDE(bool, Parent, focus_event, focused); \
49-
} \
50-
bool keyboard_event(int key, int scancode, int action, int modifiers) override { \
51-
NB_OVERRIDE(bool, Parent, keyboard_event, key, scancode, action, modifiers); \
52-
} \
53-
bool keyboard_character_event(unsigned int codepoint) override { \
54-
NB_OVERRIDE(bool, Parent, keyboard_character_event, codepoint); \
55-
} \
56-
::nanogui::Vector2i preferred_size(NVGcontext *ctx) const override { \
57-
NB_OVERRIDE(::nanogui::Vector2i, Parent, preferred_size, ctx); \
58-
} \
59-
void perform_layout(NVGcontext *ctx) override { \
60-
NB_OVERRIDE(void, Parent, perform_layout, ctx); \
61-
} \
62-
void draw(NVGcontext *ctx) override { \
63-
NB_OVERRIDE(void, Parent, draw, ctx); \
64-
}
30+
#define NANOGUI_WIDGET_OVERLOADS(Parent) \
31+
NB_TRAMPOLINE(Parent, 16); \
32+
bool mouse_button_event(const ::nanogui::Vector2i &p, int button, \
33+
bool down, int modifiers) override { \
34+
NB_OVERRIDE(mouse_button_event, p, button, down, modifiers); \
35+
} \
36+
bool mouse_motion_event(const ::nanogui::Vector2i &p, \
37+
const ::nanogui::Vector2i &rel, int button, \
38+
int modifiers) override { \
39+
NB_OVERRIDE(mouse_motion_event, p, rel, button, modifiers); \
40+
} \
41+
bool mouse_drag_event(const ::nanogui::Vector2i &p, \
42+
const ::nanogui::Vector2i &rel, int button, \
43+
int modifiers) override { \
44+
NB_OVERRIDE(mouse_drag_event, p, rel, button, modifiers); \
45+
} \
46+
bool mouse_enter_event(const ::nanogui::Vector2i &p, bool enter) \
47+
override { \
48+
NB_OVERRIDE(mouse_enter_event, p, enter); \
49+
} \
50+
bool scroll_event(const ::nanogui::Vector2i &p, \
51+
const ::nanogui::Vector2f &rel) override { \
52+
NB_OVERRIDE(scroll_event, p, rel); \
53+
} \
54+
bool focus_event(bool focused) override { \
55+
NB_OVERRIDE(focus_event, focused); \
56+
} \
57+
bool keyboard_event(int key, int scancode, int action, int modifiers) \
58+
override { \
59+
NB_OVERRIDE(keyboard_event, key, scancode, action, modifiers); \
60+
} \
61+
bool keyboard_character_event(unsigned int codepoint) override { \
62+
NB_OVERRIDE(keyboard_character_event, codepoint); \
63+
} \
64+
::nanogui::Vector2i preferred_size(NVGcontext *ctx) const override { \
65+
NB_OVERRIDE(preferred_size, ctx); \
66+
} \
67+
void perform_layout(NVGcontext *ctx) override { \
68+
NB_OVERRIDE(perform_layout, ctx); \
69+
} \
70+
void draw(NVGcontext *ctx) override { NB_OVERRIDE(draw, ctx); }
6571

6672
/// Provides a ``NB_OVERRIDE`` for any relevant Layout items that need to be bound.
67-
#define NANOGUI_LAYOUT_OVERLOADS(Parent) \
68-
NB_TRAMPOLINE(Parent, 2) \
69-
::nanogui::Vector2i preferred_size(NVGcontext *ctx, const ::nanogui::Widget *widget) const override { \
70-
NB_OVERRIDE(::nanogui::Vector2i, Parent, preferred_size, ctx, widget); \
71-
} \
72-
void perform_layout(NVGcontext *ctx, ::nanogui::Widget *widget) const override { \
73-
NB_OVERRIDE(void, Parent, perform_layout, ctx, widget); \
73+
#define NANOGUI_LAYOUT_OVERLOADS(Parent) \
74+
NB_TRAMPOLINE(Parent, 2); \
75+
::nanogui::Vector2i preferred_size( \
76+
NVGcontext *ctx, const ::nanogui::Widget *widget) const override { \
77+
NB_OVERRIDE(preferred_size, ctx, widget); \
78+
} \
79+
void perform_layout(NVGcontext *ctx, ::nanogui::Widget *widget) \
80+
const override { \
81+
NB_OVERRIDE(perform_layout, ctx, widget); \
7482
}
7583

7684
/// Provides a ``NB_OVERRIDE`` for any relevant Screen items that need to be bound.
77-
#define NANOGUI_SCREEN_OVERLOADS(Parent) \
78-
virtual void draw_all() override { \
79-
NB_OVERRIDE(void, Parent, draw_all); \
80-
} \
81-
virtual void draw_contents() override { \
82-
NB_OVERRIDE(void, Parent, draw_contents); \
83-
} \
84-
virtual bool drop_event(const std::vector<std::string> &filenames) override { \
85-
NB_OVERRIDE(bool, Parent, drop_event, filenames); \
86-
} \
87-
virtual bool resize_event(const ::nanogui::Vector2i &size) override { \
88-
NB_OVERRIDE(bool, Parent, resize_event, size); \
85+
#define NANOGUI_SCREEN_OVERLOADS(Parent) \
86+
virtual void draw_all() override { NB_OVERRIDE(draw_all); } \
87+
virtual void draw_contents() override { NB_OVERRIDE(draw_contents); } \
88+
virtual bool drop_event(const std::vector<std::string> &filenames) \
89+
override { \
90+
NB_OVERRIDE(drop_event, filenames); \
91+
} \
92+
virtual bool resize_event(const ::nanogui::Vector2i &size) override { \
93+
NB_OVERRIDE(resize_event, size); \
8994
}
9095

9196
NAMESPACE_BEGIN(nanobind)

src/python/canvas.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55

66
class PyCanvas : public Canvas {
77
public:
8-
using Canvas::Canvas;
98
NANOGUI_WIDGET_OVERLOADS(Canvas);
109

1110
void draw_contents() override {
12-
NB_OVERRIDE(void, Canvas, draw_contents);
11+
NB_OVERRIDE(draw_contents);
1312
}
1413
};
1514

1615
class PyImageView : public ImageView {
1716
public:
18-
using ImageView::ImageView;
1917
NANOGUI_WIDGET_OVERLOADS(ImageView);
2018

2119
void draw_contents() override {
22-
NB_OVERRIDE(void, ImageView, draw_contents);
20+
NB_OVERRIDE(draw_contents);
2321
}
2422
};
2523

src/python/color.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ void register_eigen(nb::module_ &m) {
1010
.def(nb::init<float, float>(), D(Color, Color, 5))
1111
.def("contrasting_color", &Color::contrasting_color,
1212
D(Color, contrasting_color))
13-
.def_property("r", [](const Color &c) { return c.r(); },
13+
.def_prop_rw("r", [](const Color &c) { return c.r(); },
1414
[](Color &c, float v) { c.r() = v; }, D(Color, r))
15-
.def_property("g", [](const Color &c) { return c.g(); },
15+
.def_prop_rw("g", [](const Color &c) { return c.g(); },
1616
[](Color &c, float v) { c.g() = v; }, D(Color, g))
17-
.def_property("b", [](const Color &c) { return c.b(); },
17+
.def_prop_rw("b", [](const Color &c) { return c.b(); },
1818
[](Color &c, float v) { c.b() = v; }, D(Color, b))
19-
.def_property("w", [](const Color &c) { return c.w(); },
19+
.def_prop_rw("w", [](const Color &c) { return c.w(); },
2020
[](Color &c, float v) { c.w() = v; }, "Return a reference to the alpha channel.")
2121
.def("__repr__", [](const Color &c) {
2222
std::ostringstream oss;

src/python/python.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,18 @@
1818
#define DECLARE_LAYOUT(Name) \
1919
class Py##Name : public Name { \
2020
public: \
21-
using Name::Name; \
2221
NANOGUI_LAYOUT_OVERLOADS(Name); \
2322
}
2423

2524
#define DECLARE_WIDGET(Name) \
2625
class Py##Name : public Name { \
2726
public: \
28-
using Name::Name; \
2927
NANOGUI_WIDGET_OVERLOADS(Name); \
3028
}
3129

3230
#define DECLARE_SCREEN(Name) \
3331
class Py##Name : public Name { \
3432
public: \
35-
using Name::Name; \
3633
NANOGUI_WIDGET_OVERLOADS(Name); \
3734
NANOGUI_SCREEN_OVERLOADS(Name); \
3835
}

src/python/render.cpp

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#ifdef NANOGUI_PYTHON
22

3-
#include <nanobind/tensor.h>
3+
#include <nanobind/ndarray.h>
44

55
#include "python.h"
66

7-
static VariableType dtype_to_enoki(nb::dlpack::dtype dtype) {
7+
static VariableType interpret_dlpack_dtype(nb::dlpack::dtype dtype) {
88
switch ((nb::dlpack::dtype_code) dtype.code) {
99
case nb::dlpack::dtype_code::Int:
1010
switch (dtype.bits) {
@@ -44,25 +44,25 @@ static VariableType dtype_to_enoki(nb::dlpack::dtype dtype) {
4444

4545
static void
4646
shader_set_buffer(Shader &shader, const std::string &name,
47-
nb::tensor<nb::device::cpu, nb::c_contig> tensor) {
48-
if (tensor.ndim() > 3)
49-
throw nb::type_error("Shader::set_buffer(): tensor rank must be < 3!");
47+
nb::ndarray<nb::device::cpu, nb::c_contig> array) {
48+
if (array.ndim() > 3)
49+
throw nb::type_error("Shader::set_buffer(): number of array dimensions must be < 3!");
5050

51-
VariableType dtype = dtype_to_enoki(tensor.dtype());
51+
VariableType dtype = interpret_dlpack_dtype(array.dtype());
5252

5353
if (dtype == VariableType::Invalid)
5454
throw nb::type_error("Shader::set_buffer(): unsupported array dtype!");
5555

5656
size_t dim[3] {
57-
tensor.ndim() > 0 ? (size_t) tensor.shape(0) : 1,
58-
tensor.ndim() > 1 ? (size_t) tensor.shape(1) : 1,
59-
tensor.ndim() > 2 ? (size_t) tensor.shape(2) : 1
57+
array.ndim() > 0 ? (size_t) array.shape(0) : 1,
58+
array.ndim() > 1 ? (size_t) array.shape(1) : 1,
59+
array.ndim() > 2 ? (size_t) array.shape(2) : 1
6060
};
6161

62-
shader.set_buffer(name, dtype, tensor.ndim(), dim, tensor.data());
62+
shader.set_buffer(name, dtype, array.ndim(), dim, array.data());
6363
}
6464

65-
static nb::tensor<nb::numpy> texture_download(Texture &texture) {
65+
static nb::ndarray<nb::numpy> texture_download(Texture &texture) {
6666
nb::dlpack::dtype dt;
6767

6868
switch (texture.component_format()) {
@@ -90,18 +90,19 @@ static nb::tensor<nb::numpy> texture_download(Texture &texture) {
9090

9191
texture.download(ptr);
9292

93-
return nb::tensor<nb::numpy>(ptr, 3, shape, owner, nullptr, dt);
93+
return nb::ndarray<nb::numpy>(ptr, 3, shape, owner, nullptr, dt);
9494
}
9595

96-
static void texture_upload(Texture &texture, nb::tensor<nb::device::cpu, nb::c_contig> tensor) {
97-
size_t n_channels = tensor.ndim() == 3 ? tensor.shape(2) : 1;
98-
VariableType dtype = dtype_to_enoki(tensor.dtype()),
96+
static void texture_upload(Texture &texture,
97+
nb::ndarray<nb::device::cpu, nb::c_contig> array) {
98+
size_t n_channels = array.ndim() == 3 ? array.shape(2) : 1;
99+
VariableType dtype = interpret_dlpack_dtype(array.dtype()),
99100
dtype_texture = (VariableType) texture.component_format();
100101

101-
if (tensor.ndim() != 2 && tensor.ndim() != 3)
102+
if (array.ndim() != 2 && array.ndim() != 3)
102103
throw std::runtime_error("Texture::upload(): expected a 2 or 3-dimensional array!");
103-
else if (tensor.shape(0) != (size_t) texture.size().y() ||
104-
tensor.shape(1) != (size_t) texture.size().x())
104+
else if (array.shape(0) != (size_t) texture.size().y() ||
105+
array.shape(1) != (size_t) texture.size().x())
105106
throw std::runtime_error("Texture::upload(): array size does not match the texture!");
106107
else if (n_channels != texture.channels())
107108
throw std::runtime_error(
@@ -114,18 +115,21 @@ static void texture_upload(Texture &texture, nb::tensor<nb::device::cpu, nb::c_c
114115
type_name(dtype) + ") does not match the texture (" +
115116
type_name(dtype_texture) + ")!");
116117

117-
texture.upload((const uint8_t *) tensor.data());
118+
texture.upload((const uint8_t *) array.data());
118119
}
119120

120-
static void texture_upload_sub_region(Texture &texture, nb::tensor<nb::device::cpu, nb::c_contig> tensor, const Vector2i &origin) {
121-
size_t n_channels = tensor.ndim() == 3 ? tensor.shape(2) : 1;
122-
VariableType dtype = dtype_to_enoki(tensor.dtype()),
121+
static void
122+
texture_upload_sub_region(Texture &texture,
123+
nb::ndarray<nb::device::cpu, nb::c_contig> array,
124+
const Vector2i &origin) {
125+
size_t n_channels = array.ndim() == 3 ? array.shape(2) : 1;
126+
VariableType dtype = interpret_dlpack_dtype(array.dtype()),
123127
dtype_texture = (VariableType) texture.component_format();
124128

125-
if (tensor.ndim() != 2 && tensor.ndim() != 3)
129+
if (array.ndim() != 2 && array.ndim() != 3)
126130
throw std::runtime_error("Texture::upload_sub_region(): expected a 2 or 3-dimensional array!");
127-
else if (tensor.shape(0) + (size_t) origin.x() > (size_t) texture.size().y() ||
128-
tensor.shape(1) + (size_t) origin.y() > (size_t) texture.size().x())
131+
else if (array.shape(0) + (size_t) origin.x() > (size_t) texture.size().y() ||
132+
array.shape(1) + (size_t) origin.y() > (size_t) texture.size().x())
129133
throw std::runtime_error("Texture::upload_sub_region(): bounds exceed the size of the texture!");
130134
else if (n_channels != texture.channels())
131135
throw std::runtime_error(
@@ -139,8 +143,8 @@ static void texture_upload_sub_region(Texture &texture, nb::tensor<nb::device::c
139143
type_name(dtype_texture) + ")!");
140144

141145
texture.upload_sub_region(
142-
(const uint8_t *) tensor.data(), origin,
143-
{ (int32_t) tensor.shape(0), (int32_t) tensor.shape(1) });
146+
(const uint8_t *) array.data(), origin,
147+
{ (int32_t) array.shape(0), (int32_t) array.shape(1) });
144148
}
145149
#endif
146150

0 commit comments

Comments
 (0)