]> git.sesse.net Git - movit/blob - effect_util.cpp
Use UBOs instead of glUniform. Work in progress; no clear wins seen yet.
[movit] / effect_util.cpp
1 #include <epoxy/gl.h>
2 #include <Eigen/Core>
3 #include <stddef.h>
4 #include <string>
5 #include "util.h"
6
7 using namespace std;
8
9 namespace movit {
10
11 GLint get_uniform_location(GLuint glsl_program_num, const string &prefix, const string &key)
12 {
13         string name = prefix + "_" + key;
14         return glGetUniformLocation(glsl_program_num, name.c_str());
15 }
16
17 void get_uniform_offset_and_size(GLuint glsl_program_num, const string &prefix, const string &key, GLint *offset, GLint *size)
18 {
19         string name = prefix + "_" + key;
20         GLuint index;
21         const GLchar *name_cstr = name.c_str();
22         glGetUniformIndices(glsl_program_num, 1, &name_cstr, &index);
23         check_error();
24         if (index == GL_INVALID_INDEX) {
25                 *offset = -1;
26                 *size = 0;
27                 return;
28         }
29         glGetActiveUniformsiv(glsl_program_num, 1, &index, GL_UNIFORM_OFFSET, offset);
30         check_error();
31         glGetActiveUniformsiv(glsl_program_num, 1, &index, GL_UNIFORM_SIZE, size);
32         check_error();
33 }
34
35 void set_uniform_int(GLuint glsl_program_num, const string &prefix, const string &key, int value)
36 {
37         GLint location = get_uniform_location(glsl_program_num, prefix, key);
38         if (location == -1) {
39                 return;
40         }
41         check_error();
42         glUniform1i(location, value);
43         check_error();
44 }
45
46 void set_uniform_float(GLuint glsl_program_num, const string &prefix, const string &key, float value)
47 {
48         GLint location = get_uniform_location(glsl_program_num, prefix, key);
49         if (location == -1) {
50                 return;
51         }
52         check_error();
53         glUniform1f(location, value);
54         check_error();
55 }
56
57 void set_uniform_vec2(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
58 {
59         GLint location = get_uniform_location(glsl_program_num, prefix, key);
60         if (location == -1) {
61                 return;
62         }
63         check_error();
64         glUniform2fv(location, 1, values);
65         check_error();
66 }
67
68 void set_uniform_vec3(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
69 {
70         GLint location = get_uniform_location(glsl_program_num, prefix, key);
71         if (location == -1) {
72                 return;
73         }
74         check_error();
75         glUniform3fv(location, 1, values);
76         check_error();
77 }
78
79 void set_uniform_vec4(GLuint glsl_program_num, const string &prefix, const string &key, const float *values)
80 {
81         GLint location = get_uniform_location(glsl_program_num, prefix, key);
82         if (location == -1) {
83                 return;
84         }
85         check_error();
86         glUniform4fv(location, 1, values);
87         check_error();
88 }
89
90 void set_uniform_vec2_array(GLuint glsl_program_num, const string &prefix, const string &key, const float *values, size_t num_values)
91 {
92         GLint location = get_uniform_location(glsl_program_num, prefix, key);
93         if (location == -1) {
94                 return;
95         }
96         check_error();
97         glUniform2fv(location, num_values, values);
98         check_error();
99 }
100
101 void set_uniform_vec4_array(GLuint glsl_program_num, const string &prefix, const string &key, const float *values, size_t num_values)
102 {
103         GLint location = get_uniform_location(glsl_program_num, prefix, key);
104         if (location == -1) {
105                 return;
106         }
107         check_error();
108         glUniform4fv(location, num_values, values);
109         check_error();
110 }
111
112 void set_uniform_mat3(GLuint glsl_program_num, const string &prefix, const string &key, const Eigen::Matrix3d& matrix)
113 {
114         GLint location = get_uniform_location(glsl_program_num, prefix, key);
115         if (location == -1) {
116                 return;
117         }
118         check_error();
119
120         // Convert to float (GLSL has no double matrices).
121         float matrixf[9];
122         for (unsigned y = 0; y < 3; ++y) {
123                 for (unsigned x = 0; x < 3; ++x) {
124                         matrixf[y + x * 3] = matrix(y, x);
125                 }
126         }
127
128         glUniformMatrix3fv(location, 1, GL_FALSE, matrixf);
129         check_error();
130 }
131
132 }  // namespace movit