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