Skip to content

Commit 492a0ad

Browse files
author
devsh
committed
ladies and gentlemen we have C++20 concepts in HLSL !
1 parent 5b6b09e commit 492a0ad

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

include/nbl/builtin/hlsl/concepts.hlsl

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
// Copyright (C) 2023 - DevSH Graphics Programming Sp. z O.O.
1+
// Copyright (C) 2023-2024 - DevSH Graphics Programming Sp. z O.O.
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
4-
54
#ifndef _NBL_BUILTIN_HLSL_CONCEPTS_INCLUDED_
65
#define _NBL_BUILTIN_HLSL_CONCEPTS_INCLUDED_
76

7+
88
#include <nbl/builtin/hlsl/cpp_compat/vector.hlsl>
99
#include <nbl/builtin/hlsl/cpp_compat/matrix.hlsl>
1010
#include <nbl/builtin/hlsl/type_traits.hlsl>
1111

1212

13-
#if (__cplusplus >= 202002L && __cpp_concepts)
13+
#ifndef __HLSL_VERSION
1414

15+
// TODO: old stuff, see how much we can remove
1516
#define NBL_CONCEPT_TYPE_PARAMS(...) template <__VA_ARGS__>
1617
#define NBL_CONCEPT_SIGNATURE(NAME, ...) concept NAME = requires(__VA_ARGS__)
1718
#define NBL_CONCEPT_BODY(...) { __VA_ARGS__ };
1819
#define NBL_CONCEPT_ASSIGN(NAME, ...) concept NAME = __VA_ARGS__;
1920
#define NBL_REQUIRES(...) requires __VA_ARGS__
2021

22+
// for struct definitions, use instead of closing `>` on the primary template parameter list
23+
#define NBL_PRIMARY_REQUIRES(...) > requires (__VA_ARGS__)
24+
25+
// to put right before the closing `>` of the primary template definition, otherwise `NBL_PARTIAL_REQUIRES` wont work on specializations
26+
#define NBL_STRUCT_CONSTRAINABLE
27+
// NOTE: C++20 requires and C++11 enable_if have to be in different places! ITS OF UTTMOST IMPORTANCE YOUR REQUIRE CLAUSES ARE IDENTICAL FOR BOTH MACROS
28+
// put just after the closing `>` on the partial template specialization `template` declaration e.g. `template<typename U, typename V, typename T> NBL_PARTIAL_REQ_TOP(SomeCond<U>)
29+
#define NBL_PARTIAL_REQ_TOP(...) requires (__VA_ARGS__)
30+
// put just before closing `>` on the partial template specialization Type args, e.g. `MyStruct<U,V,T NBL_PARTIAL_REQ_BOT(SomeCond<U>)>
31+
#define NBL_PARTIAL_REQ_BOT(...)
32+
33+
// condition
34+
#define NBL_FUNC_REQUIRES_BEGIN(...) requires (__VA_ARGS__)
35+
// return value
36+
#define NBL_FUNC_REQUIRES_END(...) __VA_ARGS__
37+
2138
#include <concepts>
2239

2340
namespace nbl
@@ -77,12 +94,31 @@ concept matricial = is_matrix<T>::value;
7794

7895
#else
7996

97+
// TODO: old stuff, see how much we can remove
8098
// No C++20 support. Do nothing.
8199
#define NBL_CONCEPT_TYPE_PARAMS(...)
82100
#define NBL_CONCEPT_SIGNATURE(NAME, ...)
83101
#define NBL_CONCEPT_BODY(...)
84102
#define NBL_REQUIRES(...)
85103

104+
105+
// for struct definitions, use instead of closing `>` on the primary template parameter list
106+
#define NBL_PRIMARY_REQUIRES(...) ,typename __requires=::nbl::hlsl::enable_if_t<(__VA_ARGS__),void> >
107+
108+
// to put right before the closing `>` of the primary template definition, otherwise `NBL_PARTIAL_REQUIRES` wont work on specializations
109+
#define NBL_STRUCT_CONSTRAINABLE ,typename __requires=void
110+
// NOTE: C++20 requires and C++11 enable_if have to be in different places! ITS OF UTTMOST IMPORTANCE YOUR REQUIRE CLAUSES ARE IDENTICAL FOR BOTH MACROS
111+
// put just after the closing `>` on the partial template specialization `template` declaration e.g. `template<typename U, typename V, typename T> NBL_PARTIAL_REQ_TOP(SomeCond<U>)
112+
#define NBL_PARTIAL_REQ_TOP(...)
113+
// put just before closing `>` on the partial template specialization Type args, e.g. `MyStruct<U,V,T NBL_PARTIAL_REQ_BOT(SomeCond<U>)>
114+
#define NBL_PARTIAL_REQ_BOT(...) ,std::enable_if_t<(__VA_ARGS__),void>
115+
116+
// condition, use right after the closing `>` of a function template
117+
#define NBL_FUNC_REQUIRES_BEGIN(...) ::nbl::hlsl::enable_if_t<(__VA_ARGS__),
118+
// return value, use `END(T)` instead of the return value type declaration
119+
#define NBL_FUNC_REQUIRES_END(...) __VA_ARGS__>
120+
86121
#endif
87122

123+
88124
#endif

0 commit comments

Comments
 (0)