Skip to content

Instantly share code, notes, and snippets.

@ncthbrt
Last active September 2, 2024 18:33
Show Gist options
  • Save ncthbrt/9ba127e7b583650eb336e3a6b6ad5208 to your computer and use it in GitHub Desktop.
Save ncthbrt/9ba127e7b583650eb336e3a6b6ad5208 to your computer and use it in GitHub Desktop.

Module System

New keyword mod allows declaration of a sub-module. This allows discovery of modules within a crate as well as providing a single entry point for processing preprocessor directives. Modules follow rust rules such that if a crate is called x which has a sub-module a, the canonical name for the module will be x::a. x::a is added to x's symbol scope. Additional keywords self, super, and crate allow users to refer to modules relative to the current module.

Future work

  • Allowing declaration of sub module code inline to the parent module
  • Rust macro that creates a set of shader modules embedded in the code based off of rust/cargo file resolution logic. These would also include the encased shader types in the rust code if no preprocessor directives would influence the result.
  • Allow modules in the same crate to mutually import symbols from one another. This will require parsing wgsl to generate the headers.
  • Later: Extension to the language to allow for definition of module and struct "shapes" and module constructors that take in modules as arguments and return a new module based off of these arguments.
  • Generic functions

Visibility

Introduction of the pub keyword that can be applied to modules, functions and other root elements to control access to each element. Visibility is first tested on the module level and then the element level. Glsl modules expose everything as they do not have support for advanced naga oil syntax. Only public entrypoints declared in the main module are exposed by the system, with the rest being demoted to a function.

Future work

  • Addition of the other visibility modifiers present in Rust
  • Support type aliasing

Imports/Rexports

The import syntax has been changed to be use <elements>.

Rexports from a module can be done by specifying pub use. Reexports don't change the symbol name in the generated code and mainly serves to better control visibility. In addition to the new use syntax, wildcard imports have been added. These follow the rust syntax and bring all exported symbols into the current module scope.

Future work

  • Scoped imports to be closer to the rust syntax

Extensions

The extend keyword will soon be added to the mvp branch. This essentially will bring all the referenced module's symbols into the current scope but unlike use, copies/ renames the symbols to be relative to the current module. Extend acts recursively so will also apply to submodules. Extend can also apply to functions provided the base function has been declared virtual and the module the function comes from has been extended in the current module. The base function can still be called using a normal use statement. The extend keyword is the only way to expose an entrypoint from another module.

Patchsets

The patch and use patchset expressions have been added to the language and replace the current function overrides. The primary difference (other than the keyword change to avoid clashes with override constants), is that patches are hygienic in the sense that they only apply to their particular subset of the import tree and respect visibility rules. This allows them to be better language citizens.

@stefnotch
Copy link

stefnotch commented Sep 2, 2024

Reposting suggestion regarding side effects from Discord

Side effects:

  • Things that are specified when creating a WGSL pipeline
  • Directives: Behind cfg flags, root module can specify required directives
  • (Maybe const_assert?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment