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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include <stdbool.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <lua.h>
#include <lauxlib.h>
#include "util/util.h"
int gl_create_shader(lua_State *L);
int gl_shader_set_source(lua_State *L);
int gl_shader_compile(lua_State *L);
int gl_shader_delete(lua_State *L);
int gl_program_create(lua_State *L);
int gl_program_attach_shader(lua_State *L);
int gl_program_link(lua_State *L);
int gl_program_use(lua_State *L);
int gl_uniform_get_location(lua_State *L);
int gl_uniform_1i(lua_State *L);
int gl_uniform_1f(lua_State *L);
int gl_uniform_3f(lua_State *L);
int gl_uniform_4f(lua_State *L);
int gl_uniform_matrix_4fv(lua_State *L);
void setup_shader(lua_State *L, int gl_index)
{
struct honey_tbl_t tbl[] = {
/* functions */
H_FUNC("CreateShader", gl_create_shader),
H_FUNC("ShaderSource", gl_shader_set_source),
H_FUNC("CompileShader", gl_shader_compile),
H_FUNC("DeleteShader", gl_shader_delete),
H_FUNC("CreateProgram", gl_program_create),
H_FUNC("AttachShader", gl_program_attach_shader),
H_FUNC("LinkProgram", gl_program_link),
H_FUNC("UseProgram", gl_program_use),
H_FUNC("GetUniformLocation", gl_uniform_get_location),
H_FUNC("Uniform1i", gl_uniform_1i),
H_FUNC("Uniform1f", gl_uniform_1f),
H_FUNC("Uniform3f", gl_uniform_3f),
H_FUNC("Uniform4f", gl_uniform_4f),
H_FUNC("UniformMatrix4fv", gl_uniform_matrix_4fv),
/******** enums ********/
/* shader types */
H_INT("VERTEX_SHADER", GL_VERTEX_SHADER),
H_INT("FRAGMENT_SHADER", GL_FRAGMENT_SHADER),
H_END
};
create_table(L, tbl);
append_table(L, gl_index, lua_gettop(L));
lua_pop(L, 1);
}
int gl_create_shader(lua_State *L)
{
lua_Integer type = luaL_checkinteger(L, 1);
lua_Integer shader = glCreateShader(type);
lua_pushinteger(L, shader);
return 1;
}
int gl_shader_set_source(lua_State *L)
{
lua_Integer shader = luaL_checkinteger(L, 1);
const char *code = luaL_checkstring(L, 2);
glShaderSource(shader, 1, (const GLchar * const*)&code, NULL);
return 0;
}
int gl_shader_compile(lua_State *L)
{
lua_Integer shader = luaL_checkinteger(L, 1);
glCompileShader(shader);
int success; char log[1024];
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, log);
luaL_error(L, "shader compilation failed: %s", log);
}
return 0;
}
int gl_shader_delete(lua_State *L)
{
lua_Integer shader = luaL_checkinteger(L, 1);
glDeleteShader(shader);
return 0;
}
int gl_program_create(lua_State *L)
{
lua_Integer program = glCreateProgram();
lua_pushinteger(L, program);
return 1;
}
int gl_program_attach_shader(lua_State *L)
{
lua_Integer program, shader;
program = luaL_checkinteger(L, 1);
shader = luaL_checkinteger(L, 2);
glAttachShader(program, shader);
return 0;
}
int gl_program_link(lua_State *L)
{
lua_Integer program = luaL_checkinteger(L, 1);
glLinkProgram(program);
int success; char log[1024];
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, 1024, NULL, log);
luaL_error(L, "shader linking failed: %s", log);
}
return 0;
}
int gl_program_use(lua_State *L)
{
lua_Integer program = luaL_checkinteger(L, 1);
glUseProgram(program);
return 0;
}
int gl_uniform_get_location(lua_State *L)
{
lua_Integer program = luaL_checkinteger(L, 1);
const char *name = luaL_checkstring(L, 2);
int location = glGetUniformLocation(program, (const GLchar*)name);
lua_pushinteger(L, location);
return 1;
}
int gl_uniform_1i(lua_State *L)
{
lua_Integer location, v0;
location = luaL_checkinteger(L, 1);
v0 = luaL_checkinteger(L, 2);
glUniform1i(location, v0);
return 0;
}
int gl_uniform_1f(lua_State *L)
{
int location = luaL_checkinteger(L, 1);
double value = luaL_checknumber(L, 2);
glUniform1f(location, value);
return 0;
}
int gl_uniform_3f(lua_State *L)
{
int location = luaL_checkinteger(L, 1);
double v0 = luaL_checknumber(L, 2);
double v1 = luaL_checknumber(L, 3);
double v2 = luaL_checknumber(L, 4);
glUniform3f(location, v0, v1, v2);
return 0;
}
int gl_uniform_4f(lua_State *L)
{
lua_Integer location = luaL_checkinteger(L, 1);
lua_Number v0 = luaL_checknumber(L, 2);
lua_Number v1 = luaL_checknumber(L, 3);
lua_Number v2 = luaL_checknumber(L, 4);
lua_Number v3 = luaL_checknumber(L, 5);
glUniform4f(location, v0, v1, v2, v3);
return 0;
}
int gl_uniform_matrix_4fv(lua_State *L)
{
lua_Integer location = luaL_checkinteger(L, 1);
bool transpose = lua_toboolean(L, 2);
float *value = lua_touserdata(L, 3);
glUniformMatrix4fv(location, 1, transpose, value);
return 0;
}
|