mightymandel v16

GPU-based Mandelbrot set explorer

shader.c
Go to the documentation of this file.
1 // mightymandel -- GPU-based Mandelbrot Set explorer
2 // Copyright (C) 2012,2013,2014,2015 Claude Heiland-Allen
3 // License GPL3+ http://www.gnu.org/licenses/gpl.html
4 
10 #include <stdlib.h>
11 
12 #include "shader.h"
13 #include "logging.h"
14 #include "mightymandel.h"
15 
16 extern const char *fp32_preamble;
17 extern const char *fp64_preamble;
18 extern const char *fp32_complex;
19 extern const char *fp64_complex;
20 extern const char *config;
21 
28 void debug_program(GLuint program, const char *name) {
29  if (program) {
30  GLint linked = GL_FALSE;
31  glGetProgramiv(program, GL_LINK_STATUS, &linked);D;
32  if (linked != GL_TRUE) {
33  log_message(LOG_ERROR, "%s: OpenGL shader program link failed\n", name);
34  }
35  int length;
36  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);D;
37  char *buffer = (char *) malloc(length + 1);
38  glGetProgramInfoLog(program, length, NULL, buffer);D;
39  buffer[length] = 0;
40  if (buffer[0]) {
41  log_message(LOG_WARN, "%s: shader compile log\n", name);
42  if (LOG_WARN <= log_level) {
43  fprintf(log_target, "%s\n", buffer);
44  }
45  }
46  free(buffer);
47  } else {
48  log_message(LOG_ERROR, "%s: OpenGL program creation failed\n", name);
49  }
50 }
51 
62 void compile_shader(GLint program, GLenum type, const GLchar *source) {
63  const GLchar *fp32_sources[5] = { fp32_preamble, config, DE ? "#define DE 1\n" : "", fp32_complex, source };
64  const GLchar *fp64_sources[6] = { fp64_preamble, config, DE ? "#define DE 1\n" : "", fp32_complex, fp64_complex, source };
65  GLuint shader = glCreateShader(type);D;
66  glShaderSource(shader, FP64 ? 6 : 5, FP64 ? fp64_sources : fp32_sources, NULL);D;
67  glCompileShader(shader);D;
68  glAttachShader(program, shader);D;
69  glDeleteShader(shader);D;
70 }
71 
83 GLint compile_program_tf(const char *name, const GLchar *vert, const GLchar *geom, const GLchar *frag, int nvaryings, const GLchar **varyings) {
84  GLint program = glCreateProgram();D;
85  if (vert) { compile_shader(program, GL_VERTEX_SHADER , vert); }
86  if (geom) { compile_shader(program, GL_GEOMETRY_SHADER, geom); }
87  if (frag) { compile_shader(program, GL_FRAGMENT_SHADER, frag); }
88  if (nvaryings) {
89  glTransformFeedbackVaryings(program, nvaryings, varyings, GL_INTERLEAVED_ATTRIBS);D;
90  }
91  glLinkProgram(program);D;
92  debug_program(program, name);
93  return program;
94 }
95 
105 GLint compile_program(const char *name, const GLchar *vert, const GLchar *geom, const GLchar *frag) {
106  return compile_program_tf(name, vert, geom, frag, 0, 0);
107 }