1. Introduction
When we use OpenGL for image processing, such as texture mapping related programs, we must call the following functions:
1. Glteximage{1,2,3}d
2. Glcopyteximage{1,2,3}d
These functions often encounter a parameter of Internalformat, and also encounter two other parameters, format and type, which are not as intuitive as parameters such as width and height in the ideographic form. This article is a detailed introduction to all aspects of parameter internalformat, another article, "OpenGL pixel format" describes the other two parameters format and type. 2. Introduction
Where in OpenGL can you store images (image) as an image? The first thing we think about is:
1. Texture objects
2. Frame Cache Object
3. Pixel Buffer Object
These three kinds of objects are in the video card's memory, then there is another place is in RAM
4. An area in memory
We need to classify these objects, where 1 and 2 are for specific purposes, 1 are used as geometry maps, and 2 can be understood as the scenes we see on the screen. 3 and 4 are less useful, and they can be understood as warehouses that store pixel data.
Based on this understanding, then we need to explain what the data stored in 1 and 2 is in what format, RGB, RGBA, or otherwise, this is the subject of this article, the image format of the topic (images) [personal translation, Chinese accurate translation needs to be studied] 3. Detailed Introduction
Image Format (imageformat) Describes how images are stored in textures and frame caches, and defines the meaning of image data.
There are three types of image formats: color, depth, depth/template, all formats can be applied to texture storage units or frame cache storage units (unless otherwise specified) 3.1 color format
Color formats can be stored in 3 ways, normalized integers (normalized integers), floating-point numbers, and integers. The integral type and floating-point type are resolved into floating-point array in shader, and the integral type is parsed into integer data.
The normalized integer number can be divided into two categories, including the signed type and the unsigned type, the value range of the signed type is [ -1,1], the value range of the unsigned type is [0,1]
When OpenGL defines the color format, the syntax is as follows:
Gl_[components][size][type]
Components: Refers to the color component, OpenGL only allows the image format using the following 4 kinds: "R" "RG" "RGB" "RGBA", size defines the number of bits per component occupied (bit), type defines how the color format is used for storage, the values can include:
type takes value |
parameter Interpretation |
"" (empty) |
No suffix indicates the use of an unsigned normalized integer number |
"_snorm" |
Normalized signed integral type |
F |
Floating-point types, such as: gl_rgba32f means that each component (R\G\B\A) is a 32-bit floating-point number |
"I" |
A signed integer, such as gl_rgba8i, where each component is a number of integers at [-128,127] |
"UI" |
unsigned shaping number |
According to the above, if you need a 3-component unsigned integer format, and each component occupies 8 bits (1 bytes), the format used should be: Gl_rgb8ui
For each type of color format, there is a limit on the number of bits per component of the color:
format Type |
the number of bits per component |
unsigned normalized integral type |
2, 4, 5, 8, 10, 12, 16 |
Signed normalized integral type |
8, 16 |
Unsigned integral type |
8, 16, 32 |
Signed Integral type |
8, 16, 32 |
Floating point number |
16, 32 |
It is important to note that:
1. For cases where the number of components is 2 bits, only RGBA can be used, otherwise it is not allowed (such as you cannot use GL_RG2 format)
2. For the number of component bits is 4,5,12, only RGB and RGBA can be used, and other methods are not allowed
3. For cases where the number of components is 10, only RGB can be used, and other methods are not allowed (for example, you cannot use the GL_RGBA10 format
It is also possible to omit the number of bits occupied by the component during use, which only applies to unsigned normalized integer formats, and if omitted, OpenGL will help us select a number of digits. In general, it is best to specify a number of digits. 3.2 Special Color formats
The above is the general color format, OpenGL in addition to support some other color format, listed as follows:
Color Format |
Format Description |
Gl_r3_g3_b2 |
Normalized integral type, RG component occupies 3 bits, B component occupies 2 bits (exactly 1 bytes) |
Gl_rgb5_a1 |
The RGB components occupy 5 bits, the a component occupies 1 bits, and this format is typically used for compressed formats |
Gl_rgb10_a2 |
RGB components occupy 10 bits, and a component occupies 2 bits (exactly 4 bytes) |
Gl_rgb10_a2ui |
unsigned integer number, RGB occupies 10 bits, a occupies 2 bits |
gl_r11f_g11f_b10f |
Format of floating-point number types, minimizing storage space |
Gl_rgb9_e5 |
The format of floating-point types, which are computationally complex, are generally not used in frame buffer formats, and E is an identification of scientific notation |
3.3 Compressed Format
Texture compression is a very important way to reduce memory consumption, which is best used when texture compression is available. There are two types of compression formats in OpenGL: General-purpose compression and specific compression methods.
The implementation of the universal texture compression method relies on OpenGL's hardware driver, which is generally more free, preferably avoided.
The common texture compression format uses the following syntax:
Gl_compressed_components
Components can be set to red RG RGB RGBA SRGB Srgb_alpha
The compression format cannot be set to the format of the frame buffer 3.4 depth Format
There are two types of depth formats, normalized integer and floating-point number types, and the depth image format provides several:
Gl_depth_component16
gl_depth_component24
gl_depth_component32
gl_depth_component32f
3.4 Depth, template format
The depth template format blends depth and texture, allowing you to set the depth buffer while setting the template buffer.
If OpenGL provides a arb_stencil_texturing extension (OpenGL4.3) then the texture object can set a parameter to get the part of the template through the sampler.
This type of image format has only two types:
Gl_depth24_stencil8
Gl_depth32f_stencil8
3.6 Template Format
The template format is used to store a template value, the template value is generally unsigned integer, and all template format types use the gl_stencil_index# method, including
Gl_stencil_index1
gl_stencil_index4
gl_stencil_index8
gl_stencil_index16
3.7 commonly used image formats
The OpenGL specification is relatively loose in the image format implementation, but the specification also indicates that some formats must be supported, including some of the following formats: 3.7.1 supports textures and frame buffers
Basic Format |
Data Type |
the number of digits occupied by each component |
Rgba,rg,red |
Normalized integral type |
8,16 |
Rgba,rg,red |
Floating point Type |
16,32 |
Rgba,rg,red |
Unsigned integral type |
8,16,32 |
Rgba,rg,red |
Signed Integral type |
8,16,32 |
It also includes
GL_RGB10_A2
gl_rgb10_a2ui
gl_r11f_g11f_b10f
gl_srgb8_alpha8
gl_depth_component16
GL_DEPTH_ COMPONENT24
gl_depth_component32f
gl_depth24_stencil8
gl_depth32f_stencil8
Gl_stencil_ INDEX8 (4.3 supports frame buffers, 4.4 supports frame buffers and textures)
3.7.2 Support Textures only
The following formats must support textures, but the ability to support the frame buffer OpenGL specification does not enforce the requirement:
Basic Type |
Data Format |
number of digits occupied by the component |
Rgb |
unsigned normalized integral type |
8,16 |
Rgba,rgb,rg,red |
Signed normalized integral type |
8,16 |
Rgb |
Floating point |
16,32 |
Rgb |
Signed Integral type |
8,16,32 |
Rgb |
Unsigned integral type |
8,16,32 |
Rg,red |
Unsigned integral type |
Compressed with RGTC (TODO?) |
In addition, it includes:
Gl_srgb8
Gl_rgb9_e5
3.8 Image Format Query
Many of the properties of OpenGL's image format vary slightly depending on the implementation of OpenGL (this is largely due to OpenGL's looser requirements for image formats), and OpenGL provides a mechanism for querying the image format in 4.3 and above, using the following function to obtain the image format:
void Glgetinternalformativ (
glenum target,
glenum Internalformat,
glenum pname,
Glsizei bufSize,
glint *params);
void glgetinternalformati64v (
glenum target,
glenum Internalformat,
glenum pname,
Glsizei bufSize, C10/>glint64 *params);
3.9 Obsolete image formats
There are two types of color formats in OpenGL Legecy, luminance and intensity, which represent formats that typically have only two components (somewhat similar to red or RG), but they behave differently from red to RG. Embodies the following:
When gl_red is used in shader, VEC4 is (red,0,0,1)
Gl_intensity is (i,i,i,i), Gl_luminance is (l,l,l,1), Gl_luminance_alpha Yes (l,l,l,a)
The two formats are 8-bit and 16-bit:
Gl_intensity8,
gl_intensity16,
Gl_luminance8,
gl_luminance_alpha16
4. Other
It is relatively free to set the Internalformat of OpenGL, but generally, when you call a function that needs to set Internalformat, there are generally pixel format and type parameters that appear together, for example:
void Glteximage2d ( glenum target,
glint level,
glint Internalformat,
glsizei width,
glsizei Height,
glint border,
glenum format,
glenum type,
const glvoid * data);
In a nutshell, Internalformat defines the format and storage of pixels in the GPU, and the format and type and data of the three parameters together define how the pixels in memory are formatted and stored, and if we set these two formats differently, OpenGL internally will help us transform , which often leads to loss of performance, generally it is better to set the two more compatible, convenient OpenGL do data transmission, avoid conversion.
Reference:
Image Format
StackOverflow about Internalformat Discussion