1 | #include <GL/gl3w.h> |
2 |
|
3 | #include "ZydecoCommon.hpp" |
4 | #include "GLProgram.hpp" |
5 | #include "GLShader.hpp" |
6 |
|
7 |
|
8 | static Logger LOGGER("GLProgram"); |
9 |
|
10 |
|
11 | // static initialize members |
12 | std::map<std::string, GLProgram*> GLProgram::s_glPrograms {}; |
13 |
|
14 |
|
15 | GLProgram *GLProgram::GetGLProgram(std::string name) |
16 | { |
17 | if (!s_glPrograms.count(name)) { ZydecoFault("GetGLProgram(): Program '{}' does not exist", name); } |
18 | return s_glPrograms.at(name); |
19 | } |
20 |
|
21 | GLProgram::GLProgram(std::string name): |
22 | m_glProgramId(-1), |
23 | m_glProgramName(name) |
24 | { |
25 | LOGGER.Log(Logger::TRACE, "GLProgram(name: {})", name); |
26 |
|
27 | s_glPrograms.emplace(std::pair<std::string, GLProgram*>(m_glProgramName, this)); |
28 | } |
29 |
|
30 | GLProgram::~GLProgram() |
31 | { |
32 | LOGGER.Log(Logger::TRACE, "~GLProgram() for '{}'", m_glProgramName); |
33 | if (m_glProgramId != -1) { glDeleteProgram(m_glProgramId); } |
34 | s_glPrograms.erase(m_glProgramName); |
35 | } |
36 |
|
37 | void GLProgram::Generate(std::vector<GLShader*> shaders) |
38 | { |
39 | uint64_t pid = glCreateProgram(); |
40 |
|
41 | for (GLShader *p_shader : shaders) |
42 | { |
43 | glAttachShader(pid, p_shader->GetGLShaderID()); |
44 | } |
45 |
|
46 | glLinkProgram(pid); |
47 |
|
48 | for (GLShader *p_shader : shaders) |
49 | { |
50 | glDetachShader(pid, p_shader->GetGLShaderID()); |
51 | } |
52 |
|
53 | int success; |
54 | char infoLog[512]; |
55 | glGetProgramiv(pid, GL_LINK_STATUS, &success); |
56 | if(!success) |
57 | { |
58 | glGetProgramInfoLog(pid, 512, NULL, infoLog); |
59 | ZydecoFault("Shader program linking failed:\n{}", infoLog); |
60 | return; |
61 | } |
62 | else |
63 | { |
64 | if (m_glProgramId != -1) { glDeleteProgram(m_glProgramId); } |
65 | m_glProgramId = pid; |
66 | } |
67 | } |
68 |
|
69 | uint64_t GLProgram::GetGLProgramID() |
70 | { |
71 | return m_glProgramId; |
72 | } |
73 |
|
74 | std::string GLProgram::GetGLProgramName() |
75 | { |
76 | return m_glProgramName; |
77 | } |
78 |
|