aboutsummaryrefslogtreecommitdiff
path: root/src/rendering/framebuffer.h
blob: cf1fbef1436c70ab87104db71295e04882fdd123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
80
#pragma once

#include "core/memory.h"
#include "texture.h"

#include <vector>

namespace Donut
{
    enum class FramebufferTextureFormat
    {
        None = 0,

        RGBA8,
        RED_INTEGER,

        DEPTH24STENCIL8,

        Depth = DEPTH24STENCIL8
    };

    struct FramebufferTextureSpecification
    {
        FramebufferTextureSpecification() = default;
        FramebufferTextureSpecification(FramebufferTextureFormat format)
            : texture_format(format) {}

        FramebufferTextureFormat texture_format = FramebufferTextureFormat::None;
    };

    struct FramebufferAttachmentSpecification
    {
        FramebufferAttachmentSpecification() = default;
        FramebufferAttachmentSpecification(std::initializer_list<FramebufferTextureSpecification> attachments)
            : attachments(attachments) {}

        std::vector<FramebufferTextureSpecification> attachments;
    };

    struct FramebufferSpecification
    {
        uint32_t Width = 0, Height = 0;
        FramebufferAttachmentSpecification attachments;
        uint32_t Samples = 1;

        bool SwapChainTarget = false;
    };

    class Framebuffer
    {
    public:
        Framebuffer(const FramebufferSpecification& spec);
        ~Framebuffer();

        auto bind() -> void;
        auto unbind() -> void;

        auto resize(uint32_t width, uint32_t height) -> void;
        auto read_pixel(uint32_t attachment_index, int x, int y) -> int;

        auto clear_attachment(uint32_t attachment_index, int value) -> void;
        auto get_color_attachment_renderer_id(uint32_t index = 0) const -> uint32_t { return m_color_attachments[index]; }

        auto get_specification() const -> const FramebufferSpecification& { return m_specification; }

        static auto create(const FramebufferSpecification& spec) -> Ref<Framebuffer>;

    private:
        auto invalidate() -> void;

        uint32_t m_renderer_id = 0;
        FramebufferSpecification m_specification;

        std::vector<FramebufferTextureSpecification> m_color_attachment_specifications;
        FramebufferTextureSpecification m_depth_attachment_specification = FramebufferTextureFormat::None;

        std::vector<uint32_t> m_color_attachments;
        uint32_t m_depth_attachment = 0;
    };
};