Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::MaterialCache Class Reference

Manages creation, storage, and lifecycle of Material objects within the rendering engine. More...

#include <material_cache.hpp>

Inherits rendering_engine::IRendererObserver.

Public Member Functions

 MaterialCache (IRenderer *renderer)
 Constructs a MaterialCache and registers it as a renderer observer.
 ~MaterialCache ()
 Destructor. Automatically unregisters from the renderer.
void CreateBuildInMaterials ()
 Creates built-in materials for testing or demonstration.
void AddMaterial (MaterialSettings matSettings)
 Adds a new Material instance using the provided settings.
MaterialGetMaterial (std::string materialName)
 Retrieves a pointer to a Material instance by name.
void RemoveMaterial (std::string materialName)
 Removes a material from the cache.
void ReleaseAll ()
 Releases all GPU resources associated with cached materials.
Public Member Functions inherited from rendering_engine::IRendererObserver
virtual ~IRendererObserver ()=default
 Virtual destructor.

Protected Member Functions

void OnRenderResourcesRelease () override
 Renderer callback: release all GPU resources (used during device loss/reset).
void OnRenderResourcesRebuild () override
 Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).

Protected Attributes

IRenderermRenderer
std::unordered_map< std::string, std::shared_ptr< Material > > mMaterials

Detailed Description

Manages creation, storage, and lifecycle of Material objects within the rendering engine.

MaterialCache serves as a centralized system to instantiate, store, and retrieve materials used in the engine. It supports both manually added materials and, in the future, loading materials from JSON definitions.

See also
Jira EPIC: MAT-101 - Node-Based Material Editor

It observes renderer events (such as swapchain recreation) to reinitialize or release GPU resources tied to materials.

Definition at line 29 of file material_cache.hpp.

Constructor & Destructor Documentation

◆ MaterialCache()

rendering_engine::MaterialCache::MaterialCache ( IRenderer * renderer)

Constructs a MaterialCache and registers it as a renderer observer.

Parameters
rendererPointer to the renderer interface used for creating GPU resources.

Definition at line 9 of file material_cache.cpp.

10 :
11 mRenderer(renderer)
12{
13 mRenderer->RegisterObserver(this);
14}

◆ ~MaterialCache()

rendering_engine::MaterialCache::~MaterialCache ( )

Destructor. Automatically unregisters from the renderer.

Definition at line 16 of file material_cache.cpp.

17{
18 mRenderer->UnregisterObserver(this);
19}

Member Function Documentation

◆ AddMaterial()

void rendering_engine::MaterialCache::AddMaterial ( MaterialSettings matSettings)

Adds a new Material instance using the provided settings.

Parameters
matSettingsMaterialSettings struct containing metadata and configuration for the new material.

If a material with the same name already exists, it will be replaced.

Definition at line 71 of file material_cache.cpp.

72{
73 if (matSettings.materialName.empty())
74 {
75 // Log incorrect material name
76 return;
77 }
78 mMaterials[matSettings.materialName] = std::make_shared<Material>(mRenderer, matSettings);
79}
std::unordered_map< std::string, std::shared_ptr< Material > > mMaterials

◆ CreateBuildInMaterials()

void rendering_engine::MaterialCache::CreateBuildInMaterials ( )

Creates built-in materials for testing or demonstration.

This is a temporary function to manually define materials in code. In the future, materials will be created by parsing JSON files from a materials folder.

Definition at line 21 of file material_cache.cpp.

