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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use vulkano::pipeline::GraphicsPipelineAbstract;
use std::sync::Arc;
use crate::canvas::shader::common::{ShaderType, CompiledGraphicsPipeline, CompiledGraphicsPipelineResources};
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};
use crate::canvas::shader::ShaderSpecializationConstants;
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, OneVertexOneInstanceDefinition};
use crate::util::vertex_3d::Vertex3D;
use crate::canvas::shader::generic_shader::GenericShader;
use shade_runner as sr;
use crate::canvas::shader::dynamic_vertex::RuntimeVertexDef;
use crate::canvas::CompiledGraphicsPipelineHandle;

#[derive(Default, Debug, Clone, Copy)]
pub struct GlyphInstance {
    pub screen_position: (f32, f32),
    pub atlas_position: (f32, f32),
    pub atlas_size: (f32, f32),
    pub scale: f32,
}
vulkano::impl_vertex!(GlyphInstance, screen_position, atlas_position, atlas_size, scale);

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

    handle: Arc<CompiledGraphicsPipelineHandle>,
    name: String,

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

impl TextShader {}

/// Gives CanvasShader the resource functions
impl CompiledGraphicsPipelineResources for TextShader {}

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

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

        let compiled_vertex = GenericShader::compile(
            GenericShader::get_path(filename.clone(), ShaderType::VERTEX).into(),
            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 stencil = DepthStencil {
            depth_compare: Compare::Less,
            depth_write: true,
            depth_bounds_test: DepthBounds::Disabled,
            stencil_front: Stencil {
                compare: Compare::Equal,
                pass_op: StencilOp::IncrementAndWrap,
                fail_op: StencilOp::DecrementAndClamp,
                depth_fail_op: StencilOp::Keep,
                compare_mask: None,
                write_mask: None,
                reference: None,
            },
            stencil_back: Stencil {
                compare: Compare::Equal,
                pass_op: StencilOp::Invert,
                fail_op: StencilOp::Zero,
                depth_fail_op: StencilOp::Zero,
                compare_mask: None,
                write_mask: None,
                reference: None,
            },
        };

        let vertex_definition = RuntimeVertexDef::from_primitive(0);

        TextShader {
            graphics_pipeline:
            Some(Arc::new(GraphicsPipeline::start()
//OneVertexOneInstanceDefinition::<Vertex3D, GlyphInstance>
                .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(stencil)

                // 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<CompiledGraphicsPipelineHandle> {
        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(self, render_pass: Arc<dyn RenderPassAbstract + Send + Sync>) -> TextShader {
        TextShader::new(self.name,
                        self.device,
                        self.handle,
                        self.renderpass.clone())
    }
}