11.08., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

texturemanager.h 2.47 KB
Newer Older
Jakob Weiss's avatar
Jakob Weiss committed
1 2 3 4
#ifndef TEXTUREMANAGER_H__
#define TEXTUREMANAGER_H__

#include "cgt/cgt_gl.h"
Jakob Weiss's avatar
Jakob Weiss committed
5
#include "cgt/runnable.h"
Jakob Weiss's avatar
Jakob Weiss committed
6 7 8 9
#include "cgt/singleton.h"
#include "cgt/vector.h"

#include <map>
Jakob Weiss's avatar
Jakob Weiss committed
10
#include <mutex>
Jakob Weiss's avatar
Jakob Weiss committed
11 12 13 14
#include <tuple>

namespace cgt {

Jakob Weiss's avatar
Jakob Weiss committed
15
    class TextureManager : public Singleton<TextureManager>, public RunnableWithConditionalWait {
Jakob Weiss's avatar
Jakob Weiss committed
16 17 18 19 20
    public:
        TextureManager();

        ~TextureManager();

Jakob Weiss's avatar
Jakob Weiss committed
21 22 23 24 25 26 27 28 29 30 31
        /**
         *	Forces the manager to perform a (synchronous) garbage collection. Can be called manually by user code if
         *	GPU memory seems low. This method is threadsafe.
         */
        void forceGarbageCollection();

        /**
         *  Forces the texture pool to be cleared synchronously. This method is threadsafe.
         */
        void forceClearPool();

Jakob Weiss's avatar
Jakob Weiss committed
32 33 34 35
        GLuint generateId(GLenum type, const cgt::svec3& dimensions, GLenum internalFormat);

        void releaseId(GLuint id, GLenum type, const cgt::svec3& dimensions, GLenum internalFormat);

Jakob Weiss's avatar
Jakob Weiss committed
36 37 38
        /// \see Runnable::run()
        void run() override;

Jakob Weiss's avatar
Jakob Weiss committed
39
    private:
Jakob Weiss's avatar
Jakob Weiss committed
40 41 42 43 44 45 46 47 48 49 50 51
        static const std::string loggerCat_;
        using cache_key_t = std::tuple<GLenum, size_t, size_t, size_t, GLenum>;
        enum CacheEntryTupleIndex {
            TEX_TYPE = 0,
            SIZE_X = 1,
            SIZE_Y = 2,
            SIZE_Z = 3,
            INTERNAL_FORMAT = 4
        };

        /// GC will be performed regularly at this interval, in [ms]
        const size_t garbageCollectionDelayMs = 15000;
Jakob Weiss's avatar
Jakob Weiss committed
52

Jakob Weiss's avatar
Jakob Weiss committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        /// Compute the memory size from the cache entry key
        static constexpr size_t textureByteSize(const cache_key_t& key);

        /// Convert the texture type to a humanly readable string
        static std::string textureTypeToStr(GLenum tt);

        /**
         *	Performs a garbage collection event. This method is NOT threadsafe.
         */
        void performGarbageCollection();

        /**
         *	Clears the current texture pool. This method is NOT threadsafe.
         */
        void clearPool();

        /// the actual texture pool containing all textures not in use
        std::multimap<cache_key_t, GLuint> texturePool_;

        /// Currently allocated GPU memory from textures still in use
        size_t activeAllocatedMemory_;

        /// Allocated GPU memory from textures in the texture pool
        size_t texturePoolMemory_;

        /// Mutex ensuring thread safety 
        std::mutex accessMutex_;
Jakob Weiss's avatar
Jakob Weiss committed
80 81 82 83 84 85 86
    };

}

#define TexMgr cgt::Singleton<cgt::TextureManager>::getRef()

#endif // TEXTUREMANAGER_H__