summaryrefslogtreecommitdiffstats
path: root/third_party/rust/naga/src/front/spv/null.rs
blob: 85350c563ed863156f28de3901aebf70abd52cda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use super::Error;
use crate::arena::{Arena, Handle, UniqueArena};

const fn make_scalar_inner(kind: crate::ScalarKind, width: crate::Bytes) -> crate::ConstantInner {
    crate::ConstantInner::Scalar {
        width,
        value: match kind {
            crate::ScalarKind::Uint => crate::ScalarValue::Uint(0),
            crate::ScalarKind::Sint => crate::ScalarValue::Sint(0),
            crate::ScalarKind::Float => crate::ScalarValue::Float(0.0),
            crate::ScalarKind::Bool => crate::ScalarValue::Bool(false),
        },
    }
}

pub fn generate_null_constant(
    ty: Handle<crate::Type>,
    type_arena: &UniqueArena<crate::Type>,
    constant_arena: &mut Arena<crate::Constant>,
    span: crate::Span,
) -> Result<crate::ConstantInner, Error> {
    let inner = match type_arena[ty].inner {
        crate::TypeInner::Scalar { kind, width } => make_scalar_inner(kind, width),
        crate::TypeInner::Vector { size, kind, width } => {
            let mut components = Vec::with_capacity(size as usize);
            for _ in 0..size as usize {
                components.push(constant_arena.fetch_or_append(
                    crate::Constant {
                        name: None,
                        specialization: None,
                        inner: make_scalar_inner(kind, width),
                    },
                    span,
                ));
            }
            crate::ConstantInner::Composite { ty, components }
        }
        crate::TypeInner::Matrix {
            columns,
            rows,
            width,
        } => {
            // If we successfully declared a matrix type, we have declared a vector type for it too.
            let vector_ty = type_arena
                .get(&crate::Type {
                    name: None,
                    inner: crate::TypeInner::Vector {
                        kind: crate::ScalarKind::Float,
                        size: rows,
                        width,
                    },
                })
                .unwrap();
            let vector_inner = generate_null_constant(vector_ty, type_arena, constant_arena, span)?;
            let vector_handle = constant_arena.fetch_or_append(
                crate::Constant {
                    name: None,
                    specialization: None,
                    inner: vector_inner,
                },
                span,
            );
            crate::ConstantInner::Composite {
                ty,
                components: vec![vector_handle; columns as usize],
            }
        }
        crate::TypeInner::Struct { ref members, .. } => {
            let mut components = Vec::with_capacity(members.len());
            // copy out the types to avoid borrowing `members`
            let member_tys = members.iter().map(|member| member.ty).collect::<Vec<_>>();
            for member_ty in member_tys {
                let inner = generate_null_constant(member_ty, type_arena, constant_arena, span)?;
                components.push(constant_arena.fetch_or_append(
                    crate::Constant {
                        name: None,
                        specialization: None,
                        inner,
                    },
                    span,
                ));
            }
            crate::ConstantInner::Composite { ty, components }
        }
        crate::TypeInner::Array {
            base,
            size: crate::ArraySize::Constant(handle),
            ..
        } => {
            let size = constant_arena[handle]
                .to_array_length()
                .ok_or(Error::InvalidArraySize(handle))?;
            let inner = generate_null_constant(base, type_arena, constant_arena, span)?;
            let value = constant_arena.fetch_or_append(
                crate::Constant {
                    name: None,
                    specialization: None,
                    inner,
                },
                span,
            );
            crate::ConstantInner::Composite {
                ty,
                components: vec![value; size as usize],
            }
        }
        ref other => {
            log::warn!("null constant type {:?}", other);
            return Err(Error::UnsupportedType(ty));
        }
    };
    Ok(inner)
}

/// Create a default value for an output built-in.
pub fn generate_default_built_in(
    built_in: Option<crate::BuiltIn>,
    ty: Handle<crate::Type>,
    type_arena: &UniqueArena<crate::Type>,
    constant_arena: &mut Arena<crate::Constant>,
    span: crate::Span,
) -> Result<Handle<crate::Constant>, Error> {
    let inner = match built_in {
        Some(crate::BuiltIn::Position { .. }) => {
            let zero = constant_arena.fetch_or_append(
                crate::Constant {
                    name: None,
                    specialization: None,
                    inner: crate::ConstantInner::Scalar {
                        value: crate::ScalarValue::Float(0.0),
                        width: 4,
                    },
                },
                span,
            );
            let one = constant_arena.fetch_or_append(
                crate::Constant {
                    name: None,
                    specialization: None,
                    inner: crate::ConstantInner::Scalar {
                        value: crate::ScalarValue::Float(1.0),
                        width: 4,
                    },
                },
                span,
            );
            crate::ConstantInner::Composite {
                ty,
                components: vec![zero, zero, zero, one],
            }
        }
        Some(crate::BuiltIn::PointSize) => crate::ConstantInner::Scalar {
            value: crate::ScalarValue::Float(1.0),
            width: 4,
        },
        Some(crate::BuiltIn::FragDepth) => crate::ConstantInner::Scalar {
            value: crate::ScalarValue::Float(0.0),
            width: 4,
        },
        Some(crate::BuiltIn::SampleMask) => crate::ConstantInner::Scalar {
            value: crate::ScalarValue::Uint(u64::MAX),
            width: 4,
        },
        //Note: `crate::BuiltIn::ClipDistance` is intentionally left for the default path
        _ => generate_null_constant(ty, type_arena, constant_arena, span)?,
    };
    Ok(constant_arena.fetch_or_append(
        crate::Constant {
            name: None,
            specialization: None,
            inner,
        },
        span,
    ))
}