use std::io::Cursor;
use std::io::SeekFrom;
use crate::ByteSpan;
use binrw::BinRead;
use binrw::binread;
#[binread]
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum ShaderStage {
#[br(magic = 0u8)]
Vertex,
#[br(magic = 1u8)]
Pixel,
}
#[binread]
#[derive(Debug)]
#[brw(little)]
#[brw(magic = b"ShCd")]
#[allow(dead_code)]
pub struct SHCD {
version: i32,
#[br(count = 4)]
#[bw(pad_size_to = 4)]
#[bw(map = |x : &String | x.as_bytes())]
#[br(map = | x: Vec<u8> | String::from_utf8(x).unwrap().trim_matches(char::from(0)).to_string())]
format: String,
file_length: u32,
shader_offset: u32,
parameter_offset: u32,
#[brw(pad_size_to = 4)]
stage: ShaderStage,
shader_data_length: u32,
#[br(seek_before = SeekFrom::Start(shader_offset as u64))]
#[br(count = shader_data_length)]
#[br(restore_position)]
pub bytecode: Vec<u8>,
}
impl SHCD {
pub fn from_existing(buffer: ByteSpan) -> Option<Self> {
let mut cursor = Cursor::new(buffer);
SHCD::read(&mut cursor).ok()
}
}