在 NVidia 与 Intel 显卡上链接着色器时出错

Error Linking shaders on NVidia vs Intel graphics

我的 GLFW/C++ 程序出现问题,我遇到着色器将在 NVIDIA GPU 上正确编译和 link 但在英特尔集成显卡上无法正常编译的问题卡片。我一直试图为一个学校项目解决这个问题几个小时,但它似乎无处可去。着色器 在英特尔方面正确编译,但无法 link 着色器。

我知道着色器本身很好,因为它们在我完成的其他项目中工作,但在这个特定项目中失败了。

这是着色器的一些代码 linking

void ResourceManager::LoadMaterial(const std::string name, const char *prefix){

    // Load vertex program source code
    std::string filename = std::string(prefix) + std::string(VERTEX_PROGRAM_EXTENSION);
    std::string vp = LoadTextFile(filename.c_str());

    // Load fragment program source code
    filename = std::string(prefix) + std::string(FRAGMENT_PROGRAM_EXTENSION);
    std::string fp = LoadTextFile(filename.c_str());

    // Create a shader from the vertex program source code
    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    const char *source_vp = vp.c_str();
    glShaderSource(vs, 1, &source_vp, NULL);
    glCompileShader(vs);

    // Check if shader compiled successfully
    GLint status;
    glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
    if (status != GL_TRUE){
        char buffer[512];
        glGetShaderInfoLog(vs, 512, NULL, buffer);
        throw(std::ios_base::failure(std::string("Error compiling vertex shader: ")+std::string(buffer)));
    }

    // Create a shader from the fragment program source code
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    const char *source_fp = fp.c_str();
    glShaderSource(fs, 1, &source_fp, NULL);
    glCompileShader(fs);

    // Check if shader compiled successfully
    glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
    if (status != GL_TRUE){
        char buffer[512];
        glGetShaderInfoLog(fs, 512, NULL, buffer);
        throw(std::ios_base::failure(std::string("Error compiling fragment shader: ")+std::string(buffer)));
    }

    // Create a shader program linking both vertex and fragment shaders
    // together
    GLuint sp = glCreateProgram();
    glAttachShader(sp, vs);
    glAttachShader(sp, fs);
    glLinkProgram(sp);

    // Check if shaders were linked successfully
    glGetProgramiv(sp, GL_LINK_STATUS, &status);
    if (status != GL_TRUE){
        char buffer[512];
        glGetShaderInfoLog(sp, 512, NULL, buffer);
        throw(std::ios_base::failure(std::string("Error linking shaders: ")+std::string(buffer)));
    }

    // Delete memory used by shaders, since they were already compiled
    // and linked
    glDeleteShader(vs);
    glDeleteShader(fs);

    // Add a resource for the shader program
    AddResource(Material, name, sp, 0);
}

如果您需要代码的其他部分,我很乐意提供更多,但这是英特尔方面似乎失败的地方。

我真的希望有一个我还没有找到的非常简单的解决方法,因为这是一个棘手的问题。提前致谢。

附录 #1:错误代码

Error linking shaders: ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠: iostream stream error

附录 #2 Cmake 代码

    cmake_minimum_required(VERSION 2.6)

# Name of project
project(IlluminationDemo)

# Specify project files: header files and source files
set(HDRS
    asteroid.h camera.h game.h model_loader.h resource.h resource_manager.h scene_graph.h scene_node.h
)

set(SRCS
    asteroid.cpp camera.cpp game.cpp main.cpp resource.cpp resource_manager.cpp scene_graph.cpp scene_node.cpp material_fp.glsl material_vp.glsl metal_fp.glsl metal_vp.glsl plastic_fp.glsl plastic_vp.glsl textured_material_fp.glsl textured_material_vp.glsl three-term_shiny_blue_fp.glsl three-term_shiny_blue_vp.glsl three-term_textured_fp.glsl three-term_textured_vp.glsl three-term_toon_fp.glsl three-term_toon_vp.glsl
)

# Add path name to configuration file
configure_file(path_config.h.in path_config.h)

# Add executable based on the source files
add_executable(IlluminationDemo ${HDRS} ${SRCS})

# Require OpenGL library
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
target_link_libraries(IlluminationDemo ${OPENGL_gl_LIBRARY})

# Other libraries needed
set(LIBRARY_PATH "" CACHE PATH "Folder with GLEW, GLFW, GLM, and SOIL libraries")
include_directories(${LIBRARY_PATH}/include)
if(NOT WIN32)
    find_library(GLEW_LIBRARY GLEW)
    find_library(GLFW_LIBRARY glfw)
    find_library(SOIL_LIBRARY SOIL)
elseif(WIN32)
    find_library(GLEW_LIBRARY glew32s HINTS ${LIBRARY_PATH}/lib)
    find_library(GLFW_LIBRARY glfw3 HINTS ${LIBRARY_PATH}/lib)
    find_library(SOIL_LIBRARY SOIL HINTS ${LIBRARY_PATH}/lib)
endif(NOT WIN32)
target_link_libraries(IlluminationDemo ${GLEW_LIBRARY})
target_link_libraries(IlluminationDemo ${GLFW_LIBRARY})
target_link_libraries(IlluminationDemo ${SOIL_LIBRARY})

