Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Naga variant to ShaderSource #2801

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wgpu/examples/boids/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ impl framework::Example for Example {
device: &wgpu::Device,
_queue: &wgpu::Queue,
) -> Self {
let compute_shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let compute_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("compute.wgsl"))),
});
let draw_shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let draw_shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("draw.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl framework::Example for Example {
device: &wgpu::Device,
queue: &wgpu::Queue,
) -> Self {
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
"../../../wgpu-hal/examples/halmark/shader.wgsl"
Expand Down
15 changes: 7 additions & 8 deletions wgpu/examples/conservative-raster/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ impl framework::Example for Example {
push_constant_ranges: &[],
});

let shader_triangle_and_lines =
device.create_shader_module(&wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
"triangle_and_lines.wgsl"
))),
});
let shader_triangle_and_lines = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!(
"triangle_and_lines.wgsl"
))),
});

let pipeline_triangle_conservative =
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
Expand Down Expand Up @@ -197,7 +196,7 @@ impl framework::Example for Example {
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
});
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("upscale.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/cube/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl framework::Example for Example {
label: None,
});

let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/hello-compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn execute_gpu_inner(
numbers: &[u32],
) -> Option<Vec<u32>> {
// Loads the shader from WGSL
let cs_module = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let cs_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/hello-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
.expect("Failed to create device");

// Load the shaders from disk
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/mipmap/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Example {
query_sets: &Option<QuerySets>,
mip_count: u32,
) {
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("blit.wgsl"))),
});
Expand Down Expand Up @@ -272,7 +272,7 @@ impl framework::Example for Example {
});

// Create the render pipeline
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("draw.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/msaa-line/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl framework::Example for Example {
log::info!("Press left/right arrow keys to change sample_count.");
let sample_count = 4;

let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl framework::Example for Example {
attributes: &vertex_attr,
};

let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
2 changes: 1 addition & 1 deletion wgpu/examples/skybox/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl framework::Example for Skybox {
});

