[rs] Merge #21

21: Add native surface creation r=kvark a=grovesNL

- (API change) Rename `create_surface_with_metal_layer` to `create_surface_from_macos_layer` to match wgpu-native
- Expose `create_surface_from_xlib` and `create_surface_from_windows_hwnd`

Co-authored-by: Joshua Groves <josh@joshgroves.com>
This commit is contained in:
bors[bot] 2019-06-12 03:48:56 +00:00
commit 154ed3a9a5
7 changed files with 110 additions and 84 deletions

View File

@ -63,7 +63,7 @@ fn create_vertices() -> (Vec<Vertex>, Vec<u16>) {
fn create_texels(size: usize) -> Vec<u8> {
use std::iter;
(0..size * size)
(0 .. size * size)
.flat_map(|id| {
// get high five for recognizing this ;)
let cx = 3.0 * (id % size) as f32 / (size - 1) as f32 - 2.0;
@ -216,7 +216,7 @@ impl framework::Example for Example {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &uniform_buf,
range: 0..64,
range: 0 .. 64,
},
},
wgpu::Binding {
@ -231,8 +231,12 @@ impl framework::Example for Example {
});
// Create the render pipeline
let vs_bytes = framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex);
let fs_bytes = framework::load_glsl(include_str!("shader.frag"), framework::ShaderStage::Fragment);
let vs_bytes =
framework::load_glsl(include_str!("shader.vert"), framework::ShaderStage::Vertex);
let fs_bytes = framework::load_glsl(
include_str!("shader.frag"),
framework::ShaderStage::Fragment,
);
let vs_module = device.create_shader_module(&vs_bytes);
let fs_module = device.create_shader_module(&fs_bytes);
@ -335,7 +339,7 @@ impl framework::Example for Example {
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_index_buffer(&self.index_buf, 0);
rpass.set_vertex_buffers(&[(&self.vertex_buf, 0)]);
rpass.draw_indexed(0..self.index_count as u32, 0, 0..1);
rpass.draw_indexed(0 .. self.index_count as u32, 0, 0 .. 1);
}
device.get_queue().submit(&[encoder.finish()]);

View File

@ -62,10 +62,7 @@ pub fn run<E: Example>(title: &str) {
let window = Window::new(&events_loop).unwrap();
window.set_title(title);
let hidpi_factor = window.get_hidpi_factor();
let size = window
.get_inner_size()
.unwrap()
.to_physical(hidpi_factor);
let size = window.get_inner_size().unwrap().to_physical(hidpi_factor);
let surface = instance.create_surface(&window);

View File

@ -58,7 +58,7 @@ fn main() {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &storage_buffer,
range: 0..size,
range: 0 .. size,
},
}],
});

View File

@ -150,7 +150,7 @@ fn main() {
});
rpass.set_pipeline(&render_pipeline);
rpass.set_bind_group(0, &bind_group, &[]);
rpass.draw(0..3, 0..1);
rpass.draw(0 .. 3, 0 .. 1);
}
device.get_queue().submit(&[encoder.finish()]);

View File

