Skip to content

Commit

Permalink
Implemented sprite alpha rendering + updated deps
Browse files Browse the repository at this point in the history
Also releasing a new update: 0.1.0-dev.5
  • Loading branch information
ecton committed Dec 6, 2021
1 parent 09e7734 commit 6675a86
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kludgine-app"
version = "0.1.0-dev.4"
version = "0.1.0-dev.5"
authors = ["Jonathan Johnson <[email protected]>"]
edition = "2018"
description = "Application and Windowing for Kludgine"
Expand All @@ -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" }
Expand All @@ -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"
2 changes: 1 addition & 1 deletion app/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kludgine-core"
version = "0.1.0-dev.4"
version = "0.1.0-dev.5"
authors = ["Jonathan Johnson <[email protected]>"]
edition = "2018"
description = "2D rendering for Kludgine"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions core/src/frame_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ where
dest,
sprite::SpriteRotation::none(),
text.color.into(),
1.,
);
}
render_commands.push(RenderCommand::FontBuffer(
Expand Down
6 changes: 2 additions & 4 deletions core/src/scene.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::HashSet,
sync::Arc,
time::{Duration, Instant},
};
Expand All @@ -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.
Expand Down Expand Up @@ -59,7 +59,6 @@ pub struct Scene {
event_sender: flume::Sender<SceneEvent>,
now: Option<Instant>,
elapsed: Option<Duration>,
fonts: HashMap<String, Vec<Font>>,
system_theme: Theme,
}

Expand Down Expand Up @@ -209,7 +208,6 @@ impl Scene {
keys_pressed: HashSet::new(),
now: None,
elapsed: None,
fonts: HashMap::new(),
system_theme: default_system_theme,
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl SpriteAnimations {
/// Returns the animation for `tag`.
#[must_use]
pub fn animation_for(&self, tag: &Option<impl ToString>) -> Option<&'_ SpriteAnimation> {
self.animations.get(&tag.as_ref().map(|s| s.to_string()))
self.animations.get(&tag.as_ref().map(ToString::to_string))
}
}

Expand Down
11 changes: 9 additions & 2 deletions core/src/sprite/gpu_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -57,6 +57,7 @@ impl GpuBatch {
destination.as_extents(),
sprite.rotation,
white_transparent,
sprite.alpha,
);
}
}
Expand All @@ -68,6 +69,7 @@ impl GpuBatch {
src: Point<f32, Unknown>,
dest: Point<f32, Pixels>,
color: Rgba8,
alpha: f32,
) -> Vertex {
Vertex {
position: [dest.x, dest.y, 0.],
Expand All @@ -76,6 +78,7 @@ impl GpuBatch {
src.y / self.size.height as f32,
],
color,
alpha,
}
}

Expand All @@ -85,6 +88,7 @@ impl GpuBatch {
mut dest: ExtentsRect<f32, Pixels>,
rotation: SpriteRotation<Pixels>,
color: Rgba8,
alpha: f32,
) {
let mut src = src.cast::<f32>();
if let Some(clip) = &self.clip {
Expand Down Expand Up @@ -142,27 +146,30 @@ 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
.vertex(
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
.vertex(
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);

Expand Down
2 changes: 2 additions & 0 deletions core/src/sprite/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Vertex {
pub position: [f32; 3],
pub uv: [f32; 2],
pub color: Rgba8,
pub alpha: f32,
}

impl Vertex {
Expand Down Expand Up @@ -72,6 +73,7 @@ where
VertexFormat::Float3,
VertexFormat::Float2,
VertexFormat::UByte4,
VertexFormat::Float,
],
pipeline_layout: &[
Set(&[Binding {
Expand Down
3 changes: 3 additions & 0 deletions core/src/sprite/shaders/sprite-srgb.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
Binary file modified core/src/sprite/shaders/sprite-srgb.vert.spv
Binary file not shown.
3 changes: 2 additions & 1 deletion core/src/sprite/shaders/sprite.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,6 +14,6 @@ void main() {

fragColor = vec4(
mix(texel.rgb, f_color.rgb, f_color.a),
texel.a
texel.a * f_alpha
);
}
Binary file modified core/src/sprite/shaders/sprite.frag.spv
Binary file not shown.
3 changes: 3 additions & 0 deletions core/src/sprite/shaders/sprite.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Binary file modified core/src/sprite/shaders/sprite.vert.spv
Binary file not shown.
7 changes: 1 addition & 6 deletions core/src/sprite/sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ where
T: Debug,
{
tile_size: Size<u32>,
dimensions: Size<u32>,
sprites: HashMap<T, Rect<u32>>,
}

Expand Down Expand Up @@ -100,11 +99,7 @@ impl<T: Debug + Eq + Hash> SpriteSheetData<T> {
);
}

Self {
tile_size,
dimensions,
sprites,
}
Self { tile_size, sprites }
}
}

Expand Down
6 changes: 3 additions & 3 deletions kludgine/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kludgine"
version = "0.1.0-dev.4"
version = "0.1.0-dev.5"
authors = ["Jonathan Johnson <[email protected]>"]
edition = "2018"
description = "An asynchronous app and 2d game framework"
Expand All @@ -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]
Expand Down

0 comments on commit 6675a86

Please sign in to comment.