【问题标题】:Possible bug while defining a vertex attribute定义顶点属性时可能出现的错误
【发布时间】:2014-03-31 23:52:11
【问题描述】:

我在尝试编译顶点着色器时遇到分段错误。我想我在传递顶点属性时发现了问题。以下行编译(它们可能不起作用,但它们仍然编译):

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=2) in vec4 in_Norm;

还有

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=25) in vec4 in_Norm;

但是

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=2) in vec4 in_Norm;
layout(location=33) in vec4 in_Anything

不会编译。我想我只能定义 3 个 vec4 属性。但是,带有 GL_MAX_VERTEX_ATTRIBS 的 glGetIntegerv 返回 16,这符合 OpenGL 标准。这是与我的硬件相关的某种错误吗?我使用的是英特尔显卡

    *-display
         description: VGA compatible controller
         product: 3rd Gen Core processor Graphics Controller
         vendor: Intel Corporation
         physical id: 2
         bus info: pci@0000:00:02.0
         version: 09
         width: 64 bits
         clock: 33MHz
         capabilities: msi pm vga_controller bus_master cap_list rom
         configuration: driver=i915 latency=0
         resources: irq:49 memory:f0000000-f03fffff memory:e0000000-efffffff ioport:3000(size=64)

和台面 10:

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Ivybridge Mobile 
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.1.0
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile.

操作系统是 ubuntu 14.04。

【问题讨论】:

  • 如果您的 GL 只有 16 个属性,为什么还要指定这么大的位置索引?但无论如何它不应该崩溃,您可以在您的问题中添加 c crash backtrace 吗?
  • @keltar:在尝试查找错误源时,我尝试了“正常”位置,效果是一样的。我不确定如何获得 c 崩溃回溯,因为我在 glCompileShader 期间遇到了一个简单的分段错误。
  • 在调试器(gdb ./your_program)中运行,然后当报告崩溃时,输入bt 命令。使用调试器总是好的。
  • @keltar:这需要我一点时间,因为我是从 python 调用函数(使用 cython)。但我无法想象这可能是错误的根源。

标签: opengl glsl vertex-attributes


【解决方案1】:

感谢您的评论,我发现了错误。仅使用 c 创建测试用例后,编译 glsl 文件。事实证明,分段错误发生在“cython”中,但如果我直接从 c 调用库,则不会(尽管 c 中有一个“错误”)。该错误是通过在 glsl 文件中添加额外的行而产生的,这触发了分段错误。我对此感到困惑,并认为错误出在编译 glsl 中。但是,问题在于加载 glsl 文件的函数:

void load_file(const char* fname, char** buffer)
{
    FILE* fp;
    long fsize;
    fprintf(stdout, "Loading %s \n", fname);
    fp = fopen(fname, "r");
    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (fp == NULL)
    {
      fprintf(stderr, "Can't open %s\n", fname);
      exit(1);
    }
    *buffer = (char* ) malloc(fsize + 1);
    fprintf(stdout, "%d\n", (int ) fsize);
    fread(*buffer, fsize, 1, fp);
    buffer[fsize] = 0;
    fclose(fp);
}

通过替换 buffer[fsize] = 0;使用缓冲区 [0] [fsize] = 0;我解决了这个问题。我不知道这是从哪里来的,也不知道这是否真的是我的代码中的一个错误,但它可能与 cython 分配内存的方式有关。

【讨论】:

    猜你喜欢
    • 2020-02-12
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2017-02-01
    • 2016-09-24
    • 2012-05-01
    • 1970-01-01
    • 2019-07-23
    相关资源
    最近更新 更多