2 #include <msp/core/hash.h>
3 #include <msp/strings/format.h>
4 #include "arb_shader_objects.h"
5 #include "arb_vertex_shader.h"
15 const char *standard_vertex_src[] =
17 "n", "attribute vec3 tangent;\n",
18 "n", "attribute vec3 binormal;\n",
19 "t|n", "varying vec2 v_texcoord;\n",
20 "s", "varying vec3 v_shadowcoord;\n",
21 "!lm", "varying vec4 v_color;\n",
22 "l!n", "varying vec3 v_normal;\n",
23 "l", "varying vec3 v_light_dir;\n",
24 "p|e", "varying vec3 v_eye_dir;\n",
25 "r", "vec4 transform_vertex(vec4);\n",
26 "lr", "vec3 transform_normal(vec3);\n",
29 "r", "\tvec4 eye_pos = transform_vertex(gl_Vertex);\n",
30 "!r", "\tvec4 eye_pos = gl_ModelViewMatrix*gl_Vertex;\n",
31 0, "\tgl_Position = gl_ProjectionMatrix*eye_pos;\n",
32 "lr", "\tvec3 eye_normal = transform_normal(gl_Normal);\n",
33 "l!r", "\tvec3 eye_normal = gl_NormalMatrix*gl_Normal;\n",
34 "l!n", "\tv_normal = eye_normal;\n",
35 "nr", "\tvec3 eye_tangent = transform_normal(tangent);\n",
36 "n!r", "\tvec3 eye_tangent = gl_NormalMatrix*tangent;\n",
37 "nr", "\tvec3 eye_binormal = transform_normal(binormal);\n",
38 "n!r", "\tvec3 eye_binormal = gl_NormalMatrix*binormal;\n",
39 "l", "\tvec3 eye_light_dir = normalize(gl_LightSource[0].position.xyz-eye_pos.xyz*gl_LightSource[0].position.w);\n",
40 "n", "\tv_light_dir = vec3(dot(eye_tangent, eye_light_dir), dot(eye_binormal, eye_light_dir), dot(eye_normal, eye_light_dir));\n",
41 "l!n", "\tv_light_dir = eye_light_dir;\n",
42 "p|e", "\tvec3 eye_dir = -normalize(eye_pos.xyz);\n",
43 "p|en", "\tv_eye_dir = vec3(dot(eye_tangent, eye_dir), dot(eye_binormal, eye_dir), dot(eye_normal, eye_dir));\n",
44 "p|e!n", "\tv_eye_dir = eye_dir;\n",
45 "t|n", "\tv_texcoord = gl_MultiTexCoord0.xy;\n",
46 "s", "\tv_shadowcoord = vec3(dot(gl_EyePlaneS[3], eye_pos), dot(gl_EyePlaneT[3], eye_pos), dot(gl_EyePlaneR[3], eye_pos));\n",
47 "!lm", "\tv_color = gl_Color;\n",
52 const char *standard_fragment_src[] =
54 "t", "uniform sampler2D texture;\n",
55 "n", "uniform sampler2D normalmap;\n",
56 "s", "uniform sampler2DShadow shadow;\n",
57 "e", "uniform samplerCube environment;\n",
58 "e", "uniform float reflectivity;\n",
59 "t|n", "varying vec2 v_texcoord;\n",
60 "s", "varying vec3 v_shadowcoord;\n",
61 "!lm", "varying vec4 v_color;\n",
62 "l!n", "varying vec3 v_normal;\n",
63 "l", "varying vec3 v_light_dir;\n",
64 "p|e", "varying vec3 v_eye_dir;\n",
67 "n", "\tvec3 n_normal = texture2D(normalmap, v_texcoord).xyz*2.0-1.0;\n",
68 "l!n", "\tvec3 n_normal = normalize(v_normal);\n",
69 "l", "\tfloat l_diffuse = max(dot(n_normal, normalize(v_light_dir)), 0.0);\n",
70 "p", "\tvec3 half_dir = normalize(v_eye_dir+v_light_dir);\n",
71 "p", "\tfloat l_specular = pow(max(dot(half_dir, n_normal), 0.0), gl_FrontMaterial.shininess);\n",
72 "s", "\tfloat l_shadow = shadow2D(shadow, v_shadowcoord).r;\n",
73 /* XXX This is incorrect with normal mapping, since the vectors are in TBN
74 space but environment map is expected to be in eye space */
75 "e", "\tvec4 reflection = textureCube(environment, n_normal*(dot(n_normal, v_eye_dir)*2.0)-v_eye_dir);\n",
76 0, "\tgl_FragColor = ",
77 "!t!l!m", "vec4(1.0, 1.0, 1.0, 1.0)",
78 "t", "texture2D(texture, v_texcoord)",
82 "lm", "*gl_FrontLightProduct[0].diffuse",
84 "pm", "*gl_FrontLightProduct[0].specular",
87 "lm", "+gl_FrontLightModelProduct.sceneColor",
89 "e", "+reflection*reflectivity",
105 Program::Program(const StandardFeatures &features)
109 add_standard_shaders(features);
110 if(!features.transform)
114 Program::Program(const string &vert, const string &frag)
118 attach_shader_owned(new Shader(VERTEX_SHADER, vert));
119 attach_shader_owned(new Shader(FRAGMENT_SHADER, frag));
125 static RequireExtension _ext("GL_ARB_shader_objects");
128 id = glCreateProgramObjectARB();
133 for(list<Shader *>::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
135 glDeleteObjectARB(id);
138 void Program::attach_shader(Shader &shader)
140 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
142 glAttachObjectARB(id, shader.get_id());
143 shaders.push_back(&shader);
147 void Program::attach_shader_owned(Shader *shader)
149 attach_shader(*shader);
150 if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
151 owned_data.push_back(shader);
154 void Program::detach_shader(Shader &shader)
156 list<Shader *>::iterator i = remove(shaders.begin(), shaders.end(), &shader);
159 shaders.erase(i, shaders.end());
160 glDetachObjectARB(id, shader.get_id());
164 void Program::add_standard_shaders(const StandardFeatures &features)
166 string flags = features.create_flags();
167 string vertex_src = process_standard_source(standard_vertex_src, flags);
168 string fragment_src = process_standard_source(standard_fragment_src, flags);
169 attach_shader_owned(new Shader(VERTEX_SHADER, vertex_src));
170 attach_shader_owned(new Shader(FRAGMENT_SHADER, fragment_src));
173 string Program::process_standard_source(const char **source, const string &flags)
177 for(unsigned i=0; source[i+1]; i+=2)
183 for(const char *c=source[i]; *c; ++c)
185 if(*c>='a' && *c<='z')
187 bool found = (flags.find(*c)!=string::npos);
189 cond = (cond || found);
191 cond = (cond && !found);
193 cond = (cond && found);
204 result += source[i+1];
210 void Program::bind_attribute(unsigned index, const string &name)
212 static RequireExtension _ext("GL_ARB_vertex_shader");
213 glBindAttribLocationARB(id, index, name.c_str());
218 for(list<Shader *>::iterator i=shaders.begin(); i!=shaders.end(); ++i)
219 if(!(*i)->is_compiled())
224 glLinkProgramARB(id);
226 glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value);
227 if(!(linked = value))
228 throw compile_error(get_info_log());
230 glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value);
231 for(int i=0; i<value; ++i)
236 glGetActiveUniformARB(id, i, 128, &len, &info.size, &info.type, name);
240 info.location = glGetUniformLocationARB(id, name);
241 uniforms[name] = info;
245 string layout_descriptor;
246 for(map<string, UniformInfo>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
247 if(i->second.location>=0)
249 if(!layout_descriptor.empty())
250 layout_descriptor += '\n';
251 layout_descriptor += format("%d:%s:%x", i->second.location, i->second.name, i->second.type);
253 uniform_layout_hash = hash32(layout_descriptor);
256 string Program::get_info_log() const
259 glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
260 char *buf = new char[len+1];
261 glGetInfoLogARB(id, len+1, &len, buf);
262 string log(buf, len);
267 void Program::bind() const
270 throw invalid_operation("Program::bind");
272 if(!set_current(this))
275 glUseProgramObjectARB(id);
278 int Program::get_uniform_location(const string &n) const
280 map<string, UniformInfo>::const_iterator i = uniforms.find(n);
281 if(i==uniforms.end())
283 if(n[n.size()-1]==']')
285 string::size_type open_bracket = n.rfind('[');
286 if(open_bracket!=string::npos)
288 /* The requested name looks like an array. glGetActiveUniform only
289 gives us the first element of the array, so try to look that up and
291 int offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
292 i = uniforms.find(n.substr(0, open_bracket)+"[0]");
293 if(i!=uniforms.end() && offset<i->second.size)
294 return i->second.location+offset;
300 return i->second.location;
303 void Program::unbind()
308 glUseProgramObjectARB(0);
312 Program::StandardFeatures::StandardFeatures():
323 string Program::StandardFeatures::create_flags() const
349 Program::Loader::Loader(Program &p):
350 DataFile::ObjectLoader<Program>(p)
352 add("attribute", &Loader::attribute);
353 add("fragment_shader", &Loader::fragment_shader);
354 add("standard", &Loader::standard);
355 add("vertex_shader", &Loader::vertex_shader);
358 void Program::Loader::finish()
363 void Program::Loader::attribute(unsigned i, const string &n)
365 obj.bind_attribute(i, n);
368 void Program::Loader::fragment_shader(const string &src)
370 obj.attach_shader_owned(new Shader(FRAGMENT_SHADER, src));
373 void Program::Loader::standard()
375 StandardFeatures feat;
377 obj.add_standard_shaders(feat);
380 void Program::Loader::vertex_shader(const string &src)
382 obj.attach_shader_owned(new Shader(VERTEX_SHADER, src));
386 Program::StandardFeatures::Loader::Loader(StandardFeatures &f):
387 DataFile::ObjectLoader<StandardFeatures>(f)
389 add("lighting", &StandardFeatures::lighting);
390 add("material", &StandardFeatures::material);
391 add("normalmap", &StandardFeatures::normalmap);
392 add("shadow", &StandardFeatures::shadow);
393 add("specular", &StandardFeatures::specular);
394 add("texture", &StandardFeatures::texture);
395 add("transform", &StandardFeatures::transform);