armory/blender/arm/lib/make_datas.py

283 lines
10 KiB
Python
Raw Normal View History

2017-03-15 12:30:14 +01:00
import arm.utils
2017-12-20 15:37:58 +01:00
import arm.assets as assets
2015-12-03 02:36:18 +01:00
2021-09-07 23:25:54 +02:00
def parse_context(c: dict, sres: dict, asset, defs: list[str], vert: list[str] = None, frag: list[str] = None):
con = {
'name': c['name'],
'constants': [],
'texture_units': [],
'vertex_elements': []
}
2018-09-13 12:13:32 +02:00
sres['contexts'].append(con)
# Names
con['vertex_shader'] = c['vertex_shader'].rsplit('.', 1)[0].split('/')[-1]
if con['vertex_shader'] not in asset:
asset.append(con['vertex_shader'])
con['fragment_shader'] = c['fragment_shader'].rsplit('.', 1)[0].split('/')[-1]
if con['fragment_shader'] not in asset:
asset.append(con['fragment_shader'])
if 'geometry_shader' in c:
con['geometry_shader'] = c['geometry_shader'].rsplit('.', 1)[0].split('/')[-1]
if con['geometry_shader'] not in asset:
asset.append(con['geometry_shader'])
if 'tesscontrol_shader' in c:
con['tesscontrol_shader'] = c['tesscontrol_shader'].rsplit('.', 1)[0].split('/')[-1]
if con['tesscontrol_shader'] not in asset:
asset.append(con['tesscontrol_shader'])
if 'tesseval_shader' in c:
con['tesseval_shader'] = c['tesseval_shader'].rsplit('.', 1)[0].split('/')[-1]
if con['tesseval_shader'] not in asset:
asset.append(con['tesseval_shader'])
if 'color_attachments' in c:
con['color_attachments'] = c['color_attachments']
for i in range(len(con['color_attachments'])):
if con['color_attachments'][i] == '_HDR':
con['color_attachments'][i] = 'RGBA32' if '_LDR' in defs else 'RGBA64'
2020-05-06 18:11:02 +02:00
2018-09-13 12:13:32 +02:00
# Params
2019-04-06 13:03:04 +02:00
params = ['depth_write', 'compare_mode', 'cull_mode', \
2018-09-13 12:13:32 +02:00
'blend_source', 'blend_destination', 'blend_operation', \
'alpha_blend_source', 'alpha_blend_destination', 'alpha_blend_operation' \
2018-10-18 14:53:28 +02:00
'color_writes_red', 'color_writes_green', 'color_writes_blue', 'color_writes_alpha', \
'conservative_raster']
2018-09-13 12:13:32 +02:00
for p in params:
if p in c:
con[p] = c[p]
# Parse shaders
2021-09-07 23:25:54 +02:00
if vert is None:
2017-12-20 15:37:58 +01:00
with open(c['vertex_shader']) as f:
vert = f.read().splitlines()
2018-09-13 12:13:32 +02:00
parse_shader(sres, c, con, defs, vert, True) # Parse attribs for vertex shader
2017-12-20 15:37:58 +01:00
2021-09-07 23:25:54 +02:00
if frag is None:
2017-12-20 15:37:58 +01:00
with open(c['fragment_shader']) as f:
frag = f.read().splitlines()
2018-09-13 12:13:32 +02:00
parse_shader(sres, c, con, defs, frag, False)
if 'geometry_shader' in c:
with open(c['geometry_shader']) as f:
geom = f.read().splitlines()
parse_shader(sres, c, con, defs, geom, False)
if 'tesscontrol_shader' in c:
with open(c['tesscontrol_shader']) as f:
tesc = f.read().splitlines()
parse_shader(sres, c, con, defs, tesc, False)
2020-05-06 18:11:02 +02:00
2018-09-13 12:13:32 +02:00
if 'tesseval_shader' in c:
with open(c['tesseval_shader']) as f:
tese = f.read().splitlines()
parse_shader(sres, c, con, defs, tese, False)
2016-01-28 00:58:00 +01:00
2021-09-07 23:25:54 +02:00
def parse_shader(sres, c: dict, con: dict, defs: list[str], lines: list[str], parse_attributes: bool):
"""Parses the given shader to get information about the used vertex
elements, uniforms and constants. This information is later used in
Iron to check what data each shader requires.
@param defs A list of set defines for the preprocessor
@param lines The list of lines of the shader file
@param parse_attributes Whether to parse vertex elements
"""
2018-12-14 15:27:43 +01:00
vertex_elements_parsed = False
vertex_elements_parsing = False
2020-05-06 18:11:02 +02:00
2021-09-07 23:25:54 +02:00
# Stack of the state of all preprocessor conditions for the current
# line. If there is a `False` in the stack, at least one surrounding
# condition is false and the line must not be parsed
stack: list[bool] = []
2016-12-02 00:13:09 +01:00
2021-09-07 23:25:54 +02:00
if not parse_attributes:
2018-12-14 15:27:43 +01:00
vertex_elements_parsed = True
2020-05-06 18:11:02 +02:00
for line in lines:
line = line.lstrip()
2016-12-02 00:13:09 +01:00
# Preprocessor
2021-09-07 23:25:54 +02:00
if line.startswith('#if'): # if, ifdef, ifndef
s = line.split(' ')[1]
2017-12-13 14:21:42 +01:00
found = s in defs
2016-12-02 00:13:09 +01:00
if line.startswith('#ifndef'):
found = not found
2021-09-07 23:25:54 +02:00
stack.append(found)
continue
2016-01-28 00:58:00 +01:00
2016-12-02 00:13:09 +01:00
if line.startswith('#else'):
2021-09-07 23:25:54 +02:00
stack[-1] = not stack[-1]
continue
2016-01-28 00:58:00 +01:00
2016-12-02 00:13:09 +01:00
if line.startswith('#endif'):
stack.pop()
continue
2015-12-17 20:07:23 +01:00
2021-09-07 23:25:54 +02:00
# Skip lines if the stack contains at least one preprocessor
# condition that is not fulfilled
2018-11-22 18:07:32 +01:00
skip = False
2021-09-07 23:25:54 +02:00
for condition in stack:
if not condition:
2018-11-22 18:07:32 +01:00
skip = True
break
if skip:
continue
2016-07-20 17:33:17 +02:00
2021-09-07 23:25:54 +02:00
if not vertex_elements_parsed and line.startswith('in '):
2018-12-14 15:27:43 +01:00
vertex_elements_parsing = True
s = line.split(' ')
2021-09-07 23:25:54 +02:00
con['vertex_elements'].append({
'data': 'float' + s[1][-1:],
'name': s[2][:-1] # [:1] to get rid of the semicolon
})
# Stop the vertex element parsing if no other vertex elements
# follow directly (assuming all vertex elements are positioned
# directly after each other apart from empty lines and comments)
if vertex_elements_parsing and len(line) > 0 and not line.startswith('//') and not line.startswith('in '):
2018-12-14 15:27:43 +01:00
vertex_elements_parsed = True
2016-10-17 17:39:40 +02:00
if line.startswith('uniform ') or line.startswith('//!uniform'): # Uniforms included from header files
s = line.split(' ')
2021-09-07 23:25:54 +02:00
# Examples:
# uniform sampler2D myname;
# uniform layout(RGBA8) image3D myname;
2016-10-09 16:06:18 +02:00
if s[1].startswith('layout'):
ctype = s[2]
2018-06-12 13:50:27 +02:00
cid = s[3]
if cid[-1] == ';':
cid = cid[:-1]
2016-10-09 16:06:18 +02:00
else:
ctype = s[1]
2018-06-12 13:50:27 +02:00
cid = s[2]
if cid[-1] == ';':
cid = cid[:-1]
2016-10-09 16:06:18 +02:00
2021-09-07 23:25:54 +02:00
found = False # Uniqueness check
2018-11-29 23:43:33 +01:00
if ctype.startswith('sampler') or ctype.startswith('image') or ctype.startswith('uimage'): # Texture unit
2021-09-07 23:25:54 +02:00
for tu in con['texture_units']:
if tu['name'] == cid:
2021-09-07 23:25:54 +02:00
# Texture already present
found = True
break
2021-09-07 23:25:54 +02:00
if not found:
2018-12-11 23:05:18 +01:00
if cid[-1] == ']': # Array of samplers - sampler2D mySamplers[2]
# Add individual units - mySamplers[0], mySamplers[1]
for i in range(int(cid[-2])):
2021-09-07 23:25:54 +02:00
tu = {'name': cid[:-2] + str(i) + ']'}
2018-12-11 23:05:18 +01:00
con['texture_units'].append(tu)
else:
2021-09-07 23:25:54 +02:00
tu = {'name': cid}
2018-12-11 23:05:18 +01:00
con['texture_units'].append(tu)
if ctype.startswith('image') or ctype.startswith('uimage'):
tu['is_image'] = True
2021-09-07 23:25:54 +02:00
check_link(c, defs, cid, tu)
else: # Constant
if cid.find('[') != -1: # Float arrays
cid = cid.split('[')[0]
2017-04-12 13:25:09 +02:00
ctype = 'floats'
for const in con['constants']:
if const['name'] == cid:
found = True
break
2021-09-07 23:25:54 +02:00
if not found:
const = {
'type': ctype,
'name': cid
}
2018-12-11 23:05:18 +01:00
con['constants'].append(const)
2021-09-07 23:25:54 +02:00
check_link(c, defs, cid, const)
def check_link(source_context: dict, defs: list[str], cid: str, out: dict):
"""Checks whether the uniform/constant with the given name (`cid`)
has a link stated in the json (`source_context`) that can be safely
included based on the given defines (`defs`). If that is the case,
the found link is written to the `out` dictionary.
"""
for link in source_context['links']:
if link['name'] == cid:
valid_link = True
# Optionally only use link if at least
# one of the given defines is set
if 'ifdef' in link:
def_found = False
for d in defs:
for link_def in link['ifdef']:
if d == link_def:
def_found = True
break
if def_found:
break
if not def_found:
valid_link = False
# Optionally only use link if none of
# the given defines are set
if 'ifndef' in link:
def_found = False
for d in defs:
for link_def in link['ifndef']:
if d == link_def:
def_found = True
break
2021-09-07 23:25:54 +02:00
if def_found:
break
if def_found:
valid_link = False
if valid_link:
out['link'] = link['link']
break
2015-12-17 20:07:23 +01:00
2021-09-07 23:25:54 +02:00
def make(res: dict, base_name: str, json_data: dict, fp, defs: list[str], make_variants: bool):
sres = {
'name': base_name,
'contexts': []
}
2018-09-13 12:13:32 +02:00
res['shader_datas'].append(sres)
asset = assets.shader_passes_assets[base_name]
vert = None
frag = None
has_variants = 'variants' in json_data and len(json_data['variants']) > 0
if make_variants and has_variants:
2018-09-13 12:13:32 +02:00
d = json_data['variants'][0]
if d in defs:
# Write shader variant with define
c = json_data['contexts'][0]
with open(c['vertex_shader']) as f:
vert = f.read().split('\n', 1)[1]
vert = "#version 450\n#define " + d + "\n" + vert
with open(c['fragment_shader']) as f:
frag = f.read().split('\n', 1)[1]
frag = "#version 450\n#define " + d + "\n" + frag
with open(arm.utils.get_fp_build() + '/compiled/Shaders/' + base_name + d + '.vert.glsl', 'w') as f:
f.write(vert)
with open(arm.utils.get_fp_build() + '/compiled/Shaders/' + base_name + d + '.frag.glsl', 'w') as f:
f.write(frag)
# Add context variant
c2 = c.copy()
c2['vertex_shader'] = base_name + d + '.vert.glsl'
c2['fragment_shader'] = base_name + d + '.frag.glsl'
c2['name'] = c['name'] + d
parse_context(c2, sres, asset, defs, vert.splitlines(), frag.splitlines())
for c in json_data['contexts']:
parse_context(c, sres, asset, defs)