Browse Source

GL: store pixel size in the BufferImage instance.

All other image classes do that, and thus code generally assumes that
querying it is an immediate operation, not a monster switch over
hundreds of values. Plus this prepares for the future internal
representation that is just sizes + strides instead of the
overcomplicated PixelStorage madness.
pull/651/merge
Vladimír Vondruš 1 year ago
parent
commit
9a13a08dc7
  1. 12
      src/Magnum/GL/BufferImage.cpp
  2. 3
      src/Magnum/GL/BufferImage.h
  3. 19
      src/Magnum/GL/Test/BufferImageGLTest.cpp
  4. 1
      src/Magnum/GL/Test/BufferImageTest.cpp

12
src/Magnum/GL/BufferImage.cpp

@ -38,22 +38,22 @@ template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const Pixe
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const Magnum::PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Containers::ArrayView<const void> const data, const BufferUsage usage): BufferImage{storage, GL::pixelFormat(format), GL::pixelType(format), size, data, usage} {}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const PixelFormat format, const PixelType type, const VectorTypeFor<dimensions, Int>& size, Buffer&& buffer, const std::size_t dataSize) noexcept: _storage{storage}, _format{format}, _type{type}, _size{size}, _buffer{Utility::move(buffer)}, _dataSize{dataSize} {
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const PixelFormat format, const PixelType type, const VectorTypeFor<dimensions, Int>& size, Buffer&& buffer, const std::size_t dataSize) noexcept: _storage{storage}, _format{format}, _type{type}, _size{size}, _buffer{Utility::move(buffer)}, _pixelSize{pixelFormatSize(format, type)}, _dataSize{dataSize} {
CORRADE_ASSERT(Magnum::Implementation::imageDataSize(*this) <= dataSize, "GL::BufferImage::BufferImage(): data too small, got" << dataSize << "but expected at least" << Magnum::Implementation::imageDataSize(*this) << "bytes", );
}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const Magnum::PixelFormat format, const VectorTypeFor<dimensions, Int>& size, Buffer&& buffer, const std::size_t dataSize) noexcept: BufferImage{storage, GL::pixelFormat(format), GL::pixelType(format), size, Utility::move(buffer), dataSize} {}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const PixelFormat format, const PixelType type): _storage{storage}, _format{format}, _type{type}, _buffer{Buffer::TargetHint::PixelPack}, _dataSize{} {
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const PixelFormat format, const PixelType type): _storage{storage}, _format{format}, _type{type}, _buffer{Buffer::TargetHint::PixelPack}, _pixelSize{pixelFormatSize(format, type)}, _dataSize{} {
/* Not delegating to the (buffer&&, dataSize) constructor to avoid a size
assertion that'd happen with certain storage parameters */
}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(const PixelStorage storage, const Magnum::PixelFormat format): BufferImage{storage, GL::pixelFormat(format), GL::pixelType(format)} {}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(NoCreateT) noexcept: _format{PixelFormat::RGBA}, _type{PixelType::UnsignedByte}, _buffer{NoCreate}, _dataSize{} {}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(NoCreateT) noexcept: _format{PixelFormat::RGBA}, _type{PixelType::UnsignedByte}, _buffer{NoCreate}, _pixelSize{4}, _dataSize{} {}
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(BufferImage<dimensions>&& other) noexcept: _storage{Utility::move(other._storage)}, _format{Utility::move(other._format)}, _type{Utility::move(other._type)}, _size{Utility::move(other._size)}, _buffer{Utility::move(other._buffer)}, _dataSize{Utility::move(other._dataSize)} {
template<UnsignedInt dimensions> BufferImage<dimensions>::BufferImage(BufferImage<dimensions>&& other) noexcept: _storage{Utility::move(other._storage)}, _format{Utility::move(other._format)}, _type{Utility::move(other._type)}, _size{Utility::move(other._size)}, _buffer{Utility::move(other._buffer)}, _pixelSize{Utility::move(other._pixelSize)}, _dataSize{Utility::move(other._dataSize)} {
other._size = {};
}
@ -64,12 +64,11 @@ template<UnsignedInt dimensions> BufferImage<dimensions>& BufferImage<dimensions
swap(_type, other._type);
swap(_size, other._size);
swap(_buffer, other._buffer);
swap(_pixelSize, other._pixelSize);
swap(_dataSize, other._dataSize);
return *this;
}
template<UnsignedInt dimensions> UnsignedInt BufferImage<dimensions>::pixelSize() const { return pixelFormatSize(_format, _type); }
template<UnsignedInt dimensions> std::pair<VectorTypeFor<dimensions, std::size_t>, VectorTypeFor<dimensions, std::size_t>> BufferImage<dimensions>::dataProperties() const {
return Magnum::Implementation::imageDataProperties<dimensions>(*this);
}
@ -79,6 +78,7 @@ template<UnsignedInt dimensions> void BufferImage<dimensions>::setData(const Pix
_format = format;
_type = type;
_size = size;
_pixelSize = pixelFormatSize(format, type);
/* Keep the old storage if zero-sized nullptr buffer was passed */
if(data.data() == nullptr && data.size() == 0)

3
src/Magnum/GL/BufferImage.h

@ -286,7 +286,7 @@ template<UnsignedInt dimensions> class BufferImage {
*
* @see @ref Magnum::pixelFormatSize(), @ref GL::pixelFormatSize()
*/
UnsignedInt pixelSize() const;
UnsignedInt pixelSize() const { return _pixelSize; }
/** @brief Image size in pixels */
VectorTypeFor<Dimensions, Int> size() const { return _size; }
@ -374,6 +374,7 @@ template<UnsignedInt dimensions> class BufferImage {
PixelType _type;
Math::Vector<Dimensions, Int> _size;
Buffer _buffer;
UnsignedInt _pixelSize;
std::size_t _dataSize;
};

19
src/Magnum/GL/Test/BufferImageGLTest.cpp

@ -112,6 +112,7 @@ void BufferImageGLTest::construct() {
CORRADE_COMPARE(a.storage().alignment(), 1);
CORRADE_COMPARE(a.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(a.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(a.pixelSize(), 1);
CORRADE_COMPARE(a.size(), Vector2i(1, 3));
CORRADE_COMPARE(a.dataSize(), 3);
@ -136,6 +137,7 @@ void BufferImageGLTest::constructGeneric() {
CORRADE_COMPARE(a.storage().alignment(), 1);
CORRADE_COMPARE(a.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(a.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(a.pixelSize(), 1);
CORRADE_COMPARE(a.size(), Vector2i(1, 3));
CORRADE_COMPARE(a.dataSize(), 3);
@ -280,6 +282,7 @@ void BufferImageGLTest::constructBuffer() {
CORRADE_COMPARE(a.storage().alignment(), 1);
CORRADE_COMPARE(a.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(a.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(a.pixelSize(), 1);
CORRADE_COMPARE(a.size(), Vector2i(1, 3));
CORRADE_COMPARE(a.dataSize(), 3);
@ -310,6 +313,7 @@ void BufferImageGLTest::constructBufferGeneric() {
CORRADE_COMPARE(a.storage().alignment(), 1);
CORRADE_COMPARE(a.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(a.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(a.pixelSize(), 1);
CORRADE_COMPARE(a.size(), Vector2i(1, 3));
CORRADE_COMPARE(a.dataSize(), 3);
@ -421,8 +425,7 @@ void BufferImageGLTest::constructCompressedInvalidSize() {
}
void BufferImageGLTest::constructMove() {
const char data[4] = { 'a', 'b', 'c', 'd' };
BufferImage2D a{PixelFormat::Red, PixelType::UnsignedByte, {4, 1}, data, BufferUsage::StaticDraw};
BufferImage2D a{PixelFormat::RGB, PixelType::UnsignedByte, {4, 1}, "abcabcabcab", BufferUsage::StaticDraw};
const Int id = a.buffer().id();
MAGNUM_VERIFY_NO_GL_ERROR();
@ -434,10 +437,11 @@ void BufferImageGLTest::constructMove() {
CORRADE_COMPARE(a.size(), Vector2i());
CORRADE_COMPARE(b.storage().alignment(), 4);
CORRADE_COMPARE(b.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(b.format(), GL::PixelFormat::RGB);
CORRADE_COMPARE(b.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(b.pixelSize(), 3);
CORRADE_COMPARE(b.size(), Vector2i(4, 1));
CORRADE_COMPARE(b.dataSize(), 4);
CORRADE_COMPARE(b.dataSize(), 12);
CORRADE_COMPARE(b.buffer().id(), id);
const unsigned short data2[2*4] = { 1, 2, 3, 4, 5, 6, 7, 8 };
@ -453,10 +457,11 @@ void BufferImageGLTest::constructMove() {
CORRADE_COMPARE(b.size(), Vector2i(1, 2));
CORRADE_COMPARE(c.storage().alignment(), 4);
CORRADE_COMPARE(c.format(), GL::PixelFormat::Red);
CORRADE_COMPARE(c.format(), GL::PixelFormat::RGB);
CORRADE_COMPARE(c.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(c.pixelSize(), 3);
CORRADE_COMPARE(c.size(), Vector2i(4, 1));
CORRADE_COMPARE(c.dataSize(), 4);
CORRADE_COMPARE(c.dataSize(), 12);
CORRADE_COMPARE(c.buffer().id(), id);
CORRADE_VERIFY(std::is_nothrow_move_constructible<BufferImage2D>::value);
@ -556,6 +561,7 @@ void BufferImageGLTest::setData() {
CORRADE_COMPARE(a.format(), GL::PixelFormat::RGBA);
CORRADE_COMPARE(a.type(), PixelType::UnsignedShort);
CORRADE_COMPARE(a.size(), Vector2i(1, 2));
CORRADE_COMPARE(a.pixelSize(), 8);
CORRADE_COMPARE(a.dataSize(), 16);
/** @todo How to verify the contents in ES? */
@ -584,6 +590,7 @@ void BufferImageGLTest::setDataGeneric() {
CORRADE_COMPARE(a.format(), GL::PixelFormat::RGBA);
CORRADE_COMPARE(a.type(), PixelType::UnsignedShort);
CORRADE_COMPARE(a.size(), Vector2i(1, 2));
CORRADE_COMPARE(a.pixelSize(), 8);
CORRADE_COMPARE(a.dataSize(), 16);
/** @todo How to verify the contents in ES? */

1
src/Magnum/GL/Test/BufferImageTest.cpp

@ -56,6 +56,7 @@ void BufferImageTest::constructNoCreate() {
CORRADE_COMPARE(image.size(), Vector2i{});
CORRADE_COMPARE(image.format(), PixelFormat::RGBA);
CORRADE_COMPARE(image.type(), PixelType::UnsignedByte);
CORRADE_COMPARE(image.pixelSize(), 4);
CORRADE_COMPARE(image.dataSize(), 0);
}

Loading…
Cancel
Save