首页 > 解决方案 > 是否应该尽可能在顶点着色器中硬编码纹理坐标数组?

问题描述

所以我制作了一个体素引擎,我没有将纹理坐标数组传递给 VAO,而是在顶点着色器中对该数组进行硬编码:

const vec2 texcoords[4] = vec2[4](
    const vec2(1.0, 1.0),
    const vec2(1.0, 0.0),
    const vec2(0.0, 0.0),
    const vec2(0.0, 1.0)
);

相反,我将 vertexIndex 传递给 VAO(因此 vertexIndex 确定要使用该数组的哪个索引)。所以它为我节省了很多内存,我可以将每个体素的所有信息都放在单个 uint 中。但是我想知道这种方法有什么缺点,当内存利润不多时我应该使用它吗?

编辑: 这里是 chunk.vert 的代码:

#version 460 core

in uint vertex;
out vec2 pass_texcoord;

out float pass_faceLight;
uniform mat4 projection;
uniform mat4 view;
uniform uint texAtlasRowsCount;
uniform vec3 chunkPos;
    
#define vertexIndex (vertex & 0x600000) >> 21
#define xc (vertex & 0x3F)
#define yc ((vertex & 0xFC0) >> 6)
#define zc ((vertex & 0x3F000) >> 12)
#define light ((vertex & 0x1C0000) >> 18) * 0.2F
#define pos vec4(vec3(xc, yc, zc) + chunkPos, 1)
#define position projection * view * pos

const vec2 texcoords[4] = vec2[4](
    const vec2(1.0, 1.0),
    const vec2(1.0, 0.0),
    const vec2(0.0, 0.0),
    const vec2(0.0, 1.0)
);

void main() {
    uint row = ((vertex & 0x3F800000) >> 23) - 1;
    const uint col = row / texAtlasRowsCount;
    row %= texAtlasRowsCount;

    pass_texcoord = texcoords[vertexIndex];
    pass_texcoord.x += row;
    pass_texcoord.y += col;

    pass_texcoord /= texAtlasRowsCount;

    //float AmbientOclussion = float((vertex & 0xC0000000) >> 30);

    pass_faceLight = light;

    gl_Position = position;
}

标签: performanceopenglmemoryshader

解决方案


推荐阅读