Make shader compiler feedback a little nicer
@@ -201,6 +201,7 @@ class Shader:
raise TypeError("The `shader_type` '{}' is not yet supported".format(shader_type))
self.type = shader_type
+ source_string = source_string.strip()
shader_source_utf8 = source_string.encode("utf8")
source_buffer_pointer = cast(c_char_p(shader_source_utf8), POINTER(c_char))
source_length = c_int(len(shader_source_utf8))
@@ -213,8 +214,18 @@ class Shader:
glGetShaderiv(shader_id, GL_COMPILE_STATUS, byref(status))
if status.value != GL_TRUE:
- raise GLException("The {0} shader failed to compile. "
- "\n{1}".format(self.type, self._get_shader_log(shader_id)))
+ source = self._get_shader_source(shader_id)
+ source_lines = "{0}".format("\n".join(f"{str(i+1).zfill(3)}: {line} "
+ for i, line in enumerate(source.split("\n"))))
+ raise GLException(
+ (
+ f"The {self.type} shader failed to compile.\n"
+ f"{self._get_shader_log(shader_id)}"
+ "------------------------------------------------------------\n"
+ f"{source_lines}\n"
+ "------------------------------------------------------------"
+ )
+ )
elif _debug_gl_shaders:
print(self._get_shader_log(shader_id))
@@ -235,6 +246,14 @@ class Shader:
else:
return f"{self.type.capitalize()} Shader '{shader_id}' compiled successfully."
+ def _get_shader_source(self, shader_id):
+ """Get the shader source from the shader object"""
+ source_length = c_int(0)
+ glGetShaderiv(shader_id, GL_SHADER_SOURCE_LENGTH, source_length)
+ source_str = create_string_buffer(source_length.value)
+ glGetShaderSource(shader_id, source_length, None, source_str)
+ return source_str.value.decode('utf8')
+
def __del__(self):
try:
glDeleteShader(self._id)
