Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor gles3 and glsl python builders #671

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions gles3_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def __init__(self):
self.specialization_values = []


def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct, depth: int):
def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct, depth: int) -> GLES3HeaderStruct:
with open(filename, "r", encoding="utf-8") as fs:
line = fs.readline()

while line:
if line.find("=") != -1 and header_data.reading == "":
if "=" in line and header_data.reading == "":
# Mode
eqpos = line.find("=")
defname = line[:eqpos].strip().upper()
Expand All @@ -48,7 +48,7 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,
header_data.vertex_offset = header_data.line_offset
continue

if line.find("=") != -1 and header_data.reading == "specializations":
if "=" in line and header_data.reading == "specializations":
# Specialization
eqpos = line.find("=")
specname = line[:eqpos].strip()
Expand All @@ -60,35 +60,35 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,
header_data.vertex_offset = header_data.line_offset
continue

if line.find("#[modes]") != -1:
if "#[modes]" in line:
# Nothing really, just skip
line = fs.readline()
header_data.line_offset += 1
header_data.vertex_offset = header_data.line_offset
continue

if line.find("#[specializations]") != -1:
if "#[specializations]" in line:
header_data.reading = "specializations"
line = fs.readline()
header_data.line_offset += 1
header_data.vertex_offset = header_data.line_offset
continue

if line.find("#[vertex]") != -1:
if "#[vertex]" in line:
header_data.reading = "vertex"
line = fs.readline()
header_data.line_offset += 1
header_data.vertex_offset = header_data.line_offset
continue

if line.find("#[fragment]") != -1:
if "#[fragment]" in line:
header_data.reading = "fragment"
line = fs.readline()
header_data.line_offset += 1
header_data.fragment_offset = header_data.line_offset
continue

while line.find("#include ") != -1:
while "#include " in line:
includeline = line.replace("#include ", "").strip()[1:-1]

included_file = os.path.relpath(os.path.dirname(filename) + "/" + includeline)
Expand All @@ -103,7 +103,7 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,

line = fs.readline()

if line.find("uniform") != -1 and line.lower().find("texunit:") != -1:
if "uniform" in line and "texunit:" in line.lower():
# texture unit
texunitstr = line[line.find(":") + 1 :].strip()
if texunitstr == "auto":
Expand All @@ -126,7 +126,7 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,
header_data.texunits += [(x, texunit)]
header_data.texunit_names += [x]

elif line.find("uniform") != -1 and line.lower().find("ubo:") != -1:
elif "uniform" in line and "ubo:" in line.lower():
# uniform buffer object
ubostr = line[line.find(":") + 1 :].strip()
ubo = str(int(ubostr))
Expand All @@ -147,7 +147,7 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,
header_data.ubos += [(x, ubo)]
header_data.ubo_names += [x]

elif line.find("uniform") != -1 and line.find("{") == -1 and line.find(";") != -1:
elif "uniform" in line and "{" in line and ";" in line:
uline = line.replace("uniform", "")
uline = uline.replace(";", "")
lines = uline.split(",")
Expand All @@ -161,7 +161,7 @@ def include_file_in_gles3_header(filename: str, header_data: GLES3HeaderStruct,
if x not in header_data.uniforms:
header_data.uniforms += [x]

if (line.strip().find("out ") == 0 or line.strip().find("flat ") == 0) and line.find("tfb:") != -1:
if (line.strip().startswith("out ") or line.strip().startswith("flat ")) and "tfb:" in line:
uline = line.replace("flat ", "")
uline = uline.replace("out ", "")
uline = uline.replace("highp ", "")
Expand Down Expand Up @@ -195,14 +195,11 @@ def build_gles3_header(
class_suffix: str,
optional_output_filename: Optional[str] = None,
header_data: Optional[GLES3HeaderStruct] = None,
):
) -> None:
header_data = header_data or GLES3HeaderStruct()
include_file_in_gles3_header(filename, header_data, 0)

if optional_output_filename is None:
out_file = filename + ".gen.h"
else:
out_file = optional_output_filename
out_file = optional_output_filename or filename + ".gen.h"

with open(out_file, "w", encoding="utf-8", newline="\n") as fd:
defspec = 0
Expand Down
22 changes: 8 additions & 14 deletions glsl_builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ def include_file_in_rd_header(filename: str, header_data: RDHeaderStruct, depth:
if index != -1:
line = line[:index]

if line.find("#[vertex]") != -1:
if "#[vertex]" in line:
header_data.reading = "vertex"
line = fs.readline()
header_data.line_offset += 1
header_data.vertex_offset = header_data.line_offset
continue

if line.find("#[fragment]") != -1:
if "#[fragment]" in line:
header_data.reading = "fragment"
line = fs.readline()
header_data.line_offset += 1
header_data.fragment_offset = header_data.line_offset
continue

if line.find("#[compute]") != -1:
if "#[compute]" in line:
header_data.reading = "compute"
line = fs.readline()
header_data.line_offset += 1
header_data.compute_offset = header_data.line_offset
continue

while line.find("#include ") != -1:
while "#include " in line:
includeline = line.replace("#include ", "").strip()[1:-1]

if includeline.startswith("thirdparty/"):
Expand Down Expand Up @@ -98,10 +98,7 @@ def build_rd_header(
header_data = header_data or RDHeaderStruct()
include_file_in_rd_header(filename, header_data, 0)

if optional_output_filename is None:
out_file = filename + ".gen.h"
else:
out_file = optional_output_filename
out_file = optional_output_filename or filename + ".gen.h"

out_file_base = out_file
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
Expand Down Expand Up @@ -162,7 +159,7 @@ def include_file_in_raw_header(filename: str, header_data: RAWHeaderStruct, dept
line = fs.readline()

while line:
while line.find("#include ") != -1:
while "#include " in line:
includeline = line.replace("#include ", "").strip()[1:-1]

included_file = os.path.relpath(os.path.dirname(filename) + "/" + includeline)
Expand All @@ -176,14 +173,11 @@ def include_file_in_raw_header(filename: str, header_data: RAWHeaderStruct, dept

def build_raw_header(
filename: str, optional_output_filename: Optional[str] = None, header_data: Optional[RAWHeaderStruct] = None
):
) -> None:
header_data = header_data or RAWHeaderStruct()
include_file_in_raw_header(filename, header_data, 0)

if optional_output_filename is None:
out_file = filename + ".gen.h"
else:
out_file = optional_output_filename
out_file = optional_output_filename or filename + ".gen.h"

out_file_base = out_file.replace(".glsl.gen.h", "_shader_glsl")
out_file_base = out_file_base[out_file_base.rfind("/") + 1 :]
Expand Down