AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
Framebuffer.h
    1
    2/*
    3 * AUI Framework - Declarative UI toolkit for modern C++20
    4 * Copyright (C) 2020-2025 Alex2772 and Contributors
    5 *
    6 * SPDX-License-Identifier: MPL-2.0
    7 *
    8 * This Source Code Form is subject to the terms of the Mozilla Public
    9 * License, v. 2.0. If a copy of the MPL was not distributed with this
   10 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   11 */
   12
   13#pragma once
   14
   15#include "AUI/Traits/values.h"
   16#include "glm/fwd.hpp"
   17#include <AUI/Common/AString.h>
   18#include <cstdint>
   19#include <glm/glm.hpp>
   20#include <AUI/Common/AMap.h>
   21#include <AUI/Common/AVector.h>
   22#include <AUI/Views.h>
   23#include <AUI/GL/gl.h>
   24
   25namespace gl {
   26    class API_AUI_VIEWS Framebuffer: public aui::noncopyable {
   27    public:
   28        
   29#if AUI_PLATFORM_IOS
   30        static constexpr auto DEFAULT_FB = 1;
   31#elif AUI_PLATFORM_LINUX
   32        static int DEFAULT_FB;
   33#else
   34        static constexpr auto DEFAULT_FB = 0;
   35#endif
   36
   37        class IRenderTarget {
   38        friend class Framebuffer;
   39        public:
   40            virtual ~IRenderTarget() = default;
   41
   42        protected:
   43            virtual void onFramebufferResize(glm::u32vec2 size) = 0;
   44            virtual void attach(Framebuffer& to, GLenum attachmentType) = 0;
   45        };
   46
   47        Framebuffer();
   48        Framebuffer(Framebuffer&& rhs) noexcept: mHandle(rhs.mHandle),
   49                                                 mSize(rhs.mSize),
   50                                                 mAttachedTargets(std::move(rhs.mAttachedTargets)),
   51                                                 mSupersamplingRatio(rhs.mSupersamplingRatio) {
   52            rhs.mHandle = 0;            
   53        }
   54        virtual ~Framebuffer();
   55
   56        void bind();
   57        void bindForRead();
   58        void bindForWrite();
   59        static void unbind();
   60        void resize(glm::u32vec2 newSize);
   61
   62        [[nodiscard]]
   63        std::uint32_t supersamlingRatio() const noexcept {
   64            return mSupersamplingRatio;
   65        }
   66
   67        void setSupersamplingRatio(std::uint32_t ratio) noexcept {
   68            mSupersamplingRatio = ratio;
   69        }
   70
   71        [[nodiscard]]
   72        glm::u32vec2 supersampledSize() const noexcept {
   73            return mSize * mSupersamplingRatio;
   74        }
   75
   76        [[nodiscard]]
   77        glm::u32vec2 size() const noexcept {
   78            return mSize;
   79        }
   80
   81        operator bool() const {
   82            return mHandle;
   83        }
   84        uint32_t getHandle() const {
   85            return mHandle;
   86        }
   87
   88        void attach(_<IRenderTarget> renderTarget, GLenum attachmentType /* = GL_COLOR_ATTACHEMNT0 */) {
   89            renderTarget->attach(*this, attachmentType);
   90            unbind();
   91            mAttachedTargets << std::move(renderTarget);
   92        }
   93
   94        Framebuffer& operator=(Framebuffer&& rhs) noexcept {
   95            if (mHandle == rhs.mHandle) {
   96                return *this;
   97            }
   98            std::swap(mHandle, rhs.mHandle);
   99            mSize = rhs.mSize;
  100            mSupersamplingRatio = rhs.mSupersamplingRatio;
  101            mAttachedTargets = std::move(rhs.mAttachedTargets);
  102            return *this;
  103        }
  104
  105        static gl::Framebuffer* current();
  106
  107        void bindViewport();
  108
  109    private:
  110        uint32_t mHandle = 0;
  111        std::uint32_t mSupersamplingRatio = 1;
  112        glm::u32vec2 mSize{0, 0};
  113        AVector<_<IRenderTarget>> mAttachedTargets;
  114    };
  115}
Definition Framebuffer.h:37
Definition Framebuffer.h:26
Forbids copy of your class.
Definition values.h:45