Visitar URL original
Ship processing and lygia shaders in the wheel. · processing/libprocessing@b4d70a3 · GitHub
Skip to content

Commit b4d70a3

Browse files
committed
Ship processing and lygia shaders in the wheel.
1 parent 7348e92 commit b4d70a3

11 files changed

Lines changed: 272 additions & 7 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ jobs:
2020
run: |
2121
cargo build --release -p processing_pyo3
2222
cargo run --release -p generate_stubs
23+
- name: Bundle shader packages
24+
run: cargo run --release -p bundle_shaders
2325
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
2426
with:
25-
name: type-stubs
26-
path: crates/processing_pyo3/mewnala/*.pyi
27+
name: python-assets
28+
path: |
29+
crates/processing_pyo3/mewnala/*.pyi
30+
crates/processing_pyo3/mewnala/shaders/
2731
2832
linux:
2933
runs-on: ubuntu-22.04
@@ -41,7 +45,7 @@ jobs:
4145
python-version: '3.x'
4246
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
4347
with:
44-
name: type-stubs
48+
name: python-assets
4549
path: crates/processing_pyo3/mewnala
4650
- name: Setup QEMU
4751
if: matrix.target == 'aarch64'
@@ -90,7 +94,7 @@ jobs:
9094
python-version: '3.x'
9195
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
9296
with:
93-
name: type-stubs
97+
name: python-assets
9498
path: crates/processing_pyo3/mewnala
9599
- name: Build wheels
96100
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
@@ -115,7 +119,7 @@ jobs:
115119
python-version: '3.x'
116120
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
117121
with:
118-
name: type-stubs
122+
name: python-assets
119123
path: crates/processing_pyo3/mewnala
120124
- name: Build wheels
121125
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0
@@ -137,7 +141,7 @@ jobs:
137141
submodules: true
138142
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
139143
with:
140-
name: type-stubs
144+
name: python-assets
141145
path: crates/processing_pyo3/mewnala
142146
- name: Build sdist
143147
uses: PyO3/maturin-action@e83996d129638aa358a18fbd1dfb82f0b0fb5d3b # v1.51.0

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
/site/
33
crates/processing_ffi/include/
44
crates/processing_pyo3/mewnala/*.pyi
5+
crates/processing_pyo3/mewnala/shaders/
56
.DS_Store
67
.ipynb_checkpoints/

‎Cargo.lock‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/processing_pyo3/README.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ $ python
2222
>>> import processing
2323
>>> processing.size(500, 500)
2424
```
25+
26+
### Shader editor support
27+
28+
The wheel ships the `processing` and `lygia` WESL packages. To have
29+
[wgsl-analyzer](https://github.com/wgsl-analyzer/wgsl-analyzer) resolve
30+
`import processing::...` and `import lygia::...` in your sketch's shaders:
31+
32+
```
33+
$ python -m mewnala shaders init
34+
```
35+
36+
This writes (or updates) `wesl.toml` in the current directory, pointing at the
37+
installed packages. Pass `--vendor` to copy them into `.mewnala/shaders` instead.
38+
Rerun after upgrading mewnala. In a source checkout, `just py-shaders` generates
39+
the bundle.

‎crates/processing_pyo3/mewnala/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import importlib as _importlib
1919
math = _importlib.import_module(f"{__name__}.math") # noqa: E402
2020
from .math import * # noqa: E402,F401,F403
21+
from ._shaders import shader_workspace # noqa: E402,F401
2122

2223
# global var handling. for wildcard import of our module, we copy into globals, otherwise
2324
# we dispatch to get attr and call the underlying getter method
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import argparse
2+
3+
from ._shaders import shader_packages, shader_workspace
4+
5+
6+
def main(argv=None):
7+
parser = argparse.ArgumentParser(prog="python -m mewnala")
8+
commands = parser.add_subparsers(dest="command", required=True)
9+
10+
shaders = commands.add_parser("shaders", help="bundled WESL packages")
11+
shaders_commands = shaders.add_subparsers(dest="shaders_command", required=True)
12+
13+
init = shaders_commands.add_parser(
14+
"init", help="create or update wesl.toml so editors resolve processing:: and lygia::"
15+
)
16+
init.add_argument("dir", nargs="?", default=".")
17+
init.add_argument(
18+
"--vendor", action="store_true", help="copy the packages into DIR/.mewnala/shaders"
19+
)
20+
21+
shaders_commands.add_parser("path", help="print where the bundled packages are installed")
22+
23+
args = parser.parse_args(argv)
24+
if args.shaders_command == "init":
25+
print(f"wrote {shader_workspace(args.dir, vendor=args.vendor)}")
26+
else:
27+
for name, path in shader_packages().items():
28+
print(f"{name}\t{path}")
29+
30+
31+
if __name__ == "__main__":
32+
main()
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""WESL packages bundled with mewnala, and wiring them into a project's `wesl.toml`
2+
so editor tooling (wgsl-analyzer) resolves `import processing::...` and
3+
`import lygia::...` the same way `Shader` does at runtime."""
4+
5+
import json
6+
import shutil
7+
from pathlib import Path
8+
9+
PACKAGES = ("processing", "lygia")
10+
11+
_BEGIN = "# BEGIN mewnala"
12+
_END = "# END mewnala"
13+
_VENDOR_DIR = Path(".mewnala") / "shaders"
14+
15+
16+
def shader_packages():
17+
"""Return `{name: directory}` for the WESL packages shipped in the wheel."""
18+
root = Path(__file__).parent / "shaders"
19+
found = {name: root / name for name in PACKAGES if (root / name / "wesl.toml").is_file()}
20+
if not found:
21+
raise FileNotFoundError(
22+
f"no bundled shader packages under {root}; "
23+
"in a source checkout run `cargo run -p bundle_shaders`"
24+
)
25+
return found
26+
27+
28+
def _toml_path(path, base):
29+
try:
30+
p = path.relative_to(base)
31+
except ValueError:
32+
p = path
33+
return json.dumps(p.as_posix())
34+
35+
36+
def _dependency_block(packages, base):
37+
lines = [_BEGIN]
38+
for name, path in packages.items():
39+
lines.append(f"{name} = {{ path = {_toml_path(path, base)} }}")
40+
lines.append(_END)
41+
return lines
42+
43+
44+
def _splice(text, block):
45+
lines = text.splitlines()
46+
if _BEGIN in lines:
47+
start = lines.index(_BEGIN)
48+
try:
49+
end = lines.index(_END, start)
50+
except ValueError:
51+
raise ValueError(f"`{_BEGIN}` without a matching `{_END}`") from None
52+
lines[start : end + 1] = block
53+
else:
54+
header = next(
55+
(i for i, l in enumerate(lines) if l.split("#", 1)[0].strip() == "[dependencies]"),
56+
None,
57+
)
58+
if header is None:
59+
if lines and lines[-1].strip():
60+
lines.append("")
61+
lines += ["[dependencies]", *block]
62+
else:
63+
lines[header + 1 : header + 1] = block
64+
return "\n".join(lines) + "\n"
65+
66+
67+
def _validate(text, path):
68+
try:
69+
import tomllib
70+
except ImportError:
71+
return
72+
try:
73+
doc = tomllib.loads(text)
74+
except tomllib.TOMLDecodeError as e:
75+
raise ValueError(f"updating {path} would produce invalid TOML ({e}); edit it by hand") from None
76+
if not isinstance(doc.get("dependencies"), dict):
77+
raise ValueError(f"{path}: `dependencies` is not a table")
78+
79+
80+
def shader_workspace(path=".", vendor=False):
81+
"""Create or update `wesl.toml` in `path` so the bundled `processing` and
82+
`lygia` packages resolve in the editor.
83+
84+
Dependencies point into the installed package. With `vendor=True` they are
85+
copied to `.mewnala/shaders` first, for editors that can't see the
86+
environment's site-packages or for checking them in. Rerun after upgrading
87+
mewnala either way. Returns the path of the `wesl.toml`.
88+
"""
89+
base = Path(path).resolve()
90+
base.mkdir(parents=True, exist_ok=True)
91+
packages = shader_packages()
92+
93+
if vendor:
94+
dst = base / _VENDOR_DIR
95+
vendored = {}
96+
for name, src in packages.items():
97+
target = dst / name
98+
if target.exists():
99+
shutil.rmtree(target)
100+
shutil.copytree(src, target)
101+
vendored[name] = target
102+
packages = vendored
103+
104+
manifest = base / "wesl.toml"
105+
block = _dependency_block(packages, base)
106+
if manifest.exists():
107+
text = _splice(manifest.read_text(encoding="utf-8"), block)
108+
else:
109+
header = ['edition = "2026_pre"', 'root = "."']
110+
if vendor:
111+
header.append(f'exclude = ["{_VENDOR_DIR.as_posix()}/**"]')
112+
text = "\n".join([*header, "", "[dependencies]", *block]) + "\n"
113+
_validate(text, manifest)
114+
manifest.write_text(text, encoding="utf-8")
115+
return manifest

‎crates/processing_pyo3/pyproject.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ include = [
4848
{ path = "mewnala/*.pyi", format = "wheel" },
4949
{ path = "mewnala/py.typed", format = "sdist" },
5050
{ path = "mewnala/py.typed", format = "wheel" },
51+
{ path = "mewnala/shaders/**/*", format = "sdist" },
52+
{ path = "mewnala/shaders/**/*", format = "wheel" },
5153
]
5254

5355
[tool.uv]

‎justfile‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ export PROCESSING_ASSET_ROOT := canonicalize("./assets")
55
default:
66
@just --list
77

8-
py-build *args:
8+
py-build *args: py-shaders
99
cd crates/processing_pyo3; uv run maturin develop --release {{args}}
1010

11+
py-shaders:
12+
cargo run -p bundle_shaders
13+
1114
py-stubs: py-build
1215
cargo run --release -p generate_stubs
1316

‎tools/bundle_shaders/Cargo.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "bundle_shaders"
3+
version = "0.0.8"
4+
edition = "2024"
5+
publish = false

0 commit comments

Comments
 (0)