Skip to content

Commit 332e7ed

Browse files
committed
Merge branch 'cad_shaders_work_hatches' into cad_shaders_work
2 parents 1357f02 + 040057e commit 332e7ed

File tree

8 files changed

+310
-58
lines changed

8 files changed

+310
-58
lines changed

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> \
@@ -41,6 +42,7 @@ using add_pointer = std::add_pointer<T>;
4142
#define ARROW .arrow().
4243
#define NBL_CONSTEXPR const static
4344
#define NBL_CONSTEXPR_STATIC_INLINE const static
45+
#define NBL_CONST_MEMBER_FUNC
4446

4547
namespace nbl
4648
{
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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_EQUATIONS_CUBIC_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_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 equations
27+
{
28+
//TODO: use numeric_limits<float_t>::PI
29+
NBL_CONSTEXPR double PI_DOUBLE = 3.14159265358979323846;
30+
31+
template<typename float_t>
32+
struct Cubic
33+
{
34+
using float2_t = vector<float_t, 2>;
35+
using float3_t = vector<float_t, 3>;
36+
37+
float_t c[4];
38+
39+
static Cubic construct(float_t a, float_t b, float_t c, float_t d)
40+
{
41+
Cubic ret;
42+
ret.c[0] = d;
43+
ret.c[1] = c;
44+
ret.c[2] = b;
45+
ret.c[3] = a;
46+
return ret;
47+
}
48+
49+
// Originally from: https://github.com/erich666/GraphicsGems/blob/master/gems/Roots3And4.c
50+
float3_t computeRoots() {
51+
int i;
52+
double sub;
53+
double A, B, C;
54+
double sq_A, p, q;
55+
double cb_p, D;
56+
float3_t s = float3_t(NBL_NOT_A_NUMBER(), NBL_NOT_A_NUMBER(), NBL_NOT_A_NUMBER());
57+
uint rootCount = 0;
58+
59+
/* normal form: x^3 + Ax^2 + Bx + C = 0 */
60+
61+
A = c[2] / c[3];
62+
B = c[1] / c[3];
63+
C = c[0] / c[3];
64+
65+
/* substitute x = y - A/3 to eliminate quadric term:
66+
x^3 +px + q = 0 */
67+
68+
sq_A = A * A;
69+
p = 1.0 / 3 * (-1.0 / 3 * sq_A + B);
70+
q = 1.0 / 2 * (2.0 / 27 * A * sq_A - 1.0 / 3 * A * B + C);
71+
72+
/* use Cardano's formula */
73+
74+
cb_p = p * p * p;
75+
D = q * q + cb_p;
76+
77+
if (D == 0.0)
78+
{
79+
if (q == 0.0) /* one triple solution */
80+
{
81+
s[rootCount++] = 0.0;
82+
}
83+
else /* one single and one double solution */
84+
{
85+
double u = cbrt(-q);
86+
s[rootCount++] = 2 * u;
87+
s[rootCount++] = -u;
88+
}
89+
}
90+
else if (D < 0) /* Casus irreducibilis: three real solutions */
91+
{
92+
double phi = 1.0 / 3 * acos(-q / sqrt(-cb_p));
93+
double t = 2 * sqrt(-p);
94+
95+
s[rootCount++] = t * cos(phi);
96+
s[rootCount++] = -t * cos(phi + PI_DOUBLE / 3);
97+
s[rootCount++] = -t * cos(phi - PI_DOUBLE / 3);
98+
}
99+
else /* one real solution */
100+
{
101+
double sqrt_D = sqrt(D);
102+
double u = cbrt(sqrt_D - q);
103+
double v = -cbrt(sqrt_D + q);
104+
105+
s[rootCount++] = u + v;
106+
}
107+
108+
/* resubstitute */
109+
110+
sub = 1.0 / 3 * A;
111+
112+
for (i = 0; i < rootCount; ++i)
113+
s[i] -= sub;
114+
115+
return s;
116+
}
117+
118+
119+
};
120+
}
121+
}
122+
}
123+
124+
#endif

