Code Monkey home page Code Monkey logo

glsl-layout's Introduction

GLSL-LAYOUT

This crates provides data types and traits to build structures ready to upload into UBO.

Usage

Deriving Uniform with proc-macro will produce associated type Std140 with same data layout as uniform blocks declared with layout(std140). All members of structure that derives Uniform must implement Uniform as well.

Implementing Uniform should be done via deriving. Implementing it manually is possible. It requires to provide associated type Std140 which must implement Std140 trait. But trait Std140 is marked unsafe so be careful.

Trait Uniform also requires Copy.

Typical usage scenario is:

#[derive(Copy, Clone, Uniform)]
struct FragmentArgs {
    pos: vec3,
    dir: vec3,
    count: uint,
}

write_to_buffer(&FragmentArgs {
    pos: [0.0, 1.0, 2.0].into(),
    dir: [3.0, 4.0, 5.0].into(),
    count: 42,
}.std140());

Data types

There are basic data types from glsl:

  • boolean (name bool is already occupied)
  • int
  • uint
  • float
  • double

Also more complex types:

  • vectors - (vec2, vec3, vec4, bvec2, ivec2, uvec2, dvec2 etc)
  • matrices - (mat2x3, dmat4 etc)
  • arrays -

License

glsl-layout is free and open source software distributed under the terms of both the MIT License and the Apache License 2.0.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Known issues

On MacOS

If uniform block contains array of structures and this array accessed with dynamic index (variable instead of literal) it may load wrong bytes for members not aligned to size of vec4 (16 bytes). In this case manual padding can be the fix.

glsl-layout's People

Contributors

dasetwas avatar ishitatsuyuki avatar joe1994 avatar nazariglez avatar zakarumych avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

glsl-layout's Issues

Mint support

In order to support all the math libraries, it would be useful if glsl-layout would support conversions from any type that can be converted into the corresponding mint type.

I put together something similar for the (flawed) shader-types crate, but with a derive macro I think the ergonomics can be even better.

bigger-arrays feature doesn't work

Cargo.toml

glsl-layout = { version = "0.3.2", features = ["bigger-arrays"] }

main.rs

use glsl_layout::AsStd140;

#[derive(Copy, Clone, AsStd140)]
struct MyStd140 {
    array: [f32; 256]
}

Gives the errors:

error[E0277]: the trait bound `[_GLSL_LAYOUT_MyStd140::_glsl_layout::Element<f32>; 256]: std::default::Default` is not satisfied
 --> src/main.rs:3:23
  |
3 | #[derive(Copy, Clone, AsStd140)]
  |                       ^^^^^^^^ the trait `std::default::Default` is not implemented for `[_GLSL_LAYOUT_MyStd140::_glsl_layout::Element<f32>; 256]`
  |
  = help: the following implementations were found:
            <&[T] as std::default::Default>
            <&mut [T] as std::default::Default>
            <[T; 0] as std::default::Default>
            <[T; 10] as std::default::Default>
          and 31 others
  = note: required by `std::default::Default::default`

error[E0277]: arrays only have std trait implementations for lengths 0..=32
 --> src/main.rs:3:23
  |
3 | #[derive(Copy, Clone, AsStd140)]
  |                       ^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[_GLSL_LAYOUT_MyStd140::_glsl_layout::Element<f32>; 256]`
  |
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `[_GLSL_LAYOUT_MyStd140::_glsl_layout::Element<f32>; 256]`
  = note: required because of the requirements on the impl of `std::fmt::Debug` for `&[_GLSL_LAYOUT_MyStd140::_glsl_layout::Element<f32>; 256]`
  = note: required for the cast to the object type `dyn std::fmt::Debug`

Wrapper to align values for dynamic uniforms

Some graphics APIs like WebGPU require dynamic uniform offsets to be a multiple of 256. shader-types has a type named DymamicOffsetMember that helps with this, but it creates trailing padding bytes which interacts poorly with crates like bytemuck.

Would it make sense for glsl-layout to have a type like this? If it can calculate and add manual trailing padding to the AsStd140 version of the type, that would help avoid unspecified padding bytes.

impl From for nalgebra types

It would be useful to be able to convert nalgebra types to glsl-layout types with an optional feature, just as is currently done with cgmath.

I'd be willing to give this a shot if a PR is welcome.

`as_bytes` is unsound

It is unsafe to view an arbitrary type as bytes because it might contain uninitialized padding bytes.

zerocopy solves this with a derive macro but it is a bit limited (it doesn't work with generics, and implementing it manually is discouraged)

Unfortunately many types in this crate have padding, so there isn't really a great alternative ๐Ÿ˜•

Question: How to use glam types (or another lib) directly?

Hey hi, I would like to use glsl-layout to expose the Uniform macro from notan and I was thinking that because notan uses glam, to do that I need to convert with a macro from a struct with glam types, to a struct with glsl-layout types that is at the same converted to internal glsl-types to do the bytes conversion.

I would like to avoid this triple conversion and just do something like struct with glam types -> struct with inner glsl-layout types, do you have some hint to do that?

I am thinking of re-export the Uniform trait from notan without requiring the user to install directly glsl-layout, though I am not sure if that will work due to how derive macros imports types.

Anyway, I this can be summarized in that I would like to reuse and contribute to glsl-layout as much as possible without forking it to adapt to notan. It would be great if you have any hint that can guide/lead me to this.

Thanks!

std430 Layout Support

std140 is probably the most common layout for GLSL types, std430 is also relevant.

Would it be in scope for this library to also support Std430 and AsStd430 traits?

Glam test failing with weird bytes

Hello @zakarumych , I am preparing the update to glam 0.24 and saw that the test for glam fails in a weird way, I tested with 0.22 0.23, and 024, and all of them failed.

    let m3: mat3 = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]].into();
    let gm3_to_m3: mat3 = Mat3::IDENTITY.into();
    let gm3 = Mat3::IDENTITY;
    assert_eq!(m3.std140().as_raw(), gm3.std140().as_raw());
    assert_eq!(gm3.std140().as_raw(), gm3_to_m3.std140().as_raw());

Fails in the last assert with:

thread 'glam::test_glam' panicked at 'assertion failed: `(left == right)`
  left: `[0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0]`,
 right: `[0, 0, 128, 63, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 1, 0, 0, 0]`', src/glam.rs:83:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

There is some weird 1 where it should be a 0. Does this ring a bell for you? Any hint of what could be wrong? Thanks!

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.