@ -1,8 +1,4 @@
use std::{
mem,
ops::Range,
rc::Rc,
};
use std::{mem, ops::Range, rc::Rc};
#[path = "../framework.rs"]
mod framework;
@ -232,7 +228,7 @@ impl framework::Example for Example {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &plane_uniform_buf,
range: 0..entity_uniform_size,
range: 0 .. entity_uniform_size,
},
}],
});
@ -306,7 +302,7 @@ impl framework::Example for Example {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &uniform_buf,
range: 0..entity_uniform_size,
range: 0 .. entity_uniform_size,
},
}],
}),
@ -338,7 +334,7 @@ impl framework::Example for Example {
});
let shadow_view = shadow_texture.create_default_view();
let mut shadow_target_views = (0..2)
let mut shadow_target_views = (0 .. 2)
.map(|i| {
Some(shadow_texture.create_view(&wgpu::TextureViewDescriptor {
format: Self::SHADOW_FORMAT,
@ -361,7 +357,7 @@ impl framework::Example for Example {
a: 1.0,
},
fov: 60.0,
depth: 1.0..20.0,
depth: 1.0 .. 20.0,
target_view: shadow_target_views[0].take().unwrap(),
},
Light {
@ -373,11 +369,12 @@ impl framework::Example for Example {
a: 1.0,
},
fov: 45.0,
depth: 1.0..20.0,
depth: 1.0 .. 20.0,
target_view: shadow_target_views[1].take().unwrap(),
},
];
let light_uniform_size = (Self::MAX_LIGHTS * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
let light_uniform_size =
(Self::MAX_LIGHTS * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
let light_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: light_uniform_size,
usage: wgpu::BufferUsage::UNIFORM
@ -429,20 +426,16 @@ impl framework::Example for Example {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &uniform_buf,
range: 0..uniform_size,
range: 0 .. uniform_size,
},
}],
});
// Create the render pipeline
let vs_bytes = framework::load_glsl(
include_str!("bake.vert"),
framework::ShaderStage::Vertex,
);
let fs_bytes = framework::load_glsl(
include_str!("bake.frag"),
framework::ShaderStage::Fragment,
);
let vs_bytes =
framework::load_glsl(include_str!("bake.vert"), framework::ShaderStage::Vertex);
let fs_bytes =
framework::load_glsl(include_str!("bake.frag"), framework::ShaderStage::Fragment);
let vs_module = device.create_shader_module(&vs_bytes);
let fs_module = device.create_shader_module(&fs_bytes);
@ -493,14 +486,12 @@ impl framework::Example for Example {
bindings: &[
wgpu::BindGroupLayoutBinding {
binding: 0, // global
visibility: wgpu::ShaderStage::VERTEX
| wgpu::ShaderStage::FRAGMENT,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer,
},
wgpu::BindGroupLayoutBinding {
binding: 1, // lights
visibility: wgpu::ShaderStage::VERTEX
| wgpu::ShaderStage::FRAGMENT,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer,
},
wgpu::BindGroupLayoutBinding {
@ -540,14 +531,14 @@ impl framework::Example for Example {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &uniform_buf,
range: 0..uniform_size,
range: 0 .. uniform_size,
},
},
wgpu::Binding {
binding: 1,
resource: wgpu::BindingResource::Buffer {
buffer: &light_uniform_buf,
range: 0..light_uniform_size,
range: 0 .. light_uniform_size,
},
},
wgpu::Binding {
@ -562,10 +553,8 @@ impl framework::Example for Example {
});
// Create the render pipeline
let vs_bytes = framework::load_glsl(
include_str!("forward.vert"),
framework::ShaderStage::Vertex,
);
let vs_bytes =
framework::load_glsl(include_str!("forward.vert"), framework::ShaderStage::Vertex);
let fs_bytes = framework::load_glsl(
include_str!("forward.frag"),
framework::ShaderStage::Fragment,
@ -683,8 +672,8 @@ impl framework::Example for Example {
{
let size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
let temp_buf_data = device
.create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::TRANSFER_SRC);
let temp_buf_data =
device.create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::TRANSFER_SRC);
for (i, entity) in self.entities.iter_mut().enumerate() {
if entity.rotation_speed != 0.0 {
@ -719,8 +708,8 @@ impl framework::Example for Example {
if self.lights_are_dirty {
self.lights_are_dirty = false;
let size = (self.lights.len() * mem::size_of::<LightRaw>()) as wgpu::BufferAddress;
let temp_buf_data = device
.create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::TRANSFER_SRC);
let temp_buf_data =
device.create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::TRANSFER_SRC);
for (i, light) in self.lights.iter().enumerate() {
temp_buf_data.data[i] = light.to_raw();
}
@ -763,7 +752,7 @@ impl framework::Example for Example {
pass.set_bind_group(1, &entity.bind_group, &[]);
pass.set_index_buffer(&entity.index_buf, 0);
pass.set_vertex_buffers(&[(&entity.vertex_buf, 0)]);
pass.draw_indexed(0..entity.index_count as u32, 0, 0..1);
pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1);
}
}
@ -799,7 +788,7 @@ impl framework::Example for Example {
pass.set_bind_group(1, &entity.bind_group, &[]);
pass.set_index_buffer(&entity.index_buf, 0);
pass.set_vertex_buffers(&[(&entity.vertex_buf, 0)]);
pass.draw_indexed(0..entity.index_count as u32, 0, 0..1);
pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1);
}
}

View File

@ -468,7 +468,7 @@ impl Instance {
#[cfg(feature = "gl")]
pub fn new(windowed_context: wgn::glutin::WindowedContext) -> Self {
Instance {
id: wgn::wgpu_create_gl_instance(windowed_context)
id: wgn::wgpu_create_gl_instance(windowed_context),
}
}
@ -502,10 +502,29 @@ impl Instance {
}
}
#[cfg(feature = "metal")]
pub fn create_surface_with_metal_layer(&self, window: *mut std::ffi::c_void) -> Surface {
pub fn create_surface_from_xlib(
&self,
display: *mut *const std::ffi::c_void,
window: u64,
) -> Surface {
Surface {
id: wgn::wgpu_instance_create_surface_from_macos_layer(self.id, window),
id: wgn::wgpu_instance_create_surface_from_xlib(self.id, display, window),
}
}
pub fn create_surface_from_macos_layer(&self, layer: *mut std::ffi::c_void) -> Surface {
Surface {
id: wgn::wgpu_instance_create_surface_from_macos_layer(self.id, layer),
}
}
pub fn create_surface_from_windows_hwnd(
&self,
hinstance: *mut std::ffi::c_void,
hwnd: *mut std::ffi::c_void,
) -> Surface {
Surface {
id: wgn::wgpu_instance_create_surface_from_windows_hwnd(self.id, hinstance, hwnd),
}
}
}
@ -634,16 +653,17 @@ impl Device {
module: desc.vertex_stage.module.id,
entry_point: vertex_entry_point.as_ptr(),
};
let (_fragment_entry_point, fragment_stage) = if let Some(fragment_stage) = &desc.fragment_stage {
let fragment_entry_point = CString::new(fragment_stage.entry_point).unwrap();
let fragment_stage = wgn::PipelineStageDescriptor {
module: fragment_stage.module.id,
entry_point: fragment_entry_point.as_ptr(),
let (_fragment_entry_point, fragment_stage) =
if let Some(fragment_stage) = &desc.fragment_stage {
let fragment_entry_point = CString::new(fragment_stage.entry_point).unwrap();
let fragment_stage = wgn::PipelineStageDescriptor {
module: fragment_stage.module.id,
entry_point: fragment_entry_point.as_ptr(),
};
(fragment_entry_point, Some(fragment_stage))
} else {
(CString::default(), None)
};
(fragment_entry_point, Some(fragment_stage))
} else {
(CString::default(), None)
};
let temp_color_states = desc.color_states.to_vec();
let temp_vertex_buffers = desc
@ -663,7 +683,9 @@ impl Device {
&wgn::RenderPipelineDescriptor {
layout: desc.layout.id,
vertex_stage,
fragment_stage: fragment_stage.as_ref().map_or(ptr::null(), |fs| fs as *const _),
fragment_stage: fragment_stage
.as_ref()
.map_or(ptr::null(), |fs| fs as *const _),
rasterization_state: desc.rasterization_state.clone(),
primitive_topology: desc.primitive_topology,
color_states: temp_color_states.as_ptr(),
@ -1082,7 +1104,12 @@ impl CommandEncoder {
impl<'a> RenderPass<'a> {
/// Sets the active bind group for a given bind group index.
pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[BufferAddress]) {
pub fn set_bind_group(
&mut self,
index: u32,
bind_group: &BindGroup,
offsets: &[BufferAddress],
) {
wgn::wgpu_render_pass_set_bind_group(
self.id,
index,
@ -1174,7 +1201,12 @@ impl<'a> Drop for RenderPass<'a> {
impl<'a> ComputePass<'a> {
/// Sets the active bind group for a given bind group index.
pub fn set_bind_group(&mut self, index: u32, bind_group: &BindGroup, offsets: &[BufferAddress]) {
pub fn set_bind_group(
&mut self,
index: u32,
bind_group: &BindGroup,
offsets: &[BufferAddress],
) {
wgn::wgpu_compute_pass_set_bind_group(
self.id,
index,

View File

@ -1,17 +1,17 @@
#[test]
#[cfg(any(feature = "vulkan", feature = "metal", feature = "dx12"))]
fn multithreaded_compute() {
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use std::sync::mpsc;
let thread_count = 8;
let (tx, rx) = mpsc::channel();
for _ in 0..thread_count {
for _ in 0 .. thread_count {
let tx = tx.clone();
thread::spawn(move || {
let numbers = vec!(100, 100, 100);
let numbers = vec![100, 100, 100];
let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;
@ -45,13 +45,14 @@ fn multithreaded_compute() {
| wgpu::BufferUsage::TRANSFER_SRC,
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
bindings: &[wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer,
}],
});
let bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
bindings: &[wgpu::BindGroupLayoutBinding {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer,
}],
});
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
@ -59,7 +60,7 @@ fn multithreaded_compute() {
binding: 0,
resource: wgpu::BindingResource::Buffer {
buffer: &storage_buffer,
range: 0..size,
range: 0 .. size,
},
}],
});
@ -68,15 +69,17 @@ fn multithreaded_compute() {
bind_group_layouts: &[&bind_group_layout],
});
let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
layout: &pipeline_layout,
compute_stage: wgpu::PipelineStageDescriptor {
module: &cs_module,
entry_point: "main",
},
});
let compute_pipeline =
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
layout: &pipeline_layout,
compute_stage: wgpu::PipelineStageDescriptor {
module: &cs_module,
entry_point: "main",
},
});
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&staging_buffer, 0, &storage_buffer, 0, size);
{
let mut cpass = encoder.begin_compute_pass();
@ -95,7 +98,8 @@ fn multithreaded_compute() {
});
}
for _ in 0..thread_count {
rx.recv_timeout(Duration::from_secs(10)).expect("A thread never completed.");
for _ in 0 .. thread_count {
rx.recv_timeout(Duration::from_secs(10))
.expect("A thread never completed.");
}
}