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),
23 Framebuffer::Framebuffer():
24 FramebufferBackend(false),
30 Framebuffer::Framebuffer(FrameAttachment fa):
36 Framebuffer::Framebuffer(const FrameFormat &f):
42 void Framebuffer::set_format(const FrameFormat &fmt)
45 throw invalid_operation("Framebuffer::set_format");
46 if(fmt.empty() || !is_format_supported(fmt))
47 throw invalid_argument("Framebuffer::set_format");
50 attachments.resize(format.size());
53 void Framebuffer::update() const
55 FramebufferBackend::update(dirty);
59 void Framebuffer::check_size()
62 for(Attachment &a: attachments)
67 if(const Texture2D *tex2d = dynamic_cast<const Texture2D *>(a.tex))
69 w = max(tex2d->get_width()>>a.level, 1U);
70 h = max(tex2d->get_height()>>a.level, 1U);
72 else if(const Texture2DMultisample *tex2d_ms = dynamic_cast<const Texture2DMultisample *>(a.tex))
74 w = tex2d_ms->get_width();
75 h = tex2d_ms->get_height();
77 else if(const Texture3D *tex3d = dynamic_cast<const Texture3D *>(a.tex))
79 w = max(tex3d->get_width()>>a.level, 1U);
80 h = max(tex3d->get_height()>>a.level, 1U);
82 else if(const TextureCube *tex_cube = dynamic_cast<const TextureCube *>(a.tex))
84 w = max(tex_cube->get_size()>>a.level, 1U);
96 width = min(width, w);
97 height = min(height, h);
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 void Framebuffer::require_complete() const
174 bool layered = (!attachments.empty() && attachments.front().layer<0);
175 for(const Attachment &a: attachments)
178 throw framebuffer_incomplete("missing attachment");
179 if(layered!=(a.layer<0))
180 throw framebuffer_incomplete("inconsistent layering");
183 FramebufferBackend::require_complete();
187 void Framebuffer::Attachment::set(Texture &t, unsigned l, int z)
194 void Framebuffer::Attachment::clear()