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
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

use vulkano::pipeline::GraphicsPipelineAbstract;
use std::sync::Arc;
use std::collections::{HashSet, HashMap};
use vulkano::device::Device;
use vulkano::framebuffer::{RenderPassAbstract, Subpass};
use vulkano::pipeline::GraphicsPipeline;
use vulkano::pipeline::shader::{GraphicsEntryPoint, ShaderModule, GraphicsShaderType, GeometryShaderExecutionMode, ShaderInterfaceDef};
use shade_runner::{Input, Output, Layout, Entry};
use std::ffi::CStr;
use std::marker::PhantomData;
use vulkano::pipeline::depth_stencil::{DepthStencil, Compare, DepthBounds, Stencil, StencilOp};
use vulkano::pipeline::vertex::{SingleBufferDefinition, VertexDefinition, Vertex};
use shade_runner as sr;
use vulkano::memory::pool::PotentialDedicatedAllocation::Generic;
use vulkano::SafeDeref;
use crate::canvas::managed::shader::shader_common::{ShaderType, CompiledShaderResources, CompiledShader};
use crate::canvas::managed::handles::CompiledShaderHandle;
use crate::canvas::managed::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::managed::ShaderSpecializationConstants;
use crate::util::vertex::{VertexTypes, ColorVertex3D};

/// CanvasShader holds the pipeline and render pass for the input shader source
#[derive(Clone)]
pub struct GenericShader {
    graphics_pipeline: Option<Arc<dyn GraphicsPipelineAbstract + Sync + Send>>,

    handle: Arc<CompiledShaderHandle>,
    name: String,

    device: Arc<Device>,
    renderpass: Arc<dyn RenderPassAbstract + Send + Sync>,
}


impl GenericShader {
}

/// Gives CanvasShader the resource functions
impl CompiledShaderResources for GenericShader {}

/// Convenience interface so we don't have to juggle shader types
impl CompiledShader for GenericShader {

    /// This will explode when the shader does not want to compile
    fn new<V: Vertex>(filename: String,
           device: Arc<Device>,
           handle: Arc<CompiledShaderHandle>,
           render_pass: Arc<dyn RenderPassAbstract + Send + Sync>) -> GenericShader {

        let compiled_vertex = GenericShader::compile(
            GenericShader::get_path(filename.clone(), ShaderType::VERTEX),
            device.clone(), ShaderType::VERTEX
        );

        let vertex_entry_point = unsafe {
            Some(compiled_vertex.1.graphics_entry_point(
                &CStr::from_bytes_with_nul_unchecked(b"main\0"),
                compiled_vertex.0.input.unwrap(),
                compiled_vertex.0.output.unwrap(),
                compiled_vertex.0.layout,
                GenericShader::convert_vk(ShaderType::VERTEX)
            )).unwrap()
        };

        let compiled_fragment = GenericShader::compile(
            GenericShader::get_path(filename.clone(), ShaderType::FRAGMENT).into(),
            device.clone(), ShaderType::FRAGMENT
        );

        let fragment_entry_point = unsafe {
            Some(compiled_fragment.1.graphics_entry_point(
                &CStr::from_bytes_with_nul_unchecked(b"main\0"),
                compiled_fragment.0.input.unwrap(),
                compiled_fragment.0.output.unwrap(),
                compiled_fragment.0.layout,
                GenericShader::convert_vk(ShaderType::FRAGMENT)
            )).unwrap()
        };

        let vertex_definition = RuntimeVertexDef::from_primitive(0);

        GenericShader {
            graphics_pipeline:
            Some(Arc::new(GraphicsPipeline::start()

                .vertex_input(SingleBufferDefinition::<V>::new())
                //.vertex_input(vertex_definition)

                .vertex_shader(vertex_entry_point.clone(), ShaderSpecializationConstants {
                    first_constant: 0,
                    second_constant: 0,
                    third_constant: 0.0,
                })

                .triangle_list()
                // Use a resizable viewport set to draw over the entire window
                .viewports_dynamic_scissors_irrelevant(1)

                .fragment_shader(fragment_entry_point.clone(), ShaderSpecializationConstants {
                    first_constant: 0,
                    second_constant: 0,
                    third_constant: 0.0,
                })

                .depth_stencil_simple_depth()

                // We have to indicate which subpass of which render pass this pipeline is going to be used
                // in. The pipeline will only be usable from this particular subpass.
                .render_pass(Subpass::from(render_pass.clone(), 0).unwrap())

                .build(device.clone())
                .unwrap())),

            device: device,
            handle: handle.clone(),
            name: filename.clone(),
            renderpass: render_pass.clone(),
        }
    }

    fn get_name(&self) -> String {
        self.name.clone()
    }

    fn get_handle(&self) -> Arc<CompiledShaderHandle> {
        self.handle.clone()
    }

    fn get_pipeline(&self) -> Arc<dyn GraphicsPipelineAbstract + Sync + Send> {
        self.graphics_pipeline.clone().unwrap()
    }

    fn get_renderpass(&self) -> Arc<dyn RenderPassAbstract + Send + Sync> {
        self.renderpass.clone()
    }

    fn recompile<V: Vertex>(self, render_pass: Arc<dyn RenderPassAbstract + Send + Sync>) -> GenericShader {
        GenericShader::new::<V>(self.name,
                           self.device,
                           self.handle,
                           render_pass.clone())
    }
}