Skip to content

Commit

Permalink
chore: updated the translator frontend to new structure (#176)
Browse files Browse the repository at this point in the history
* chore: updated the translator frontend to new structure

* fix: removed the unused ast nodes

* chore: updated the metal to new struture

* chore: re-structure opengl frontend
  • Loading branch information
samthakur587 authored Oct 6, 2024
1 parent 27f4d4a commit cc1db9f
Show file tree
Hide file tree
Showing 19 changed files with 2,042 additions and 2,159 deletions.
111 changes: 23 additions & 88 deletions crosstl/src/backend/DirectX/DirectxCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,99 +31,34 @@ def __init__(self):
"TextureCube": "samplerCube",
}
self.semantic_map = {
# Vertex inputs position
"POSITION": "in_Position",
"POSITION0": "in_Position0",
"POSITION1": "in_Position1",
"POSITION2": "in_Position2",
"POSITION3": "in_Position3",
"POSITION4": "in_Position4",
"POSITION5": "in_Position5",
"POSITION6": "in_Position6",
"POSITION7": "in_Position7",
# Vertex inputs normal
"NORMAL": "in_Normal",
"NORMAL0": "in_Normal0",
"NORMAL1": "in_Normal1",
"NORMAL2": "in_Normal2",
"NORMAL3": "in_Normal3",
"NORMAL4": "in_Normal4",
"NORMAL5": "in_Normal5",
"NORMAL6": "in_Normal6",
"NORMAL7": "in_Normal7",
# Vertex inputs tangent
"TANGENT": "in_Tangent",
"TANGENT0": "in_Tangent0",
"TANGENT1": "in_Tangent1",
"TANGENT2": "in_Tangent2",
"TANGENT3": "in_Tangent3",
"TANGENT4": "in_Tangent4",
"TANGENT5": "in_Tangent5",
"TANGENT6": "in_Tangent6",
"TANGENT7": "in_Tangent7",
# Vertex inputs binormal
"BINORMAL": "in_Binormal",
"BINORMAL0": "in_Binormal0",
"BINORMAL1": "in_Binormal1",
"BINORMAL2": "in_Binormal2",
"BINORMAL3": "in_Binormal3",
"BINORMAL4": "in_Binormal4",
"BINORMAL5": "in_Binormal5",
"BINORMAL6": "in_Binormal6",
"BINORMAL7": "in_Binormal7",
# Vertex inputs color
"COLOR": "Color",
"COLOR0": "Color0",
"COLOR1": "Color1",
"COLOR2": "Color2",
"COLOR3": "Color3",
"COLOR4": "Color4",
"COLOR5": "Color5",
"COLOR6": "Color6",
"COLOR7": "Color7",
# Vertex inputs texcoord
"TEXCOORD": "TexCoord",
"TEXCOORD0": "TexCoord0",
"TEXCOORD1": "TexCoord1",
"TEXCOORD2": "TexCoord2",
"TEXCOORD3": "TexCoord3",
"TEXCOORD4": "TexCoord4",
"TEXCOORD5": "TexCoord5",
"TEXCOORD6": "TexCoord6",
# Vertex inputs instance
"FRONT_FACE": "gl_IsFrontFace",
"PRIMITIVE_ID": "gl_PrimitiveID",
"INSTANCE_ID": "gl_InstanceID",
"VERTEX_ID": "gl_VertexID",
"INSTANCE_ID": "InstanceID",
"VERTEX_ID": "VertexID",
"SV_InstanceID": "gl_InstanceID",
"SV_VertexID": "gl_VertexID",
# Vertex outputs
"SV_Position": "Out_Position",
"SV_Position0": "Out_Position0",
"SV_Position1": "Out_Position1",
"SV_Position2": "Out_Position2",
"SV_Position3": "Out_Position3",
"SV_Position4": "Out_Position4",
"SV_Position5": "Out_Position5",
"SV_Position6": "Out_Position6",
"SV_Position7": "Out_Position7",
"SV_POSITION": "gl_Position",
# Fragment inputs
"SV_Target": "Out_Color",
"SV_Target0": "Out_Color0",
"SV_Target1": "Out_Color1",
"SV_Target2": "Out_Color2",
"SV_Target3": "Out_Color3",
"SV_Target4": "Out_Color4",
"SV_Target5": "Out_Color5",
"SV_Target6": "Out_Color6",
"SV_Target7": "Out_Color7",
"SV_Depth": "Out_Depth",
"SV_Depth0": "Out_Depth0",
"SV_Depth1": "Out_Depth1",
"SV_Depth2": "Out_Depth2",
"SV_Depth3": "Out_Depth3",
"SV_Depth4": "Out_Depth4",
"SV_Depth5": "Out_Depth5",
"SV_Depth6": "Out_Depth6",
"SV_Depth7": "Out_Depth7",
"SV_TARGET": "gl_FragColor",
"SV_TARGET0": "gl_FragColor0",
"SV_TARGET1": "gl_FragColor1",
"SV_TARGET2": "gl_FragColor2",
"SV_TARGET3": "gl_FragColor3",
"SV_TARGET4": "gl_FragColor4",
"SV_TARGET5": "gl_FragColor5",
"SV_TARGET6": "gl_FragColor6",
"SV_TARGET7": "gl_FragColor7",
"SV_DEPTH": "gl_FragDepth",
"SV_DEPTH0": "gl_FragDepth0",
"SV_DEPTH1": "gl_FragDepth1",
"SV_DEPTH2": "gl_FragDepth2",
"SV_DEPTH3": "gl_FragDepth3",
"SV_DEPTH4": "gl_FragDepth4",
"SV_DEPTH5": "gl_FragDepth5",
"SV_DEPTH6": "gl_FragDepth6",
"SV_DEPTH7": "gl_FragDepth7",
}

