Skip to content

Commit

Permalink
Update wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Apr 24, 2020
1 parent a1ce095 commit 2599d58
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 285 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ features = ["opengl", "debug"]
[features]
default = []
opengl = ["gfx", "gfx_core", "glutin", "gfx_device_gl", "gfx_glyph"]
vulkan = ["wgpu"]
metal = ["wgpu"]
dx11 = ["wgpu"]
dx12 = ["wgpu"]
vulkan = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
metal = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
dx11 = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
dx12 = ["wgpu", "wgpu_glyph", "zerocopy", "futures"]
debug = []

[dependencies]
Expand All @@ -36,7 +36,6 @@ twox-hash = "1.3"
lyon_tessellation = "0.13"
gilrs = "0.7"
winit = "0.22"
raw-window-handle = "0.1"

# gfx (OpenGL)
gfx = { version = "0.18", optional = true }
Expand All @@ -46,8 +45,10 @@ gfx_glyph = { version = "0.15", optional = true }
glutin = { version = "0.24", optional = true }

# wgpu (Vulkan, Metal, D3D)
wgpu = { version = "0.4", optional = true }
wgpu_glyph = { version = "0.7", optional = true }
wgpu = { version = "0.5", optional = true }
wgpu_glyph = { version = "0.8", optional = true }
zerocopy = { version = "0.3", optional = true }
futures = { version = "0.3", optional = true }

[dev-dependencies]
rand = "0.6"
Expand Down
7 changes: 5 additions & 2 deletions src/graphics/backend_gfx/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use gfx_device_gl as gl;
use gfx_glyph::GlyphCruncher;

use crate::graphics::gpu::{TargetView, Transformation};
use crate::graphics::{HorizontalAlignment, Text, VerticalAlignment};
use crate::graphics::{HorizontalAlignment, Text, Vector, VerticalAlignment};

pub struct Font {
glyphs: gfx_glyph::GlyphBrush<'static, gl::Resources, gl::Factory>,
Expand Down Expand Up @@ -46,7 +46,10 @@ impl Font {

self.glyphs
.use_queue()
.transform(transformation)
.transform(
Transformation::nonuniform_scale(Vector::new(1.0, -1.0))
* transformation,
)
.draw(encoder, &typed_target)
.expect("Font draw");
}
Expand Down
9 changes: 1 addition & 8 deletions src/graphics/backend_gfx/shader/quad.vert
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ layout (std140) uniform Globals {
out vec2 v_Uv;
flat out uint v_Layer;

const mat4 INVERT_Y_AXIS = mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, -1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);

void main() {
v_Uv = a_Pos * a_Src.zw + a_Src.xy;
v_Layer = t_Layer;
Expand All @@ -32,7 +25,7 @@ void main() {
vec4(a_Translation, 0.0, 1.0)
);

vec4 position = INVERT_Y_AXIS * u_MVP * instance_transform * vec4(a_Pos, 0.0, 1.0);
vec4 position = u_MVP * instance_transform * vec4(a_Pos, 0.0, 1.0);

gl_Position = position;
}
9 changes: 1 addition & 8 deletions src/graphics/backend_gfx/shader/triangle.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@ layout (std140) uniform Globals {

out vec4 v_Color;

const mat4 INVERT_Y_AXIS = mat4(
vec4(1.0, 0.0, 0.0, 0.0),
vec4(0.0, -1.0, 0.0, 0.0),
vec4(0.0, 0.0, 1.0, 0.0),
vec4(0.0, 0.0, 0.0, 1.0)
);

void main() {
v_Color = a_Color;

gl_Position = INVERT_Y_AXIS * u_MVP * vec4(a_Pos, 0.0, 1.0);
gl_Position = u_MVP * vec4(a_Pos, 0.0, 1.0);
}
1 change: 1 addition & 0 deletions src/graphics/backend_wgpu/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl Font {
pub fn from_bytes(device: &mut wgpu::Device, bytes: &'static [u8]) -> Font {
Font {
glyphs: wgpu_glyph::GlyphBrushBuilder::using_font_bytes(bytes)
.expect("Load font")
.texture_filter_method(wgpu::FilterMode::Nearest)
.build(device, wgpu::TextureFormat::Bgra8UnormSrgb),
}
Expand Down
54 changes: 37 additions & 17 deletions src/graphics/backend_wgpu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{Error, Result};
#[allow(missing_docs)]
pub struct Gpu {
device: wgpu::Device,
queue: wgpu::Queue,
quad_pipeline: quad::Pipeline,
triangle_pipeline: triangle::Pipeline,
encoder: wgpu::CommandEncoder,
Expand All @@ -33,32 +34,43 @@ impl Gpu {
.build(event_loop)
.map_err(|error| Error::WindowCreation(error.to_string()))?;

let instance = wgpu::Instance::new();

let adapter = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
});

let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
let (mut device, queue) = futures::executor::block_on(async {
let adapter = wgpu::Adapter::request(
&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: None,
},
wgpu::BackendBit::all(),
)
.await
.expect("Request adapter");

let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
})
.await;

(device, queue)
});

let surface = Surface::new(window, &instance, &device);
let surface = Surface::new(window, &device);

let quad_pipeline = quad::Pipeline::new(&mut device);
let triangle_pipeline = triangle::Pipeline::new(&mut device);

let encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
todo: 0,
label: Some("coffee::backend encoder"),
});

Ok((
Gpu {
device,
queue,
quad_pipeline,
triangle_pipeline,
encoder,
Expand Down Expand Up @@ -91,14 +103,19 @@ impl Gpu {
&mut self,
image: &image::DynamicImage,
) -> Texture {
Texture::new(&mut self.device, &self.quad_pipeline, image)
Texture::new(&mut self.device, &self.queue, &self.quad_pipeline, image)
}

pub(super) fn upload_texture_array(
&mut self,
layers: &[image::DynamicImage],
) -> Texture {
Texture::new_array(&mut self.device, &self.quad_pipeline, layers)
Texture::new_array(
&mut self.device,
&self.queue,
&self.quad_pipeline,
layers,
)
}

pub(super) fn create_drawable_texture(
Expand All @@ -108,6 +125,7 @@ impl Gpu {
) -> texture::Drawable {
texture::Drawable::new(
&mut self.device,
&self.queue,
&self.quad_pipeline,
width,
height,
Expand All @@ -119,12 +137,14 @@ impl Gpu {
drawable: &texture::Drawable,
) -> image::DynamicImage {
let new_encoder = self.device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { todo: 0 },
&wgpu::CommandEncoderDescriptor {
label: Some("coffee::backend encoder"),
},
);

let encoder = std::mem::replace(&mut self.encoder, new_encoder);

drawable.read_pixels(&mut self.device, encoder)
drawable.read_pixels(&mut self.device, &self.queue, encoder)
}

pub(super) fn upload_font(&mut self, bytes: &'static [u8]) -> Font {
Expand Down
Loading

0 comments on commit 2599d58

Please sign in to comment.