Skip to content

Commit

Permalink
Release 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriBaal committed Jun 21, 2023
1 parent 4e3b605 commit 65dc7fc
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 98 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shura"
version = "0.5.2"
version = "0.6.0"
edition = "2021"
repository = "https://github.com/AndriBaal/shura"
documentation = "https://docs.rs/shura"
Expand Down Expand Up @@ -59,7 +59,6 @@ mint = { version = "0.5" }
nalgebra = { version = "0.32", features = ["convert-bytemuck", "mint"] }
glyph_brush = { version = "0.7", optional = true }
gilrs = { version = "0.10", optional = true }
naga = "0.12"
egui-wgpu = { version = "0.22", features = ["winit"], optional = true }
egui = { version = "0.22", default-features = false, optional = true, features = [
"bytemuck",
Expand All @@ -86,7 +85,7 @@ rand = "0.8.5"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
egui-winit = { version = "0.22", features = ["links"], optional = true }
wgpu = { version = "0.16", features = ["glsl"] }
wgpu = { version = "0.16" }
rodio = { version = "0.17", default-features = false, optional = true, features = [
"symphonia-all",
] }
Expand Down
24 changes: 0 additions & 24 deletions res/shader/vertex.glsl

This file was deleted.

5 changes: 4 additions & 1 deletion res/shader/vertex.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ struct InstanceInput {
@location(5) position: vec2<f32>,
@location(6) rotation: vec4<f32>,
@location(7) sprite: vec2<i32>,
// SHURA_MARKER_INSTANCE_INPUT
}

struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex: vec2<f32>,
@location(1) sprite: vec2<i32>
@location(1) sprite: vec2<i32>
// SHURA_MARKER_VERTEX_OUTPUT
}

@vertex
Expand All @@ -32,6 +34,7 @@ fn main(
var out: VertexOutput;
out.tex = model.tex;
out.sprite = instance.sprite;
// SHURA_MARKER_VARIABLE_ASSIGNMENT

let pos = model.position * mat2x2<f32>(instance.rotation.xy, instance.rotation.zw) + instance.position;
out.clip_position = camera.view_proj * vec4<f32>(pos, 0.0, 1.0);
Expand Down
9 changes: 9 additions & 0 deletions src/component/component_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ where

/// Method called when the game is closed or the scene gets removed
fn end(ctx: &mut Context, reason: EndReason) {}

// fn buffer<'a>(components: impl Iterator<Item=BoxedComponent>) -> &'a [u8] {
// todo!();
// }

// fn additional_layout(#[cfg(feature = "physics")] world: &mut World) -> Option<&[wgpu::VertexFormat]> {
// todo!();
// }

}

impl<C: ComponentDerive + ?Sized> ComponentDerive for Box<C> {
Expand Down
10 changes: 10 additions & 0 deletions src/component/component_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,16 @@ impl ComponentManager {
ty.render_each(encoder, config, each)
}

pub fn render_single<'a, C: ComponentController>(
&'a self,
encoder: &'a mut RenderEncoder,
config: RenderConfig<'a>,
each: impl FnOnce(&mut Renderer<'a>, &'a C, InstanceIndex),
) -> Renderer<'a> {
let ty = type_ref!(self, C);
ty.render_single(encoder, config, each)
}

pub fn render_each_prepare<'a, C: ComponentController>(
&'a self,
encoder: &'a mut RenderEncoder,
Expand Down
9 changes: 9 additions & 0 deletions src/component/component_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ impl<'a, C: ComponentController> ComponentSetMut<'a, C> {
) -> Renderer<'a> {
self.ty.render_each(encoder, config, each)
}

pub fn render_single(
&'a self,
encoder: &'a mut RenderEncoder,
config: RenderConfig<'a>,
each: impl FnOnce(&mut Renderer<'a>, &'a C, InstanceIndex),
) -> Renderer<'a> {
self.ty.render_single(encoder, config, each)
}

pub fn render_each_prepare(
&'a self,
Expand Down
27 changes: 27 additions & 0 deletions src/component/component_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,33 @@ impl ComponentType {
return renderer;
}

pub fn render_single<'a, C: ComponentController>(
&'a self,
encoder: &'a mut RenderEncoder,
config: RenderConfig<'a>,
each: impl FnOnce(&mut Renderer<'a>, &'a C, InstanceIndex),
) -> Renderer<'a> {
let mut renderer = encoder.renderer(config);
match &self.storage {
ComponentTypeStorage::Single {
buffer, component, ..
} => {
renderer.use_instances(buffer.as_ref().expect(BUFFER_ERROR));
if let Some(component) = component {
(each)(
&mut renderer,
component.downcast_ref::<C>().unwrap(),
InstanceIndex::new(0),
);
}
}
_ => {
panic!("Cannot get single on component without ComponentStorage::Single!")
}
}
return renderer;
}

