Skip to content

Commit

Permalink
Merge pull request #340 from kas-gui/wgpu13
Browse files Browse the repository at this point in the history
Update WGPU to v0.13
  • Loading branch information
dhardy committed Jul 28, 2022
2 parents 4a95869 + 28a08cd commit f2c0c6c
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 61 deletions.
2 changes: 1 addition & 1 deletion crates/kas-wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bytemuck = "1.7.0"
futures = "0.3"
log = "0.4"
smallvec = "1.6.1"
wgpu = { version = "0.11.0", features = ["spirv"] }
wgpu = { version = "0.13.0", features = ["spirv"] }
winit = "0.26"
thiserror = "1.0.23"
window_clipboard = { version = "0.2.0", optional = true }
Expand Down
11 changes: 5 additions & 6 deletions crates/kas-wgpu/src/draw/atlases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl<I: bytemuck::Pod> Pipeline<I> {
/// - `tex_format`: texture format
pub fn new(
device: &wgpu::Device,
label: Option<&'static str>,
bg_common: &wgpu::BindGroupLayout,
tex_size: i32,
tex_format: wgpu::TextureFormat,
Expand All @@ -118,10 +119,7 @@ impl<I: bytemuck::Pod> Pipeline<I> {
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Sampler {
filtering: false,
comparison: false,
},
ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering),
count: None,
},
],
Expand All @@ -134,21 +132,22 @@ impl<I: bytemuck::Pod> Pipeline<I> {
});

let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("atlas render pipeline"),
label,
layout: Some(&pipeline_layout),
vertex,
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleStrip,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), // not required
clamp_depth: false,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: Default::default(),
fragment: Some(fragment),
multiview: None,
});

let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
Expand Down
21 changes: 7 additions & 14 deletions crates/kas-wgpu/src/draw/draw_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl<C: CustomPipe> DrawPipe<C> {

// Create staging belt and a local pool
let staging_belt = wgpu::util::StagingBelt::new(1024);
let local_pool = futures::executor::LocalPool::new();

let bgl_common = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("common bind group layout"),
Expand All @@ -38,7 +37,7 @@ impl<C: CustomPipe> DrawPipe<C> {
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None, // TODO
min_binding_size: wgpu::BufferSize::new(16),
},
count: None,
},
Expand All @@ -48,7 +47,7 @@ impl<C: CustomPipe> DrawPipe<C> {
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None, // TODO
min_binding_size: wgpu::BufferSize::new(16),
},
count: None,
},
Expand All @@ -63,7 +62,7 @@ impl<C: CustomPipe> DrawPipe<C> {
let a = (dir.0.sin(), dir.0.cos());
// We normalise intensity:
let f = a.0 / a.1;
let light_norm = [dir.1.sin() * f, -dir.1.cos() * f, 1.0];
let light_norm = [dir.1.sin() * f, -dir.1.cos() * f, 1.0, 0.0];

let light_norm_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("light_norm_buf"),
Expand All @@ -82,7 +81,6 @@ impl<C: CustomPipe> DrawPipe<C> {
DrawPipe {
device,
queue,
local_pool,
staging_belt,
bgl_common,
light_norm_buf,
Expand Down Expand Up @@ -225,14 +223,14 @@ impl<C: CustomPipe> DrawPipe<C> {
.text
.write_buffers(&self.device, &mut self.staging_belt, &mut encoder);

let mut color_attachments = [wgpu::RenderPassColorAttachment {
let mut color_attachments = [Some(wgpu::RenderPassColorAttachment {
view: frame_view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(clear_color),
store: true,
},
}];
})];

// We use a separate render pass for each clipped region.
for (pass, (rect, _)) in window.clip_regions.iter().enumerate() {
Expand Down Expand Up @@ -274,7 +272,7 @@ impl<C: CustomPipe> DrawPipe<C> {
self.text.render(&window.text, pass, &mut rpass, bg_common);
}

color_attachments[0].ops.load = wgpu::LoadOp::Load;
color_attachments[0].as_mut().unwrap().ops.load = wgpu::LoadOp::Load;
}

let size = window.clip_regions[0].0.size;
Expand All @@ -293,12 +291,7 @@ impl<C: CustomPipe> DrawPipe<C> {
self.staging_belt.finish();
self.queue.submit(std::iter::once(encoder.finish()));

use futures::task::SpawnExt;
self.local_pool
.spawner()
.spawn(self.staging_belt.recall())
.expect("Recall staging belt");
self.local_pool.run_until_stalled();
self.staging_belt.recall();
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/kas-wgpu/src/draw/flat_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Pipeline {
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), // not required
clamp_depth: false,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
Expand All @@ -94,12 +94,13 @@ impl Pipeline {
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_flat_round,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
})],
}),
multiview: None,
});

Pipeline { render_pipeline }
Expand Down
5 changes: 3 additions & 2 deletions crates/kas-wgpu/src/draw/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl Images {
) -> Self {
let atlas_pipe = atlases::Pipeline::new(
device,
Some("images pipe"),
bgl_common,
2048,
wgpu::TextureFormat::Rgba8UnormSrgb,
Expand All @@ -109,11 +110,11 @@ impl Images {
wgpu::FragmentState {
module: &shaders.frag_image,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
})],
},
);
Images {
Expand Down
1 change: 0 additions & 1 deletion crates/kas-wgpu/src/draw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type Scale = [f32; 4];
pub struct DrawPipe<C> {
pub(crate) device: wgpu::Device,
queue: wgpu::Queue,
local_pool: futures::executor::LocalPool,
staging_belt: wgpu::util::StagingBelt,
bgl_common: wgpu::BindGroupLayout,
light_norm_buf: wgpu::Buffer,
Expand Down
7 changes: 4 additions & 3 deletions crates/kas-wgpu/src/draw/round_2col.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Pipeline {
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), // not required
clamp_depth: false,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
Expand All @@ -82,12 +82,13 @@ impl Pipeline {
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_round_2col,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
})],
}),
multiview: None,
});

