计算着色器均匀优化不正确吗?

时间:2018-12-19 13:55:42

标签: opengl compute-shader

获取统一的“统一球体球[10]”的位置(通过glGetUniformLocation(program,name))。即使程序(frustum_planes)中有其他有效的制服,也要返回-1。

平截头体平面和球体均在相同的地方使用,这使得两者中仅存在一个怪异。

任何想法可能出什么问题吗?

着色器

/usr/lib/x86_64-linux-gnu/libGLEW.so

正在调用glGetUniformLocation

#version 460 core
// Required by compute shaders apparently
// Process one object per shader invocation (optimisation?)
layout (local_size_x = 1) in;

struct Sphere {
    vec3 center;
    float radius;
};

uniform Sphere spheres[10];

// Plane defined as: Ax + By + Cz = D
uniform vec4 frustum_planes[6];

uint test(Sphere obj, vec4 plane) {
    const float distance = plane.x * obj.center.x + plane.y * obj.center.y + plane.z * obj.center.z + plane.w;
    if (distance < -obj.radius) {
        return 0; // Negative halfspace
    }
    return 1; // Positive halfspace or on the plane
}

/// Same as the OpenGL provided struct: DrawElementsIndirectCommand
struct DrawCommand {
    uint count; // Num elements (vertices)
    uint instanceCount; // Number of instances to draw (a.k.a primcount)
    uint firstIndex; // Specifies a byte offset (cast to a pointer type) into the buffer bound to GL_ELEMENT_ARRAY_BUFFER to start reading indices from.
    uint baseVertex; // Specifies a constant that should be added to each element of indices​ when chosing elements from the enabled vertex arrays.
    uint baseInstance; // Specifies the base instance for use in fetching instanced vertex attributes.
    // 20 bytes
    uint padding0;
    uint padding1;
    uint padding2;
    // 32 bytes (multiple of 16)
};

// One draw command per object 
// Command buffer backed by Shader Storage Object Buffer (SSBO)
layout(std140, binding = 0) writeonly buffer DrawCommandsBlock {
    DrawCommand draw_commands[];
};

void main() {
    const uint idx = gl_GlobalInvocationID.x;

    uint inside = 0; 
    for (int i = 0; i < 6; i++) {
        inside += test(spheres[idx], frustum_planes[i]) << i;
    }

    const uint INSIDE_ALL_PLANES = 63; // = 0b111111;
    const bool visible = inside == INSIDE_ALL_PLANES;
    draw_commands[idx].count = 25350; // sphere.indices.size();
    draw_commands[idx].instanceCount = visible ? 1 : 0; // This is the trick right here
    draw_commands[idx].baseInstance = 0;
    draw_commands[idx].baseVertex = 0;
    draw_commands[idx].padding0 = 0; // Avoid optimisation 
    draw_commands[idx].padding1 = 0;
    draw_commands[idx].padding2 = 0;
}

main.cpp

glGetUniformLocation(gl_state.cull_shader.gl_program, "spheres[0].center");

1 个答案:

答案 0 :(得分:5)

不可能查询不是基本类型或基本类型数组的制服的位置。 OpenGL Wiki指出:

  

自省API通常会在GLSL中使单个“变量”定义看起来像是多个变量。对于不是基本类型或基本类型数组的变量,可以执行此操作(请注意,在GLSL中,“基本类型”包括向量和矩阵类型)。

     

...

     

相反,作为基本类型的struct的每个子元素都是可见的。

这意味着您无法查询spheres(或spheres[0])。您要做的是分别查询每个结构的每个成员。为了获得第一个球体中心的位置,您必须查询spheres[0].center

请注意,查询frustum_planes可以正常工作,因为它是vec4数组(这是基本类型)。