Skip to content

Commit

Permalink
only merge image-sampler-pair bindings for shader programs, but not a…
Browse files Browse the repository at this point in the history
…cross programs, see #154 for details
  • Loading branch information
floooh committed Nov 9, 2024
1 parent a7eabc4 commit 932c77e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
6 changes: 0 additions & 6 deletions src/shdc/generators/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ void Generator::gen_bindings_info(const GenInput& gen) {
cbl("Bind slot: {} => {}\n", sampler_bind_slot_name(smp), smp.sokol_slot);
cbl_close();
}
for (const ImageSampler& img_smp: gen.refl.bindings.image_samplers) {
cbl_open("Image Sampler Pair '{}':\n", img_smp.name);
cbl("Image: {}\n", img_smp.image_name);
cbl("Sampler: {}\n", img_smp.sampler_name);
cbl_close();
}
}

void Generator::gen_vertex_attr_consts(const GenInput& gen) {
Expand Down
29 changes: 17 additions & 12 deletions src/shdc/reflection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ StageReflection Reflection::parse_snippet_reflection(const Compiler& compiler, c
return refl;
}

Bindings Reflection::merge_bindings(const std::vector<Bindings>& in_bindings, bool assign_image_sampler_slots, ErrMsg& out_error) {
Bindings Reflection::merge_bindings(const std::vector<Bindings>& in_bindings, bool to_prog_bindings, ErrMsg& out_error) {
Bindings out_bindings;
out_error = ErrMsg();
for (const Bindings& src_bindings: in_bindings) {
Expand Down Expand Up @@ -433,24 +433,29 @@ Bindings Reflection::merge_bindings(const std::vector<Bindings>& in_bindings, bo
}
}

// merge image samplers
for (const ImageSampler& img_smp: src_bindings.image_samplers) {
const ImageSampler* other_img_smp = out_bindings.find_image_sampler_by_name(img_smp.name);
if (other_img_smp) {
// another image sampler of the same name exists, make sure it's identical
if (!img_smp.equals(*other_img_smp)) {
out_error = ErrMsg::error(fmt::format("conflicting image-sampler definition found for '{}'", img_smp.name));
return Bindings();
// merge image samplers (only for prog bindings)
// since image-samplers will have their bindings auto-assigned their slots won't
// match across programs, but common image-sampler bindings across programs are also not required
// anywhere (such bindings are only needed for generating the common slot constants)
if (to_prog_bindings) {
for (const ImageSampler& img_smp: src_bindings.image_samplers) {
const ImageSampler* other_img_smp = out_bindings.find_image_sampler_by_name(img_smp.name);
if (other_img_smp) {
// another image sampler of the same name exists, make sure it's identical
if (!img_smp.equals(*other_img_smp)) {
out_error = ErrMsg::error(fmt::format("conflicting image-sampler definition found for '{}'", img_smp.name));
return Bindings();
}
} else {
out_bindings.image_samplers.push_back(img_smp);
}
} else {
out_bindings.image_samplers.push_back(img_smp);
}
}
}

// if requested, assign new image-sampler slots which are unique across shader stages, this
// is needed when merging the per-shader-stage bindings into per-program-bindings
if (assign_image_sampler_slots) {
if (to_prog_bindings) {
int sokol_slot = 0;
for (ImageSampler& img_smp: out_bindings.image_samplers) {
img_smp.sokol_slot = sokol_slot++;
Expand Down
2 changes: 1 addition & 1 deletion src/shdc/reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Reflection {

private:
// create a set of unique resource bindings from shader snippet input bindings
static Bindings merge_bindings(const std::vector<Bindings>& in_bindings, bool assign_image_sampler_slots, ErrMsg& out_error);
static Bindings merge_bindings(const std::vector<Bindings>& in_bindings, bool to_prog_bindings, ErrMsg& out_error);
// parse a struct
static Type parse_toplevel_struct(const spirv_cross::Compiler& compiler, const spirv_cross::Resource& res, ErrMsg& out_error);
// parse a struct item
Expand Down

0 comments on commit 932c77e

Please sign in to comment.