1 /* 2 * Copyright (C) 2023 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 #ifndef API_RENDER_DEVICE_GPU_RESOURCE_DESC_H 17 #define API_RENDER_DEVICE_GPU_RESOURCE_DESC_H 18 19 #include <cstdint> 20 21 #include <base/util/formats.h> 22 #include <render/device/pipeline_layout_desc.h> 23 #include <render/device/pipeline_state_desc.h> 24 #include <render/namespace.h> 25 26 RENDER_BEGIN_NAMESPACE() 27 /** Image type */ 28 enum ImageType { 29 /** 1D */ 30 CORE_IMAGE_TYPE_1D = 0, 31 /** 2D */ 32 CORE_IMAGE_TYPE_2D = 1, 33 /** 3D */ 34 CORE_IMAGE_TYPE_3D = 2, 35 /** Max enumeration */ 36 CORE_IMAGE_TYPE_MAX_ENUM = 0x7FFFFFFF 37 }; 38 39 /** Image view type */ 40 enum ImageViewType { 41 /** 1D */ 42 CORE_IMAGE_VIEW_TYPE_1D = 0, 43 /** 2D */ 44 CORE_IMAGE_VIEW_TYPE_2D = 1, 45 /** 3D */ 46 CORE_IMAGE_VIEW_TYPE_3D = 2, 47 /** Cube */ 48 CORE_IMAGE_VIEW_TYPE_CUBE = 3, 49 /** 1D array */ 50 CORE_IMAGE_VIEW_TYPE_1D_ARRAY = 4, 51 /** 2D array */ 52 CORE_IMAGE_VIEW_TYPE_2D_ARRAY = 5, 53 /** Cube array */ 54 CORE_IMAGE_VIEW_TYPE_CUBE_ARRAY = 6, 55 /** Max enumeration */ 56 CORE_IMAGE_VIEW_TYPE_MAX_ENUM = 0x7FFFFFFF 57 }; 58 59 /** Image tiling */ 60 enum ImageTiling { 61 /** Optimal */ 62 CORE_IMAGE_TILING_OPTIMAL = 0, 63 /** Linear */ 64 CORE_IMAGE_TILING_LINEAR = 1, 65 /** Max enumeration */ 66 CORE_IMAGE_TILING_MAX_ENUM = 0x7FFFFFFF 67 }; 68 69 /** Image usage flag bits */ 70 enum ImageUsageFlagBits { 71 /** Transfer source bit */ 72 CORE_IMAGE_USAGE_TRANSFER_SRC_BIT = 0x00000001, 73 /** Transfer destination bit */ 74 CORE_IMAGE_USAGE_TRANSFER_DST_BIT = 0x00000002, 75 /** Sampled bit */ 76 CORE_IMAGE_USAGE_SAMPLED_BIT = 0x00000004, 77 /** Storage bit */ 78 CORE_IMAGE_USAGE_STORAGE_BIT = 0x00000008, 79 /** Color attachment bit */ 80 CORE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT = 0x00000010, 81 /** Depth stencil attachment bit */ 82 CORE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT = 0x00000020, 83 /** Transient attachment bit */ 84 CORE_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT = 0x00000040, 85 /** Input attachment bit */ 86 CORE_IMAGE_USAGE_INPUT_ATTACHMENT_BIT = 0x00000080, 87 }; 88 /** Container for image usage flag bits */ 89 using ImageUsageFlags = uint32_t; 90 91 /** Engine image creation flag bits */ 92 enum EngineImageCreationFlagBits { 93 /** Dynamic barriers */ 94 CORE_ENGINE_IMAGE_CREATION_DYNAMIC_BARRIERS = 0x00000001, 95 /** Reset state on frame borders */ 96 CORE_ENGINE_IMAGE_CREATION_RESET_STATE_ON_FRAME_BORDERS = 0x00000002, 97 /** Generate mips */ 98 CORE_ENGINE_IMAGE_CREATION_GENERATE_MIPS = 0x00000004, 99 /** Scale image when created from data */ 100 CORE_ENGINE_IMAGE_CREATION_SCALE = 0x00000008, 101 /** Destroy is deferred to the end of the current frame */ 102 CORE_ENGINE_IMAGE_CREATION_DEFERRED_DESTROY = 0x00000010, 103 }; 104 /** Container for engine image creation flag bits */ 105 using EngineImageCreationFlags = uint32_t; 106 107 /** Engine sampler creation flag bits */ 108 enum EngineSamplerCreationFlagBits { 109 /** Create ycbcr sampler */ 110 CORE_ENGINE_SAMPLER_CREATION_YCBCR = 0x00000001, 111 }; 112 /** Container for engine sampler creation flag bits */ 113 using EngineSamplerCreationFlags = uint32_t; 114 115 /** Image create flag bits */ 116 enum ImageCreateFlagBits { 117 /** Cube compatible bit */ 118 CORE_IMAGE_CREATE_CUBE_COMPATIBLE_BIT = 0x00000010, 119 /** 2D array compatible bit */ 120 CORE_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT = 0x00000020, 121 }; 122 /** Container for image create flag bits */ 123 using ImageCreateFlags = uint32_t; 124 125 /** Engine buffer creation flag bits */ 126 enum EngineBufferCreationFlagBits { 127 /** Dynamic barriers */ 128 CORE_ENGINE_BUFFER_CREATION_DYNAMIC_BARRIERS = 0x00000001, 129 /** Dynamic ring buffer */ 130 CORE_ENGINE_BUFFER_CREATION_DYNAMIC_RING_BUFFER = 0x00000002, 131 /** Single shot staging */ 132 CORE_ENGINE_BUFFER_CREATION_SINGLE_SHOT_STAGING = 0x00000004, 133 /** Hint to enable memory optimizations if possible (e.g. on integrated might use direct copies without staging) */ 134 CORE_ENGINE_BUFFER_CREATION_ENABLE_MEMORY_OPTIMIZATIONS = 0x00000008, 135 /** Immediately created GPU resource */ 136 CORE_ENGINE_BUFFER_CREATION_CREATE_IMMEDIATE = 0x00000010, 137 /** Destroy is deferred to the end of the current frame */ 138 CORE_ENGINE_BUFFER_CREATION_DEFERRED_DESTROY = 0x00000020, 139 /** Buffer is mappable outside renderer (i.e. outside render nodes). Slower, do not used if not necessary. */ 140 CORE_ENGINE_BUFFER_CREATION_MAP_OUTSIDE_RENDERER = 0x00000040, 141 }; 142 /** Container for engine buffer creation flag bits */ 143 using EngineBufferCreationFlags = uint32_t; 144 145 /** Buffer usage flag bits */ 146 enum BufferUsageFlagBits { 147 /** Transfer source bit */ 148 CORE_BUFFER_USAGE_TRANSFER_SRC_BIT = 0x00000001, 149 /** Transfer destination bit */ 150 CORE_BUFFER_USAGE_TRANSFER_DST_BIT = 0x00000002, 151 /** Uniform texel buffer bit */ 152 CORE_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT = 0x00000004, 153 /** Storage texel buffer bit */ 154 CORE_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT = 0x00000008, 155 /** Uniform buffer bit */ 156 CORE_BUFFER_USAGE_UNIFORM_BUFFER_BIT = 0x00000010, 157 /** Storage buffer bit */ 158 CORE_BUFFER_USAGE_STORAGE_BUFFER_BIT = 0x00000020, 159 /** Index buffer bit */ 160 CORE_BUFFER_USAGE_INDEX_BUFFER_BIT = 0x00000040, 161 /** Vertex buffer bit */ 162 CORE_BUFFER_USAGE_VERTEX_BUFFER_BIT = 0x00000080, 163 /** Indirect buffer bit */ 164 CORE_BUFFER_USAGE_INDIRECT_BUFFER_BIT = 0x00000100, 165 /** Acceleration structure binding table bit */ 166 CORE_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT = 0x00000400, 167 /** Shader device address bit */ 168 CORE_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT = 0x00020000, 169 /** Acceleration structure build input read only bit */ 170 CORE_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT = 0x00080000, 171 /** Acceleration structure storage bit */ 172 CORE_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT = 0x00100000, 173 }; 174 /** Container for buffer usage flag bits */ 175 using BufferUsageFlags = uint32_t; 176 177 /** Filter */ 178 enum Filter { 179 /** Nearest */ 180 CORE_FILTER_NEAREST = 0, 181 /** Linear */ 182 CORE_FILTER_LINEAR = 1, 183 }; 184 185 /** Sampler address mode */ 186 enum SamplerAddressMode { 187 /** Repeat */ 188 CORE_SAMPLER_ADDRESS_MODE_REPEAT = 0, 189 /** Mirrored repeat */ 190 CORE_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT = 1, 191 /** Clamp to edge */ 192 CORE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE = 2, 193 /** Clamp to border */ 194 CORE_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER = 3, 195 /** Mirror clamp to edge */ 196 CORE_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE = 4, 197 }; 198 199 /** Border color */ 200 enum BorderColor { 201 /** Float transparent black */ 202 CORE_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK = 0, 203 /** Int transparent black */ 204 CORE_BORDER_COLOR_INT_TRANSPARENT_BLACK = 1, 205 /** Float opaque black */ 206 CORE_BORDER_COLOR_FLOAT_OPAQUE_BLACK = 2, 207 /** Int opaque black */ 208 CORE_BORDER_COLOR_INT_OPAQUE_BLACK = 3, 209 /** Float opaque white */ 210 CORE_BORDER_COLOR_FLOAT_OPAQUE_WHITE = 4, 211 /** Int opaque white */ 212 CORE_BORDER_COLOR_INT_OPAQUE_WHITE = 5, 213 /** Max enumeration */ 214 CORE_BORDER_COLOR_MAX_ENUM = 0x7FFFFFFF 215 }; 216 217 /** Sample count flag bits */ 218 enum SampleCountFlagBits : uint32_t { 219 /** 1 bit */ 220 CORE_SAMPLE_COUNT_1_BIT = 0x00000001, 221 /** 2 bit */ 222 CORE_SAMPLE_COUNT_2_BIT = 0x00000002, 223 /** 4 bit */ 224 CORE_SAMPLE_COUNT_4_BIT = 0x00000004, 225 /** 8 bit */ 226 CORE_SAMPLE_COUNT_8_BIT = 0x00000008, 227 /** 16 bit */ 228 CORE_SAMPLE_COUNT_16_BIT = 0x00000010, 229 /** 32 bit */ 230 CORE_SAMPLE_COUNT_32_BIT = 0x00000020, 231 /** 64 bit */ 232 CORE_SAMPLE_COUNT_64_BIT = 0x00000040, 233 }; 234 /** Sample count flags */ 235 using SampleCountFlags = uint32_t; 236 237 /** Component swizzle */ 238 enum ComponentSwizzle : uint32_t { 239 /** Swizzle identity */ 240 CORE_COMPONENT_SWIZZLE_IDENTITY = 0, 241 /** Swizzle zero */ 242 CORE_COMPONENT_SWIZZLE_ZERO = 1, 243 /** Swizzle one */ 244 CORE_COMPONENT_SWIZZLE_ONE = 2, 245 /** Swizzle red */ 246 CORE_COMPONENT_SWIZZLE_R = 3, 247 /** Swizzle green */ 248 CORE_COMPONENT_SWIZZLE_G = 4, 249 /** Swizzle blue */ 250 CORE_COMPONENT_SWIZZLE_B = 5, 251 /** Swizzle alpha */ 252 CORE_COMPONENT_SWIZZLE_A = 6, 253 /** Max enumeration */ 254 CORE_COMPONENT_SWIZZLE_MAX_ENUM = 0x7FFFFFFF 255 }; 256 257 /** Acceleration structure create flag bits */ 258 enum AccelerationStructureCreateFlagBits {}; 259 /** Container for acceleration create flag bits */ 260 using AccelerationStructureCreateFlags = uint32_t; 261 262 /** Engine acceleration structure creation flag bits */ 263 enum EngineAccelerationStructureCreationFlagBits {}; 264 /** Container for engine acceleration structure creation flag bits */ 265 using EngineAccelerationStructureCreationFlags = uint32_t; 266 267 /** 268 * UBO: 269 * - BufferUsageFlags: 270 * CORE_BUFFER_USAGE_UNIFORM_BUFFER_BIT 271 * - EngineBufferCreationFlags 272 * CORE_ENGINE_BUFFER_CREATION_DYNAMIC_RING_BUFFER 273 * NOTE: automatically buffered with map-method (ring buffer hidden inside) (the binding offset is updated with 274 * UpdateDescriptorSets) 275 * - MemoryPropertyFlags 276 * CORE_MEMORY_PROPERTY_HOST_VISIBLE_BIT | CORE_MEMORY_PROPERTY_HOST_COHERENT_BIT 277 * 278 * VBO: 279 * - BufferUsageFlags: 280 * CORE_BUFFER_USAGE_VERTEX_BUFFER_BIT | CORE_BUFFER_USAGE_TRANSFER_DST_BIT 281 * - MemoryPropertyFlags: 282 * CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT 283 * 284 * SSBO: 285 * - BufferUsageFlags: 286 * CORE_BUFFER_USAGE_STORAGE_BUFFER_BIT 287 * MemoryPropertyFlags 288 * CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT 289 * 290 * EngineBufferCreationFlags: 291 * 292 * CORE_ENGINE_BUFFER_CREATION_DYNAMIC_BARRIERS, set this flag only for dynamic state changing data (e.g. SSBOs etc) 293 * This flag is not needed for regular vertex buffers. Staging handles the copies and barriers automatically. 294 * 295 * CORE_ENGINE_BUFFER_CREATION_DYNAMIC_RING_BUFFER, set this for dynamic buffer mapping 296 * Dynamic ring buffer updates offsets automatically when buffer is mapped and descriptor set is updated. 297 * 298 * CORE_ENGINE_BUFFER_CREATION_SINGLE_SHOT_STAGING, tells memory allocator that this resource can be forget after this 299 * frame Engine internally flags staging buffers with this flag, Do not use if you cannot guarentee that the gpu buffer 300 * is destroyed immediately 301 * 302 * CORE_ENGINE_BUFFER_CREATION_ENABLE_MEMORY_OPTIMIZATIONS, can enable some memory optimizations. 303 * E.g. on some platforms all memory can be host visible (except lazy allocations). 304 * With this flag you can make sure that your CORE_BUFFER_USAGE_TRANSFER_DST_BIT buffer 305 * Might by-pass staging buffers and enable direct cpu->gpu memcpy on some platforms and use less memory. 306 * 307 * CORE_ENGINE_BUFFER_CREATION_CREATE_IMMEDIATE, creates the resource immediately/instantly so it can be mapped. 308 * Should only be used when instant mapping is required. 309 * 310 * CORE_ENGINE_BUFFER_CREATION_DEFERRED_DESTROY, the resource is destroyed at the end of the frame 311 * after Destroy has been called to the GpuResourceHandle. 312 * Should only be used when object lifetime cannot be guaranteed in some scoped section. 313 * Might increase memory consumption. 314 * 315 * CORE_ENGINE_BUFFER_CREATION_MAP_OUTSIDE_RENDERER, the resource is mappable outside 316 * renderer. Normal mapping is suggested to be done within render nodes or with default staging memory copy. 317 * 318 */ 319 /** \addtogroup group_gpuresourcedesc 320 * @{ 321 */ 322 /** GPU buffer descriptor */ 323 struct GpuBufferDesc { 324 /** Usage flags */ 325 BufferUsageFlags usageFlags { 0 }; 326 /** Memory property flags */ 327 MemoryPropertyFlags memoryPropertyFlags { 0 }; 328 /** Engine creation flags */ 329 EngineBufferCreationFlags engineCreationFlags { 0 }; 330 /** Byte size */ 331 uint32_t byteSize { 0 }; 332 /** View format (for texel buffer view) */ 333 BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED }; 334 }; 335 336 /** RGBA component mapping */ 337 struct ComponentMapping { 338 /* Swizzle for red */ 339 ComponentSwizzle r { CORE_COMPONENT_SWIZZLE_IDENTITY }; 340 /* Swizzle for green */ 341 ComponentSwizzle g { CORE_COMPONENT_SWIZZLE_IDENTITY }; 342 /* Swizzle for blue */ 343 ComponentSwizzle b { CORE_COMPONENT_SWIZZLE_IDENTITY }; 344 /* Swizzle for alpha */ 345 ComponentSwizzle a { CORE_COMPONENT_SWIZZLE_IDENTITY }; 346 }; 347 348 /** 349 * EngineImageCreationFlags: 350 * 351 * CORE_ENGINE_IMAGE_CREATION_DYNAMIC_BARRIERS, set this flag only for dynamic state changing data (e.g. render targets) 352 * default value (zero, 0), is usually always the correct one (e.g. loaded textures, sampled textures) 353 * 354 * CORE_ENGINE_IMAGE_CREATION_SCALE, set this if a simple image scaling is needed when staging the 355 * Images to the GPU. Linear single pass scaling is used. 356 * Final image size is taken from the GpuImageDesc, 357 * the input size is taken from the IImageContainer or BufferImageCopies which are given to Create -method. 358 */ 359 /** \addtogroup group_gpuresourcedesc 360 * @{ 361 */ 362 /** GPU image descriptor */ 363 struct GpuImageDesc { 364 /** Image type */ 365 ImageType imageType { ImageType::CORE_IMAGE_TYPE_MAX_ENUM }; 366 /** Image view type */ 367 ImageViewType imageViewType { ImageViewType::CORE_IMAGE_VIEW_TYPE_MAX_ENUM }; 368 /** Format */ 369 BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED }; 370 /** Image tiling */ 371 ImageTiling imageTiling { ImageTiling::CORE_IMAGE_TILING_MAX_ENUM }; 372 /** Usage tiling */ 373 ImageUsageFlags usageFlags { 0 }; 374 /** Memory property flags */ 375 MemoryPropertyFlags memoryPropertyFlags { 0 }; 376 377 /** Create flags */ 378 ImageCreateFlags createFlags { 0 }; 379 380 /** Engine creation flags */ 381 EngineImageCreationFlags engineCreationFlags { 0 }; 382 383 /** Width */ 384 uint32_t width { 0 }; 385 /** Height */ 386 uint32_t height { 0 }; 387 /** Depth */ 388 uint32_t depth { 0 }; 389 390 /** Mip count */ 391 uint32_t mipCount { 1 }; 392 /** Layer count */ 393 uint32_t layerCount { 1 }; 394 395 /** Sample count flags */ 396 SampleCountFlags sampleCountFlags { SampleCountFlagBits::CORE_SAMPLE_COUNT_1_BIT }; 397 398 /** RGBA component mapping */ 399 ComponentMapping componentMapping; 400 }; 401 402 /** GPU sampler descriptor */ 403 struct GpuSamplerDesc { 404 /** Magnification filter */ 405 Filter magFilter { Filter::CORE_FILTER_NEAREST }; 406 /** Minification filter */ 407 Filter minFilter { Filter::CORE_FILTER_NEAREST }; 408 /** Mip map filter mode */ 409 Filter mipMapMode { Filter::CORE_FILTER_NEAREST }; 410 411 /** Address mode U */ 412 SamplerAddressMode addressModeU { SamplerAddressMode::CORE_SAMPLER_ADDRESS_MODE_REPEAT }; 413 /** Address mode V */ 414 SamplerAddressMode addressModeV { SamplerAddressMode::CORE_SAMPLER_ADDRESS_MODE_REPEAT }; 415 /** Address mode W */ 416 SamplerAddressMode addressModeW { SamplerAddressMode::CORE_SAMPLER_ADDRESS_MODE_REPEAT }; 417 418 /** Engine creation flags */ 419 EngineSamplerCreationFlags engineCreationFlags { 0 }; 420 421 /** Mip lod bias */ 422 float mipLodBias { 0.0f }; 423 424 /** Enable anisotropy */ 425 bool enableAnisotropy { false }; 426 /** Max anisotropy */ 427 float maxAnisotropy { 1.0f }; 428 429 /** Enable compare operation */ 430 bool enableCompareOp { false }; 431 /** Compare operation */ 432 CompareOp compareOp { CompareOp::CORE_COMPARE_OP_NEVER }; 433 434 /** Minimum lod */ 435 float minLod { 0.0f }; 436 /** Maximum lod */ 437 float maxLod { 0.0f }; 438 439 /** Border color */ 440 BorderColor borderColor { BorderColor::CORE_BORDER_COLOR_FLOAT_OPAQUE_BLACK }; 441 442 /** Enable unnormalized coordinates */ 443 bool enableUnnormalizedCoordinates { false }; 444 }; 445 446 /** GPU acceleration structure descriptor */ 447 struct GpuAccelerationStructureDesc { 448 /* Acceleration structure type */ 449 AccelerationStructureType accelerationStructureType { 450 AccelerationStructureType::CORE_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL 451 }; 452 /* Buffer desc which holds the buffer info */ 453 GpuBufferDesc bufferDesc; 454 }; 455 /** @} */ 456 RENDER_END_NAMESPACE() 457 458 #endif // API_RENDER_DEVICE_GPU_RESOURCE_DESC_H 459