276 lignes
9.7 KiB
Rust
276 lignes
9.7 KiB
Rust
use std::sync::Arc;
|
|
use vulkano::device::Device;
|
|
use vulkano::pipeline::graphics::color_blend::{ColorBlendAttachmentState, ColorBlendState};
|
|
use vulkano::pipeline::graphics::depth_stencil::{CompareOp, DepthState, DepthStencilState};
|
|
use vulkano::pipeline::graphics::input_assembly::{InputAssemblyState, PrimitiveTopology};
|
|
use vulkano::pipeline::graphics::multisample::MultisampleState;
|
|
use vulkano::pipeline::graphics::rasterization::{CullMode, FrontFace, RasterizationState};
|
|
use vulkano::pipeline::graphics::vertex_input::{Vertex, VertexDefinition};
|
|
use vulkano::pipeline::graphics::viewport::ViewportState;
|
|
use vulkano::pipeline::graphics::GraphicsPipelineCreateInfo;
|
|
use vulkano::pipeline::layout::PipelineDescriptorSetLayoutCreateInfo;
|
|
use vulkano::pipeline::{DynamicState, GraphicsPipeline, PipelineLayout, PipelineShaderStageCreateInfo};
|
|
use vulkano::render_pass::Subpass;
|
|
|
|
use super::mesh::CubeVertex;
|
|
|
|
pub mod vs {
|
|
vulkano_shaders::shader! {
|
|
ty: "vertex",
|
|
src: r"
|
|
#version 450
|
|
layout(location = 0) in vec3 position;
|
|
layout(location = 1) in vec3 color;
|
|
layout(location = 2) in vec3 normal;
|
|
layout(location = 3) in vec2 uv;
|
|
|
|
layout(push_constant) uniform PushConstants {
|
|
mat4 mvp;
|
|
} pc;
|
|
|
|
layout(location = 0) out vec3 v_color;
|
|
layout(location = 1) out vec3 v_normal;
|
|
layout(location = 2) out vec2 v_uv;
|
|
|
|
void main() {
|
|
gl_Position = pc.mvp * vec4(position, 1.0);
|
|
v_color = color;
|
|
v_normal = normal;
|
|
v_uv = uv;
|
|
}
|
|
",
|
|
}
|
|
}
|
|
|
|
pub mod fs {
|
|
vulkano_shaders::shader! {
|
|
ty: "fragment",
|
|
src: r"
|
|
#version 450
|
|
layout(location = 0) in vec3 v_color;
|
|
layout(location = 1) in vec3 v_normal;
|
|
layout(location = 2) in vec2 v_uv;
|
|
|
|
layout(set = 0, binding = 0) uniform sampler2D tex_sampler;
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
void main() {
|
|
// Éclairage diffus direct (type soleil de Minecraft)
|
|
vec3 light_dir = normalize(vec3(0.4, 0.9, 0.3));
|
|
float diff = max(dot(normalize(v_normal), light_dir), 0.35);
|
|
vec4 tex_color = texture(tex_sampler, v_uv);
|
|
f_color = vec4(tex_color.rgb * v_color * diff, tex_color.a);
|
|
}
|
|
",
|
|
}
|
|
}
|
|
|
|
pub mod line_fs {
|
|
vulkano_shaders::shader! {
|
|
ty: "fragment",
|
|
src: r"
|
|
#version 450
|
|
layout(location = 0) in vec3 v_color;
|
|
layout(location = 1) in vec3 v_normal;
|
|
layout(location = 2) in vec2 v_uv;
|
|
|
|
layout(location = 0) out vec4 f_color;
|
|
|
|
void main() {
|
|
// Rendu de la couleur directe sans ombrage pour le wireframe
|
|
f_color = vec4(v_color, 1.0);
|
|
}
|
|
",
|
|
}
|
|
}
|
|
|
|
/// Crée le pipeline graphique 3D avec Depth Testing, Culling et Push Constants.
|
|
pub fn create_graphics_pipeline(
|
|
device: Arc<Device>,
|
|
subpass: Subpass,
|
|
) -> Result<Arc<GraphicsPipeline>, Box<dyn std::error::Error>> {
|
|
let vs = vs::load(device.clone())?.entry_point("main").unwrap();
|
|
let fs = fs::load(device.clone())?.entry_point("main").unwrap();
|
|
|
|
let vertex_input_state = [CubeVertex::per_vertex()]
|
|
.definition(&vs)?;
|
|
|
|
let stages = [
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
PipelineShaderStageCreateInfo::new(fs),
|
|
];
|
|
|
|
let layout = PipelineLayout::new(
|
|
device.clone(),
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
.into_pipeline_layout_create_info(device.clone())?,
|
|
)?;
|
|
|
|
let pipeline = GraphicsPipeline::new(
|
|
device,
|
|
None,
|
|
GraphicsPipelineCreateInfo {
|
|
stages: stages.into_iter().collect(),
|
|
vertex_input_state: Some(vertex_input_state),
|
|
input_assembly_state: Some(InputAssemblyState::default()),
|
|
viewport_state: Some(ViewportState::default()),
|
|
rasterization_state: Some(RasterizationState {
|
|
cull_mode: CullMode::Back,
|
|
front_face: FrontFace::Clockwise,
|
|
..Default::default()
|
|
}),
|
|
depth_stencil_state: Some(DepthStencilState {
|
|
depth: Some(DepthState::simple()),
|
|
..Default::default()
|
|
}),
|
|
multisample_state: Some(MultisampleState::default()),
|
|
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
|
subpass.num_color_attachments(),
|
|
ColorBlendAttachmentState::default(),
|
|
)),
|
|
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
|
subpass: Some(subpass.into()),
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
},
|
|
)?;
|
|
|
|
Ok(pipeline)
|
|
}
|
|
|
|
/// Crée le pipeline graphique dédié au rendu des lignes du cadre de sélection (Wireframe).
|
|
pub fn create_line_pipeline(
|
|
device: Arc<Device>,
|
|
subpass: Subpass,
|
|
) -> Result<Arc<GraphicsPipeline>, Box<dyn std::error::Error>> {
|
|
let vs = vs::load(device.clone())?.entry_point("main").unwrap();
|
|
let fs = line_fs::load(device.clone())?.entry_point("main").unwrap();
|
|
|
|
let vertex_input_state = [CubeVertex::per_vertex()]
|
|
.definition(&vs)?;
|
|
|
|
let stages = [
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
PipelineShaderStageCreateInfo::new(fs),
|
|
];
|
|
|
|
let layout = PipelineLayout::new(
|
|
device.clone(),
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
.into_pipeline_layout_create_info(device.clone())?,
|
|
)?;
|
|
|
|
let pipeline = GraphicsPipeline::new(
|
|
device,
|
|
None,
|
|
GraphicsPipelineCreateInfo {
|
|
stages: stages.into_iter().collect(),
|
|
vertex_input_state: Some(vertex_input_state),
|
|
// Topologie LineList pour dessiner les arêtes du cube
|
|
input_assembly_state: Some(InputAssemblyState {
|
|
topology: PrimitiveTopology::LineList,
|
|
..Default::default()
|
|
}),
|
|
viewport_state: Some(ViewportState::default()),
|
|
rasterization_state: Some(RasterizationState {
|
|
cull_mode: CullMode::None,
|
|
..Default::default()
|
|
}),
|
|
// CompareOp::LessOrEqual sans écriture dans le depth buffer pour un contour net
|
|
depth_stencil_state: Some(DepthStencilState {
|
|
depth: Some(DepthState {
|
|
compare_op: CompareOp::LessOrEqual,
|
|
write_enable: false,
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
multisample_state: Some(MultisampleState::default()),
|
|
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
|
subpass.num_color_attachments(),
|
|
ColorBlendAttachmentState::default(),
|
|
)),
|
|
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
|
subpass: Some(subpass.into()),
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
},
|
|
)?;
|
|
|
|
Ok(pipeline)
|
|
}
|
|
|
|
pub mod ui_vs {
|
|
vulkano_shaders::shader! {
|
|
ty: "vertex",
|
|
src: r"
|
|
#version 450
|
|
layout(location = 0) in vec3 position;
|
|
layout(location = 1) in vec3 color;
|
|
layout(location = 2) in vec3 normal;
|
|
layout(location = 3) in vec2 uv;
|
|
|
|
layout(location = 0) out vec3 v_color;
|
|
|
|
void main() {
|
|
// Coordonnées 2D écran directes en NDC [-1..1]
|
|
gl_Position = vec4(position.xy, 0.0, 1.0);
|
|
v_color = color;
|
|
}
|
|
",
|
|
}
|
|
}
|
|
|
|
/// Crée le pipeline graphique dédié à l'interface 2D (Hotbar d'inventaire).
|
|
pub fn create_ui_pipeline(
|
|
device: Arc<Device>,
|
|
subpass: Subpass,
|
|
) -> Result<Arc<GraphicsPipeline>, Box<dyn std::error::Error>> {
|
|
let vs = ui_vs::load(device.clone())?.entry_point("main").unwrap();
|
|
let fs = line_fs::load(device.clone())?.entry_point("main").unwrap();
|
|
|
|
let vertex_input_state = [CubeVertex::per_vertex()].definition(&vs)?;
|
|
|
|
let stages = [
|
|
PipelineShaderStageCreateInfo::new(vs),
|
|
PipelineShaderStageCreateInfo::new(fs),
|
|
];
|
|
|
|
let layout = PipelineLayout::new(
|
|
device.clone(),
|
|
PipelineDescriptorSetLayoutCreateInfo::from_stages(&stages)
|
|
.into_pipeline_layout_create_info(device.clone())?,
|
|
)?;
|
|
|
|
let pipeline = GraphicsPipeline::new(
|
|
device,
|
|
None,
|
|
GraphicsPipelineCreateInfo {
|
|
stages: stages.into_iter().collect(),
|
|
vertex_input_state: Some(vertex_input_state),
|
|
input_assembly_state: Some(InputAssemblyState::default()),
|
|
viewport_state: Some(ViewportState::default()),
|
|
rasterization_state: Some(RasterizationState {
|
|
cull_mode: CullMode::None,
|
|
..Default::default()
|
|
}),
|
|
// Depth test désactivé : l'UI s'affiche toujours au premier plan
|
|
depth_stencil_state: Some(DepthStencilState {
|
|
depth: Some(DepthState {
|
|
compare_op: CompareOp::Always,
|
|
write_enable: false,
|
|
}),
|
|
..Default::default()
|
|
}),
|
|
multisample_state: Some(MultisampleState::default()),
|
|
color_blend_state: Some(ColorBlendState::with_attachment_states(
|
|
subpass.num_color_attachments(),
|
|
ColorBlendAttachmentState::default(),
|
|
)),
|
|
dynamic_state: [DynamicState::Viewport].into_iter().collect(),
|
|
subpass: Some(subpass.into()),
|
|
..GraphicsPipelineCreateInfo::layout(layout)
|
|
},
|
|
)?;
|
|
|
|
Ok(pipeline)
|
|
}
|