X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fframebuffer.cpp;h=01a4107494387cac61ce8c46e90dd7234e07d5b4;hb=d6122b75b00c0889a54536b5cea971d08184af8f;hp=93a726868c040d6464382ead82c148d58ed20c08;hpb=cd5f37b066352119cf92d53d0001af7ff99be437;p=libs%2Fgl.git diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index 93a72686..01a41074 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -1,10 +1,13 @@ #include #include +#include +#include #include #include #include #include #include +#include #include "error.h" #include "framebuffer.h" #include "misc.h" @@ -18,73 +21,69 @@ using namespace std; namespace Msp { namespace GL { -void operator<<(LexicalConverter &conv, FramebufferStatus status) -{ - switch(status) - { - case FRAMEBUFFER_INCOMPLETE_ATTACHMENT: - conv.result("incomplete attachment"); - break; - case FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: - conv.result("missing attachment"); - break; - case FRAMEBUFFER_INCOMPLETE_DIMENSIONS: - conv.result("mismatched attachment dimensions"); - break; - case FRAMEBUFFER_INCOMPLETE_FORMATS: - conv.result("mismatched attachment formats"); - break; - case FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER: - conv.result("missing draw buffer attachment"); - break; - case FRAMEBUFFER_INCOMPLETE_READ_BUFFER: - conv.result("missing read buffer attachment"); - break; - case FRAMEBUFFER_INCOMPLETE_MULTISAMPLE: - conv.result("mismatched attachment sample counts"); - break; - case FRAMEBUFFER_INCOMPLETE_LAYER_COUNT: - conv.result("mismatched attachment layer counts"); - break; - case FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS: - conv.result("mismatched attachment layering"); - break; - case FRAMEBUFFER_UNSUPPORTED: - conv.result("unsupported"); - break; - default: - conv.result(lexical_cast(status, "%#x")); - break; - } -} - -framebuffer_incomplete::framebuffer_incomplete(FramebufferStatus status): - runtime_error(lexical_cast(status)) +framebuffer_incomplete::framebuffer_incomplete(const std::string &reason): + runtime_error(reason) { } Framebuffer::Framebuffer(unsigned i): id(i), - status(FRAMEBUFFER_COMPLETE), + status(GL_FRAMEBUFFER_COMPLETE), dirty(0) { if(id) throw invalid_argument("System framebuffer must have id 0"); + if(EXT_framebuffer_object) + { + int value; + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_BACK, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value); + if(value==GL_NONE) + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_FRONT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value); + if(value!=GL_NONE) + format = (format,COLOR_ATTACHMENT); + + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value); + if(value!=GL_NONE) + format = (format,DEPTH_ATTACHMENT); + + glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_STENCIL, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &value); + if(value!=GL_NONE) + format = (format,STENCIL_ATTACHMENT); + } + int view[4]; glGetIntegerv(GL_VIEWPORT, view); width = view[2]; height = view[3]; } -Framebuffer::Framebuffer(): - width(0), - height(0), - status(FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT), - dirty(0) +Framebuffer::Framebuffer() +{ + init(); +} + +Framebuffer::Framebuffer(FrameAttachment fa) +{ + init(); + set_format(fa); +} + +Framebuffer::Framebuffer(const FrameFormat &f) +{ + init(); + set_format(f); +} + +void Framebuffer::init() { static Require _req(EXT_framebuffer_object); + width = 0; + height = 0; + status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT; + dirty = 0; + if(ARB_direct_state_access) glCreateFramebuffers(1, &id); else @@ -97,44 +96,71 @@ Framebuffer::~Framebuffer() glDeleteFramebuffers(1, &id); } +void Framebuffer::set_format(const FrameFormat &fmt) +{ + if(!format.empty() || !id) + throw invalid_operation("Framebuffer::set_format"); + if(fmt.empty()) + throw invalid_argument("Framebuffer::set_format"); + + if(ARB_internalformat_query && ARB_internalformat_query2) + { + unsigned target = (fmt.get_samples()>1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D); + for(FrameAttachment a: fmt) + { + unsigned pf = get_gl_pixelformat(get_attachment_pixelformat(a)); + int supported = 0; + glGetInternalformativ(target, pf, GL_FRAMEBUFFER_RENDERABLE, 1, &supported); + if(supported!=GL_FULL_SUPPORT) + throw invalid_argument("Framebuffer::set_format"); + } + } + + format = fmt; + attachments.resize(format.size()); +} + void Framebuffer::update() const { vector color_bufs; - color_bufs.reserve(attachments.size()); - for(unsigned i=0; iget_target(); if(ARB_direct_state_access) { - if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0) - glNamedFramebufferTexture(id, attch.attachment, attch.tex->get_id(), attch.level); + if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE || attch.layer<0) + glNamedFramebufferTexture(id, gl_attach_point, attch.tex->id, attch.level); else - glNamedFramebufferTextureLayer(id, attch.attachment, attch.tex->get_id(), attch.level, attch.layer); + glNamedFramebufferTextureLayer(id, gl_attach_point, attch.tex->id, attch.level, attch.layer); } - else if(type==GL_TEXTURE_2D || type==GL_TEXTURE_2D_MULTISAMPLE) - glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, type, attch.tex->get_id(), attch.level); + else if(attch.tex->target==GL_TEXTURE_2D || attch.tex->target==GL_TEXTURE_2D_MULTISAMPLE) + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level); else if(attch.layer<0) - glFramebufferTexture(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level); - else if(type==GL_TEXTURE_2D_ARRAY) - glFramebufferTextureLayer(GL_FRAMEBUFFER, attch.attachment, attch.tex->get_id(), attch.level, attch.layer); - else if(type==GL_TEXTURE_3D) - glFramebufferTexture3D(GL_FRAMEBUFFER, attch.attachment, type, attch.tex->get_id(), attch.level, attch.layer); - else if(type==GL_TEXTURE_CUBE_MAP) - glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, TextureCube::enumerate_faces(attch.layer), attch.tex->get_id(), attch.level); + glFramebufferTexture(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level); + else if(attch.tex->target==GL_TEXTURE_2D_ARRAY) + glFramebufferTextureLayer(GL_FRAMEBUFFER, gl_attach_point, attch.tex->id, attch.level, attch.layer); + else if(attch.tex->target==GL_TEXTURE_3D) + glFramebufferTexture3D(GL_FRAMEBUFFER, gl_attach_point, attch.tex->target, attch.tex->id, attch.level, attch.layer); + else if(attch.tex->target==GL_TEXTURE_CUBE_MAP) + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, get_gl_cube_face(static_cast(attch.layer)), attch.tex->id, attch.level); } else if(ARB_direct_state_access) - glNamedFramebufferTexture(id, attch.attachment, 0, 0); + glNamedFramebufferTexture(id, gl_attach_point, 0, 0); else - glFramebufferTexture2D(GL_FRAMEBUFFER, attch.attachment, GL_TEXTURE_2D, 0, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, gl_attach_point, GL_TEXTURE_2D, 0, 0); } - if(attch.attachment>=COLOR_ATTACHMENT0 && attch.attachment<=COLOR_ATTACHMENT3) - color_bufs.push_back(attch.attachment); + if(gl_attach_point!=GL_DEPTH_ATTACHMENT && gl_attach_point!=GL_STENCIL_ATTACHMENT) + color_bufs.push_back(gl_attach_point); + + ++i; } if(color_bufs.size()>1) @@ -160,112 +186,127 @@ void Framebuffer::update() const } if(ARB_direct_state_access) - status = static_cast(glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER)); + status = glCheckNamedFramebufferStatus(id, GL_FRAMEBUFFER); else - status = static_cast(glCheckFramebufferStatus(GL_FRAMEBUFFER)); + status = glCheckFramebufferStatus(GL_FRAMEBUFFER); dirty = 0; } void Framebuffer::check_size() { - for(vector::iterator i=attachments.begin(); i!=attachments.end(); ++i) - if(i->tex) + bool first = true; + for(Attachment &a: attachments) + if(a.tex) { - GLenum type = i->tex->get_target(); - if(type==GL_TEXTURE_2D) + unsigned w = 0; + unsigned h = 0; + if(a.tex->target==GL_TEXTURE_2D) { - Texture2D *tex = static_cast(i->tex); - width = max(tex->get_width()>>i->level, 1U); - height = max(tex->get_height()>>i->level, 1U); + Texture2D *tex = static_cast(a.tex); + w = max(tex->get_width()>>a.level, 1U); + h = max(tex->get_height()>>a.level, 1U); } - else if(type==GL_TEXTURE_2D_MULTISAMPLE) + else if(a.tex->target==GL_TEXTURE_2D_MULTISAMPLE) { - Texture2DMultisample *tex = static_cast(i->tex); - width = tex->get_width(); - height = tex->get_height(); + Texture2DMultisample *tex = static_cast(a.tex); + w = tex->get_width(); + h = tex->get_height(); } - else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY) + else if(a.tex->target==GL_TEXTURE_3D || a.tex->target==GL_TEXTURE_2D_ARRAY) { - Texture3D *tex = static_cast(i->tex); - width = max(tex->get_width()>>i->level, 1U); - height = max(tex->get_height()>>i->level, 1U); + Texture3D *tex = static_cast(a.tex); + w = max(tex->get_width()>>a.level, 1U); + h = max(tex->get_height()>>a.level, 1U); } - else if(type==GL_TEXTURE_CUBE_MAP) + else if(a.tex->target==GL_TEXTURE_CUBE_MAP) { - width = max(static_cast(i->tex)->get_size()>>i->level, 1U); - height = width; + w = max(static_cast(a.tex)->get_size()>>a.level, 1U); + h = w; } - break; - } -} -unsigned Framebuffer::get_attachment_index(FramebufferAttachment attch) -{ - for(unsigned i=0; i1 && samples!=format.get_samples()) || (format.get_samples()==1 && samples)) + throw incompatible_data("Framebuffer::attach"); + + unsigned i = 0; + for(FrameAttachment a: format) + { + if(a==attch) + { + attachments[i].set(tex, level, layer); + dirty |= 1<=0) + { + attachments[i].clear(); + dirty |= 1<