citra/src/video_core/renderer_opengl/gl_shader_util.cpp

99 lines
2.8 KiB
C++
Raw Normal View History

// Copyright 2014 Citra Emulator Project
2014-12-17 06:38:14 +01:00
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
2015-09-11 13:20:02 +02:00
#include <vector>
#include <glad/glad.h>
#include "common/assert.h"
2015-09-11 13:20:02 +02:00
#include "common/logging/log.h"
#include "video_core/renderer_opengl/gl_shader_util.h"
namespace GLShader {
GLuint LoadShader(const char* source, GLenum type) {
const char* debug_type;
switch (type) {
case GL_VERTEX_SHADER:
debug_type = "vertex";
break;
case GL_GEOMETRY_SHADER:
debug_type = "geometry";
break;
case GL_FRAGMENT_SHADER:
debug_type = "fragment";
break;
default:
UNREACHABLE();
}
GLuint shader_id = glCreateShader(type);
glShaderSource(shader_id, 1, &source, nullptr);
NGLOG_DEBUG(Render_OpenGL, "Compiling {} shader...", debug_type);
glCompileShader(shader_id);
GLint result = GL_FALSE;
GLint info_log_length;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result);
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &info_log_length);
if (info_log_length > 1) {
std::vector<char> shader_error(info_log_length);
glGetShaderInfoLog(shader_id, info_log_length, nullptr, &shader_error[0]);
2016-12-04 12:44:29 +01:00
if (result == GL_TRUE) {
NGLOG_DEBUG(Render_OpenGL, "{}", &shader_error[0]);
} else {
NGLOG_ERROR(Render_OpenGL, "Error compiling {} shader:\n{}", debug_type,
&shader_error[0]);
NGLOG_ERROR(Render_OpenGL, "Shader source code:\n{}", source);
}
}
return shader_id;
}
GLuint LoadProgram(bool separable_program, const std::vector<GLuint>& shaders) {
// Link the program
NGLOG_DEBUG(Render_OpenGL, "Linking program...");
GLuint program_id = glCreateProgram();
for (GLuint shader : shaders) {
if (shader != 0) {
glAttachShader(program_id, shader);
}
}
if (separable_program) {
glProgramParameteri(program_id, GL_PROGRAM_SEPARABLE, GL_TRUE);
}
glLinkProgram(program_id);
// Check the program
GLint result = GL_FALSE;
GLint info_log_length;
glGetProgramiv(program_id, GL_LINK_STATUS, &result);
glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &info_log_length);
if (info_log_length > 1) {
std::vector<char> program_error(info_log_length);
2014-12-03 19:57:57 +01:00
glGetProgramInfoLog(program_id, info_log_length, nullptr, &program_error[0]);
2016-12-04 12:44:29 +01:00
if (result == GL_TRUE) {
NGLOG_DEBUG(Render_OpenGL, "{}", &program_error[0]);
} else {
NGLOG_ERROR(Render_OpenGL, "Error linking shader:\n{}", &program_error[0]);
}
}
ASSERT_MSG(result == GL_TRUE, "Shader not linked");
2016-12-04 12:44:29 +01:00
for (GLuint shader : shaders) {
if (shader != 0) {
glDetachShader(program_id, shader);
}
}
return program_id;
}
} // namespace GLShader