Berserk
MeshFormats.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_MESHFORMATS_HPP
29 #define BERSERK_MESHFORMATS_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Typedefs.hpp>
33 #include <core/templates/Mask.hpp>
34 #include <rhi/RHIDefs.hpp>
36 
37 #include <mutex>
38 #include <unordered_map>
39 
41 
51 enum class MeshAttribute : uint8 {
53  Position = 0,
55  Normal = 1,
57  Tangent = 2,
59  Color = 3,
61  UV = 4,
63  UV2 = 5,
65  Weights = 6,
67  Bones = 7
68 };
69 
75  bool vertex = false;
76  bool attribute = false;
77  bool skinning = false;
78  uint32 size = 0;
80 };
81 
87 
92 class MeshFormats final {
93 public:
94  BRK_API MeshFormats() = default;
95  BRK_API ~MeshFormats() = default;
96 
100  BRK_API void GetStride(MeshFormat format, uint32 &vertex, uint32 &attribute, uint32 &skinning);
101 
102 private:
103  std::unordered_map<String, Ref<RHIVertexDeclaration>> mCached;
104  mutable std::mutex mMutex;
105 };
106 
109  MeshAttributeInfo info{};
110 
111  switch (attribute) {
115  info.vertex = true;
116  info.size = 3 * sizeof(float);
117  info.elementType = RHIVertexElementType::Float3;
118  break;
120  info.attribute = true;
121  info.size = 3 * sizeof(float);
122  info.elementType = RHIVertexElementType::Float3;
123  break;
124  case MeshAttribute::UV:
125  case MeshAttribute::UV2:
126  info.attribute = true;
127  info.size = 2 * sizeof(float);
128  info.elementType = RHIVertexElementType::Float2;
129  break;
131  info.skinning = true;
132  info.size = 4 * sizeof(float);
133  info.elementType = RHIVertexElementType::Float4;
134  break;
136  info.skinning = true;
137  info.size = 4 * sizeof(int);
138  info.elementType = RHIVertexElementType::Int4;
139  break;
140  default:
141  break;
142  }
143 
144  return info;
145 }
146 
152 
153 #endif//BERSERK_MESHFORMATS_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
std::uint8_t uint8
Definition: Typedefs.hpp:38
std::uint32_t uint32
Definition: Typedefs.hpp:44
Available mesh vertex attributes.
Mask defining mesh format (composed from attributes)
Manages vertex layouts for default engine mesh formats.
Definition: MeshFormats.hpp:92
BRK_API uint32 GetAttributeOffset(MeshAttribute attribute, MeshFormat format)
Definition: MeshFormats.cpp:123
BRK_API Ref< RHIVertexDeclaration > GetDeclaration(MeshFormat target, MeshFormat format)
Definition: MeshFormats.cpp:42
BRK_API ~MeshFormats()=default
BRK_API MeshFormats()=default
BRK_API void GetStride(MeshFormat format, uint32 &vertex, uint32 &attribute, uint32 &skinning)
Definition: MeshFormats.cpp:146
MeshAttributeInfo MeshGetAttributeInfo(MeshAttribute attribute)
Definition: MeshFormats.hpp:108
RHIVertexElementType
Definition: RHIDefs.hpp:71
Definition: GLDevice.cpp:46
Info about attribute.
Definition: MeshFormats.hpp:74
bool attribute
Definition: MeshFormats.hpp:76
bool vertex
Definition: MeshFormats.hpp:75
bool skinning
Definition: MeshFormats.hpp:77
RHIVertexElementType elementType
Definition: MeshFormats.hpp:79
uint32 size
Definition: MeshFormats.hpp:78