pythonpyopengl

GLError 1281 invalid value in glGetUniformLocation


I wrote a shader class and for some reason I get an error:

OpenGL.error.GLError: GLError(
    err = 1281,
    description = b'invalid value',
    baseOperation = glGetUniformLocation,
    cArguments = (0, b'projection\x00'),
    result = -1
)

Here is my code:

# shader.py

from rengine.debugging import Debug
from rengine.math import REMath
from rengine.gfx.gl import *

class Shader():
    def __init__(self, vertex_src:str = None, fragment_src:str = None,
                       vertex_path:str = None, fragment_path:str = None):
        self.__uniform_locations = {}
        if vertex_src and fragment_src:
            self.load_from_src(vertex_src, fragment_src)
        elif vertex_path and fragment_path:
            self.load_from_file(vertex_path, fragment_path)
        else: self.__program_id = 0

    def use(self):
        glUseProgram(self.__program_id)
    
    def unbind(self):
        glUseProgram(0)

    def destroy(self):
        glDeleteShader(self.__vertex_shader)
        glDeleteShader(self.__fragment_shader)

    def set_uniform_location(self, uniform_name:str):
        if uniform_name not in self.__uniform_locations:
            self.__uniform_locations[uniform_name] = glGetUniformLocation(self.__program_id, uniform_name)
            # OpenGL.error.GLError: GLError(
            #     err = 1281,
            #     description = b'invalid value',
            #     baseOperation = glGetUniformLocation,
            #     cArguments = (0, b'projection\x00'),
            #     result = -1
            # )


    def set_uniform_mat4(self, uniform_name:str, matrix:REMath.mat4):
        self.set_uniform_location(uniform_name)
        glUniformMatrix4fv(self.__uniform_locations[uniform_name], 1, GL_FALSE, REMath.value_ptr(matrix))

    def set_uniform_float(self, uniform_name:str, value:float):
        self.set_uniform_location(uniform_name)
        glUniform1f(self.__uniform_locations[uniform_name], value)

    def set_uniform_int(self, uniform_name:str, value:int):
        self.set_uniform_location(uniform_name)
        glUniform1i(self.__uniform_locations[uniform_name], value)

    def set_uniform_vec2(self, uniform_name:str, vector:REMath.vec2):
        self.set_uniform_location(uniform_name)
        glUniform2fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))

    def set_uniform_vec3(self, uniform_name:str, vector:REMath.vec3):
        self.set_uniform_location(uniform_name)
        glUniform3fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))

    def set_uniform_vec4(self, uniform_name:str, vector:REMath.vec4):
        self.set_uniform_location(uniform_name)
        glUniform4fv(self.__uniform_locations[uniform_name], 1, REMath.value_ptr(vector))

    def compile_shader(self, source:str, shader_type:GL_SHADER_TYPE):
        shader_id = glCreateShader(shader_type)
        glShaderSource(shader_id, source)
        glCompileShader(shader_id)

        if not glGetShaderiv(shader_id, GL_COMPILE_STATUS):
            error = glGetShaderInfoLog(shader_id).decode()
            Debug.print_err(f"Shader Compilation Failed: {error}")
            glDeleteShader(shader_id)
            return
        
        Debug.print_succ(f"Shader Compiled Successfully: {shader_id}")

        return shader_id
    
    def compile_program(self, vertex_shader:GL_SHADER, fragment_shader:GL_SHADER):
        program_id = glCreateProgram()
        glAttachShader(program_id, vertex_shader)
        glAttachShader(program_id, fragment_shader)
        glLinkProgram(program_id)
        return program_id
    
    def load_from_src(self, vertex_src:str, fragment_src:str):
        self.__vertex_shader = self.compile_shader(vertex_src, GL_VERTEX_SHADER)
        self.__fragment_shader = self.compile_shader(fragment_src, GL_FRAGMENT_SHADER)
        self.__program_id = self.compile_program(self.__vertex_shader, self.__fragment_shader)
    
    def load_from_file(self, vertex_path:str, fragment_path:str):
        with open(vertex_path, "r") as vf:
            self.__vertex_shader = self.compile_shader(vf.read(), GL_VERTEX_SHADER)
        with open(fragment_path, "r") as ff:
            self.__fragment_shader = self.compile_shader(ff.read(), GL_FRAGMENT_SHADER)
        self.__program_id = self.compile_program(self.__vertex_shader, self.__fragment_shader)

Here is my shaders: VERTEX SHADER:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(aPos, 1.0);
}

FRAGMENT SHADER:

#version 330 core

out vec4 fragColor;

void main()
{
    fragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

I tried to debug the code and noticed that when I create shaders they have the same id (0), I think this is the problem, but I don't know why this happens

It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.


Solution

  • The mistake was that I created the shaders before I created the OpenGL context