Rendering Engine 0.2.9
Modular Graphics Rendering Engine | v0.2.9
material.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#pragma once
6
7#include <string>
8#include <unordered_map>
9#include <memory>
10
11#define GLM_FORCE_RADIANS
12#define GLM_FORCE_DEPTH_ZERO_TO_ONE
13#include <glm/glm.hpp>
14#include <glm/vec3.hpp>
15#include <glm/vec4.hpp>
16#include "material_types.hpp"
17
18namespace rendering_engine
19{
20class IRenderer;
21class IMaterialRenderResources;
22
23/**
24 * @brief Represents a material instance with parameter values, texture bindings, and rendering configuration.
25 *
26 * This class encapsulates both the CPU-side representation of a material (its parameters and textures),
27 * and a pointer to its backend-specific GPU resource handle.
28 */
30{
31public:
32 /**
33 * @brief Constructs a new Material instance.
34 *
35 * @param renderer Pointer to the rendering engine interface.
36 * @param matSettings Settings that define the material behavior.
37 */
38 explicit Material(IRenderer* renderer,
39 MaterialSettings matSettings);
40
41 ~Material();
42 /**
43 * @brief Returns the material's static settings (domain, blend mode, shading model, etc.).
44 */
46
47 /**
48 * @brief Initializes backend-specific GPU resources associated with this material.
49 *
50 * Must be called after construction and before rendering.
51 */
53 /**
54 * @brief Releases any GPU resources associated with this material.
55 */
57
58 /**
59 * @brief Packs the current float/vector parameters into a binary buffer and layout metadata.
60 *
61 * This buffer can be uploaded to the GPU as a uniform buffer or push constants.
62 *
63 * @return PackedMaterialData Struct containing binary data and parameter layout info.
64 */
66
67 /**
68 * @brief Returns the list of texture names used by this material.
69 */
70 std::vector<std::string> GetTextures() const;
71
72 /**
73 * @brief Returns the backend-specific GPU handle of the material.
74 */
76
77 /**
78 * @brief Sets a float parameter for the material.
79 *
80 * @param name Name of the parameter.
81 * @param value Float value to set.
82 */
83 void SetFloat(const std::string& name, float value);
84
85 /**
86 * @brief Sets a vec3 parameter for the material.
87 *
88 * @param name Name of the parameter.
89 * @param value glm::vec3 value to set.
90 */
91 void SetVec2(const std::string& name, glm::vec2 value);
92
93 /**
94 * @brief Sets a vec3 parameter for the material.
95 *
96 * @param name Name of the parameter.
97 * @param value glm::vec3 value to set.
98 */
99 void SetVec3(const std::string& name, glm::vec3 value);
100
101 /**
102 * @brief Sets a vec4 parameter for the material.
103 *
104 * @param name Name of the parameter.
105 * @param value glm::vec4 value to set.
106 */
107 void SetVec4(const std::string& name, glm::vec4 value);
108
109 /**
110 * @brief Adds a texture name to the material's list of used textures.
111 *
112 * @param textureName Name of the texture (e.g., texture ID or resource name).
113 */
114 void AddTexture(const std::string& textureName);
115
116private:
117 size_t std140_align(MaterialParameterLayoutEntry::Type type);
118 size_t std140_size(MaterialParameterLayoutEntry::Type type);
119
120private:
121 std::unordered_map<std::string, float> mFloatParameters;
122 std::unordered_map<std::string, glm::vec2> mVec2Parameters;
123 std::unordered_map<std::string, glm::vec3> mVec3Parameters;
124 std::unordered_map<std::string, glm::vec4> mVec4Parameters;
125
126 MaterialSettings mMaterialSettings;
127 std::vector<std::string> mTextures;
128
129 IRenderer* mRenderer;
130 std::unique_ptr<IMaterialRenderResources> mGpuHandle;
131 const std::vector<MaterialParameterLayoutEntry>* mParameterLayout;
132};
133
134} // namespace rendering_engine
Interface for backend-specific material GPU resources.
Defines an abstract interface for rendering backends.
Definition: i_renderer.hpp:29
Represents a material instance with parameter values, texture bindings, and rendering configuration.
Definition: material.hpp:30
void ReleaseRenderResources()
Releases any GPU resources associated with this material.
Definition: material.cpp:31
void InitializeRenderResources()
Initializes backend-specific GPU resources associated with this material.
Definition: material.cpp:26
void SetVec3(const std::string &name, glm::vec3 value)
Sets a vec3 parameter for the material.
Definition: material.cpp:102
void SetVec2(const std::string &name, glm::vec2 value)
Sets a vec3 parameter for the material.
Definition: material.cpp:97
void SetVec4(const std::string &name, glm::vec4 value)
Sets a vec4 parameter for the material.
Definition: material.cpp:107
std::vector< std::string > GetTextures() const
Returns the list of texture names used by this material.
Definition: material.cpp:82
Material(IRenderer *renderer, MaterialSettings matSettings)
Constructs a new Material instance.
Definition: material.cpp:10
PackedMaterialData PackMaterialParameters()
Packs the current float/vector parameters into a binary buffer and layout metadata.
Definition: material.cpp:39
const MaterialSettings GetMaterialSettings() const
Returns the material's static settings (domain, blend mode, shading model, etc.).
Definition: material.cpp:22
void AddTexture(const std::string &textureName)
Adds a texture name to the material's list of used textures.
Definition: material.cpp:112
void SetFloat(const std::string &name, float value)
Sets a float parameter for the material.
Definition: material.cpp:92
IMaterialRenderResources * GetMaterialRenderResources() const
Returns the backend-specific GPU handle of the material.
Definition: material.cpp:87
Settings required to define a material instance.
Contains the raw buffer data and layout metadata of packed material parameters.