-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.hpp
83 lines (71 loc) · 1.89 KB
/
program.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//@ {"targets":[{"name":"program.hpp","type":"include"}]}
#ifndef GIESELA_PROGRAM_HPP
#define GIESELA_PROGRAM_HPP
#include "shader.hpp"
#include "error.hpp"
namespace Giesela
{
class Program
{
public:
template<class ... T>
explicit Program(const Shader& shader,const T&...shaders);
Program(const Program&)=delete;
Program& operator=(const Program&)=delete;
Program(Program&& obj) noexcept:m_handle(obj.m_handle)
{obj.m_handle=0;}
Program& operator=(Program&& obj)
{
std::swap(obj.m_handle,m_handle);
return *this;
}
~Program() noexcept
{
unbind();
glDeleteProgram(m_handle);
}
void bind() const noexcept
{
glUseProgram(m_handle);
}
void unbind() const noexcept
{
glUseProgram(0);
}
private:
static constexpr void collect(GLuint* handles) noexcept
{}
template<class ...T>
static constexpr void collect(GLuint* handles,const Shader& shader,const T&...shaders) noexcept
{
handles[0]=shader.handle();
collect(handles+1,shaders...);
}
GLuint m_handle;
};
template<class ... T>
Program::Program(const Shader& shader,const T&...shaders)
{
assert(glCreateProgram!=nullptr);
m_handle=glCreateProgram();
GLuint handles[sizeof...(T)+1];
collect(handles,shader,shaders...);
for(size_t k=0;k<sizeof...(T)+1;++k)
{glAttachShader(m_handle,handles[k]);}
glLinkProgram(m_handle);
auto status=static_cast<GLint>(0);
glGetProgramiv(m_handle,GL_LINK_STATUS,&status);
if(status!=GL_TRUE)
{
auto length=static_cast<GLint>(0);
glGetProgramiv(m_handle,GL_INFO_LOG_LENGTH,&length);
char message[1024];
glGetProgramInfoLog(m_handle,std::min(1024,length),NULL,message);
for(size_t k=0;k<sizeof...(T)+1;++k)
{glDetachShader(m_handle,handles[k]);}
glDeleteProgram(m_handle);
throw Error("Program link error: ",static_cast<const char*>(message));
}
}
}
#endif