3 #include <alicorn/std/string.hpp>
4 #include <alicorn/std/string/encoding.hpp>
5 #include <alicorn/std/regex.hpp>
6 #include <boost/algorithm/string/replace.hpp>
7 #include <Covellite/Api/Component.inl>
8 #include <Covellite/Api/Defines.hpp>
36 using String_t = ::alicorn::extension::std::String;
54 class Component::Rasterizer
57 const String_t CullMode;
60 explicit Rasterizer(Component_t & _Component) :
61 CullMode(_Component[uT(
"cull")].Default(uT(
"Back")))
65 Rasterizer(
const Rasterizer &) =
delete;
66 Rasterizer(Rasterizer &&) =
delete;
67 Rasterizer & operator= (
const Rasterizer &) =
delete;
68 Rasterizer & operator= (Rasterizer &&) =
delete;
69 ~Rasterizer(
void) =
default;
72 class Component::Scissor
80 const uint8_t Align[3] = { 0 };
83 explicit Scissor(Component_t & _Component) :
84 Left(_Component[uT(
"left")].Default(0)),
85 Top(_Component[uT(
"top")].Default(0)),
86 Right(_Component[uT(
"right")].Default(0)),
87 Bottom(_Component[uT(
"bottom")].Default(0)),
88 IsEnabled(_Component[uT(
"enabled")].Default(false))
92 Scissor(
const Scissor &) =
delete;
93 Scissor(Scissor &&) =
delete;
94 Scissor & operator= (
const Scissor &) =
delete;
95 Scissor & operator= (Scissor &&) =
delete;
96 ~Scissor(
void) =
default;
100 class Component::Buffer
102 static const Buffer_t<T> & GetFakeData(
void) noexcept
104 static const Buffer_t<T> FakeData;
109 const Buffer_t<T> Data;
111 const uint8_t Align[4] = { 0 };
114 inline static Buffer_t<T> GetContent(
115 Component_t & _Component,
116 const Buffer_t<T> & _Data)
118 return ::std::move((Buffer_t<T> &)_Component[uT(
"content")].Default(_Data));
122 explicit Buffer(Component_t & _Component,
const Buffer_t<T> & _Data = GetFakeData()) :
123 Data(GetContent(_Component, _Data)),
124 Dimension(_Component[uT(
"dimension")].Default(3))
127 Buffer(
const Buffer &) =
delete;
128 Buffer(Buffer &&) =
delete;
129 Buffer & operator= (
const Buffer &) =
delete;
130 Buffer & operator= (Buffer &&) =
delete;
131 ~Buffer(
void) =
default;
134 class Component::Texture :
135 public Buffer<uint8_t>
138 const uint8_t *
const pTextureData;
142 const String_t Destination;
145 const bool IsUsingMipmapping;
146 const bool IsMapping;
148 const uint8_t Align[6] = { 0 };
151 Texture(Component_t & _Component,
const String_t & _DefaultDestination) :
153 pTextureData(Data.empty() ? nullptr : Data.data()),
154 Width(_Component[uT(
"width")].Default(0)),
155 Height(_Component[uT(
"height")].Default(0)),
156 Name(_Component[uT(
"name")].Default(uT(
"Unknown"))),
157 Destination(_Component[uT(
"destination")].Default(_DefaultDestination)),
158 Index(_Component[uT(
"index")].Default(-1)),
159 Capacity(_Component[uT(
"capacity")].Default(8)),
160 IsUsingMipmapping(_Component[uT(
"mipmapping")].Default(false)),
161 IsMapping(_Component[uT(
"mapper")].IsType<const cbBufferMap_t<const void> &>())
165 Texture(
const Texture &) =
delete;
166 Texture(Texture &&) =
delete;
167 Texture & operator= (
const Texture &) =
delete;
168 Texture & operator= (Texture &&) =
delete;
169 ~Texture(
void) =
default;
172 class Component::Shader :
173 public Buffer<uint8_t>
176 const ::std::string Entry;
177 ::std::string ReturnType;
179 const ::std::vector<String_t> Instance;
182 static String_t GetShaderType(
183 const ::std::string & _Entry,
184 const uint8_t * _pBegin,
185 const uint8_t * _pEnd,
186 ::std::string & _ReturnType)
191 const auto * pLastBreak = _pBegin;
195 const auto * pBreak = ::std::find(pLastBreak, _pEnd,
'\n');
198 throw STD_EXCEPTION <<
"Entry point not found: " << _Entry;
201 const ::std::string Line{ pLastBreak, pBreak };
202 const auto EntryPosition = Line.find(
" " + _Entry +
"(", 0);
203 if (EntryPosition != ::std::string::npos)
205 _ReturnType = Line.substr(0, Line.find(
" ", 0));
207 const auto TypeBegin = EntryPosition + _Entry.length()
209 const auto TypeEnd = Line.find(
" ", TypeBegin);
211 using namespace ::alicorn::extension::std;
213 return string_cast<String, Encoding::Ascii128>(
214 Line.substr(TypeBegin, TypeEnd - TypeBegin));
217 pLastBreak = pBreak + 1;
221 static ::std::vector<String_t> GetInstance(
const String_t _Value)
223 ::std::vector<String_t> Result;
225 if (_Value == uT(
""))
return Result;
227 namespace regex = ::alicorn::extension::std::regex;
229 regex::Match mInstanceDataStruct{ uT(
"(?:[if]4)*") };
230 if (!mInstanceDataStruct.IsMatch(_Value))
232 throw STD_EXCEPTION <<
"Unexpected instance value: " << _Value;
235 regex::Search sInstanceDataStruct{ uT(
"(?:([if])4)") };
236 if (!sInstanceDataStruct.IsSearch(_Value))
return Result;
238 const auto & Elements = sInstanceDataStruct.GetCoincided();
240 for (::std::size_t i = 0; i < Elements.size(); i++)
242 Result.push_back(Elements[i].second[0]);
248 ::std::string GetInstanceInput(const ::std::string & _Input)
const
250 static const ::std::string InstanceBlockDeclaration =
251 "/* place for instance variables */";
252 ::std::string InstanceBlockImplementation;
254 for (::std::size_t i = 0; i < Instance.size(); i++)
256 const auto Index = ::std::to_string(i + 1);
258 (Instance[i] == uT(
"f")) ?
"float4" :
259 (Instance[i] == uT(
"i")) ?
"int4" :
"";
261 InstanceBlockImplementation += ::std::string{
"COVELLITE_IN " } +
262 Type +
" iValue" + Index +
" COVELLITE_INPUT_SEMANTIC(TEXCOORD" +
263 Index +
");" +
char{ 0x5C } +
"\r\n";
266 return ::boost::algorithm::replace_first_copy(_Input,
267 InstanceBlockDeclaration, InstanceBlockImplementation);
271 inline BinaryData_t GetInstanceInput(
const BinaryData_t & _Input)
const
273 if (Instance.empty())
return _Input;
275 const auto strInput =
276 GetInstanceInput(::std::string{ _Input.cbegin(), _Input.cend() });
277 return BinaryData_t{ strInput.cbegin(), strInput.cend() };
280 ::std::string GetInstanceCopyData(
void)
const
282 ::std::string InstanceCopyDataImplementation;
284 for (::std::size_t i = 0; i < Instance.size(); i++)
286 const auto iValue =
"iValue" + ::std::to_string(i + 1);
288 InstanceCopyDataImplementation +=
289 " InputData." + iValue +
" = " + iValue +
";\r\n";
292 return InstanceCopyDataImplementation;
296 Shader(Component_t & _Component,
const BinaryData_t & _Data) :
297 Buffer(_Component, _Data),
298 Entry((const ::std::string &)_Component[uT(
"entry")].Default(
"Unknown")),
299 Kind(GetShaderType(Entry, Data.data(), Data.data() + Data.size(), ReturnType)),
300 Instance{ GetInstance(_Component[uT(
"instance")].Default(uT(
""))) }
303 Shader(
const Shader &) =
delete;
304 Shader(Shader &&) =
delete;
305 Shader & operator= (
const Shader &) =
delete;
306 Shader & operator= (Shader &&) =
delete;
307 ~Shader(
void) =
default;
310 class Component::Transform
313 inline static size_t GetHashX(
void)
315 static const size_t Hash = ::std::hash<String_t>{}(uT(
"x"));
319 inline static size_t GetHashY(
void)
321 static const size_t Hash = ::std::hash<String_t>{}(uT(
"y"));
325 inline static size_t GetHashZ(
void)
327 static const size_t Hash = ::std::hash<String_t>{}(uT(
"z"));
337 Transform(Component_t & _Component,
const float _DefaultValue) :
338 X(_Component[GetHashX()].Default(_DefaultValue)),
339 Y(_Component[GetHashY()].Default(_DefaultValue)),
340 Z(_Component[GetHashZ()].Default(_DefaultValue))
344 Transform(
const Transform &) =
delete;
345 Transform(Transform &&) =
delete;
346 Transform & operator= (
const Transform &) =
delete;
347 Transform & operator= (Transform &&) =
delete;
348 ~Transform(
void) =
default;
351 class Component::Position :
355 explicit Position(Component_t & _Component) :
356 Transform(_Component, 0.0f)
360 Position(
const Position &) =
delete;
361 Position(Position &&) =
delete;
362 Position & operator= (
const Position &) =
delete;
363 Position & operator= (Position &&) =
delete;
364 ~Position(
void) =
default;
367 class Component::Rotation :
371 explicit Rotation(Component_t & _Component) :
372 Transform(_Component, 0.0f)
376 Rotation(
const Rotation &) =
delete;
377 Rotation(Rotation &&) =
delete;
378 Rotation & operator= (
const Rotation &) =
delete;
379 Rotation & operator= (Rotation &&) =
delete;
380 ~Rotation(
void) =
default;
383 class Component::Scale :
387 explicit Scale(Component_t & _Component) :
388 Transform(_Component, 1.0f)
392 Scale(
const Scale &) =
delete;
393 Scale(Scale &&) =
delete;
394 Scale & operator= (
const Scale &) =
delete;
395 Scale & operator= (Scale &&) =
delete;
396 ~Scale(
void) =
default;
402 const uint32_t Color;
408 explicit Fog(Component_t & _Component) :
409 Color(_Component[uT(
"color")].Default(0xFFFFFFFF)),
410 Near(_Component[uT(
"near")].Default(10.0f)),
411 Far(_Component[uT(
"far")].Default(100.0f)),
412 Density(_Component[uT(
"density")].Default(1.0f))
416 Fog(
const Fog &) =
delete;
417 Fog(Fog &&) =
delete;
418 Fog & operator= (
const Fog &) =
delete;
419 Fog & operator= (Fog &&) =
delete;
420 ~Fog(
void) =
default;