Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
material.cpp
Go to the documentation of this file.
1#include "material.hpp"
2#include "i_renderer.hpp"
4
5#include <cstring>
6
7namespace rendering_engine
8{
9
11 :
12 mRenderer(renderer),
13 mMaterialSettings(matSettings),
14 mGpuHandle(nullptr)
15{}
17{
18 return mMaterialSettings;
19}
21{
22 mGpuHandle = std::unique_ptr<IMaterialRenderResources>(mRenderer->ProvideMaterialRenderResources());
23 mGpuHandle->Initialize(this);
24}
26{
27 mGpuHandle->Shutdown();
28 mGpuHandle.release();
29 mGpuHandle = nullptr;
30}
32{
33 PackedMaterialData result;
34 size_t currentOffset = 0;
35
36 // floats
37 for (const auto& [name, value] : mFloatParameters)
38 {
39 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Float);
40 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
41 result.layout.push_back({ name, currentOffset, 4, MaterialParameterLayoutEntry::Type::Float });
42 result.buffer.resize(currentOffset + 4);
43 std::memcpy(result.buffer.data() + currentOffset, &value, 4);
44 currentOffset += 4;
45 }
46 // vec3
47 for (const auto& [name, value] : mVec3Parameters)
48 {
49 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Vec3);
50 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
51 result.layout.push_back({ name, currentOffset, 12, MaterialParameterLayoutEntry::Type::Vec3 });
52 result.buffer.resize(currentOffset + 16); // 16 bytes for std140 vec3
53 std::memcpy(result.buffer.data() + currentOffset, &value, 12);
54 // pad last 4 bytes with zero
55 std::memset(result.buffer.data() + currentOffset + 12, 0, 4);
56 currentOffset += 16;
57 }
58 // vec4
59 for (const auto& [name, value] : mVec4Parameters)
60 {
61 size_t alignment = std140_align(MaterialParameterLayoutEntry::Type::Vec4);
62 currentOffset = (currentOffset + alignment - 1) & ~(alignment - 1);
63 result.layout.push_back({ name, currentOffset, 16, MaterialParameterLayoutEntry::Type::Vec4 });
64 result.buffer.resize(currentOffset + 16);
65 std::memcpy(result.buffer.data() + currentOffset, &value, 16);
66 currentOffset += 16;
67 }
68 return result;
69}
70
71std::vector<std::string> Material::GetTextures() const
72{
73 return mTextures;
74}
75
77{
78 return mGpuHandle.get();
79}
80
81void Material::SetFloat(const std::string& name, float value)
82{
83 mFloatParameters[name] = value;
84}
85
86void Material::SetVec3(const std::string& name, glm::vec3 value)
87{
88 mVec3Parameters[name] = value;
89}
90
91void Material::SetVec4(const std::string& name, glm::vec4 value)
92{
93 mVec4Parameters[name] = value;
94}
95
96void Material::AddTexture(const std::string& textureName)
97{
98 mTextures.push_back(textureName);
99}
100
101size_t Material::std140_align(MaterialParameterLayoutEntry::Type type)
102{
103 switch (type)
104 {
106 case MaterialParameterLayoutEntry::Type::Vec3: // falls through
108 }
109 return 4;
110}
111
112size_t Material::std140_size(MaterialParameterLayoutEntry::Type type)
113{
114 switch (type)
115 {
117 case MaterialParameterLayoutEntry::Type::Vec3: return 16; // padded to 16
119 }
120 return 4;
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
@ Vec3
@ Float
@ Vec4
Settings required to define a material instance.
Contains the raw buffer data and layout metadata of packed material parameters.
std::vector< MaterialParameterLayoutEntry > layout