-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgk_shader.c
160 lines (117 loc) · 4.63 KB
/
dgk_shader.c
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "dgk_shader.h"
#include "glad.h"
#include "d_memory.h"
#include "gui.vert.h"
#include "gui.frag.h"
#include "d_logger.h"
typedef struct _dgk_shader {
GLuint program;
} DGKShader;
typedef struct _dgk_shader_pool {
DGKShader* builtin_shaders[DGKSHADER_BUILTIN_TOTAL];
} DGKShaderPool;
static DGKShaderPool* g_dgk_shader_pool = NULL;
static DGKShader* current_bind_shader = NULL;
DGKShader* dgk_shader_load(const char* vert_shader_source,const char* frag_shader_source,DError** error){
DGKShader* new_shader = d_malloc(sizeof(DGKShader));
new_shader->program = 0;
GLuint vertex_shader = 0;
GLuint fragment_shader = 0;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
if ( vertex_shader == 0 ){
if ( error ){
*error = DERROR("Vertex shader creation failed");
goto error;
}
}
glShaderSource(vertex_shader,1,&vert_shader_source,NULL);
glCompileShader(vertex_shader);
GLint compile_status;
glGetShaderiv(vertex_shader,GL_COMPILE_STATUS,&compile_status);
if ( compile_status == GL_FALSE ){
if ( error ){
GLint log_length;
glGetShaderiv(vertex_shader,GL_INFO_LOG_LENGTH,&log_length);
GLchar* log_buffer = d_malloc(sizeof(GLchar) * log_length+1);
glGetShaderInfoLog(vertex_shader,log_length+1,NULL,log_buffer);
*error = DERROR("Vertex shader compilation failed : %s",log_buffer);
free(log_buffer);
goto error;
}
}
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
if ( fragment_shader == 0 ){
if ( error ){
*error = DERROR("Fragment shader creation failed");
goto error;
}
}
glShaderSource(fragment_shader,1,&frag_shader_source,NULL);
glCompileShader(fragment_shader);
glGetShaderiv(fragment_shader,GL_COMPILE_STATUS,&compile_status);
if ( compile_status == GL_FALSE ){
if ( error ){
GLint log_length;
glGetShaderiv(fragment_shader,GL_INFO_LOG_LENGTH,&log_length);
GLchar* log_buffer = d_malloc(sizeof(GLchar) * log_length+1);
glGetShaderInfoLog(fragment_shader,log_length+1,NULL,log_buffer);
*error = DERROR("Fragment shader compilation failed : %s",log_buffer);
free(log_buffer);
goto error;
}
}
new_shader->program = glCreateProgram();
if ( new_shader->program == 0 ){
if ( error ){
*error = DERROR("Program creation failed");
goto error;
}
}
glAttachShader(new_shader->program,vertex_shader);
glAttachShader(new_shader->program,fragment_shader);
glLinkProgram(new_shader->program);
GLint link_status;
glGetProgramiv(new_shader->program,GL_LINK_STATUS,&link_status);
if ( link_status == GL_FALSE ){
if ( error ){
GLint log_length;
glGetProgramiv(new_shader->program,GL_INFO_LOG_LENGTH,&log_length);
GLchar* log_buffer = d_malloc(sizeof(GLchar) * log_length+1);
glGetProgramInfoLog(new_shader->program,log_length+1,NULL,log_buffer);
*error = DERROR("Program Linking failed : %s",log_buffer);
free(log_buffer);
goto error;
}
}
if ( vertex_shader ) glDeleteShader(vertex_shader);
if ( fragment_shader ) glDeleteShader(fragment_shader);
return new_shader;
error:
if ( new_shader ) dgk_shader_free(new_shader);
if ( vertex_shader ) glDeleteShader(vertex_shader);
if ( fragment_shader ) glDeleteShader(fragment_shader);
return NULL;
}
void dgk_shader_bind(DGKShader* shader){
if ( current_bind_shader != shader)
glUseProgram(shader->program);
}
void dgk_shader_free(DGKShader* shader){
if ( current_bind_shader == shader)
current_bind_shader = NULL;
if ( shader->program ) glDeleteProgram(shader->program);
if ( shader ) free(shader);
}
DGKShaderPool* dgk_shader_pool_new(){
DGKShaderPool* new_pool = d_malloc(sizeof(DGKShaderPool));
DError* error = NULL;
new_pool->builtin_shaders[DGKSHADER_BUILTIN_SPRITE] = dgk_shader_load(gui_vert_shader,gui_frag_shader,&error);
if ( error )
DLOG_ERR_C(error,"Couldn't load builtin sprite shader");
return new_pool;
}
DGKShader* dgk_shader_pool_get_builtin_shader(int shader){
if ( g_dgk_shader_pool == NULL)
g_dgk_shader_pool = dgk_shader_pool_new();
return g_dgk_shader_pool->builtin_shaders[shader];
}