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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
#include "../../../include/RenderBackend/OpenGL/ShaderProgram.hpp"
#include "../../../include/RenderBackend/OpenGL/Shader.hpp"
#include "../../../include/RenderBackend/OpenGL/opengl.hpp"
using namespace std;
namespace amalgine {
ShaderProgram::ShaderProgram(u32 shader_program_id) : program_id(shader_program_id) {
}
ShaderProgram::~ShaderProgram()
{
GLint numAttachedShaders = 0;
glGetProgramiv(program_id, GL_ATTACHED_SHADERS, &numAttachedShaders);
GLuint *attachedShaders = new GLuint[numAttachedShaders];
glGetAttachedShaders(program_id, numAttachedShaders, nullptr, attachedShaders);
for(int i = 0; i < numAttachedShaders; ++i)
{
GLuint attachedShaderId = attachedShaders[i];
glDetachShader(program_id, attachedShaderId);
}
glDeleteProgram(program_id);
delete []attachedShaders;
}
#if 0
bool ShaderProgram::setPixelShader(CompiledPixelShader *pixelShader)
{
if(!pixelShader) return false;
// TODO: Do not allow adding shader if the program has already been built
glAttachShader(shaderProgramId, pixelShader->getShaderId());
const auto &pixelAttributes = pixelShader->getPixelAttributes();
for(const auto &pixelAttribute : pixelAttributes)
{
const string &attributeName = pixelAttribute.first;
i32 attributeLocation = pixelAttribute.second;
glBindFragDataLocation(shaderProgramId, attributeLocation, attributeName.c_str());
}
return true;
}
#endif
// static
Result<std::unique_ptr<ShaderProgram>> ShaderProgram::build(const std::vector<Shader*> &shaders) {
u32 shader_program_id = glCreateProgram();
if(shader_program_id == 0)
return Result<std::unique_ptr<ShaderProgram>>::Err("Failed to create shader program");
for(Shader *shader : shaders) {
glAttachShader(shader_program_id, shader->id);
}
glLinkProgram(shader_program_id);
std::unique_ptr<ShaderProgram> shader_program;
shader_program.reset(new ShaderProgram(shader_program_id));
return Result<std::unique_ptr<ShaderProgram>>::Ok(std::move(shader_program));
}
Result<Uniform> ShaderProgram::get_uniform_by_name(const char *name) {
GLint uniform_id = glGetUniformLocation(program_id, name);
if(uniform_id == -1)
return Result<Uniform>::Err(std::string("Uniform with name ") + name + " was not found");
Uniform uniform(uniform_id, program_id);
return Result<Uniform>::Ok(std::move(uniform));
}
int ShaderProgram::set_input_data(const char *name, const DeviceMemory &data) {
GLint attrib_location = glGetAttribLocation(program_id, name);
if(attrib_location == -1) {
fprintf(stderr, "No such attribute in shader: %s\n", name);
return -1;
}
data.use();
glEnableVertexAttribArray(attrib_location);
switch(data.get_type()) {
case DeviceMemoryType::NONE:
return -1;
case DeviceMemoryType::VEC2: {
glVertexAttribPointer(attrib_location, 2, GL_FLOAT, GL_FALSE, 0, 0);
break;
}
case DeviceMemoryType::VEC3: {
glVertexAttribPointer(attrib_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
break;
}
}
return 0;
}
void ShaderProgram::use() {
glUseProgram(program_id);
}
}
|