/*
 * Copyright (c) 2022 EKA2L1 Team.
 * 
 * This file is part of EKA2L1 project.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <drivers/graphics/common.h>
#include <drivers/itc.h>
#include <vector>

namespace eka2l1::drivers {
    class graphics_driver;
}

namespace eka2l1::dispatch {
    // General idea from PPSSPP (thanks!)
    struct graphics_buffer_pusher {
    private:
        struct buffer_info {
            drivers::handle buffer_;
            std::size_t used_size_;
            std::uint8_t *data_;
        };

        std::vector<buffer_info> buffers_;
        std::uint8_t current_buffer_;
        std::size_t size_per_buffer_;

        void add_buffer();

    public:
        explicit graphics_buffer_pusher();

        bool is_initialized() const {
            return (size_per_buffer_ != 0);
        }

        void initialize(const std::size_t size_per_buffer);
        void destroy(drivers::graphics_command_builder &builder);
        void done_frame();

        drivers::handle push_buffer(drivers::graphics_driver *drv, const std::uint8_t *data, const std::size_t buffer_size,
            std::size_t &buffer_offset);

        void flush(drivers::graphics_command_builder &builder);
    };
}