include/nbl/builtin/hlsl/equations/quadratic.hlsl

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
#define nbl_hlsl_FLT_EPSILON 5.96046447754e-08
1111
#endif
1212

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+
1322
#define SHADER_CRASHING_ASSERT(expr) \
1423
do { \
1524
[branch] if (!(expr)) \
@@ -28,74 +37,44 @@ namespace equations
2837
using float2_t = vector<float_t, 2>;
2938
using float3_t = vector<float_t, 3>;
3039

31-
float_t A;
32-
float_t B;
33-
float_t C;
40+
float_t a;
41+
float_t b;
42+
float_t c;
3443

35-
static Quadratic construct(float_t A, float_t B, float_t C)
44+
static Quadratic construct(float_t a, float_t b, float_t c)
3645
{
37-
Quadratic ret = { A, B, C };
46+
Quadratic ret = { a, b, c };
3847
return ret;
3948
}
4049

4150
float_t evaluate(float_t t)
4251
{
43-
return t * (A * t + B) + C;
52+
return t * (a * t + b) + c;
4453
}
4554

46-
// SolveQuadratic:
47-
// det = b*b-4.f*a*c;
48-
// rcp = 0.5f/a;
49-
// detSqrt = sqrt(det)*rcp;
50-
// tmp = b*rcp;
51-
// res = float2(-detSqrt,detSqrt)-tmp;
52-
//
53-
// Collapsed version:
54-
// detrcp2 = det * rcp * rcp
55-
// brcp = b * rcp
56-
//
57-
// (computeRoots())
58-
// detSqrt = sqrt(detrcp2)
59-
// res = float2(-detSqrt,detSqrt)-bRcp;
60-
struct PrecomputedRootFinder
55+
float2_t computeRoots()
6156
{
62-
float_t detRcp2;
63-
float_t brcp;
57+
float2_t ret;
6458

65-
float2_t computeRoots() {
66-
float_t detSqrt = sqrt(detRcp2);
67-
float2_t roots = float2_t(-detSqrt,detSqrt)-brcp;
68-
// assert(roots.x == roots.y);
69-
// assert(!isnan(roots.x));
70-
// if a = 0, brcp is inf
71-
// then we return detRcp2, which was set below to -c / b
72-
// that works as a solution to the linear equation bx+c=0
73-
return isinf(brcp) ? detRcp2 : roots;
74-
}
59+
const float_t det = b * b - 4.0 * a * c;
60+
const float_t detSqrt = sqrt(det);
61+
const float_t rcp = 0.5 / a;
62+
const float_t bOver2A = b * rcp;
7563

76-
static PrecomputedRootFinder construct(float_t detRcp2, float_t brcp)
64+
float_t t0 = 0.0, t1 = 0.0;
65+
if (b >= 0)
7766
{
78-
PrecomputedRootFinder result;
79-
result.detRcp2 = detRcp2;
80-
result.brcp = brcp;
81-
return result;
67+
ret[0] = -detSqrt * rcp - bOver2A;
68+
ret[1] = 2 * c / (-b - detSqrt);
8269
}
83-
84-
static PrecomputedRootFinder construct(nbl::hlsl::equations::Quadratic<float_t> quadratic)
70+
else
8571
{
86-
float_t a = quadratic.A;
87-
float_t b = quadratic.B;
88-
float_t c = quadratic.C;
89-
90-
float_t det = b*b-4.f*a*c;
91-
float_t rcp = 0.5f/a;
92-
93-
PrecomputedRootFinder result;
94-
result.brcp = b * rcp;
95-
result.detRcp2 = isinf(result.brcp) ? -c / b : det * rcp * rcp;
96-
return result;
72+
ret[0] = 2 * c / (-b + detSqrt);
73+
ret[1] = +detSqrt * rcp - bOver2A;
9774
}
98-
};
75+
76+
return ret;
77+
}
9978

10079

10180
};

0 commit comments

Comments
 (0)