Skip to content

Commit 9af73f8

Browse files
committed
Merge branch 'hlsl' of github.com:Devsh-Graphics-Programming/Nabla into dxcIntegration
2 parents a9841d1 + 3bcd088 commit 9af73f8

File tree

7 files changed

+847
-0
lines changed

7 files changed

+847
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
// Copyright (C) 2022 - 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_ALGORITHM_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_ALGORITHM_INCLUDED_
6+
7+
namespace nbl
8+
{
9+
namespace hlsl
10+
{
11+
12+
13+
namespace impl
14+
{
15+
16+
// TODO: move this to some other header
17+
bool isNPoT(const uint x)
18+
{
19+
return x&(x-1);
20+
}
21+
22+
template<class Accessor, class Comparator>
23+
struct bound_t
24+
{
25+
static bound_t<Accessor,Comparator> setup(uint begin, const uint end, const typename Accessor::value_type _value, const Comparator _comp)
26+
{
27+
bound_t<Accessor,Comparator> retval;
28+
retval.comp = _comp;
29+
retval.value = _value;
30+
retval.it = begin;
31+
retval.len = end-begin;
32+
return retval;
33+
}
34+
35+
36+
uint operator()(inout Accessor accessor)
37+
{
38+
if (isNPoT(len))
39+
{
40+
const uint newLen = 0x1u<<firstbithigh(len);
41+
const uint testPoint = it+(len-newLen);
42+
len = newLen;
43+
comp_step(accessor,testPoint);
44+
}
45+
while (len)
46+
{
47+
// could unroll 3 times or more
48+
iteration(accessor);
49+
iteration(accessor);
50+
}
51+
comp_step(accessor,it,it+1u);
52+
return it;
53+
}
54+
55+
void iteration(inout Accessor accessor)
56+
{
57+
len >>= 1;
58+
const uint mid = it+len;
59+
comp_step(accessor,mid);
60+
}
61+
62+
void comp_step(inout Accessor accessor, const uint testPoint, const uint rightBegin)
63+
{
64+
if (comp(accessor[testPoint],value))
65+
it = rightBegin;
66+
}
67+
void comp_step(inout Accessor accessor, const uint testPoint)
68+
{
69+
comp_step(accessor,testPoint,testPoint);
70+
}
71+
72+
Comparator comp;
73+
typename Accessor::value_type value;
74+
uint it;
75+
uint len;
76+
};
77+
78+
template<class Accessor, class Comparator>
79+
struct lower_to_upper_comparator_transform_t
80+
{
81+
bool operator()(const typename Accessor::value_type lhs, const typename Accessor::value_type rhs)
82+
{
83+
return !comp(rhs,lhs);
84+
}
85+
86+
Comparator comp;
87+
};
88+
89+
}
90+
91+
92+
template<class Accessor, class Comparator>
93+
uint lower_bound(inout Accessor accessor, const uint begin, const uint end, const typename Accessor::value_type value, const Comparator comp)
94+
{
95+
impl::bound_t<Accessor,Comparator> implementation = impl::bound_t<Accessor,Comparator>::setup(begin,end,value,comp);
96+
return implementation(accessor);
97+
}
98+
99+
template<class Accessor, class Comparator>
100+
uint upper_bound(inout Accessor accessor, const uint begin, const uint end, const typename Accessor::value_type value, const Comparator comp)
101+
{
102+
using TransformedComparator = impl::lower_to_upper_comparator_transform_t<Accessor,Comparator>;
103+
TransformedComparator transformedComparator;
104+
transformedComparator.comp = comp;
105+
return lower_bound<Accessor,TransformedComparator>(accessor,begin,end,value,transformedComparator);
106+
}
107+
108+
109+
namespace impl
110+
{
111+
112+
template<typename T>
113+
struct comparator_lt_t
114+
{
115+
bool operator()(const T lhs, const T rhs)
116+
{
117+
return lhs<rhs;
118+
}
119+
};
120+
121+
// extra indirection due to https://github.com/microsoft/DirectXShaderCompiler/issues/4771
122+
template<class Accessor, typename T>
123+
uint lower_bound(inout Accessor accessor, const uint begin, const uint end, const T value)
124+
{
125+
using Comparator = impl::comparator_lt_t<T>;
126+
Comparator comp;
127+
return nbl::hlsl::lower_bound<Accessor,Comparator>(accessor,begin,end,value,comp);
128+
}
129+
template<class Accessor, typename T>
130+
uint upper_bound(inout Accessor accessor, const uint begin, const uint end, const T value)
131+
{
132+
using Comparator = impl::comparator_lt_t<T>;
133+
Comparator comp;
134+
return nbl::hlsl::upper_bound<Accessor,Comparator>(accessor,begin,end,value,comp);
135+
}
136+
137+
}
138+
139+
template<class Accessor>
140+
uint lower_bound(inout Accessor accessor, const uint begin, const uint end, const typename Accessor::value_type value)
141+
{
142+
return impl::lower_bound<Accessor,typename Accessor::value_type>(accessor,begin,end,value);
143+
}
144+
template<class Accessor>
145+
uint upper_bound(inout Accessor accessor, const uint begin, const uint end, const typename Accessor::value_type value)
146+
{
147+
return impl::upper_bound<Accessor,typename Accessor::value_type>(accessor,begin,end,value);
148+
}
149+
150+
}
151+
}
152+
153+
#endif
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (C) 2018-2022 - 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_BARYCENTRIC_UTILS_INCLUDED_
5+
#define _NBL_BUILTIN_HLSL_BARYCENTRIC_UTILS_INCLUDED_
6+
7+
namespace nbl
8+
{
9+
namespace hlsl
10+
{
11+
namespace barycentric
12+
{
13+
14+
float2 reconstructBarycentrics(in float3 positionRelativeToV0, in float2x3 edges)
15+
{
16+
const float e0_2 = dot(edges[0], edges[0]);
17+
const float e0e1 = dot(edges[0], edges[1]);
18+
const float e1_2 = dot(edges[1], edges[1]);
19+
20+
const float qe0 = dot(positionRelativeToV0, edges[0]);
21+
const float qe1 = dot(positionRelativeToV0, edges[1]);
22+
const float2 protoBary = float2(qe0 * e1_2 - qe1 * e0e1, qe1 * e0_2 - qe0 * e0e1);
23+
24+
const float rcp_dep = 1.f / (e0_2 * e1_2 - e0e1 * e0e1);
25+
return protoBary * rcp_dep;
26+
}
27+
float2 reconstructBarycentrics(in float3 pointPosition, in float3x3 vertexPositions)
28+
{
29+
return reconstructBarycentrics(pointPosition - vertexPositions[2], float2x3(vertexPositions[0] - vertexPositions[2], vertexPositions[1] - vertexPositions[2]));
30+
}
31+
32+
float3 expand(in float2 compactBarycentrics)
33+
{
34+
return float3(compactBarycentrics.xy,1.f-compactBarycentrics.x-compactBarycentrics.y);
35+
}
36+
37+
}
38+
}
39+
}
40+
41+
#endif

0 commit comments

Comments
 (0)