spirv_tools/val.rs
1#[cfg(feature = "use-compiled-tools")]
2pub mod compiled;
3#[cfg(feature = "use-installed-tools")]
4pub mod tool;
5
6pub use spirv_tools_sys::val::ValidatorLimits;
7
8#[derive(Default, Clone)]
9pub struct ValidatorOptions {
10 /// Record whether or not the validator should relax the rules on types for
11 /// stores to structs. When relaxed, it will allow a type mismatch as long as
12 /// the types are structs with the same layout. Two structs have the same layout
13 /// if
14 ///
15 /// 1) the members of the structs are either the same type or are structs with
16 /// same layout, and
17 ///
18 /// 2) the decorations that affect the memory layout are identical for both
19 /// types. Other decorations are not relevant.
20 pub relax_struct_store: bool,
21 /// Records whether or not the validator should relax the rules on pointer usage
22 /// in logical addressing mode.
23 ///
24 /// When relaxed, it will allow the following usage cases of pointers:
25 /// 1) `OpVariable` allocating an object whose type is a pointer type
26 /// 2) `OpReturnValue` returning a pointer value
27 pub relax_logical_pointer: bool,
28 /// Records whether or not the validator should relax the rules because it is
29 /// expected that the optimizations will make the code legal.
30 ///
31 /// When relaxed, it will allow the following:
32 /// 1) It will allow relaxed logical pointers. Setting this option will also
33 /// set that option.
34 /// 2) Pointers that are pass as parameters to function calls do not have to
35 /// match the storage class of the formal parameter.
36 /// 3) Pointers that are actaul parameters on function calls do not have to point
37 /// to the same type pointed as the formal parameter. The types just need to
38 /// logically match.
39 pub before_legalization: bool,
40 /// Records whether the validator should use "relaxed" block layout rules.
41 /// Relaxed layout rules are described by Vulkan extension
42 /// `VK_KHR_relaxed_block_layout`, and they affect uniform blocks, storage blocks,
43 /// and push constants.
44 ///
45 /// This is enabled by default when targeting Vulkan 1.1 or later.
46 /// Relaxed layout is more permissive than the default rules in Vulkan 1.0.
47 pub relax_block_layout: Option<bool>,
48 /// Records whether the validator should use standard block layout rules for
49 /// uniform blocks.
50 pub uniform_buffer_standard_layout: bool,
51 /// Records whether the validator should use "scalar" block layout rules.
52 /// Scalar layout rules are more permissive than relaxed block layout.
53 ///
54 /// See Vulkan extnesion `VK_EXT_scalar_block_layout`. The scalar alignment is
55 /// defined as follows:
56 /// - scalar alignment of a scalar is the scalar size
57 /// - scalar alignment of a vector is the scalar alignment of its component
58 /// - scalar alignment of a matrix is the scalar alignment of its component
59 /// - scalar alignment of an array is the scalar alignment of its element
60 /// - scalar alignment of a struct is the max scalar alignment among its
61 /// members
62 ///
63 /// For a struct in Uniform, `StorageClass`, or `PushConstant`:
64 /// - a member Offset must be a multiple of the member's scalar alignment
65 /// - `ArrayStride` or `MatrixStride` must be a multiple of the array or matrix
66 /// scalar alignment
67 pub scalar_block_layout: bool,
68 /// Records whether or not the validator should skip validating standard
69 /// uniform/storage block layout.
70 pub skip_block_layout: bool,
71 /// Applies a maximum to one or more Universal limits
72 pub max_limits: Vec<(ValidatorLimits, u32)>,
73}
74
75pub trait Validator: Default {
76 fn with_env(target_env: crate::TargetEnv) -> Self;
77 fn validate(
78 &self,
79 binary: impl AsRef<[u32]>,
80 options: Option<ValidatorOptions>,
81 ) -> Result<(), crate::error::Error>;
82}
83
84pub fn create(te: Option<crate::TargetEnv>) -> impl Validator {
85 let target_env = te.unwrap_or_default();
86
87 #[cfg(feature = "use-compiled-tools")]
88 {
89 compiled::CompiledValidator::with_env(target_env)
90 }
91
92 #[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
93 {
94 tool::ToolValidator::with_env(target_env)
95 }
96}