[rs] Update to latest wgpu-native commit

This commit is contained in:
Rukai 2019-08-17 23:35:37 +10:00
parent 85ee7de986
commit c3529869d1
11 changed files with 217 additions and 148 deletions

View File

@ -24,7 +24,7 @@ gl = ["wgn/gfx-backend-gl"]
[dependencies]
#TODO: only depend on the published version
wgn = { package = "wgpu-native", version = "0.2.6", features = ["local", "window-winit"], git = "https://github.com/gfx-rs/wgpu", rev = "cf1bee30d6406d6393220736efdf91b5e429e0ca"}
wgn = { package = "wgpu-native", version = "0.2.6", features = ["local", "window-winit"], git = "https://github.com/gfx-rs/wgpu", rev = "a47ff090bb042f1cb2ad7b13c76eca228390a311"}
arrayvec = "0.4"
zerocopy = "0.2"

View File

@ -9,16 +9,16 @@ fn main() {
let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
});
}));
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
}));
// Rendered image is 256×256 with 32-bit RGBA color
let size = 256u32;
@ -26,7 +26,7 @@ fn main() {
// The output buffer lets us retrieve the data as an array
let output_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size: (size * size) as u64 * size_of::<u32>() as u64,
usage: wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::TRANSFER_DST,
usage: wgpu::BufferUsage::MAP_READ | wgpu::BufferUsage::COPY_DST,
});
let texture_extent = wgpu::Extent3d {
@ -43,7 +43,7 @@ fn main() {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::TRANSFER_SRC,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::COPY_SRC,
});
// Set the background to be red
@ -52,7 +52,7 @@ fn main() {
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: &texture.create_default_view(),
attachment: &texture.create_view(None),
resolve_target: None,
load_op: wgpu::LoadOp::Clear,
store_op: wgpu::StoreOp::Store,
@ -78,7 +78,7 @@ fn main() {
texture_extent,
);
encoder.finish()
encoder.finish(None)
};
device.get_queue().submit(&[command_buffer]);

View File

@ -130,16 +130,25 @@ impl framework::Example for Example {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
],
});
@ -162,11 +171,11 @@ impl framework::Example for Example {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::TRANSFER_DST,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
});
let texture_view = texture.create_default_view();
let texture_view = texture.create_view(None);
let temp_buf = device
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::TRANSFER_SRC)
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(&texels);
init_encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
@ -205,7 +214,7 @@ impl framework::Example for Example {
let uniform_buf = device
.create_buffer_mapped(
16,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
)
.fill_from_slice(mx_ref);
@ -243,21 +252,21 @@ impl framework::Example for Example {
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor {
format: sc_desc.format,
@ -284,10 +293,12 @@ impl framework::Example for Example {
],
}],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
// Done
let init_command_buf = init_encoder.finish();
let init_command_buf = init_encoder.finish(None);
device.get_queue().submit(&[init_command_buf]);
Example {
vertex_buf,
@ -308,13 +319,13 @@ impl framework::Example for Example {
let mx_ref: &[f32; 16] = mx_total.as_ref();
let temp_buf = device
.create_buffer_mapped(16, wgpu::BufferUsage::TRANSFER_SRC)
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(mx_ref);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&temp_buf, 0, &self.uniform_buf, 0, 64);
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
@ -339,11 +350,11 @@ impl framework::Example for Example {
rpass.set_pipeline(&self.pipeline);
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.set_vertex_buffers(0, &[(&self.vertex_buf, 0)]);
rpass.draw_indexed(0 .. self.index_count as u32, 0, 0 .. 1);
}
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
}

View File

@ -94,16 +94,16 @@ pub fn run<E: Example>(title: &str) {
(window, instance, hidpi_factor, size, surface)
};
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
});
}));
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
}));
let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,

View File

