Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
ProtoBufUtils.cpp
1/*
2Copyright (C) 2016,2018,2019,2021,2025 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#include <array>
17#include <regex>
19#include "aeongames/ProtoBufUtils.hpp"
20#include "aeongames/CRC.hpp"
21#include "aeongames/Mesh.hpp"
22#ifdef _MSC_VER
23#pragma warning( push )
24#pragma warning( disable : PROTOBUF_WARNINGS )
25#endif
26#include "reference.pb.h"
27#include "pipeline.pb.h"
28#include "property.pb.h"
29#include "vector3.pb.h"
30#include "quaternion.pb.h"
31#include "transform.pb.h"
32#include "scene.pb.h"
33#include "mesh.pb.h"
34#ifdef _MSC_VER
35#pragma warning( pop )
36#endif
37
38namespace AeonGames
39{
40 static_assert ( static_cast<uint32_t> ( Mesh::BYTE ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_BYTE ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
41 static_assert ( static_cast<uint32_t> ( Mesh::UNSIGNED_BYTE ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_UNSIGNED_BYTE ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
42 static_assert ( static_cast<uint32_t> ( Mesh::HALF_FLOAT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_HALF_FLOAT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
43 static_assert ( static_cast<uint32_t> ( Mesh::INT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_INT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
44 static_assert ( static_cast<uint32_t> ( Mesh::UNSIGNED_INT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_UNSIGNED_INT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
45 static_assert ( static_cast<uint32_t> ( Mesh::FLOAT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_FLOAT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
46 static_assert ( static_cast<uint32_t> ( Mesh::FIXED ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_FIXED ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
47 static_assert ( static_cast<uint32_t> ( Mesh::DOUBLE ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_DOUBLE ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
48 static_assert ( static_cast<uint32_t> ( Mesh::SHORT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_SHORT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
49 static_assert ( static_cast<uint32_t> ( Mesh::UNSIGNED_SHORT ) == static_cast<uint32_t> ( AttributeMsg_AttributeType_UNSIGNED_SHORT ), "Mesh AttributeType and MeshMSG AttributeType do not match" );
50
51 static_assert ( static_cast<uint32_t> ( Mesh::POSITION ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_POSITION ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
52 static_assert ( static_cast<uint32_t> ( Mesh::NORMAL ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_NORMAL ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
53 static_assert ( static_cast<uint32_t> ( Mesh::TANGENT ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_TANGENT ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
54 static_assert ( static_cast<uint32_t> ( Mesh::BITANGENT ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_BITANGENT ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
55 static_assert ( static_cast<uint32_t> ( Mesh::TEXCOORD ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_TEXCOORD ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
56 static_assert ( static_cast<uint32_t> ( Mesh::WEIGHT_INDEX ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_WEIGHT_INDEX ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
57 static_assert ( static_cast<uint32_t> ( Mesh::WEIGHT_VALUE ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_WEIGHT_VALUE ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
58 static_assert ( static_cast<uint32_t> ( Mesh::COLOR ) == static_cast<uint32_t> ( AttributeMsg_AttributeSemantic_COLOR ), "Mesh AttributeSemantic and MeshMSG AttributeSemantic do not match" );
59
60 uint32_t GetReferenceMsgId ( const ReferenceMsg& reference_buffer )
61 {
62 switch ( reference_buffer.reference_case() )
63 {
64 case ReferenceMsg::kPath:
65 return crc32i ( reference_buffer.path().c_str(), reference_buffer.path().size() );
66 case ReferenceMsg::kId:
67 return reference_buffer.id();
68 default:
69 return 0;
70 }
71 }
72
73 Vector3 GetVector3 ( const Vector3Msg& aVector3 )
74 {
75 return {aVector3.x(), aVector3.y(), aVector3.z() };
76 }
77 Quaternion GetQuaternion ( const QuaternionMsg& aQuaternion )
78 {
79 return {aQuaternion.w(), aQuaternion.x(), aQuaternion.y(), aQuaternion.z() };
80 }
81
82 Transform GetTransform ( const TransformMsg& aTransform )
83 {
84 return
85 {
86 GetVector3 ( aTransform.scale() ),
87 GetQuaternion ( aTransform.rotation() ),
88 GetVector3 ( aTransform.translation() )
89 };
90 }
91
92 Property GetProperty ( const ComponentPropertyMsg& aComponentPropertyMsg )
93 {
94 switch ( aComponentPropertyMsg.value_case() )
95 {
96 case ComponentPropertyMsg::kInt:
97 return static_cast<int> ( aComponentPropertyMsg.int_() );
98 case ComponentPropertyMsg::kLong:
99 return static_cast<long> ( aComponentPropertyMsg.long_() );
100 case ComponentPropertyMsg::kLongLong:
101 return static_cast<long long> ( aComponentPropertyMsg.long_long() );
102 case ComponentPropertyMsg::kUnsigned:
103 return static_cast<unsigned> ( aComponentPropertyMsg.int_() );
104 case ComponentPropertyMsg::kUnsignedLong:
105 return static_cast<long> ( aComponentPropertyMsg.unsigned_long() );
106 case ComponentPropertyMsg::kUnsignedLongLong:
107 return static_cast<long long> ( aComponentPropertyMsg.unsigned_long_long() );
108 case ComponentPropertyMsg::kFloat:
109 return static_cast<float> ( aComponentPropertyMsg.float_() );
110 case ComponentPropertyMsg::kDouble:
111 return static_cast<double> ( aComponentPropertyMsg.double_() );
112 case ComponentPropertyMsg::kString:
113 return aComponentPropertyMsg.string();
114 case ComponentPropertyMsg::kPath:
115 return std::filesystem::path ( aComponentPropertyMsg.string() );
116 case ComponentPropertyMsg::VALUE_NOT_SET:
118 throw std::runtime_error ( "Component property value not set." );
119 break;
120 }
121 return Property{};
122 }
123#if 0
124 size_t GetUniformBufferSize ( const PipelineMsg& aPipelineMsg )
125 {
126 size_t size = 0;
127 for ( auto& i : aPipelineMsg.uniform() )
128 {
129 switch ( i.type() )
130 {
131 case UniformDescriptorMsg::SCALAR_FLOAT:
132 size += ( size % sizeof ( float ) ) ? sizeof ( float ) - ( size % sizeof ( float ) ) : 0; // Align to float
133 size += sizeof ( float );
134 break;
135 case UniformDescriptorMsg::SCALAR_UINT:
136 size += ( size % sizeof ( uint32_t ) ) ? sizeof ( uint32_t ) - ( size % sizeof ( uint32_t ) ) : 0; // Align to uint
137 size += sizeof ( uint32_t );
138 break;
139 case UniformDescriptorMsg::SCALAR_INT:
140 size += ( size % sizeof ( int32_t ) ) ? sizeof ( int32_t ) - ( size % sizeof ( int32_t ) ) : 0; // Align to int
141 size += sizeof ( int32_t );
142 break;
143 case UniformDescriptorMsg::VECTOR_FLOAT_2:
144 size += ( size % ( sizeof ( float ) * 2 ) ) ? ( sizeof ( float ) * 2 ) - ( size % ( sizeof ( float ) * 2 ) ) : 0; // Align to 2 floats
145 size += sizeof ( float ) * 2;
146 break;
147 case UniformDescriptorMsg::VECTOR_FLOAT_3:
148 size += ( size % ( sizeof ( float ) * 4 ) ) ? ( sizeof ( float ) * 4 ) - ( size % ( sizeof ( float ) * 4 ) ) : 0; // Align to 4 floats
149 size += sizeof ( float ) * 3;
150 break;
151 case UniformDescriptorMsg::VECTOR_FLOAT_4:
152 size += ( size % ( sizeof ( float ) * 4 ) ) ? ( sizeof ( float ) * 4 ) - ( size % ( sizeof ( float ) * 4 ) ) : 0; // Align to 4 floats
153 size += sizeof ( float ) * 4;
154 break;
155 default:
156 break;
157 }
158 }
159 return size + ( size % ( sizeof ( float ) * 4 ) ) ? ( sizeof ( float ) * 4 ) - ( size % ( sizeof ( float ) * 4 ) ) : 0; // align the final value to 4 float
160 }
161#endif
162 Material::UniformKeyValue PropertyToKeyValue ( const PropertyMsg& aProperty )
163 {
164 switch ( aProperty.value_case() )
165 {
166 case PropertyMsg::ValueCase::kScalarFloat:
167 return Material::UniformKeyValue{aProperty.name(), aProperty.scalar_float() };
168 case PropertyMsg::ValueCase::kScalarUint:
169 return Material::UniformKeyValue{aProperty.name(), aProperty.scalar_uint() };
170 case PropertyMsg::ValueCase::kScalarInt:
171 return Material::UniformKeyValue{aProperty.name(), aProperty.scalar_int() };
172 case PropertyMsg::ValueCase::kVector2:
173 return Material::UniformKeyValue{aProperty.name(), Vector2{aProperty.vector2().x(), aProperty.vector2().y() }};
174 case PropertyMsg::ValueCase::kVector3:
175 return Material::UniformKeyValue{aProperty.name(), Vector3{aProperty.vector3().x(), aProperty.vector3().y(), aProperty.vector3().z() }};
176 case PropertyMsg::ValueCase::kVector4:
177 return Material::UniformKeyValue{aProperty.name(), Vector4{aProperty.vector4().x(), aProperty.vector4().y(), aProperty.vector4().z(), aProperty.vector4().w() }};
178 case PropertyMsg::ValueCase::kMatrix4X4:
179 return Material::UniformKeyValue{aProperty.name(), Matrix4x4
180 {
181 aProperty.matrix4x4().m0(),
182 aProperty.matrix4x4().m1(),
183 aProperty.matrix4x4().m2(),
184 aProperty.matrix4x4().m3(),
185 aProperty.matrix4x4().m4(),
186 aProperty.matrix4x4().m5(),
187 aProperty.matrix4x4().m6(),
188 aProperty.matrix4x4().m7(),
189 aProperty.matrix4x4().m8(),
190 aProperty.matrix4x4().m9(),
191 aProperty.matrix4x4().m10(),
192 aProperty.matrix4x4().m11(),
193 aProperty.matrix4x4().m12(),
194 aProperty.matrix4x4().m13(),
195 aProperty.matrix4x4().m14(),
196 aProperty.matrix4x4().m15()
197 }};
198 default:
199 break;
200 }
201 throw std::runtime_error ( "Property contained no value." );
202 return {};
203 }
204}
Provides the DLL_PROTOBUF export/import macro for protobuf wrapper classes.
std::tuple< std::string, UniformValue > UniformKeyValue
Key-value pair mapping a uniform name to its value.
Definition Material.hpp:43
4 by 4 matrix in colum mayor order.
Definition Matrix4x4.hpp:35
@ WEIGHT_INDEX
Bone weight indices.
Definition Mesh.hpp:41
@ TANGENT
Vertex tangent.
Definition Mesh.hpp:38
@ TEXCOORD
Texture coordinate.
Definition Mesh.hpp:40
@ NORMAL
Vertex normal.
Definition Mesh.hpp:37
@ COLOR
Vertex color.
Definition Mesh.hpp:43
@ BITANGENT
Vertex bitangent.
Definition Mesh.hpp:39
@ POSITION
Vertex position.
Definition Mesh.hpp:36
@ WEIGHT_VALUE
Bone weight values.
Definition Mesh.hpp:42
@ UNSIGNED_SHORT
Unsigned 16-bit integer.
Definition Mesh.hpp:52
@ HALF_FLOAT
16-bit floating point.
Definition Mesh.hpp:53
@ BYTE
Signed 8-bit integer.
Definition Mesh.hpp:49
@ DOUBLE
64-bit floating point.
Definition Mesh.hpp:58
@ SHORT
Signed 16-bit integer.
Definition Mesh.hpp:51
@ UNSIGNED_BYTE
Unsigned 8-bit integer.
Definition Mesh.hpp:50
@ FLOAT
32-bit floating point.
Definition Mesh.hpp:56
@ FIXED
Fixed-point.
Definition Mesh.hpp:57
@ INT
Signed 32-bit integer.
Definition Mesh.hpp:54
@ UNSIGNED_INT
Unsigned 32-bit integer.
Definition Mesh.hpp:55
Quaternion class.
Component class for any object that requires space transformations.
Definition Transform.hpp:34
2D vector class.
Definition Vector2.hpp:26
3D vector class.
Definition Vector3.hpp:32
4D vector class.
Definition Vector4.hpp:31
<- This is here just for the literals
Definition AABB.hpp:31
Property GetProperty(const ComponentPropertyMsg &aComponentPropertyMsg)
Convert a ComponentPropertyMsg protobuf message to a Property.
Transform GetTransform(const TransformMsg &aTransform)
Convert a TransformMsg protobuf message to a Transform.
std::variant< int, long, long long, unsigned, unsigned long, unsigned long long, float, double, std::string, std::filesystem::path > Property
A variant type that can hold any commonly used property value.
Definition Property.hpp:31
uint32_t crc32i(const char *message, size_t size, uint32_t previous_crc)
Compute the CRC32 of a given message, continuing from a previous CRC value.
Definition CRC.cpp:27
DLL Material::UniformKeyValue PropertyToKeyValue(const PropertyMsg &aProperty)
Convert a PropertyMsg to a Material uniform key-value pair.
Vector3 GetVector3(const Vector3Msg &aVector3)
Convert a Vector3Msg protobuf message to a Vector3.
DLL uint32_t GetReferenceMsgId(const ReferenceMsg &reference_buffer)
Retrieve the identifier from a ReferenceMsg.
Quaternion GetQuaternion(const QuaternionMsg &aQuaternion)
Convert a QuaternionMsg protobuf message to a Quaternion.
size_t GetUniformBufferSize(const PipelineMsg &aPipelineMsg)
Calculate the uniform buffer size required by a pipeline.