]> git.sesse.net Git - movit/blob - effect.cpp
Remove dead function set_uniform_float_array().
[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_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_array(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const float *values, size_t num_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, num_values, values);
68         check_error();
69 }
70
71 void set_uniform_mat3(GLuint glsl_program_num, const std::string &prefix, const std::string &key, const Matrix3x3 matrix)
72 {
73         GLint location = get_uniform_location(glsl_program_num, prefix, key);
74         if (location == -1) {
75                 return;
76         }
77         check_error();
78
79         // Convert to float (GLSL has no double matrices).
80         float matrixf[9];
81         for (unsigned i = 0; i < 9; ++i) {
82                 matrixf[i] = matrix[i];
83         }
84
85         glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
86         check_error();
87 }
88
89 bool Effect::set_int(const std::string &key, int value)
90 {
91         if (params_int.count(key) == 0) {
92                 return false;
93         }
94         *params_int[key] = value;
95         return true;
96 }
97
98 bool Effect::set_float(const std::string &key, float value)
99 {
100         if (params_float.count(key) == 0) {
101                 return false;
102         }
103         *params_float[key] = value;
104         return true;
105 }
106
107 bool Effect::set_vec2(const std::string &key, const float *values)
108 {
109         if (params_vec2.count(key) == 0) {
110                 return false;
111         }
112         memcpy(params_vec2[key], values, sizeof(float) * 2);
113         return true;
114 }
115
116 bool Effect::set_vec3(const std::string &key, const float *values)
117 {
118         if (params_vec3.count(key) == 0) {
119                 return false;
120         }
121         memcpy(params_vec3[key], values, sizeof(float) * 3);
122         return true;
123 }
124
125 void Effect::register_int(const std::string &key, int *value)
126 {
127         assert(params_int.count(key) == 0);
128         params_int[key] = value;
129 }
130
131 void Effect::register_float(const std::string &key, float *value)
132 {
133         assert(params_float.count(key) == 0);
134         params_float[key] = value;
135 }
136
137 void Effect::register_vec2(const std::string &key, float *values)
138 {
139         assert(params_vec2.count(key) == 0);
140         params_vec2[key] = values;
141 }
142
143 void Effect::register_vec3(const std::string &key, float *values)
144 {
145         assert(params_vec3.count(key) == 0);
146         params_vec3[key] = values;
147 }
148
149 void Effect::register_1d_texture(const std::string &key, float *values, size_t size)
150 {
151         assert(params_tex_1d.count(key) == 0);
152
153         Texture1D tex;
154         tex.values = values;
155         tex.size = size;
156         tex.needs_update = false;
157         glGenTextures(1, &tex.texture_num);
158
159         glBindTexture(GL_TEXTURE_1D, tex.texture_num);
160         check_error();
161         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
162         check_error();
163         glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
164         check_error();
165         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, size, 0, GL_LUMINANCE, GL_FLOAT, values);
166         check_error();
167
168         params_tex_1d[key] = tex;
169 }
170
171 void Effect::invalidate_1d_texture(const std::string &key)
172 {
173         assert(params_tex_1d.count(key) != 0);
174         params_tex_1d[key].needs_update = true;
175 }
176
177 // Output convenience uniforms for each parameter.
178 // These will be filled in per-frame.
179 std::string Effect::output_convenience_uniforms() const
180 {
181         std::string output = "";
182         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
183              it != params_float.end();
184              ++it) {
185                 char buf[256];
186                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
187                 output.append(buf);
188         }
189         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
190              it != params_vec2.end();
191              ++it) {
192                 char buf[256];
193                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
194                 output.append(buf);
195         }
196         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
197              it != params_vec3.end();
198              ++it) {
199                 char buf[256];
200                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
201                 output.append(buf);
202         }
203         for (std::map<std::string, Texture1D>::const_iterator it = params_tex_1d.begin();
204              it != params_tex_1d.end();
205              ++it) {
206                 char buf[256];
207                 sprintf(buf, "uniform sampler1D PREFIX(%s);\n", it->first.c_str());
208                 output.append(buf);
209         }
210         return output;
211 }
212
213 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
214 {
215         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
216              it != params_float.end();
217              ++it) {
218                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
219         }
220         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
221              it != params_vec2.end();
222              ++it) {
223                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
224         }
225         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
226              it != params_vec3.end();
227              ++it) {
228                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
229         }
230
231         for (std::map<std::string, Texture1D>::iterator it = params_tex_1d.begin();
232              it != params_tex_1d.end();
233              ++it) {
234                 glActiveTexture(GL_TEXTURE0 + *sampler_num);
235                 check_error();
236                 glBindTexture(GL_TEXTURE_1D, it->second.texture_num);
237                 check_error();
238
239                 if (it->second.needs_update) {
240                         glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE16F_ARB, it->second.size, 0, GL_LUMINANCE, GL_FLOAT, it->second.values);
241                         check_error();
242                         it->second.needs_update = false;
243                 }
244
245                 set_uniform_int(glsl_program_num, prefix, it->first, *sampler_num);
246                 ++*sampler_num;
247         }
248 }
249
250 void Effect::clear_gl_state() {}