2015-12-03 02:36:18 +01:00
|
|
|
import os
|
2017-03-15 12:30:14 +01:00
|
|
|
import arm.utils
|
2015-12-03 02:36:18 +01:00
|
|
|
|
2016-10-19 13:28:06 +02:00
|
|
|
def write_data(res, defs, json_data, base_name):
|
2016-09-28 00:00:59 +02:00
|
|
|
# Define
|
|
|
|
sres = {}
|
|
|
|
res['shader_datas'].append(sres)
|
2015-12-03 02:36:18 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
shader_id = base_name
|
|
|
|
for s in defs:
|
|
|
|
shader_id += s
|
2015-12-03 02:36:18 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
sres['name'] = shader_id
|
|
|
|
sres['vertex_structure'] = []
|
|
|
|
sres['contexts'] = []
|
2015-12-03 02:36:18 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
# Parse
|
|
|
|
for c in json_data['contexts']:
|
|
|
|
con = {}
|
|
|
|
sres['contexts'].append(con)
|
|
|
|
con['name'] = c['name']
|
|
|
|
con['constants'] = []
|
|
|
|
con['texture_units'] = []
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
# Names
|
|
|
|
vert_name = c['vertex_shader'].split('.')[0]
|
|
|
|
frag_name = c['fragment_shader'].split('.')[0]
|
|
|
|
if 'geometry_shader' in c:
|
|
|
|
geom_name = c['geometry_shader'].split('.')[0]
|
|
|
|
if 'tesscontrol_shader' in c:
|
|
|
|
tesc_name = c['tesscontrol_shader'].split('.')[0]
|
|
|
|
if 'tesseval_shader' in c:
|
|
|
|
tese_name = c['tesseval_shader'].split('.')[0]
|
|
|
|
for d in defs:
|
|
|
|
vert_name += d
|
|
|
|
frag_name += d
|
|
|
|
if 'geometry_shader' in c:
|
|
|
|
geom_name += d
|
|
|
|
if 'tesscontrol_shader' in c:
|
|
|
|
tesc_name += d
|
|
|
|
if 'tesseval_shader' in c:
|
|
|
|
tese_name += d
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
con['vertex_shader'] = vert_name + '.vert'
|
|
|
|
con['fragment_shader'] = frag_name + '.frag'
|
|
|
|
if 'geometry_shader' in c:
|
|
|
|
con['geometry_shader'] = geom_name + '.geom'
|
|
|
|
if 'tesscontrol_shader' in c:
|
|
|
|
con['tesscontrol_shader'] = tesc_name + '.tesc'
|
|
|
|
if 'tesseval_shader' in c:
|
|
|
|
con['tesseval_shader'] = tese_name + '.tese'
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
# Params
|
2016-10-17 12:02:41 +02:00
|
|
|
params = ['depth_write', 'compare_mode', 'stencil_mode', \
|
|
|
|
'stencil_pass', 'stencil_fail', 'stencil_reference_value', \
|
|
|
|
'stencil_read_mask', 'stencil_write_mask', 'cull_mode', \
|
|
|
|
'blend_source', 'blend_destination', 'blend_operation', \
|
|
|
|
'alpha_blend_source', 'alpha_blend_destination', 'alpha_blend_operation' \
|
|
|
|
'color_write_red', 'color_write_green', 'color_write_blue', \
|
|
|
|
'color_write_alpha']
|
|
|
|
|
|
|
|
for p in params:
|
|
|
|
if p in c:
|
|
|
|
con[p] = c[p]
|
2016-01-28 00:58:00 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
# Parse shaders
|
2016-10-17 00:02:51 +02:00
|
|
|
if 'vertex_shader_path' in c:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['vertex_shader_path']) as f:
|
|
|
|
vert = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
else:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['vertex_shader']) as f:
|
|
|
|
vert = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
|
|
|
|
if 'fragment_shader_path' in c:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['fragment_shader_path']) as f:
|
|
|
|
frag = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
else:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['fragment_shader']) as f:
|
|
|
|
frag = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_shader(sres, c, con, defs, vert, len(sres['contexts']) == 1) # Parse attribs for the first vertex shader
|
|
|
|
parse_shader(sres, c, con, defs, frag, False)
|
2016-10-25 16:15:07 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if 'geometry_shader' in c:
|
2016-10-17 00:02:51 +02:00
|
|
|
if 'geometry_shader_path' in c:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['geometry_shader_path']) as f:
|
|
|
|
geom = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
else:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['geometry_shader']) as f:
|
|
|
|
geom = f.read().splitlines()
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_shader(sres, c, con, defs, geom, False)
|
2016-10-25 16:15:07 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if 'tesscontrol_shader' in c:
|
2016-10-17 00:02:51 +02:00
|
|
|
if 'tesscontrol_shader_path' in c:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['tesscontrol_shader_path']) as f:
|
|
|
|
tesc = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
else:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['tesscontrol_shader']) as f:
|
|
|
|
tesc = f.read().splitlines()
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_shader(sres, c, con, defs, tesc, False)
|
2016-10-25 16:15:07 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if 'tesseval_shader' in c:
|
2016-10-17 12:02:41 +02:00
|
|
|
if 'tesseval_shader_path' in c:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['tesseval_shader_path']) as f:
|
|
|
|
tese = f.read().splitlines()
|
2016-10-17 00:02:51 +02:00
|
|
|
else:
|
2016-10-25 16:15:07 +02:00
|
|
|
with open(c['tesseval_shader']) as f:
|
|
|
|
tese = f.read().splitlines()
|
2016-09-28 00:00:59 +02:00
|
|
|
parse_shader(sres, c, con, defs, tese, False)
|
2016-01-28 00:58:00 +01:00
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
def find_def(defs, s):
|
|
|
|
for d in defs:
|
|
|
|
if d == s:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
def parse_shader(sres, c, con, defs, lines, parse_attributes):
|
2016-10-19 13:28:06 +02:00
|
|
|
skip_till_endif = 0
|
|
|
|
skip_else = False
|
2016-09-28 00:00:59 +02:00
|
|
|
vertex_structure_parsed = False
|
|
|
|
vertex_structure_parsing = False
|
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
stack = []
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if parse_attributes == False:
|
|
|
|
vertex_structure_parsed = True
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
line = line.lstrip()
|
2016-12-02 00:13:09 +01:00
|
|
|
|
|
|
|
# Preprocessor
|
|
|
|
if line.startswith('#ifdef') or line.startswith('#ifndef'):
|
2016-09-28 00:00:59 +02:00
|
|
|
s = line.split(' ')[1]
|
2016-12-02 00:13:09 +01:00
|
|
|
found = find_def(defs, s)
|
|
|
|
if line.startswith('#ifndef'):
|
|
|
|
found = not found
|
2017-01-14 17:37:18 +01:00
|
|
|
if found == False:
|
2016-12-02 00:13:09 +01:00
|
|
|
stack.append(0)
|
|
|
|
else:
|
|
|
|
stack.append(1)
|
2016-09-28 00:00:59 +02:00
|
|
|
continue
|
2016-01-28 00:58:00 +01:00
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
if line.startswith('#else'):
|
|
|
|
stack[-1] = 1 - stack[-1]
|
2016-09-28 00:00:59 +02:00
|
|
|
continue
|
2016-01-28 00:58:00 +01:00
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
if line.startswith('#endif'):
|
|
|
|
stack.pop()
|
2016-09-28 00:00:59 +02:00
|
|
|
continue
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
if len(stack) > 0 and stack[-1] == 0:
|
2016-09-28 00:00:59 +02:00
|
|
|
continue
|
2016-07-20 17:33:17 +02:00
|
|
|
|
2016-12-02 00:13:09 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if vertex_structure_parsed == False and line.startswith('in '):
|
|
|
|
vertex_structure_parsing = True
|
|
|
|
vd = {}
|
|
|
|
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
|
2016-05-19 22:22:41 +02:00
|
|
|
|
2016-10-17 17:39:40 +02:00
|
|
|
if line.startswith('uniform ') or line.startswith('//!uniform'): # Uniforms included from header files
|
2016-09-28 00:00:59 +02:00
|
|
|
s = line.split(' ')
|
2016-10-09 16:06:18 +02:00
|
|
|
# uniform sampler2D myname;
|
|
|
|
# uniform layout(RGBA8) image3D myname;
|
|
|
|
if s[1].startswith('layout'):
|
|
|
|
ctype = s[2]
|
|
|
|
cid = s[3][:-1]
|
|
|
|
else:
|
|
|
|
ctype = s[1]
|
|
|
|
cid = s[2][:-1]
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
found = False # Unique check
|
2017-03-17 18:34:03 +01:00
|
|
|
if ctype == 'sampler2D' or ctype == 'sampler2DShadow' or ctype == 'sampler3D' or ctype == 'samplerCube' or ctype == 'image2D' or ctype == 'image3D': # Texture unit
|
2016-10-09 16:06:18 +02:00
|
|
|
for tu in con['texture_units']: # Texture already present
|
2016-09-28 00:00:59 +02:00
|
|
|
if tu['name'] == cid:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if found == False:
|
|
|
|
tu = {}
|
|
|
|
tu['name'] = cid
|
2016-10-09 16:06:18 +02:00
|
|
|
# sampler2D / image2D
|
|
|
|
if ctype == 'image2D' or ctype == 'image3D':
|
|
|
|
tu['is_image'] = True
|
2016-09-28 00:00:59 +02:00
|
|
|
# Check for link
|
|
|
|
for l in c['links']:
|
|
|
|
if l['name'] == cid:
|
|
|
|
valid_link = True
|
2016-10-12 17:52:27 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if 'ifdef' in l:
|
2016-10-12 17:52:27 +02:00
|
|
|
def_found = False
|
2016-09-28 00:00:59 +02:00
|
|
|
for d in defs:
|
|
|
|
for link_def in l['ifdef']:
|
|
|
|
if d == link_def:
|
2016-10-12 17:52:27 +02:00
|
|
|
def_found = True
|
2016-09-28 00:00:59 +02:00
|
|
|
break
|
2016-10-12 17:52:27 +02:00
|
|
|
if def_found:
|
2016-09-28 00:00:59 +02:00
|
|
|
break
|
2016-10-12 17:52:27 +02:00
|
|
|
if not def_found:
|
|
|
|
valid_link = False
|
|
|
|
|
|
|
|
if 'ifndef' in l:
|
|
|
|
def_found = False
|
|
|
|
for d in defs:
|
2016-11-08 15:14:56 +01:00
|
|
|
for link_def in l['ifndef']:
|
2016-10-12 17:52:27 +02:00
|
|
|
if d == link_def:
|
|
|
|
def_found = True
|
|
|
|
break
|
|
|
|
if def_found:
|
|
|
|
break
|
|
|
|
if def_found:
|
|
|
|
valid_link = False
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if valid_link:
|
|
|
|
tu['link'] = l['link']
|
|
|
|
break
|
|
|
|
con['texture_units'].append(tu)
|
|
|
|
else: # Constant
|
|
|
|
if cid.find('[') != -1: # Float arrays
|
|
|
|
cid = cid.split('[')[0]
|
2017-02-07 11:50:21 +01:00
|
|
|
if ctype == 'vec4':
|
|
|
|
ctype = 'float4s'
|
|
|
|
else:
|
|
|
|
ctype = 'floats'
|
2016-09-28 00:00:59 +02:00
|
|
|
for const in con['constants']:
|
|
|
|
if const['name'] == cid:
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
if found == False:
|
|
|
|
const = {}
|
|
|
|
const['type'] = ctype
|
|
|
|
const['name'] = cid
|
|
|
|
# Check for link
|
|
|
|
for l in c['links']:
|
|
|
|
if l['name'] == cid:
|
|
|
|
valid_link = True
|
2016-10-12 17:52:27 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if 'ifdef' in l:
|
2016-10-12 17:52:27 +02:00
|
|
|
def_found = False
|
2016-09-28 00:00:59 +02:00
|
|
|
for d in defs:
|
2016-10-12 17:52:27 +02:00
|
|
|
for link_def in l['ifdef']:
|
|
|
|
if d == link_def:
|
|
|
|
def_found = True
|
2016-09-28 00:00:59 +02:00
|
|
|
break
|
2016-10-12 17:52:27 +02:00
|
|
|
if def_found:
|
2016-09-28 00:00:59 +02:00
|
|
|
break
|
2016-10-12 17:52:27 +02:00
|
|
|
if not def_found:
|
|
|
|
valid_link = False
|
|
|
|
|
|
|
|
if 'ifndef' in l:
|
|
|
|
def_found = False
|
|
|
|
for d in defs:
|
2016-11-05 20:57:04 +01:00
|
|
|
for link_def in l['ifndef']:
|
2016-10-12 17:52:27 +02:00
|
|
|
if d == link_def:
|
|
|
|
def_found = True
|
|
|
|
break
|
|
|
|
if def_found:
|
|
|
|
break
|
|
|
|
if def_found:
|
|
|
|
valid_link = False
|
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
if valid_link:
|
|
|
|
const['link'] = l['link']
|
|
|
|
break
|
|
|
|
con['constants'].append(const)
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-10-19 13:28:06 +02:00
|
|
|
def save_data(path, base_name, subset, res):
|
2016-09-28 00:00:59 +02:00
|
|
|
res_name = base_name
|
|
|
|
for s in subset:
|
|
|
|
res_name += s
|
2015-12-17 20:07:23 +01:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
r = {}
|
|
|
|
r['shader_datas'] = [res['shader_datas'][-1]]
|
2017-03-15 12:30:14 +01:00
|
|
|
arm.utils.write_arm(path + '/' + res_name + '.arm', r)
|
2016-05-19 22:22:41 +02:00
|
|
|
|
2016-10-17 00:02:51 +02:00
|
|
|
def make(base_name, json_data, fp, defs):
|
2016-10-19 13:28:06 +02:00
|
|
|
|
2017-03-14 20:43:54 +01:00
|
|
|
path = fp + '/build/compiled/Shaders/' + base_name
|
2016-09-28 00:00:59 +02:00
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
2016-05-06 11:13:46 +02:00
|
|
|
|
2016-09-28 00:00:59 +02:00
|
|
|
res = {}
|
|
|
|
res['shader_datas'] = []
|
2016-05-06 11:13:46 +02:00
|
|
|
|
2016-10-19 13:28:06 +02:00
|
|
|
write_data(res, defs, json_data, base_name)
|
|
|
|
save_data(path, base_name, defs, res)
|