Skip to content

Commit

Permalink
- improve handling of descriptor layout and resource aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Mar 24, 2023
1 parent 7242c96 commit 8fdc11a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
28 changes: 28 additions & 0 deletions examples/v2/v2_examples.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ cbuffer per_pass_vs : register(b1) {
float4 test;
};

struct cbuffer_data {
float4 data2;
};

SamplerState decl_sampler : register(s0);

ConstantBuffer<sb_sb> decl_cbuffer_array[] : register(b2);
Expand All @@ -46,6 +50,13 @@ Texture1D decl_texture1d : register(t0);
Texture2D decl_texture2d : register(t1);
Texture3D decl_texture3d : register(t2);

// alias resource types types on t0
Texture2D textures[] : register(t0);
TextureCube cubemaps[] : register(t0);
Texture2DArray texture_arrays[] : register(t0);
Texture3D volume_textures[] : register(t0);
ConstantBuffer<cbuffer_data> cbuffers[] : register(b0);

void test_func() {
decl_texture1d;
decl_cbuffer_array;
Expand Down Expand Up @@ -104,4 +115,21 @@ vs_output vs_main_separate_elements(float4 pos : POSITION, float4 tex : TEXCOORD
ps_output ps_main() {
test_func2();
return ps_output_default();
}

vs_output vs_test_bindless_aliasing(vs_input input) {
return vs_output_default();
}

ps_output ps_test_bindless_aliasing() {
test_func();
// using this void cast touches the resources so they are detected by pmfx and compiled in
(textures);
(cubemaps);
(texture_arrays);
(volume_textures);

// cbuffers go on a different slot
(cbuffers);
return ps_output_default();
}
4 changes: 4 additions & 0 deletions examples/v2/v2_examples.pmfx
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,9 @@
ps: ps_main
blend_state: additive
}
bindless: {
vs: vs_test_bindless_aliasing
ps: ps_test_bindless_aliasing
}
}
}
39 changes: 33 additions & 6 deletions pmfx_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,41 @@ def generate_descriptor_layout(pmfx, pmfx_pipeline, resources):
"num_descriptors": get_descriptor_array_size(resource)
}
descriptor_layout["bindings"].append(binding)

# combine bindings on the same slot (allow resource type aliasing)
combined_bindings = dict()
for binding in descriptor_layout["bindings"]:
bh = pmfx_hash({
"shader_register": binding["shader_register"],
"register_space": binding["register_space"],
"register_space": binding["register_space"],
"binding_type": binding["binding_type"]
})
if bh not in combined_bindings:
combined_bindings[bh] = binding
else:
cur = combined_bindings[bh]
# extend visibility
if binding["visibility"] != cur["visibility"]:
cur["visibility"] = "All"

# sort bindings in index order
sorted_bindings = list()
for binding in descriptor_layout["bindings"]:
insert_pos = 0
for i in range(0, len(sorted_bindings)):
if binding["shader_register"] < sorted_bindings[i]["shader_register"]:
insert_pos = i
sorted_bindings.insert(insert_pos, binding)
for binding in combined_bindings.values():
sorted_bindings.append(binding)

# bubble sort sort them
sorted = False
while not sorted:
sorted = True
for i in range(0, len(sorted_bindings) - 1):
j = i + 1
if sorted_bindings[j]["shader_register"] < sorted_bindings[i]["shader_register"]:
temp = sorted_bindings[j]["shader_register"]
sorted_bindings[j]["shader_register"] = sorted_bindings[i]["shader_register"]
sorted_bindings[i]["shader_register"] = temp
sorted = False

descriptor_layout["bindings"] = sorted_bindings
return descriptor_layout

Expand Down

0 comments on commit 8fdc11a

Please sign in to comment.