def generate(self, ast):
Expand Down
24 changes: 12 additions & 12 deletions crosstl/src/backend/Metal/MetalCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ def __init__(self):
}

self.map_semantics = {
"attribute(0)": "in_Position",
"attribute(1)": "in_Normal",
"attribute(2)": "in_Tangent",
"attribute(3)": "in_Binormal",
"attribute(0)": "Position",
"attribute(1)": "Normal",
"attribute(2)": "Tangent",
"attribute(3)": "Binormal",
"attribute(4)": "TexCoord",
"attribute(5)": "TexCoord0",
"attribute(6)": "TexCoord1",
Expand All @@ -53,18 +53,18 @@ def __init__(self):
"instance_id": "gl_InstanceID",
"base_vertex": "gl_BaseVertex",
"base_instance": "gl_BaseInstance",
"position": "out_Position",
"position": "gl_Position",
"point_size": "gl_PointSize",
"clip_distance": "gl_ClipDistance",
"front_facing": "gl_IsFrontFace",
"point_coord": "gl_PointCoord",
"color(0)": "out_Color",
"color(1)": "out_Color1",
"color(2)": "out_Color2",
"color(3)": "out_Color3",
"color(4)": "out_Color4",
"depth(any)": "Out_Depth",
"stage_in": "gl_in",
"color(0)": "gl_FragColor",
"color(1)": "gl_FragColor1",
"color(2)": "gl_FragColor2",
"color(3)": "gl_FragColor3",
"color(4)": "gl_FragColor4",
"depth(any)": "gl_FragDepth",
"stage_in": "gl_FragColor",
}

def generate(self, ast):
Expand Down
10 changes: 6 additions & 4 deletions crosstl/src/backend/Opengl/OpenglAst.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ def __repr__(self):


class LayoutNode:
def __init__(self, location_number, dtype, name, io_type):
def __init__(self, location_number, dtype, name, io_type, semantic=None):
self.location_number = location_number
self.dtype = dtype
self.name = name
self.io_type = io_type
self.semantic = semantic

def __repr__(self):
return f"LayoutNode(location_number={self.location_number}, dtype={self.dtype}, name={self.name}, io_type={self.io_type})"
return f"LayoutNode(location_number={self.location_number}, dtype={self.dtype}, name={self.name}, io_type={self.io_type}, semantic={self.semantic})"


class ShaderNode(ASTNode):
Expand Down Expand Up @@ -81,13 +82,14 @@ def __repr__(self):


class VariableNode(ASTNode):
def __init__(self, vtype, name, io_type=None):
def __init__(self, vtype, name, io_type=None, semantic=None):
self.vtype = vtype
self.name = name
self.io_type = io_type
self.semantic = semantic

def __repr__(self):
return f"VariableNode(vtype='{self.vtype}', name='{self.name}', io_type={self.io_type})"
return f"VariableNode(vtype='{self.vtype}', name='{self.name}', io_type={self.io_type}, semantic={self.semantic})"


class AssignmentNode(ASTNode):
Expand Down
19 changes: 14 additions & 5 deletions crosstl/src/backend/Opengl/OpenglParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@


class GLSLParser:
def __init__(self, tokens, shader_type):
def __init__(self, tokens, shader_type="vertex"):
self.tokens = tokens
self.shader_type = shader_type
self.pos = 0
self.main_count = 0
self.current_token = self.tokens[self.pos]
self.skip_comments()

Expand All @@ -15,6 +16,9 @@ def skip_comments(self):
self.eat(self.current_token[0])

def eat(self, token_type):
if self.current_token[1] == "main" and self.shader_type == "vertex":
self.shader_type = "fragment"
# If we encounter the second 'main', switch shader_type to 'fragment'
if self.current_token[0] == token_type:
self.pos += 1
self.current_token = (
Expand Down Expand Up @@ -60,7 +64,7 @@ def parse_shader(self):
else:
global_variables.append(self.parse_global_variable())
else:
self.eat(self.current_token[0]) # Skip unknown tokens
self.eat(self.current_token[0]) # Skip token

return ShaderNode(
io_variables,
Expand Down Expand Up @@ -181,7 +185,13 @@ def parse_layout(self):
if self.current_token[0] == "COMMA":
self.eat("COMMA")
self.eat("SEMICOLON")
return LayoutNode(location_number, dtype, name, f"{self.shader_type}_{io_type}")
return LayoutNode(
location_number,
dtype,
name,
f"{self.shader_type}_{io_type}",
f"layout(location = {location_number})",
)

def parse_uniform(self):
self.eat("UNIFORM")
Expand All @@ -198,8 +208,7 @@ def parse_function(self):
name = self.current_token[1]
qualifier = None
if name == "main":
qualifier = "main"

qualifier = self.shader_type
self.eat("IDENTIFIER")
self.eat("LPAREN")
params = self.parse_parameters()
Expand Down
Loading

0 comments on commit cc1db9f

Please sign in to comment.