Pipeline { render_pipeline }
Expand Down
7 changes: 4 additions & 3 deletions crates/kas-wgpu/src/draw/shaded_round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Pipeline {
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), // not required
clamp_depth: false,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
Expand All @@ -89,12 +89,13 @@ impl Pipeline {
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_shaded_round,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
})],
}),
multiview: None,
});

Pipeline { render_pipeline }
Expand Down
21 changes: 13 additions & 8 deletions crates/kas-wgpu/src/draw/shaded_square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,39 @@ impl Pipeline {
label: Some("SS render_pipeline"),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shaders.vert_shaded_square,
entry_point: "main",
module: &shaders.shaded_square,
entry_point: "vert",
buffers: &[wgpu::VertexBufferLayout {
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x4, 2 => Float32x2],
attributes: &wgpu::vertex_attr_array![
0 => Float32x2,
1 => Float32x4,
2 => Float32x2,
],
}],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Cw,
cull_mode: Some(wgpu::Face::Back), // not required
clamp_depth: false,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: Default::default(),
fragment: Some(wgpu::FragmentState {
module: &shaders.frag_shaded_square,
entry_point: "main",
targets: &[wgpu::ColorTargetState {
module: &shaders.shaded_square,
entry_point: "frag",
targets: &[Some(wgpu::ColorTargetState {
format: super::RENDER_TEX_FORMAT,
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
})],
}),
multiview: None,
});

Pipeline { render_pipeline }
Expand Down
15 changes: 7 additions & 8 deletions crates/kas-wgpu/src/draw/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,58 @@

//! Shader management

use wgpu::{include_spirv, ShaderModule};
use wgpu::{include_spirv, include_wgsl, ShaderModule};

/// Shader manager
pub struct ShaderManager {
pub vert_flat_round: ShaderModule,
pub vert_round_2col: ShaderModule,
pub vert_shaded_square: ShaderModule,
pub vert_shaded_round: ShaderModule,
pub vert_image: ShaderModule,
pub vert_glyph: ShaderModule,
pub frag_flat_round: ShaderModule,
pub frag_round_2col: ShaderModule,
pub frag_shaded_square: ShaderModule,
pub frag_shaded_round: ShaderModule,
pub frag_image: ShaderModule,
pub frag_glyph: ShaderModule,
pub shaded_square: ShaderModule,
}

macro_rules! create {
($device:ident, $path:expr) => {{
$device.create_shader_module(&include_spirv!($path))
$device.create_shader_module(include_spirv!($path))
}};
}

impl ShaderManager {
pub fn new(device: &wgpu::Device) -> Self {
let vert_flat_round = create!(device, "shaders/flat_round.vert.spv");
let vert_round_2col = create!(device, "shaders/round_2col.vert.spv");
let vert_shaded_square = create!(device, "shaders/shaded_square.vert.spv");
let vert_shaded_round = create!(device, "shaders/shaded_round.vert.spv");
let vert_image = create!(device, "shaders/image.vert.spv");
let vert_glyph = create!(device, "shaders/glyph.vert.spv");

let frag_flat_round = create!(device, "shaders/flat_round.frag.spv");
let frag_round_2col = create!(device, "shaders/round_2col.frag.spv");
let frag_shaded_square = create!(device, "shaders/shaded_square.frag.spv");
let frag_shaded_round = create!(device, "shaders/shaded_round.frag.spv");
let frag_image = create!(device, "shaders/image.frag.spv");
let frag_glyph = create!(device, "shaders/glyph.frag.spv");

let shaded_square =
device.create_shader_module(include_wgsl!("shaders/shaded_square.wgsl"));

ShaderManager {
vert_image,
vert_glyph,
vert_flat_round,
vert_round_2col,
vert_shaded_square,
vert_shaded_round,
frag_flat_round,
frag_round_2col,
frag_shaded_square,
frag_shaded_round,
frag_image,
frag_glyph,
shaded_square,
}
}
}
3 changes: 3 additions & 0 deletions crates/kas-wgpu/src/draw/shaders/shaded_round.frag
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 1) uniform FragCommon {
vec3 lightNorm;
// Note: since WGPU v0.12 this type is interpreted as 16 bytes.
// For compatibility, we explicitly pad to 16 bytes.
float _padding;
};

float sample_a(vec2 dir) {
Expand Down
Binary file modified crates/kas-wgpu/src/draw/shaders/shaded_round.frag.spv
Binary file not shown.
4 changes: 3 additions & 1 deletion crates/kas-wgpu/src/draw/shaders/shaded_square.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ layout(location = 0) out vec4 outColor;

layout(set = 0, binding = 1) uniform FragCommon {
vec3 lightNorm;
// Note: since WGPU v0.12 this type is interpreted as 16 bytes.
// For compatibility, we explicitly pad to 16 bytes.
float _padding;
};


void main() {
float n3 = 1.0 - sqrt(norm2.x * norm2.x + norm2.y * norm2.y);
vec3 norm = vec3(norm2, n3);
Expand Down
Binary file modified crates/kas-wgpu/src/draw/shaders/shaded_square.frag.spv
Binary file not shown.
Loading

0 comments on commit f2c0c6c

Please sign in to comment.