use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(C)]
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
pub enum TargetEnv {
Universal_1_0,
Vulkan_1_0,
Universal_1_1,
OpenCL_2_1,
OpenCL_2_2,
OpenGL_4_0,
OpenGL_4_1,
OpenGL_4_2,
OpenGL_4_3,
OpenGL_4_5,
Universal_1_2,
OpenCL_1_2,
OpenCLEmbedded_1_2,
OpenCL_2_0,
OpenCLEmbedded_2_0,
OpenCLEmbedded_2_1,
OpenCLEmbedded_2_2,
Universal_1_3,
Vulkan_1_1,
WebGPU_0,
Universal_1_4,
Vulkan_1_1_Spirv_1_4,
Universal_1_5,
Vulkan_1_2,
}
impl Default for TargetEnv {
fn default() -> Self {
Self::Universal_1_5
}
}
impl std::str::FromStr for TargetEnv {
type Err = SpirvResult;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"vulkan1.1spv1.4" => Self::Vulkan_1_1_Spirv_1_4,
"vulkan1.0" => Self::Vulkan_1_0,
"vulkan1.1" => Self::Vulkan_1_1,
"vulkan1.2" => Self::Vulkan_1_2,
"spv1.0" => Self::Universal_1_0,
"spv1.1" => Self::Universal_1_1,
"spv1.2" => Self::Universal_1_2,
"spv1.3" => Self::Universal_1_3,
"spv1.4" => Self::Universal_1_4,
"spv1.5" => Self::Universal_1_5,
"opencl1.2embedded" => Self::OpenCLEmbedded_1_2,
"opencl1.2" => Self::OpenCL_1_2,
"opencl2.0embedded" => Self::OpenCLEmbedded_2_0,
"opencl2.0" => Self::OpenCL_2_0,
"opencl2.1embedded" => Self::OpenCLEmbedded_2_1,
"opencl2.1" => Self::OpenCL_2_1,
"opencl2.2embedded" => Self::OpenCLEmbedded_2_2,
"opencl2.2" => Self::OpenCL_2_2,
"opengl4.0" => Self::OpenGL_4_0,
"opengl4.1" => Self::OpenGL_4_1,
"opengl4.2" => Self::OpenGL_4_2,
"opengl4.3" => Self::OpenGL_4_3,
"opengl4.5" => Self::OpenGL_4_5,
"webgpu0" => Self::WebGPU_0,
_ => return Err(SpirvResult::InvalidValue),
})
}
}
impl fmt::Display for TargetEnv {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Vulkan_1_1_Spirv_1_4 => "vulkan1.1spv1.4",
Self::Vulkan_1_0 => "vulkan1.0",
Self::Vulkan_1_1 => "vulkan1.1",
Self::Vulkan_1_2 => "vulkan1.2",
Self::Universal_1_0 => "spv1.0",
Self::Universal_1_1 => "spv1.1",
Self::Universal_1_2 => "spv1.2",
Self::Universal_1_3 => "spv1.3",
Self::Universal_1_4 => "spv1.4",
Self::Universal_1_5 => "spv1.5",
Self::OpenCLEmbedded_1_2 => "opencl1.2embedded",
Self::OpenCL_1_2 => "opencl1.2",
Self::OpenCLEmbedded_2_0 => "opencl2.0embedded",
Self::OpenCL_2_0 => "opencl2.0",
Self::OpenCLEmbedded_2_1 => "opencl2.1embedded",
Self::OpenCL_2_1 => "opencl2.1",
Self::OpenCLEmbedded_2_2 => "opencl2.2embedded",
Self::OpenCL_2_2 => "opencl2.2",
Self::OpenGL_4_0 => "opengl4.0",
Self::OpenGL_4_1 => "opengl4.1",
Self::OpenGL_4_2 => "opengl4.2",
Self::OpenGL_4_3 => "opengl4.3",
Self::OpenGL_4_5 => "opengl4.5",
Self::WebGPU_0 => "webgpu0",
})
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(i32)] pub enum SpirvResult {
Success = 0,
Unsupported = 1,
EndOfStream = 2,
Warning = 3,
FailedMatch = 4,
RequestedTermination = 5,
InternalError = -1,
OutOfMemory = -2,
InvalidPointer = -3,
InvalidBinary = -4,
InvalidText = -5,
InvalidTable = -6,
InvalidValue = -7,
InvalidDiagnostic = -8,
InvalidLookup = -9,
InvalidId = -10,
InvalidCfg = -11,
InvalidLayout = -12,
InvalidCapability = -13,
InvalidData = -14,
MissingExtension = -15,
WrongVersion = -16,
}
impl fmt::Display for SpirvResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[allow(clippy::enum_glob_use)]
use SpirvResult::*;
match self {
Success => f.write_str("success"),
Unsupported => f.write_str("unsupported"),
EndOfStream => f.write_str("end of stream"),
Warning => f.write_str("warning"),
FailedMatch => f.write_str("failed match"),
RequestedTermination => f.write_str("requested termination"),
InternalError => f.write_str("internal error"),
OutOfMemory => f.write_str("out of memory"),
InvalidPointer => f.write_str("invalid pointer"),
InvalidBinary => f.write_str("invalid binary"),
InvalidText => f.write_str("invalid text"),
InvalidTable => f.write_str("invalid table"),
InvalidValue => f.write_str("invalid value"),
InvalidDiagnostic => f.write_str("invalid diagnostic"),
InvalidLookup => f.write_str("invalid lookup"),
InvalidId => f.write_str("invalid id"),
InvalidCfg => f.write_str("invalid cfg"),
InvalidLayout => f.write_str("invalid layout"),
InvalidCapability => f.write_str("invalid capability"),
InvalidData => f.write_str("invalid data"),
MissingExtension => f.write_str("missing extension"),
WrongVersion => f.write_str("wrong SPIR-V version"),
}
}
}
impl std::error::Error for SpirvResult {}
#[repr(C)]
pub struct Binary {
pub code: *const u32,
pub size: usize,
}
#[repr(C)]
pub struct ToolContext {
_unused: [u8; 0],
}
extern "C" {
#[link_name = "spvContextCreate"]
pub fn context_create(env: TargetEnv) -> *mut ToolContext;
#[link_name = "spvContextDestroy"]
pub fn context_destroy(opt: *mut ToolContext);
#[link_name = "spvBinaryDestroy"]
pub fn binary_destroy(binary: *mut Binary);
}