Skip to content

Commit

Permalink
define sokol bindings via layout(binding=n) instead of custom tag
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Oct 7, 2024
1 parent a82e716 commit 5e42076
Show file tree
Hide file tree
Showing 26 changed files with 394 additions and 292 deletions.
48 changes: 24 additions & 24 deletions src/shdc/generators/generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ void Generator::gen_header(const GenInput& gen) {
gen_program_info(gen, prog);
cbl_close();
}
cbl_open("Bindings:\n");
gen_bindings_info(gen);
cbl_close();
cbl_end();
}

Expand All @@ -99,37 +102,36 @@ void Generator::gen_program_info(const GenInput& gen, const ProgramReflection& p
}
}
cbl_close();
gen_bindings_info(gen, prog);
}

void Generator::gen_bindings_info(const GenInput& gen, const ProgramReflection& prog) {
for (const UniformBlock& ub: prog.bindings.uniform_blocks) {
void Generator::gen_bindings_info(const GenInput& gen) {
for (const UniformBlock& ub: gen.refl.bindings.uniform_blocks) {
cbl_open("Uniform block '{}':\n", ub.struct_info.name);
cbl("{} struct: {}\n", lang_name(), struct_name(ub.struct_info.name));
cbl("Bind slot: {} => {}\n", uniform_block_bind_slot_name(prog.name, ub), ub.sokol_slot);
cbl("Bind slot: {} => {}\n", uniform_block_bind_slot_name(ub), ub.sokol_slot);
cbl_close();
}
for (const StorageBuffer& sbuf: prog.bindings.storage_buffers) {
for (const StorageBuffer& sbuf: gen.refl.bindings.storage_buffers) {
cbl_open("Storage buffer '{}':\n", sbuf.struct_info.name);
cbl("{} struct: {}\n", lang_name(), struct_name(sbuf.struct_info.name));
cbl("Bind slot: {} => {}\n", storage_buffer_bind_slot_name(prog.name, sbuf), sbuf.sokol_slot);
cbl("Bind slot: {} => {}\n", storage_buffer_bind_slot_name(sbuf), sbuf.sokol_slot);
cbl_close();
}
for (const Image& img: prog.bindings.images) {
for (const Image& img: gen.refl.bindings.images) {
cbl_open("Image '{}':\n", img.name);
cbl("Image type: {}\n", image_type(img.type));
cbl("Sample type: {}\n", image_sample_type(img.sample_type));
cbl("Multisampled: {}\n", img.multisampled);
cbl("Bind slot: {} => {}\n", image_bind_slot_name(prog.name, img), img.sokol_slot);
cbl("Bind slot: {} => {}\n", image_bind_slot_name(img), img.sokol_slot);
cbl_close();
}
for (const Sampler& smp: prog.bindings.samplers) {
for (const Sampler& smp: gen.refl.bindings.samplers) {
cbl_open("Sampler '{}':\n", smp.name);
cbl("Type: {}\n", sampler_type(smp.type));
cbl("Bind slot: {} => {}\n", sampler_bind_slot_name(prog.name, smp), smp.sokol_slot);
cbl("Bind slot: {} => {}\n", sampler_bind_slot_name(smp), smp.sokol_slot);
cbl_close();
}
for (const ImageSampler& img_smp: prog.bindings.image_samplers) {
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);
Expand All @@ -148,19 +150,17 @@ void Generator::gen_vertex_attr_consts(const GenInput& gen) {
}

void Generator::gen_bind_slot_consts(const GenInput& gen) {
for (const ProgramReflection& prog: gen.refl.progs) {
for (const UniformBlock& ub: prog.bindings.uniform_blocks) {
l("{}\n", uniform_block_bind_slot_definition(prog.name, ub));
}
for (const StorageBuffer& sbuf: prog.bindings.storage_buffers) {
l("{}\n", storage_buffer_bind_slot_definition(prog.name, sbuf));
}
for (const Image& img: prog.bindings.images) {
l("{}\n", image_bind_slot_definition(prog.name, img));
}
for (const Sampler& smp: prog.bindings.samplers) {
l("{}\n", sampler_bind_slot_definition(prog.name, smp));
}
for (const UniformBlock& ub: gen.refl.bindings.uniform_blocks) {
l("{}\n", uniform_block_bind_slot_definition(ub));
}
for (const StorageBuffer& sbuf: gen.refl.bindings.storage_buffers) {
l("{}\n", storage_buffer_bind_slot_definition(sbuf));
}
for (const Image& img: gen.refl.bindings.images) {
l("{}\n", image_bind_slot_definition(img));
}
for (const Sampler& smp: gen.refl.bindings.samplers) {
l("{}\n", sampler_bind_slot_definition(smp));
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/shdc/generators/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Generator {

// called by gen_header()
virtual void gen_program_info(const GenInput& gen, const refl::ProgramReflection& prog);
virtual void gen_bindings_info(const GenInput& gen, const refl::ProgramReflection& prog);
virtual void gen_bindings_info(const GenInput& gen);

// called by gen_uniform_block_decls()
virtual void gen_uniform_block_decl(const GenInput& gen, const refl::UniformBlock& ub) { assert(false && "implement me"); };
Expand Down Expand Up @@ -77,16 +77,16 @@ class Generator {

virtual std::string struct_name(const std::string& name) { assert(false && "implement me"); return ""; };
virtual std::string vertex_attr_name(const std::string& prog_name, const refl::StageAttr& attr) { assert(false && "implement me"); return ""; };
virtual std::string image_bind_slot_name(const std::string& prog_name, const refl::Image& img) { assert(false && "implement me"); return ""; };
virtual std::string sampler_bind_slot_name(const std::string& prog_name, const refl::Sampler& smp) { assert(false && "implement me"); return ""; };
virtual std::string uniform_block_bind_slot_name(const std::string& prog_name, const refl::UniformBlock& ub) { assert(false && "implement me"); return ""; };
virtual std::string storage_buffer_bind_slot_name(const std::string& prog_name, const refl::StorageBuffer& sbuf) { assert(false && "implement me"); return ""; };
virtual std::string image_bind_slot_name(const refl::Image& img) { assert(false && "implement me"); return ""; };
virtual std::string sampler_bind_slot_name(const refl::Sampler& smp) { assert(false && "implement me"); return ""; };
virtual std::string uniform_block_bind_slot_name(const refl::UniformBlock& ub) { assert(false && "implement me"); return ""; };
virtual std::string storage_buffer_bind_slot_name(const refl::StorageBuffer& sbuf) { assert(false && "implement me"); return ""; };

virtual std::string vertex_attr_definition(const std::string& prog_name, const refl::StageAttr& attr) { assert(false && "implement me"); return ""; };
virtual std::string image_bind_slot_definition(const std::string& prog_name, const refl::Image& img) { assert(false && "implement me"); return ""; };
virtual std::string sampler_bind_slot_definition(const std::string& prog_name, const refl::Sampler& smp) { assert(false && "implement me"); return ""; };
virtual std::string uniform_block_bind_slot_definition(const std::string& prog_name, const refl::UniformBlock& ub) { assert(false && "implement me"); return ""; };
virtual std::string storage_buffer_bind_slot_definition(const std::string& prog_name, const refl::StorageBuffer& sbuf) { assert(false && "implement me"); return ""; };
virtual std::string image_bind_slot_definition(const refl::Image& img) { assert(false && "implement me"); return ""; };
virtual std::string sampler_bind_slot_definition(const refl::Sampler& smp) { assert(false && "implement me"); return ""; };
virtual std::string uniform_block_bind_slot_definition(const refl::UniformBlock& ub) { assert(false && "implement me"); return ""; };
virtual std::string storage_buffer_bind_slot_definition(const refl::StorageBuffer& sbuf) { assert(false && "implement me"); return ""; };

struct ShaderStageArrayInfo {
public:
Expand Down
32 changes: 16 additions & 16 deletions src/shdc/generators/sokolc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -684,40 +684,40 @@ std::string SokolCGenerator::vertex_attr_name(const std::string& prog_name, cons
return fmt::format("ATTR_{}_{}", prog_name, attr.name);
}

std::string SokolCGenerator::image_bind_slot_name(const std::string& prog_name, const Image& img) {
return fmt::format("IMG_{}_{}", prog_name, img.name);
std::string SokolCGenerator::image_bind_slot_name(const Image& img) {
return fmt::format("IMG_{}", img.name);
}

std::string SokolCGenerator::sampler_bind_slot_name(const std::string& prog_name, const Sampler& smp) {
return fmt::format("SMP_{}_{}", prog_name, smp.name);
std::string SokolCGenerator::sampler_bind_slot_name(const Sampler& smp) {
return fmt::format("SMP_{}", smp.name);
}

std::string SokolCGenerator::uniform_block_bind_slot_name(const std::string& prog_name, const UniformBlock& ub) {
return fmt::format("UB_{}_{}", prog_name, ub.struct_info.name);
std::string SokolCGenerator::uniform_block_bind_slot_name(const UniformBlock& ub) {
return fmt::format("UB_{}", ub.struct_info.name);
}

std::string SokolCGenerator::storage_buffer_bind_slot_name(const std::string& prog_name, const StorageBuffer& sbuf) {
return fmt::format("SBUF_{}_{}", prog_name, sbuf.struct_info.name);
std::string SokolCGenerator::storage_buffer_bind_slot_name(const StorageBuffer& sbuf) {
return fmt::format("SBUF_{}", sbuf.struct_info.name);
}

std::string SokolCGenerator::vertex_attr_definition(const std::string& prog_name, const StageAttr& attr) {
return fmt::format("#define {} ({})", vertex_attr_name(prog_name, attr), attr.slot);
}

std::string SokolCGenerator::image_bind_slot_definition(const std::string& prog_name, const Image& img) {
return fmt::format("#define {} ({})", image_bind_slot_name(prog_name, img), img.sokol_slot);
std::string SokolCGenerator::image_bind_slot_definition(const Image& img) {
return fmt::format("#define {} ({})", image_bind_slot_name(img), img.sokol_slot);
}

std::string SokolCGenerator::sampler_bind_slot_definition(const std::string& prog_name, const Sampler& smp) {
return fmt::format("#define {} ({})", sampler_bind_slot_name(prog_name, smp), smp.sokol_slot);
std::string SokolCGenerator::sampler_bind_slot_definition(const Sampler& smp) {
return fmt::format("#define {} ({})", sampler_bind_slot_name(smp), smp.sokol_slot);
}

std::string SokolCGenerator::uniform_block_bind_slot_definition(const std::string& prog_name, const UniformBlock& ub) {
return fmt::format("#define {} ({})", uniform_block_bind_slot_name(prog_name, ub), ub.sokol_slot);
std::string SokolCGenerator::uniform_block_bind_slot_definition(const UniformBlock& ub) {
return fmt::format("#define {} ({})", uniform_block_bind_slot_name(ub), ub.sokol_slot);
}

std::string SokolCGenerator::storage_buffer_bind_slot_definition(const std::string& prog_name, const StorageBuffer& sbuf) {
return fmt::format("#define {} ({})", storage_buffer_bind_slot_name(prog_name, sbuf), sbuf.sokol_slot);
std::string SokolCGenerator::storage_buffer_bind_slot_definition(const StorageBuffer& sbuf) {
return fmt::format("#define {} ({})", storage_buffer_bind_slot_name(sbuf), sbuf.sokol_slot);
}

} // namespace
16 changes: 8 additions & 8 deletions src/shdc/generators/sokolc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class SokolCGenerator: public Generator {
virtual std::string backend(Slang::Enum e);
virtual std::string struct_name(const std::string& name);
virtual std::string vertex_attr_name(const std::string& prog_name, const refl::StageAttr& attr);
virtual std::string image_bind_slot_name(const std::string& prog_name, const refl::Image& img);
virtual std::string sampler_bind_slot_name(const std::string& prog_name, const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_name(const std::string& prog_name, const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_name(const std::string& prog_name, const refl::StorageBuffer& sbuf);
virtual std::string image_bind_slot_name(const refl::Image& img);
virtual std::string sampler_bind_slot_name(const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_name(const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_name(const refl::StorageBuffer& sbuf);
virtual std::string vertex_attr_definition(const std::string& prog_name, const refl::StageAttr& attr);
virtual std::string image_bind_slot_definition(const std::string& prog_name, const refl::Image& img);
virtual std::string sampler_bind_slot_definition(const std::string& prog_name, const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_definition(const std::string& prog_name, const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_definition(const std::string& prog_name, const refl::StorageBuffer& sbuf);
virtual std::string image_bind_slot_definition(const refl::Image& img);
virtual std::string sampler_bind_slot_definition(const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_definition(const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_definition(const refl::StorageBuffer& sbuf);
private:
virtual void gen_struct_interior_decl_std430(const GenInput& gen, const refl::Type& struc, int pad_to_size);
};
Expand Down
32 changes: 16 additions & 16 deletions src/shdc/generators/sokold.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,20 @@ std::string SokolDGenerator::vertex_attr_name(const std::string& prog_name, cons
return pystring::upper(fmt::format("ATTR_{}_{}", prog_name, attr.name));
}

std::string SokolDGenerator::image_bind_slot_name(const std::string& prog_name, const Image& img) {
return pystring::upper(fmt::format("IMG_{}_{}", prog_name, img.name));
std::string SokolDGenerator::image_bind_slot_name(const Image& img) {
return pystring::upper(fmt::format("IMG_{}", img.name));
}

std::string SokolDGenerator::sampler_bind_slot_name(const std::string& prog_name, const Sampler& smp) {
return pystring::upper(fmt::format("SMP_{}_{}", prog_name, smp.name));
std::string SokolDGenerator::sampler_bind_slot_name(const Sampler& smp) {
return pystring::upper(fmt::format("SMP_{}", smp.name));
}

std::string SokolDGenerator::uniform_block_bind_slot_name(const std::string& prog_name, const UniformBlock& ub) {
return pystring::upper(fmt::format("UB_{}_{}", prog_name, ub.struct_info.name));
std::string SokolDGenerator::uniform_block_bind_slot_name(const UniformBlock& ub) {
return pystring::upper(fmt::format("UB_{}", ub.struct_info.name));
}

std::string SokolDGenerator::storage_buffer_bind_slot_name(const std::string& prog_name, const StorageBuffer& sbuf) {
return pystring::upper(fmt::format("SBUF_{}_{}", prog_name, sbuf.struct_info.name));
std::string SokolDGenerator::storage_buffer_bind_slot_name(const StorageBuffer& sbuf) {
return pystring::upper(fmt::format("SBUF_{}", sbuf.struct_info.name));
}

static std::string const_def(const std::string& name, int slot) {
Expand All @@ -461,20 +461,20 @@ std::string SokolDGenerator::vertex_attr_definition(const std::string& prog_name
return const_def(vertex_attr_name(prog_name, attr), attr.slot);
}

std::string SokolDGenerator::image_bind_slot_definition(const std::string& prog_name, const Image& img) {
return const_def(image_bind_slot_name(prog_name, img), img.sokol_slot);
std::string SokolDGenerator::image_bind_slot_definition(const Image& img) {
return const_def(image_bind_slot_name(img), img.sokol_slot);
}

std::string SokolDGenerator::sampler_bind_slot_definition(const std::string& prog_name, const Sampler& smp) {
return const_def(sampler_bind_slot_name(prog_name, smp), smp.sokol_slot);
std::string SokolDGenerator::sampler_bind_slot_definition(const Sampler& smp) {
return const_def(sampler_bind_slot_name(smp), smp.sokol_slot);
}

std::string SokolDGenerator::uniform_block_bind_slot_definition(const std::string& prog_name, const UniformBlock& ub) {
return const_def(uniform_block_bind_slot_name(prog_name, ub), ub.sokol_slot);
std::string SokolDGenerator::uniform_block_bind_slot_definition(const UniformBlock& ub) {
return const_def(uniform_block_bind_slot_name(ub), ub.sokol_slot);
}

std::string SokolDGenerator::storage_buffer_bind_slot_definition(const std::string& prog_name, const StorageBuffer& sbuf) {
return const_def(storage_buffer_bind_slot_name(prog_name, sbuf), sbuf.sokol_slot);
std::string SokolDGenerator::storage_buffer_bind_slot_definition(const StorageBuffer& sbuf) {
return const_def(storage_buffer_bind_slot_name(sbuf), sbuf.sokol_slot);
}

} // namespace
16 changes: 8 additions & 8 deletions src/shdc/generators/sokold.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class SokolDGenerator : public Generator {
virtual std::string backend(Slang::Enum e);
virtual std::string struct_name(const std::string& name);
virtual std::string vertex_attr_name(const std::string& prog_name, const refl::StageAttr& attr);
virtual std::string image_bind_slot_name(const std::string& prog_name, const refl::Image& img);
virtual std::string sampler_bind_slot_name(const std::string& prog_name, const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_name(const std::string& prog_name, const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_name(const std::string& prog_name, const refl::StorageBuffer& sbuf);
virtual std::string image_bind_slot_name(const refl::Image& img);
virtual std::string sampler_bind_slot_name(const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_name(const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_name(const refl::StorageBuffer& sbuf);
virtual std::string vertex_attr_definition(const std::string& prog_name, const refl::StageAttr& attr);
virtual std::string image_bind_slot_definition(const std::string& prog_name, const refl::Image& img);
virtual std::string sampler_bind_slot_definition(const std::string& prog_name, const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_definition(const std::string& prog_name, const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_definition(const std::string& prog_name, const refl::StorageBuffer& sbuf);
virtual std::string image_bind_slot_definition(const refl::Image& img);
virtual std::string sampler_bind_slot_definition(const refl::Sampler& smp);
virtual std::string uniform_block_bind_slot_definition(const refl::UniformBlock& ub);
virtual std::string storage_buffer_bind_slot_definition(const refl::StorageBuffer& sbuf);
private:
virtual void gen_struct_interior_decl_std430(const GenInput& gen, const refl::Type& struc, int alignment, int pad_to_size);
};
Expand Down
Loading

0 comments on commit 5e42076

Please sign in to comment.