55#define _NBL_BUILTIN_HLSL_BDA_REF_INCLUDED_
66
77#include "nbl/builtin/hlsl/functional.hlsl"
8+ #include "nbl/builtin/hlsl/mpl.hlsl"
89
910namespace nbl
1011{
@@ -26,6 +27,69 @@ struct __spv_ptr_t<T,true>
2627 [[vk::ext_decorate (spv::DecorationRestrictPointer)]] spirv::bda_pointer_t<T> value;
2728};
2829
30+
31+ // silly utility traits
32+ template<typename T>
33+ struct member_count
34+ {
35+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 0 ;
36+ };
37+ template<typename T>
38+ NBL_CONSTEXPR uint32_t member_count_v = member_count<T>::value;
39+
40+ template<typename T, int32_t MemberIx>
41+ struct member_type;
42+ template<typename T, int32_t MemberIx>
43+ using member_type_t = typename member_type<T,MemberIx>::type;
44+
45+ // default alignment is the alignment of the type
46+ template<typename T, int32_t MemberIx>
47+ struct member_alignment
48+ {
49+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = alignment_of_v<member_type_t<T,MemberIx> >;
50+ };
51+ template<typename T, int32_t MemberIx>
52+ NBL_CONSTEXPR uint32_t member_alignment_v = member_alignment<T,MemberIx>::value;
53+
54+ namespace impl
55+ {
56+ template<typename T, uint32_t N>
57+ struct default_alignment
58+ {
59+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = mpl::max_v<uint32_t,member_alignment_v<T,N-1 >,member_alignment_v<T,N-2 > >;
60+ };
61+ template<typename T>
62+ struct default_alignment<T,1 >
63+ {
64+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = member_alignment_v<T,0 >;
65+ };
66+ // le invalid value
67+ template<typename T>
68+ struct default_alignment<T,0 >
69+ {
70+ NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 0 ;
71+ };
72+ template<typename T, typename MemberCount=member_count<T> >
73+ NBL_CONSTEXPR uint32_t default_alignment_v = default_alignment<T,MemberCount::value>::value;
74+ }
75+
76+ // the default specialization of the offset assumes scalar layout
77+ template<typename T, int32_t MemberIx>
78+ struct member_offset
79+ {
80+ // TODO: assert that the custom alignment is no less than the type's natural alignment?
81+ // first byte past previous member, rounded up to out alignment
82+ NBL_CONSTEXPR_STATIC_INLINE uint64_t value = mpl::align_up_v<member_offset<T,MemberIx-1 >::value+size_of_v<member_type_t<T,MemberIx-1 > >,member_alignment_v<T,MemberIx> >;
83+ };
84+ template<typename T>
85+ struct member_offset<T,0 >
86+ {
87+ NBL_CONSTEXPR_STATIC_INLINE uint64_t value = 0 ;
88+ };
89+ template<typename T, int32_t MemberIx>
90+ NBL_CONSTEXPR uint64_t member_offset_v = member_offset<T,MemberIx>::value;
91+
92+
2993template<typename T, uint32_t alignment, bool _restrict>
3094struct __base_ref
3195{
@@ -112,9 +176,9 @@ struct nbl::hlsl::bda::__ref<MyStruct,alignment,_restrict> : nbl::hlsl::bda::__b
112176 void __init(const nbl::hlsl::spirv::bda_pointer_t<MyStruct> _ptr)
113177 {
114178 base_t::__init(_ptr);
115- a.__init(spirv::accessChain<float32_t>(base_t::__get_spv_ptr() ,0));
116- b.__init(spirv::accessChain<int32_t>(base_t::__get_spv_ptr() ,1));
117- c.__init(spirv::accessChain<int16_t2>(base_t::__get_spv_ptr() ,2));
179+ a.__init(spirv::accessChain<float32_t>(base_t::ptr.value ,0));
180+ b.__init(spirv::accessChain<int32_t>(base_t::ptr.value ,1));
181+ c.__init(spirv::accessChain<int16_t2>(base_t::ptr.value ,2));
118182 }
119183};
120184*/
0 commit comments