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)
64 if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
66 w = max(tex2d->get_width()>>a.level, 1U);
67 h = max(tex2d->get_height()>>a.level, 1U);
69 else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
71 w = tex2d_ms->get_width();
72 h = tex2d_ms->get_height();
74 else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
76 w = max(tex3d->get_width()>>a.level, 1U);
77 h = max(tex3d->get_height()>>a.level, 1U);
78 l = (a.layer<0 ? tex3d->get_depth() : 1);
80 else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
82 w = max(tex_cube->get_size()>>a.level, 1U);
95 width = min(width, w);
96 height = min(height, h);
97 layers = min(layers, l);
102 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
104 if(format.empty() || attachments.empty())
105 throw invalid_operation("Framebuffer::attach");
107 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
108 throw incompatible_data("Framebuffer::attach");
111 for(FrameAttachment a: format)
115 attachments[i].set(tex, level, layer);
123 throw incompatible_data("Framebuffer::attach");
126 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
128 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
131 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
133 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
136 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
138 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
141 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
143 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
146 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
149 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
152 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
155 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
158 void Framebuffer::detach(FrameAttachment attch)
160 if(attachments.empty())
161 throw invalid_operation("Framebuffer::detach");
163 int i = format.index(attch);
166 attachments[i].clear();
172 const Texture *Framebuffer::get_attachment(FrameAttachment attch) const
174 if(attachments.empty())
177 int i = format.index(attch);
178 return (i>=0 ? attachments[i].tex : 0);
181 const Texture *Framebuffer::get_attachment(unsigned i) const
183 return (i<attachments.size() ? attachments[i].tex : 0);
186 void Framebuffer::require_complete() const
188 bool layered = (!attachments.empty() && attachments.front().layer<0);
189 for(const Attachment &a: attachments)
192 throw framebuffer_incomplete("missing attachment");
193 if(layered!=(a.layer<0))
194 throw framebuffer_incomplete("inconsistent layering");
197 FramebufferBackend::require_complete();
201 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
208 void Framebuffer::Attachment::clear()