]> git.sesse.net Git - movit/blob - effect.cpp
Minor stylistic fix in the deconvolution test.
[movit] / effect.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "effect.h"
5 #include "effect_chain.h"
6 #include "util.h"
7
8 #include "opengl.h"
9
10 GLint get_uniform_location(GLuint glsl_program_num, const std::string &prefix, const std::string &key)
11 {
12         std::string name = prefix + "_" + key;
13         return glGetUniformLocation(glsl_program_num, name.c_str());
14 }
15
16 void set_uniform_int(GLuint glsl_program_num, const std::string &prefix, const std::string &key, int value)
17 {
18         GLint location = get_uniform_location(glsl_program_num, prefix, key);
19         if (location == -1) {
20                 return;
21         }
22         check_error();
23         glUniform1i(location, value);
24         check_error();
25 }
26
27 void set_uniform_float(GLuint glsl_program_num, const std::string &prefix, const std::string &key, float value)
28 {
29         GLint location = get_uniform_location(glsl_program_num, prefix, key);
30         if (location == -1) {
31                 return;
32         }
33         check_error();
34         glUniform1f(location, value);
35         check_error();
36 }
37
38 void set_uniform_float_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
39 {
40         GLint location = get_uniform_location(glsl_program_num, prefix, key);
41         if (location == -1) {
42                 return;
43         }
44         check_error();
45         glUniform1fv(location, num_values, values);
46         check_error();
47 }
48
49 void set_uniform_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
50 {
51         GLint location = get_uniform_location(glsl_program_num, prefix, key);
52         if (location == -1) {
53                 return;
54         }
55         check_error();
56         glUniform2fv(location, 1, values);
57         check_error();
58 }
59
60 void set_uniform_vec3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
61 {
62         GLint location = get_uniform_location(glsl_program_num, prefix, key);
63         if (location == -1) {
64                 return;
65         }
66         check_error();
67         glUniform3fv(location, 1, values);
68         check_error();
69 }
70
71 void set_uniform_vec4_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_values)
72 {
73         GLint location = get_uniform_location(glsl_program_num, prefix, key);
74         if (location == -1) {
75                 return;
76         }
77         check_error();
78         glUniform4fv(location, num_values, values);
79         check_error();
80 }
81
82 void set_uniform_mat3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const Matrix3x3 matrix)
83 {
84         GLint location = get_uniform_location(glsl_program_num, prefix, key);
85         if (location == -1) {
86                 return;
87         }
88         check_error();
89
90         // Convert to float (GLSL has no double matrices).
91         float matrixf[9];
92         for (unsigned i = 0; i < 9; ++i) {
93                 matrixf[i] = matrix[i];
94         }
95
96         glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
97         check_error();
98 }
99
100 bool Effect::set_int(const std::string &key, int value)
101 {
102         if (params_int.count(key) == 0) {
103                 return false;
104         }
105         *params_int[key] = value;
106         return true;
107 }
108
109 bool Effect::set_float(const std::string &key, float value)
110 {
111         if (params_float.count(key) == 0) {
112                 return false;
113         }
114         *params_float[key] = value;
115         return true;
116 }
117
118 bool Effect::set_vec2(const std::string &key, const float *values)
119 {
120         if (params_vec2.count(key) == 0) {
121                 return false;
122         }
123         memcpy(params_vec2[key], values, sizeof(float) * 2);
124         return true;
125 }
126
127 bool Effect::set_vec3(const std::string &key, const float *values)
128 {
129         if (params_vec3.count(key) == 0) {
130                 return false;
131         }
132         memcpy(params_vec3[key], values, sizeof(float) * 3);
133         return true;
134 }
135
136 void Effect::register_int(const std::string &key, int *value)
137 {
138         assert(params_int.count(key) == 0);
139         params_int[key] = value;
140 }
141
142 void Effect::register_float(const std::string &key, float *value)
143 {
144         assert(params_float.count(key) == 0);
145         params_float[key] = value;
146 }
147
148 void Effect::register_vec2(const std::string &key, float *values)
149 {
150         assert(params_vec2.count(key) == 0);
151         params_vec2[key] = values;
152 }
153
154 void Effect::register_vec3(const std::string &key, float *values)
155 {
156         assert(params_vec3.count(key) == 0);
157         params_vec3[key] = values;
158 }
159
160 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
161 {
162         assert(params_tex_1d.count(key) == 0);
163
164         Texture1D tex;
165         tex.values = values;
166         tex.size = size;
167         tex.needs_update = false;
168         glGenTextures(1, &tex.texture_num);
169
170         glBindTexture(GL_TEXTURE_1D, tex.texture_num);
171         check_error();
172         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
173         check_error();
174         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
175         check_error();
176         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
177         check_error();
178
179         params_tex_1d[key] = tex;
180 }
181
182 void Effect::invalidate_1d_texture(const std::string &key)
183 {
184         assert(params_tex_1d.count(key) != 0);
185         params_tex_1d[key].needs_update = true;
186 }
187
188 // Output convenience uniforms for each parameter.
189 // These will be filled in per-frame.
190 std::string Effect::output_convenience_uniforms() const
191 {
192         std::string output = "";
193         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
194              it != params_float.end();
195              ++it) {
196                 char buf[256];
197                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
198                 output.append(buf);
199         }
200         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
201              it != params_vec2.end();
202              ++it) {
203                 char buf[256];
204                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
205                 output.append(buf);
206         }
207         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
208              it != params_vec3.end();
209              ++it) {
210                 char buf[256];
211                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
212                 output.append(buf);
213         }
214         for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
215              it != params_tex_1d.end();
216              ++it) {
217                 char buf[256];
218                 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
219                 output.append(buf);
220         }
221         return output;
222 }
223
224 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
225 {
226         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
227              it != params_float.end();
228              ++it) {
229                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
230         }
231         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
232              it != params_vec2.end();
233              ++it) {
234                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
235         }
236         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
237              it != params_vec3.end();
238              ++it) {
239                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
240         }
241
242         for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
243              it != params_tex_1d.end();
244              ++it) {
245                 glActiveTexture(GL_TEXTURE0 + *sampler_num);
246                 check_error();
247                 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
248                 check_error();
249
250                 if (it->second.needs_update) {
251                         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
252                         check_error();
253                         it->second.needs_update = false;
254                 }
255
256                 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
257                 ++*sampler_num;
258         }
259 }
260
261 void Effect::clear_gl_state() {}