Skip to content

Commit

Permalink
libgui|GLTexture|Image: More OpenGL pixel formats
Browse files Browse the repository at this point in the history
Added 32-bit float and integer formats. GLPixelFormat now specifies
both the internal format and the user data format.
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 61f114d commit 1862f62
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 56 deletions.
10 changes: 7 additions & 3 deletions doomsday/libs/gui/include/de/graphics/glpixelformat.h
Expand Up @@ -29,14 +29,18 @@ namespace de {
* @ingroup gl
*/
struct LIBGUI_PUBLIC GLPixelFormat {
duint internalFormat;
duint format;
duint type;
duint rowAlignment;

GLPixelFormat(duint glFormat, duint glDataType = 0, duint glRowAlignment = 0)
: format(glFormat)
GLPixelFormat(duint glInternalFormat, duint glFormat, duint glDataType = 0,
duint glRowAlignment = 0)
: internalFormat(glInternalFormat)
, format(glFormat)
, type(glDataType)
, rowAlignment(glRowAlignment) {}
, rowAlignment(glRowAlignment)
{}
};

} // namespace de
Expand Down
39 changes: 25 additions & 14 deletions doomsday/libs/gui/include/de/graphics/image.h
Expand Up @@ -50,22 +50,33 @@ class LIBGUI_PUBLIC Image : public ISerializable
* Supported GL-friendly formats. Note that all QImage formats cannot be
* uploaded to OpenGL.
*/
enum Format
{
Unknown = -1,
UseQImageFormat = 0, ///< May not be GL friendly.
enum Format {
Unknown = -1,
UseQImageFormat = 0, ///< May not be GL friendly.

Luminance_8 = 1,
Luminance_8 = 1,
LuminanceAlpha_88 = 2,
Alpha_8 = 3,
RGB_555 = 4,
RGB_565 = 5,
RGB_444 = 6,
RGB_888 = 7, ///< 24-bit depth.
RGBA_4444 = 8,
RGBA_5551 = 9,
RGBA_8888 = 10,
RGBx_8888 = 11 ///< 32-bit depth, alpha data ignored.
Alpha_8 = 3,
RGB_555 = 4,
RGB_565 = 5,
RGB_444 = 6,
RGB_888 = 7, ///< 24-bit depth.
RGBA_4444 = 8,
RGBA_5551 = 9,
RGBA_8888 = 10,
RGBx_8888 = 11, ///< 32-bit depth, alpha data ignored.
R_32f = 12,
RG_32f = 13,
RGB_32f = 14,
RGBA_32f = 15,
R_32i = 16,
RG_32i = 17,
RGB_32i = 18,
RGBA_32i = 19,
R_32ui = 20,
RG_32ui = 21,
RGB_32ui = 22,
RGBA_32ui = 23,
};

typedef Vector2ui Size;
Expand Down
38 changes: 21 additions & 17 deletions doomsday/libs/gui/src/graphics/gltexture.cpp
Expand Up @@ -180,34 +180,36 @@ DENG2_PIMPL(GLTexture)
void glImage(int level, Size const &size, GLPixelFormat const &glFormat,
void const *data, CubeFace face = PositiveX)
{
/// @todo GLES2: Check for the BGRA extension.

// Choose suitable informal format.
GLenum const internalFormat =
(glFormat.format == GL_BGRA? GL_RGBA :
glFormat.format == GL_DEPTH_STENCIL? GL_DEPTH24_STENCIL8 :
glFormat.format);

/*qDebug() << "glTexImage2D:" << name << (isCube()? glFace(face) : texTarget)
<< level << internalFormat << size.x << size.y << 0
<< glFormat.format << glFormat.type << data;*/

if (data) LIBGUI_GL.glPixelStorei(GL_UNPACK_ALIGNMENT, GLint(glFormat.rowAlignment));
LIBGUI_GL.glTexImage2D(isCube()? glFace(face) : texTarget,
level, internalFormat, size.x, size.y, 0,
glFormat.format, glFormat.type, data);

LIBGUI_GL.glTexImage2D(isCube() ? glFace(face) : texTarget,
level,
glFormat.internalFormat,
size.x,
size.y,
0,
glFormat.format,
glFormat.type,
data);
LIBGUI_ASSERT_GL_OK();
}

void glSubImage(int level, Vector2i const &pos, Size const &size,
GLPixelFormat const &glFormat, void const *data, CubeFace face = PositiveX)
{
if (data) LIBGUI_GL.glPixelStorei(GL_UNPACK_ALIGNMENT, GLint(glFormat.rowAlignment));
LIBGUI_GL.glTexSubImage2D(isCube()? glFace(face) : texTarget,
level, pos.x, pos.y, size.x, size.y,
glFormat.format, glFormat.type, data);

LIBGUI_GL.glTexSubImage2D(isCube() ? glFace(face) : texTarget,
level,
pos.x,
pos.y,
size.x,
size.y,
glFormat.format,
glFormat.type,
data);
LIBGUI_ASSERT_GL_OK();
}

Expand Down Expand Up @@ -402,7 +404,9 @@ void GLTexture::setUndefinedContent(CubeFace face, Size const &size, GLPixelForm

void GLTexture::setDepthStencilContent(Size const &size)
{
setUndefinedContent(size, GLPixelFormat(GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8));
setUndefinedContent(size, GLPixelFormat(GL_DEPTH24_STENCIL8,
GL_DEPTH_STENCIL,
GL_UNSIGNED_INT_24_8));
}

void GLTexture::setImage(Image const &image, int level)
Expand Down
100 changes: 78 additions & 22 deletions doomsday/libs/gui/src/graphics/image.cpp
Expand Up @@ -443,6 +443,26 @@ int Image::depth() const
case RGBx_8888:
return 32;

case R_32f:
case R_32i:
case R_32ui:
return 32;

case RG_32f:
case RG_32i:
case RG_32ui:
return 64;

case RGB_32f:
case RGB_32i:
case RGB_32ui:
return 96;

case RGBA_32f:
case RGBA_32i:
case RGBA_32ui:
return 128;

default:
return 0;
}
Expand Down Expand Up @@ -520,7 +540,7 @@ bool Image::isGLCompatible() const
return false;
}
}
return d->format >= Luminance_8 && d->format <= RGBx_8888;
return d->format >= Luminance_8 && d->format <= RGBA_32ui;
}

