From 6675a8609d5c95e492b02dcfb50fbba756f286cc Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Mon, 6 Dec 2021 14:22:55 -0800 Subject: [PATCH] Implemented sprite alpha rendering + updated deps Also releasing a new update: 0.1.0-dev.5 --- CHANGELOG.md | 7 +++++++ Cargo.toml | 4 ++-- README.md | 2 +- app/Cargo.toml | 6 +++--- app/src/runtime.rs | 2 +- core/Cargo.toml | 10 +++++----- core/src/frame_renderer.rs | 1 + core/src/scene.rs | 6 ++---- core/src/sprite.rs | 2 +- core/src/sprite/gpu_batch.rs | 11 +++++++++-- core/src/sprite/pipeline.rs | 2 ++ core/src/sprite/shaders/sprite-srgb.vert | 3 +++ core/src/sprite/shaders/sprite-srgb.vert.spv | Bin 2640 -> 2788 bytes core/src/sprite/shaders/sprite.frag | 3 ++- core/src/sprite/shaders/sprite.frag.spv | Bin 1264 -> 1352 bytes core/src/sprite/shaders/sprite.vert | 3 +++ core/src/sprite/shaders/sprite.vert.spv | Bin 1664 -> 1828 bytes core/src/sprite/sheet.rs | 7 +------ kludgine/Cargo.toml | 6 +++--- 19 files changed, 46 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa6a38149..1117ffce9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.1.0-dev.5 + +### Added + +- Updated dependencies for compatability with wgpu 0.11.1. +- Implemented Sprite alpha rendering. The APIs already existed, but the alpha value was being ignored. + ## v0.1.0-dev.4 ### Added diff --git a/Cargo.toml b/Cargo.toml index 097d48ec9..1309b133c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,8 +6,8 @@ resolver = "2" # easygpu = { git = "https://github.com/khonsulabs/easygpu.git", branch = "main" } # easygpu-lyon = { git = "https://github.com/khonsulabs/easygpu.git", branch = "main" } # must be commented out for CI to succeed -# easygpu = { path = "../easygpu/easygpu" } -# easygpu-lyon = { path = "../easygpu/lyon" } +# easygpu = { path = "../easygpu/easygpu", version = "0.0.14" } +# easygpu-lyon = { path = "../easygpu/lyon", version = "0.0.14" } # [patch."https://github.com/khonsulabs/figures.git"] # figures = { path = "../figures/figures" } diff --git a/README.md b/README.md index 09b78b267..46e5ecb20 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ lines to add to your `Cargo.toml` look like this: resolver = "2" [dependencies] -kludgine = "0.1.0-dev.4" +kludgine = "0.1.0-dev.5" ``` The `resolver` requirement is inherited from `wgpu`. This setting [will become diff --git a/app/Cargo.toml b/app/Cargo.toml index e1faced4a..291757a90 100644 --- a/app/Cargo.toml +++ b/app/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kludgine-app" -version = "0.1.0-dev.4" +version = "0.1.0-dev.5" authors = ["Jonathan Johnson "] edition = "2018" description = "Application and Windowing for Kludgine" @@ -17,7 +17,7 @@ tokio-rt = ["tokio"] smol-rt = ["smol", "smol-timeout", "easy-parallel"] [dependencies] -kludgine-core = { version = "0.1.0-dev.4", path = "../core" } +kludgine-core = { version = "0.1.0-dev.5", path = "../core" } parking_lot = "0.11" tracing = { version = "0.1" } @@ -30,7 +30,7 @@ tokio = { version = "1.0", features = ["full"], optional = true } once_cell = "1" lazy_static = "1" -platforms = "1" +platforms = "2" thiserror = "1" anyhow = "1.0" futures = "0.3" diff --git a/app/src/runtime.rs b/app/src/runtime.rs index d338fcbfb..d1542a031 100644 --- a/app/src/runtime.rs +++ b/app/src/runtime.rs @@ -115,7 +115,7 @@ where let guard = GLOBAL_RUNTIME_RECEIVER.lock(); guard.as_ref().expect("Receiver was not set").clone() }; - while let Some(event) = event_receiver.recv().ok() { + while let Ok(event) = event_receiver.recv() { match event { RuntimeEvent::Running => { running = true; diff --git a/core/Cargo.toml b/core/Cargo.toml index e0051f522..139e481f7 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kludgine-core" -version = "0.1.0-dev.4" +version = "0.1.0-dev.5" authors = ["Jonathan Johnson "] edition = "2018" description = "2D rendering for Kludgine" @@ -29,8 +29,8 @@ serialization = ["serde", "figures/serde", "palette/serializing"] [dependencies] approx = "0.5" -easygpu = "0.0.14" -easygpu-lyon = "0.0.14" +easygpu = "0.0.15" +easygpu-lyon = "0.0.15" image = { version = ">=0.23.12", default-features = false } palette = "0.6" futures-timer = "3" @@ -39,11 +39,11 @@ futures = "0.3" instant = "0.1.9" flume = "0.10" rusttype = { version = "0.9", features = ["gpu_cache"] } -platforms = "1" +platforms = "2" ttf-parser = "0.6" json = "0.12" serde = { version = "1", optional = true, features = ["derive"] } -winit = "0.25" +winit = "0.26" bytemuck = { version = "1", features = ["derive"] } lazy_static = "1" thiserror = "1" diff --git a/core/src/frame_renderer.rs b/core/src/frame_renderer.rs index f7f29f118..7ba39a34e 100644 --- a/core/src/frame_renderer.rs +++ b/core/src/frame_renderer.rs @@ -488,6 +488,7 @@ where dest, sprite::SpriteRotation::none(), text.color.into(), + 1., ); } render_commands.push(RenderCommand::FontBuffer( diff --git a/core/src/scene.rs b/core/src/scene.rs index 326f15f7b..13a5ba054 100644 --- a/core/src/scene.rs +++ b/core/src/scene.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, sync::Arc, time::{Duration, Instant}, }; @@ -12,7 +12,7 @@ use crate::{ math::{Point, Scale, Scaled, Size, Vector}, shape::Shape, sprite::RenderedSprite, - text::{font::Font, prepared::PreparedSpan}, + text::prepared::PreparedSpan, }; /// An individual render instruction. @@ -59,7 +59,6 @@ pub struct Scene { event_sender: flume::Sender, now: Option, elapsed: Option, - fonts: HashMap>, system_theme: Theme, } @@ -209,7 +208,6 @@ impl Scene { keys_pressed: HashSet::new(), now: None, elapsed: None, - fonts: HashMap::new(), system_theme: default_system_theme, } } diff --git a/core/src/sprite.rs b/core/src/sprite.rs index 371050726..f1c461949 100644 --- a/core/src/sprite.rs +++ b/core/src/sprite.rs @@ -433,7 +433,7 @@ impl SpriteAnimations { /// Returns the animation for `tag`. #[must_use] pub fn animation_for(&self, tag: &Option) -> Option<&'_ SpriteAnimation> { - self.animations.get(&tag.as_ref().map(|s| s.to_string())) + self.animations.get(&tag.as_ref().map(ToString::to_string)) } } diff --git a/core/src/sprite/gpu_batch.rs b/core/src/sprite/gpu_batch.rs index f0925095e..e46053748 100644 --- a/core/src/sprite/gpu_batch.rs +++ b/core/src/sprite/gpu_batch.rs @@ -32,13 +32,13 @@ impl GpuBatch { b: 255, a: 0, }; - match &sprite.source.location { SpriteSourceLocation::Rect(location) => self.add_box( location.as_extents(), sprite.render_at, sprite.rotation, white_transparent, + sprite.alpha, ), SpriteSourceLocation::Joined(locations) => { let source_bounds = sprite.source.location.bounds(); @@ -57,6 +57,7 @@ impl GpuBatch { destination.as_extents(), sprite.rotation, white_transparent, + sprite.alpha, ); } } @@ -68,6 +69,7 @@ impl GpuBatch { src: Point, dest: Point, color: Rgba8, + alpha: f32, ) -> Vertex { Vertex { position: [dest.x, dest.y, 0.], @@ -76,6 +78,7 @@ impl GpuBatch { src.y / self.size.height as f32, ], color, + alpha, } } @@ -85,6 +88,7 @@ impl GpuBatch { mut dest: ExtentsRect, rotation: SpriteRotation, color: Rgba8, + alpha: f32, ) { let mut src = src.cast::(); if let Some(clip) = &self.clip { @@ -142,13 +146,14 @@ impl GpuBatch { let origin = rotation.location.unwrap_or_else(|| dest.center()); let top_left = self - .vertex(src.origin, dest.origin, color) + .vertex(src.origin, dest.origin, color, alpha) .rotate_by(rotation.angle, origin); let top_right = self .vertex( Point::from_figures(src.extent.x(), src.origin.y()), Point::from_figures(dest.extent.x(), dest.origin.y()), color, + alpha, ) .rotate_by(rotation.angle, origin); let bottom_left = self @@ -156,6 +161,7 @@ impl GpuBatch { Point::from_figures(src.origin.x(), src.extent.y()), Point::from_figures(dest.origin.x(), dest.extent.y()), color, + alpha, ) .rotate_by(rotation.angle, origin); let bottom_right = self @@ -163,6 +169,7 @@ impl GpuBatch { Point::from_figures(src.extent.x(), src.extent.y()), Point::from_figures(dest.extent.x(), dest.extent.y()), color, + alpha, ) .rotate_by(rotation.angle, origin); diff --git a/core/src/sprite/pipeline.rs b/core/src/sprite/pipeline.rs index 055abddad..d34d2147b 100644 --- a/core/src/sprite/pipeline.rs +++ b/core/src/sprite/pipeline.rs @@ -28,6 +28,7 @@ pub struct Vertex { pub position: [f32; 3], pub uv: [f32; 2], pub color: Rgba8, + pub alpha: f32, } impl Vertex { @@ -72,6 +73,7 @@ where VertexFormat::Float3, VertexFormat::Float2, VertexFormat::UByte4, + VertexFormat::Float, ], pipeline_layout: &[ Set(&[Binding { diff --git a/core/src/sprite/shaders/sprite-srgb.vert b/core/src/sprite/shaders/sprite-srgb.vert index fcf9e461f..f2670b9fa 100644 --- a/core/src/sprite/shaders/sprite-srgb.vert +++ b/core/src/sprite/shaders/sprite-srgb.vert @@ -8,9 +8,11 @@ layout(set = 0, binding = 0) uniform Globals { layout(location = 0) in vec3 position; layout(location = 1) in vec2 uv; layout(location = 2) in vec4 color; +layout(location = 3) in float alpha; layout(location = 0) out vec2 f_uv; layout(location = 1) out vec4 f_color; +layout(location = 2) out float f_alpha; // Convert an sRGB color to linear space. @@ -25,6 +27,7 @@ vec3 linearize(vec3 srgb) { void main() { f_color = vec4(linearize(color.rgb), color.a); f_uv = uv; + f_alpha = alpha; gl_Position = global.ortho * global.transform * vec4(position, 1.0); } diff --git a/core/src/sprite/shaders/sprite-srgb.vert.spv b/core/src/sprite/shaders/sprite-srgb.vert.spv index 9ee2e396ace832c8801627052df8b34ed9766e78..b2c8d320729e51da0c0f4ffce4ad499fa8c0f0cc 100644 GIT binary patch delta 1083 zcmYk6&1w@-7>2(y`Kc%amRc%ALu(fIIXROn`ZV% z*cRH~2cg?28rp|WG(0(utYpR1pD`0J+m`%C!%nL|-tUhm{m-IOr6*Ij;{LeGO*(Su(3GtYN8M<6s)$m`@r}llDAdLBof>{( z$*P-Wu>of_Gijw<)3e(}A?hx$(F^+KROI}lIbmjZeov#&cbjFHE@ z(jEPa>X7qVAP=1wjThTn5oX&}5Oci~&^WpFUUxM1%e9Wzc$Ah(@t}tS`NZQt5T>e& dFA^BwP+;gAvRzxq{sGvG&AYPE?Dx4(?k^I{V9Wpj delta 923 zcmYk3-AWr#6ovPh$t0)*3aM0yl~^xIv6mHuQc+?|G;NLji9c->1uH^K&;;*Fo*|tl z@DWO0qY>o%}*s)bw_&U;mnUrQllPUgoTrI(*KoAfO18kTdL7B+9QE(aCEDx z8(TdSIB7gL3X`}wybX&%e?ih$qRRp;W)!cWGf?0w#d z`@~Jpq0chv?r<^p-1S%WrqtUK=)s_Gq@i|0fKN3aQ`)#ozcUPyjvoU16ZYG R;>76w>baI|cs15`{{X_YQ6vBW diff --git a/core/src/sprite/shaders/sprite.frag b/core/src/sprite/shaders/sprite.frag index 2966d2ebb..a845cfd1e 100644 --- a/core/src/sprite/shaders/sprite.frag +++ b/core/src/sprite/shaders/sprite.frag @@ -5,6 +5,7 @@ layout(set = 1, binding = 1) uniform sampler sam; layout(location = 0) in vec2 f_uv; layout(location = 1) in vec4 f_color; +layout(location = 2) in float f_alpha; layout(location = 0) out vec4 fragColor; @@ -13,6 +14,6 @@ void main() { fragColor = vec4( mix(texel.rgb, f_color.rgb, f_color.a), - texel.a + texel.a * f_alpha ); } diff --git a/core/src/sprite/shaders/sprite.frag.spv b/core/src/sprite/shaders/sprite.frag.spv index 920e3f2c6de5af569817774e604bedb307a3b5df..a153934aa42ad7d44f91bc7c0f500bd67d9a5dfe 100644 GIT binary patch delta 245 zcmeysd4h|VnMs+Qfq{{Mi-DKHcp|Tkbu@1Bx?kp2=9v$ZHK0QUU1!V#dva%*Bk3wm=~^AXgVE z+{(ZT=Ia6Z+EDR8sF*&GuK~m$024EqypToM#t>n ze1NfnakB?=0b@=e11kd?P)Y}gHGmidU}CyJHb{&SNlXta#ss7T7}$Z3fk7XLLHa@J WLGs!_Y{$S1wh|=w7ifY75CZ^Ay%8k< diff --git a/core/src/sprite/shaders/sprite.vert b/core/src/sprite/shaders/sprite.vert index a3332cabd..66ee8e7df 100644 --- a/core/src/sprite/shaders/sprite.vert +++ b/core/src/sprite/shaders/sprite.vert @@ -8,13 +8,16 @@ layout(set = 0, binding = 0) uniform Globals { layout(location = 0) in vec3 position; layout(location = 1) in vec2 uv; layout(location = 2) in vec4 color; +layout(location = 3) in float alpha; layout(location = 0) out vec2 f_uv; layout(location = 1) out vec4 f_color; +layout(location = 2) out float f_alpha; void main() { f_color = color; f_uv = uv; + f_alpha = alpha; gl_Position = global.ortho * global.transform * vec4(position, 1.0); } diff --git a/core/src/sprite/shaders/sprite.vert.spv b/core/src/sprite/shaders/sprite.vert.spv index 9e840b587d6c87d11869d7e596393ee94f4498cf..6e59cb080db515eb7af1ef0b061e66dbc8a3eac5 100644 GIT binary patch literal 1828 zcmYk7S#Q%o6oseRT3To+r3+Ak)36u9)>cS}O%%Bgky=!VS1Tk=Yss}E+o^yj{sR0Y zeibiBoX^e#4?4Yb&Ryot9Y<6v?O9`H%)Gf|_Dr#sO+|_^3#O4;pnuNbNVkB{)V|BeiC{|e)8E*GXJZn)Iw*EFHVCjh$E@9c|7yV^&rZQ zgYUW(IfajT$oC-(M*BgU^`gE%^)YRH<1n21=$Co2=(i>7hVhqPm@2QHb9`}HV@6_aW?hsT!`HgSj+{Ep9SalVCS}?Eb>~aQI%!Crd>nU5!MyYIdJaj=RCUWw?-p>MV44vzXq>~$ z9f7)>1HUD(HpCrS>N4l1{eENH^#1F9h<5$Isk;sxH1Af%A!>XMR7s0^bJr;@)uL J{_3Qc!av@#e>VUC literal 1664 zcmY+F+iuf96h$XVla@kTO6dhCsnc*T6p#QRA#MWXK16C!C0?zNIK`4{N48S|Py7OW z5?{p&5_^q3QJCuF%--jI#!?&g?z}NIvuKvgV^f}0Qx{{*dDF7m?;ZE{^J2LF|Rz_!fsj z93^2ejIwZ0L}wv>3p0LEGKeR`S%;=PW^03~KMDqEoMr}0FC{V-)7L1NoJk2cY>%L( z=G_o?5(h_N_9@JY@GCy8isz0k&7&ep6S4EwpFLPUN{Zv?ySgT(v@x%2@8W3uKFW)J zG6<(Owuo&K$5R{qurCh1Z%g}e`nezHifdYoEzOEAsWiReLk~sPPx4Wko$5|(Tl&Pt zf}8dY#;$qU{AySD5f5fpVEP8LD=-JldpiFERbVQz58q7w5x zyS=O_inxw)Fc0?)-CEL~WK;3f0W+tk-vw!W;a^mk?-maGqI6B->|nP~@+}FIk6QRI zOA`xEJ}__R^06E8IXjrQgj<*YnzSe3>{a-(?%215*QKck*Vc`9ax-B==wnlwn%L%<$v~^A2tgJJlV3mw#7x>an+5 z(#+xZa=Ws@lMl?>y8iIJCo$&7>u36+s6%fYa{Lj)clJ^&eP599POoI;J;B)6`y~lG z$Hv=$-zu7(JqdHL3;gN7Bk?58r>i?Y%;S7+=nnr&d5F0wAr2lN=DI6krjPPrp1q2b znsWCmt~SFxs5oXm(sazXFCiX(?Cj>J@?v`^p*}wJ%s0+I4Bnr)m>Z1WUv2og, - dimensions: Size, sprites: HashMap>, } @@ -100,11 +99,7 @@ impl SpriteSheetData { ); } - Self { - tile_size, - dimensions, - sprites, - } + Self { tile_size, sprites } } } diff --git a/kludgine/Cargo.toml b/kludgine/Cargo.toml index f7dc35c2f..b1b38ca5d 100644 --- a/kludgine/Cargo.toml +++ b/kludgine/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kludgine" -version = "0.1.0-dev.4" +version = "0.1.0-dev.5" authors = ["Jonathan Johnson "] edition = "2018" description = "An asynchronous app and 2d game framework" @@ -27,8 +27,8 @@ bundled-fonts-roboto = ["kludgine-core/bundled-fonts-roboto"] serialization = ["kludgine-core/serialization"] [dependencies] -kludgine-core = { version = "0.1.0-dev.4", path = "../core" } -kludgine-app = { version = "0.1.0-dev.4", path = "../app", optional = true, default-features = false } +kludgine-core = { version = "0.1.0-dev.5", path = "../core" } +kludgine-app = { version = "0.1.0-dev.5", path = "../app", optional = true, default-features = false } cfg-if = "1" # [target.'cfg(target_arch = "wasm32")'.dependencies]