@@ -74,88 +74,86 @@ NBL_CONSTEXPR uint32_t default_alignment_v = default_alignment<T,MemberCount::va
7474}
7575}
7676
77- // time for some macros!
78- // need to gen identical struct in HLSL and C++
79- #if 0
80- // forward declaration - outside of macro
81- struct MyStruct;
77+ //! Need to gen identical struct in HLSL and C++, right now this tool can declare non-templated structs and full explicit specialized ones
8278
83- // TODO: to be generated by a BOOST_PP macro:
84- // - take template parameters list
85- // - take partial spec name
86- // - custom alignas on the struct or not
87- // - sequence of variable name and type (identifier0,Type0)...(identifierN,TypeN)
88-
89- template</*T_ARGS*/ >
90- struct ::nbl::hlsl::bda::member_count<MyStruct >
91- {
92- NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 3 ;
93- };
94- template</*T_ARGS*/ >
95- struct ::nbl::hlsl::bda::member_type<MyStruct,0 >
96- {
97- using type = float32_t;
79+ //implementation details
80+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_TYPE (identifier,...) __VA_ARGS__
81+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_NAME (identifier,...) identifier
82+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER_TYPE (r,IDENTIFIER,i,e) template<> \
83+ struct ::nbl::hlsl::bda::member_type<NBL_EVAL IDENTIFIER,i> \
84+ { \
85+ using type = NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_TYPE e; \
9886};
99- template</*T_ARGS*/ >
100- struct ::nbl::hlsl::bda::member_type<MyStruct,1 >
101- {
102- using type = int32_t;
103- };
104- template</*T_ARGS*/ >
105- struct ::nbl::hlsl::bda::member_type<MyStruct,2 >
106- {
107- using type = int16_t2;
108- };
109- // self alignment
110- #define NBL_ALIGNAS 8
111- template<>
112- struct ::nbl::hlsl::alignment_of<MyStruct >
113- {
114- NBL_CONSTEXPR_STATIC_INLINE uint32_t value = ::nbl::hlsl::conditional_value<NBL_ALIGNAS!=0 ,uint32_t,NBL_ALIGNAS,::nbl::hlsl::bda::impl::default_alignment_v<MyStruct > >::value;
115- };
116- template<>
117- struct ::nbl::hlsl::size_of<MyStruct >
118- {
119- NBL_CONSTEXPR_STATIC_INLINE uint32_t __last_member_ix_v = ::nbl::hlsl::bda::member_count_v<MyStruct >-1 ;
120- NBL_CONSTEXPR_STATIC_INLINE uint64_t __last_member_offset_v = ::nbl::hlsl::bda::member_offset_v<MyStruct,__last_member_ix_v>;
121- NBL_CONSTEXPR_STATIC_INLINE uint64_t __last_member_size_v = ::nbl::hlsl::size_of_v<::nbl::hlsl::bda::member_type_t<MyStruct,__last_member_ix_v> >;
122- NBL_CONSTEXPR_STATIC_INLINE uint32_t value = mpl::align_up_v<__last_member_offset_v+__last_member_size_v,alignment_of_v<MyStruct > >;
123- };
124- #ifdef __HLSL_VERSION
125- /*template<T_ARGS>*/
126- struct MyStruct
127- {
128- [[vk::ext_decorate (spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,0 >)]] float32_t a;
129- [[vk::ext_decorate (spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,1 >)]] int32_t b;
130- [[vk::ext_decorate (spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,2 >)]] int16_t2 c;
131- };
132- template<uint32_t alignment, bool _restrict/*, T_ARGS*/ >
133- struct nbl::hlsl::bda::__ref<MyStruct,alignment,_restrict> : nbl::hlsl::bda::__base_ref<MyStruct,alignment,_restrict>
134- {
135- using base_t = __base_ref<MyStruct,alignment,_restrict>;
136- using this_t = __ref<MyStruct,alignment,_restrict>;
13787
138- ::nbl::hlsl::bda::__ref<float32_t,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,0 >,alignment>,_restrict> a;
139- ::nbl::hlsl::bda::__ref<int32_t,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,1 >,alignment>,_restrict> b;
140- ::nbl::hlsl::bda::__ref<int16_t2,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,2 >,alignment>,_restrict> c;
141-
142- void __init (const ::nbl::hlsl::spirv::bda_pointer_t<MyStruct > _ptr)
143- {
144- base_t::__init (_ptr);
145- a.__init (::nbl::hlsl::spirv::accessChain<float32_t>(base_t::ptr.value,0 ));
146- b.__init (::nbl::hlsl::spirv::accessChain<int32_t>(base_t::ptr.value,1 ));
147- c.__init (::nbl::hlsl::spirv::accessChain<int16_t2>(base_t::ptr.value,2 ));
148- }
149- };
88+ //! TODO: handle declarations for partial template specializations and non-specializations
89+ #define NBL_HLSL_IMPL_DECLARE_STRUCT_MEMBER (identifier,...) __VA_ARGS__ identifier;
90+ #ifdef __HLSL_VERSION
91+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER (r,IDENTIFIER,i,e) [[vk::ext_decorate (spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<NBL_EVAL IDENTIFIER,i>)]] NBL_HLSL_IMPL_DECLARE_STRUCT_MEMBER e
92+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER_REFERENCE (r,unused,i,e) ::nbl::hlsl::bda::__ref< \
93+ NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_TYPE e, \
94+ ::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<__referenced_t,i>,alignment>, \
95+ _restrict> NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_NAME e;
96+ #define NBL_HLSL_IMPL_INIT_STRUCT_MEMBER_REFERENCE (r,unused,i,e) NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_NAME e .__init ( \
97+ ::nbl::hlsl::spirv::accessChain<NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_TYPE e>(base_t::ptr.value,i) \
98+ );
99+ #define NBL_HLSL_IMPL_DEFINE_STRUCT (IDENTIFIER,MEMBER_SEQ) NBL_EVAL IDENTIFIER \
100+ { \
101+ BOOST_PP_SEQ_FOR_EACH_I (NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER,IDENTIFIER,MEMBER_SEQ) \
102+ }; \
103+ template<uint32_t alignment, bool _restrict> \
104+ struct ::nbl::hlsl::bda::__ref<NBL_EVAL IDENTIFIER,alignment,_restrict> : ::nbl::hlsl::bda::__base_ref<NBL_EVAL IDENTIFIER,alignment,_restrict> \
105+ { \
106+ using __referenced_t = NBL_EVAL IDENTIFIER; \
107+ using base_t = __base_ref<__referenced_t,alignment,_restrict>; \
108+ using this_t = __ref<__referenced_t,alignment,_restrict>; \
109+ \
110+ BOOST_PP_SEQ_FOR_EACH_I (NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER_REFERENCE,dummy,MEMBER_SEQ) \
111+ \
112+ void __init (const ::nbl::hlsl::spirv::bda_pointer_t<__referenced_t> _ptr) \
113+ { \
114+ base_t::__init (_ptr); \
115+ BOOST_PP_SEQ_FOR_EACH_I (NBL_HLSL_IMPL_INIT_STRUCT_MEMBER_REFERENCE,dummy,MEMBER_SEQ) \
116+ } \
117+ }
150118#else
151- /*template<T_ARGS>*/
152- struct /*alignas(NBL_ALIGNAS)*/ MyStruct
153- {
154- alignas (::nbl::hlsl::bda::member_alignment_v<MyStruct,0 >) float32_t a;
155- alignas (::nbl::hlsl::bda::member_alignment_v<MyStruct,1 >) int32_t b;
156- alignas (::nbl::hlsl::bda::member_alignment_v<MyStruct,2 >) int16_t2 c;
157- };
158- #endif
119+ #define NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER (r,IDENTIFIER,i,e) alignas (::nbl::hlsl::bda::member_alignment_v<NBL_EVAL IDENTIFIER,i>) NBL_HLSL_IMPL_DECLARE_STRUCT_MEMBER e
120+ #define NBL_HLSL_IMPL_DEFINE_STRUCT (IDENTIFIER,MEMBER_SEQ) alignas (::nbl::hlsl::alignment_of_v<NBL_EVAL IDENTIFIER >) NBL_EVAL IDENTIFIER \
121+ { \
122+ BOOST_PP_SEQ_FOR_EACH_I (NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER,IDENTIFIER,MEMBER_SEQ) \
123+ }
159124#endif
160125
126+ #include <boost/preprocessor/seq/for_each_i.hpp>
127+ #include <boost/preprocessor/seq/size.hpp>
128+ // MEMBER_SEQ is to be a sequence of variable name and type (identifier0,Type0)...(identifierN,TypeN) @see NBL_HLSL_IMPL_DEFINE_STRUCT_GET_MEMBER_TYPE
129+ // the VA_ARGS is the struct alignment for alignas, usage example
130+ // ```
131+ // NBL_HLSL_DEFINE_STRUCT((MyStruct2),
132+ // ((a, float32_t))
133+ // ((b, int32_t))
134+ // ((c, int32_t2))
135+ // );
136+ // ```
137+ #define NBL_HLSL_DEFINE_STRUCT (IDENTIFIER,MEMBER_SEQ, ...) template<> \
138+ struct ::nbl::hlsl::bda::member_count<NBL_EVAL IDENTIFIER > \
139+ { \
140+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = BOOST_PP_SEQ_SIZE (MEMBER_SEQ); \
141+ }; \
142+ BOOST_PP_SEQ_FOR_EACH_I (NBL_HLSL_IMPL_DEFINE_STRUCT_MEMBER_TYPE,IDENTIFIER,MEMBER_SEQ) \
143+ template <> \
144+ struct ::nbl::hlsl::alignment_of<NBL_EVAL IDENTIFIER > \
145+ { \
146+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = nbl::hlsl::conditional_value<true __VA_OPT__ (&&false ),uint32_t,::nbl::hlsl::bda::impl::default_alignment_v<NBL_EVAL IDENTIFIER >,__VA_OPT__ ((__VA_ARGS__)-)0 >::value; \
147+ }; \
148+ template<> \
149+ struct ::nbl::hlsl::size_of<NBL_EVAL IDENTIFIER > \
150+ { \
151+ using type = NBL_EVAL IDENTIFIER; \
152+ NBL_CONSTEXPR_STATIC_INLINE uint32_t __last_member_ix_v = ::nbl::hlsl::bda::member_count_v<type>-1 ; \
153+ NBL_CONSTEXPR_STATIC_INLINE uint64_t __last_member_offset_v = ::nbl::hlsl::bda::member_offset_v<type,__last_member_ix_v>; \
154+ NBL_CONSTEXPR_STATIC_INLINE uint64_t __last_member_size_v = ::nbl::hlsl::size_of_v<::nbl::hlsl::bda::member_type_t<type,__last_member_ix_v> >; \
155+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = ::nbl::hlsl::mpl::align_up_v<__last_member_offset_v+__last_member_size_v,alignment_of_v<type > >; \
156+ }; \
157+ struct NBL_HLSL_IMPL_DEFINE_STRUCT (IDENTIFIER,MEMBER_SEQ)
158+
161159#endif
0 commit comments