bool Image::canConvertToQImage() const
Expand Down Expand Up @@ -763,43 +783,79 @@ void Image::operator << (Reader &from)

GLPixelFormat Image::glFormat(Format imageFormat)
{
DENG2_ASSERT(imageFormat >= Luminance_8 && imageFormat <= RGBx_8888);
DENG2_ASSERT(imageFormat >= Luminance_8 && imageFormat <= RGBA_32ui);

switch (imageFormat)
{
case Luminance_8:
return GLPixelFormat(GL_LUMINANCE, GL_UNSIGNED_BYTE, 1);
return GLPixelFormat(GL_R8, GL_RED, GL_UNSIGNED_BYTE, 1);

case LuminanceAlpha_88:
return GLPixelFormat(GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, 2);
return GLPixelFormat(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, 2);

case Alpha_8:
return GLPixelFormat(GL_ALPHA, GL_UNSIGNED_BYTE, 1);
return GLPixelFormat(GL_R8, GL_ALPHA, GL_UNSIGNED_BYTE, 1);

case RGB_555:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_5_5_5_1, 2);
return GLPixelFormat(GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_5_5_1, 2);

case RGB_565:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2);
return GLPixelFormat(GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2);

case RGB_444:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_4_4_4_4, 2);
return GLPixelFormat(GL_RGB4, GL_RGB, GL_UNSIGNED_SHORT_4_4_4_4, 2);

case RGB_888:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_BYTE, 1);
return GLPixelFormat(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 1);

