Skip to content

Commit

Permalink
Merge pull request #128 from floooh/dlang-generator
Browse files Browse the repository at this point in the history
New attempt at D language generator.
  • Loading branch information
floooh authored May 12, 2024
2 parents 42f6bbd + 6a980d7 commit 2a89f17
Show file tree
Hide file tree
Showing 7 changed files with 543 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

#### **12-May-2024**

A new code generator for the D language bindings via `-f sokol_d`. Note that the actual D bindings are
still WIP and haven't been released yet. Many thanks to @kassane for the initial D code generator!

#### **10-May-2024**

A minor regression in the YAML output has been fixed (wrong indentation after storage blocks), see
Expand Down
1 change: 1 addition & 0 deletions docs/sokol-shdc.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ sokol-shdc supports the following output formats:
- Rust for integration with [sokol-rust](https://github.com/floooh/sokol-rust)
- Odin for integration with [sokol-odin](https://github.com/floooh/sokol-odin)
- Nim for integration with [sokol-nim](https://github.com/floooh/sokol-nim)
- D for integration with [sokol-d](https://github.com/floooh/sokol-d)
- 'raw' output files in GLSL, MSL and HLSL along with reflection data in YAML files

Error messages from `glslang` are mapped back to the original annotated
Expand Down
3 changes: 2 additions & 1 deletion src/shdc/args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static const getopt_option_t option_list[] = {
{ "module", 'm', GETOPT_OPTION_TYPE_REQUIRED, 0, OPTION_MODULE, "optional @module name override" },
{ "reflection", 'r', GETOPT_OPTION_TYPE_NO_ARG, 0, OPTION_REFLECTION, "generate runtime reflection functions" },
{ "bytecode", 'b', GETOPT_OPTION_TYPE_NO_ARG, 0, OPTION_BYTECODE, "output bytecode (HLSL and Metal)"},
{ "format", 'f', GETOPT_OPTION_TYPE_REQUIRED, 0, OPTION_FORMAT, "output format (default: sokol)", "[sokol|sokol_impl|sokol_zig|sokol_nim|sokol_odin|sokol_rust|bare|bare_yaml]" },
{ "format", 'f', GETOPT_OPTION_TYPE_REQUIRED, 0, OPTION_FORMAT, "output format (default: sokol)", "[sokol|sokol_impl|sokol_zig|sokol_nim|sokol_odin|sokol_rust|sokol_d|bare|bare_yaml]" },
{ "errfmt", 'e', GETOPT_OPTION_TYPE_REQUIRED, 0, OPTION_ERRFMT, "error message format (default: gcc)", "[gcc|msvc]"},
{ "dump", 'd', GETOPT_OPTION_TYPE_NO_ARG, 0, OPTION_DUMP, "dump debugging information to stderr"},
{ "genver", 'g', GETOPT_OPTION_TYPE_REQUIRED, 0, OPTION_GENVER, "version-stamp for code-generation", "[int]"},
Expand Down Expand Up @@ -77,6 +77,7 @@ static void print_help_string(getopt_context_t& ctx) {
" - sokol_nim Nim module file\n"
" - sokol_odin Odin module file\n"
" - sokol_rust Rust module file\n"
" - sokol_d D module file\n"
" - bare raw output of SPIRV-Cross compiler, in text or binary format\n"
" - bare_yaml like bare, but with reflection file in YAML format\n\n"
"Options:\n\n");
Expand Down
3 changes: 3 additions & 0 deletions src/shdc/generators/generate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "sokolodin.h"
#include "sokolrust.h"
#include "sokolzig.h"
#include "sokold.h"
#include "yaml.h"
#include <memory>

Expand All @@ -28,6 +29,8 @@ std::unique_ptr<Generator> make_generator(Format::Enum format) {
return std::make_unique<SokolOdinGenerator>();
case Format::SOKOL_RUST:
return std::make_unique<SokolRustGenerator>();
case Format::SOKOL_D:
return std::make_unique<SokolDGenerator>();
default:
return std::make_unique<SokolCGenerator>();
}
Expand Down
484 changes: 484 additions & 0 deletions src/shdc/generators/sokold.cc

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions src/shdc/generators/sokold.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include "generator.h"

namespace shdc::gen {

class SokolDGenerator : public Generator {
protected:
virtual void gen_prolog(const GenInput& gen);
virtual void gen_epilog(const GenInput& gen);
virtual void gen_prerequisites(const GenInput& gen);
virtual void gen_uniform_block_decl(const GenInput& gen, const refl::UniformBlock& ub);
virtual void gen_storage_buffer_decl(const GenInput& gen, const refl::StorageBuffer& sbuf);
virtual void gen_shader_array_start(const GenInput& gen, const std::string& array_name, size_t num_bytes, Slang::Enum slang);
virtual void gen_shader_array_end(const GenInput& gen);
virtual void gen_shader_desc_func(const GenInput& gen, const refl::ProgramReflection& prog);
virtual std::string lang_name();
virtual std::string comment_block_start();
virtual std::string comment_block_line_prefix();
virtual std::string comment_block_end();
virtual std::string shader_bytecode_array_name(const std::string& snippet_name, Slang::Enum slang);
virtual std::string shader_source_array_name(const std::string& snippet_name, Slang::Enum slang);
virtual std::string get_shader_desc_help(const std::string& prog_name);
virtual std::string uniform_type(refl::Type::Enum e);
virtual std::string flattened_uniform_type(refl::Type::Enum e);
virtual std::string image_type(refl::ImageType::Enum e);
virtual std::string image_sample_type(refl::ImageSampleType::Enum e);
virtual std::string sampler_type(refl::SamplerType::Enum e);
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& snippet_name, const refl::StageAttr& attr);
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& snippet_name, const refl::StageAttr& attr);
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);
};

} // namespace
4 changes: 4 additions & 0 deletions src/shdc/types/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Format {
SOKOL_NIM,
SOKOL_ODIN,
SOKOL_RUST,
SOKOL_D,
BARE,
BARE_YAML,
NUM,
Expand All @@ -30,6 +31,7 @@ inline const char* Format::to_str(Enum f) {
case SOKOL_NIM: return "sokol_nim";
case SOKOL_ODIN: return "sokol_odin";
case SOKOL_RUST: return "sokol_rust";
case SOKOL_D: return "sokol_d";
case BARE: return "bare";
case BARE_YAML: return "bare_yaml";
default: return "<invalid>";
Expand All @@ -49,6 +51,8 @@ inline Format::Enum Format::from_str(const std::string& str) {
return SOKOL_ODIN;
} else if (str == "sokol_rust") {
return SOKOL_RUST;
} else if (str == "sokol_d") {
return SOKOL_D;
} else if (str == "bare") {
return BARE;
} else if (str == "bare_yaml") {
Expand Down

0 comments on commit 2a89f17

Please sign in to comment.