Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add module organization suggestion doc #6509

Merged
merged 9 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added docs/assets/moduletree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 33 additions & 4 deletions docs/user-guide/04-modules-and-access-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ It is only valid for the user code to `import m`. Attempting to `import helper`
Multiple `import`s of the same module from different input files will only cause the module to be loaded once (there is no need for "include guards" or `#pragma once`).
Note that preprocessor definitions in the current file will not affect the compilation of `import`ed code, and the preprocessor definitions in the imported code is not visible to the current file.

> #### Note ####
> Future versions of the Slang system will support loading of modules from pre-compiled binaries instead of source code.
> The same `import` keyword will continue to work in that case.

## Access Control

Slang supports access control modifiers: `public`, `internal` and `private`. The module boundary plays an important role in access control.
Expand Down Expand Up @@ -195,6 +191,39 @@ The Slang compiler enforces the following rules regarding access control:
- Type definitions themselves cannot be `private`, for example, `private struct S {}` is not valid code.
- `interface` requirements cannot be `private`.

## Organizing File Structure of Modules

Slang does not seek to impose any specific organization of modules. However, there are some conventions that have emerged as being useful.

### Module Organization Suggestions

- The top-level directory contains modules that would be `import`ed by user code.
- The implementation details of the modules are placed in files at lower levels of the tree.

This has the benefit that it is easy for a user to distinguish the public API from the implementation details.

### Module Organization Example

<img src="../assets/moduletree.png" width="300em" alt="Module organization tree diagram"/>

### Module Organization Example

The above diagram shows a module organization example.

Top-level module files such as `utils.slang` are those that are directly `import`ed by user code. The implementation details of the module are placed in the lower levels of the tree, organized into similarly named subdirectories for clarity.

Modules like `utils.slang` needn't contain anything more than a module declaration and a list of included files, with optional `import` statement(s) to pull in any external dependencies, e.g.

```
module utils;
import slangpy;

__include "utils/accumlator.slang";
__include "utils/tonemap.slang";
__include "utils/fill.slang";
```

Here, all the public symbols defined in `accumlator.slang`, `tonemap.slang`, and `fill.slang` are visible to the user of the `utils` module, and these constituent helper files do not need to clutter the top-level file hierarchy.

## Legacy Modules

Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<li data-link="modules#defining-a-module"><span>Defining a Module</span></li>
<li data-link="modules#importing-a-module"><span>Importing a Module</span></li>
<li data-link="modules#access-control"><span>Access Control</span></li>
<li data-link="modules#organizing-file-structure-of-modules"><span>Organizing File Structure of Modules</span></li>
<li data-link="modules#legacy-modules"><span>Legacy Modules</span></li>
</ul>
</li>
Expand Down
Loading