case RGBA_4444:
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2);
return GLPixelFormat(GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2);

case RGBA_5551:
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2);
return GLPixelFormat(GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, 2);

case RGBA_8888:
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_BYTE, 4);
return GLPixelFormat(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4);

default:
case RGBx_8888:
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_BYTE, 4);
return GLPixelFormat(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4);

case R_32f:
return GLPixelFormat(GL_R32F, GL_RED, GL_FLOAT, 4);

case RG_32f:
return GLPixelFormat(GL_RG32F, GL_RG, GL_FLOAT, 8);

case RGB_32f:
return GLPixelFormat(GL_RGB32F, GL_RGB, GL_FLOAT, 12);

case RGBA_32f:
return GLPixelFormat(GL_RGBA32F, GL_RGBA, GL_FLOAT, 16);

case R_32i:
return GLPixelFormat(GL_R32I, GL_RED, GL_INT, 4);

case RG_32i:
return GLPixelFormat(GL_RG32I, GL_RG, GL_INT, 8);

case RGB_32i:
return GLPixelFormat(GL_RGB32I, GL_RGB, GL_INT, 12);

case RGBA_32i:
return GLPixelFormat(GL_RGBA32I, GL_RGBA, GL_INT, 16);

case R_32ui:
return GLPixelFormat(GL_R32UI, GL_RED, GL_UNSIGNED_INT, 4);

case RG_32ui:
return GLPixelFormat(GL_RG32UI, GL_RG, GL_UNSIGNED_INT, 8);

case RGB_32ui:
return GLPixelFormat(GL_RGB32UI, GL_RGB, GL_UNSIGNED_INT, 12);

case RGBA_32ui:
return GLPixelFormat(GL_RGBA32UI, GL_RGBA, GL_UNSIGNED_INT, 16);
}
}

Expand All @@ -808,39 +864,39 @@ GLPixelFormat Image::glFormat(QImage::Format format)
switch (format)
{
case QImage::Format_Indexed8:
return GLPixelFormat(GL_LUMINANCE, GL_UNSIGNED_BYTE, 1);
return GLPixelFormat(GL_R8UI, GL_RED, GL_UNSIGNED_BYTE, 1);

case QImage::Format_RGB444:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_4_4_4_4, 2);
return GLPixelFormat(GL_RGB4, GL_RGB, GL_UNSIGNED_SHORT_4_4_4_4, 2);

case QImage::Format_ARGB4444_Premultiplied:
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2);
return GLPixelFormat(GL_RGBA4, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, 2);

case QImage::Format_RGB555:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_5_5_5_1, 2);
return GLPixelFormat(GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_5_5_1, 2);

case QImage::Format_RGB16:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2);
return GLPixelFormat(GL_RGB5, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, 2);

case QImage::Format_RGB888:
return GLPixelFormat(GL_RGB, GL_UNSIGNED_BYTE, 1);
return GLPixelFormat(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 1);

case QImage::Format_RGB32:
#if defined (DENG_OPENGL)
/// @todo Is GL_BGR in any GL standard spec? Check for EXT_bgra.
return GLPixelFormat(GL_BGR, GL_UNSIGNED_BYTE, 4);
return GLPixelFormat(GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE, 4);
#else
return GLPixelFormat(GL_BGRA8_EXT, GL_UNSIGNED_BYTE, 4);
#endif

case QImage::Format_ARGB32:
/// @todo Is GL_BGRA in any GL standard spec? Check for EXT_bgra.
return GLPixelFormat(GL_BGRA, GL_UNSIGNED_BYTE, 4);
return GLPixelFormat(GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, 4);

default:
break;
}
return GLPixelFormat(GL_RGBA, GL_UNSIGNED_BYTE, 4);
return GLPixelFormat(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, 4);
}

Image Image::solidColor(Color const &color, Size const &size)
Expand Down

0 comments on commit 1862f62

Please sign in to comment.