1 #ifndef MSP_GL_FRAMEBUFFER_H_
2 #define MSP_GL_FRAMEBUFFER_H_
5 #include "frameformat.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>
18 class Texture2DMultisample;
22 enum FramebufferStatus
24 FRAMEBUFFER_INCOMPLETE_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
25 FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
26 FRAMEBUFFER_INCOMPLETE_DIMENSIONS = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT,
27 FRAMEBUFFER_INCOMPLETE_FORMATS = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT,
28 FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
29 FRAMEBUFFER_INCOMPLETE_READ_BUFFER = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
30 FRAMEBUFFER_INCOMPLETE_MULTISAMPLE = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
31 FRAMEBUFFER_INCOMPLETE_LAYER_COUNT = GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB,
32 FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS,
33 FRAMEBUFFER_UNSUPPORTED = GL_FRAMEBUFFER_UNSUPPORTED,
34 FRAMEBUFFER_COMPLETE = GL_FRAMEBUFFER_COMPLETE
39 COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
40 DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT,
41 STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT
44 class framebuffer_incomplete: public std::runtime_error
47 framebuffer_incomplete(FramebufferStatus);
48 virtual ~framebuffer_incomplete() throw() { }
52 Framebuffer objects can be used to perform offscreen rendering. The most
53 common application is rendering to a texture, which can then be used for
54 fullscreen shader effects.
56 A framebuffer consist of a number of logical buffers, such as color and depth
57 buffers. Textures can be attached to the logical buffers. At least one image
58 must be attached for the framebuffer to be usable.
60 Requires the GL_EXT_framebuffer_object extension. The blit functions require
61 the GL_EXT_framebuffer_blit extension.
73 void set(Texture &, unsigned, int);
79 std::vector<Attachment> attachments;
82 mutable FramebufferStatus status;
83 mutable unsigned dirty;
85 Framebuffer(unsigned);
87 /** Creates an empty framebuffer. Format must be set before textures can
91 /** Creates a framebuffer and sets its format to a single attachment. */
92 Framebuffer(FrameAttachment);
94 /** Creates a framebuffer and sets its format. */
95 Framebuffer(const FrameFormat &);
102 /** Sets the format of the framebuffer. Once the format is set, it can't
104 void set_format(const FrameFormat &);
106 const FrameFormat &get_format() const { return format; }
108 unsigned get_width() const { return width; }
109 unsigned get_height() const { return height; }
114 void set_attachment(FrameAttachment, Texture &, unsigned, int, unsigned);
117 /** Attaches a texture to the framebuffer. Only the attachment point
118 portion of attch is considered; pixel format is ignored. The framebuffer
119 must have a format and the format of the texture must match that defined
120 in the framebuffer for this attachment point. */
121 void attach(FrameAttachment attch, Texture2D &tex, unsigned level = 0);
123 void attach(FrameAttachment attch, Texture2DMultisample &tex);
124 void attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level = 0);
125 void attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level = 0);
126 void attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level = 0);
127 void attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level = 0);
128 void detach(FrameAttachment attch);
130 void resize(const WindowView &);
132 /** Returns FRAMEBUFFER_COMPLETE if the framebuffer is complete and can be
133 rendered to, or one of the error status codes otherwise. */
134 FramebufferStatus get_status() const { return status; }
136 /** Ensures that the framebuffer is complete, throwing an exception if it
138 void require_complete() const;
140 void refresh() const { if(dirty) update(); }
142 unsigned get_id() const { return id; }
144 void set_debug_name(const std::string &);
146 static Framebuffer &system();
149 inline BufferBits operator|(BufferBits a, BufferBits b)
150 { return static_cast<BufferBits>(static_cast<int>(a)|static_cast<int>(b)); }