Giter Site home page Giter Site logo

wgsl_to_wgpu's Introduction

wgsl_to_wgpu

Latest Version docs.rs
An experimental library for generating typesafe Rust bindings from WGSL shaders to wgpu.

wgsl_to_wgpu is designed to be incorporated into the compilation process using a build script. The WGSL shaders are parsed using naga to generate a corresponding Rust module. The generated Rust module contains the type definitions and boilerplate code needed to work with the WGSL shader module. Using the generated code can also reduce many instances of invalid API usage. wgsl_to_wgpu facilitates a shader focused workflow where edits to WGSL code are automatically reflected in the corresponding Rust file. For example, changing the type of a uniform in WGSL will raise a compile error in Rust code using the generated struct to initialize the buffer.

Features

  • more strongly typed bind group and bindings initialization
  • shader module initialization
  • Rust structs for vertex, storage, and uniform buffers
  • optional derives for encase, bytemuck, and serde
  • const validation of WGSL memory layout for generated structs when using bytemuck

Usage

The generated code currently relies on memoffset for calculating field offsets for vertex input structs. Add the following lines to the Cargo.toml and fill in the appropriate versions for memoffset and wgsl_to_wgpu. When enabling derives for crates like bytemuck, serde, or encase, these dependencies should also be added to the Cargo.toml with the appropriate derive features. See the provided example project for basic usage.

[dependencies]
memoffset = "..."

[build-dependencies]
wgsl_to_wgpu = "..."

See the example crate for how to use the generated code. Run the example with cargo run.

Limitations

  • It may be necessary to disable running this function for shaders with unsupported types or features. Please make an issue if any new or existing WGSL syntax is unsupported.
  • This library is not a rendering library and will not generate any high level abstractions like a material or scene graph. The goal is just to generate most of the tedious and error prone boilerplate required to use WGSL shaders with wgpu.
  • The generated code will not prevent accidentally calling a function from an unrelated generated module. It's recommended to name the shader module with the same name as the shader and use unique shader names to avoid issues. Using generated code from a different shader module may be desirable in some cases such as using the same camera struct definition in multiple WGSL shaders.
  • The current implementation assumes all shader stages are part of a single WGSL source file. Shader modules split across files may be supported in a future release.
  • Uniform and storage buffers can be initialized using the wrong generated Rust struct. WGPU will still validate the size of the buffer binding at runtime.
  • Most but not all WGSL types are currently supported.
  • Vertex attributes using floating point types in WGSL like vec2<f32> are assumed to use float inputs instead of normalized attributes like unorm or snorm integers.
  • All textures are assumed to be filterable and all samplers are assumed to be filtering. This may lead to compatibility issues. This can usually be resolved by requesting the native only feature TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES.
  • It's possible to achieve slightly better performance than the generated code in some cases like avoiding redundant bind group bindings or adjusting resource shader stage visibility. This should be addressed by using some handwritten code where appropriate.

wgsl_to_wgpu's People

Contributors

andrewbrownk avatar badicsalex avatar bconnorwhite avatar daxpedda avatar scanmountgoat avatar slackers123 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.