Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
Loading...
Searching...
No Matches
material_cache.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2026 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
9#include <string>
10#include <unordered_map>
11#include <memory>
12
13namespace rendering_engine
14{
15class IRenderer;
16class Material;
17struct MaterialSettings;
18
19/**
20 * @class MaterialCache
21 * @brief Manages creation, storage, and lifecycle of Material objects within the rendering engine.
22 *
23 * MaterialCache serves as a centralized system to instantiate, store, and retrieve materials used in the engine.
24 * It supports both manually added materials and, in the future, loading materials from JSON definitions.
25 * @see Jira EPIC: MAT-101 - Node-Based Material Editor
26 *
27 * It observes renderer events (such as swapchain recreation) to reinitialize or release GPU resources tied to materials.
28 */
30{
31public:
32 /**
33 * @brief Constructs a MaterialCache and registers it as a renderer observer.
34 * @param renderer Pointer to the renderer interface used for creating GPU resources.
35 */
36 MaterialCache(IRenderer* renderer);
37 /**
38 * @brief Destructor. Automatically unregisters from the renderer.
39 */
41
42 /**
43 * @brief Creates built-in materials for testing or demonstration.
44 *
45 * This is a temporary function to manually define materials in code.
46 * In the future, materials will be created by parsing JSON files from a materials folder.
47 */
49
50 /**
51 * @brief Adds a new Material instance using the provided settings.
52 * @param matSettings MaterialSettings struct containing metadata and configuration for the new material.
53 *
54 * If a material with the same name already exists, it will be replaced.
55 */
56 void AddMaterial(MaterialSettings matSettings);
57
58 /**
59 * @brief Retrieves a pointer to a Material instance by name.
60 * @param materialName Name of the material to retrieve.
61 * @return Pointer to the Material instance, or nullptr if not found.
62 */
63 Material* GetMaterial(std::string materialName);
64
65 /**
66 * @brief Removes a material from the cache.
67 * @param materialName Name of the material to remove.
68 */
69 void RemoveMaterial(std::string materialName);
70
71 /**
72 * @brief Releases all GPU resources associated with cached materials.
73 *
74 * Iterates through every material in the cache and calls their resource
75 * release routines, freeing descriptor set layouts, pipelines, and any other
76 * backend allocations. Unlike @ref RemoveMaterial, this does not erase the
77 * material records themselves from the cache, only their GPU-side resources.
78 *
79 * @details
80 * - Use when unloading a scene or before destroying the renderer to ensure
81 * all GPU resources are released in a controlled manner.
82 * - After calling this, materials remain in the cache but are not renderable
83 * until @ref OnRenderResourcesRebuild (or per-material reinitialization)
84 * is performed.
85 * - Safe to call multiple times; redundant calls will have no effect once
86 * resources are released.
87 *
88 * @see RemoveMaterial()
89 * @see OnRenderResourcesRelease()
90 * @see OnRenderResourcesRebuild()
91 */
92 void ReleaseAll();
93
94protected:
95 /**
96 * @copydoc IRendererObserver::OnRenderResourcesRelease
97 */
98 void OnRenderResourcesRelease() override;
99 /**
100 * @copydoc IRendererObserver::OnRenderResourcesRebuild
101 */
102 void OnRenderResourcesRebuild() override;
103
104protected:
106 std::unordered_map<std::string, std::shared_ptr<Material>> mMaterials;
107};
108
109} // namespace rendering_engine
Interface for observing renderer resource lifecycle events.
Defines an abstract interface for rendering backends.
Manages creation, storage, and lifecycle of Material objects within the rendering engine.
void OnRenderResourcesRebuild() override
Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).
Material * GetMaterial(std::string materialName)
Retrieves a pointer to a Material instance by name.
void CreateBuildInMaterials()
Creates built-in materials for testing or demonstration.
void RemoveMaterial(std::string materialName)
Removes a material from the cache.
void AddMaterial(MaterialSettings matSettings)
Adds a new Material instance using the provided settings.
std::unordered_map< std::string, std::shared_ptr< Material > > mMaterials
~MaterialCache()
Destructor. Automatically unregisters from the renderer.
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).
void ReleaseAll()
Releases all GPU resources associated with cached materials.
Represents a material instance with parameter values, texture bindings, and rendering configuration.
Definition material.hpp:30
Settings required to define a material instance.