5 #include "state_manager.h"
6 #include "opengl/shader-gl.h"
8 extern ShaderProgram *current_program;
20 bool ShaderGL::create(char *buf, unsigned int bsz, const char *fname)
22 /* find shader type and create shader */
26 stype = GL_VERTEX_SHADER;
29 stype = GL_FRAGMENT_SHADER;
32 fprintf(stderr, "Unknown shader type.\n");
35 name = std::string(fname);
36 sdr = glCreateShader(stype);
39 glShaderSource(sdr, 1, (const char **)&buf, 0);
44 /* check compile status */
46 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
48 printf("Successfully compiled shader: %s\n", fname);
50 fprintf(stderr, "Failed to compile %s shader.\n", fname);
52 /* print the info log */
54 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
55 if(loglen > 0 && (buf = new char[loglen + 1])) {
56 glGetShaderInfoLog(sdr, loglen, 0, buf);
71 void ShaderGL::destroy()
82 ShaderProgramGL::ShaderProgramGL()
85 memset(shaders, 0, sizeof shaders / sizeof *shaders);
91 ShaderProgramGL::~ShaderProgramGL()
96 bool ShaderProgramGL::create()
98 prog = glCreateProgram();
100 fprintf(stderr, "Failed to create shader program.\n");
106 bool ShaderProgramGL::link()
114 glGetProgramiv(prog, GL_LINK_STATUS, &status);
116 printf("Successfully linked shader program.\n");
120 printf("Failed to link shader program.\n");
123 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &loglen);
126 if(loglen > 0 && (buf = new char[loglen + 1])) {
127 glGetProgramInfoLog(prog, loglen, 0, buf);
133 return status ? true : false;
136 bool ShaderProgramGL::use()
138 if(!is_linked && !link()) {
148 current_program = this;
150 static void (*const set_uniform[16])(GLint, GLsizei, const GLfloat *) = {
151 0, glUniform1fv, glUniform2fv, glUniform3fv, glUniform4fv
154 for(size_t i=0; i<uniforms.size(); i++) {
155 const State *st = state_manager.get_state(uniforms[i].state_idx);
157 set_uniform[st->num](uniforms[i].location, 1, st->data);
159 else if(st->num == 16) {
160 glUniformMatrix4fv(uniforms[i].location, 1, GL_TRUE, st->data);
163 fprintf(stderr, "Invalid number of floats in state %s: %d\n", st->name, st->num);
170 void ShaderProgramGL::destroy()
172 glDeleteProgram(prog);
177 void ShaderProgramGL::attach_shader(Shader *shader)
179 glAttachShader(prog, ((ShaderGL *)shader)->sdr);
183 int ShaderProgramGL::get_uniform_location(const char *name) const
185 if(!((ShaderProgramGL *)this)->use())
188 return glGetUniformLocation(prog, name);
191 int ShaderProgramGL::get_attribute_location(const char *name) const
193 if(!((ShaderProgramGL *)this)->use())
196 return glGetAttribLocation(prog, name);
199 static int get_floats_num(unsigned int utype)
223 void ShaderProgramGL::cache_uniforms()
228 glGetProgramiv(prog, GL_ACTIVE_UNIFORMS, &num_uniforms);
231 glGetProgramiv(prog, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_ulength);
233 char *name = new char[max_ulength + 1];
234 name[max_ulength] = 0;
236 for(int i=0; i<num_uniforms; i++) {
239 glGetActiveUniform(prog, i, max_ulength, 0, &usize, &utype, name);
241 if(strstr(name, "gl_") == name)
244 if(strstr(name, "st_") != name)
247 int num_floats = get_floats_num(utype);
251 int idx = state_manager.add_state_element(name, num_floats);
257 uniform.location = glGetUniformLocation(prog, name);
258 uniform.state_idx = idx;
260 uniforms.push_back(uniform);
266 void ShaderProgramGL::set_uniformi(int location, int value)
268 if(!use() || location == -1) {
272 glUniform1i(location, value);
275 void ShaderProgramGL::set_uniformi(int location, int x, int y)
277 if(!use() || location == -1) {
281 glUniform2i(location, x, y);
284 void ShaderProgramGL::set_uniformi(int location, int x, int y, int z)
286 if(!use() || location == -1) {
290 glUniform3i(location, x, y, z);
293 void ShaderProgramGL::set_uniformi(int location, int x, int y, int z, int w)
295 if(!use() || location == -1) {
299 glUniform4i(location, x, y, z, w);
302 void ShaderProgramGL::set_uniformf(int location, float value)
304 if(!use() || location == -1) {
308 glUniform1f(location, value);
311 void ShaderProgramGL::set_uniformf(int location, float x, float y)
313 if(!use() || location == -1) {
317 glUniform2f(location, x, y);
320 void ShaderProgramGL::set_uniformf(int location, float x, float y, float z)
322 if(!use() || location == -1) {
326 glUniform3f(location, x, y, z);
329 void ShaderProgramGL::set_uniformf(int location, float x, float y, float z, float w)
331 if(!use() || location == -1) {
335 glUniform4f(location, x, y, z, w);
338 void ShaderProgramGL::set_uniform_matrix(int location, const Mat4 &mat)
340 if(!use() || location == -1) {
341 fprintf(stderr, "FOO\n");
345 glUniformMatrix4fv(location, 1, GL_TRUE, (float *)&mat.m[0][0]);