]> git.sesse.net Git - movit/blob - effect.cpp
In resizing effects, add the notion of a “virtual output size”.
[movit] / effect.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5
6 #include "effect.h"
7 #include "effect_chain.h"
8 #include "util.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_vec2(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values)
39 {
40         GLint location = get_uniform_location(glsl_program_num, prefix, key);
41         if (location == -1) {
42                 return;
43         }
44         check_error();
45         glUniform2fv(location, 1, values);
46         check_error();
47 }
48
49 void set_uniform_vec3(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         glUniform3fv(location, 1, values);
57         check_error();
58 }
59
60 void set_uniform_vec4(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         glUniform4fv(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 Eigen::Matrix3d& 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 y = 0; y < 3; ++y) {
93                 for (unsigned x = 0; x < 3; ++x) {
94                         matrixf[y + x * 3] = matrix(y, x);
95                 }
96         }
97
98         glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
99         check_error();
100 }
101
102 bool Effect::set_int(const std::string &key, int value)
103 {
104         if (params_int.count(key) == 0) {
105                 return false;
106         }
107         *params_int[key] = value;
108         return true;
109 }
110
111 bool Effect::set_float(const std::string &key, float value)
112 {
113         if (params_float.count(key) == 0) {
114                 return false;
115         }
116         *params_float[key] = value;
117         return true;
118 }
119
120 bool Effect::set_vec2(const std::string &key, const float *values)
121 {
122         if (params_vec2.count(key) == 0) {
123                 return false;
124         }
125         memcpy(params_vec2[key], values, sizeof(float) * 2);
126         return true;
127 }
128
129 bool Effect::set_vec3(const std::string &key, const float *values)
130 {
131         if (params_vec3.count(key) == 0) {
132                 return false;
133         }
134         memcpy(params_vec3[key], values, sizeof(float) * 3);
135         return true;
136 }
137
138 bool Effect::set_vec4(const std::string &key, const float *values)
139 {
140         if (params_vec4.count(key) == 0) {
141                 return false;
142         }
143         memcpy(params_vec4[key], values, sizeof(float) * 4);
144         return true;
145 }
146
147 void Effect::register_int(const std::string &key, int *value)
148 {
149         assert(params_int.count(key) == 0);
150         params_int[key] = value;
151 }
152
153 void Effect::register_float(const std::string &key, float *value)
154 {
155         assert(params_float.count(key) == 0);
156         params_float[key] = value;
157 }
158
159 void Effect::register_vec2(const std::string &key, float *values)
160 {
161         assert(params_vec2.count(key) == 0);
162         params_vec2[key] = values;
163 }
164
165 void Effect::register_vec3(const std::string &key, float *values)
166 {
167         assert(params_vec3.count(key) == 0);
168         params_vec3[key] = values;
169 }
170
171 void Effect::register_vec4(const std::string &key, float *values)
172 {
173         assert(params_vec4.count(key) == 0);
174         params_vec4[key] = values;
175 }
176
177 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
178 {
179         assert(params_tex_1d.count(key) == 0);
180
181         Texture1D tex;
182         tex.values = values;
183         tex.size = size;
184         tex.needs_update = false;
185         glGenTextures(1, &tex.texture_num);
186
187         glBindTexture(GL_TEXTURE_1D, tex.texture_num);
188         check_error();
189         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190         check_error();
191         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
192         check_error();
193         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
194         check_error();
195
196         params_tex_1d[key] = tex;
197 }
198
199 void Effect::invalidate_1d_texture(const std::string &key)
200 {
201         assert(params_tex_1d.count(key) != 0);
202         params_tex_1d[key].needs_update = true;
203 }
204
205 // Output convenience uniforms for each parameter.
206 // These will be filled in per-frame.
207 std::string Effect::output_convenience_uniforms() const
208 {
209         std::string output = "";
210         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
211              it != params_float.end();
212              ++it) {
213                 char buf[256];
214                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
215                 output.append(buf);
216         }
217         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
218              it != params_vec2.end();
219              ++it) {
220                 char buf[256];
221                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
222                 output.append(buf);
223         }
224         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
225              it != params_vec3.end();
226              ++it) {
227                 char buf[256];
228                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
229                 output.append(buf);
230         }
231         for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
232              it != params_vec4.end();
233              ++it) {
234                 char buf[256];
235                 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
236                 output.append(buf);
237         }
238         for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
239              it != params_tex_1d.end();
240              ++it) {
241                 char buf[256];
242                 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
243                 output.append(buf);
244         }
245         return output;
246 }
247
248 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
249 {
250         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
251              it != params_float.end();
252              ++it) {
253                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
254         }
255         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
256              it != params_vec2.end();
257              ++it) {
258                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
259         }
260         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
261              it != params_vec3.end();
262              ++it) {
263                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
264         }
265         for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
266              it != params_vec4.end();
267              ++it) {
268                 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
269         }
270
271         for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
272              it != params_tex_1d.end();
273              ++it) {
274                 glActiveTexture(GL_TEXTURE0 + *sampler_num);
275                 check_error();
276                 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
277                 check_error();
278
279                 if (it->second.needs_update) {
280                         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
281                         check_error();
282                         it->second.needs_update = false;
283                 }
284
285                 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
286                 ++*sampler_num;
287         }
288 }
289
290 void Effect::clear_gl_state() {}