]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Remove support for array size specialization from the engine as well
[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 private:
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;
51         unsigned height;
52         mutable unsigned dirty;
53
54         Framebuffer(bool);
55 public:
56         /** Creates an empty framebuffer.  Format must be set before textures can
57         be attached. */
58         Framebuffer();
59
60         /** Creates a framebuffer and sets its format to a single attachment. */
61         Framebuffer(FrameAttachment);
62
63         /** Creates a framebuffer and sets its format. */
64         Framebuffer(const FrameFormat &);
65
66         /** Sets the format of the framebuffer.  Once the format is set, it can't
67         be changed. */
68         void set_format(const FrameFormat &);
69
70         const FrameFormat &get_format() const { return format; }
71
72         unsigned get_width() const { return width; }
73         unsigned get_height() const { return height; }
74
75 private:
76         void update() const;
77         void check_size();
78         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
79
80 public:
81         /** Attaches a texture to the framebuffer.  Only the attachment point
82         portion of attch is considered; pixel format is ignored.  The framebuffer
83         must have a format and the format of the texture must match that defined
84         in the framebuffer for this attachment point. */
85         void attach(FrameAttachment attch, Texture2D &, unsigned level = 0);
86
87         void attach(FrameAttachment attch, Texture2DMultisample &);
88
89         /** Attaches a single layer from a 3-dimensional texture to the
90         framebuffer. */
91         void attach(FrameAttachment attch, Texture3D &, unsigned layer, unsigned level = 0);
92
93         void attach(FrameAttachment attch, TextureCube &, TextureCubeFace face, unsigned level = 0);
94
95         /** Attaches a layered texture to the framebuffer.  Shaders can direct
96         output to a particular layer. */
97         void attach_layered(FrameAttachment attch, Texture3D &, unsigned level = 0);
98
99         void attach_layered(FrameAttachment attch, TextureCube &, unsigned level = 0);
100         void detach(FrameAttachment attch);
101
102         void resize(const WindowView &);
103
104         /** Ensures that the framebuffer is complete, throwing an exception if it
105         isn't. */
106         void require_complete() const;
107
108         void refresh() const { if(dirty) update(); }
109
110         using FramebufferBackend::set_debug_name;
111
112         static Framebuffer &system();
113 };
114
115
116 union ClearValue
117 {
118         Color color;
119         struct
120         {
121                 float depth;
122                 int stencil;
123         } depth_stencil;
124
125         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
126 };
127
128 } // namespace GL
129 } // namespace Msp
130
131 #endif