【发布时间】:2018-11-19 17:07:45
【问题描述】:
我的 GLFW/C++ 程序出现问题,我遇到的问题是着色器将在 NVIDIA gpu 上正确编译和链接,但在英特尔集成显卡上却没有。我一直试图为一个学校项目解决这个问题几个小时,但它似乎无济于事。着色器将在英特尔端正确编译,但无法链接着色器。
我知道着色器本身很好,因为它们在我做过的其他项目中工作,只是在这个特定的项目中失败了。
这是着色器链接的一些代码
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 970m
【问题讨论】:
-
你能发布链接器错误吗?
-
链接器错误消息令人感兴趣,但着色器代码更令人感兴趣。
-
无论使用什么着色器代码都会发生错误,我有多个着色器程序并尝试过其他程序
-
必须使用
glGetProgramInfoLog查询程序对象。 -
@Smcelrea 着色器由图形驱动程序编译。误差容限因制造商而异。可能在您的所有着色器程序中都存在相同的问题,NVIDIA 接受了这一问题,但会导致 Intel HD 出现错误。所以请显示错误信息和着色器代码。
标签: c++ opengl linker shader glfw