Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
Material.hpp
1/*
2Copyright (C) 2016-2019,2021,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGAMES_MATERIAL_H
17#define AEONGAMES_MATERIAL_H
18#include <string>
19#include <vector>
20#include <memory>
21#include <variant>
22#include <initializer_list>
24#include "aeongames/ResourceId.hpp"
25#include "aeongames/Vector2.hpp"
26#include "aeongames/Vector3.hpp"
27#include "aeongames/Vector4.hpp"
29#include "aeongames/Resource.hpp"
30
31namespace AeonGames
32{
33 class Image;
34 class MaterialMsg;
35 class PropertyMsg;
37 class Material final : public Resource
38 {
39 public:
41 using UniformValue = std::variant<uint32_t, int32_t, float, Vector2, Vector3, Vector4, Matrix4x4>;
43 using UniformKeyValue = std::tuple<std::string, UniformValue>;
45 using SamplerKeyValue = std::tuple<uint32_t, ResourceId>;
47 DLL Material();
49 DLL Material ( const Material& aMaterial );
51 DLL Material& operator= ( const Material& aMaterial );
55 Material ( Material&& ) = delete;
57 DLL ~Material() final;
60
63 DLL void LoadFromPBMsg ( const MaterialMsg& aMaterialMsg );
68 DLL void LoadFromMemory ( const void* aBuffer, size_t aBufferSize ) final;
70 DLL void Unload() final;
74
78 DLL void Set ( size_t aIndex, const UniformValue& aValue );
82 DLL void Set ( const UniformKeyValue& aValue );
87 DLL void SetSampler ( const std::string& aName, const ResourceId& aValue );
91
95 DLL ResourceId GetSampler ( const std::string& aName );
99 DLL const std::vector<std::tuple<uint32_t, ResourceId >> & GetSamplers() const;
101
104 DLL const std::vector<uint8_t>& GetUniformBuffer() const;
105 private:
107 class UniformVariable
108 {
109 public:
110 UniformVariable ( const std::string & aName, size_t aOffset ) :
111 mName{aName},
112 mOffset{aOffset} {}
113 const std::string & GetName() const
114 {
115 return mName;
116 }
117 size_t GetOffset()
118 {
119 return mOffset;
120 }
121 private:
122 std::string mName{};
123 size_t mOffset{};
124 };
125 DLL size_t LoadVariables ( const MaterialMsg& aMaterialMsg );
126 DLL void LoadSamplers ( const MaterialMsg& aMaterialMsg );
127 DLL size_t LoadVariables ( std::initializer_list<UniformKeyValue> aUniforms );
128 DLL void LoadSamplers ( std::initializer_list<SamplerKeyValue> aSamplers );
129 std::vector<UniformVariable> mVariables{};
130 std::vector<SamplerKeyValue> mSamplers{};
131 std::vector<uint8_t> mUniformBuffer{};
132 };
133
137 DLL size_t GetUniformValueSize ( const Material::UniformValue& aValue );
142 DLL const void* GetUniformValuePointer ( const Material::UniformValue& aValue );
143}
144#endif
Header for 4x4 matrix class.
Platform-specific macros, includes, and DLL export/import definitions.
Header for the 3D vector class.
Header for the 4D vector class.
Material(Material &&)=delete
No move allowed.
DLL void SetSampler(const std::string &aName, const ResourceId &aValue)
Set a sampler binding by name.
Definition Material.cpp:231
DLL void Unload() final
Unload material data and release resources.
Definition Material.cpp:265
std::variant< uint32_t, int32_t, float, Vector2, Vector3, Vector4, Matrix4x4 > UniformValue
Variant type holding any supported uniform value.
Definition Material.hpp:41
DLL void Set(size_t aIndex, const UniformValue &aValue)
Set a uniform value by index.
Definition Material.cpp:205
DLL const std::vector< uint8_t > & GetUniformBuffer() const
Get the raw uniform buffer.
Definition Material.cpp:45
std::tuple< std::string, UniformValue > UniformKeyValue
Key-value pair mapping a uniform name to its value.
Definition Material.hpp:43
DLL void LoadFromMemory(const void *aBuffer, size_t aBufferSize) final
Load material data from a raw memory buffer.
Definition Material.cpp:186
DLL Material()
Default constructor.
DLL ResourceId GetSampler(const std::string &aName)
Get the resource id of a sampler by name.
Definition Material.cpp:250
DLL void LoadFromPBMsg(const MaterialMsg &aMaterialMsg)
Load material data from a protobuf message.
Definition Material.cpp:191
DLL ~Material() final
Destructor.
DLL const std::vector< std::tuple< uint32_t, ResourceId > > & GetSamplers() const
Get all sampler bindings.
Definition Material.cpp:245
std::tuple< uint32_t, ResourceId > SamplerKeyValue
Key-value pair mapping a sampler binding index to an image resource id.
Definition Material.hpp:45
DLL Material & operator=(const Material &aMaterial)
Assignment operator due to rule of zero/three/five.
Definition Material.cpp:38
Base class for loadable engine resources.
Definition Resource.hpp:33
Identifies a resource by its type and path CRC32 hashes.
<- This is here just for the literals
Definition AABB.hpp:31
DLL const void * GetUniformValuePointer(const Material::UniformValue &aValue)
Get a pointer to the raw data of a uniform value.
Definition Material.cpp:162
DLL size_t GetUniformValueSize(const Material::UniformValue &aValue)
Get the byte size of a uniform value.
Definition Material.cpp:154
STL namespace.