22{
23 // BasicTexture3D material
24 MaterialSettings setBasicTexture3D;
25 setBasicTexture3D.materialName = std::string{ "BasicTexture3D" };
26 setBasicTexture3D.materialDomain = MaterialDomain::Surface3D;
27 setBasicTexture3D.shadingModel = ShadingModel::Unlit;
28 setBasicTexture3D.blendMode = BlendMode::Opaque;
29
30 AddMaterial(setBasicTexture3D);
31 Material* basicTexture3DMat = GetMaterial(setBasicTexture3D.materialName);
32 if (basicTexture3DMat)
33 {
34 basicTexture3DMat->AddTexture("test_cube_color");
35 //basicTexture3DMat->AddTexture("PNG_transparency_demonstration_1");
36 }
37 basicTexture3DMat->InitializeRenderResources();
38
39 // FlatColorFiltering material
40 MaterialSettings setFlatColorFiltering;
41 setFlatColorFiltering.materialName = std::string{ "FlatColorFiltering" };
42 setFlatColorFiltering.materialDomain = MaterialDomain::Surface3D;
43 setFlatColorFiltering.shadingModel = ShadingModel::Unlit;
44 setFlatColorFiltering.blendMode = BlendMode::Opaque;
45
46 AddMaterial(setFlatColorFiltering);
47 Material* flatColorFilteringMat = GetMaterial(setFlatColorFiltering.materialName);
48 if (flatColorFilteringMat)
49 {
50 flatColorFilteringMat->AddTexture("D4FontTextureSegoeScript");
51 }
52 flatColorFilteringMat->InitializeRenderResources();
53
54 // Test sprite 2d
55 MaterialSettings setTestSprite2d;
56 setTestSprite2d.materialName = std::string{ "Quad2D" };
57 setTestSprite2d.materialDomain = MaterialDomain::Sprite2D;
58 setTestSprite2d.shadingModel = ShadingModel::Unlit;
59 setTestSprite2d.blendMode = BlendMode::Opaque;
60
61 AddMaterial(setTestSprite2d);
62 Material* testSprite2dMat = GetMaterial(setTestSprite2d.materialName);
63 if (testSprite2dMat)
64 {
65 testSprite2dMat->AddTexture("PNG_transparency_demonstration_1");
66 }
67
68 testSprite2dMat->InitializeRenderResources();
69}
Material * GetMaterial(std::string materialName)
Retrieves a pointer to a Material instance by name.
void AddMaterial(MaterialSettings matSettings)
Adds a new Material instance using the provided settings.

◆ GetMaterial()

Material * rendering_engine::MaterialCache::GetMaterial ( std::string materialName)

Retrieves a pointer to a Material instance by name.

Parameters
materialNameName of the material to retrieve.
Returns
Pointer to the Material instance, or nullptr if not found.

Definition at line 81 of file material_cache.cpp.

82{
83 if (auto search = mMaterials.find(materialName); search == mMaterials.end())
84 {
85 return nullptr;
86 }
87
88 return mMaterials.at(materialName).get();
89}

◆ OnRenderResourcesRebuild()

void rendering_engine::MaterialCache::OnRenderResourcesRebuild ( )
overrideprotectedvirtual

Renderer callback: re-upload or recreate all GPU resources (used after device reset/rebuild).

This method will be called after the device or swapchain is recreated, allowing the observer to re-upload or recreate all necessary resources for rendering.

Implements rendering_engine::IRendererObserver.

Definition at line 113 of file material_cache.cpp.

114{
115 for (auto& material : mMaterials)
116 {
117 material.second->InitializeRenderResources();
118 }
119}

◆ OnRenderResourcesRelease()

void rendering_engine::MaterialCache::OnRenderResourcesRelease ( )
overrideprotectedvirtual

Renderer callback: release all GPU resources (used during device loss/reset).

This method will be called before any device or swapchain is destroyed, allowing the observer to safely release all handles and deallocate any GPU memory.

Implements rendering_engine::IRendererObserver.

Definition at line 105 of file material_cache.cpp.

106{
107 for (auto& material : mMaterials)
108 {
109 material.second->ReleaseRenderResources();
110 }
111}

◆ ReleaseAll()

void rendering_engine::MaterialCache::ReleaseAll ( )

Releases all GPU resources associated with cached materials.

Iterates through every material in the cache and calls their resource release routines, freeing descriptor set layouts, pipelines, and any other backend allocations. Unlike RemoveMaterial, this does not erase the material records themselves from the cache, only their GPU-side resources.

  • Use when unloading a scene or before destroying the renderer to ensure all GPU resources are released in a controlled manner.
  • After calling this, materials remain in the cache but are not renderable until OnRenderResourcesRebuild (or per-material reinitialization) is performed.
  • Safe to call multiple times; redundant calls will have no effect once resources are released.
See also
RemoveMaterial()
OnRenderResourcesRelease()
OnRenderResourcesRebuild()

Definition at line 100 of file material_cache.cpp.

101{
103}
void OnRenderResourcesRelease() override
Renderer callback: release all GPU resources (used during device loss/reset).

◆ RemoveMaterial()

void rendering_engine::MaterialCache::RemoveMaterial ( std::string materialName)

Removes a material from the cache.

Parameters
materialNameName of the material to remove.

Definition at line 91 of file material_cache.cpp.

92{
93 if (auto it = mMaterials.find(materialName); it != mMaterials.end())
94 {
95 it->second->ReleaseRenderResources();
96 mMaterials.erase(it);
97 }
98}

Member Data Documentation

◆ mMaterials

std::unordered_map<std::string, std::shared_ptr<Material> > rendering_engine::MaterialCache::mMaterials
protected

Definition at line 106 of file material_cache.hpp.

◆ mRenderer

IRenderer* rendering_engine::MaterialCache::mRenderer
protected

Definition at line 105 of file material_cache.hpp.


The documentation for this class was generated from the following files: