-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

3D Graphics Rendering Cookbook
By :

One significant drawback of high-resolution textured data is that it requires a lot of GPU memory to store and process. All modern, real-time rendering APIs provide some sort of texture compression, allowing us to store textures in compressed formats on the GPU. One such format is ETC2. This is a standard texture compression format for OpenGL and Vulkan.
Etc2Comp is an open source tool that converts bitmaps into ETC2 format. The tool is built with a focus on encoding performance to reduce the amount of time required to package asset-heavy applications and reduce the overall application size. In this recipe, you will learn how to integrate this tool within your own applications to construct tools for your custom graphics preparation pipelines.
The project can be downloaded using this Bootstrap snippet:
{ "name": "etc2comp", "source": { "type": "git", "url": "https://github.com/google/etc2comp", "revision": "9cd0f9cae0f32338943699bb418107db61bb66f2" } }
Etc2Comp was released by Google "as-is" with the intention of being used as a command-line tool. Therefore, we will need to include some additional .cpp
files within the CMake target to use it as a library that is linked to our application. The Chapter2/08_ETC2Comp/CMakeLists.txt
file contains the following lines to do this:
target_sources(Ch2_Sample08_ETC2Comp PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/../../../ deps/src/etc2comp/EtcTool/EtcFile.cpp) target_sources(Ch2_Sample08_ETC2Comp PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/../../../ deps/src/etc2comp/EtcTool/EtcFileHeader.cpp)
The complete source code is located at Chapter2/08_ETC2Comp
.
Let's build an application that loads a .jpg
image via the STB library, converts it into an ETC2 image, and saves it within the .ktx
file format:
#include "etc2comp/EtcLib/Etc/Etc.h" #include "etc2comp/EtcLib/Etc/EtcImage.h" #include "etc2comp/EtcLib/Etc/EtcFilter.h" #include "etc2comp/EtcTool/EtcFile.h" #define STB_IMAGE_IMPLEMENTATION #include <stb/stb_image.h>
int main() { int w, h, comp; const uint8_t* img = stbi_load( "data/ch2_sample3_STB.jpg", &w, &h, &comp, 4);
std::vector<float> rgbaf; for (int i = 0; i != w * h * 4; i+=4) { rgbaf.push_back(img[i+0] / 255.0f); rgbaf.push_back(img[i+1] / 255.0f); rgbaf.push_back(img[i+2] / 255.0f); rgbaf.push_back(img[i+3] / 255.0f); }
Note
Besides manual conversion, as mentioned earlier, STB can load 8-bit-per-channel images as floating-point images directly via the stbi_loadf()
API. However, it will do an automatic gamma correction. Use stbi_ldr_to_hdr_scale(1.0f)
and stbi_ldr_to_hdr_gamma(2.2f)
if you want to control the amount of gamma correction.
RGB8
. We will use the default BT.709
error metric minimization schema:const auto etcFormat = Etc::Image::Format::RGB8; const auto errorMetric = Etc::ErrorMetric::BT709; Etc::Image image(rgbaf.data(), w, h, errorMetric);
The encoder takes the number of threads as input. Let's use the value returned by thread::hardware_concurrency()
and 1024
as the number of concurrent jobs:
image.Encode(etcFormat, errorMetric, ETCCOMP_DEFAULT_EFFORT_LEVEL, std::thread::hardware_concurrency(), 1024);
.ktx
file format, which can store compressed texture data that is directly consumable by OpenGL. Etc2Comp provides the Etc::File
helper class to do this:Etc::File etcFile("image.ktx", Etc::File::Format::KTX, etcFormat, image.GetEncodingBits(), image.GetEncodingBitsBytes(), image.GetSourceWidth(), image.GetSourceHeight(), image.GetExtendedWidth(), image.GetExtendedHeight()); etcFile.Write(); return 0; }
The bitmap is saved within the image.ktx
file. It can be loaded into an OpenGL or Vulkan texture, which we will do in subsequent chapters.
Pico Pixel is a great tool that you can use to view .ktx
files and other texture formats (https://pixelandpolygon.com). It is freeware but not open source. There is an issues tracker that is publicly available on GitHub. You can find it at https://github.com/inalogic/pico-pixel-public/issues.
For those who want to jump into the latest state-of-the-art texture compression techniques, please refer to the Basis project from Binomial at https://github.com/BinomialLLC/basis_universal.