2015-12-03 02:36:18 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2016-01-28 00:58:00 +01:00
|
|
|
# Usage: 'python makeres.py blender.shader.json'
|
2015-12-03 02:36:18 +01:00
|
|
|
# Output: blender_resource.json
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
class Object:
|
|
|
|
def to_JSON(self):
|
|
|
|
return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
|
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
def writeResource(res, defs, json_data, base_name):
|
2015-12-03 02:36:18 +01:00
|
|
|
# Define
|
|
|
|
sres = Object()
|
|
|
|
res.shader_resources.append(sres)
|
|
|
|
|
2015-12-17 20:07:23 +01:00
|
|
|
shader_id = base_name
|
2015-12-03 02:36:18 +01:00
|
|
|
for s in defs:
|
|
|
|
shader_id += s
|
|
|
|
|
|
|
|
sres.id = shader_id
|
|
|
|
sres.vertex_structure = []
|
|
|
|
sres.contexts = []
|
|
|
|
|
2015-12-17 20:07:23 +01:00
|
|
|
# Parse
|
|
|
|
for c in json_data['contexts']:
|
|
|
|
con = Object()
|
|
|
|
sres.contexts.append(con)
|
|
|
|
con.id = c['id']
|
|
|
|
con.constants = []
|
|
|
|
con.texture_units = []
|
|
|
|
|
|
|
|
# Names
|
|
|
|
vert_name = c['vertex_shader'].split('.')[0]
|
|
|
|
frag_name = c['fragment_shader'].split('.')[0]
|
|
|
|
for d in defs:
|
|
|
|
vert_name += d
|
|
|
|
frag_name += d
|
|
|
|
con.vertex_shader = vert_name + '.vert'
|
|
|
|
con.fragment_shader = frag_name + '.frag'
|
|
|
|
|
|
|
|
# Params
|
|
|
|
for p in c['params']:
|
|
|
|
if p['id'] == 'depth_write':
|
|
|
|
if p['value'] == 'true':
|
|
|
|
con.depth_write = True
|
|
|
|
else:
|
|
|
|
con.depth_write = False
|
|
|
|
elif p['id'] == 'compare_mode':
|
|
|
|
con.compare_mode = p['value']
|
2016-04-02 13:42:07 +02:00
|
|
|
elif p['id'] == 'stencil_mode':
|
|
|
|
con.stencil_mode = p['value']
|
2016-04-02 00:33:19 +02:00
|
|
|
elif p['id'] == 'stencil_pass':
|
|
|
|
con.stencil_pass = p['value']
|
|
|
|
elif p['id'] == 'stencil_fail':
|
|
|
|
con.stencil_fail = p['value']
|
|
|
|
elif p['id'] == 'stencil_reference_value':
|
|
|
|
con.stencil_reference_value = p['value']
|
2016-04-02 13:42:07 +02:00
|
|
|
elif p['id'] == 'stencil_read_mask':
|
|
|
|
con.stencil_read_mask = p['value']
|
|
|
|
elif p['id'] == 'stencil_write_mask':
|
|
|
|
con.stencil_write_mask = p['value']
|
2015-12-17 20:07:23 +01:00
|
|
|
elif p['id'] == 'cull_mode':
|
|
|
|
con.cull_mode = p['value']
|
|
|
|
elif p['id'] == 'blend_source':
|
|
|
|
con.blend_source = p['value']
|
|
|
|
elif p['id'] == 'blend_destination':
|
|
|
|
con.blend_destination = p['value']
|
|
|
|
|
|
|
|
# Parse shaders
|
|
|
|
vs = open(c['vertex_shader']).read().splitlines()
|
|
|
|
fs = open(c['fragment_shader']).read().splitlines()
|
2016-01-28 14:47:46 +01:00
|
|
|
parse_shader(sres, c, con, defs, vs, len(sres.contexts) == 1) # Parse attribs for the first vertex shader
|
|
|
|
parse_shader(sres, c, con, defs, fs, False)
|
2016-01-28 00:58:00 +01:00
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
def parse_shader(sres, c, con, defs, lines, parse_attributes):
|
2016-02-17 01:26:07 +01:00
|
|
|
skipTillEndIf = 0
|
2016-02-21 21:06:01 +01:00
|
|
|
skipElse = False
|
2016-01-28 00:58:00 +01:00
|
|
|
vertex_structure_parsed = False
|
|
|
|
vertex_structure_parsing = False
|
|
|
|
|
|
|
|
if parse_attributes == False:
|
|
|
|
vertex_structure_parsed = True
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith('#ifdef'):
|
|
|
|
s = line.split(' ')[1]
|
|
|
|
if s != 'GL_ES':
|
|
|
|
found = False
|
|
|
|
for d in defs:
|
|
|
|
if d == s:
|
|
|
|
found = True
|
|
|
|
break
|
2016-02-21 21:06:01 +01:00
|
|
|
if found == False or s == '_Instancing': # TODO: Prevent instanced data to go into main vertex structure
|
2016-02-17 01:26:07 +01:00
|
|
|
skipTillEndIf += 1
|
2016-02-21 21:06:01 +01:00
|
|
|
else:
|
|
|
|
skipElse = True # #ifdef passed, skip #else if present
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Previous ifdef passed, skip else
|
|
|
|
if skipElse == True and line.startswith('#else'):
|
|
|
|
skipElse = False
|
|
|
|
skipTillEndIf += 1
|
2016-01-28 00:58:00 +01:00
|
|
|
continue
|
|
|
|
|
2016-02-21 21:06:01 +01:00
|
|
|
if line.startswith('#endif') or line.startswith('#else'): # Starts parsing again
|
2016-02-17 01:26:07 +01:00
|
|
|
skipTillEndIf -= 1
|
2016-02-21 21:06:01 +01:00
|
|
|
skipElse = False
|
2016-02-17 01:26:07 +01:00
|
|
|
if skipTillEndIf < 0: # #else + #endif will go below 0
|
|
|
|
skipTillEndIf = 0
|
2016-01-28 00:58:00 +01:00
|
|
|
continue
|
|
|
|
|
2016-02-17 01:26:07 +01:00
|
|
|
if skipTillEndIf > 0:
|
2016-01-28 00:58:00 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
if vertex_structure_parsed == False and line.startswith('in '):
|
|
|
|
vertex_structure_parsing = True
|
|
|
|
vd = Object()
|
|
|
|
s = line.split(' ')
|
|
|
|
vd.size = int(s[1][-1:])
|
|
|
|
vd.name = s[2][:-1]
|
|
|
|
sres.vertex_structure.append(vd)
|
|
|
|
if vertex_structure_parsing == True and len(line) > 0 and line.startswith('//') == False and line.startswith('in ') == False:
|
|
|
|
vertex_structure_parsed = True
|
|
|
|
|
|
|
|
if line.startswith('uniform'):
|
|
|
|
s = line.split(' ')
|
|
|
|
ctype = s[1]
|
|
|
|
cid = s[2][:-1]
|
|
|
|
found = False # Unique check
|
2016-03-07 22:51:17 +01:00
|
|
|
if ctype == 'sampler2D' or ctype == 'sampler2DShadow': # Texture unit
|
2016-01-28 00:58:00 +01:00
|
|
|
for tu in con.texture_units:
|
|
|
|
if tu.id == cid:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if found == False:
|
|
|
|
tu = Object()
|
|
|
|
tu.id = cid
|
|
|
|
con.texture_units.append(tu)
|
|
|
|
else: # Constant
|
|
|
|
if cid.find('[') != -1: # Float arrays
|
|
|
|
cid = cid.split('[')[0]
|
|
|
|
ctype = 'floats'
|
|
|
|
for const in con.constants:
|
|
|
|
if const.id == cid:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if found == False:
|
|
|
|
const = Object()
|
|
|
|
const.type = ctype
|
|
|
|
const.id = cid
|
|
|
|
# Check for link
|
|
|
|
for l in c['links']:
|
|
|
|
if l['id'] == cid:
|
|
|
|
valid_link = True
|
|
|
|
if 'ifdef' in l:
|
|
|
|
valid_link = False
|
|
|
|
for d in defs:
|
|
|
|
if d == l['ifdef']:
|
|
|
|
valid_link = True
|
|
|
|
break
|
|
|
|
if valid_link:
|
|
|
|
const.link = l['link']
|
2015-12-17 20:07:23 +01:00
|
|
|
break
|
2016-01-28 00:58:00 +01:00
|
|
|
con.constants.append(const)
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
def make(json_name):
|
|
|
|
#base_name = sys.argv[1].split('.', 1)[0]
|
|
|
|
base_name = json_name.split('.', 1)[0]
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
# Make out dir
|
|
|
|
#if not os.path.exists('out'):
|
|
|
|
# os.makedirs('out')
|
2016-02-07 23:03:52 +01:00
|
|
|
path = '../../../../compiled/ShaderResources/' + base_name
|
2016-01-28 14:47:46 +01:00
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
# Open json file
|
|
|
|
# json_file = open(sys.argv[1]).read()
|
|
|
|
json_file = open(json_name).read()
|
|
|
|
json_data = json.loads(json_file)
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-01-28 14:47:46 +01:00
|
|
|
# Go through every context shaders and gather ifdefs
|
|
|
|
defs = []
|
|
|
|
for c in json_data['contexts']:
|
|
|
|
vs = open(c['vertex_shader']).read().splitlines()
|
|
|
|
fs = open(c['fragment_shader']).read().splitlines()
|
|
|
|
lines = vs + fs
|
|
|
|
for line in lines:
|
|
|
|
if line.startswith('#ifdef'):
|
|
|
|
d = line.split(' ')[1]
|
|
|
|
if d != 'GL_ES':
|
|
|
|
defs.append(d)
|
|
|
|
|
|
|
|
# Merge duplicates and sort
|
|
|
|
defs = sorted(list(set(defs)))
|
|
|
|
|
|
|
|
# Process #defines
|
|
|
|
res = Object()
|
|
|
|
res.shader_resources = []
|
|
|
|
for L in range(0, len(defs)+1):
|
|
|
|
for subset in itertools.combinations(defs, L):
|
|
|
|
writeResource(res, subset, json_data, base_name)
|
|
|
|
# Save separately
|
|
|
|
res_name = base_name
|
|
|
|
for s in subset:
|
|
|
|
res_name += s
|
|
|
|
#with open('out/' + res_name + '.json', 'w') as f:
|
2016-02-07 23:03:52 +01:00
|
|
|
with open(path + '/' + res_name + '.json', 'w') as f:
|
2016-01-28 14:47:46 +01:00
|
|
|
r = Object()
|
|
|
|
r.shader_resources = [res.shader_resources[-1]]
|
|
|
|
f.write(r.to_JSON())
|
|
|
|
|
|
|
|
# Save combined
|
|
|
|
#with open('out/' + base_name + '_resource.json', 'w') as f:
|
|
|
|
# f.write(res.to_JSON())
|