]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programbuilder.cpp
Copy ProgramData::uniform_slots in copy constructor and operator=
[libs/gl.git] / source / programbuilder.cpp
index 8215220081fd2cb6a32a543877bd208e3823fc7f..540518e49bf4223594f2e53cdbd8b6cb3be55f11 100644 (file)
@@ -95,7 +95,7 @@ const ProgramBuilder::VariableDefinition ProgramBuilder::standard_variables[] =
        { FRAGMENT, "l_diffuse[i]", "float", "max(dot(n_zzz_normal, n_zzz_light_dir[i]), 0.0)", 0 },
 
        { FRAGMENT, "l_shadow", "float", "mix(1.0, shadow_sample, shadow_darkness)", 0 },
-       { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex).r", 0 },
+       { FRAGMENT, "shadow_sample", "float", "shadow2D(shadow, shd_vertex)", 0 },
 
        { FRAGMENT, "zzz_reflect_dir", "vec3", "reflect(zzz_incident_dir, n_zzz_normal)", 0 },
        { FRAGMENT, "n_zzz_half_vec[i]", "vec3", "normalize(zzz_light_dir[i]-zzz_incident_dir)", 0 },
@@ -241,6 +241,8 @@ ProgramBuilder::ProgramBuilder(const StandardFeatures &f):
                aliases["texture2D"] = "texture";
                aliases["shadow2D"] = "texture";
        }
+       else
+               aliases["shadow2D"] = "shadow2D(...).r";
 }
 
 void ProgramBuilder::set_optimize(bool o)
@@ -374,6 +376,7 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
 {
        string source;
 
+       bool legacy_qualifiers = features.legacy || (get_gl_api()==OPENGL_ES2 && !(get_glsl_version()>=Version(3, 0)));
        bool use_blocks = !features.legacy && ARB_uniform_buffer_object;
 
        if(!features.legacy)
@@ -382,6 +385,7 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
                {
                        if(use_blocks)
                                source += "#version 300 es\n";
+                       source += "precision mediump float;\n";
                }
                else
                {
@@ -427,13 +431,13 @@ string ProgramBuilder::create_source(const list<ShaderVariable *> &variables, Va
 
                if(interface&INPUT)
                {
-                       const char *qualifier = (features.legacy ? scope==VERTEX ? "attribute" : "varying" : "in");
+                       const char *qualifier = (legacy_qualifiers ? scope==VERTEX ? "attribute" : "varying" : "in");
                        source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope-1]));
                }
 
                if(interface&OUTPUT)
                {
-                       const char *qualifier = (features.legacy ? "varying" : "out");
+                       const char *qualifier = (legacy_qualifiers ? "varying" : "out");
                        source += format("%s %s;\n", qualifier, (*i)->create_declaration(interfaces[scope]));
                }
        }
@@ -649,7 +653,7 @@ vector<string> ProgramBuilder::extract_identifiers(const char *expression)
        return result;
 }
 
-string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map)
+string ProgramBuilder::replace_identifiers(const char *expression, const map<string, string> &replace_map, bool with_functions)
 {
        string result;
        const char *ptr = expression;
@@ -661,7 +665,35 @@ string ProgramBuilder::replace_identifiers(const char *expression, const map<str
                string identifier(ptr+start, length);
                map<string, string>::const_iterator i = replace_map.find(identifier);
                if(i!=replace_map.end())
+               {
+                       if(with_functions && ptr[start+length]=='(')
+                       {
+                               string::size_type lparen = i->second.find('(');
+                               string::size_type rparen = i->second.rfind(')');
+                               if(lparen!=string::npos && rparen!=string::npos)
+                               {
+                                       unsigned level = 1;
+                                       unsigned j;
+                                       for(j=start+length+1; (ptr[j] && level); ++j)
+                                       {
+                                               level += (ptr[j]=='(');
+                                               level -= (ptr[j]==')');
+                                       }
+
+                                       if(!level)
+                                       {
+                                               string subexpr(ptr+start+length, ptr+j);
+                                               result += i->second.substr(0, lparen);
+                                               result += replace_identifiers(subexpr.c_str(), replace_map, with_functions);
+                                               result += i->second.substr(rparen+1);
+                                               ptr += j;
+                                               continue;
+                                       }
+                               }
+                       }
+
                        result += i->second;
+               }
                else
                        result += identifier;
                ptr += start+length;
@@ -673,7 +705,7 @@ string ProgramBuilder::replace_identifiers(const char *expression, const map<str
 string ProgramBuilder::create_expression(const ShaderVariable &var, const char *loop) const
 {
        string expr = var.create_expression(loop);
-       return replace_identifiers(expr.c_str(), aliases);
+       return replace_identifiers(expr.c_str(), aliases, true);
 }
 
 
@@ -687,7 +719,7 @@ ProgramBuilder::StandardFeatures::StandardFeatures():
        normalmap(false),
        shadow(false),
        reflection(false),
-       legacy(!(get_glsl_version()>=Version(1, 30)))
+       legacy(get_gl_api()==OPENGL && !(get_glsl_version()>=Version(1, 30)))
 { }
 
 string ProgramBuilder::StandardFeatures::create_flags() const