Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Added compatibility to the sfx effects.  (Read 4049 times)

0 Members and 1 Guest are viewing this topic.

Dorth

  • Newbie
  • *
  • Posts: 1
    • View Profile
Added compatibility to the sfx effects.
« on: April 10, 2009, 12:20:23 am »
Hi there. New user, like the sfml a lot. I had to implement a small patch to keep compatibility with glsl shaders I wanted to use. It keeps backward compatibility while adding compatibility with most fragment shaders and a tad more optimization (removing empty lines or commented lines if the // is at the beginning of the line.)

Code: [Select]

Index: src/SFML/Graphics/PostFX.cpp
===================================================================
--- src/SFML/Graphics/PostFX.cpp (revision 1072)
+++ src/SFML/Graphics/PostFX.cpp (working copy)
@@ -339,7 +339,7 @@
 
     // Variable declarations
     std::string Line;
-    while (std::getline(File, Line) && (Line.substr(0, 6) != "effect"))
+    while (std::getline(File, Line) && (Line.substr(0, 6) != "effect") && (Line.substr(0,11) != "void main()"))
     {
         // Remove the ending '\r', if any
         if (!Line.empty() && (Line[Line.size() - 1] == '\r'))
@@ -349,10 +349,16 @@
         if (Line == "")
             continue;
 
+ // Skip comments
+ if (Line.substr(0,2) == "//")
+ {
+ continue;
+ }
+
         // Extract variables type and name and convert them
         std::string Type, Name;
         std::istringstream iss(Line);
-        if (!(iss >> Type >> Name))
+        if (!(iss >> Type) || ((Name = (iss.str()).substr(Type.size())) == ""))
         {
             std::cerr << "Post-effect error : invalid declaration (should be \"[type][name]\")" << std::endl
                       << "> " << Line << std::endl;
@@ -371,8 +377,16 @@
             Out += "uniform sampler2D " + Name + ";\n";
             myTextures.insert(Name);
         }
-        else
+        else if (Type == "uniform")
         {
+ Out += Type + " " + Name + ";\n";
+ if (Name.substr(0, 9) != "sampler2D")
+ {
+ myTextures.insert(Name.substr(10));
+ }
+        }
+ else
+        {
             // Other types are just copied to output with "uniform" prefix
             Out += "uniform " + Type + " " + Name + ";\n";
         }
@@ -382,21 +396,32 @@
     Out += "void main()\n";
     while (std::getline(File, Line))
     {
+ if (Line == "" || Line.substr(0,2) == "//")
+ {
+ continue;
+ }
+
         // Replace any texture lookup "T(" by "texture2D(T, "
         for (std::set<std::string>::const_iterator i = myTextures.begin(); i != myTextures.end(); ++i)
         {
-            std::string::size_type Pos = Line.find(*i);
+            std::string::size_type Pos = Line.find(*i + "(");
             if (Pos != std::string::npos)
+ {
                 Line.replace(Pos, i->size() + 1, "texture2D(" + *i + ", ");
+ }
         }
 
         // Replace "_in" by "gl_TexCoord[0].xy"
         for (std::string::size_type Pos = Line.find("_in"); Pos != std::string::npos; Pos = Line.find("_in"))
+ {
             Line.replace(Pos, 3, "gl_TexCoord[0].xy");
+ }
 
         // Replace "_out" by "gl_FragColor"
         for (std::string::size_type Pos = Line.find("_out"); Pos != std::string::npos; Pos = Line.find("_out"))
+ {
             Line.replace(Pos, 4, "gl_FragColor");
+ }
 
         // Write modified line to output string
         Out += Line + "\n";

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Added compatibility to the sfx effects.
« Reply #1 on: April 10, 2009, 11:21:54 am »
Thanks a lot :)

Actually, I plan to remove all the extra preprocessing and directly support the GLSL syntax. Shader-related classes may change a lot in SFML 2.0.
Laurent Gomila - SFML developer

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Added compatibility to the sfx effects.
« Reply #2 on: October 29, 2009, 06:52:23 pm »
Will you make it so you can add vertex shaders too?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Added compatibility to the sfx effects.
« Reply #3 on: October 29, 2009, 07:51:14 pm »
Vertex shaders wouldn't make sense

- Sprites: I would have to tesselate them, thus creating much more geometry than necessary

- String: one quad per character; what can you do with that in a vertex shader?

- Shape: triangle strip, fan or quad depending on the number of points, and completely an internal detail :)

Plus, I may have to change the geometry of any of them in the future for other reasons (it happened recently for sf::Shape), making usage of vertex shaders inconsistent.
Laurent Gomila - SFML developer

 

anything