2 #include "framebuffer.h"
4 #include "texture2dmultisample.h"
6 #include "windowview.h"
13 framebuffer_incomplete::framebuffer_incomplete(const std::string &reason):
18 Framebuffer::Framebuffer(bool s):
22 Framebuffer::Framebuffer():
23 FramebufferBackend(false)
26 Framebuffer::Framebuffer(FrameAttachment fa):
32 Framebuffer::Framebuffer(const FrameFormat &f):
38 void Framebuffer::set_format(const FrameFormat &fmt)
41 throw invalid_operation("Framebuffer::set_format");
42 if(fmt.empty() || !is_format_supported(fmt))
43 throw invalid_argument("Framebuffer::set_format");
46 attachments.resize(format.size());
49 void Framebuffer::update() const
51 FramebufferBackend::update(dirty);
55 void Framebuffer::check_size()
58 for(Attachment &a: attachments)
63 if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
65 w = max(tex2d->get_width()>>a.level, 1U);
66 h = max(tex2d->get_height()>>a.level, 1U);
68 else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
70 w = tex2d_ms->get_width();
71 h = tex2d_ms->get_height();
73 else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
75 w = max(tex3d->get_width()>>a.level, 1U);
76 h = max(tex3d->get_height()>>a.level, 1U);
78 else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
80 w = max(tex_cube->get_size()>>a.level, 1U);
92 width = min(width, w);
93 height = min(height, h);
98 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
100 if(format.empty() || attachments.empty())
101 throw invalid_operation("Framebuffer::attach");
103 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
104 throw incompatible_data("Framebuffer::attach");
107 for(FrameAttachment a: format)
111 attachments[i].set(tex, level, layer);
119 throw incompatible_data("Framebuffer::attach");
122 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
124 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
127 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
129 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
132 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
134 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
137 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
139 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
142 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
145 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
148 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
151 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
154 void Framebuffer::detach(FrameAttachment attch)
156 if(attachments.empty())
157 throw invalid_operation("Framebuffer::detach");
159 int i = format.index(attch);
162 attachments[i].clear();
168 const Texture *Framebuffer::get_attachment(FrameAttachment attch) const
170 if(attachments.empty())
173 int i = format.index(attch);
174 return (i>=0 ? attachments[i].tex : 0);
177 const Texture *Framebuffer::get_attachment(unsigned i) const
179 return (i<attachments.size() ? attachments[i].tex : 0);
182 void Framebuffer::require_complete() const
184 bool layered = (!attachments.empty() && attachments.front().layer<0);
185 for(const Attachment &a: attachments)
188 throw framebuffer_incomplete("missing attachment");
189 if(layered!=(a.layer<0))
190 throw framebuffer_incomplete("inconsistent layering");
193 FramebufferBackend::require_complete();
197 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
204 void Framebuffer::Attachment::clear()