Slang Language and Compiler

repository·master·Indexed 26 days ago

https://github.com/shader-slang/slang

A shader language and compiler featuring a reflection API and support for multiple targets including CPU, CUDA, DXIL, WGSL, and WebGPU. The project includes the slangc command-line tool, slang-coverage for HTML coverage reports, and a comprehensive build system for managing dependencies like glslang, spirv-tools, and DXC.

Tokens
195.7K
Snippets
365
Records
915
Agent score
90%

What's inside Slang

  1. Overview of Generics in Slang

    master

    Generics in Slang allow you to parameterize various language constructs, enabling code reuse and abstraction. You can use generics with:

    • Structures (struct)
    • Interfaces (interface)
    • Type Aliases (typealias)
    • Functions and Member Functions (func or traditional syntax)
    • Subscript Operators (__subscript)
    • Constructors (__init)
    • Generic Extensions

    Supported Parameter Types

    Generic parameters can be:

    • A Type
    • A Boolean value
    • An Integer value
    • An Enumeration (support exists but current utility is limited)

    Specialization and Binding

    When generic parameters are bound, the generic type or function is specialized into a concrete entity. Binding can occur via:

    • Explicit binding: Providing arguments directly.
    • Inference (Implicit binding): The compiler determines the arguments.
    • Combination: Both explicit and implicit binding.

    Note: Value-typed arguments used for binding must be link-time constants.

  2. Overview of Auto-Diff IR Pass Workflow

    master

    Slang's auto-diff processing follows a multi-step workflow managed by AutoDiffPass::processReferencedFunctions. The process is iterative to support nested differentiation (e.g., IRForwardDifferentiate(IRBackwardDifferentiate(...))):

    1. Scanning: The compiler scans reachable instructions for IRForwardDifferentiate or IRBackwardDifferentiate. The subject can be an IRFunc, an IRSpecialize (for generic methods), or an IRLookupWitness (for interface methods).
    2. Dispatching: Requests are sent to a 'transcriber' (implementing AutodiffTranscriberBase). The transcriber replaces the request with a generated derivative function or a call to an existing derivative function.
    3. Follow-up Work: Transcribers can add new tasks to a global work-list. For example, differentiating a function that calls another function will generate a follow-up task for the inner function.
    4. Iteration: The loop repeats until all transcription requests and follow-up tasks are resolved.
  3. Overview of slang-coverage-html and slang-coverage-merge

    master

    The slang-coverage tools are two Python 3 utilities designed to generate static-HTML coverage reports from standard coverage data formats. They are project-neutral and do not require external dependencies like pip, Perl (genhtml), or .NET.

    • slang-coverage-html: Renders a single coverage input file into a directory of static HTML. It supports LCOV (.info, .lcov, or gzipped versions) and llvm-cov JSON exports (.json or gzipped versions). The format is automatically determined by the file extension.
    • slang-coverage-merge: Combines multiple LCOV inputs (e.g., from different CI hosts) into a single merged LCOV file using max-aggregation. Note that this tool only supports LCOV; for JSON inputs, you should render each file individually to HTML instead of merging.
  4. Overview of Slang language features

    master

    Slang is a shading language and compiler system designed for real-time graphics. It extends HLSL with modern programming features while maintaining high GPU performance. Key features include:

    • HLSL Compatibility: Backward compatible with most existing HLSL code.
    • Parameter Blocks: Groups shader parameters by update rate to optimize Direct3D 12 descriptor tables and Vulkan descriptor sets.
    • Interfaces and Generics: Provides first-class alternatives to preprocessor-based specialization (similar to Rust, Swift, or C#).
    • Automatic Differentiation: Supports generating both forward and backward derivative propagation functions for learning-based shader techniques.
    • Module System: Enables separate compilation and semantic checking.
    • Multi-Target Compilation: A single compiler can generate DX bytecode, DXIL, SPIR-V, HLSL, GLSL, CUDA, and more.
    • Reflection API: A robust API providing consistent binding, offset, and layout information for shader parameters across all targets.
    • Shader Types: Supports compute, rasterization, and ray-tracing shaders.
  5. Overview of slang-coverage tools

    master

    The slang-coverage toolset provides utilities for rendering code coverage data into HTML reports and merging multiple coverage files. It consists of two primary CLI tools:

    • slang-coverage-html.py: A renderer that converts coverage data into an interactive HTML report.
    • slang-coverage-merge.py: A utility to merge multiple LCOV files (e.g., from different operating systems) into a single report.

    The tools support both LCOV input (typically used for shader coverage on Windows) and llvm-cov export -format=json input (typically used for C++ coverage on Linux/macOS).

  6. Understand Slang Program Execution

    master

    Slang program execution follows a hierarchy of workloads, entry point invocations, and threads.

    • Graphics Launches: Determined by draw calls and pipeline configuration. A fragment shader, for example, is invoked once per rasterized fragment. Inputs come from the rasterizer and vertex shader, and outputs are per-fragment values.
    • Compute Dispatches: Explicitly defined by the user as a 3D grid of thread groups. A compute kernel is invoked once per user-defined input parameter point (thread coordinates) and typically stores results in output buffers rather than returning a value.
  7. Understand Slang Capabilities

    master

    Slang uses a capability system to handle cross-platform shader programming. A capability is a discrete feature that a compilation target either supports or does not support (e.g., fragment stage, vulkan API, or specific hardware features like implicit_gradient_texture_fetches).

    Capabilities solve two primary problems:

    1. Validation: Ensuring users don't use constructs (like fragment-only functions) on unsupported targets.
    2. Platform-Specific Implementation: Allowing developers to write a single codebase that selects the best implementation path for different platforms without using pervasive preprocessor #ifdef blocks.
  8. Understand Slang Memory Consistency and Data Races

    master

    In multi-threaded Slang programs, a data race occurs when two threads access the same memory location, at least one access is a write, and they use non-atomic accesses without an established happens-before relationship.

    To avoid data races, ensure that:

    • Memory accesses are executed by the same thread,
    • Memory accesses are atomic,
    • Or one memory access happens before the other (established via atomic load-acquire/store-release, memory barriers, or Slang Standard Library constructs).

    Warning: slangc currently emits incorrect code for Atomic<T> for multiple targets (see GitHub issue #10683).

  9. Understand the structure of the external/ directory

    master

    The external/ directory contains all third-party dependencies required by Slang. It is organized into four types of content:

    1. Git submodules: Managed via git submodule update --init --recursive. Examples include glslang, spirv-tools, vulkan, glm, and imgui.
    2. Vendored headers: Small header sets checked directly into the repository (e.g., dxc/, stb/, spirv/).
    3. Pre-generated and committed files: Files like glslang-generated/ and spirv-tools-generated/ that are generated out-of-band by maintainer scripts and committed to avoid regeneration during normal builds.
    4. Fetched prebuilt binaries: Dependencies obtained by CMake at configure time, such as slang-tint, webgpu_dawn, slang-llvm, and DXC. Some have source-build fallbacks.
  10. Understand Shader Coverage Binding and Attribution

    master

    Shader coverage in Slang is implemented through two distinct mechanisms to handle runtime instrumentation without polluting the public language surface:

    1. Binding (Where the buffer is): Slang synthesizes a __slang_coverage buffer (either RWStructuredBuffer<uint64_t> or RWStructuredBuffer<uint> depending on the -trace-coverage-counter-width flag) during the IR-pass. This buffer is hidden from Slang's public reflection (e.g., IComponentType::getLayout()). To find and bind this buffer, hosts must consult ISyntheticResourceMetadata to determine the correct slot for their pipeline-layout, root-signature, or descriptor-set machinery.

    2. Attribution (What the counters mean): To map runtime counter values back to source code locations (files, lines, functions), Slang uses ICoverageTracingMetadata and its on-disk sidecar format. This provides the semantic intent that standard reflection lacks.

    A host requires both mechanisms: ISyntheticResourceMetadata to allocate and bind the buffer, and ICoverageTracingMetadata to interpret the data read back from the GPU.