@ -15,15 +15,16 @@ fn main() {
let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;
let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
});
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
}));
let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
}));
let cs = include_bytes!("shader.comp.spv");
let cs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
@ -32,16 +33,16 @@ fn main() {
.create_buffer_mapped(
numbers.len(),
wgpu::BufferUsage::MAP_READ
| wgpu::BufferUsage::TRANSFER_DST
| wgpu::BufferUsage::TRANSFER_SRC,
| wgpu::BufferUsage::COPY_DST
| wgpu::BufferUsage::COPY_SRC,
)
.fill_from_slice(&numbers);
let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size,
usage: wgpu::BufferUsage::STORAGE
| wgpu::BufferUsage::TRANSFER_DST
| wgpu::BufferUsage::TRANSFER_SRC,
| wgpu::BufferUsage::COPY_DST
| wgpu::BufferUsage::COPY_SRC,
});
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
@ -49,6 +50,9 @@ fn main() {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
}],
});
@ -69,7 +73,7 @@ fn main() {
let compute_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
layout: &pipeline_layout,
compute_stage: wgpu::PipelineStageDescriptor {
compute_stage: wgpu::ProgrammableStageDescriptor {
module: &cs_module,
entry_point: "main",
},
@ -85,7 +89,7 @@ fn main() {
}
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| {
if let Ok(mapping) = result {

View File

@ -49,16 +49,16 @@ fn main() {
(window, instance, size, surface)
};
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::LowPower,
});
}));
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
}));
let vs = include_bytes!("shader.vert.spv");
let vs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap());
@ -78,21 +78,21 @@ fn main() {
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
@ -104,6 +104,8 @@ fn main() {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
let mut swap_chain = device.create_swap_chain(
@ -157,6 +159,6 @@ fn main() {
rpass.draw(0 .. 3, 0 .. 1);
}
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
}

View File

@ -77,11 +77,17 @@ impl Example {
binding: 0,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
],
});
@ -102,21 +108,21 @@ impl Example {
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleStrip,
color_states: &[wgpu::ColorStateDescriptor {
format: TEXTURE_FORMAT,
@ -128,6 +134,8 @@ impl Example {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
@ -143,15 +151,15 @@ impl Example {
});
let views = (0 .. mip_count)
.map(|mip| texture.create_view(&wgpu::TextureViewDescriptor {
.map(|mip| texture.create_view(Some(&wgpu::TextureViewDescriptor {
format: TEXTURE_FORMAT,
dimension: wgpu::TextureViewDimension::D2,
aspect: wgpu::TextureAspectFlags::COLOR,
aspect: wgpu::TextureAspect::All,
base_mip_level: mip,
level_count: 1,
base_array_layer: 0,
array_count: 1,
}))
array_layer_count: 1,
})))
.collect::<Vec<_>>();
let mut encoder = device.create_command_encoder(
@ -188,7 +196,7 @@ impl Example {
rpass.draw(0 .. 4, 0 .. 1);
}
encoder.finish()
encoder.finish(None)
}
}
@ -213,16 +221,25 @@ impl framework::Example for Example {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
],
});
@ -246,11 +263,11 @@ impl framework::Example for Example {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: TEXTURE_FORMAT,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::TRANSFER_DST,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::COPY_DST,
});
let texture_view = texture.create_default_view();
let texture_view = texture.create_view(None);
let temp_buf = device
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::TRANSFER_SRC)
.create_buffer_mapped(texels.len(), wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(&texels);
init_encoder.copy_buffer_to_texture(
wgpu::BufferCopyView {
@ -289,7 +306,7 @@ impl framework::Example for Example {
let uniform_buf = device
.create_buffer_mapped(
16,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
)
.fill_from_slice(mx_ref);
@ -329,21 +346,21 @@ impl framework::Example for Example {
let draw_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleStrip,
color_states: &[wgpu::ColorStateDescriptor {
format: sc_desc.format,
@ -365,10 +382,12 @@ impl framework::Example for Example {
],
}],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
// Done
let init_command_buf = init_encoder.finish();
let init_command_buf = init_encoder.finish(None);
let mipmap_command_buf = Self::generate_mipmaps(&device, &texture, mip_level_count);
device.get_queue().submit(&[init_command_buf, mipmap_command_buf]);
Example {
@ -388,13 +407,13 @@ impl framework::Example for Example {
let mx_ref: &[f32; 16] = mx_total.as_ref();
let temp_buf = device
.create_buffer_mapped(16, wgpu::BufferUsage::TRANSFER_SRC)
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(mx_ref);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&temp_buf, 0, &self.uniform_buf, 0, 64);
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
@ -418,11 +437,11 @@ impl framework::Example for Example {
});
rpass.set_pipeline(&self.draw_pipeline);
rpass.set_bind_group(0, &self.bind_group, &[]);
rpass.set_vertex_buffers(&[(&self.vertex_buf, 0)]);
rpass.set_vertex_buffers(0, &[(&self.vertex_buf, 0)]);
rpass.draw(0 .. 4, 0 .. 1);
}
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
}

View File

@ -34,21 +34,21 @@ impl Example {
println!("sample_count: {}", sample_count);
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::None,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::LineList,
color_states: &[wgpu::ColorStateDescriptor {
format: sc_desc.format,
@ -75,6 +75,8 @@ impl Example {
],
}],
sample_count,
sample_mask: !0,
alpha_to_coverage_enabled: false,
})
}
@ -94,7 +96,7 @@ impl Example {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
};
device.create_texture(multisampled_frame_descriptor).create_default_view()
device.create_texture(multisampled_frame_descriptor).create_view(None)
}
}
@ -212,11 +214,11 @@ impl framework::Example for Example {
depth_stencil_attachment: None,
});
rpass.set_pipeline(&self.pipeline);
rpass.set_vertex_buffers(&[(&self.vertex_buffer, 0)]);
rpass.set_vertex_buffers(0, &[(&self.vertex_buffer, 0)]);
rpass.draw(0..self.vertex_count, 0..1);
}
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
}

