]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Make various enums use uint8_t as their underlying type
[libs/gl.git] / source / core / framebuffer.h
1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
3
4 #include <vector>
5 #include "color.h"
6 #include "framebuffer_backend.h"
7 #include "frameformat.h"
8 #include "texturecube.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class Texture2D;
14 class Texture2DMultisample;
15 class Texture3D;
16 class WindowView;
17
18 class framebuffer_incomplete: public std::runtime_error
19 {
20 public:
21         framebuffer_incomplete(const std::string &);
22         virtual ~framebuffer_incomplete() throw() { }
23 };
24
25 /**
26 Uses one or more textures as buffers to draw into.  Framebuffers can contain
27 multiple color buffers to match multiple outputs from a fragment shader, but
28 only one depth and stencil buffer.
29
30 RenderTarget provides a higher-level interface which manages the textures as
31 well as the framebuffer itself.
32 */
33 class Framebuffer: public FramebufferBackend
34 {
35         friend FramebufferBackend;
36
37 protected:
38         struct Attachment
39         {
40                 Texture *tex = 0;
41                 unsigned level = 0;
42                 int layer = 0;
43
44                 void set(Texture &, unsigned, int);
45                 void clear();
46         };
47
48         FrameFormat format;
49         std::vector<Attachment> attachments;
50         unsigned width = 0;
51         unsigned height = 0;
52         unsigned layers = 0;
53         mutable unsigned dirty = 0;
54
55         Framebuffer(bool);
56 public:
57         /** Creates an empty framebuffer.  Format must be set before textures can
58         be attached. */
59         Framebuffer();
60
61         /** Creates a framebuffer and sets its format to a single attachment. */
62         Framebuffer(FrameAttachment);
63
64         /** Creates a framebuffer and sets its format. */
65         Framebuffer(const FrameFormat &);
66
67         /** Sets the format of the framebuffer.  Once the format is set, it can't
68         be changed. */
69         void set_format(const FrameFormat &);
70
71         const FrameFormat &get_format() const { return format; }
72
73         unsigned get_width() const { return width; }
74         unsigned get_height() const { return height; }
75         unsigned get_layers() const { return layers; }
76
77 protected:
78         void update() const;
79         void check_size();
80         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
81
82 public:
83         /** Attaches a texture to the framebuffer.  Only the attachment point
84         portion of attch is considered; pixel format is ignored.  The framebuffer
85         must have a format and the format of the texture must match that defined
86         in the framebuffer for this attachment point. */
87         void attach(FrameAttachment attch, Texture2D &, unsigned level = 0);
88
89         void attach(FrameAttachment attch, Texture2DMultisample &);
90
91         /** Attaches a single layer from a 3-dimensional texture to the
92         framebuffer. */
93         void attach(FrameAttachment attch, Texture3D &, unsigned layer, unsigned level = 0);
94
95         void attach(FrameAttachment attch, TextureCube &, TextureCubeFace face, unsigned level = 0);
96
97         /** Attaches a layered texture to the framebuffer.  Shaders can direct
98         output to a particular layer. */
99         void attach_layered(FrameAttachment attch, Texture3D &, unsigned level = 0);
100
101         void attach_layered(FrameAttachment attch, TextureCube &, unsigned level = 0);
102         void detach(FrameAttachment attch);
103
104         const Texture *get_attachment(FrameAttachment) const;
105         const Texture *get_attachment(unsigned) const;
106
107         /** Ensures that the framebuffer is complete, throwing an exception if it
108         isn't. */
109         void require_complete() const;
110
111         void refresh() const { if(dirty) update(); }
112
113         using FramebufferBackend::set_debug_name;
114 };
115
116
117 union ClearValue
118 {
119         Color color;
120         struct
121         {
122                 float depth;
123                 int stencil;
124         } depth_stencil;
125
126         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
127 };
128
129 } // namespace GL
130 } // namespace Msp
131
132 #endif