################
Resource Manager
################

The resource manager is a singleton. It can be created or accessed using the same code:

.. include:: /tasks/ta_ca/ta_ca_application/tk_creating_or_accessing_the_resource_manager.rst

The resource manager serves several purposes:

    * Licensing: The resource manager implements the ``RED::ILicense`` option. It must be created, accessed as a licensing interface in order to supply a valid product license to HOOPS Luminate. See the :doc:`/getting_started/licensing` page for details.
    * Management of hardware platform: The resource manager implements the ``RED::IGraphicDevice`` interface for querying the current graphic adapter in use, getting informations on the driver revision installed and supply driver recommendations. See the :doc:`/book/subjects/bk_ba/bk_ba_hardware_platforms` for details.
    * Management of shared resources: The resource manager is the container of all shared resources in HOOPS Luminate: this includes images, materials, shader programs and fonts. See below in **Shared Graphic Resources**.
    * Cleanup: Several cleanup tools are available from the resource manager to remove useless data from the current cluster of objects. See below the **Cleanup Methods**.
    * Transaction management. See :doc:`/book/subjects/bk_ba/bk_ba_transaction_management` for details.

It can be also used to set / retrieve some basic informations on the engine, such as:

    * The number of available processors (``RED::IResourceManager::GetNumberOfProcessors``)
    * The list of windows that exist in the cluster (``RED::IResourceManager::GetWindowList``)
    * Callbacks (interruption, batch synchronization, ...)
    * ``RED::IDataManager`` access. The data manager of the cluster can be retrieved directly from the resource manager

So, this is a key object in any HOOPS Luminate application. The fact that the resource manager is a singleton makes it easy to access from anywhere in the calling application.


**************************************
Releasing All HOOPS Luminate Resources
**************************************

On exit, after a reset, for the purpose of getting back memory, an application may want to delete all resources used by HOOPS Luminate. This can be achieved by destroying the resource manager of the application. On using the ``RED::Factory::DeleteInstance`` method for the resource manager, all resources in HOOPS Luminate will be destroyed. All data in the video memory will be released too.

After destruction, HOOPS Luminate can be restarted by creating another resource manager singleton.


************************
Shared Graphic Resources
************************

There are different kinds of resources that are shared among HOOPS Luminate objects:

    * Image: An image can be shared among several materials containing shaders (``RED::Shader``) for the rendering
    * Materials: A material can be shared among several shapes for the rendering
    * Fonts: A font can be shared among different text shapes (``RED::ITextShape``) for the rendering
    * Shader programs: A ``RED::RenderShader`` (or a composite image - ``RED::IImageComposite``) defines its behavior using program strings. The resource manager holds all these strings and supply identifiers for their manipulation

Generally speaking, it's the responsibility of the calling application to manage how shared resources are used. On the destruction of an image, HOOPS Luminate does not parse all materials to remove that image from all the found shaders using it. This because HOOPS Luminate can't know what should be set instead of the image. If the image can be destroyed by the application, then it's the responsibility of the application to make sure that this image is not used anywhere else by the engine. If this rule is not enforced, the engine may return ``RED_SCG_INVALID_IMAGE_ADDRESS``.

Similarly, releasing a material or a font does not invalidate the usage of this material or font from all shapes in the HOOPS Luminate scene graphs that exist. It's the responsibility of the calling application to ensure that the deleted resource is not used anymore before releasing it from the HOOPS Luminate resource manager.

Finally, shader programs are loaded using ``RED::IResourceManager::LoadShaderFromString`` and never discarded from the resource manager.

Please note that all images, materials and fonts generated in the cluster after loading .red files are under the control of the ``RED::IDataManager`` of the cluster. This is the data manager that will release these resources on releasing contexts.

.. include:: /tasks/ta_ca/ta_ca_application/tk_creating_or_destroying_an_image.rst

.. include:: /tasks/ta_ca/ta_ca_application/tk_creating_or_destroying_a_material.rst

.. include:: /tasks/ta_ca/ta_ca_application/tk_creating_or_destroying_a_font.rst


***************
Cleanup Methods
***************

HOOPS Luminate offers cleanup methods that can be used to collect and erase all resources that are not in use anymore. Calling these methods can be time consuming if there are lots of objects in the cluster. Any resource that is not in use in the cluster will be released unless specified otherwise in the cleanup method. See:

    * ``RED::IResourceManager::CleanupMaterials``
    * ``RED::IResourceManager::CleanupImages``
    * ``RED::IResourceManager::CleanupFonts``
    * Or the global ``RED::IResourceManager::Cleanup`` method




