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