Arm Community
  • Site
  • User
  • Site
  • Search
  • User
  • Groups
    • Arm Research
    • DesignStart
    • Education Hub
    • Graphics and Gaming
    • High Performance Computing
    • Innovation
    • Multimedia
    • Open Source Software and Platforms
    • Physical
    • Processors
    • Security
    • System
    • Software Tools
    • TrustZone for Armv8-M
    • 中文社区
  • Blog
    • Artificial Intelligence
    • Automotive
    • Healthcare
    • HPC
    • Infrastructure
    • Innovation
    • Internet of Things
    • Machine Learning
    • Mobile
    • Smart Homes
    • Wearables
  • Forums
    • All developer forums
    • IP Product forums
    • Tool & Software forums
    • Pelion IoT Platform
  • Activity
  • Support
    • Open a support case
    • Documentation
    • Downloads
    • Training
    • Arm Approved program
    • Arm Design Reviews
  • More
  • Cancel
Graphics and Gaming
  • Developer Community
  • Tools and Software
  • Graphics and Gaming
  • Jump...
  • Cancel
Graphics and Gaming
Graphics and Gaming blog OpenGL Pitfalls : Distorted textures when packing texels colours components as GL_UNSIGNED_SHORT
  • Blog
  • Graphics - Most active members
  • Forum
  • Videos & Files
  • Mentions
  • Sub-Groups
  • Tags
  • Jump...
  • Cancel
  • New
More blogs in Graphics and Gaming
  • Graphics and Gaming blog

Tell us what you think
Tags
  • glteximage2d
  • OpenGL
  • opengles
  • texture
  • texture_compression
Actions
  • RSS
  • More
  • Cancel
Related blog posts
Related forum threads

OpenGL Pitfalls : Distorted textures when packing texels colours components as GL_UNSIGNED_SHORT

Myy
Myy
September 21, 2016

I lost a few days wondering why some textures were completely distorted when loaded in OpenGL.

The thing is, they were only distorted when the colours components were packed as GL_UNSIGNED_SHORT_5_5_5_1 or GL_UNSIGNED_SHORT_4_4_4_4. When packing colour components as GL_UNSIGNED_BYTE (RGBA8888), the textures were loaded correctly.

Why ?

Since I'm using a small personal Ruby hack to generate raw textures from BMP with the desired colour packing, I really thought the problem was in the Ruby code. After verifying that the generated 4444 and 5551 textures were the exact counterpart of the working 8888 textures, and tracing the OpenGL glTexImage2D calls to be sure that the data were sent correctly, I wondered if a special parameter was to be passed to glTexImage2D after all.

Ok, maybe I missed something in the glTexImage2D manual...

Sure did...

width × height texels are read from memory, starting at location data. By default, these texels are taken from adjacent memory locations, except that after all width texels are read, the read pointer is advanced to the next four-byte boundary. The four-byte row alignment is specified by glPixelStorei with argument GL_UNPACK_ALIGNMENT, and it can be set to one, two, four, or eight bytes.

The solution

Either :

  • have textures with a width multiple of 4,
  • call glPixelStorei(GL_UNPACK_ALIGNMENT, 2); before calling glTexImage2D.

RTFM, as they always say !

Anonymous
Graphics and Gaming blog
  • Graphics and Gaming blog: Making Gaming Faster with Updatable Mali GPU Drivers and Android GPU Inspector

    mattyclarkson
    mattyclarkson
    This blog explains how developers can make their games run faster with updateable Mali GPU drivers and Android GPU Inspector
    • June 16, 2020
  • Graphics and Gaming blog: Launching Performance Analysis for Android CI

    Peter Harris
    Peter Harris
    Arm Mobile Studio 2020.1 launches with Professional Edition, enabling deployment of our Android game profiling tools into continuous integration systems.
    • June 11, 2020
  • Graphics and Gaming blog: Vulkan Samples: Bandwidth and Throughput Optimizations for Mobile

    José Emilio Muñoz-López
    José Emilio Muñoz-López
    Learn more about making bandwidth and throughput optimizations for mobile gaming through Arm's Vulkan samples.
    • June 9, 2020