Browse Source

Use Containers::Array instead of naked allocation in *Framebuffer.

GL_NONE is fortunately zero, so we can skip std::fill_n altogether and
replace it with zero-initialized allocation. Added just-in-case
static_assert to check that.
pull/34/head
Vladimír Vondruš 13 years ago
parent
commit
14cff2bdb8
  1. 13
      src/DefaultFramebuffer.cpp
  2. 13
      src/Framebuffer.cpp

13
src/DefaultFramebuffer.cpp

@ -45,37 +45,32 @@ DefaultFramebuffer& DefaultFramebuffer::mapForDraw(std::initializer_list<std::pa
/* Create linear array from associative */ /* Create linear array from associative */
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[max+1]; static_assert(GL_NONE == 0, "Expecting zero GL_NONE for zero-initialization");
std::fill_n(_attachments, max, GL_NONE); auto _attachments = Containers::Array<GLenum>::zeroInitialized(max+1);
for(const auto& attachment: attachments) for(const auto& attachment: attachments)
_attachments[attachment.first] = GLenum(attachment.second); _attachments[attachment.first] = GLenum(attachment.second);
(this->*drawBuffersImplementation)(max+1, _attachments); (this->*drawBuffersImplementation)(max+1, _attachments);
delete[] _attachments;
return *this; return *this;
} }
#endif #endif
void DefaultFramebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments) { void DefaultFramebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments) {
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[attachments.size()]; Containers::Array<GLenum> _attachments(attachments.size());
for(std::size_t i = 0; i != attachments.size(); ++i) for(std::size_t i = 0; i != attachments.size(); ++i)
_attachments[i] = GLenum(*(attachments.begin()+i)); _attachments[i] = GLenum(*(attachments.begin()+i));
invalidateImplementation(attachments.size(), _attachments); invalidateImplementation(attachments.size(), _attachments);
delete[] _attachments;
} }
void DefaultFramebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments, const Rectanglei& rectangle) { void DefaultFramebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments, const Rectanglei& rectangle) {
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[attachments.size()]; Containers::Array<GLenum> _attachments(attachments.size());
for(std::size_t i = 0; i != attachments.size(); ++i) for(std::size_t i = 0; i != attachments.size(); ++i)
_attachments[i] = GLenum(*(attachments.begin()+i)); _attachments[i] = GLenum(*(attachments.begin()+i));
invalidateImplementation(attachments.size(), _attachments, rectangle); invalidateImplementation(attachments.size(), _attachments, rectangle);
delete[] _attachments;
} }
void DefaultFramebuffer::initializeContextBasedFunctionality(Context& context) { void DefaultFramebuffer::initializeContextBasedFunctionality(Context& context) {

13
src/Framebuffer.cpp

@ -94,36 +94,31 @@ Framebuffer& Framebuffer::mapForDraw(std::initializer_list<std::pair<UnsignedInt
/* Create linear array from associative */ /* Create linear array from associative */
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[max+1]; static_assert(GL_NONE == 0, "Expecting zero GL_NONE for zero-initialization");
std::fill_n(_attachments, max, GL_NONE); auto _attachments = Containers::Array<GLenum>::zeroInitialized(max+1);
for(const auto& attachment: attachments) for(const auto& attachment: attachments)
_attachments[attachment.first] = GLenum(attachment.second); _attachments[attachment.first] = GLenum(attachment.second);
(this->*drawBuffersImplementation)(max+1, _attachments); (this->*drawBuffersImplementation)(max+1, _attachments);
delete[] _attachments;
return *this; return *this;
} }
void Framebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments) { void Framebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments) {
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[attachments.size()]; Containers::Array<GLenum> _attachments(attachments.size());
for(std::size_t i = 0; i != attachments.size(); ++i) for(std::size_t i = 0; i != attachments.size(); ++i)
_attachments[i] = GLenum(*(attachments.begin()+i)); _attachments[i] = GLenum(*(attachments.begin()+i));
invalidateImplementation(attachments.size(), _attachments); invalidateImplementation(attachments.size(), _attachments);
delete[] _attachments;
} }
void Framebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments, const Rectanglei& rectangle) { void Framebuffer::invalidate(std::initializer_list<InvalidationAttachment> attachments, const Rectanglei& rectangle) {
/** @todo C++14: use VLA to avoid heap allocation */ /** @todo C++14: use VLA to avoid heap allocation */
GLenum* _attachments = new GLenum[attachments.size()]; Containers::Array<GLenum> _attachments(attachments.size());
for(std::size_t i = 0; i != attachments.size(); ++i) for(std::size_t i = 0; i != attachments.size(); ++i)
_attachments[i] = GLenum(*(attachments.begin()+i)); _attachments[i] = GLenum(*(attachments.begin()+i));
invalidateImplementation(attachments.size(), _attachments, rectangle); invalidateImplementation(attachments.size(), _attachments, rectangle);
delete[] _attachments;
} }
Framebuffer& Framebuffer::attachTexture2D(BufferAttachment attachment, Texture2D& texture, Int mipLevel) { Framebuffer& Framebuffer::attachTexture2D(BufferAttachment attachment, Texture2D& texture, Int mipLevel) {

Loading…
Cancel
Save