Skip to content

Commit 188270c

Browse files
polymonsterGBDixonAlex
authored and
GBDixonAlex
committed
- initial support for dxr shaders
1 parent baffb84 commit 188270c

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

cgu.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,22 @@ def find_typedefs(fully_qualified_name, source):
464464
return typedefs, typedef_names
465465

466466

467+
# return list of any typedefs for a particular type
468+
def find_typedef_decls(source):
469+
pos = 0
470+
typedef_decls = []
471+
while True:
472+
start_pos = find_token("typedef", source[pos:])
473+
if start_pos != -1:
474+
start_pos += pos
475+
end_pos = start_pos + source[start_pos:].find(";")
476+
typedef_decls.append(source[start_pos:end_pos])
477+
pos = end_pos
478+
else:
479+
break
480+
return typedef_decls
481+
482+
467483
def find_type_attributes(source, type_pos):
468484
delimiters = [";", "}"]
469485
attr = source[:type_pos].rfind("[[")
@@ -723,7 +739,7 @@ def find_functions(source):
723739
pos = 0
724740
attributes = []
725741
while True:
726-
statement_end, statement_token = find_first(source, [";", "{"], pos)
742+
statement_end, statement_token = find_first(source, [";", "{", "}"], pos)
727743
if statement_end == -1:
728744
break
729745
statement = source[pos:statement_end].strip()
@@ -795,6 +811,28 @@ def get_funtion_prototype(func):
795811
return "(" + args + ")"
796812

797813

814+
# find the line, column position within source
815+
def position_to_line_column(source, position):
816+
if position < 0 or position > len(source):
817+
raise ValueError("position out of bounds")
818+
819+
# split the string into lines
820+
lines = source.splitlines(keepends=True)
821+
822+
# find the line and column
823+
current_pos = 0
824+
for line_number, line in enumerate(lines, start=1):
825+
line_length = len(line)
826+
if current_pos + line_length > position:
827+
# Found the line
828+
column = position - current_pos + 1 # Convert to 1-based
829+
return line_number, column
830+
current_pos += line_length
831+
832+
# If we exit the loop, something went wrong
833+
raise ValueError("position not found in string")
834+
835+
798836
# main function for scope
799837
def test():
800838
# read source from file

pmfx_pipeline.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ def get_shader_stages():
4747
return [
4848
"vs",
4949
"ps",
50-
"cs"
50+
"cs",
51+
"rg",
52+
"ch",
53+
"ah",
54+
"mi",
55+
"is",
56+
"ca"
5157
]
5258

5359

@@ -92,7 +98,8 @@ def get_bindable_resource_keys():
9298
"RWTexture2DArray",
9399
"RWTexture3D",
94100
"SamplerState",
95-
"SamplerComparisonState"
101+
"SamplerComparisonState",
102+
"RaytracingAccelerationStructure"
96103
]
97104

98105

@@ -124,6 +131,7 @@ def get_resource_mappings():
124131
{"category": "textures", "identifier": "RWTexture2D"},
125132
{"category": "textures", "identifier": "RWTexture2DArray"},
126133
{"category": "textures", "identifier": "RWTexture3D"},
134+
{"category": "acceleration_structures", "identifier": "RaytracingAccelerationStructure"},
127135
]
128136

129137

@@ -134,7 +142,8 @@ def get_resource_categories():
134142
"cbuffers",
135143
"structured_buffers",
136144
"textures",
137-
"samplers"
145+
"samplers",
146+
"acceleration_structures"
138147
]
139148

140149

@@ -225,9 +234,10 @@ def get_shader_visibility(vis):
225234
stages = {
226235
"vs": "Vertex",
227236
"ps": "Fragment",
228-
"cs": "Compute"
237+
"cs": "Compute",
229238
}
230-
return stages[vis[0]]
239+
if vis[0] in stages:
240+
return stages[vis[0]]
231241
return "All"
232242

233243

@@ -763,6 +773,13 @@ def cross_compile_hlsl_metal(info, src, stage, entry_point, temp_filepath, outpu
763773
return 0, error_list, output_list
764774

765775

776+
# convert satage to correct hlsl profile
777+
def hlsl_stage(stage):
778+
if stage in ["rg", "ch", "ah", "mi"]:
779+
return "lib"
780+
return stage
781+
782+
766783
# compile a hlsl version 2
767784
def compile_shader_hlsl(info, src, stage, entry_point, temp_filepath, output_filepath):
768785
exe = os.path.join(info.tools_dir, "bin", "dxc", "dxc")
@@ -775,7 +792,7 @@ def compile_shader_hlsl(info, src, stage, entry_point, temp_filepath, output_fil
775792
if info.shader_platform == "metal":
776793
error_code, error_list, output_list = cross_compile_hlsl_metal(info, src, stage, entry_point, temp_filepath, output_filepath)
777794
elif info.shader_platform == "hlsl":
778-
cmdline = "{} -T {}_{} -E {} -Fo {} {}".format(exe, stage, info.shader_version, entry_point, output_filepath, temp_filepath)
795+
cmdline = "{} -T {}_{} -E {} -Fo {} {}".format(exe, hlsl_stage(stage), info.shader_version, entry_point, output_filepath, temp_filepath)
779796
cmdline += " " + build_pmfx.get_info().args
780797
error_code, error_list, output_list = build_pmfx.call_wait_subprocess(cmdline)
781798

@@ -940,19 +957,33 @@ def generate_shader_info(pmfx, entry_point, stage, permute=None):
940957
res += "{}\n".format(pragma)
941958

942959
# resources input structs, textures, buffers etc
960+
added_resources = []
943961
if len(resources) > 0:
944962
res += "// resource declarations\n"
945963
for resource in recursive_resources:
964+
if resource in added_resources:
965+
continue
946966
if recursive_resources[resource]["depth"] > 0:
947967
res += recursive_resources[resource]["declaration"] + ";\n"
968+
added_resources.append(resource)
948969

949970
for resource in resources:
971+
if resource in added_resources:
972+
continue
950973
res += resources[resource]["declaration"] + ";\n"
974+
added_resources.append(resource)
951975

952976
# extract vs_input (input layout)
953977
if stage == "vs":
954978
vertex_elements = get_vertex_elements(pmfx, entry_point)
955979

980+
# typedefs
981+
typedef_decls = cgu.find_typedef_decls(pmfx["source"])
982+
if len(typedef_decls) > 0:
983+
res += "// typedefs\n"
984+
for typedef_decl in typedef_decls:
985+
res += typedef_decl + ";\n"
986+
956987
# add fwd function decls
957988
if len(forward_decls) > 0:
958989
res += "// function foward declarations\n"

0 commit comments

Comments
 (0)