I'd like to compile a GLSL shader and run the shader by loading the binary file on the execution side.
The following error occurs, what is the cause of this?What is invalid?
1. The compiled binary file is 8.4 kb in size.
2. Obtain errors such as compilation errors and program link errors.It doesn't come out, so there's no problem.
3. 0x501 is shown in glGetError().
4. Compiling and using Opengl 4.5
5. If you want to compile and run it on the spot, you can run it without any problems
Error: https://qiita.com/ydah/items/da56763e94ba58af3d91
Binaryization and loading: https://puarts.com/?pid=1298
glProgramBinary:https://registry.khronos.org/OpenGL-Refpages/gl4/html/glProgramBinary.xhtml
0x501—If the argument value of the called function is invalid or out of range
$./program
formats:1
binaryFormats:0x55bd29693fe0
Error 0
setBindAttribLocation():-1
setBindAttribLocation():-1
glGetError()—0x501
program:src/Shader.cpp:826:void FrameWork::Shader::setUniformSampler2D(const char*,GLuint,GLuint): Assertion `0' failed.
Stop (Core dump)
/*############################################################################################
# load
############################################################################################*/
voidFrameWork::Shader::Load(const char*vert, const char*frag)
{
GLuint program=glCreateProgram();
FILE*fp=fopen(vert, "rb");
if(fp==NULL)
{
std::cout<<"The file is missing."<<std::endl;
}
fseek(fp,0,SEEK_END);
GLintlen=(GLint)ftell(fp);
unsigned char * binary = new unsigned char [len];
fseek(fp,0,SEEK_SET);
fread(binary,len,1,fp);
fclose(fp);
GLint formats = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, & formats);
GLint*binaryFormats = new GLint [forms];
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, binaryFormats);
std::cout<<"forms:"<<forms<<<std::endl;
std::cout<<"binaryFormats:"<binaryFormats<<std::endl;
glProgramBinary(program, *binaryFormats, binary, len);
glLinkProgram (program); // Link Program
delete [ ] binary;
// View linked logs
if(ProgramInfoLog(program)==false)
{
std::cerr<<"Program link failure"<<std::endl;
assert(0);
}
GLint success;
glGetProgramiv(program, GL_LINK_STATUS, & success);
if(!success)
{
std::cout<<"Error"<<success<<std::endl;
// Loading failed...
}
// Delete(); // Delete current shader
// program=CreateProgram(LoadShader(vert)->data(),LoadShader(frag)->data());
if(program==0)
{
std::cerr<<"Shader Program Creation Error"<<std::endl;
assert(0);
}
}
/*############################################################################################
# initialization
############################################################################################*/
bool Init()
{
GLbooleane=glfwInit();
if(e==GL_FALSE)
{
std::cerr<<"Error:glfwInit()-1"<<std::endl;
return false;
}
GLFWindow*window=glfwCreateWindow(100,100, "test", NULL, NULL); // window generation
if(window==NULL)
{
std::cerr<<"Error:glfwCreateWindow() NULL"<<std::endl;
return false;
}
glfwMakeContextCurrent (window);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Glenumerr=glewInit();
if(err!=GLEW_OK)
{
std::cerr<<"Error:glewInit()"<err<<std::endl;
return false;
}
return true;
}
/*############################################################################################
# compilation
############################################################################################*/
bool Compile (const GLint&program, const GLchar file[], const GLint shaderType)
{
// vertex shader
GLuint shader=glCreateShader(shaderType);
glShaderSource (shader, 1, & file, NULL);
glCompileShader (shader);
GLboolean succ=CompileInfoLog (shader, shaderType); // Error
if(succ==GL_TRUE)
{
glAttachShader (program, shader);
return true;
}
glDeleteShader (shader);
return false;
}
/*############################################################################################
# binary generation
############################################################################################*/
bool GenBinary (const GLint&program, const char*fileName)
{
glLinkProgram (program);
bool succ=ProgramInfoLog(program); // Error
if(succ==true)
{
glUseProgram (program);
int size = 0;
GLint formats = 0;
glGetProgramiv(program, GL_PROGRAM_BINARY_LENGTH, & size);
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, & formats);
GLint*binaryFormats = new GLint [forms];
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, binaryFormats);
unsigned char * binary = new unsigned char [size];
glGetProgramBinary (program, size, NULL, (Glenum*) binaryFormats, binary);
glUseProgram(0);
FILE*fp=fopen(fileName, "wb");
fwrite(binary,size,1,fp);
fclose(fp);
delete [ ] binary;
}
return false;
}
#version420
layout(location=0)invec3vertexPosition;
layout(location=1) invec2vertexUV;
layout(location=2)outvec2vUV;
void main()
{
vUV = vertexUV;
gl_Position=vec4(vertexPosition.x, -vertexPosition.y, vertexPosition.z, 1.0);
}
# version 420
outvec4 fragment;
layout(location=2)invec2 UV;
uniform sampler2DuImage;
void main()
{
fragment=texture(uImage, UV);
// fragment = vec4 (1.0, 0.0, 0.0, 1.0);
}
I have never touched OpenGL, but I can read that the value of Glenum
written to the fourth argument after the glGetProgramBinary()
call must be specified as the second argument of glProgramBinary()
.
https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetProgramBinary.xhtml
The binary produced by the GL may subsequentially be returned to the GL by calling glProgramBinary
, with binaryFormat
and length
set to the values returned by glGetProgramBinary
, and recurring
© 2024 OneMinuteCode. All rights reserved.