Skip to content

Commit

Permalink
template bindgroup
Browse files Browse the repository at this point in the history
:
  • Loading branch information
shiinamiyuki committed Jul 27, 2023
1 parent 693f0dd commit a978321
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
16 changes: 11 additions & 5 deletions include/luisa/dsl/binding_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#define LUISA_BINDING_GROUP_MAKE_INVOKE(m) \
invoke << s.m;

#define LUISA_BINDING_GROUP(S, ...) \
template<> \
#define LUISA_BINDING_GROUP_TEMPLATE_IMPL(TEMPLATE, TEMPLATE2, S, ...) \
LUISA_MACRO_EVAL(TEMPLATE()) \
struct luisa_compute_extension<S>; \
namespace luisa::compute { \
template<> \
LUISA_MACRO_EVAL(TEMPLATE()) \
struct Var<S> { \
using this_type = S; \
LUISA_MAP(LUISA_STRUCT_MAKE_MEMBER_TYPE, __VA_ARGS__) \
Expand All @@ -39,7 +39,7 @@
return reinterpret_cast<const luisa_compute_extension<this_type> *>(this); \
} \
}; \
template<> \
LUISA_MACRO_EVAL(TEMPLATE()) \
struct Expr<S> { \
using this_type = S; \
LUISA_MAP(LUISA_STRUCT_MAKE_MEMBER_TYPE, __VA_ARGS__) \
Expand All @@ -51,15 +51,21 @@
Expr &operator=(Expr) noexcept = delete; \
}; \
namespace detail { \
LUISA_MACRO_EVAL(TEMPLATE2()) \
CallableInvoke &operator<<(CallableInvoke &invoke, Expr<S> s) noexcept { \
LUISA_MAP(LUISA_BINDING_GROUP_MAKE_INVOKE, __VA_ARGS__) \
return invoke; \
} \
LUISA_MACRO_EVAL(TEMPLATE2()) \
ShaderInvokeBase &operator<<(ShaderInvokeBase &invoke, const S &s) noexcept { \
LUISA_MAP(LUISA_BINDING_GROUP_MAKE_INVOKE, __VA_ARGS__) \
return invoke; \
} \
} \
} \
template<> \
LUISA_MACRO_EVAL(TEMPLATE()) \
struct luisa_compute_extension<S> final : luisa::compute::Var<S>
#define _LUISA_BIND_GROUP_EMPTY_TEMPLATE() template<>
#define _LUISA_BIND_GROUP_EMPTY()
#define LUISA_BINDING_GROUP(S, ...) LUISA_BINDING_GROUP_TEMPLATE_IMPL(_LUISA_BIND_GROUP_EMPTY_TEMPLATE, _LUISA_BIND_GROUP_EMPTY, S, __VA_ARGS__)
#define LUISA_BINDING_GROUP_TEMPLATE(S, TEMPLATE, ...) LUISA_BINDING_GROUP_TEMPLATE_IMPL(TEMPLATE, TEMPLATE, S, __VA_ARGS__)
36 changes: 20 additions & 16 deletions src/tests/test_binding_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,42 @@
using namespace luisa;
using namespace luisa::compute;

struct Arguments {
Image<float> image;
template<class T>
struct TArguments {
Image<T> image;
uint2 resolution;
};

struct ArgumentsView {
ImageView<float> image;
template<class T>
struct TArgumentsView {
ImageView<T> image;
uint2 resolution;
};

struct NestedArguments {
ArgumentsView args;
Image<float> image;
template<class T>
struct TNestedArguments {
TArgumentsView<T> args;
Image<T> image;
};

#define TEMPLATE_T() template<class T>
// clang-format off
LUISA_BINDING_GROUP(Arguments, image, resolution) {
LUISA_BINDING_GROUP_TEMPLATE_IMPL(TEMPLATE_T, TEMPLATE_T, TArguments<T>, image, resolution) {
[[nodiscard]] auto write(const UInt2 &coord, const Float4 &color) noexcept {
image->write(coord, color);
this->image->write(coord, color);
}
};
LUISA_BINDING_GROUP(ArgumentsView, image, resolution) {
LUISA_BINDING_GROUP_TEMPLATE_IMPL(TEMPLATE_T, TEMPLATE_T, TArgumentsView<T>, image, resolution) {
[[nodiscard]] auto write(const UInt2 &coord, const Float4 &color) noexcept {
image->write(coord, color);
this->image->write(coord, color);
}
};
LUISA_BINDING_GROUP(NestedArguments, args, image) {
LUISA_BINDING_GROUP_TEMPLATE_IMPL(TEMPLATE_T, TEMPLATE_T, TNestedArguments<T>, args, image) {
void blit(const UInt2 &coord) noexcept {
auto color = args.image.read(coord).xyz();
image->write(coord, make_float4(1.f - color, 1.f));
this->image->write(coord, make_float4(1.f - color, 1.f));
}
};
using Arguments = TArguments<float>;
using ArgumentsView = TArgumentsView<float>;
using NestedArguments = TNestedArguments<float>;
// clang-format on

int main(int argc, char *argv[]) {
Expand Down

0 comments on commit a978321

Please sign in to comment.