Updates and fixes for cg2glsl.
This commit is contained in:
parent
954727aa79
commit
1a071c4992
119
tools/cg2glsl.py
119
tools/cg2glsl.py
|
@ -60,6 +60,8 @@ def replace_global_vertex(source):
|
||||||
|
|
||||||
source = replace_global_in(source)
|
source = replace_global_in(source)
|
||||||
replace_table = [
|
replace_table = [
|
||||||
|
('attribute', 'COMPAT_attribute'),
|
||||||
|
('varying', 'COMPAT_varying'),
|
||||||
('POSITION', 'VertexCoord'),
|
('POSITION', 'VertexCoord'),
|
||||||
('TEXCOORD1', 'LUTTexCoord'),
|
('TEXCOORD1', 'LUTTexCoord'),
|
||||||
('TEXCOORD0', 'TexCoord'),
|
('TEXCOORD0', 'TexCoord'),
|
||||||
|
@ -143,7 +145,7 @@ def destructify_varyings(source, direction):
|
||||||
while (j < len(source)) and ('};' not in source[j]):
|
while (j < len(source)) and ('};' not in source[j]):
|
||||||
j += 1
|
j += 1
|
||||||
|
|
||||||
lines = ['varying ' + string for string in source[i + 1 : j]]
|
lines = ['COMPAT_varying ' + string for string in source[i + 1 : j]]
|
||||||
varyings.extend(lines)
|
varyings.extend(lines)
|
||||||
names = [string.strip().split(' ')[1].split(';')[0].strip() for string in source[i + 1 : j]]
|
names = [string.strip().split(' ')[1].split(';')[0].strip() for string in source[i + 1 : j]]
|
||||||
varyings_name.extend(names)
|
varyings_name.extend(names)
|
||||||
|
@ -173,12 +175,15 @@ def destructify_varyings(source, direction):
|
||||||
|
|
||||||
for struct in struct_types:
|
for struct in struct_types:
|
||||||
if struct in line:
|
if struct in line:
|
||||||
variable = line.split(' ')[1].split(';')[0]
|
decomment_line = line.split('//')[0].strip()
|
||||||
|
if len(decomment_line) == 0:
|
||||||
|
continue
|
||||||
|
variable = decomment_line.split(' ')[1].split(';')[0]
|
||||||
|
|
||||||
# Only redirect if the struct is actually used as vertex output.
|
# Only redirect if the struct is actually used as vertex output.
|
||||||
for vout_line in vout_lines:
|
for vout_line in vout_lines:
|
||||||
if variable in vout_line:
|
if variable in vout_line:
|
||||||
log('Found struct variable for', struct + ':', variable)
|
log('Found struct variable for', struct + ':', variable, 'in line:', line)
|
||||||
variables.append(variable)
|
variables.append(variable)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@ -335,15 +340,11 @@ def replace_varyings(source):
|
||||||
if 'void main()' in line:
|
if 'void main()' in line:
|
||||||
for attrib in attribs:
|
for attrib in attribs:
|
||||||
if attrib == 'VertexCoord':
|
if attrib == 'VertexCoord':
|
||||||
source.insert(index, 'attribute vec4 ' + attrib + ';')
|
source.insert(index, 'COMPAT_attribute vec4 ' + attrib + ';')
|
||||||
else:
|
else:
|
||||||
source.insert(index, 'attribute vec2 ' + attrib + ';')
|
source.insert(index, 'COMPAT_attribute vec2 ' + attrib + ';')
|
||||||
for uniform in uniforms:
|
for uniform in uniforms:
|
||||||
source.insert(index, '#endif')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 ' + uniform + ';')
|
||||||
source.insert(index, 'uniform vec2 ' + uniform + ';')
|
|
||||||
source.insert(index, '#else')
|
|
||||||
source.insert(index, 'uniform mediump vec2 ' + uniform + ';')
|
|
||||||
source.insert(index, '#ifdef GL_ES')
|
|
||||||
break
|
break
|
||||||
|
|
||||||
for line in source:
|
for line in source:
|
||||||
|
@ -357,15 +358,9 @@ def hack_source_vertex(source):
|
||||||
ref_index = 0
|
ref_index = 0
|
||||||
for index, line in enumerate(source):
|
for index, line in enumerate(source):
|
||||||
if 'void main()' in line:
|
if 'void main()' in line:
|
||||||
source.insert(index, '#endif')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 InputSize;')
|
||||||
source.insert(index, 'uniform vec2 InputSize;')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 TextureSize;')
|
||||||
source.insert(index, 'uniform vec2 TextureSize;')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 OutputSize;')
|
||||||
source.insert(index, 'uniform vec2 OutputSize;')
|
|
||||||
source.insert(index, '#else')
|
|
||||||
source.insert(index, 'uniform mediump vec2 InputSize;')
|
|
||||||
source.insert(index, 'uniform mediump vec2 TextureSize;')
|
|
||||||
source.insert(index, 'uniform mediump vec2 OutputSize;')
|
|
||||||
source.insert(index, '#ifdef GL_ES')
|
|
||||||
source.insert(index, 'uniform int FrameCount;')
|
source.insert(index, 'uniform int FrameCount;')
|
||||||
source.insert(index, 'uniform int FrameDirection;')
|
source.insert(index, 'uniform int FrameDirection;')
|
||||||
source.insert(index, 'uniform mat4 MVPMatrix;')
|
source.insert(index, 'uniform mat4 MVPMatrix;')
|
||||||
|
@ -412,10 +407,12 @@ def hack_source_vertex(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'),
|
||||||
('FrameCount', 'float(FrameCount)'),
|
('FrameCount', 'float(FrameCount)'),
|
||||||
('FrameDirection', 'float(FrameDirection)'),
|
('FrameDirection', 'float(FrameDirection)'),
|
||||||
('input', 'input_dummy'),
|
('input', 'input_dummy'),
|
||||||
('output', 'output_dummy'), # 'output' is reserved in GLSL.
|
('output', 'output_dummy'), # 'output' is reserved in GLSL.
|
||||||
|
('gl_FragColor', 'FragColor'),
|
||||||
]
|
]
|
||||||
|
|
||||||
for replacement in replace_table:
|
for replacement in replace_table:
|
||||||
|
@ -461,15 +458,9 @@ def hack_source_fragment(source):
|
||||||
ref_index = 0
|
ref_index = 0
|
||||||
for index, line in enumerate(source):
|
for index, line in enumerate(source):
|
||||||
if 'void main()' in line:
|
if 'void main()' in line:
|
||||||
source.insert(index, '#endif')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 InputSize;')
|
||||||
source.insert(index, 'uniform vec2 InputSize;')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 TextureSize;')
|
||||||
source.insert(index, 'uniform vec2 TextureSize;')
|
source.insert(index, 'uniform COMPAT_PRECISION vec2 OutputSize;')
|
||||||
source.insert(index, 'uniform vec2 OutputSize;')
|
|
||||||
source.insert(index, '#else')
|
|
||||||
source.insert(index, 'uniform mediump vec2 InputSize;')
|
|
||||||
source.insert(index, 'uniform mediump vec2 TextureSize;')
|
|
||||||
source.insert(index, 'uniform mediump vec2 OutputSize;')
|
|
||||||
source.insert(index, '#ifdef GL_ES')
|
|
||||||
source.insert(index, 'uniform int FrameCount;')
|
source.insert(index, 'uniform int FrameCount;')
|
||||||
source.insert(index, 'uniform int FrameDirection;')
|
source.insert(index, 'uniform int FrameDirection;')
|
||||||
ref_index = index
|
ref_index = index
|
||||||
|
@ -510,11 +501,7 @@ def hack_source_fragment(source):
|
||||||
for sampler in added_samplers:
|
for sampler in added_samplers:
|
||||||
source.insert(ref_index, sampler)
|
source.insert(ref_index, sampler)
|
||||||
for uniform in uniforms:
|
for uniform in uniforms:
|
||||||
source.insert(ref_index, '#endif')
|
source.insert(ref_index, 'uniform COMPAT_PRECISION vec2 ' + uniform + ';')
|
||||||
source.insert(ref_index, 'uniform vec2 ' + uniform + ';')
|
|
||||||
source.insert(ref_index, '#else')
|
|
||||||
source.insert(ref_index, 'uniform mediump vec2 ' + uniform + ';')
|
|
||||||
source.insert(ref_index, '#ifdef GL_ES')
|
|
||||||
if struct_texunit0:
|
if struct_texunit0:
|
||||||
source.insert(ref_index, 'uniform sampler2D Texture;')
|
source.insert(ref_index, 'uniform sampler2D Texture;')
|
||||||
|
|
||||||
|
@ -528,24 +515,38 @@ def hack_source_fragment(source):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def validate_shader(source, target):
|
def validate_shader(source, target):
|
||||||
command = ['cgc', '-noentry', '-ogles']
|
|
||||||
p = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
|
||||||
stdout_ret, stderr_ret = p.communicate(source.encode())
|
|
||||||
|
|
||||||
log('Shader:')
|
log('Shader:')
|
||||||
log('===')
|
log('===')
|
||||||
log(source)
|
log(source)
|
||||||
log('===')
|
log('===')
|
||||||
|
|
||||||
|
command = ['cgc', '-noentry', '-ogles']
|
||||||
|
p = subprocess.Popen(command, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
|
||||||
|
stdout_ret, stderr_ret = p.communicate(source.encode())
|
||||||
|
|
||||||
log('CGC:', stderr_ret.decode())
|
log('CGC:', stderr_ret.decode())
|
||||||
|
|
||||||
return p.returncode == 0
|
return p.returncode == 0
|
||||||
|
|
||||||
|
def preprocess_vertex(source_data):
|
||||||
|
input_data = source_data.split('\n')
|
||||||
|
ret = []
|
||||||
|
for line in input_data:
|
||||||
|
if 'uniform float4x4' in line:
|
||||||
|
ret.append('#pragma pack_matrix(column_major)\n')
|
||||||
|
ret.append(line)
|
||||||
|
ret.append('#pragma pack_matrix(row_major)\n')
|
||||||
|
else:
|
||||||
|
ret.append(line)
|
||||||
|
return '\n'.join(ret)
|
||||||
|
|
||||||
def convert(source, dest):
|
def convert(source, dest):
|
||||||
vert_cmd = ['cgc', '-profile', 'glesv', '-entry', 'main_vertex']
|
vert_cmd = ['cgc', '-profile', 'glesv', '-entry', 'main_vertex']
|
||||||
with open(source, 'r') as f:
|
with open(source, 'r') as f:
|
||||||
source_data = f.read()
|
source_data = f.read()
|
||||||
p = subprocess.Popen(vert_cmd, stdin = subprocess.PIPE, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
p = subprocess.Popen(vert_cmd, stdin = subprocess.PIPE, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
|
||||||
vertex_source, stderr_ret = p.communicate(input = ('#pragma pack_matrix(column_major)\n' + source_data).encode())
|
source_data = preprocess_vertex(source_data)
|
||||||
|
vertex_source, stderr_ret = p.communicate(input = source_data.encode())
|
||||||
log(stderr_ret.decode())
|
log(stderr_ret.decode())
|
||||||
vertex_source = vertex_source.decode()
|
vertex_source = vertex_source.decode()
|
||||||
|
|
||||||
|
@ -586,8 +587,46 @@ def convert(source, dest):
|
||||||
vertex_source = remove_comments(vertex_source[1:])
|
vertex_source = remove_comments(vertex_source[1:])
|
||||||
fragment_source = remove_comments(fragment_source[1:])
|
fragment_source = remove_comments(fragment_source[1:])
|
||||||
|
|
||||||
out_vertex = '\n'.join(vertex_source)
|
vert_hacks = []
|
||||||
out_fragment = '\n'.join(['#ifdef GL_ES', 'precision mediump float;', '#endif'] + fragment_source)
|
vert_hacks.append('''
|
||||||
|
#if __VERSION__ >= 130
|
||||||
|
#define COMPAT_varying out
|
||||||
|
#define COMPAT_attribute in
|
||||||
|
#else
|
||||||
|
#define COMPAT_varying varying
|
||||||
|
#define COMPAT_attribute attribute
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef GL_ES
|
||||||
|
#define COMPAT_PRECISION mediump
|
||||||
|
#else
|
||||||
|
#define COMPAT_PRECISION
|
||||||
|
#endif''')
|
||||||
|
|
||||||
|
out_vertex = '\n'.join(vert_hacks + vertex_source)
|
||||||
|
|
||||||
|
frag_hacks = []
|
||||||
|
frag_hacks.append('''
|
||||||
|
#if __VERSION__ >= 130
|
||||||
|
#define COMPAT_varying in
|
||||||
|
out vec4 FragColor;
|
||||||
|
#else
|
||||||
|
#define COMPAT_varying varying
|
||||||
|
#define FragColor gl_FragColor
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef GL_ES
|
||||||
|
#ifdef GL_FRAGMENT_PRECISION_HIGH
|
||||||
|
precision highp float;
|
||||||
|
#else
|
||||||
|
precision mediump float;
|
||||||
|
#endif
|
||||||
|
#define COMPAT_PRECISION mediump
|
||||||
|
#else
|
||||||
|
#define COMPAT_PRECISION
|
||||||
|
#endif''')
|
||||||
|
|
||||||
|
out_fragment = '\n'.join(frag_hacks + fragment_source)
|
||||||
|
|
||||||
if not validate_shader(out_vertex, 'glesv'):
|
if not validate_shader(out_vertex, 'glesv'):
|
||||||
log('Vertex shader does not compile ...')
|
log('Vertex shader does not compile ...')
|
||||||
|
|
Loading…
Reference in New Issue