]> git.tdb.fi Git - libs/gl.git/blob - source/core/framebuffer.h
Make it possible to specify explicit clear values
[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 "frameformat.h"
7 #include "gl.h"
8 #include "texturecube.h"
9 #include <msp/gl/extensions/arb_geometry_shader4.h>
10 #include <msp/gl/extensions/ext_framebuffer_multisample.h>
11 #include <msp/gl/extensions/ext_framebuffer_object.h>
12 #include <msp/gl/extensions/nv_fbo_color_attachments.h>
13
14 namespace Msp {
15 namespace GL {
16
17 class Texture;
18 class Texture2D;
19 class Texture2DMultisample;
20 class Texture3D;
21 class WindowView;
22
23 enum BufferBits
24 {
25         COLOR_BUFFER_BIT   = GL_COLOR_BUFFER_BIT,
26         DEPTH_BUFFER_BIT   = GL_DEPTH_BUFFER_BIT,
27         STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
28 };
29
30 class framebuffer_incomplete: public std::runtime_error
31 {
32 public:
33         framebuffer_incomplete(const std::string &);
34         virtual ~framebuffer_incomplete() throw() { }
35 };
36
37 /**
38 Framebuffer objects can be used to perform offscreen rendering.  The most
39 common application is rendering to a texture, which can then be used for
40 fullscreen shader effects.
41
42 A framebuffer consist of a number of logical buffers, such as color and depth
43 buffers.  Textures can be attached to the logical buffers.  At least one image
44 must be attached for the framebuffer to be usable.
45
46 Requires the GL_EXT_framebuffer_object extension.  The blit functions require
47 the GL_EXT_framebuffer_blit extension.
48 */
49 class Framebuffer
50 {
51         friend class Commands;
52         friend class PipelineState;
53
54 private:
55         struct Attachment
56         {
57                 Texture *tex;
58                 unsigned level;
59                 int layer;
60
61                 Attachment();
62                 void set(Texture &, unsigned, int);
63                 void clear();
64         };
65
66         unsigned id;
67         FrameFormat format;
68         std::vector<Attachment> attachments;
69         unsigned width;
70         unsigned height;
71         mutable unsigned status;
72         mutable unsigned dirty;
73
74         Framebuffer(unsigned);
75 public:
76         /** Creates an empty framebuffer.  Format must be set before textures can
77         be attached. */
78         Framebuffer();
79
80         /** Creates a framebuffer and sets its format to a single attachment. */
81         Framebuffer(FrameAttachment);
82
83         /** Creates a framebuffer and sets its format. */
84         Framebuffer(const FrameFormat &);
85
86 private:
87         void init();
88 public:
89         ~Framebuffer();
90
91         /** Sets the format of the framebuffer.  Once the format is set, it can't
92         be changed. */
93         void set_format(const FrameFormat &);
94
95         const FrameFormat &get_format() const { return format; }
96
97         unsigned get_width() const { return width; }
98         unsigned get_height() const { return height; }
99
100 private:
101         void update() const;
102         void check_size();
103         void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
104 public:
105
106         /** Attaches a texture to the framebuffer.  Only the attachment point
107         portion of attch is considered; pixel format is ignored.  The framebuffer
108         must have a format and the format of the texture must match that defined
109         in the framebuffer for this attachment point. */
110         void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
111
112         void attach(FrameAttachment attch, Texture2DMultisample &tex);
113         void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
114         void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
115         void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
116         void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
117         void detach(FrameAttachment attch);
118
119         void resize(const WindowView &);
120
121         /** Ensures that the framebuffer is complete, throwing an exception if it
122         isn't. */
123         void require_complete() const;
124
125         void refresh() const { if(dirty) update(); }
126
127         void set_debug_name(const std::string &);
128
129         static Framebuffer &system();
130 };
131
132
133 union ClearValue
134 {
135         Color color;
136         struct
137         {
138                 float depth;
139                 int stencil;
140         } depth_stencil;
141
142         ClearValue(): color(0.0f, 0.0f, 0.0f, 0.0f) { }
143 };
144
145 inline BufferBits operator|(BufferBits a, BufferBits b)
146 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }
147
148 } // namespace GL
149 } // namespace Msp
150
151 #endif