naga/back/glsl/
conv.rs

1use crate::back::glsl::{BackendResult, Error, VaryingOptions};
2
3/// Structure returned by [`glsl_scalar`]
4///
5/// It contains both a prefix used in other types and the full type name
6pub(in crate::back::glsl) struct ScalarString<'a> {
7    /// The prefix used to compose other types
8    pub prefix: &'a str,
9    /// The name of the scalar type
10    pub full: &'a str,
11}
12
13/// Helper function that returns scalar related strings
14///
15/// Check [`ScalarString`] for the information provided
16///
17/// # Errors
18/// If a [`Float`](crate::ScalarKind::Float) with an width that isn't 4 or 8
19pub(in crate::back::glsl) const fn glsl_scalar(
20    scalar: crate::Scalar,
21) -> Result<ScalarString<'static>, Error> {
22    use crate::ScalarKind as Sk;
23
24    Ok(match scalar.kind {
25        Sk::Sint => ScalarString {
26            prefix: "i",
27            full: "int",
28        },
29        Sk::Uint => ScalarString {
30            prefix: "u",
31            full: "uint",
32        },
33        Sk::Float => match scalar.width {
34            4 => ScalarString {
35                prefix: "",
36                full: "float",
37            },
38            8 => ScalarString {
39                prefix: "d",
40                full: "double",
41            },
42            _ => return Err(Error::UnsupportedScalar(scalar)),
43        },
44        Sk::Bool => ScalarString {
45            prefix: "b",
46            full: "bool",
47        },
48        Sk::AbstractInt | Sk::AbstractFloat => {
49            return Err(Error::UnsupportedScalar(scalar));
50        }
51    })
52}
53
54/// Helper function that returns the glsl variable name for a builtin
55pub(in crate::back::glsl) const fn glsl_built_in(
56    built_in: crate::BuiltIn,
57    options: VaryingOptions,
58) -> &'static str {
59    use crate::BuiltIn as Bi;
60
61    match built_in {
62        Bi::Position { .. } => {
63            if options.output {
64                "gl_Position"
65            } else {
66                "gl_FragCoord"
67            }
68        }
69        Bi::ViewIndex => {
70            if options.targeting_webgl {
71                "gl_ViewID_OVR"
72            } else {
73                "uint(gl_ViewIndex)"
74            }
75        }
76        // vertex
77        Bi::BaseInstance => "uint(gl_BaseInstance)",
78        Bi::BaseVertex => "uint(gl_BaseVertex)",
79        Bi::ClipDistance => "gl_ClipDistance",
80        Bi::CullDistance => "gl_CullDistance",
81        Bi::InstanceIndex => {
82            if options.draw_parameters {
83                "(uint(gl_InstanceID) + uint(gl_BaseInstanceARB))"
84            } else {
85                // Must match FIRST_INSTANCE_BINDING
86                "(uint(gl_InstanceID) + naga_vs_first_instance)"
87            }
88        }
89        Bi::PointSize => "gl_PointSize",
90        Bi::VertexIndex => "uint(gl_VertexID)",
91        Bi::DrawID => "gl_DrawID",
92        // fragment
93        Bi::FragDepth => "gl_FragDepth",
94        Bi::PointCoord => "gl_PointCoord",
95        Bi::FrontFacing => "gl_FrontFacing",
96        Bi::PrimitiveIndex => "uint(gl_PrimitiveID)",
97        Bi::Barycentric => "gl_BaryCoordEXT",
98        Bi::SampleIndex => "gl_SampleID",
99        Bi::SampleMask => {
100            if options.output {
101                "gl_SampleMask"
102            } else {
103                "gl_SampleMaskIn"
104            }
105        }
106        // compute
107        Bi::GlobalInvocationId => "gl_GlobalInvocationID",
108        Bi::LocalInvocationId => "gl_LocalInvocationID",
109        Bi::LocalInvocationIndex => "gl_LocalInvocationIndex",
110        Bi::WorkGroupId => "gl_WorkGroupID",
111        Bi::WorkGroupSize => "gl_WorkGroupSize",
112        Bi::NumWorkGroups => "gl_NumWorkGroups",
113        // subgroup
114        Bi::NumSubgroups => "gl_NumSubgroups",
115        Bi::SubgroupId => "gl_SubgroupID",
116        Bi::SubgroupSize => "gl_SubgroupSize",
117        Bi::SubgroupInvocationId => "gl_SubgroupInvocationID",
118        // mesh
119        // TODO: figure out how to map these to glsl things as glsl treats them as arrays
120        Bi::CullPrimitive
121        | Bi::PointIndex
122        | Bi::LineIndices
123        | Bi::TriangleIndices
124        | Bi::MeshTaskSize
125        | Bi::VertexCount
126        | Bi::PrimitiveCount
127        | Bi::Vertices
128        | Bi::Primitives => {
129            unimplemented!()
130        }
131    }
132}
133
134/// Helper function that returns the string corresponding to the address space
135pub(in crate::back::glsl) const fn glsl_storage_qualifier(
136    space: crate::AddressSpace,
137) -> Option<&'static str> {
138    use crate::AddressSpace as As;
139
140    match space {
141        As::Function => None,
142        As::Private => None,
143        As::Storage { .. } => Some("buffer"),
144        As::Uniform => Some("uniform"),
145        As::Handle => Some("uniform"),
146        As::WorkGroup => Some("shared"),
147        As::Immediate => Some("uniform"),
148        As::TaskPayload => unreachable!(),
149    }
150}
151
152/// Helper function that returns the string corresponding to the glsl interpolation qualifier
153pub(in crate::back::glsl) const fn glsl_interpolation(
154    interpolation: crate::Interpolation,
155) -> &'static str {
156    use crate::Interpolation as I;
157
158    match interpolation {
159        I::Perspective => "smooth",
160        I::Linear => "noperspective",
161        I::Flat => "flat",
162    }
163}
164
165/// Return the GLSL auxiliary qualifier for the given sampling value.
166pub(in crate::back::glsl) const fn glsl_sampling(
167    sampling: crate::Sampling,
168) -> BackendResult<Option<&'static str>> {
169    use crate::Sampling as S;
170
171    Ok(match sampling {
172        S::First => return Err(Error::FirstSamplingNotSupported),
173        S::Center | S::Either => None,
174        S::Centroid => Some("centroid"),
175        S::Sample => Some("sample"),
176    })
177}
178
179/// Helper function that returns the glsl dimension string of [`ImageDimension`](crate::ImageDimension)
180pub(in crate::back::glsl) const fn glsl_dimension(dim: crate::ImageDimension) -> &'static str {
181    use crate::ImageDimension as IDim;
182
183    match dim {
184        IDim::D1 => "1D",
185        IDim::D2 => "2D",
186        IDim::D3 => "3D",
187        IDim::Cube => "Cube",
188    }
189}
190
191/// Helper function that returns the glsl storage format string of [`StorageFormat`](crate::StorageFormat)
192pub(in crate::back::glsl) fn glsl_storage_format(
193    format: crate::StorageFormat,
194) -> Result<&'static str, Error> {
195    use crate::StorageFormat as Sf;
196
197    Ok(match format {
198        Sf::R8Unorm => "r8",
199        Sf::R8Snorm => "r8_snorm",
200        Sf::R8Uint => "r8ui",
201        Sf::R8Sint => "r8i",
202        Sf::R16Uint => "r16ui",
203        Sf::R16Sint => "r16i",
204        Sf::R16Float => "r16f",
205        Sf::Rg8Unorm => "rg8",
206        Sf::Rg8Snorm => "rg8_snorm",
207        Sf::Rg8Uint => "rg8ui",
208        Sf::Rg8Sint => "rg8i",
209        Sf::R32Uint => "r32ui",
210        Sf::R32Sint => "r32i",
211        Sf::R32Float => "r32f",
212        Sf::Rg16Uint => "rg16ui",
213        Sf::Rg16Sint => "rg16i",
214        Sf::Rg16Float => "rg16f",
215        Sf::Rgba8Unorm => "rgba8",
216        Sf::Rgba8Snorm => "rgba8_snorm",
217        Sf::Rgba8Uint => "rgba8ui",
218        Sf::Rgba8Sint => "rgba8i",
219        Sf::Rgb10a2Uint => "rgb10_a2ui",
220        Sf::Rgb10a2Unorm => "rgb10_a2",
221        Sf::Rg11b10Ufloat => "r11f_g11f_b10f",
222        Sf::R64Uint => "r64ui",
223        Sf::Rg32Uint => "rg32ui",
224        Sf::Rg32Sint => "rg32i",
225        Sf::Rg32Float => "rg32f",
226        Sf::Rgba16Uint => "rgba16ui",
227        Sf::Rgba16Sint => "rgba16i",
228        Sf::Rgba16Float => "rgba16f",
229        Sf::Rgba32Uint => "rgba32ui",
230        Sf::Rgba32Sint => "rgba32i",
231        Sf::Rgba32Float => "rgba32f",
232        Sf::R16Unorm => "r16",
233        Sf::R16Snorm => "r16_snorm",
234        Sf::Rg16Unorm => "rg16",
235        Sf::Rg16Snorm => "rg16_snorm",
236        Sf::Rgba16Unorm => "rgba16",
237        Sf::Rgba16Snorm => "rgba16_snorm",
238
239        Sf::Bgra8Unorm => {
240            return Err(Error::Custom(
241                "Support format BGRA8 is not implemented".into(),
242            ))
243        }
244    })
245}