Skip to content

Commit 9745682

Browse files
author
devsh
committed
I am very sick in the head.
1 parent 1eb3eb9 commit 9745682

File tree

8 files changed

+270
-192
lines changed

8 files changed

+270
-192
lines changed

include/nbl/core/algorithm/utility.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,30 @@ struct type_list_size<type_list<T...>> : std::integral_constant<size_t,sizeof...
1818
template<typename TypeList>
1919
inline constexpr size_t type_list_size_v = type_list_size<TypeList>::value;
2020

21+
template<template<typename> class, typename TypeList>
22+
struct filter;
23+
template<template<typename> class Pred, typename... T>
24+
struct filter<Pred,type_list<T...>>
25+
{
26+
using type = type_list<>;
27+
};
28+
29+
template<template<typename> class Pred, typename T, typename... Ts>
30+
struct filter<Pred,type_list<T,Ts...>>
31+
{
32+
template<typename, typename>
33+
struct Cons;
34+
template<typename Head, typename... Tail>
35+
struct Cons<Head,type_list<Tail...>>
36+
{
37+
using type = type_list<Head,Tail...>;
38+
};
39+
40+
using type = std::conditional_t<Pred<T>::value,typename Cons<T,typename filter<Pred,type_list<Ts...>>::type>::type,typename filter<Pred,type_list<Ts...>>::type>;
41+
};
42+
template<template<typename> class Pred, typename TypeList>
43+
using filter_t = filter<Pred,TypeList>::type;
44+
2145
template<template<class...> class ListLikeOutT, template<class> class X, typename ListLike>
2246
struct list_transform
2347
{

include/nbl/ext/MitsubaLoader/CElementSensor.h

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,13 @@ class CElementSensor final : public IElement
3030
PERSPECTIVE_RDIST,
3131
INVALID
3232
};
33-
static inline core::unordered_map<core::string,Type,core::CaseInsensitiveHash,core::CaseInsensitiveEquals> compStringToTypeMap()
34-
{
35-
return {
36-
{"perspective", Type::PERSPECTIVE},
37-
{"thinlens", Type::THINLENS},
38-
{"orthographic", Type::ORTHOGRAPHIC},
39-
{"telecentric", Type::TELECENTRIC},
40-
{"spherical", Type::SPHERICAL},
41-
{"irradiancemeter", Type::IRRADIANCEMETER},
42-
{"radiancemeter", Type::RADIANCEMETER},
43-
{"fluencemeter", Type::FLUENCEMETER}/*,
44-
{"perspective_rdist", PERSPECTIVE_RDIST}*/
45-
};
46-
}
4733

4834
constexpr static inline uint8_t MaxClipPlanes = 6u;
4935