# The rules here are specific to Windows Systems
if(WIN32)
    # Avoid ZERO_CHECK target in Visual Studio
    set(CMAKE_SUPPRESS_REGENERATION TRUE)

    # This will use the proper libraries in debug mode in Visual Studio
    set_target_properties(IlluminationDemo PROPERTIES DEBUG_POSTFIX _d)
endif(WIN32)

附录 #3 着色器代码

片段着色器

// Illumination based on the traditional three-term model

#version 130

// Attributes passed from the vertex shader
in vec3 position_interp;
in vec3 normal_interp;
in vec3 light_pos[2];
in vec3 camera_pos;

// Material attributes (constants)
vec4 ambient_color = vec4(0.0, 0.1, 0.0, 1.0);
vec4 diffuse_color = vec4(0.4, 0.8, 0.3, 1.0);
vec4 specular_color = vec4(0.9, 0.9, 0.9, 1.0);
float phong_exponent = 128.0;


void main() 
{
    // Blinn-Phong shading

    vec3 N, // Interpolated normal for fragment
         L, // Light-source direction
         V, // View direction
         H; // Half-way vector
    for(int i = 0; i < light_pos.length; i++){
    // Compute Lambertian lighting Id
    N = normalize(normal_interp);


    L = (light_pos[i] - position_interp);
    L = normalize(L);

    float Id = max(dot(N, L), 0.0);
    Id = round(Id*2.0) / 2.0;

    // Compute specular term for Blinn-Phong shading
    // V = (eye_position - position_interp);
    V = camera_pos - position_interp; // Eye position is (0, 0, 0) in view coordinates
    V = normalize(V);

    //H = 0.5*(V + L); // Halfway vector
    H = (V + L); // Halfway vector
    H = normalize(H);

    float spec_angle_cos = max(dot(N, H), 0.0);
    float Is = pow(spec_angle_cos, phong_exponent);
    Is = round(Is*2.0) / 2.0;

    if(dot(V,N) > mix(0.5, 0.5, max(0.0, dot(N,L)))){
    // Assign light to the fragment
    gl_FragColor += ambient_color + Id*diffuse_color + Is*specular_color;
    } else {

        gl_FragColor = vec4(0.0,0.0,0.0,1.0) * (ambient_color + Id*diffuse_color + Is*specular_color);
    }
    }


    // For debug, we can display the different values
    //gl_FragColor = ambient_color;
    //gl_FragColor = diffuse_color;
    //gl_FragColor = specular_color;
    //gl_FragColor = color_interp;
    //gl_FragColor = vec4(N.xyz, 1.0);
    //gl_FragColor = vec4(L.xyz, 1.0);
    //gl_FragColor = vec4(V.xyz, 1.0);
}

// Illumination based on the traditional three-term model

顶点着色器

#version 130

// Vertex buffer
in vec3 vertex;
in vec3 normal;
in vec3 color;

// Uniform (global) buffer
uniform mat4 world_mat;
uniform mat4 view_mat;
uniform mat4 projection_mat;
uniform mat4 normal_mat;
uniform vec3 cameraPos;

// Attributes forwarded to the fragment shader
out vec3 position_interp;
out vec3 normal_interp;
out vec3 camera_pos;
out vec3 light_pos[2];

// Material attributes (constants)
//
// Could be loaded from a configuration file and also passed with the
// uniform buffer
vec3 light_position = vec3(-0.5, -0.5, 1.5);
vec3 light_position2 = vec3(4.0, -1.0, -1.0);


void main()
{
    camera_pos = cameraPos;
    // Transform vertex position
    gl_Position = projection_mat * view_mat * world_mat * vec4(vertex, 1.0);

    // Transform vertex position without including projection
    position_interp = vec3(view_mat * world_mat * vec4(vertex, 1.0));

    // Transform normal
    normal_interp = vec3(normal_mat * vec4(normal, 0.0));

    // Transform light position to align with view
    light_pos[0] = vec3(view_mat * vec4(light_position, 1.0));
    light_pos[1] = vec3(view_mat * vec4(light_position2, 1.0));
}

附录 #4 问题似乎与数组 light_pos[] 如何在着色器之间传递然后程序在 light_pos.length 被调用时崩溃有关。对此的澄清将不胜感激。

附录 #5 已测试图形适配器: 英特尔: 高清4600, 高清5600, 高清615 ,英伟达: GTX 750钛, GTX 1080, GTX 970 米

正如 GLSL specification version 1.30; 5.7 Structure and Array Operations; page 46 中明确规定的那样,length 是一种方法而不是成员,正确的使用语法是:

vec3 light_pos[2];

int l = light_pos.length();

light_pos.length 更改为 light_pos.length() 以解决您的问题。

当然,编译器在这种情况下没有生成错误消息仍然令人惊讶。
在我的例子中,NVIDIA 驱动程序接受 light_pos.length 就好像它是 light_pos.length().
当然,Intel HD 驱动程序接受了 light_pos.length()。使用 light_pos.length 没有给出任何错误消息,但它会在 glLinkProgram.

处导致访问冲突