1 /* 2 * Copyright (c) 2022 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 #include <render/device/pipeline_state_desc.h> 28 29 CORE3D_BEGIN_NAMESPACE() 30 /** \addtogroup group_mesh_meshdesc 31 * @{ 32 */ 33 #endif 34 35 /** Mesh component represents a mesh which can be e.g. rendered by attaching the owning entity to RenderMeshComponents. 36 * GPU buffers are attached via EntityReferences. These refer to entities which are expected to have a 37 * RenderHandleComponent pointing to the actual resource. 38 */ 39 BEGIN_COMPONENT(IMeshComponentManager, MeshComponent) 40 #if !defined(IMPLEMENT_MANAGER) 41 /** Submesh descriptor */ 42 struct Submesh { 43 /** Submesh flag bits */ 44 enum FlagBits { 45 /** Defines whether to use tangents with this submesh. */ 46 TANGENTS_BIT = (1 << 0), 47 /** Defines whether to use vertex colors with this submesh. */ 48 VERTEX_COLORS_BIT = (1 << 1), 49 /** Defines whether to use skinning with this submesh. */ 50 SKIN_BIT = (1 << 2), 51 /** Defines whether the submesh has second texcoord set available. */ 52 SECOND_TEXCOORD_BIT = (1 << 3), 53 }; 54 55 /** Container for submesh flag bits */ 56 using Flags = uint32_t; 57 58 /** Buffer indices from which default material shaders fetch data. */ 59 enum DefaultMaterialBufferIndices { 60 /** Vertex positions. */ 61 DM_VB_POS = 0, 62 /** Vertex normals. */ 63 DM_VB_NOR, 64 /** First UV coordinates. */ 65 DM_VB_UV0, 66 /** Second UV coordinates. */ 67 DM_VB_UV1, 68 /** Vertex tangents. */ 69 DM_VB_TAN, 70 /** Joint indices. */ 71 DM_VB_JOI, 72 /** Joint weights. */ 73 DM_VB_JOW, 74 /** Vertex colors. */ 75 DM_VB_COL, 76 }; 77 78 /** Maximum number of data buffers. 79 * Special buffers (index and indirect args) are still separated for easier access. 80 */ 81 static constexpr uint32_t BUFFER_COUNT = 8; 82 83 /** Default value to access whole buffer byte size */ 84 static constexpr uint32_t MAX_BUFFER_ACCESS_BYTE_SIZE = 0xFFFFffff; 85 86 /** Default render sort layer id */ 87 static constexpr uint32_t DEFAULT_RENDER_SORT_LAYER_ID = 32u; 88 89 /** Buffer access */ 90 struct BufferAccess { 91 /** The buffer */ 92 CORE_NS::EntityReference buffer; 93 /** Byte offset to buffer data */ 94 uint32_t offset { 0 }; 95 /** Byte size to be used */ 96 uint32_t byteSize { MAX_BUFFER_ACCESS_BYTE_SIZE }; 97 }; 98 /** Index buffer access */ 99 struct IndexBufferAccess { 100 /** The buffer */ 101 CORE_NS::EntityReference buffer; 102 /** Byte offset to buffer data */ 103 uint32_t offset { 0 }; 104 /** Byte size to be used */ 105 uint32_t byteSize { MAX_BUFFER_ACCESS_BYTE_SIZE }; 106 /** Enumeration for index type (uint16, uint32) */ 107 RENDER_NS::IndexType indexType { RENDER_NS::IndexType::CORE_INDEX_TYPE_UINT32 }; 108 }; 109 110 /** Instance count */ 111 uint32_t instanceCount { 1U }; 112 113 /** Vertex data buffers */ 114 BufferAccess bufferAccess[BUFFER_COUNT]; 115 /** Index buffer */ 116 IndexBufferAccess indexBuffer; 117 /** Indirect args buffer */ 118 BufferAccess indirectArgsBuffer; 119 /** Morph target buffer */ 120 BufferAccess morphTargetBuffer; 121 122 /** Vertex count */ 123 uint32_t vertexCount { 0U }; 124 /** Index count */ 125 uint32_t indexCount { 0U }; 126 /** Morph target count */ 127 uint32_t morphTargetCount { 0U }; 128 129 /** Indirect draw count (default to one, as with normal instance count) */ 130 uint32_t drawCountIndirect { 1U }; 131 /** Indirect draw stride */ 132 uint32_t strideIndirect { 0U }; 133 134 /** First index in draw */ 135 uint32_t firstIndex { 0U }; 136 /** First vertex offset in draw */ 137 uint32_t vertexOffset { 0U }; 138 /** First instance in draw */ 139 uint32_t firstInstance { 0U }; 140 141 /** AABB min */ 142 BASE_NS::Math::Vec3 aabbMin { 0.0f, 0.0f, 0.0f }; 143 /** AABB max */ 144 BASE_NS::Math::Vec3 aabbMax { 0.0f, 0.0f, 0.0f }; 145 146 /** Material to be used with this submesh. */ 147 CORE_NS::Entity material {}; 148 149 /** Material to be used with this submesh. */ 150 BASE_NS::vector<CORE_NS::Entity> additionalMaterials; 151 152 /** Submesh flags that define the shader variant to be used with this submesh. */ 153 Flags flags { 0 }; 154 155 /** Render sort layer. Within a render slot a layer can define a sort layer order. 156 * There are 0-63 values available. Default id value is 32. 157 * 0 first, 63 last 158 * 1. Typical use case is to set render sort layer to objects which render with depth test without depth write. 159 * 2. Typical use case is to always render character and/or camera object first to cull large parts of the view. 160 * 3. Sort e.g. plane layers. 161 */ 162 /** Sort layer used sorting submeshes in rendering in render slots. Valid ID values 0 - 63. */ 163 uint8_t renderSortLayer { DEFAULT_RENDER_SORT_LAYER_ID }; 164 /** Sort layer order to describe fine order within sort layer. Valid order 0 - 255 */ 165 uint8_t renderSortLayerOrder { 0u }; 166 167 /** Optional input assembly. If not set the input assembly is used from the graphics state usually defined with 168 * the shader or separate graphics state. 169 */ 170 RENDER_NS::GraphicsState::InputAssembly inputAssembly { false, 171 RENDER_NS::PrimitiveTopology::CORE_PRIMITIVE_TOPOLOGY_MAX_ENUM }; 172 }; 173 #endif 174 175 /** Submeshes */ 176 DEFINE_PROPERTY(BASE_NS::vector<Submesh>, submeshes, "Submeshes", 0, ) 177 178 /** Joint bounds */ 179 DEFINE_PROPERTY(BASE_NS::vector<float>, jointBounds, "Joint Bounds", 0, ) 180 181 /** AABB min */ 182 DEFINE_PROPERTY(BASE_NS::Math::Vec3, aabbMin, "Min AABB", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f)) 183 184 /** AABB max */ 185 DEFINE_PROPERTY(BASE_NS::Math::Vec3, aabbMax, "Max AABB", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f)) 186 END_COMPONENT(IMeshComponentManager, MeshComponent, "cd08dae1-d13b-4a4a-a317-56acbb332f76") 187 #if !defined(IMPLEMENT_MANAGER) 188 CORE3D_END_NAMESPACE() 189 #endif 190 #endif 191