[][src]Function emu_core::compile::compile

pub fn compile<I: Hash, U: CompileToSpirv<I, P>, P, C: Cache>(
    src: I
) -> Result<SpirvOrFinished<P, C>, CompileError> where
    P: BorrowMut<[u32]>, 

Compiles the given source to SpirvOrFinished

There are 4 things this function is generic over.

  1. The source language which must be Hashable for caching
  2. The compiler implementing CompileToSpirv
  3. The target bytecode, a mutable borrow of a u32 slice
  4. The cache, implementing Cache

The returned SpirvOrFinished is a finished DeviceFnMut if the source was in the cache or just the compiled SPIR-V if not. You can then call finish on the result to finish the compiled SPIR-V to a DeviceFnMut in the case that source wasn't in cache.

Here's how you might use it.

let kernel: Vec<u32> = convert_to_spirv(Cursor::new(vec![
    // Magic number.           Version number: 1.0.
    0x03, 0x02, 0x23, 0x07,    0x00, 0x00, 0x01, 0x00,
    // Generator number: 0.    Bound: 0.
    0x00, 0x00, 0x00, 0x00,    0x00, 0x00, 0x00, 0x00,
    // Reserved word: 0.
    0x00, 0x00, 0x00, 0x00,
    // OpMemoryModel.          Logical.
    0x0e, 0x00, 0x03, 0x00,    0x00, 0x00, 0x00, 0x00,
    // GLSL450.
    0x01, 0x00, 0x00, 0x00]))?;

// later on, if we finish by compiling to a `DeviceFnMut`, it will panic
// at runtime just because our SPIR-V doesn't actually include a main function
let spirv: Spirv<Vec<u32>> = SpirvBuilder::new()
    .set_entry_point_name("main")
    .add_param_mut::<[f32]>()
    .add_param::<[f32]>()
    .set_code_with_u32(kernel)?
    .build();

// in this case, `compile` doesn't do much other than caching
// but where `Spirv` is replaced with `Glsl` or `GlslKernel`,
// actual compilation takes place here
let spirv_or_finished = compile::<Spirv<_>, SpirvCompile, Vec<u32>, GlobalCache>(spirv)?;
// now at this point you can call `finish` to turn `spirv_or_finished` into
// a finished `DeviceFnMut`