From bd533e27c7e8e3002edf9c2b71035061d8e5b945 Mon Sep 17 00:00:00 2001 From: Nobbele Date: Sun, 18 Sep 2022 14:30:56 +0200 Subject: [PATCH 1/3] Fixes doc comment --- src/conf.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/conf.rs b/src/conf.rs index ad209d8e4..4c251e474 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -56,6 +56,7 @@ pub enum FullscreenType { /// visible: true, /// transparent: false, /// resize_on_scale_factor_change: false, +/// logical_size: None, /// } /// # , WindowMode::default());} /// ``` From 931ef54b41cd7cba0e6a97a9758cfaad0abc9e67 Mon Sep 17 00:00:00 2001 From: Nobbele Date: Sun, 18 Sep 2022 14:47:22 +0200 Subject: [PATCH 2/3] Fixes shadows example --- examples/shadows.rs | 170 +------------------------------------- resources/lights.wgsl | 55 ++++++++++++ resources/occlusions.wgsl | 42 ++++++++++ resources/shadows.wgsl | 43 ++++++++++ 4 files changed, 143 insertions(+), 167 deletions(-) create mode 100644 resources/lights.wgsl create mode 100644 resources/occlusions.wgsl create mode 100644 resources/shadows.wgsl diff --git a/examples/shadows.rs b/examples/shadows.rs index 296445e98..c52bae708 100644 --- a/examples/shadows.rs +++ b/examples/shadows.rs @@ -25,57 +25,7 @@ struct Light { /// and encoded in the red channel (it is halved because if the distance can be /// greater than 1.0 - think bottom left to top right corner, that sqrt(1) and /// will not get properly encoded). -const OCCLUSIONS_SHADER_SOURCE: &str = " -struct VertexOutput { - @builtin(position) position: vec4; - @location(0) uv: vec2; - @location(1) color: vec4; -}; - -struct Light { - light_color: vec4, - shadow_color: vec4, - pos: vec2, - screen_size: vec2, - glow: f32, - strength: f32, -} - -@group(1) @binding(0) -var t: texture_2d; - -<<<<<<< HEAD -@group(1) @binding(1) -var s: sampler; - -@group(3) @binding(0) -======= -[[group(2), binding(0)]] -var s: sampler; - -[[group(4), binding(0)]] ->>>>>>> ec2076b1421aec71939a0eafcbabc43ec5deb4b5 -var light: Light; - -@fragment -fn main(in: VertexOutput) -> @location(0) vec4 { - var dist = 1.0; - var theta = in.uv.x * 6.28318530718; - var dir = vec2(cos(theta), sin(theta)); - for (var i: i32 = 0; i < 1024; i = i + 1) { - var fi = f32(i); - var r = fi / 1024.0; - var rel = r * dir; - var p = clamp(light.pos + rel, vec2(0.0), vec2(1.0)); - if (textureSample(t, s, p).a > 0.8) { - dist = distance(light.pos, p) * 0.5; - break; - } - } - var others = select(dist, 0.0, dist == 1.0); - return vec4(dist, others, others, 1.0); -} -"; +const OCCLUSIONS_SHADER_SOURCE: &str = include_str!("../resources/occlusions.wgsl"); /// Shader for drawing shadows based on a 1D shadow map. It takes current /// fragment coordinates and converts them to polar coordinates centered @@ -84,58 +34,7 @@ fn main(in: VertexOutput) -> @location(0) vec4 { /// closest reported shadow, then the output is the shadow color, else it calculates some /// shadow based on the distance from light source based on strength and glow /// uniform parameters. -const SHADOWS_SHADER_SOURCE: &str = " -struct VertexOutput { - @builtin(position) position: vec4, - @location(0) uv: vec2, - @location(1) color: vec4, -} - -struct Light { - light_color: vec4, - shadow_color: vec4, - pos: vec2, - screen_size: vec2, - glow: f32, - strength: f32, -} - -@group(1) @binding(0) -var t: texture_2d; - -<<<<<<< HEAD -@group(1) @binding(1) -var s: sampler; - -@group(0) binding(0) -======= -[[group(2), binding(0)]] -var s: sampler; - -[[group(4), binding(0)]] ->>>>>>> ec2076b1421aec71939a0eafcbabc43ec5deb4b5 -var light: Light; - -fn degrees(x: f32) -> f32 { - return x * 57.2957795130823208767981548141051703; -} - -@fragment -fn main(in: VertexOutput) -> @location(0) vec4 { - var rel = light.pos - in.uv; - var theta = atan2(rel.y, rel.x); - var ox = (theta + 3.1415926) / 6.2831853; - var r = length(rel); - var occl = 1.0 - step(r, textureSample(t, s, vec2(ox, 0.5)).r * 2.0); - - var g = light.screen_size / light.screen_size.y; - var p = light.strength + light.glow; - var d = distance(g * in.uv, g * light.pos); - var intensity = 1.0 - clamp(p/(d*d), 0.0, 1.0); - - return light.shadow_color * vec4(vec3(mix(intensity, 1.0, occl)), 1.0); -} -"; +const SHADOWS_SHADER_SOURCE: &str = include_str!("../resources/shadows.wgsl"); /// Shader for drawing lights based on a 1D shadow map. It takes current /// fragment coordinates and converts them to polar coordinates centered @@ -145,70 +44,7 @@ fn main(in: VertexOutput) -> @location(0) vec4 { /// light based on the distance from light source based on strength and glow /// uniform parameters. It is meant to be used additively for drawing multiple /// lights. -const LIGHTS_SHADER_SOURCE: &str = " -struct VertexOutput { - @builtin(position) position: vec4, - @location(0) uv: vec2, - @location(1) color: vec4, -} - -struct Light { - light_color: vec4, - shadow_color: vec4, - pos: vec2, - screen_size: vec2, - glow: f32, - strength: f32, -} - -@group(1) @binding(0) -var t: texture_2d; - -<<<<<<< HEAD -@group(1) @binding(1) -var s: sampler; - -@group(0) binding(0) -======= -[[group(2), binding(0)]] -var s: sampler; - -[[group(4), binding(0)]] ->>>>>>> ec2076b1421aec71939a0eafcbabc43ec5deb4b5 -var light: Light; - -fn degrees(x: f32) -> f32 { - return x * 57.2957795130823208767981548141051703; -} - -@fragment -fn main(in: VertexOutput) -> @location(0) vec4 { - var rel = light.pos - in.uv; - var theta = atan2(rel.y, rel.x); - var ox = (theta + 3.1415926) / 6.2831853; - var r = length(rel); - var occl = step(r, textureSample(t, s, vec2(ox, 0.5)).r * 2.0); - - var g = light.screen_size / light.screen_size.y; - var p = light.strength + light.glow; - var d = distance(g * in.uv, g * light.pos); - var intensity = clamp(p/(d*d), 0.0, 0.6); - - var blur = (2.5 / light.screen_size.x) * smoothStep(0.0, 1.0, r); - var sum = 0.0; - sum = sum + step(r, textureSample(t, s, vec2(ox - 4.0 * blur, 0.5)).r * 2.0) * 0.05; - sum = sum + step(r, textureSample(t, s, vec2(ox - 3.0 * blur, 0.5)).r * 2.0) * 0.09; - sum = sum + step(r, textureSample(t, s, vec2(ox - 2.0 * blur, 0.5)).r * 2.0) * 0.12; - sum = sum + step(r, textureSample(t, s, vec2(ox - 1.0 * blur, 0.5)).r * 2.0) * 0.15; - sum = sum + occl * 0.16; - sum = sum + step(r, textureSample(t, s, vec2(ox + 1.0 * blur, 0.5)).r * 2.0) * 0.15; - sum = sum + step(r, textureSample(t, s, vec2(ox + 2.0 * blur, 0.5)).r * 2.0) * 0.12; - sum = sum + step(r, textureSample(t, s, vec2(ox + 3.0 * blur, 0.5)).r * 2.0) * 0.09; - sum = sum + step(r, textureSample(t, s, vec2(ox + 4.0 * blur, 0.5)).r * 2.0) * 0.05; - - return light.light_color * vec4(vec3(sum * intensity), 1.0); -} -"; +const LIGHTS_SHADER_SOURCE: &str = include_str!("../resources/lights.wgsl"); struct MainState { background: graphics::Image, diff --git a/resources/lights.wgsl b/resources/lights.wgsl new file mode 100644 index 000000000..15dc7820c --- /dev/null +++ b/resources/lights.wgsl @@ -0,0 +1,55 @@ +struct VertexOutput { + @builtin(position) position: vec4, + @location(0) uv: vec2, + @location(1) color: vec4, +} + +struct Light { + light_color: vec4, + shadow_color: vec4, + pos: vec2, + screen_size: vec2, + glow: f32, + strength: f32, +} + +@group(1) @binding(0) +var t: texture_2d; + +@group(2) @binding(0) +var s: sampler; + +@group(4) @binding(0) +var light: Light; + +fn degrees(x: f32) -> f32 { + return x * 57.2957795130823208767981548141051703; +} + +@fragment +fn main(in: VertexOutput) -> @location(0) vec4 { + var rel = light.pos - in.uv; + var theta = atan2(rel.y, rel.x); + var ox = (theta + 3.1415926) / 6.2831853; + var r = length(rel); + var occl = step(r, textureSample(t, s, vec2(ox, 0.5)).r * 2.0); + + var g = light.screen_size / light.screen_size.y; + var p = light.strength + light.glow; + var d = distance(g * in.uv, g * light.pos); + var intensity = clamp(p / (d * d), 0.0, 0.6); + + var blur = (2.5 / light.screen_size.x) * smoothstep(0.0, 1.0, r); + var sum = 0.0; + sum = sum + step(r, textureSample(t, s, vec2(ox - 4.0 * blur, 0.5)).r * 2.0) * 0.05; + sum = sum + step(r, textureSample(t, s, vec2(ox - 3.0 * blur, 0.5)).r * 2.0) * 0.09; + sum = sum + step(r, textureSample(t, s, vec2(ox - 2.0 * blur, 0.5)).r * 2.0) * 0.12; + sum = sum + step(r, textureSample(t, s, vec2(ox - 1.0 * blur, 0.5)).r * 2.0) * 0.15; + sum = sum + occl * 0.16; + sum = sum + step(r, textureSample(t, s, vec2(ox + 1.0 * blur, 0.5)).r * 2.0) * 0.15; + sum = sum + step(r, textureSample(t, s, vec2(ox + 2.0 * blur, 0.5)).r * 2.0) * 0.12; + sum = sum + step(r, textureSample(t, s, vec2(ox + 3.0 * blur, 0.5)).r * 2.0) * 0.09; + sum = sum + step(r, textureSample(t, s, vec2(ox + 4.0 * blur, 0.5)).r * 2.0) * 0.05; + + return light.light_color * vec4(vec3(sum * intensity), 1.0); +} \ No newline at end of file diff --git a/resources/occlusions.wgsl b/resources/occlusions.wgsl new file mode 100644 index 000000000..90e7b68b8 --- /dev/null +++ b/resources/occlusions.wgsl @@ -0,0 +1,42 @@ +struct VertexOutput { + @builtin(position) position: vec4, + @location(0) uv: vec2, + @location(1) color: vec4, +}; + +struct Light { + light_color: vec4, + shadow_color: vec4, + pos: vec2, + screen_size: vec2, + glow: f32, + strength: f32, +} + +@group(1) @binding(0) +var t: texture_2d; + +@group(2) @binding(0) +var s: sampler; + +@group(4) @binding(0) +var light: Light; + +@fragment +fn main(in: VertexOutput) -> @location(0) vec4 { + var dist = 1.0; + var theta = in.uv.x * 6.28318530718; + var dir = vec2(cos(theta), sin(theta)); + for (var i: i32 = 0; i < 1024; i = i + 1) { + var fi = f32(i); + var r = fi / 1024.0; + var rel = r * dir; + var p = clamp(light.pos + rel, vec2(0.0), vec2(1.0)); + if (textureSample(t, s, p).a > 0.8) { + dist = distance(light.pos, p) * 0.5; + break; + } + } + var others = select(dist, 0.0, dist == 1.0); + return vec4(dist, others, others, 1.0); +} \ No newline at end of file diff --git a/resources/shadows.wgsl b/resources/shadows.wgsl new file mode 100644 index 000000000..7c50ea3dc --- /dev/null +++ b/resources/shadows.wgsl @@ -0,0 +1,43 @@ +struct VertexOutput { + @builtin(position) position: vec4, + @location(0) uv: vec2, + @location(1) color: vec4, +} + +struct Light { + light_color: vec4, + shadow_color: vec4, + pos: vec2, + screen_size: vec2, + glow: f32, + strength: f32, +} + +@group(1) @binding(0) +var t: texture_2d; + +@group(2) @binding(0) +var s: sampler; + +@group(4) @binding(0) +var light: Light; + +fn degrees(x: f32) -> f32 { + return x * 57.2957795130823208767981548141051703; +} + +@fragment +fn main(in: VertexOutput) -> @location(0) vec4 { + var rel = light.pos - in.uv; + var theta = atan2(rel.y, rel.x); + var ox = (theta + 3.1415926) / 6.2831853; + var r = length(rel); + var occl = 1.0 - step(r, textureSample(t, s, vec2(ox, 0.5)).r * 2.0); + + var g = light.screen_size / light.screen_size.y; + var p = light.strength + light.glow; + var d = distance(g * in.uv, g * light.pos); + var intensity = 1.0 - clamp(p / (d * d), 0.0, 1.0); + + return light.shadow_color * vec4(vec3(mix(intensity, 1.0, occl)), 1.0); +} \ No newline at end of file From 0d8ab27975485a24a0d4773877fc92dd1f49ef01 Mon Sep 17 00:00:00 2001 From: Nobbele Date: Sun, 18 Sep 2022 14:48:17 +0200 Subject: [PATCH 3/3] Removes srgb parameter --- examples/03_drawing.rs | 6 +++--- examples/05_astroblasto.rs | 6 +++--- examples/animation.rs | 2 +- examples/bunnymark.rs | 2 +- examples/canvas_subframe.rs | 2 +- examples/graphics_settings.rs | 2 +- examples/imageview.rs | 2 +- examples/instance_array.rs | 2 +- examples/shadows.rs | 4 ++-- examples/transforms.rs | 2 +- src/graphics/image.rs | 12 ++---------- 11 files changed, 17 insertions(+), 25 deletions(-) diff --git a/examples/03_drawing.rs b/examples/03_drawing.rs index 6c84ca47f..4a13dc33f 100644 --- a/examples/03_drawing.rs +++ b/examples/03_drawing.rs @@ -19,8 +19,8 @@ struct MainState { impl MainState { /// Load images and create meshes. fn new(ctx: &mut Context) -> GameResult { - let image1 = graphics::Image::from_path(ctx, "/dragon1.png", true)?; - let image2 = graphics::Image::from_path(ctx, "/shot.png", true)?; + let image1 = graphics::Image::from_path(ctx, "/dragon1.png")?; + let image2 = graphics::Image::from_path(ctx, "/shot.png")?; let mb = &mut graphics::MeshBuilder::new(); mb.rectangle( @@ -29,7 +29,7 @@ impl MainState { graphics::Color::new(1.0, 0.0, 0.0, 1.0), )?; - let rock = graphics::Image::from_path(ctx, "/rock.png", true)?; + let rock = graphics::Image::from_path(ctx, "/rock.png")?; let meshes = vec![ (None, build_mesh(ctx)?), diff --git a/examples/05_astroblasto.rs b/examples/05_astroblasto.rs index c36238f25..d527a7dcd 100644 --- a/examples/05_astroblasto.rs +++ b/examples/05_astroblasto.rs @@ -246,9 +246,9 @@ struct Assets { impl Assets { fn new(ctx: &mut Context) -> GameResult { - let player_image = graphics::Image::from_path(ctx, "/player.png", true)?; - let shot_image = graphics::Image::from_path(ctx, "/shot.png", true)?; - let rock_image = graphics::Image::from_path(ctx, "/rock.png", true)?; + let player_image = graphics::Image::from_path(ctx, "/player.png")?; + let shot_image = graphics::Image::from_path(ctx, "/shot.png")?; + let rock_image = graphics::Image::from_path(ctx, "/rock.png")?; let shot_sound = audio::Source::new(ctx, "/pew.ogg")?; let hit_sound = audio::Source::new(ctx, "/boom.ogg")?; diff --git a/examples/animation.rs b/examples/animation.rs index 9177ec942..b1b3cf7fa 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -234,7 +234,7 @@ impl MainState { Color::WHITE, )?; - let img = graphics::Image::from_path(ctx, "/player_sheet.png", true)?; + let img = graphics::Image::from_path(ctx, "/player_sheet.png")?; let s = MainState { ball, spritesheet: img, diff --git a/examples/bunnymark.rs b/examples/bunnymark.rs index f70b72d91..b7a9d1913 100644 --- a/examples/bunnymark.rs +++ b/examples/bunnymark.rs @@ -54,7 +54,7 @@ impl GameState { fn new(ctx: &mut Context) -> ggez::GameResult { // We just use the same RNG seed every time. let mut rng = Rand32::new(12345); - let texture = Image::from_path(ctx, "/wabbit_alpha.png", true)?; + let texture = Image::from_path(ctx, "/wabbit_alpha.png")?; let mut bunnies = Vec::with_capacity(INITIAL_BUNNIES); let max_x = (WIDTH - texture.width() as u16) as f32; let max_y = (HEIGHT - texture.height() as u16) as f32; diff --git a/examples/canvas_subframe.rs b/examples/canvas_subframe.rs index fee5de45f..401c741b7 100644 --- a/examples/canvas_subframe.rs +++ b/examples/canvas_subframe.rs @@ -21,7 +21,7 @@ struct MainState { impl MainState { fn new(ctx: &mut Context) -> GameResult { - let image = graphics::Image::from_path(ctx, "/tile.png", true).unwrap(); + let image = graphics::Image::from_path(ctx, "/tile.png").unwrap(); let instances = graphics::InstanceArray::new(ctx, image, 150 * 150, false); let canvas_image = graphics::ScreenImage::new(ctx, None, 1., 1., 1); let draw_pt = Point2::new(0.0, 0.0); diff --git a/examples/graphics_settings.rs b/examples/graphics_settings.rs index 853e53382..60c84b418 100644 --- a/examples/graphics_settings.rs +++ b/examples/graphics_settings.rs @@ -38,7 +38,7 @@ impl MainState { let s = MainState { angle: 0.0, zoom: 1.0, - image: graphics::Image::from_path(ctx, "/tile.png", true)?, + image: graphics::Image::from_path(ctx, "/tile.png")?, window_settings: WindowSettings { toggle_fullscreen: false, is_fullscreen: false, diff --git a/examples/imageview.rs b/examples/imageview.rs index 916df349f..5cd2f12ff 100644 --- a/examples/imageview.rs +++ b/examples/imageview.rs @@ -44,7 +44,7 @@ impl MainState { fn new(ctx: &mut Context) -> GameResult { ctx.fs.print_all(); - let image = graphics::Image::from_path(ctx, "/dragon1.png", true)?; + let image = graphics::Image::from_path(ctx, "/dragon1.png")?; let mut sound = audio::Source::new(ctx, "/sound.ogg")?; diff --git a/examples/instance_array.rs b/examples/instance_array.rs index db0a0904d..cb9d8c5ff 100644 --- a/examples/instance_array.rs +++ b/examples/instance_array.rs @@ -17,7 +17,7 @@ struct MainState { impl MainState { fn new(ctx: &mut Context) -> GameResult { - let image = graphics::Image::from_path(ctx, "/tile.png", true)?; + let image = graphics::Image::from_path(ctx, "/tile.png")?; let instances = graphics::InstanceArray::new(ctx, image, 150 * 150, false); Ok(MainState { instances }) } diff --git a/examples/shadows.rs b/examples/shadows.rs index c52bae708..1160abbd0 100644 --- a/examples/shadows.rs +++ b/examples/shadows.rs @@ -81,8 +81,8 @@ const LIGHT_GLOW_RATE: f32 = 0.9; impl MainState { fn new(ctx: &mut Context) -> GameResult { - let background = graphics::Image::from_path(ctx, "/bg_top.png", true)?; - let tile = graphics::Image::from_path(ctx, "/tile.png", true)?; + let background = graphics::Image::from_path(ctx, "/bg_top.png")?; + let tile = graphics::Image::from_path(ctx, "/tile.png")?; let screen_size = { let size = ctx.gfx.drawable_size(); diff --git a/examples/transforms.rs b/examples/transforms.rs index b327ca496..5f069ba70 100644 --- a/examples/transforms.rs +++ b/examples/transforms.rs @@ -22,7 +22,7 @@ impl MainState { const GRID_POINT_RADIUS: f32 = 5.0; fn new(ctx: &mut Context) -> GameResult { - let angle = graphics::Image::from_path(ctx, "/angle.png", true)?; + let angle = graphics::Image::from_path(ctx, "/angle.png")?; let gridmesh_builder = &mut graphics::MeshBuilder::new(); for x in 0..Self::GRID_SIZE { for y in 0..Self::GRID_SIZE { diff --git a/src/graphics/image.rs b/src/graphics/image.rs index 84fe5b579..0c5ce0014 100644 --- a/src/graphics/image.rs +++ b/src/graphics/image.rs @@ -131,11 +131,7 @@ impl Image { /// Creates a new image initialized with pixel data loaded from an encoded image `Read` (e.g. PNG or JPEG). #[allow(unused_results)] - pub fn from_path( - ctxs: &impl Has, - path: impl AsRef, - srgb: bool, - ) -> GameResult { + pub fn from_path(ctxs: &impl Has, path: impl AsRef) -> GameResult { let gfx = ctxs.retrieve(); let mut encoded = Vec::new(); @@ -148,11 +144,7 @@ impl Image { Ok(Self::from_pixels( gfx, rgba8.as_ref(), - if srgb { - ImageFormat::Rgba8UnormSrgb - } else { - ImageFormat::Rgba8Unorm - }, + ImageFormat::Rgba8UnormSrgb, width, height, ))