Skip to content

[Merged by Bors] - Prepare crevice for vendored release #3394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ ron = "0.7.0"
serde = { version = "1", features = ["derive"] }
# Needed to poll Task examples
futures-lite = "1.11.3"
crevice = { path = "crates/crevice", version = "0.8.0", features = ["glam"] }

[[example]]
name = "hello_world"
Expand Down
10 changes: 5 additions & 5 deletions crates/crevice/Cargo.toml → crates/bevy_crevice/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "crevice"
description = "Create GLSL-compatible versions of structs with explicitly-initialized padding"
version = "0.8.0"
name = "bevy_crevice"
description = "Create GLSL-compatible versions of structs with explicitly-initialized padding (Bevy version)"
version = "0.5.0"
edition = "2021"
authors = ["Lucien Greathouse <[email protected]>"]
documentation = "https://docs.rs/crevice"
homepage = "https://github.com/LPGhatguy/crevice"
repository = "https://github.com/LPGhatguy/crevice"
repository = "https://github.com/bevyengine/bevy"
readme = "README.md"
keywords = ["glsl", "std140", "std430"]
license = "MIT OR Apache-2.0"
Expand All @@ -23,7 +23,7 @@ std = []
# default-members = ["crevice-derive", "crevice-tests"]

[dependencies]
crevice-derive = { version = "0.8.0", path = "crevice-derive" }
bevy-crevice-derive = { version = "0.5.0", path = "bevy-crevice-derive" }

bytemuck = "1.4.1"
mint = "0.5.8"
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 18 additions & 7 deletions crates/crevice/README.md → crates/bevy_crevice/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Crevice
# Bevy Crevice

