crosstl README

项目的 README,来自 github.com/CrossGL/crosstl。合并为一个 markdown 文件:README.md。

CrossTL

Tests PyPI License DOI

CrossTL translates GPU shaders and compute kernels between languages. Every translation goes through CrossGL, a small intermediate language: a frontend imports GLSL, HLSL, Metal or another source language into CrossGL, and a code generator writes CrossGL out as GLSL, HLSL, Metal, SPIR-V, WGSL or another target. You can also write CrossGL directly and generate every target from one source file.

CrossTL coverage per language and direction

CI rebuilds this chart on every push to main and every night. Each language has a feature list generated from pinned upstream specifications and compiler tables. Every feature gets a probe: CrossTL translates it, and the language's reference compiler or validator checks the result. Where CI has a runtime for the language, on a GPU, on Mesa's software drivers or on Direct3D 12 WARP, the translated compute probes also run and must compute what the native probes compute. CI has no runtime for CUDA, HIP, OpenCL C, Mojo, GLSL ES or WebGL, so the chart marks those rows compile-only. The score is supported / (supported + refused + wrong). COVERAGE.md defines each outcome and lists every gap behind each bar.

Install

pip install crosstl

CrossTL needs Python 3.11 or later. Validating the generated code needs the target's own compiler or validator (glslang, DXC, spirv-tools, naga, the Metal compiler, nvcc, hipcc, slangc, rust-gpu or the Mojo compiler), which CrossTL does not install. crosstl doctor lists the tools it looks for and which of them are on PATH.

Quickstart

Save this compute kernel as saxpy.cgl:

shader Saxpy {
    compute {
        layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
        buffer float* x;
        buffer float* y;
        uniform float a;

        void main() {
            uint i = gl_GlobalInvocationID.x;
            y[i] = a * x[i] + y[i];
        }
    }
}

Then translate it from Python, where the source language comes from the file extension:

import crosstl

metal = crosstl.translate("saxpy.cgl", backend="metal")
crosstl.translate("saxpy.cgl", backend="wgsl", save_shader="saxpy.wgsl")

# A native shader is imported into CrossGL first, then generated for the target.
glsl = crosstl.translate("blur.hlsl", backend="opengl")
crossgl = crosstl.translate("blur.hlsl", backend="cgl")

or from the command line, where the target comes from the output file's extension:

crosstl saxpy.cgl -o saxpy.hlsl
crosstl blur.hlsl -o blur.metal
crosstl translate saxpy.cgl --backend vulkan --output -

--backend chooses the target explicitly; without it, -o - writes CrossGL to standard output. An output file name whose extension names no language CrossTL writes is refused. python -m crosstl is the same command. The examples/ directory has larger CrossGL programs.

Languages

Language backend Output Input
GLSL opengl measured measured
HLSL directx measured measured
Metal metal measured measured
SPIR-V assembly vulkan measured measured
WGSL wgsl measured measured
GLSL ES 3.00 (WebGL 2) webgl measured measured
CUDA cuda measured measured
HIP hip measured measured
Slang slang measured measured
Rust (rust-gpu) rust measured measured
Mojo mojo measured measured
OpenCL C not offered measured

A measured direction has a row in COVERAGE.md, and each language has a page of its own with its scores by category, the language pairs it translates to and from, and every gap. CrossTL has no OpenCL C generator, so the OpenCL C input row checks the HLSL that CrossTL writes from each OpenCL C probe. The language names glsl, hlsl, msl and spirv work as backend names too, and cgl writes the CrossGL a native source imports to. backend="gles" writes GLSL ES 3.x for OpenGL ES, which has rows of its own in both directions. CrossGL has its own coverage row, CrossGL IR, which measures how many of the features of all the measured output languages CrossGL can express.

CrossTL is meant to fail closed: a construct it cannot translate raises a diagnostic that names the construct and the target, instead of emitting a placeholder. Output that the reference tool rejects, or that has a comment saying CrossTL wrote a placeholder, counts as wrong in the coverage report, and the Coverage workflow files it as a bug.

How it works

 .hlsl .metal .glsl .cu ...                                        .glsl .hlsl .metal .spvasm .wgsl ...
 native source --> frontend --> CrossGL source --> CrossGL parser --> code generator --> target source
                   (parse and                      (AST and types)     (one per target)
                    import)

