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 #ifndef API_RENDER_DEVICE_IGPU_RESOURCE_MANAGER_H 17 #define API_RENDER_DEVICE_IGPU_RESOURCE_MANAGER_H 18 19 #include <cstdint> 20 21 #include <base/containers/array_view.h> 22 #include <base/containers/string.h> 23 #include <base/containers/string_view.h> 24 #include <base/util/color.h> 25 #include <core/image/intf_image_container.h> 26 #include <render/device/gpu_resource_desc.h> 27 #include <render/device/pipeline_state_desc.h> 28 #include <render/namespace.h> 29 #include <render/resource_handle.h> 30 31 RENDER_BEGIN_NAMESPACE() 32 /** \addtogroup group_igpuresourcemanager 33 * @{ 34 */ 35 /** Backend specific image descriptor */ 36 struct BackendSpecificImageDesc {}; 37 38 /** Backend specific buffer descriptor */ 39 struct BackendSpecificBufferDesc {}; 40 41 class IGpuResourceCache; 42 43 /** Gpu resource manager. 44 * Internally synchronized. 45 * (except WaitForIdleAndDestroyGpuResources, MapBuffer, and UnmapBuffer are not internally synchronized) 46 * 47 * Handles waiting of gpu resource destruction internally. 48 * 49 * Create methods take a unique name (if named). 50 * Object is re-created if the name is already in use. 51 * Therefore do not use scoped handles if you're recreating them. 52 * Create method with a given GpuResourceHandle will replace the handle and return the same valid handle. 53 */ 54 class IGpuResourceManager { 55 public: 56 IGpuResourceManager(const IGpuResourceManager&) = delete; 57 IGpuResourceManager& operator=(const IGpuResourceManager&) = delete; 58 59 /** Get render handle reference of raw handle. Up-to-date with generation. 60 * @param handle Raw render handle 61 * @return Returns A up-to-date (with generation) render handle reference for the handle. 62 */ 63 virtual RenderHandleReference Get(const RenderHandle& handle) const = 0; 64 65 /** Get render handle of raw handle. Up-to-date with generation. 66 * @param handle Raw render handle 67 * @return Returns A up-to-date (with generation) render handle for the handle. 68 */ 69 virtual RenderHandle GetRawHandle(const RenderHandle& handle) const = 0; 70 71 /** Get or create a GpuBuffer with unique buffer name. 72 * Keeps the data locked. Can be used to create resource only if it's not created already with unique name / uri. 73 * @param name Name of buffer 74 * @param desc Descriptor 75 * @return Returns A valid resource handle to existing or new if the creation was successfull. 76 */ 77 virtual RenderHandleReference GetOrCreate(const BASE_NS::string_view name, const GpuBufferDesc& desc) = 0; 78 79 /** Create a GpuBuffer with unique buffer name. 80 * @param name Name of buffer 81 * @param desc Descriptor 82 * @return Returns A valid resource handle if the creation was successfull. 83 */ 84 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuBufferDesc& desc) = 0; 85 86 /** Create a GpuBuffer with unique buffer name and data. 87 * @param name Name of buffer 88 * @param desc Descriptor 89 * @param data Gpu buffer data 90 * @return Returns A valid resource handle if the creation was successfull. 91 */ 92 virtual RenderHandleReference Create( 93 const BASE_NS::string_view name, const GpuBufferDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 94 95 /** Create an unnamed GpuBuffer with data. 96 * @param desc Descriptor 97 * @param data Gpu buffer data 98 * @return Returns A valid resource handle if the creation was successfull. 99 */ 100 virtual RenderHandleReference Create(const GpuBufferDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 101 102 /** Create a new GPU resource for a given GpuResourceHandle. (Old handle and name (if given) are valid) 103 * @param replacedHandle A valid handle which current resource will be destroyed and replaced with a new one. 104 * @param desc Descriptor 105 * @return Returns the same handle that was given if the resource handle was valid. 106 */ 107 virtual RenderHandleReference Create(const RenderHandleReference& replacedHandle, const GpuBufferDesc& desc) = 0; 108 109 /** Create an unnamed GpuBuffer. 110 * @param desc Descriptor 111 * @return Returns A valid resource handle if the creation was successfull. 112 */ 113 virtual RenderHandleReference Create(const GpuBufferDesc& desc) = 0; 114 115 /** Get or create a GpuImage with unique image name. 116 * Keeps the data locked. Can be used to create resource only if it's not created already with unique name / uri. 117 * @param name Name of image 118 * @param desc Descriptor 119 * @return Returns A valid resource handle to existing or to a new if the creation was successfull. 120 */ 121 virtual RenderHandleReference GetOrCreate(const BASE_NS::string_view name, const GpuImageDesc& desc) = 0; 122 123 /** Create a GpuImage with unique image name. 124 * @param name Name of image 125 * @param desc Descriptor 126 * @return Returns A valid resource handle if the creation was successfull. 127 */ 128 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuImageDesc& desc) = 0; 129 130 /** Create a GpuImage with unique image name and data. 131 * @param name Name of image 132 * @param desc Descriptor 133 * @param data Gpu image data 134 * @return Returns A valid resource handle if the creation was successfull. 135 */ 136 virtual RenderHandleReference Create( 137 const BASE_NS::string_view name, const GpuImageDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 138 139 /** Create a GpuImage with unique image name and data with image copy description. 140 * @param name Name of image 141 * @param desc Descriptor 142 * @param data Gpu image data 143 * @param bufferImageCopies Array of buffer image copies 144 * @return Returns A valid resource handle if the creation was successfull. 145 */ 146 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuImageDesc& desc, 147 const BASE_NS::array_view<const uint8_t> data, 148 const BASE_NS::array_view<const BufferImageCopy> bufferImageCopies) = 0; 149 150 /** Create a new GPU resource for a given GpuResourceHandle. (Old handle and name (if given) are valid) 151 * @param replacedHandle A valid handle which current resource will be destroyed and replaced with a new one. 152 * @param desc Descriptor 153 * @return Returns the same handle that was given if the resource handle was valid. 154 */ 155 virtual RenderHandleReference Create(const RenderHandleReference& replacedHandle, const GpuImageDesc& desc) = 0; 156 157 /** Create an unnamed GpuImage. 158 * @param desc Descriptor 159 * @return Returns A valid resource handle if the creation was successfull. 160 */ 161 virtual RenderHandleReference Create(const GpuImageDesc& desc) = 0; 162 163 /** Create an unnamed GpuImage with data. 164 * @param desc Descriptor 165 * @param data Gpu image data 166 * @return Returns A valid resource handle if the creation was successfull. 167 */ 168 virtual RenderHandleReference Create(const GpuImageDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 169 170 /** Create an unnamed GpuImage with data and image copy description. 171 * @param desc Descriptor 172 * @param data Gpu image data 173 * @param bufferImageCopies Array of buffer image copies 174 * @return Returns A valid resource handle if the creation was successfull. 175 */ 176 virtual RenderHandleReference Create(const GpuImageDesc& desc, const BASE_NS::array_view<const uint8_t> data, 177 const BASE_NS::array_view<const BufferImageCopy> bufferImageCopies) = 0; 178 179 /** Create GpuImage with unique image name from IImageContainer::Ptr 180 * @param name Name of image 181 * @param desc Descriptor 182 * @param image Image container 183 * @return Returns A valid resource handle if the creation was successfull. 184 */ 185 virtual RenderHandleReference Create( 186 const BASE_NS::string_view name, const GpuImageDesc& desc, CORE_NS::IImageContainer::Ptr image) = 0; 187 188 /** Create Unnamed GpuImage from IImageContainer::Ptr 189 * @param desc Descriptor 190 * @param image Image container 191 * @return Returns A valid resource handle if the creation was successfull. 192 */ 193 virtual RenderHandleReference Create(const GpuImageDesc& desc, CORE_NS::IImageContainer::Ptr image) = 0; 194 195 /** Create a GpuImage with unique image name from external image resource 196 * NOTE: the external image resource is not owned and not deleted by the manager when the handle is destroyed 197 * (e.g. if using hw buffers the hw buffer reference is released) 198 * @param name Name of image 199 * @param desc Descriptor 200 * @param backendSpecificData Image description 201 * @return Returns A valid resource handle if the creation was successfull. 202 */ 203 virtual RenderHandleReference CreateView(const BASE_NS::string_view name, const GpuImageDesc& desc, 204 const BackendSpecificImageDesc& backendSpecificData) = 0; 205 206 /** Get or create a GpuSample with unique sampler name. 207 * Keeps the data locked. Can be used to create resource only if it's not created already with unique name / uri. 208 * @param name Name of image 209 * @param desc Descriptor 210 * @return Returns A valid resource handle to existing or to a new if the creation was successfull. 211 */ 212 virtual RenderHandleReference GetOrCreate(const BASE_NS::string_view name, const GpuSamplerDesc& desc) = 0; 213 214 /** Create a GpuSampler with unique image name. 215 * @param name Name of image 216 * @param desc Descriptor 217 * @return Returns A valid resource handle if the creation was successfull. 218 */ 219 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuSamplerDesc& desc) = 0; 220 221 /** Create a GpuSampler with replaced handle. 222 * @param replacedHandle Replaced handle 223 * @param desc Descriptor 224 * @return Returns A valid resource handle if the creation was successfull. 225 */ 226 virtual RenderHandleReference Create(const RenderHandleReference& replacedHandle, const GpuSamplerDesc& desc) = 0; 227 228 /** Create unnamed GpuSampler. 229 * @param desc Descriptor 230 * @return Returns A valid resource handle if the creation was successfull. 231 */ 232 virtual RenderHandleReference Create(const GpuSamplerDesc& desc) = 0; 233 234 /** Create unnamed GpuAccelerationStructure. A GPU buffer handle is returned with additional acceleration structure. 235 * @param desc Descriptor 236 * @return Returns A valid resource handle if the creation was successfull. 237 */ 238 virtual RenderHandleReference Create(const GpuAccelerationStructureDesc& desc) = 0; 239 240 /** Create uniquely named GpuAccelerationStructure. If name is found, the handle is replaced. 241 * A GPU buffer handle is returned with additional acceleration structure. 242 * @param name Unique name for the acceleration structure 243 * @param desc Descriptor 244 * @return Returns A valid resource handle if the creation was successfull. 245 */ 246 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuAccelerationStructureDesc& desc) = 0; 247 248 /** Create GpuAccelerationStructure and replace the given handle if it's valid (the same is returned in valid case). 249 * A GPU buffer handle is returned with additional acceleration structure. 250 * @param replacedHandle A valid handle which current resource will be destroyed and replaced with a new one. 251 * @param desc Descriptor 252 * @return Returns A valid resource handle if the creation was successfull. 253 */ 254 virtual RenderHandleReference Create( 255 const RenderHandleReference& replacedHandle, const GpuAccelerationStructureDesc& desc) = 0; 256 257 /** Get buffer handle. (Invalid handle if not found.) 258 * @param name Name of buffer 259 * @return Returns A valid resource handle if the named resource is available. 260 */ 261 virtual RenderHandleReference GetBufferHandle(const BASE_NS::string_view name) const = 0; 262 263 /** Get image handle. (Invalid handle if not found.) 264 * @param name Name of image 265 * @return Returns A valid resource handle if the named resource is available. 266 */ 267 virtual RenderHandleReference GetImageHandle(const BASE_NS::string_view name) const = 0; 268 269 /** Get sampler handle. (Invalid handle if not found.) 270 * @param name Name of sampler 271 * @return Returns A valid resource handle if the named resource is available. 272 */ 273 virtual RenderHandleReference GetSamplerHandle(const BASE_NS::string_view name) const = 0; 274 275 /** See if there is already a buffer with the name. The names are unique and often URI. 276 * @param name Name of the buffer (often URI). 277 * @return True if named buffer is found. 278 */ 279 virtual bool HasBuffer(BASE_NS::string_view name) const = 0; 280 281 /** See if there is already a image with the name. The names are unique and often URI. 282 * @param name Name of the image (often URI). 283 * @return True if named image is found. 284 */ 285 virtual bool HasImage(BASE_NS::string_view name) const = 0; 286 287 /** See if there is already a sampler with the name. The names are unique and often URI. 288 * @param name Name of the sampler (often URI). 289 * @return True if named sampler is found. 290 */ 291 virtual bool HasSampler(BASE_NS::string_view name) const = 0; 292 293 /** Get buffer descriptor 294 * @param handle Handle to resource 295 * @return Returns A GpuBufferDesc of a given GPU resource handle. 296 */ 297 virtual GpuBufferDesc GetBufferDescriptor(const RenderHandleReference& handle) const = 0; 298 299 /** Get image descriptor 300 * @param handle Handle to resource 301 * @return Returns A GpuImageDesc of a given GPU resource handle. 302 */ 303 virtual GpuImageDesc GetImageDescriptor(const RenderHandleReference& handle) const = 0; 304 305 /** Get sampler descriptor 306 * @param handle Handle to resource 307 * @return Returns A GpuSamplerDesc of a given GPU resource handle. 308 */ 309 virtual GpuSamplerDesc GetSamplerDescriptor(const RenderHandleReference& handle) const = 0; 310 311 /** Get acceleration structure descriptor 312 * @param handle Handle to resource 313 * @return Returns A GpuAccelerationStructureDesc of a given GPU resource handle. 314 */ 315 virtual GpuAccelerationStructureDesc GetAccelerationStructureDescriptor( 316 const RenderHandleReference& handle) const = 0; 317 318 /** Wait for Gpu to become idle and destroy all Gpu resources of destroyed handles. 319 * Not internally syncronized. Needs to be called from render thread where renderFrame is called. 320 * 321 * This is a hazardous method and should be called with extreme caution. 322 * In normal rendering situation RenderFrame() does the resource management automatically. 323 * This method is provided for forced destruction of Gpu resurces when RenderFrame() is not called. 324 * 325 * Do not call this method per frame. 326 */ 327 virtual void WaitForIdleAndDestroyGpuResources() = 0; 328 329 /** Map buffer memory. Always maps from the beginning of buffer. 330 * Preferrably use only for staging kind of data (e.g. fill data in another thread and then pass to staging). 331 * 1. Map once and get the pointer to memory 332 * 2. Write data to memory 333 * 3. Unmap and pass the handle to rendering/staging or whatever (with offset) 334 * The buffer needs to created with CORE_ENGINE_BUFFER_CREATION_MAP_OUTSIDE_RENDERER, 335 * CORE_ENGINE_BUFFER_CREATION_CREATE_IMMEDIATE and CORE_ENGINE_BUFFER_CREATION_DEFERRED_DESTROY and cannot be 336 * replaced with name or handle. 337 * @param handle Handle to resource 338 */ 339 virtual void* MapBufferMemory(const RenderHandleReference& handle) const = 0; 340 341 /** Unmap buffer 342 * @param handle Handle to resource 343 */ 344 virtual void UnmapBuffer(const RenderHandleReference& handle) const = 0; 345 346 /** Checks if resource is a GPU buffer */ 347 virtual bool IsGpuBuffer(const RenderHandleReference& handle) const = 0; 348 /** Checks if resource is a GPU image */ 349 virtual bool IsGpuImage(const RenderHandleReference& handle) const = 0; 350 /** Checks if resource is a GPU sampler */ 351 virtual bool IsGpuSampler(const RenderHandleReference& handle) const = 0; 352 /** Checks if resource is a GPU acceleration structure. If is GPU acceleration structure, it is GPU buffer as well. 353 */ 354 virtual bool IsGpuAccelerationStructure(const RenderHandleReference& handle) const = 0; 355 /** Image has been created to be used as a swapchain image. Fast check based on handle. 356 * Due to possible remapping with built-in CORE_DEFAULT_BACKBUFFER might not be an actual swapchain. 357 */ 358 virtual bool IsSwapchain(const RenderHandleReference& handle) const = 0; 359 360 /** Checks if resource is a client mappable */ 361 virtual bool IsMappableOutsideRender(const RenderHandleReference& handle) const = 0; 362 363 /** Returns supported flags for given resource handle format. */ 364 virtual FormatProperties GetFormatProperties(const RenderHandleReference& handle) const = 0; 365 /** Returns supported flags for given format. */ 366 virtual FormatProperties GetFormatProperties(const BASE_NS::Format format) const = 0; 367 368 /** Returns GpuImageDesc based on image container's ImageDesc. 369 * Conversion helper when loading images with image loaders. 370 * Sets automatically basic values which are needed for typical image usage: 371 * imageTiling = CORE_IMAGE_TILING_OPTIMAL 372 * usageFlags |= CORE_IMAGE_USAGE_SAMPLED_BIT | CORE_IMAGE_USAGE_TRANSFER_DST_BIT 373 * memoryPropertyFlags = CORE_MEMORY_PROPERTY_DEVICE_LOCAL_BIT 374 * if mips are requested adds mips related flags 375 * @param desc ImageDesc from IImageContainer. 376 * @return GpuImageDesc GPU image desc based on input. 377 */ 378 virtual GpuImageDesc CreateGpuImageDesc(const CORE_NS::IImageContainer::ImageDesc& desc) const = 0; 379 380 /** Returns name for the resource "" if no name found. 381 * NOTE: method should not be used during run-time on hot path. 382 * @param handle Handle of the resource. 383 * @return string Name of the resource. 384 */ 385 virtual BASE_NS::string GetName(const RenderHandleReference& handle) const = 0; 386 387 /** Returns all valid GPU buffer render handles. 388 * @return vector of handles. 389 */ 390 virtual BASE_NS::vector<RenderHandleReference> GetBufferHandles() const = 0; 391 /** Returns all valid GPU image render handles. 392 * @return vector of handles. 393 */ 394 virtual BASE_NS::vector<RenderHandleReference> GetImageHandles() const = 0; 395 /** Returns all valid GPU sampler render handles. 396 * @return vector of handles. 397 */ 398 virtual BASE_NS::vector<RenderHandleReference> GetSamplerHandles() const = 0; 399 400 /** Force default GPU buffer creation usage flags. Used as bitwise OR with given descs. 401 * NOTE: Reduced performance expected 402 * Use only in situtation when necessary and not in real products 403 * @param bufferUsageFlags Default buffer usage flags. With zero no flags (default) 404 */ 405 virtual void SetDefaultGpuBufferCreationFlags(const BufferUsageFlags usageFlags) = 0; 406 /** Force default GPU image creation usage flags. Used as bitwise OR with given descs. 407 * NOTE: Reduced performance expected 408 * Use only in situtation when necessary and not in real products 409 * @param bufferUsageFlags Default buffer usage flags. With zero no flags (default) 410 */ 411 virtual void SetDefaultGpuImageCreationFlags(const ImageUsageFlags usageFlags) = 0; 412 413 /** Get gpu resource cache. 414 * @return Reference to GPU resource cache interface. 415 */ 416 virtual IGpuResourceCache& GetGpuResourceCache() const = 0; 417 418 /** Get image aspect flags for a given resource handle format. 419 * @param handle Render handle reference. 420 * @return Image aspect flags for a given format. 421 */ 422 virtual ImageAspectFlags GetImageAspectFlags(const RenderHandleReference& handle) const = 0; 423 424 /** Get image aspect flags for a given format. 425 * @param format Format. 426 * @return Image aspect flags for a given format. 427 */ 428 virtual ImageAspectFlags GetImageAspectFlags(const BASE_NS::Format format) const = 0; 429 430 /** Get color space flags. RenderContext::GetColorSpaceFlags. 431 * @return Color space flags. 432 */ 433 virtual BASE_NS::ColorSpaceFlags GetColorSpaceFlags() const = 0; 434 435 /** Get color space flags related suggested format. Typically selects a format with or without sRGB. 436 * One needs to input the color space flags due to different plugins might be in different color spaces. 437 * Obviously some image data needs always linear formats and this method should not be used for those. 438 * @param format Format. 439 * @param colorSpaceFlags color space flags. 440 * @return Color space suggested format. 441 */ 442 virtual BASE_NS::Format GetColorSpaceFormat( 443 const BASE_NS::Format format, const BASE_NS::ColorSpaceFlags colorSpaceFlags) const = 0; 444 445 /** Get surface transform flags. 446 * @param handle Handle of the surface. 447 * @return Surface transform flags for given surface handle. If not surface a zero flags are returned. 448 */ 449 virtual SurfaceTransformFlags GetSurfaceTransformFlags(const RenderHandleReference& handle) const = 0; 450 451 protected: 452 IGpuResourceManager() = default; 453 virtual ~IGpuResourceManager() = default; 454 }; 455 456 /** IRenderNodeGpuResourceManager. 457 * Gpu resource manager interface to be used inside render nodes. 458 * Has management for per render node graph resources. 459 * All resources, created through this interface, are automatically tracked 460 * and destroyed when the render node graph which uses this render node is destroyed. 461 * 462 * Create methods take a unique name (if named). 463 * Object is re-created if the name is already in use. 464 * Therefore do not use scoped handles if you're recreating them. 465 */ 466 class IRenderNodeGpuResourceManager { 467 public: 468 /** Get render handle reference of raw handle. Up-to-date with generation. 469 * @param handle Raw render handle 470 * @return Returns A up-to-date (with generation) render handle reference for the handle. 471 */ 472 virtual RenderHandleReference Get(const RenderHandle& handle) const = 0; 473 474 /** Get render handle of raw handle. Up-to-date with generation. 475 * @param handle Raw render handle 476 * @return Returns A up-to-date (with generation) render handle. 477 */ 478 virtual RenderHandle GetRawHandle(const RenderHandle& handle) const = 0; 479 480 /** Create a GpuBuffer. 481 * @param desc Descriptor 482 */ 483 virtual RenderHandleReference Create(const GpuBufferDesc& desc) = 0; 484 485 /** Create a GpuBuffer with unique buffer name. 486 * @param name Name used for creation 487 * @param desc Descriptor 488 */ 489 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuBufferDesc& desc) = 0; 490 491 /** Create a GpuBuffer with replaced handle. 492 * @param handle Replaced handle 493 * @param desc Descriptor 494 */ 495 virtual RenderHandleReference Create(const RenderHandleReference& handle, const GpuBufferDesc& desc) = 0; 496 497 /** Create a GpuBuffer with unique buffer name and data. 498 * @param name Name for buffer 499 * @param desc Descriptor 500 * @param data Buffer data 501 */ 502 virtual RenderHandleReference Create( 503 const BASE_NS::string_view name, const GpuBufferDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 504 505 /** Create a GpuImage. 506 * @param desc Descriptor 507 */ 508 virtual RenderHandleReference Create(const GpuImageDesc& desc) = 0; 509 510 /** Create a GpuImage with unique image name. 511 * @param name Name for image 512 * @param desc Descriptor 513 */ 514 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuImageDesc& desc) = 0; 515 516 /** Create a GpuImage with replaced handle. 517 * @param handle Replaced handle 518 * @param desc Descriptor 519 */ 520 virtual RenderHandleReference Create(const RenderHandleReference& handle, const GpuImageDesc& desc) = 0; 521 522 /** Create a GpuImage with unique image name and data. 523 * @param name Name for image 524 * @param desc Descriptor 525 * @param data Image buffer data 526 */ 527 virtual RenderHandleReference Create( 528 const BASE_NS::string_view name, const GpuImageDesc& desc, const BASE_NS::array_view<const uint8_t> data) = 0; 529 530 /** Create a GpuSampler. 531 * @param desc Descriptor 532 */ 533 virtual RenderHandleReference Create(const GpuSamplerDesc& desc) = 0; 534 535 /** Create a GpuSampler with unique sampler name. 536 * @param name Name for sampler 537 * @param desc Descriptor 538 */ 539 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuSamplerDesc& desc) = 0; 540 541 /** Create a GpuSampler with replaced handle. 542 * @param handle Replaced handle 543 * @param desc Descriptor 544 */ 545 virtual RenderHandleReference Create(const RenderHandleReference& handle, const GpuSamplerDesc& desc) = 0; 546 547 /** Create a GpuAccelerationStructureDesc. 548 * @param desc Descriptor 549 */ 550 virtual RenderHandleReference Create(const GpuAccelerationStructureDesc& desc) = 0; 551 552 /** Create a GpuAccelerationStructureDesc with unique name. A GPU buffer handle is returned with additional 553 * acceleration structure. 554 * @param name Name for acceleration structure. 555 * @param desc Descriptor 556 */ 557 virtual RenderHandleReference Create(const BASE_NS::string_view name, const GpuAccelerationStructureDesc& desc) = 0; 558 559 /** Create a GpuAccelerationStructureDesc with replaced handle. A GPU buffer handle is returned with additional 560 * acceleration structure. 561 * @param handle Replaced handle 562 * @param desc Descriptor 563 */ 564 virtual RenderHandleReference Create( 565 const RenderHandleReference& handle, const GpuAccelerationStructureDesc& desc) = 0; 566 567 /** Get buffer handle 568 * @param name Name of buffer 569 */ 570 virtual RenderHandle GetBufferHandle(const BASE_NS::string_view name) const = 0; 571 572 /** Get image handle 573 * @param name Name of image 574 */ 575 virtual RenderHandle GetImageHandle(const BASE_NS::string_view name) const = 0; 576 577 /** Get sampler handle 578 * @param name Name of sampler 579 */ 580 virtual RenderHandle GetSamplerHandle(const BASE_NS::string_view name) const = 0; 581 582 /** Get buffer descriptor 583 * @param handle Handle to resource 584 */ 585 virtual GpuBufferDesc GetBufferDescriptor(const RenderHandle& handle) const = 0; 586 587 /** Get image descriptor 588 * @param handle Handle to resource 589 */ 590 virtual GpuImageDesc GetImageDescriptor(const RenderHandle& handle) const = 0; 591 592 /** Get sampler descriptor 593 * @param handle Handle to resource 594 */ 595 virtual GpuSamplerDesc GetSamplerDescriptor(const RenderHandle& handle) const = 0; 596 597 /** Get acceleration structure descriptor 598 * @param handle Handle to resource 599 */ 600 virtual GpuAccelerationStructureDesc GetAccelerationStructureDescriptor(const RenderHandle& handle) const = 0; 601 602 /** Map buffer, Only available in render nodes. 603 * Automatically advances buffered dynamic ring buffer offset. 604 * @param handle Handle to resource 605 */ 606 virtual void* MapBuffer(const RenderHandle& handle) const = 0; 607 608 /** Map buffer memory, Only available in render nodes. 609 * Always maps from the beginning of buffer. 610 * @param handle Handle to resource 611 */ 612 virtual void* MapBufferMemory(const RenderHandle& handle) const = 0; 613 614 /** Unmap buffer 615 * @param handle Handle to resource 616 */ 617 virtual void UnmapBuffer(const RenderHandle& handle) const = 0; 618 619 /** Checks validity of a handle */ 620 virtual bool IsValid(const RenderHandle& handle) const = 0; 621 /** Checks if resource is a GPU buffer */ 622 virtual bool IsGpuBuffer(const RenderHandle& handle) const = 0; 623 /** Checks if resource is a GPU image */ 624 virtual bool IsGpuImage(const RenderHandle& handle) const = 0; 625 /** Checks if resource is a GPU sampler */ 626 virtual bool IsGpuSampler(const RenderHandle& handle) const = 0; 627 /** Checks if resource is a GPU acceleration structure. If is GPU acceleration structure, it is GPU buffer as 628 * well.*/ 629 virtual bool IsGpuAccelerationStructure(const RenderHandle& handle) const = 0; 630 /** Image has been created to be used as a swapchain image. Fast check based on handle. 631 * Due to possible remapping with built-in CORE_DEFAULT_BACKBUFFER might not be an actual swapchain. 632 */ 633 virtual bool IsSwapchain(const RenderHandle& handle) const = 0; 634 635 /** Returns supported flags for given resource handle format. */ 636 virtual FormatProperties GetFormatProperties(const RenderHandle& handle) const = 0; 637 /** Returns supported flags for given format. */ 638 virtual FormatProperties GetFormatProperties(const BASE_NS::Format format) const = 0; 639 640 /** Returns name for the resource "" if no name found. 641 * NOTE: method should not be used during run-time on hot path. 642 * @param handle Handle of the resource. 643 * @return string Name of the resource. 644 */ 645 virtual BASE_NS::string GetName(const RenderHandle& handle) const = 0; 646 647 /** Returns all valid GPU buffer render handles. 648 * @return vector of handles. 649 */ 650 virtual BASE_NS::vector<RenderHandle> GetBufferHandles() const = 0; 651 /** Returns all valid GPU image render handles. 652 * @return vector of handles. 653 */ 654 virtual BASE_NS::vector<RenderHandle> GetImageHandles() const = 0; 655 /** Returns all valid GPU sampler render handles. 656 * @return vector of handles. 657 */ 658 virtual BASE_NS::vector<RenderHandle> GetSamplerHandles() const = 0; 659 660 /** Get gpu resource cache. 661 * @return Reference to GPU resource cache interface. 662 */ 663 virtual IGpuResourceCache& GetGpuResourceCache() const = 0; 664 665 /** Get image aspect flags for given format. 666 * @param handle Render handle of the resource 667 * @return Image aspect flags for given format. 668 */ 669 virtual ImageAspectFlags GetImageAspectFlags(const RenderHandle& handle) const = 0; 670 671 /** Get image aspect flags for given format. 672 * @param format Format. 673 * @return Image aspect flags for given format. 674 */ 675 virtual ImageAspectFlags GetImageAspectFlags(const BASE_NS::Format format) const = 0; 676 677 /** Get color space flags. RenderContext::GetColorSpaceFlags. 678 * @return Color space flags. 679 */ 680 virtual BASE_NS::ColorSpaceFlags GetColorSpaceFlags() const = 0; 681 682 /** Get color space flags related suggested format. Typically selects a format with or without sRGB. 683 * Obviously some image data needs always linear formats and this method should not be used for those. 684 * @param format Format. 685 * @return Color space suggested format. 686 */ 687 virtual BASE_NS::Format GetColorSpaceFormat( 688 const BASE_NS::Format format, const BASE_NS::ColorSpaceFlags colorSpaceFlags) const = 0; 689 690 /** Get surface transform flags. 691 * @param handle Handle of the surface. 692 * @return Surface transform flags for given surface handle. If not surface a zero flags are returned. 693 */ 694 virtual SurfaceTransformFlags GetSurfaceTransformFlags(const RenderHandle& handle) const = 0; 695 696 /** Render time automatic mapped GPU buffer data */ 697 struct MappedGpuBufferData { 698 /** Handle for binding the data to shader (cannot be mapped) */ 699 RenderHandle handle; 700 /** Binding byte offset */ 701 uint32_t bindingByteOffset { 0U }; 702 703 /** Byte size of data for writing and binding */ 704 uint32_t byteSize { 0U }; 705 /** Pointer to write byte size amount of data */ 706 uint8_t* data { nullptr }; 707 }; 708 709 /** Reserve render time UBO buffer data section in PreExecuteFrame(). 710 * Returns handle, which can be acquired later for mapping and binding. 711 * The returned handle is not valid handle for rendering. 712 * @param byteSize Size of the allocation (ask for the maximum amount). 713 * @return handle Valid only for AcquireGpuBuffer method in ExecuteFrame() 714 */ 715 virtual RenderHandle ReserveGpuBuffer(uint32_t byteSize) = 0; 716 717 /** Acquire reserved UBO buffer section during ExecuteFrame(). 718 * Returns a valid GpuBuffer handle, pointer, and offsets for writing and binding 719 * Do not separately call MapBuffer(). The data is already mapped. 720 * @param handle Reserved handle during PreExecuteFrame(). 721 * @return Mappable GPU buffer data. 722 */ 723 virtual MappedGpuBufferData AcquireGpubuffer(RenderHandle handle) = 0; 724 725 protected: 726 IRenderNodeGpuResourceManager() = default; 727 virtual ~IRenderNodeGpuResourceManager() = default; 728 }; 729 /** @} */ 730 RENDER_END_NAMESPACE() 731 732 #endif // API_RENDER_DEVICE_IGPU_RESOURCE_MANAGER_H 733