]> git.tdb.fi Git - libs/gl.git/commitdiff
Generate the entire shaderlib into a single file
authorMikko Rasa <tdb@tdb.fi>
Fri, 18 Nov 2016 21:16:54 +0000 (23:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 18 Nov 2016 22:00:10 +0000 (00:00 +0200)
Generating lots of small files seemed rather silly, and I found no good
way to automatically generate the builtin init code.

Build
scripts/resgen.py
source/resources.cpp

diff --git a/Build b/Build
index fcdb63284618d3829180edbb18675b35045da118..610fd18cc6c262fc07dc9e78650a5b493eddc30e 100644 (file)
--- a/Build
+++ b/Build
@@ -34,6 +34,7 @@ package "mspgl"
                in_suffix ".glsl";
                out_suffix ".cpp";
                command "scripts/resgen.py";
+               processing_unit DIRECTORY;
        };
 
        library "mspgl"
index 16cfbfb9a76e99c7fc3c09d6130071030423aacf..110055cf4980ea815684849604f3a44ca3a9b097 100755 (executable)
@@ -12,30 +12,47 @@ def makename(text):
                        result += '_'
        return result
 
-def escape(text):
-       result = ""
-       for c in text:
-               if c=='\t':
-                       result += "\\t"
-               elif c=='\n':
-                       result += "\\n"
-               elif c=='"':
-                       result += "\\\""
-               else:
-                       result += c
-       return result
+def escape_char(c):
+       if c=='\t':
+               return "\\t"
+       elif c=='\n':
+               return "\\n"
+       elif c=='"':
+               return "\\\""
+       else:
+               return c
 
-name = makename(os.path.split(sys.argv[1])[1])
-lines = open(sys.argv[1]).readlines()
-out = open(sys.argv[2], "w")
-out.write("""namespace Msp {
+out = open(sys.argv[-1], "w")
+
+out.write("""#include <msp/datafile/builtinsource.h>
+
+namespace Msp {
 namespace GL {
+
 """)
-out.write("extern const char {}_data[] =\n".format(name))
-for l in lines:
-       out.write("\t\"{}\"\n".format(escape(l)))
-out.write(""";
 
+objects = {}
+for fn in sys.argv[1:-1]:
+       in_base = os.path.split(fn)[1]
+       name = makename(in_base)+"_data"
+       objects[in_base] = name
+       out.write("const char {}[] =\n".format(name))
+       data = open(fn).read()
+       line = ""
+       for c in data:
+               line += escape_char(c)
+               if len(line)>=68:
+                       out.write("\t\"{}\"\n".format(line))
+                       line = ""
+       out.write("\t\"{}\";\n\n".format(line))
+
+out_base = os.path.splitext(os.path.split(sys.argv[-1])[1])[0]
+out.write("void init_{}(DataFile::BuiltinSource &source)\n{{\n".format(makename(out_base)))
+for n, d in objects.items():
+       out.write("\tsource.add_object(\"{}\", {});\n".format(n, d))
+out.write("}\n")
+
+out.write("""
 } // namespace GL
 } // namespace Msp
 """)
index 48e986cea064b2742eadf3d2625f418ccf1cef24..8f3b4b8ba29fd3c49d9039d7747f3099ec821d8e 100644 (file)
@@ -23,7 +23,7 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-extern const char singlepass_glsl_data[];
+void init_shaderlib(DataFile::BuiltinSource &);
 
 Resources::Resources():
        default_tex_filter(SGIS_generate_mipmap ? LINEAR_MIPMAP_LINEAR : LINEAR),
@@ -56,7 +56,7 @@ DataFile::BuiltinSource &Resources::get_builtins()
 
        if(!init_done)
        {
-               builtins.add_object("singlepass.glsl", singlepass_glsl_data);
+               init_shaderlib(builtins);
                init_done = true;
        }