Android 12 三星 Galaxy S21 OpenGL 着色器编译错误
我有以下顶点和片段着色器:
SimpleFragmentShader.fragmentshader:
#version 300 es
precision highp float;
out vec4 FragColor;
uniform vec4 vertexColor;
void main()
{
FragColor = vertexColor;
}
SimpleVertexShader.vertexshader:
#version 300 es
layout (location = 0) in vec3 aPos;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(aPos, 1.0);
}
加载着色器的函数:
GLuint LoadShaderProgram(const char* vertex_file_path,
const char* fragment_file_path) {
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
CHECK(VertexShaderStream.is_open())
<< "Unable to open shader at " << vertex_file_path;
std::string Line = "";
while (getline(VertexShaderStream, Line)) VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
if (FragmentShaderStream.is_open()) {
std::string Line = "";
while (getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const* VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL,
&VertexShaderErrorMessage[0]);
printf("%s\n", &VertexShaderErrorMessage[0]);
}
// Compile Fragment Shader
printf("Compiling shader : %s\n", fragment_file_path);
char const* FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL,
&FragmentShaderErrorMessage[0]);
printf("%s\n", &FragmentShaderErrorMessage[0]);
}
// Link the program
printf("Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL,
&ProgramErrorMessage[0]);
printf("%s\n", &ProgramErrorMessage[0]);
}
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
build.gradle: targetSdkVersion 31, minSdkVersion 26
当我尝试编译软件并运行它时我的手机(三星 Galaxy S21 5G、Exynos CPU、Mali-G78 GPU、Android 12),我收到以下错误:
D/native: Compiling shader : /data/user/0/app/files/shaders/SimpleVertexShader.vertexshader
D/native: 0:2: P0005: #version must be on the first line in a program and only whitespace are allowed in the declaration
Compiling shader
D/native: : /data/user/0/app/files/shaders/SimpleFragmentShader.fragmentshader
D/native: 0:2: P0005: #version must be on the first line in a program and only whitespace are allowed in the declaration
Linking program
D/native: Link failed because of invalid vertex shader.
但是,当我尝试在运行 Android 12 的 Samsung Galaxy Tab S7(SM-T870、Snapdragon 865+)上运行相同的代码时,它可以工作。
该错误消息暗示存在语法错误,但我认为这不是问题,因为该软件在 Galaxy Tab S7 上编译得很好。
我在 Samsung Galaxy S21 手机上尝试了不同版本 (#version 100/200/300 es/310 es/320 es
),但代码仅在使用 #version 100
时编译代码>.不过,ARM 官方主页指出 Galaxy S21 Mali-G78 GPU 应支持所有这些版本。
什么可能导致这个问题?与 Snapdragon 版本相比,三星设备的 Exynos 版本上是否有任何图形驱动程序缺乏 OpenGL 支持?我使用 Google 或 StackOverflow 没有找到相关问题。我发现三星设备上 OpenGL 和 Android 12 的唯一其他问题 (此处 和 此处)涉及模拟器,但我的应用程序中并非如此。
感谢您的帮助!
I have the following vertex and fragment shaders:
SimpleFragmentShader.fragmentshader:
#version 300 es
precision highp float;
out vec4 FragColor;
uniform vec4 vertexColor;
void main()
{
FragColor = vertexColor;
}
SimpleVertexShader.vertexshader:
#version 300 es
layout (location = 0) in vec3 aPos;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(aPos, 1.0);
}
Function to load the shaders:
GLuint LoadShaderProgram(const char* vertex_file_path,
const char* fragment_file_path) {
// Create the shaders
GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string VertexShaderCode;
std::ifstream VertexShaderStream(vertex_file_path, std::ios::in);
CHECK(VertexShaderStream.is_open())
<< "Unable to open shader at " << vertex_file_path;
std::string Line = "";
while (getline(VertexShaderStream, Line)) VertexShaderCode += "\n" + Line;
VertexShaderStream.close();
// Read the Fragment Shader code from the file
std::string FragmentShaderCode;
std::ifstream FragmentShaderStream(fragment_file_path, std::ios::in);
if (FragmentShaderStream.is_open()) {
std::string Line = "";
while (getline(FragmentShaderStream, Line))
FragmentShaderCode += "\n" + Line;
FragmentShaderStream.close();
}
GLint Result = GL_FALSE;
int InfoLogLength;
// Compile Vertex Shader
printf("Compiling shader : %s\n", vertex_file_path);
char const* VertexSourcePointer = VertexShaderCode.c_str();
glShaderSource(VertexShaderID, 1, &VertexSourcePointer, NULL);
glCompileShader(VertexShaderID);
// Check Vertex Shader
glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> VertexShaderErrorMessage(InfoLogLength + 1);
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL,
&VertexShaderErrorMessage[0]);
printf("%s\n", &VertexShaderErrorMessage[0]);
}
// Compile Fragment Shader
printf("Compiling shader : %s\n", fragment_file_path);
char const* FragmentSourcePointer = FragmentShaderCode.c_str();
glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer, NULL);
glCompileShader(FragmentShaderID);
// Check Fragment Shader
glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> FragmentShaderErrorMessage(InfoLogLength + 1);
glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL,
&FragmentShaderErrorMessage[0]);
printf("%s\n", &FragmentShaderErrorMessage[0]);
}
// Link the program
printf("Linking program\n");
GLuint ProgramID = glCreateProgram();
glAttachShader(ProgramID, VertexShaderID);
glAttachShader(ProgramID, FragmentShaderID);
glLinkProgram(ProgramID);
// Check the program
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
if (InfoLogLength > 0) {
std::vector<char> ProgramErrorMessage(InfoLogLength + 1);
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL,
&ProgramErrorMessage[0]);
printf("%s\n", &ProgramErrorMessage[0]);
}
glDeleteShader(VertexShaderID);
glDeleteShader(FragmentShaderID);
return ProgramID;
}
build.gradle: targetSdkVersion 31, minSdkVersion 26
When I try to compile the software and run it on my phone (Samsung Galaxy S21 5G, Exynos CPU, Mali-G78 GPU, Android 12), I get the following error:
D/native: Compiling shader : /data/user/0/app/files/shaders/SimpleVertexShader.vertexshader
D/native: 0:2: P0005: #version must be on the first line in a program and only whitespace are allowed in the declaration
Compiling shader
D/native: : /data/user/0/app/files/shaders/SimpleFragmentShader.fragmentshader
D/native: 0:2: P0005: #version must be on the first line in a program and only whitespace are allowed in the declaration
Linking program
D/native: Link failed because of invalid vertex shader.
However, when I try to run the same code on a Samsung Galaxy Tab S7 (SM-T870, Snapdragon 865+) running Android 12, it works.
The error message hints at a syntax error, however I do not think that this is the issue since the software compiles fine on the Galaxy Tab S7.
I have tried different versions (#version 100/200/300 es/310 es/320 es
) with the Samsung Galaxy S21 phone, but the code only compiled when using #version 100
. However, the official ARM-Homepage states that the Galaxy S21 Mali-G78 GPU should support all these versions.
What can be causing this problem? Are there any Graphics Drivers that lack OpenGL support on Exynos Versions of Samsung Devices compared to their Snapdragon counterparts? I did not find related issues using Google or StackOverflow. The only other issue with OpenGL and Android 12 on Samsung Devices I found (here and here) were involving Emulators, which is not the case in my application.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中提到的,代码...
在着色器源
VertexShaderCode
的开头添加了一个空行。因此会出现有关版本说明符缺失或放错位置的错误消息。只需将代码更改为...并对片段着色器代码部分执行类似的操作。
As mentioned in the comment, the code...
adds a blank line at the beginning of the shader source
VertexShaderCode
. Hence the error message regarding the missing or misplaced version specifier. Just change the code to...and do similarly for the fragment shader code section.