More refactoring of cg2glsl.py.

This commit is contained in:
Felix Laurie von Massenbach 2015-08-08 22:50:50 +01:00
parent 867b83e352
commit c5930e5a0c

View File

@ -37,8 +37,19 @@ def remove_comments(source_lines):
return keep_line_if(lambda line: line, lines_without_comments) return keep_line_if(lambda line: line, lines_without_comments)
def defines_var(line):
return ('//var' in line) or ('#var' in line)
def replace_by_table(source, table):
for orig, new in table:
if orig:
source = source.replace(orig, new)
return source
def replace_global_in(source): def replace_global_in(source):
split_source = source.split('\n')
replace_table = [ replace_table = [
('IN.video_size', 'InputSize'), ('IN.video_size', 'InputSize'),
('IN.texture_size', 'TextureSize'), ('IN.texture_size', 'TextureSize'),
@ -47,8 +58,8 @@ def replace_global_in(source):
('IN.frame_direction', 'FrameDirection'), ('IN.frame_direction', 'FrameDirection'),
] ]
for line in split_source: for line in source.splitlines():
if ('//var' in line) or ('#var' in line): if defines_var(line):
for index, replace in enumerate(replace_table): for index, replace in enumerate(replace_table):
orig = line.split()[2] orig = line.split()[2]
if replace[0] == orig: if replace[0] == orig:
@ -56,15 +67,12 @@ def replace_global_in(source):
log('Replace globals:', replace_table) log('Replace globals:', replace_table)
for orig, new in replace_table: return replace_by_table(source, replace_table)
if orig:
source = source.replace(orig, new)
return source
def replace_global_vertex(source): def replace_global_vertex(source):
source = replace_global_in(source) source = replace_global_in(source)
replace_table = [ replace_table = [
('attribute', 'COMPAT_ATTRIBUTE'), ('attribute', 'COMPAT_ATTRIBUTE'),
('varying', 'COMPAT_VARYING'), ('varying', 'COMPAT_VARYING'),
@ -86,17 +94,14 @@ def replace_global_vertex(source):
('output', 'output_dummy'), # 'output' is reserved in GLSL. ('output', 'output_dummy'), # 'output' is reserved in GLSL.
] ]
for orig, new in replace_table: return replace_by_table(source, replace_table)
source = source.replace(orig, new)
return source
def translate_varyings(varyings, source, direction): def translate_varyings(varyings, source, direction):
dictionary = {} dictionary = {}
for varying in varyings: for varying in varyings:
for line in source: for line in source:
if (varying in line) and (('//var' in line) or ('#var' in line)) and (direction in line): if defines_var(line) and (varying in line) and (direction in line):
log('Found line for', varying + ':', line) log('Found line for', varying + ':', line)
dictionary[varying] = 'VAR' + line.split(':')[0].split('.')[-1].strip() dictionary[varying] = 'VAR' + line.split(':')[0].split('.')[-1].strip()
break break
@ -120,6 +125,7 @@ def no_uniform(elem):
for ban in banned: for ban in banned:
if ban in elem: if ban in elem:
return False return False
return True return True
@ -132,7 +138,7 @@ def destructify_varyings(source, direction):
# Don't try to remove this as it breaks compile. # Don't try to remove this as it breaks compile.
vout_lines = [] vout_lines = []
for line in source: for line in source:
if (('//var' in line) or ('#var' in line)) and (('$vout.' in line) or ('$vin.' in line)): if defines_var(line) and (('$vout.' in line) or ('$vin.' in line)):
vout_lines.append(line) vout_lines.append(line)
struct_types = [] struct_types = []
@ -227,9 +233,14 @@ def destructify_varyings(source, direction):
return source return source
def translate(cg, translations):
if cg in translations:
return translations[cg]
else:
return cg
def translate_varying(cg): def translate_varying(cg):
# Ye, it's ugly as shit. :(
# log('Translate:', cg)
translations = { translations = {
'IN.tex_coord': 'TexCoord', 'IN.tex_coord': 'TexCoord',
'IN.vertex_coord': 'VertexCoord', 'IN.vertex_coord': 'VertexCoord',
@ -259,15 +270,10 @@ def translate_varying(cg):
'PASSPREV8.tex_coord': 'PassPrev8TexCoord', 'PASSPREV8.tex_coord': 'PassPrev8TexCoord',
} }
if cg in translations: return translate(cg, translations)
return translations[cg]
else:
return cg
def translate_texture_size(cg): def translate_texture_size(cg):
# Ye, it's ugly as shit. :(
# log('Translate:', cg)
translations = { translations = {
'ORIG.texture_size': 'OrigTextureSize', 'ORIG.texture_size': 'OrigTextureSize',
'PREV.texture_size': 'PrevTextureSize', 'PREV.texture_size': 'PrevTextureSize',
@ -317,10 +323,7 @@ def translate_texture_size(cg):
'PASSPREV8.video_size': 'PassPrev8InputSize', 'PASSPREV8.video_size': 'PassPrev8InputSize',
} }
if cg in translations: return translate(cg, translations)
return translations[cg]
else:
return cg
def replace_varyings(source): def replace_varyings(source):
@ -329,7 +332,7 @@ def replace_varyings(source):
attribs = [] attribs = []
uniforms = [] uniforms = []
for index, line in enumerate(source): for index, line in enumerate(source):
if ('//var' in line) or ('#var' in line): if defines_var(line):
func = translate_texture_size func = translate_texture_size
collection = uniforms collection = uniforms
if '$vin.' in line: if '$vin.' in line:
@ -386,7 +389,7 @@ def fix_samplers(log_prefix, ref_index, source):
translated_samplers.append(translated) translated_samplers.append(translated)
added_samplers.append('uniform sampler2D ' + translated + ';') added_samplers.append('uniform sampler2D ' + translated + ';')
translations.append((new_name, orig_name)) translations.append((new_name, orig_name))
elif ('//var' in line) or ('#var' in line): elif defines_var(line):
orig = line.split()[2] orig = line.split()[2]
translated = translate_texture_size(orig) translated = translate_texture_size(orig)
if translated != orig and translated not in uniforms: if translated != orig and translated not in uniforms:
@ -425,11 +428,13 @@ def hack_source_vertex(source):
source = fix_samplers('Vertex:', ref_index, source) source = fix_samplers('Vertex:', ref_index, source)
source = destructify_varyings(source, '$vout.') source = destructify_varyings(source, '$vout.')
source = replace_varyings(source) source = replace_varyings(source)
return source return source
def replace_global_fragment(source): def replace_global_fragment(source):
source = replace_global_in(source) source = replace_global_in(source)
replace_table = [ replace_table = [
('varying', 'COMPAT_VARYING'), ('varying', 'COMPAT_VARYING'),
('texture2D', 'COMPAT_TEXTURE'), ('texture2D', 'COMPAT_TEXTURE'),
@ -440,14 +445,10 @@ def replace_global_fragment(source):
('gl_FragColor', 'FragColor'), ('gl_FragColor', 'FragColor'),
] ]
for replacement in replace_table: return replace_by_table(source, replace_table)
source = source.replace(replacement[0], replacement[1])
return source
def translate_texture(cg): def translate_texture(cg):
log('Translate:', cg)
translations = { translations = {
'ORIG.texture': 'OrigTexture', 'ORIG.texture': 'OrigTexture',
'PREV.texture': 'PrevTexture', 'PREV.texture': 'PrevTexture',
@ -474,10 +475,7 @@ def translate_texture(cg):
'PASSPREV8.texture': 'PassPrev8Texture', 'PASSPREV8.texture': 'PassPrev8Texture',
} }
if cg in translations: return translate(cg, translations)
return translations[cg]
else:
return cg
def hack_source_fragment(source): def hack_source_fragment(source):
@ -494,6 +492,7 @@ def hack_source_fragment(source):
source = fix_samplers('Fragment:', ref_index, source) source = fix_samplers('Fragment:', ref_index, source)
source = destructify_varyings(source, '$vin.') source = destructify_varyings(source, '$vin.')
return source return source
@ -513,7 +512,7 @@ def validate_shader(source, target):
def preprocess_vertex(source_data): def preprocess_vertex(source_data):
input_data = source_data.split('\n') input_data = source_data.splitlines()
ret = [] ret = []
for line in input_data: for line in input_data:
if ('uniform' in line) and (('float4x4' in line) or ('half4x4' in line)): if ('uniform' in line) and (('float4x4' in line) or ('half4x4' in line)):
@ -561,8 +560,8 @@ def convert(source, dest):
vertex_source = replace_global_vertex(vertex_source) vertex_source = replace_global_vertex(vertex_source)
fragment_source = replace_global_fragment(fragment_source) fragment_source = replace_global_fragment(fragment_source)
vertex_source = vertex_source.split('\n') vertex_source = vertex_source.splitlines()
fragment_source = fragment_source.split('\n') fragment_source = fragment_source.splitlines()
# Cg think we're using row-major matrices, but we're using column major. # Cg think we're using row-major matrices, but we're using column major.
# Also, Cg tends to compile matrix multiplications as dot products in GLSL. # Also, Cg tends to compile matrix multiplications as dot products in GLSL.