5036
struct ShutterSensor
5137
{
5238
hlsl::float32_t3 up = hlsl::float32_t3(0,1,0);
53-
hlsl::float32_t3 clipPlanes[MaxClipPlanes] = {};
39+
hlsl::float32_t4 clipPlanes[MaxClipPlanes] = {};
5440
float moveSpeed = core::nan<float>();
5541
float zoomSpeed = core::nan<float>();
5642
float rotateSpeed = core::nan<float>();
@@ -64,6 +50,8 @@ class CElementSensor final : public IElement
6450
};
6551
struct PerspectivePinhole : CameraBase
6652
{
53+
constexpr static inline Type VariantType = Type::PERSPECTIVE;
54+
6755
enum class FOVAxis
6856
{
6957
INVALID,
@@ -86,6 +74,7 @@ class CElementSensor final : public IElement
8674
};
8775
struct Orthographic : CameraBase
8876
{
77+
constexpr static inline Type VariantType = Type::ORTHOGRAPHIC;
8978
};
9079
struct DepthOfFieldBase
9180
{
@@ -94,27 +83,59 @@ class CElementSensor final : public IElement
9483
};
9584
struct PerspectiveThinLens : PerspectivePinhole, DepthOfFieldBase
9685
{
86+
constexpr static inline Type VariantType = Type::THINLENS;
9787
};
9888
struct TelecentricLens : Orthographic, DepthOfFieldBase
9989
{
90+
constexpr static inline Type VariantType = Type::TELECENTRIC;
10091
};
10192
struct SphericalCamera : CameraBase
10293
{
94+
constexpr static inline Type VariantType = Type::SPHERICAL;
10395
};
10496
struct IrradianceMeter : ShutterSensor
10597
{
98+
constexpr static inline Type VariantType = Type::IRRADIANCEMETER;
10699
};
107100
struct RadianceMeter : ShutterSensor
108101
{
102+
constexpr static inline Type VariantType = Type::RADIANCEMETER;
109103
};
110104
struct FluenceMeter : ShutterSensor
111105
{
106+
constexpr static inline Type VariantType = Type::FLUENCEMETER;
112107
};/*
113108
struct PerspectivePinholeRadialDistortion : PerspectivePinhole
114109
{
115110
kc;
116111
};*/
117112

113+
using variant_list_t = core::type_list<
114+
PerspectivePinhole,
115+
PerspectiveThinLens,
116+
Orthographic,
117+
TelecentricLens,
118+
SphericalCamera,
119+
IrradianceMeter,
120+
RadianceMeter,
121+
FluenceMeter
122+
>;
123+
static inline core::unordered_map<core::string,Type,core::CaseInsensitiveHash,core::CaseInsensitiveEquals> compStringToTypeMap()
124+
{
125+
return {
126+
{"perspective", Type::PERSPECTIVE},
127+
{"thinlens", Type::THINLENS},
128+
{"orthographic", Type::ORTHOGRAPHIC},
129+
{"telecentric", Type::TELECENTRIC},
130+
{"spherical", Type::SPHERICAL},
131+
{"irradiancemeter", Type::IRRADIANCEMETER},
132+
{"radiancemeter", Type::RADIANCEMETER},
133+
{"fluencemeter", Type::FLUENCEMETER}/*,
134+
{"perspective_rdist", PERSPECTIVE_RDIST}*/
135+
};
136+
}
137+
static AddPropertyMap<CElementSensor> compAddPropertyMap();
138+
118139
inline CElementSensor(const char* id) : IElement(id), type(Type::INVALID), /*toWorldType(IElement::Type::TRANSFORM),*/ transform(), film(""), sampler("")
119140
{
120141
}
@@ -182,7 +203,9 @@ class CElementSensor final : public IElement
182203

183204
bool addProperty(SNamedPropertyElement&& _property, system::logger_opt_ptr logger) override;
184205
bool onEndTag(CMitsubaMetadata* globalMetadata, system::logger_opt_ptr logger) override;
185-
inline IElement::Type getType() const override { return IElement::Type::SENSOR; }
206+
207+
constexpr static inline auto ElementType = IElement::Type::SENSOR;
208+
inline IElement::Type getType() const override { return ElementType; }
186209
inline std::string getLogName() const override { return "sensor"; }
187210

188211
inline bool processChildData(IElement* _child, const std::string& name) override

include/nbl/ext/MitsubaLoader/IElement.h

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#define _NBL_EXT_MISTUBA_LOADER_I_ELEMENT_H_INCLUDED_
66

77

8+
#include "nbl/core/algorithm/utility.h"
89
#include "nbl/asset/interchange/IAssetLoader.h"
910

1011
#include "nbl/ext/MitsubaLoader/PropertyElement.h"
@@ -14,6 +15,21 @@ namespace nbl::ext::MitsubaLoader
1415
{
1516
class CMitsubaMetadata;
1617

18+
namespace impl
19+
{
20+
template<template<typename...> class Pred, typename... Args>
21+
struct ToUnaryPred
22+
{
23+
template<typename T>
24+
struct type : bool_constant<Pred<T,Args...>::value> {};
25+
};
26+
27+
template<typename T, typename TypeList>
28+
struct mpl_of_passing;
29+
template<typename T, typename TypeList>
30+
using mpl_of_passing_t = mpl_of_passing<T,TypeList>::type;
31+
}
32+
1733
class IElement
1834
{
1935
public:
@@ -108,7 +124,12 @@ class IElement
108124
return _atts[attrCount];
109125
}
110126

111-
//
127+
// if we used `variant` instead of union we could default implement this
128+
//template<typename Derived, typename... Variants>
129+
//static inline void defaultVisit(Derived* this)
130+
//{
131+
// generated switch / visit of `Variant`
132+
//}
112133
template<typename Derived>
113134
static inline void copyVariant(Derived* to, const Derived* from)
114135
{
@@ -123,7 +144,69 @@ class IElement
123144
}
124145
);
125146
}
147+
148+
// could move it to `nbl/builtin/hlsl/mpl`
149+
template<typename Type, Type... values>
150+
struct mpl_array
151+
{
152+
constexpr static inline Type data[] = { values... };
153+
};
154+
//
155+
template<typename Derived>
156+
struct AddPropertyCallback
157+
{
158+
using element_t = Derived;
159+
// TODO: list or map of supported variants (if `visit` is present)
160+
using func_t = bool(*)(Derived*,SNamedPropertyElement&&,const system::logger_opt_ptr);
161+
162+
inline bool operator()(Derived* d, SNamedPropertyElement&& p, const system::logger_opt_ptr l) const {return func(d,std::move(p),l);}
163+
164+
func_t func;
165+
// will usually point at
166+
std::span<const typename Derived::Type> allowedVariantTypes = {};
167+
};
168+
template<typename Derived>
169+
using PropertyNameCallbackMap = core::unordered_map<core::string,AddPropertyCallback<Derived>,core::CaseInsensitiveHash,core::CaseInsensitiveEquals>;
170+
template<typename Derived>
171+
class AddPropertyMap
172+
{
173+
template<Derived::Type... types>
174+
inline void registerCallback(const SNamedPropertyElement::Type type, std::string&& propertyName, AddPropertyCallback<Derived> cb)
175+
{
176+
if constexpr (sizeof...(types))
177+
cb.allowedVariantTypes = mpl_array<Type,types...>::data;
178+
registerCallback(type,std::move(propertyName),cb);
179+
}
180+
181+
public:
182+
inline void registerCallback(const SNamedPropertyElement::Type type, std::string&& propertyName, const AddPropertyCallback<Derived>& cb)
183+
{
184+
auto [nameIt,inserted] = byPropertyType[type].emplace(std::move(propertyName),cb);
185+
assert(inserted);
186+
}
187+
template<template<typename...> class Pred, typename... Args>
188+
inline void registerCallback(const SNamedPropertyElement::Type type, std::string&& propertyName, AddPropertyCallback<Derived>::func_t cb)
189+
{
190+
AddPropertyCallback<Derived> callback = {.func=cb};
191+
using UnaryPred = impl::ToUnaryPred<Pred,Args...>;
192+
using passing_types = core::filter_t<typename UnaryPred::type,typename Derived::variant_list_t>;
193+
if constexpr (core::type_list_size_v<passing_types>)
194+
callback.allowedVariantTypes = impl::mpl_of_passing_t<typename Derived::Type,passing_types>::data;
195+
registerCallback(type,std::move(propertyName),callback);
196+
}
197+
198+
std::array<PropertyNameCallbackMap<Derived>,SNamedPropertyElement::Type::INVALID> byPropertyType = {};
199+
};
200+
};
201+
202+
namespace impl
203+
{
204+
template<typename T, typename... V>
205+
struct mpl_of_passing<T,core::type_list<V...>>
206+
{
207+
using type = IElement::mpl_array<T,V::VariantType...>;
126208
};
209+
}
127210