Each source language has a frontend in crosstl/backend/<Language>/ that parses it and writes CrossGL. The CrossGL parser in crosstl/translator/ builds the typed AST that every code generator in crosstl/translator/codegen/ reads, so a feature added to CrossGL is available to every target that can express it.

Porting a repository

For a project with many shader or kernel sources, scan it before writing anything, then translate it and check the report:

python -m crosstl scan path/to/repo --target metal --output scan-report.json
python -m crosstl translate-project path/to/repo --target metal --output-dir crosstl-out \
  --report crosstl-out/report.json
python -m crosstl validate-project crosstl-out/report.json --format text
python -m crosstl inspect-report crosstl-out/report.json --format text

A relative --output-dir is resolved against the project, so the translated files land in path/to/repo/crosstl-out/<target>/. The report records each translated file, its diagnostics and provenance, the reference toolchain checks that ran, and the host-side changes that remain manual. The project porting guide covers the options, and demos/ has pinned open-source ports with checked artifacts.

Documentation

The user documentation is at docs.crossgl.net. The Sphinx sources, including the architecture notes and decision records, are in docs/source.

Contributing

Read CONTRIBUTING.md for setup, the test tiers and the pull request flow. Issues labelled good first issue are a good place to start, and questions belong in Discussions or on Discord. Report security problems as described in SECURITY.md.

Citation

If you use CrossTL in your work, cite it with the metadata in CITATION.cff; GitHub's "Cite this repository" button produces BibTeX and APA from it. Each release is archived on Zenodo under 10.5281/zenodo.15826974.

License

CrossTL is licensed under the Apache License 2.0.

crosstl (CrossGL/crosstl)

正在搜索数据库…

这是什么?CrossTL 是一个着色器和计算程序转换器,围绕 CrossGL 构建——CrossGL 是一种中间表示(IR)语言,用于衔接各种图形 API、GPU 计算平台和系统级语言目标。
这是 Python 库,还是独立的编译器工具?CrossTL 是一个 Python 库,导入后可以解析原生着色器或 CrossGL(.cgl)源码,并为 Metal、HLSL、GLSL、WGSL、SPIR-V、CUDA、HIP、Rust、Mojo 和 Slang 等目标生成代码。
怎么用 CrossTL 转换着色器?安装 CrossTL,编写或导入一个 CrossGL(.cgl)着色器文件,然后调用转换器为选定的后端目标生成代码。
用 CrossTL 除 Python 外还需要什么?CrossTL 是一个 Python 包,不需要任何 API 密钥,不过最终的 GPU 部署(例如 DXBC/DXIL 打包)仍需要厂商的原生工具链。
每个后端都支持完整的 CrossGL 语言吗?不支持——在 DirectX、Metal 和 OpenGL 之外,泛型函数以及 geometry、tessellation 和 mesh/task 阶段等高级特性只有部分覆盖,不受支持的组合会给出明确的报错。
我能把现有的 WebGL 或 WebGPU 着色器导入 CrossGL 吗?暂时不能——WebGL 和 WGSL 目前只能作为目标输出,.webgl.glsl、.wgsl 和 .wesl 源文件会被拒绝,直到加入专门的源码前端为止。
哪些后端有完整的双向(导入和导出)支持?DirectX/HLSL、Metal 和 OpenGL/GLSL 是第一梯队的双向后端——每一个都能把原生着色器源码解析成 CrossGL,也能反向生成代码,并通过往返测试验证(321 项通过)。
CrossGL 是什么?CrossGL 是 CrossTL 核心的中间表示(IR)语言:着色器要么从原生源码解析成它,要么直接写成 CrossGL(.cgl)文件,然后再编译输出到任何受支持的后端。
项目审计工作流是做什么的?对于有大量着色器或 GPU 源文件的代码库,CrossTL 可以在写出任何转换文件之前,先跑一份仅扫描的报告,涵盖转换诊断、产物来源和人工迁移备注。
转换流水线要经过哪些阶段?它依次执行词法分析(token 化)、语法分析(生成 AST)、语义分析(类型/作用域检查)、生成 CrossGL 中间表示,最后进行特定后端的代码生成。
有哪些类似的项目?Vulkan, bgfx, filament, wgpu, iw4L, MoltenVK, renderdoc, taichi
作者
CrossGL
大小
133.1 MB
星标
2.4K
时长
2年
提交 · 90 天