pub struct Decoder<'a> { /* private fields */ }
Expand description
The SPIR-V binary decoder.
Takes in a vector of bytes, and serves requests for raw SPIR-V words or values of a specific SPIR-V enum type. Successful decoding will surely consume the number of words decoded, while unsuccessful decoding may consume any number of bytes.
TODO: The decoder should not conume words if an error occurs.
Different from the Parser
,
this decoder is low-level; it has no knowledge of the SPIR-V grammar.
Given a vector of bytes, it solely responds to word decoding requests
via method calls: both raw words requests and decoding the raw words
into a value of a specific SPIR-V enum type.
It also provides a limit mechanism. Users can set a limit, and then
requesting words. If that limit is reached before the end of the
stream, State::LimitReached
will be
returned.
Errors
For its methods, there can be the following errors:
DecodeError::LimitReached(offset)
if the most recent limit has reached.DecodeError::StreamExpected(offset)
if more bytes are needed to decode the next word.DecodeError::<spirv-enum>Unknown(offset, value)
if failed to decode the next word as the given<spirv-enum>
.
All errors contain the byte offset of the word failed decoding.
Examples
use rspirv::binary::{Decoder, DecodeError};
use spirv::SourceLanguage;
fn main() {
let v = vec![0x12, 0x34, 0x56, 0x78,
0x90, 0xab, 0xcd, 0xef,
0x02, 0x00, 0x00, 0x00];
let mut d = Decoder::new(&v);
assert_eq!(Ok(0x78563412), d.word());
assert_eq!(Ok(0xefcdab90), d.word());
assert_eq!(Ok(SourceLanguage::GLSL), d.source_language());
assert_eq!(Err(DecodeError::StreamExpected(12)), d.word());
}
Implementations§
source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
sourcepub fn word(&mut self) -> Result<Word, DecodeError>
pub fn word(&mut self) -> Result<Word, DecodeError>
Decodes and returns the next raw SPIR-V word.
source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
sourcepub fn set_limit(&mut self, num_words: usize)
pub fn set_limit(&mut self, num_words: usize)
Sets the limit to num_words
words.
The decoder will return State::LimitReached
after num_words
words have been requested, if having not consumed
the whole stream.
sourcepub fn clear_limit(&mut self)
pub fn clear_limit(&mut self)
Clear the previously set limit (if any).
sourcepub fn limit_reached(&self) -> bool
pub fn limit_reached(&self) -> bool
Returns true if the previously set limit has been reached.
This will always return false if no limit has been ever set.
source§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
sourcepub fn id(&mut self) -> Result<Word, DecodeError>
pub fn id(&mut self) -> Result<Word, DecodeError>
Decodes and returns the next SPIR-V word as an id.
sourcepub fn string(&mut self) -> Result<String, DecodeError>
pub fn string(&mut self) -> Result<String, DecodeError>
Decodes and returns a literal string.
This method will consume as many words as necessary until finding a
null character (\0
), or reaching the limit or end of the stream
and erroring out.
sourcepub fn int32(&mut self) -> Result<u32, DecodeError>
pub fn int32(&mut self) -> Result<u32, DecodeError>
Decodes and returns the next SPIR-V word as a 32-bit literal integer.
sourcepub fn int64(&mut self) -> Result<u64, DecodeError>
pub fn int64(&mut self) -> Result<u64, DecodeError>
Decodes and returns the next two SPIR-V words as a 64-bit literal integer.
sourcepub fn float32(&mut self) -> Result<f32, DecodeError>
pub fn float32(&mut self) -> Result<f32, DecodeError>
Decodes and returns the next SPIR-V word as a 32-bit literal floating point number.
sourcepub fn float64(&mut self) -> Result<f64, DecodeError>
pub fn float64(&mut self) -> Result<f64, DecodeError>
Decodes and returns the next two SPIR-V words as a 64-bit literal floating point number.
sourcepub fn ext_inst_integer(&mut self) -> Result<u32, DecodeError>
pub fn ext_inst_integer(&mut self) -> Result<u32, DecodeError>
Decodes and returns the next SPIR-V word as a 32-bit extended-instruction-set number.
§impl<'a> Decoder<'a>
impl<'a> Decoder<'a>
pub fn image_operands(&mut self) -> Result<ImageOperands, DecodeError>
pub fn image_operands(&mut self) -> Result<ImageOperands, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ImageOperands value.
pub fn fp_fast_math_mode(&mut self) -> Result<FPFastMathMode, DecodeError>
pub fn fp_fast_math_mode(&mut self) -> Result<FPFastMathMode, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V FPFastMathMode value.
pub fn selection_control(&mut self) -> Result<SelectionControl, DecodeError>
pub fn selection_control(&mut self) -> Result<SelectionControl, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V SelectionControl value.
pub fn loop_control(&mut self) -> Result<LoopControl, DecodeError>
pub fn loop_control(&mut self) -> Result<LoopControl, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V LoopControl value.
pub fn function_control(&mut self) -> Result<FunctionControl, DecodeError>
pub fn function_control(&mut self) -> Result<FunctionControl, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V FunctionControl value.
pub fn memory_semantics(&mut self) -> Result<MemorySemantics, DecodeError>
pub fn memory_semantics(&mut self) -> Result<MemorySemantics, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V MemorySemantics value.
pub fn memory_access(&mut self) -> Result<MemoryAccess, DecodeError>
pub fn memory_access(&mut self) -> Result<MemoryAccess, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V MemoryAccess value.
pub fn kernel_profiling_info(
&mut self
) -> Result<KernelProfilingInfo, DecodeError>
pub fn kernel_profiling_info( &mut self ) -> Result<KernelProfilingInfo, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V KernelProfilingInfo value.
pub fn ray_flags(&mut self) -> Result<RayFlags, DecodeError>
pub fn ray_flags(&mut self) -> Result<RayFlags, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V RayFlags value.
pub fn fragment_shading_rate(
&mut self
) -> Result<FragmentShadingRate, DecodeError>
pub fn fragment_shading_rate( &mut self ) -> Result<FragmentShadingRate, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V FragmentShadingRate value.
pub fn source_language(&mut self) -> Result<SourceLanguage, DecodeError>
pub fn source_language(&mut self) -> Result<SourceLanguage, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V SourceLanguage value.
pub fn execution_model(&mut self) -> Result<ExecutionModel, DecodeError>
pub fn execution_model(&mut self) -> Result<ExecutionModel, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ExecutionModel value.
pub fn addressing_model(&mut self) -> Result<AddressingModel, DecodeError>
pub fn addressing_model(&mut self) -> Result<AddressingModel, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V AddressingModel value.
pub fn memory_model(&mut self) -> Result<MemoryModel, DecodeError>
pub fn memory_model(&mut self) -> Result<MemoryModel, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V MemoryModel value.
pub fn execution_mode(&mut self) -> Result<ExecutionMode, DecodeError>
pub fn execution_mode(&mut self) -> Result<ExecutionMode, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ExecutionMode value.
pub fn storage_class(&mut self) -> Result<StorageClass, DecodeError>
pub fn storage_class(&mut self) -> Result<StorageClass, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V StorageClass value.
pub fn dim(&mut self) -> Result<Dim, DecodeError>
pub fn dim(&mut self) -> Result<Dim, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V Dim value.
pub fn sampler_addressing_mode(
&mut self
) -> Result<SamplerAddressingMode, DecodeError>
pub fn sampler_addressing_mode( &mut self ) -> Result<SamplerAddressingMode, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V SamplerAddressingMode value.
pub fn sampler_filter_mode(&mut self) -> Result<SamplerFilterMode, DecodeError>
pub fn sampler_filter_mode(&mut self) -> Result<SamplerFilterMode, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V SamplerFilterMode value.
pub fn image_format(&mut self) -> Result<ImageFormat, DecodeError>
pub fn image_format(&mut self) -> Result<ImageFormat, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ImageFormat value.
pub fn image_channel_order(&mut self) -> Result<ImageChannelOrder, DecodeError>
pub fn image_channel_order(&mut self) -> Result<ImageChannelOrder, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ImageChannelOrder value.
pub fn image_channel_data_type(
&mut self
) -> Result<ImageChannelDataType, DecodeError>
pub fn image_channel_data_type( &mut self ) -> Result<ImageChannelDataType, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V ImageChannelDataType value.
pub fn fp_rounding_mode(&mut self) -> Result<FPRoundingMode, DecodeError>
pub fn fp_rounding_mode(&mut self) -> Result<FPRoundingMode, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V FPRoundingMode value.
pub fn linkage_type(&mut self) -> Result<LinkageType, DecodeError>
pub fn linkage_type(&mut self) -> Result<LinkageType, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V LinkageType value.
pub fn access_qualifier(&mut self) -> Result<AccessQualifier, DecodeError>
pub fn access_qualifier(&mut self) -> Result<AccessQualifier, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V AccessQualifier value.
pub fn function_parameter_attribute(
&mut self
) -> Result<FunctionParameterAttribute, DecodeError>
pub fn function_parameter_attribute( &mut self ) -> Result<FunctionParameterAttribute, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V FunctionParameterAttribute value.
pub fn decoration(&mut self) -> Result<Decoration, DecodeError>
pub fn decoration(&mut self) -> Result<Decoration, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V Decoration value.
pub fn built_in(&mut self) -> Result<BuiltIn, DecodeError>
pub fn built_in(&mut self) -> Result<BuiltIn, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V BuiltIn value.
pub fn scope(&mut self) -> Result<Scope, DecodeError>
pub fn scope(&mut self) -> Result<Scope, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V Scope value.
pub fn group_operation(&mut self) -> Result<GroupOperation, DecodeError>
pub fn group_operation(&mut self) -> Result<GroupOperation, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V GroupOperation value.
pub fn kernel_enqueue_flags(
&mut self
) -> Result<KernelEnqueueFlags, DecodeError>
pub fn kernel_enqueue_flags( &mut self ) -> Result<KernelEnqueueFlags, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V KernelEnqueueFlags value.
pub fn capability(&mut self) -> Result<Capability, DecodeError>
pub fn capability(&mut self) -> Result<Capability, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V Capability value.
pub fn ray_query_intersection(
&mut self
) -> Result<RayQueryIntersection, DecodeError>
pub fn ray_query_intersection( &mut self ) -> Result<RayQueryIntersection, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V RayQueryIntersection value.
pub fn ray_query_committed_intersection_type(
&mut self
) -> Result<RayQueryCommittedIntersectionType, DecodeError>
pub fn ray_query_committed_intersection_type( &mut self ) -> Result<RayQueryCommittedIntersectionType, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V RayQueryCommittedIntersectionType value.
pub fn ray_query_candidate_intersection_type(
&mut self
) -> Result<RayQueryCandidateIntersectionType, DecodeError>
pub fn ray_query_candidate_intersection_type( &mut self ) -> Result<RayQueryCandidateIntersectionType, DecodeError>
Decodes and returns the next SPIR-V word as a SPIR-V RayQueryCandidateIntersectionType value.