Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
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) 2025 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 /**
42 * @brief Returns the material's static settings (domain, blend mode, shading model, etc.).
43 */
45
46 /**
47 * @brief Initializes backend-specific GPU resources associated with this material.
48 *
49 * Must be called after construction and before rendering.
50 */
52 /**
53 * @brief Releases any GPU resources associated with this material.
54 */
56
57 /**
58 * @brief Packs the current float/vector parameters into a binary buffer and layout metadata.
59 *
60 * This buffer can be uploaded to the GPU as a uniform buffer or push constants.
61 *
62 * @return PackedMaterialData Struct containing binary data and parameter layout info.
63 */
65
66 /**
67 * @brief Returns the list of texture names used by this material.
68 */
69 std::vector<std::string> GetTextures() const;
70
71 /**
72 * @brief Returns the backend-specific GPU handle of the material.
73 */
75
76 /**
77 * @brief Sets a float parameter for the material.
78 *
79 * @param name Name of the parameter.
80 * @param value Float value to set.
81 */
82 void SetFloat(const std::string& name, float value);
83
84 /**
85 * @brief Sets a vec3 parameter for the material.
86 *
87 * @param name Name of the parameter.
88 * @param value glm::vec3 value to set.
89 */
90 void SetVec3(const std::string& name, glm::vec3 value);
91
92 /**
93 * @brief Sets a vec4 parameter for the material.
94 *
95 * @param name Name of the parameter.
96 * @param value glm::vec4 value to set.
97 */
98 void SetVec4(const std::string& name, glm::vec4 value);
99
100 /**
101 * @brief Adds a texture name to the material's list of used textures.
102 *
103 * @param textureName Name of the texture (e.g., texture ID or resource name).
104 */
105 void AddTexture(const std::string& textureName);
106
107private:
108 size_t std140_align(MaterialParameterLayoutEntry::Type type);
109 size_t std140_size(MaterialParameterLayoutEntry::Type type);
110
111private:
112 std::unordered_map<std::string, float> mFloatParameters;
113 std::unordered_map<std::string, glm::vec3> mVec3Parameters;
114 std::unordered_map<std::string, glm::vec4> mVec4Parameters;
115
116 MaterialSettings mMaterialSettings;
117 std::vector<std::string> mTextures;
118
119 IRenderer* mRenderer;
120 std::unique_ptr<IMaterialRenderResources> mGpuHandle;
121};
122
123} // namespace rendering_engine
Interface for backend-specific material GPU resources.
Defines an abstract interface for rendering backends.
void ReleaseRenderResources()
Releases any GPU resources associated with this material.
Definition material.cpp:25
void InitializeRenderResources()
Initializes backend-specific GPU resources associated with this material.
Definition material.cpp:20
void SetVec3(const std::string &name, glm::vec3 value)
Sets a vec3 parameter for the material.
Definition material.cpp:86
void SetVec4(const std::string &name, glm::vec4 value)
Sets a vec4 parameter for the material.
Definition material.cpp:91
std::vector< std::string > GetTextures() const
Returns the list of texture names used by this material.
Definition material.cpp:71
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:31
const MaterialSettings GetMaterialSettings() const
Returns the material's static settings (domain, blend mode, shading model, etc.).
Definition material.cpp:16
void AddTexture(const std::string &textureName)
Adds a texture name to the material's list of used textures.
Definition material.cpp:96
void SetFloat(const std::string &name, float value)
Sets a float parameter for the material.
Definition material.cpp:81
IMaterialRenderResources * GetMaterialRenderResources() const
Returns the backend-specific GPU handle of the material.
Definition material.cpp:76
Type
Settings required to define a material instance.
Contains the raw buffer data and layout metadata of packed material parameters.