View File

@ -118,7 +118,7 @@ impl Light {
LightRaw {
proj: *mx_view_proj.as_ref(),
pos: [self.pos.x, self.pos.y, self.pos.z, 1.0],
color: [self.color.r, self.color.g, self.color.b, 1.0],
color: [self.color.r as f32, self.color.g as f32, self.color.b as f32, 1.0],
}
}
}
@ -160,13 +160,13 @@ struct Example {
impl Example {
const MAX_LIGHTS: usize = 10;
const SHADOW_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::D32Float;
const SHADOW_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
const SHADOW_SIZE: wgpu::Extent3d = wgpu::Extent3d {
width: 512,
height: 512,
depth: 1,
};
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::D32Float;
const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4<f32> {
let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0);
@ -209,7 +209,7 @@ impl framework::Example for Example {
let entity_uniform_size = mem::size_of::<EntityUniforms>() as wgpu::BufferAddress;
let plane_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: entity_uniform_size,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
let local_bind_group_layout =
@ -218,6 +218,9 @@ impl framework::Example for Example {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
}],
});
@ -289,7 +292,7 @@ impl framework::Example for Example {
};
let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: entity_uniform_size,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
entities.push(Entity {
mx_world: cgmath::Matrix4::from(transform),
@ -334,19 +337,19 @@ impl framework::Example for Example {
format: Self::SHADOW_FORMAT,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT | wgpu::TextureUsage::SAMPLED,
});
let shadow_view = shadow_texture.create_default_view();
let shadow_view = shadow_texture.create_view(None);
let mut shadow_target_views = (0 .. 2)
.map(|i| {
Some(shadow_texture.create_view(&wgpu::TextureViewDescriptor {
Some(shadow_texture.create_view(Some(&wgpu::TextureViewDescriptor {
format: Self::SHADOW_FORMAT,
dimension: wgpu::TextureViewDimension::D2,
aspect: wgpu::TextureAspectFlags::DEPTH,
aspect: wgpu::TextureAspect::DepthOnly,
base_mip_level: 0,
level_count: 1,
base_array_layer: i as u32,
array_count: 1,
}))
array_layer_count: 1,
})))
})
.collect::<Vec<_>>();
let lights = vec![
@ -380,8 +383,8 @@ impl framework::Example for Example {
let light_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: light_uniform_size,
usage: wgpu::BufferUsage::UNIFORM
| wgpu::BufferUsage::TRANSFER_SRC
| wgpu::BufferUsage::TRANSFER_DST,
| wgpu::BufferUsage::COPY_SRC
| wgpu::BufferUsage::COPY_DST,
});
let vb_desc = wgpu::VertexBufferDescriptor {
@ -409,6 +412,9 @@ impl framework::Example for Example {
binding: 0, // global
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
}],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
@ -418,7 +424,7 @@ impl framework::Example for Example {
let uniform_size = mem::size_of::<ShadowUniforms>() as wgpu::BufferAddress;
let uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
size: uniform_size,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
});
// Create bind group
@ -443,21 +449,21 @@ impl framework::Example for Example {
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 2, // corresponds to bilinear filtering
depth_bias_slope_scale: 2.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[],
depth_stencil_state: Some(wgpu::DepthStencilStateDescriptor {
@ -472,6 +478,8 @@ impl framework::Example for Example {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[vb_desc.clone()],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
Pass {
@ -490,21 +498,33 @@ impl framework::Example for Example {
binding: 0, // global
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 1, // lights
visibility: wgpu::ShaderStage::VERTEX | wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::UniformBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 2,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::SampledTexture,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
wgpu::BindGroupLayoutBinding {
binding: 3,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Sampler,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
},
],
});
@ -521,7 +541,7 @@ impl framework::Example for Example {
let uniform_buf = device
.create_buffer_mapped(
1,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::TRANSFER_DST,
wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
)
.fill_from_slice(&[forward_uniforms]);
@ -566,21 +586,21 @@ impl framework::Example for Example {
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout,
vertex_stage: wgpu::PipelineStageDescriptor {
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module,
entry_point: "main",
},
fragment_stage: Some(wgpu::PipelineStageDescriptor {
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fs_module,
entry_point: "main",
}),
rasterization_state: wgpu::RasterizationStateDescriptor {
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::Back,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
},
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor {
format: sc_desc.format,
@ -600,6 +620,8 @@ impl framework::Example for Example {
index_format: wgpu::IndexFormat::Uint16,
vertex_buffers: &[vb_desc],
sample_count: 1,
sample_mask: !0,
alpha_to_coverage_enabled: false,
});
Pass {
@ -629,7 +651,7 @@ impl framework::Example for Example {
lights_are_dirty: true,
shadow_pass,
forward_pass,
forward_depth: depth_texture.create_default_view(),
forward_depth: depth_texture.create_view(None),
light_uniform_buf,
}
}
@ -643,13 +665,13 @@ impl framework::Example for Example {
let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32);
let mx_ref: &[f32; 16] = mx_total.as_ref();
let temp_buf = device
.create_buffer_mapped(16, wgpu::BufferUsage::TRANSFER_SRC)
.create_buffer_mapped(16, wgpu::BufferUsage::COPY_SRC)
.fill_from_slice(mx_ref);
let mut encoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 });
encoder.copy_buffer_to_buffer(&temp_buf, 0, &self.forward_pass.uniform_buf, 0, 64);
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
let depth_texture = device.create_texture(&wgpu::TextureDescriptor {
@ -665,7 +687,7 @@ impl framework::Example for Example {
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
});
self.forward_depth = depth_texture.create_default_view();
self.forward_depth = depth_texture.create_view(None);
}
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device) {
@ -675,7 +697,7 @@ 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);
device.create_buffer_mapped(self.entities.len(), wgpu::BufferUsage::COPY_SRC);
for (i, entity) in self.entities.iter_mut().enumerate() {
if entity.rotation_speed != 0.0 {
@ -686,10 +708,10 @@ impl framework::Example for Example {
temp_buf_data.data[i] = EntityUniforms {
model: entity.mx_world.clone(),
color: [
entity.color.r,
entity.color.g,
entity.color.b,
entity.color.a,
entity.color.r as f32,
entity.color.g as f32,
entity.color.b as f32,
entity.color.a as f32,
],
};
}
@ -711,7 +733,7 @@ impl framework::Example for Example {
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);
device.create_buffer_mapped(self.lights.len(), wgpu::BufferUsage::COPY_SRC);
for (i, light) in self.lights.iter().enumerate() {
temp_buf_data.data[i] = light.to_raw();
}
@ -753,7 +775,7 @@ impl framework::Example for Example {
for entity in &self.entities {
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.set_vertex_buffers(0, &[(&entity.vertex_buf, 0)]);
pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1);
}
}
@ -789,12 +811,12 @@ impl framework::Example for Example {
for entity in &self.entities {
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.set_vertex_buffers(0, &[(&entity.vertex_buf, 0)]);
pass.draw_indexed(0 .. entity.index_count as u32, 0, 0 .. 1);
}
}
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
}
}

View File

@ -11,7 +11,7 @@ use std::slice;
pub use wgn::winit;
pub use wgn::{
AdapterDescriptor,
RequestAdapterOptions,
AddressMode,
BindGroupLayoutBinding,
BindingType,
@ -26,6 +26,7 @@ pub use wgn::{
ColorStateDescriptor,
ColorWrite,
CommandEncoderDescriptor,
CommandBufferDescriptor,
CompareFunction,
CullMode,
DepthStencilStateDescriptor,
@ -52,7 +53,7 @@ pub use wgn::{
StencilStateFaceDescriptor,
StoreOp,
SwapChainDescriptor,
TextureAspectFlags,
TextureAspect,
TextureDescriptor,
TextureDimension,
TextureFormat,
@ -282,7 +283,7 @@ pub struct PipelineLayoutDescriptor<'a> {
}
/// A description of a programmable pipeline stage.
pub struct PipelineStageDescriptor<'a> {
pub struct ProgrammableStageDescriptor<'a> {
/// The compiled shader module for this stage.
pub module: &'a ShaderModule,
@ -308,13 +309,13 @@ pub struct RenderPipelineDescriptor<'a> {
pub layout: &'a PipelineLayout,
/// The compiled vertex stage and its entry point.
pub vertex_stage: PipelineStageDescriptor<'a>,
pub vertex_stage: ProgrammableStageDescriptor<'a>,
/// The compiled fragment stage and its entry point, if any.
pub fragment_stage: Option<PipelineStageDescriptor<'a>>,
pub fragment_stage: Option<ProgrammableStageDescriptor<'a>>,
/// The rasterization process for this pipeline.
pub rasterization_state: RasterizationStateDescriptor,
pub rasterization_state: Option<RasterizationStateDescriptor>,
/// The primitive topology used to interpret vertices.
pub primitive_topology: PrimitiveTopology,
@ -333,6 +334,16 @@ pub struct RenderPipelineDescriptor<'a> {
/// The number of samples calculated per pixel (for MSAA).
pub sample_count: u32,
/// Bitmask that restricts the samples of a pixel modified by this pipeline.
pub sample_mask: u32,
/// When enabled, produces another sample mask per pixel based on the alpha output value, that
/// is ANDed with the sample_mask and the primitive coverage to restrict the set of samples
/// affected by a primitive.
/// The implicit mask produced for alpha of zero is guaranteed to be zero, and for alpha of one
/// is guaranteed to be all 1-s.
pub alpha_to_coverage_enabled: bool,
}
/// A complete description of a compute pipeline.
@ -341,7 +352,7 @@ pub struct ComputePipelineDescriptor<'a> {
pub layout: &'a PipelineLayout,
/// The compiled compute stage and its entry point.
pub compute_stage: PipelineStageDescriptor<'a>,
pub compute_stage: ProgrammableStageDescriptor<'a>,
}
/// A description of all the attachments of a render pass.
@ -485,9 +496,9 @@ impl Instance {
///
/// Panics if there are no available adapters. This will occur if none of the graphics backends
/// are enabled.
pub fn get_adapter(&self, desc: &AdapterDescriptor) -> Adapter {
pub fn get_adapter(&self, options: Option<&RequestAdapterOptions>) -> Adapter {
Adapter {
id: wgn::wgpu_instance_get_adapter(self.id, desc),
id: wgn::wgpu_instance_request_adapter(self.id, options),
}
}
@ -542,7 +553,7 @@ impl Adapter {
/// # Panics
///
/// Panics if the extensions specified by `desc` are not supported by this adapter.
pub fn request_device(&self, desc: &DeviceDescriptor) -> Device {
pub fn request_device(&self, desc: Option<&DeviceDescriptor>) -> Device {
Device {
id: wgn::wgpu_adapter_request_device(self.id, desc),
temp: Temp::default(),
@ -656,14 +667,14 @@ impl Device {
/// Creates a render pipeline.
pub fn create_render_pipeline(&self, desc: &RenderPipelineDescriptor) -> RenderPipeline {
let vertex_entry_point = CString::new(desc.vertex_stage.entry_point).unwrap();
let vertex_stage = wgn::PipelineStageDescriptor {
let vertex_stage = wgn::ProgrammableStageDescriptor {
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 {
let fragment_stage = wgn::ProgrammableStageDescriptor {
module: fragment_stage.module.id,
entry_point: fragment_entry_point.as_ptr(),
};
@ -693,7 +704,9 @@ impl Device {
fragment_stage: fragment_stage
.as_ref()
.map_or(ptr::null(), |fs| fs as *const _),
rasterization_state: desc.rasterization_state.clone(),
rasterization_state: desc.rasterization_state
.as_ref()
.map_or(ptr::null(), |fs| fs as *const _),
primitive_topology: desc.primitive_topology,
color_states: temp_color_states.as_ptr(),
color_states_length: temp_color_states.len(),
@ -707,6 +720,8 @@ impl Device {
vertex_buffers_length: temp_vertex_buffers.len(),
},
sample_count: desc.sample_count,
sample_mask: desc.sample_mask,
alpha_to_coverage_enabled: desc.alpha_to_coverage_enabled,
},
),
}
@ -721,7 +736,7 @@ impl Device {
self.id,
&wgn::ComputePipelineDescriptor {
layout: desc.layout.id,
compute_stage: wgn::PipelineStageDescriptor {
compute_stage: wgn::ProgrammableStageDescriptor {
module: desc.compute_stage.module.id,
entry_point: entry_point.as_ptr(),
},
@ -943,23 +958,12 @@ impl Drop for Buffer {
impl Texture {
/// Creates a view of this texture.
pub fn create_view(&self, desc: &TextureViewDescriptor) -> TextureView {
pub fn create_view(&self, desc: Option<&TextureViewDescriptor>) -> TextureView {
TextureView {
id: wgn::wgpu_texture_create_view(self.id, desc),
owned: true,
}
}
/// Creates the default view of this texture.
///
/// The default view reflects the full dimensions and size of the texture and matches its
/// format.
pub fn create_default_view(&self) -> TextureView {
TextureView {
id: wgn::wgpu_texture_create_default_view(self.id),
owned: true,
}
}
}
impl Drop for Texture {
@ -980,9 +984,9 @@ impl Drop for TextureView {
impl CommandEncoder {
/// Finishes recording and returns a [`CommandBuffer`] that can be submitted for execution.
pub fn finish(self) -> CommandBuffer {
pub fn finish(self, desc: Option<&CommandBufferDescriptor>) -> CommandBuffer {
CommandBuffer {
id: wgn::wgpu_command_encoder_finish(self.id),
id: wgn::wgpu_command_encoder_finish(self.id, desc),
}
}
@ -1145,7 +1149,7 @@ impl<'a> RenderPass<'a> {
///
/// Each element of `buffer_pairs` describes a vertex buffer and an offset in bytes into that
/// buffer. The offset must be aligned to a multiple of 4 bytes.
pub fn set_vertex_buffers(&mut self, buffer_pairs: &[(&Buffer, BufferAddress)]) {
pub fn set_vertex_buffers(&mut self, start_slot: u32, buffer_pairs: &[(&Buffer, BufferAddress)]) {
let mut buffers = Vec::new();
let mut offsets = Vec::new();
for &(buffer, offset) in buffer_pairs {
@ -1154,6 +1158,7 @@ impl<'a> RenderPass<'a> {
}
wgn::wgpu_render_pass_set_vertex_buffers(
self.id,
start_slot,
buffers.as_ptr(),
offsets.as_ptr(),
buffer_pairs.len(),

View File

@ -16,15 +16,16 @@ fn multithreaded_compute() {
let size = (numbers.len() * std::mem::size_of::<u32>()) as wgpu::BufferAddress;
let instance = wgpu::Instance::new();
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
let adapter = instance.get_adapter(Some(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
});
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
}));
let mut device = adapter.request_device(Some(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
});
}));
let cs = include_bytes!("../examples/hello-compute/shader.comp.spv");
let cs_module = device.create_shader_module(&wgpu::read_spirv(std::io::Cursor::new(&cs[..])).unwrap());
@ -33,16 +34,16 @@ fn multithreaded_compute() {
.create_buffer_mapped(
numbers.len(),
wgpu::BufferUsage::MAP_READ
| wgpu::BufferUsage::TRANSFER_DST
| wgpu::BufferUsage::TRANSFER_SRC,
| wgpu::BufferUsage::COPY_DST
| wgpu::BufferUsage::COPY_SRC,
)
.fill_from_slice(&numbers);
let storage_buffer = device.create_buffer(&wgpu::BufferDescriptor {
size,
usage: wgpu::BufferUsage::STORAGE
| wgpu::BufferUsage::TRANSFER_DST
| wgpu::BufferUsage::TRANSFER_SRC,
| wgpu::BufferUsage::COPY_DST
| wgpu::BufferUsage::COPY_SRC,
});
let bind_group_layout =
@ -51,6 +52,9 @@ fn multithreaded_compute() {
binding: 0,
visibility: wgpu::ShaderStage::COMPUTE,
ty: wgpu::BindingType::StorageBuffer,
dynamic: false,
multisampled: false,
texture_dimension: wgn::TextureViewDimension::D2,
}],
});
@ -72,7 +76,7 @@ fn multithreaded_compute() {
let compute_pipeline =
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
layout: &pipeline_layout,
compute_stage: wgpu::PipelineStageDescriptor {
compute_stage: wgpu::ProgrammableStageDescriptor {
module: &cs_module,
entry_point: "main",
},
@ -89,7 +93,7 @@ fn multithreaded_compute() {
}
encoder.copy_buffer_to_buffer(&storage_buffer, 0, &staging_buffer, 0, size);
device.get_queue().submit(&[encoder.finish()]);
device.get_queue().submit(&[encoder.finish(None)]);
staging_buffer.map_read_async(0, size, |result: wgpu::BufferMapAsyncResult<&[u32]>| {
assert_eq!(result.unwrap().data, [25, 25, 25]);