Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用';压缩纹理';在openGL中?_Opengl_Textures - Fatal编程技术网

如何使用';压缩纹理';在openGL中?

如何使用';压缩纹理';在openGL中?,opengl,textures,Opengl,Textures,在我们的项目中,我们同时加载了如此多的4K图像。 我们希望减少上传时间,这将纹理传输到GPU 我的GPU是GTX 1080 ti 我看到使用压缩纹理可以加快上传速度 首先我尝试“ASTC(自适应可伸缩纹理压缩)”,使用“马里纹理压缩工具”进行压缩。 我得到“压缩”文件。1~2mb jpg文件(2048*2048图像)变为456kb lower是我的加载器函数 struct astc_header { uint8_t magic[4]; uint8_t blockdim_x;

在我们的项目中,我们同时加载了如此多的4K图像。 我们希望减少上传时间,这将纹理传输到GPU

我的GPU是GTX 1080 ti

我看到使用压缩纹理可以加快上传速度

首先我尝试“ASTC(自适应可伸缩纹理压缩)”,使用“马里纹理压缩工具”进行压缩。 我得到“压缩”文件。1~2mb jpg文件(2048*2048图像)变为456kb

lower是我的加载器函数

  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;
}
    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比没有压缩的图像更快。错了吗? 我认为glTexImage2D的压缩速度比glTexImage2D慢。是吗

接下来,我用这个文档尝试ARB。

但它完全不起作用。。。。 保存的文件(压缩文件)只有4kb。原版是16MB。。。当然不行

对于使用压缩纹理的示例或教程,我不能很好地理解。
请帮帮我

由于传输到GPU的数据量要小得多,压缩负载应该明显更快,而不是更慢

最可能的问题是,特定总账调用的计时会给您带来错误。由于某种原因,驾驶员可能阻塞/让步。尝试用代码更改来计时整个加载部分。您的缓冲区对象可能意味着在一种情况下与另一种情况下有一些同步

另一个可能的问题是,是否启用了某种格式转换/其他功能,导致解压。因此,请检查它是否也在自动生成mipmap或从左上角到左下角[0,0]翻转命令,或者在驱动程序中执行某些质量选项(确保在NVidia控制面板中禁用/控制应用程序)