在我们的项目中,我们同时加载了许多4K图像。我们希望减少上传时间,从而将纹理传输到GPU。
我的GPU是GTX 1080 ti。
我看到使用压缩纹理使上传速度更快。
[首先,我尝试使用“ ASTC(自适应可伸缩纹理压缩)”,使用“马里纹理压缩工具”进行压缩。我得到“压缩”文件。 1〜2mb jpg文件(2048 * 2048图片)转到456kb。
下面是我的加载程序功能。
struct astc_header
{
uint8_t magic[4];
uint8_t blockdim_x;
uint8_t blockdim_y;
uint8_t blockdim_z;
uint8_t xsize[3]; // x-size = xsize[0] + xsize[1] + xsize[2]
uint8_t ysize[3]; // x-size, y-size and z-size are given in texels;
uint8_t zsize[3]; // block count is inferred
};
int suppress_progress_counter = 0;
int perform_srgb_transform = 0;
#define MAGIC_FILE_CONSTANT 0x5CA1AB13
float load_astc_file(const char *filename)
{
FILE *f = fopen(filename, "rb");
if (!f)
{
printf("Failed to open file %s\n", filename);
exit(1);
}
astc_header hdr;
size_t hdr_bytes_read = fread(&hdr, 1, sizeof(astc_header), f);
if (hdr_bytes_read != sizeof(astc_header))
{
fclose(f);
printf("Failed to read file %s\n", filename);
exit(1);
}
uint32_t magicval = hdr.magic[0] + 256 * (uint32_t)(hdr.magic[1]) + 65536 * (uint32_t)(hdr.magic[2]) + 16777216 * (uint32_t)(hdr.magic[3]);
if (magicval != MAGIC_FILE_CONSTANT)
{
fclose(f);
printf("File %s not recognized\n", filename);
exit(1);
}
int xdim = hdr.blockdim_x;
int ydim = hdr.blockdim_y;
int zdim = hdr.blockdim_z;
if (xdim < 3 || xdim > 12 || ydim < 3 || ydim > 12 || (zdim < 3 && zdim != 1) || zdim > 12)
{
fclose(f);
printf("File %s not recognized %d %d %d\n", filename, xdim, ydim, zdim);
exit(1);
}
int xsize = hdr.xsize[0] + 256 * hdr.xsize[1] + 65536 * hdr.xsize[2];
int ysize = hdr.ysize[0] + 256 * hdr.ysize[1] + 65536 * hdr.ysize[2];
int zsize = hdr.zsize[0] + 256 * hdr.zsize[1] + 65536 * hdr.zsize[2];
int xblocks = (xsize + xdim - 1) / xdim;
int yblocks = (ysize + ydim - 1) / ydim;
int zblocks = (zsize + zdim - 1) / zdim;
int size = xblocks * yblocks * zblocks * 16;
uint8_t *buffer = (uint8_t *)malloc(size);
if (!buffer)
{
fclose(f);
printf("Ran out of memory\n");
exit(1);
}
size_t bytes_to_read = xblocks * yblocks * zblocks * 16;
size_t bytes_read = fread(buffer, 1, bytes_to_read, f);
fclose(f);
if (bytes_read != bytes_to_read)
{
printf("Failed to read file %s\n", filename);
exit(1);
}
float compute_time;
glFinish();
CHECK_TIME_START;
if (xdim == 12 && ydim == 12) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_12x12_KHR, xsize, ysize, 0, size, buffer);
}
else if (xdim == 10 && ydim == 10) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_10x10_KHR, xsize, ysize, 0, size, buffer);
}
else if (xdim == 8 && ydim == 8) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_8x8_KHR, xsize, ysize, 0, size, buffer);
}
else if (xdim == 6 && ydim == 6) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_6x6_KHR, xsize, ysize, 0, size, buffer);
}
else if (xdim == 5 && ydim == 5) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_5x5_KHR, xsize, ysize, 0, size, buffer);
}
else if (xdim == 4 && ydim == 4) {
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_ASTC_4x4_KHR, xsize, ysize, 0, size, buffer);
}
glFinish();
CHECK_TIME_END(compute_time);
free(buffer);
return compute_time;
}
遗憾的是,它可以完美运行,但是上传时间比仅上传jpg文件长40倍。jpg-112毫秒,ASTC-4124毫秒...
下部是载入jpg功能的一部分。
glFinish();
CHECK_TIME_START;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA, GL_UNSIGNED_BYTE, data);
glFinish();
CHECK_TIME_END(compute_time);
在某些页面中,astc比没有压缩图像要快。这是不对的?我认为glCompressedTexImage2D比glTexImage2D慢。是吗?
接下来,我尝试使用本文档的ARB。https://www.oldunreal.com/editing/s3tc/ARB_texture_compression.pdf
但是它完全不起作用。保存的文件(压缩文件)仅为4kb。原来是16MB ...当然不起作用。
我无法使用压缩纹理的示例或教程。请帮助我!
压缩负载应该明显更快,而不会因为传输到GPU的数据量少得多而大大降低。
[最有可能的问题是,计时特定总帐电话给您一个错误。驾驶员可能出于某些原因在阻挡/屈服。尝试仅通过代码更改来对整个加载部分进行计时。您的缓冲对象可能意味着一种情况下与另一种情况下存在一些同步?
其他可能的问题是,是否启用了某种格式转换/其他功能,导致其解压缩。因此,检查它是否还在自动生成Mipmap或从上左下-左[0,0]翻转坐标,或驱动程序中是否有某些质量选项(确保已禁用/在NVidia控件中由应用程序控制)面板)。