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