Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
ResourcesManager.hpp
1#pragma once
2
3#include "graphics/GPUTypes.hpp"
4#include "graphics/Program.hpp"
5#include "resources/Font.hpp"
6#include "resources/Mesh.hpp"
7#include "Common.hpp"
8
9
14enum class Storage : uint {
15 NONE = 0,
16 GPU = 1,
17 CPU = 2,
18 BOTH = (GPU | CPU),
19 FORCE_FRAME = 4
20};
21
27inline Storage operator|(Storage t0, Storage t1) {
28 return static_cast<Storage>(static_cast<uint>(t0) | static_cast<uint>(t1));
29}
30
36inline bool operator&(Storage t0, Storage t1) {
37 return bool(static_cast<uint>(t0) & static_cast<uint>(t1));
38}
39
41using Data = std::vector<char>;
42
48class Resources {
49
51 friend class Image;
52
53public:
57 static Resources & manager();
58
62 void addResources(const std::string & path);
63
66 void reload();
67
69 void clean();
70
74 Resources & operator=(const Resources &) = delete;
75
77 Resources(const Resources &) = delete;
78
83
85 Resources(Resources &&) = delete;
86
87private:
90 Resources() = default;
91
95 void parseArchive(const std::string & archivePath);
96
100 void parseDirectory(const std::string & directoryPath);
101
106 std::string getImagePath(const std::string & name);
107
112 std::vector<std::string> getCubemapPaths(const std::string & name);
113
119 std::vector<std::string> getLayeredPaths(const std::string & name, const std::string & suffix);
120
126 char * getRawData(const std::string & path, size_t & size);
127
128public:
133 std::string getString(const std::string & filename);
134
140 std::string getStringWithIncludes(const std::string & filename, std::vector<std::string> & names);
141
146 std::string getStringWithIncludes(const std::string& filename);
147
153 const Mesh * getMesh(const std::string & name, Storage options);
154
165 const Texture * getTexture(const std::string & name, const Layout & format, Storage options, const std::string & refName = "");
166
171 const Texture * getTexture(const std::string & name);
172
178
187 Program * getProgram(const std::string & name, const std::string & vertexName = "", const std::string & fragmentName= "", const std::string & tessControlName = "", const std::string & tessEvalName = "");
188
194 Program * getProgram2D(const std::string & name);
195
201 Program * getProgramCompute(const std::string & name);
202
207 Font * getFont(const std::string & name);
208
213 const Data * getData(const std::string & filename);
214
215public:
216
222 static char * loadRawDataFromExternalFile(const std::string & path, size_t & size);
223
229 static std::string loadStringFromExternalFile(const std::string & path);
230
236 static void saveRawDataToExternalFile(const std::string & path, char * rawContent, size_t size);
237
242 static void saveStringToExternalFile(const std::string & path, const std::string & content);
243
248 static bool externalFileExists(const std::string & path);
249
251 struct FileInfos {
252 std::string path;
253 std::string name;
254
259 FileInfos(const std::string& apath, const std::string& aname);
260 };
261
266 void getFiles(const std::string & extension, std::vector<FileInfos> & files) const;
267
268private:
270 ~Resources() = default;
271
274
281 ProgramInfos(const std::string & vertex, const std::string & fragment, const std::string & tessControl, const std::string & tessEval);
282
286 ProgramInfos(const std::string & compute);
287
288 std::string vertexName;
289 std::string fragmentName;
290 std::string tessContName;
291 std::string tessEvalName;
292 std::string computeName;
293 };
294
295 std::unordered_map<std::string, std::string> _files;
296 std::unordered_map<std::string, Texture> _textures;
297 std::unordered_map<std::string, Mesh> _meshes;
298 std::unordered_map<std::string, Font> _fonts;
299 std::unordered_map<std::string, Data> _blobs;
300 std::unordered_map<std::string, Program> _programs;
301 std::unordered_map<std::string, ProgramInfos> _progInfos;
302};
Font loading and storage: texture atlas, codepoints supported, dimensions of each glyph.
Definition: Font.hpp:12
Provide utility functions to communicate with the driver and GPU.
Definition: GPU.hpp:20
Represents an image composed of pixels with values in [0,1]. Provide image loading/saving utilities,...
Definition: Image.hpp:8
Represents a geometric mesh composed of vertices, other attributes and triangles.
Definition: Mesh.hpp:15
Represents a group of shaders used for rendering.
Definition: Program.hpp:31
The Resources manager is responsible for all resources loading and setup.
Definition: ResourcesManager.hpp:48
std::unordered_map< std::string, Texture > _textures
Loaded textures, identified by name.
Definition: ResourcesManager.hpp:296
std::vector< std::string > getCubemapPaths(const std::string &name)
Definition: ResourcesManager.cpp:143
std::unordered_map< std::string, Program > _programs
Loaded shader programs, identified by name.
Definition: ResourcesManager.hpp:300
void reload()
Definition: ResourcesManager.cpp:644
void clean()
Definition: ResourcesManager.cpp:769
std::string getStringWithIncludes(const std::string &filename, std::vector< std::string > &names)
Definition: ResourcesManager.cpp:250
~Resources()=default
const Texture * getTexture(const std::string &name, const Layout &format, Storage options, const std::string &refName="")
Definition: ResourcesManager.cpp:352
Program * getProgram2D(const std::string &name)
Definition: ResourcesManager.cpp:624
static void saveRawDataToExternalFile(const std::string &path, char *rawContent, size_t size)
Definition: ResourcesManager.cpp:740
const Mesh * getMesh(const std::string &name, Storage options)
Definition: ResourcesManager.cpp:306
Program * getProgram(const std::string &name, const std::string &vertexName="", const std::string &fragmentName="", const std::string &tessControlName="", const std::string &tessEvalName="")
Definition: ResourcesManager.cpp:598
static char * loadRawDataFromExternalFile(const std::string &path, size_t &size)
Definition: ResourcesManager.cpp:708
Resources & operator=(const Resources &)=delete
void getFiles(const std::string &extension, std::vector< FileInfos > &files) const
Definition: ResourcesManager.cpp:683
void addResources(const std::string &path)
Definition: ResourcesManager.cpp:33
static void saveStringToExternalFile(const std::string &path, const std::string &content)
Definition: ResourcesManager.cpp:751
static bool externalFileExists(const std::string &path)
Definition: ResourcesManager.cpp:761
std::unordered_map< std::string, Mesh > _meshes
Loaded meshes, identified by name.
Definition: ResourcesManager.hpp:297
Resources()=default
std::unordered_map< std::string, ProgramInfos > _progInfos
Additional info to support shader reloading.
Definition: ResourcesManager.hpp:301
static std::string loadStringFromExternalFile(const std::string &path)
Definition: ResourcesManager.cpp:725
std::unordered_map< std::string, Font > _fonts
Loaded font infos, identified by name.
Definition: ResourcesManager.hpp:298
std::unordered_map< std::string, Data > _blobs
Loaded binary blobs, identified by name.
Definition: ResourcesManager.hpp:299
Program * getProgramCompute(const std::string &name)
Definition: ResourcesManager.cpp:628
std::vector< std::string > getLayeredPaths(const std::string &name, const std::string &suffix)
Definition: ResourcesManager.cpp:159
std::string getString(const std::string &filename)
Definition: ResourcesManager.cpp:205
char * getRawData(const std::string &path, size_t &size)
Definition: ResourcesManager.cpp:199
Resources(Resources &&)=delete
std::unordered_map< std::string, std::string > _files
Listing of available files and their paths.
Definition: ResourcesManager.hpp:295
const Texture * getDefaultTexture(TextureShape shape)
Definition: ResourcesManager.cpp:542
std::string getImagePath(const std::string &name)
Definition: ResourcesManager.cpp:124
void parseArchive(const std::string &archivePath)
Definition: ResourcesManager.cpp:39
Font * getFont(const std::string &name)
Definition: ResourcesManager.cpp:663
static Resources & manager()
Definition: ResourcesManager.cpp:20
Resources & operator=(Resources &&)=delete
Resources(const Resources &)=delete
const Data * getData(const std::string &filename)
Definition: ResourcesManager.cpp:223
void parseDirectory(const std::string &directoryPath)
Definition: ResourcesManager.cpp:76
Represents a texture containing one or more images, stored on the CPU and/or GPU.
Definition: Texture.hpp:12
TextureShape
The shape of a texture: dimensions, layers organisation.
Definition: GPUTypes.hpp:153
Layout
The layout of a texture: components count and type.
Definition: GPUTypes.hpp:225
Storage
Storage and loading options.
Definition: ResourcesManager.hpp:14
@ CPU
Store on the CPU.
@ FORCE_FRAME
For meshes, force computation of a local frame.
@ BOTH
Store on both the CPU and GPU.
Properties of a resource file.
Definition: ResourcesManager.hpp:251
std::string path
The file path.
Definition: ResourcesManager.hpp:252
std::string name
The file name, without the extension.
Definition: ResourcesManager.hpp:253
Definition: ResourcesManager.hpp:273
std::string fragmentName
Fragment shader filename.
Definition: ResourcesManager.hpp:289
std::string tessEvalName
Tessellation evaluation shader filename.
Definition: ResourcesManager.hpp:291
std::string vertexName
Vertex shader filename.
Definition: ResourcesManager.hpp:288
std::string tessContName
Tessellation control shader filename.
Definition: ResourcesManager.hpp:290
std::string computeName
Compute shader filename.
Definition: ResourcesManager.hpp:292