[![GitHub CI Status](https://github.com/LPGhatguy/crevice/workflows/CI/badge.svg)](https://github.com/LPGhatguy/crevice/actions)
[![crevice on crates.io](https://img.shields.io/crates/v/crevice.svg)](https://crates.io/crates/crevice)
[![crevice docs](https://img.shields.io/badge/docs-docs.rs-orange.svg)](https://docs.rs/crevice)
This is a fork of [Crevice](https://crates.io/crates/crevice) for
[Bevy](https://bevyengine.org).

For use outside of Bevy, you should consider
using [Crevice](https://crates.io/crates/crevice) directly.

It was forked to allow better integration in Bevy:

* Easier derive macro usage, without needing to depend on `Crevice` directly.
* Use of unmerged features (as of the fork), like
[Array Support](https://github.com/LPGhatguy/crevice/pull/27/).
* Renaming of traits and macros to better match Bevy API.

## Crevice

Crevice creates GLSL-compatible versions of types through the power of derive
macros. Generated structures provide an [`as_bytes`][std140::Std140::as_bytes]
Expand All @@ -21,7 +32,7 @@ other math libraries by use of the mint crate. Crevice currently supports:
* mint 0.5, enabled by default
* cgmath 0.18, using the `cgmath` feature
* nalgebra 0.29, using the `nalgebra` feature
* glam 0.19, using the `glam` feature
* glam 0.20, using the `glam` feature

PRs are welcome to add or update math libraries to Crevice.

Expand Down Expand Up @@ -50,7 +61,7 @@ uniform MAIN {
```

```rust
use crevice::std140::{AsStd140, Std140};
use bevy_crevice::std140::{AsStd140, Std140};

#[derive(AsStd140)]
struct MainUniform {
Expand Down Expand Up @@ -93,7 +104,7 @@ buffer POINT_LIGHTS {
```

```rust
use crevice::std140::{self, AsStd140};
use bevy_crevice::std140::{self, AsStd140};

#[derive(AsStd140)]
struct PointLight {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "crevice-derive"
description = "Derive crate for the 'crevice' crate"
version = "0.8.0"
name = "bevy-crevice-derive"
description = "Derive crate for the 'crevice' crate (Bevy version)"
version = "0.5.0"
edition = "2018"
authors = ["Lucien Greathouse <[email protected]>"]
documentation = "https://docs.rs/crevice-derive"
homepage = "https://github.com/LPGhatguy/crevice"
repository = "https://github.com/LPGhatguy/crevice"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"

[features]
Expand All @@ -24,3 +24,4 @@ proc-macro = true
syn = "1.0.40"
quote = "1.0.7"
proc-macro2 = "1.0.21"
bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.5.0" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use quote::quote;
use syn::{parse_quote, Data, DeriveInput, Fields, Path};

pub fn emit(input: DeriveInput) -> TokenStream {
let bevy_crevice_path = crate::bevy_crevice_path();

let fields = match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => fields,
Expand All @@ -12,8 +14,8 @@ pub fn emit(input: DeriveInput) -> TokenStream {
Data::Enum(_) | Data::Union(_) => panic!("Only structs are supported"),
};

let base_trait_path: Path = parse_quote!(::crevice::glsl::Glsl);
let struct_trait_path: Path = parse_quote!(::crevice::glsl::GlslStruct);
let base_trait_path: Path = parse_quote!(#bevy_crevice_path::glsl::Glsl);
let struct_trait_path: Path = parse_quote!(#bevy_crevice_path::glsl::GlslStruct);

let name = input.ident;
let name_str = Literal::string(&name.to_string());
Expand All @@ -23,14 +25,14 @@ pub fn emit(input: DeriveInput) -> TokenStream {
let glsl_fields = fields.named.iter().map(|field| {
let field_ty = &field.ty;
let field_name_str = Literal::string(&field.ident.as_ref().unwrap().to_string());
let field_as = quote! {<#field_ty as ::crevice::glsl::GlslArray>};
let field_as = quote! {<#field_ty as #bevy_crevice_path::glsl::GlslArray>};

quote! {
s.push_str("\t");
s.push_str(#field_as::NAME);
s.push_str(" ");
s.push_str(#field_name_str);
<#field_as::ArraySize as ::crevice::glsl::DimensionList>::push_to_string(s);
<#field_as::ArraySize as #bevy_crevice_path::glsl::DimensionList>::push_to_string(s);
s.push_str(";\n");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub fn emit(
mod_name: &'static str,
min_struct_alignment: usize,
) -> TokenStream {
let bevy_crevice_path = crate::bevy_crevice_path();

let mod_name = Ident::new(mod_name, Span::call_site());
let trait_name = Ident::new(trait_name, Span::call_site());

let mod_path: Path = parse_quote!(::crevice::#mod_name);
let mod_path: Path = parse_quote!(#bevy_crevice_path::#mod_name);
let trait_path: Path = parse_quote!(#mod_path::#trait_name);

let as_trait_name = format_ident!("As{}", trait_name);
Expand Down Expand Up @@ -63,7 +65,7 @@ pub fn emit(

let field_alignments = fields.iter().map(|field| layout_alignment_of_ty(&field.ty));
let struct_alignment = quote! {
::crevice::internal::max_arr([
#bevy_crevice_path::internal::max_arr([
#min_struct_alignment,
#(#field_alignments,)*
])
Expand Down Expand Up @@ -139,13 +141,13 @@ pub fn emit(
// We set our target alignment to the larger of the
// alignment due to the previous field and the alignment
// requirement of the next field.
let alignment = ::crevice::internal::max(
let alignment = #bevy_crevice_path::internal::max(
#next_field_or_self_alignment,
min_alignment,
);

// Using everything we've got, compute our padding amount.
::crevice::internal::align_offset(starting_offset, alignment)
#bevy_crevice_path::internal::align_offset(starting_offset, alignment)
}
}
})
Expand Down Expand Up @@ -222,7 +224,7 @@ pub fn emit(
let size = ::core::mem::size_of::<Self>();
let align = <Self as #trait_path>::ALIGNMENT;

let zeroed: Self = ::crevice::internal::bytemuck::Zeroable::zeroed();
let zeroed: Self = #bevy_crevice_path::internal::bytemuck::Zeroable::zeroed();

#[derive(Debug)]
struct Field {
Expand Down Expand Up @@ -253,13 +255,13 @@ pub fn emit(
#pad_fn_impls
#struct_definition

unsafe impl #impl_generics ::crevice::internal::bytemuck::Zeroable for #generated_name #ty_generics #where_clause {}
unsafe impl #impl_generics ::crevice::internal::bytemuck::Pod for #generated_name #ty_generics #where_clause {}
unsafe impl #impl_generics #bevy_crevice_path::internal::bytemuck::Zeroable for #generated_name #ty_generics #where_clause {}
unsafe impl #impl_generics #bevy_crevice_path::internal::bytemuck::Pod for #generated_name #ty_generics #where_clause {}

unsafe impl #impl_generics #mod_path::#trait_name for #generated_name #ty_generics #where_clause {
const ALIGNMENT: usize = #struct_alignment;
const PAD_AT_END: bool = true;
type Padded = #padded_path<Self, {::crevice::internal::align_offset(
type Padded = #padded_path<Self, {#bevy_crevice_path::internal::align_offset(
::core::mem::size_of::<#generated_name>(),
#struct_alignment
)}>;
Expand All @@ -272,7 +274,7 @@ pub fn emit(
Self::Output {
#generated_struct_field_init

..::crevice::internal::bytemuck::Zeroable::zeroed()
..#bevy_crevice_path::internal::bytemuck::Zeroable::zeroed()
}
}

Expand Down
60 changes: 60 additions & 0 deletions crates/bevy_crevice/bevy-crevice-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
mod glsl;
mod layout;

use bevy_macro_utils::BevyManifest;
use proc_macro::TokenStream as CompilerTokenStream;

use syn::{parse_macro_input, DeriveInput, Path};

#[proc_macro_derive(AsStd140)]
pub fn derive_as_std140(input: CompilerTokenStream) -> CompilerTokenStream {
let input = parse_macro_input!(input as DeriveInput);
let expanded = layout::emit(input, "Std140", "std140", 16);

CompilerTokenStream::from(expanded)
}

#[proc_macro_derive(AsStd430)]
pub fn derive_as_std430(input: CompilerTokenStream) -> CompilerTokenStream {
let input = parse_macro_input!(input as DeriveInput);
let expanded = layout::emit(input, "Std430", "std430", 0);

CompilerTokenStream::from(expanded)
}

#[proc_macro_derive(GlslStruct)]
pub fn derive_glsl_struct(input: CompilerTokenStream) -> CompilerTokenStream {
let input = parse_macro_input!(input as DeriveInput);
let expanded = glsl::emit(input);

CompilerTokenStream::from(expanded)
}

const BEVY: &str = "bevy";
const BEVY_CREVICE: &str = "bevy_crevice";
const BEVY_RENDER: &str = "bevy_render";

fn bevy_crevice_path() -> Path {
let bevy_manifest = BevyManifest::default();
bevy_manifest
.maybe_get_path(crate::BEVY)
.map(|bevy_path| {
let mut segments = bevy_path.segments;
segments.push(BevyManifest::parse_str("render"));
segments.push(BevyManifest::parse_str("render_resource"));
Path {
leading_colon: None,
segments,
}
})
.or_else(|| bevy_manifest.maybe_get_path(crate::BEVY_RENDER))
.map(|bevy_render_path| {
let mut segments = bevy_render_path.segments;
segments.push(BevyManifest::parse_str("render_resource"));
Path {
leading_colon: None,
segments,
}
})
.unwrap_or_else(|| bevy_manifest.get_path(crate::BEVY_CREVICE))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ edition = "2018"
wgpu-validation = ["wgpu", "naga", "futures"]

[dependencies]
crevice = { path = ".." }
crevice-derive = { path = "../crevice-derive", features = ["debug-methods"] }
bevy_crevice = { path = ".." }
bevy-crevice-derive = { path = "../bevy-crevice-derive", features = ["debug-methods"] }

anyhow = "1.0.44"
bytemuck = "1.7.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::borrow::Cow;
use std::fmt::Debug;
use std::marker::PhantomData;

use crevice::glsl::{Glsl, GlslStruct};
use crevice::std140::{AsStd140, Std140};
use crevice::std430::{AsStd430, Std430};
use bevy_crevice::glsl::{Glsl, GlslStruct};
use bevy_crevice::std140::{AsStd140, Std140};
use bevy_crevice::std430::{AsStd430, Std430};
use futures::executor::block_on;
use wgpu::util::DeviceExt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn test_round_trip_primitive<T>(_value: T) {}
#[macro_use]
mod util;

use crevice::glsl::GlslStruct;
use crevice::std140::AsStd140;
use crevice::std430::AsStd430;
use bevy_crevice::glsl::GlslStruct;
use bevy_crevice::std140::AsStd140;
use bevy_crevice::std430::AsStd430;
use mint::{ColumnMatrix2, ColumnMatrix3, ColumnMatrix4, Vector2, Vector3, Vector4};

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Defines traits and types for generating GLSL code from Rust definitions.

pub use crevice_derive::GlslStruct;
pub use bevy_crevice_derive::GlslStruct;
use std::marker::PhantomData;

/// Type-level linked list of array dimensions
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions crates/crevice/src/lib.rs → crates/bevy_crevice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ uniform MAIN {
```

```rust
use crevice::std140::{AsStd140, Std140};
use bevy_crevice::std140::{AsStd140, Std140};

#[derive(AsStd140)]
struct MainUniform {
Expand Down Expand Up @@ -100,7 +100,7 @@ buffer POINT_LIGHTS {
```

```rust
use crevice::std140::{self, AsStd140};
use bevy_crevice::std140::{self, AsStd140};

#[derive(AsStd140)]
struct PointLight {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ pub use self::traits::*;
#[cfg(feature = "std")]
pub use self::writer::*;

pub use crevice_derive::AsStd140;
pub use bevy_crevice_derive::AsStd140;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ buffer FROBS {
```

```
use crevice::std140::{self, AsStd140};
use bevy_crevice::std140::{self, AsStd140};

#[derive(AsStd140)]
struct Frob {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uniform CAMERA {
```

```no_run
use crevice::std140::{AsStd140, Std140};
use bevy_crevice::std140::{AsStd140, Std140};

#[derive(AsStd140)]
struct CameraUniform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ buffer POINT_LIGHTS {
```

```
use crevice::std140::{self, AsStd140};
use bevy_crevice::std140::{self, AsStd140};

#[derive(AsStd140)]
struct PointLight {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub use self::traits::*;
#[cfg(feature = "std")]
pub use self::writer::*;

pub use crevice_derive::AsStd430;
pub use bevy_crevice_derive::AsStd430;
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ buffer FROBS {
```

```
use crevice::std430::{self, AsStd430};
use bevy_crevice::std430::{self, AsStd430};

#[derive(AsStd430)]
struct Frob {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uniform CAMERA {
```

```no_run
use crevice::std430::{AsStd430, Std430};
use bevy_crevice::std430::{AsStd430, Std430};

#[derive(AsStd430)]
struct CameraUniform {
Expand Down
Loading