Skip to content

Commit be825d6

Browse files
committed
Merge branch 'cad_shaders_work'
# Conflicts: # examples_tests -> resolved by not touching it
2 parents 4682256 + 04d8286 commit be825d6

File tree

14 files changed

+827
-739
lines changed

14 files changed

+827
-739
lines changed

include/nbl/builtin/hlsl/algorithm.hlsl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#ifndef _NBL_BUILTIN_HLSL_ALGORITHM_INCLUDED_
55
#define _NBL_BUILTIN_HLSL_ALGORITHM_INCLUDED_
66

7+
#include "nbl/builtin/hlsl/functional.hlsl"
8+
79
namespace nbl
810
{
911
namespace hlsl
@@ -99,10 +101,12 @@ uint lower_bound(inout Accessor accessor, const uint begin, const uint end, cons
99101
template<class Accessor, class Comparator>
100102
uint upper_bound(inout Accessor accessor, const uint begin, const uint end, const typename Accessor::value_type value, const Comparator comp)
101103
{
102-
using TransformedComparator = impl::lower_to_upper_comparator_transform_t<Accessor,Comparator>;
103-
TransformedComparator transformedComparator;
104+
//using TransformedComparator = impl::lower_to_upper_comparator_transform_t<Accessor,Comparator>;
105+
//TransformedComparator transformedComparator;
106+
107+
impl::lower_to_upper_comparator_transform_t<Accessor,Comparator> transformedComparator;
104108
transformedComparator.comp = comp;
105-
return lower_bound<Accessor,TransformedComparator>(accessor,begin,end,value,transformedComparator);
109+
return lower_bound<Accessor,impl::lower_to_upper_comparator_transform_t<Accessor,Comparator> >(accessor,begin,end,value,transformedComparator);
106110
}
107111

108112

@@ -113,16 +117,20 @@ namespace impl
113117
template<class Accessor, typename T>
114118
uint lower_bound(inout Accessor accessor, const uint begin, const uint end, const T value)
115119
{
116-
using Comparator = impl::comparator_lt_t<T>;
117-
Comparator comp;
118-
return nbl::hlsl::lower_bound<Accessor,Comparator>(accessor,begin,end,value,comp);
120+
//using Comparator = nbl::hlsl::less<T>;
121+
//Comparator comp;
122+
123+
nbl::hlsl::less<T> comp;
124+
return nbl::hlsl::lower_bound<Accessor, nbl::hlsl::less<T> >(accessor,begin,end,value,comp);
119125
}
120126
template<class Accessor, typename T>
121127
uint upper_bound(inout Accessor accessor, const uint begin, const uint end, const T value)
122128
{
123-
using Comparator = impl::comparator_lt_t<T>;
124-
Comparator comp;
125-
return nbl::hlsl::upper_bound<Accessor,Comparator>(accessor,begin,end,value,comp);
129+
//using Comparator = nbl::hlsl::less<T>;
130+
//Comparator comp;
131+
132+
nbl::hlsl::less<T> comp;
133+
return nbl::hlsl::upper_bound<Accessor, nbl::hlsl::less<T> >(accessor,begin,end,value,comp);
126134
}
127135

128136
}

include/nbl/builtin/hlsl/cpp_compat.hlsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define NBL_CONSTEXPR constexpr
1212
#define NBL_CONSTEXPR_STATIC constexpr static
1313
#define NBL_CONSTEXPR_STATIC_INLINE constexpr static inline
14+
#define NBL_CONST_MEMBER_FUNC const
1415

1516
#define NBL_ALIAS_TEMPLATE_FUNCTION(origFunctionName, functionAlias) \
1617
template<typename... Args> \
@@ -38,6 +39,7 @@ using add_pointer = std::add_pointer<T>;
3839
#define ARROW .arrow().
3940
#define NBL_CONSTEXPR const static
4041
#define NBL_CONSTEXPR_STATIC_INLINE const static
42+
#define NBL_CONST_MEMBER_FUNC
4143

4244
namespace nbl
4345
{
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (C) 2018-2023 - 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+
5+
#ifndef _NBL_BUILTIN_HLSL_MATH_EQUATIONS_CUBIC_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_MATH_EQUATIONS_CUBIC_INCLUDED_
7+
8+
// TODO: Later include from correct hlsl header
9+
#ifndef nbl_hlsl_FLT_EPSILON
10+
#define nbl_hlsl_FLT_EPSILON 5.96046447754e-08
11+
#endif
12+
13+
#ifndef NBL_NOT_A_NUMBER
14+
#ifdef __cplusplus
15+
#define NBL_NOT_A_NUMBER() nbl::core::nan<float_t>()
16+
#else
17+
// https://learn.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-float-rules#honored-ieee-754-rules
18+
#define NBL_NOT_A_NUMBER() 0.0/0.0
19+
#endif
20+
#endif //NBL_NOT_A_NUMBER
21+
22+
namespace nbl
23+
{
24+
namespace hlsl
25+
{
26+
namespace math
27+
{
28+
namespace equations
29+
{
30+
//TODO: use numeric_limits<float_t>::PI
31+
NBL_CONSTEXPR double PI_DOUBLE = 3.14159265358979323846;
32+
33+
template<typename float_t>
34+
struct Cubic
35+
{
36+
using float2_t = vector<float_t, 2>;
37+
using float3_t = vector<float_t, 3>;
38+
39+
float_t c[4];
40+
41+
static Cubic construct(float_t a, float_t b, float_t c, float_t d)
42+
{
43+
Cubic ret;
44+
ret.c[0] = d;
45+
ret.c[1] = c;
46+
ret.c[2] = b;
47+
ret.c[3] = a;
48+
return ret;
49+
}
50+
51+
// Originally from: https://github.com/erich666/GraphicsGems/blob/master/gems/Roots3And4.c
52+
float3_t computeRoots() {
53+
int i;
54+
double sub;
55+
double A, B, C;
56+
double sq_A, p, q;
57+
double cb_p, D;
58+
float3_t s = float3_t(NBL_NOT_A_NUMBER(), NBL_NOT_A_NUMBER(), NBL_NOT_A_NUMBER());
59+
uint rootCount = 0;
60+
61+
/* normal form: x^3 + Ax^2 + Bx + C = 0 */
62+
63+
A = c[2] / c[3];
64+
B = c[1] / c[3];
65+
C = c[0] / c[3];
66+
67+
/* substitute x = y - A/3 to eliminate quadric term:
68+
x^3 +px + q = 0 */
69+
70+
sq_A = A * A;
71+
p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
72+
q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);
73+
74+
/* use Cardano's formula */
75+
76+
cb_p = p * p * p;
77+
D = q * q + cb_p;
78+
79+
if (D == 0.0)
80+
{
81+
if (q == 0.0) /* one triple solution */
82+
{
83+
s[rootCount++] = 0.0;
84+
}
85+
else /* one single and one double solution */
86+
{
87+
double u = cbrt(-q);
88+
s[rootCount++] = 2 * u;
89+
s[rootCount++] = -u;
90+
}
91+
}
92+
else if (D < 0) /* Casus irreducibilis: three real solutions */
93+
{
94+
double phi = 1.0 / 3 * acos(-q / sqrt(-cb_p));
95+
double t = 2 * sqrt(-p);
96+
97+
s[rootCount++] = t * cos(phi);
98+
s[rootCount++] = -t * cos(phi + PI_DOUBLE / 3);
99+
s[rootCount++] = -t * cos(phi - PI_DOUBLE / 3);
100+
}
101+
else /* one real solution */
102+
{
103+
double sqrt_D = sqrt(D);
104+
double u = cbrt(sqrt_D - q);
105+
double v = -cbrt(sqrt_D + q);
106+
107+
s[rootCount++] = u + v;
108+
}
109+
110+
/* resubstitute */
111+
112+
sub = 1.0 / 3 * A;
113+
114+
for (i = 0; i < rootCount; ++i)
115+
s[i] -= sub;
116+
117+
return s;
118+
}
119+
120+
121+
};
122+
}
123+
}
124+
}
125+
}
126+
127+
#endif
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (C) 2018-2023 - 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+
5+
#ifndef _NBL_BUILTIN_HLSL_MATH_EQUATIONS_QUADRATIC_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_MATH_EQUATIONS_QUADRATIC_INCLUDED_
7+
8+
// TODO: Later include from correct hlsl header
9+
#ifndef nbl_hlsl_FLT_EPSILON
10+
#define nbl_hlsl_FLT_EPSILON 5.96046447754e-08
11+
#endif
12+
13+
#ifndef NBL_NOT_A_NUMBER
14+
#ifdef __cplusplus
15+
#define NBL_NOT_A_NUMBER() nbl::core::nan<float_t>()
16+
#else
17+
// https://learn.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-float-rules#honored-ieee-754-rules
18+
#define NBL_NOT_A_NUMBER() 0.0/0.0
19+
#endif
20+
#endif //NBL_NOT_A_NUMBER
21+
22+
#define SHADER_CRASHING_ASSERT(expr) \
23+
do { \
24+
[branch] if (!(expr)) \
25+
vk::RawBufferStore<uint32_t>(0xdeadbeefBADC0FFbull,0x45u,4u); \
26+
} while(true)
27+
28+
namespace nbl
29+
{
30+
namespace hlsl
31+
{
32+
namespace math
33+
{
34+
namespace equations
35+
{
36+
template<typename float_t>
37+
struct Quadratic
38+
{
39+
using float2_t = vector<float_t, 2>;
40+
using float3_t = vector<float_t, 3>;
41+
42+
float_t a;
43+
float_t b;
44+
float_t c;
45+
46+
static Quadratic construct(float_t a, float_t b, float_t c)
47+
{
48+
Quadratic ret = { a, b, c };
49+
return ret;
50+
}
51+
52+
float_t evaluate(float_t t)
53+
{
54+
return t * (a * t + b) + c;
55+
}
56+
57+
float2_t computeRoots()
58+
{
59+
float2_t ret;
60+
61+
const float_t det = b * b - 4.0 * a * c;
62+
const float_t detSqrt = sqrt(det);
63+
const float_t rcp = 0.5 / a;
64+
const float_t bOver2A = b * rcp;
65+
66+
float_t t0 = 0.0, t1 = 0.0;
67+
if (b >= 0)
68+
{
69+
ret[0] = -detSqrt * rcp - bOver2A;
70+
ret[1] = 2 * c / (-b - detSqrt);
71+
}
72+
else
73+
{
74+
ret[0] = 2 * c / (-b + detSqrt);
75+
ret[1] = +detSqrt * rcp - bOver2A;
76+
}
77+
78+
return ret;
79+
}
80+
81+
82+
};
83+
}
84+
}
85+
}
86+
}
87+
88+
#endif

0 commit comments

Comments
 (0)