Skip to content

Commit 02f18a2

Browse files
author
devsh
committed
everything but the macros are ready
1 parent 9a1c347 commit 02f18a2

File tree

3 files changed

+175
-117
lines changed

3 files changed

+175
-117
lines changed

include/nbl/builtin/hlsl/bda/__ref.hlsl

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
#define _NBL_BUILTIN_HLSL_BDA_REF_INCLUDED_
66

77
#include "nbl/builtin/hlsl/functional.hlsl"
8-
#include "nbl/builtin/hlsl/mpl.hlsl"
8+
#include "nbl/builtin/hlsl/bda/struct_declare.hlsl"
99

1010
namespace nbl
1111
{
1212
namespace hlsl
1313
{
1414
namespace bda
1515
{
16-
1716
template<typename T, bool _restrict>
1817
struct __spv_ptr_t;
1918
template<typename T>
@@ -27,69 +26,6 @@ struct __spv_ptr_t<T,true>
2726
[[vk::ext_decorate(spv::DecorationRestrictPointer)]] spirv::bda_pointer_t<T> value;
2827
};
2928

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-
9329
template<typename T, uint32_t alignment, bool _restrict>
9430
struct __base_ref
9531
{
@@ -130,56 +66,4 @@ struct __ref : __base_ref<T,alignment,_restrict>
13066
}
13167
}
13268
}
133-
134-
// time for some macros!
135-
// Sequence of (variableName,Type)
136-
// need to gen identical struct in HLSL and C++
137-
/*
138-
struct MyStruct
139-
{
140-
// TODO: compute offsets from sizes and alignments
141-
[[vk::ext_decorate(spv::DecorationOffset,0)]] float32_t a;
142-
[[vk::ext_decorate(spv::DecorationOffset,4)]] int32_t b;
143-
[[vk::ext_decorate(spv::DecorationOffset,8)]] int16_t2 c;
144-
};
145-
template<>
146-
struct nbl::hlsl::alignment_of<MyStruct>
147-
{
148-
// TODO: compute alignment if not user specified
149-
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 4;
150-
};
151-
template<<>
152-
struct nbl::hlsl::impl::member_info<MyStruct,0>
153-
{
154-
using type = float32_t;
155-
NBL_CONSTEXPR_STATIC_INLINE uint32_t offset = 0;
156-
};
157-
template<>
158-
struct nbl::hlsl::impl::member_info<MyStruct,1>
159-
{
160-
using type = int32_t;
161-
NBL_CONSTEXPR_STATIC_INLINE uint32_t offset = nbl::hlsl::mpl::round_up<member_info<MyStruct,0>::offset+sizeof(member_info<MyStruct,0>::type),alignof<type> >::value;
162-
};
163-
164-
165-
template<uint32_t alignment, bool _restrict>
166-
struct nbl::hlsl::bda::__ref<MyStruct,alignment,_restrict> : nbl::hlsl::bda::__base_ref<MyStruct,alignment,_restrict>
167-
{
168-
using base_t = __base_ref<MyStruct,alignment,_restrict>;
169-
using this_t = __ref<MyStruct,alignment,_restrict>;
170-
171-
// TODO: compute alignment as min of base alignment and offset alignment
172-
nbl::hlsl::bda::__ref<float32_t,1,_restrict> a;
173-
nbl::hlsl::bda::__ref<int32_t,1,_restrict> b;
174-
nbl::hlsl::bda::__ref<int16_t2,1,_restrict> c;
175-
176-
void __init(const nbl::hlsl::spirv::bda_pointer_t<MyStruct> _ptr)
177-
{
178-
base_t::__init(_ptr);
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));
182-
}
183-
};
184-
*/
18569
#endif
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
#ifndef _NBL_BUILTIN_HLSL_BDA_STRUCT_DECLARE_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_BDA_STRUCT_DECLARE_INCLUDED_
6+
7+
#include "nbl/builtin/hlsl/mpl.hlsl"
8+
9+
namespace nbl
10+
{
11+
namespace hlsl
12+
{
13+
namespace bda
14+
{
15+
// silly utility traits
16+
template<typename T>
17+
struct member_count
18+
{
19+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 0;
20+
};
21+
template<typename T>
22+
NBL_CONSTEXPR uint32_t member_count_v = member_count<T>::value;
23+
24+
template<typename T, int32_t MemberIx>
25+
struct member_type;
26+
template<typename T, int32_t MemberIx>
27+
using member_type_t = typename member_type<T,MemberIx>::type;
28+
29+
// default alignment is the alignment of the type
30+
template<typename T, int32_t MemberIx>
31+
struct member_alignment
32+
{
33+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = alignment_of_v<member_type_t<T,MemberIx> >;
34+
};
35+
template<typename T, int32_t MemberIx>
36+
NBL_CONSTEXPR uint32_t member_alignment_v = member_alignment<T,MemberIx>::value;
37+
38+
// the default specialization of the offset assumes scalar layout
39+
template<typename T, int32_t MemberIx>
40+
struct member_offset
41+
{
42+
// TODO: assert that the custom alignment is no less than the type's natural alignment?
43+
// first byte past previous member, rounded up to out alignment
44+
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> >;
45+
};
46+
template<typename T>
47+
struct member_offset<T,0>
48+
{
49+
NBL_CONSTEXPR_STATIC_INLINE uint64_t value = 0;
50+
};
51+
template<typename T, int32_t MemberIx>
52+
NBL_CONSTEXPR uint64_t member_offset_v = member_offset<T,MemberIx>::value;
53+
54+
// stuff needed to compute alignment of the struct properly
55+
#ifdef __HLSL_VERSION
56+
namespace impl
57+
{
58+
template<typename T, uint32_t N>
59+
struct default_alignment
60+
{
61+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = mpl::max_v<uint32_t,member_alignment_v<T,N-1>,member_alignment_v<T,N-2> >;
62+
};
63+
template<typename T>
64+
struct default_alignment<T,1>
65+
{
66+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = member_alignment_v<T,0>;
67+
};
68+
// le invalid value
69+
template<typename T>
70+
struct default_alignment<T,0>
71+
{
72+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 0;
73+
};
74+
template<typename T, typename MemberCount=member_count<T> >
75+
NBL_CONSTEXPR uint32_t default_alignment_v = default_alignment<T,MemberCount::value>::value;
76+
}
77+
#endif
78+
}
79+
}
80+
}
81+
82+
// time for some macros!
83+
// need to gen identical struct in HLSL and C++
84+
#if 0
85+
// forward declaration - outside of macro
86+
struct MyStruct;
87+
88+
// TODO: to be generated by a BOOST_PP macro:
89+
// - take template parameters list
90+
// - take partial spec name
91+
// - custom alignas on the struct or not
92+
// - sequence of variable name and type (identifier0,Type0)...(identifierN,TypeN)
93+
94+
template</*T_ARGS*/>
95+
struct ::nbl::hlsl::bda::member_count<MyStruct >
96+
{
97+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = 3;
98+
};
99+
template</*T_ARGS*/>
100+
struct ::nbl::hlsl::bda::member_type<MyStruct,0>
101+
{
102+
using type = float32_t;
103+
};
104+
template</*T_ARGS*/>
105+
struct ::nbl::hlsl::bda::member_type<MyStruct,1>
106+
{
107+
using type = int32_t;
108+
};
109+
template</*T_ARGS*/>
110+
struct ::nbl::hlsl::bda::member_type<MyStruct,2>
111+
{
112+
using type = int16_t2;
113+
};
114+
// self alignment
115+
#define NBL_ALIGNAS 8
116+
template<>
117+
struct ::nbl::hlsl::alignment_of<MyStruct >
118+
{
119+
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;
120+
};
121+
template<>
122+
struct ::nbl::hlsl::size_of<MyStruct >
123+
{
124+
NBL_CONSTEXPR_STATIC_INLINE uint32_t __last_member_ix_v = ::nbl::hlsl::bda::member_count_v<MyStruct >-1;
125+
NBL_CONSTEXPR_STATIC_INLINE uint64_t __last_member_offset_v = ::nbl::hlsl::bda::member_offset_v<MyStruct,__last_member_ix_v>;
126+
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> >;
127+
NBL_CONSTEXPR_STATIC_INLINE uint32_t value = mpl::align_up_v<__last_member_offset_v+__last_member_size_v,alignment_of_v<MyStruct > >;
128+
};
129+
#ifdef __HLSL_VERSION
130+
/*template<T_ARGS>*/
131+
struct MyStruct
132+
{
133+
[[vk::ext_decorate(spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,0>)]] float32_t a;
134+
[[vk::ext_decorate(spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,1>)]] int32_t b;
135+
[[vk::ext_decorate(spv::DecorationOffset,::nbl::hlsl::bda::member_offset_v<MyStruct,2>)]] int16_t2 c;
136+
};
137+
template<uint32_t alignment, bool _restrict/*, T_ARGS*/>
138+
struct nbl::hlsl::bda::__ref<MyStruct,alignment,_restrict> : nbl::hlsl::bda::__base_ref<MyStruct,alignment,_restrict>
139+
{
140+
using base_t = __base_ref<MyStruct,alignment,_restrict>;
141+
using this_t = __ref<MyStruct,alignment,_restrict>;
142+
143+
::nbl::hlsl::bda::__ref<float32_t,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,0>,alignment>,_restrict> a;
144+
::nbl::hlsl::bda::__ref<int32_t,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,1>,alignment>,_restrict> b;
145+
::nbl::hlsl::bda::__ref<int16_t2,::nbl::hlsl::mpl::min_v<uint32_t,::nbl::hlsl::bda::member_alignment_v<MyStruct,2>,alignment>,_restrict> c;
146+
147+
void __init(const ::nbl::hlsl::spirv::bda_pointer_t<MyStruct > _ptr)
148+
{
149+
base_t::__init(_ptr);
150+
a.__init(::nbl::hlsl::spirv::accessChain<float32_t>(base_t::ptr.value,0));
151+
b.__init(::nbl::hlsl::spirv::accessChain<int32_t>(base_t::ptr.value,1));
152+
c.__init(::nbl::hlsl::spirv::accessChain<int16_t2>(base_t::ptr.value,2));
153+
}
154+
};
155+
#else
156+
/*template<T_ARGS>*/
157+
struct /*alignas(NBL_ALIGNAS)*/ MyStruct
158+
{
159+
alignas(::nbl::hlsl::bda::member_alignment_v<MyStruct,0>) float32_t a;
160+
alignas(::nbl::hlsl::bda::member_alignment_v<MyStruct,1>) int32_t b;
161+
alignas(::nbl::hlsl::bda::member_alignment_v<MyStruct,2>) int16_t2 c;
162+
};
163+
#endif
164+
#endif
165+
166+
#endif

include/nbl/builtin/hlsl/mpl.hlsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ struct max
9191
};
9292
template<typename T, T X, T Y>
9393
NBL_CONSTEXPR T max_v = max<T,X,Y>::value;
94+
95+
template<typename T, T X, T Y>
96+
struct min
97+
{
98+
NBL_CONSTEXPR_STATIC_INLINE T value = X<Y ? X:Y;
99+
};
100+
template<typename T, T X, T Y>
101+
NBL_CONSTEXPR T min_v = min<T,X,Y>::value;
94102
}
95103
}
96104
}

0 commit comments

Comments
 (0)