summaryrefslogtreecommitdiff
path: root/split-to-files.py
blob: 4a02b02a1ba101ad44149fd052f2b8523ce8829c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3

import re
import os
import argparse


def parse_input(infile):
    shaders = dict()
    programs = dict()
    shadertuple = ("bad", 0)
    prognum = ""
    reading = False
    is_glsl = True

    for line in infile.splitlines():
        declmatch = re.match(
            r"GLSL (.*) shader (.*) source for linked program (.*):", line)
        arbmatch = re.match(
            r"ARB_([^_]*)_program source for program (.*):", line)
        if declmatch:
            shadertype = declmatch.group(1)
            shadernum = declmatch.group(2)
            prognum = declmatch.group(3)
            shadertuple = (shadertype, shadernum)

            # don't save driver-internal shaders.
            if prognum == "0":
                continue

            if prognum not in shaders:
                shaders[prognum] = dict()
            if shadertuple in shaders[prognum]:
                print("Warning: duplicate", shadertype, " shader ", shadernum,
                      "in program", prognum, "...tossing old shader.")
            shaders[prognum][shadertuple] = ''
            reading = True
            is_glsl = True
            print("Reading program {0} {1} shader {2}".format(
                prognum, shadertype, shadernum))
        elif arbmatch:
            shadertype = arbmatch.group(1)
            prognum = arbmatch.group(2)
            if prognum in programs:
                print("dupe!")
                exit(1)
            programs[prognum] = (shadertype, '')
            reading = True
            is_glsl = False
            print("Reading program {0} {1} shader".format(prognum, shadertype))
        elif re.match("GLSL IR for ", line):
            reading = False
        elif re.match("Mesa IR for ", line):
            reading = False
        elif re.match("GLSL source for ", line):
            reading = False
        elif reading:
            if is_glsl:
                shaders[prognum][shadertuple] += line + '\n'
            else:
                type, source = programs[prognum]
                programs[prognum] = (type, ''.join([source, line, '\n']))

    return (shaders, programs)


def write_shader_test(filename, shaders):
    print("Writing {0}".format(filename))
    out = open(filename, 'w')

    out.write("[require]\n")
    out.write("GLSL >= 1.10\n")
    out.write("\n")

    for stage, num in shaders:
        if stage == "vertex":
            out.write("[vertex shader]\n")
            out.write(shaders[(stage, num)])
        if stage == "fragment":
            out.write("[fragment shader]\n")
            out.write(shaders[(stage, num)])

    out.close()

def write_arb_shader_test(filename, type, source):
    print("Writing {0}".format(filename))
    out = open(filename, 'w')
    out.write("[require]\n")
    out.write("GL_ARB_{0}_program\n".format(type))
    out.write("\n")
    out.write("[{0} program]\n".format(type))
    out.write(source)
    # INTEL_DEBUG won't output anything for ARB programs unless you draw
    out.write("\n[test]\ndraw rect -1 -1 1 2\n");
    out.close()

def write_files(directory, shaders, programs):
    for prog in shaders:
        write_shader_test("{0}/{1}.shader_test".format(directory, prog),
                          shaders[prog])
    for prognum in programs:
        prog = programs[prognum]
        write_arb_shader_test("{0}/{1}p-{2}.shader_test".format(directory,
            prog[0][0], prognum), prog[0], prog[1])

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('appname', help='Output directory (application name)')
    parser.add_argument('mesadebug', help='MESA_GLSL=dump output file')
    args = parser.parse_args()

    dirname = "shaders/{0}".format(args.appname)
    if not os.path.isdir(dirname):
        os.mkdir(dirname)

    with open(args.mesadebug, 'r') as infile:
        shaders, programs = parse_input(infile.read())

    write_files(dirname, shaders, programs)

if __name__ == "__main__":
    main()