Skip to content

Commit 342f3de

Browse files
committed
Fixup
1 parent ae32489 commit 342f3de

File tree

8 files changed

+308
-58
lines changed

8 files changed

+308
-58
lines changed

spirv-tools-sys/build.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ fn opt(build: &mut Build) {
170170
}
171171

172172
fn val(build: &mut Build) {
173-
build.file("src/c/val.cpp");
174-
175173
add_sources(
176174
build,
177175
"spirv-tools/source/val",
@@ -242,6 +240,22 @@ fn main() {
242240
opt(&mut build);
243241
}
244242

243+
build.define("SPIRV_CHECK_CONTEXT", None);
244+
245+
let target_def = match std::env::var("CARGO_CFG_TARGET_OS")
246+
.expect("CARGO_CFG_TARGET_OS not set")
247+
.as_str()
248+
{
249+
"linux" => "SPIRV_LINUX",
250+
"windows" => "SPIRV_WINDOWS",
251+
"darwin" => "SPIRV_MAC",
252+
android if android.starts_with("android") => "SPIRV_ANDROID",
253+
"freebsd" => "SPIRV_FREEBSD",
254+
other => panic!("unsupported target os '{}'", other),
255+
};
256+
257+
build.define(target_def, None);
258+
245259
let compiler = build.get_compiler();
246260

247261
if compiler.is_like_gnu() {
@@ -259,8 +273,7 @@ fn main() {
259273
.flag("-Wundef")
260274
.flag("-Wconversion")
261275
.flag("-Wno-sign-conversion")
262-
.flag("-std=gnu++11")
263-
.flag("-Wno-error=switch");
276+
.flag("-std=gnu++11");
264277
} else if compiler.is_like_clang() {
265278
build
266279
.flag("-Wextra-semi")
@@ -279,8 +292,7 @@ fn main() {
279292
.flag("-Wconversion")
280293
.flag("-Wno-sign-conversion")
281294
.flag("-ftemplate-depth=1024")
282-
.flag("-std=gnu++11")
283-
.flag("-Wno-error=switch");
295+
.flag("-std=gnu++11");
284296
}
285297

286298
build.cpp(true);

spirv-tools-sys/generate.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,14 @@ fn python<S: AsRef<std::ffi::OsStr>>(args: impl IntoIterator<Item = S>) -> Resul
2222
fn main() {
2323
fs::create_dir_all("generated").expect("unable to create 'generated'");
2424

25-
python(&[
26-
"spirv-tools/utils/generate_grammar_tables.py",
27-
"--spirv-core-grammar=spirv-headers/include/spirv/unified1/spirv.core.grammar.json",
28-
"--extinst-debuginfo-grammar=spirv-headers/include/spirv/unified1/extinst.debuginfo.grammar.json",
29-
"--extinst-cldebuginfo100-grammar=spirv-headers/include/spirv/unified1/extinst.opencl.debuginfo.100.grammar.json",
30-
"--extension-enum-output=generated/extension_enum.inc",
31-
"--enum-string-mapping-output=generated/enum_string_mapping.inc",
32-
]).expect("failed to generate enum includes from spirv-headers");
33-
3425
python(&[
3526
"spirv-tools/utils/update_build_version.py",
3627
"spirv-tools",
3728
"generated/build-version.inc",
3829
])
3930
.expect("failed to generate build version from spirv-headers");
4031

32+
enum_string_mapping("unified1");
4133
core_table("unified1");
4234
glsl_table("unified1");
4335
opencl_table("unified1");
@@ -53,6 +45,17 @@ fn main() {
5345
registry_table();
5446
}
5547

48+
fn enum_string_mapping(version: &str) {
49+
python(&[
50+
"spirv-tools/utils/generate_grammar_tables.py".to_owned(),
51+
format!("--spirv-core-grammar=spirv-headers/include/spirv/{}/spirv.core.grammar.json", version),
52+
"--extinst-debuginfo-grammar=spirv-headers/include/spirv/unified1/extinst.debuginfo.grammar.json".to_owned(),
53+
"--extinst-cldebuginfo100-grammar=spirv-headers/include/spirv/unified1/extinst.opencl.debuginfo.100.grammar.json".to_owned(),
54+
"--extension-enum-output=generated/extension_enum.inc".to_owned(),
55+
"--enum-string-mapping-output=generated/enum_string_mapping.inc".to_owned(),
56+
]).expect("failed to generate enum includes from spirv-headers");
57+
}
58+
5659
fn vendor_table(which: &str, prefix: Option<&str>) {
5760
python(&[
5861
"spirv-tools/utils/generate_grammar_tables.py".to_owned(),

spirv-tools-sys/src/c/val.cpp

-26
This file was deleted.

spirv-tools-sys/src/shared.rs

+168-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
/// Certain target environments impose additional restrictions on SPIR-V, so it's
24
/// often necessary to specify which one applies. `Universal_*` implies an
35
/// environment-agnostic SPIR-V.
@@ -11,7 +13,7 @@
1113
/// SPV_ENV_VULKAN_1_2 -> SPIR-V 1.5
1214
///
1315
/// Consult the description of API entry points for specific rules.
14-
#[derive(Copy, Clone, Debug)]
16+
#[derive(Copy, Clone, Debug, PartialEq)]
1517
#[repr(C)]
1618
#[allow(non_camel_case_types)]
1719
pub enum TargetEnv {
@@ -65,14 +67,176 @@ pub enum TargetEnv {
6567
Vulkan_1_2,
6668
}
6769

70+
impl Default for TargetEnv {
71+
fn default() -> Self {
72+
// This is the default target environment for (AFAICT) all spirv-tools
73+
Self::Universal_1_5
74+
}
75+
}
76+
77+
impl std::str::FromStr for TargetEnv {
78+
type Err = SpirvResult;
79+
80+
fn from_str(s: &str) -> Result<Self, Self::Err> {
81+
Ok(match s {
82+
"vulkan1.1spv1.4" => Self::Vulkan_1_1_Spirv_1_4,
83+
"vulkan1.0" => Self::Vulkan_1_0,
84+
"vulkan1.1" => Self::Vulkan_1_1,
85+
"vulkan1.2" => Self::Vulkan_1_2,
86+
"spv1.0" => Self::Universal_1_0,
87+
"spv1.1" => Self::Universal_1_1,
88+
"spv1.2" => Self::Universal_1_2,
89+
"spv1.3" => Self::Universal_1_3,
90+
"spv1.4" => Self::Universal_1_4,
91+
"spv1.5" => Self::Universal_1_5,
92+
"opencl1.2embedded" => Self::OpenCLEmbedded_1_2,
93+
"opencl1.2" => Self::OpenCL_1_2,
94+
"opencl2.0embedded" => Self::OpenCLEmbedded_2_0,
95+
"opencl2.0" => Self::OpenCL_2_0,
96+
"opencl2.1embedded" => Self::OpenCLEmbedded_2_1,
97+
"opencl2.1" => Self::OpenCL_2_1,
98+
"opencl2.2embedded" => Self::OpenCLEmbedded_2_2,
99+
"opencl2.2" => Self::OpenCL_2_2,
100+
"opengl4.0" => Self::OpenGL_4_0,
101+
"opengl4.1" => Self::OpenGL_4_1,
102+
"opengl4.2" => Self::OpenGL_4_2,
103+
"opengl4.3" => Self::OpenGL_4_3,
104+
"opengl4.5" => Self::OpenGL_4_5,
105+
"webgpu0" => Self::WebGPU_0,
106+
_ => return Err(SpirvResult::InvalidValue),
107+
})
108+
}
109+
}
110+
111+
impl fmt::Display for TargetEnv {
112+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113+
f.write_str(match self {
114+
Self::Vulkan_1_1_Spirv_1_4 => "vulkan1.1spv1.4",
115+
Self::Vulkan_1_0 => "vulkan1.0",
116+
Self::Vulkan_1_1 => "vulkan1.1",
117+
Self::Vulkan_1_2 => "vulkan1.2",
118+
Self::Universal_1_0 => "spv1.0",
119+
Self::Universal_1_1 => "spv1.1",
120+
Self::Universal_1_2 => "spv1.2",
121+
Self::Universal_1_3 => "spv1.3",
122+
Self::Universal_1_4 => "spv1.4",
123+
Self::Universal_1_5 => "spv1.5",
124+
Self::OpenCLEmbedded_1_2 => "opencl1.2embedded",
125+
Self::OpenCL_1_2 => "opencl1.2",
126+
Self::OpenCLEmbedded_2_0 => "opencl2.0embedded",
127+
Self::OpenCL_2_0 => "opencl2.0",
128+
Self::OpenCLEmbedded_2_1 => "opencl2.1embedded",
129+
Self::OpenCL_2_1 => "opencl2.1",
130+
Self::OpenCLEmbedded_2_2 => "opencl2.2embedded",
131+
Self::OpenCL_2_2 => "opencl2.2",
132+
Self::OpenGL_4_0 => "opengl4.0",
133+
Self::OpenGL_4_1 => "opengl4.1",
134+
Self::OpenGL_4_2 => "opengl4.2",
135+
Self::OpenGL_4_3 => "opengl4.3",
136+
Self::OpenGL_4_5 => "opengl4.5",
137+
Self::WebGPU_0 => "webgpu0",
138+
})
139+
}
140+
}
141+
142+
#[derive(Copy, Clone, Debug, PartialEq)]
143+
#[repr(i32)] // SPV_FORCE_32_BIT_ENUM
144+
pub enum SpirvResult {
145+
Success = 0,
146+
Unsupported = 1,
147+
EndOfStream = 2,
148+
Warning = 3,
149+
FailedMatch = 4,
150+
/// Success, but signals early termination.
151+
RequestedTermination = 5,
152+
InternalError = -1,
153+
OutOfMemory = -2,
154+
InvalidPointer = -3,
155+
InvalidBinary = -4,
156+
InvalidText = -5,
157+
InvalidTable = -6,
158+
InvalidValue = -7,
159+
InvalidDiagnostic = -8,
160+
InvalidLookup = -9,
161+
InvalidId = -10,
162+
InvalidCfg = -11,
163+
InvalidLayout = -12,
164+
InvalidCapability = -13,
165+
/// Indicates data rules validation failure.
166+
InvalidData = -14,
167+
MissingExtension = -15,
168+
/// Indicates wrong SPIR-V version
169+
WrongVersion = -16,
170+
}
171+
172+
impl fmt::Display for SpirvResult {
173+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174+
use SpirvResult::*;
175+
176+
match self {
177+
Success => f.write_str("success"),
178+
Unsupported => f.write_str("unsupported"),
179+
EndOfStream => f.write_str("end of stream"),
180+
Warning => f.write_str("warning"),
181+
FailedMatch => f.write_str("failed match"),
182+
RequestedTermination => f.write_str("requested termination"),
183+
InternalError => f.write_str("internal error"),
184+
OutOfMemory => f.write_str("out of memory"),
185+
InvalidPointer => f.write_str("invalid pointer"),
186+
InvalidBinary => f.write_str("invalid binary"),
187+
InvalidText => f.write_str("invalid text"),
188+
InvalidTable => f.write_str("invalid table"),
189+
InvalidValue => f.write_str("invalid value"),
190+
InvalidDiagnostic => f.write_str("invalid diagnostic"),
191+
InvalidLookup => f.write_str("invalid lookup"),
192+
InvalidId => f.write_str("invalid id"),
193+
InvalidCfg => f.write_str("invalid cfg"),
194+
InvalidLayout => f.write_str("invalid layout"),
195+
InvalidCapability => f.write_str("invalid capability"),
196+
InvalidData => f.write_str("invalid data"),
197+
MissingExtension => f.write_str("missing extension"),
198+
WrongVersion => f.write_str("wrong SPIR-V version"),
199+
}
200+
}
201+
}
202+
203+
impl std::error::Error for SpirvResult {}
204+
68205
#[cfg(feature = "tools")]
69206
#[repr(C)]
70-
pub struct Tool {
207+
pub struct ToolContext {
71208
_unused: [u8; 0],
72209
}
73210

211+
#[repr(C)]
212+
pub struct Position {
213+
pub line: usize,
214+
pub column: usize,
215+
pub index: usize,
216+
}
217+
218+
#[repr(C)]
219+
pub struct Diagnostic {
220+
pub position: Position,
221+
pub error: *const std::os::raw::c_char,
222+
pub is_text_source: bool,
223+
}
224+
74225
#[cfg(feature = "tools")]
75226
extern "C" {
76-
pub fn tool_create(env: TargetEnv) -> *mut Tool;
77-
pub fn tool_destroy(opt: *mut Tool);
227+
/// Creates a context object for most of the SPIRV-Tools API.
228+
/// Returns null if env is invalid.
229+
///
230+
/// See specific API calls for how the target environment is interpeted
231+
/// (particularly assembly and validation).
232+
#[link_name = "spvContextCreate"]
233+
pub fn context_create(env: TargetEnv) -> *mut ToolContext;
234+
/// Destroys the given context object.
235+
#[link_name = "spvContextDestroy"]
236+
pub fn context_destroy(opt: *mut ToolContext);
237+
238+
/// Destroys a diagnostic object. This is a no-op if diagnostic is a null
239+
/// pointer.
240+
#[link_name = "spvDiagnosticDestroy"]
241+
pub fn diagnostic_destroy(diag: *mut Diagnostic);
78242
}

spirv-tools-sys/src/val.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::shared;
2+
13
#[repr(C)]
24
pub struct ValidatorOptions {
35
_unused: [u8; 0],
@@ -18,12 +20,17 @@ pub enum ValidatorLimits {
1820
}
1921

2022
extern "C" {
23+
/// Validates a raw SPIR-V binary for correctness. Any errors will be written
24+
/// into *diagnostic if diagnostic is non-null, otherwise the context's message
25+
/// consumer will be used.
26+
#[link_name = "spvValidateBinary"]
2127
pub fn validate(
22-
tool: *const crate::shared::Tool,
28+
tool: *const shared::ToolContext,
2329
binary: *const u32,
2430
size: usize,
2531
options: *const ValidatorOptions,
26-
) -> bool;
32+
diagnostic: *mut *mut shared::Diagnostic,
33+
) -> crate::shared::SpirvResult;
2734

2835
/// Creates a Validator options object with default options. Returns a valid
2936
/// options object. The object remains valid until it is passed into

0 commit comments

Comments
 (0)