pub fn render_each_prepare<'a, C: ComponentController>(
&'a self,
encoder: &'a mut RenderEncoder,
Expand Down
37 changes: 13 additions & 24 deletions src/graphics/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::text::{FontBrush, TextPipeline};
use crate::{
Camera, CameraBuffer, Color, ColorWrites, InstanceBuffer, InstanceData, Isometry, Model,
ModelBuilder, RenderConfig, RenderEncoder, RenderTarget, Shader, ShaderConfig, ShaderField,
ShaderLang, Sprite, SpriteSheet, Uniform, Vector,
Sprite, SpriteSheet, Uniform, Vector,
};
use std::borrow::Cow;
use wgpu::{util::DeviceExt, BlendState};
Expand Down Expand Up @@ -251,8 +251,7 @@ pub struct WgpuBase {
pub sprite_layout: wgpu::BindGroupLayout,
pub camera_layout: wgpu::BindGroupLayout,
pub uniform_layout: wgpu::BindGroupLayout,
pub vertex_wgsl: wgpu::ShaderModule,
pub vertex_glsl: wgpu::ShaderModule,
pub vertex_shader: wgpu::ShaderModule,
pub texture_sampler: wgpu::Sampler,
#[cfg(feature = "text")]
pub text_pipeline: TextPipeline,
Expand Down Expand Up @@ -343,17 +342,9 @@ impl WgpuBase {
],
});

let vertex_wgsl = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("vertex_wgsl"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(Shader::VERTEX_WGSL)),
});
let vertex_glsl = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("vertex_glsl"),
source: wgpu::ShaderSource::Glsl {
shader: Cow::Borrowed(Shader::VERTEX_GLSL),
stage: naga::ShaderStage::Vertex,
defines: Default::default(),
},
let vertex_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("vertex_shader"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(Shader::VERTEX)),
});