// Create the render pipeline
let shader = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("shader.wgsl"))),
});
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/texture-arrays/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl framework::Example for Example {
queue: &wgpu::Queue,
) -> Self {
let mut uniform_workaround = false;
let base_shader_module = device.create_shader_module(&wgpu::include_wgsl!("indexing.wgsl"));
let base_shader_module = device.create_shader_module(wgpu::include_wgsl!("indexing.wgsl"));
let env_override = match std::env::var("WGPU_TEXTURE_ARRAY_STYLE") {
Ok(value) => match &*value.to_lowercase() {
"nonuniform" | "non_uniform" => Some(true),
Expand Down Expand Up @@ -119,7 +119,7 @@ impl framework::Example for Example {
// capabilities even if we don't use it. So for now put it in a separate module.
let fragment_shader_module = if !uniform_workaround {
non_uniform_shader_module =
device.create_shader_module(&wgpu::include_wgsl!("non_uniform_indexing.wgsl"));
device.create_shader_module(wgpu::include_wgsl!("non_uniform_indexing.wgsl"));
&non_uniform_shader_module
} else {
&base_shader_module
Expand Down
4 changes: 2 additions & 2 deletions wgpu/examples/water/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,11 @@ impl framework::Example for Example {
});

// Upload/compile them to GPU code.
let terrain_module = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let terrain_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("terrain"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("terrain.wgsl"))),
});
let water_module = device.create_shader_module(&wgpu::ShaderModuleDescriptor {
let water_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("water"),
source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(include_str!("water.wgsl"))),
});
Expand Down
4 changes: 3 additions & 1 deletion wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ impl crate::Context for Context {
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
desc: &ShaderModuleDescriptor,
desc: ShaderModuleDescriptor,
shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId {
let global = &self.0;
Expand Down Expand Up @@ -1103,6 +1103,8 @@ impl crate::Context for Context {
wgc::pipeline::ShaderModuleSource::Naga(module)
}
ShaderSource::Wgsl(ref code) => wgc::pipeline::ShaderModuleSource::Wgsl(Borrowed(code)),
#[cfg(feature = "naga")]
ShaderSource::Naga(module) => wgc::pipeline::ShaderModuleSource::Naga(module),
};
let (id, error) = wgc::gfx_select!(
device.id => global.device_create_shader_module(device.id, &descriptor, source, PhantomData)
Expand Down
17 changes: 16 additions & 1 deletion wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ impl crate::Context for Context {
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
desc: &crate::ShaderModuleDescriptor,
desc: crate::ShaderModuleDescriptor,
_shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId {
let mut descriptor = match desc.source {
Expand Down Expand Up @@ -1265,6 +1265,21 @@ impl crate::Context for Context {
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
crate::ShaderSource::Wgsl(ref code) => web_sys::GpuShaderModuleDescriptor::new(code),
#[cfg(feature = "naga")]
crate::ShaderSource::Naga(module) => {
use naga::{back, valid};

let mut validator = valid::Validator::new(
valid::ValidationFlags::all(),
valid::Capabilities::all(),
);
let module_info = validator.validate(&module).unwrap();

let writer_flags = naga::back::wgsl::WriterFlags::empty();
let wgsl_text =
back::wgsl::write_string(&module, &module_info, writer_flags).unwrap();
web_sys::GpuShaderModuleDescriptor::new(wgsl_text.as_str())
}
};
if let Some(label) = desc.label {
descriptor.label(label);
Expand Down
10 changes: 7 additions & 3 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ trait Context: Debug + Send + Sized + Sync {
fn device_create_shader_module(
&self,
device: &Self::DeviceId,
desc: &ShaderModuleDescriptor,
desc: ShaderModuleDescriptor,
shader_bound_checks: wgt::ShaderBoundChecks,
) -> Self::ShaderModuleId;
unsafe fn device_create_shader_module_spirv(
Expand Down Expand Up @@ -812,6 +812,10 @@ pub enum ShaderSource<'a> {
},
/// WGSL module as a string slice.
Wgsl(Cow<'a, str>),
/// Naga module.
#[cfg(feature = "naga")]
#[cfg_attr(docsrs, doc(cfg(feature = "naga")))]
Naga(naga::Module),
}

/// Descriptor for use with [`Device::create_shader_module`].
Expand Down Expand Up @@ -1954,7 +1958,7 @@ impl Device {
}

/// Creates a shader module from either SPIR-V or WGSL source code.
pub fn create_shader_module(&self, desc: &ShaderModuleDescriptor) -> ShaderModule {
pub fn create_shader_module(&self, desc: ShaderModuleDescriptor) -> ShaderModule {
ShaderModule {
context: Arc::clone(&self.context),
id: Context::device_create_shader_module(
Expand All @@ -1978,7 +1982,7 @@ impl Device {
/// This has no effect on web.
pub unsafe fn create_shader_module_unchecked(
&self,
desc: &ShaderModuleDescriptor,
desc: ShaderModuleDescriptor,
) -> ShaderModule {
ShaderModule {
context: Arc::clone(&self.context),
Expand Down
2 changes: 1 addition & 1 deletion wgpu/tests/shader_primitive_index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn pulling_common(
) {
let shader = ctx
.device
.create_shader_module(&wgpu::include_wgsl!("primitive_index.wgsl"));
.create_shader_module(wgpu::include_wgsl!("primitive_index.wgsl"));

let two_triangles_xy: [f32; 12] = [
-1.0, -1.0, 0.0, -1.0, -0.5, 0.0, // left triangle, negative x, negative y
Expand Down
2 changes: 1 addition & 1 deletion wgpu/tests/vertex_indices/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn pulling_common(
) {
let shader = ctx
.device
.create_shader_module(&wgpu::include_wgsl!("draw.vert.wgsl"));
.create_shader_module(wgpu::include_wgsl!("draw.vert.wgsl"));

let bgl = ctx
.device
Expand Down