Program Listing for File GPU.hpp

↰ Return to documentation for file (engine/include/Cacao/GPU.hpp)

#pragma once

#include <future>
#include <memory>

#include "DllHelper.hpp"
#include "Camera.hpp"
#include "Material.hpp"
#include "Mesh.hpp"
#include "Transform.hpp"

#include "glm/glm.hpp"
#include "libcacaoasset.hpp"

namespace Cacao {
    class CACAO_API CommandBuffer {
      public:
        static std::unique_ptr<CommandBuffer> Create();

        CommandBuffer(const CommandBuffer&) = delete;
        CommandBuffer& operator=(const CommandBuffer&) = delete;
        CommandBuffer(CommandBuffer&&);
        CommandBuffer& operator=(CommandBuffer&&);

        virtual void DrawMesh([[maybe_unused]] std::shared_ptr<Mesh> mesh, [[maybe_unused]] std::shared_ptr<Material> material, [[maybe_unused]] Transform transform) {}

        virtual ~CommandBuffer() {};

      protected:
        CommandBuffer() {}

        virtual void Execute() {};

        friend class GPUManager;
        friend class PALModule;

        virtual bool SetupContext([[maybe_unused]] bool rendering = false) {
            return true;
        }
        virtual void StartRendering([[maybe_unused]] glm::vec3 clearColor) {}
        virtual void EndRendering() {}
        virtual void UpdateEngineData([[maybe_unused]] std::shared_ptr<Camera> cam, [[maybe_unused]] bool worldRefresh) {}
    };

    class CACAO_API GPUManager {
      public:
        static GPUManager& Get();

        GPUManager(const GPUManager&) = delete;
        GPUManager(GPUManager&&) = delete;
        GPUManager& operator=(const GPUManager&) = delete;
        GPUManager& operator=(GPUManager&&) = delete;

        void Start();

        void Stop();

        bool IsRunning() const {
            return running;
        }

        std::shared_future<void> Submit(std::unique_ptr<CommandBuffer> cmd);

        void SetVSync(bool newState);

        bool IsVSynced();

        unsigned int GetCurrentFPS();

        enum class Phase {
            Opaque,
            Transparent
        };

        xg::Guid RegisterRenderingCallback(std::function<void(std::unique_ptr<CommandBuffer>&)> callback, Phase phase, bool runPost);

        void UnregisterRenderingCallback(const xg::Guid& callbackGUID);

        class Impl;
      private:
        std::unique_ptr<Impl> impl;
        friend class ImplAccessor;
        friend class PAL;

        bool running;

        GPUManager();
        ~GPUManager();
    };
}