Skip to content

Commit fd72582

Browse files
author
hartsantler
committed
refactoring to allow return of mat4 from gpu.main
1 parent c091813 commit fd72582

5 files changed

Lines changed: 382 additions & 231 deletions

File tree

pythonjs/python_to_pythonjs.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,10 +1201,25 @@ def visit_Return(self, node):
12011201
## _id_ is inserted into all function headers by pythonjs.py for glsl functions.
12021202
if not self._gpu_return_types:
12031203
raise SyntaxError( self.format_error('function return type unknown - required decorator `@returns(array/vec4=[w,h])`') )
1204+
1205+
## only one return type is allowed ##
12041206
if 'array' in self._gpu_return_types:
12051207
writer.write('out_float = %s' %self.visit(node.value))
1206-
if 'vec4' in self._gpu_return_types:
1208+
elif 'vec4' in self._gpu_return_types:
12071209
writer.write('out_float4 = %s' %self.visit(node.value))
1210+
elif 'mat4' in self._gpu_return_types:
1211+
nv = self.visit(node.value)
1212+
writer.write('inline("mat4 _res_ = %s;")' %nv)
1213+
a = 'vec4(_res_[3][0],_res_[3][1],_res_[3][2],_res_[3][3])'
1214+
writer.write('if int(mod(float(_FRAGMENT_ID_), 4.0))==0: out_float4 = %s' % a)
1215+
a = 'vec4(_res_[2][0],_res_[2][1],_res_[2][2],_res_[2][3])'
1216+
writer.write('elif int(mod(float(_FRAGMENT_ID_), 3.0))==0: out_float4 = %s' % a)
1217+
a = 'vec4(_res_[1][0],_res_[1][1],_res_[1][2],_res_[1][3])'
1218+
writer.write('elif int(mod(float(_FRAGMENT_ID_), 2.0))==0: out_float4 = %s' % a)
1219+
a = 'vec4(_res_[0][0],_res_[0][1],_res_[0][2],_res_[0][3])'
1220+
writer.write('else: out_float4 = %s'%a)
1221+
else:
1222+
raise SyntaxError( self.format_error('invalid GPU return type: %s' %self._gpu_return_types) )
12081223

12091224
elif self._inline:
12101225
writer.write('__returns__%s = %s' %(self._inline[-1], self.visit(node.value)) )
@@ -1792,6 +1807,9 @@ def _visit_assign_helper(self, node, target):
17921807
if node_value in self._typedef_vars:
17931808
writer.write('%s = %s' % (self.visit(target), self.visit(node.value)))
17941809

1810+
elif isinstance(node.value, ast.Subscript) and isinstance(node.value.slice, ast.Ellipsis):
1811+
writer.write('glsl_inline_assign_from_iterable("%s", "%s", %s)'%(self._typedef_vars[target.id], target.id, self.visit(node.value.value)) )
1812+
17951813
else:
17961814

17971815
## also assign variable in current javascript scope ##
@@ -2109,6 +2127,12 @@ def visit_Call(self, node):
21092127

21102128
F = '%s_%s' %(clsname, node.func.attr)
21112129

2130+
elif isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name) and node.func.value.id in self._typedef_vars:
2131+
#raise RuntimeError(node.func.value.id)
2132+
clsname = self._typedef_vars[ node.func.value.id ]
2133+
F = '%s_%s' %(clsname, node.func.attr)
2134+
args.insert(0, node.func.value.id)
2135+
21122136

21132137
if node.keywords:
21142138
args.extend( [self.visit(x.value) for x in node.keywords] )
@@ -2559,6 +2583,8 @@ def visit_FunctionDef(self, node):
25592583
assert len(decorator.args) == 1
25602584
assert isinstance( decorator.args[0], Name)
25612585
return_type = decorator.args[0].id
2586+
if return_type in typedpython.glsl_types:
2587+
self._gpu_return_types.add( return_type )
25622588

25632589
elif isinstance(decorator, Attribute) and isinstance(decorator.value, Name) and decorator.value.id == 'gpu':
25642590
gpu = True

0 commit comments

Comments
 (0)