]> git.sesse.net Git - movit/blob - effect.cpp
Store GL_FLOAT FlatInputs as fp32, not fp16.
[movit] / effect.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <utility>
6
7 #include "effect.h"
8 #include "effect_util.h"
9 #include "util.h"
10
11 bool Effect::set_int(const std::string &key, int value)
12 {
13         if (params_int.count(key) == 0) {
14                 return false;
15         }
16         *params_int[key] = value;
17         return true;
18 }
19
20 bool Effect::set_float(const std::string &key, float value)
21 {
22         if (params_float.count(key) == 0) {
23                 return false;
24         }
25         *params_float[key] = value;
26         return true;
27 }
28
29 bool Effect::set_vec2(const std::string &key, const float *values)
30 {
31         if (params_vec2.count(key) == 0) {
32                 return false;
33         }
34         memcpy(params_vec2[key], values, sizeof(float) * 2);
35         return true;
36 }
37
38 bool Effect::set_vec3(const std::string &key, const float *values)
39 {
40         if (params_vec3.count(key) == 0) {
41                 return false;
42         }
43         memcpy(params_vec3[key], values, sizeof(float) * 3);
44         return true;
45 }
46
47 bool Effect::set_vec4(const std::string &key, const float *values)
48 {
49         if (params_vec4.count(key) == 0) {
50                 return false;
51         }
52         memcpy(params_vec4[key], values, sizeof(float) * 4);
53         return true;
54 }
55
56 void Effect::register_int(const std::string &key, int *value)
57 {
58         assert(params_int.count(key) == 0);
59         params_int[key] = value;
60 }
61
62 void Effect::register_float(const std::string &key, float *value)
63 {
64         assert(params_float.count(key) == 0);
65         params_float[key] = value;
66 }
67
68 void Effect::register_vec2(const std::string &key, float *values)
69 {
70         assert(params_vec2.count(key) == 0);
71         params_vec2[key] = values;
72 }
73
74 void Effect::register_vec3(const std::string &key, float *values)
75 {
76         assert(params_vec3.count(key) == 0);
77         params_vec3[key] = values;
78 }
79
80 void Effect::register_vec4(const std::string &key, float *values)
81 {
82         assert(params_vec4.count(key) == 0);
83         params_vec4[key] = values;
84 }
85
86 // Output convenience uniforms for each parameter.
87 // These will be filled in per-frame.
88 std::string Effect::output_convenience_uniforms() const
89 {
90         std::string output = "";
91         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
92              it != params_float.end();
93              ++it) {
94                 char buf[256];
95                 sprintf(buf, "uniform float PREFIX(%s);\n", it->first.c_str());
96                 output.append(buf);
97         }
98         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
99              it != params_vec2.end();
100              ++it) {
101                 char buf[256];
102                 sprintf(buf, "uniform vec2 PREFIX(%s);\n", it->first.c_str());
103                 output.append(buf);
104         }
105         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
106              it != params_vec3.end();
107              ++it) {
108                 char buf[256];
109                 sprintf(buf, "uniform vec3 PREFIX(%s);\n", it->first.c_str());
110                 output.append(buf);
111         }
112         for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
113              it != params_vec4.end();
114              ++it) {
115                 char buf[256];
116                 sprintf(buf, "uniform vec4 PREFIX(%s);\n", it->first.c_str());
117                 output.append(buf);
118         }
119         return output;
120 }
121
122 void Effect::set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num)
123 {
124         for (std::map<std::string, float*>::const_iterator it = params_float.begin();
125              it != params_float.end();
126              ++it) {
127                 set_uniform_float(glsl_program_num, prefix, it->first, *it->second);
128         }
129         for (std::map<std::string, float*>::const_iterator it = params_vec2.begin();
130              it != params_vec2.end();
131              ++it) {
132                 set_uniform_vec2(glsl_program_num, prefix, it->first, it->second);
133         }
134         for (std::map<std::string, float*>::const_iterator it = params_vec3.begin();
135              it != params_vec3.end();
136              ++it) {
137                 set_uniform_vec3(glsl_program_num, prefix, it->first, it->second);
138         }
139         for (std::map<std::string, float*>::const_iterator it = params_vec4.begin();
140              it != params_vec4.end();
141              ++it) {
142                 set_uniform_vec4(glsl_program_num, prefix, it->first, it->second);
143         }
144 }
145
146 void Effect::clear_gl_state() {}