|
| 1 | +use spirv_tools_sys::{assembler, shared}; |
| 2 | + |
| 3 | +pub struct Binary { |
| 4 | + inner: *mut assembler::Binary, |
| 5 | +} |
| 6 | + |
| 7 | +impl AsRef<[u32]> for Binary { |
| 8 | + fn as_ref(&self) -> &[u32] { |
| 9 | + unsafe { std::slice::from_raw_parts((*self.inner).code, (*self.inner).size) } |
| 10 | + } |
| 11 | +} |
| 12 | + |
| 13 | +impl AsRef<[u8]> for Binary { |
| 14 | + fn as_ref(&self) -> &[u8] { |
| 15 | + unsafe { |
| 16 | + std::slice::from_raw_parts( |
| 17 | + (*self.inner).code as *const u8, |
| 18 | + (*self.inner).size * std::mem::size_of::<u32>(), |
| 19 | + ) |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl Drop for Binary { |
| 25 | + fn drop(&mut self) { |
| 26 | + unsafe { |
| 27 | + assembler::binary_destroy(self.inner); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Copy, Clone, Default)] |
| 33 | +pub struct AssemblerOptions { |
| 34 | + /// Numeric IDs in the binary will have the same values as in the source. |
| 35 | + /// Non-numeric IDs are allocated by filling in the gaps, starting with 1 |
| 36 | + /// and going up. |
| 37 | + pub preserve_numeric_ids: bool, |
| 38 | +} |
| 39 | + |
| 40 | +impl Into<u32> for AssemblerOptions { |
| 41 | + fn into(self) -> u32 { |
| 42 | + // This is weird, the "none" is 1, so I'm not sure if that means having |
| 43 | + // it disables all other options or...? |
| 44 | + let mut res = 0; //assembler::BinaryOptions::None as u32; |
| 45 | + |
| 46 | + if self.preserve_numeric_ids { |
| 47 | + res |= assembler::BinaryOptions::PreserveNumberIds as u32; |
| 48 | + } |
| 49 | + |
| 50 | + res |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +pub struct Assembler { |
| 55 | + inner: *mut shared::ToolContext, |
| 56 | +} |
| 57 | + |
| 58 | +impl Assembler { |
| 59 | + pub fn new(target_env: shared::TargetEnv) -> Self { |
| 60 | + Self { |
| 61 | + inner: unsafe { shared::context_create(target_env) }, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn assemble( |
| 66 | + &self, |
| 67 | + text: &str, |
| 68 | + options: AssemblerOptions, |
| 69 | + ) -> Result<Binary, crate::error::Error> { |
| 70 | + unsafe { |
| 71 | + let mut binary = std::ptr::null_mut(); |
| 72 | + let mut diagnostic = std::ptr::null_mut(); |
| 73 | + |
| 74 | + let res = assembler::assemble( |
| 75 | + self.inner, |
| 76 | + text.as_ptr() as *const _, |
| 77 | + text.len(), |
| 78 | + options.into(), |
| 79 | + &mut binary, |
| 80 | + &mut diagnostic, |
| 81 | + ); |
| 82 | + |
| 83 | + // Always wrap diagnostic, it's fine if it's null |
| 84 | + use std::convert::TryFrom; |
| 85 | + let diagnostic = crate::error::Diagnostic::try_from(diagnostic).ok(); |
| 86 | + |
| 87 | + match res { |
| 88 | + shared::SpirvResult::Success => { |
| 89 | + if binary.is_null() { |
| 90 | + return Err(crate::error::Error { |
| 91 | + inner: shared::SpirvResult::InternalError, |
| 92 | + diagnostic: Some(crate::error::Diagnostic { |
| 93 | + line: 0, |
| 94 | + column: 0, |
| 95 | + index: 0, |
| 96 | + message: "spirv assemble indicated success but did not return a valid binary".to_owned(), |
| 97 | + is_text: true, |
| 98 | + }), |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + Ok(Binary { inner: binary }) |
| 103 | + } |
| 104 | + other => Err(crate::error::Error { |
| 105 | + inner: other, |
| 106 | + diagnostic, |
| 107 | + }), |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Drop for Assembler { |
| 114 | + fn drop(&mut self) { |
| 115 | + unsafe { |
| 116 | + shared::context_destroy(self.inner); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments