在我的程序中,我尝试使用Sasha Willems(https://github.com/SaschaWillems/openglcpp/blob/master/SPIRVShader/main.cpp)的示例代码加载预编译的二进制着色器:
bool loadBinaryShader(const char *fileName, GLuint stage, GLuint binaryFormat, GLuint &shader)
{
std::ifstream shaderFile;
shaderFile.open(fileName, std::ios::binary | std::ios::ate);
if (shaderFile.is_open())
{
size_t size = shaderFile.tellg();
shaderFile.seekg(0, std::ios::beg);
char* bin = new char[size];
shaderFile.read(bin, size);
GLint status;
shader = glCreateShader(stage); // Create a new shader
glShaderBinary(1, &shader, binaryFormat, bin, size); // Load the binary shader file
glSpecializeShaderARB(shader, "main", 0, nullptr, nullptr); // Set main entry point (required, no specialization used in this example)
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); // Check compilation status
return status;
}
else
{
std::cerr << "Could not open \"" << fileName << "\"" << std::endl;
return false;
}
}
我创建了以下两个用于测试的简单着色器:
test.frag
#version 450
in vec4 color;
out vec4 outCol;
void main()
{
outCol = vec4(1., 0., 0., 1.);
}
和text.vert
#version 450
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inColor;
layout (location = 0) out vec3 outColor;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outColor = vec3(0.2, 1., 0.2);
gl_Position = vec4(5.*inPos.xyz, 1.0);
}
我使用github中的glslangValidator将它们转换为SPIR-V格式: https://github.com/KhronosGroup/glslang 我用过:
glslangValidator.exe test.vert -G -o anypath.spv
当我尝试加载这些着色器时,它会在行
处出现分段错误而崩溃glSpecializeShaderARB(shader, "main", 0, nullptr, nullptr);
我在使用旧版GPU(GeForce GTX 660)的另一台PC上尝试过相同的功能,它运行正常。但它在我的新电脑上不适用于Radeon R9 Fury X.
任何想法?