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):
19 FramebufferBackend(s),
24 format = get_system_format();
25 get_system_size(width, height);
29 Framebuffer::Framebuffer():
30 FramebufferBackend(false),
36 Framebuffer::Framebuffer(FrameAttachment fa):
42 Framebuffer::Framebuffer(const FrameFormat &f):
48 void Framebuffer::set_format(const FrameFormat &fmt)
50 if(!format.empty() || !id)
51 throw invalid_operation("Framebuffer::set_format");
52 if(fmt.empty() || !is_format_supported(fmt))
53 throw invalid_argument("Framebuffer::set_format");
56 attachments.resize(format.size());
59 void Framebuffer::update() const
61 FramebufferBackend::update(dirty);
65 void Framebuffer::check_size()
68 for(Attachment &a: attachments)
73 if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
75 w = max(tex2d->get_width()>>a.level, 1U);
76 h = max(tex2d->get_height()>>a.level, 1U);
78 else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
80 w = tex2d_ms->get_width();
81 h = tex2d_ms->get_height();
83 else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
85 w = max(tex3d->get_width()>>a.level, 1U);
86 h = max(tex3d->get_height()>>a.level, 1U);
88 else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
90 w = max(tex_cube->get_size()>>a.level, 1U);
102 width = min(width, w);
103 height = min(height, h);
108 void Framebuffer::set_attachment(FrameAttachment attch, Texture &tex, unsigned level, int layer, unsigned samples)
110 if(format.empty() || !id)
111 throw invalid_operation("Framebuffer::attach");
113 if((format.get_samples()>1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples))
114 throw incompatible_data("Framebuffer::attach");
117 for(FrameAttachment a: format)
121 attachments[i].set(tex, level, layer);
129 throw incompatible_data("Framebuffer::attach");
132 void Framebuffer::attach(FrameAttachment attch, Texture2D &tex, unsigned level)
134 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, 0, 0);
137 void Framebuffer::attach(FrameAttachment attch, Texture2DMultisample &tex)
139 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, 0, 0, tex.get_samples());
142 void Framebuffer::attach(FrameAttachment attch, Texture3D &tex, unsigned layer, unsigned level)
144 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, layer, 0);
147 void Framebuffer::attach(FrameAttachment attch, TextureCube &tex, TextureCubeFace face, unsigned level)
149 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, face, 0);
152 void Framebuffer::attach_layered(FrameAttachment attch, Texture3D &tex, unsigned level)
155 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
158 void Framebuffer::attach_layered(FrameAttachment attch, TextureCube &tex, unsigned level)
161 set_attachment(make_typed_attachment(attch, tex.get_format()), tex, level, -1, 0);
164 void Framebuffer::detach(FrameAttachment attch)
167 throw invalid_operation("Framebuffer::detach");
169 int i = format.index(attch);
172 attachments[i].clear();
178 void Framebuffer::resize(const WindowView &view)
181 throw invalid_operation("Framebuffer::resize");
183 width = view.get_width();
184 height = view.get_height();
187 void Framebuffer::require_complete() const
192 bool layered = (!attachments.empty() && attachments.front().layer<0);
193 for(const Attachment &a: attachments)
196 throw framebuffer_incomplete("missing attachment");
197 if(layered!=(a.layer<0))
198 throw framebuffer_incomplete("inconsistent layering");
201 FramebufferBackend::require_complete();
204 Framebuffer &Framebuffer::system()
206 static Framebuffer sys_framebuf(true);
211 Framebuffer::Attachment::Attachment():
217 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
224 void Framebuffer::Attachment::clear()