let texture_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
Expand Down Expand Up @@ -388,8 +379,7 @@ impl WgpuBase {
sprite_layout,
camera_layout,
uniform_layout,
vertex_wgsl,
vertex_glsl,
vertex_shader,
texture_sampler,
#[cfg(feature = "text")]
text_pipeline,
Expand Down Expand Up @@ -431,8 +421,7 @@ pub struct GpuDefaults {
impl GpuDefaults {
pub(crate) fn new(gpu: &Gpu, window_size: Vector<u32>) -> Self {
let sprite_no_msaa = gpu.create_shader(ShaderConfig {
fragment_source: Shader::SPRITE_WGSL,
shader_lang: ShaderLang::WGSL,
fragment_source: Shader::SPRITE,
shader_fields: &[ShaderField::Sprite],
msaa: false,
blend: BlendState::ALPHA_BLENDING,
Expand All @@ -441,37 +430,37 @@ impl GpuDefaults {
});

let sprite_sheet = gpu.create_shader(ShaderConfig {
fragment_source: Shader::SPRITE_SHEET_WGSL,
fragment_source: Shader::SPRITE_SHEET,
shader_fields: &[ShaderField::SpriteSheet],
..Default::default()
});

let sprite_sheet_uniform = gpu.create_shader(ShaderConfig {
fragment_source: Shader::SPRITE_SHEET_UNIFORM_WGSL,
fragment_source: Shader::SPRITE_SHEET_UNIFORM,
shader_fields: &[ShaderField::SpriteSheet, ShaderField::Uniform],
..Default::default()
});

let sprite = gpu.create_shader(ShaderConfig {
fragment_source: Shader::SPRITE_WGSL,
fragment_source: Shader::SPRITE,
shader_fields: &[ShaderField::Sprite],
..Default::default()
});

let rainbow = gpu.create_shader(ShaderConfig {
fragment_source: Shader::RAINBOW_WGSL,
fragment_source: Shader::RAINBOW,
shader_fields: &[ShaderField::Uniform],
..Default::default()
});

let grey = gpu.create_shader(ShaderConfig {
fragment_source: Shader::GREY_WGSL,
fragment_source: Shader::GREY,
shader_fields: &[ShaderField::Sprite],
..Default::default()
});

let blurr = gpu.create_shader(ShaderConfig {
fragment_source: Shader::BLURR_WGSL,
fragment_source: Shader::BLURR,
shader_fields: &[ShaderField::Sprite],
..Default::default()
});
Expand Down
61 changes: 15 additions & 46 deletions src/graphics/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub use wgpu::{BlendComponent, BlendFactor, BlendOperation, BlendState, ColorWri
/// Properties of a [Shader]
pub struct ShaderConfig<'a> {
pub fragment_source: &'a str,
pub shader_lang: ShaderLang,
pub shader_fields: &'a [ShaderField],
pub msaa: bool,
pub blend: BlendState,
Expand All @@ -18,7 +17,6 @@ impl Default for ShaderConfig<'static> {
fn default() -> Self {
Self {
fragment_source: "",
shader_lang: ShaderLang::WGSL,
shader_fields: &[],
msaa: true,
blend: BlendState::ALPHA_BLENDING,
Expand Down Expand Up @@ -51,13 +49,6 @@ pub enum ShaderField {
SpriteSheet,
}

#[derive(Copy, Clone, Eq, PartialEq)]
/// Supported shader languages.
pub enum ShaderLang {
GLSL,
WGSL,
}

/// Shader following the shura shader system. The vertex shader is the same along every shader and is provided
/// by shura.
///
Expand Down Expand Up @@ -100,20 +91,18 @@ pub enum ShaderLang {
///
pub struct Shader {
pipeline: wgpu::RenderPipeline,
lang: ShaderLang,
msaa: bool,
}

impl Shader {
pub const VERTEX_GLSL: &'static str = include_str!("../../res/shader/vertex.glsl");
pub const VERTEX_WGSL: &'static str = include_str!("../../res/shader/vertex.wgsl");
pub const SPRITE_WGSL: &'static str = include_str!("../../res/shader/sprite.wgsl");
pub const SPRITE_SHEET_WGSL: &'static str = include_str!("../../res/shader/sprite_sheet.wgsl");
pub const SPRITE_SHEET_UNIFORM_WGSL: &'static str =
pub const VERTEX: &'static str = include_str!("../../res/shader/vertex.wgsl");
pub const SPRITE: &'static str = include_str!("../../res/shader/sprite.wgsl");
pub const SPRITE_SHEET: &'static str = include_str!("../../res/shader/sprite_sheet.wgsl");
pub const SPRITE_SHEET_UNIFORM: &'static str =
include_str!("../../res/shader/sprite_sheet_uniform.wgsl");
pub const RAINBOW_WGSL: &'static str = include_str!("../../res/shader/rainbow.wgsl");
pub const GREY_WGSL: &'static str = include_str!("../../res/shader/grey.wgsl");
pub const BLURR_WGSL: &'static str = include_str!("../../res/shader/blurr.wgsl");
pub const RAINBOW: &'static str = include_str!("../../res/shader/rainbow.wgsl");
pub const GREY: &'static str = include_str!("../../res/shader/grey.wgsl");
pub const BLURR: &'static str = include_str!("../../res/shader/blurr.wgsl");
pub fn new(gpu: &Gpu, config: ShaderConfig) -> Self {
let mut layouts: Vec<&wgpu::BindGroupLayout> = vec![&gpu.base.camera_layout];
for link in config.shader_fields.iter() {
Expand All @@ -130,28 +119,13 @@ impl Shader {
}
}

let vertex_shader = match config.shader_lang {
ShaderLang::GLSL => &gpu.base.vertex_glsl,
ShaderLang::WGSL => &gpu.base.vertex_wgsl,
};
let fragment_shader = match config.shader_lang {
ShaderLang::GLSL => gpu
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Glsl {
shader: Cow::Borrowed(config.fragment_source),
stage: naga::ShaderStage::Fragment,
defines: Default::default(),
},
}),
ShaderLang::WGSL => gpu
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(config.fragment_source)),
}),
};

let fragment_shader = gpu
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(config.fragment_source)),
});

let render_pipeline_layout =
gpu.device
Expand All @@ -170,7 +144,7 @@ impl Shader {
label: None,
layout: Some(&render_pipeline_layout),
vertex: wgpu::VertexState {
module: &vertex_shader,
module: &gpu.base.vertex_shader,
entry_point: "main",
buffers: &buffers[..],
},
Expand Down Expand Up @@ -207,7 +181,6 @@ impl Shader {

Shader {
pipeline,
lang: config.shader_lang,
msaa: config.msaa,
}
}
Expand All @@ -216,10 +189,6 @@ impl Shader {
&self.pipeline
}

pub fn lang(&self) -> ShaderLang {
self.lang
}

pub fn msaa(&self) -> bool {
self.msaa
}
Expand Down

0 comments on commit 65dc7fc

Please sign in to comment.