wgpu/api/
shader_module.rs1use std::{borrow::Cow, future::Future, marker::PhantomData, sync::Arc, thread};
2
3use crate::*;
4
5#[derive(Debug)]
14pub struct ShaderModule {
15 pub(crate) context: Arc<C>,
16 pub(crate) data: Box<Data>,
17}
18#[cfg(send_sync)]
19static_assertions::assert_impl_all!(ShaderModule: Send, Sync);
20
21super::impl_partialeq_eq_hash!(ShaderModule);
22
23impl Drop for ShaderModule {
24 fn drop(&mut self) {
25 if !thread::panicking() {
26 self.context.shader_module_drop(self.data.as_ref());
27 }
28 }
29}
30
31impl ShaderModule {
32 pub fn get_compilation_info(&self) -> impl Future<Output = CompilationInfo> + WasmNotSend {
34 self.context.shader_get_compilation_info(self.data.as_ref())
35 }
36}
37
38#[derive(Debug, Clone)]
43pub struct CompilationInfo {
44 pub messages: Vec<CompilationMessage>,
46}
47
48#[derive(Debug, Clone)]
53pub struct CompilationMessage {
54 pub message: String,
56 pub message_type: CompilationMessageType,
58 pub location: Option<SourceLocation>,
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum CompilationMessageType {
65 Error,
67 Warning,
69 Info,
71}
72
73#[derive(Copy, Clone, Debug, PartialEq, Eq)]
82pub struct SourceLocation {
83 pub line_number: u32,
85 pub line_position: u32,
88 pub offset: u32,
90 pub length: u32,
92}
93
94#[cfg(all(feature = "wgsl", wgpu_core))]
95impl From<crate::naga::error::ShaderError<crate::naga::front::wgsl::ParseError>>
96 for CompilationInfo
97{
98 fn from(value: crate::naga::error::ShaderError<crate::naga::front::wgsl::ParseError>) -> Self {
99 CompilationInfo {
100 messages: vec![CompilationMessage {
101 message: value.to_string(),
102 message_type: CompilationMessageType::Error,
103 location: value.inner.location(&value.source).map(Into::into),
104 }],
105 }
106 }
107}
108#[cfg(feature = "glsl")]
109impl From<naga::error::ShaderError<naga::front::glsl::ParseErrors>> for CompilationInfo {
110 fn from(value: naga::error::ShaderError<naga::front::glsl::ParseErrors>) -> Self {
111 let messages = value
112 .inner
113 .errors
114 .into_iter()
115 .map(|err| CompilationMessage {
116 message: err.to_string(),
117 message_type: CompilationMessageType::Error,
118 location: err.location(&value.source).map(Into::into),
119 })
120 .collect();
121 CompilationInfo { messages }
122 }
123}
124
125#[cfg(feature = "spirv")]
126impl From<naga::error::ShaderError<naga::front::spv::Error>> for CompilationInfo {
127 fn from(value: naga::error::ShaderError<naga::front::spv::Error>) -> Self {
128 CompilationInfo {
129 messages: vec![CompilationMessage {
130 message: value.to_string(),
131 message_type: CompilationMessageType::Error,
132 location: None,
133 }],
134 }
135 }
136}
137
138#[cfg(any(wgpu_core, naga))]
139impl
140 From<
141 crate::naga::error::ShaderError<crate::naga::WithSpan<crate::naga::valid::ValidationError>>,
142 > for CompilationInfo
143{
144 fn from(
145 value: crate::naga::error::ShaderError<
146 crate::naga::WithSpan<crate::naga::valid::ValidationError>,
147 >,
148 ) -> Self {
149 CompilationInfo {
150 messages: vec![CompilationMessage {
151 message: value.to_string(),
152 message_type: CompilationMessageType::Error,
153 location: value.inner.location(&value.source).map(Into::into),
154 }],
155 }
156 }
157}
158
159#[cfg(any(wgpu_core, naga))]
160impl From<crate::naga::SourceLocation> for SourceLocation {
161 fn from(value: crate::naga::SourceLocation) -> Self {
162 SourceLocation {
163 length: value.length,
164 offset: value.offset,
165 line_number: value.line_number,
166 line_position: value.line_position,
167 }
168 }
169}
170
171#[cfg_attr(feature = "naga-ir", allow(clippy::large_enum_variant))]
181#[derive(Clone, Debug)]
182#[non_exhaustive]
183pub enum ShaderSource<'a> {
184 #[cfg(feature = "spirv")]
188 SpirV(Cow<'a, [u32]>),
189 #[cfg(feature = "glsl")]
193 Glsl {
194 shader: Cow<'a, str>,
196 stage: naga::ShaderStage,
198 defines: naga::FastHashMap<String, String>,
200 },
201 #[cfg(feature = "wgsl")]
203 Wgsl(Cow<'a, str>),
204 #[cfg(feature = "naga-ir")]
206 Naga(Cow<'static, naga::Module>),
207 #[doc(hidden)]
210 Dummy(PhantomData<&'a ()>),
211}
212static_assertions::assert_impl_all!(ShaderSource<'_>: Send, Sync);
213
214#[derive(Clone, Debug)]
219pub struct ShaderModuleDescriptor<'a> {
220 pub label: Label<'a>,
222 pub source: ShaderSource<'a>,
224}
225static_assertions::assert_impl_all!(ShaderModuleDescriptor<'_>: Send, Sync);
226
227#[derive(Debug)]
233pub struct ShaderModuleDescriptorSpirV<'a> {
234 pub label: Label<'a>,
236 pub source: Cow<'a, [u32]>,
238}
239static_assertions::assert_impl_all!(ShaderModuleDescriptorSpirV<'_>: Send, Sync);