【问题标题】:GLSL: Removal of dead code causes visual errorsGLSL:删除死代码会导致视觉错误
【发布时间】:2013-05-03 17:31:21
【问题描述】:

我在尝试在 opengl 着色器中编写光线追踪器时遇到了很多奇怪的问题。我试图确定错误的根源是否是我自己,通常情况就是这样,但我得出的结论是,其中一些问题可能只是我的图形驱动程序中的错误(我在 ATI 上)。在这些情况下,我刚刚实施了解决方法。

但我只是遇到了一些我不确定如何解决的问题(至少没有真正奇怪的代码),事实上我无法将我的一些数据从统一数组切换到纹理缓冲区,因为那一刻我删除了对统一数组的引用(它不再做任何事情;我已经删除了数据的任何实际使用),我的着色器看起来像这样:

到这里:

请注意,移动相机也会导致您看到的内容发生不规律的变化。

这是我为获得这些结果而更改的方法:(有问题的行已被注释掉,它们访问统一数组 quad_vertex_indices)

bool Collide_KDTree(Ray ray, out Surface surface)
{
float t_entry, t_exit;

if(!RayBox(ray.tail, ray.head, scene_bounds.position, scene_bounds.extent, t_entry, t_exit))
    return false;

uint node_indices[TREE_DEPTH];
float node_exits[TREE_DEPTH];

uint top= 0;

node_indices[top]= kd_nodes.length()- 1;
node_exits[top++]= t_exit;

while(top> 0)
{
    uint node_index_foo= node_indices[top- 1];
    KDNode node= kd_nodes[node_indices[top- 1]];
    t_exit= node_exits[top- 1];
    top--;

    if(node.node_type== NodeType_Parent)
    {
        uint near_index, far_index;

        if(ray.tail[node.split_axis] < node.split)
        {
            near_index=  node.left_index_or_offset+ 1;
            far_index= node.right_index_or_count+ 1;
        }
        else
        {
            near_index=  node.right_index_or_count+ 1;
            far_index= node.left_index_or_offset+ 1;
        }

        float t_intersection;

        RayAxisAlignedPlane(ray.tail, ray.head, node.split_axis, node.split, t_intersection);

        if(t_intersection> t_exit)
        {
            node_indices[top]= near_index;
            node_exits[top++]= t_exit;
        }
        else if(t_intersection< t_entry)
        {
            if(t_intersection< 0)
            {
                node_indices[top]= near_index;
                node_exits[top++]= t_exit;
            }
            else
            {
                node_indices[top]= far_index;
                node_exits[top++]= t_exit;
            }
        }
        else if(t_intersection> t_entry && t_intersection< t_exit)
        {
            if(t_intersection< 0)
            {
                node_indices[top]= near_index;
                node_exits[top++]= t_exit;
            }
            else
            {
                node_indices[top]= far_index;
                node_exits[top++]= t_exit;

                node_indices[top]= near_index;
                node_exits[top++]= t_intersection;
            }
        }
    }
    else
    {
        float shortest_distance= INFINITY;
        bool collision_detected= false;

        uint primitive_offset= node.left_index_or_offset;
        uint primitive_count= node.right_index_or_count;

        for(uint i= primitive_offset; i< (primitive_offset+ primitive_count); i++)
        {
            uint primitive_index= primitive_indices[i];

            if(primitive_index< QUAD_COUNT)
            {
                uint quad_index= primitive_index;

                vec3 intersection;

                //uint foo0= quad_vertex_indices[quad_index* 4+ 0];
                //uint foo1= quad_vertex_indices[quad_index* 4+ 1];
                //uint foo2= quad_vertex_indices[quad_index* 4+ 2];
                //uint foo3= quad_vertex_indices[quad_index* 4+ 3];

                vec3 vertex0= vertices[texelFetch(test_texture_buffer, int(quad_index* 4+ 0)).r];
                vec3 vertex1= vertices[texelFetch(test_texture_buffer, int(quad_index* 4+ 1)).r];
                vec3 vertex2= vertices[texelFetch(test_texture_buffer, int(quad_index* 4+ 2)).r];
                vec3 vertex3= vertices[texelFetch(test_texture_buffer, int(quad_index* 4+ 3)).r];

                if(RayQuad(ray.tail, ray.head, vertex0, vertex1, vertex2, vertex3, quad_normals[quad_index], intersection))
                {
                    float this_distance= distance(ray.tail, intersection);

                    if(this_distance< shortest_distance)
                    {
                        surface.position= intersection;
                        surface.normal= quad_normals[quad_index];
                        surface.material= materials[quad_material_indices[quad_index]];

                        shortest_distance= this_distance;
                        collision_detected= true;
                    }
                }
            }
            else
            {
                uint sphere_index= primitive_index- QUAD_COUNT;

                vec3 intersection;

                if(RaySphere(ray.tail, ray.head, spheres[sphere_index].position, spheres[sphere_index].radius, intersection))
                {
                    float this_distance= distance(ray.tail, intersection);

                    if(this_distance< shortest_distance)
                    {
                        surface.position= intersection;
                        surface.normal= normalize(intersection- spheres[sphere_index].position);
                        surface.material= materials[sphere_material_indices[sphere_index]];

                        shortest_distance= this_distance;
                        collision_detected= true;
                    }
                }
            }
        }

        if(collision_detected && (shortest_distance/ length(ray.head))< t_exit)
            return true;

        t_entry= t_exit;
    }
}

return false;

}

在我看来,这只是一个编译器问题,但如果不是,那就太好了,因为这意味着我可以修复它。

有谁知道这可能是什么原因,我的选择是什么?我的时间有限,所以我正在考虑做一个 hack 来让它工作(即只留下线条,或类似的东西)

【问题讨论】:

  • 确保将变量初始化为一些默认值。在 NVidia 驱动程序中,我的着色器疯狂打印像素奇怪的东西,因为我有一些变量没有初始化。也许你的情况也遇到了同样的问题。

标签: opengl buffer glsl textures dead-code


【解决方案1】:

恐怕它看起来很像着色器编译器错误。无论这些行是否被注释掉,统一数组都应该在着色器程序之外进行优化(除非它在其他地方被引用)。

可能值得检查程序对象的制服(使用glGetActiveUniform),无论是否注释了这些行。

另外 - 尝试在不同的硬件上运行它。

【讨论】:

  • 我不知道 glGetActiveUniform,谢谢。我让其他一些人运行它,看来我的统一数组大小太大了。 (尽管这就是我尝试切换到不同内存的原因)。这很容易确定,因为这些其他编译器正在吐出有用的信息,包括行号,以及“数组太大”的实际单词,更不用说拒绝编译着色器(它出现的 nvidia 卡)。我现在相当确定我的问题与拥有巨大的统一数组和接受它的编译器有关,然后在执行期间变得狂暴。
  • @user2345397 既然您找到了问题的解决方案,您应该为自己的问题创建一个答案
  • 不幸的是,我无法有意义地解释我的错误与观察到的问题之间的联系,所以我认为我不能称我为解决问题所做的事情(或者更确切地说是我为解决问题所做的事情)观察到的可以称为合法答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2017-02-17
  • 1970-01-01
  • 2018-10-15
  • 2012-08-27
相关资源
最近更新 更多