1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #if !defined(API_3D_ECS_COMPONENTS_MESH_COMPONENT_H) || defined(IMPLEMENT_MANAGER)
17 #define API_3D_ECS_COMPONENTS_MESH_COMPONENT_H
18 
19 #if !defined(IMPLEMENT_MANAGER)
20 #include <3d/namespace.h>
21 #include <base/math/vector.h>
22 #include <base/util/formats.h>
23 #include <core/ecs/component_struct_macros.h>
24 #include <core/ecs/entity_reference.h>
25 #include <core/ecs/intf_component_manager.h>
26 #include <render/device/gpu_resource_desc.h>
27 
28 CORE3D_BEGIN_NAMESPACE()
29 /** \addtogroup group_mesh_meshdesc
30  *  @{
31  */
32 #endif
33 
34 /** Mesh component represents a mesh which can be e.g. rendered by attaching the owning entity to RenderMeshComponents.
35  * GPU buffers are attached via EntityReferences. These refer to entities which are expected to have a
36  * RenderHandleComponent pointing to the actual resource.
37  */
38 BEGIN_COMPONENT(IMeshComponentManager, MeshComponent)
39 #if !defined(IMPLEMENT_MANAGER)
40     /** Submesh descriptor */
41     struct Submesh {
42         /** Submesh flag bits */
43         enum FlagBits {
44             /** Defines whether to use tangents with this submesh. */
45             TANGENTS_BIT = (1 << 0),
46             /** Defines whether to use vertex colors with this submesh. */
47             VERTEX_COLORS_BIT = (1 << 1),
48             /** Defines whether to use skinning with this submesh. */
49             SKIN_BIT = (1 << 2),
50             /** Defines whether the submesh has second texcoord set available. */
51             SECOND_TEXCOORD_BIT = (1 << 3),
52         };
53 
54         /** Container for submesh flag bits */
55         using Flags = uint32_t;
56 
57         /** Buffer indices from which default material shaders fetch data. */
58         enum DefaultMaterialBufferIndices {
59             /** Vertex positions. */
60             DM_VB_POS = 0,
61             /** Vertex normals. */
62             DM_VB_NOR,
63             /** First UV coordinates. */
64             DM_VB_UV0,
65             /** Second UV coordinates. */
66             DM_VB_UV1,
67             /** Vertex tangents. */
68             DM_VB_TAN,
69             /** Joint indices. */
70             DM_VB_JOI,
71             /** Joint weights. */
72             DM_VB_JOW,
73             /** Vertex colors. */
74             DM_VB_COL,
75         };
76 
77         /** Maximum number of data buffers.
78          * Special buffers (index and indirect args) are still separated for easier access.
79          */
80         static constexpr uint32_t BUFFER_COUNT = 8;
81 
82         /** Default value to access whole buffer byte size */
83         static constexpr uint32_t MAX_BUFFER_ACCESS_BYTE_SIZE = 0xFFFFffff;
84 
85         /** Default render sort layer id */
86         static constexpr uint32_t DEFAULT_RENDER_SORT_LAYER_ID = 32u;
87 
88         /** Buffer access */
89         struct BufferAccess {
90             /** The buffer */
91             CORE_NS::EntityReference buffer;
92             /** Byte offset to buffer data */
93             uint32_t offset { 0 };
94             /** Byte size to be used */
95             uint32_t byteSize { MAX_BUFFER_ACCESS_BYTE_SIZE };
96         };
97         /** Index buffer access */
98         struct IndexBufferAccess {
99             /** The buffer */
100             CORE_NS::EntityReference buffer;
101             /** Byte offset to buffer data */
102             uint32_t offset { 0 };
103             /** Byte size to be used */
104             uint32_t byteSize { MAX_BUFFER_ACCESS_BYTE_SIZE };
105             /** Enumeration for index type (uint16, uint32) */
106             RENDER_NS::IndexType indexType { RENDER_NS::IndexType::CORE_INDEX_TYPE_UINT32 };
107         };
108 
109         /** Instance count */
110         uint32_t instanceCount { 1u };
111 
112         /** Vertex data buffers */
113         BufferAccess bufferAccess[BUFFER_COUNT];
114         /** Index buffer */
115         IndexBufferAccess indexBuffer;
116         /** Indirect args buffer */
117         BufferAccess indirectArgsBuffer;
118         /** Morph target buffer */
119         BufferAccess morphTargetBuffer;
120 
121         /** Vertex count */
122         uint32_t vertexCount { 0 };
123         /** Index count */
124         uint32_t indexCount { 0 };
125         /** Morph target count */
126         uint32_t morphTargetCount { 0 };
127 
128         /** AABB min */
129         BASE_NS::Math::Vec3 aabbMin { 0.0f, 0.0f, 0.0f };
130         /** AABB max */
131         BASE_NS::Math::Vec3 aabbMax { 0.0f, 0.0f, 0.0f };
132 
133         /** Material to be used with this submesh. */
134         CORE_NS::Entity material {};
135 
136         /** Material to be used with this submesh. */
137         BASE_NS::vector<CORE_NS::Entity> additionalMaterials;
138 
139         /** Submesh flags that define the shader variant to be used with this submesh. */
140         Flags flags { 0 };
141 
142         /** Render sort layer. Within a render slot a layer can define a sort layer order.
143          * There are 0-63 values available. Default id value is 32.
144          * 0 first, 63 last
145          * 1. Typical use case is to set render sort layer to objects which render with depth test without depth write.
146          * 2. Typical use case is to always render character and/or camera object first to cull large parts of the view.
147          * 3. Sort e.g. plane layers.
148          */
149         /** Sort layer used sorting submeshes in rendering in render slots. Valid ID values 0 - 63. */
150         uint8_t renderSortLayer { DEFAULT_RENDER_SORT_LAYER_ID };
151         /** Sort layer order to describe fine order within sort layer. Valid order 0 - 255 */
152         uint8_t renderSortLayerOrder { 0u };
153     };
154 #endif
155 
156     /** Submeshes */
157     DEFINE_PROPERTY(BASE_NS::vector<Submesh>, submeshes, "Submeshes", 0, )
158 
159     /** Joint bounds */
160     DEFINE_PROPERTY(BASE_NS::vector<float>, jointBounds, "Joint Bounds", 0, )
161 
162     /** AABB min */
163     DEFINE_PROPERTY(BASE_NS::Math::Vec3, aabbMin, "Min AABB", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f))
164 
165     /** AABB max */
166     DEFINE_PROPERTY(BASE_NS::Math::Vec3, aabbMax, "Max AABB", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f))
167 END_COMPONENT(IMeshComponentManager, MeshComponent, "cd08dae1-d13b-4a4a-a317-56acbb332f76")
168 #if !defined(IMPLEMENT_MANAGER)
169 CORE3D_END_NAMESPACE()
170 #endif
171 #endif
172