SpiecsEngine
 
Loading...
Searching...
No Matches
YamlUtils.h
Go to the documentation of this file.
1/**
2* @file YamlUtils.h.
3* @brief YamlUtils Definitions.
4* @author Spices.
5*/
6
7#pragma once
8#include "Core/Core.h"
9
10#include <yaml-cpp/yaml.h>
11#include "Resources/Material/Material.h"
12#include <glm/glm.hpp>
13
14namespace YAML {
15
16 template<>
17 struct convert<glm::vec2>
18 {
19 static Node encode(const glm::vec2& rhs)
20 {
21 Node node;
22 node.push_back(rhs.x);
23 node.push_back(rhs.y);
24 return node;
25 }
26
27 static bool decode(const Node& node, glm::vec2& rhs)
28 {
29 if (!node.IsSequence() || node.size() != 2)
30 {
31 return false;
32 }
33
34 rhs.x = node[0].as<float>();
35 rhs.y = node[1].as<float>();
36
37 return true;
38 }
39 };
40
41 template<>
42 struct convert<glm::vec3>
43 {
44 static Node encode(const glm::vec3& rhs)
45 {
46 Node node;
50 return node;
51 }
52
53 static bool decode(const Node& node, glm::vec3& rhs)
54 {
55 if (!node.IsSequence() || node.size() != 3)
56 {
57 return false;
58 }
59
60 rhs.x = node[0].as<float>();
61 rhs.y = node[1].as<float>();
62 rhs.z = node[2].as<float>();
63
64 return true;
65 }
66 };
67
68 template<>
69 struct convert<glm::vec4>
70 {
71 static Node encode(const glm::vec4& rhs)
72 {
73 Node node;
78 return node;
79 }
80
81 static bool decode(const Node& node, glm::vec4& rhs)
82 {
83 if (!node.IsSequence() || node.size() != 4)
84 {
85 return false;
86 }
87
88 rhs.x = node[0].as<float>();
89 rhs.y = node[1].as<float>();
90 rhs.z = node[2].as<float>();
91 rhs.w = node[3].as<float>();
92
93 return true;
94 }
95 };
96
97 template<>
98 struct convert<Spices::TextureParam>
99 {
101 {
102 Node node;
105 return node;
106 }
107
108 static bool decode(const Node& node, Spices::TextureParam& param)
109 {
110 if (!node.IsSequence() || node.size() != 2)
111 {
112 return false;
113 }
114
117
118 return true;
119 }
120 };
121
122 template<>
123 struct convert<Spices::ConstantParam>
124 {
126 {
127 Node node;
129
131 else if(param.paramType == "float3") node.push_back(std::any_cast<glm::vec3>(param.paramValue));
132 else if(param.paramType == "float2") node.push_back(std::any_cast<glm::vec2>(param.paramValue));
133 else if(param.paramType == "float") node.push_back(std::any_cast<float>(param.paramValue));
134 else if(param.paramType == "int") node.push_back(std::any_cast<int>(param.paramValue));
135 else if(param.paramType == "bool") node.push_back(std::any_cast<bool>(param.paramValue));
136 else
137 {
139 ss << "YAML::convert<Spices::Material::ConstantParam>: not supported paramType: " << param.paramType;
140
142 }
143
144 return node;
145 }
146
147 static bool decode(const Node& node, Spices::ConstantParam& param)
148 {
149 if (!node.IsSequence() || node.size() != 2)
150 {
152 ss << "YAML::convert<Spices::Material::ConstantParam>: bad node size, current size is" << node.size() << ", required 4";
153
155 return false;
156 }
157
158 param.paramType = node[0].as<std::string>();
159
160 if (param.paramType == "float4") param.paramValue = node[1].as<glm::vec4>();
161 else if(param.paramType == "float3") param.paramValue = node[1].as<glm::vec3>();
162 else if(param.paramType == "float2") param.paramValue = node[1].as<glm::vec2>();
163 else if(param.paramType == "float") param.paramValue = node[1].as<float>();
164 else if(param.paramType == "int") param.paramValue = node[1].as<int>();
165 else if(param.paramType == "bool") param.paramValue = node[1].as<bool>();
166 else
167 {
169 ss << "YAML::convert<Spices::Material::ConstantParam>: not supported paramType: " << param.paramType;
170
172 }
173
174 return true;
175 }
176 };
177}
178
179namespace Spices {
180
182 {
183 out << YAML::Flow;
184 out << YAML::BeginSeq << v.x << v.y << YAML::EndSeq;
185 return out;
186 }
187
189 {
190 out << YAML::Flow;
191 out << YAML::BeginSeq << v.x << v.y << v.z << YAML::EndSeq;
192 return out;
193 }
194
196 {
197 out << YAML::Flow;
198 out << YAML::BeginSeq << v.x << v.y << v.z << v.w << YAML::EndSeq;
199 return out;
200 }
201
203 {
204 out << YAML::Flow;
205 out << YAML::BeginSeq << p.textureType << p.texturePath << YAML::EndSeq;
206 return out;
207 }
208
210 {
211 out << YAML::Flow;
212 out << YAML::BeginSeq << p.paramType;
213
214 if(p.paramType == "float4")
215 {
216 out << std::any_cast<glm::vec4>(p.paramValue);
217 }
218 else if(p.paramType == "float3")
219 {
220 out << std::any_cast<glm::vec3>(p.paramValue);
221 }
222 else if(p.paramType == "float2")
223 {
224 out << std::any_cast<glm::vec2>(p.paramValue);
225 }
226 else if(p.paramType == "float")
227 {
228 out << std::any_cast<float>(p.paramValue);
229 }
230 else if(p.paramType == "int")
231 {
232 out << std::any_cast<int>(p.paramValue);
233 }
234 else if (p.paramType == "bool")
235 {
236 out << std::any_cast<bool>(p.paramValue);
237 }
238 else
239 {
240 std::stringstream ss;
241 ss << "YAML::operator<<: not supported paramType: " << p.paramType;
242
243 SPICES_CORE_WARN(ss.str())
244
245 out << YAML::EndSeq;
246 }
247
248 out << YAML::EndSeq;
249 return out;
250 }
251}
#define SPICES_PROFILE_ZONE
static bool FileLibrary_Read(const FileHandle *handle, uint64_t data_size, void *out_data, uint64_t *out_bytes_read)
Read Specific size of data form the current file handle pointer, and move pointer the same size.
static bool FileLibrary_Open(const char *path, FileModes mode, bool binary, FileHandle *out_handle)
Open the file using given string.
static void FileLibrary_Close(FileHandle *handle)
Close the file by the file handle.
File Static Function Library.
Definition FileLibrary.h:49
static bool SaveDefaultMaterial()
Test function.
static bool Load(const std::string &fileName, Material *outMaterial)
Public called API, it is entrance.
static bool LoadFromSASSET(const std::string &fileName, Material *outMaterial)
Load data from a .sasset file.
static bool LoadFromMaterial(const std::string &fileName, Material *outMaterial)
Load data from a .material file.
This enum defines tree types of material file.
Material Class. This class contains a branch of parameter and shader, also descriptor.
Definition Material.h:62
static bool StringsEqual(const char *str0, const char *str1)
Determine if the strings given are equal. Platform Specific.
String Static Function Library.
const std::string defaultMaterialPath
Const variable: Original Material File Path.
YAML::Emitter & operator<<(YAML::Emitter &out, const glm::vec2 &v)
Definition YamlUtils.h:181
const std::string defaultBinMaterialPath
Const variable: Bin Material File Path.
static void SerializeShaderConfig(YAML::Emitter &out, const std::string &shaderStage, const std::string &shaderPath)
Serialize Shader Config.
const std::string defaultBinShaderPath
Const variable: Bin Shader File Path.
constexpr char LoaderSignStart[100]
Const variable: Material File Confirm header start.
static void SerializeTextureConfig(YAML::Emitter &out, const std::string &name, const TextureParam &param)
Serialize Texture Config.
YAML::Emitter & operator<<(YAML::Emitter &out, const TextureParam &p)
Definition YamlUtils.h:202
constexpr char LoaderSignOver[100]
Const variable: Material File Confirm header over.
@ FILE_MODE_READ
model : read
Definition FileLibrary.h:37
YAML::Emitter & operator<<(YAML::Emitter &out, const ConstantParam &p)
Definition YamlUtils.h:209
std::string paramType
Definition Material.h:38
This Struct is FILE* handle pointer Wrapper.
Definition FileLibrary.h:15
This struct's data is defined from .material file.
Definition Material.h:27
static Node encode(const glm::vec2 &rhs)
Definition YamlUtils.h:19
static bool decode(const Node &node, glm::vec2 &rhs)
Definition YamlUtils.h:27