aboutsummaryrefslogtreecommitdiff
path: root/stockton-render/src/draw/builders/shader.rs
blob: fde185dfbbc7b396d44233c7f3c6eb78fb061ef2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::types::*;

use anyhow::{Context, Result};
use hal::pso::Specialization;
use shaderc::{Compiler, ShaderKind};

#[derive(Debug, Clone)]
pub struct ShaderDesc {
    pub source: String,
    pub entry: String,
    pub kind: ShaderKind,
}

impl ShaderDesc {
    pub fn compile(&self, compiler: &mut Compiler, device: &mut DeviceT) -> Result<ShaderModuleT> {
        let artifact = compiler
            .compile_into_spirv(&self.source, self.kind, "shader", &self.entry, None)
            .context("Shader compilation failed")?;

        // Make into shader module
        Ok(unsafe {
            device
                .create_shader_module(artifact.as_binary())
                .context("Shader module creation failed")?
        })
    }

    pub fn as_entry<'a>(&'a self, module: &'a ShaderModuleT) -> EntryPoint<'a> {
        EntryPoint {
            entry: &self.entry,
            module,
            specialization: Specialization::default(),
        }
    }
}