|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "Renderer/Shader.hpp" |
| 4 | + |
| 5 | +#include <map> |
| 6 | +#include <memory> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +namespace libprojectM { |
| 10 | +namespace Renderer { |
| 11 | + |
| 12 | +/** |
| 13 | + * @brief Instance shader cache. |
| 14 | + * |
| 15 | + * Used to store shaders which only need to be compiled once per projectM instance. |
| 16 | + * Removes the need for the previously used static shader provider, and reduces |
| 17 | + * shader recompilation times. |
| 18 | + * |
| 19 | + * Shaders are accessed by arbitrary strings as keys. |
| 20 | + * |
| 21 | + * All cached shader programs will be properly deleted when the projectM instance is destroyed. |
| 22 | + * Classes storing a reference to a cached shader should ideally use an std::weak_ptr to do so. |
| 23 | + */ |
| 24 | +class ShaderCache |
| 25 | +{ |
| 26 | +public: |
| 27 | + /** |
| 28 | + * @brief Adds a new shader to the cache. |
| 29 | + * If the shader already exists, the existing cache entry it NOT replaced. |
| 30 | + * @param key The key to store the shader with. |
| 31 | + * @param shader A shared pointer to the shader program to be cached. |
| 32 | + */ |
| 33 | + void Insert(const std::string& key, const std::shared_ptr<Shader>& shader); |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Adds a new shader to the cache. |
| 37 | + * If the shader already exists, the existing cache entry it NOT replaced. |
| 38 | + * @param key The key to store the shader with. |
| 39 | + * @param shader A shared pointer to the shader program to be cached. |
| 40 | + */ |
| 41 | + void Insert(const std::string& key, std::shared_ptr<Shader>&& shader); |
| 42 | + |
| 43 | + /** |
| 44 | + * @brief Removes a shader from the cache. |
| 45 | + * If the key does not exist in the cache, this function will not do anything. |
| 46 | + * @param key The key of the sahder to be removed. |
| 47 | + */ |
| 48 | + void Remove(const std::string& key); |
| 49 | + |
| 50 | + /** |
| 51 | + * @brief Returns a cached shader. |
| 52 | + * @param key the key of the cached shader to be returned. |
| 53 | + * @return A shared pointer to the cached shader program, or nullptr if the key wasn't found. |
| 54 | + */ |
| 55 | + auto Get(const std::string& key) const -> std::shared_ptr<Shader>; |
| 56 | + |
| 57 | +private: |
| 58 | + std::map<std::string, std::shared_ptr<Shader>> m_cachedShaders; |
| 59 | +}; |
| 60 | + |
| 61 | +} // namespace Renderer |
| 62 | +} // namespace libprojectM |
0 commit comments