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 3 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 Systems 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

- Top-level modules are those that are `import`ed by user code.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terminology isn't very accurate: the util.slang is the "main module file", and files being __included are just files in that module. They are not "submodules".

Think about a module as a Visual Studio C++/C# Project, and a file is just a file in that project.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please let me know if I missed any inaccuracies.

- The implementation details of the module are placed in the 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 modules 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.

In this example, the `utils.slang` module needn't contain anything more than a module declaration and a list of included sub-modules, 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 symbols defined in `accumlator.slang`, `tonemap.slang`, and `fill.slang` are visible to the user of the `utils` module, and these helper modules 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-systems-of-modules"><span>Organizing Systems of Modules</span></li>
<li data-link="modules#legacy-modules"><span>Legacy Modules</span></li>
</ul>
</li>
Expand Down
Loading