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);
84 l = (a.layer<0 ? 6 : 1);
96 width = min(width, w);
97 height = min(height, h);
98 layers = min(layers, l);
103 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
105 if(format.empty() || attachments.empty())
106 throw invalid_operation("Framebuffer::attach");
108 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
109 throw incompatible_data("Framebuffer::attach");
112 for(FrameAttachment a: format)
116 attachments[i].set(tex, level, layer);
124 throw incompatible_data("Framebuffer::attach");
127 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
129 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
132 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
134 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
137 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
139 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
142 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
144 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
147 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
150 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
153 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
156 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
159 void Framebuffer::detach(FrameAttachment attch)
161 if(attachments.empty())
162 throw invalid_operation("Framebuffer::detach");
164 int i = format.index(attch);
167 attachments[i].clear();
173 const Texture *Framebuffer::get_attachment(FrameAttachment attch) const
175 if(attachments.empty())
178 int i = format.index(attch);
179 return (i>=0 ? attachments[i].tex : 0);
182 const Texture *Framebuffer::get_attachment(unsigned i) const
184 return (i<attachments.size() ? attachments[i].tex : 0);
187 void Framebuffer::require_complete() const
189 bool layered = (!attachments.empty() && attachments.front().layer<0);
190 for(const Attachment &a: attachments)
193 throw framebuffer_incomplete("missing attachment");
194 if(layered!=(a.layer<0))
195 throw framebuffer_incomplete("inconsistent layering");
198 FramebufferBackend::require_complete();
202 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
209 void Framebuffer::Attachment::clear()