]> git.tdb.fi Git - libs/gl.git/commitdiff
The GLSL texture function returns a float for shadow samplers
authorMikko Rasa <tdb@tdb.fi>
Sun, 19 Oct 2014 23:33:59 +0000 (02:33 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 19 Oct 2014 23:33:59 +0000 (02:33 +0300)
In contrast with the legacy shadow2D function, which returned vec4.

source/programbuilder.cpp
source/programbuilder.h

index 55d2cf22d9962e3ed1622119a31b1ae592ef761e..3fb9094c180975111f4f43bb7a2e0f7beb1eeb9d 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)
@@ -650,7 +652,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;
@@ -662,7 +664,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;
@@ -674,7 +704,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);
 }
 
 
index 9ca9c8539b54c1046a2d97b29e7b854506c128d2..62d8e07a087f7cebca651ee00b78a947131f96f0 100644 (file)
@@ -147,7 +147,7 @@ private:
        static MatchType name_match(const char *, const char *, const char ** = 0);
        static bool parse_identifier(const char *, unsigned &, unsigned &);
        static std::vector<std::string> extract_identifiers(const char *);
-       static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &);
+       static std::string replace_identifiers(const char *, const std::map<std::string, std::string> &, bool = false);
        std::string create_expression(const ShaderVariable &, const char * = 0) const;
 };