Skip to content

Commit

Permalink
reflection refactoring wip
Browse files Browse the repository at this point in the history
  • Loading branch information
floooh committed Mar 17, 2024
1 parent cc4d9cf commit 0ac391b
Show file tree
Hide file tree
Showing 22 changed files with 343 additions and 398 deletions.
5 changes: 4 additions & 1 deletion src/shdc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
fips_begin_app(sokol-shdc cmdline)
fips_src(.)
fips_src(. NO_RECURSE)
fips_src(formats NO_RECURSE)
fips_src(types NO_RECURSE)
fips_src(types/reflection)
fips_deps(fmt getopt pystring glslang SPIRV-Cross tint)
target_include_directories(sokol-shdc PRIVATE .)
if (FIPS_GCC OR FIPS_CLANG)
Expand Down
10 changes: 5 additions & 5 deletions src/shdc/formats/bare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ static ErrMsg write_shader_sources_and_blobs(const Args& args,

ErrMsg err;
err = write_stage(file_path_vs, vs_src, vs_blob);
if (err.has_error) {
if (err.valid()) {
return err;
}
err = write_stage(file_path_fs, fs_src, fs_blob);
if (err.has_error) {
if (err.valid()) {
return err;
}
}
Expand All @@ -74,11 +74,11 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
Slang::Enum slang = (Slang::Enum) i;
if (args.slang & Slang::bit(slang)) {
ErrMsg err = check_errors(inp, spirvcross[i], slang);
if (err.has_error) {
if (err.valid()) {
return err;
}
err = write_shader_sources_and_blobs(args, inp, spirvcross[i], bytecode[i], slang);
if (err.has_error) {
if (err.valid()) {
return err;
}
}
Expand All @@ -87,4 +87,4 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
return ErrMsg();
}

} // namespace
} // namespace
78 changes: 39 additions & 39 deletions src/shdc/formats/sokol.cc

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions src/shdc/formats/sokolnim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,47 +127,47 @@ static void write_header(const Args& args, const Input& inp, const Spirvcross& s
L("# ATTR_{}{}_{} = {}\n", mod_prefix(inp), vs_snippet.name, attr.name, attr.slot);
}
}
for (const UniformBlock& ub: vs_src->refl.uniform_blocks) {
for (const UniformBlock& ub: vs_src->refl.bindings.uniform_blocks) {
L("# Uniform block '{}':\n", ub.struct_name);
L("# Nim struct: {}\n", to_nim_struct_name(mod_prefix(inp), ub.struct_name));
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), ub.struct_name, ub.slot);
}
for (const Image& img: vs_src->refl.images) {
for (const Image& img: vs_src->refl.bindings.images) {
L("# Image '{}':\n", img.name);
L("# Image Type: {}\n", img_type_to_sokol_type_str(img.type));
L("# Sample Type: {}\n", img_basetype_to_sokol_sampletype_str(img.sample_type));
L("# Multisampled: {}\n", img.multisampled);
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), img.name, img.slot);
}
for (const Sampler& smp: vs_src->refl.samplers) {
for (const Sampler& smp: vs_src->refl.bindings.samplers) {
L("# Sampler '{}':\n", smp.name);
L("# Type: {}\n", smp_type_to_sokol_type_str(smp.type));
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), smp.name, smp.slot);
}
for (const ImageSampler& img_smp: vs_src->refl.image_samplers) {
for (const ImageSampler& img_smp: vs_src->refl.bindings.image_samplers) {
L("# Image Sampler Pair '{}':\n", img_smp.name);
L("# Image: {}\n", img_smp.image_name);
L("# Sampler: {}\n", img_smp.sampler_name);
}
L("# Fragment shader: {}\n", prog.fs_name);
for (const UniformBlock& ub: fs_src->refl.uniform_blocks) {
for (const UniformBlock& ub: fs_src->refl.bindings.uniform_blocks) {
L("# Uniform block '{}':\n", ub.struct_name);
L("# Nim struct: {}\n", to_nim_struct_name(mod_prefix(inp), ub.struct_name));
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), ub.struct_name, ub.slot);
}
for (const Image& img: fs_src->refl.images) {
for (const Image& img: fs_src->refl.bindings.images) {
L("# Image '{}':\n", img.name);
L("# Image Type: {}\n", img_type_to_sokol_type_str(img.type));
L("# Sample Type: {}\n", img_basetype_to_sokol_sampletype_str(img.sample_type));
L("# Multisampled: {}\n", img.multisampled);
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), img.name, img.slot);
}
for (const Sampler& smp: fs_src->refl.samplers) {
for (const Sampler& smp: fs_src->refl.bindings.samplers) {
L("# Sampler '{}':\n", smp.name);
L("# Type: {}\n", smp_type_to_sokol_type_str(smp.type));
L("# Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), smp.name, smp.slot);
}
for (const ImageSampler& img_smp: fs_src->refl.image_samplers) {
for (const ImageSampler& img_smp: fs_src->refl.bindings.image_samplers) {
L("# Image Sampler Pair '{}':\n", img_smp.name);
L("# Image: {}\n", img_smp.image_name);
L("# Sampler: {}\n", img_smp.sampler_name);
Expand Down Expand Up @@ -198,23 +198,23 @@ static void write_vertex_attrs(const Input& inp, const Spirvcross& spirvcross) {
}

static void write_image_bind_slots(const Input& inp, const Spirvcross& spirvcross) {
for (const Image& img: spirvcross.unique_images) {
for (const Image& img: spirvcross.bindings.images) {
const auto slotName = to_camel_case(fmt::format("SLOT_{}_{}", mod_prefix(inp), img.name));
L("const {}* = {}\n", slotName, img.slot);
}
L("\n");
}

static void write_sampler_bind_slots(const Input& inp, const Spirvcross& spirvcross) {
for (const Sampler& smp: spirvcross.unique_samplers) {
for (const Sampler& smp: spirvcross.bindings.samplers) {
const auto slotName = to_camel_case(fmt::format("SLOT_{}_{}", mod_prefix(inp), smp.name));
L("const {}* = {}\n", slotName, smp.slot);
}
L("\n");
}

static void write_uniform_blocks(const Input& inp, const Spirvcross& spirvcross, Slang::Enum slang) {
for (const UniformBlock& ub: spirvcross.unique_uniform_blocks) {
for (const UniformBlock& ub: spirvcross.bindings.uniform_blocks) {
const auto slotName = to_camel_case(fmt::format("SLOT_{}_{}", mod_prefix(inp), ub.struct_name));
L("const {}* = {}\n", slotName, ub.slot);
L("type {}* {{.packed.}} = object\n", to_nim_struct_name(mod_prefix(inp), ub.struct_name));
Expand Down Expand Up @@ -361,7 +361,7 @@ static void write_stage(const char* indent,
assert(src);
L("{}result.{}.entry = \"{}\"\n", indent, stage_name, src->refl.entry_point);
for (int ub_index = 0; ub_index < UniformBlock::NUM; ub_index++) {
const UniformBlock* ub = src->refl.find_uniform_block_by_slot(ub_index);
const UniformBlock* ub = src->refl.bindings.find_uniform_block_by_slot(ub_index);
if (ub) {
L("{}result.{}.uniformBlocks[{}].size = {}\n", indent, stage_name, ub_index, roundup(ub->size, 16));
L("{}result.{}.uniformBlocks[{}].layout = uniformLayoutStd140\n", indent, stage_name, ub_index);
Expand All @@ -382,7 +382,7 @@ static void write_stage(const char* indent,
}
}
for (int img_index = 0; img_index < Image::NUM; img_index++) {
const Image* img = src->refl.find_image_by_slot(img_index);
const Image* img = src->refl.bindings.find_image_by_slot(img_index);
if (img) {
L("{}result.{}.images[{}].used = true\n", indent, stage_name, img_index);
L("{}result.{}.images[{}].multisampled = {}\n", indent, stage_name, img_index, img->multisampled ? "true" : "false");
Expand All @@ -391,18 +391,18 @@ static void write_stage(const char* indent,
}
}
for (int smp_index = 0; smp_index < Sampler::NUM; smp_index++) {
const Sampler* smp = src->refl.find_sampler_by_slot(smp_index);
const Sampler* smp = src->refl.bindings.find_sampler_by_slot(smp_index);
if (smp) {
L("{}result.{}.samplers[{}].used = true\n", indent, stage_name, smp_index);
L("{}result.{}.samplers[{}].samplerType = {}\n", indent, stage_name, smp_index, smp_type_to_sokol_type_str(smp->type));
}
}
for (int img_smp_index = 0; img_smp_index < ImageSampler::NUM; img_smp_index++) {
const ImageSampler* img_smp = src->refl.find_image_sampler_by_slot(img_smp_index);
const ImageSampler* img_smp = src->refl.bindings.find_image_sampler_by_slot(img_smp_index);
if (img_smp) {
L("{}result.{}.imageSamplerPairs[{}].used = true\n", indent, stage_name, img_smp_index);
L("{}result.{}.imageSamplerPairs[{}].imageSlot = {}\n", indent, stage_name, img_smp_index, src->refl.find_image_by_name(img_smp->image_name)->slot);
L("{}result.{}.imageSamplerPairs[{}].samplerSlot = {}\n", indent, stage_name, img_smp_index, src->refl.find_sampler_by_name(img_smp->sampler_name)->slot);
L("{}result.{}.imageSamplerPairs[{}].imageSlot = {}\n", indent, stage_name, img_smp_index, src->refl.bindings.find_image_by_name(img_smp->image_name)->slot);
L("{}result.{}.imageSamplerPairs[{}].samplerSlot = {}\n", indent, stage_name, img_smp_index, src->refl.bindings.find_sampler_by_name(img_smp->sampler_name)->slot);
if (Slang::is_glsl(slang)) {
L("{}result.{}.imageSamplerPairs[{}].glslName = \"{}\"\n", indent, stage_name, img_smp_index, img_smp->name);
}
Expand Down Expand Up @@ -458,7 +458,7 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
Slang::Enum slang = (Slang::Enum) i;
if (args.slang & Slang::bit(slang)) {
ErrMsg err = check_errors(inp, spirvcross[i], slang);
if (err.has_error) {
if (err.valid()) {
return err;
}
if (!comment_header_written) {
Expand Down
36 changes: 18 additions & 18 deletions src/shdc/formats/sokolodin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,47 +123,47 @@ static void write_header(const Args& args, const Input& inp, const Spirvcross& s
L(" ATTR_{}{}_{} = {}\n", mod_prefix(inp), vs_snippet.name, attr.name, attr.slot);
}
}
for (const UniformBlock& ub: vs_src->refl.uniform_blocks) {
for (const UniformBlock& ub: vs_src->refl.bindings.uniform_blocks) {
L(" Uniform block '{}':\n", ub.struct_name);
L(" C struct: {}{}_t\n", mod_prefix(inp), ub.struct_name);
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), ub.struct_name, ub.slot);
}
for (const Image& img: vs_src->refl.images) {
for (const Image& img: vs_src->refl.bindings.images) {
L(" Image '{}':\n", img.name);
L(" Image Type: {}\n", img_type_to_sokol_type_str(img.type));
L(" Sample Type: {}\n", img_basetype_to_sokol_sampletype_str(img.sample_type));
L(" Multisampled: {}\n", img.multisampled);
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), img.name, img.slot);
}
for (const Sampler& smp: vs_src->refl.samplers) {
for (const Sampler& smp: vs_src->refl.bindings.samplers) {
L(" Sampler '{}':\n", smp.name);
L(" Type: {}\n", smp_type_to_sokol_type_str(smp.type));
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), smp.name, smp.slot);
}
for (const ImageSampler& img_smp: vs_src->refl.image_samplers) {
for (const ImageSampler& img_smp: vs_src->refl.bindings.image_samplers) {
L(" Image Sampler Pair '{}':\n", img_smp.name);
L(" Image: {}\n", img_smp.image_name);
L(" Sampler: {}\n", img_smp.sampler_name);
}
L(" Fragment shader: {}\n", prog.fs_name);
for (const UniformBlock& ub: fs_src->refl.uniform_blocks) {
for (const UniformBlock& ub: fs_src->refl.bindings.uniform_blocks) {
L(" Uniform block '{}':\n", ub.struct_name);
L(" C struct: {}{}_t\n", mod_prefix(inp), ub.struct_name);
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), ub.struct_name, ub.slot);
}
for (const Image& img: fs_src->refl.images) {
for (const Image& img: fs_src->refl.bindings.images) {
L(" Image '{}':\n", img.name);
L(" Image Type: {}\n", img_type_to_sokol_type_str(img.type));
L(" Sample Type: {}\n", img_basetype_to_sokol_sampletype_str(img.sample_type));
L(" Multisampled: {}\n", img.multisampled);
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), img.name, img.slot);
}
for (const Sampler& smp: fs_src->refl.samplers) {
for (const Sampler& smp: fs_src->refl.bindings.samplers) {
L(" Sampler '{}':\n", smp.name);
L(" Type: {}\n", smp_type_to_sokol_type_str(smp.type));
L(" Bind slot: SLOT_{}{} = {}\n", mod_prefix(inp), smp.name, smp.slot);
}
for (const ImageSampler& img_smp: fs_src->refl.image_samplers) {
for (const ImageSampler& img_smp: fs_src->refl.bindings.image_samplers) {
L(" Image Sampler Pair '{}':\n", img_smp.name);
L(" Image: {}\n", img_smp.image_name);
L(" Sampler: {}\n", img_smp.sampler_name);
Expand All @@ -190,19 +190,19 @@ static void write_vertex_attrs(const Input& inp, const Spirvcross& spirvcross) {
}

static void write_image_bind_slots(const Input& inp, const Spirvcross& spirvcross) {
for (const Image& img: spirvcross.unique_images) {
for (const Image& img: spirvcross.bindings.images) {
L("SLOT_{}{} :: {}\n", mod_prefix(inp), img.name, img.slot);
}
}

static void write_sampler_bind_slots(const Input& inp, const Spirvcross& spirvcross) {
for (const Sampler& smp: spirvcross.unique_samplers) {
for (const Sampler& smp: spirvcross.bindings.samplers) {
L("SLOT_{}{} :: {}\n", mod_prefix(inp), smp.name, smp.slot);
}
}

static void write_uniform_blocks(const Input& inp, const Spirvcross& spirvcross, Slang::Enum slang) {
for (const UniformBlock& ub: spirvcross.unique_uniform_blocks) {
for (const UniformBlock& ub: spirvcross.bindings.uniform_blocks) {
L("SLOT_{}{} :: {}\n", mod_prefix(inp), ub.struct_name, ub.slot);
L("{} :: struct {{\n", to_ada_case(fmt::format("{}{}", mod_prefix(inp), ub.struct_name)));
int cur_offset = 0;
Expand Down Expand Up @@ -342,7 +342,7 @@ static void write_stage(const char* indent,
assert(src);
L("{}desc.{}.entry = \"{}\"\n", indent, stage_name, src->refl.entry_point);
for (int ub_index = 0; ub_index < UniformBlock::NUM; ub_index++) {
const UniformBlock* ub = src->refl.find_uniform_block_by_slot(ub_index);
const UniformBlock* ub = src->refl.bindings.find_uniform_block_by_slot(ub_index);
if (ub) {
L("{}desc.{}.uniform_blocks[{}].size = {}\n", indent, stage_name, ub_index, roundup(ub->size, 16));
L("{}desc.{}.uniform_blocks[{}].layout = .STD140\n", indent, stage_name, ub_index);
Expand All @@ -363,7 +363,7 @@ static void write_stage(const char* indent,
}
}
for (int img_index = 0; img_index < Image::NUM; img_index++) {
const Image* img = src->refl.find_image_by_slot(img_index);
const Image* img = src->refl.bindings.find_image_by_slot(img_index);
if (img) {
L("{}desc.{}.images[{}].used = true\n", indent, stage_name, img_index);
L("{}desc.{}.images[{}].multisampled = {}\n", indent, stage_name, img_index, img->multisampled ? "true" : "false");
Expand All @@ -372,18 +372,18 @@ static void write_stage(const char* indent,
}
}
for (int smp_index = 0; smp_index < Sampler::NUM; smp_index++) {
const Sampler* smp = src->refl.find_sampler_by_slot(smp_index);
const Sampler* smp = src->refl.bindings.find_sampler_by_slot(smp_index);
if (smp) {
L("{}desc.{}.samplers[{}].used = true\n", indent, stage_name, smp_index);
L("{}desc.{}.samplers[{}].sampler_type = {}\n", indent, stage_name, smp_index, smp_type_to_sokol_type_str(smp->type));
}
}
for (int img_smp_index = 0; img_smp_index < ImageSampler::NUM; img_smp_index++) {
const ImageSampler* img_smp = src->refl.find_image_sampler_by_slot(img_smp_index);
const ImageSampler* img_smp = src->refl.bindings.find_image_sampler_by_slot(img_smp_index);
if (img_smp) {
L("{}desc.{}.image_sampler_pairs[{}].used = true\n", indent, stage_name, img_smp_index);
L("{}desc.{}.image_sampler_pairs[{}].image_slot = {}\n", indent, stage_name, img_smp_index, src->refl.find_image_by_name(img_smp->image_name)->slot);
L("{}desc.{}.image_sampler_pairs[{}].sampler_slot = {}\n", indent, stage_name, img_smp_index, src->refl.find_sampler_by_name(img_smp->sampler_name)->slot);
L("{}desc.{}.image_sampler_pairs[{}].image_slot = {}\n", indent, stage_name, img_smp_index, src->refl.bindings.find_image_by_name(img_smp->image_name)->slot);
L("{}desc.{}.image_sampler_pairs[{}].sampler_slot = {}\n", indent, stage_name, img_smp_index, src->refl.bindings.find_sampler_by_name(img_smp->sampler_name)->slot);
if (Slang::is_glsl(slang)) {
L("{}desc.{}.image_sampler_pairs[{}].glsl_name = \"{}\"\n", indent, stage_name, img_smp_index, img_smp->name);
}
Expand Down Expand Up @@ -438,7 +438,7 @@ ErrMsg gen(const Args& args, const Input& inp, const std::array<Spirvcross,Slang
Slang::Enum slang = (Slang::Enum) i;
if (args.slang & Slang::bit(slang)) {
ErrMsg err = check_errors(inp, spirvcross[i], slang);
if (err.has_error) {
if (err.valid()) {
return err;
}
if (!comment_header_written) {
Expand Down
Loading

0 comments on commit 0ac391b

Please # to comment.