128211
}
129212
#endif

include/nbl/ext/MitsubaLoader/ParserUtil.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "nbl/ext/MitsubaLoader/CMitsubaMetadata.h"
1111
#include "nbl/ext/MitsubaLoader/PropertyElement.h"
12+
#include "nbl/ext/MitsubaLoader/CElementSensor.h"
1213
#include "nbl/ext/MitsubaLoader/CElementShape.h"
1314

1415
#include <stack>
@@ -72,7 +73,22 @@ class ParserManager final
7273
const core::unordered_set<std::string,core::CaseInsensitiveHash,core::CaseInsensitiveEquals> propertyElements;
7374
const CPropertyElementManager propertyElementManager;
7475

76+
using supported_elements_t = core::type_list<
77+
// CElementIntegrator,
78+
CElementSensor
79+
// CElementFilm,
80+
// CElementRFilter,
81+
// CElementSampler,
82+
/// CElementShape,
83+
/// CElementBSDF,
84+
/// CElementTexture,
85+
/// CElementEmitter,
86+
// CElementEmissionProfile
87+
>;
88+
7589
private:
90+
const core::tuple_transform_t<IElement::AddPropertyMap,supported_elements_t> addPropertyMaps;
91+
7692
struct SNamedElement
7793
{
7894
IElement* element = nullptr;
@@ -97,17 +113,7 @@ class ParserManager final
97113
//
98114
uint32_t sceneDeclCount = 0;
99115
// TODO: This leaks memory all over the place because destructors are not ran!
100-
ElementPool</*
101-
CElementIntegrator,
102-
CElementSensor,
103-
CElementFilm,
104-
CElementRFilter,
105-
CElementSampler,
106-
CElementShape,
107-
CElementBSDF,
108-
CElementTexture,
109-
CElementEmitter*/
110-
> objects = {};
116+
ElementPool<> objects = {};
111117
// aliases and names (in Mitsbua XML you can give nodes names and `ref` them)
112118
core::unordered_map<core::string,IElement*,core::CaseInsensitiveHash,core::CaseInsensitiveEquals> handles = {};
113119
// stack of currently processed elements, each element of index N is parent of the element of index N+1

0 commit comments

Comments
 (0)